bq27x00: Improve temperature property precession
[deliverable/linux.git] / drivers / power / bq27x00_battery.c
CommitLineData
b996ad0e
RG
1/*
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>
b996ad0e 26#include <linux/i2c.h>
5a0e3ad6 27#include <linux/slab.h>
8aef7e8f 28#include <asm/unaligned.h>
b996ad0e 29
e20908d9 30#define DRIVER_VERSION "1.1.0"
b996ad0e
RG
31
32#define BQ27x00_REG_TEMP 0x06
33#define BQ27x00_REG_VOLT 0x08
b996ad0e
RG
34#define BQ27x00_REG_AI 0x14
35#define BQ27x00_REG_FLAGS 0x0A
4e924a81
GI
36#define BQ27x00_REG_TTE 0x16
37#define BQ27x00_REG_TTF 0x18
38#define BQ27x00_REG_TTECP 0x26
b996ad0e 39
e20908d9 40#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
4e924a81 41#define BQ27000_FLAG_CHGS BIT(7)
e20908d9
GI
42
43#define BQ27500_REG_SOC 0x2c
4e924a81
GI
44#define BQ27500_FLAG_DSC BIT(0)
45#define BQ27500_FLAG_FC BIT(9)
e20908d9 46
b996ad0e
RG
47/* If the system has several batteries we need a different name for each
48 * of them...
49 */
50static DEFINE_IDR(battery_id);
51static DEFINE_MUTEX(battery_mutex);
52
53struct bq27x00_device_info;
54struct bq27x00_access_methods {
55 int (*read)(u8 reg, int *rt_value, int b_single,
56 struct bq27x00_device_info *di);
57};
58
e20908d9
GI
59enum bq27x00_chip { BQ27000, BQ27500 };
60
b996ad0e
RG
61struct bq27x00_device_info {
62 struct device *dev;
63 int id;
b996ad0e
RG
64 struct bq27x00_access_methods *bus;
65 struct power_supply bat;
e20908d9 66 enum bq27x00_chip chip;
b996ad0e
RG
67
68 struct i2c_client *client;
69};
70
71static enum power_supply_property bq27x00_battery_props[] = {
4e924a81 72 POWER_SUPPLY_PROP_STATUS,
b996ad0e
RG
73 POWER_SUPPLY_PROP_PRESENT,
74 POWER_SUPPLY_PROP_VOLTAGE_NOW,
75 POWER_SUPPLY_PROP_CURRENT_NOW,
76 POWER_SUPPLY_PROP_CAPACITY,
77 POWER_SUPPLY_PROP_TEMP,
4e924a81
GI
78 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
79 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
80 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
5661f334 81 POWER_SUPPLY_PROP_TECHNOLOGY,
b996ad0e
RG
82};
83
84/*
85 * Common code for BQ27x00 devices
86 */
87
88static int bq27x00_read(u8 reg, int *rt_value, int b_single,
89 struct bq27x00_device_info *di)
90{
97f70c23 91 return di->bus->read(reg, rt_value, b_single, di);
b996ad0e
RG
92}
93
94/*
b4de3608 95 * Return the battery temperature in tenths of degree Celsius
b996ad0e
RG
96 * Or < 0 if something fails.
97 */
98static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
99{
100 int ret;
101 int temp = 0;
102
103 ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
104 if (ret) {
105 dev_err(di->dev, "error reading temperature\n");
106 return ret;
107 }
108
e20908d9
GI
109 if (di->chip == BQ27500)
110 return temp - 2731;
111 else
0e9f3049 112 return ((temp * 5) - 5463) / 2;
b996ad0e
RG
113}
114
115/*
116 * Return the battery Voltage in milivolts
117 * Or < 0 if something fails.
118 */
119static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
120{
121 int ret;
122 int volt = 0;
123
124 ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
125 if (ret) {
126 dev_err(di->dev, "error reading voltage\n");
127 return ret;
128 }
129
afbc74fd 130 return volt * 1000;
b996ad0e
RG
131}
132
133/*
134 * Return the battery average current
135 * Note that current can be negative signed as well
136 * Or 0 if something fails.
137 */
138static int bq27x00_battery_current(struct bq27x00_device_info *di)
139{
140 int ret;
141 int curr = 0;
142 int flags = 0;
143
144 ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
145 if (ret) {
146 dev_err(di->dev, "error reading current\n");
147 return 0;
148 }
e20908d9
GI
149
150 if (di->chip == BQ27500) {
151 /* bq27500 returns signed value */
152 curr = (int)(s16)curr;
153 } else {
154 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
155 if (ret < 0) {
156 dev_err(di->dev, "error reading flags\n");
157 return 0;
158 }
4e924a81 159 if (flags & BQ27000_FLAG_CHGS) {
e20908d9 160 dev_dbg(di->dev, "negative current!\n");
afbc74fd 161 curr = -curr;
e20908d9 162 }
b996ad0e 163 }
e20908d9 164
afbc74fd 165 return curr * 1000;
b996ad0e
RG
166}
167
168/*
169 * Return the battery Relative State-of-Charge
170 * Or < 0 if something fails.
171 */
172static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
173{
174 int ret;
175 int rsoc = 0;
176
e20908d9
GI
177 if (di->chip == BQ27500)
178 ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
179 else
180 ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
b996ad0e
RG
181 if (ret) {
182 dev_err(di->dev, "error reading relative State-of-Charge\n");
183 return ret;
184 }
185
97f70c23 186 return rsoc;
b996ad0e
RG
187}
188
4e924a81
GI
189static int bq27x00_battery_status(struct bq27x00_device_info *di,
190 union power_supply_propval *val)
191{
192 int flags = 0;
193 int status;
194 int ret;
195
196 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
197 if (ret < 0) {
198 dev_err(di->dev, "error reading flags\n");
199 return ret;
200 }
201
202 if (di->chip == BQ27500) {
203 if (flags & BQ27500_FLAG_FC)
204 status = POWER_SUPPLY_STATUS_FULL;
205 else if (flags & BQ27500_FLAG_DSC)
206 status = POWER_SUPPLY_STATUS_DISCHARGING;
207 else
208 status = POWER_SUPPLY_STATUS_CHARGING;
209 } else {
210 if (flags & BQ27000_FLAG_CHGS)
211 status = POWER_SUPPLY_STATUS_CHARGING;
212 else
213 status = POWER_SUPPLY_STATUS_DISCHARGING;
214 }
215
216 val->intval = status;
217 return 0;
218}
219
220/*
221 * Read a time register.
222 * Return < 0 if something fails.
223 */
224static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
225 union power_supply_propval *val)
226{
227 int tval = 0;
228 int ret;
229
230 ret = bq27x00_read(reg, &tval, 0, di);
231 if (ret) {
232 dev_err(di->dev, "error reading register %02x\n", reg);
233 return ret;
234 }
235
236 if (tval == 65535)
237 return -ENODATA;
238
239 val->intval = tval * 60;
240 return 0;
241}
242
b996ad0e
RG
243#define to_bq27x00_device_info(x) container_of((x), \
244 struct bq27x00_device_info, bat);
245
246static int bq27x00_battery_get_property(struct power_supply *psy,
247 enum power_supply_property psp,
248 union power_supply_propval *val)
249{
4e924a81 250 int ret = 0;
b996ad0e
RG
251 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
252
253 switch (psp) {
4e924a81
GI
254 case POWER_SUPPLY_PROP_STATUS:
255 ret = bq27x00_battery_status(di, val);
256 break;
b996ad0e
RG
257 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
258 case POWER_SUPPLY_PROP_PRESENT:
259 val->intval = bq27x00_battery_voltage(di);
260 if (psp == POWER_SUPPLY_PROP_PRESENT)
261 val->intval = val->intval <= 0 ? 0 : 1;
262 break;
263 case POWER_SUPPLY_PROP_CURRENT_NOW:
264 val->intval = bq27x00_battery_current(di);
265 break;
266 case POWER_SUPPLY_PROP_CAPACITY:
267 val->intval = bq27x00_battery_rsoc(di);
268 break;
269 case POWER_SUPPLY_PROP_TEMP:
270 val->intval = bq27x00_battery_temperature(di);
271 break;
4e924a81
GI
272 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
273 ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
274 break;
275 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
276 ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
277 break;
278 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
279 ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
280 break;
5661f334
LPC
281 case POWER_SUPPLY_PROP_TECHNOLOGY:
282 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
283 break;
b996ad0e
RG
284 default:
285 return -EINVAL;
286 }
287
4e924a81 288 return ret;
b996ad0e
RG
289}
290
291static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
292{
293 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
294 di->bat.properties = bq27x00_battery_props;
295 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
296 di->bat.get_property = bq27x00_battery_get_property;
297 di->bat.external_power_changed = NULL;
298}
299
300/*
e20908d9 301 * i2c specific code
b996ad0e
RG
302 */
303
e20908d9 304static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
b996ad0e
RG
305 struct bq27x00_device_info *di)
306{
307 struct i2c_client *client = di->client;
308 struct i2c_msg msg[1];
309 unsigned char data[2];
310 int err;
311
312 if (!client->adapter)
313 return -ENODEV;
314
315 msg->addr = client->addr;
316 msg->flags = 0;
317 msg->len = 1;
318 msg->buf = data;
319
320 data[0] = reg;
321 err = i2c_transfer(client->adapter, msg, 1);
322
323 if (err >= 0) {
324 if (!b_single)
325 msg->len = 2;
326 else
327 msg->len = 1;
328
329 msg->flags = I2C_M_RD;
330 err = i2c_transfer(client->adapter, msg, 1);
331 if (err >= 0) {
332 if (!b_single)
97f70c23 333 *rt_value = get_unaligned_le16(data);
b996ad0e
RG
334 else
335 *rt_value = data[0];
336
337 return 0;
338 }
339 }
340 return err;
341}
342
e20908d9 343static int bq27x00_battery_probe(struct i2c_client *client,
b996ad0e
RG
344 const struct i2c_device_id *id)
345{
346 char *name;
347 struct bq27x00_device_info *di;
348 struct bq27x00_access_methods *bus;
349 int num;
350 int retval = 0;
351
352 /* Get new ID for the new battery device */
353 retval = idr_pre_get(&battery_id, GFP_KERNEL);
354 if (retval == 0)
355 return -ENOMEM;
356 mutex_lock(&battery_mutex);
357 retval = idr_get_new(&battery_id, client, &num);
358 mutex_unlock(&battery_mutex);
359 if (retval < 0)
360 return retval;
361
e20908d9 362 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
b996ad0e
RG
363 if (!name) {
364 dev_err(&client->dev, "failed to allocate device name\n");
365 retval = -ENOMEM;
366 goto batt_failed_1;
367 }
368
369 di = kzalloc(sizeof(*di), GFP_KERNEL);
370 if (!di) {
371 dev_err(&client->dev, "failed to allocate device info data\n");
372 retval = -ENOMEM;
373 goto batt_failed_2;
374 }
375 di->id = num;
e20908d9 376 di->chip = id->driver_data;
b996ad0e
RG
377
378 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
379 if (!bus) {
380 dev_err(&client->dev, "failed to allocate access method "
381 "data\n");
382 retval = -ENOMEM;
383 goto batt_failed_3;
384 }
385
386 i2c_set_clientdata(client, di);
387 di->dev = &client->dev;
388 di->bat.name = name;
e20908d9 389 bus->read = &bq27x00_read_i2c;
b996ad0e
RG
390 di->bus = bus;
391 di->client = client;
392
393 bq27x00_powersupply_init(di);
394
395 retval = power_supply_register(&client->dev, &di->bat);
396 if (retval) {
397 dev_err(&client->dev, "failed to register battery\n");
398 goto batt_failed_4;
399 }
400
401 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
402
403 return 0;
404
405batt_failed_4:
406 kfree(bus);
407batt_failed_3:
408 kfree(di);
409batt_failed_2:
410 kfree(name);
411batt_failed_1:
412 mutex_lock(&battery_mutex);
413 idr_remove(&battery_id, num);
414 mutex_unlock(&battery_mutex);
415
416 return retval;
417}
418
e20908d9 419static int bq27x00_battery_remove(struct i2c_client *client)
b996ad0e
RG
420{
421 struct bq27x00_device_info *di = i2c_get_clientdata(client);
422
423 power_supply_unregister(&di->bat);
424
9292585c 425 kfree(di->bus);
b996ad0e
RG
426 kfree(di->bat.name);
427
428 mutex_lock(&battery_mutex);
429 idr_remove(&battery_id, di->id);
430 mutex_unlock(&battery_mutex);
431
432 kfree(di);
433
434 return 0;
435}
436
437/*
438 * Module stuff
439 */
440
e20908d9
GI
441static const struct i2c_device_id bq27x00_id[] = {
442 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
443 { "bq27500", BQ27500 },
b996ad0e
RG
444 {},
445};
446
e20908d9 447static struct i2c_driver bq27x00_battery_driver = {
b996ad0e 448 .driver = {
e20908d9 449 .name = "bq27x00-battery",
b996ad0e 450 },
e20908d9
GI
451 .probe = bq27x00_battery_probe,
452 .remove = bq27x00_battery_remove,
453 .id_table = bq27x00_id,
b996ad0e
RG
454};
455
456static int __init bq27x00_battery_init(void)
457{
458 int ret;
459
e20908d9 460 ret = i2c_add_driver(&bq27x00_battery_driver);
b996ad0e 461 if (ret)
e20908d9 462 printk(KERN_ERR "Unable to register BQ27x00 driver\n");
b996ad0e
RG
463
464 return ret;
465}
466module_init(bq27x00_battery_init);
467
468static void __exit bq27x00_battery_exit(void)
469{
e20908d9 470 i2c_del_driver(&bq27x00_battery_driver);
b996ad0e
RG
471}
472module_exit(bq27x00_battery_exit);
473
474MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
475MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
476MODULE_LICENSE("GPL");
This page took 0.192282 seconds and 5 git commands to generate.