blob: baae2151cb9a72e4b3fcb97a9e43b8e08a560920 [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 *
19 */
Pali Rohár631c17e2011-02-01 00:08:02 +010020
21/*
22 * Datasheets:
23 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
24 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
Saranya Gopala66f59b2012-07-13 20:20:07 -070025 * http://www.ti.com/product/bq27425-g1
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -070026 * http://www.ti.com/product/BQ27742-G1
Pali Rohár631c17e2011-02-01 00:08:02 +010027 */
28
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +053029#include <linux/device.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070030#include <linux/module.h>
31#include <linux/param.h>
32#include <linux/jiffies.h>
33#include <linux/workqueue.h>
34#include <linux/delay.h>
35#include <linux/platform_device.h>
36#include <linux/power_supply.h>
37#include <linux/idr.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070038#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Harvey Harrison8aef7e82008-09-22 14:53:50 -070040#include <asm/unaligned.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070041
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020042#include <linux/power/bq27x00_battery.h>
43
Pali Rohár631c17e2011-02-01 00:08:02 +010044#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070045
46#define BQ27x00_REG_TEMP 0x06
47#define BQ27x00_REG_VOLT 0x08
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070048#define BQ27x00_REG_AI 0x14
49#define BQ27x00_REG_FLAGS 0x0A
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020050#define BQ27x00_REG_TTE 0x16
51#define BQ27x00_REG_TTF 0x18
52#define BQ27x00_REG_TTECP 0x26
Thomas Weber202e0112012-02-16 08:58:15 +010053#define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
Pali Rohár631c17e2011-02-01 00:08:02 +010054#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
55#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
Thomas Weber202e0112012-02-16 08:58:15 +010056#define BQ27x00_REG_AE 0x22 /* Available energy */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053057#define BQ27x00_POWER_AVG 0x24
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070058
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020059#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
Pali Rohár631c17e2011-02-01 00:08:02 +010060#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
Pali Rohárd66bab32011-11-01 01:43:04 +010061#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 */
Pali Rohár4b226c22011-11-01 01:43:05 +010063#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +020064#define BQ27000_FLAG_FC BIT(5)
Pali Rohárd66bab32011-11-01 01:43:04 +010065#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020066
Pali Rohárbf7d4142011-02-01 00:23:13 +010067#define BQ27500_REG_SOC 0x2C
Pali Rohár631c17e2011-02-01 00:08:02 +010068#define BQ27500_REG_DCAP 0x3C /* Design capacity */
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +040069#define BQ27500_FLAG_DSC BIT(0)
Pali Rohárd66bab32011-11-01 01:43:04 +010070#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
71#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +040072#define BQ27500_FLAG_FC BIT(9)
Syed Rafiuddin9903e622012-05-17 12:51:15 +053073#define BQ27500_FLAG_OTC BIT(15)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020074
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -070075#define BQ27742_POWER_AVG 0x76
76
Saranya Gopala66f59b2012-07-13 20:20:07 -070077/* bq27425 register addresses are same as bq27x00 addresses minus 4 */
78#define BQ27425_REG_OFFSET 0x04
Eric Bénard9dbf5a22014-11-12 23:04:08 +010079#define BQ27425_REG_SOC (0x1C + BQ27425_REG_OFFSET)
80#define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET)
Saranya Gopala66f59b2012-07-13 20:20:07 -070081
Pali Rohára2e51182010-05-24 20:52:13 +020082#define BQ27000_RS 20 /* Resistor sense */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053083#define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
Pali Rohára2e51182010-05-24 20:52:13 +020084
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070085struct bq27x00_device_info;
86struct bq27x00_access_methods {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010087 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070088};
89
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -070090enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742};
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020091
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010092struct bq27x00_reg_cache {
93 int temperature;
94 int time_to_empty;
95 int time_to_empty_avg;
96 int time_to_full;
Pali Rohár631c17e2011-02-01 00:08:02 +010097 int charge_full;
Pali Rohár73c244a2011-04-30 23:59:35 +020098 int cycle_count;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010099 int capacity;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100100 int energy;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100101 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530102 int power_avg;
103 int health;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100104};
105
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700106struct bq27x00_device_info {
107 struct device *dev;
108 int id;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200109 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700110
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100111 struct bq27x00_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +0100112 int charge_design_full;
113
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100114 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100115 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100116
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200117 struct power_supply bat;
118
119 struct bq27x00_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100120
121 struct mutex lock;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700122};
123
124static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200125 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700126 POWER_SUPPLY_PROP_PRESENT,
127 POWER_SUPPLY_PROP_VOLTAGE_NOW,
128 POWER_SUPPLY_PROP_CURRENT_NOW,
129 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100130 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700131 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200132 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
133 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
134 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200135 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100136 POWER_SUPPLY_PROP_CHARGE_FULL,
137 POWER_SUPPLY_PROP_CHARGE_NOW,
138 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200139 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100140 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530141 POWER_SUPPLY_PROP_POWER_AVG,
142 POWER_SUPPLY_PROP_HEALTH,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700143};
144
Saranya Gopala66f59b2012-07-13 20:20:07 -0700145static enum power_supply_property bq27425_battery_props[] = {
146 POWER_SUPPLY_PROP_STATUS,
147 POWER_SUPPLY_PROP_PRESENT,
148 POWER_SUPPLY_PROP_VOLTAGE_NOW,
149 POWER_SUPPLY_PROP_CURRENT_NOW,
150 POWER_SUPPLY_PROP_CAPACITY,
151 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
152 POWER_SUPPLY_PROP_TEMP,
153 POWER_SUPPLY_PROP_TECHNOLOGY,
154 POWER_SUPPLY_PROP_CHARGE_FULL,
155 POWER_SUPPLY_PROP_CHARGE_NOW,
156 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
157};
158
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700159static enum power_supply_property bq27742_battery_props[] = {
160 POWER_SUPPLY_PROP_STATUS,
161 POWER_SUPPLY_PROP_PRESENT,
162 POWER_SUPPLY_PROP_VOLTAGE_NOW,
163 POWER_SUPPLY_PROP_CURRENT_NOW,
164 POWER_SUPPLY_PROP_CAPACITY,
165 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
166 POWER_SUPPLY_PROP_TEMP,
167 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
168 POWER_SUPPLY_PROP_TECHNOLOGY,
169 POWER_SUPPLY_PROP_CHARGE_FULL,
170 POWER_SUPPLY_PROP_CHARGE_NOW,
171 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
172 POWER_SUPPLY_PROP_CYCLE_COUNT,
173 POWER_SUPPLY_PROP_POWER_AVG,
174 POWER_SUPPLY_PROP_HEALTH,
175};
176
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100177static unsigned int poll_interval = 360;
178module_param(poll_interval, uint, 0644);
179MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
180 "0 disables polling");
181
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700182/*
183 * Common code for BQ27x00 devices
184 */
185
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200186static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100187 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700188{
Saranya Gopala66f59b2012-07-13 20:20:07 -0700189 if (di->chip == BQ27425)
190 return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100191 return di->bus.read(di, reg, single);
192}
193
194/*
Saranya Gopala66f59b2012-07-13 20:20:07 -0700195 * Higher versions of the chip like BQ27425 and BQ27500
196 * differ from BQ27000 and BQ27200 in calculation of certain
197 * parameters. Hence we need to check for the chip type.
198 */
199static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
200{
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700201 if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742)
Saranya Gopala66f59b2012-07-13 20:20:07 -0700202 return true;
203 return false;
204}
205
206/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100207 * Return the battery Relative State-of-Charge
208 * Or < 0 if something fails.
209 */
210static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
211{
212 int rsoc;
213
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700214 if (di->chip == BQ27500 || di->chip == BQ27742)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100215 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700216 else if (di->chip == BQ27425)
217 rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100218 else
219 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
220
221 if (rsoc < 0)
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100222 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100223
224 return rsoc;
225}
226
227/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100228 * Return a battery charge value in µAh
229 * Or < 0 if something fails.
230 */
231static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
232{
233 int charge;
234
235 charge = bq27x00_read(di, reg, false);
236 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100237 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
238 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100239 return charge;
240 }
241
Saranya Gopala66f59b2012-07-13 20:20:07 -0700242 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100243 charge *= 1000;
244 else
245 charge = charge * 3570 / BQ27000_RS;
246
247 return charge;
248}
249
250/*
251 * Return the battery Nominal available capaciy in µAh
252 * Or < 0 if something fails.
253 */
254static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
255{
Pali Roháre59ec4a2012-11-10 15:42:48 +0100256 int flags;
257 bool is_bq27500 = di->chip == BQ27500;
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700258 bool is_bq27742 = di->chip == BQ27742;
Pali Roháre59ec4a2012-11-10 15:42:48 +0100259 bool is_higher = bq27xxx_is_chip_version_higher(di);
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700260 bool flags_1b = !(is_bq27500 || is_bq27742);
Pali Roháre59ec4a2012-11-10 15:42:48 +0100261
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700262 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
Pali Roháre59ec4a2012-11-10 15:42:48 +0100263 if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
264 return -ENODATA;
265
Pali Rohár631c17e2011-02-01 00:08:02 +0100266 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
267}
268
269/*
270 * Return the battery Last measured discharge in µAh
271 * Or < 0 if something fails.
272 */
273static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
274{
275 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
276}
277
278/*
279 * Return the battery Initial last measured discharge in µAh
280 * Or < 0 if something fails.
281 */
282static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
283{
284 int ilmd;
285
Eric Bénard9dbf5a22014-11-12 23:04:08 +0100286 if (bq27xxx_is_chip_version_higher(di)) {
287 if (di->chip == BQ27425)
288 ilmd = bq27x00_read(di, BQ27425_REG_DCAP, false);
289 else
290 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
291 } else
Pali Rohár631c17e2011-02-01 00:08:02 +0100292 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
293
294 if (ilmd < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100295 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Pali Rohár631c17e2011-02-01 00:08:02 +0100296 return ilmd;
297 }
298
Saranya Gopala66f59b2012-07-13 20:20:07 -0700299 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100300 ilmd *= 1000;
301 else
302 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
303
304 return ilmd;
305}
306
307/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100308 * Return the battery Available energy in µWh
309 * Or < 0 if something fails.
310 */
311static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
312{
313 int ae;
314
315 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
316 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100317 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100318 return ae;
319 }
320
321 if (di->chip == BQ27500)
322 ae *= 1000;
323 else
324 ae = ae * 29200 / BQ27000_RS;
325
326 return ae;
327}
328
329/*
Pali Rohár5dc34432013-02-02 11:06:09 +0100330 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +0100331 * Or < 0 if something fails.
332 */
333static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
334{
335 int temp;
336
337 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
338 if (temp < 0) {
339 dev_err(di->dev, "error reading temperature\n");
340 return temp;
341 }
342
Pali Rohár5dc34432013-02-02 11:06:09 +0100343 if (!bq27xxx_is_chip_version_higher(di))
344 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +0100345
346 return temp;
347}
348
349/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100350 * Return the battery Cycle count total
351 * Or < 0 if something fails.
352 */
353static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
354{
355 int cyct;
356
357 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
358 if (cyct < 0)
359 dev_err(di->dev, "error reading cycle count total\n");
360
361 return cyct;
362}
363
364/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100365 * Read a time register.
366 * Return < 0 if something fails.
367 */
368static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
369{
370 int tval;
371
372 tval = bq27x00_read(di, reg, false);
373 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100374 dev_dbg(di->dev, "error reading time register %02x: %d\n",
375 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100376 return tval;
377 }
378
379 if (tval == 65535)
380 return -ENODATA;
381
382 return tval * 60;
383}
384
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530385/*
386 * Read a power avg register.
387 * Return < 0 if something fails.
388 */
389static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
390{
391 int tval;
392
393 tval = bq27x00_read(di, reg, false);
394 if (tval < 0) {
395 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
396 reg, tval);
397 return tval;
398 }
399
400 if (di->chip == BQ27500)
401 return tval;
402 else
403 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
404}
405
406/*
407 * Read flag register.
408 * Return < 0 if something fails.
409 */
410static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
411{
412 int tval;
413
414 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
415 if (tval < 0) {
416 dev_err(di->dev, "error reading flag register:%d\n", tval);
417 return tval;
418 }
419
420 if ((di->chip == BQ27500)) {
421 if (tval & BQ27500_FLAG_SOCF)
422 tval = POWER_SUPPLY_HEALTH_DEAD;
423 else if (tval & BQ27500_FLAG_OTC)
424 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
425 else
426 tval = POWER_SUPPLY_HEALTH_GOOD;
427 return tval;
428 } else {
429 if (tval & BQ27000_FLAG_EDV1)
430 tval = POWER_SUPPLY_HEALTH_DEAD;
431 else
432 tval = POWER_SUPPLY_HEALTH_GOOD;
433 return tval;
434 }
435
436 return -1;
437}
438
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100439static void bq27x00_update(struct bq27x00_device_info *di)
440{
441 struct bq27x00_reg_cache cache = {0, };
442 bool is_bq27500 = di->chip == BQ27500;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700443 bool is_bq27425 = di->chip == BQ27425;
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700444 bool is_bq27742 = di->chip == BQ27742;
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700445 bool flags_1b = !(is_bq27500 || is_bq27742);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100446
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700447 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
Marek Belisko3dd843e2014-06-30 21:49:56 +0200448 if ((cache.flags & 0xff) == 0xff)
449 /* read error */
450 cache.flags = -1;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100451 if (cache.flags >= 0) {
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700452 if (!is_bq27500 && !is_bq27425 && !is_bq27742
Saranya Gopala66f59b2012-07-13 20:20:07 -0700453 && (cache.flags & BQ27000_FLAG_CI)) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100454 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +0100455 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100456 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100457 cache.time_to_empty = -ENODATA;
458 cache.time_to_empty_avg = -ENODATA;
459 cache.time_to_full = -ENODATA;
460 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530461 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100462 } else {
463 cache.capacity = bq27x00_battery_read_rsoc(di);
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700464 if (is_bq27742)
465 cache.time_to_empty =
466 bq27x00_battery_read_time(di,
467 BQ27x00_REG_TTE);
468 else if (!is_bq27425) {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700469 cache.energy = bq27x00_battery_read_energy(di);
470 cache.time_to_empty =
471 bq27x00_battery_read_time(di,
472 BQ27x00_REG_TTE);
473 cache.time_to_empty_avg =
474 bq27x00_battery_read_time(di,
475 BQ27x00_REG_TTECP);
476 cache.time_to_full =
477 bq27x00_battery_read_time(di,
478 BQ27x00_REG_TTF);
479 }
Puthikorn Voravootivata3c0c3e2014-09-30 11:18:56 -0700480 cache.charge_full = bq27x00_battery_read_lmd(di);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530481 cache.health = bq27x00_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100482 }
Pali Rohárd149e982011-11-01 01:43:07 +0100483 cache.temperature = bq27x00_battery_read_temperature(di);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700484 if (!is_bq27425)
485 cache.cycle_count = bq27x00_battery_read_cyct(di);
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700486 if (is_bq27742)
487 cache.power_avg =
488 bq27x00_battery_read_pwr_avg(di,
489 BQ27742_POWER_AVG);
490 else
491 cache.power_avg =
492 bq27x00_battery_read_pwr_avg(di,
493 BQ27x00_POWER_AVG);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100494
Pali Rohár631c17e2011-02-01 00:08:02 +0100495 /* We only have to read charge design full once */
496 if (di->charge_design_full <= 0)
497 di->charge_design_full = bq27x00_battery_read_ilmd(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100498 }
499
Pali Rohárb68f6212011-11-01 01:43:03 +0100500 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100501 di->cache = cache;
502 power_supply_changed(&di->bat);
503 }
504
505 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700506}
507
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100508static void bq27x00_battery_poll(struct work_struct *work)
509{
510 struct bq27x00_device_info *di =
511 container_of(work, struct bq27x00_device_info, work.work);
512
513 bq27x00_update(di);
514
515 if (poll_interval > 0) {
516 /* The timer does not have to be accurate. */
517 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
518 schedule_delayed_work(&di->work, poll_interval * HZ);
519 }
520}
521
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700522/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100523 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700524 * Note that current can be negative signed as well
525 * Or 0 if something fails.
526 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100527static int bq27x00_battery_current(struct bq27x00_device_info *di,
528 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700529{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100530 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100531 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700532
Pali Rohárb68f6212011-11-01 01:43:03 +0100533 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100534 if (curr < 0) {
535 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100536 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100537 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200538
Saranya Gopala66f59b2012-07-13 20:20:07 -0700539 if (bq27xxx_is_chip_version_higher(di)) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200540 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100541 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200542 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100543 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
544 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200545 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200546 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200547 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100548
549 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700550 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200551
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100552 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700553}
554
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200555static int bq27x00_battery_status(struct bq27x00_device_info *di,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100556 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200557{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200558 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200559
Saranya Gopala66f59b2012-07-13 20:20:07 -0700560 if (bq27xxx_is_chip_version_higher(di)) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100561 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200562 status = POWER_SUPPLY_STATUS_FULL;
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400563 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200564 status = POWER_SUPPLY_STATUS_DISCHARGING;
Pali Rohár270968c2011-11-01 01:43:08 +0100565 else
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400566 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200567 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200568 if (di->cache.flags & BQ27000_FLAG_FC)
569 status = POWER_SUPPLY_STATUS_FULL;
570 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200571 status = POWER_SUPPLY_STATUS_CHARGING;
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200572 else if (power_supply_am_i_supplied(&di->bat))
573 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200574 else
575 status = POWER_SUPPLY_STATUS_DISCHARGING;
576 }
577
578 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100579
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200580 return 0;
581}
582
Pali Rohárd66bab32011-11-01 01:43:04 +0100583static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
584 union power_supply_propval *val)
585{
586 int level;
587
Saranya Gopala66f59b2012-07-13 20:20:07 -0700588 if (bq27xxx_is_chip_version_higher(di)) {
Pali Rohárd66bab32011-11-01 01:43:04 +0100589 if (di->cache.flags & BQ27500_FLAG_FC)
590 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
591 else if (di->cache.flags & BQ27500_FLAG_SOC1)
592 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
593 else if (di->cache.flags & BQ27500_FLAG_SOCF)
594 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
595 else
596 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
597 } else {
598 if (di->cache.flags & BQ27000_FLAG_FC)
599 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
600 else if (di->cache.flags & BQ27000_FLAG_EDV1)
601 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
602 else if (di->cache.flags & BQ27000_FLAG_EDVF)
603 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
604 else
605 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
606 }
607
608 val->intval = level;
609
610 return 0;
611}
612
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200613/*
Thomas Weber91fe4d52012-02-17 17:46:21 +0100614 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100615 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200616 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100617static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
618 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200619{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100620 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200621
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100622 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100623 if (volt < 0) {
624 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100625 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100626 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200627
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100628 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200629
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100630 return 0;
631}
632
633static int bq27x00_simple_value(int value,
634 union power_supply_propval *val)
635{
636 if (value < 0)
637 return value;
638
639 val->intval = value;
640
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200641 return 0;
642}
643
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700644#define to_bq27x00_device_info(x) container_of((x), \
645 struct bq27x00_device_info, bat);
646
647static int bq27x00_battery_get_property(struct power_supply *psy,
648 enum power_supply_property psp,
649 union power_supply_propval *val)
650{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200651 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700652 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200653
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100654 mutex_lock(&di->lock);
655 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
656 cancel_delayed_work_sync(&di->work);
657 bq27x00_battery_poll(&di->work.work);
658 }
659 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100660
661 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200662 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700663
664 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200665 case POWER_SUPPLY_PROP_STATUS:
666 ret = bq27x00_battery_status(di, val);
667 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700668 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100669 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200670 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700671 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100672 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700673 break;
674 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100675 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700676 break;
677 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100678 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700679 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100680 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
681 ret = bq27x00_battery_capacity_level(di, val);
682 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700683 case POWER_SUPPLY_PROP_TEMP:
Pali Rohárd149e982011-11-01 01:43:07 +0100684 ret = bq27x00_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +0100685 if (ret == 0)
686 val->intval -= 2731;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700687 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200688 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100689 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200690 break;
691 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100692 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200693 break;
694 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100695 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200696 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200697 case POWER_SUPPLY_PROP_TECHNOLOGY:
698 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
699 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100700 case POWER_SUPPLY_PROP_CHARGE_NOW:
701 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
702 break;
703 case POWER_SUPPLY_PROP_CHARGE_FULL:
704 ret = bq27x00_simple_value(di->cache.charge_full, val);
705 break;
706 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
707 ret = bq27x00_simple_value(di->charge_design_full, val);
708 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200709 case POWER_SUPPLY_PROP_CYCLE_COUNT:
710 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100711 break;
712 case POWER_SUPPLY_PROP_ENERGY_NOW:
Pali Rohára8f6bd22011-11-01 01:43:06 +0100713 ret = bq27x00_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100714 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530715 case POWER_SUPPLY_PROP_POWER_AVG:
716 ret = bq27x00_simple_value(di->cache.power_avg, val);
717 break;
718 case POWER_SUPPLY_PROP_HEALTH:
719 ret = bq27x00_simple_value(di->cache.health, val);
720 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700721 default:
722 return -EINVAL;
723 }
724
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200725 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700726}
727
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100728static void bq27x00_external_power_changed(struct power_supply *psy)
729{
730 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
731
732 cancel_delayed_work_sync(&di->work);
733 schedule_delayed_work(&di->work, 0);
734}
735
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200736static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700737{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200738 int ret;
739
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700740 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700741 if (di->chip == BQ27425) {
742 di->bat.properties = bq27425_battery_props;
743 di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700744 } else if (di->chip == BQ27742) {
745 di->bat.properties = bq27742_battery_props;
746 di->bat.num_properties = ARRAY_SIZE(bq27742_battery_props);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700747 } else {
748 di->bat.properties = bq27x00_battery_props;
749 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
750 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700751 di->bat.get_property = bq27x00_battery_get_property;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100752 di->bat.external_power_changed = bq27x00_external_power_changed;
753
754 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
755 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200756
757 ret = power_supply_register(di->dev, &di->bat);
758 if (ret) {
759 dev_err(di->dev, "failed to register battery: %d\n", ret);
760 return ret;
761 }
762
763 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
764
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100765 bq27x00_update(di);
766
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200767 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700768}
769
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100770static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
771{
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100772 /*
773 * power_supply_unregister call bq27x00_battery_get_property which
774 * call bq27x00_battery_poll.
775 * Make sure that bq27x00_battery_poll will not call
776 * schedule_delayed_work again after unregister (which cause OOPS).
777 */
778 poll_interval = 0;
779
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100780 cancel_delayed_work_sync(&di->work);
781
782 power_supply_unregister(&di->bat);
783
784 mutex_destroy(&di->lock);
785}
786
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200787
788/* i2c specific code */
789#ifdef CONFIG_BATTERY_BQ27X00_I2C
790
791/* If the system has several batteries we need a different name for each
792 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700793 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200794static DEFINE_IDR(battery_id);
795static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700796
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100797static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700798{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200799 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200800 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700801 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100802 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700803
804 if (!client->adapter)
805 return -ENODEV;
806
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200807 msg[0].addr = client->addr;
808 msg[0].flags = 0;
809 msg[0].buf = &reg;
810 msg[0].len = sizeof(reg);
811 msg[1].addr = client->addr;
812 msg[1].flags = I2C_M_RD;
813 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100814 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200815 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100816 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200817 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100818
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200819 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100820 if (ret < 0)
821 return ret;
822
823 if (!single)
824 ret = get_unaligned_le16(data);
825 else
826 ret = data[0];
827
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100828 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700829}
830
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200831static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700832 const struct i2c_device_id *id)
833{
834 char *name;
835 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700836 int num;
837 int retval = 0;
838
839 /* Get new ID for the new battery device */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700840 mutex_lock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -0800841 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700842 mutex_unlock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -0800843 if (num < 0)
844 return num;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700845
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200846 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700847 if (!name) {
848 dev_err(&client->dev, "failed to allocate device name\n");
849 retval = -ENOMEM;
850 goto batt_failed_1;
851 }
852
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530853 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700854 if (!di) {
855 dev_err(&client->dev, "failed to allocate device info data\n");
856 retval = -ENOMEM;
857 goto batt_failed_2;
858 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700859
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200860 di->id = num;
861 di->dev = &client->dev;
862 di->chip = id->driver_data;
863 di->bat.name = name;
864 di->bus.read = &bq27x00_read_i2c;
865
Julia Lawallf7760992012-08-19 10:44:20 +0200866 retval = bq27x00_powersupply_init(di);
867 if (retval)
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530868 goto batt_failed_2;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700869
870 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700871
872 return 0;
873
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700874batt_failed_2:
875 kfree(name);
876batt_failed_1:
877 mutex_lock(&battery_mutex);
878 idr_remove(&battery_id, num);
879 mutex_unlock(&battery_mutex);
880
881 return retval;
882}
883
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200884static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700885{
886 struct bq27x00_device_info *di = i2c_get_clientdata(client);
887
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100888 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700889
890 kfree(di->bat.name);
891
892 mutex_lock(&battery_mutex);
893 idr_remove(&battery_id, di->id);
894 mutex_unlock(&battery_mutex);
895
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700896 return 0;
897}
898
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200899static const struct i2c_device_id bq27x00_id[] = {
900 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
901 { "bq27500", BQ27500 },
Saranya Gopala66f59b2012-07-13 20:20:07 -0700902 { "bq27425", BQ27425 },
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700903 { "bq27742", BQ27742 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700904 {},
905};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100906MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700907
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200908static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700909 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200910 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700911 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200912 .probe = bq27x00_battery_probe,
913 .remove = bq27x00_battery_remove,
914 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700915};
916
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200917static inline int bq27x00_battery_i2c_init(void)
918{
919 int ret = i2c_add_driver(&bq27x00_battery_driver);
920 if (ret)
921 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
922
923 return ret;
924}
925
926static inline void bq27x00_battery_i2c_exit(void)
927{
928 i2c_del_driver(&bq27x00_battery_driver);
929}
930
931#else
932
933static inline int bq27x00_battery_i2c_init(void) { return 0; }
934static inline void bq27x00_battery_i2c_exit(void) {};
935
936#endif
937
938/* platform specific code */
939#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
940
941static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100942 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200943{
944 struct device *dev = di->dev;
945 struct bq27000_platform_data *pdata = dev->platform_data;
946 unsigned int timeout = 3;
947 int upper, lower;
948 int temp;
949
950 if (!single) {
951 /* Make sure the value has not changed in between reading the
952 * lower and the upper part */
953 upper = pdata->read(dev, reg + 1);
954 do {
955 temp = upper;
956 if (upper < 0)
957 return upper;
958
959 lower = pdata->read(dev, reg);
960 if (lower < 0)
961 return lower;
962
963 upper = pdata->read(dev, reg + 1);
964 } while (temp != upper && --timeout);
965
966 if (timeout == 0)
967 return -EIO;
968
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100969 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200970 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100971
972 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200973}
974
Bill Pembertonc8afa642012-11-19 13:22:23 -0500975static int bq27000_battery_probe(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200976{
977 struct bq27x00_device_info *di;
978 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200979
980 if (!pdata) {
981 dev_err(&pdev->dev, "no platform_data supplied\n");
982 return -EINVAL;
983 }
984
985 if (!pdata->read) {
986 dev_err(&pdev->dev, "no hdq read callback supplied\n");
987 return -EINVAL;
988 }
989
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530990 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200991 if (!di) {
992 dev_err(&pdev->dev, "failed to allocate device info data\n");
993 return -ENOMEM;
994 }
995
996 platform_set_drvdata(pdev, di);
997
998 di->dev = &pdev->dev;
999 di->chip = BQ27000;
1000
1001 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
1002 di->bus.read = &bq27000_read_platform;
1003
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +05301004 return bq27x00_powersupply_init(di);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001005}
1006
Bill Pemberton415ec692012-11-19 13:26:07 -05001007static int bq27000_battery_remove(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001008{
1009 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
1010
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001011 bq27x00_powersupply_unregister(di);
1012
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001013 return 0;
1014}
1015
1016static struct platform_driver bq27000_battery_driver = {
1017 .probe = bq27000_battery_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001018 .remove = bq27000_battery_remove,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001019 .driver = {
1020 .name = "bq27000-battery",
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001021 },
1022};
1023
1024static inline int bq27x00_battery_platform_init(void)
1025{
1026 int ret = platform_driver_register(&bq27000_battery_driver);
1027 if (ret)
1028 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
1029
1030 return ret;
1031}
1032
1033static inline void bq27x00_battery_platform_exit(void)
1034{
1035 platform_driver_unregister(&bq27000_battery_driver);
1036}
1037
1038#else
1039
1040static inline int bq27x00_battery_platform_init(void) { return 0; }
1041static inline void bq27x00_battery_platform_exit(void) {};
1042
1043#endif
1044
1045/*
1046 * Module stuff
1047 */
1048
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001049static int __init bq27x00_battery_init(void)
1050{
1051 int ret;
1052
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001053 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001054 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001055 return ret;
1056
1057 ret = bq27x00_battery_platform_init();
1058 if (ret)
1059 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001060
1061 return ret;
1062}
1063module_init(bq27x00_battery_init);
1064
1065static void __exit bq27x00_battery_exit(void)
1066{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001067 bq27x00_battery_platform_exit();
1068 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001069}
1070module_exit(bq27x00_battery_exit);
1071
1072MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1073MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1074MODULE_LICENSE("GPL");