blob: 5d940fad8d43a3de82da9c6a30a998110e408296 [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>
Harvey Harrison8aef7e82008-09-22 14:53:50 -070027#include <asm/unaligned.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070028
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020029#define DRIVER_VERSION "1.1.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070030
31#define BQ27x00_REG_TEMP 0x06
32#define BQ27x00_REG_VOLT 0x08
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070033#define BQ27x00_REG_AI 0x14
34#define BQ27x00_REG_FLAGS 0x0A
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020035#define BQ27x00_REG_TTE 0x16
36#define BQ27x00_REG_TTF 0x18
37#define BQ27x00_REG_TTECP 0x26
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070038
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020039#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020040#define BQ27000_FLAG_CHGS BIT(7)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020041
42#define BQ27500_REG_SOC 0x2c
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020043#define BQ27500_FLAG_DSC BIT(0)
44#define BQ27500_FLAG_FC BIT(9)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020045
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070046/* If the system has several batteries we need a different name for each
47 * of them...
48 */
49static DEFINE_IDR(battery_id);
50static DEFINE_MUTEX(battery_mutex);
51
52struct bq27x00_device_info;
53struct bq27x00_access_methods {
54 int (*read)(u8 reg, int *rt_value, int b_single,
55 struct bq27x00_device_info *di);
56};
57
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020058enum bq27x00_chip { BQ27000, BQ27500 };
59
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070060struct bq27x00_device_info {
61 struct device *dev;
62 int id;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070063 struct bq27x00_access_methods *bus;
64 struct power_supply bat;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020065 enum bq27x00_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070066
67 struct i2c_client *client;
68};
69
70static enum power_supply_property bq27x00_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020071 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070072 POWER_SUPPLY_PROP_PRESENT,
73 POWER_SUPPLY_PROP_VOLTAGE_NOW,
74 POWER_SUPPLY_PROP_CURRENT_NOW,
75 POWER_SUPPLY_PROP_CAPACITY,
76 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +020077 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
78 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
79 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070080};
81
82/*
83 * Common code for BQ27x00 devices
84 */
85
86static int bq27x00_read(u8 reg, int *rt_value, int b_single,
87 struct bq27x00_device_info *di)
88{
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +020089 return di->bus->read(reg, rt_value, b_single, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070090}
91
92/*
Grazvydas Ignotasb4de3602010-02-12 23:57:13 +020093 * Return the battery temperature in tenths of degree Celsius
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070094 * Or < 0 if something fails.
95 */
96static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
97{
98 int ret;
99 int temp = 0;
100
101 ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
102 if (ret) {
103 dev_err(di->dev, "error reading temperature\n");
104 return ret;
105 }
106
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200107 if (di->chip == BQ27500)
108 return temp - 2731;
109 else
110 return ((temp >> 2) - 273) * 10;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700111}
112
113/*
114 * Return the battery Voltage in milivolts
115 * Or < 0 if something fails.
116 */
117static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
118{
119 int ret;
120 int volt = 0;
121
122 ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
123 if (ret) {
124 dev_err(di->dev, "error reading voltage\n");
125 return ret;
126 }
127
128 return volt;
129}
130
131/*
132 * Return the battery average current
133 * Note that current can be negative signed as well
134 * Or 0 if something fails.
135 */
136static int bq27x00_battery_current(struct bq27x00_device_info *di)
137{
138 int ret;
139 int curr = 0;
140 int flags = 0;
141
142 ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
143 if (ret) {
144 dev_err(di->dev, "error reading current\n");
145 return 0;
146 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200147
148 if (di->chip == BQ27500) {
149 /* bq27500 returns signed value */
150 curr = (int)(s16)curr;
151 } else {
152 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
153 if (ret < 0) {
154 dev_err(di->dev, "error reading flags\n");
155 return 0;
156 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200157 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200158 dev_dbg(di->dev, "negative current!\n");
159 return -curr;
160 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700161 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200162
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700163 return curr;
164}
165
166/*
167 * Return the battery Relative State-of-Charge
168 * Or < 0 if something fails.
169 */
170static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
171{
172 int ret;
173 int rsoc = 0;
174
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200175 if (di->chip == BQ27500)
176 ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
177 else
178 ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700179 if (ret) {
180 dev_err(di->dev, "error reading relative State-of-Charge\n");
181 return ret;
182 }
183
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +0200184 return rsoc;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700185}
186
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200187static int bq27x00_battery_status(struct bq27x00_device_info *di,
188 union power_supply_propval *val)
189{
190 int flags = 0;
191 int status;
192 int ret;
193
194 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
195 if (ret < 0) {
196 dev_err(di->dev, "error reading flags\n");
197 return ret;
198 }
199
200 if (di->chip == BQ27500) {
201 if (flags & BQ27500_FLAG_FC)
202 status = POWER_SUPPLY_STATUS_FULL;
203 else if (flags & BQ27500_FLAG_DSC)
204 status = POWER_SUPPLY_STATUS_DISCHARGING;
205 else
206 status = POWER_SUPPLY_STATUS_CHARGING;
207 } else {
208 if (flags & BQ27000_FLAG_CHGS)
209 status = POWER_SUPPLY_STATUS_CHARGING;
210 else
211 status = POWER_SUPPLY_STATUS_DISCHARGING;
212 }
213
214 val->intval = status;
215 return 0;
216}
217
218/*
219 * Read a time register.
220 * Return < 0 if something fails.
221 */
222static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
223 union power_supply_propval *val)
224{
225 int tval = 0;
226 int ret;
227
228 ret = bq27x00_read(reg, &tval, 0, di);
229 if (ret) {
230 dev_err(di->dev, "error reading register %02x\n", reg);
231 return ret;
232 }
233
234 if (tval == 65535)
235 return -ENODATA;
236
237 val->intval = tval * 60;
238 return 0;
239}
240
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700241#define to_bq27x00_device_info(x) container_of((x), \
242 struct bq27x00_device_info, bat);
243
244static int bq27x00_battery_get_property(struct power_supply *psy,
245 enum power_supply_property psp,
246 union power_supply_propval *val)
247{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200248 int ret = 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700249 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
250
251 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200252 case POWER_SUPPLY_PROP_STATUS:
253 ret = bq27x00_battery_status(di, val);
254 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700255 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
256 case POWER_SUPPLY_PROP_PRESENT:
257 val->intval = bq27x00_battery_voltage(di);
258 if (psp == POWER_SUPPLY_PROP_PRESENT)
259 val->intval = val->intval <= 0 ? 0 : 1;
260 break;
261 case POWER_SUPPLY_PROP_CURRENT_NOW:
262 val->intval = bq27x00_battery_current(di);
263 break;
264 case POWER_SUPPLY_PROP_CAPACITY:
265 val->intval = bq27x00_battery_rsoc(di);
266 break;
267 case POWER_SUPPLY_PROP_TEMP:
268 val->intval = bq27x00_battery_temperature(di);
269 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200270 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
271 ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
272 break;
273 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
274 ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
275 break;
276 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
277 ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
278 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700279 default:
280 return -EINVAL;
281 }
282
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200283 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700284}
285
286static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
287{
288 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
289 di->bat.properties = bq27x00_battery_props;
290 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
291 di->bat.get_property = bq27x00_battery_get_property;
292 di->bat.external_power_changed = NULL;
293}
294
295/*
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200296 * i2c specific code
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700297 */
298
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200299static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700300 struct bq27x00_device_info *di)
301{
302 struct i2c_client *client = di->client;
303 struct i2c_msg msg[1];
304 unsigned char data[2];
305 int err;
306
307 if (!client->adapter)
308 return -ENODEV;
309
310 msg->addr = client->addr;
311 msg->flags = 0;
312 msg->len = 1;
313 msg->buf = data;
314
315 data[0] = reg;
316 err = i2c_transfer(client->adapter, msg, 1);
317
318 if (err >= 0) {
319 if (!b_single)
320 msg->len = 2;
321 else
322 msg->len = 1;
323
324 msg->flags = I2C_M_RD;
325 err = i2c_transfer(client->adapter, msg, 1);
326 if (err >= 0) {
327 if (!b_single)
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +0200328 *rt_value = get_unaligned_le16(data);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700329 else
330 *rt_value = data[0];
331
332 return 0;
333 }
334 }
335 return err;
336}
337
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200338static int bq27x00_battery_probe(struct i2c_client *client,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700339 const struct i2c_device_id *id)
340{
341 char *name;
342 struct bq27x00_device_info *di;
343 struct bq27x00_access_methods *bus;
344 int num;
345 int retval = 0;
346
347 /* Get new ID for the new battery device */
348 retval = idr_pre_get(&battery_id, GFP_KERNEL);
349 if (retval == 0)
350 return -ENOMEM;
351 mutex_lock(&battery_mutex);
352 retval = idr_get_new(&battery_id, client, &num);
353 mutex_unlock(&battery_mutex);
354 if (retval < 0)
355 return retval;
356
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200357 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700358 if (!name) {
359 dev_err(&client->dev, "failed to allocate device name\n");
360 retval = -ENOMEM;
361 goto batt_failed_1;
362 }
363
364 di = kzalloc(sizeof(*di), GFP_KERNEL);
365 if (!di) {
366 dev_err(&client->dev, "failed to allocate device info data\n");
367 retval = -ENOMEM;
368 goto batt_failed_2;
369 }
370 di->id = num;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200371 di->chip = id->driver_data;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700372
373 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
374 if (!bus) {
375 dev_err(&client->dev, "failed to allocate access method "
376 "data\n");
377 retval = -ENOMEM;
378 goto batt_failed_3;
379 }
380
381 i2c_set_clientdata(client, di);
382 di->dev = &client->dev;
383 di->bat.name = name;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200384 bus->read = &bq27x00_read_i2c;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700385 di->bus = bus;
386 di->client = client;
387
388 bq27x00_powersupply_init(di);
389
390 retval = power_supply_register(&client->dev, &di->bat);
391 if (retval) {
392 dev_err(&client->dev, "failed to register battery\n");
393 goto batt_failed_4;
394 }
395
396 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
397
398 return 0;
399
400batt_failed_4:
401 kfree(bus);
402batt_failed_3:
403 kfree(di);
404batt_failed_2:
405 kfree(name);
406batt_failed_1:
407 mutex_lock(&battery_mutex);
408 idr_remove(&battery_id, num);
409 mutex_unlock(&battery_mutex);
410
411 return retval;
412}
413
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200414static int bq27x00_battery_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700415{
416 struct bq27x00_device_info *di = i2c_get_clientdata(client);
417
418 power_supply_unregister(&di->bat);
419
420 kfree(di->bat.name);
421
422 mutex_lock(&battery_mutex);
423 idr_remove(&battery_id, di->id);
424 mutex_unlock(&battery_mutex);
425
426 kfree(di);
427
428 return 0;
429}
430
431/*
432 * Module stuff
433 */
434
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200435static const struct i2c_device_id bq27x00_id[] = {
436 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
437 { "bq27500", BQ27500 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700438 {},
439};
440
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200441static struct i2c_driver bq27x00_battery_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700442 .driver = {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200443 .name = "bq27x00-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700444 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200445 .probe = bq27x00_battery_probe,
446 .remove = bq27x00_battery_remove,
447 .id_table = bq27x00_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700448};
449
450static int __init bq27x00_battery_init(void)
451{
452 int ret;
453
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200454 ret = i2c_add_driver(&bq27x00_battery_driver);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700455 if (ret)
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200456 printk(KERN_ERR "Unable to register BQ27x00 driver\n");
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700457
458 return ret;
459}
460module_init(bq27x00_battery_init);
461
462static void __exit bq27x00_battery_exit(void)
463{
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200464 i2c_del_driver(&bq27x00_battery_driver);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700465}
466module_exit(bq27x00_battery_exit);
467
468MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
469MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
470MODULE_LICENSE("GPL");