blob: 5657990b7aceea102733e7d42fb88ce1436fa4f2 [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
25 */
26
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070027#include <linux/module.h>
28#include <linux/param.h>
29#include <linux/jiffies.h>
30#include <linux/workqueue.h>
31#include <linux/delay.h>
32#include <linux/platform_device.h>
33#include <linux/power_supply.h>
34#include <linux/idr.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070035#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Harvey Harrison8aef7e82008-09-22 14:53:50 -070037#include <asm/unaligned.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070038
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020039#include <linux/power/bq27x00_battery.h>
40
Pali Rohár631c17e2011-02-01 00:08:02 +010041#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070042
43#define BQ27x00_REG_TEMP 0x06
44#define BQ27x00_REG_VOLT 0x08
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070045#define BQ27x00_REG_AI 0x14
46#define BQ27x00_REG_FLAGS 0x0A
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020047#define BQ27x00_REG_TTE 0x16
48#define BQ27x00_REG_TTF 0x18
49#define BQ27x00_REG_TTECP 0x26
Thomas Weber202e0112012-02-16 08:58:15 +010050#define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
Pali Rohár631c17e2011-02-01 00:08:02 +010051#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
52#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
Thomas Weber202e0112012-02-16 08:58:15 +010053#define BQ27x00_REG_AE 0x22 /* Available energy */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053054#define BQ27x00_POWER_AVG 0x24
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070055
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020056#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
Pali Rohár631c17e2011-02-01 00:08:02 +010057#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
Pali Rohárd66bab32011-11-01 01:43:04 +010058#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
59#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
Pali Rohár4b226c22011-11-01 01:43:05 +010060#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +020061#define BQ27000_FLAG_FC BIT(5)
Pali Rohárd66bab32011-11-01 01:43:04 +010062#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020063
Pali Rohárbf7d4142011-02-01 00:23:13 +010064#define BQ27500_REG_SOC 0x2C
Pali Rohár631c17e2011-02-01 00:08:02 +010065#define BQ27500_REG_DCAP 0x3C /* Design capacity */
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +040066#define BQ27500_FLAG_DSC BIT(0)
Pali Rohárd66bab32011-11-01 01:43:04 +010067#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
68#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +040069#define BQ27500_FLAG_FC BIT(9)
Syed Rafiuddin9903e622012-05-17 12:51:15 +053070#define BQ27500_FLAG_OTC BIT(15)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020071
Pali Rohára2e51182010-05-24 20:52:13 +020072#define BQ27000_RS 20 /* Resistor sense */
Syed Rafiuddin9903e622012-05-17 12:51:15 +053073#define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
Pali Rohára2e51182010-05-24 20:52:13 +020074
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070075struct bq27x00_device_info;
76struct bq27x00_access_methods {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010077 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070078};
79
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020080enum bq27x00_chip { BQ27000, BQ27500 };
81
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010082struct bq27x00_reg_cache {
83 int temperature;
84 int time_to_empty;
85 int time_to_empty_avg;
86 int time_to_full;
Pali Rohár631c17e2011-02-01 00:08:02 +010087 int charge_full;
Pali Rohár73c244a2011-04-30 23:59:35 +020088 int cycle_count;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010089 int capacity;
Pali Rohára8f6bd22011-11-01 01:43:06 +010090 int energy;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010091 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +053092 int power_avg;
93 int health;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010094};
95
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070096struct bq27x00_device_info {
97 struct device *dev;
98 int id;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020099 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700100
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100101 struct bq27x00_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +0100102 int charge_design_full;
103
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100104 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100105 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100106
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200107 struct power_supply bat;
108
109 struct bq27x00_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100110
111 struct mutex lock;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700112};
113
114static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200115 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700116 POWER_SUPPLY_PROP_PRESENT,
117 POWER_SUPPLY_PROP_VOLTAGE_NOW,
118 POWER_SUPPLY_PROP_CURRENT_NOW,
119 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100120 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700121 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200122 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
123 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
124 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200125 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100126 POWER_SUPPLY_PROP_CHARGE_FULL,
127 POWER_SUPPLY_PROP_CHARGE_NOW,
128 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200129 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100130 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530131 POWER_SUPPLY_PROP_POWER_AVG,
132 POWER_SUPPLY_PROP_HEALTH,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700133};
134
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100135static unsigned int poll_interval = 360;
136module_param(poll_interval, uint, 0644);
137MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
138 "0 disables polling");
139
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700140/*
141 * Common code for BQ27x00 devices
142 */
143
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200144static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100145 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700146{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100147 return di->bus.read(di, reg, single);
148}
149
150/*
151 * Return the battery Relative State-of-Charge
152 * Or < 0 if something fails.
153 */
154static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
155{
156 int rsoc;
157
158 if (di->chip == BQ27500)
159 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
160 else
161 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
162
163 if (rsoc < 0)
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100164 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100165
166 return rsoc;
167}
168
169/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100170 * Return a battery charge value in µAh
171 * Or < 0 if something fails.
172 */
173static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
174{
175 int charge;
176
177 charge = bq27x00_read(di, reg, false);
178 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100179 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
180 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100181 return charge;
182 }
183
184 if (di->chip == BQ27500)
185 charge *= 1000;
186 else
187 charge = charge * 3570 / BQ27000_RS;
188
189 return charge;
190}
191
192/*
193 * Return the battery Nominal available capaciy in µAh
194 * Or < 0 if something fails.
195 */
196static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
197{
198 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
199}
200
201/*
202 * Return the battery Last measured discharge in µAh
203 * Or < 0 if something fails.
204 */
205static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
206{
207 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
208}
209
210/*
211 * Return the battery Initial last measured discharge in µAh
212 * Or < 0 if something fails.
213 */
214static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
215{
216 int ilmd;
217
218 if (di->chip == BQ27500)
219 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
220 else
221 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
222
223 if (ilmd < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100224 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Pali Rohár631c17e2011-02-01 00:08:02 +0100225 return ilmd;
226 }
227
228 if (di->chip == BQ27500)
229 ilmd *= 1000;
230 else
231 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
232
233 return ilmd;
234}
235
236/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100237 * Return the battery Available energy in µWh
238 * Or < 0 if something fails.
239 */
240static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
241{
242 int ae;
243
244 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
245 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100246 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100247 return ae;
248 }
249
250 if (di->chip == BQ27500)
251 ae *= 1000;
252 else
253 ae = ae * 29200 / BQ27000_RS;
254
255 return ae;
256}
257
258/*
Pali Rohárd149e982011-11-01 01:43:07 +0100259 * Return the battery temperature in tenths of degree Celsius
260 * Or < 0 if something fails.
261 */
262static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
263{
264 int temp;
265
266 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
267 if (temp < 0) {
268 dev_err(di->dev, "error reading temperature\n");
269 return temp;
270 }
271
272 if (di->chip == BQ27500)
273 temp -= 2731;
274 else
275 temp = ((temp * 5) - 5463) / 2;
276
277 return temp;
278}
279
280/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100281 * Return the battery Cycle count total
282 * Or < 0 if something fails.
283 */
284static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
285{
286 int cyct;
287
288 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
289 if (cyct < 0)
290 dev_err(di->dev, "error reading cycle count total\n");
291
292 return cyct;
293}
294
295/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100296 * Read a time register.
297 * Return < 0 if something fails.
298 */
299static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
300{
301 int tval;
302
303 tval = bq27x00_read(di, reg, false);
304 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100305 dev_dbg(di->dev, "error reading time register %02x: %d\n",
306 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100307 return tval;
308 }
309
310 if (tval == 65535)
311 return -ENODATA;
312
313 return tval * 60;
314}
315
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530316/*
317 * Read a power avg register.
318 * Return < 0 if something fails.
319 */
320static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
321{
322 int tval;
323
324 tval = bq27x00_read(di, reg, false);
325 if (tval < 0) {
326 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
327 reg, tval);
328 return tval;
329 }
330
331 if (di->chip == BQ27500)
332 return tval;
333 else
334 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
335}
336
337/*
338 * Read flag register.
339 * Return < 0 if something fails.
340 */
341static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
342{
343 int tval;
344
345 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
346 if (tval < 0) {
347 dev_err(di->dev, "error reading flag register:%d\n", tval);
348 return tval;
349 }
350
351 if ((di->chip == BQ27500)) {
352 if (tval & BQ27500_FLAG_SOCF)
353 tval = POWER_SUPPLY_HEALTH_DEAD;
354 else if (tval & BQ27500_FLAG_OTC)
355 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
356 else
357 tval = POWER_SUPPLY_HEALTH_GOOD;
358 return tval;
359 } else {
360 if (tval & BQ27000_FLAG_EDV1)
361 tval = POWER_SUPPLY_HEALTH_DEAD;
362 else
363 tval = POWER_SUPPLY_HEALTH_GOOD;
364 return tval;
365 }
366
367 return -1;
368}
369
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100370static void bq27x00_update(struct bq27x00_device_info *di)
371{
372 struct bq27x00_reg_cache cache = {0, };
373 bool is_bq27500 = di->chip == BQ27500;
374
Grazvydas Ignotas4d403652012-01-14 22:50:49 +0200375 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100376 if (cache.flags >= 0) {
Pali Rohár4b226c22011-11-01 01:43:05 +0100377 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100378 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +0100379 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100380 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100381 cache.time_to_empty = -ENODATA;
382 cache.time_to_empty_avg = -ENODATA;
383 cache.time_to_full = -ENODATA;
384 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530385 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100386 } else {
387 cache.capacity = bq27x00_battery_read_rsoc(di);
Pali Rohára8f6bd22011-11-01 01:43:06 +0100388 cache.energy = bq27x00_battery_read_energy(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100389 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
390 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
391 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
392 cache.charge_full = bq27x00_battery_read_lmd(di);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530393 cache.health = bq27x00_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100394 }
Pali Rohárd149e982011-11-01 01:43:07 +0100395 cache.temperature = bq27x00_battery_read_temperature(di);
Pali Rohár73c244a2011-04-30 23:59:35 +0200396 cache.cycle_count = bq27x00_battery_read_cyct(di);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530397 cache.power_avg =
398 bq27x00_battery_read_pwr_avg(di, BQ27x00_POWER_AVG);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100399
Pali Rohár631c17e2011-02-01 00:08:02 +0100400 /* We only have to read charge design full once */
401 if (di->charge_design_full <= 0)
402 di->charge_design_full = bq27x00_battery_read_ilmd(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100403 }
404
Pali Rohárb68f6212011-11-01 01:43:03 +0100405 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100406 di->cache = cache;
407 power_supply_changed(&di->bat);
408 }
409
410 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700411}
412
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100413static void bq27x00_battery_poll(struct work_struct *work)
414{
415 struct bq27x00_device_info *di =
416 container_of(work, struct bq27x00_device_info, work.work);
417
418 bq27x00_update(di);
419
420 if (poll_interval > 0) {
421 /* The timer does not have to be accurate. */
422 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
423 schedule_delayed_work(&di->work, poll_interval * HZ);
424 }
425}
426
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700427/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100428 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700429 * Note that current can be negative signed as well
430 * Or 0 if something fails.
431 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100432static int bq27x00_battery_current(struct bq27x00_device_info *di,
433 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700434{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100435 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100436 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700437
Pali Rohárb68f6212011-11-01 01:43:03 +0100438 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100439 if (curr < 0) {
440 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100441 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100442 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200443
444 if (di->chip == BQ27500) {
445 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100446 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200447 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100448 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
449 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200450 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200451 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200452 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100453
454 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700455 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200456
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100457 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700458}
459
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200460static int bq27x00_battery_status(struct bq27x00_device_info *di,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100461 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200462{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200463 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200464
465 if (di->chip == BQ27500) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100466 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200467 status = POWER_SUPPLY_STATUS_FULL;
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400468 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200469 status = POWER_SUPPLY_STATUS_DISCHARGING;
Pali Rohár270968c2011-11-01 01:43:08 +0100470 else
Anton Vorontsovb7aaacf2012-02-01 21:20:15 +0400471 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200472 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200473 if (di->cache.flags & BQ27000_FLAG_FC)
474 status = POWER_SUPPLY_STATUS_FULL;
475 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200476 status = POWER_SUPPLY_STATUS_CHARGING;
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200477 else if (power_supply_am_i_supplied(&di->bat))
478 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200479 else
480 status = POWER_SUPPLY_STATUS_DISCHARGING;
481 }
482
483 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100484
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200485 return 0;
486}
487
Pali Rohárd66bab32011-11-01 01:43:04 +0100488static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
489 union power_supply_propval *val)
490{
491 int level;
492
493 if (di->chip == BQ27500) {
494 if (di->cache.flags & BQ27500_FLAG_FC)
495 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
496 else if (di->cache.flags & BQ27500_FLAG_SOC1)
497 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
498 else if (di->cache.flags & BQ27500_FLAG_SOCF)
499 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
500 else
501 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
502 } else {
503 if (di->cache.flags & BQ27000_FLAG_FC)
504 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
505 else if (di->cache.flags & BQ27000_FLAG_EDV1)
506 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
507 else if (di->cache.flags & BQ27000_FLAG_EDVF)
508 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
509 else
510 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
511 }
512
513 val->intval = level;
514
515 return 0;
516}
517
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200518/*
Thomas Weber91fe4d52012-02-17 17:46:21 +0100519 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100520 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200521 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100522static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
523 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200524{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100525 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200526
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100527 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100528 if (volt < 0) {
529 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100530 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100531 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200532
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100533 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200534
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100535 return 0;
536}
537
538static int bq27x00_simple_value(int value,
539 union power_supply_propval *val)
540{
541 if (value < 0)
542 return value;
543
544 val->intval = value;
545
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200546 return 0;
547}
548
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700549#define to_bq27x00_device_info(x) container_of((x), \
550 struct bq27x00_device_info, bat);
551
552static int bq27x00_battery_get_property(struct power_supply *psy,
553 enum power_supply_property psp,
554 union power_supply_propval *val)
555{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200556 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700557 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200558
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100559 mutex_lock(&di->lock);
560 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
561 cancel_delayed_work_sync(&di->work);
562 bq27x00_battery_poll(&di->work.work);
563 }
564 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100565
566 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200567 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700568
569 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200570 case POWER_SUPPLY_PROP_STATUS:
571 ret = bq27x00_battery_status(di, val);
572 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700573 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100574 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200575 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700576 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100577 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700578 break;
579 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100580 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700581 break;
582 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100583 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700584 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100585 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
586 ret = bq27x00_battery_capacity_level(di, val);
587 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700588 case POWER_SUPPLY_PROP_TEMP:
Pali Rohárd149e982011-11-01 01:43:07 +0100589 ret = bq27x00_simple_value(di->cache.temperature, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700590 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200591 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100592 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200593 break;
594 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100595 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200596 break;
597 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100598 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200599 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200600 case POWER_SUPPLY_PROP_TECHNOLOGY:
601 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
602 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100603 case POWER_SUPPLY_PROP_CHARGE_NOW:
604 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
605 break;
606 case POWER_SUPPLY_PROP_CHARGE_FULL:
607 ret = bq27x00_simple_value(di->cache.charge_full, val);
608 break;
609 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
610 ret = bq27x00_simple_value(di->charge_design_full, val);
611 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200612 case POWER_SUPPLY_PROP_CYCLE_COUNT:
613 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100614 break;
615 case POWER_SUPPLY_PROP_ENERGY_NOW:
Pali Rohára8f6bd22011-11-01 01:43:06 +0100616 ret = bq27x00_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100617 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530618 case POWER_SUPPLY_PROP_POWER_AVG:
619 ret = bq27x00_simple_value(di->cache.power_avg, val);
620 break;
621 case POWER_SUPPLY_PROP_HEALTH:
622 ret = bq27x00_simple_value(di->cache.health, val);
623 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700624 default:
625 return -EINVAL;
626 }
627
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200628 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700629}
630
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100631static void bq27x00_external_power_changed(struct power_supply *psy)
632{
633 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
634
635 cancel_delayed_work_sync(&di->work);
636 schedule_delayed_work(&di->work, 0);
637}
638
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200639static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700640{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200641 int ret;
642
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700643 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
644 di->bat.properties = bq27x00_battery_props;
645 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
646 di->bat.get_property = bq27x00_battery_get_property;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100647 di->bat.external_power_changed = bq27x00_external_power_changed;
648
649 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
650 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200651
652 ret = power_supply_register(di->dev, &di->bat);
653 if (ret) {
654 dev_err(di->dev, "failed to register battery: %d\n", ret);
655 return ret;
656 }
657
658 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
659
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100660 bq27x00_update(di);
661
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200662 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700663}
664
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100665static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
666{
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100667 /*
668 * power_supply_unregister call bq27x00_battery_get_property which
669 * call bq27x00_battery_poll.
670 * Make sure that bq27x00_battery_poll will not call
671 * schedule_delayed_work again after unregister (which cause OOPS).
672 */
673 poll_interval = 0;
674
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100675 cancel_delayed_work_sync(&di->work);
676
677 power_supply_unregister(&di->bat);
678
679 mutex_destroy(&di->lock);
680}
681
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200682
683/* i2c specific code */
684#ifdef CONFIG_BATTERY_BQ27X00_I2C
685
686/* If the system has several batteries we need a different name for each
687 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700688 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200689static DEFINE_IDR(battery_id);
690static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700691
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100692static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700693{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200694 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200695 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700696 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100697 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700698
699 if (!client->adapter)
700 return -ENODEV;
701
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200702 msg[0].addr = client->addr;
703 msg[0].flags = 0;
704 msg[0].buf = &reg;
705 msg[0].len = sizeof(reg);
706 msg[1].addr = client->addr;
707 msg[1].flags = I2C_M_RD;
708 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100709 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200710 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100711 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200712 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100713
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200714 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100715 if (ret < 0)
716 return ret;
717
718 if (!single)
719 ret = get_unaligned_le16(data);
720 else
721 ret = data[0];
722
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100723 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700724}
725
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200726static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700727 const struct i2c_device_id *id)
728{
729 char *name;
730 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700731 int num;
732 int retval = 0;
733
734 /* Get new ID for the new battery device */
735 retval = idr_pre_get(&battery_id, GFP_KERNEL);
736 if (retval == 0)
737 return -ENOMEM;
738 mutex_lock(&battery_mutex);
739 retval = idr_get_new(&battery_id, client, &num);
740 mutex_unlock(&battery_mutex);
741 if (retval < 0)
742 return retval;
743
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200744 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700745 if (!name) {
746 dev_err(&client->dev, "failed to allocate device name\n");
747 retval = -ENOMEM;
748 goto batt_failed_1;
749 }
750
751 di = kzalloc(sizeof(*di), GFP_KERNEL);
752 if (!di) {
753 dev_err(&client->dev, "failed to allocate device info data\n");
754 retval = -ENOMEM;
755 goto batt_failed_2;
756 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700757
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200758 di->id = num;
759 di->dev = &client->dev;
760 di->chip = id->driver_data;
761 di->bat.name = name;
762 di->bus.read = &bq27x00_read_i2c;
763
764 if (bq27x00_powersupply_init(di))
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700765 goto batt_failed_3;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700766
767 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700768
769 return 0;
770
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700771batt_failed_3:
772 kfree(di);
773batt_failed_2:
774 kfree(name);
775batt_failed_1:
776 mutex_lock(&battery_mutex);
777 idr_remove(&battery_id, num);
778 mutex_unlock(&battery_mutex);
779
780 return retval;
781}
782
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200783static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700784{
785 struct bq27x00_device_info *di = i2c_get_clientdata(client);
786
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100787 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700788
789 kfree(di->bat.name);
790
791 mutex_lock(&battery_mutex);
792 idr_remove(&battery_id, di->id);
793 mutex_unlock(&battery_mutex);
794
795 kfree(di);
796
797 return 0;
798}
799
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200800static const struct i2c_device_id bq27x00_id[] = {
801 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
802 { "bq27500", BQ27500 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700803 {},
804};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100805MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700806
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200807static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700808 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200809 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700810 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200811 .probe = bq27x00_battery_probe,
812 .remove = bq27x00_battery_remove,
813 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700814};
815
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200816static inline int bq27x00_battery_i2c_init(void)
817{
818 int ret = i2c_add_driver(&bq27x00_battery_driver);
819 if (ret)
820 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
821
822 return ret;
823}
824
825static inline void bq27x00_battery_i2c_exit(void)
826{
827 i2c_del_driver(&bq27x00_battery_driver);
828}
829
830#else
831
832static inline int bq27x00_battery_i2c_init(void) { return 0; }
833static inline void bq27x00_battery_i2c_exit(void) {};
834
835#endif
836
837/* platform specific code */
838#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
839
840static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100841 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200842{
843 struct device *dev = di->dev;
844 struct bq27000_platform_data *pdata = dev->platform_data;
845 unsigned int timeout = 3;
846 int upper, lower;
847 int temp;
848
849 if (!single) {
850 /* Make sure the value has not changed in between reading the
851 * lower and the upper part */
852 upper = pdata->read(dev, reg + 1);
853 do {
854 temp = upper;
855 if (upper < 0)
856 return upper;
857
858 lower = pdata->read(dev, reg);
859 if (lower < 0)
860 return lower;
861
862 upper = pdata->read(dev, reg + 1);
863 } while (temp != upper && --timeout);
864
865 if (timeout == 0)
866 return -EIO;
867
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100868 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200869 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100870
871 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200872}
873
874static int __devinit bq27000_battery_probe(struct platform_device *pdev)
875{
876 struct bq27x00_device_info *di;
877 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
878 int ret;
879
880 if (!pdata) {
881 dev_err(&pdev->dev, "no platform_data supplied\n");
882 return -EINVAL;
883 }
884
885 if (!pdata->read) {
886 dev_err(&pdev->dev, "no hdq read callback supplied\n");
887 return -EINVAL;
888 }
889
890 di = kzalloc(sizeof(*di), GFP_KERNEL);
891 if (!di) {
892 dev_err(&pdev->dev, "failed to allocate device info data\n");
893 return -ENOMEM;
894 }
895
896 platform_set_drvdata(pdev, di);
897
898 di->dev = &pdev->dev;
899 di->chip = BQ27000;
900
901 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
902 di->bus.read = &bq27000_read_platform;
903
904 ret = bq27x00_powersupply_init(di);
905 if (ret)
906 goto err_free;
907
908 return 0;
909
910err_free:
911 platform_set_drvdata(pdev, NULL);
912 kfree(di);
913
914 return ret;
915}
916
917static int __devexit bq27000_battery_remove(struct platform_device *pdev)
918{
919 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
920
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100921 bq27x00_powersupply_unregister(di);
922
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200923 platform_set_drvdata(pdev, NULL);
924 kfree(di);
925
926 return 0;
927}
928
929static struct platform_driver bq27000_battery_driver = {
930 .probe = bq27000_battery_probe,
931 .remove = __devexit_p(bq27000_battery_remove),
932 .driver = {
933 .name = "bq27000-battery",
934 .owner = THIS_MODULE,
935 },
936};
937
938static inline int bq27x00_battery_platform_init(void)
939{
940 int ret = platform_driver_register(&bq27000_battery_driver);
941 if (ret)
942 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
943
944 return ret;
945}
946
947static inline void bq27x00_battery_platform_exit(void)
948{
949 platform_driver_unregister(&bq27000_battery_driver);
950}
951
952#else
953
954static inline int bq27x00_battery_platform_init(void) { return 0; }
955static inline void bq27x00_battery_platform_exit(void) {};
956
957#endif
958
959/*
960 * Module stuff
961 */
962
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700963static int __init bq27x00_battery_init(void)
964{
965 int ret;
966
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200967 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700968 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200969 return ret;
970
971 ret = bq27x00_battery_platform_init();
972 if (ret)
973 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700974
975 return ret;
976}
977module_init(bq27x00_battery_init);
978
979static void __exit bq27x00_battery_exit(void)
980{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200981 bq27x00_battery_platform_exit();
982 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700983}
984module_exit(bq27x00_battery_exit);
985
986MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
987MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
988MODULE_LICENSE("GPL");