blob: 6c8dfdb169a14fd4174386056a576f0468fd2968 [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;
87 int flags;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010088};
89
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070090struct bq27x00_device_info {
91 struct device *dev;
92 int id;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020093 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070094
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010095 struct bq27x00_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +010096 int charge_design_full;
97
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010098 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +010099 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100100
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200101 struct power_supply bat;
102
103 struct bq27x00_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100104
105 struct mutex lock;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700106};
107
108static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200109 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700110 POWER_SUPPLY_PROP_PRESENT,
111 POWER_SUPPLY_PROP_VOLTAGE_NOW,
112 POWER_SUPPLY_PROP_CURRENT_NOW,
113 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100114 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700115 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200116 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
117 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
118 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200119 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100120 POWER_SUPPLY_PROP_CHARGE_FULL,
121 POWER_SUPPLY_PROP_CHARGE_NOW,
122 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200123 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100124 POWER_SUPPLY_PROP_ENERGY_NOW,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700125};
126
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100127static unsigned int poll_interval = 360;
128module_param(poll_interval, uint, 0644);
129MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
130 "0 disables polling");
131
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700132/*
133 * Common code for BQ27x00 devices
134 */
135
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200136static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100137 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700138{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100139 return di->bus.read(di, reg, single);
140}
141
142/*
143 * Return the battery Relative State-of-Charge
144 * Or < 0 if something fails.
145 */
146static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
147{
148 int rsoc;
149
150 if (di->chip == BQ27500)
151 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
152 else
153 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
154
155 if (rsoc < 0)
156 dev_err(di->dev, "error reading relative State-of-Charge\n");
157
158 return rsoc;
159}
160
161/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100162 * Return a battery charge value in µAh
163 * Or < 0 if something fails.
164 */
165static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
166{
167 int charge;
168
169 charge = bq27x00_read(di, reg, false);
170 if (charge < 0) {
171 dev_err(di->dev, "error reading nominal available capacity\n");
172 return charge;
173 }
174
175 if (di->chip == BQ27500)
176 charge *= 1000;
177 else
178 charge = charge * 3570 / BQ27000_RS;
179
180 return charge;
181}
182
183/*
184 * Return the battery Nominal available capaciy in µAh
185 * Or < 0 if something fails.
186 */
187static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
188{
189 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
190}
191
192/*
193 * Return the battery Last measured discharge in µAh
194 * Or < 0 if something fails.
195 */
196static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
197{
198 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
199}
200
201/*
202 * Return the battery Initial last measured discharge in µAh
203 * Or < 0 if something fails.
204 */
205static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
206{
207 int ilmd;
208
209 if (di->chip == BQ27500)
210 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
211 else
212 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
213
214 if (ilmd < 0) {
215 dev_err(di->dev, "error reading initial last measured discharge\n");
216 return ilmd;
217 }
218
219 if (di->chip == BQ27500)
220 ilmd *= 1000;
221 else
222 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
223
224 return ilmd;
225}
226
227/*
228 * Return the battery Cycle count total
229 * Or < 0 if something fails.
230 */
231static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
232{
233 int cyct;
234
235 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
236 if (cyct < 0)
237 dev_err(di->dev, "error reading cycle count total\n");
238
239 return cyct;
240}
241
242/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100243 * Read a time register.
244 * Return < 0 if something fails.
245 */
246static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
247{
248 int tval;
249
250 tval = bq27x00_read(di, reg, false);
251 if (tval < 0) {
252 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
253 return tval;
254 }
255
256 if (tval == 65535)
257 return -ENODATA;
258
259 return tval * 60;
260}
261
262static void bq27x00_update(struct bq27x00_device_info *di)
263{
264 struct bq27x00_reg_cache cache = {0, };
265 bool is_bq27500 = di->chip == BQ27500;
266
267 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
268 if (cache.flags >= 0) {
Pali Rohár4b226c22011-11-01 01:43:05 +0100269 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
270 cache.capacity = -ENODATA;
271 cache.time_to_empty = -ENODATA;
272 cache.time_to_empty_avg = -ENODATA;
273 cache.time_to_full = -ENODATA;
274 cache.charge_full = -ENODATA;
275 } else {
276 cache.capacity = bq27x00_battery_read_rsoc(di);
277 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
278 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
279 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
280 cache.charge_full = bq27x00_battery_read_lmd(di);
281 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100282 cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
Pali Rohár73c244a2011-04-30 23:59:35 +0200283 cache.cycle_count = bq27x00_battery_read_cyct(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100284
Pali Rohár631c17e2011-02-01 00:08:02 +0100285 /* We only have to read charge design full once */
286 if (di->charge_design_full <= 0)
287 di->charge_design_full = bq27x00_battery_read_ilmd(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100288 }
289
Pali Rohárb68f6212011-11-01 01:43:03 +0100290 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100291 di->cache = cache;
292 power_supply_changed(&di->bat);
293 }
294
295 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700296}
297
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100298static void bq27x00_battery_poll(struct work_struct *work)
299{
300 struct bq27x00_device_info *di =
301 container_of(work, struct bq27x00_device_info, work.work);
302
303 bq27x00_update(di);
304
305 if (poll_interval > 0) {
306 /* The timer does not have to be accurate. */
307 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
308 schedule_delayed_work(&di->work, poll_interval * HZ);
309 }
310}
311
312
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700313/*
Grazvydas Ignotasb4de3602010-02-12 23:57:13 +0200314 * Return the battery temperature in tenths of degree Celsius
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700315 * Or < 0 if something fails.
316 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100317static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
318 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700319{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100320 if (di->cache.temperature < 0)
321 return di->cache.temperature;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700322
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200323 if (di->chip == BQ27500)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100324 val->intval = di->cache.temperature - 2731;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200325 else
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100326 val->intval = ((di->cache.temperature * 5) - 5463) / 2;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700327
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100328 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700329}
330
331/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100332 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700333 * Note that current can be negative signed as well
334 * Or 0 if something fails.
335 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100336static int bq27x00_battery_current(struct bq27x00_device_info *di,
337 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700338{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100339 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100340 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700341
Pali Rohárb68f6212011-11-01 01:43:03 +0100342 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100343 if (curr < 0)
344 return curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200345
346 if (di->chip == BQ27500) {
347 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100348 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200349 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100350 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
351 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200352 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200353 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200354 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100355
356 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700357 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200358
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100359 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700360}
361
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200362static int bq27x00_battery_status(struct bq27x00_device_info *di,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100363 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200364{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200365 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200366
367 if (di->chip == BQ27500) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100368 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200369 status = POWER_SUPPLY_STATUS_FULL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100370 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200371 status = POWER_SUPPLY_STATUS_DISCHARGING;
372 else
373 status = POWER_SUPPLY_STATUS_CHARGING;
374 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200375 if (di->cache.flags & BQ27000_FLAG_FC)
376 status = POWER_SUPPLY_STATUS_FULL;
377 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200378 status = POWER_SUPPLY_STATUS_CHARGING;
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200379 else if (power_supply_am_i_supplied(&di->bat))
380 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200381 else
382 status = POWER_SUPPLY_STATUS_DISCHARGING;
383 }
384
385 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100386
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200387 return 0;
388}
389
Pali Rohárd66bab32011-11-01 01:43:04 +0100390static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
391 union power_supply_propval *val)
392{
393 int level;
394
395 if (di->chip == BQ27500) {
396 if (di->cache.flags & BQ27500_FLAG_FC)
397 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
398 else if (di->cache.flags & BQ27500_FLAG_SOC1)
399 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
400 else if (di->cache.flags & BQ27500_FLAG_SOCF)
401 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
402 else
403 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
404 } else {
405 if (di->cache.flags & BQ27000_FLAG_FC)
406 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
407 else if (di->cache.flags & BQ27000_FLAG_EDV1)
408 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
409 else if (di->cache.flags & BQ27000_FLAG_EDVF)
410 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
411 else
412 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
413 }
414
415 val->intval = level;
416
417 return 0;
418}
419
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200420/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100421 * Return the battery Voltage in milivolts
422 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200423 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100424static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
425 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200426{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100427 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200428
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100429 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
430 if (volt < 0)
431 return volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200432
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100433 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200434
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100435 return 0;
436}
437
Pali Rohár631c17e2011-02-01 00:08:02 +0100438/*
439 * Return the battery Available energy in µWh
440 * Or < 0 if something fails.
441 */
442static int bq27x00_battery_energy(struct bq27x00_device_info *di,
443 union power_supply_propval *val)
444{
445 int ae;
446
447 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
448 if (ae < 0) {
449 dev_err(di->dev, "error reading available energy\n");
450 return ae;
451 }
452
453 if (di->chip == BQ27500)
454 ae *= 1000;
455 else
456 ae = ae * 29200 / BQ27000_RS;
457
458 val->intval = ae;
459
460 return 0;
461}
462
463
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100464static int bq27x00_simple_value(int value,
465 union power_supply_propval *val)
466{
467 if (value < 0)
468 return value;
469
470 val->intval = value;
471
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200472 return 0;
473}
474
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700475#define to_bq27x00_device_info(x) container_of((x), \
476 struct bq27x00_device_info, bat);
477
478static int bq27x00_battery_get_property(struct power_supply *psy,
479 enum power_supply_property psp,
480 union power_supply_propval *val)
481{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200482 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700483 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200484
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100485 mutex_lock(&di->lock);
486 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
487 cancel_delayed_work_sync(&di->work);
488 bq27x00_battery_poll(&di->work.work);
489 }
490 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100491
492 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200493 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700494
495 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200496 case POWER_SUPPLY_PROP_STATUS:
497 ret = bq27x00_battery_status(di, val);
498 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700499 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100500 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200501 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700502 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100503 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700504 break;
505 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100506 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700507 break;
508 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100509 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700510 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100511 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
512 ret = bq27x00_battery_capacity_level(di, val);
513 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700514 case POWER_SUPPLY_PROP_TEMP:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100515 ret = bq27x00_battery_temperature(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700516 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200517 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100518 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200519 break;
520 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100521 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200522 break;
523 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100524 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200525 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200526 case POWER_SUPPLY_PROP_TECHNOLOGY:
527 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
528 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100529 case POWER_SUPPLY_PROP_CHARGE_NOW:
530 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
531 break;
532 case POWER_SUPPLY_PROP_CHARGE_FULL:
533 ret = bq27x00_simple_value(di->cache.charge_full, val);
534 break;
535 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
536 ret = bq27x00_simple_value(di->charge_design_full, val);
537 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200538 case POWER_SUPPLY_PROP_CYCLE_COUNT:
539 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100540 break;
541 case POWER_SUPPLY_PROP_ENERGY_NOW:
542 ret = bq27x00_battery_energy(di, val);
543 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700544 default:
545 return -EINVAL;
546 }
547
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200548 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700549}
550
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100551static void bq27x00_external_power_changed(struct power_supply *psy)
552{
553 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
554
555 cancel_delayed_work_sync(&di->work);
556 schedule_delayed_work(&di->work, 0);
557}
558
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200559static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700560{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200561 int ret;
562
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700563 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
564 di->bat.properties = bq27x00_battery_props;
565 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
566 di->bat.get_property = bq27x00_battery_get_property;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100567 di->bat.external_power_changed = bq27x00_external_power_changed;
568
569 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
570 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200571
572 ret = power_supply_register(di->dev, &di->bat);
573 if (ret) {
574 dev_err(di->dev, "failed to register battery: %d\n", ret);
575 return ret;
576 }
577
578 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
579
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100580 bq27x00_update(di);
581
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200582 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700583}
584
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100585static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
586{
587 cancel_delayed_work_sync(&di->work);
588
589 power_supply_unregister(&di->bat);
590
591 mutex_destroy(&di->lock);
592}
593
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200594
595/* i2c specific code */
596#ifdef CONFIG_BATTERY_BQ27X00_I2C
597
598/* If the system has several batteries we need a different name for each
599 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700600 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200601static DEFINE_IDR(battery_id);
602static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700603
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100604static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700605{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200606 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200607 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700608 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100609 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700610
611 if (!client->adapter)
612 return -ENODEV;
613
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200614 msg[0].addr = client->addr;
615 msg[0].flags = 0;
616 msg[0].buf = &reg;
617 msg[0].len = sizeof(reg);
618 msg[1].addr = client->addr;
619 msg[1].flags = I2C_M_RD;
620 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100621 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200622 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100623 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200624 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100625
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200626 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100627 if (ret < 0)
628 return ret;
629
630 if (!single)
631 ret = get_unaligned_le16(data);
632 else
633 ret = data[0];
634
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100635 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700636}
637
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200638static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700639 const struct i2c_device_id *id)
640{
641 char *name;
642 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700643 int num;
644 int retval = 0;
645
646 /* Get new ID for the new battery device */
647 retval = idr_pre_get(&battery_id, GFP_KERNEL);
648 if (retval == 0)
649 return -ENOMEM;
650 mutex_lock(&battery_mutex);
651 retval = idr_get_new(&battery_id, client, &num);
652 mutex_unlock(&battery_mutex);
653 if (retval < 0)
654 return retval;
655
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200656 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700657 if (!name) {
658 dev_err(&client->dev, "failed to allocate device name\n");
659 retval = -ENOMEM;
660 goto batt_failed_1;
661 }
662
663 di = kzalloc(sizeof(*di), GFP_KERNEL);
664 if (!di) {
665 dev_err(&client->dev, "failed to allocate device info data\n");
666 retval = -ENOMEM;
667 goto batt_failed_2;
668 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700669
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200670 di->id = num;
671 di->dev = &client->dev;
672 di->chip = id->driver_data;
673 di->bat.name = name;
674 di->bus.read = &bq27x00_read_i2c;
675
676 if (bq27x00_powersupply_init(di))
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700677 goto batt_failed_3;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700678
679 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700680
681 return 0;
682
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700683batt_failed_3:
684 kfree(di);
685batt_failed_2:
686 kfree(name);
687batt_failed_1:
688 mutex_lock(&battery_mutex);
689 idr_remove(&battery_id, num);
690 mutex_unlock(&battery_mutex);
691
692 return retval;
693}
694
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200695static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700696{
697 struct bq27x00_device_info *di = i2c_get_clientdata(client);
698
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100699 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700700
701 kfree(di->bat.name);
702
703 mutex_lock(&battery_mutex);
704 idr_remove(&battery_id, di->id);
705 mutex_unlock(&battery_mutex);
706
707 kfree(di);
708
709 return 0;
710}
711
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200712static const struct i2c_device_id bq27x00_id[] = {
713 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
714 { "bq27500", BQ27500 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700715 {},
716};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100717MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700718
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200719static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700720 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200721 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700722 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200723 .probe = bq27x00_battery_probe,
724 .remove = bq27x00_battery_remove,
725 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700726};
727
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200728static inline int bq27x00_battery_i2c_init(void)
729{
730 int ret = i2c_add_driver(&bq27x00_battery_driver);
731 if (ret)
732 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
733
734 return ret;
735}
736
737static inline void bq27x00_battery_i2c_exit(void)
738{
739 i2c_del_driver(&bq27x00_battery_driver);
740}
741
742#else
743
744static inline int bq27x00_battery_i2c_init(void) { return 0; }
745static inline void bq27x00_battery_i2c_exit(void) {};
746
747#endif
748
749/* platform specific code */
750#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
751
752static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100753 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200754{
755 struct device *dev = di->dev;
756 struct bq27000_platform_data *pdata = dev->platform_data;
757 unsigned int timeout = 3;
758 int upper, lower;
759 int temp;
760
761 if (!single) {
762 /* Make sure the value has not changed in between reading the
763 * lower and the upper part */
764 upper = pdata->read(dev, reg + 1);
765 do {
766 temp = upper;
767 if (upper < 0)
768 return upper;
769
770 lower = pdata->read(dev, reg);
771 if (lower < 0)
772 return lower;
773
774 upper = pdata->read(dev, reg + 1);
775 } while (temp != upper && --timeout);
776
777 if (timeout == 0)
778 return -EIO;
779
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100780 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200781 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100782
783 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200784}
785
786static int __devinit bq27000_battery_probe(struct platform_device *pdev)
787{
788 struct bq27x00_device_info *di;
789 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
790 int ret;
791
792 if (!pdata) {
793 dev_err(&pdev->dev, "no platform_data supplied\n");
794 return -EINVAL;
795 }
796
797 if (!pdata->read) {
798 dev_err(&pdev->dev, "no hdq read callback supplied\n");
799 return -EINVAL;
800 }
801
802 di = kzalloc(sizeof(*di), GFP_KERNEL);
803 if (!di) {
804 dev_err(&pdev->dev, "failed to allocate device info data\n");
805 return -ENOMEM;
806 }
807
808 platform_set_drvdata(pdev, di);
809
810 di->dev = &pdev->dev;
811 di->chip = BQ27000;
812
813 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
814 di->bus.read = &bq27000_read_platform;
815
816 ret = bq27x00_powersupply_init(di);
817 if (ret)
818 goto err_free;
819
820 return 0;
821
822err_free:
823 platform_set_drvdata(pdev, NULL);
824 kfree(di);
825
826 return ret;
827}
828
829static int __devexit bq27000_battery_remove(struct platform_device *pdev)
830{
831 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
832
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100833 bq27x00_powersupply_unregister(di);
834
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200835 platform_set_drvdata(pdev, NULL);
836 kfree(di);
837
838 return 0;
839}
840
841static struct platform_driver bq27000_battery_driver = {
842 .probe = bq27000_battery_probe,
843 .remove = __devexit_p(bq27000_battery_remove),
844 .driver = {
845 .name = "bq27000-battery",
846 .owner = THIS_MODULE,
847 },
848};
849
850static inline int bq27x00_battery_platform_init(void)
851{
852 int ret = platform_driver_register(&bq27000_battery_driver);
853 if (ret)
854 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
855
856 return ret;
857}
858
859static inline void bq27x00_battery_platform_exit(void)
860{
861 platform_driver_unregister(&bq27000_battery_driver);
862}
863
864#else
865
866static inline int bq27x00_battery_platform_init(void) { return 0; }
867static inline void bq27x00_battery_platform_exit(void) {};
868
869#endif
870
871/*
872 * Module stuff
873 */
874
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700875static int __init bq27x00_battery_init(void)
876{
877 int ret;
878
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200879 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700880 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200881 return ret;
882
883 ret = bq27x00_battery_platform_init();
884 if (ret)
885 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700886
887 return ret;
888}
889module_init(bq27x00_battery_init);
890
891static void __exit bq27x00_battery_exit(void)
892{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200893 bq27x00_battery_platform_exit();
894 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700895}
896module_exit(bq27x00_battery_exit);
897
898MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
899MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
900MODULE_LICENSE("GPL");