dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / power / bq27xxx_battery.c
blob6c3a447f378b818cb45b321e16f8dfee5986578f
1 /*
2 * BQ27xxx battery driver
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>
9 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
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.
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.
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
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>
54 #include <linux/power/bq27xxx_battery.h>
56 #define DRIVER_VERSION "1.2.0"
58 #define BQ27XXX_MANUFACTURER "Texas Instruments"
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)
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 */
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 */
81 struct bq27xxx_device_info;
82 struct bq27xxx_access_methods {
83 int (*read)(struct bq27xxx_device_info *di, u8 reg, bool single);
86 #define INVALID_REG_ADDR 0xff
89 * bq27xxx_reg_index - Register names
91 * These are indexes into a device's register mapping array.
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 */
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;
127 struct bq27xxx_device_info {
128 struct device *dev;
129 int id;
130 enum bq27xxx_chip chip;
132 struct bq27xxx_reg_cache cache;
133 int charge_design_full;
135 unsigned long last_update;
136 struct delayed_work work;
138 struct power_supply *bat;
140 struct bq27xxx_access_methods bus;
142 struct mutex lock;
144 u8 *regs;
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 */
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 */
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 0x1e, /* CYCT */
202 INVALID_REG_ADDR, /* AE - NA */
203 0x20, /* SOC(RSOC) */
204 0x2e, /* DCAP(ILMD) */
205 INVALID_REG_ADDR, /* AP - NA */
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 */
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 0x76, /* AP */
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 */
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 */
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,
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,
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,
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,
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,
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,
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,
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,
428 #define BQ27XXX_PROP(_id, _prop) \
429 [_id] = { \
430 .props = _prop, \
431 .size = ARRAY_SIZE(_prop), \
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),
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");
453 * Common code for BQ27xxx devices
456 static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
457 bool single)
459 /* Reports EINVAL for invalid/missing registers */
460 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
461 return -EINVAL;
463 return di->bus.read(di, di->regs[reg_index], single);
467 * Return the battery State-of-Charge
468 * Or < 0 if something fails.
470 static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
472 int soc;
474 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
476 if (soc < 0)
477 dev_dbg(di->dev, "error reading State-of-Charge\n");
479 return soc;
483 * Return a battery charge value in µAh
484 * Or < 0 if something fails.
486 static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
488 int charge;
490 charge = bq27xxx_read(di, reg, false);
491 if (charge < 0) {
492 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
493 reg, charge);
494 return charge;
497 if (di->chip == BQ27000 || di->chip == BQ27010)
498 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
499 else
500 charge *= 1000;
502 return charge;
506 * Return the battery Nominal available capacity in µAh
507 * Or < 0 if something fails.
509 static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
511 int flags;
513 if (di->chip == BQ27000 || di->chip == BQ27010) {
514 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
515 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
516 return -ENODATA;
519 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
523 * Return the battery Full Charge Capacity in µAh
524 * Or < 0 if something fails.
526 static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
528 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
532 * Return the Design Capacity in µAh
533 * Or < 0 if something fails.
535 static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
537 int dcap;
539 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
541 if (dcap < 0) {
542 dev_dbg(di->dev, "error reading initial last measured discharge\n");
543 return dcap;
546 if (di->chip == BQ27000 || di->chip == BQ27010)
547 dcap *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
548 else
549 dcap *= 1000;
551 return dcap;
555 * Return the battery Available energy in µWh
556 * Or < 0 if something fails.
558 static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
560 int ae;
562 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
563 if (ae < 0) {
564 dev_dbg(di->dev, "error reading available energy\n");
565 return ae;
568 if (di->chip == BQ27000 || di->chip == BQ27010)
569 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
570 else
571 ae *= 1000;
573 return ae;
577 * Return the battery temperature in tenths of degree Kelvin
578 * Or < 0 if something fails.
580 static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
582 int temp;
584 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
585 if (temp < 0) {
586 dev_err(di->dev, "error reading temperature\n");
587 return temp;
590 if (di->chip == BQ27000 || di->chip == BQ27010)
591 temp = 5 * temp / 2;
593 return temp;
597 * Return the battery Cycle count total
598 * Or < 0 if something fails.
600 static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
602 int cyct;
604 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
605 if (cyct < 0)
606 dev_err(di->dev, "error reading cycle count total\n");
608 return cyct;
612 * Read a time register.
613 * Return < 0 if something fails.
615 static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
617 int tval;
619 tval = bq27xxx_read(di, reg, false);
620 if (tval < 0) {
621 dev_dbg(di->dev, "error reading time register %02x: %d\n",
622 reg, tval);
623 return tval;
626 if (tval == 65535)
627 return -ENODATA;
629 return tval * 60;
633 * Read an average power register.
634 * Return < 0 if something fails.
636 static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
638 int tval;
640 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
641 if (tval < 0) {
642 dev_err(di->dev, "error reading average power register %02x: %d\n",
643 BQ27XXX_REG_AP, tval);
644 return tval;
647 if (di->chip == BQ27000 || di->chip == BQ27010)
648 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
649 else
650 return tval;
654 * Returns true if a battery over temperature condition is detected
656 static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
658 if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
659 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
660 if (di->chip == BQ27530 || di->chip == BQ27421)
661 return flags & BQ27XXX_FLAG_OT;
663 return false;
667 * Returns true if a battery under temperature condition is detected
669 static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
671 if (di->chip == BQ27530 || di->chip == BQ27421)
672 return flags & BQ27XXX_FLAG_UT;
674 return false;
678 * Returns true if a low state of charge condition is detected
680 static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
682 if (di->chip == BQ27000 || di->chip == BQ27010)
683 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
684 else
685 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
689 * Read flag register.
690 * Return < 0 if something fails.
692 static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
694 int flags;
696 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
697 if (flags < 0) {
698 dev_err(di->dev, "error reading flag register:%d\n", flags);
699 return flags;
702 /* Unlikely but important to return first */
703 if (unlikely(bq27xxx_battery_overtemp(di, flags)))
704 return POWER_SUPPLY_HEALTH_OVERHEAT;
705 if (unlikely(bq27xxx_battery_undertemp(di, flags)))
706 return POWER_SUPPLY_HEALTH_COLD;
707 if (unlikely(bq27xxx_battery_dead(di, flags)))
708 return POWER_SUPPLY_HEALTH_DEAD;
710 return POWER_SUPPLY_HEALTH_GOOD;
713 static void bq27xxx_battery_update(struct bq27xxx_device_info *di)
715 struct bq27xxx_reg_cache cache = {0, };
716 bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
717 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
719 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
720 if ((cache.flags & 0xff) == 0xff)
721 cache.flags = -1; /* read error */
722 if (cache.flags >= 0) {
723 cache.temperature = bq27xxx_battery_read_temperature(di);
724 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
725 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
726 cache.capacity = -ENODATA;
727 cache.energy = -ENODATA;
728 cache.time_to_empty = -ENODATA;
729 cache.time_to_empty_avg = -ENODATA;
730 cache.time_to_full = -ENODATA;
731 cache.charge_full = -ENODATA;
732 cache.health = -ENODATA;
733 } else {
734 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
735 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
736 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
737 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
738 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
739 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
740 cache.charge_full = bq27xxx_battery_read_fcc(di);
741 cache.capacity = bq27xxx_battery_read_soc(di);
742 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
743 cache.energy = bq27xxx_battery_read_energy(di);
744 cache.health = bq27xxx_battery_read_health(di);
746 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
747 cache.cycle_count = bq27xxx_battery_read_cyct(di);
748 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
749 cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
751 /* We only have to read charge design full once */
752 if (di->charge_design_full <= 0)
753 di->charge_design_full = bq27xxx_battery_read_dcap(di);
756 if (di->cache.capacity != cache.capacity)
757 power_supply_changed(di->bat);
759 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
760 di->cache = cache;
762 di->last_update = jiffies;
765 static void bq27xxx_battery_poll(struct work_struct *work)
767 struct bq27xxx_device_info *di =
768 container_of(work, struct bq27xxx_device_info,
769 work.work);
771 bq27xxx_battery_update(di);
773 if (poll_interval > 0) {
774 /* The timer does not have to be accurate. */
775 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
776 schedule_delayed_work(&di->work, poll_interval * HZ);
781 * Return the battery average current in µA
782 * Note that current can be negative signed as well
783 * Or 0 if something fails.
785 static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
786 union power_supply_propval *val)
788 int curr;
789 int flags;
791 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
792 if (curr < 0) {
793 dev_err(di->dev, "error reading current\n");
794 return curr;
797 if (di->chip == BQ27000 || di->chip == BQ27010) {
798 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
799 if (flags & BQ27000_FLAG_CHGS) {
800 dev_dbg(di->dev, "negative current!\n");
801 curr = -curr;
804 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
805 } else {
806 /* Other gauges return signed value */
807 val->intval = (int)((s16)curr) * 1000;
810 return 0;
813 static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
814 union power_supply_propval *val)
816 int status;
818 if (di->chip == BQ27000 || di->chip == BQ27010) {
819 if (di->cache.flags & BQ27000_FLAG_FC)
820 status = POWER_SUPPLY_STATUS_FULL;
821 else if (di->cache.flags & BQ27000_FLAG_CHGS)
822 status = POWER_SUPPLY_STATUS_CHARGING;
823 else if (power_supply_am_i_supplied(di->bat))
824 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
825 else
826 status = POWER_SUPPLY_STATUS_DISCHARGING;
827 } else {
828 if (di->cache.flags & BQ27XXX_FLAG_FC)
829 status = POWER_SUPPLY_STATUS_FULL;
830 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
831 status = POWER_SUPPLY_STATUS_DISCHARGING;
832 else
833 status = POWER_SUPPLY_STATUS_CHARGING;
836 val->intval = status;
838 return 0;
841 static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
842 union power_supply_propval *val)
844 int level;
846 if (di->chip == BQ27000 || di->chip == BQ27010) {
847 if (di->cache.flags & BQ27000_FLAG_FC)
848 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
849 else if (di->cache.flags & BQ27000_FLAG_EDV1)
850 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
851 else if (di->cache.flags & BQ27000_FLAG_EDVF)
852 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
853 else
854 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
855 } else {
856 if (di->cache.flags & BQ27XXX_FLAG_FC)
857 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
858 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
859 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
860 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
861 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
862 else
863 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
866 val->intval = level;
868 return 0;
872 * Return the battery Voltage in millivolts
873 * Or < 0 if something fails.
875 static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
876 union power_supply_propval *val)
878 int volt;
880 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
881 if (volt < 0) {
882 dev_err(di->dev, "error reading voltage\n");
883 return volt;
886 val->intval = volt * 1000;
888 return 0;
891 static int bq27xxx_simple_value(int value,
892 union power_supply_propval *val)
894 if (value < 0)
895 return value;
897 val->intval = value;
899 return 0;
902 static int bq27xxx_battery_get_property(struct power_supply *psy,
903 enum power_supply_property psp,
904 union power_supply_propval *val)
906 int ret = 0;
907 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
909 mutex_lock(&di->lock);
910 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
911 cancel_delayed_work_sync(&di->work);
912 bq27xxx_battery_poll(&di->work.work);
914 mutex_unlock(&di->lock);
916 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
917 return -ENODEV;
919 switch (psp) {
920 case POWER_SUPPLY_PROP_STATUS:
921 ret = bq27xxx_battery_status(di, val);
922 break;
923 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
924 ret = bq27xxx_battery_voltage(di, val);
925 break;
926 case POWER_SUPPLY_PROP_PRESENT:
927 val->intval = di->cache.flags < 0 ? 0 : 1;
928 break;
929 case POWER_SUPPLY_PROP_CURRENT_NOW:
930 ret = bq27xxx_battery_current(di, val);
931 break;
932 case POWER_SUPPLY_PROP_CAPACITY:
933 ret = bq27xxx_simple_value(di->cache.capacity, val);
934 break;
935 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
936 ret = bq27xxx_battery_capacity_level(di, val);
937 break;
938 case POWER_SUPPLY_PROP_TEMP:
939 ret = bq27xxx_simple_value(di->cache.temperature, val);
940 if (ret == 0)
941 val->intval -= 2731; /* convert decidegree k to c */
942 break;
943 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
944 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
945 break;
946 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
947 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
948 break;
949 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
950 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
951 break;
952 case POWER_SUPPLY_PROP_TECHNOLOGY:
953 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
954 break;
955 case POWER_SUPPLY_PROP_CHARGE_NOW:
956 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
957 break;
958 case POWER_SUPPLY_PROP_CHARGE_FULL:
959 ret = bq27xxx_simple_value(di->cache.charge_full, val);
960 break;
961 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
962 ret = bq27xxx_simple_value(di->charge_design_full, val);
963 break;
964 case POWER_SUPPLY_PROP_CYCLE_COUNT:
965 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
966 break;
967 case POWER_SUPPLY_PROP_ENERGY_NOW:
968 ret = bq27xxx_simple_value(di->cache.energy, val);
969 break;
970 case POWER_SUPPLY_PROP_POWER_AVG:
971 ret = bq27xxx_simple_value(di->cache.power_avg, val);
972 break;
973 case POWER_SUPPLY_PROP_HEALTH:
974 ret = bq27xxx_simple_value(di->cache.health, val);
975 break;
976 case POWER_SUPPLY_PROP_MANUFACTURER:
977 val->strval = BQ27XXX_MANUFACTURER;
978 break;
979 default:
980 return -EINVAL;
983 return ret;
986 static void bq27xxx_external_power_changed(struct power_supply *psy)
988 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
990 cancel_delayed_work_sync(&di->work);
991 schedule_delayed_work(&di->work, 0);
994 static int __maybe_unused bq27xxx_powersupply_init(struct bq27xxx_device_info *di,
995 const char *name)
997 int ret;
998 struct power_supply_desc *psy_desc;
999 struct power_supply_config psy_cfg = { .drv_data = di, };
1001 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
1002 if (!psy_desc)
1003 return -ENOMEM;
1005 psy_desc->name = name;
1006 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1007 psy_desc->properties = bq27xxx_battery_props[di->chip].props;
1008 psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
1009 psy_desc->get_property = bq27xxx_battery_get_property;
1010 psy_desc->external_power_changed = bq27xxx_external_power_changed;
1012 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
1013 mutex_init(&di->lock);
1015 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
1016 if (IS_ERR(di->bat)) {
1017 ret = PTR_ERR(di->bat);
1018 dev_err(di->dev, "failed to register battery: %d\n", ret);
1019 return ret;
1022 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
1024 bq27xxx_battery_update(di);
1026 return 0;
1029 static void __maybe_unused bq27xxx_powersupply_unregister(struct bq27xxx_device_info *di)
1032 * power_supply_unregister call bq27xxx_battery_get_property which
1033 * call bq27xxx_battery_poll.
1034 * Make sure that bq27xxx_battery_poll will not call
1035 * schedule_delayed_work again after unregister (which cause OOPS).
1037 poll_interval = 0;
1039 cancel_delayed_work_sync(&di->work);
1041 power_supply_unregister(di->bat);
1043 mutex_destroy(&di->lock);
1046 /* i2c specific code */
1047 #ifdef CONFIG_BATTERY_BQ27XXX_I2C
1049 /* If the system has several batteries we need a different name for each
1050 * of them...
1052 static DEFINE_IDR(battery_id);
1053 static DEFINE_MUTEX(battery_mutex);
1055 static irqreturn_t bq27xxx_battery_irq_handler_thread(int irq, void *data)
1057 struct bq27xxx_device_info *di = data;
1059 bq27xxx_battery_update(di);
1061 return IRQ_HANDLED;
1064 static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info *di, u8 reg,
1065 bool single)
1067 struct i2c_client *client = to_i2c_client(di->dev);
1068 struct i2c_msg msg[2];
1069 unsigned char data[2];
1070 int ret;
1072 if (!client->adapter)
1073 return -ENODEV;
1075 msg[0].addr = client->addr;
1076 msg[0].flags = 0;
1077 msg[0].buf = &reg;
1078 msg[0].len = sizeof(reg);
1079 msg[1].addr = client->addr;
1080 msg[1].flags = I2C_M_RD;
1081 msg[1].buf = data;
1082 if (single)
1083 msg[1].len = 1;
1084 else
1085 msg[1].len = 2;
1087 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
1088 if (ret < 0)
1089 return ret;
1091 if (!single)
1092 ret = get_unaligned_le16(data);
1093 else
1094 ret = data[0];
1096 return ret;
1099 static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
1100 const struct i2c_device_id *id)
1102 char *name;
1103 struct bq27xxx_device_info *di;
1104 int num;
1105 int retval = 0;
1107 /* Get new ID for the new battery device */
1108 mutex_lock(&battery_mutex);
1109 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
1110 mutex_unlock(&battery_mutex);
1111 if (num < 0)
1112 return num;
1114 name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
1115 if (!name) {
1116 retval = -ENOMEM;
1117 goto batt_failed;
1120 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
1121 if (!di) {
1122 retval = -ENOMEM;
1123 goto batt_failed;
1126 di->id = num;
1127 di->dev = &client->dev;
1128 di->chip = id->driver_data;
1129 di->bus.read = &bq27xxx_battery_i2c_read;
1130 di->regs = bq27xxx_regs[di->chip];
1132 retval = bq27xxx_powersupply_init(di, name);
1133 if (retval)
1134 goto batt_failed;
1136 /* Schedule a polling after about 1 min */
1137 schedule_delayed_work(&di->work, 60 * HZ);
1139 i2c_set_clientdata(client, di);
1141 if (client->irq) {
1142 retval = devm_request_threaded_irq(&client->dev, client->irq,
1143 NULL, bq27xxx_battery_irq_handler_thread,
1144 IRQF_ONESHOT,
1145 name, di);
1146 if (retval) {
1147 dev_err(&client->dev,
1148 "Unable to register IRQ %d error %d\n",
1149 client->irq, retval);
1150 return retval;
1154 return 0;
1156 batt_failed:
1157 mutex_lock(&battery_mutex);
1158 idr_remove(&battery_id, num);
1159 mutex_unlock(&battery_mutex);
1161 return retval;
1164 static int bq27xxx_battery_i2c_remove(struct i2c_client *client)
1166 struct bq27xxx_device_info *di = i2c_get_clientdata(client);
1168 bq27xxx_powersupply_unregister(di);
1170 mutex_lock(&battery_mutex);
1171 idr_remove(&battery_id, di->id);
1172 mutex_unlock(&battery_mutex);
1174 return 0;
1177 static const struct i2c_device_id bq27xxx_id[] = {
1178 { "bq27200", BQ27000 },
1179 { "bq27210", BQ27010 },
1180 { "bq27500", BQ27500 },
1181 { "bq27510", BQ27500 },
1182 { "bq27520", BQ27500 },
1183 { "bq27530", BQ27530 },
1184 { "bq27531", BQ27530 },
1185 { "bq27541", BQ27541 },
1186 { "bq27542", BQ27541 },
1187 { "bq27546", BQ27541 },
1188 { "bq27742", BQ27541 },
1189 { "bq27545", BQ27545 },
1190 { "bq27421", BQ27421 },
1191 { "bq27425", BQ27421 },
1192 { "bq27441", BQ27421 },
1193 { "bq27621", BQ27421 },
1196 MODULE_DEVICE_TABLE(i2c, bq27xxx_id);
1198 static struct i2c_driver bq27xxx_battery_i2c_driver = {
1199 .driver = {
1200 .name = "bq27xxx-battery",
1202 .probe = bq27xxx_battery_i2c_probe,
1203 .remove = bq27xxx_battery_i2c_remove,
1204 .id_table = bq27xxx_id,
1207 static inline int bq27xxx_battery_i2c_init(void)
1209 int ret = i2c_add_driver(&bq27xxx_battery_i2c_driver);
1211 if (ret)
1212 pr_err("Unable to register BQ27xxx i2c driver\n");
1214 return ret;
1217 static inline void bq27xxx_battery_i2c_exit(void)
1219 i2c_del_driver(&bq27xxx_battery_i2c_driver);
1222 #else
1224 static inline int bq27xxx_battery_i2c_init(void) { return 0; }
1225 static inline void bq27xxx_battery_i2c_exit(void) {};
1227 #endif
1229 /* platform specific code */
1230 #ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
1232 static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
1233 bool single)
1235 struct device *dev = di->dev;
1236 struct bq27xxx_platform_data *pdata = dev->platform_data;
1237 unsigned int timeout = 3;
1238 int upper, lower;
1239 int temp;
1241 if (!single) {
1242 /* Make sure the value has not changed in between reading the
1243 * lower and the upper part */
1244 upper = pdata->read(dev, reg + 1);
1245 do {
1246 temp = upper;
1247 if (upper < 0)
1248 return upper;
1250 lower = pdata->read(dev, reg);
1251 if (lower < 0)
1252 return lower;
1254 upper = pdata->read(dev, reg + 1);
1255 } while (temp != upper && --timeout);
1257 if (timeout == 0)
1258 return -EIO;
1260 return (upper << 8) | lower;
1263 return pdata->read(dev, reg);
1266 static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
1268 struct bq27xxx_device_info *di;
1269 struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
1270 const char *name;
1272 if (!pdata) {
1273 dev_err(&pdev->dev, "no platform_data supplied\n");
1274 return -EINVAL;
1277 if (!pdata->read) {
1278 dev_err(&pdev->dev, "no hdq read callback supplied\n");
1279 return -EINVAL;
1282 if (!pdata->chip) {
1283 dev_err(&pdev->dev, "no device supplied\n");
1284 return -EINVAL;
1287 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1288 if (!di)
1289 return -ENOMEM;
1291 platform_set_drvdata(pdev, di);
1293 di->dev = &pdev->dev;
1294 di->chip = pdata->chip;
1295 di->regs = bq27xxx_regs[di->chip];
1297 name = pdata->name ?: dev_name(&pdev->dev);
1298 di->bus.read = &bq27xxx_battery_platform_read;
1300 return bq27xxx_powersupply_init(di, name);
1303 static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
1305 struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
1307 bq27xxx_powersupply_unregister(di);
1309 return 0;
1312 static struct platform_driver bq27xxx_battery_platform_driver = {
1313 .probe = bq27xxx_battery_platform_probe,
1314 .remove = bq27xxx_battery_platform_remove,
1315 .driver = {
1316 .name = "bq27000-battery",
1320 static inline int bq27xxx_battery_platform_init(void)
1322 int ret = platform_driver_register(&bq27xxx_battery_platform_driver);
1324 if (ret)
1325 pr_err("Unable to register BQ27xxx platform driver\n");
1327 return ret;
1330 static inline void bq27xxx_battery_platform_exit(void)
1332 platform_driver_unregister(&bq27xxx_battery_platform_driver);
1335 #else
1337 static inline int bq27xxx_battery_platform_init(void) { return 0; }
1338 static inline void bq27xxx_battery_platform_exit(void) {};
1340 #endif
1343 * Module stuff
1346 static int __init bq27xxx_battery_init(void)
1348 int ret;
1350 ret = bq27xxx_battery_i2c_init();
1351 if (ret)
1352 return ret;
1354 ret = bq27xxx_battery_platform_init();
1355 if (ret)
1356 bq27xxx_battery_i2c_exit();
1358 return ret;
1360 module_init(bq27xxx_battery_init);
1362 static void __exit bq27xxx_battery_exit(void)
1364 bq27xxx_battery_platform_exit();
1365 bq27xxx_battery_i2c_exit();
1367 module_exit(bq27xxx_battery_exit);
1369 #ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
1370 MODULE_ALIAS("platform:bq27000-battery");
1371 #endif
1373 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1374 MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
1375 MODULE_LICENSE("GPL");