ds2760 battery driver
RFC PATCH 6/7 ds2760 battery driver
This is driver for batteries with ds2760 chip inside. Such batteries used in almost every HP iPaq and HTC PDAs/phones.
--- drivers/battery/Kconfig | 7 + drivers/battery/Makefile | 1 + drivers/battery/ds2760_battery.c | 466 ++++++++++++++++++++++++++++++++++++++ include/linux/ds2760_battery.h | 32 +++ 4 files changed, 506 insertions(+), 0 deletions(-) create mode 100644 drivers/battery/ds2760_battery.c create mode 100644 include/linux/ds2760_battery.h
drivers/battery/Kconfig +7 -0
@@ -8,4 +8,11 @@ config BATTERY Say Y here to enable generic battery status reporting in the /sys filesystem. +config BATTERY_DS2760 + tristate "DS2760 battery driver (HP iPAQ & others)" + depends on BATTERY && W1 + select W1_SLAVE_DS2760 + help + Say Y here to enable support for batteries with ds2760 chip. + endmenu
drivers/battery/Makefile +1 -0
@@ -1 +1,2 @@ obj-$(CONFIG_BATTERY) += battery.o +obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
drivers/battery/ds2760_battery.c new file +466 -0
466 lines not on this page · raw message
include/linux/ds2760_battery.h new file +32 -0
@@ -0,0 +1,32 @@ +/* +* Driver for batteries with DS2760 chips inside. +* +* Copyright (c) 2007 Anton Vorontsov +* 2004 Matt Reimer +* 2004 Szabolcs Gyurko +* +* Use consistent with the GNU GPL is permitted, +* provided that this copyright notice is +* preserved in its entirety in all copies and derived works. +* +* Author: Anton Vorontsov <[email protected]> +* February 2007 +* +* Matt Reimer <[email protected]> +* April 2004, 2005 +* +* Szabolcs Gyurko <[email protected]> +* September 2004 +*/ + +#ifndef __DS2760_BATTERY__ +#define __DS2760_BATTERY__ + +#include <linux/battery.h> + +struct ds2760_platform_data { +/* Battery information and characteristics. */ +struct battery_info battery_info; +}; + +#endif
Signature · 1 line
1.5.0.5-dirty
Quoted 2 lines · Anton Vorontsov
This is driver for batteries with ds2760 chip inside. Such batteries used in almost every HP iPaq and HTC PDAs/phones.
Changes:
- follow battery class changes (get rid of vast amount of macro-created functions).
- cleanups based on comments by Randy Dunlap.
Subject: [PATCH] [take2] ds2760 battery driver
--- drivers/battery/Kconfig | 7 + drivers/battery/Makefile | 1 + drivers/battery/ds2760_battery.c | 493 ++++++++++++++++++++++++++++++++++++++ include/linux/ds2760_battery.h | 32 +++ 4 files changed, 533 insertions(+), 0 deletions(-) create mode 100644 drivers/battery/ds2760_battery.c create mode 100644 include/linux/ds2760_battery.h
drivers/battery/Kconfig +7 -0
@@ -8,4 +8,11 @@ config BATTERY Say Y here to enable generic battery status reporting in the /sys filesystem. +config BATTERY_DS2760 + tristate "DS2760 battery driver (HP iPAQ & others)" + depends on BATTERY && W1 + select W1_SLAVE_DS2760 + help + Say Y here to enable support for batteries with ds2760 chip. + endmenu
drivers/battery/Makefile +1 -0
@@ -1 +1,2 @@ obj-$(CONFIG_BATTERY) += battery.o +obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
drivers/battery/ds2760_battery.c new file +493 -0
493 lines not on this page · raw message
include/linux/ds2760_battery.h new file +32 -0
@@ -0,0 +1,32 @@ +/* +* Driver for batteries with DS2760 chips inside. +* +* Copyright (c) 2007 Anton Vorontsov +* 2004 Matt Reimer +* 2004 Szabolcs Gyurko +* +* Use consistent with the GNU GPL is permitted, +* provided that this copyright notice is +* preserved in its entirety in all copies and derived works. +* +* Author: Anton Vorontsov <[email protected]> +* February 2007 +* +* Matt Reimer <[email protected]> +* April 2004, 2005 +* +* Szabolcs Gyurko <[email protected]> +* September 2004 +*/ + +#ifndef __DS2760_BATTERY__ +#define __DS2760_BATTERY__ + +#include <linux/battery.h> + +struct ds2760_platform_data { +/* Battery information and characteristics. */ +struct battery_info battery_info; +}; + +#endif
Signature · 1 line
1.5.0.5-dirty
Hi!
Quoted 2 lines
This is driver for batteries with ds2760 chip inside. Such batteries used in almost every HP iPaq and HTC PDAs/phones.
Looks nice to me. It even knows about dependency between temperature and remaining power :-).
drivers/battery/ds2760_battery.c line 136
Quoted 8 lines
drivers/battery/ds2760_battery.c new file +8
-0+ di->update_time = jiffies; + +/* DS2760 reports voltage in units of 4.88mV, but the battery class +* reports in units of mV, so convert by multiplying by 4.875. +* We approximate because integer math is cheap, and close enough. */ + di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB ] <<3 ) | + (di->raw[DS2760_VOLTAGE_LSB ] >>5 ); + di->voltage_mV = (di->voltage_raw *5 ) - (di->voltage_raw /8 );
Hmm, not sure if such tricks re really worth it... should not compiler be doing this?
drivers/battery/ds2760_battery.c line 182
Quoted 6 lines
drivers/battery/ds2760_battery.c new file +6
-0+/* Calculate the empty level at the present temperature. */ + scale[4 ] = di->raw[DS2760_ACTIVE_EMPTY +4 ]; +for (i =3 ; i >=0 ; i--) + scale[i] = scale[i +1 ] + di->raw[DS2760_ACTIVE_EMPTY + i]; + + di->empty_mAh =battery_interpolate (scale, di->temp_C /10 );
Wow.
drivers/battery/ds2760_battery.c line 446
Quoted 10 lines
drivers/battery/ds2760_battery.c new file +10
-0+static struct platform_driver ds2760_battery_driver = { + .driver = { + .name ="ds2760-battery" , + }, + .probe = ds2760_battery_probe, + .remove = ds2760_battery_remove, + #ifdef CONFIG_PM + .suspend = ds2760_battery_suspend, + .resume = ds2760_battery_resume, + #endif
I thought ifdefs need to be at column 0?
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Quoted 11 lines · Pavel Machek
drivers/battery/ds2760_battery.c line 136
Quoted 8 lines
drivers/battery/ds2760_battery.c new file +8
-0+ di->update_time = jiffies; + +/* DS2760 reports voltage in units of 4.88mV, but the battery class +* reports in units of mV, so convert by multiplying by 4.875. +* We approximate because integer math is cheap, and close enough. */ + di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB ] <<3 ) | + (di->raw[DS2760_VOLTAGE_LSB ] >>5 ); + di->voltage_mV = (di->voltage_raw *5 ) - (di->voltage_raw /8 );Hmm, not sure if such tricks re really worth it... should not compiler be doing this?
The shifts (<< 3 and >> 5) are just to get the bits reassembled in the right positions. The multiplication by 5 and subtracting 1/8 is because (AFAIK) we can't do floating point multiplication in the kernel. I'm open to suggestions.
Quoted 8 lines
drivers/battery/ds2760_battery.c line 182
Quoted 6 lines
drivers/battery/ds2760_battery.c new file +6
-0+/* Calculate the empty level at the present temperature. */ + scale[4 ] = di->raw[DS2760_ACTIVE_EMPTY +4 ]; +for (i =3 ; i >=0 ; i--) + scale[i] = scale[i +1 ] + di->raw[DS2760_ACTIVE_EMPTY + i]; + + di->empty_mAh =battery_interpolate (scale, di->temp_C /10 );Wow.
Yeah, mea culpa. I don't like its obtuseness either, and haven't revisited it since I got it working. Basically the battery has an array stored in its EEPROM that represents the empty level at various temperature thresholds. What makes this complicated is that the array does not contain all absolute values; just the first is absolute, and the others are relative to it, in reverse order. The above code converts it to an array of absolute values from low to high. I'm open to suggestions about this one too.
Matt
Quoted 11 lines · Matt Reimer
Quoted 10 lines · Pavel Machek
drivers/battery/ds2760_battery.c
Quoted 5 lines
drivers/battery/ds2760_battery.c new file +5
-0+ di->update_time = jiffies; + +/* DS2760 reports voltage in units of 4.88mV, but the battery class +* reports in units of mV, so convert by multiplying by 4.875. +* We approximate because integer math is cheap, and close enough. */
Quoted 3 lines
drivers/battery/ds2760_battery.c new file +3
-0+ di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB ] <<3 ) | + (di->raw[DS2760_VOLTAGE_LSB ] >>5 ); + di->voltage_mV = (di->voltage_raw *5 ) - (di->voltage_raw /8 );
Quoted 7 lines
Quoted 2 lines
Hmm, not sure if such tricks re really worth it... should not compiler be doing this?
The shifts (<< 3 and >> 5) are just to get the bits reassembled in the right positions. The multiplication by 5 and subtracting 1/8 is because (AFAIK) we can't do floating point multiplication in the kernel. I'm open to suggestions.
Because we are in micro world now, divisions already replaced by multiplication. I.e.
/* DS2760 reports voltage in units of 4.88mV, but the battery class
* reports in units of uV, so convert by multiplying by 4880. */
di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
(di->raw[DS2760_VOLTAGE_LSB] >> 5);
di->voltage_uV = di->voltage_raw * 4880;As a side effect, now we're not losing any precision. :-)
Quoted 9 lines
Quoted 8 lines
drivers/battery/ds2760_battery.c line 182
Quoted 6 lines
drivers/battery/ds2760_battery.c new file +6
-0+/* Calculate the empty level at the present temperature. */ + scale[4 ] = di->raw[DS2760_ACTIVE_EMPTY +4 ]; +for (i =3 ; i >=0 ; i--) + scale[i] = scale[i +1 ] + di->raw[DS2760_ACTIVE_EMPTY + i]; + + di->empty_mAh =battery_interpolate (scale, di->temp_C /10 );Wow.
Quoted 8 lines
Yeah, mea culpa. I don't like its obtuseness either, and haven't revisited it since I got it working. Basically the battery has an array stored in its EEPROM that represents the empty level at various temperature thresholds. What makes this complicated is that the array does not contain all absolute values; just the first is absolute, and the others are relative to it, in reverse order. The above code converts it to an array of absolute values from low to high. I'm open to suggestions about this one too.
By the way. Matt, you're more familiar with ds2760 specs, could you enlighten me about "* 4" in this snippet?
Quoted 1 line
acr[0] = (di->full_active_mAh * 4) >> 8;
^^^
Quoted 1 line
acr[1] = (di->full_active_mAh * 4) & 0xff;
^^^
Quoted 6 lines
if (w1_ds2760_write(di->w1_dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2) printk(KERN_ERR "ACR reset failed\n");Matt
Thanks!
Signature · 4 lines
Anton Vorontsov email: [email protected] backup email: [email protected] irc://irc.freenode.org/bd2
Quoted 9 lines · Anton Vorontsov
Quoted 4 lines · Matt Reimer
The shifts (<< 3 and >> 5) are just to get the bits reassembled in the right positions. The multiplication by 5 and subtracting 1/8 is because (AFAIK) we can't do floating point multiplication in the kernel. I'm open to suggestions.
Because we are in micro world now, divisions already replaced by multiplication. I.e.
Quoted 7 lines
/* DS2760 reports voltage in units of 4.88mV, but the battery class * reports in units of uV, so convert by multiplying by 4880. */ di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) | (di->raw[DS2760_VOLTAGE_LSB] >> 5); di->voltage_uV = di->voltage_raw * 4880;As a side effect, now we're not losing any precision. :-)
That's a good way to solve the problem. :-)
Quoted 10 lines
By the way. Matt, you're more familiar with ds2760 specs, could you enlighten me about "* 4" in this snippet?
Quoted 1 line
acr[0] = (di->full_active_mAh * 4) >> 8;^^^Quoted 1 line
acr[1] = (di->full_active_mAh * 4) & 0xff;^^^Quoted 3 lines
if (w1_ds2760_write(di->w1_dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2) printk(KERN_ERR "ACR reset failed\n");
The accumulated current register (acr) value is in units of 0.25 mAh, so we have to multiply by 4 to convert from units of 1 mAh to 0.25 mAh.
Thanks for all your work on this Anton.
Matt
Hi!
Quoted 11 lines
Quoted 8 lines
drivers/battery/ds2760_battery.c line 182
Quoted 6 lines
drivers/battery/ds2760_battery.c new file +6
-0+/* Calculate the empty level at the present temperature. */ + scale[4 ] = di->raw[DS2760_ACTIVE_EMPTY +4 ]; +for (i =3 ; i >=0 ; i--) + scale[i] = scale[i +1 ] + di->raw[DS2760_ACTIVE_EMPTY + i]; + + di->empty_mAh =battery_interpolate (scale, di->temp_C /10 );Wow.
Yeah, mea culpa. I don't like its obtuseness either, and haven't revisited it since I got it working. Basically the battery has an
No, I actually like the fact that someone cared enough about temperature's influence on li-ion.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html