In this article I will explain what battery charging thresholds are, and if you’re using a Thinkpad, how to install the tp_smapi kernel module (kmod), have it run at boot time, and automatically rebuild itself when you update kernel versions. This article is a bit long, but I’ve tried to explain why things are useful.
I recently upgraded from Fedora 13, to the new Fedora 14 release. Despite keeping my old "/home" directory, there was a lot of reinstalling and tweaking that I had to go through. With my laptop I make use of battery charging thresholds, which are good for preserving the longevity of your battery life, and stop it being prematurely depleted. The way battery charging thresholds work, are that they inhibit charging when the AC adaptor is connected, until the battery level is above and below a certain percentage. I tend to use the levels, 20% and 80%, so that the battery doesn’t begin charging until the battery level is below 20%, and when this occurs it stops charging at 80% of the total battery capacity; as I have mentioned, this prevents unnecessary wear to the battery. It is especially useful if your laptop is generally always connected to the AC adaptor, apart from the odd rare occasion. Because then you can set the battery to stay at a safe, 40% or so, which would keep it from being worn down, despite being installed in the laptop, with the AC adaptor attached.
Battery charging thresholds are a rather advanced feature, and it’s not in-built into the Linux kernel, so far as I know. There is however a kernel module for Thinkpads called tp_smapi. This module provides an interface for advanced features, such as battery charging thresholds, among other things. Now I will move on to explaining how to install the tp_smapi kernel module.
You can build tp_smapi from source, in which case here’s the link to some more detailed instructions and steps to follow on ThinkWiki. However, I find that this still does require a modecombe of concious effort on my part to keep tp_smapi up to date and working with my current kernel version (which gets updated a lot more frequently than tp_smapi itself.) So, the solution is to use a prepackaged kernel module, that can automatically rebuild itself. This is called an akmod or automatic kernel module. Thankfully, somebody smarter than me has already built just such a package for use with Fedora. You can download it from here, you’re looking for the package titled akmod-tp_smapi-0.40-1.fc11.i686.rpm.
So I’m presuming that you’ve downloaded it to your “/home/YourUsernameHere/Downloads” directory, in which case we need to open a terminal and switch to that directory:
cd /Downloads
The next step is to install the akmod. Probably the best way to do this is a local install with YUM. You need to be root in order to install this program, so use “sudo” if you’ve set it up, if not you can use “su -c” which I have used below (so you can quickly copy and paste it):
su -c 'yum localinstall akmod-tp_smapi-0.40-1.fc11.i686.rpm --nogpgcheck'
Now read the on screen instructions to confirm that it has installed okay.
Before tp_smapi will work you need to reboot your computer. Instead of watching the pretty Fedora startup screen (the silhouette of the Fedora logo slowly filling up with white), press the Escape key on your keyboard, to show the log of what’s happening on boot. You’ll see an entry for tp_smapi, being built. If it’s successful you’ll see a green “[Ok]” next to the entry. If not, you’ll also be warned at boot time, with an orange caution-triangle, showing boot time errors, before and after you log in.
So now, hopefully it has built successfully and should work. However, you have to manually load the new kernel module. To do this, again, become root user and execute the following command in a terminal:
su -c 'modprobe tp_smapi'
To verify this has worked I then (still as root user, and you have to explicitly type “su” and the root password) execute a command to show me the battery charging thresholds, as follows:
cat /sys/devices/platform/smapi/BAT0/*_charge_thresh
This should display two numbers, on above the other, in the terminal. The first, and top one is the thresholds to start charging at, the one below it, the last one, is the level at which the battery stops charging. It may be a good idea, to note down the above command, as it’s quite useful if you want to remember how to use these battery charging thresholds.
The final part of setting up the akmod_tp-smapi is to make sure that it is loaded _automatically_ for you, at boot time, so you don’t have to execute “modprobe tp_smapi” yourself, manually, every time you reboot. To do this, you need to be root, and execute the following command:
echo modprobe tp_smapi > /etc/rc.d/rc.local
Now if you reboot your laptop and then try the command to show you the battery charging thresholds, as root (cat /sys/devices/platform/smapi/BAT0/*_charge_thresh), it should just work for you.
A final point to note are the two commands below (you, again, must switch to root with “su“):
echo 20 > /sys/devices/platform/smapi/BAT0/start_charge_thresh
echo 80 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh
As I have mentioned before, these are the two battery charging thresholds that I use. The top one, as the end of the command suggests, is the start threshold, and the second is the stop charging threshold. Execute these commands, with your own numbers after “echo“, and it’ll set those values for you. If you put the two commands on to one line, and separate them with a semi-colon, like so:
echo 20 > /sys/devices/platform/smapi/BAT0/start_charge_thresh; echo 80 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh
Then it will execute the two commands at the same time, to avoid having to copy and paste twice. Remember, use the following command to check the charging levels:
cat /sys/devices/platform/smapi/BAT0/*_charge_thresh