blob: a5311225b964fd90686c6d534f3d8bd56979187d [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 */
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +020059#define BQ27000_FLAG_FC BIT(5)
Pali Rohárd66bab32011-11-01 01:43:04 +010060#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020061
Pali Rohárbf7d4142011-02-01 00:23:13 +010062#define BQ27500_REG_SOC 0x2C
Pali Rohár631c17e2011-02-01 00:08:02 +010063#define BQ27500_REG_DCAP 0x3C /* Design capacity */
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020064#define BQ27500_FLAG_DSC BIT(0)
Pali Rohárd66bab32011-11-01 01:43:04 +010065#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
66#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020067#define BQ27500_FLAG_FC BIT(9)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020068
Pali Rohára2e51182010-05-24 20:52:13 +020069#define BQ27000_RS 20 /* Resistor sense */
70
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070071struct bq27x00_device_info;
72struct bq27x00_access_methods {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010073 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070074};
75
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020076enum bq27x00_chip { BQ27000, BQ27500 };
77
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010078struct bq27x00_reg_cache {
79 int temperature;
80 int time_to_empty;
81 int time_to_empty_avg;
82 int time_to_full;
Pali Rohár631c17e2011-02-01 00:08:02 +010083 int charge_full;
Pali Rohár73c244a2011-04-30 23:59:35 +020084 int cycle_count;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010085 int capacity;
86 int flags;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010087};
88
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070089struct bq27x00_device_info {
90 struct device *dev;
91 int id;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020092 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070093
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010094 struct bq27x00_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +010095 int charge_design_full;
96
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010097 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +010098 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +010099
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200100 struct power_supply bat;
101
102 struct bq27x00_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100103
104 struct mutex lock;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700105};
106
107static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200108 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700109 POWER_SUPPLY_PROP_PRESENT,
110 POWER_SUPPLY_PROP_VOLTAGE_NOW,
111 POWER_SUPPLY_PROP_CURRENT_NOW,
112 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100113 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700114 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200115 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
116 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
117 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200118 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100119 POWER_SUPPLY_PROP_CHARGE_FULL,
120 POWER_SUPPLY_PROP_CHARGE_NOW,
121 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200122 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100123 POWER_SUPPLY_PROP_ENERGY_NOW,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700124};
125
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100126static unsigned int poll_interval = 360;
127module_param(poll_interval, uint, 0644);
128MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
129 "0 disables polling");
130
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700131/*
132 * Common code for BQ27x00 devices
133 */
134
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200135static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100136 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700137{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100138 return di->bus.read(di, reg, single);
139}
140
141/*
142 * Return the battery Relative State-of-Charge
143 * Or < 0 if something fails.
144 */
145static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
146{
147 int rsoc;
148
149 if (di->chip == BQ27500)
150 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
151 else
152 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
153
154 if (rsoc < 0)
155 dev_err(di->dev, "error reading relative State-of-Charge\n");
156
157 return rsoc;
158}
159
160/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100161 * Return a battery charge value in µAh
162 * Or < 0 if something fails.
163 */
164static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
165{
166 int charge;
167
168 charge = bq27x00_read(di, reg, false);
169 if (charge < 0) {
170 dev_err(di->dev, "error reading nominal available capacity\n");
171 return charge;
172 }
173
174 if (di->chip == BQ27500)
175 charge *= 1000;
176 else
177 charge = charge * 3570 / BQ27000_RS;
178
179 return charge;
180}
181
182/*
183 * Return the battery Nominal available capaciy in µAh
184 * Or < 0 if something fails.
185 */
186static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
187{
188 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
189}
190
191/*
192 * Return the battery Last measured discharge in µAh
193 * Or < 0 if something fails.
194 */
195static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
196{
197 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
198}
199
200/*
201 * Return the battery Initial last measured discharge in µAh
202 * Or < 0 if something fails.
203 */
204static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
205{
206 int ilmd;
207
208 if (di->chip == BQ27500)
209 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
210 else
211 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
212
213 if (ilmd < 0) {
214 dev_err(di->dev, "error reading initial last measured discharge\n");
215 return ilmd;
216 }
217
218 if (di->chip == BQ27500)
219 ilmd *= 1000;
220 else
221 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
222
223 return ilmd;
224}
225
226/*
227 * Return the battery Cycle count total
228 * Or < 0 if something fails.
229 */
230static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
231{
232 int cyct;
233
234 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
235 if (cyct < 0)
236 dev_err(di->dev, "error reading cycle count total\n");
237
238 return cyct;
239}
240
241/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100242 * Read a time register.
243 * Return < 0 if something fails.
244 */
245static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
246{
247 int tval;
248
249 tval = bq27x00_read(di, reg, false);
250 if (tval < 0) {
251 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
252 return tval;
253 }
254
255 if (tval == 65535)
256 return -ENODATA;
257
258 return tval * 60;
259}
260
261static void bq27x00_update(struct bq27x00_device_info *di)
262{
263 struct bq27x00_reg_cache cache = {0, };
264 bool is_bq27500 = di->chip == BQ27500;
265
266 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
267 if (cache.flags >= 0) {
268 cache.capacity = bq27x00_battery_read_rsoc(di);
269 cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
270 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
271 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
272 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
Pali Rohár631c17e2011-02-01 00:08:02 +0100273 cache.charge_full = bq27x00_battery_read_lmd(di);
Pali Rohár73c244a2011-04-30 23:59:35 +0200274 cache.cycle_count = bq27x00_battery_read_cyct(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100275
Pali Rohár631c17e2011-02-01 00:08:02 +0100276 /* We only have to read charge design full once */
277 if (di->charge_design_full <= 0)
278 di->charge_design_full = bq27x00_battery_read_ilmd(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100279 }
280
Pali Rohárb68f6212011-11-01 01:43:03 +0100281 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100282 di->cache = cache;
283 power_supply_changed(&di->bat);
284 }
285
286 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700287}
288
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100289static void bq27x00_battery_poll(struct work_struct *work)
290{
291 struct bq27x00_device_info *di =
292 container_of(work, struct bq27x00_device_info, work.work);
293
294 bq27x00_update(di);
295
296 if (poll_interval > 0) {
297 /* The timer does not have to be accurate. */
298 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
299 schedule_delayed_work(&di->work, poll_interval * HZ);
300 }
301}
302
303
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700304/*
Grazvydas Ignotasb4de3602010-02-12 23:57:13 +0200305 * Return the battery temperature in tenths of degree Celsius
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700306 * Or < 0 if something fails.
307 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100308static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
309 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700310{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100311 if (di->cache.temperature < 0)
312 return di->cache.temperature;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700313
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200314 if (di->chip == BQ27500)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100315 val->intval = di->cache.temperature - 2731;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200316 else
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100317 val->intval = ((di->cache.temperature * 5) - 5463) / 2;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700318
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100319 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700320}
321
322/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100323 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700324 * Note that current can be negative signed as well
325 * Or 0 if something fails.
326 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100327static int bq27x00_battery_current(struct bq27x00_device_info *di,
328 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700329{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100330 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100331 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700332
Pali Rohárb68f6212011-11-01 01:43:03 +0100333 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100334 if (curr < 0)
335 return curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200336
337 if (di->chip == BQ27500) {
338 /* bq27500 returns signed value */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100339 val->intval = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200340 } else {
Pali Rohárb68f6212011-11-01 01:43:03 +0100341 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
342 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200343 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200344 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200345 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100346
347 val->intval = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700348 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200349
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100350 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700351}
352
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200353static int bq27x00_battery_status(struct bq27x00_device_info *di,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100354 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200355{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200356 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200357
358 if (di->chip == BQ27500) {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100359 if (di->cache.flags & BQ27500_FLAG_FC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200360 status = POWER_SUPPLY_STATUS_FULL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100361 else if (di->cache.flags & BQ27500_FLAG_DSC)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200362 status = POWER_SUPPLY_STATUS_DISCHARGING;
363 else
364 status = POWER_SUPPLY_STATUS_CHARGING;
365 } else {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200366 if (di->cache.flags & BQ27000_FLAG_FC)
367 status = POWER_SUPPLY_STATUS_FULL;
368 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200369 status = POWER_SUPPLY_STATUS_CHARGING;
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200370 else if (power_supply_am_i_supplied(&di->bat))
371 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200372 else
373 status = POWER_SUPPLY_STATUS_DISCHARGING;
374 }
375
376 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100377
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200378 return 0;
379}
380
Pali Rohárd66bab32011-11-01 01:43:04 +0100381static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
382 union power_supply_propval *val)
383{
384 int level;
385
386 if (di->chip == BQ27500) {
387 if (di->cache.flags & BQ27500_FLAG_FC)
388 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
389 else if (di->cache.flags & BQ27500_FLAG_SOC1)
390 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
391 else if (di->cache.flags & BQ27500_FLAG_SOCF)
392 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
393 else
394 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
395 } else {
396 if (di->cache.flags & BQ27000_FLAG_FC)
397 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
398 else if (di->cache.flags & BQ27000_FLAG_EDV1)
399 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
400 else if (di->cache.flags & BQ27000_FLAG_EDVF)
401 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
402 else
403 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
404 }
405
406 val->intval = level;
407
408 return 0;
409}
410
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200411/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100412 * Return the battery Voltage in milivolts
413 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200414 */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100415static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
416 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200417{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100418 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200419
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100420 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
421 if (volt < 0)
422 return volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200423
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100424 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200425
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100426 return 0;
427}
428
Pali Rohár631c17e2011-02-01 00:08:02 +0100429/*
430 * Return the battery Available energy in µWh
431 * Or < 0 if something fails.
432 */
433static int bq27x00_battery_energy(struct bq27x00_device_info *di,
434 union power_supply_propval *val)
435{
436 int ae;
437
438 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
439 if (ae < 0) {
440 dev_err(di->dev, "error reading available energy\n");
441 return ae;
442 }
443
444 if (di->chip == BQ27500)
445 ae *= 1000;
446 else
447 ae = ae * 29200 / BQ27000_RS;
448
449 val->intval = ae;
450
451 return 0;
452}
453
454
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100455static int bq27x00_simple_value(int value,
456 union power_supply_propval *val)
457{
458 if (value < 0)
459 return value;
460
461 val->intval = value;
462
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200463 return 0;
464}
465
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700466#define to_bq27x00_device_info(x) container_of((x), \
467 struct bq27x00_device_info, bat);
468
469static int bq27x00_battery_get_property(struct power_supply *psy,
470 enum power_supply_property psp,
471 union power_supply_propval *val)
472{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200473 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700474 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200475
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100476 mutex_lock(&di->lock);
477 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
478 cancel_delayed_work_sync(&di->work);
479 bq27x00_battery_poll(&di->work.work);
480 }
481 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100482
483 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200484 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700485
486 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200487 case POWER_SUPPLY_PROP_STATUS:
488 ret = bq27x00_battery_status(di, val);
489 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700490 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100491 ret = bq27x00_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200492 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700493 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100494 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700495 break;
496 case POWER_SUPPLY_PROP_CURRENT_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100497 ret = bq27x00_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700498 break;
499 case POWER_SUPPLY_PROP_CAPACITY:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100500 ret = bq27x00_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700501 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100502 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
503 ret = bq27x00_battery_capacity_level(di, val);
504 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700505 case POWER_SUPPLY_PROP_TEMP:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100506 ret = bq27x00_battery_temperature(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700507 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200508 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100509 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200510 break;
511 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100512 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200513 break;
514 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100515 ret = bq27x00_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200516 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200517 case POWER_SUPPLY_PROP_TECHNOLOGY:
518 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
519 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100520 case POWER_SUPPLY_PROP_CHARGE_NOW:
521 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
522 break;
523 case POWER_SUPPLY_PROP_CHARGE_FULL:
524 ret = bq27x00_simple_value(di->cache.charge_full, val);
525 break;
526 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
527 ret = bq27x00_simple_value(di->charge_design_full, val);
528 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200529 case POWER_SUPPLY_PROP_CYCLE_COUNT:
530 ret = bq27x00_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100531 break;
532 case POWER_SUPPLY_PROP_ENERGY_NOW:
533 ret = bq27x00_battery_energy(di, val);
534 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700535 default:
536 return -EINVAL;
537 }
538
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200539 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700540}
541
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100542static void bq27x00_external_power_changed(struct power_supply *psy)
543{
544 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
545
546 cancel_delayed_work_sync(&di->work);
547 schedule_delayed_work(&di->work, 0);
548}
549
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200550static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700551{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200552 int ret;
553
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700554 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
555 di->bat.properties = bq27x00_battery_props;
556 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
557 di->bat.get_property = bq27x00_battery_get_property;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100558 di->bat.external_power_changed = bq27x00_external_power_changed;
559
560 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
561 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200562
563 ret = power_supply_register(di->dev, &di->bat);
564 if (ret) {
565 dev_err(di->dev, "failed to register battery: %d\n", ret);
566 return ret;
567 }
568
569 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
570
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100571 bq27x00_update(di);
572
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200573 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700574}
575
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100576static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
577{
578 cancel_delayed_work_sync(&di->work);
579
580 power_supply_unregister(&di->bat);
581
582 mutex_destroy(&di->lock);
583}
584
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200585
586/* i2c specific code */
587#ifdef CONFIG_BATTERY_BQ27X00_I2C
588
589/* If the system has several batteries we need a different name for each
590 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700591 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200592static DEFINE_IDR(battery_id);
593static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700594
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100595static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700596{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200597 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200598 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700599 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100600 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700601
602 if (!client->adapter)
603 return -ENODEV;
604
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200605 msg[0].addr = client->addr;
606 msg[0].flags = 0;
607 msg[0].buf = &reg;
608 msg[0].len = sizeof(reg);
609 msg[1].addr = client->addr;
610 msg[1].flags = I2C_M_RD;
611 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100612 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200613 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100614 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200615 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100616
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +0200617 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +0100618 if (ret < 0)
619 return ret;
620
621 if (!single)
622 ret = get_unaligned_le16(data);
623 else
624 ret = data[0];
625
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100626 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700627}
628
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200629static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700630 const struct i2c_device_id *id)
631{
632 char *name;
633 struct bq27x00_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700634 int num;
635 int retval = 0;
636
637 /* Get new ID for the new battery device */
638 retval = idr_pre_get(&battery_id, GFP_KERNEL);
639 if (retval == 0)
640 return -ENOMEM;
641 mutex_lock(&battery_mutex);
642 retval = idr_get_new(&battery_id, client, &num);
643 mutex_unlock(&battery_mutex);
644 if (retval < 0)
645 return retval;
646
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200647 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700648 if (!name) {
649 dev_err(&client->dev, "failed to allocate device name\n");
650 retval = -ENOMEM;
651 goto batt_failed_1;
652 }
653
654 di = kzalloc(sizeof(*di), GFP_KERNEL);
655 if (!di) {
656 dev_err(&client->dev, "failed to allocate device info data\n");
657 retval = -ENOMEM;
658 goto batt_failed_2;
659 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700660
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200661 di->id = num;
662 di->dev = &client->dev;
663 di->chip = id->driver_data;
664 di->bat.name = name;
665 di->bus.read = &bq27x00_read_i2c;
666
667 if (bq27x00_powersupply_init(di))
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700668 goto batt_failed_3;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700669
670 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700671
672 return 0;
673
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700674batt_failed_3:
675 kfree(di);
676batt_failed_2:
677 kfree(name);
678batt_failed_1:
679 mutex_lock(&battery_mutex);
680 idr_remove(&battery_id, num);
681 mutex_unlock(&battery_mutex);
682
683 return retval;
684}
685
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200686static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700687{
688 struct bq27x00_device_info *di = i2c_get_clientdata(client);
689
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100690 bq27x00_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700691
692 kfree(di->bat.name);
693
694 mutex_lock(&battery_mutex);
695 idr_remove(&battery_id, di->id);
696 mutex_unlock(&battery_mutex);
697
698 kfree(di);
699
700 return 0;
701}
702
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200703static const struct i2c_device_id bq27x00_id[] = {
704 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
705 { "bq27500", BQ27500 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700706 {},
707};
Pali Rohárfd9b9582011-02-01 00:10:41 +0100708MODULE_DEVICE_TABLE(i2c, bq27x00_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700709
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200710static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700711 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200712 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700713 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200714 .probe = bq27x00_battery_probe,
715 .remove = bq27x00_battery_remove,
716 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700717};
718
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200719static inline int bq27x00_battery_i2c_init(void)
720{
721 int ret = i2c_add_driver(&bq27x00_battery_driver);
722 if (ret)
723 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
724
725 return ret;
726}
727
728static inline void bq27x00_battery_i2c_exit(void)
729{
730 i2c_del_driver(&bq27x00_battery_driver);
731}
732
733#else
734
735static inline int bq27x00_battery_i2c_init(void) { return 0; }
736static inline void bq27x00_battery_i2c_exit(void) {};
737
738#endif
739
740/* platform specific code */
741#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
742
743static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100744 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200745{
746 struct device *dev = di->dev;
747 struct bq27000_platform_data *pdata = dev->platform_data;
748 unsigned int timeout = 3;
749 int upper, lower;
750 int temp;
751
752 if (!single) {
753 /* Make sure the value has not changed in between reading the
754 * lower and the upper part */
755 upper = pdata->read(dev, reg + 1);
756 do {
757 temp = upper;
758 if (upper < 0)
759 return upper;
760
761 lower = pdata->read(dev, reg);
762 if (lower < 0)
763 return lower;
764
765 upper = pdata->read(dev, reg + 1);
766 } while (temp != upper && --timeout);
767
768 if (timeout == 0)
769 return -EIO;
770
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100771 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200772 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100773
774 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200775}
776
777static int __devinit bq27000_battery_probe(struct platform_device *pdev)
778{
779 struct bq27x00_device_info *di;
780 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
781 int ret;
782
783 if (!pdata) {
784 dev_err(&pdev->dev, "no platform_data supplied\n");
785 return -EINVAL;
786 }
787
788 if (!pdata->read) {
789 dev_err(&pdev->dev, "no hdq read callback supplied\n");
790 return -EINVAL;
791 }
792
793 di = kzalloc(sizeof(*di), GFP_KERNEL);
794 if (!di) {
795 dev_err(&pdev->dev, "failed to allocate device info data\n");
796 return -ENOMEM;
797 }
798
799 platform_set_drvdata(pdev, di);
800
801 di->dev = &pdev->dev;
802 di->chip = BQ27000;
803
804 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
805 di->bus.read = &bq27000_read_platform;
806
807 ret = bq27x00_powersupply_init(di);
808 if (ret)
809 goto err_free;
810
811 return 0;
812
813err_free:
814 platform_set_drvdata(pdev, NULL);
815 kfree(di);
816
817 return ret;
818}
819
820static int __devexit bq27000_battery_remove(struct platform_device *pdev)
821{
822 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
823
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100824 bq27x00_powersupply_unregister(di);
825
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200826 platform_set_drvdata(pdev, NULL);
827 kfree(di);
828
829 return 0;
830}
831
832static struct platform_driver bq27000_battery_driver = {
833 .probe = bq27000_battery_probe,
834 .remove = __devexit_p(bq27000_battery_remove),
835 .driver = {
836 .name = "bq27000-battery",
837 .owner = THIS_MODULE,
838 },
839};
840
841static inline int bq27x00_battery_platform_init(void)
842{
843 int ret = platform_driver_register(&bq27000_battery_driver);
844 if (ret)
845 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
846
847 return ret;
848}
849
850static inline void bq27x00_battery_platform_exit(void)
851{
852 platform_driver_unregister(&bq27000_battery_driver);
853}
854
855#else
856
857static inline int bq27x00_battery_platform_init(void) { return 0; }
858static inline void bq27x00_battery_platform_exit(void) {};
859
860#endif
861
862/*
863 * Module stuff
864 */
865
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700866static int __init bq27x00_battery_init(void)
867{
868 int ret;
869
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200870 ret = bq27x00_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700871 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200872 return ret;
873
874 ret = bq27x00_battery_platform_init();
875 if (ret)
876 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700877
878 return ret;
879}
880module_init(bq27x00_battery_init);
881
882static void __exit bq27x00_battery_exit(void)
883{
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +0200884 bq27x00_battery_platform_exit();
885 bq27x00_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700886}
887module_exit(bq27x00_battery_exit);
888
889MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
890MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
891MODULE_LICENSE("GPL");