blob: f2c9cc33c0f9f795a81125368c1dc758c2dd81c8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Anton Vorontsovfe0e3152007-05-04 00:45:39 +040028#include <linux/platform_device.h>
29#include <linux/power_supply.h>
30
31#include "../w1/w1.h"
32#include "../w1/slaves/w1_ds2760.h"
33
34struct ds2760_device_info {
35 struct device *dev;
36
37 /* DS2760 data, valid after calling ds2760_battery_read_status() */
38 unsigned long update_time; /* jiffies when data read */
39 char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */
40 int voltage_raw; /* units of 4.88 mV */
41 int voltage_uV; /* units of µV */
42 int current_raw; /* units of 0.625 mA */
43 int current_uA; /* units of µA */
44 int accum_current_raw; /* units of 0.25 mAh */
45 int accum_current_uAh; /* units of µAh */
46 int temp_raw; /* units of 0.125 °C */
47 int temp_C; /* units of 0.1 °C */
48 int rated_capacity; /* units of µAh */
49 int rem_capacity; /* percentage */
50 int full_active_uAh; /* units of µAh */
51 int empty_uAh; /* units of µAh */
52 int life_sec; /* units of seconds */
53 int charge_status; /* POWER_SUPPLY_STATUS_* */
54
55 int full_counter;
56 struct power_supply bat;
57 struct device *w1_dev;
58 struct workqueue_struct *monitor_wqueue;
59 struct delayed_work monitor_work;
Daniel Mack8d631cc2009-07-23 20:35:54 +020060 struct delayed_work set_charged_work;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +040061};
62
63static unsigned int cache_time = 1000;
64module_param(cache_time, uint, 0644);
65MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
66
Daniel Mackcef437e2009-04-28 10:55:02 +020067static unsigned int pmod_enabled;
68module_param(pmod_enabled, bool, 0644);
69MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
70
Daniel Mackc1e72192009-07-15 18:20:40 +020071static unsigned int rated_capacity;
72module_param(rated_capacity, uint, 0644);
73MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
74
Daniel Mack02d0d272009-07-15 22:57:16 +020075static unsigned int current_accum;
76module_param(current_accum, uint, 0644);
77MODULE_PARM_DESC(current_accum, "current accumulator value");
78
Anton Vorontsovfe0e3152007-05-04 00:45:39 +040079/* Some batteries have their rated capacity stored a N * 10 mAh, while
80 * others use an index into this table. */
81static int rated_capacities[] = {
82 0,
83 920, /* Samsung */
84 920, /* BYD */
85 920, /* Lishen */
86 920, /* NEC */
87 1440, /* Samsung */
88 1440, /* BYD */
Paul Parsons9c6f8742011-04-25 13:45:40 +000089#ifdef CONFIG_MACH_H4700
90 1800, /* HP iPAQ hx4700 3.7V 1800mAh (359113-001) */
91#else
Anton Vorontsovfe0e3152007-05-04 00:45:39 +040092 1440, /* Lishen */
Paul Parsons9c6f8742011-04-25 13:45:40 +000093#endif
Anton Vorontsovfe0e3152007-05-04 00:45:39 +040094 1440, /* NEC */
95 2880, /* Samsung */
96 2880, /* BYD */
97 2880, /* Lishen */
98 2880 /* NEC */
99};
100
101/* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
102 * temp is in Celsius */
103static int battery_interpolate(int array[], int temp)
104{
105 int index, dt;
106
107 if (temp <= 0)
108 return array[0];
109 if (temp >= 40)
110 return array[4];
111
112 index = temp / 10;
113 dt = temp % 10;
114
115 return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
116}
117
118static int ds2760_battery_read_status(struct ds2760_device_info *di)
119{
120 int ret, i, start, count, scale[5];
121
122 if (di->update_time && time_before(jiffies, di->update_time +
123 msecs_to_jiffies(cache_time)))
124 return 0;
125
126 /* The first time we read the entire contents of SRAM/EEPROM,
127 * but after that we just read the interesting bits that change. */
128 if (di->update_time == 0) {
129 start = 0;
130 count = DS2760_DATA_SIZE;
131 } else {
132 start = DS2760_VOLTAGE_MSB;
133 count = DS2760_TEMP_LSB - start + 1;
134 }
135
136 ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
137 if (ret != count) {
138 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
139 di->w1_dev);
140 return 1;
141 }
142
143 di->update_time = jiffies;
144
145 /* DS2760 reports voltage in units of 4.88mV, but the battery class
146 * reports in units of uV, so convert by multiplying by 4880. */
147 di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
148 (di->raw[DS2760_VOLTAGE_LSB] >> 5);
149 di->voltage_uV = di->voltage_raw * 4880;
150
151 /* DS2760 reports current in signed units of 0.625mA, but the battery
152 * class reports in units of µA, so convert by multiplying by 625. */
153 di->current_raw =
154 (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
155 (di->raw[DS2760_CURRENT_LSB] >> 3);
156 di->current_uA = di->current_raw * 625;
157
158 /* DS2760 reports accumulated current in signed units of 0.25mAh. */
159 di->accum_current_raw =
160 (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
161 di->raw[DS2760_CURRENT_ACCUM_LSB];
162 di->accum_current_uAh = di->accum_current_raw * 250;
163
164 /* DS2760 reports temperature in signed units of 0.125°C, but the
165 * battery class reports in units of 1/10 °C, so we convert by
166 * multiplying by .125 * 10 = 1.25. */
167 di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
168 (di->raw[DS2760_TEMP_LSB] >> 5);
169 di->temp_C = di->temp_raw + (di->temp_raw / 4);
170
171 /* At least some battery monitors (e.g. HP iPAQ) store the battery's
172 * maximum rated capacity. */
173 if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
174 di->rated_capacity = rated_capacities[
175 (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
176 else
177 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
178
179 di->rated_capacity *= 1000; /* convert to µAh */
180
181 /* Calculate the full level at the present temperature. */
182 di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
183 di->raw[DS2760_ACTIVE_FULL + 1];
184
Daniel Mack25f2bfa2009-07-15 18:20:41 +0200185 /* If the full_active_uAh value is not given, fall back to the rated
186 * capacity. This is likely to happen when chips are not part of the
187 * battery pack and is therefore not bootstrapped. */
188 if (di->full_active_uAh == 0)
189 di->full_active_uAh = di->rated_capacity / 1000L;
190
191 scale[0] = di->full_active_uAh;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400192 for (i = 1; i < 5; i++)
Paul Parsons3af98a82011-04-25 14:43:30 +0000193 scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i];
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400194
195 di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
196 di->full_active_uAh *= 1000; /* convert to µAh */
197
198 /* Calculate the empty level at the present temperature. */
199 scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
200 for (i = 3; i >= 0; i--)
201 scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
202
203 di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
204 di->empty_uAh *= 1000; /* convert to µAh */
205
Daniel Macka4e3f912009-03-12 14:31:30 -0700206 if (di->full_active_uAh == di->empty_uAh)
207 di->rem_capacity = 0;
208 else
209 /* From Maxim Application Note 131: remaining capacity =
210 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
211 di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
212 (di->full_active_uAh - di->empty_uAh);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400213
214 if (di->rem_capacity < 0)
215 di->rem_capacity = 0;
216 if (di->rem_capacity > 100)
217 di->rem_capacity = 100;
218
Sven Neumann86af9502010-11-12 11:36:22 +0100219 if (di->current_uA < -100L)
Daniel Mackb0525b42009-07-24 15:08:11 +0200220 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
221 / (di->current_uA / 100L);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400222 else
223 di->life_sec = 0;
224
225 return 0;
226}
227
Daniel Mack02d0d272009-07-15 22:57:16 +0200228static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
229 unsigned int acr_val)
230{
231 unsigned char acr[2];
232
233 /* acr is in units of 0.25 mAh */
234 acr_val *= 4L;
235 acr_val /= 1000;
236
237 acr[0] = acr_val >> 8;
238 acr[1] = acr_val & 0xff;
239
240 if (w1_ds2760_write(di->w1_dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
241 dev_warn(di->dev, "ACR write failed\n");
242}
243
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400244static void ds2760_battery_update_status(struct ds2760_device_info *di)
245{
246 int old_charge_status = di->charge_status;
247
248 ds2760_battery_read_status(di);
249
250 if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
251 di->full_counter = 0;
252
253 if (power_supply_am_i_supplied(&di->bat)) {
254 if (di->current_uA > 10000) {
255 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
256 di->full_counter = 0;
257 } else if (di->current_uA < -5000) {
258 if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
259 dev_notice(di->dev, "not enough power to "
260 "charge\n");
261 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
262 di->full_counter = 0;
263 } else if (di->current_uA < 10000 &&
264 di->charge_status != POWER_SUPPLY_STATUS_FULL) {
265
266 /* Don't consider the battery to be full unless
267 * we've seen the current < 10 mA at least two
268 * consecutive times. */
269
270 di->full_counter++;
271
272 if (di->full_counter < 2) {
273 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
274 } else {
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400275 di->charge_status = POWER_SUPPLY_STATUS_FULL;
Daniel Mack02d0d272009-07-15 22:57:16 +0200276 ds2760_battery_set_current_accum(di,
277 di->full_active_uAh);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400278 }
279 }
280 } else {
281 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
282 di->full_counter = 0;
283 }
284
285 if (di->charge_status != old_charge_status)
286 power_supply_changed(&di->bat);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400287}
288
Daniel Mackcef437e2009-04-28 10:55:02 +0200289static void ds2760_battery_write_status(struct ds2760_device_info *di,
290 char status)
291{
292 if (status == di->raw[DS2760_STATUS_REG])
293 return;
294
295 w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1);
296 w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
297 w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
298}
299
Daniel Mackc1e72192009-07-15 18:20:40 +0200300static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
301 unsigned char rated_capacity)
302{
303 if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
304 return;
305
306 w1_ds2760_write(di->w1_dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
307 w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
308 w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
309}
310
Daniel Mackbd52ca52010-05-18 21:49:53 +0200311static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
312 int active_full)
313{
314 unsigned char tmp[2] = {
315 active_full >> 8,
316 active_full & 0xff
317 };
318
319 if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
320 tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
321 return;
322
323 w1_ds2760_write(di->w1_dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
324 w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK0);
325 w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK0);
326
327 /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
328 * values won't be read back by ds2760_battery_read_status() */
329 di->raw[DS2760_ACTIVE_FULL] = tmp[0];
330 di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
331}
332
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400333static void ds2760_battery_work(struct work_struct *work)
334{
335 struct ds2760_device_info *di = container_of(work,
336 struct ds2760_device_info, monitor_work.work);
337 const int interval = HZ * 60;
338
Harvey Harrison0cddc0a2008-04-29 00:58:29 -0700339 dev_dbg(di->dev, "%s\n", __func__);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400340
341 ds2760_battery_update_status(di);
342 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400343}
344
345#define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
346 bat);
347
348static void ds2760_battery_external_power_changed(struct power_supply *psy)
349{
350 struct ds2760_device_info *di = to_ds2760_device_info(psy);
351
Harvey Harrison0cddc0a2008-04-29 00:58:29 -0700352 dev_dbg(di->dev, "%s\n", __func__);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400353
354 cancel_delayed_work(&di->monitor_work);
355 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400356}
357
Daniel Mack8d631cc2009-07-23 20:35:54 +0200358
359static void ds2760_battery_set_charged_work(struct work_struct *work)
360{
361 char bias;
362 struct ds2760_device_info *di = container_of(work,
363 struct ds2760_device_info, set_charged_work.work);
364
365 dev_dbg(di->dev, "%s\n", __func__);
366
367 ds2760_battery_read_status(di);
368
369 /* When we get notified by external circuitry that the battery is
370 * considered fully charged now, we know that there is no current
371 * flow any more. However, the ds2760's internal current meter is
372 * too inaccurate to rely on - spec say something ~15% failure.
373 * Hence, we use the current offset bias register to compensate
374 * that error.
375 */
376
377 if (!power_supply_am_i_supplied(&di->bat))
378 return;
379
380 bias = (signed char) di->current_raw +
381 (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
382
383 dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
384
385 w1_ds2760_write(di->w1_dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
386 w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
387 w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
388
389 /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
390 * value won't be read back by ds2760_battery_read_status() */
391 di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
392}
393
394static void ds2760_battery_set_charged(struct power_supply *psy)
395{
396 struct ds2760_device_info *di = to_ds2760_device_info(psy);
397
398 /* postpone the actual work by 20 secs. This is for debouncing GPIO
399 * signals and to let the current value settle. See AN4188. */
400 cancel_delayed_work(&di->set_charged_work);
401 queue_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
402}
403
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400404static int ds2760_battery_get_property(struct power_supply *psy,
405 enum power_supply_property psp,
406 union power_supply_propval *val)
407{
408 struct ds2760_device_info *di = to_ds2760_device_info(psy);
409
410 switch (psp) {
411 case POWER_SUPPLY_PROP_STATUS:
412 val->intval = di->charge_status;
413 return 0;
414 default:
415 break;
416 }
417
418 ds2760_battery_read_status(di);
419
420 switch (psp) {
421 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
422 val->intval = di->voltage_uV;
423 break;
424 case POWER_SUPPLY_PROP_CURRENT_NOW:
425 val->intval = di->current_uA;
426 break;
427 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
428 val->intval = di->rated_capacity;
429 break;
430 case POWER_SUPPLY_PROP_CHARGE_FULL:
431 val->intval = di->full_active_uAh;
432 break;
433 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
434 val->intval = di->empty_uAh;
435 break;
436 case POWER_SUPPLY_PROP_CHARGE_NOW:
437 val->intval = di->accum_current_uAh;
438 break;
439 case POWER_SUPPLY_PROP_TEMP:
440 val->intval = di->temp_C;
441 break;
Daniel Mack5c6e9bf22009-07-15 18:20:39 +0200442 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
443 val->intval = di->life_sec;
444 break;
445 case POWER_SUPPLY_PROP_CAPACITY:
446 val->intval = di->rem_capacity;
447 break;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400448 default:
449 return -EINVAL;
450 }
451
452 return 0;
453}
454
Daniel Mackbd52ca52010-05-18 21:49:53 +0200455static int ds2760_battery_set_property(struct power_supply *psy,
456 enum power_supply_property psp,
457 const union power_supply_propval *val)
458{
459 struct ds2760_device_info *di = to_ds2760_device_info(psy);
460
461 switch (psp) {
462 case POWER_SUPPLY_PROP_CHARGE_FULL:
463 /* the interface counts in uAh, convert the value */
464 ds2760_battery_write_active_full(di, val->intval / 1000L);
465 break;
466
467 case POWER_SUPPLY_PROP_CHARGE_NOW:
468 /* ds2760_battery_set_current_accum() does the conversion */
469 ds2760_battery_set_current_accum(di, val->intval);
470 break;
471
472 default:
473 return -EPERM;
474 }
475
476 return 0;
477}
478
479static int ds2760_battery_property_is_writeable(struct power_supply *psy,
480 enum power_supply_property psp)
481{
482 switch (psp) {
483 case POWER_SUPPLY_PROP_CHARGE_FULL:
484 case POWER_SUPPLY_PROP_CHARGE_NOW:
485 return 1;
486
487 default:
488 break;
489 }
490
491 return 0;
492}
493
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400494static enum power_supply_property ds2760_battery_props[] = {
495 POWER_SUPPLY_PROP_STATUS,
496 POWER_SUPPLY_PROP_VOLTAGE_NOW,
497 POWER_SUPPLY_PROP_CURRENT_NOW,
498 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
499 POWER_SUPPLY_PROP_CHARGE_FULL,
500 POWER_SUPPLY_PROP_CHARGE_EMPTY,
501 POWER_SUPPLY_PROP_CHARGE_NOW,
502 POWER_SUPPLY_PROP_TEMP,
Daniel Mack5c6e9bf22009-07-15 18:20:39 +0200503 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
504 POWER_SUPPLY_PROP_CAPACITY,
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400505};
506
507static int ds2760_battery_probe(struct platform_device *pdev)
508{
Daniel Mackcef437e2009-04-28 10:55:02 +0200509 char status;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400510 int retval = 0;
511 struct ds2760_device_info *di;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400512
513 di = kzalloc(sizeof(*di), GFP_KERNEL);
514 if (!di) {
515 retval = -ENOMEM;
516 goto di_alloc_failed;
517 }
518
519 platform_set_drvdata(pdev, di);
520
Daniel Mackae9fb6e2009-04-28 10:55:00 +0200521 di->dev = &pdev->dev;
522 di->w1_dev = pdev->dev.parent;
523 di->bat.name = dev_name(&pdev->dev);
524 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
525 di->bat.properties = ds2760_battery_props;
526 di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
527 di->bat.get_property = ds2760_battery_get_property;
Daniel Mackbd52ca52010-05-18 21:49:53 +0200528 di->bat.set_property = ds2760_battery_set_property;
529 di->bat.property_is_writeable =
530 ds2760_battery_property_is_writeable;
Daniel Mack8d631cc2009-07-23 20:35:54 +0200531 di->bat.set_charged = ds2760_battery_set_charged;
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400532 di->bat.external_power_changed =
533 ds2760_battery_external_power_changed;
534
535 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
536
Daniel Mackcef437e2009-04-28 10:55:02 +0200537 /* enable sleep mode feature */
538 ds2760_battery_read_status(di);
539 status = di->raw[DS2760_STATUS_REG];
540 if (pmod_enabled)
541 status |= DS2760_STATUS_PMOD;
542 else
543 status &= ~DS2760_STATUS_PMOD;
544
545 ds2760_battery_write_status(di, status);
546
Daniel Mackc1e72192009-07-15 18:20:40 +0200547 /* set rated capacity from module param */
548 if (rated_capacity)
549 ds2760_battery_write_rated_capacity(di, rated_capacity);
550
Daniel Mack02d0d272009-07-15 22:57:16 +0200551 /* set current accumulator if given as parameter.
552 * this should only be done for bootstrapping the value */
553 if (current_accum)
554 ds2760_battery_set_current_accum(di, current_accum);
555
Daniel Mack2e83a5c2009-07-15 18:20:38 +0200556 retval = power_supply_register(&pdev->dev, &di->bat);
557 if (retval) {
558 dev_err(di->dev, "failed to register battery\n");
559 goto batt_failed;
560 }
561
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400562 INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
Daniel Mack8d631cc2009-07-23 20:35:54 +0200563 INIT_DELAYED_WORK(&di->set_charged_work,
564 ds2760_battery_set_charged_work);
Kay Sieversba88b002009-01-06 10:44:38 -0800565 di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400566 if (!di->monitor_wqueue) {
567 retval = -ESRCH;
568 goto workqueue_failed;
569 }
570 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
571
572 goto success;
573
574workqueue_failed:
575 power_supply_unregister(&di->bat);
576batt_failed:
577 kfree(di);
578di_alloc_failed:
579success:
580 return retval;
581}
582
583static int ds2760_battery_remove(struct platform_device *pdev)
584{
585 struct ds2760_device_info *di = platform_get_drvdata(pdev);
586
Tejun Heoafe2c512010-12-14 16:21:17 +0100587 cancel_delayed_work_sync(&di->monitor_work);
588 cancel_delayed_work_sync(&di->set_charged_work);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400589 destroy_workqueue(di->monitor_wqueue);
590 power_supply_unregister(&di->bat);
Axel Lina01bce62010-09-27 15:07:12 +0800591 kfree(di);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400592
593 return 0;
594}
595
596#ifdef CONFIG_PM
597
598static int ds2760_battery_suspend(struct platform_device *pdev,
599 pm_message_t state)
600{
601 struct ds2760_device_info *di = platform_get_drvdata(pdev);
602
603 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
604
605 return 0;
606}
607
608static int ds2760_battery_resume(struct platform_device *pdev)
609{
610 struct ds2760_device_info *di = platform_get_drvdata(pdev);
611
612 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
613 power_supply_changed(&di->bat);
614
615 cancel_delayed_work(&di->monitor_work);
616 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
617
618 return 0;
619}
620
621#else
622
623#define ds2760_battery_suspend NULL
624#define ds2760_battery_resume NULL
625
626#endif /* CONFIG_PM */
627
Kay Sievers2f5a5cf2008-07-25 01:45:46 -0700628MODULE_ALIAS("platform:ds2760-battery");
629
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400630static struct platform_driver ds2760_battery_driver = {
631 .driver = {
632 .name = "ds2760-battery",
633 },
634 .probe = ds2760_battery_probe,
635 .remove = ds2760_battery_remove,
636 .suspend = ds2760_battery_suspend,
637 .resume = ds2760_battery_resume,
638};
639
640static int __init ds2760_battery_init(void)
641{
642 return platform_driver_register(&ds2760_battery_driver);
643}
644
645static void __exit ds2760_battery_exit(void)
646{
647 platform_driver_unregister(&ds2760_battery_driver);
Anton Vorontsovfe0e3152007-05-04 00:45:39 +0400648}
649
650module_init(ds2760_battery_init);
651module_exit(ds2760_battery_exit);
652
653MODULE_LICENSE("GPL");
654MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
655 "Matt Reimer <mreimer@vpop.net>, "
656 "Anton Vorontsov <cbou@mail.ru>");
657MODULE_DESCRIPTION("ds2760 battery driver");