Merge branch 'omap-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / power / bq20z75.c
blob492da27e1a47ed8960bd76e7626225e7fe0510f8
1 /*
2 * Gas Gauge driver for TI's BQ20Z75
4 * Copyright (c) 2010, NVIDIA Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/err.h>
25 #include <linux/power_supply.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
29 enum {
30 REG_MANUFACTURER_DATA,
31 REG_TEMPERATURE,
32 REG_VOLTAGE,
33 REG_CURRENT,
34 REG_CAPACITY,
35 REG_TIME_TO_EMPTY,
36 REG_TIME_TO_FULL,
37 REG_STATUS,
38 REG_CYCLE_COUNT,
39 REG_SERIAL_NUMBER,
40 REG_REMAINING_CAPACITY,
41 REG_FULL_CHARGE_CAPACITY,
42 REG_DESIGN_CAPACITY,
43 REG_DESIGN_VOLTAGE,
46 /* manufacturer access defines */
47 #define MANUFACTURER_ACCESS_STATUS 0x0006
48 #define MANUFACTURER_ACCESS_SLEEP 0x0011
50 /* battery status value bits */
51 #define BATTERY_DISCHARGING 0x40
52 #define BATTERY_FULL_CHARGED 0x20
53 #define BATTERY_FULL_DISCHARGED 0x10
55 #define BQ20Z75_DATA(_psp, _addr, _min_value, _max_value) { \
56 .psp = _psp, \
57 .addr = _addr, \
58 .min_value = _min_value, \
59 .max_value = _max_value, \
62 static const struct bq20z75_device_data {
63 enum power_supply_property psp;
64 u8 addr;
65 int min_value;
66 int max_value;
67 } bq20z75_data[] = {
68 [REG_MANUFACTURER_DATA] =
69 BQ20Z75_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
70 [REG_TEMPERATURE] =
71 BQ20Z75_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
72 [REG_VOLTAGE] =
73 BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
74 [REG_CURRENT] =
75 BQ20Z75_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768,
76 32767),
77 [REG_CAPACITY] =
78 BQ20Z75_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100),
79 [REG_REMAINING_CAPACITY] =
80 BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
81 [REG_FULL_CHARGE_CAPACITY] =
82 BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
83 [REG_TIME_TO_EMPTY] =
84 BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0,
85 65535),
86 [REG_TIME_TO_FULL] =
87 BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0,
88 65535),
89 [REG_STATUS] =
90 BQ20Z75_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
91 [REG_CYCLE_COUNT] =
92 BQ20Z75_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
93 [REG_DESIGN_CAPACITY] =
94 BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0,
95 65535),
96 [REG_DESIGN_VOLTAGE] =
97 BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0,
98 65535),
99 [REG_SERIAL_NUMBER] =
100 BQ20Z75_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
103 static enum power_supply_property bq20z75_properties[] = {
104 POWER_SUPPLY_PROP_STATUS,
105 POWER_SUPPLY_PROP_HEALTH,
106 POWER_SUPPLY_PROP_PRESENT,
107 POWER_SUPPLY_PROP_TECHNOLOGY,
108 POWER_SUPPLY_PROP_CYCLE_COUNT,
109 POWER_SUPPLY_PROP_VOLTAGE_NOW,
110 POWER_SUPPLY_PROP_CURRENT_NOW,
111 POWER_SUPPLY_PROP_CAPACITY,
112 POWER_SUPPLY_PROP_TEMP,
113 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
114 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
115 POWER_SUPPLY_PROP_SERIAL_NUMBER,
116 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
117 POWER_SUPPLY_PROP_ENERGY_NOW,
118 POWER_SUPPLY_PROP_ENERGY_FULL,
119 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
122 struct bq20z75_info {
123 struct i2c_client *client;
124 struct power_supply power_supply;
127 static int bq20z75_read_word_data(struct i2c_client *client, u8 address)
129 s32 ret;
131 ret = i2c_smbus_read_word_data(client, address);
132 if (ret < 0) {
133 dev_err(&client->dev,
134 "%s: i2c read at address 0x%x failed\n",
135 __func__, address);
136 return ret;
138 return le16_to_cpu(ret);
141 static int bq20z75_write_word_data(struct i2c_client *client, u8 address,
142 u16 value)
144 s32 ret;
146 ret = i2c_smbus_write_word_data(client, address, le16_to_cpu(value));
147 if (ret < 0) {
148 dev_err(&client->dev,
149 "%s: i2c write to address 0x%x failed\n",
150 __func__, address);
151 return ret;
153 return 0;
156 static int bq20z75_get_battery_presence_and_health(
157 struct i2c_client *client, enum power_supply_property psp,
158 union power_supply_propval *val)
160 s32 ret;
162 /* Write to ManufacturerAccess with
163 * ManufacturerAccess command and then
164 * read the status */
165 ret = bq20z75_write_word_data(client,
166 bq20z75_data[REG_MANUFACTURER_DATA].addr,
167 MANUFACTURER_ACCESS_STATUS);
168 if (ret < 0)
169 return ret;
172 ret = bq20z75_read_word_data(client,
173 bq20z75_data[REG_MANUFACTURER_DATA].addr);
174 if (ret < 0)
175 return ret;
177 if (ret < bq20z75_data[REG_MANUFACTURER_DATA].min_value ||
178 ret > bq20z75_data[REG_MANUFACTURER_DATA].max_value) {
179 val->intval = 0;
180 return 0;
183 /* Mask the upper nibble of 2nd byte and
184 * lower byte of response then
185 * shift the result by 8 to get status*/
186 ret &= 0x0F00;
187 ret >>= 8;
188 if (psp == POWER_SUPPLY_PROP_PRESENT) {
189 if (ret == 0x0F)
190 /* battery removed */
191 val->intval = 0;
192 else
193 val->intval = 1;
194 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
195 if (ret == 0x09)
196 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
197 else if (ret == 0x0B)
198 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
199 else if (ret == 0x0C)
200 val->intval = POWER_SUPPLY_HEALTH_DEAD;
201 else
202 val->intval = POWER_SUPPLY_HEALTH_GOOD;
205 return 0;
208 static int bq20z75_get_battery_property(struct i2c_client *client,
209 int reg_offset, enum power_supply_property psp,
210 union power_supply_propval *val)
212 s32 ret;
214 ret = bq20z75_read_word_data(client,
215 bq20z75_data[reg_offset].addr);
216 if (ret < 0)
217 return ret;
219 /* returned values are 16 bit */
220 if (bq20z75_data[reg_offset].min_value < 0)
221 ret = (s16)ret;
223 if (ret >= bq20z75_data[reg_offset].min_value &&
224 ret <= bq20z75_data[reg_offset].max_value) {
225 val->intval = ret;
226 if (psp == POWER_SUPPLY_PROP_STATUS) {
227 if (ret & BATTERY_FULL_CHARGED)
228 val->intval = POWER_SUPPLY_STATUS_FULL;
229 else if (ret & BATTERY_FULL_DISCHARGED)
230 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
231 else if (ret & BATTERY_DISCHARGING)
232 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
233 else
234 val->intval = POWER_SUPPLY_STATUS_CHARGING;
236 } else {
237 if (psp == POWER_SUPPLY_PROP_STATUS)
238 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
239 else
240 val->intval = 0;
243 return 0;
246 static void bq20z75_unit_adjustment(struct i2c_client *client,
247 enum power_supply_property psp, union power_supply_propval *val)
249 #define BASE_UNIT_CONVERSION 1000
250 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
251 #define TIME_UNIT_CONVERSION 600
252 #define TEMP_KELVIN_TO_CELCIUS 2731
253 switch (psp) {
254 case POWER_SUPPLY_PROP_ENERGY_NOW:
255 case POWER_SUPPLY_PROP_ENERGY_FULL:
256 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
257 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
258 break;
260 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
261 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
262 case POWER_SUPPLY_PROP_CURRENT_NOW:
263 val->intval *= BASE_UNIT_CONVERSION;
264 break;
266 case POWER_SUPPLY_PROP_TEMP:
267 /* bq20z75 provides battery tempreture in 0.1°K
268 * so convert it to 0.1°C */
269 val->intval -= TEMP_KELVIN_TO_CELCIUS;
270 val->intval *= 10;
271 break;
273 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
274 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
275 val->intval *= TIME_UNIT_CONVERSION;
276 break;
278 default:
279 dev_dbg(&client->dev,
280 "%s: no need for unit conversion %d\n", __func__, psp);
284 static int bq20z75_get_battery_capacity(struct i2c_client *client,
285 int reg_offset, enum power_supply_property psp,
286 union power_supply_propval *val)
288 s32 ret;
290 ret = bq20z75_read_word_data(client, bq20z75_data[reg_offset].addr);
291 if (ret < 0)
292 return ret;
294 if (psp == POWER_SUPPLY_PROP_CAPACITY) {
295 /* bq20z75 spec says that this can be >100 %
296 * even if max value is 100 % */
297 val->intval = min(ret, 100);
298 } else
299 val->intval = ret;
301 return 0;
304 static char bq20z75_serial[5];
305 static int bq20z75_get_battery_serial_number(struct i2c_client *client,
306 union power_supply_propval *val)
308 int ret;
310 ret = bq20z75_read_word_data(client,
311 bq20z75_data[REG_SERIAL_NUMBER].addr);
312 if (ret < 0)
313 return ret;
315 ret = sprintf(bq20z75_serial, "%04x", ret);
316 val->strval = bq20z75_serial;
318 return 0;
321 static int bq20z75_get_property(struct power_supply *psy,
322 enum power_supply_property psp,
323 union power_supply_propval *val)
325 int count;
326 int ret;
327 struct bq20z75_info *bq20z75_device = container_of(psy,
328 struct bq20z75_info, power_supply);
329 struct i2c_client *client = bq20z75_device->client;
331 switch (psp) {
332 case POWER_SUPPLY_PROP_PRESENT:
333 case POWER_SUPPLY_PROP_HEALTH:
334 ret = bq20z75_get_battery_presence_and_health(client, psp, val);
335 if (ret)
336 return ret;
337 break;
339 case POWER_SUPPLY_PROP_TECHNOLOGY:
340 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
341 break;
343 case POWER_SUPPLY_PROP_ENERGY_NOW:
344 case POWER_SUPPLY_PROP_ENERGY_FULL:
345 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
346 case POWER_SUPPLY_PROP_CAPACITY:
347 for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) {
348 if (psp == bq20z75_data[count].psp)
349 break;
352 ret = bq20z75_get_battery_capacity(client, count, psp, val);
353 if (ret)
354 return ret;
356 break;
358 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
359 ret = bq20z75_get_battery_serial_number(client, val);
360 if (ret)
361 return ret;
362 break;
364 case POWER_SUPPLY_PROP_STATUS:
365 case POWER_SUPPLY_PROP_CYCLE_COUNT:
366 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
367 case POWER_SUPPLY_PROP_CURRENT_NOW:
368 case POWER_SUPPLY_PROP_TEMP:
369 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
370 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
371 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
372 for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) {
373 if (psp == bq20z75_data[count].psp)
374 break;
377 ret = bq20z75_get_battery_property(client, count, psp, val);
378 if (ret)
379 return ret;
381 break;
383 default:
384 dev_err(&client->dev,
385 "%s: INVALID property\n", __func__);
386 return -EINVAL;
389 /* Convert units to match requirements for power supply class */
390 bq20z75_unit_adjustment(client, psp, val);
392 dev_dbg(&client->dev,
393 "%s: property = %d, value = %d\n", __func__, psp, val->intval);
395 return 0;
398 static int bq20z75_probe(struct i2c_client *client,
399 const struct i2c_device_id *id)
401 struct bq20z75_info *bq20z75_device;
402 int rc;
404 bq20z75_device = kzalloc(sizeof(struct bq20z75_info), GFP_KERNEL);
405 if (!bq20z75_device)
406 return -ENOMEM;
408 bq20z75_device->client = client;
409 bq20z75_device->power_supply.name = "battery";
410 bq20z75_device->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
411 bq20z75_device->power_supply.properties = bq20z75_properties;
412 bq20z75_device->power_supply.num_properties =
413 ARRAY_SIZE(bq20z75_properties);
414 bq20z75_device->power_supply.get_property = bq20z75_get_property;
416 i2c_set_clientdata(client, bq20z75_device);
418 rc = power_supply_register(&client->dev, &bq20z75_device->power_supply);
419 if (rc) {
420 dev_err(&client->dev,
421 "%s: Failed to register power supply\n", __func__);
422 kfree(bq20z75_device);
423 return rc;
426 dev_info(&client->dev,
427 "%s: battery gas gauge device registered\n", client->name);
429 return 0;
432 static int bq20z75_remove(struct i2c_client *client)
434 struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client);
436 power_supply_unregister(&bq20z75_device->power_supply);
437 kfree(bq20z75_device);
438 bq20z75_device = NULL;
440 return 0;
443 #if defined CONFIG_PM
444 static int bq20z75_suspend(struct i2c_client *client,
445 pm_message_t state)
447 s32 ret;
449 /* write to manufacturer access with sleep command */
450 ret = bq20z75_write_word_data(client,
451 bq20z75_data[REG_MANUFACTURER_DATA].addr,
452 MANUFACTURER_ACCESS_SLEEP);
453 if (ret < 0)
454 return ret;
456 return 0;
458 #else
459 #define bq20z75_suspend NULL
460 #endif
461 /* any smbus transaction will wake up bq20z75 */
462 #define bq20z75_resume NULL
464 static const struct i2c_device_id bq20z75_id[] = {
465 { "bq20z75", 0 },
469 static struct i2c_driver bq20z75_battery_driver = {
470 .probe = bq20z75_probe,
471 .remove = bq20z75_remove,
472 .suspend = bq20z75_suspend,
473 .resume = bq20z75_resume,
474 .id_table = bq20z75_id,
475 .driver = {
476 .name = "bq20z75-battery",
480 static int __init bq20z75_battery_init(void)
482 return i2c_add_driver(&bq20z75_battery_driver);
484 module_init(bq20z75_battery_init);
486 static void __exit bq20z75_battery_exit(void)
488 i2c_del_driver(&bq20z75_battery_driver);
490 module_exit(bq20z75_battery_exit);
492 MODULE_DESCRIPTION("BQ20z75 battery monitor driver");
493 MODULE_LICENSE("GPL");