blob: ed0ea5e275c5414e73a60f7ab5a0e1cec2c9edb9 [file] [log] [blame]
Anton Vorontsovfe0e3152007-05-04 00:45:39 +04001/*
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
Daniel Mackcef437e2009-04-28 10:55:02 +020065static unsigned int pmod_enabled;
66module_param(pmod_enabled, bool, 0644);
67MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
68
Daniel Mackc1e72192009-07-15 18:20:40 +020069static unsigned int rated_capacity;
70module_param(rated_capacity, uint, 0644);
71MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
72
Anton Vorontsovfe0e3152007-05-04 00:45:39 +040073/* Some batteries have their rated capacity stored a N * 10 mAh, while
74 * others use an index into this table. */
75static int rated_capacities[] = {
76 0,
77 920, /* Samsung */
78 920, /* BYD */
79 920, /* Lishen */
80 920, /* NEC */
81 1440, /* Samsung */
82 1440, /* BYD */
83 1440, /* Lishen */
84 1440, /* NEC */
85 2880, /* Samsung */
86 2880, /* BYD */
87 2880, /* Lishen */
88 2880 /* NEC */
89};
90
91/* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
92 * temp is in Celsius */
93static int battery_interpolate(int array[], int temp)
94{
95 int index, dt;
96
97 if (temp <= 0)
98 return array[0];
99 if (temp >= 40)
100 return array[4];
101
102 index = temp / 10;
103 dt = temp % 10;
104
105 return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
106}
107
108static int ds2760_battery_read_status(struct ds2760_device_info *di)
109{
110 int ret, i, start, count, scale[5];
111
112 if (di->update_time && time_before(jiffies, di->update_time +
113 msecs_to_jiffies(cache_time)))
114 return 0;
115
116 /* The first time we read the entire contents of SRAM/EEPROM,
117 * but after that we just read the interesting bits that change. */
118 if (di->update_time == 0) {
119 start = 0;
120 count = DS2760_DATA_SIZE;
121 } else {
122 start = DS2760_VOLTAGE_MSB;
123 count = DS2760_TEMP_LSB - start + 1;
124 }
125
126 ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
127 if (ret != count) {
128 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
129 di->w1_dev);
130 return 1;
131 }
132
133 di->update_time = jiffies;
134
135 /* DS2760 reports voltage in units of 4.88mV, but the battery class
136 * reports in units of uV, so convert by multiplying by 4880. */
137 di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
138 (di->raw[DS2760_VOLTAGE_LSB] >> 5);
139 di->voltage_uV = di->voltage_raw * 4880;
140
141 /* DS2760 reports current in signed units of 0.625mA, but the battery
142 * class reports in units of µA, so convert by multiplying by 625. */
143 di->current_raw =
144 (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
145 (di->raw[DS2760_CURRENT_LSB] >> 3);
146 di->current_uA = di->current_raw * 625;
147
148 /* DS2760 reports accumulated current in signed units of 0.25mAh. */
149 di->accum_current_raw =
150 (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
151 di->raw[DS2760_CURRENT_ACCUM_LSB];
152 di->accum_current_uAh = di->accum_current_raw * 250;
153
154 /* DS2760 reports temperature in signed units of 0.125°C, but the
155 * battery class reports in units of 1/10 °C, so we convert by
156 * multiplying by .125 * 10 = 1.25. */
157 di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
158 (di->raw[DS2760_TEMP_LSB] >> 5);
159 di->temp_C = di->temp_raw + (di->temp_raw / 4);
160
161 /* At least some battery monitors (e.g. HP iPAQ) store the battery's
162 * maximum rated capacity. */
163 if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
164 di->rated_capacity = rated_capacities[
165 (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
166 else
167 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
168
169 di->rated_capacity *= 1000; /* convert to µAh */
170
171 /* Calculate the full level at the present temperature. */
172 di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
173 di->raw[DS2760_ACTIVE_FULL + 1];
174
175 scale[0] = di->raw[DS2760_ACTIVE_FULL] << 8 |
176 di->raw[DS2760_ACTIVE_FULL + 1];
177 for (i = 1; i < 5; i++)
178 scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i];
179
180 di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
181 di->full_active_uAh *= 1000; /* convert to µAh */
182
183 /* Calculate the empty level at the present temperature. */
184 scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
185 for (i = 3; i >= 0; i--)
186 scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
187
188 di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
189 di->empty_uAh *= 1000; /* convert to µAh */
190
Daniel Macka4e3f912009-03-12 14:31:30 -0700191 if (di->full_active_uAh == di->empty_uAh)
192 di->rem_capacity = 0;
193 else
194 /* From Maxim Application Note 131: remaining capacity =
195 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
196 di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
197 (di->full_active_uAh - di->empty_uAh);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400198
199 if (di->rem_capacity < 0)
200 di->rem_capacity = 0;
201 if (di->rem_capacity > 100)
202 di->rem_capacity = 100;
203
204 if (di->current_uA)
205 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
206 3600L) / di->current_uA;
207 else
208 di->life_sec = 0;
209
210 return 0;
211}
212
213static void ds2760_battery_update_status(struct ds2760_device_info *di)
214{
215 int old_charge_status = di->charge_status;
216
217 ds2760_battery_read_status(di);
218
219 if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
220 di->full_counter = 0;
221
222 if (power_supply_am_i_supplied(&di->bat)) {
223 if (di->current_uA > 10000) {
224 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
225 di->full_counter = 0;
226 } else if (di->current_uA < -5000) {
227 if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
228 dev_notice(di->dev, "not enough power to "
229 "charge\n");
230 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
231 di->full_counter = 0;
232 } else if (di->current_uA < 10000 &&
233 di->charge_status != POWER_SUPPLY_STATUS_FULL) {
234
235 /* Don't consider the battery to be full unless
236 * we've seen the current < 10 mA at least two
237 * consecutive times. */
238
239 di->full_counter++;
240
241 if (di->full_counter < 2) {
242 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
243 } else {
244 unsigned char acr[2];
245 int acr_val;
246
247 /* acr is in units of 0.25 mAh */
248 acr_val = di->full_active_uAh * 4L / 1000;
249
250 acr[0] = acr_val >> 8;
251 acr[1] = acr_val & 0xff;
252
253 if (w1_ds2760_write(di->w1_dev, acr,
254 DS2760_CURRENT_ACCUM_MSB, 2) < 2)
255 dev_warn(di->dev,
256 "ACR reset failed\n");
257
258 di->charge_status = POWER_SUPPLY_STATUS_FULL;
259 }
260 }
261 } else {
262 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
263 di->full_counter = 0;
264 }
265
266 if (di->charge_status != old_charge_status)
267 power_supply_changed(&di->bat);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400268}
269
Daniel Mackcef437e2009-04-28 10:55:02 +0200270static void ds2760_battery_write_status(struct ds2760_device_info *di,
271 char status)
272{
273 if (status == di->raw[DS2760_STATUS_REG])
274 return;
275
276 w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1);
277 w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
278 w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
279}
280
Daniel Mackc1e72192009-07-15 18:20:40 +0200281static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
282 unsigned char rated_capacity)
283{
284 if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
285 return;
286
287 w1_ds2760_write(di->w1_dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
288 w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
289 w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
290}
291
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400292static void ds2760_battery_work(struct work_struct *work)
293{
294 struct ds2760_device_info *di = container_of(work,
295 struct ds2760_device_info, monitor_work.work);
296 const int interval = HZ * 60;
297
Harvey Harrison0cddc0a2008-04-29 00:58:29 -0700298 dev_dbg(di->dev, "%s\n", __func__);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400299
300 ds2760_battery_update_status(di);
301 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400302}
303
304#define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
305 bat);
306
307static void ds2760_battery_external_power_changed(struct power_supply *psy)
308{
309 struct ds2760_device_info *di = to_ds2760_device_info(psy);
310
Harvey Harrison0cddc0a2008-04-29 00:58:29 -0700311 dev_dbg(di->dev, "%s\n", __func__);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400312
313 cancel_delayed_work(&di->monitor_work);
314 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400315}
316
317static int ds2760_battery_get_property(struct power_supply *psy,
318 enum power_supply_property psp,
319 union power_supply_propval *val)
320{
321 struct ds2760_device_info *di = to_ds2760_device_info(psy);
322
323 switch (psp) {
324 case POWER_SUPPLY_PROP_STATUS:
325 val->intval = di->charge_status;
326 return 0;
327 default:
328 break;
329 }
330
331 ds2760_battery_read_status(di);
332
333 switch (psp) {
334 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
335 val->intval = di->voltage_uV;
336 break;
337 case POWER_SUPPLY_PROP_CURRENT_NOW:
338 val->intval = di->current_uA;
339 break;
340 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
341 val->intval = di->rated_capacity;
342 break;
343 case POWER_SUPPLY_PROP_CHARGE_FULL:
344 val->intval = di->full_active_uAh;
345 break;
346 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
347 val->intval = di->empty_uAh;
348 break;
349 case POWER_SUPPLY_PROP_CHARGE_NOW:
350 val->intval = di->accum_current_uAh;
351 break;
352 case POWER_SUPPLY_PROP_TEMP:
353 val->intval = di->temp_C;
354 break;
Daniel Mack5c6e9bf22009-07-15 18:20:39 +0200355 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
356 val->intval = di->life_sec;
357 break;
358 case POWER_SUPPLY_PROP_CAPACITY:
359 val->intval = di->rem_capacity;
360 break;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400361 default:
362 return -EINVAL;
363 }
364
365 return 0;
366}
367
368static enum power_supply_property ds2760_battery_props[] = {
369 POWER_SUPPLY_PROP_STATUS,
370 POWER_SUPPLY_PROP_VOLTAGE_NOW,
371 POWER_SUPPLY_PROP_CURRENT_NOW,
372 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
373 POWER_SUPPLY_PROP_CHARGE_FULL,
374 POWER_SUPPLY_PROP_CHARGE_EMPTY,
375 POWER_SUPPLY_PROP_CHARGE_NOW,
376 POWER_SUPPLY_PROP_TEMP,
Daniel Mack5c6e9bf22009-07-15 18:20:39 +0200377 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
378 POWER_SUPPLY_PROP_CAPACITY,
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400379};
380
381static int ds2760_battery_probe(struct platform_device *pdev)
382{
Daniel Mackcef437e2009-04-28 10:55:02 +0200383 char status;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400384 int retval = 0;
385 struct ds2760_device_info *di;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400386
387 di = kzalloc(sizeof(*di), GFP_KERNEL);
388 if (!di) {
389 retval = -ENOMEM;
390 goto di_alloc_failed;
391 }
392
393 platform_set_drvdata(pdev, di);
394
Daniel Mackae9fb6e2009-04-28 10:55:00 +0200395 di->dev = &pdev->dev;
396 di->w1_dev = pdev->dev.parent;
397 di->bat.name = dev_name(&pdev->dev);
398 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
399 di->bat.properties = ds2760_battery_props;
400 di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
401 di->bat.get_property = ds2760_battery_get_property;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400402 di->bat.external_power_changed =
403 ds2760_battery_external_power_changed;
404
405 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
406
Daniel Mackcef437e2009-04-28 10:55:02 +0200407 /* enable sleep mode feature */
408 ds2760_battery_read_status(di);
409 status = di->raw[DS2760_STATUS_REG];
410 if (pmod_enabled)
411 status |= DS2760_STATUS_PMOD;
412 else
413 status &= ~DS2760_STATUS_PMOD;
414
415 ds2760_battery_write_status(di, status);
416
Daniel Mackc1e72192009-07-15 18:20:40 +0200417 /* set rated capacity from module param */
418 if (rated_capacity)
419 ds2760_battery_write_rated_capacity(di, rated_capacity);
420
Daniel Mack2e83a5c2009-07-15 18:20:38 +0200421 retval = power_supply_register(&pdev->dev, &di->bat);
422 if (retval) {
423 dev_err(di->dev, "failed to register battery\n");
424 goto batt_failed;
425 }
426
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400427 INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
Kay Sieversba88b002009-01-06 10:44:38 -0800428 di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400429 if (!di->monitor_wqueue) {
430 retval = -ESRCH;
431 goto workqueue_failed;
432 }
433 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
434
435 goto success;
436
437workqueue_failed:
438 power_supply_unregister(&di->bat);
439batt_failed:
440 kfree(di);
441di_alloc_failed:
442success:
443 return retval;
444}
445
446static int ds2760_battery_remove(struct platform_device *pdev)
447{
448 struct ds2760_device_info *di = platform_get_drvdata(pdev);
449
450 cancel_rearming_delayed_workqueue(di->monitor_wqueue,
451 &di->monitor_work);
452 destroy_workqueue(di->monitor_wqueue);
453 power_supply_unregister(&di->bat);
454
455 return 0;
456}
457
458#ifdef CONFIG_PM
459
460static int ds2760_battery_suspend(struct platform_device *pdev,
461 pm_message_t state)
462{
463 struct ds2760_device_info *di = platform_get_drvdata(pdev);
464
465 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
466
467 return 0;
468}
469
470static int ds2760_battery_resume(struct platform_device *pdev)
471{
472 struct ds2760_device_info *di = platform_get_drvdata(pdev);
473
474 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
475 power_supply_changed(&di->bat);
476
477 cancel_delayed_work(&di->monitor_work);
478 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
479
480 return 0;
481}
482
483#else
484
485#define ds2760_battery_suspend NULL
486#define ds2760_battery_resume NULL
487
488#endif /* CONFIG_PM */
489
Kay Sievers2f5a5cf2008-07-25 01:45:46 -0700490MODULE_ALIAS("platform:ds2760-battery");
491
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400492static struct platform_driver ds2760_battery_driver = {
493 .driver = {
494 .name = "ds2760-battery",
495 },
496 .probe = ds2760_battery_probe,
497 .remove = ds2760_battery_remove,
498 .suspend = ds2760_battery_suspend,
499 .resume = ds2760_battery_resume,
500};
501
502static int __init ds2760_battery_init(void)
503{
504 return platform_driver_register(&ds2760_battery_driver);
505}
506
507static void __exit ds2760_battery_exit(void)
508{
509 platform_driver_unregister(&ds2760_battery_driver);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400510}
511
512module_init(ds2760_battery_init);
513module_exit(ds2760_battery_exit);
514
515MODULE_LICENSE("GPL");
516MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
517 "Matt Reimer <mreimer@vpop.net>, "
518 "Anton Vorontsov <cbou@mail.ru>");
519MODULE_DESCRIPTION("ds2760 battery driver");