2 * linux/drivers/power/bq27x00_battery.c
4 * BQ27000/BQ27200 battery driver
6 * Copyright (C) 2008 Texas Instruments, Inc.
8 * Author: Texas Instruments
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 #include <linux/module.h>
20 #include <linux/param.h>
21 #include <linux/jiffies.h>
22 #include <linux/workqueue.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/power_supply.h>
27 #ifdef CONFIG_BATTERY_BQ27000
30 #ifdef CONFIG_BATTERY_BQ27200
31 #include <linux/i2c.h>
34 #define BQ27x00_REG_TEMP 0x06
35 #define BQ27x00_REG_VOLT 0x08
36 #define BQ27x00_REG_RSOC 0x0B /* Relative State-of-Charge */
37 #define BQ27x00_REG_AI 0x14
38 #define BQ27x00_REG_FLAGS 0x0A
39 #define HIGH_BYTE(A) ((A) << 8)
41 #ifdef CONFIG_BATTERY_BQ27000
42 extern int w1_bq27000_read(struct device
*dev
, u8 reg
);
45 struct bq27x00_device_info
;
46 struct bq27x00_access_methods
{
47 int (*read
)(u8 reg
, int *rt_value
, int b_single
,
48 struct bq27x00_device_info
*di
);
51 struct bq27x00_device_info
{
53 #ifdef CONFIG_BATTERY_BQ27000
54 struct device
*w1_dev
;
56 #ifdef CONFIG_BATTERY_BQ27200
57 struct i2c_client
*client
;
59 unsigned long update_time
;
64 struct bq27x00_access_methods
*bus
;
65 struct power_supply bat
;
66 struct delayed_work monitor_work
;
69 static unsigned int cache_time
= 60000;
70 module_param(cache_time
, uint
, 0644);
71 MODULE_PARM_DESC(cache_time
, "cache time in milliseconds");
73 static enum power_supply_property bq27x00_battery_props
[] = {
74 POWER_SUPPLY_PROP_PRESENT
,
75 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
76 POWER_SUPPLY_PROP_CURRENT_NOW
,
77 POWER_SUPPLY_PROP_CHARGE_NOW
,
78 POWER_SUPPLY_PROP_CAPACITY
,
79 POWER_SUPPLY_PROP_TEMP
,
82 static int bq27x00_read(u8 reg
, int *rt_value
, int b_single
,
83 struct bq27x00_device_info
*di
);
85 #ifdef CONFIG_BATTERY_BQ27000
86 static int bq27000_battery_probe(struct platform_device
*dev
);
87 static int bq27000_battery_remove(struct platform_device
*dev
);
89 static int bq27000_battery_suspend(struct platform_device
*dev
,
91 static int bq27000_battery_resume(struct platform_device
*dev
);
92 #endif /* CONFIG_PM */
94 static struct platform_driver bq27000_battery_driver
= {
95 .probe
= bq27000_battery_probe
,
96 .remove
= bq27000_battery_remove
,
98 .suspend
= bq27000_battery_suspend
,
99 .resume
= bq27000_battery_resume
,
100 #endif /* CONFIG_PM */
102 .name
= "bq27000-battery",
105 #endif /* CONFIG_BATTERY_BQ27000 */
107 #ifdef CONFIG_BATTERY_BQ27200
108 static int bq27200_battery_probe(struct i2c_client
*client
);
109 static int bq27200_battery_remove(struct i2c_client
*client
);
111 static int bq27200_battery_suspend(struct i2c_client
*client
,
113 static int bq27200_battery_resume(struct i2c_client
*client
);
114 #endif /* CONFIG_PM */
115 static struct i2c_driver bq27200_battery_driver
= {
117 .name
= "bq27200-bat",
119 .probe
= bq27200_battery_probe
,
120 .remove
= bq27200_battery_remove
,
122 .suspend
= bq27200_battery_suspend
,
123 .resume
= bq27200_battery_resume
,
124 #endif /* CONFIG_PM */
126 #endif /* CONFIG_BATTERY_BQ27200 */
129 * Return the battery temperature in Celcius degrees
130 * Or < 0 if something fails.
132 static int bq27x00_battery_temperature(struct bq27x00_device_info
*di
)
136 ret
= bq27x00_read(BQ27x00_REG_TEMP
, &temp
, 0, di
);
138 pr_err("BQ27x00 battery driver:"
139 "Error reading temperature from the battery\n");
143 return (temp
>> 2) - 273;
147 * Return the battery Voltage in milivolts
148 * Or < 0 if something fails.
150 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
)
154 ret
= bq27x00_read(BQ27x00_REG_VOLT
, &volt
, 0, di
);
156 pr_err("BQ27x00 battery driver:"
157 "Error reading battery voltage from the battery\n");
165 * Return the battery average current
166 * Note that current can be negative signed as well
167 * Or 0 if something fails.
169 static int bq27x00_battery_current(struct bq27x00_device_info
*di
)
171 int ret
, curr
= 0, flags
= 0;
173 ret
= bq27x00_read(BQ27x00_REG_AI
, &curr
, 0, di
);
175 pr_err("BQ27x00 battery driver:"
176 "Error reading current from the battery\n");
179 ret
= bq27x00_read(BQ27x00_REG_FLAGS
, &flags
, 0, di
);
181 pr_err("BQ27x00 battery driver:"
182 "Error reading battery flags\n");
185 if ((flags
& (1 << 7)) != 0) {
186 pr_debug("Negative current\n");
194 * Return the battery Relative State-of-Charge
195 * Or < 0 if something fails.
197 static int bq27x00_battery_rsoc(struct bq27x00_device_info
*di
)
201 ret
= bq27x00_read(BQ27x00_REG_RSOC
, &rsoc
, 1, di
);
203 pr_err("BQ27x00 battery driver:"
204 "Error reading battery Relative"
205 "State-of-Charge\n");
211 #ifdef CONFIG_BATTERY_BQ27000
212 static inline int bq27000_read(u8 reg
, int *rt_value
, int b_single
,
213 struct bq27x00_device_info
*di
)
217 val
= w1_bq27000_read(di
->w1_dev
, reg
);
222 val
= w1_bq27000_read(di
->w1_dev
, reg
+ 1);
223 *rt_value
+= HIGH_BYTE((int) val
);
229 static inline int bq27000_read(u8 reg
, int *rt_value
, int b_single
,
230 struct bq27x00_device_info
*di
)
236 #ifdef CONFIG_BATTERY_BQ27200
237 static inline int bq27200_read(u8 reg
, int *rt_value
, int b_single
,
238 struct bq27x00_device_info
*di
)
240 struct i2c_client
*client
= di
->client
;
241 struct i2c_msg msg
[1];
242 unsigned char data
[2];
245 if (!client
->adapter
)
248 msg
->addr
= client
->addr
;
254 err
= i2c_transfer(client
->adapter
, msg
, 1);
262 msg
->flags
= I2C_M_RD
;
263 err
= i2c_transfer(client
->adapter
, msg
, 1);
266 *rt_value
= data
[1] | HIGH_BYTE(data
[0]);
272 pr_err("BQ27200 I2C read failed\n");
276 pr_err("BQ27200 I2C write failed\n");
281 static inline int bq27200_read(u8 reg
, int *rt_value
, int b_single
,
282 struct bq27x00_device_info
*di
)
288 static int bq27x00_read(u8 reg
, int *rt_value
, int b_single
,
289 struct bq27x00_device_info
*di
)
293 ret
= di
->bus
->read(reg
, rt_value
, b_single
, di
);
298 * Read the battery temp, voltage, current and relative state of charge.
300 static void bq27x00_battery_read_status(struct bq27x00_device_info
*di
)
302 if (di
->update_time
&& time_before(jiffies
, di
->update_time
+
303 msecs_to_jiffies(cache_time
)))
306 di
->temp_C
= bq27x00_battery_temperature(di
);
307 di
->voltage_uV
= bq27x00_battery_voltage(di
);
308 di
->current_uA
= bq27x00_battery_current(di
);
309 di
->charge_rsoc
= bq27x00_battery_rsoc(di
);
311 di
->update_time
= jiffies
;
316 static void bq27x00_battery_work(struct delayed_work
*work
)
318 struct bq27x00_device_info
*di
= container_of(work
,
319 struct bq27x00_device_info
, monitor_work
);
321 bq27x00_battery_read_status(di
);
322 schedule_delayed_work(&di
->monitor_work
, 100);
326 #define to_bq27x00_device_info(x) container_of((x), \
327 struct bq27x00_device_info, bat);
329 static int bq27x00_battery_get_property(struct power_supply
*psy
,
330 enum power_supply_property psp
,
331 union power_supply_propval
*val
)
333 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
336 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
337 val
->intval
= di
->voltage_uV
;
339 case POWER_SUPPLY_PROP_CURRENT_NOW
:
340 val
->intval
= di
->current_uA
;
342 case POWER_SUPPLY_PROP_CHARGE_NOW
:
343 val
->intval
= di
->charge_rsoc
;
345 case POWER_SUPPLY_PROP_TEMP
:
346 val
->intval
= di
->temp_C
;
348 case POWER_SUPPLY_PROP_CAPACITY
:
349 val
->intval
= di
->charge_rsoc
;
351 case POWER_SUPPLY_PROP_PRESENT
:
352 if (di
->voltage_uV
== 0)
364 static void bq27x00_powersupply_init(struct bq27x00_device_info
*di
)
366 di
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
367 di
->bat
.properties
= bq27x00_battery_props
;
368 di
->bat
.num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
369 di
->bat
.get_property
= bq27x00_battery_get_property
;
370 di
->bat
.external_power_changed
= NULL
;
374 #ifdef CONFIG_BATTERY_BQ27200
375 static int bq27200_battery_probe(struct i2c_client
*client
)
377 struct bq27x00_device_info
*di
;
378 struct bq27x00_access_methods
*bus
;
381 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
383 pr_err("BQ27000 battery driver:"
384 "Failed to allocate device info structure\n");
388 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
390 pr_err("BQ27000 battery driver:"
391 "Failed to allocate access method structure\n");
396 i2c_set_clientdata(client
, di
);
397 di
->dev
= &client
->dev
;
398 di
->bat
.name
= "bq27200";
399 bus
->read
= &bq27200_read
;
403 bq27x00_powersupply_init(di
);
405 retval
= power_supply_register(&client
->dev
, &di
->bat
);
407 pr_err("BQ27200 battery driver: Failed to register battery\n");
411 INIT_DELAYED_WORK(&di
->monitor_work
, bq27x00_battery_work
);
412 schedule_delayed_work(&di
->monitor_work
, 100);
422 static int bq27200_battery_remove(struct i2c_client
*client
)
424 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
426 flush_scheduled_work();
427 power_supply_unregister(&di
->bat
);
434 static int bq27200_battery_suspend(struct i2c_client
*client
, pm_message_t mesg
)
436 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
438 cancel_delayed_work(&di
->monitor_work
);
442 static int bq27200_battery_resume(struct i2c_client
*client
)
444 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
446 schedule_delayed_work(&di
->monitor_work
, 0);
449 #endif /* CONFIG_PM */
450 #endif /* CONFIG_BATTERY_BQ27200 */
452 #ifdef CONFIG_BATTERY_BQ27000
453 static int bq27000_battery_probe(struct platform_device
*pdev
)
455 struct bq27x00_device_info
*di
;
456 struct bq27x00_access_methods
*bus
;
459 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
461 pr_err("BQ27000 battery driver:"
462 "Failed to allocate device info structure\n");
466 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
468 pr_err("BQ27000 battery driver:"
469 "Failed to allocate access method structure\n");
474 platform_set_drvdata(pdev
, di
);
476 di
->dev
= &pdev
->dev
;
477 di
->w1_dev
= pdev
->dev
.parent
;
478 di
->bat
.name
= "bq27000";
479 bus
->read
= &bq27000_read
;
482 bq27x00_powersupply_init(di
);
484 retval
= power_supply_register(&pdev
->dev
, &di
->bat
);
486 pr_err("BQ27000 battery driver: Failed to register battery\n");
490 INIT_DELAYED_WORK(&di
->monitor_work
, bq27x00_battery_work
);
491 schedule_delayed_work(&di
->monitor_work
, 50);
501 static int bq27000_battery_remove(struct platform_device
*pdev
)
503 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
505 flush_scheduled_work();
506 power_supply_unregister(&di
->bat
);
507 platform_set_drvdata(pdev
, NULL
);
514 static int bq27000_battery_suspend(struct platform_device
*pdev
,
517 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
519 cancel_delayed_work(&di
->monitor_work
);
523 static int bq27000_battery_resume(struct platform_device
*pdev
)
525 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
527 schedule_delayed_work(&di
->monitor_work
, 0);
530 #endif /* CONFIG_PM */
531 #endif /* CONFIG_BATTERY_BQ27000 */
533 static int __init
bq27x00_battery_init(void)
537 #ifdef CONFIG_BATTERY_BQ27000
538 status
= platform_driver_register(&bq27000_battery_driver
);
540 printk(KERN_ERR
"Unable to register BQ27000 driver\n");
542 #ifdef CONFIG_BATTERY_BQ27200
543 status
= i2c_add_driver(&bq27200_battery_driver
);
544 printk(KERN_ERR
"Unable to register BQ27200 driver\n");
549 static void __exit
bq27x00_battery_exit(void)
551 #ifdef CONFIG_BATTERY_BQ27000
552 platform_driver_unregister(&bq27000_battery_driver
);
554 #ifdef CONFIG_BATTERY_BQ27200
555 i2c_del_driver(&bq27200_battery_driver
);
559 module_init(bq27x00_battery_init
);
560 module_exit(bq27x00_battery_exit
);
562 MODULE_AUTHOR("Texas Instruments");
563 MODULE_DESCRIPTION("BQ27x00 battery moniter driver");
564 MODULE_LICENSE("GPL");