aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorAnton Vorontsov <cbou@mail.ru>2007-05-03 16:45:39 -0400
committerDavid Woodhouse <dwmw2@infradead.org>2007-07-10 06:26:18 -0400
commitfe0e3153acfef4864b69932cf116eb5f38f7500c (patch)
tree49875913f9166c5aae2b92192ca52f334ada0523 /drivers
parent3788ec932bfd4942831e9a5034191509a2e11924 (diff)
[BATTERY] 1-Wire ds2760 chip battery driver
This is driver for batteries with ds2760 chip inside, found inside almost every HP iPaq and HTC PDAs/phones. Signed-off-by: Anton Vorontsov <cbou@mail.ru> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: David Woodhouse <dwmw2@infradead.org>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/power/Kconfig7
-rw-r--r--drivers/power/Makefile2
-rw-r--r--drivers/power/ds2760_battery.c470
3 files changed, 479 insertions, 0 deletions
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 791fa0cc45fe..921456243e4b 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -28,4 +28,11 @@ config APM_POWER
28 Say Y here to enable support APM status emulation using 28 Say Y here to enable support APM status emulation using
29 battery class devices. 29 battery class devices.
30 30
31config BATTERY_DS2760
32 tristate "DS2760 battery driver (HP iPAQ & others)"
33 select W1
34 select W1_SLAVE_DS2760
35 help
36 Say Y here to enable support for batteries with ds2760 chip.
37
31endif # POWER_SUPPLY 38endif # POWER_SUPPLY
diff --git a/drivers/power/Makefile b/drivers/power/Makefile
index 7779cebcb3ae..a978ba15f6c5 100644
--- a/drivers/power/Makefile
+++ b/drivers/power/Makefile
@@ -16,3 +16,5 @@ obj-$(CONFIG_POWER_SUPPLY) += power_supply.o
16 16
17obj-$(CONFIG_PDA_POWER) += pda_power.o 17obj-$(CONFIG_PDA_POWER) += pda_power.o
18obj-$(CONFIG_APM_POWER) += apm_power.o 18obj-$(CONFIG_APM_POWER) += apm_power.o
19
20obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
diff --git a/drivers/power/ds2760_battery.c b/drivers/power/ds2760_battery.c
new file mode 100644
index 000000000000..00e1ea6f1de2
--- /dev/null
+++ b/drivers/power/ds2760_battery.c
@@ -0,0 +1,470 @@
1/*
2 * Driver for batteries with DS2760 chips inside.
3 *
4 * Copyright © 2007 Anton Vorontsov
5 * 2004-2007 Matt Reimer
6 * 2004 Szabolcs Gyurko
7 *
8 * Use consistent with the GNU GPL is permitted,
9 * provided that this copyright notice is
10 * preserved in its entirety in all copies and derived works.
11 *
12 * Author: Anton Vorontsov <cbou@mail.ru>
13 * February 2007
14 *
15 * Matt Reimer <mreimer@vpop.net>
16 * April 2004, 2005, 2007
17 *
18 * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
19 * September 2004
20 */
21
22#include <linux/module.h>
23#include <linux/param.h>
24#include <linux/jiffies.h>
25#include <linux/workqueue.h>
26#include <linux/pm.h>
27#include <linux/platform_device.h>
28#include <linux/power_supply.h>
29
30#include "../w1/w1.h"
31#include "../w1/slaves/w1_ds2760.h"
32
33struct ds2760_device_info {
34 struct device *dev;
35
36 /* DS2760 data, valid after calling ds2760_battery_read_status() */
37 unsigned long update_time; /* jiffies when data read */
38 char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */
39 int voltage_raw; /* units of 4.88 mV */
40 int voltage_uV; /* units of µV */
41 int current_raw; /* units of 0.625 mA */
42 int current_uA; /* units of µA */
43 int accum_current_raw; /* units of 0.25 mAh */
44 int accum_current_uAh; /* units of µAh */
45 int temp_raw; /* units of 0.125 °C */
46 int temp_C; /* units of 0.1 °C */
47 int rated_capacity; /* units of µAh */
48 int rem_capacity; /* percentage */
49 int full_active_uAh; /* units of µAh */
50 int empty_uAh; /* units of µAh */
51 int life_sec; /* units of seconds */
52 int charge_status; /* POWER_SUPPLY_STATUS_* */
53
54 int full_counter;
55 struct power_supply bat;
56 struct device *w1_dev;
57 struct workqueue_struct *monitor_wqueue;
58 struct delayed_work monitor_work;
59};
60
61static unsigned int cache_time = 1000;
62module_param(cache_time, uint, 0644);
63MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
64
65/* Some batteries have their rated capacity stored a N * 10 mAh, while
66 * others use an index into this table. */
67static int rated_capacities[] = {
68 0,
69 920, /* Samsung */
70 920, /* BYD */
71 920, /* Lishen */
72 920, /* NEC */
73 1440, /* Samsung */
74 1440, /* BYD */
75 1440, /* Lishen */
76 1440, /* NEC */
77 2880, /* Samsung */
78 2880, /* BYD */
79 2880, /* Lishen */
80 2880 /* NEC */
81};
82
83/* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
84 * temp is in Celsius */
85static int battery_interpolate(int array[], int temp)
86{
87 int index, dt;
88
89 if (temp <= 0)
90 return array[0];
91 if (temp >= 40)
92 return array[4];
93
94 index = temp / 10;
95 dt = temp % 10;
96
97 return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
98}
99
100static int ds2760_battery_read_status(struct ds2760_device_info *di)
101{
102 int ret, i, start, count, scale[5];
103
104 if (di->update_time && time_before(jiffies, di->update_time +
105 msecs_to_jiffies(cache_time)))
106 return 0;
107
108 /* The first time we read the entire contents of SRAM/EEPROM,
109 * but after that we just read the interesting bits that change. */
110 if (di->update_time == 0) {
111 start = 0;
112 count = DS2760_DATA_SIZE;
113 } else {
114 start = DS2760_VOLTAGE_MSB;
115 count = DS2760_TEMP_LSB - start + 1;
116 }
117
118 ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
119 if (ret != count) {
120 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
121 di->w1_dev);
122 return 1;
123 }
124
125 di->update_time = jiffies;
126
127 /* DS2760 reports voltage in units of 4.88mV, but the battery class
128 * reports in units of uV, so convert by multiplying by 4880. */
129 di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
130 (di->raw[DS2760_VOLTAGE_LSB] >> 5);
131 di->voltage_uV = di->voltage_raw * 4880;
132
133 /* DS2760 reports current in signed units of 0.625mA, but the battery
134 * class reports in units of µA, so convert by multiplying by 625. */
135 di->current_raw =
136 (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
137 (di->raw[DS2760_CURRENT_LSB] >> 3);
138 di->current_uA = di->current_raw * 625;
139
140 /* DS2760 reports accumulated current in signed units of 0.25mAh. */
141 di->accum_current_raw =
142 (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
143 di->raw[DS2760_CURRENT_ACCUM_LSB];
144 di->accum_current_uAh = di->accum_current_raw * 250;
145
146 /* DS2760 reports temperature in signed units of 0.125°C, but the
147 * battery class reports in units of 1/10 °C, so we convert by
148 * multiplying by .125 * 10 = 1.25. */
149 di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
150 (di->raw[DS2760_TEMP_LSB] >> 5);
151 di->temp_C = di->temp_raw + (di->temp_raw / 4);
152
153 /* At least some battery monitors (e.g. HP iPAQ) store the battery's
154 * maximum rated capacity. */
155 if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
156 di->rated_capacity = rated_capacities[
157 (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
158 else
159 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
160
161 di->rated_capacity *= 1000; /* convert to µAh */
162
163 /* Calculate the full level at the present temperature. */
164 di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
165 di->raw[DS2760_ACTIVE_FULL + 1];
166
167 scale[0] = di->raw[DS2760_ACTIVE_FULL] << 8 |
168 di->raw[DS2760_ACTIVE_FULL + 1];
169 for (i = 1; i < 5; i++)
170 scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i];
171
172 di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
173 di->full_active_uAh *= 1000; /* convert to µAh */
174
175 /* Calculate the empty level at the present temperature. */
176 scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
177 for (i = 3; i >= 0; i--)
178 scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
179
180 di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
181 di->empty_uAh *= 1000; /* convert to µAh */
182
183 /* From Maxim Application Note 131: remaining capacity =
184 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
185 di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
186 (di->full_active_uAh - di->empty_uAh);
187
188 if (di->rem_capacity < 0)
189 di->rem_capacity = 0;
190 if (di->rem_capacity > 100)
191 di->rem_capacity = 100;
192
193 if (di->current_uA)
194 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
195 3600L) / di->current_uA;
196 else
197 di->life_sec = 0;
198
199 return 0;
200}
201
202static void ds2760_battery_update_status(struct ds2760_device_info *di)
203{
204 int old_charge_status = di->charge_status;
205
206 ds2760_battery_read_status(di);
207
208 if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
209 di->full_counter = 0;
210
211 if (power_supply_am_i_supplied(&di->bat)) {
212 if (di->current_uA > 10000) {
213 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
214 di->full_counter = 0;
215 } else if (di->current_uA < -5000) {
216 if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
217 dev_notice(di->dev, "not enough power to "
218 "charge\n");
219 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
220 di->full_counter = 0;
221 } else if (di->current_uA < 10000 &&
222 di->charge_status != POWER_SUPPLY_STATUS_FULL) {
223
224 /* Don't consider the battery to be full unless
225 * we've seen the current < 10 mA at least two
226 * consecutive times. */
227
228 di->full_counter++;
229
230 if (di->full_counter < 2) {
231 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
232 } else {
233 unsigned char acr[2];
234 int acr_val;
235
236 /* acr is in units of 0.25 mAh */
237 acr_val = di->full_active_uAh * 4L / 1000;
238
239 acr[0] = acr_val >> 8;
240 acr[1] = acr_val & 0xff;
241
242 if (w1_ds2760_write(di->w1_dev, acr,
243 DS2760_CURRENT_ACCUM_MSB, 2) < 2)
244 dev_warn(di->dev,
245 "ACR reset failed\n");
246
247 di->charge_status = POWER_SUPPLY_STATUS_FULL;
248 }
249 }
250 } else {
251 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
252 di->full_counter = 0;
253 }
254
255 if (di->charge_status != old_charge_status)
256 power_supply_changed(&di->bat);
257
258 return;
259}
260
261static void ds2760_battery_work(struct work_struct *work)
262{
263 struct ds2760_device_info *di = container_of(work,
264 struct ds2760_device_info, monitor_work.work);
265 const int interval = HZ * 60;
266
267 dev_dbg(di->dev, "%s\n", __FUNCTION__);
268
269 ds2760_battery_update_status(di);
270 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
271
272 return;
273}
274
275#define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
276 bat);
277
278static void ds2760_battery_external_power_changed(struct power_supply *psy)
279{
280 struct ds2760_device_info *di = to_ds2760_device_info(psy);
281
282 dev_dbg(di->dev, "%s\n", __FUNCTION__);
283
284 cancel_delayed_work(&di->monitor_work);
285 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
286
287 return;
288}
289
290static int ds2760_battery_get_property(struct power_supply *psy,
291 enum power_supply_property psp,
292 union power_supply_propval *val)
293{
294 struct ds2760_device_info *di = to_ds2760_device_info(psy);
295
296 switch (psp) {
297 case POWER_SUPPLY_PROP_STATUS:
298 val->intval = di->charge_status;
299 return 0;
300 default:
301 break;
302 }
303
304 ds2760_battery_read_status(di);
305
306 switch (psp) {
307 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
308 val->intval = di->voltage_uV;
309 break;
310 case POWER_SUPPLY_PROP_CURRENT_NOW:
311 val->intval = di->current_uA;
312 break;
313 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
314 val->intval = di->rated_capacity;
315 break;
316 case POWER_SUPPLY_PROP_CHARGE_FULL:
317 val->intval = di->full_active_uAh;
318 break;
319 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
320 val->intval = di->empty_uAh;
321 break;
322 case POWER_SUPPLY_PROP_CHARGE_NOW:
323 val->intval = di->accum_current_uAh;
324 break;
325 case POWER_SUPPLY_PROP_TEMP:
326 val->intval = di->temp_C;
327 break;
328 default:
329 return -EINVAL;
330 }
331
332 return 0;
333}
334
335static enum power_supply_property ds2760_battery_props[] = {
336 POWER_SUPPLY_PROP_STATUS,
337 POWER_SUPPLY_PROP_VOLTAGE_NOW,
338 POWER_SUPPLY_PROP_CURRENT_NOW,
339 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
340 POWER_SUPPLY_PROP_CHARGE_FULL,
341 POWER_SUPPLY_PROP_CHARGE_EMPTY,
342 POWER_SUPPLY_PROP_CHARGE_NOW,
343 POWER_SUPPLY_PROP_TEMP,
344};
345
346static int ds2760_battery_probe(struct platform_device *pdev)
347{
348 int retval = 0;
349 struct ds2760_device_info *di;
350 struct ds2760_platform_data *pdata;
351
352 di = kzalloc(sizeof(*di), GFP_KERNEL);
353 if (!di) {
354 retval = -ENOMEM;
355 goto di_alloc_failed;
356 }
357
358 platform_set_drvdata(pdev, di);
359
360 pdata = pdev->dev.platform_data;
361 di->dev = &pdev->dev;
362 di->w1_dev = pdev->dev.parent;
363 di->bat.name = pdev->dev.bus_id;
364 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
365 di->bat.properties = ds2760_battery_props;
366 di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
367 di->bat.get_property = ds2760_battery_get_property;
368 di->bat.external_power_changed =
369 ds2760_battery_external_power_changed;
370
371 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
372
373 retval = power_supply_register(&pdev->dev, &di->bat);
374 if (retval) {
375 dev_err(di->dev, "failed to register battery");
376 goto batt_failed;
377 }
378
379 INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
380 di->monitor_wqueue = create_singlethread_workqueue(pdev->dev.bus_id);
381 if (!di->monitor_wqueue) {
382 retval = -ESRCH;
383 goto workqueue_failed;
384 }
385 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
386
387 goto success;
388
389workqueue_failed:
390 power_supply_unregister(&di->bat);
391batt_failed:
392 kfree(di);
393di_alloc_failed:
394success:
395 return retval;
396}
397
398static int ds2760_battery_remove(struct platform_device *pdev)
399{
400 struct ds2760_device_info *di = platform_get_drvdata(pdev);
401
402 cancel_rearming_delayed_workqueue(di->monitor_wqueue,
403 &di->monitor_work);
404 destroy_workqueue(di->monitor_wqueue);
405 power_supply_unregister(&di->bat);
406
407 return 0;
408}
409
410#ifdef CONFIG_PM
411
412static int ds2760_battery_suspend(struct platform_device *pdev,
413 pm_message_t state)
414{
415 struct ds2760_device_info *di = platform_get_drvdata(pdev);
416
417 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
418
419 return 0;
420}
421
422static int ds2760_battery_resume(struct platform_device *pdev)
423{
424 struct ds2760_device_info *di = platform_get_drvdata(pdev);
425
426 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
427 power_supply_changed(&di->bat);
428
429 cancel_delayed_work(&di->monitor_work);
430 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
431
432 return 0;
433}
434
435#else
436
437#define ds2760_battery_suspend NULL
438#define ds2760_battery_resume NULL
439
440#endif /* CONFIG_PM */
441
442static struct platform_driver ds2760_battery_driver = {
443 .driver = {
444 .name = "ds2760-battery",
445 },
446 .probe = ds2760_battery_probe,
447 .remove = ds2760_battery_remove,
448 .suspend = ds2760_battery_suspend,
449 .resume = ds2760_battery_resume,
450};
451
452static int __init ds2760_battery_init(void)
453{
454 return platform_driver_register(&ds2760_battery_driver);
455}
456
457static void __exit ds2760_battery_exit(void)
458{
459 platform_driver_unregister(&ds2760_battery_driver);
460 return;
461}
462
463module_init(ds2760_battery_init);
464module_exit(ds2760_battery_exit);
465
466MODULE_LICENSE("GPL");
467MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
468 "Matt Reimer <mreimer@vpop.net>, "
469 "Anton Vorontsov <cbou@mail.ru>");
470MODULE_DESCRIPTION("ds2760 battery driver");