blob: 1b06134e0ae883b88852af2719da59996024e39b [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>
6 *
7 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
8 *
9 * This package is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 */
18#include <linux/module.h>
19#include <linux/param.h>
20#include <linux/jiffies.h>
21#include <linux/workqueue.h>
22#include <linux/delay.h>
23#include <linux/platform_device.h>
24#include <linux/power_supply.h>
25#include <linux/idr.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070026#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Harvey Harrison8aef7e82008-09-22 14:53:50 -070028#include <asm/unaligned.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070029
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020030#define DRIVER_VERSION "1.1.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070031
32#define BQ27x00_REG_TEMP 0x06
33#define BQ27x00_REG_VOLT 0x08
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070034#define BQ27x00_REG_AI 0x14
35#define BQ27x00_REG_FLAGS 0x0A
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020036#define BQ27x00_REG_TTE 0x16
37#define BQ27x00_REG_TTF 0x18
38#define BQ27x00_REG_TTECP 0x26
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070039
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020040#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020041#define BQ27000_FLAG_CHGS BIT(7)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020042
43#define BQ27500_REG_SOC 0x2c
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020044#define BQ27500_FLAG_DSC BIT(0)
45#define BQ27500_FLAG_FC BIT(9)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020046
Pali Rohára2e51182010-05-24 20:52:13 +020047#define BQ27000_RS 20 /* Resistor sense */
48
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070049/* If the system has several batteries we need a different name for each
50 * of them...
51 */
52static DEFINE_IDR(battery_id);
53static DEFINE_MUTEX(battery_mutex);
54
55struct bq27x00_device_info;
56struct bq27x00_access_methods {
57 int (*read)(u8 reg, int *rt_value, int b_single,
58 struct bq27x00_device_info *di);
59};
60
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020061enum bq27x00_chip { BQ27000, BQ27500 };
62
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070063struct bq27x00_device_info {
64 struct device *dev;
65 int id;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070066 struct bq27x00_access_methods *bus;
67 struct power_supply bat;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020068 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070069
70 struct i2c_client *client;
71};
72
73static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020074 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070075 POWER_SUPPLY_PROP_PRESENT,
76 POWER_SUPPLY_PROP_VOLTAGE_NOW,
77 POWER_SUPPLY_PROP_CURRENT_NOW,
78 POWER_SUPPLY_PROP_CAPACITY,
79 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020080 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
81 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
82 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +020083 POWER_SUPPLY_PROP_TECHNOLOGY,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070084};
85
86/*
87 * Common code for BQ27x00 devices
88 */
89
90static int bq27x00_read(u8 reg, int *rt_value, int b_single,
91 struct bq27x00_device_info *di)
92{
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +020093 return di->bus->read(reg, rt_value, b_single, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070094}
95
96/*
Grazvydas Ignotasb4de3602010-02-12 23:57:13 +020097 * Return the battery temperature in tenths of degree Celsius
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070098 * Or < 0 if something fails.
99 */
100static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
101{
102 int ret;
103 int temp = 0;
104
105 ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
106 if (ret) {
107 dev_err(di->dev, "error reading temperature\n");
108 return ret;
109 }
110
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200111 if (di->chip == BQ27500)
112 return temp - 2731;
113 else
Lars-Peter Clausen0e9f3042010-05-24 20:20:57 +0200114 return ((temp * 5) - 5463) / 2;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700115}
116
117/*
118 * Return the battery Voltage in milivolts
119 * Or < 0 if something fails.
120 */
121static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
122{
123 int ret;
124 int volt = 0;
125
126 ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
127 if (ret) {
128 dev_err(di->dev, "error reading voltage\n");
129 return ret;
130 }
131
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200132 return volt * 1000;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700133}
134
135/*
136 * Return the battery average current
137 * Note that current can be negative signed as well
138 * Or 0 if something fails.
139 */
140static int bq27x00_battery_current(struct bq27x00_device_info *di)
141{
142 int ret;
143 int curr = 0;
144 int flags = 0;
145
146 ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
147 if (ret) {
148 dev_err(di->dev, "error reading current\n");
149 return 0;
150 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200151
152 if (di->chip == BQ27500) {
153 /* bq27500 returns signed value */
Pali Rohára2e51182010-05-24 20:52:13 +0200154 curr = (int)((s16)curr) * 1000;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200155 } else {
156 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
157 if (ret < 0) {
158 dev_err(di->dev, "error reading flags\n");
159 return 0;
160 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200161 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200162 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200163 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200164 }
Pali Rohára2e51182010-05-24 20:52:13 +0200165 curr = curr * 3570 / BQ27000_RS;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700166 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200167
Pali Rohára2e51182010-05-24 20:52:13 +0200168 return curr;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700169}
170
171/*
172 * Return the battery Relative State-of-Charge
173 * Or < 0 if something fails.
174 */
175static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
176{
177 int ret;
178 int rsoc = 0;
179
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200180 if (di->chip == BQ27500)
181 ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
182 else
183 ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700184 if (ret) {
185 dev_err(di->dev, "error reading relative State-of-Charge\n");
186 return ret;
187 }
188
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +0200189 return rsoc;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700190}
191
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200192static int bq27x00_battery_status(struct bq27x00_device_info *di,
193 union power_supply_propval *val)
194{
195 int flags = 0;
196 int status;
197 int ret;
198
199 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
200 if (ret < 0) {
201 dev_err(di->dev, "error reading flags\n");
202 return ret;
203 }
204
205 if (di->chip == BQ27500) {
206 if (flags & BQ27500_FLAG_FC)
207 status = POWER_SUPPLY_STATUS_FULL;
208 else if (flags & BQ27500_FLAG_DSC)
209 status = POWER_SUPPLY_STATUS_DISCHARGING;
210 else
211 status = POWER_SUPPLY_STATUS_CHARGING;
212 } else {
213 if (flags & BQ27000_FLAG_CHGS)
214 status = POWER_SUPPLY_STATUS_CHARGING;
215 else
216 status = POWER_SUPPLY_STATUS_DISCHARGING;
217 }
218
219 val->intval = status;
220 return 0;
221}
222
223/*
224 * Read a time register.
225 * Return < 0 if something fails.
226 */
227static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
228 union power_supply_propval *val)
229{
230 int tval = 0;
231 int ret;
232
233 ret = bq27x00_read(reg, &tval, 0, di);
234 if (ret) {
235 dev_err(di->dev, "error reading register %02x\n", reg);
236 return ret;
237 }
238
239 if (tval == 65535)
240 return -ENODATA;
241
242 val->intval = tval * 60;
243 return 0;
244}
245
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700246#define to_bq27x00_device_info(x) container_of((x), \
247 struct bq27x00_device_info, bat);
248
249static int bq27x00_battery_get_property(struct power_supply *psy,
250 enum power_supply_property psp,
251 union power_supply_propval *val)
252{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200253 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700254 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
255
256 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200257 case POWER_SUPPLY_PROP_STATUS:
258 ret = bq27x00_battery_status(di, val);
259 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700260 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
261 case POWER_SUPPLY_PROP_PRESENT:
262 val->intval = bq27x00_battery_voltage(di);
263 if (psp == POWER_SUPPLY_PROP_PRESENT)
264 val->intval = val->intval <= 0 ? 0 : 1;
265 break;
266 case POWER_SUPPLY_PROP_CURRENT_NOW:
267 val->intval = bq27x00_battery_current(di);
268 break;
269 case POWER_SUPPLY_PROP_CAPACITY:
270 val->intval = bq27x00_battery_rsoc(di);
271 break;
272 case POWER_SUPPLY_PROP_TEMP:
273 val->intval = bq27x00_battery_temperature(di);
274 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200275 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
276 ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
277 break;
278 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
279 ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
280 break;
281 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
282 ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
283 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200284 case POWER_SUPPLY_PROP_TECHNOLOGY:
285 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
286 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700287 default:
288 return -EINVAL;
289 }
290
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200291 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700292}
293
294static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
295{
296 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
297 di->bat.properties = bq27x00_battery_props;
298 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
299 di->bat.get_property = bq27x00_battery_get_property;
300 di->bat.external_power_changed = NULL;
301}
302
303/*
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200304 * i2c specific code
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700305 */
306
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200307static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700308 struct bq27x00_device_info *di)
309{
310 struct i2c_client *client = di->client;
311 struct i2c_msg msg[1];
312 unsigned char data[2];
313 int err;
314
315 if (!client->adapter)
316 return -ENODEV;
317
318 msg->addr = client->addr;
319 msg->flags = 0;
320 msg->len = 1;
321 msg->buf = data;
322
323 data[0] = reg;
324 err = i2c_transfer(client->adapter, msg, 1);
325
326 if (err >= 0) {
327 if (!b_single)
328 msg->len = 2;
329 else
330 msg->len = 1;
331
332 msg->flags = I2C_M_RD;
333 err = i2c_transfer(client->adapter, msg, 1);
334 if (err >= 0) {
335 if (!b_single)
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +0200336 *rt_value = get_unaligned_le16(data);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700337 else
338 *rt_value = data[0];
339
340 return 0;
341 }
342 }
343 return err;
344}
345
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200346static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700347 const struct i2c_device_id *id)
348{
349 char *name;
350 struct bq27x00_device_info *di;
351 struct bq27x00_access_methods *bus;
352 int num;
353 int retval = 0;
354
355 /* Get new ID for the new battery device */
356 retval = idr_pre_get(&battery_id, GFP_KERNEL);
357 if (retval == 0)
358 return -ENOMEM;
359 mutex_lock(&battery_mutex);
360 retval = idr_get_new(&battery_id, client, &num);
361 mutex_unlock(&battery_mutex);
362 if (retval < 0)
363 return retval;
364
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200365 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700366 if (!name) {
367 dev_err(&client->dev, "failed to allocate device name\n");
368 retval = -ENOMEM;
369 goto batt_failed_1;
370 }
371
372 di = kzalloc(sizeof(*di), GFP_KERNEL);
373 if (!di) {
374 dev_err(&client->dev, "failed to allocate device info data\n");
375 retval = -ENOMEM;
376 goto batt_failed_2;
377 }
378 di->id = num;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200379 di->chip = id->driver_data;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700380
381 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
382 if (!bus) {
383 dev_err(&client->dev, "failed to allocate access method "
384 "data\n");
385 retval = -ENOMEM;
386 goto batt_failed_3;
387 }
388
389 i2c_set_clientdata(client, di);
390 di->dev = &client->dev;
391 di->bat.name = name;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200392 bus->read = &bq27x00_read_i2c;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700393 di->bus = bus;
394 di->client = client;
395
396 bq27x00_powersupply_init(di);
397
398 retval = power_supply_register(&client->dev, &di->bat);
399 if (retval) {
400 dev_err(&client->dev, "failed to register battery\n");
401 goto batt_failed_4;
402 }
403
404 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
405
406 return 0;
407
408batt_failed_4:
409 kfree(bus);
410batt_failed_3:
411 kfree(di);
412batt_failed_2:
413 kfree(name);
414batt_failed_1:
415 mutex_lock(&battery_mutex);
416 idr_remove(&battery_id, num);
417 mutex_unlock(&battery_mutex);
418
419 return retval;
420}
421
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200422static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700423{
424 struct bq27x00_device_info *di = i2c_get_clientdata(client);
425
426 power_supply_unregister(&di->bat);
427
Axel Lin92925852010-10-05 10:05:34 +0800428 kfree(di->bus);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700429 kfree(di->bat.name);
430
431 mutex_lock(&battery_mutex);
432 idr_remove(&battery_id, di->id);
433 mutex_unlock(&battery_mutex);
434
435 kfree(di);
436
437 return 0;
438}
439
440/*
441 * Module stuff
442 */
443
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200444static const struct i2c_device_id bq27x00_id[] = {
445 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
446 { "bq27500", BQ27500 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700447 {},
448};
449
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200450static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700451 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200452 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700453 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200454 .probe = bq27x00_battery_probe,
455 .remove = bq27x00_battery_remove,
456 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700457};
458
459static int __init bq27x00_battery_init(void)
460{
461 int ret;
462
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200463 ret = i2c_add_driver(&bq27x00_battery_driver);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700464 if (ret)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200465 printk(KERN_ERR "Unable to register BQ27x00 driver\n");
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700466
467 return ret;
468}
469module_init(bq27x00_battery_init);
470
471static void __exit bq27x00_battery_exit(void)
472{
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200473 i2c_del_driver(&bq27x00_battery_driver);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700474}
475module_exit(bq27x00_battery_exit);
476
477MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
478MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
479MODULE_LICENSE("GPL");