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 #if CONFIG_NO_BATTERY_IC
113 ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
115 dev_err(di->dev, "error reading temperature\n");
118 temp = get_unaligned_le16(buf);
120 DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
125 * Return the battery Voltage in milivolts
126 * Or < 0 if something fails.
128 static int bq27510_battery_voltage(struct bq27510_device_info *di)
133 #if CONFIG_NO_BATTERY_IC
136 ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2);
138 dev_err(di->dev, "error reading voltage\n");
141 volt = get_unaligned_le16(buf);
143 DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
148 * Return the battery average current
149 * Note that current can be negative signed as well
150 * Or 0 if something fails.
152 static int bq27510_battery_current(struct bq27510_device_info *di)
157 #if CONFIG_NO_BATTERY_IC
160 ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
162 dev_err(di->dev, "error reading current\n");
166 curr = get_unaligned_le16(buf);
168 curr = 0xFFFF^(curr-1);
171 DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
176 * Return the battery Relative State-of-Charge
177 * Or < 0 if something fails.
179 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
184 int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
187 #if CONFIG_NO_BATTERY_IC
190 ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2);
192 dev_err(di->dev, "error reading relative State-of-Charge\n");
195 rsoc = get_unaligned_le16(buf);
196 DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
197 #if CONFIG_NO_BATTERY_IC
201 ret = bq27510_read(di->client,0x0c,buf,2);
202 nvcap = get_unaligned_le16(buf);
203 DBG("Enter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
204 ret = bq27510_read(di->client,0x0e,buf,2);
205 facap = get_unaligned_le16(buf);
206 DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
207 ret = bq27510_read(di->client,0x10,buf,2);
208 remcap = get_unaligned_le16(buf);
209 DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
210 ret = bq27510_read(di->client,0x12,buf,2);
211 fccap = get_unaligned_le16(buf);
212 DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
213 ret = bq27510_read(di->client,0x3c,buf,2);
214 full = get_unaligned_le16(buf);
215 DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,full);
216 ret = bq27510_read(di->client,0x00,buf,2);
217 cnt = get_unaligned_le16(buf);
218 DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,cnt);
223 static int bq27510_battery_status(struct bq27510_device_info *di,
224 union power_supply_propval *val)
230 #if CONFIG_NO_BATTERY_IC
231 val->intval = POWER_SUPPLY_STATUS_FULL;
234 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
236 dev_err(di->dev, "error reading flags\n");
239 flags = get_unaligned_le16(buf);
240 if (flags & BQ27500_FLAG_FC)
241 status = POWER_SUPPLY_STATUS_FULL;
242 else if (flags & BQ27500_FLAG_DSC)
243 status = POWER_SUPPLY_STATUS_DISCHARGING;
245 status = POWER_SUPPLY_STATUS_CHARGING;
247 val->intval = status;
252 * Read a time register.
253 * Return < 0 if something fails.
255 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
256 union power_supply_propval *val)
262 ret = bq27510_read(di->client,reg,buf,2);
264 dev_err(di->dev, "error reading register %02x\n", reg);
267 tval = get_unaligned_le16(buf);
268 DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
272 val->intval = tval * 60;
273 DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
277 #define to_bq27510_device_info(x) container_of((x), \
278 struct bq27510_device_info, bat);
280 static int bq27510_battery_get_property(struct power_supply *psy,
281 enum power_supply_property psp,
282 union power_supply_propval *val)
285 struct bq27510_device_info *di = to_bq27510_device_info(psy);
288 case POWER_SUPPLY_PROP_STATUS:
289 ret = bq27510_battery_status(di, val);
291 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
292 case POWER_SUPPLY_PROP_PRESENT:
293 val->intval = bq27510_battery_voltage(di);
294 if (psp == POWER_SUPPLY_PROP_PRESENT)
295 val->intval = val->intval <= 0 ? 0 : 1;
297 case POWER_SUPPLY_PROP_CURRENT_NOW:
298 val->intval = bq27510_battery_current(di);
300 case POWER_SUPPLY_PROP_CAPACITY:
301 val->intval = bq27510_battery_rsoc(di);
303 case POWER_SUPPLY_PROP_TEMP:
304 val->intval = bq27510_battery_temperature(di);
306 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
307 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
309 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
310 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
312 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
313 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
322 static int rk29_ac_get_property(struct power_supply *psy,
323 enum power_supply_property psp,
324 union power_supply_propval *val)
328 case POWER_SUPPLY_PROP_ONLINE:
329 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
330 if(gpio_get_value(DC_CHECK_PIN))
335 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
345 static void bq27510_powersupply_init(struct bq27510_device_info *di)
347 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
348 di->bat.properties = bq27510_battery_props;
349 di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
350 di->bat.get_property = bq27510_battery_get_property;
352 /*di->ac.type = POWER_SUPPLY_TYPE_MAINS;
353 di->ac.properties = rk29_ac_props;
354 di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
355 di->ac.get_property = rk29_ac_get_property;*/
359 static void bq27510_battery_update_status(struct bq27510_device_info *di)
361 power_supply_changed(&di->bat);
364 static void bq27510_battery_work(struct work_struct *work)
366 struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work);
367 bq27510_battery_update_status(di);
368 /* reschedule for the next time */
369 schedule_delayed_work(&di->work, di->interval);
372 static int bq27510_battery_probe(struct i2c_client *client,
373 const struct i2c_device_id *id)
375 struct bq27510_device_info *di;
378 #ifdef CONFIG_CHECK_BATT_CAPACITY
382 di = kzalloc(sizeof(*di), GFP_KERNEL);
384 dev_err(&client->dev, "failed to allocate device info data\n");
388 i2c_set_clientdata(client, di);
389 di->dev = &client->dev;
390 di->bat.name = "bq27510-battery";
392 /* 4 seconds between monotor runs interval */
393 di->interval = msecs_to_jiffies(4 * 1000);
395 gpio_request(DC_CHECK_PIN,"dc_check");
396 gpio_direction_input(DC_CHECK_PIN);
397 bq27510_powersupply_init(di);
398 #ifdef CONFIG_CHECK_BATT_CAPACITY
401 bq27510_write(di->client,0x00,buf,2);
404 bq27510_write(di->client,0x00,buf,2);
406 retval = power_supply_register(&client->dev, &di->bat);
408 dev_err(&client->dev, "failed to register battery\n");
411 INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
412 schedule_delayed_work(&di->work, di->interval);
413 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
423 static int bq27510_battery_remove(struct i2c_client *client)
425 struct bq27510_device_info *di = i2c_get_clientdata(client);
427 power_supply_unregister(&di->bat);
437 static const struct i2c_device_id bq27510_id[] = {
441 static struct i2c_driver bq27510_battery_driver = {
443 .name = "bq27510-battery",
445 .probe = bq27510_battery_probe,
446 .remove = bq27510_battery_remove,
447 .id_table = bq27510_id,
450 static int __init bq27510_battery_init(void)
454 ret = i2c_add_driver(&bq27510_battery_driver);
456 printk(KERN_ERR "Unable to register BQ27510 driver\n");
459 module_init(bq27510_battery_init);
461 static void __exit bq27510_battery_exit(void)
463 i2c_del_driver(&bq27510_battery_driver);
465 module_exit(bq27510_battery_exit);
467 MODULE_AUTHOR("Rockchip");
468 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
469 MODULE_LICENSE("GPL");