Tp smapi

From ThinkWiki
Revision as of 04:56, 15 August 2009 by Thinker (Talk | contribs) (Battery charge control features: Battery balancing script)
Jump to: navigation, search

The tp_smapi kernel module exposes some features of the ThinkPad hardware/firmware via a sysfs interface. Currently, the main implemented functionality is control of battery charging and extended battery status. The tp_smapi software package also includes an improved version of the HDAPS driver. The underlying hardware interfaces are SMAPI and direct access to the embedded controller.

This driver uses undocumented features and direct hardware access, so it may work unreliably or even damage your hardware; but so far no such damage has been reported.

Features

  • tp_smapi driver
    • Battery charge/discharge control
    • Battery status information
  • hdaps driver (compared with the standard hdaps driver)
    • Improved stability
    • Improved model support
    • Improved functionality

Project Homepage / Availability

Installation

Installation from source

You will need the kernel headers and makefiles corresponding to your current kernel version. On Fedora, this means # yum install kernel-devel-$(uname -r) .

# tar xzvf tp_smapi-0.40.tgz
# cd tp_smapi-0.40

Then, either compile and load the driver within the current working directory (for testing):

# make load

OR compile and install into the kernel's module path:

# make install

If you use the HDAPS driver, add HDAPS=1 to also patch the hdaps for compatibility with tp_smapi (this requires a kernel source tree matching the current kernel). Again, either load the driver within the current working directory:

# make load HDAPS=1

OR install into the kernel's module path:

# make install HDAPS=1

To prepare a stand-alone patch against the current kernel tree (including a patch against hdaps and new Kconfig entries):

# make patch

To delete all autogenerated files:

# make clean

The original kernel tree is never modified by any these commands. The /lib/modules directory is modified only by # make install.

Installation in Gentoo

The Gentoo portage system carries a tp_smapi package, which follows the latest version pretty closely. On a Gentoo system, you can install and load as follows.

If you use the HDAPS driver, do this first:

  • Disable the hdaps module in your kernel configuration (Device Drivers → Hardware Monitoring Support → IBM Hard Drive Active Protection System)
  • Rebuild and install the kernel
  • Add the hdaps use flag in /etc/make.conf
  • # rmmod hdaps

Then:

  • # emerge tp_smapi (or install tp_smapi with hdaps support manually, as above)
  • # echo "tp_smapi" >> /etc/modules.autoload.d/kernel-2.6
  • # echo "hdaps" >> /etc/modules.autoload.d/kernel-2.6

Then reboot, or run:

  • # modprobe tp_smapi
  • # modprobe hdaps

Installation on Debian unstable (sid), testing (squeeze) or stable (lenny)

Debian now contains the packaged module source and prebuilt modules for Debian kernels.

If you use a stock Debian kernel, you can install the tp_smapi modules with:

  • # apt-get install tp-smapi-modules-`uname -r`
  • in order to make sure the modules package is kept in sync with the kernel one, you may also want to install the tm-smapi-modules-2.6-686 or tm-smapi-modules-2.6-amd64 package, depending on your architecture

If you use a custom kernel, you can build tp_smapi with module-assistant:

  • # aptitude install tp-smapi-source
  • # m-a -t a-i tp-smapi

Installation on openSUSE

openSUSE provides rpm packages. Look for them at the package search: [1], search for the package named "tp_smapi".

A step-by-step guide on how a relative linux newbie installed tp_smapi on a Thinkpad W500 running openSuse 11.1 using YAST Software Manager is available here

In case you are taking the hard way around, while compiling on 11.0, make complained about not being able to locate the kernel sources. It worked for me after I did this:

cp /usr/include/linux/aio_abi.h /lib/modules/`uname -r`/build/include/linux/

Installation on Ubuntu

Ubuntu ships tp_smapi in their linux-ubuntu-modules-* package for Hardy, so you don't have to build it yourself. Please note that they have renamed tp_smapi's modified hdaps module to hdaps_ec so that the two can coexist. You should load hdaps_ec, not hdaps.

Ubuntu Intrepid and Jaunty, however, lack the hdaps_ec module, as reported here. It will not be included again until said bundled hdaps driver is merged into mainline.

Installation on Ubuntu Jaunty

I have posted my experiences here

Installation on older Ubuntu/Debian

Installation on Ubuntu or Debian is quite easy, but there are a few things to look after:

To get your system ready for compiling code, install the build-essentials (as root, of course, as all of the following comands; Ubuntu users have to prepend 'sudo' to every line and enter their own password when prompted):

apt-get install build-essential

To get tp_smapi to work, obtain the latest source as mentioned above and unpack it. If you want to use HDAPS, you need to install the kernel source matching te kernel you are running. To do so, issue this:

uname -r

This will give you the version of your current kernel. Debian users execute the following:

apt-get install linux-source-`uname -r`

Ubuntu users execute the following using the kernel-version only (exclude -XX-generic; e.g. 'linux-source-2.6.20'):

apt-get install linux-source-`uname -r`

Be sure to unpack the source file. The easiest way is to open Nautilus as root:

sudo nautilus

Then browse to /usr/src/ and extract the source file to that directory.

Now change to the tp_smapi dir:

cd tp_smapi-X.YY

(X.YY being the version-number of tp_smapi)

and make and install tp_smapi as instructed above.

If you get an error that the kernel version isn't matching (or that you need to set KSRC/KBUILD), please check that there is a symlink from the modules dir to the kernel source:

root@localhost:~#ls -l /lib/modules/2.6.20-16-generic
lrwxrwxrwx  1 root root     28 2007-02-02 08:39 build -> /usr/src/linux-source-2.6.20

Create the link if the line above is not existent:

root@localhost:~#ln -s /usr/src/linux-source-2.6.20 /lib/modules/2.6.20-16-generic/build

Now the following will build and install the correct modules to their locations:

make install HDAPS=1

To make sure your system loads the modules at boot time, do this:

echo "tp_smapi" >> /etc/modules
echo "hdaps" >> /etc/modules

and update your initramfs:

update-initramfs -u

To get tp_smapi running now, just load the modules:

modprobe -a tp_smapi hdaps

This description was tested on Kubuntu 'Feisty Fawn' and should work on all Debian-based distros with minor tweaks.

EDIT: Works on Kubuntu 7.10 ("Gutsy Gibbon"), too

Battery charge control features

To set the thresholds for starting and stopping battery charging (in percent of current full charge capacity):

# echo 40 > /sys/devices/platform/smapi/BAT0/start_charge_thresh
# echo 70 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh
# cat /sys/devices/platform/smapi/BAT0/*_charge_thresh
Hint:
Battery charging thresholds can be used to keep Li-Ion ad Li-Polymer batteries partially charged, in order to increase their lifetime.

To prevent charging for 17 minutes (regardless of thresholds):

# echo 17 > /sys/devices/platform/smapi/BAT0/inhibit_charge_minutes
Hint:
Charge inhibiting can be used to reduce the power draw of the laptop, in order to use an under-spec power supply that can't handle the combined power draw of running and charging. It can also be used to control which battery is charged when using an Ultrabay battery.

To cancel charging preventation:

# echo 0 > /sys/devices/platform/smapi/BAT0/inhibit_charge_minutes

To force battery discharging (even if connected to AC):

# echo 1 > /sys/devices/platform/smapi/BAT0/force_discharge
Hint:
This can be used to choose which battery is discharged when using an UltraBay battery. For example, see the tp-bat-balance (download) script.

To cancel forced discharge:

# echo 0 > /sys/devices/platform/smapi/BAT0/force_discharge

Battery status features

To view extended battery status such as charging state, voltage, current, capacity, cycle count and model information:

# cat /sys/devices/platform/smapi/BAT0/installed
# cat /sys/devices/platform/smapi/BAT0/state       # idle/charging/discharging
# cat /sys/devices/platform/smapi/BAT0/cycle_count
# cat /sys/devices/platform/smapi/BAT0/current_now # instantaneous current
# cat /sys/devices/platform/smapi/BAT0/current_avg # last minute average
# cat /sys/devices/platform/smapi/BAT0/power_now   # instantaneous power
# cat /sys/devices/platform/smapi/BAT0/power_avg   # last minute average
# cat /sys/devices/platform/smapi/BAT0/last_full_capacity
# cat /sys/devices/platform/smapi/BAT0/remaining_percent
# cat /sys/devices/platform/smapi/BAT0/remaining_running_time
# cat /sys/devices/platform/smapi/BAT0/remaining_charging_time
# cat /sys/devices/platform/smapi/BAT0/remaining_capacity
# cat /sys/devices/platform/smapi/BAT0/design_capacity
# cat /sys/devices/platform/smapi/BAT0/voltage
# cat /sys/devices/platform/smapi/BAT0/design_voltage
# cat /sys/devices/platform/smapi/BAT0/manufacturer
# cat /sys/devices/platform/smapi/BAT0/model
# cat /sys/devices/platform/smapi/BAT0/barcoding
# cat /sys/devices/platform/smapi/BAT0/chemistry
# cat /sys/devices/platform/smapi/BAT0/serial
# cat /sys/devices/platform/smapi/BAT0/manufacture_date
# cat /sys/devices/platform/smapi/BAT0/first_use_date
# cat /sys/devices/platform/smapi/BAT0/temperature # in milli-Celsius
# cat /sys/devices/platform/smapi/ac_connected

The raw status data is also available, including some fields not listed above (in case you can figure them out):

# cat /sys/devices/platform/smapi/BAT0/dump

In all of the above, replace BAT0 with BAT1 to address the 2nd battery.

Note that the battery status readout conflicts with the stock hdaps driver, so if you use hdaps you will need to load tp_smapi using # make load HDAPS=1 (see Bundled hdaps driver below).

On ACPI-enabled systems, most of above information is also available through the files under /proc/acpi/battery. However, the ACPI interface does not include the instantaneous power and cycle count readouts, and does not work well when hotswapping UltraBay batteries.

Scripts

Some scripts can make it easier to grep the information you need from tp_smapi

Other features

There is also sysfs attribute for making direct SMAPI requests to the SM BIOS firmware. Don't touch it unless you really know what you're doing. Example:

# echo '211a 100 0 0' > /sys/devices/platform/smapi/smapi_request; cat /sys/devices/platform/smapi/smapi_request
211a 34b b2 0 0 0 'OK'

The "4b" in the 2nd value, converted to decimal, is 75: the current charging stop threshold (stop_charge_thresh).

Bundled hdaps driver

The tp_smapi package includes a modified version of the hdaps Linux kernel driver used by the HDAPS system. It has remained outside the mainline kernel for a few years for several reasons, [2] including the upstream maintainer's assertion that the new code's anonymous author was under a NDA. (Although this would not normally expose the kernel team to litigation.) [3] [4] [5]

To use tp_smapi and hdaps concurrently, you must use the modified version.

To build the modified version, simply append the HDAPS=1 parameter to the make command (see Installation above):

# make load HDAPS=1

or

# make install HDAPS=1

If you don't do that, you will not be able to load tp_smapi (and its support module thinkpad_ec) when hdaps is loaded, and vice versa. You can use rmmod to switch between these modules.

Note that some of the battery status is also visible through ACPI (/proc/acpi/battery/*), independently of tp_smapi.

The modified hdaps has several changes:

  • The hdaps driver in mainline kernels conflicts with the extended battery status (they use the same IO ports). The modified hdaps coordinates this access through the bundled thinkpad_ec driver.
  • The modified hdaps driver fixes reliability and improves support for recent ThinkPad models (*60 and newer), since unlike the mainline driver, it correctly follows the Embedded Controller communication protocol.
  • Several other improvements, such as the ability to control the polling rate.

Troubleshooting

thinkpad_ec: cannot claim io ports 0x1600-0x161f

The "thinkpad_ec: cannot claim io ports 0x1600-0x161f!" error message is printed when loading the thinkpad_ec module on some recent ThinkPad models. This occurs because the ACPI BIOS DSDT is reserving ports used by tp_smapi.

As a workaround, add the force_io=1 module parameter to thinkpad_ec in /etc/modprobe.conf (or your distribution's equivalent). For transient testing, load the modules as follows:

# make load HDAPS=1 FORCE_IO=1

Note: force_io option was added in tp_smapi v0.40 (released 2008-12-16).

Models which need this option include:

More information:

thinkpad_ec: no ThinkPad embedded controller!

If you get "thinkpad_ec: no ThinkPad embedded controller!" when trying to load the module on a supported model listed below, you should upgrade your BIOS. Some early BIOS (like 1.x on the X31) don't handle the embedded controller.

Model-specific status

tp_smapi feature support matrix
A series
Model and machine type start_
charge_
thresh
stop_
charge_
thresh
inhbit_
charge_
minutes
force_
discharge
battery status files Bundled HDAPS HDAPS axis orientation1 tp_smapi version BIOS version EC
firmware
version
Notes
A22p 2629-USG no no no no no N/A N/A
A30 no no no no yes N/A N/A
G series
Model and machine type start_
charge_
thresh
stop_
charge_
thresh
inhbit_
charge_
minutes
force_
discharge
battery status files Bundled HDAPS HDAPS axis orientation1 tp_smapi version BIOS version EC
firmware
version
Notes
G41 yes no yes unknown unknown N/A N/A
R series
Model and machine type start_
charge_
thresh
stop_
charge_
thresh
inhbit_
charge_
minutes
force_
discharge
battery status files Bundled HDAPS HDAPS axis orientation1 tp_smapi version BIOS version EC
firmware
version
Notes
R31 no no no no no N/A N/A No SMAPI BIOS
R40 no no no no unknown N/A N/A
R50 unknown no unknown unknown yes unknown unknown
R50e 1834-JAG yes no yes yes yes N/A N/A
R50e 1834-5US yes no yes yes yes N/A N/A
R50p no no no no yes unknown unknown
R51 18* yes no yes yes yes yes 0 0.32 3.22 Machine types 1829, 1830, 1831 and 1836
R51 28* yes no yes unknown yes unknown unknown 1.29 Machine types 2883, 2887, 2888, 2889, 2894 and 2495
R52 yes yes yes yes yes yes 0 0.33 1.29 1.06
R60 yes yes yes yes yes yes 1 0.33
R61 yes yes unknown unknown yes yes 1 0.35-test1 7KETA7WW 2.07 7KHT24WW 1.08 Machine type 8918
R61i yes yes unknown unknown yes unknown unknown Machine type 8250
R400 yes yes yes yes yes yes 1 0.40-1 Machine type 7443
T series
Model and machine type start_
charge_
thresh
stop_
charge_
thresh
inhbit_
charge_
minutes
force_
discharge
battery status files Bundled HDAPS HDAPS axis orientation1 tp_smapi version BIOS version EC
firmware
version
Notes
T20 no no no no no N/A N/A Has SMAPI BIOS but no function is supported. EC LPC3 protocol fails.
T22 no no no no no N/A N/A Has SMAPI BIOS but no function is supported. EC LPC3 protocol fails.
T23 no no no no yes N/A N/A
T30 no no no no yes N/A N/A
T40 no no no no yes N/A N/A
T40p no no no no yes N/A N/A
T41 no no no no yes unknown unknown 0.37 3.23 3.04
T41p 2373-AM9 no no no no yes yes 1 0.32 3.20 v3.04
T42 yes no yes yes yes unknown unknown
T42p 2373-KXU yes no yes yes yes yes 1 0.32 3.21 Stop charge threshold works in Windows, but dmesg says "__get_real_thresh: cannot get stop_thresh of bat=0: Function is not supported by SMAPI BIOS".
T42p 2373-KUU yes no yes unknown yes unknown unknown
T43 2686 yes yes yes yes yes yes 0 0.33 1.27
T43p yes yes yes yes yes unknown 0
T60 yes yes unknown yes yes yes 7 0.32
T60 yes yes unknown yes yes yes 1 0.34
T60 1951-24G yes yes unknown yes yes yes unknown 0.36 1.09a
T60 2007-FSG yes yes yes yes yes unknown unknown 0.39 1.10 1.05b
T60 1951-CZ1 yes yes yes yes yes yes 1 0.40 2.23 1.07
T60p 8743-CTO yes yes yes yes yes yes 1 0.32
T61 yes yes yes yes yes yes 1 0.32 ref
T61p 6457-7XG yes yes yes yes yes yes 1 0.32 ref
T400 yes yes yes yes yes yes 1 0.40 1.19 Requires force_io=1 parameter to thinkpad_ec
T400s yes yes yes yes yes yes unknown 0.40 1.06
T500 yes yes yes yes yes yes yes 1 0.40 2.07 Requires force_io=1 parameter to thinkpad_ec
X series
Model and machine type start_
charge_
thresh
stop_
charge_
thresh
inhbit_
charge_
minutes
force_
discharge
battery status files Bundled HDAPS HDAPS axis orientation1 tp_smapi version BIOS version EC
firmware
version
Notes
X20 2662-31G no no no no no N/A N/A
X22 no no no no no N/A N/A no EC controller found
X24 no no no unknown yes N/A N/A
X30 no no no no yes N/A N/A 0.34 1.09
X31 no no no unknown yes N/A N/A
X32 no no no no yes N/A N/A
X40 yes yes yes yes yes yes 3 0.32 2.08 1.62
X41 yes yes yes yes yes yes 3 0.32
X41 Tablet yes yes yes yes yes yes (see ref) 0.32 ref
X60 yes yes yes yes yes yes 6 2.07 1.10 2.6.20 issue (see discussion)
X60 Tablet yes yes yes yes yes yes 3 0.34 7JET25WW (1.10 ) 7JHT13WW 1.04 ref ref
X60s 1704-5UG yes yes unknown unknown unknown unknown 3 0.34 7BETD2WW 2.13 7BHT40WW 1.13
X61 7673-CTO yes yes yes yes yes unknown unknown 0.37 7NETB9WW (2.19) unknown
X61 7675-CTO unknown unknown unknown unknown unknown yes 6 0.35-test1
X61 7675-4KU yes yes yes yes yes yes 6 0.34 7NET30WW (1.11 ) 7MHT24WW-1.02
X61 Tablet yes yes unknown unknown yes yes 6 0.32
X61s yes yes yes yes yes yes 6 / 3 0.32 ref says 3 but conflicting report received
X200 yes yes unknown unknown yes yes 7 0.40 6DET40WW (2.04 ) 7XHT22WW-1.04
X300 yes yes yes unknown yes yes unknown 0.37 7TET25WW (1.02 ) 7THT15WW-1.00c
Z series
Model and machine type start_
charge_
thresh
stop_
charge_
thresh
inhbit_
charge_
minutes
force_
discharge
battery status files Bundled HDAPS HDAPS axis orientation1 tp_smapi version BIOS version EC
firmware
version
Notes
Z60m yes yes yes yes yes unknown unknown
Z60t yes yes unknown unknown yes unknown unknown
Z61m yes yes yes yes yes yes 0 0.32
Z61t yes yes yes yes yes yes unknown ref
Z61p yes yes unknown yes yes yes unknown
SL series and IdeaPad

The ThinkPad SL series and IdeaPad series have firmware that is completely different from other ThinkPad models. Neither tp_smapi nor thinkpad-acpi support these models.


SMAPI capabilities may depend on the BIOS version as well, so upgrading to the latest version of the BIOS might provide more SMAPI functions (especially true for long-lived BIOS with lots of releases, like the TP-1R).

Please update the above and report your experience on the discussion page. If the module loads but gives a "not supported" or "not implementeded" error when you try to use some specific file in /sys/devices/platform/smapi/, please report the dmesg output and whether the corresponding functionality is available under Windows - maybe your ThinkPad just can't do that.

While at it, you may also want to add your laptop to the list of DMI IDs.

HDAPS axis orientation

1 The HDAPS axis orientation is set using the invert module parameter (at load time) or sysfs attribute (at runtime). The hdaps driver of tp_smapi supports all 8 possible sensor orientations (inversion and swapping). You can use hdaps visualisation to check if the settings is correct.

The invert parameter is an integer between 0 and 7, whose meaning is defined below. (This is confusing. Most people will find it easier to just try all 8 possibilities.)

Let X and Y denote the hardware readouts. Let R denote the laptop's roll (tilt left/right), and let P denote the laptop's pitch (tilt forward/backward). The possible values are as follows:

   invert=0:   R= X  P= Y   (same as mainline)
   invert=1:   R=-X  P=-Y   (same as mainline)
   invert=2:   R=-X  P= Y   (new)
   invert=3:   R= X  P=-Y   (new)
   invert=4:   R= Y  P= X   (new)
   invert=5:   R=-Y  P=-X   (new)
   invert=6:   R=-Y  P= X   (new)
   invert=7:   R= Y  P=-X   (new)

Tools using this driver

The driver's interface can be accessed directly through the files under /sys/devices/platform/smapi, or via the following tools:

Headline text