Merge branches 'for-3.18/upstream-fixes' and 'for-3.19/upstream' into for-linus
[cascardo/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_bq27742 = di->chip == BQ27742;
258         bool is_higher = bq27xxx_is_chip_version_higher(di);
259         bool flags_1b = !(is_bq27500 || is_bq27742);
260
261         flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
262         if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
263                 return -ENODATA;
264
265         return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
266 }
267
268 /*
269  * Return the battery Last measured discharge in µAh
270  * Or < 0 if something fails.
271  */
272 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
273 {
274         return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
275 }
276
277 /*
278  * Return the battery Initial last measured discharge in µAh
279  * Or < 0 if something fails.
280  */
281 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
282 {
283         int ilmd;
284
285         if (bq27xxx_is_chip_version_higher(di))
286                 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
287         else
288                 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
289
290         if (ilmd < 0) {
291                 dev_dbg(di->dev, "error reading initial last measured discharge\n");
292                 return ilmd;
293         }
294
295         if (bq27xxx_is_chip_version_higher(di))
296                 ilmd *= 1000;
297         else
298                 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
299
300         return ilmd;
301 }
302
303 /*
304  * Return the battery Available energy in µWh
305  * Or < 0 if something fails.
306  */
307 static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
308 {
309         int ae;
310
311         ae = bq27x00_read(di, BQ27x00_REG_AE, false);
312         if (ae < 0) {
313                 dev_dbg(di->dev, "error reading available energy\n");
314                 return ae;
315         }
316
317         if (di->chip == BQ27500)
318                 ae *= 1000;
319         else
320                 ae = ae * 29200 / BQ27000_RS;
321
322         return ae;
323 }
324
325 /*
326  * Return the battery temperature in tenths of degree Kelvin
327  * Or < 0 if something fails.
328  */
329 static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
330 {
331         int temp;
332
333         temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
334         if (temp < 0) {
335                 dev_err(di->dev, "error reading temperature\n");
336                 return temp;
337         }
338
339         if (!bq27xxx_is_chip_version_higher(di))
340                 temp = 5 * temp / 2;
341
342         return temp;
343 }
344
345 /*
346  * Return the battery Cycle count total
347  * Or < 0 if something fails.
348  */
349 static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
350 {
351         int cyct;
352
353         cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
354         if (cyct < 0)
355                 dev_err(di->dev, "error reading cycle count total\n");
356
357         return cyct;
358 }
359
360 /*
361  * Read a time register.
362  * Return < 0 if something fails.
363  */
364 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
365 {
366         int tval;
367
368         tval = bq27x00_read(di, reg, false);
369         if (tval < 0) {
370                 dev_dbg(di->dev, "error reading time register %02x: %d\n",
371                         reg, tval);
372                 return tval;
373         }
374
375         if (tval == 65535)
376                 return -ENODATA;
377
378         return tval * 60;
379 }
380
381 /*
382  * Read a power avg register.
383  * Return < 0 if something fails.
384  */
385 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
386 {
387         int tval;
388
389         tval = bq27x00_read(di, reg, false);
390         if (tval < 0) {
391                 dev_err(di->dev, "error reading power avg rgister  %02x: %d\n",
392                         reg, tval);
393                 return tval;
394         }
395
396         if (di->chip == BQ27500)
397                 return tval;
398         else
399                 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
400 }
401
402 /*
403  * Read flag register.
404  * Return < 0 if something fails.
405  */
406 static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
407 {
408         int tval;
409
410         tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
411         if (tval < 0) {
412                 dev_err(di->dev, "error reading flag register:%d\n", tval);
413                 return tval;
414         }
415
416         if ((di->chip == BQ27500)) {
417                 if (tval & BQ27500_FLAG_SOCF)
418                         tval = POWER_SUPPLY_HEALTH_DEAD;
419                 else if (tval & BQ27500_FLAG_OTC)
420                         tval = POWER_SUPPLY_HEALTH_OVERHEAT;
421                 else
422                         tval = POWER_SUPPLY_HEALTH_GOOD;
423                 return tval;
424         } else {
425                 if (tval & BQ27000_FLAG_EDV1)
426                         tval = POWER_SUPPLY_HEALTH_DEAD;
427                 else
428                         tval = POWER_SUPPLY_HEALTH_GOOD;
429                 return tval;
430         }
431
432         return -1;
433 }
434
435 static void bq27x00_update(struct bq27x00_device_info *di)
436 {
437         struct bq27x00_reg_cache cache = {0, };
438         bool is_bq27500 = di->chip == BQ27500;
439         bool is_bq27425 = di->chip == BQ27425;
440         bool is_bq27742 = di->chip == BQ27742;
441         bool flags_1b = !(is_bq27500 || is_bq27742);
442
443         cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
444         if ((cache.flags & 0xff) == 0xff)
445                 /* read error */
446                 cache.flags = -1;
447         if (cache.flags >= 0) {
448                 if (!is_bq27500 && !is_bq27425 && !is_bq27742
449                                 && (cache.flags & BQ27000_FLAG_CI)) {
450                         dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
451                         cache.capacity = -ENODATA;
452                         cache.energy = -ENODATA;
453                         cache.time_to_empty = -ENODATA;
454                         cache.time_to_empty_avg = -ENODATA;
455                         cache.time_to_full = -ENODATA;
456                         cache.charge_full = -ENODATA;
457                         cache.health = -ENODATA;
458                 } else {
459                         cache.capacity = bq27x00_battery_read_rsoc(di);
460                         if (is_bq27742)
461                                 cache.time_to_empty =
462                                         bq27x00_battery_read_time(di,
463                                                         BQ27x00_REG_TTE);
464                         else if (!is_bq27425) {
465                                 cache.energy = bq27x00_battery_read_energy(di);
466                                 cache.time_to_empty =
467                                         bq27x00_battery_read_time(di,
468                                                         BQ27x00_REG_TTE);
469                                 cache.time_to_empty_avg =
470                                         bq27x00_battery_read_time(di,
471                                                         BQ27x00_REG_TTECP);
472                                 cache.time_to_full =
473                                         bq27x00_battery_read_time(di,
474                                                         BQ27x00_REG_TTF);
475                         }
476                         cache.charge_full = bq27x00_battery_read_lmd(di);
477                         cache.health = bq27x00_battery_read_health(di);
478                 }
479                 cache.temperature = bq27x00_battery_read_temperature(di);
480                 if (!is_bq27425)
481                         cache.cycle_count = bq27x00_battery_read_cyct(di);
482                 if (is_bq27742)
483                         cache.power_avg =
484                                 bq27x00_battery_read_pwr_avg(di,
485                                                 BQ27742_POWER_AVG);
486                 else
487                         cache.power_avg =
488                                 bq27x00_battery_read_pwr_avg(di,
489                                                 BQ27x00_POWER_AVG);
490
491                 /* We only have to read charge design full once */
492                 if (di->charge_design_full <= 0)
493                         di->charge_design_full = bq27x00_battery_read_ilmd(di);
494         }
495
496         if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
497                 di->cache = cache;
498                 power_supply_changed(&di->bat);
499         }
500
501         di->last_update = jiffies;
502 }
503
504 static void bq27x00_battery_poll(struct work_struct *work)
505 {
506         struct bq27x00_device_info *di =
507                 container_of(work, struct bq27x00_device_info, work.work);
508
509         bq27x00_update(di);
510
511         if (poll_interval > 0) {
512                 /* The timer does not have to be accurate. */
513                 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
514                 schedule_delayed_work(&di->work, poll_interval * HZ);
515         }
516 }
517
518 /*
519  * Return the battery average current in µA
520  * Note that current can be negative signed as well
521  * Or 0 if something fails.
522  */
523 static int bq27x00_battery_current(struct bq27x00_device_info *di,
524         union power_supply_propval *val)
525 {
526         int curr;
527         int flags;
528
529         curr = bq27x00_read(di, BQ27x00_REG_AI, false);
530         if (curr < 0) {
531                 dev_err(di->dev, "error reading current\n");
532                 return curr;
533         }
534
535         if (bq27xxx_is_chip_version_higher(di)) {
536                 /* bq27500 returns signed value */
537                 val->intval = (int)((s16)curr) * 1000;
538         } else {
539                 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
540                 if (flags & BQ27000_FLAG_CHGS) {
541                         dev_dbg(di->dev, "negative current!\n");
542                         curr = -curr;
543                 }
544
545                 val->intval = curr * 3570 / BQ27000_RS;
546         }
547
548         return 0;
549 }
550
551 static int bq27x00_battery_status(struct bq27x00_device_info *di,
552         union power_supply_propval *val)
553 {
554         int status;
555
556         if (bq27xxx_is_chip_version_higher(di)) {
557                 if (di->cache.flags & BQ27500_FLAG_FC)
558                         status = POWER_SUPPLY_STATUS_FULL;
559                 else if (di->cache.flags & BQ27500_FLAG_DSC)
560                         status = POWER_SUPPLY_STATUS_DISCHARGING;
561                 else
562                         status = POWER_SUPPLY_STATUS_CHARGING;
563         } else {
564                 if (di->cache.flags & BQ27000_FLAG_FC)
565                         status = POWER_SUPPLY_STATUS_FULL;
566                 else if (di->cache.flags & BQ27000_FLAG_CHGS)
567                         status = POWER_SUPPLY_STATUS_CHARGING;
568                 else if (power_supply_am_i_supplied(&di->bat))
569                         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
570                 else
571                         status = POWER_SUPPLY_STATUS_DISCHARGING;
572         }
573
574         val->intval = status;
575
576         return 0;
577 }
578
579 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
580         union power_supply_propval *val)
581 {
582         int level;
583
584         if (bq27xxx_is_chip_version_higher(di)) {
585                 if (di->cache.flags & BQ27500_FLAG_FC)
586                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
587                 else if (di->cache.flags & BQ27500_FLAG_SOC1)
588                         level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
589                 else if (di->cache.flags & BQ27500_FLAG_SOCF)
590                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
591                 else
592                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
593         } else {
594                 if (di->cache.flags & BQ27000_FLAG_FC)
595                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
596                 else if (di->cache.flags & BQ27000_FLAG_EDV1)
597                         level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
598                 else if (di->cache.flags & BQ27000_FLAG_EDVF)
599                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
600                 else
601                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
602         }
603
604         val->intval = level;
605
606         return 0;
607 }
608
609 /*
610  * Return the battery Voltage in millivolts
611  * Or < 0 if something fails.
612  */
613 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
614         union power_supply_propval *val)
615 {
616         int volt;
617
618         volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
619         if (volt < 0) {
620                 dev_err(di->dev, "error reading voltage\n");
621                 return volt;
622         }
623
624         val->intval = volt * 1000;
625
626         return 0;
627 }
628
629 static int bq27x00_simple_value(int value,
630         union power_supply_propval *val)
631 {
632         if (value < 0)
633                 return value;
634
635         val->intval = value;
636
637         return 0;
638 }
639
640 #define to_bq27x00_device_info(x) container_of((x), \
641                                 struct bq27x00_device_info, bat);
642
643 static int bq27x00_battery_get_property(struct power_supply *psy,
644                                         enum power_supply_property psp,
645                                         union power_supply_propval *val)
646 {
647         int ret = 0;
648         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
649
650         mutex_lock(&di->lock);
651         if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
652                 cancel_delayed_work_sync(&di->work);
653                 bq27x00_battery_poll(&di->work.work);
654         }
655         mutex_unlock(&di->lock);
656
657         if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
658                 return -ENODEV;
659
660         switch (psp) {
661         case POWER_SUPPLY_PROP_STATUS:
662                 ret = bq27x00_battery_status(di, val);
663                 break;
664         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
665                 ret = bq27x00_battery_voltage(di, val);
666                 break;
667         case POWER_SUPPLY_PROP_PRESENT:
668                 val->intval = di->cache.flags < 0 ? 0 : 1;
669                 break;
670         case POWER_SUPPLY_PROP_CURRENT_NOW:
671                 ret = bq27x00_battery_current(di, val);
672                 break;
673         case POWER_SUPPLY_PROP_CAPACITY:
674                 ret = bq27x00_simple_value(di->cache.capacity, val);
675                 break;
676         case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
677                 ret = bq27x00_battery_capacity_level(di, val);
678                 break;
679         case POWER_SUPPLY_PROP_TEMP:
680                 ret = bq27x00_simple_value(di->cache.temperature, val);
681                 if (ret == 0)
682                         val->intval -= 2731;
683                 break;
684         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
685                 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
686                 break;
687         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
688                 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
689                 break;
690         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
691                 ret = bq27x00_simple_value(di->cache.time_to_full, val);
692                 break;
693         case POWER_SUPPLY_PROP_TECHNOLOGY:
694                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
695                 break;
696         case POWER_SUPPLY_PROP_CHARGE_NOW:
697                 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
698                 break;
699         case POWER_SUPPLY_PROP_CHARGE_FULL:
700                 ret = bq27x00_simple_value(di->cache.charge_full, val);
701                 break;
702         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
703                 ret = bq27x00_simple_value(di->charge_design_full, val);
704                 break;
705         case POWER_SUPPLY_PROP_CYCLE_COUNT:
706                 ret = bq27x00_simple_value(di->cache.cycle_count, val);
707                 break;
708         case POWER_SUPPLY_PROP_ENERGY_NOW:
709                 ret = bq27x00_simple_value(di->cache.energy, val);
710                 break;
711         case POWER_SUPPLY_PROP_POWER_AVG:
712                 ret = bq27x00_simple_value(di->cache.power_avg, val);
713                 break;
714         case POWER_SUPPLY_PROP_HEALTH:
715                 ret = bq27x00_simple_value(di->cache.health, val);
716                 break;
717         default:
718                 return -EINVAL;
719         }
720
721         return ret;
722 }
723
724 static void bq27x00_external_power_changed(struct power_supply *psy)
725 {
726         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
727
728         cancel_delayed_work_sync(&di->work);
729         schedule_delayed_work(&di->work, 0);
730 }
731
732 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
733 {
734         int ret;
735
736         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
737         if (di->chip == BQ27425) {
738                 di->bat.properties = bq27425_battery_props;
739                 di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
740         } else if (di->chip == BQ27742) {
741                 di->bat.properties = bq27742_battery_props;
742                 di->bat.num_properties = ARRAY_SIZE(bq27742_battery_props);
743         } else {
744                 di->bat.properties = bq27x00_battery_props;
745                 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
746         }
747         di->bat.get_property = bq27x00_battery_get_property;
748         di->bat.external_power_changed = bq27x00_external_power_changed;
749
750         INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
751         mutex_init(&di->lock);
752
753         ret = power_supply_register(di->dev, &di->bat);
754         if (ret) {
755                 dev_err(di->dev, "failed to register battery: %d\n", ret);
756                 return ret;
757         }
758
759         dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
760
761         bq27x00_update(di);
762
763         return 0;
764 }
765
766 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
767 {
768         /*
769          * power_supply_unregister call bq27x00_battery_get_property which
770          * call bq27x00_battery_poll.
771          * Make sure that bq27x00_battery_poll will not call
772          * schedule_delayed_work again after unregister (which cause OOPS).
773          */
774         poll_interval = 0;
775
776         cancel_delayed_work_sync(&di->work);
777
778         power_supply_unregister(&di->bat);
779
780         mutex_destroy(&di->lock);
781 }
782
783
784 /* i2c specific code */
785 #ifdef CONFIG_BATTERY_BQ27X00_I2C
786
787 /* If the system has several batteries we need a different name for each
788  * of them...
789  */
790 static DEFINE_IDR(battery_id);
791 static DEFINE_MUTEX(battery_mutex);
792
793 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
794 {
795         struct i2c_client *client = to_i2c_client(di->dev);
796         struct i2c_msg msg[2];
797         unsigned char data[2];
798         int ret;
799
800         if (!client->adapter)
801                 return -ENODEV;
802
803         msg[0].addr = client->addr;
804         msg[0].flags = 0;
805         msg[0].buf = &reg;
806         msg[0].len = sizeof(reg);
807         msg[1].addr = client->addr;
808         msg[1].flags = I2C_M_RD;
809         msg[1].buf = data;
810         if (single)
811                 msg[1].len = 1;
812         else
813                 msg[1].len = 2;
814
815         ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
816         if (ret < 0)
817                 return ret;
818
819         if (!single)
820                 ret = get_unaligned_le16(data);
821         else
822                 ret = data[0];
823
824         return ret;
825 }
826
827 static int bq27x00_battery_probe(struct i2c_client *client,
828                                  const struct i2c_device_id *id)
829 {
830         char *name;
831         struct bq27x00_device_info *di;
832         int num;
833         int retval = 0;
834
835         /* Get new ID for the new battery device */
836         mutex_lock(&battery_mutex);
837         num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
838         mutex_unlock(&battery_mutex);
839         if (num < 0)
840                 return num;
841
842         name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
843         if (!name) {
844                 dev_err(&client->dev, "failed to allocate device name\n");
845                 retval = -ENOMEM;
846                 goto batt_failed_1;
847         }
848
849         di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
850         if (!di) {
851                 dev_err(&client->dev, "failed to allocate device info data\n");
852                 retval = -ENOMEM;
853                 goto batt_failed_2;
854         }
855
856         di->id = num;
857         di->dev = &client->dev;
858         di->chip = id->driver_data;
859         di->bat.name = name;
860         di->bus.read = &bq27x00_read_i2c;
861
862         retval = bq27x00_powersupply_init(di);
863         if (retval)
864                 goto batt_failed_2;
865
866         i2c_set_clientdata(client, di);
867
868         return 0;
869
870 batt_failed_2:
871         kfree(name);
872 batt_failed_1:
873         mutex_lock(&battery_mutex);
874         idr_remove(&battery_id, num);
875         mutex_unlock(&battery_mutex);
876
877         return retval;
878 }
879
880 static int bq27x00_battery_remove(struct i2c_client *client)
881 {
882         struct bq27x00_device_info *di = i2c_get_clientdata(client);
883
884         bq27x00_powersupply_unregister(di);
885
886         kfree(di->bat.name);
887
888         mutex_lock(&battery_mutex);
889         idr_remove(&battery_id, di->id);
890         mutex_unlock(&battery_mutex);
891
892         return 0;
893 }
894
895 static const struct i2c_device_id bq27x00_id[] = {
896         { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
897         { "bq27500", BQ27500 },
898         { "bq27425", BQ27425 },
899         { "bq27742", BQ27742 },
900         {},
901 };
902 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
903
904 static struct i2c_driver bq27x00_battery_driver = {
905         .driver = {
906                 .name = "bq27x00-battery",
907         },
908         .probe = bq27x00_battery_probe,
909         .remove = bq27x00_battery_remove,
910         .id_table = bq27x00_id,
911 };
912
913 static inline int bq27x00_battery_i2c_init(void)
914 {
915         int ret = i2c_add_driver(&bq27x00_battery_driver);
916         if (ret)
917                 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
918
919         return ret;
920 }
921
922 static inline void bq27x00_battery_i2c_exit(void)
923 {
924         i2c_del_driver(&bq27x00_battery_driver);
925 }
926
927 #else
928
929 static inline int bq27x00_battery_i2c_init(void) { return 0; }
930 static inline void bq27x00_battery_i2c_exit(void) {};
931
932 #endif
933
934 /* platform specific code */
935 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
936
937 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
938                         bool single)
939 {
940         struct device *dev = di->dev;
941         struct bq27000_platform_data *pdata = dev->platform_data;
942         unsigned int timeout = 3;
943         int upper, lower;
944         int temp;
945
946         if (!single) {
947                 /* Make sure the value has not changed in between reading the
948                  * lower and the upper part */
949                 upper = pdata->read(dev, reg + 1);
950                 do {
951                         temp = upper;
952                         if (upper < 0)
953                                 return upper;
954
955                         lower = pdata->read(dev, reg);
956                         if (lower < 0)
957                                 return lower;
958
959                         upper = pdata->read(dev, reg + 1);
960                 } while (temp != upper && --timeout);
961
962                 if (timeout == 0)
963                         return -EIO;
964
965                 return (upper << 8) | lower;
966         }
967
968         return pdata->read(dev, reg);
969 }
970
971 static int bq27000_battery_probe(struct platform_device *pdev)
972 {
973         struct bq27x00_device_info *di;
974         struct bq27000_platform_data *pdata = pdev->dev.platform_data;
975
976         if (!pdata) {
977                 dev_err(&pdev->dev, "no platform_data supplied\n");
978                 return -EINVAL;
979         }
980
981         if (!pdata->read) {
982                 dev_err(&pdev->dev, "no hdq read callback supplied\n");
983                 return -EINVAL;
984         }
985
986         di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
987         if (!di) {
988                 dev_err(&pdev->dev, "failed to allocate device info data\n");
989                 return -ENOMEM;
990         }
991
992         platform_set_drvdata(pdev, di);
993
994         di->dev = &pdev->dev;
995         di->chip = BQ27000;
996
997         di->bat.name = pdata->name ?: dev_name(&pdev->dev);
998         di->bus.read = &bq27000_read_platform;
999
1000         return bq27x00_powersupply_init(di);
1001 }
1002
1003 static int bq27000_battery_remove(struct platform_device *pdev)
1004 {
1005         struct bq27x00_device_info *di = platform_get_drvdata(pdev);
1006
1007         bq27x00_powersupply_unregister(di);
1008
1009         return 0;
1010 }
1011
1012 static struct platform_driver bq27000_battery_driver = {
1013         .probe  = bq27000_battery_probe,
1014         .remove = bq27000_battery_remove,
1015         .driver = {
1016                 .name = "bq27000-battery",
1017                 .owner = THIS_MODULE,
1018         },
1019 };
1020
1021 static inline int bq27x00_battery_platform_init(void)
1022 {
1023         int ret = platform_driver_register(&bq27000_battery_driver);
1024         if (ret)
1025                 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
1026
1027         return ret;
1028 }
1029
1030 static inline void bq27x00_battery_platform_exit(void)
1031 {
1032         platform_driver_unregister(&bq27000_battery_driver);
1033 }
1034
1035 #else
1036
1037 static inline int bq27x00_battery_platform_init(void) { return 0; }
1038 static inline void bq27x00_battery_platform_exit(void) {};
1039
1040 #endif
1041
1042 /*
1043  * Module stuff
1044  */
1045
1046 static int __init bq27x00_battery_init(void)
1047 {
1048         int ret;
1049
1050         ret = bq27x00_battery_i2c_init();
1051         if (ret)
1052                 return ret;
1053
1054         ret = bq27x00_battery_platform_init();
1055         if (ret)
1056                 bq27x00_battery_i2c_exit();
1057
1058         return ret;
1059 }
1060 module_init(bq27x00_battery_init);
1061
1062 static void __exit bq27x00_battery_exit(void)
1063 {
1064         bq27x00_battery_platform_exit();
1065         bq27x00_battery_i2c_exit();
1066 }
1067 module_exit(bq27x00_battery_exit);
1068
1069 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1070 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1071 MODULE_LICENSE("GPL");