blob: 8287261fd978895afca216d993537c7f55ea1dee [file] [log] [blame]
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001/*
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>
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02006 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
Pali Rohár73c244a2011-04-30 23:59:35 +02007 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07008 *
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 *
Pali Rohár631c17e2011-02-01 00:08:02 +010019 * Datasheets:
20 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
21 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
Saranya Gopala66f59b2012-07-13 20:20:07 -070022 * http://www.ti.com/product/bq27425-g1
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -070023 * http://www.ti.com/product/BQ27742-G1
Alexandre Bellonif46bf822015-02-28 18:59:28 +010024 * http://www.ti.com/product/BQ27510-G3
Pali Rohár631c17e2011-02-01 00:08:02 +010025 */
26
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +053027#include <linux/device.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070028#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>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070036#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Harvey Harrison8aef7e82008-09-22 14:53:50 -070038#include <asm/unaligned.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070039
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020040#include <linux/power/bq27x00_battery.h>
41
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050042#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070043
Andrew F. Davisdb04fc52015-07-22 16:51:53 -050044#define BQ27XXX_MANUFACTURER "Texas Instruments"
45
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050046#define BQ27x00_REG_TEMP 0x06
47#define BQ27x00_REG_VOLT 0x08
48#define BQ27x00_REG_AI 0x14
49#define BQ27x00_REG_FLAGS 0x0A
50#define BQ27x00_REG_TTE 0x16
51#define BQ27x00_REG_TTF 0x18
52#define BQ27x00_REG_TTECP 0x26
53#define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
54#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
55#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
56#define BQ27x00_REG_AE 0x22 /* Available energy */
57#define BQ27x00_POWER_AVG 0x24
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070058
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050059#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
60#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
61#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
62#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
63#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
64#define BQ27000_FLAG_FC BIT(5)
65#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020066
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050067#define BQ27500_REG_SOC 0x2C
68#define BQ27500_REG_DCAP 0x3C /* Design capacity */
69#define BQ27500_FLAG_DSC BIT(0)
70#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
71#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
72#define BQ27500_FLAG_FC BIT(9)
73#define BQ27500_FLAG_OTC BIT(15)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020074
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050075#define BQ27742_POWER_AVG 0x76
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -070076
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050077#define BQ27510_REG_SOC 0x20
78#define BQ27510_REG_DCAP 0x2E /* Design capacity */
79#define BQ27510_REG_CYCT 0x1E /* Cycle count total */
Alexandre Bellonif46bf822015-02-28 18:59:28 +010080
Saranya Gopala66f59b2012-07-13 20:20:07 -070081/* bq27425 register addresses are same as bq27x00 addresses minus 4 */
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050082#define BQ27425_REG_OFFSET 0x04
Eric Bénard9dbf5a22014-11-12 23:04:08 +010083#define BQ27425_REG_SOC (0x1C + BQ27425_REG_OFFSET)
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050084#define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET)
Saranya Gopala66f59b2012-07-13 20:20:07 -070085
Pali Rohára2e51182010-05-24 20:52:13 +020086#define BQ27000_RS 20 /* Resistor sense */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053087#define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
Pali Rohára2e51182010-05-24 20:52:13 +020088
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070089struct bq27x00_device_info;
90struct bq27x00_access_methods {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010091 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070092};
93
Alexandre Bellonif46bf822015-02-28 18:59:28 +010094enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742, BQ27510};
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020095
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010096struct bq27x00_reg_cache {
97 int temperature;
98 int time_to_empty;
99 int time_to_empty_avg;
100 int time_to_full;
Pali Rohár631c17e2011-02-01 00:08:02 +0100101 int charge_full;
Pali Rohár73c244a2011-04-30 23:59:35 +0200102 int cycle_count;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100103 int capacity;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100104 int energy;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100105 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530106 int power_avg;
107 int health;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100108};
109
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700110struct bq27x00_device_info {
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500111 struct device *dev;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700112 int id;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200113 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700114
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100115 struct bq27x00_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +0100116 int charge_design_full;
117
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100118 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100119 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100120
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100121 struct power_supply *bat;
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200122
123 struct bq27x00_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100124
125 struct mutex lock;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700126};
127
128static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200129 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700130 POWER_SUPPLY_PROP_PRESENT,
131 POWER_SUPPLY_PROP_VOLTAGE_NOW,
132 POWER_SUPPLY_PROP_CURRENT_NOW,
133 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100134 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700135 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200136 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
137 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
138 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200139 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100140 POWER_SUPPLY_PROP_CHARGE_FULL,
141 POWER_SUPPLY_PROP_CHARGE_NOW,
142 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200143 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100144 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530145 POWER_SUPPLY_PROP_POWER_AVG,
146 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500147 POWER_SUPPLY_PROP_MANUFACTURER,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700148};
149
Saranya Gopala66f59b2012-07-13 20:20:07 -0700150static enum power_supply_property bq27425_battery_props[] = {
151 POWER_SUPPLY_PROP_STATUS,
152 POWER_SUPPLY_PROP_PRESENT,
153 POWER_SUPPLY_PROP_VOLTAGE_NOW,
154 POWER_SUPPLY_PROP_CURRENT_NOW,
155 POWER_SUPPLY_PROP_CAPACITY,
156 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
157 POWER_SUPPLY_PROP_TEMP,
158 POWER_SUPPLY_PROP_TECHNOLOGY,
159 POWER_SUPPLY_PROP_CHARGE_FULL,
160 POWER_SUPPLY_PROP_CHARGE_NOW,
161 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500162 POWER_SUPPLY_PROP_MANUFACTURER,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700163};
164
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700165static enum power_supply_property bq27742_battery_props[] = {
166 POWER_SUPPLY_PROP_STATUS,
167 POWER_SUPPLY_PROP_PRESENT,
168 POWER_SUPPLY_PROP_VOLTAGE_NOW,
169 POWER_SUPPLY_PROP_CURRENT_NOW,
170 POWER_SUPPLY_PROP_CAPACITY,
171 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
172 POWER_SUPPLY_PROP_TEMP,
173 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
174 POWER_SUPPLY_PROP_TECHNOLOGY,
175 POWER_SUPPLY_PROP_CHARGE_FULL,
176 POWER_SUPPLY_PROP_CHARGE_NOW,
177 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
178 POWER_SUPPLY_PROP_CYCLE_COUNT,
179 POWER_SUPPLY_PROP_POWER_AVG,
180 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500181 POWER_SUPPLY_PROP_MANUFACTURER,
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700182};
183
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100184static enum power_supply_property bq27510_battery_props[] = {
185 POWER_SUPPLY_PROP_STATUS,
186 POWER_SUPPLY_PROP_PRESENT,
187 POWER_SUPPLY_PROP_VOLTAGE_NOW,
188 POWER_SUPPLY_PROP_CURRENT_NOW,
189 POWER_SUPPLY_PROP_CAPACITY,
190 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
191 POWER_SUPPLY_PROP_TEMP,
192 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
193 POWER_SUPPLY_PROP_TECHNOLOGY,
194 POWER_SUPPLY_PROP_CHARGE_FULL,
195 POWER_SUPPLY_PROP_CHARGE_NOW,
196 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
197 POWER_SUPPLY_PROP_CYCLE_COUNT,
198 POWER_SUPPLY_PROP_POWER_AVG,
199 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500200 POWER_SUPPLY_PROP_MANUFACTURER,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100201};
202
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100203static unsigned int poll_interval = 360;
204module_param(poll_interval, uint, 0644);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500205MODULE_PARM_DESC(poll_interval,
206 "battery poll interval in seconds - 0 disables polling");
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100207
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700208/*
209 * Common code for BQ27x00 devices
210 */
211
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200212static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500213 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700214{
Saranya Gopala66f59b2012-07-13 20:20:07 -0700215 if (di->chip == BQ27425)
216 return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100217 return di->bus.read(di, reg, single);
218}
219
220/*
Saranya Gopala66f59b2012-07-13 20:20:07 -0700221 * Higher versions of the chip like BQ27425 and BQ27500
222 * differ from BQ27000 and BQ27200 in calculation of certain
223 * parameters. Hence we need to check for the chip type.
224 */
225static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
226{
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100227 if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742
228 || di->chip == BQ27510)
Saranya Gopala66f59b2012-07-13 20:20:07 -0700229 return true;
230 return false;
231}
232
233/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100234 * Return the battery Relative State-of-Charge
235 * Or < 0 if something fails.
236 */
237static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
238{
239 int rsoc;
240
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700241 if (di->chip == BQ27500 || di->chip == BQ27742)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100242 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100243 else if (di->chip == BQ27510)
244 rsoc = bq27x00_read(di, BQ27510_REG_SOC, false);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700245 else if (di->chip == BQ27425)
246 rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100247 else
248 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
249
250 if (rsoc < 0)
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100251 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100252
253 return rsoc;
254}
255
256/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100257 * Return a battery charge value in µAh
258 * Or < 0 if something fails.
259 */
260static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
261{
262 int charge;
263
264 charge = bq27x00_read(di, reg, false);
265 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100266 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
267 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100268 return charge;
269 }
270
Saranya Gopala66f59b2012-07-13 20:20:07 -0700271 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100272 charge *= 1000;
273 else
274 charge = charge * 3570 / BQ27000_RS;
275
276 return charge;
277}
278
279/*
280 * Return the battery Nominal available capaciy in µAh
281 * Or < 0 if something fails.
282 */
283static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
284{
Pali Roháre59ec4a2012-11-10 15:42:48 +0100285 int flags;
286 bool is_bq27500 = di->chip == BQ27500;
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700287 bool is_bq27742 = di->chip == BQ27742;
Pali Roháre59ec4a2012-11-10 15:42:48 +0100288 bool is_higher = bq27xxx_is_chip_version_higher(di);
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700289 bool flags_1b = !(is_bq27500 || is_bq27742);
Pali Roháre59ec4a2012-11-10 15:42:48 +0100290
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700291 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
Pali Roháre59ec4a2012-11-10 15:42:48 +0100292 if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
293 return -ENODATA;
294
Pali Rohár631c17e2011-02-01 00:08:02 +0100295 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
296}
297
298/*
299 * Return the battery Last measured discharge in µAh
300 * Or < 0 if something fails.
301 */
302static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
303{
304 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
305}
306
307/*
308 * Return the battery Initial last measured discharge in µAh
309 * Or < 0 if something fails.
310 */
311static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
312{
313 int ilmd;
314
Eric Bénard9dbf5a22014-11-12 23:04:08 +0100315 if (bq27xxx_is_chip_version_higher(di)) {
316 if (di->chip == BQ27425)
317 ilmd = bq27x00_read(di, BQ27425_REG_DCAP, false);
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100318 else if (di->chip == BQ27510)
319 ilmd = bq27x00_read(di, BQ27510_REG_DCAP, false);
Eric Bénard9dbf5a22014-11-12 23:04:08 +0100320 else
321 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500322 } else {
Pali Rohár631c17e2011-02-01 00:08:02 +0100323 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500324 }
Pali Rohár631c17e2011-02-01 00:08:02 +0100325
326 if (ilmd < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100327 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Pali Rohár631c17e2011-02-01 00:08:02 +0100328 return ilmd;
329 }
330
Saranya Gopala66f59b2012-07-13 20:20:07 -0700331 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100332 ilmd *= 1000;
333 else
334 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
335
336 return ilmd;
337}
338
339/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100340 * Return the battery Available energy in µWh
341 * Or < 0 if something fails.
342 */
343static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
344{
345 int ae;
346
347 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
348 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100349 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100350 return ae;
351 }
352
353 if (di->chip == BQ27500)
354 ae *= 1000;
355 else
356 ae = ae * 29200 / BQ27000_RS;
357
358 return ae;
359}
360
361/*
Pali Rohár5dc34432013-02-02 11:06:09 +0100362 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +0100363 * Or < 0 if something fails.
364 */
365static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
366{
367 int temp;
368
369 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
370 if (temp < 0) {
371 dev_err(di->dev, "error reading temperature\n");
372 return temp;
373 }
374
Pali Rohár5dc34432013-02-02 11:06:09 +0100375 if (!bq27xxx_is_chip_version_higher(di))
376 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +0100377
378 return temp;
379}
380
381/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100382 * Return the battery Cycle count total
383 * Or < 0 if something fails.
384 */
385static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
386{
387 int cyct;
388
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100389 if (di->chip == BQ27510)
390 cyct = bq27x00_read(di, BQ27510_REG_CYCT, false);
391 else
392 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100393 if (cyct < 0)
394 dev_err(di->dev, "error reading cycle count total\n");
395
396 return cyct;
397}
398
399/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100400 * Read a time register.
401 * Return < 0 if something fails.
402 */
403static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
404{
405 int tval;
406
407 tval = bq27x00_read(di, reg, false);
408 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100409 dev_dbg(di->dev, "error reading time register %02x: %d\n",
410 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100411 return tval;
412 }
413
414 if (tval == 65535)
415 return -ENODATA;
416
417 return tval * 60;
418}
419
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530420/*
421 * Read a power avg register.
422 * Return < 0 if something fails.
423 */
424static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
425{
426 int tval;
427
428 tval = bq27x00_read(di, reg, false);
429 if (tval < 0) {
430 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
431 reg, tval);
432 return tval;
433 }
434
435 if (di->chip == BQ27500)
436 return tval;
437 else
438 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
439}
440
441/*
442 * Read flag register.
443 * Return < 0 if something fails.
444 */
445static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
446{
447 int tval;
448
449 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
450 if (tval < 0) {
451 dev_err(di->dev, "error reading flag register:%d\n", tval);
452 return tval;
453 }
454
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500455 if (di->chip == BQ27500) {
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530456 if (tval & BQ27500_FLAG_SOCF)
457 tval = POWER_SUPPLY_HEALTH_DEAD;
458 else if (tval & BQ27500_FLAG_OTC)
459 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
460 else
461 tval = POWER_SUPPLY_HEALTH_GOOD;
462 return tval;
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100463 } else if (di->chip == BQ27510) {
464 if (tval & BQ27500_FLAG_OTC)
465 return POWER_SUPPLY_HEALTH_OVERHEAT;
466 return POWER_SUPPLY_HEALTH_GOOD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530467 } else {
468 if (tval & BQ27000_FLAG_EDV1)
469 tval = POWER_SUPPLY_HEALTH_DEAD;
470 else
471 tval = POWER_SUPPLY_HEALTH_GOOD;
472 return tval;
473 }
474
475 return -1;
476}
477
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100478static void bq27x00_update(struct bq27x00_device_info *di)
479{
480 struct bq27x00_reg_cache cache = {0, };
481 bool is_bq27500 = di->chip == BQ27500;
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100482 bool is_bq27510 = di->chip == BQ27510;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700483 bool is_bq27425 = di->chip == BQ27425;
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700484 bool is_bq27742 = di->chip == BQ27742;
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700485 bool flags_1b = !(is_bq27500 || is_bq27742);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100486
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700487 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
Marek Belisko3dd843e2014-06-30 21:49:56 +0200488 if ((cache.flags & 0xff) == 0xff)
489 /* read error */
490 cache.flags = -1;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100491 if (cache.flags >= 0) {
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100492 if (!is_bq27500 && !is_bq27425 && !is_bq27742 && !is_bq27510
Saranya Gopala66f59b2012-07-13 20:20:07 -0700493 && (cache.flags & BQ27000_FLAG_CI)) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100494 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +0100495 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100496 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100497 cache.time_to_empty = -ENODATA;
498 cache.time_to_empty_avg = -ENODATA;
499 cache.time_to_full = -ENODATA;
500 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530501 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100502 } else {
503 cache.capacity = bq27x00_battery_read_rsoc(di);
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100504 if (is_bq27742 || is_bq27510)
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700505 cache.time_to_empty =
506 bq27x00_battery_read_time(di,
507 BQ27x00_REG_TTE);
508 else if (!is_bq27425) {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700509 cache.energy = bq27x00_battery_read_energy(di);
510 cache.time_to_empty =
511 bq27x00_battery_read_time(di,
512 BQ27x00_REG_TTE);
513 cache.time_to_empty_avg =
514 bq27x00_battery_read_time(di,
515 BQ27x00_REG_TTECP);
516 cache.time_to_full =
517 bq27x00_battery_read_time(di,
518 BQ27x00_REG_TTF);
519 }
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700520 cache.charge_full = bq27x00_battery_read_lmd(di);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530521 cache.health = bq27x00_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100522 }
Pali Rohárd149e982011-11-01 01:43:07 +0100523 cache.temperature = bq27x00_battery_read_temperature(di);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700524 if (!is_bq27425)
525 cache.cycle_count = bq27x00_battery_read_cyct(di);
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700526 if (is_bq27742)
527 cache.power_avg =
528 bq27x00_battery_read_pwr_avg(di,
529 BQ27742_POWER_AVG);
530 else
531 cache.power_avg =
532 bq27x00_battery_read_pwr_avg(di,
533 BQ27x00_POWER_AVG);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100534
Pali Rohár631c17e2011-02-01 00:08:02 +0100535 /* We only have to read charge design full once */
536 if (di->charge_design_full <= 0)
537 di->charge_design_full = bq27x00_battery_read_ilmd(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100538 }
539
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700540 if (di->cache.capacity != cache.capacity)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100541 power_supply_changed(di->bat);
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700542
543 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
544 di->cache = cache;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100545
546 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700547}
548
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100549static void bq27x00_battery_poll(struct work_struct *work)
550{
551 struct bq27x00_device_info *di =
552 container_of(work, struct bq27x00_device_info, work.work);
553
554 bq27x00_update(di);
555
556 if (poll_interval > 0) {
557 /* The timer does not have to be accurate. */
558 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
559 schedule_delayed_work(&di->work, poll_interval * HZ);
560 }
561}
562
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700563/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100564 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700565 * Note that current can be negative signed as well
566 * Or 0 if something fails.
567 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100568static int bq27x00_battery_current(struct bq27x00_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500569 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700570{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100571 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100572 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700573
Pali Rohárb68f6212011-11-01 01:43:03 +0100574 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100575 if (curr < 0) {
576 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100577 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100578 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200579
Saranya Gopala66f59b2012-07-13 20:20:07 -0700580 if (bq27xxx_is_chip_version_higher(di)) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200581 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100582 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200583 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100584 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
585 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200586 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74fd2010-02-27 17:06:44 +0200587 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200588 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100589
590 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700591 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200592
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100593 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700594}
595
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200596static int bq27x00_battery_status(struct bq27x00_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500597 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200598{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200599 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200600
Saranya Gopala66f59b2012-07-13 20:20:07 -0700601 if (bq27xxx_is_chip_version_higher(di)) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100602 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200603 status = POWER_SUPPLY_STATUS_FULL;
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400604 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200605 status = POWER_SUPPLY_STATUS_DISCHARGING;
Pali Rohár270968c2011-11-01 01:43:08 +0100606 else
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400607 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200608 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200609 if (di->cache.flags & BQ27000_FLAG_FC)
610 status = POWER_SUPPLY_STATUS_FULL;
611 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200612 status = POWER_SUPPLY_STATUS_CHARGING;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100613 else if (power_supply_am_i_supplied(di->bat))
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200614 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200615 else
616 status = POWER_SUPPLY_STATUS_DISCHARGING;
617 }
618
619 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100620
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200621 return 0;
622}
623
Pali Rohárd66bab32011-11-01 01:43:04 +0100624static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500625 union power_supply_propval *val)
Pali Rohárd66bab32011-11-01 01:43:04 +0100626{
627 int level;
628
Saranya Gopala66f59b2012-07-13 20:20:07 -0700629 if (bq27xxx_is_chip_version_higher(di)) {
Pali Rohárd66bab32011-11-01 01:43:04 +0100630 if (di->cache.flags & BQ27500_FLAG_FC)
631 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
632 else if (di->cache.flags & BQ27500_FLAG_SOC1)
633 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
634 else if (di->cache.flags & BQ27500_FLAG_SOCF)
635 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
636 else
637 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
638 } else {
639 if (di->cache.flags & BQ27000_FLAG_FC)
640 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
641 else if (di->cache.flags & BQ27000_FLAG_EDV1)
642 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
643 else if (di->cache.flags & BQ27000_FLAG_EDVF)
644 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
645 else
646 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
647 }
648
649 val->intval = level;
650
651 return 0;
652}
653
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200654/*
Thomas Weber91fe4d52012-02-17 17:46:21 +0100655 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100656 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200657 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100658static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500659 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200660{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100661 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200662
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100663 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100664 if (volt < 0) {
665 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100666 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100667 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200668
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100669 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200670
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100671 return 0;
672}
673
674static int bq27x00_simple_value(int value,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500675 union power_supply_propval *val)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100676{
677 if (value < 0)
678 return value;
679
680 val->intval = value;
681
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200682 return 0;
683}
684
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700685static int bq27x00_battery_get_property(struct power_supply *psy,
686 enum power_supply_property psp,
687 union power_supply_propval *val)
688{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200689 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100690 struct bq27x00_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200691
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100692 mutex_lock(&di->lock);
693 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
694 cancel_delayed_work_sync(&di->work);
695 bq27x00_battery_poll(&di->work.work);
696 }
697 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100698
699 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200700 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700701
702 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200703 case POWER_SUPPLY_PROP_STATUS:
704 ret = bq27x00_battery_status(di, val);
705 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700706 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100707 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200708 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700709 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100710 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700711 break;
712 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100713 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700714 break;
715 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100716 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700717 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100718 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
719 ret = bq27x00_battery_capacity_level(di, val);
720 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700721 case POWER_SUPPLY_PROP_TEMP:
Pali Rohárd149e982011-11-01 01:43:07 +0100722 ret = bq27x00_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +0100723 if (ret == 0)
724 val->intval -= 2731;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700725 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200726 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100727 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200728 break;
729 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100730 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200731 break;
732 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100733 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200734 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200735 case POWER_SUPPLY_PROP_TECHNOLOGY:
736 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
737 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100738 case POWER_SUPPLY_PROP_CHARGE_NOW:
739 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
740 break;
741 case POWER_SUPPLY_PROP_CHARGE_FULL:
742 ret = bq27x00_simple_value(di->cache.charge_full, val);
743 break;
744 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
745 ret = bq27x00_simple_value(di->charge_design_full, val);
746 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200747 case POWER_SUPPLY_PROP_CYCLE_COUNT:
748 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100749 break;
750 case POWER_SUPPLY_PROP_ENERGY_NOW:
Pali Rohára8f6bd22011-11-01 01:43:06 +0100751 ret = bq27x00_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100752 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530753 case POWER_SUPPLY_PROP_POWER_AVG:
754 ret = bq27x00_simple_value(di->cache.power_avg, val);
755 break;
756 case POWER_SUPPLY_PROP_HEALTH:
757 ret = bq27x00_simple_value(di->cache.health, val);
758 break;
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500759 case POWER_SUPPLY_PROP_MANUFACTURER:
760 val->strval = BQ27XXX_MANUFACTURER;
761 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700762 default:
763 return -EINVAL;
764 }
765
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200766 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700767}
768
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100769static void bq27x00_external_power_changed(struct power_supply *psy)
770{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100771 struct bq27x00_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100772
773 cancel_delayed_work_sync(&di->work);
774 schedule_delayed_work(&di->work, 0);
775}
776
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100777static int bq27x00_powersupply_init(struct bq27x00_device_info *di,
778 const char *name)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700779{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200780 int ret;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100781 struct power_supply_desc *psy_desc;
782 struct power_supply_config psy_cfg = { .drv_data = di, };
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200783
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100784 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
785 if (!psy_desc)
786 return -ENOMEM;
787
788 psy_desc->name = name;
789 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700790 if (di->chip == BQ27425) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100791 psy_desc->properties = bq27425_battery_props;
792 psy_desc->num_properties = ARRAY_SIZE(bq27425_battery_props);
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700793 } else if (di->chip == BQ27742) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100794 psy_desc->properties = bq27742_battery_props;
795 psy_desc->num_properties = ARRAY_SIZE(bq27742_battery_props);
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100796 } else if (di->chip == BQ27510) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100797 psy_desc->properties = bq27510_battery_props;
798 psy_desc->num_properties = ARRAY_SIZE(bq27510_battery_props);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700799 } else {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100800 psy_desc->properties = bq27x00_battery_props;
801 psy_desc->num_properties = ARRAY_SIZE(bq27x00_battery_props);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700802 }
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100803 psy_desc->get_property = bq27x00_battery_get_property;
804 psy_desc->external_power_changed = bq27x00_external_power_changed;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100805
806 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
807 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200808
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100809 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
810 if (IS_ERR(di->bat)) {
811 ret = PTR_ERR(di->bat);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200812 dev_err(di->dev, "failed to register battery: %d\n", ret);
813 return ret;
814 }
815
816 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
817
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100818 bq27x00_update(di);
819
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200820 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700821}
822
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100823static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
824{
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100825 /*
826 * power_supply_unregister call bq27x00_battery_get_property which
827 * call bq27x00_battery_poll.
828 * Make sure that bq27x00_battery_poll will not call
829 * schedule_delayed_work again after unregister (which cause OOPS).
830 */
831 poll_interval = 0;
832
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100833 cancel_delayed_work_sync(&di->work);
834
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100835 power_supply_unregister(di->bat);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100836
837 mutex_destroy(&di->lock);
838}
839
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200840/* i2c specific code */
841#ifdef CONFIG_BATTERY_BQ27X00_I2C
842
843/* If the system has several batteries we need a different name for each
844 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700845 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200846static DEFINE_IDR(battery_id);
847static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700848
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100849static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700850{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200851 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200852 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700853 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100854 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700855
856 if (!client->adapter)
857 return -ENODEV;
858
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200859 msg[0].addr = client->addr;
860 msg[0].flags = 0;
861 msg[0].buf = &reg;
862 msg[0].len = sizeof(reg);
863 msg[1].addr = client->addr;
864 msg[1].flags = I2C_M_RD;
865 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100866 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200867 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100868 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200869 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100870
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200871 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100872 if (ret < 0)
873 return ret;
874
875 if (!single)
876 ret = get_unaligned_le16(data);
877 else
878 ret = data[0];
879
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100880 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700881}
882
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200883static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700884 const struct i2c_device_id *id)
885{
886 char *name;
887 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700888 int num;
889 int retval = 0;
890
891 /* Get new ID for the new battery device */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700892 mutex_lock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -0800893 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700894 mutex_unlock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -0800895 if (num < 0)
896 return num;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700897
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100898 name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700899 if (!name) {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700900 retval = -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100901 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700902 }
903
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530904 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700905 if (!di) {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700906 retval = -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100907 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700908 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700909
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200910 di->id = num;
911 di->dev = &client->dev;
912 di->chip = id->driver_data;
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200913 di->bus.read = &bq27x00_read_i2c;
914
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100915 retval = bq27x00_powersupply_init(di, name);
Julia Lawallf7760992012-08-19 10:44:20 +0200916 if (retval)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100917 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700918
919 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700920
921 return 0;
922
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100923batt_failed:
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700924 mutex_lock(&battery_mutex);
925 idr_remove(&battery_id, num);
926 mutex_unlock(&battery_mutex);
927
928 return retval;
929}
930
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200931static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700932{
933 struct bq27x00_device_info *di = i2c_get_clientdata(client);
934
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100935 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700936
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700937 mutex_lock(&battery_mutex);
938 idr_remove(&battery_id, di->id);
939 mutex_unlock(&battery_mutex);
940
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700941 return 0;
942}
943
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200944static const struct i2c_device_id bq27x00_id[] = {
945 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
946 { "bq27500", BQ27500 },
Saranya Gopala66f59b2012-07-13 20:20:07 -0700947 { "bq27425", BQ27425 },
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700948 { "bq27742", BQ27742 },
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100949 { "bq27510", BQ27510 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700950 {},
951};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100952MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700953
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200954static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700955 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200956 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700957 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200958 .probe = bq27x00_battery_probe,
959 .remove = bq27x00_battery_remove,
960 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700961};
962
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200963static inline int bq27x00_battery_i2c_init(void)
964{
965 int ret = i2c_add_driver(&bq27x00_battery_driver);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500966
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200967 if (ret)
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500968 pr_err("Unable to register BQ27x00 i2c driver\n");
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200969
970 return ret;
971}
972
973static inline void bq27x00_battery_i2c_exit(void)
974{
975 i2c_del_driver(&bq27x00_battery_driver);
976}
977
978#else
979
980static inline int bq27x00_battery_i2c_init(void) { return 0; }
981static inline void bq27x00_battery_i2c_exit(void) {};
982
983#endif
984
985/* platform specific code */
986#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
987
988static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500989 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200990{
991 struct device *dev = di->dev;
992 struct bq27000_platform_data *pdata = dev->platform_data;
993 unsigned int timeout = 3;
994 int upper, lower;
995 int temp;
996
997 if (!single) {
998 /* Make sure the value has not changed in between reading the
999 * lower and the upper part */
1000 upper = pdata->read(dev, reg + 1);
1001 do {
1002 temp = upper;
1003 if (upper < 0)
1004 return upper;
1005
1006 lower = pdata->read(dev, reg);
1007 if (lower < 0)
1008 return lower;
1009
1010 upper = pdata->read(dev, reg + 1);
1011 } while (temp != upper && --timeout);
1012
1013 if (timeout == 0)
1014 return -EIO;
1015
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001016 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001017 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001018
1019 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001020}
1021
Bill Pembertonc8afa642012-11-19 13:22:23 -05001022static int bq27000_battery_probe(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001023{
1024 struct bq27x00_device_info *di;
1025 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001026 const char *name;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001027
1028 if (!pdata) {
1029 dev_err(&pdev->dev, "no platform_data supplied\n");
1030 return -EINVAL;
1031 }
1032
1033 if (!pdata->read) {
1034 dev_err(&pdev->dev, "no hdq read callback supplied\n");
1035 return -EINVAL;
1036 }
1037
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +05301038 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001039 if (!di)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001040 return -ENOMEM;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001041
1042 platform_set_drvdata(pdev, di);
1043
1044 di->dev = &pdev->dev;
1045 di->chip = BQ27000;
1046
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001047 name = pdata->name ?: dev_name(&pdev->dev);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001048 di->bus.read = &bq27000_read_platform;
1049
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001050 return bq27x00_powersupply_init(di, name);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001051}
1052
Bill Pemberton415ec692012-11-19 13:26:07 -05001053static int bq27000_battery_remove(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001054{
1055 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
1056
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001057 bq27x00_powersupply_unregister(di);
1058
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001059 return 0;
1060}
1061
1062static struct platform_driver bq27000_battery_driver = {
1063 .probe = bq27000_battery_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001064 .remove = bq27000_battery_remove,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001065 .driver = {
1066 .name = "bq27000-battery",
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001067 },
1068};
1069
1070static inline int bq27x00_battery_platform_init(void)
1071{
1072 int ret = platform_driver_register(&bq27000_battery_driver);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001073
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001074 if (ret)
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001075 pr_err("Unable to register BQ27000 platform driver\n");
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001076
1077 return ret;
1078}
1079
1080static inline void bq27x00_battery_platform_exit(void)
1081{
1082 platform_driver_unregister(&bq27000_battery_driver);
1083}
1084
1085#else
1086
1087static inline int bq27x00_battery_platform_init(void) { return 0; }
1088static inline void bq27x00_battery_platform_exit(void) {};
1089
1090#endif
1091
1092/*
1093 * Module stuff
1094 */
1095
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001096static int __init bq27x00_battery_init(void)
1097{
1098 int ret;
1099
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001100 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001101 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001102 return ret;
1103
1104 ret = bq27x00_battery_platform_init();
1105 if (ret)
1106 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001107
1108 return ret;
1109}
1110module_init(bq27x00_battery_init);
1111
1112static void __exit bq27x00_battery_exit(void)
1113{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001114 bq27x00_battery_platform_exit();
1115 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001116}
1117module_exit(bq27x00_battery_exit);
1118
Marek Belisko8ebb7e92015-04-30 22:05:58 +02001119#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
1120MODULE_ALIAS("platform:bq27000-battery");
1121#endif
1122
1123#ifdef CONFIG_BATTERY_BQ27X00_I2C
1124MODULE_ALIAS("i2c:bq27000-battery");
1125#endif
1126
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001127MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1128MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1129MODULE_LICENSE("GPL");