Merge tag 'tags/restart-handler-for-v3.18' into next
[deliverable/linux.git] / drivers / power / bq27x00_battery.c
1 /*
2 * BQ27x00 battery driver
3 *
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
6 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
7 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
8 *
9 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
10 *
11 * This package is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 */
20
21 /*
22 * Datasheets:
23 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
24 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
25 * http://www.ti.com/product/bq27425-g1
26 * http://www.ti.com/product/BQ27742-G1
27 */
28
29 #include <linux/device.h>
30 #include <linux/module.h>
31 #include <linux/param.h>
32 #include <linux/jiffies.h>
33 #include <linux/workqueue.h>
34 #include <linux/delay.h>
35 #include <linux/platform_device.h>
36 #include <linux/power_supply.h>
37 #include <linux/idr.h>
38 #include <linux/i2c.h>
39 #include <linux/slab.h>
40 #include <asm/unaligned.h>
41
42 #include <linux/power/bq27x00_battery.h>
43
44 #define DRIVER_VERSION "1.2.0"
45
46 #define BQ27x00_REG_TEMP 0x06
47 #define BQ27x00_REG_VOLT 0x08
48 #define BQ27x00_REG_AI 0x14
49 #define BQ27x00_REG_FLAGS 0x0A
50 #define BQ27x00_REG_TTE 0x16
51 #define BQ27x00_REG_TTF 0x18
52 #define BQ27x00_REG_TTECP 0x26
53 #define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
54 #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
55 #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
56 #define BQ27x00_REG_AE 0x22 /* Available energy */
57 #define BQ27x00_POWER_AVG 0x24
58
59 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
60 #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
61 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
62 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
63 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
64 #define BQ27000_FLAG_FC BIT(5)
65 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
66
67 #define BQ27500_REG_SOC 0x2C
68 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
69 #define BQ27500_FLAG_DSC BIT(0)
70 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
71 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
72 #define BQ27500_FLAG_FC BIT(9)
73 #define BQ27500_FLAG_OTC BIT(15)
74
75 #define BQ27742_POWER_AVG 0x76
76
77 /* bq27425 register addresses are same as bq27x00 addresses minus 4 */
78 #define BQ27425_REG_OFFSET 0x04
79 #define BQ27425_REG_SOC 0x18 /* Register address plus offset */
80
81 #define BQ27000_RS 20 /* Resistor sense */
82 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
83
84 struct bq27x00_device_info;
85 struct bq27x00_access_methods {
86 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
87 };
88
89 enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742};
90
91 struct bq27x00_reg_cache {
92 int temperature;
93 int time_to_empty;
94 int time_to_empty_avg;
95 int time_to_full;
96 int charge_full;
97 int cycle_count;
98 int capacity;
99 int energy;
100 int flags;
101 int power_avg;
102 int health;
103 };
104
105 struct bq27x00_device_info {
106 struct device *dev;
107 int id;
108 enum bq27x00_chip chip;
109
110 struct bq27x00_reg_cache cache;
111 int charge_design_full;
112
113 unsigned long last_update;
114 struct delayed_work work;
115
116 struct power_supply bat;
117
118 struct bq27x00_access_methods bus;
119
120 struct mutex lock;
121 };
122
123 static enum power_supply_property bq27x00_battery_props[] = {
124 POWER_SUPPLY_PROP_STATUS,
125 POWER_SUPPLY_PROP_PRESENT,
126 POWER_SUPPLY_PROP_VOLTAGE_NOW,
127 POWER_SUPPLY_PROP_CURRENT_NOW,
128 POWER_SUPPLY_PROP_CAPACITY,
129 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
130 POWER_SUPPLY_PROP_TEMP,
131 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
132 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
133 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
134 POWER_SUPPLY_PROP_TECHNOLOGY,
135 POWER_SUPPLY_PROP_CHARGE_FULL,
136 POWER_SUPPLY_PROP_CHARGE_NOW,
137 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
138 POWER_SUPPLY_PROP_CYCLE_COUNT,
139 POWER_SUPPLY_PROP_ENERGY_NOW,
140 POWER_SUPPLY_PROP_POWER_AVG,
141 POWER_SUPPLY_PROP_HEALTH,
142 };
143
144 static enum power_supply_property bq27425_battery_props[] = {
145 POWER_SUPPLY_PROP_STATUS,
146 POWER_SUPPLY_PROP_PRESENT,
147 POWER_SUPPLY_PROP_VOLTAGE_NOW,
148 POWER_SUPPLY_PROP_CURRENT_NOW,
149 POWER_SUPPLY_PROP_CAPACITY,
150 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
151 POWER_SUPPLY_PROP_TEMP,
152 POWER_SUPPLY_PROP_TECHNOLOGY,
153 POWER_SUPPLY_PROP_CHARGE_FULL,
154 POWER_SUPPLY_PROP_CHARGE_NOW,
155 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
156 };
157
158 static enum power_supply_property bq27742_battery_props[] = {
159 POWER_SUPPLY_PROP_STATUS,
160 POWER_SUPPLY_PROP_PRESENT,
161 POWER_SUPPLY_PROP_VOLTAGE_NOW,
162 POWER_SUPPLY_PROP_CURRENT_NOW,
163 POWER_SUPPLY_PROP_CAPACITY,
164 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
165 POWER_SUPPLY_PROP_TEMP,
166 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
167 POWER_SUPPLY_PROP_TECHNOLOGY,
168 POWER_SUPPLY_PROP_CHARGE_FULL,
169 POWER_SUPPLY_PROP_CHARGE_NOW,
170 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
171 POWER_SUPPLY_PROP_CYCLE_COUNT,
172 POWER_SUPPLY_PROP_POWER_AVG,
173 POWER_SUPPLY_PROP_HEALTH,
174 };
175
176 static unsigned int poll_interval = 360;
177 module_param(poll_interval, uint, 0644);
178 MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
179 "0 disables polling");
180
181 /*
182 * Common code for BQ27x00 devices
183 */
184
185 static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
186 bool single)
187 {
188 if (di->chip == BQ27425)
189 return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
190 return di->bus.read(di, reg, single);
191 }
192
193 /*
194 * Higher versions of the chip like BQ27425 and BQ27500
195 * differ from BQ27000 and BQ27200 in calculation of certain
196 * parameters. Hence we need to check for the chip type.
197 */
198 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
199 {
200 if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742)
201 return true;
202 return false;
203 }
204
205 /*
206 * Return the battery Relative State-of-Charge
207 * Or < 0 if something fails.
208 */
209 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
210 {
211 int rsoc;
212
213 if (di->chip == BQ27500 || di->chip == BQ27742)
214 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
215 else if (di->chip == BQ27425)
216 rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
217 else
218 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
219
220 if (rsoc < 0)
221 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
222
223 return rsoc;
224 }
225
226 /*
227 * Return a battery charge value in µAh
228 * Or < 0 if something fails.
229 */
230 static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
231 {
232 int charge;
233
234 charge = bq27x00_read(di, reg, false);
235 if (charge < 0) {
236 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
237 reg, charge);
238 return charge;
239 }
240
241 if (bq27xxx_is_chip_version_higher(di))
242 charge *= 1000;
243 else
244 charge = charge * 3570 / BQ27000_RS;
245
246 return charge;
247 }
248
249 /*
250 * Return the battery Nominal available capaciy in µAh
251 * Or < 0 if something fails.
252 */
253 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
254 {
255 int flags;
256 bool is_bq27500 = di->chip == BQ27500;
257 bool is_higher = bq27xxx_is_chip_version_higher(di);
258
259 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
260 if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
261 return -ENODATA;
262
263 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
264 }
265
266 /*
267 * Return the battery Last measured discharge in µAh
268 * Or < 0 if something fails.
269 */
270 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
271 {
272 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
273 }
274
275 /*
276 * Return the battery Initial last measured discharge in µAh
277 * Or < 0 if something fails.
278 */
279 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
280 {
281 int ilmd;
282
283 if (bq27xxx_is_chip_version_higher(di))
284 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
285 else
286 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
287
288 if (ilmd < 0) {
289 dev_dbg(di->dev, "error reading initial last measured discharge\n");
290 return ilmd;
291 }
292
293 if (bq27xxx_is_chip_version_higher(di))
294 ilmd *= 1000;
295 else
296 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
297
298 return ilmd;
299 }
300
301 /*
302 * Return the battery Available energy in µWh
303 * Or < 0 if something fails.
304 */
305 static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
306 {
307 int ae;
308
309 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
310 if (ae < 0) {
311 dev_dbg(di->dev, "error reading available energy\n");
312 return ae;
313 }
314
315 if (di->chip == BQ27500)
316 ae *= 1000;
317 else
318 ae = ae * 29200 / BQ27000_RS;
319
320 return ae;
321 }
322
323 /*
324 * Return the battery temperature in tenths of degree Kelvin
325 * Or < 0 if something fails.
326 */
327 static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
328 {
329 int temp;
330
331 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
332 if (temp < 0) {
333 dev_err(di->dev, "error reading temperature\n");
334 return temp;
335 }
336
337 if (!bq27xxx_is_chip_version_higher(di))
338 temp = 5 * temp / 2;
339
340 return temp;
341 }
342
343 /*
344 * Return the battery Cycle count total
345 * Or < 0 if something fails.
346 */
347 static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
348 {
349 int cyct;
350
351 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
352 if (cyct < 0)
353 dev_err(di->dev, "error reading cycle count total\n");
354
355 return cyct;
356 }
357
358 /*
359 * Read a time register.
360 * Return < 0 if something fails.
361 */
362 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
363 {
364 int tval;
365
366 tval = bq27x00_read(di, reg, false);
367 if (tval < 0) {
368 dev_dbg(di->dev, "error reading time register %02x: %d\n",
369 reg, tval);
370 return tval;
371 }
372
373 if (tval == 65535)
374 return -ENODATA;
375
376 return tval * 60;
377 }
378
379 /*
380 * Read a power avg register.
381 * Return < 0 if something fails.
382 */
383 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
384 {
385 int tval;
386
387 tval = bq27x00_read(di, reg, false);
388 if (tval < 0) {
389 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
390 reg, tval);
391 return tval;
392 }
393
394 if (di->chip == BQ27500)
395 return tval;
396 else
397 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
398 }
399
400 /*
401 * Read flag register.
402 * Return < 0 if something fails.
403 */
404 static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
405 {
406 int tval;
407
408 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
409 if (tval < 0) {
410 dev_err(di->dev, "error reading flag register:%d\n", tval);
411 return tval;
412 }
413
414 if ((di->chip == BQ27500)) {
415 if (tval & BQ27500_FLAG_SOCF)
416 tval = POWER_SUPPLY_HEALTH_DEAD;
417 else if (tval & BQ27500_FLAG_OTC)
418 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
419 else
420 tval = POWER_SUPPLY_HEALTH_GOOD;
421 return tval;
422 } else {
423 if (tval & BQ27000_FLAG_EDV1)
424 tval = POWER_SUPPLY_HEALTH_DEAD;
425 else
426 tval = POWER_SUPPLY_HEALTH_GOOD;
427 return tval;
428 }
429
430 return -1;
431 }
432
433 static void bq27x00_update(struct bq27x00_device_info *di)
434 {
435 struct bq27x00_reg_cache cache = {0, };
436 bool is_bq27500 = di->chip == BQ27500;
437 bool is_bq27425 = di->chip == BQ27425;
438 bool is_bq27742 = di->chip == BQ27742;
439
440 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
441 if ((cache.flags & 0xff) == 0xff)
442 /* read error */
443 cache.flags = -1;
444 if (cache.flags >= 0) {
445 if (!is_bq27500 && !is_bq27425
446 && (cache.flags & BQ27000_FLAG_CI)) {
447 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
448 cache.capacity = -ENODATA;
449 cache.energy = -ENODATA;
450 cache.time_to_empty = -ENODATA;
451 cache.time_to_empty_avg = -ENODATA;
452 cache.time_to_full = -ENODATA;
453 cache.charge_full = -ENODATA;
454 cache.health = -ENODATA;
455 } else {
456 cache.capacity = bq27x00_battery_read_rsoc(di);
457 if (is_bq27742)
458 cache.time_to_empty =
459 bq27x00_battery_read_time(di,
460 BQ27x00_REG_TTE);
461 else if (!is_bq27425) {
462 cache.energy = bq27x00_battery_read_energy(di);
463 cache.time_to_empty =
464 bq27x00_battery_read_time(di,
465 BQ27x00_REG_TTE);
466 cache.time_to_empty_avg =
467 bq27x00_battery_read_time(di,
468 BQ27x00_REG_TTECP);
469 cache.time_to_full =
470 bq27x00_battery_read_time(di,
471 BQ27x00_REG_TTF);
472 }
473 if (!is_bq27742)
474 cache.charge_full =
475 bq27x00_battery_read_lmd(di);
476 cache.health = bq27x00_battery_read_health(di);
477 }
478 cache.temperature = bq27x00_battery_read_temperature(di);
479 if (!is_bq27425)
480 cache.cycle_count = bq27x00_battery_read_cyct(di);
481 if (is_bq27742)
482 cache.power_avg =
483 bq27x00_battery_read_pwr_avg(di,
484 BQ27742_POWER_AVG);
485 else
486 cache.power_avg =
487 bq27x00_battery_read_pwr_avg(di,
488 BQ27x00_POWER_AVG);
489
490 /* We only have to read charge design full once */
491 if (di->charge_design_full <= 0)
492 di->charge_design_full = bq27x00_battery_read_ilmd(di);
493 }
494
495 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
496 di->cache = cache;
497 power_supply_changed(&di->bat);
498 }
499
500 di->last_update = jiffies;
501 }
502
503 static void bq27x00_battery_poll(struct work_struct *work)
504 {
505 struct bq27x00_device_info *di =
506 container_of(work, struct bq27x00_device_info, work.work);
507
508 bq27x00_update(di);
509
510 if (poll_interval > 0) {
511 /* The timer does not have to be accurate. */
512 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
513 schedule_delayed_work(&di->work, poll_interval * HZ);
514 }
515 }
516
517 /*
518 * Return the battery average current in µA
519 * Note that current can be negative signed as well
520 * Or 0 if something fails.
521 */
522 static int bq27x00_battery_current(struct bq27x00_device_info *di,
523 union power_supply_propval *val)
524 {
525 int curr;
526 int flags;
527
528 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
529 if (curr < 0) {
530 dev_err(di->dev, "error reading current\n");
531 return curr;
532 }
533
534 if (bq27xxx_is_chip_version_higher(di)) {
535 /* bq27500 returns signed value */
536 val->intval = (int)((s16)curr) * 1000;
537 } else {
538 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
539 if (flags & BQ27000_FLAG_CHGS) {
540 dev_dbg(di->dev, "negative current!\n");
541 curr = -curr;
542 }
543
544 val->intval = curr * 3570 / BQ27000_RS;
545 }
546
547 return 0;
548 }
549
550 static int bq27x00_battery_status(struct bq27x00_device_info *di,
551 union power_supply_propval *val)
552 {
553 int status;
554
555 if (bq27xxx_is_chip_version_higher(di)) {
556 if (di->cache.flags & BQ27500_FLAG_FC)
557 status = POWER_SUPPLY_STATUS_FULL;
558 else if (di->cache.flags & BQ27500_FLAG_DSC)
559 status = POWER_SUPPLY_STATUS_DISCHARGING;
560 else
561 status = POWER_SUPPLY_STATUS_CHARGING;
562 } else {
563 if (di->cache.flags & BQ27000_FLAG_FC)
564 status = POWER_SUPPLY_STATUS_FULL;
565 else if (di->cache.flags & BQ27000_FLAG_CHGS)
566 status = POWER_SUPPLY_STATUS_CHARGING;
567 else if (power_supply_am_i_supplied(&di->bat))
568 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
569 else
570 status = POWER_SUPPLY_STATUS_DISCHARGING;
571 }
572
573 val->intval = status;
574
575 return 0;
576 }
577
578 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
579 union power_supply_propval *val)
580 {
581 int level;
582
583 if (bq27xxx_is_chip_version_higher(di)) {
584 if (di->cache.flags & BQ27500_FLAG_FC)
585 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
586 else if (di->cache.flags & BQ27500_FLAG_SOC1)
587 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
588 else if (di->cache.flags & BQ27500_FLAG_SOCF)
589 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
590 else
591 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
592 } else {
593 if (di->cache.flags & BQ27000_FLAG_FC)
594 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
595 else if (di->cache.flags & BQ27000_FLAG_EDV1)
596 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
597 else if (di->cache.flags & BQ27000_FLAG_EDVF)
598 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
599 else
600 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
601 }
602
603 val->intval = level;
604
605 return 0;
606 }
607
608 /*
609 * Return the battery Voltage in millivolts
610 * Or < 0 if something fails.
611 */
612 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
613 union power_supply_propval *val)
614 {
615 int volt;
616
617 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
618 if (volt < 0) {
619 dev_err(di->dev, "error reading voltage\n");
620 return volt;
621 }
622
623 val->intval = volt * 1000;
624
625 return 0;
626 }
627
628 static int bq27x00_simple_value(int value,
629 union power_supply_propval *val)
630 {
631 if (value < 0)
632 return value;
633
634 val->intval = value;
635
636 return 0;
637 }
638
639 #define to_bq27x00_device_info(x) container_of((x), \
640 struct bq27x00_device_info, bat);
641
642 static int bq27x00_battery_get_property(struct power_supply *psy,
643 enum power_supply_property psp,
644 union power_supply_propval *val)
645 {
646 int ret = 0;
647 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
648
649 mutex_lock(&di->lock);
650 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
651 cancel_delayed_work_sync(&di->work);
652 bq27x00_battery_poll(&di->work.work);
653 }
654 mutex_unlock(&di->lock);
655
656 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
657 return -ENODEV;
658
659 switch (psp) {
660 case POWER_SUPPLY_PROP_STATUS:
661 ret = bq27x00_battery_status(di, val);
662 break;
663 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
664 ret = bq27x00_battery_voltage(di, val);
665 break;
666 case POWER_SUPPLY_PROP_PRESENT:
667 val->intval = di->cache.flags < 0 ? 0 : 1;
668 break;
669 case POWER_SUPPLY_PROP_CURRENT_NOW:
670 ret = bq27x00_battery_current(di, val);
671 break;
672 case POWER_SUPPLY_PROP_CAPACITY:
673 ret = bq27x00_simple_value(di->cache.capacity, val);
674 break;
675 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
676 ret = bq27x00_battery_capacity_level(di, val);
677 break;
678 case POWER_SUPPLY_PROP_TEMP:
679 ret = bq27x00_simple_value(di->cache.temperature, val);
680 if (ret == 0)
681 val->intval -= 2731;
682 break;
683 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
684 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
685 break;
686 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
687 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
688 break;
689 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
690 ret = bq27x00_simple_value(di->cache.time_to_full, val);
691 break;
692 case POWER_SUPPLY_PROP_TECHNOLOGY:
693 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
694 break;
695 case POWER_SUPPLY_PROP_CHARGE_NOW:
696 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
697 break;
698 case POWER_SUPPLY_PROP_CHARGE_FULL:
699 ret = bq27x00_simple_value(di->cache.charge_full, val);
700 break;
701 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
702 ret = bq27x00_simple_value(di->charge_design_full, val);
703 break;
704 case POWER_SUPPLY_PROP_CYCLE_COUNT:
705 ret = bq27x00_simple_value(di->cache.cycle_count, val);
706 break;
707 case POWER_SUPPLY_PROP_ENERGY_NOW:
708 ret = bq27x00_simple_value(di->cache.energy, val);
709 break;
710 case POWER_SUPPLY_PROP_POWER_AVG:
711 ret = bq27x00_simple_value(di->cache.power_avg, val);
712 break;
713 case POWER_SUPPLY_PROP_HEALTH:
714 ret = bq27x00_simple_value(di->cache.health, val);
715 break;
716 default:
717 return -EINVAL;
718 }
719
720 return ret;
721 }
722
723 static void bq27x00_external_power_changed(struct power_supply *psy)
724 {
725 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
726
727 cancel_delayed_work_sync(&di->work);
728 schedule_delayed_work(&di->work, 0);
729 }
730
731 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
732 {
733 int ret;
734
735 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
736 if (di->chip == BQ27425) {
737 di->bat.properties = bq27425_battery_props;
738 di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
739 } else if (di->chip == BQ27742) {
740 di->bat.properties = bq27742_battery_props;
741 di->bat.num_properties = ARRAY_SIZE(bq27742_battery_props);
742 } else {
743 di->bat.properties = bq27x00_battery_props;
744 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
745 }
746 di->bat.get_property = bq27x00_battery_get_property;
747 di->bat.external_power_changed = bq27x00_external_power_changed;
748
749 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
750 mutex_init(&di->lock);
751
752 ret = power_supply_register(di->dev, &di->bat);
753 if (ret) {
754 dev_err(di->dev, "failed to register battery: %d\n", ret);
755 return ret;
756 }
757
758 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
759
760 bq27x00_update(di);
761
762 return 0;
763 }
764
765 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
766 {
767 /*
768 * power_supply_unregister call bq27x00_battery_get_property which
769 * call bq27x00_battery_poll.
770 * Make sure that bq27x00_battery_poll will not call
771 * schedule_delayed_work again after unregister (which cause OOPS).
772 */
773 poll_interval = 0;
774
775 cancel_delayed_work_sync(&di->work);
776
777 power_supply_unregister(&di->bat);
778
779 mutex_destroy(&di->lock);
780 }
781
782
783 /* i2c specific code */
784 #ifdef CONFIG_BATTERY_BQ27X00_I2C
785
786 /* If the system has several batteries we need a different name for each
787 * of them...
788 */
789 static DEFINE_IDR(battery_id);
790 static DEFINE_MUTEX(battery_mutex);
791
792 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
793 {
794 struct i2c_client *client = to_i2c_client(di->dev);
795 struct i2c_msg msg[2];
796 unsigned char data[2];
797 int ret;
798
799 if (!client->adapter)
800 return -ENODEV;
801
802 msg[0].addr = client->addr;
803 msg[0].flags = 0;
804 msg[0].buf = &reg;
805 msg[0].len = sizeof(reg);
806 msg[1].addr = client->addr;
807 msg[1].flags = I2C_M_RD;
808 msg[1].buf = data;
809 if (single)
810 msg[1].len = 1;
811 else
812 msg[1].len = 2;
813
814 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
815 if (ret < 0)
816 return ret;
817
818 if (!single)
819 ret = get_unaligned_le16(data);
820 else
821 ret = data[0];
822
823 return ret;
824 }
825
826 static int bq27x00_battery_probe(struct i2c_client *client,
827 const struct i2c_device_id *id)
828 {
829 char *name;
830 struct bq27x00_device_info *di;
831 int num;
832 int retval = 0;
833
834 /* Get new ID for the new battery device */
835 mutex_lock(&battery_mutex);
836 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
837 mutex_unlock(&battery_mutex);
838 if (num < 0)
839 return num;
840
841 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
842 if (!name) {
843 dev_err(&client->dev, "failed to allocate device name\n");
844 retval = -ENOMEM;
845 goto batt_failed_1;
846 }
847
848 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
849 if (!di) {
850 dev_err(&client->dev, "failed to allocate device info data\n");
851 retval = -ENOMEM;
852 goto batt_failed_2;
853 }
854
855 di->id = num;
856 di->dev = &client->dev;
857 di->chip = id->driver_data;
858 di->bat.name = name;
859 di->bus.read = &bq27x00_read_i2c;
860
861 retval = bq27x00_powersupply_init(di);
862 if (retval)
863 goto batt_failed_2;
864
865 i2c_set_clientdata(client, di);
866
867 return 0;
868
869 batt_failed_2:
870 kfree(name);
871 batt_failed_1:
872 mutex_lock(&battery_mutex);
873 idr_remove(&battery_id, num);
874 mutex_unlock(&battery_mutex);
875
876 return retval;
877 }
878
879 static int bq27x00_battery_remove(struct i2c_client *client)
880 {
881 struct bq27x00_device_info *di = i2c_get_clientdata(client);
882
883 bq27x00_powersupply_unregister(di);
884
885 kfree(di->bat.name);
886
887 mutex_lock(&battery_mutex);
888 idr_remove(&battery_id, di->id);
889 mutex_unlock(&battery_mutex);
890
891 return 0;
892 }
893
894 static const struct i2c_device_id bq27x00_id[] = {
895 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
896 { "bq27500", BQ27500 },
897 { "bq27425", BQ27425 },
898 { "bq27742", BQ27742 },
899 {},
900 };
901 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
902
903 static struct i2c_driver bq27x00_battery_driver = {
904 .driver = {
905 .name = "bq27x00-battery",
906 },
907 .probe = bq27x00_battery_probe,
908 .remove = bq27x00_battery_remove,
909 .id_table = bq27x00_id,
910 };
911
912 static inline int bq27x00_battery_i2c_init(void)
913 {
914 int ret = i2c_add_driver(&bq27x00_battery_driver);
915 if (ret)
916 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
917
918 return ret;
919 }
920
921 static inline void bq27x00_battery_i2c_exit(void)
922 {
923 i2c_del_driver(&bq27x00_battery_driver);
924 }
925
926 #else
927
928 static inline int bq27x00_battery_i2c_init(void) { return 0; }
929 static inline void bq27x00_battery_i2c_exit(void) {};
930
931 #endif
932
933 /* platform specific code */
934 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
935
936 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
937 bool single)
938 {
939 struct device *dev = di->dev;
940 struct bq27000_platform_data *pdata = dev->platform_data;
941 unsigned int timeout = 3;
942 int upper, lower;
943 int temp;
944
945 if (!single) {
946 /* Make sure the value has not changed in between reading the
947 * lower and the upper part */
948 upper = pdata->read(dev, reg + 1);
949 do {
950 temp = upper;
951 if (upper < 0)
952 return upper;
953
954 lower = pdata->read(dev, reg);
955 if (lower < 0)
956 return lower;
957
958 upper = pdata->read(dev, reg + 1);
959 } while (temp != upper && --timeout);
960
961 if (timeout == 0)
962 return -EIO;
963
964 return (upper << 8) | lower;
965 }
966
967 return pdata->read(dev, reg);
968 }
969
970 static int bq27000_battery_probe(struct platform_device *pdev)
971 {
972 struct bq27x00_device_info *di;
973 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
974
975 if (!pdata) {
976 dev_err(&pdev->dev, "no platform_data supplied\n");
977 return -EINVAL;
978 }
979
980 if (!pdata->read) {
981 dev_err(&pdev->dev, "no hdq read callback supplied\n");
982 return -EINVAL;
983 }
984
985 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
986 if (!di) {
987 dev_err(&pdev->dev, "failed to allocate device info data\n");
988 return -ENOMEM;
989 }
990
991 platform_set_drvdata(pdev, di);
992
993 di->dev = &pdev->dev;
994 di->chip = BQ27000;
995
996 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
997 di->bus.read = &bq27000_read_platform;
998
999 return bq27x00_powersupply_init(di);
1000 }
1001
1002 static int bq27000_battery_remove(struct platform_device *pdev)
1003 {
1004 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
1005
1006 bq27x00_powersupply_unregister(di);
1007
1008 return 0;
1009 }
1010
1011 static struct platform_driver bq27000_battery_driver = {
1012 .probe = bq27000_battery_probe,
1013 .remove = bq27000_battery_remove,
1014 .driver = {
1015 .name = "bq27000-battery",
1016 .owner = THIS_MODULE,
1017 },
1018 };
1019
1020 static inline int bq27x00_battery_platform_init(void)
1021 {
1022 int ret = platform_driver_register(&bq27000_battery_driver);
1023 if (ret)
1024 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
1025
1026 return ret;
1027 }
1028
1029 static inline void bq27x00_battery_platform_exit(void)
1030 {
1031 platform_driver_unregister(&bq27000_battery_driver);
1032 }
1033
1034 #else
1035
1036 static inline int bq27x00_battery_platform_init(void) { return 0; }
1037 static inline void bq27x00_battery_platform_exit(void) {};
1038
1039 #endif
1040
1041 /*
1042 * Module stuff
1043 */
1044
1045 static int __init bq27x00_battery_init(void)
1046 {
1047 int ret;
1048
1049 ret = bq27x00_battery_i2c_init();
1050 if (ret)
1051 return ret;
1052
1053 ret = bq27x00_battery_platform_init();
1054 if (ret)
1055 bq27x00_battery_i2c_exit();
1056
1057 return ret;
1058 }
1059 module_init(bq27x00_battery_init);
1060
1061 static void __exit bq27x00_battery_exit(void)
1062 {
1063 bq27x00_battery_platform_exit();
1064 bq27x00_battery_i2c_exit();
1065 }
1066 module_exit(bq27x00_battery_exit);
1067
1068 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1069 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1070 MODULE_LICENSE("GPL");
This page took 0.065843 seconds and 6 git commands to generate.