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://www.ti.com/product/bq27000
21 * http://www.ti.com/product/bq27200
22 * http://www.ti.com/product/bq27010
23 * http://www.ti.com/product/bq27210
24 * http://www.ti.com/product/bq27500
25 * http://www.ti.com/product/bq27510-g3
26 * http://www.ti.com/product/bq27520-g4
27 * http://www.ti.com/product/bq27530-g1
28 * http://www.ti.com/product/bq27531-g1
29 * http://www.ti.com/product/bq27541-g1
30 * http://www.ti.com/product/bq27542-g1
31 * http://www.ti.com/product/bq27546-g1
32 * http://www.ti.com/product/bq27742-g1
33 * http://www.ti.com/product/bq27545-g1
34 * http://www.ti.com/product/bq27421-g1
35 * http://www.ti.com/product/bq27425-g1
36 * http://www.ti.com/product/bq27411-g1
37 * http://www.ti.com/product/bq27621-g1
38 */
39
40 #include <linux/device.h>
41 #include <linux/module.h>
42 #include <linux/param.h>
43 #include <linux/jiffies.h>
44 #include <linux/workqueue.h>
45 #include <linux/delay.h>
46 #include <linux/platform_device.h>
47 #include <linux/power_supply.h>
48 #include <linux/idr.h>
49 #include <linux/i2c.h>
50 #include <linux/slab.h>
51 #include <linux/interrupt.h>
52 #include <asm/unaligned.h>
53
54 #include <linux/power/bq27xxx_battery.h>
55
56 #define DRIVER_VERSION "1.2.0"
57
58 #define BQ27XXX_MANUFACTURER "Texas Instruments"
59
60 /* BQ27XXX Flags */
61 #define BQ27XXX_FLAG_DSC BIT(0)
62 #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
63 #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
64 #define BQ27XXX_FLAG_FC BIT(9)
65 #define BQ27XXX_FLAG_OTD BIT(14)
66 #define BQ27XXX_FLAG_OTC BIT(15)
67 #define BQ27XXX_FLAG_UT BIT(14)
68 #define BQ27XXX_FLAG_OT BIT(15)
69
70 /* BQ27000 has different layout for Flags register */
71 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
72 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
73 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
74 #define BQ27000_FLAG_FC BIT(5)
75 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
76
77 #define BQ27XXX_RS (20) /* Resistor sense mOhm */
78 #define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
79 #define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
80
81 struct bq27xxx_device_info;
82 struct bq27xxx_access_methods {
83 int (*read)(struct bq27xxx_device_info *di, u8 reg, bool single);
84 };
85
86 #define INVALID_REG_ADDR 0xff
87
88 /*
89 * bq27xxx_reg_index - Register names
90 *
91 * These are indexes into a device's register mapping array.
92 */
93 enum bq27xxx_reg_index {
94 BQ27XXX_REG_CTRL = 0, /* Control */
95 BQ27XXX_REG_TEMP, /* Temperature */
96 BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
97 BQ27XXX_REG_VOLT, /* Voltage */
98 BQ27XXX_REG_AI, /* Average Current */
99 BQ27XXX_REG_FLAGS, /* Flags */
100 BQ27XXX_REG_TTE, /* Time-to-Empty */
101 BQ27XXX_REG_TTF, /* Time-to-Full */
102 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
103 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
104 BQ27XXX_REG_NAC, /* Nominal Available Capacity */
105 BQ27XXX_REG_FCC, /* Full Charge Capacity */
106 BQ27XXX_REG_CYCT, /* Cycle Count */
107 BQ27XXX_REG_AE, /* Available Energy */
108 BQ27XXX_REG_SOC, /* State-of-Charge */
109 BQ27XXX_REG_DCAP, /* Design Capacity */
110 BQ27XXX_REG_AP, /* Average Power */
111 };
112
113 struct bq27xxx_reg_cache {
114 int temperature;
115 int time_to_empty;
116 int time_to_empty_avg;
117 int time_to_full;
118 int charge_full;
119 int cycle_count;
120 int capacity;
121 int energy;
122 int flags;
123 int power_avg;
124 int health;
125 };
126
127 struct bq27xxx_device_info {
128 struct device *dev;
129 int id;
130 enum bq27xxx_chip chip;
131
132 struct bq27xxx_reg_cache cache;
133 int charge_design_full;
134
135 unsigned long last_update;
136 struct delayed_work work;
137
138 struct power_supply *bat;
139
140 struct bq27xxx_access_methods bus;
141
142 struct mutex lock;
143
144 u8 *regs;
145 };
146
147 /* Register mappings */
148 static u8 bq27000_regs[] = {
149 0x00, /* CONTROL */
150 0x06, /* TEMP */
151 INVALID_REG_ADDR, /* INT TEMP - NA*/
152 0x08, /* VOLT */
153 0x14, /* AVG CURR */
154 0x0a, /* FLAGS */
155 0x16, /* TTE */
156 0x18, /* TTF */
157 0x1c, /* TTES */
158 0x26, /* TTECP */
159 0x0c, /* NAC */
160 0x12, /* LMD(FCC) */
161 0x2a, /* CYCT */
162 0x22, /* AE */
163 0x0b, /* SOC(RSOC) */
164 0x76, /* DCAP(ILMD) */
165 0x24, /* AP */
166 };
167
168 static u8 bq27010_regs[] = {
169 0x00, /* CONTROL */
170 0x06, /* TEMP */
171 INVALID_REG_ADDR, /* INT TEMP - NA*/
172 0x08, /* VOLT */
173 0x14, /* AVG CURR */
174 0x0a, /* FLAGS */
175 0x16, /* TTE */
176 0x18, /* TTF */
177 0x1c, /* TTES */
178 0x26, /* TTECP */
179 0x0c, /* NAC */
180 0x12, /* LMD(FCC) */
181 0x2a, /* CYCT */
182 INVALID_REG_ADDR, /* AE - NA */
183 0x0b, /* SOC(RSOC) */
184 0x76, /* DCAP(ILMD) */
185 INVALID_REG_ADDR, /* AP - NA */
186 };
187
188 static u8 bq27500_regs[] = {
189 0x00, /* CONTROL */
190 0x06, /* TEMP */
191 0x28, /* INT TEMP */
192 0x08, /* VOLT */
193 0x14, /* AVG CURR */
194 0x0a, /* FLAGS */
195 0x16, /* TTE */
196 INVALID_REG_ADDR, /* TTF - NA */
197 0x1a, /* TTES */
198 INVALID_REG_ADDR, /* TTECP - NA */
199 0x0c, /* NAC */
200 0x12, /* LMD(FCC) */
201 0x2a, /* CYCT */
202 INVALID_REG_ADDR, /* AE - NA */
203 0x2c, /* SOC(RSOC) */
204 0x3c, /* DCAP(ILMD) */
205 INVALID_REG_ADDR, /* AP - NA */
206 };
207
208 static u8 bq27530_regs[] = {
209 0x00, /* CONTROL */
210 0x06, /* TEMP */
211 0x32, /* INT TEMP */
212 0x08, /* VOLT */
213 0x14, /* AVG CURR */
214 0x0a, /* FLAGS */
215 0x16, /* TTE */
216 INVALID_REG_ADDR, /* TTF - NA */
217 INVALID_REG_ADDR, /* TTES - NA */
218 INVALID_REG_ADDR, /* TTECP - NA */
219 0x0c, /* NAC */
220 0x12, /* LMD(FCC) */
221 0x2a, /* CYCT */
222 INVALID_REG_ADDR, /* AE - NA */
223 0x2c, /* SOC(RSOC) */
224 INVALID_REG_ADDR, /* DCAP - NA */
225 0x24, /* AP */
226 };
227
228 static u8 bq27541_regs[] = {
229 0x00, /* CONTROL */
230 0x06, /* TEMP */
231 0x28, /* INT TEMP */
232 0x08, /* VOLT */
233 0x14, /* AVG CURR */
234 0x0a, /* FLAGS */
235 0x16, /* TTE */
236 INVALID_REG_ADDR, /* TTF - NA */
237 INVALID_REG_ADDR, /* TTES - NA */
238 INVALID_REG_ADDR, /* TTECP - NA */
239 0x0c, /* NAC */
240 0x12, /* LMD(FCC) */
241 0x2a, /* CYCT */
242 INVALID_REG_ADDR, /* AE - NA */
243 0x2c, /* SOC(RSOC) */
244 0x3c, /* DCAP */
245 0x24, /* AP */
246 };
247
248 static u8 bq27545_regs[] = {
249 0x00, /* CONTROL */
250 0x06, /* TEMP */
251 0x28, /* INT TEMP */
252 0x08, /* VOLT */
253 0x14, /* AVG CURR */
254 0x0a, /* FLAGS */
255 0x16, /* TTE */
256 INVALID_REG_ADDR, /* TTF - NA */
257 INVALID_REG_ADDR, /* TTES - NA */
258 INVALID_REG_ADDR, /* TTECP - NA */
259 0x0c, /* NAC */
260 0x12, /* LMD(FCC) */
261 0x2a, /* CYCT */
262 INVALID_REG_ADDR, /* AE - NA */
263 0x2c, /* SOC(RSOC) */
264 INVALID_REG_ADDR, /* DCAP - NA */
265 0x24, /* AP */
266 };
267
268 static u8 bq27421_regs[] = {
269 0x00, /* CONTROL */
270 0x02, /* TEMP */
271 0x1e, /* INT TEMP */
272 0x04, /* VOLT */
273 0x10, /* AVG CURR */
274 0x06, /* FLAGS */
275 INVALID_REG_ADDR, /* TTE - NA */
276 INVALID_REG_ADDR, /* TTF - NA */
277 INVALID_REG_ADDR, /* TTES - NA */
278 INVALID_REG_ADDR, /* TTECP - NA */
279 0x08, /* NAC */
280 0x0e, /* FCC */
281 INVALID_REG_ADDR, /* CYCT - NA */
282 INVALID_REG_ADDR, /* AE - NA */
283 0x1c, /* SOC */
284 0x3c, /* DCAP */
285 0x18, /* AP */
286 };
287
288 static u8 *bq27xxx_regs[] __maybe_unused = {
289 [BQ27000] = bq27000_regs,
290 [BQ27010] = bq27010_regs,
291 [BQ27500] = bq27500_regs,
292 [BQ27530] = bq27530_regs,
293 [BQ27541] = bq27541_regs,
294 [BQ27545] = bq27545_regs,
295 [BQ27421] = bq27421_regs,
296 };
297
298 static enum power_supply_property bq27000_battery_props[] = {
299 POWER_SUPPLY_PROP_STATUS,
300 POWER_SUPPLY_PROP_PRESENT,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW,
302 POWER_SUPPLY_PROP_CURRENT_NOW,
303 POWER_SUPPLY_PROP_CAPACITY,
304 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
305 POWER_SUPPLY_PROP_TEMP,
306 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
307 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
308 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
309 POWER_SUPPLY_PROP_TECHNOLOGY,
310 POWER_SUPPLY_PROP_CHARGE_FULL,
311 POWER_SUPPLY_PROP_CHARGE_NOW,
312 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
313 POWER_SUPPLY_PROP_CYCLE_COUNT,
314 POWER_SUPPLY_PROP_ENERGY_NOW,
315 POWER_SUPPLY_PROP_POWER_AVG,
316 POWER_SUPPLY_PROP_HEALTH,
317 POWER_SUPPLY_PROP_MANUFACTURER,
318 };
319
320 static enum power_supply_property bq27010_battery_props[] = {
321 POWER_SUPPLY_PROP_STATUS,
322 POWER_SUPPLY_PROP_PRESENT,
323 POWER_SUPPLY_PROP_VOLTAGE_NOW,
324 POWER_SUPPLY_PROP_CURRENT_NOW,
325 POWER_SUPPLY_PROP_CAPACITY,
326 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
327 POWER_SUPPLY_PROP_TEMP,
328 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
329 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
330 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
331 POWER_SUPPLY_PROP_TECHNOLOGY,
332 POWER_SUPPLY_PROP_CHARGE_FULL,
333 POWER_SUPPLY_PROP_CHARGE_NOW,
334 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
335 POWER_SUPPLY_PROP_CYCLE_COUNT,
336 POWER_SUPPLY_PROP_HEALTH,
337 POWER_SUPPLY_PROP_MANUFACTURER,
338 };
339
340 static enum power_supply_property bq27500_battery_props[] = {
341 POWER_SUPPLY_PROP_STATUS,
342 POWER_SUPPLY_PROP_PRESENT,
343 POWER_SUPPLY_PROP_VOLTAGE_NOW,
344 POWER_SUPPLY_PROP_CURRENT_NOW,
345 POWER_SUPPLY_PROP_CAPACITY,
346 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
347 POWER_SUPPLY_PROP_TEMP,
348 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
349 POWER_SUPPLY_PROP_TECHNOLOGY,
350 POWER_SUPPLY_PROP_CHARGE_FULL,
351 POWER_SUPPLY_PROP_CHARGE_NOW,
352 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
353 POWER_SUPPLY_PROP_CYCLE_COUNT,
354 POWER_SUPPLY_PROP_HEALTH,
355 POWER_SUPPLY_PROP_MANUFACTURER,
356 };
357
358 static enum power_supply_property bq27530_battery_props[] = {
359 POWER_SUPPLY_PROP_STATUS,
360 POWER_SUPPLY_PROP_PRESENT,
361 POWER_SUPPLY_PROP_VOLTAGE_NOW,
362 POWER_SUPPLY_PROP_CURRENT_NOW,
363 POWER_SUPPLY_PROP_CAPACITY,
364 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
365 POWER_SUPPLY_PROP_TEMP,
366 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
367 POWER_SUPPLY_PROP_TECHNOLOGY,
368 POWER_SUPPLY_PROP_CHARGE_FULL,
369 POWER_SUPPLY_PROP_CHARGE_NOW,
370 POWER_SUPPLY_PROP_POWER_AVG,
371 POWER_SUPPLY_PROP_HEALTH,
372 POWER_SUPPLY_PROP_CYCLE_COUNT,
373 POWER_SUPPLY_PROP_MANUFACTURER,
374 };
375
376 static enum power_supply_property bq27541_battery_props[] = {
377 POWER_SUPPLY_PROP_STATUS,
378 POWER_SUPPLY_PROP_PRESENT,
379 POWER_SUPPLY_PROP_VOLTAGE_NOW,
380 POWER_SUPPLY_PROP_CURRENT_NOW,
381 POWER_SUPPLY_PROP_CAPACITY,
382 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
383 POWER_SUPPLY_PROP_TEMP,
384 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
385 POWER_SUPPLY_PROP_TECHNOLOGY,
386 POWER_SUPPLY_PROP_CHARGE_FULL,
387 POWER_SUPPLY_PROP_CHARGE_NOW,
388 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
389 POWER_SUPPLY_PROP_CYCLE_COUNT,
390 POWER_SUPPLY_PROP_POWER_AVG,
391 POWER_SUPPLY_PROP_HEALTH,
392 POWER_SUPPLY_PROP_MANUFACTURER,
393 };
394
395 static enum power_supply_property bq27545_battery_props[] = {
396 POWER_SUPPLY_PROP_STATUS,
397 POWER_SUPPLY_PROP_PRESENT,
398 POWER_SUPPLY_PROP_VOLTAGE_NOW,
399 POWER_SUPPLY_PROP_CURRENT_NOW,
400 POWER_SUPPLY_PROP_CAPACITY,
401 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
402 POWER_SUPPLY_PROP_TEMP,
403 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
404 POWER_SUPPLY_PROP_TECHNOLOGY,
405 POWER_SUPPLY_PROP_CHARGE_FULL,
406 POWER_SUPPLY_PROP_CHARGE_NOW,
407 POWER_SUPPLY_PROP_HEALTH,
408 POWER_SUPPLY_PROP_CYCLE_COUNT,
409 POWER_SUPPLY_PROP_POWER_AVG,
410 POWER_SUPPLY_PROP_MANUFACTURER,
411 };
412
413 static enum power_supply_property bq27421_battery_props[] = {
414 POWER_SUPPLY_PROP_STATUS,
415 POWER_SUPPLY_PROP_PRESENT,
416 POWER_SUPPLY_PROP_VOLTAGE_NOW,
417 POWER_SUPPLY_PROP_CURRENT_NOW,
418 POWER_SUPPLY_PROP_CAPACITY,
419 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
420 POWER_SUPPLY_PROP_TEMP,
421 POWER_SUPPLY_PROP_TECHNOLOGY,
422 POWER_SUPPLY_PROP_CHARGE_FULL,
423 POWER_SUPPLY_PROP_CHARGE_NOW,
424 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
425 POWER_SUPPLY_PROP_MANUFACTURER,
426 };
427
428 #define BQ27XXX_PROP(_id, _prop) \
429 [_id] = { \
430 .props = _prop, \
431 .size = ARRAY_SIZE(_prop), \
432 }
433
434 static struct {
435 enum power_supply_property *props;
436 size_t size;
437 } bq27xxx_battery_props[] = {
438 BQ27XXX_PROP(BQ27000, bq27000_battery_props),
439 BQ27XXX_PROP(BQ27010, bq27010_battery_props),
440 BQ27XXX_PROP(BQ27500, bq27500_battery_props),
441 BQ27XXX_PROP(BQ27530, bq27530_battery_props),
442 BQ27XXX_PROP(BQ27541, bq27541_battery_props),
443 BQ27XXX_PROP(BQ27545, bq27545_battery_props),
444 BQ27XXX_PROP(BQ27421, bq27421_battery_props),
445 };
446
447 static unsigned int poll_interval = 360;
448 module_param(poll_interval, uint, 0644);
449 MODULE_PARM_DESC(poll_interval,
450 "battery poll interval in seconds - 0 disables polling");
451
452 /*
453 * Common code for BQ27xxx devices
454 */
455
bq27xxx_read(struct bq27xxx_device_info * di,int reg_index,bool single)456 static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
457 bool single)
458 {
459 /* Reports EINVAL for invalid/missing registers */
460 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
461 return -EINVAL;
462
463 return di->bus.read(di, di->regs[reg_index], single);
464 }
465
466 /*
467 * Return the battery State-of-Charge
468 * Or < 0 if something fails.
469 */
bq27xxx_battery_read_soc(struct bq27xxx_device_info * di)470 static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
471 {
472 int soc;
473
474 if (di->chip == BQ27000 || di->chip == BQ27010)
475 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
476 else
477 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
478
479 if (soc < 0)
480 dev_dbg(di->dev, "error reading State-of-Charge\n");
481
482 return soc;
483 }
484
485 /*
486 * Return a battery charge value in µAh
487 * Or < 0 if something fails.
488 */
bq27xxx_battery_read_charge(struct bq27xxx_device_info * di,u8 reg)489 static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
490 {
491 int charge;
492
493 charge = bq27xxx_read(di, reg, false);
494 if (charge < 0) {
495 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
496 reg, charge);
497 return charge;
498 }
499
500 if (di->chip == BQ27000 || di->chip == BQ27010)
501 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
502 else
503 charge *= 1000;
504
505 return charge;
506 }
507
508 /*
509 * Return the battery Nominal available capacity in µAh
510 * Or < 0 if something fails.
511 */
bq27xxx_battery_read_nac(struct bq27xxx_device_info * di)512 static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
513 {
514 int flags;
515
516 if (di->chip == BQ27000 || di->chip == BQ27010) {
517 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
518 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
519 return -ENODATA;
520 }
521
522 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
523 }
524
525 /*
526 * Return the battery Full Charge Capacity in µAh
527 * Or < 0 if something fails.
528 */
bq27xxx_battery_read_fcc(struct bq27xxx_device_info * di)529 static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
530 {
531 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
532 }
533
534 /*
535 * Return the Design Capacity in µAh
536 * Or < 0 if something fails.
537 */
bq27xxx_battery_read_dcap(struct bq27xxx_device_info * di)538 static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
539 {
540 int dcap;
541
542 if (di->chip == BQ27000 || di->chip == BQ27010)
543 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
544 else
545 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
546
547 if (dcap < 0) {
548 dev_dbg(di->dev, "error reading initial last measured discharge\n");
549 return dcap;
550 }
551
552 if (di->chip == BQ27000 || di->chip == BQ27010)
553 dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
554 else
555 dcap *= 1000;
556
557 return dcap;
558 }
559
560 /*
561 * Return the battery Available energy in µWh
562 * Or < 0 if something fails.
563 */
bq27xxx_battery_read_energy(struct bq27xxx_device_info * di)564 static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
565 {
566 int ae;
567
568 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
569 if (ae < 0) {
570 dev_dbg(di->dev, "error reading available energy\n");
571 return ae;
572 }
573
574 if (di->chip == BQ27000 || di->chip == BQ27010)
575 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
576 else
577 ae *= 1000;
578
579 return ae;
580 }
581
582 /*
583 * Return the battery temperature in tenths of degree Kelvin
584 * Or < 0 if something fails.
585 */
bq27xxx_battery_read_temperature(struct bq27xxx_device_info * di)586 static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
587 {
588 int temp;
589
590 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
591 if (temp < 0) {
592 dev_err(di->dev, "error reading temperature\n");
593 return temp;
594 }
595
596 if (di->chip == BQ27000 || di->chip == BQ27010)
597 temp = 5 * temp / 2;
598
599 return temp;
600 }
601
602 /*
603 * Return the battery Cycle count total
604 * Or < 0 if something fails.
605 */
bq27xxx_battery_read_cyct(struct bq27xxx_device_info * di)606 static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
607 {
608 int cyct;
609
610 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
611 if (cyct < 0)
612 dev_err(di->dev, "error reading cycle count total\n");
613
614 return cyct;
615 }
616
617 /*
618 * Read a time register.
619 * Return < 0 if something fails.
620 */
bq27xxx_battery_read_time(struct bq27xxx_device_info * di,u8 reg)621 static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
622 {
623 int tval;
624
625 tval = bq27xxx_read(di, reg, false);
626 if (tval < 0) {
627 dev_dbg(di->dev, "error reading time register %02x: %d\n",
628 reg, tval);
629 return tval;
630 }
631
632 if (tval == 65535)
633 return -ENODATA;
634
635 return tval * 60;
636 }
637
638 /*
639 * Read an average power register.
640 * Return < 0 if something fails.
641 */
bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info * di)642 static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
643 {
644 int tval;
645
646 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
647 if (tval < 0) {
648 dev_err(di->dev, "error reading average power register %02x: %d\n",
649 BQ27XXX_REG_AP, tval);
650 return tval;
651 }
652
653 if (di->chip == BQ27000 || di->chip == BQ27010)
654 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
655 else
656 return tval;
657 }
658
659 /*
660 * Returns true if a battery over temperature condition is detected
661 */
bq27xxx_battery_overtemp(struct bq27xxx_device_info * di,u16 flags)662 static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
663 {
664 if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
665 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
666 if (di->chip == BQ27530 || di->chip == BQ27421)
667 return flags & BQ27XXX_FLAG_OT;
668
669 return false;
670 }
671
672 /*
673 * Returns true if a battery under temperature condition is detected
674 */
bq27xxx_battery_undertemp(struct bq27xxx_device_info * di,u16 flags)675 static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
676 {
677 if (di->chip == BQ27530 || di->chip == BQ27421)
678 return flags & BQ27XXX_FLAG_UT;
679
680 return false;
681 }
682
683 /*
684 * Returns true if a low state of charge condition is detected
685 */
bq27xxx_battery_dead(struct bq27xxx_device_info * di,u16 flags)686 static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
687 {
688 if (di->chip == BQ27000 || di->chip == BQ27010)
689 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
690 else
691 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
692 }
693
694 /*
695 * Read flag register.
696 * Return < 0 if something fails.
697 */
bq27xxx_battery_read_health(struct bq27xxx_device_info * di)698 static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
699 {
700 int flags;
701
702 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
703 if (flags < 0) {
704 dev_err(di->dev, "error reading flag register:%d\n", flags);
705 return flags;
706 }
707
708 /* Unlikely but important to return first */
709 if (unlikely(bq27xxx_battery_overtemp(di, flags)))
710 return POWER_SUPPLY_HEALTH_OVERHEAT;
711 if (unlikely(bq27xxx_battery_undertemp(di, flags)))
712 return POWER_SUPPLY_HEALTH_COLD;
713 if (unlikely(bq27xxx_battery_dead(di, flags)))
714 return POWER_SUPPLY_HEALTH_DEAD;
715
716 return POWER_SUPPLY_HEALTH_GOOD;
717 }
718
bq27xxx_battery_update(struct bq27xxx_device_info * di)719 static void bq27xxx_battery_update(struct bq27xxx_device_info *di)
720 {
721 struct bq27xxx_reg_cache cache = {0, };
722 bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
723 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
724
725 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
726 if ((cache.flags & 0xff) == 0xff)
727 cache.flags = -1; /* read error */
728 if (cache.flags >= 0) {
729 cache.temperature = bq27xxx_battery_read_temperature(di);
730 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
731 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
732 cache.capacity = -ENODATA;
733 cache.energy = -ENODATA;
734 cache.time_to_empty = -ENODATA;
735 cache.time_to_empty_avg = -ENODATA;
736 cache.time_to_full = -ENODATA;
737 cache.charge_full = -ENODATA;
738 cache.health = -ENODATA;
739 } else {
740 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
741 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
742 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
743 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
744 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
745 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
746 cache.charge_full = bq27xxx_battery_read_fcc(di);
747 cache.capacity = bq27xxx_battery_read_soc(di);
748 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
749 cache.energy = bq27xxx_battery_read_energy(di);
750 cache.health = bq27xxx_battery_read_health(di);
751 }
752 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
753 cache.cycle_count = bq27xxx_battery_read_cyct(di);
754 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
755 cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
756
757 /* We only have to read charge design full once */
758 if (di->charge_design_full <= 0)
759 di->charge_design_full = bq27xxx_battery_read_dcap(di);
760 }
761
762 if (di->cache.capacity != cache.capacity)
763 power_supply_changed(di->bat);
764
765 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
766 di->cache = cache;
767
768 di->last_update = jiffies;
769 }
770
bq27xxx_battery_poll(struct work_struct * work)771 static void bq27xxx_battery_poll(struct work_struct *work)
772 {
773 struct bq27xxx_device_info *di =
774 container_of(work, struct bq27xxx_device_info,
775 work.work);
776
777 bq27xxx_battery_update(di);
778
779 if (poll_interval > 0) {
780 /* The timer does not have to be accurate. */
781 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
782 schedule_delayed_work(&di->work, poll_interval * HZ);
783 }
784 }
785
786 /*
787 * Return the battery average current in µA
788 * Note that current can be negative signed as well
789 * Or 0 if something fails.
790 */
bq27xxx_battery_current(struct bq27xxx_device_info * di,union power_supply_propval * val)791 static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
792 union power_supply_propval *val)
793 {
794 int curr;
795 int flags;
796
797 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
798 if (curr < 0) {
799 dev_err(di->dev, "error reading current\n");
800 return curr;
801 }
802
803 if (di->chip == BQ27000 || di->chip == BQ27010) {
804 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
805 if (flags & BQ27000_FLAG_CHGS) {
806 dev_dbg(di->dev, "negative current!\n");
807 curr = -curr;
808 }
809
810 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
811 } else {
812 /* Other gauges return signed value */
813 val->intval = (int)((s16)curr) * 1000;
814 }
815
816 return 0;
817 }
818
bq27xxx_battery_status(struct bq27xxx_device_info * di,union power_supply_propval * val)819 static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
820 union power_supply_propval *val)
821 {
822 int status;
823
824 if (di->chip == BQ27000 || di->chip == BQ27010) {
825 if (di->cache.flags & BQ27000_FLAG_FC)
826 status = POWER_SUPPLY_STATUS_FULL;
827 else if (di->cache.flags & BQ27000_FLAG_CHGS)
828 status = POWER_SUPPLY_STATUS_CHARGING;
829 else if (power_supply_am_i_supplied(di->bat))
830 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
831 else
832 status = POWER_SUPPLY_STATUS_DISCHARGING;
833 } else {
834 if (di->cache.flags & BQ27XXX_FLAG_FC)
835 status = POWER_SUPPLY_STATUS_FULL;
836 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
837 status = POWER_SUPPLY_STATUS_DISCHARGING;
838 else
839 status = POWER_SUPPLY_STATUS_CHARGING;
840 }
841
842 val->intval = status;
843
844 return 0;
845 }
846
bq27xxx_battery_capacity_level(struct bq27xxx_device_info * di,union power_supply_propval * val)847 static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
848 union power_supply_propval *val)
849 {
850 int level;
851
852 if (di->chip == BQ27000 || di->chip == BQ27010) {
853 if (di->cache.flags & BQ27000_FLAG_FC)
854 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
855 else if (di->cache.flags & BQ27000_FLAG_EDV1)
856 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
857 else if (di->cache.flags & BQ27000_FLAG_EDVF)
858 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
859 else
860 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
861 } else {
862 if (di->cache.flags & BQ27XXX_FLAG_FC)
863 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
864 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
865 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
866 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
867 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
868 else
869 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
870 }
871
872 val->intval = level;
873
874 return 0;
875 }
876
877 /*
878 * Return the battery Voltage in millivolts
879 * Or < 0 if something fails.
880 */
bq27xxx_battery_voltage(struct bq27xxx_device_info * di,union power_supply_propval * val)881 static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
882 union power_supply_propval *val)
883 {
884 int volt;
885
886 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
887 if (volt < 0) {
888 dev_err(di->dev, "error reading voltage\n");
889 return volt;
890 }
891
892 val->intval = volt * 1000;
893
894 return 0;
895 }
896
bq27xxx_simple_value(int value,union power_supply_propval * val)897 static int bq27xxx_simple_value(int value,
898 union power_supply_propval *val)
899 {
900 if (value < 0)
901 return value;
902
903 val->intval = value;
904
905 return 0;
906 }
907
bq27xxx_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)908 static int bq27xxx_battery_get_property(struct power_supply *psy,
909 enum power_supply_property psp,
910 union power_supply_propval *val)
911 {
912 int ret = 0;
913 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
914
915 mutex_lock(&di->lock);
916 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
917 cancel_delayed_work_sync(&di->work);
918 bq27xxx_battery_poll(&di->work.work);
919 }
920 mutex_unlock(&di->lock);
921
922 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
923 return -ENODEV;
924
925 switch (psp) {
926 case POWER_SUPPLY_PROP_STATUS:
927 ret = bq27xxx_battery_status(di, val);
928 break;
929 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
930 ret = bq27xxx_battery_voltage(di, val);
931 break;
932 case POWER_SUPPLY_PROP_PRESENT:
933 val->intval = di->cache.flags < 0 ? 0 : 1;
934 break;
935 case POWER_SUPPLY_PROP_CURRENT_NOW:
936 ret = bq27xxx_battery_current(di, val);
937 break;
938 case POWER_SUPPLY_PROP_CAPACITY:
939 ret = bq27xxx_simple_value(di->cache.capacity, val);
940 break;
941 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
942 ret = bq27xxx_battery_capacity_level(di, val);
943 break;
944 case POWER_SUPPLY_PROP_TEMP:
945 ret = bq27xxx_simple_value(di->cache.temperature, val);
946 if (ret == 0)
947 val->intval -= 2731; /* convert decidegree k to c */
948 break;
949 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
950 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
951 break;
952 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
953 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
954 break;
955 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
956 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
957 break;
958 case POWER_SUPPLY_PROP_TECHNOLOGY:
959 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
960 break;
961 case POWER_SUPPLY_PROP_CHARGE_NOW:
962 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
963 break;
964 case POWER_SUPPLY_PROP_CHARGE_FULL:
965 ret = bq27xxx_simple_value(di->cache.charge_full, val);
966 break;
967 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
968 ret = bq27xxx_simple_value(di->charge_design_full, val);
969 break;
970 case POWER_SUPPLY_PROP_CYCLE_COUNT:
971 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
972 break;
973 case POWER_SUPPLY_PROP_ENERGY_NOW:
974 ret = bq27xxx_simple_value(di->cache.energy, val);
975 break;
976 case POWER_SUPPLY_PROP_POWER_AVG:
977 ret = bq27xxx_simple_value(di->cache.power_avg, val);
978 break;
979 case POWER_SUPPLY_PROP_HEALTH:
980 ret = bq27xxx_simple_value(di->cache.health, val);
981 break;
982 case POWER_SUPPLY_PROP_MANUFACTURER:
983 val->strval = BQ27XXX_MANUFACTURER;
984 break;
985 default:
986 return -EINVAL;
987 }
988
989 return ret;
990 }
991
bq27xxx_external_power_changed(struct power_supply * psy)992 static void bq27xxx_external_power_changed(struct power_supply *psy)
993 {
994 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
995
996 cancel_delayed_work_sync(&di->work);
997 schedule_delayed_work(&di->work, 0);
998 }
999
bq27xxx_powersupply_init(struct bq27xxx_device_info * di,const char * name)1000 static int __maybe_unused bq27xxx_powersupply_init(struct bq27xxx_device_info *di,
1001 const char *name)
1002 {
1003 int ret;
1004 struct power_supply_desc *psy_desc;
1005 struct power_supply_config psy_cfg = { .drv_data = di, };
1006
1007 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
1008 if (!psy_desc)
1009 return -ENOMEM;
1010
1011 psy_desc->name = name;
1012 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1013 psy_desc->properties = bq27xxx_battery_props[di->chip].props;
1014 psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
1015 psy_desc->get_property = bq27xxx_battery_get_property;
1016 psy_desc->external_power_changed = bq27xxx_external_power_changed;
1017
1018 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
1019 mutex_init(&di->lock);
1020
1021 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
1022 if (IS_ERR(di->bat)) {
1023 ret = PTR_ERR(di->bat);
1024 dev_err(di->dev, "failed to register battery: %d\n", ret);
1025 return ret;
1026 }
1027
1028 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
1029
1030 bq27xxx_battery_update(di);
1031
1032 return 0;
1033 }
1034
bq27xxx_powersupply_unregister(struct bq27xxx_device_info * di)1035 static void __maybe_unused bq27xxx_powersupply_unregister(struct bq27xxx_device_info *di)
1036 {
1037 /*
1038 * power_supply_unregister call bq27xxx_battery_get_property which
1039 * call bq27xxx_battery_poll.
1040 * Make sure that bq27xxx_battery_poll will not call
1041 * schedule_delayed_work again after unregister (which cause OOPS).
1042 */
1043 poll_interval = 0;
1044
1045 cancel_delayed_work_sync(&di->work);
1046
1047 power_supply_unregister(di->bat);
1048
1049 mutex_destroy(&di->lock);
1050 }
1051
1052 /* i2c specific code */
1053 #ifdef CONFIG_BATTERY_BQ27XXX_I2C
1054
1055 /* If the system has several batteries we need a different name for each
1056 * of them...
1057 */
1058 static DEFINE_IDR(battery_id);
1059 static DEFINE_MUTEX(battery_mutex);
1060
bq27xxx_battery_irq_handler_thread(int irq,void * data)1061 static irqreturn_t bq27xxx_battery_irq_handler_thread(int irq, void *data)
1062 {
1063 struct bq27xxx_device_info *di = data;
1064
1065 bq27xxx_battery_update(di);
1066
1067 return IRQ_HANDLED;
1068 }
1069
bq27xxx_battery_i2c_read(struct bq27xxx_device_info * di,u8 reg,bool single)1070 static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info *di, u8 reg,
1071 bool single)
1072 {
1073 struct i2c_client *client = to_i2c_client(di->dev);
1074 struct i2c_msg msg[2];
1075 unsigned char data[2];
1076 int ret;
1077
1078 if (!client->adapter)
1079 return -ENODEV;
1080
1081 msg[0].addr = client->addr;
1082 msg[0].flags = 0;
1083 msg[0].buf = ®
1084 msg[0].len = sizeof(reg);
1085 msg[1].addr = client->addr;
1086 msg[1].flags = I2C_M_RD;
1087 msg[1].buf = data;
1088 if (single)
1089 msg[1].len = 1;
1090 else
1091 msg[1].len = 2;
1092
1093 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
1094 if (ret < 0)
1095 return ret;
1096
1097 if (!single)
1098 ret = get_unaligned_le16(data);
1099 else
1100 ret = data[0];
1101
1102 return ret;
1103 }
1104
bq27xxx_battery_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)1105 static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
1106 const struct i2c_device_id *id)
1107 {
1108 char *name;
1109 struct bq27xxx_device_info *di;
1110 int num;
1111 int retval = 0;
1112
1113 /* Get new ID for the new battery device */
1114 mutex_lock(&battery_mutex);
1115 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
1116 mutex_unlock(&battery_mutex);
1117 if (num < 0)
1118 return num;
1119
1120 name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
1121 if (!name) {
1122 retval = -ENOMEM;
1123 goto batt_failed;
1124 }
1125
1126 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
1127 if (!di) {
1128 retval = -ENOMEM;
1129 goto batt_failed;
1130 }
1131
1132 di->id = num;
1133 di->dev = &client->dev;
1134 di->chip = id->driver_data;
1135 di->bus.read = &bq27xxx_battery_i2c_read;
1136 di->regs = bq27xxx_regs[di->chip];
1137
1138 retval = bq27xxx_powersupply_init(di, name);
1139 if (retval)
1140 goto batt_failed;
1141
1142 /* Schedule a polling after about 1 min */
1143 schedule_delayed_work(&di->work, 60 * HZ);
1144
1145 i2c_set_clientdata(client, di);
1146
1147 if (client->irq) {
1148 retval = devm_request_threaded_irq(&client->dev, client->irq,
1149 NULL, bq27xxx_battery_irq_handler_thread,
1150 IRQF_ONESHOT,
1151 name, di);
1152 if (retval) {
1153 dev_err(&client->dev,
1154 "Unable to register IRQ %d error %d\n",
1155 client->irq, retval);
1156 return retval;
1157 }
1158 }
1159
1160 return 0;
1161
1162 batt_failed:
1163 mutex_lock(&battery_mutex);
1164 idr_remove(&battery_id, num);
1165 mutex_unlock(&battery_mutex);
1166
1167 return retval;
1168 }
1169
bq27xxx_battery_i2c_remove(struct i2c_client * client)1170 static int bq27xxx_battery_i2c_remove(struct i2c_client *client)
1171 {
1172 struct bq27xxx_device_info *di = i2c_get_clientdata(client);
1173
1174 bq27xxx_powersupply_unregister(di);
1175
1176 mutex_lock(&battery_mutex);
1177 idr_remove(&battery_id, di->id);
1178 mutex_unlock(&battery_mutex);
1179
1180 return 0;
1181 }
1182
1183 static const struct i2c_device_id bq27xxx_id[] = {
1184 { "bq27200", BQ27000 },
1185 { "bq27210", BQ27010 },
1186 { "bq27500", BQ27500 },
1187 { "bq27510", BQ27500 },
1188 { "bq27520", BQ27500 },
1189 { "bq27530", BQ27530 },
1190 { "bq27531", BQ27530 },
1191 { "bq27541", BQ27541 },
1192 { "bq27542", BQ27541 },
1193 { "bq27546", BQ27541 },
1194 { "bq27742", BQ27541 },
1195 { "bq27545", BQ27545 },
1196 { "bq27421", BQ27421 },
1197 { "bq27425", BQ27421 },
1198 { "bq27441", BQ27421 },
1199 { "bq27621", BQ27421 },
1200 {},
1201 };
1202 MODULE_DEVICE_TABLE(i2c, bq27xxx_id);
1203
1204 static struct i2c_driver bq27xxx_battery_i2c_driver = {
1205 .driver = {
1206 .name = "bq27xxx-battery",
1207 },
1208 .probe = bq27xxx_battery_i2c_probe,
1209 .remove = bq27xxx_battery_i2c_remove,
1210 .id_table = bq27xxx_id,
1211 };
1212
bq27xxx_battery_i2c_init(void)1213 static inline int bq27xxx_battery_i2c_init(void)
1214 {
1215 int ret = i2c_add_driver(&bq27xxx_battery_i2c_driver);
1216
1217 if (ret)
1218 pr_err("Unable to register BQ27xxx i2c driver\n");
1219
1220 return ret;
1221 }
1222
bq27xxx_battery_i2c_exit(void)1223 static inline void bq27xxx_battery_i2c_exit(void)
1224 {
1225 i2c_del_driver(&bq27xxx_battery_i2c_driver);
1226 }
1227
1228 #else
1229
bq27xxx_battery_i2c_init(void)1230 static inline int bq27xxx_battery_i2c_init(void) { return 0; }
bq27xxx_battery_i2c_exit(void)1231 static inline void bq27xxx_battery_i2c_exit(void) {};
1232
1233 #endif
1234
1235 /* platform specific code */
1236 #ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
1237
bq27xxx_battery_platform_read(struct bq27xxx_device_info * di,u8 reg,bool single)1238 static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
1239 bool single)
1240 {
1241 struct device *dev = di->dev;
1242 struct bq27xxx_platform_data *pdata = dev->platform_data;
1243 unsigned int timeout = 3;
1244 int upper, lower;
1245 int temp;
1246
1247 if (!single) {
1248 /* Make sure the value has not changed in between reading the
1249 * lower and the upper part */
1250 upper = pdata->read(dev, reg + 1);
1251 do {
1252 temp = upper;
1253 if (upper < 0)
1254 return upper;
1255
1256 lower = pdata->read(dev, reg);
1257 if (lower < 0)
1258 return lower;
1259
1260 upper = pdata->read(dev, reg + 1);
1261 } while (temp != upper && --timeout);
1262
1263 if (timeout == 0)
1264 return -EIO;
1265
1266 return (upper << 8) | lower;
1267 }
1268
1269 return pdata->read(dev, reg);
1270 }
1271
bq27xxx_battery_platform_probe(struct platform_device * pdev)1272 static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
1273 {
1274 struct bq27xxx_device_info *di;
1275 struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
1276 const char *name;
1277
1278 if (!pdata) {
1279 dev_err(&pdev->dev, "no platform_data supplied\n");
1280 return -EINVAL;
1281 }
1282
1283 if (!pdata->read) {
1284 dev_err(&pdev->dev, "no hdq read callback supplied\n");
1285 return -EINVAL;
1286 }
1287
1288 if (!pdata->chip) {
1289 dev_err(&pdev->dev, "no device supplied\n");
1290 return -EINVAL;
1291 }
1292
1293 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1294 if (!di)
1295 return -ENOMEM;
1296
1297 platform_set_drvdata(pdev, di);
1298
1299 di->dev = &pdev->dev;
1300 di->chip = pdata->chip;
1301 di->regs = bq27xxx_regs[di->chip];
1302
1303 name = pdata->name ?: dev_name(&pdev->dev);
1304 di->bus.read = &bq27xxx_battery_platform_read;
1305
1306 return bq27xxx_powersupply_init(di, name);
1307 }
1308
bq27xxx_battery_platform_remove(struct platform_device * pdev)1309 static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
1310 {
1311 struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
1312
1313 bq27xxx_powersupply_unregister(di);
1314
1315 return 0;
1316 }
1317
1318 static struct platform_driver bq27xxx_battery_platform_driver = {
1319 .probe = bq27xxx_battery_platform_probe,
1320 .remove = bq27xxx_battery_platform_remove,
1321 .driver = {
1322 .name = "bq27000-battery",
1323 },
1324 };
1325
bq27xxx_battery_platform_init(void)1326 static inline int bq27xxx_battery_platform_init(void)
1327 {
1328 int ret = platform_driver_register(&bq27xxx_battery_platform_driver);
1329
1330 if (ret)
1331 pr_err("Unable to register BQ27xxx platform driver\n");
1332
1333 return ret;
1334 }
1335
bq27xxx_battery_platform_exit(void)1336 static inline void bq27xxx_battery_platform_exit(void)
1337 {
1338 platform_driver_unregister(&bq27xxx_battery_platform_driver);
1339 }
1340
1341 #else
1342
bq27xxx_battery_platform_init(void)1343 static inline int bq27xxx_battery_platform_init(void) { return 0; }
bq27xxx_battery_platform_exit(void)1344 static inline void bq27xxx_battery_platform_exit(void) {};
1345
1346 #endif
1347
1348 /*
1349 * Module stuff
1350 */
1351
bq27xxx_battery_init(void)1352 static int __init bq27xxx_battery_init(void)
1353 {
1354 int ret;
1355
1356 ret = bq27xxx_battery_i2c_init();
1357 if (ret)
1358 return ret;
1359
1360 ret = bq27xxx_battery_platform_init();
1361 if (ret)
1362 bq27xxx_battery_i2c_exit();
1363
1364 return ret;
1365 }
1366 module_init(bq27xxx_battery_init);
1367
bq27xxx_battery_exit(void)1368 static void __exit bq27xxx_battery_exit(void)
1369 {
1370 bq27xxx_battery_platform_exit();
1371 bq27xxx_battery_i2c_exit();
1372 }
1373 module_exit(bq27xxx_battery_exit);
1374
1375 #ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
1376 MODULE_ALIAS("platform:bq27000-battery");
1377 #endif
1378
1379 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1380 MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
1381 MODULE_LICENSE("GPL");
1382