blob: a0f205b0471dd739105cd691c6099420306ec566 [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
Pali Rohár631c17e2011-02-01 00:08:02 +010026 */
27
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +053028#include <linux/device.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070029#include <linux/module.h>
30#include <linux/param.h>
31#include <linux/jiffies.h>
32#include <linux/workqueue.h>
33#include <linux/delay.h>
34#include <linux/platform_device.h>
35#include <linux/power_supply.h>
36#include <linux/idr.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070037#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Harvey Harrison8aef7e82008-09-22 14:53:50 -070039#include <asm/unaligned.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070040
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020041#include <linux/power/bq27x00_battery.h>
42
Pali Rohár631c17e2011-02-01 00:08:02 +010043#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070044
45#define BQ27x00_REG_TEMP 0x06
46#define BQ27x00_REG_VOLT 0x08
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070047#define BQ27x00_REG_AI 0x14
48#define BQ27x00_REG_FLAGS 0x0A
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020049#define BQ27x00_REG_TTE 0x16
50#define BQ27x00_REG_TTF 0x18
51#define BQ27x00_REG_TTECP 0x26
Thomas Weber202e0112012-02-16 08:58:15 +010052#define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
Pali Rohár631c17e2011-02-01 00:08:02 +010053#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
54#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
Thomas Weber202e0112012-02-16 08:58:15 +010055#define BQ27x00_REG_AE 0x22 /* Available energy */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053056#define BQ27x00_POWER_AVG 0x24
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070057
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020058#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
Pali Rohár631c17e2011-02-01 00:08:02 +010059#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
Pali Rohárd66bab32011-11-01 01:43:04 +010060#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
61#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
Pali Rohár4b226c22011-11-01 01:43:05 +010062#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +020063#define BQ27000_FLAG_FC BIT(5)
Pali Rohárd66bab32011-11-01 01:43:04 +010064#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020065
Pali Rohárbf7d4142011-02-01 00:23:13 +010066#define BQ27500_REG_SOC 0x2C
Pali Rohár631c17e2011-02-01 00:08:02 +010067#define BQ27500_REG_DCAP 0x3C /* Design capacity */
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +040068#define BQ27500_FLAG_DSC BIT(0)
Pali Rohárd66bab32011-11-01 01:43:04 +010069#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
70#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +040071#define BQ27500_FLAG_FC BIT(9)
Syed Rafiuddin9903e622012-05-17 12:51:15 +053072#define BQ27500_FLAG_OTC BIT(15)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020073
Saranya Gopala66f59b2012-07-13 20:20:07 -070074/* bq27425 register addresses are same as bq27x00 addresses minus 4 */
75#define BQ27425_REG_OFFSET 0x04
76#define BQ27425_REG_SOC 0x18 /* Register address plus offset */
77
Pali Rohára2e51182010-05-24 20:52:13 +020078#define BQ27000_RS 20 /* Resistor sense */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053079#define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
Pali Rohára2e51182010-05-24 20:52:13 +020080
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070081struct bq27x00_device_info;
82struct bq27x00_access_methods {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010083 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070084};
85
Saranya Gopala66f59b2012-07-13 20:20:07 -070086enum bq27x00_chip { BQ27000, BQ27500, BQ27425};
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020087
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010088struct bq27x00_reg_cache {
89 int temperature;
90 int time_to_empty;
91 int time_to_empty_avg;
92 int time_to_full;
Pali Rohár631c17e2011-02-01 00:08:02 +010093 int charge_full;
Pali Rohár73c244a2011-04-30 23:59:35 +020094 int cycle_count;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010095 int capacity;
Pali Rohára8f6bd22011-11-01 01:43:06 +010096 int energy;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010097 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +053098 int power_avg;
99 int health;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100100};
101
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700102struct bq27x00_device_info {
103 struct device *dev;
104 int id;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200105 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700106
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100107 struct bq27x00_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +0100108 int charge_design_full;
109
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100110 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100111 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100112
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200113 struct power_supply bat;
114
115 struct bq27x00_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100116
117 struct mutex lock;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700118};
119
120static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200121 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700122 POWER_SUPPLY_PROP_PRESENT,
123 POWER_SUPPLY_PROP_VOLTAGE_NOW,
124 POWER_SUPPLY_PROP_CURRENT_NOW,
125 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100126 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700127 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200128 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
129 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
130 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200131 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100132 POWER_SUPPLY_PROP_CHARGE_FULL,
133 POWER_SUPPLY_PROP_CHARGE_NOW,
134 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200135 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100136 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530137 POWER_SUPPLY_PROP_POWER_AVG,
138 POWER_SUPPLY_PROP_HEALTH,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700139};
140
Saranya Gopala66f59b2012-07-13 20:20:07 -0700141static enum power_supply_property bq27425_battery_props[] = {
142 POWER_SUPPLY_PROP_STATUS,
143 POWER_SUPPLY_PROP_PRESENT,
144 POWER_SUPPLY_PROP_VOLTAGE_NOW,
145 POWER_SUPPLY_PROP_CURRENT_NOW,
146 POWER_SUPPLY_PROP_CAPACITY,
147 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
148 POWER_SUPPLY_PROP_TEMP,
149 POWER_SUPPLY_PROP_TECHNOLOGY,
150 POWER_SUPPLY_PROP_CHARGE_FULL,
151 POWER_SUPPLY_PROP_CHARGE_NOW,
152 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
153};
154
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100155static unsigned int poll_interval = 360;
156module_param(poll_interval, uint, 0644);
157MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
158 "0 disables polling");
159
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700160/*
161 * Common code for BQ27x00 devices
162 */
163
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200164static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100165 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700166{
Saranya Gopala66f59b2012-07-13 20:20:07 -0700167 if (di->chip == BQ27425)
168 return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100169 return di->bus.read(di, reg, single);
170}
171
172/*
Saranya Gopala66f59b2012-07-13 20:20:07 -0700173 * Higher versions of the chip like BQ27425 and BQ27500
174 * differ from BQ27000 and BQ27200 in calculation of certain
175 * parameters. Hence we need to check for the chip type.
176 */
177static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
178{
179 if (di->chip == BQ27425 || di->chip == BQ27500)
180 return true;
181 return false;
182}
183
184/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100185 * Return the battery Relative State-of-Charge
186 * Or < 0 if something fails.
187 */
188static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
189{
190 int rsoc;
191
192 if (di->chip == BQ27500)
193 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700194 else if (di->chip == BQ27425)
195 rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100196 else
197 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
198
199 if (rsoc < 0)
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100200 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100201
202 return rsoc;
203}
204
205/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100206 * Return a battery charge value in µAh
207 * Or < 0 if something fails.
208 */
209static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
210{
211 int charge;
212
213 charge = bq27x00_read(di, reg, false);
214 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100215 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
216 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100217 return charge;
218 }
219
Saranya Gopala66f59b2012-07-13 20:20:07 -0700220 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100221 charge *= 1000;
222 else
223 charge = charge * 3570 / BQ27000_RS;
224
225 return charge;
226}
227
228/*
229 * Return the battery Nominal available capaciy in µAh
230 * Or < 0 if something fails.
231 */
232static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
233{
Pali Roháre59ec4a2012-11-10 15:42:48 +0100234 int flags;
235 bool is_bq27500 = di->chip == BQ27500;
236 bool is_higher = bq27xxx_is_chip_version_higher(di);
237
238 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
239 if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
240 return -ENODATA;
241
Pali Rohár631c17e2011-02-01 00:08:02 +0100242 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
243}
244
245/*
246 * Return the battery Last measured discharge in µAh
247 * Or < 0 if something fails.
248 */
249static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
250{
251 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
252}
253
254/*
255 * Return the battery Initial last measured discharge in µAh
256 * Or < 0 if something fails.
257 */
258static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
259{
260 int ilmd;
261
Saranya Gopala66f59b2012-07-13 20:20:07 -0700262 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100263 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
264 else
265 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
266
267 if (ilmd < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100268 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Pali Rohár631c17e2011-02-01 00:08:02 +0100269 return ilmd;
270 }
271
Saranya Gopala66f59b2012-07-13 20:20:07 -0700272 if (bq27xxx_is_chip_version_higher(di))
Pali Rohár631c17e2011-02-01 00:08:02 +0100273 ilmd *= 1000;
274 else
275 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
276
277 return ilmd;
278}
279
280/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100281 * Return the battery Available energy in µWh
282 * Or < 0 if something fails.
283 */
284static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
285{
286 int ae;
287
288 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
289 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100290 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100291 return ae;
292 }
293
294 if (di->chip == BQ27500)
295 ae *= 1000;
296 else
297 ae = ae * 29200 / BQ27000_RS;
298
299 return ae;
300}
301
302/*
Pali Rohár5dc34432013-02-02 11:06:09 +0100303 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +0100304 * Or < 0 if something fails.
305 */
306static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
307{
308 int temp;
309
310 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
311 if (temp < 0) {
312 dev_err(di->dev, "error reading temperature\n");
313 return temp;
314 }
315
Pali Rohár5dc34432013-02-02 11:06:09 +0100316 if (!bq27xxx_is_chip_version_higher(di))
317 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +0100318
319 return temp;
320}
321
322/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100323 * Return the battery Cycle count total
324 * Or < 0 if something fails.
325 */
326static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
327{
328 int cyct;
329
330 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
331 if (cyct < 0)
332 dev_err(di->dev, "error reading cycle count total\n");
333
334 return cyct;
335}
336
337/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100338 * Read a time register.
339 * Return < 0 if something fails.
340 */
341static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
342{
343 int tval;
344
345 tval = bq27x00_read(di, reg, false);
346 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100347 dev_dbg(di->dev, "error reading time register %02x: %d\n",
348 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100349 return tval;
350 }
351
352 if (tval == 65535)
353 return -ENODATA;
354
355 return tval * 60;
356}
357
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530358/*
359 * Read a power avg register.
360 * Return < 0 if something fails.
361 */
362static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
363{
364 int tval;
365
366 tval = bq27x00_read(di, reg, false);
367 if (tval < 0) {
368 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
369 reg, tval);
370 return tval;
371 }
372
373 if (di->chip == BQ27500)
374 return tval;
375 else
376 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
377}
378
379/*
380 * Read flag register.
381 * Return < 0 if something fails.
382 */
383static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
384{
385 int tval;
386
387 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
388 if (tval < 0) {
389 dev_err(di->dev, "error reading flag register:%d\n", tval);
390 return tval;
391 }
392
393 if ((di->chip == BQ27500)) {
394 if (tval & BQ27500_FLAG_SOCF)
395 tval = POWER_SUPPLY_HEALTH_DEAD;
396 else if (tval & BQ27500_FLAG_OTC)
397 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
398 else
399 tval = POWER_SUPPLY_HEALTH_GOOD;
400 return tval;
401 } else {
402 if (tval & BQ27000_FLAG_EDV1)
403 tval = POWER_SUPPLY_HEALTH_DEAD;
404 else
405 tval = POWER_SUPPLY_HEALTH_GOOD;
406 return tval;
407 }
408
409 return -1;
410}
411
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100412static void bq27x00_update(struct bq27x00_device_info *di)
413{
414 struct bq27x00_reg_cache cache = {0, };
415 bool is_bq27500 = di->chip == BQ27500;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700416 bool is_bq27425 = di->chip == BQ27425;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100417
Grazvydas Ignotas4d403652012-01-14 22:50:49 +0200418 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100419 if (cache.flags >= 0) {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700420 if (!is_bq27500 && !is_bq27425
421 && (cache.flags & BQ27000_FLAG_CI)) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100422 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +0100423 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100424 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100425 cache.time_to_empty = -ENODATA;
426 cache.time_to_empty_avg = -ENODATA;
427 cache.time_to_full = -ENODATA;
428 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530429 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100430 } else {
431 cache.capacity = bq27x00_battery_read_rsoc(di);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700432 if (!is_bq27425) {
433 cache.energy = bq27x00_battery_read_energy(di);
434 cache.time_to_empty =
435 bq27x00_battery_read_time(di,
436 BQ27x00_REG_TTE);
437 cache.time_to_empty_avg =
438 bq27x00_battery_read_time(di,
439 BQ27x00_REG_TTECP);
440 cache.time_to_full =
441 bq27x00_battery_read_time(di,
442 BQ27x00_REG_TTF);
443 }
Pali Rohár4b226c22011-11-01 01:43:05 +0100444 cache.charge_full = bq27x00_battery_read_lmd(di);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530445 cache.health = bq27x00_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100446 }
Pali Rohárd149e982011-11-01 01:43:07 +0100447 cache.temperature = bq27x00_battery_read_temperature(di);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700448 if (!is_bq27425)
449 cache.cycle_count = bq27x00_battery_read_cyct(di);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530450 cache.power_avg =
451 bq27x00_battery_read_pwr_avg(di, BQ27x00_POWER_AVG);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100452
Pali Rohár631c17e2011-02-01 00:08:02 +0100453 /* We only have to read charge design full once */
454 if (di->charge_design_full <= 0)
455 di->charge_design_full = bq27x00_battery_read_ilmd(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100456 }
457
Pali Rohárb68f6212011-11-01 01:43:03 +0100458 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100459 di->cache = cache;
460 power_supply_changed(&di->bat);
461 }
462
463 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700464}
465
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100466static void bq27x00_battery_poll(struct work_struct *work)
467{
468 struct bq27x00_device_info *di =
469 container_of(work, struct bq27x00_device_info, work.work);
470
471 bq27x00_update(di);
472
473 if (poll_interval > 0) {
474 /* The timer does not have to be accurate. */
475 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
476 schedule_delayed_work(&di->work, poll_interval * HZ);
477 }
478}
479
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700480/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100481 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700482 * Note that current can be negative signed as well
483 * Or 0 if something fails.
484 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100485static int bq27x00_battery_current(struct bq27x00_device_info *di,
486 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700487{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100488 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100489 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700490
Pali Rohárb68f6212011-11-01 01:43:03 +0100491 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100492 if (curr < 0) {
493 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100494 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100495 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200496
Saranya Gopala66f59b2012-07-13 20:20:07 -0700497 if (bq27xxx_is_chip_version_higher(di)) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200498 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100499 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200500 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100501 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
502 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200503 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200504 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200505 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100506
507 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700508 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200509
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100510 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700511}
512
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200513static int bq27x00_battery_status(struct bq27x00_device_info *di,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100514 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200515{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200516 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200517
Saranya Gopala66f59b2012-07-13 20:20:07 -0700518 if (bq27xxx_is_chip_version_higher(di)) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100519 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200520 status = POWER_SUPPLY_STATUS_FULL;
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400521 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200522 status = POWER_SUPPLY_STATUS_DISCHARGING;
Pali Rohár270968c2011-11-01 01:43:08 +0100523 else
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400524 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200525 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200526 if (di->cache.flags & BQ27000_FLAG_FC)
527 status = POWER_SUPPLY_STATUS_FULL;
528 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200529 status = POWER_SUPPLY_STATUS_CHARGING;
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200530 else if (power_supply_am_i_supplied(&di->bat))
531 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200532 else
533 status = POWER_SUPPLY_STATUS_DISCHARGING;
534 }
535
536 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100537
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200538 return 0;
539}
540
Pali Rohárd66bab32011-11-01 01:43:04 +0100541static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
542 union power_supply_propval *val)
543{
544 int level;
545
Saranya Gopala66f59b2012-07-13 20:20:07 -0700546 if (bq27xxx_is_chip_version_higher(di)) {
Pali Rohárd66bab32011-11-01 01:43:04 +0100547 if (di->cache.flags & BQ27500_FLAG_FC)
548 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
549 else if (di->cache.flags & BQ27500_FLAG_SOC1)
550 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
551 else if (di->cache.flags & BQ27500_FLAG_SOCF)
552 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
553 else
554 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
555 } else {
556 if (di->cache.flags & BQ27000_FLAG_FC)
557 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
558 else if (di->cache.flags & BQ27000_FLAG_EDV1)
559 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
560 else if (di->cache.flags & BQ27000_FLAG_EDVF)
561 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
562 else
563 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
564 }
565
566 val->intval = level;
567
568 return 0;
569}
570
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200571/*
Thomas Weber91fe4d52012-02-17 17:46:21 +0100572 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100573 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200574 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100575static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
576 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200577{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100578 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200579
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100580 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100581 if (volt < 0) {
582 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100583 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100584 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200585
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100586 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200587
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100588 return 0;
589}
590
591static int bq27x00_simple_value(int value,
592 union power_supply_propval *val)
593{
594 if (value < 0)
595 return value;
596
597 val->intval = value;
598
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200599 return 0;
600}
601
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700602#define to_bq27x00_device_info(x) container_of((x), \
603 struct bq27x00_device_info, bat);
604
605static int bq27x00_battery_get_property(struct power_supply *psy,
606 enum power_supply_property psp,
607 union power_supply_propval *val)
608{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200609 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700610 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200611
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100612 mutex_lock(&di->lock);
613 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
614 cancel_delayed_work_sync(&di->work);
615 bq27x00_battery_poll(&di->work.work);
616 }
617 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100618
619 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200620 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700621
622 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200623 case POWER_SUPPLY_PROP_STATUS:
624 ret = bq27x00_battery_status(di, val);
625 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700626 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100627 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200628 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700629 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100630 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700631 break;
632 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100633 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700634 break;
635 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100636 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700637 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100638 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
639 ret = bq27x00_battery_capacity_level(di, val);
640 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700641 case POWER_SUPPLY_PROP_TEMP:
Pali Rohárd149e982011-11-01 01:43:07 +0100642 ret = bq27x00_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +0100643 if (ret == 0)
644 val->intval -= 2731;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700645 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200646 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100647 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200648 break;
649 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100650 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200651 break;
652 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100653 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200654 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200655 case POWER_SUPPLY_PROP_TECHNOLOGY:
656 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
657 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100658 case POWER_SUPPLY_PROP_CHARGE_NOW:
659 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
660 break;
661 case POWER_SUPPLY_PROP_CHARGE_FULL:
662 ret = bq27x00_simple_value(di->cache.charge_full, val);
663 break;
664 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
665 ret = bq27x00_simple_value(di->charge_design_full, val);
666 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200667 case POWER_SUPPLY_PROP_CYCLE_COUNT:
668 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100669 break;
670 case POWER_SUPPLY_PROP_ENERGY_NOW:
Pali Rohára8f6bd22011-11-01 01:43:06 +0100671 ret = bq27x00_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100672 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530673 case POWER_SUPPLY_PROP_POWER_AVG:
674 ret = bq27x00_simple_value(di->cache.power_avg, val);
675 break;
676 case POWER_SUPPLY_PROP_HEALTH:
677 ret = bq27x00_simple_value(di->cache.health, val);
678 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700679 default:
680 return -EINVAL;
681 }
682
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200683 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700684}
685
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100686static void bq27x00_external_power_changed(struct power_supply *psy)
687{
688 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
689
690 cancel_delayed_work_sync(&di->work);
691 schedule_delayed_work(&di->work, 0);
692}
693
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200694static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700695{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200696 int ret;
697
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700698 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700699 if (di->chip == BQ27425) {
700 di->bat.properties = bq27425_battery_props;
701 di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
702 } else {
703 di->bat.properties = bq27x00_battery_props;
704 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
705 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700706 di->bat.get_property = bq27x00_battery_get_property;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100707 di->bat.external_power_changed = bq27x00_external_power_changed;
708
709 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
710 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200711
712 ret = power_supply_register(di->dev, &di->bat);
713 if (ret) {
714 dev_err(di->dev, "failed to register battery: %d\n", ret);
715 return ret;
716 }
717
718 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
719
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100720 bq27x00_update(di);
721
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200722 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700723}
724
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100725static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
726{
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100727 /*
728 * power_supply_unregister call bq27x00_battery_get_property which
729 * call bq27x00_battery_poll.
730 * Make sure that bq27x00_battery_poll will not call
731 * schedule_delayed_work again after unregister (which cause OOPS).
732 */
733 poll_interval = 0;
734
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100735 cancel_delayed_work_sync(&di->work);
736
737 power_supply_unregister(&di->bat);
738
739 mutex_destroy(&di->lock);
740}
741
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200742
743/* i2c specific code */
744#ifdef CONFIG_BATTERY_BQ27X00_I2C
745
746/* If the system has several batteries we need a different name for each
747 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700748 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200749static DEFINE_IDR(battery_id);
750static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700751
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100752static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700753{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200754 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200755 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700756 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100757 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700758
759 if (!client->adapter)
760 return -ENODEV;
761
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200762 msg[0].addr = client->addr;
763 msg[0].flags = 0;
764 msg[0].buf = &reg;
765 msg[0].len = sizeof(reg);
766 msg[1].addr = client->addr;
767 msg[1].flags = I2C_M_RD;
768 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100769 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200770 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100771 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200772 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100773
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200774 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100775 if (ret < 0)
776 return ret;
777
778 if (!single)
779 ret = get_unaligned_le16(data);
780 else
781 ret = data[0];
782
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100783 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700784}
785
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200786static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700787 const struct i2c_device_id *id)
788{
789 char *name;
790 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700791 int num;
792 int retval = 0;
793
794 /* Get new ID for the new battery device */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700795 mutex_lock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -0800796 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700797 mutex_unlock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -0800798 if (num < 0)
799 return num;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700800
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200801 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700802 if (!name) {
803 dev_err(&client->dev, "failed to allocate device name\n");
804 retval = -ENOMEM;
805 goto batt_failed_1;
806 }
807
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530808 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700809 if (!di) {
810 dev_err(&client->dev, "failed to allocate device info data\n");
811 retval = -ENOMEM;
812 goto batt_failed_2;
813 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700814
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200815 di->id = num;
816 di->dev = &client->dev;
817 di->chip = id->driver_data;
818 di->bat.name = name;
819 di->bus.read = &bq27x00_read_i2c;
820
Julia Lawallf7760992012-08-19 10:44:20 +0200821 retval = bq27x00_powersupply_init(di);
822 if (retval)
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530823 goto batt_failed_2;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700824
825 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700826
827 return 0;
828
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700829batt_failed_2:
830 kfree(name);
831batt_failed_1:
832 mutex_lock(&battery_mutex);
833 idr_remove(&battery_id, num);
834 mutex_unlock(&battery_mutex);
835
836 return retval;
837}
838
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200839static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700840{
841 struct bq27x00_device_info *di = i2c_get_clientdata(client);
842
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100843 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700844
845 kfree(di->bat.name);
846
847 mutex_lock(&battery_mutex);
848 idr_remove(&battery_id, di->id);
849 mutex_unlock(&battery_mutex);
850
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700851 return 0;
852}
853
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200854static const struct i2c_device_id bq27x00_id[] = {
855 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
856 { "bq27500", BQ27500 },
Saranya Gopala66f59b2012-07-13 20:20:07 -0700857 { "bq27425", BQ27425 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700858 {},
859};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100860MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700861
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200862static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700863 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200864 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700865 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200866 .probe = bq27x00_battery_probe,
867 .remove = bq27x00_battery_remove,
868 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700869};
870
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200871static inline int bq27x00_battery_i2c_init(void)
872{
873 int ret = i2c_add_driver(&bq27x00_battery_driver);
874 if (ret)
875 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
876
877 return ret;
878}
879
880static inline void bq27x00_battery_i2c_exit(void)
881{
882 i2c_del_driver(&bq27x00_battery_driver);
883}
884
885#else
886
887static inline int bq27x00_battery_i2c_init(void) { return 0; }
888static inline void bq27x00_battery_i2c_exit(void) {};
889
890#endif
891
892/* platform specific code */
893#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
894
895static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100896 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200897{
898 struct device *dev = di->dev;
899 struct bq27000_platform_data *pdata = dev->platform_data;
900 unsigned int timeout = 3;
901 int upper, lower;
902 int temp;
903
904 if (!single) {
905 /* Make sure the value has not changed in between reading the
906 * lower and the upper part */
907 upper = pdata->read(dev, reg + 1);
908 do {
909 temp = upper;
910 if (upper < 0)
911 return upper;
912
913 lower = pdata->read(dev, reg);
914 if (lower < 0)
915 return lower;
916
917 upper = pdata->read(dev, reg + 1);
918 } while (temp != upper && --timeout);
919
920 if (timeout == 0)
921 return -EIO;
922
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100923 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200924 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100925
926 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200927}
928
Bill Pembertonc8afa642012-11-19 13:22:23 -0500929static int bq27000_battery_probe(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200930{
931 struct bq27x00_device_info *di;
932 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200933
934 if (!pdata) {
935 dev_err(&pdev->dev, "no platform_data supplied\n");
936 return -EINVAL;
937 }
938
939 if (!pdata->read) {
940 dev_err(&pdev->dev, "no hdq read callback supplied\n");
941 return -EINVAL;
942 }
943
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530944 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200945 if (!di) {
946 dev_err(&pdev->dev, "failed to allocate device info data\n");
947 return -ENOMEM;
948 }
949
950 platform_set_drvdata(pdev, di);
951
952 di->dev = &pdev->dev;
953 di->chip = BQ27000;
954
955 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
956 di->bus.read = &bq27000_read_platform;
957
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +0530958 return bq27x00_powersupply_init(di);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200959}
960
Bill Pemberton415ec692012-11-19 13:26:07 -0500961static int bq27000_battery_remove(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200962{
963 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
964
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100965 bq27x00_powersupply_unregister(di);
966
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200967 return 0;
968}
969
970static struct platform_driver bq27000_battery_driver = {
971 .probe = bq27000_battery_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500972 .remove = bq27000_battery_remove,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200973 .driver = {
974 .name = "bq27000-battery",
975 .owner = THIS_MODULE,
976 },
977};
978
979static inline int bq27x00_battery_platform_init(void)
980{
981 int ret = platform_driver_register(&bq27000_battery_driver);
982 if (ret)
983 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
984
985 return ret;
986}
987
988static inline void bq27x00_battery_platform_exit(void)
989{
990 platform_driver_unregister(&bq27000_battery_driver);
991}
992
993#else
994
995static inline int bq27x00_battery_platform_init(void) { return 0; }
996static inline void bq27x00_battery_platform_exit(void) {};
997
998#endif
999
1000/*
1001 * Module stuff
1002 */
1003
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001004static int __init bq27x00_battery_init(void)
1005{
1006 int ret;
1007
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001008 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001009 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001010 return ret;
1011
1012 ret = bq27x00_battery_platform_init();
1013 if (ret)
1014 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001015
1016 return ret;
1017}
1018module_init(bq27x00_battery_init);
1019
1020static void __exit bq27x00_battery_exit(void)
1021{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001022 bq27x00_battery_platform_exit();
1023 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001024}
1025module_exit(bq27x00_battery_exit);
1026
1027MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1028MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1029MODULE_LICENSE("GPL");