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 #if CONFIG_NO_BATTERY_IC
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)
134 ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2);
136 dev_err(di->dev, "error reading voltage\n");
139 volt = get_unaligned_le16(buf);
141 DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
146 * Return the battery average current
147 * Note that current can be negative signed as well
148 * Or 0 if something fails.
150 static int bq27510_battery_current(struct bq27510_device_info *di)
156 ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
158 dev_err(di->dev, "error reading current\n");
162 curr = get_unaligned_le16(buf);
164 curr = 0xFFFF^(curr-1);
167 DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
172 * Return the battery Relative State-of-Charge
173 * Or < 0 if something fails.
175 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
180 int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
184 ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2);
186 dev_err(di->dev, "error reading relative State-of-Charge\n");
189 rsoc = get_unaligned_le16(buf);
190 DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
191 #if CONFIG_NO_BATTERY_IC
195 ret = bq27510_read(di->client,0x0c,buf,2);
196 nvcap = get_unaligned_le16(buf);
197 DBG("Enter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
198 ret = bq27510_read(di->client,0x0e,buf,2);
199 facap = get_unaligned_le16(buf);
200 DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
201 ret = bq27510_read(di->client,0x10,buf,2);
202 remcap = get_unaligned_le16(buf);
203 DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
204 ret = bq27510_read(di->client,0x12,buf,2);
205 fccap = get_unaligned_le16(buf);
206 DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
207 ret = bq27510_read(di->client,0x3c,buf,2);
208 full = get_unaligned_le16(buf);
209 DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,full);
210 ret = bq27510_read(di->client,0x00,buf,2);
211 cnt = get_unaligned_le16(buf);
212 DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,cnt);
217 static int bq27510_battery_status(struct bq27510_device_info *di,
218 union power_supply_propval *val)
225 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
227 dev_err(di->dev, "error reading flags\n");
230 flags = get_unaligned_le16(buf);
231 if (flags & BQ27500_FLAG_FC)
232 status = POWER_SUPPLY_STATUS_FULL;
233 else if (flags & BQ27500_FLAG_DSC)
234 status = POWER_SUPPLY_STATUS_DISCHARGING;
236 status = POWER_SUPPLY_STATUS_CHARGING;
238 val->intval = status;
243 * Read a time register.
244 * Return < 0 if something fails.
246 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
247 union power_supply_propval *val)
253 ret = bq27510_read(di->client,reg,buf,2);
255 dev_err(di->dev, "error reading register %02x\n", reg);
258 tval = get_unaligned_le16(buf);
259 DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
263 val->intval = tval * 60;
264 DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
268 #define to_bq27510_device_info(x) container_of((x), \
269 struct bq27510_device_info, bat);
271 static int bq27510_battery_get_property(struct power_supply *psy,
272 enum power_supply_property psp,
273 union power_supply_propval *val)
276 struct bq27510_device_info *di = to_bq27510_device_info(psy);
279 case POWER_SUPPLY_PROP_STATUS:
280 ret = bq27510_battery_status(di, val);
282 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
283 case POWER_SUPPLY_PROP_PRESENT:
284 val->intval = bq27510_battery_voltage(di);
285 if (psp == POWER_SUPPLY_PROP_PRESENT)
286 val->intval = val->intval <= 0 ? 0 : 1;
288 case POWER_SUPPLY_PROP_CURRENT_NOW:
289 val->intval = bq27510_battery_current(di);
291 case POWER_SUPPLY_PROP_CAPACITY:
292 val->intval = bq27510_battery_rsoc(di);
294 case POWER_SUPPLY_PROP_TEMP:
295 val->intval = bq27510_battery_temperature(di);
297 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
298 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
300 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
301 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
303 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
304 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
313 static int rk29_ac_get_property(struct power_supply *psy,
314 enum power_supply_property psp,
315 union power_supply_propval *val)
319 case POWER_SUPPLY_PROP_ONLINE:
320 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
321 if(gpio_get_value(DC_CHECK_PIN))
326 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
336 static void bq27510_powersupply_init(struct bq27510_device_info *di)
338 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
339 di->bat.properties = bq27510_battery_props;
340 di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
341 di->bat.get_property = bq27510_battery_get_property;
343 /*di->ac.type = POWER_SUPPLY_TYPE_MAINS;
344 di->ac.properties = rk29_ac_props;
345 di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
346 di->ac.get_property = rk29_ac_get_property;*/
350 static void bq27510_battery_update_status(struct bq27510_device_info *di)
352 power_supply_changed(&di->bat);
355 static void bq27510_battery_work(struct work_struct *work)
357 struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work);
358 bq27510_battery_update_status(di);
359 /* reschedule for the next time */
360 schedule_delayed_work(&di->work, di->interval);
363 static int bq27510_battery_probe(struct i2c_client *client,
364 const struct i2c_device_id *id)
366 struct bq27510_device_info *di;
369 #ifdef CONFIG_CHECK_BATT_CAPACITY
373 di = kzalloc(sizeof(*di), GFP_KERNEL);
375 dev_err(&client->dev, "failed to allocate device info data\n");
379 i2c_set_clientdata(client, di);
380 di->dev = &client->dev;
381 di->bat.name = "bq27510-battery";
383 /* 4 seconds between monotor runs interval */
384 di->interval = msecs_to_jiffies(4 * 1000);
386 gpio_request(DC_CHECK_PIN,"dc_check");
387 gpio_direction_input(DC_CHECK_PIN);
388 bq27510_powersupply_init(di);
389 #ifdef CONFIG_CHECK_BATT_CAPACITY
392 bq27510_write(di->client,0x00,buf,2);
395 bq27510_write(di->client,0x00,buf,2);
397 retval = power_supply_register(&client->dev, &di->bat);
399 dev_err(&client->dev, "failed to register battery\n");
402 INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
403 schedule_delayed_work(&di->work, di->interval);
404 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
414 static int bq27510_battery_remove(struct i2c_client *client)
416 struct bq27510_device_info *di = i2c_get_clientdata(client);
418 power_supply_unregister(&di->bat);
428 static const struct i2c_device_id bq27510_id[] = {
432 static struct i2c_driver bq27510_battery_driver = {
434 .name = "bq27510-battery",
436 .probe = bq27510_battery_probe,
437 .remove = bq27510_battery_remove,
438 .id_table = bq27510_id,
441 static int __init bq27510_battery_init(void)
445 ret = i2c_add_driver(&bq27510_battery_driver);
447 printk(KERN_ERR "Unable to register BQ27510 driver\n");
450 module_init(bq27510_battery_init);
452 static void __exit bq27510_battery_exit(void)
454 i2c_del_driver(&bq27510_battery_driver);
456 module_exit(bq27510_battery_exit);
458 MODULE_AUTHOR("Rockchip");
459 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
460 MODULE_LICENSE("GPL");