blob: e9aeb533a1f074742871963e50e6591047c1c643 [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
Pali Rohár631c17e2011-02-01 00:08:02 +010050#define BQ27x00_REG_NAC 0x0C /* Nominal available capaciy */
51#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
52#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
53#define BQ27x00_REG_AE 0x22 /* Available enery */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070054
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020055#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
Pali Rohár631c17e2011-02-01 00:08:02 +010056#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
Pali Rohárd66bab32011-11-01 01:43:04 +010057#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
58#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
Pali Rohár4b226c22011-11-01 01:43:05 +010059#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +020060#define BQ27000_FLAG_FC BIT(5)
Pali Rohárd66bab32011-11-01 01:43:04 +010061#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020062
Pali Rohárbf7d4142011-02-01 00:23:13 +010063#define BQ27500_REG_SOC 0x2C
Pali Rohár631c17e2011-02-01 00:08:02 +010064#define BQ27500_REG_DCAP 0x3C /* Design capacity */
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020065#define BQ27500_FLAG_DSC BIT(0)
Pali Rohárd66bab32011-11-01 01:43:04 +010066#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
67#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020068#define BQ27500_FLAG_FC BIT(9)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020069
Pali Rohára2e51182010-05-24 20:52:13 +020070#define BQ27000_RS 20 /* Resistor sense */
71
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070072struct bq27x00_device_info;
73struct bq27x00_access_methods {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010074 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070075};
76
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020077enum bq27x00_chip { BQ27000, BQ27500 };
78
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010079struct bq27x00_reg_cache {
80 int temperature;
81 int time_to_empty;
82 int time_to_empty_avg;
83 int time_to_full;
Pali Rohár631c17e2011-02-01 00:08:02 +010084 int charge_full;
Pali Rohár73c244a2011-04-30 23:59:35 +020085 int cycle_count;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010086 int capacity;
Pali Rohára8f6bd22011-11-01 01:43:06 +010087 int energy;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010088 int flags;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010089};
90
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070091struct bq27x00_device_info {
92 struct device *dev;
93 int id;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020094 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070095
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010096 struct bq27x00_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +010097 int charge_design_full;
98
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010099 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100100 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100101
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200102 struct power_supply bat;
103
104 struct bq27x00_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100105
106 struct mutex lock;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700107};
108
109static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200110 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700111 POWER_SUPPLY_PROP_PRESENT,
112 POWER_SUPPLY_PROP_VOLTAGE_NOW,
113 POWER_SUPPLY_PROP_CURRENT_NOW,
114 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100115 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700116 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200117 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
118 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
119 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200120 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100121 POWER_SUPPLY_PROP_CHARGE_FULL,
122 POWER_SUPPLY_PROP_CHARGE_NOW,
123 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200124 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100125 POWER_SUPPLY_PROP_ENERGY_NOW,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700126};
127
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100128static unsigned int poll_interval = 360;
129module_param(poll_interval, uint, 0644);
130MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
131 "0 disables polling");
132
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700133/*
134 * Common code for BQ27x00 devices
135 */
136
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200137static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100138 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700139{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100140 return di->bus.read(di, reg, single);
141}
142
143/*
144 * Return the battery Relative State-of-Charge
145 * Or < 0 if something fails.
146 */
147static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
148{
149 int rsoc;
150
151 if (di->chip == BQ27500)
152 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
153 else
154 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
155
156 if (rsoc < 0)
157 dev_err(di->dev, "error reading relative State-of-Charge\n");
158
159 return rsoc;
160}
161
162/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100163 * Return a battery charge value in µAh
164 * Or < 0 if something fails.
165 */
166static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
167{
168 int charge;
169
170 charge = bq27x00_read(di, reg, false);
171 if (charge < 0) {
172 dev_err(di->dev, "error reading nominal available capacity\n");
173 return charge;
174 }
175
176 if (di->chip == BQ27500)
177 charge *= 1000;
178 else
179 charge = charge * 3570 / BQ27000_RS;
180
181 return charge;
182}
183
184/*
185 * Return the battery Nominal available capaciy in µAh
186 * Or < 0 if something fails.
187 */
188static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
189{
190 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
191}
192
193/*
194 * Return the battery Last measured discharge in µAh
195 * Or < 0 if something fails.
196 */
197static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
198{
199 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
200}
201
202/*
203 * Return the battery Initial last measured discharge in µAh
204 * Or < 0 if something fails.
205 */
206static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
207{
208 int ilmd;
209
210 if (di->chip == BQ27500)
211 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
212 else
213 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
214
215 if (ilmd < 0) {
216 dev_err(di->dev, "error reading initial last measured discharge\n");
217 return ilmd;
218 }
219
220 if (di->chip == BQ27500)
221 ilmd *= 1000;
222 else
223 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
224
225 return ilmd;
226}
227
228/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100229 * Return the battery Available energy in µWh
230 * Or < 0 if something fails.
231 */
232static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
233{
234 int ae;
235
236 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
237 if (ae < 0) {
238 dev_err(di->dev, "error reading available energy\n");
239 return ae;
240 }
241
242 if (di->chip == BQ27500)
243 ae *= 1000;
244 else
245 ae = ae * 29200 / BQ27000_RS;
246
247 return ae;
248}
249
250/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100251 * Return the battery Cycle count total
252 * Or < 0 if something fails.
253 */
254static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
255{
256 int cyct;
257
258 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
259 if (cyct < 0)
260 dev_err(di->dev, "error reading cycle count total\n");
261
262 return cyct;
263}
264
265/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100266 * Read a time register.
267 * Return < 0 if something fails.
268 */
269static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
270{
271 int tval;
272
273 tval = bq27x00_read(di, reg, false);
274 if (tval < 0) {
275 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
276 return tval;
277 }
278
279 if (tval == 65535)
280 return -ENODATA;
281
282 return tval * 60;
283}
284
285static void bq27x00_update(struct bq27x00_device_info *di)
286{
287 struct bq27x00_reg_cache cache = {0, };
288 bool is_bq27500 = di->chip == BQ27500;
289
290 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
291 if (cache.flags >= 0) {
Pali Rohár4b226c22011-11-01 01:43:05 +0100292 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
293 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100294 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100295 cache.time_to_empty = -ENODATA;
296 cache.time_to_empty_avg = -ENODATA;
297 cache.time_to_full = -ENODATA;
298 cache.charge_full = -ENODATA;
299 } else {
300 cache.capacity = bq27x00_battery_read_rsoc(di);
Pali Rohára8f6bd22011-11-01 01:43:06 +0100301 cache.energy = bq27x00_battery_read_energy(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100302 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
303 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
304 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
305 cache.charge_full = bq27x00_battery_read_lmd(di);
306 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100307 cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
Pali Rohár73c244a2011-04-30 23:59:35 +0200308 cache.cycle_count = bq27x00_battery_read_cyct(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100309
Pali Rohár631c17e2011-02-01 00:08:02 +0100310 /* We only have to read charge design full once */
311 if (di->charge_design_full <= 0)
312 di->charge_design_full = bq27x00_battery_read_ilmd(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100313 }
314
Pali Rohárb68f6212011-11-01 01:43:03 +0100315 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100316 di->cache = cache;
317 power_supply_changed(&di->bat);
318 }
319
320 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700321}
322
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100323static void bq27x00_battery_poll(struct work_struct *work)
324{
325 struct bq27x00_device_info *di =
326 container_of(work, struct bq27x00_device_info, work.work);
327
328 bq27x00_update(di);
329
330 if (poll_interval > 0) {
331 /* The timer does not have to be accurate. */
332 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
333 schedule_delayed_work(&di->work, poll_interval * HZ);
334 }
335}
336
337
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700338/*
Grazvydas Ignotasb4de3602010-02-12 23:57:13 +0200339 * Return the battery temperature in tenths of degree Celsius
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700340 * Or < 0 if something fails.
341 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100342static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
343 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700344{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100345 if (di->cache.temperature < 0)
346 return di->cache.temperature;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700347
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200348 if (di->chip == BQ27500)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100349 val->intval = di->cache.temperature - 2731;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200350 else
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100351 val->intval = ((di->cache.temperature * 5) - 5463) / 2;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700352
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100353 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700354}
355
356/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100357 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700358 * Note that current can be negative signed as well
359 * Or 0 if something fails.
360 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100361static int bq27x00_battery_current(struct bq27x00_device_info *di,
362 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700363{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100364 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100365 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700366
Pali Rohárb68f6212011-11-01 01:43:03 +0100367 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100368 if (curr < 0)
369 return curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200370
371 if (di->chip == BQ27500) {
372 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100373 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200374 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100375 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
376 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200377 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200378 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200379 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100380
381 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700382 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200383
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100384 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700385}
386
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200387static int bq27x00_battery_status(struct bq27x00_device_info *di,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100388 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200389{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200390 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200391
392 if (di->chip == BQ27500) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100393 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200394 status = POWER_SUPPLY_STATUS_FULL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100395 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200396 status = POWER_SUPPLY_STATUS_DISCHARGING;
397 else
398 status = POWER_SUPPLY_STATUS_CHARGING;
399 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200400 if (di->cache.flags & BQ27000_FLAG_FC)
401 status = POWER_SUPPLY_STATUS_FULL;
402 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200403 status = POWER_SUPPLY_STATUS_CHARGING;
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200404 else if (power_supply_am_i_supplied(&di->bat))
405 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200406 else
407 status = POWER_SUPPLY_STATUS_DISCHARGING;
408 }
409
410 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100411
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200412 return 0;
413}
414
Pali Rohárd66bab32011-11-01 01:43:04 +0100415static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
416 union power_supply_propval *val)
417{
418 int level;
419
420 if (di->chip == BQ27500) {
421 if (di->cache.flags & BQ27500_FLAG_FC)
422 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
423 else if (di->cache.flags & BQ27500_FLAG_SOC1)
424 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
425 else if (di->cache.flags & BQ27500_FLAG_SOCF)
426 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
427 else
428 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
429 } else {
430 if (di->cache.flags & BQ27000_FLAG_FC)
431 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
432 else if (di->cache.flags & BQ27000_FLAG_EDV1)
433 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
434 else if (di->cache.flags & BQ27000_FLAG_EDVF)
435 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
436 else
437 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
438 }
439
440 val->intval = level;
441
442 return 0;
443}
444
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200445/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100446 * Return the battery Voltage in milivolts
447 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200448 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100449static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
450 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200451{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100452 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200453
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100454 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
455 if (volt < 0)
456 return volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200457
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100458 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200459
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100460 return 0;
461}
462
463static int bq27x00_simple_value(int value,
464 union power_supply_propval *val)
465{
466 if (value < 0)
467 return value;
468
469 val->intval = value;
470
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200471 return 0;
472}
473
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700474#define to_bq27x00_device_info(x) container_of((x), \
475 struct bq27x00_device_info, bat);
476
477static int bq27x00_battery_get_property(struct power_supply *psy,
478 enum power_supply_property psp,
479 union power_supply_propval *val)
480{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200481 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700482 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200483
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100484 mutex_lock(&di->lock);
485 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
486 cancel_delayed_work_sync(&di->work);
487 bq27x00_battery_poll(&di->work.work);
488 }
489 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100490
491 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200492 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700493
494 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200495 case POWER_SUPPLY_PROP_STATUS:
496 ret = bq27x00_battery_status(di, val);
497 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700498 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100499 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200500 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700501 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100502 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700503 break;
504 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100505 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700506 break;
507 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100508 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700509 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100510 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
511 ret = bq27x00_battery_capacity_level(di, val);
512 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700513 case POWER_SUPPLY_PROP_TEMP:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100514 ret = bq27x00_battery_temperature(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700515 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200516 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100517 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200518 break;
519 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100520 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200521 break;
522 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100523 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200524 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200525 case POWER_SUPPLY_PROP_TECHNOLOGY:
526 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
527 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100528 case POWER_SUPPLY_PROP_CHARGE_NOW:
529 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
530 break;
531 case POWER_SUPPLY_PROP_CHARGE_FULL:
532 ret = bq27x00_simple_value(di->cache.charge_full, val);
533 break;
534 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
535 ret = bq27x00_simple_value(di->charge_design_full, val);
536 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200537 case POWER_SUPPLY_PROP_CYCLE_COUNT:
538 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100539 break;
540 case POWER_SUPPLY_PROP_ENERGY_NOW:
Pali Rohára8f6bd22011-11-01 01:43:06 +0100541 ret = bq27x00_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100542 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700543 default:
544 return -EINVAL;
545 }
546
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200547 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700548}
549
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100550static void bq27x00_external_power_changed(struct power_supply *psy)
551{
552 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
553
554 cancel_delayed_work_sync(&di->work);
555 schedule_delayed_work(&di->work, 0);
556}
557
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200558static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700559{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200560 int ret;
561
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700562 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
563 di->bat.properties = bq27x00_battery_props;
564 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
565 di->bat.get_property = bq27x00_battery_get_property;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100566 di->bat.external_power_changed = bq27x00_external_power_changed;
567
568 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
569 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200570
571 ret = power_supply_register(di->dev, &di->bat);
572 if (ret) {
573 dev_err(di->dev, "failed to register battery: %d\n", ret);
574 return ret;
575 }
576
577 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
578
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100579 bq27x00_update(di);
580
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200581 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700582}
583
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100584static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
585{
586 cancel_delayed_work_sync(&di->work);
587
588 power_supply_unregister(&di->bat);
589
590 mutex_destroy(&di->lock);
591}
592
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200593
594/* i2c specific code */
595#ifdef CONFIG_BATTERY_BQ27X00_I2C
596
597/* If the system has several batteries we need a different name for each
598 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700599 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200600static DEFINE_IDR(battery_id);
601static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700602
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100603static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700604{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200605 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200606 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700607 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100608 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700609
610 if (!client->adapter)
611 return -ENODEV;
612
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200613 msg[0].addr = client->addr;
614 msg[0].flags = 0;
615 msg[0].buf = &reg;
616 msg[0].len = sizeof(reg);
617 msg[1].addr = client->addr;
618 msg[1].flags = I2C_M_RD;
619 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100620 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200621 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100622 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200623 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100624
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200625 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100626 if (ret < 0)
627 return ret;
628
629 if (!single)
630 ret = get_unaligned_le16(data);
631 else
632 ret = data[0];
633
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100634 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700635}
636
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200637static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700638 const struct i2c_device_id *id)
639{
640 char *name;
641 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700642 int num;
643 int retval = 0;
644
645 /* Get new ID for the new battery device */
646 retval = idr_pre_get(&battery_id, GFP_KERNEL);
647 if (retval == 0)
648 return -ENOMEM;
649 mutex_lock(&battery_mutex);
650 retval = idr_get_new(&battery_id, client, &num);
651 mutex_unlock(&battery_mutex);
652 if (retval < 0)
653 return retval;
654
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200655 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700656 if (!name) {
657 dev_err(&client->dev, "failed to allocate device name\n");
658 retval = -ENOMEM;
659 goto batt_failed_1;
660 }
661
662 di = kzalloc(sizeof(*di), GFP_KERNEL);
663 if (!di) {
664 dev_err(&client->dev, "failed to allocate device info data\n");
665 retval = -ENOMEM;
666 goto batt_failed_2;
667 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700668
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200669 di->id = num;
670 di->dev = &client->dev;
671 di->chip = id->driver_data;
672 di->bat.name = name;
673 di->bus.read = &bq27x00_read_i2c;
674
675 if (bq27x00_powersupply_init(di))
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700676 goto batt_failed_3;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700677
678 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700679
680 return 0;
681
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700682batt_failed_3:
683 kfree(di);
684batt_failed_2:
685 kfree(name);
686batt_failed_1:
687 mutex_lock(&battery_mutex);
688 idr_remove(&battery_id, num);
689 mutex_unlock(&battery_mutex);
690
691 return retval;
692}
693
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200694static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700695{
696 struct bq27x00_device_info *di = i2c_get_clientdata(client);
697
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100698 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700699
700 kfree(di->bat.name);
701
702 mutex_lock(&battery_mutex);
703 idr_remove(&battery_id, di->id);
704 mutex_unlock(&battery_mutex);
705
706 kfree(di);
707
708 return 0;
709}
710
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200711static const struct i2c_device_id bq27x00_id[] = {
712 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
713 { "bq27500", BQ27500 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700714 {},
715};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100716MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700717
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200718static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700719 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200720 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700721 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200722 .probe = bq27x00_battery_probe,
723 .remove = bq27x00_battery_remove,
724 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700725};
726
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200727static inline int bq27x00_battery_i2c_init(void)
728{
729 int ret = i2c_add_driver(&bq27x00_battery_driver);
730 if (ret)
731 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
732
733 return ret;
734}
735
736static inline void bq27x00_battery_i2c_exit(void)
737{
738 i2c_del_driver(&bq27x00_battery_driver);
739}
740
741#else
742
743static inline int bq27x00_battery_i2c_init(void) { return 0; }
744static inline void bq27x00_battery_i2c_exit(void) {};
745
746#endif
747
748/* platform specific code */
749#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
750
751static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100752 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200753{
754 struct device *dev = di->dev;
755 struct bq27000_platform_data *pdata = dev->platform_data;
756 unsigned int timeout = 3;
757 int upper, lower;
758 int temp;
759
760 if (!single) {
761 /* Make sure the value has not changed in between reading the
762 * lower and the upper part */
763 upper = pdata->read(dev, reg + 1);
764 do {
765 temp = upper;
766 if (upper < 0)
767 return upper;
768
769 lower = pdata->read(dev, reg);
770 if (lower < 0)
771 return lower;
772
773 upper = pdata->read(dev, reg + 1);
774 } while (temp != upper && --timeout);
775
776 if (timeout == 0)
777 return -EIO;
778
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100779 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200780 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100781
782 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200783}
784
785static int __devinit bq27000_battery_probe(struct platform_device *pdev)
786{
787 struct bq27x00_device_info *di;
788 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
789 int ret;
790
791 if (!pdata) {
792 dev_err(&pdev->dev, "no platform_data supplied\n");
793 return -EINVAL;
794 }
795
796 if (!pdata->read) {
797 dev_err(&pdev->dev, "no hdq read callback supplied\n");
798 return -EINVAL;
799 }
800
801 di = kzalloc(sizeof(*di), GFP_KERNEL);
802 if (!di) {
803 dev_err(&pdev->dev, "failed to allocate device info data\n");
804 return -ENOMEM;
805 }
806
807 platform_set_drvdata(pdev, di);
808
809 di->dev = &pdev->dev;
810 di->chip = BQ27000;
811
812 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
813 di->bus.read = &bq27000_read_platform;
814
815 ret = bq27x00_powersupply_init(di);
816 if (ret)
817 goto err_free;
818
819 return 0;
820
821err_free:
822 platform_set_drvdata(pdev, NULL);
823 kfree(di);
824
825 return ret;
826}
827
828static int __devexit bq27000_battery_remove(struct platform_device *pdev)
829{
830 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
831
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100832 bq27x00_powersupply_unregister(di);
833
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200834 platform_set_drvdata(pdev, NULL);
835 kfree(di);
836
837 return 0;
838}
839
840static struct platform_driver bq27000_battery_driver = {
841 .probe = bq27000_battery_probe,
842 .remove = __devexit_p(bq27000_battery_remove),
843 .driver = {
844 .name = "bq27000-battery",
845 .owner = THIS_MODULE,
846 },
847};
848
849static inline int bq27x00_battery_platform_init(void)
850{
851 int ret = platform_driver_register(&bq27000_battery_driver);
852 if (ret)
853 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
854
855 return ret;
856}
857
858static inline void bq27x00_battery_platform_exit(void)
859{
860 platform_driver_unregister(&bq27000_battery_driver);
861}
862
863#else
864
865static inline int bq27x00_battery_platform_init(void) { return 0; }
866static inline void bq27x00_battery_platform_exit(void) {};
867
868#endif
869
870/*
871 * Module stuff
872 */
873
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700874static int __init bq27x00_battery_init(void)
875{
876 int ret;
877
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200878 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700879 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200880 return ret;
881
882 ret = bq27x00_battery_platform_init();
883 if (ret)
884 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700885
886 return ret;
887}
888module_init(bq27x00_battery_init);
889
890static void __exit bq27x00_battery_exit(void)
891{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200892 bq27x00_battery_platform_exit();
893 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700894}
895module_exit(bq27x00_battery_exit);
896
897MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
898MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
899MODULE_LICENSE("GPL");