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
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>
30 REG_MANUFACTURER_DATA
,
40 REG_REMAINING_CAPACITY
,
41 REG_FULL_CHARGE_CAPACITY
,
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) { \
58 .min_value = _min_value, \
59 .max_value = _max_value, \
62 static const struct bq20z75_device_data
{
63 enum power_supply_property psp
;
68 [REG_MANUFACTURER_DATA
] =
69 BQ20Z75_DATA(POWER_SUPPLY_PROP_PRESENT
, 0x00, 0, 65535),
71 BQ20Z75_DATA(POWER_SUPPLY_PROP_TEMP
, 0x08, 0, 65535),
73 BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW
, 0x09, 0, 20000),
75 BQ20Z75_DATA(POWER_SUPPLY_PROP_CURRENT_NOW
, 0x0A, -32768,
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),
84 BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
, 0x12, 0,
87 BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
, 0x13, 0,
90 BQ20Z75_DATA(POWER_SUPPLY_PROP_STATUS
, 0x16, 0, 65535),
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,
96 [REG_DESIGN_VOLTAGE
] =
97 BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
, 0x19, 0,
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
)
131 ret
= i2c_smbus_read_word_data(client
, address
);
133 dev_err(&client
->dev
,
134 "%s: i2c read at address 0x%x failed\n",
138 return le16_to_cpu(ret
);
141 static int bq20z75_write_word_data(struct i2c_client
*client
, u8 address
,
146 ret
= i2c_smbus_write_word_data(client
, address
, le16_to_cpu(value
));
148 dev_err(&client
->dev
,
149 "%s: i2c write to address 0x%x failed\n",
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
)
162 /* Write to ManufacturerAccess with
163 * ManufacturerAccess command and then
165 ret
= bq20z75_write_word_data(client
,
166 bq20z75_data
[REG_MANUFACTURER_DATA
].addr
,
167 MANUFACTURER_ACCESS_STATUS
);
172 ret
= bq20z75_read_word_data(client
,
173 bq20z75_data
[REG_MANUFACTURER_DATA
].addr
);
177 if (ret
< bq20z75_data
[REG_MANUFACTURER_DATA
].min_value
||
178 ret
> bq20z75_data
[REG_MANUFACTURER_DATA
].max_value
) {
183 /* Mask the upper nibble of 2nd byte and
184 * lower byte of response then
185 * shift the result by 8 to get status*/
188 if (psp
== POWER_SUPPLY_PROP_PRESENT
) {
190 /* battery removed */
194 } else if (psp
== POWER_SUPPLY_PROP_HEALTH
) {
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
;
202 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
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
)
214 ret
= bq20z75_read_word_data(client
,
215 bq20z75_data
[reg_offset
].addr
);
219 /* returned values are 16 bit */
220 if (bq20z75_data
[reg_offset
].min_value
< 0)
223 if (ret
>= bq20z75_data
[reg_offset
].min_value
&&
224 ret
<= bq20z75_data
[reg_offset
].max_value
) {
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
;
234 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
237 if (psp
== POWER_SUPPLY_PROP_STATUS
)
238 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
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
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
;
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
;
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
;
273 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
274 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
275 val
->intval
*= TIME_UNIT_CONVERSION
;
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
)
290 ret
= bq20z75_read_word_data(client
, bq20z75_data
[reg_offset
].addr
);
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);
304 static char bq20z75_serial
[5];
305 static int bq20z75_get_battery_serial_number(struct i2c_client
*client
,
306 union power_supply_propval
*val
)
310 ret
= bq20z75_read_word_data(client
,
311 bq20z75_data
[REG_SERIAL_NUMBER
].addr
);
315 ret
= sprintf(bq20z75_serial
, "%04x", ret
);
316 val
->strval
= bq20z75_serial
;
321 static int bq20z75_get_property(struct power_supply
*psy
,
322 enum power_supply_property psp
,
323 union power_supply_propval
*val
)
327 struct bq20z75_info
*bq20z75_device
= container_of(psy
,
328 struct bq20z75_info
, power_supply
);
329 struct i2c_client
*client
= bq20z75_device
->client
;
332 case POWER_SUPPLY_PROP_PRESENT
:
333 case POWER_SUPPLY_PROP_HEALTH
:
334 ret
= bq20z75_get_battery_presence_and_health(client
, psp
, val
);
339 case POWER_SUPPLY_PROP_TECHNOLOGY
:
340 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
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
)
352 ret
= bq20z75_get_battery_capacity(client
, count
, psp
, val
);
358 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
359 ret
= bq20z75_get_battery_serial_number(client
, val
);
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
)
377 ret
= bq20z75_get_battery_property(client
, count
, psp
, val
);
384 dev_err(&client
->dev
,
385 "%s: INVALID property\n", __func__
);
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
);
398 static int bq20z75_probe(struct i2c_client
*client
,
399 const struct i2c_device_id
*id
)
401 struct bq20z75_info
*bq20z75_device
;
404 bq20z75_device
= kzalloc(sizeof(struct bq20z75_info
), GFP_KERNEL
);
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
);
420 dev_err(&client
->dev
,
421 "%s: Failed to register power supply\n", __func__
);
422 kfree(bq20z75_device
);
426 dev_info(&client
->dev
,
427 "%s: battery gas gauge device registered\n", client
->name
);
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
;
443 #if defined CONFIG_PM
444 static int bq20z75_suspend(struct i2c_client
*client
,
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
);
459 #define bq20z75_suspend NULL
461 /* any smbus transaction will wake up bq20z75 */
462 #define bq20z75_resume NULL
464 static const struct i2c_device_id bq20z75_id
[] = {
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
,
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");