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