blob: a22124ab1e3bcae059b66b5e2c0b3472ed987d82 [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árd149e982011-11-01 01:43:07 +0100251 * Return the battery temperature in tenths of degree Celsius
252 * Or < 0 if something fails.
253 */
254static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
255{
256 int temp;
257
258 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
259 if (temp < 0) {
260 dev_err(di->dev, "error reading temperature\n");
261 return temp;
262 }
263
264 if (di->chip == BQ27500)
265 temp -= 2731;
266 else
267 temp = ((temp * 5) - 5463) / 2;
268
269 return temp;
270}
271
272/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100273 * Return the battery Cycle count total
274 * Or < 0 if something fails.
275 */
276static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
277{
278 int cyct;
279
280 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
281 if (cyct < 0)
282 dev_err(di->dev, "error reading cycle count total\n");
283
284 return cyct;
285}
286
287/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100288 * Read a time register.
289 * Return < 0 if something fails.
290 */
291static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
292{
293 int tval;
294
295 tval = bq27x00_read(di, reg, false);
296 if (tval < 0) {
297 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
298 return tval;
299 }
300
301 if (tval == 65535)
302 return -ENODATA;
303
304 return tval * 60;
305}
306
307static void bq27x00_update(struct bq27x00_device_info *di)
308{
309 struct bq27x00_reg_cache cache = {0, };
310 bool is_bq27500 = di->chip == BQ27500;
311
312 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
313 if (cache.flags >= 0) {
Pali Rohár4b226c22011-11-01 01:43:05 +0100314 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
315 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100316 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100317 cache.time_to_empty = -ENODATA;
318 cache.time_to_empty_avg = -ENODATA;
319 cache.time_to_full = -ENODATA;
320 cache.charge_full = -ENODATA;
321 } else {
322 cache.capacity = bq27x00_battery_read_rsoc(di);
Pali Rohára8f6bd22011-11-01 01:43:06 +0100323 cache.energy = bq27x00_battery_read_energy(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100324 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
325 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
326 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
327 cache.charge_full = bq27x00_battery_read_lmd(di);
328 }
Pali Rohárd149e982011-11-01 01:43:07 +0100329 cache.temperature = bq27x00_battery_read_temperature(di);
Pali Rohár73c244a2011-04-30 23:59:35 +0200330 cache.cycle_count = bq27x00_battery_read_cyct(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100331
Pali Rohár631c17e2011-02-01 00:08:02 +0100332 /* We only have to read charge design full once */
333 if (di->charge_design_full <= 0)
334 di->charge_design_full = bq27x00_battery_read_ilmd(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100335 }
336
Pali Rohárb68f6212011-11-01 01:43:03 +0100337 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100338 di->cache = cache;
339 power_supply_changed(&di->bat);
340 }
341
342 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700343}
344
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100345static void bq27x00_battery_poll(struct work_struct *work)
346{
347 struct bq27x00_device_info *di =
348 container_of(work, struct bq27x00_device_info, work.work);
349
350 bq27x00_update(di);
351
352 if (poll_interval > 0) {
353 /* The timer does not have to be accurate. */
354 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
355 schedule_delayed_work(&di->work, poll_interval * HZ);
356 }
357}
358
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700359/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100360 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700361 * Note that current can be negative signed as well
362 * Or 0 if something fails.
363 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100364static int bq27x00_battery_current(struct bq27x00_device_info *di,
365 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700366{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100367 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100368 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700369
Pali Rohárb68f6212011-11-01 01:43:03 +0100370 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100371 if (curr < 0)
372 return curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200373
374 if (di->chip == BQ27500) {
375 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100376 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200377 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100378 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
379 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200380 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200381 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200382 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100383
384 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700385 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200386
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100387 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700388}
389
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200390static int bq27x00_battery_status(struct bq27x00_device_info *di,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100391 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200392{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200393 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200394
395 if (di->chip == BQ27500) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100396 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200397 status = POWER_SUPPLY_STATUS_FULL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100398 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200399 status = POWER_SUPPLY_STATUS_DISCHARGING;
400 else
401 status = POWER_SUPPLY_STATUS_CHARGING;
402 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200403 if (di->cache.flags & BQ27000_FLAG_FC)
404 status = POWER_SUPPLY_STATUS_FULL;
405 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200406 status = POWER_SUPPLY_STATUS_CHARGING;
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200407 else if (power_supply_am_i_supplied(&di->bat))
408 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200409 else
410 status = POWER_SUPPLY_STATUS_DISCHARGING;
411 }
412
413 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100414
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200415 return 0;
416}
417
Pali Rohárd66bab32011-11-01 01:43:04 +0100418static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
419 union power_supply_propval *val)
420{
421 int level;
422
423 if (di->chip == BQ27500) {
424 if (di->cache.flags & BQ27500_FLAG_FC)
425 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
426 else if (di->cache.flags & BQ27500_FLAG_SOC1)
427 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
428 else if (di->cache.flags & BQ27500_FLAG_SOCF)
429 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
430 else
431 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
432 } else {
433 if (di->cache.flags & BQ27000_FLAG_FC)
434 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
435 else if (di->cache.flags & BQ27000_FLAG_EDV1)
436 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
437 else if (di->cache.flags & BQ27000_FLAG_EDVF)
438 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
439 else
440 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
441 }
442
443 val->intval = level;
444
445 return 0;
446}
447
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200448/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100449 * Return the battery Voltage in milivolts
450 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200451 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100452static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
453 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200454{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100455 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200456
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100457 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
458 if (volt < 0)
459 return volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200460
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100461 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200462
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100463 return 0;
464}
465
466static int bq27x00_simple_value(int value,
467 union power_supply_propval *val)
468{
469 if (value < 0)
470 return value;
471
472 val->intval = value;
473
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200474 return 0;
475}
476
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700477#define to_bq27x00_device_info(x) container_of((x), \
478 struct bq27x00_device_info, bat);
479
480static int bq27x00_battery_get_property(struct power_supply *psy,
481 enum power_supply_property psp,
482 union power_supply_propval *val)
483{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200484 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700485 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200486
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100487 mutex_lock(&di->lock);
488 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
489 cancel_delayed_work_sync(&di->work);
490 bq27x00_battery_poll(&di->work.work);
491 }
492 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100493
494 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200495 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700496
497 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200498 case POWER_SUPPLY_PROP_STATUS:
499 ret = bq27x00_battery_status(di, val);
500 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700501 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100502 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200503 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700504 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100505 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700506 break;
507 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100508 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700509 break;
510 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100511 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700512 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100513 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
514 ret = bq27x00_battery_capacity_level(di, val);
515 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700516 case POWER_SUPPLY_PROP_TEMP:
Pali Rohárd149e982011-11-01 01:43:07 +0100517 ret = bq27x00_simple_value(di->cache.temperature, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700518 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200519 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100520 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200521 break;
522 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100523 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200524 break;
525 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100526 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200527 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200528 case POWER_SUPPLY_PROP_TECHNOLOGY:
529 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
530 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100531 case POWER_SUPPLY_PROP_CHARGE_NOW:
532 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
533 break;
534 case POWER_SUPPLY_PROP_CHARGE_FULL:
535 ret = bq27x00_simple_value(di->cache.charge_full, val);
536 break;
537 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
538 ret = bq27x00_simple_value(di->charge_design_full, val);
539 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200540 case POWER_SUPPLY_PROP_CYCLE_COUNT:
541 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100542 break;
543 case POWER_SUPPLY_PROP_ENERGY_NOW:
Pali Rohára8f6bd22011-11-01 01:43:06 +0100544 ret = bq27x00_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100545 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700546 default:
547 return -EINVAL;
548 }
549
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200550 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700551}
552
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100553static void bq27x00_external_power_changed(struct power_supply *psy)
554{
555 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
556
557 cancel_delayed_work_sync(&di->work);
558 schedule_delayed_work(&di->work, 0);
559}
560
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200561static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700562{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200563 int ret;
564
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700565 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
566 di->bat.properties = bq27x00_battery_props;
567 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
568 di->bat.get_property = bq27x00_battery_get_property;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100569 di->bat.external_power_changed = bq27x00_external_power_changed;
570
571 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
572 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200573
574 ret = power_supply_register(di->dev, &di->bat);
575 if (ret) {
576 dev_err(di->dev, "failed to register battery: %d\n", ret);
577 return ret;
578 }
579
580 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
581
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100582 bq27x00_update(di);
583
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200584 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700585}
586
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100587static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
588{
589 cancel_delayed_work_sync(&di->work);
590
591 power_supply_unregister(&di->bat);
592
593 mutex_destroy(&di->lock);
594}
595
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200596
597/* i2c specific code */
598#ifdef CONFIG_BATTERY_BQ27X00_I2C
599
600/* If the system has several batteries we need a different name for each
601 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700602 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200603static DEFINE_IDR(battery_id);
604static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700605
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100606static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700607{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200608 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200609 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700610 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100611 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700612
613 if (!client->adapter)
614 return -ENODEV;
615
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200616 msg[0].addr = client->addr;
617 msg[0].flags = 0;
618 msg[0].buf = &reg;
619 msg[0].len = sizeof(reg);
620 msg[1].addr = client->addr;
621 msg[1].flags = I2C_M_RD;
622 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100623 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200624 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100625 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200626 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100627
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200628 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100629 if (ret < 0)
630 return ret;
631
632 if (!single)
633 ret = get_unaligned_le16(data);
634 else
635 ret = data[0];
636
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100637 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700638}
639
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200640static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700641 const struct i2c_device_id *id)
642{
643 char *name;
644 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700645 int num;
646 int retval = 0;
647
648 /* Get new ID for the new battery device */
649 retval = idr_pre_get(&battery_id, GFP_KERNEL);
650 if (retval == 0)
651 return -ENOMEM;
652 mutex_lock(&battery_mutex);
653 retval = idr_get_new(&battery_id, client, &num);
654 mutex_unlock(&battery_mutex);
655 if (retval < 0)
656 return retval;
657
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200658 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700659 if (!name) {
660 dev_err(&client->dev, "failed to allocate device name\n");
661 retval = -ENOMEM;
662 goto batt_failed_1;
663 }
664
665 di = kzalloc(sizeof(*di), GFP_KERNEL);
666 if (!di) {
667 dev_err(&client->dev, "failed to allocate device info data\n");
668 retval = -ENOMEM;
669 goto batt_failed_2;
670 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700671
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200672 di->id = num;
673 di->dev = &client->dev;
674 di->chip = id->driver_data;
675 di->bat.name = name;
676 di->bus.read = &bq27x00_read_i2c;
677
678 if (bq27x00_powersupply_init(di))
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700679 goto batt_failed_3;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700680
681 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700682
683 return 0;
684
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700685batt_failed_3:
686 kfree(di);
687batt_failed_2:
688 kfree(name);
689batt_failed_1:
690 mutex_lock(&battery_mutex);
691 idr_remove(&battery_id, num);
692 mutex_unlock(&battery_mutex);
693
694 return retval;
695}
696
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200697static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700698{
699 struct bq27x00_device_info *di = i2c_get_clientdata(client);
700
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100701 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700702
703 kfree(di->bat.name);
704
705 mutex_lock(&battery_mutex);
706 idr_remove(&battery_id, di->id);
707 mutex_unlock(&battery_mutex);
708
709 kfree(di);
710
711 return 0;
712}
713
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200714static const struct i2c_device_id bq27x00_id[] = {
715 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
716 { "bq27500", BQ27500 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700717 {},
718};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100719MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700720
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200721static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700722 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200723 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700724 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200725 .probe = bq27x00_battery_probe,
726 .remove = bq27x00_battery_remove,
727 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700728};
729
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200730static inline int bq27x00_battery_i2c_init(void)
731{
732 int ret = i2c_add_driver(&bq27x00_battery_driver);
733 if (ret)
734 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
735
736 return ret;
737}
738
739static inline void bq27x00_battery_i2c_exit(void)
740{
741 i2c_del_driver(&bq27x00_battery_driver);
742}
743
744#else
745
746static inline int bq27x00_battery_i2c_init(void) { return 0; }
747static inline void bq27x00_battery_i2c_exit(void) {};
748
749#endif
750
751/* platform specific code */
752#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
753
754static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100755 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200756{
757 struct device *dev = di->dev;
758 struct bq27000_platform_data *pdata = dev->platform_data;
759 unsigned int timeout = 3;
760 int upper, lower;
761 int temp;
762
763 if (!single) {
764 /* Make sure the value has not changed in between reading the
765 * lower and the upper part */
766 upper = pdata->read(dev, reg + 1);
767 do {
768 temp = upper;
769 if (upper < 0)
770 return upper;
771
772 lower = pdata->read(dev, reg);
773 if (lower < 0)
774 return lower;
775
776 upper = pdata->read(dev, reg + 1);
777 } while (temp != upper && --timeout);
778
779 if (timeout == 0)
780 return -EIO;
781
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100782 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200783 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100784
785 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200786}
787
788static int __devinit bq27000_battery_probe(struct platform_device *pdev)
789{
790 struct bq27x00_device_info *di;
791 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
792 int ret;
793
794 if (!pdata) {
795 dev_err(&pdev->dev, "no platform_data supplied\n");
796 return -EINVAL;
797 }
798
799 if (!pdata->read) {
800 dev_err(&pdev->dev, "no hdq read callback supplied\n");
801 return -EINVAL;
802 }
803
804 di = kzalloc(sizeof(*di), GFP_KERNEL);
805 if (!di) {
806 dev_err(&pdev->dev, "failed to allocate device info data\n");
807 return -ENOMEM;
808 }
809
810 platform_set_drvdata(pdev, di);
811
812 di->dev = &pdev->dev;
813 di->chip = BQ27000;
814
815 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
816 di->bus.read = &bq27000_read_platform;
817
818 ret = bq27x00_powersupply_init(di);
819 if (ret)
820 goto err_free;
821
822 return 0;
823
824err_free:
825 platform_set_drvdata(pdev, NULL);
826 kfree(di);
827
828 return ret;
829}
830
831static int __devexit bq27000_battery_remove(struct platform_device *pdev)
832{
833 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
834
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100835 bq27x00_powersupply_unregister(di);
836
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200837 platform_set_drvdata(pdev, NULL);
838 kfree(di);
839
840 return 0;
841}
842
843static struct platform_driver bq27000_battery_driver = {
844 .probe = bq27000_battery_probe,
845 .remove = __devexit_p(bq27000_battery_remove),
846 .driver = {
847 .name = "bq27000-battery",
848 .owner = THIS_MODULE,
849 },
850};
851
852static inline int bq27x00_battery_platform_init(void)
853{
854 int ret = platform_driver_register(&bq27000_battery_driver);
855 if (ret)
856 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
857
858 return ret;
859}
860
861static inline void bq27x00_battery_platform_exit(void)
862{
863 platform_driver_unregister(&bq27000_battery_driver);
864}
865
866#else
867
868static inline int bq27x00_battery_platform_init(void) { return 0; }
869static inline void bq27x00_battery_platform_exit(void) {};
870
871#endif
872
873/*
874 * Module stuff
875 */
876
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700877static int __init bq27x00_battery_init(void)
878{
879 int ret;
880
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200881 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700882 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200883 return ret;
884
885 ret = bq27x00_battery_platform_init();
886 if (ret)
887 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700888
889 return ret;
890}
891module_init(bq27x00_battery_init);
892
893static void __exit bq27x00_battery_exit(void)
894{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200895 bq27x00_battery_platform_exit();
896 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700897}
898module_exit(bq27x00_battery_exit);
899
900MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
901MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
902MODULE_LICENSE("GPL");