2 * BQ27510 battery driver
4 * This package is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 #include <linux/module.h>
14 #include <linux/param.h>
15 #include <linux/jiffies.h>
16 #include <linux/workqueue.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/idr.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <asm/unaligned.h>
24 #include <mach/gpio.h>
26 #define DRIVER_VERSION "1.1.0"
28 #define BQ27x00_REG_TEMP 0x06
29 #define BQ27x00_REG_VOLT 0x08
30 #define BQ27x00_REG_AI 0x14
31 #define BQ27x00_REG_FLAGS 0x0A
32 #define BQ27x00_REG_TTE 0x16
33 #define BQ27x00_REG_TTF 0x18
34 #define BQ27x00_REG_TTECP 0x26
36 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
37 #define BQ27000_FLAG_CHGS BIT(7)
39 #define BQ27500_REG_SOC 0x2c
40 #define BQ27500_FLAG_DSC BIT(0)
41 #define BQ27500_FLAG_FC BIT(9)
43 #define BQ27510_SPEED 300 * 1000
45 #define DC_CHECK_PIN RK29_PIN4_PA1
48 #define DBG(x...) printk(KERN_INFO x)
50 #define DBG(x...) do { } while (0)
53 /* If the system has several batteries we need a different name for each
56 static DEFINE_MUTEX(battery_mutex);
58 struct bq27510_device_info {
60 struct power_supply bat;
61 struct power_supply ac;
62 struct delayed_work work;
63 unsigned int interval;
64 struct i2c_client *client;
67 static enum power_supply_property bq27510_battery_props[] = {
68 POWER_SUPPLY_PROP_STATUS,
69 POWER_SUPPLY_PROP_PRESENT,
70 POWER_SUPPLY_PROP_VOLTAGE_NOW,
71 POWER_SUPPLY_PROP_CURRENT_NOW,
72 POWER_SUPPLY_PROP_CAPACITY,
73 POWER_SUPPLY_PROP_TEMP,
74 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
75 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
76 //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
79 static enum power_supply_property rk29_ac_props[] = {
80 POWER_SUPPLY_PROP_ONLINE,
85 * Common code for BQ27510 devices read
87 static int bq27510_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
90 ret = i2c_master_reg8_recv(client, reg, buf, len, BQ27510_SPEED);
93 #ifdef CONFIG_CHECK_BATT_CAPACITY
94 static int bq27510_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
97 ret = i2c_master_reg8_send(client, reg, buf, (int)len, BQ27510_SPEED);
102 * Return the battery temperature in tenths of degree Celsius
103 * Or < 0 if something fails.
105 static int bq27510_battery_temperature(struct bq27510_device_info *di)
110 ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
112 dev_err(di->dev, "error reading temperature\n");
115 temp = get_unaligned_le16(buf);
117 DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
122 * Return the battery Voltage in milivolts
123 * Or < 0 if something fails.
125 static int bq27510_battery_voltage(struct bq27510_device_info *di)
131 ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2);
133 dev_err(di->dev, "error reading voltage\n");
136 volt = get_unaligned_le16(buf);
138 DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
143 * Return the battery average current
144 * Note that current can be negative signed as well
145 * Or 0 if something fails.
147 static int bq27510_battery_current(struct bq27510_device_info *di)
153 ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
155 dev_err(di->dev, "error reading current\n");
159 curr = get_unaligned_le16(buf);
161 curr = 0xFFFF^(curr-1);
164 DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
169 * Return the battery Relative State-of-Charge
170 * Or < 0 if something fails.
172 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
177 int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
181 ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2);
183 dev_err(di->dev, "error reading relative State-of-Charge\n");
186 rsoc = get_unaligned_le16(buf);
187 DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
188 #if CONFIG_NO_BATTERY_IC
192 ret = bq27510_read(di->client,0x0c,buf,2);
193 nvcap = get_unaligned_le16(buf);
194 DBG("Enter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
195 ret = bq27510_read(di->client,0x0e,buf,2);
196 facap = get_unaligned_le16(buf);
197 DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
198 ret = bq27510_read(di->client,0x10,buf,2);
199 remcap = get_unaligned_le16(buf);
200 DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
201 ret = bq27510_read(di->client,0x12,buf,2);
202 fccap = get_unaligned_le16(buf);
203 DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
204 ret = bq27510_read(di->client,0x3c,buf,2);
205 full = get_unaligned_le16(buf);
206 DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,full);
207 ret = bq27510_read(di->client,0x00,buf,2);
208 cnt = get_unaligned_le16(buf);
209 DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,cnt);
214 static int bq27510_battery_status(struct bq27510_device_info *di,
215 union power_supply_propval *val)
222 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
224 dev_err(di->dev, "error reading flags\n");
227 flags = get_unaligned_le16(buf);
228 if (flags & BQ27500_FLAG_FC)
229 status = POWER_SUPPLY_STATUS_FULL;
230 else if (flags & BQ27500_FLAG_DSC)
231 status = POWER_SUPPLY_STATUS_DISCHARGING;
233 status = POWER_SUPPLY_STATUS_CHARGING;
235 val->intval = status;
240 * Read a time register.
241 * Return < 0 if something fails.
243 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
244 union power_supply_propval *val)
250 ret = bq27510_read(di->client,reg,buf,2);
252 dev_err(di->dev, "error reading register %02x\n", reg);
255 tval = get_unaligned_le16(buf);
256 DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
260 val->intval = tval * 60;
261 DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
265 #define to_bq27510_device_info(x) container_of((x), \
266 struct bq27510_device_info, bat);
268 static int bq27510_battery_get_property(struct power_supply *psy,
269 enum power_supply_property psp,
270 union power_supply_propval *val)
273 struct bq27510_device_info *di = to_bq27510_device_info(psy);
276 case POWER_SUPPLY_PROP_STATUS:
277 ret = bq27510_battery_status(di, val);
279 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
280 case POWER_SUPPLY_PROP_PRESENT:
281 val->intval = bq27510_battery_voltage(di);
282 if (psp == POWER_SUPPLY_PROP_PRESENT)
283 val->intval = val->intval <= 0 ? 0 : 1;
285 case POWER_SUPPLY_PROP_CURRENT_NOW:
286 val->intval = bq27510_battery_current(di);
288 case POWER_SUPPLY_PROP_CAPACITY:
289 val->intval = bq27510_battery_rsoc(di);
291 case POWER_SUPPLY_PROP_TEMP:
292 val->intval = bq27510_battery_temperature(di);
294 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
295 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
297 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
298 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
300 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
301 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
310 static int rk29_ac_get_property(struct power_supply *psy,
311 enum power_supply_property psp,
312 union power_supply_propval *val)
316 case POWER_SUPPLY_PROP_ONLINE:
317 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
318 if(gpio_get_value(DC_CHECK_PIN))
323 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
333 static void bq27510_powersupply_init(struct bq27510_device_info *di)
335 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
336 di->bat.properties = bq27510_battery_props;
337 di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
338 di->bat.get_property = bq27510_battery_get_property;
340 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
341 di->ac.properties = rk29_ac_props;
342 di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
343 di->ac.get_property = rk29_ac_get_property;
347 static void bq27510_battery_update_status(struct bq27510_device_info *di)
349 power_supply_changed(&di->bat);
352 static void bq27510_battery_work(struct work_struct *work)
354 struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work);
355 bq27510_battery_update_status(di);
356 /* reschedule for the next time */
357 schedule_delayed_work(&di->work, di->interval);
360 static int bq27510_battery_probe(struct i2c_client *client,
361 const struct i2c_device_id *id)
363 struct bq27510_device_info *di;
366 #ifdef CONFIG_CHECK_BATT_CAPACITY
370 di = kzalloc(sizeof(*di), GFP_KERNEL);
372 dev_err(&client->dev, "failed to allocate device info data\n");
376 i2c_set_clientdata(client, di);
377 di->dev = &client->dev;
378 di->bat.name = "bq27510-battery";
380 /* 4 seconds between monotor runs interval */
381 di->interval = msecs_to_jiffies(4 * 1000);
383 gpio_request(DC_CHECK_PIN,"dc_check");
384 gpio_direction_input(DC_CHECK_PIN);
385 bq27510_powersupply_init(di);
386 #ifdef CONFIG_CHECK_BATT_CAPACITY
389 bq27510_write(di->client,0x00,buf,2);
392 bq27510_write(di->client,0x00,buf,2);
394 retval = power_supply_register(&client->dev, &di->bat);
396 dev_err(&client->dev, "failed to register battery\n");
399 INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
400 schedule_delayed_work(&di->work, di->interval);
401 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
411 static int bq27510_battery_remove(struct i2c_client *client)
413 struct bq27510_device_info *di = i2c_get_clientdata(client);
415 power_supply_unregister(&di->bat);
425 static const struct i2c_device_id bq27510_id[] = {
429 static struct i2c_driver bq27510_battery_driver = {
431 .name = "bq27510-battery",
433 .probe = bq27510_battery_probe,
434 .remove = bq27510_battery_remove,
435 .id_table = bq27510_id,
438 static int __init bq27510_battery_init(void)
442 ret = i2c_add_driver(&bq27510_battery_driver);
444 printk(KERN_ERR "Unable to register BQ27510 driver\n");
447 module_init(bq27510_battery_init);
449 static void __exit bq27510_battery_exit(void)
451 i2c_del_driver(&bq27510_battery_driver);
453 module_exit(bq27510_battery_exit);
455 MODULE_AUTHOR("Rockchip");
456 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
457 MODULE_LICENSE("GPL");