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