blob: b6b98378faa32b50c0e1900c76ca4e26da3bd6c1 [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
Pali Rohár631c17e2011-02-01 00:08:02 +010042#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070043
44#define BQ27x00_REG_TEMP 0x06
45#define BQ27x00_REG_VOLT 0x08
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070046#define BQ27x00_REG_AI 0x14
47#define BQ27x00_REG_FLAGS 0x0A
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020048#define BQ27x00_REG_TTE 0x16
49#define BQ27x00_REG_TTF 0x18
50#define BQ27x00_REG_TTECP 0x26
Thomas Weber202e0112012-02-16 08:58:15 +010051#define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
Pali Rohár631c17e2011-02-01 00:08:02 +010052#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
53#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
Thomas Weber202e0112012-02-16 08:58:15 +010054#define BQ27x00_REG_AE 0x22 /* Available energy */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053055#define BQ27x00_POWER_AVG 0x24
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070056
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020057#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
Pali Rohár631c17e2011-02-01 00:08:02 +010058#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
Pali Rohárd66bab32011-11-01 01:43:04 +010059#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 */
Pali Rohár4b226c22011-11-01 01:43:05 +010061#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +020062#define BQ27000_FLAG_FC BIT(5)
Pali Rohárd66bab32011-11-01 01:43:04 +010063#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020064
Pali Rohárbf7d4142011-02-01 00:23:13 +010065#define BQ27500_REG_SOC 0x2C
Pali Rohár631c17e2011-02-01 00:08:02 +010066#define BQ27500_REG_DCAP 0x3C /* Design capacity */
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +040067#define BQ27500_FLAG_DSC BIT(0)
Pali Rohárd66bab32011-11-01 01:43:04 +010068#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
69#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +040070#define BQ27500_FLAG_FC BIT(9)
Syed Rafiuddin9903e622012-05-17 12:51:15 +053071#define BQ27500_FLAG_OTC BIT(15)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020072
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -070073#define BQ27742_POWER_AVG 0x76
74
Alexandre Bellonif46bf822015-02-28 18:59:28 +010075#define BQ27510_REG_SOC 0x20
76#define BQ27510_REG_DCAP 0x2E /* Design capacity */
77#define BQ27510_REG_CYCT 0x1E /* Cycle count total */
78
Saranya Gopala66f59b2012-07-13 20:20:07 -070079/* bq27425 register addresses are same as bq27x00 addresses minus 4 */
80#define BQ27425_REG_OFFSET 0x04
Eric Bénard9dbf5a22014-11-12 23:04:08 +010081#define BQ27425_REG_SOC (0x1C + BQ27425_REG_OFFSET)
82#define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET)
Saranya Gopala66f59b2012-07-13 20:20:07 -070083
Pali Rohára2e51182010-05-24 20:52:13 +020084#define BQ27000_RS 20 /* Resistor sense */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053085#define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
Pali Rohára2e51182010-05-24 20:52:13 +020086
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070087struct bq27x00_device_info;
88struct bq27x00_access_methods {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010089 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070090};
91
Alexandre Bellonif46bf822015-02-28 18:59:28 +010092enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742, BQ27510};
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020093
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010094struct bq27x00_reg_cache {
95 int temperature;
96 int time_to_empty;
97 int time_to_empty_avg;
98 int time_to_full;
Pali Rohár631c17e2011-02-01 00:08:02 +010099 int charge_full;
Pali Rohár73c244a2011-04-30 23:59:35 +0200100 int cycle_count;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100101 int capacity;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100102 int energy;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100103 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530104 int power_avg;
105 int health;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100106};
107
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700108struct bq27x00_device_info {
109 struct device *dev;
110 int id;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200111 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700112
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100113 struct bq27x00_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +0100114 int charge_design_full;
115
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100116 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100117 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100118
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100119 struct power_supply *bat;
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200120
121 struct bq27x00_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100122
123 struct mutex lock;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700124};
125
126static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200127 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700128 POWER_SUPPLY_PROP_PRESENT,
129 POWER_SUPPLY_PROP_VOLTAGE_NOW,
130 POWER_SUPPLY_PROP_CURRENT_NOW,
131 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100132 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700133 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200134 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
135 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
136 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200137 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100138 POWER_SUPPLY_PROP_CHARGE_FULL,
139 POWER_SUPPLY_PROP_CHARGE_NOW,
140 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200141 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100142 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530143 POWER_SUPPLY_PROP_POWER_AVG,
144 POWER_SUPPLY_PROP_HEALTH,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700145};
146
Saranya Gopala66f59b2012-07-13 20:20:07 -0700147static 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
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700161static 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
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100179static 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
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100197static unsigned int poll_interval = 360;
198module_param(poll_interval, uint, 0644);
199MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
200 "0 disables polling");
201
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700202/*
203 * Common code for BQ27x00 devices
204 */
205
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200206static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100207 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700208{
Saranya Gopala66f59b2012-07-13 20:20:07 -0700209 if (di->chip == BQ27425)
210 return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100211 return di->bus.read(di, reg, single);
212}
213
214/*
Saranya Gopala66f59b2012-07-13 20:20:07 -0700215 * 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 */
219static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
220{
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100221 if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742
222 || di->chip == BQ27510)
Saranya Gopala66f59b2012-07-13 20:20:07 -0700223 return true;
224 return false;
225}
226
227/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100228 * Return the battery Relative State-of-Charge
229 * Or < 0 if something fails.
230 */
231static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
232{
233 int rsoc;
234
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700235 if (di->chip == BQ27500 || di->chip == BQ27742)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100236 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100237 else if (di->chip == BQ27510)
238 rsoc = bq27x00_read(di, BQ27510_REG_SOC, false);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700239 else if (di->chip == BQ27425)
240 rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100241 else
242 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
243
244 if (rsoc < 0)
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100245 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100246
247 return rsoc;
248}
249
250/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100251 * Return a battery charge value in µAh
252 * Or < 0 if something fails.
253 */
254static 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) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100260 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
261 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100262 return charge;
263 }
264
Saranya Gopala66f59b2012-07-13 20:20:07 -0700265 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100266 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 */
277static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
278{
Pali Roháre59ec4a2012-11-10 15:42:48 +0100279 int flags;
280 bool is_bq27500 = di->chip == BQ27500;
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700281 bool is_bq27742 = di->chip == BQ27742;
Pali Roháre59ec4a2012-11-10 15:42:48 +0100282 bool is_higher = bq27xxx_is_chip_version_higher(di);
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700283 bool flags_1b = !(is_bq27500 || is_bq27742);
Pali Roháre59ec4a2012-11-10 15:42:48 +0100284
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700285 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
Pali Roháre59ec4a2012-11-10 15:42:48 +0100286 if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
287 return -ENODATA;
288
Pali Rohár631c17e2011-02-01 00:08:02 +0100289 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 */
296static 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 */
305static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
306{
307 int ilmd;
308
Eric Bénard9dbf5a22014-11-12 23:04:08 +0100309 if (bq27xxx_is_chip_version_higher(di)) {
310 if (di->chip == BQ27425)
311 ilmd = bq27x00_read(di, BQ27425_REG_DCAP, false);
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100312 else if (di->chip == BQ27510)
313 ilmd = bq27x00_read(di, BQ27510_REG_DCAP, false);
Eric Bénard9dbf5a22014-11-12 23:04:08 +0100314 else
315 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
316 } else
Pali Rohár631c17e2011-02-01 00:08:02 +0100317 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
318
319 if (ilmd < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100320 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Pali Rohár631c17e2011-02-01 00:08:02 +0100321 return ilmd;
322 }
323
Saranya Gopala66f59b2012-07-13 20:20:07 -0700324 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100325 ilmd *= 1000;
326 else
327 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
328
329 return ilmd;
330}
331
332/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100333 * Return the battery Available energy in µWh
334 * Or < 0 if something fails.
335 */
336static 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) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100342 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100343 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/*
Pali Rohár5dc34432013-02-02 11:06:09 +0100355 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +0100356 * Or < 0 if something fails.
357 */
358static 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
Pali Rohár5dc34432013-02-02 11:06:09 +0100368 if (!bq27xxx_is_chip_version_higher(di))
369 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +0100370
371 return temp;
372}
373
374/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100375 * Return the battery Cycle count total
376 * Or < 0 if something fails.
377 */
378static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
379{
380 int cyct;
381
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100382 if (di->chip == BQ27510)
383 cyct = bq27x00_read(di, BQ27510_REG_CYCT, false);
384 else
385 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100386 if (cyct < 0)
387 dev_err(di->dev, "error reading cycle count total\n");
388
389 return cyct;
390}
391
392/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100393 * Read a time register.
394 * Return < 0 if something fails.
395 */
396static 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) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100402 dev_dbg(di->dev, "error reading time register %02x: %d\n",
403 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100404 return tval;
405 }
406
407 if (tval == 65535)
408 return -ENODATA;
409
410 return tval * 60;
411}
412
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530413/*
414 * Read a power avg register.
415 * Return < 0 if something fails.
416 */
417static 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 */
438static 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;
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100456 } else if (di->chip == BQ27510) {
457 if (tval & BQ27500_FLAG_OTC)
458 return POWER_SUPPLY_HEALTH_OVERHEAT;
459 return POWER_SUPPLY_HEALTH_GOOD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530460 } 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
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100471static void bq27x00_update(struct bq27x00_device_info *di)
472{
473 struct bq27x00_reg_cache cache = {0, };
474 bool is_bq27500 = di->chip == BQ27500;
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100475 bool is_bq27510 = di->chip == BQ27510;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700476 bool is_bq27425 = di->chip == BQ27425;
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700477 bool is_bq27742 = di->chip == BQ27742;
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700478 bool flags_1b = !(is_bq27500 || is_bq27742);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100479
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700480 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
Marek Belisko3dd843e2014-06-30 21:49:56 +0200481 if ((cache.flags & 0xff) == 0xff)
482 /* read error */
483 cache.flags = -1;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100484 if (cache.flags >= 0) {
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100485 if (!is_bq27500 && !is_bq27425 && !is_bq27742 && !is_bq27510
Saranya Gopala66f59b2012-07-13 20:20:07 -0700486 && (cache.flags & BQ27000_FLAG_CI)) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100487 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +0100488 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100489 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100490 cache.time_to_empty = -ENODATA;
491 cache.time_to_empty_avg = -ENODATA;
492 cache.time_to_full = -ENODATA;
493 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530494 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100495 } else {
496 cache.capacity = bq27x00_battery_read_rsoc(di);
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100497 if (is_bq27742 || is_bq27510)
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700498 cache.time_to_empty =
499 bq27x00_battery_read_time(di,
500 BQ27x00_REG_TTE);
501 else if (!is_bq27425) {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700502 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 }
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700513 cache.charge_full = bq27x00_battery_read_lmd(di);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530514 cache.health = bq27x00_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100515 }
Pali Rohárd149e982011-11-01 01:43:07 +0100516 cache.temperature = bq27x00_battery_read_temperature(di);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700517 if (!is_bq27425)
518 cache.cycle_count = bq27x00_battery_read_cyct(di);
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700519 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);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100527
Pali Rohár631c17e2011-02-01 00:08:02 +0100528 /* 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);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100531 }
532
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700533 if (di->cache.capacity != cache.capacity)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100534 power_supply_changed(di->bat);
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700535
536 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
537 di->cache = cache;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100538
539 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700540}
541
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100542static 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
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700556/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100557 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700558 * Note that current can be negative signed as well
559 * Or 0 if something fails.
560 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100561static int bq27x00_battery_current(struct bq27x00_device_info *di,
562 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700563{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100564 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100565 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700566
Pali Rohárb68f6212011-11-01 01:43:03 +0100567 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100568 if (curr < 0) {
569 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100570 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100571 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200572
Saranya Gopala66f59b2012-07-13 20:20:07 -0700573 if (bq27xxx_is_chip_version_higher(di)) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200574 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100575 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200576 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100577 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
578 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200579 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74fd2010-02-27 17:06:44 +0200580 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200581 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100582
583 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700584 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200585
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100586 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700587}
588
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200589static int bq27x00_battery_status(struct bq27x00_device_info *di,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100590 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200591{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200592 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200593
Saranya Gopala66f59b2012-07-13 20:20:07 -0700594 if (bq27xxx_is_chip_version_higher(di)) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100595 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200596 status = POWER_SUPPLY_STATUS_FULL;
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400597 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200598 status = POWER_SUPPLY_STATUS_DISCHARGING;
Pali Rohár270968c2011-11-01 01:43:08 +0100599 else
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400600 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200601 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200602 if (di->cache.flags & BQ27000_FLAG_FC)
603 status = POWER_SUPPLY_STATUS_FULL;
604 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200605 status = POWER_SUPPLY_STATUS_CHARGING;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100606 else if (power_supply_am_i_supplied(di->bat))
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200607 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200608 else
609 status = POWER_SUPPLY_STATUS_DISCHARGING;
610 }
611
612 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100613
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200614 return 0;
615}
616
Pali Rohárd66bab32011-11-01 01:43:04 +0100617static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
618 union power_supply_propval *val)
619{
620 int level;
621
Saranya Gopala66f59b2012-07-13 20:20:07 -0700622 if (bq27xxx_is_chip_version_higher(di)) {
Pali Rohárd66bab32011-11-01 01:43:04 +0100623 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
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200647/*
Thomas Weber91fe4d52012-02-17 17:46:21 +0100648 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100649 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200650 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100651static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
652 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200653{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100654 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200655
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100656 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100657 if (volt < 0) {
658 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100659 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100660 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200661
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100662 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200663
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100664 return 0;
665}
666
667static 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
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200675 return 0;
676}
677
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700678static int bq27x00_battery_get_property(struct power_supply *psy,
679 enum power_supply_property psp,
680 union power_supply_propval *val)
681{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200682 int ret = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100683 struct bq27x00_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200684
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100685 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);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100691
692 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200693 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700694
695 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200696 case POWER_SUPPLY_PROP_STATUS:
697 ret = bq27x00_battery_status(di, val);
698 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700699 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100700 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200701 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700702 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100703 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700704 break;
705 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100706 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700707 break;
708 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100709 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700710 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100711 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
712 ret = bq27x00_battery_capacity_level(di, val);
713 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700714 case POWER_SUPPLY_PROP_TEMP:
Pali Rohárd149e982011-11-01 01:43:07 +0100715 ret = bq27x00_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +0100716 if (ret == 0)
717 val->intval -= 2731;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700718 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200719 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100720 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200721 break;
722 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100723 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200724 break;
725 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100726 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200727 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200728 case POWER_SUPPLY_PROP_TECHNOLOGY:
729 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
730 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100731 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;
Pali Rohár73c244a2011-04-30 23:59:35 +0200740 case POWER_SUPPLY_PROP_CYCLE_COUNT:
741 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100742 break;
743 case POWER_SUPPLY_PROP_ENERGY_NOW:
Pali Rohára8f6bd22011-11-01 01:43:06 +0100744 ret = bq27x00_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100745 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530746 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;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700752 default:
753 return -EINVAL;
754 }
755
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200756 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700757}
758
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100759static void bq27x00_external_power_changed(struct power_supply *psy)
760{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100761 struct bq27x00_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100762
763 cancel_delayed_work_sync(&di->work);
764 schedule_delayed_work(&di->work, 0);
765}
766
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100767static int bq27x00_powersupply_init(struct bq27x00_device_info *di,
768 const char *name)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700769{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200770 int ret;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100771 struct power_supply_desc *psy_desc;
772 struct power_supply_config psy_cfg = { .drv_data = di, };
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200773
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100774 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;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700780 if (di->chip == BQ27425) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100781 psy_desc->properties = bq27425_battery_props;
782 psy_desc->num_properties = ARRAY_SIZE(bq27425_battery_props);
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700783 } else if (di->chip == BQ27742) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100784 psy_desc->properties = bq27742_battery_props;
785 psy_desc->num_properties = ARRAY_SIZE(bq27742_battery_props);
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100786 } else if (di->chip == BQ27510) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100787 psy_desc->properties = bq27510_battery_props;
788 psy_desc->num_properties = ARRAY_SIZE(bq27510_battery_props);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700789 } else {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100790 psy_desc->properties = bq27x00_battery_props;
791 psy_desc->num_properties = ARRAY_SIZE(bq27x00_battery_props);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700792 }
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100793 psy_desc->get_property = bq27x00_battery_get_property;
794 psy_desc->external_power_changed = bq27x00_external_power_changed;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100795
796 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
797 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200798
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100799 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);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200802 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
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100808 bq27x00_update(di);
809
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200810 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700811}
812
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100813static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
814{
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100815 /*
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
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100823 cancel_delayed_work_sync(&di->work);
824
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100825 power_supply_unregister(di->bat);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100826
827 mutex_destroy(&di->lock);
828}
829
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200830
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...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700836 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200837static DEFINE_IDR(battery_id);
838static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700839
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100840static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700841{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200842 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200843 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700844 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100845 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700846
847 if (!client->adapter)
848 return -ENODEV;
849
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200850 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;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100857 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200858 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100859 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200860 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100861
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200862 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100863 if (ret < 0)
864 return ret;
865
866 if (!single)
867 ret = get_unaligned_le16(data);
868 else
869 ret = data[0];
870
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100871 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700872}
873
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200874static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700875 const struct i2c_device_id *id)
876{
877 char *name;
878 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700879 int num;
880 int retval = 0;
881
882 /* Get new ID for the new battery device */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700883 mutex_lock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -0800884 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700885 mutex_unlock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -0800886 if (num < 0)
887 return num;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700888
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100889 name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700890 if (!name) {
891 dev_err(&client->dev, "failed to allocate device name\n");
892 retval = -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100893 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700894 }
895
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530896 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700897 if (!di) {
898 dev_err(&client->dev, "failed to allocate device info data\n");
899 retval = -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100900 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700901 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700902
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200903 di->id = num;
904 di->dev = &client->dev;
905 di->chip = id->driver_data;
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200906 di->bus.read = &bq27x00_read_i2c;
907
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100908 retval = bq27x00_powersupply_init(di, name);
Julia Lawallf7760992012-08-19 10:44:20 +0200909 if (retval)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100910 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700911
912 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700913
914 return 0;
915
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100916batt_failed:
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700917 mutex_lock(&battery_mutex);
918 idr_remove(&battery_id, num);
919 mutex_unlock(&battery_mutex);
920
921 return retval;
922}
923
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200924static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700925{
926 struct bq27x00_device_info *di = i2c_get_clientdata(client);
927
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100928 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700929
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700930 mutex_lock(&battery_mutex);
931 idr_remove(&battery_id, di->id);
932 mutex_unlock(&battery_mutex);
933
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700934 return 0;
935}
936
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200937static const struct i2c_device_id bq27x00_id[] = {
938 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
939 { "bq27500", BQ27500 },
Saranya Gopala66f59b2012-07-13 20:20:07 -0700940 { "bq27425", BQ27425 },
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700941 { "bq27742", BQ27742 },
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100942 { "bq27510", BQ27510 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700943 {},
944};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100945MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700946
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200947static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700948 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200949 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700950 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200951 .probe = bq27x00_battery_probe,
952 .remove = bq27x00_battery_remove,
953 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700954};
955
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200956static 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
965static inline void bq27x00_battery_i2c_exit(void)
966{
967 i2c_del_driver(&bq27x00_battery_driver);
968}
969
970#else
971
972static inline int bq27x00_battery_i2c_init(void) { return 0; }
973static inline void bq27x00_battery_i2c_exit(void) {};
974
975#endif
976
977/* platform specific code */
978#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
979
980static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100981 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200982{
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
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001008 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001009 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001010
1011 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001012}
1013
Bill Pembertonc8afa642012-11-19 13:22:23 -05001014static int bq27000_battery_probe(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001015{
1016 struct bq27x00_device_info *di;
1017 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001018 const char *name;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001019
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
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +05301030 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001031 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
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001041 name = pdata->name ?: dev_name(&pdev->dev);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001042 di->bus.read = &bq27000_read_platform;
1043
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001044 return bq27x00_powersupply_init(di, name);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001045}
1046
Bill Pemberton415ec692012-11-19 13:26:07 -05001047static int bq27000_battery_remove(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001048{
1049 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
1050
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001051 bq27x00_powersupply_unregister(di);
1052
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001053 return 0;
1054}
1055
1056static struct platform_driver bq27000_battery_driver = {
1057 .probe = bq27000_battery_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001058 .remove = bq27000_battery_remove,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001059 .driver = {
1060 .name = "bq27000-battery",
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001061 },
1062};
1063
1064static 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
1073static inline void bq27x00_battery_platform_exit(void)
1074{
1075 platform_driver_unregister(&bq27000_battery_driver);
1076}
1077
1078#else
1079
1080static inline int bq27x00_battery_platform_init(void) { return 0; }
1081static inline void bq27x00_battery_platform_exit(void) {};
1082
1083#endif
1084
1085/*
1086 * Module stuff
1087 */
1088
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001089static int __init bq27x00_battery_init(void)
1090{
1091 int ret;
1092
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001093 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001094 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001095 return ret;
1096
1097 ret = bq27x00_battery_platform_init();
1098 if (ret)
1099 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001100
1101 return ret;
1102}
1103module_init(bq27x00_battery_init);
1104
1105static void __exit bq27x00_battery_exit(void)
1106{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001107 bq27x00_battery_platform_exit();
1108 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001109}
1110module_exit(bq27x00_battery_exit);
1111
Marek Belisko8ebb7e92015-04-30 22:05:58 +02001112#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
1113MODULE_ALIAS("platform:bq27000-battery");
1114#endif
1115
1116#ifdef CONFIG_BATTERY_BQ27X00_I2C
1117MODULE_ALIAS("i2c:bq27000-battery");
1118#endif
1119
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001120MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1121MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1122MODULE_LICENSE("GPL");