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>
25 #include <linux/proc_fs.h>
26 #include <asm/uaccess.h>
27 #include <mach/board.h>
30 #define DRIVER_VERSION "1.1.0"
31 #define BQ27x00_REG_TEMP 0x06
32 #define BQ27x00_REG_VOLT 0x08
33 #define BQ27x00_REG_AI 0x14
34 #define BQ27x00_REG_FLAGS 0x0A
35 #define BQ27x00_REG_TTE 0x16
36 #define BQ27x00_REG_TTF 0x18
37 #define BQ27x00_REG_TTECP 0x26
38 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
39 #define BQ27500_REG_SOC 0x2c
41 #define BQ27500_FLAG_DSC BIT(0)
42 #define BQ27000_FLAG_CHGS BIT(8)
43 #define BQ27500_FLAG_FC BIT(9)
44 #define BQ27500_FLAG_OTD BIT(14)
45 #define BQ27500_FLAG_OTC BIT(15)
47 #define BQ27510_SPEED 300 * 1000
48 int virtual_battery_enable = 0;
49 extern int dwc_vbus_status(void);
52 #define DBG(x...) printk(KERN_INFO x)
54 #define DBG(x...) do { } while (0)
57 /* If the system has several batteries we need a different name for each
60 static DEFINE_MUTEX(battery_mutex);
62 struct bq27510_device_info {
64 struct power_supply bat;
65 struct power_supply ac;
66 struct delayed_work work;
67 struct i2c_client *client;
68 unsigned int interval;
69 unsigned int dc_check_pin;
73 static enum power_supply_property bq27510_battery_props[] = {
74 POWER_SUPPLY_PROP_STATUS,
75 POWER_SUPPLY_PROP_PRESENT,
76 POWER_SUPPLY_PROP_VOLTAGE_NOW,
77 POWER_SUPPLY_PROP_CURRENT_NOW,
78 POWER_SUPPLY_PROP_CAPACITY,
79 POWER_SUPPLY_PROP_TEMP,
80 POWER_SUPPLY_PROP_TECHNOLOGY,
81 POWER_SUPPLY_PROP_HEALTH,
82 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
83 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
84 //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
87 static enum power_supply_property rk29_ac_props[] = {
88 POWER_SUPPLY_PROP_ONLINE,
91 static ssize_t battery_proc_write(struct file *file,const char __user *buffer,
92 unsigned long count,void *data)
97 printk("echo x >/proc/driver/power\n");
98 printk("x=1,means just print log ||x=2,means log and data ||x= other,means close log\n");
100 rc = get_user(c,buffer);
104 virtual_battery_enable = 1;
106 virtual_battery_enable = 2;
108 virtual_battery_enable = 3;
110 virtual_battery_enable = 0;
111 printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
115 static const struct file_operations battery_proc_fops = {
116 .owner = THIS_MODULE,
117 .write = battery_proc_write,
121 * Common code for BQ27510 devices read
123 static int bq27510_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
126 ret = i2c_master_reg8_recv(client, reg, buf, len, BQ27510_SPEED);
129 #ifdef CONFIG_CHECK_BATT_CAPACITY
130 static int bq27510_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
133 ret = i2c_master_reg8_send(client, reg, buf, (int)len, BQ27510_SPEED);
138 * Return the battery temperature in tenths of degree Celsius
139 * Or < 0 if something fails.
141 static int bq27510_battery_temperature(struct bq27510_device_info *di)
146 if(virtual_battery_enable == 1)
148 ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
150 dev_err(di->dev, "error reading temperature\n");
153 temp = get_unaligned_le16(buf);
155 DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
160 * Return the battery Voltage in milivolts
161 * Or < 0 if something fails.
163 static int bq27510_battery_voltage(struct bq27510_device_info *di)
168 if(virtual_battery_enable == 1)
169 return 2000000/*4000000*/;
171 ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2);
173 dev_err(di->dev, "error reading voltage\n");
176 volt = get_unaligned_le16(buf);
178 //bp27510 can only measure one li-lion bat
179 if(di->bat_num == 2){
180 volt = volt * 1000 * 2;
185 DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
190 * Return the battery average current
191 * Note that current can be negative signed as well
192 * Or 0 if something fails.
194 static int bq27510_battery_current(struct bq27510_device_info *di)
199 if(virtual_battery_enable == 1)
200 return 11000/*22000*/;
201 ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
203 dev_err(di->dev, "error reading current\n");
207 curr = get_unaligned_le16(buf);
209 curr = 0xFFFF^(curr-1);
212 DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
217 * Return the battery Relative State-of-Charge
218 * Or < 0 if something fails.
220 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
225 int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
228 if(virtual_battery_enable == 1)
231 ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2);
233 dev_err(di->dev, "error reading relative State-of-Charge\n");
236 rsoc = get_unaligned_le16(buf);
237 DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
239 ret = bq27510_read(di->client,0x0c,buf,2);
240 nvcap = get_unaligned_le16(buf);
241 DBG("Enter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
242 ret = bq27510_read(di->client,0x0e,buf,2);
243 facap = get_unaligned_le16(buf);
244 DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
245 ret = bq27510_read(di->client,0x10,buf,2);
246 remcap = get_unaligned_le16(buf);
247 DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
248 ret = bq27510_read(di->client,0x12,buf,2);
249 fccap = get_unaligned_le16(buf);
250 DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
251 ret = bq27510_read(di->client,0x3c,buf,2);
252 full = get_unaligned_le16(buf);
253 DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,full);
254 ret = bq27510_read(di->client,0x00,buf,2);
255 cnt = get_unaligned_le16(buf);
256 DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,cnt);
261 static int bq27510_battery_status(struct bq27510_device_info *di,
262 union power_supply_propval *val)
268 if(virtual_battery_enable == 1)
270 val->intval = POWER_SUPPLY_STATUS_FULL;
273 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
275 dev_err(di->dev, "error reading flags\n");
278 flags = get_unaligned_le16(buf);
279 DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
280 if (flags & BQ27500_FLAG_FC)
281 status = POWER_SUPPLY_STATUS_FULL;
282 else if (flags & BQ27500_FLAG_DSC)
283 status = POWER_SUPPLY_STATUS_DISCHARGING;
285 status = POWER_SUPPLY_STATUS_CHARGING;
287 val->intval = status;
291 static int bq27510_health_status(struct bq27510_device_info *di,
292 union power_supply_propval *val)
298 if(virtual_battery_enable == 1)
300 val->intval = POWER_SUPPLY_HEALTH_GOOD;
303 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
305 dev_err(di->dev, "error reading flags\n");
308 flags = get_unaligned_le16(buf);
309 DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
310 if ((flags & BQ27500_FLAG_OTD)||(flags & BQ27500_FLAG_OTC))
311 status = POWER_SUPPLY_HEALTH_OVERHEAT;
313 status = POWER_SUPPLY_HEALTH_GOOD;
315 val->intval = status;
321 * Read a time register.
322 * Return < 0 if something fails.
324 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
325 union power_supply_propval *val)
331 ret = bq27510_read(di->client,reg,buf,2);
333 dev_err(di->dev, "error reading register %02x\n", reg);
336 tval = get_unaligned_le16(buf);
337 DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
341 val->intval = tval * 60;
342 DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
346 #define to_bq27510_device_info(x) container_of((x), \
347 struct bq27510_device_info, bat);
349 static int bq27510_battery_get_property(struct power_supply *psy,
350 enum power_supply_property psp,
351 union power_supply_propval *val)
354 struct bq27510_device_info *di = to_bq27510_device_info(psy);
355 DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
359 case POWER_SUPPLY_PROP_STATUS:
360 ret = bq27510_battery_status(di, val);
362 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
363 case POWER_SUPPLY_PROP_PRESENT:
364 val->intval = bq27510_battery_voltage(di);
365 if (psp == POWER_SUPPLY_PROP_PRESENT)
366 val->intval = val->intval <= 0 ? 0 : 1;
368 case POWER_SUPPLY_PROP_CURRENT_NOW:
369 val->intval = bq27510_battery_current(di);
371 case POWER_SUPPLY_PROP_CAPACITY:
372 val->intval = bq27510_battery_rsoc(di);
374 case POWER_SUPPLY_PROP_TEMP:
375 val->intval = bq27510_battery_temperature(di);
377 case POWER_SUPPLY_PROP_TECHNOLOGY:
378 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
380 case POWER_SUPPLY_PROP_HEALTH:
381 ret = bq27510_health_status(di, val);
383 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
384 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
386 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
387 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
389 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
390 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
399 static int rk29_ac_get_property(struct power_supply *psy,
400 enum power_supply_property psp,
401 union power_supply_propval *val)
404 struct bq27510_device_info *di = container_of(psy, struct bq27510_device_info, ac);
407 case POWER_SUPPLY_PROP_ONLINE:
408 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
409 if(gpio_get_value(di->dc_check_pin))
410 val->intval = 0; /*discharging*/
412 val->intval = 1; /*charging*/
414 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
424 static void bq27510_powersupply_init(struct bq27510_device_info *di)
426 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
427 di->bat.properties = bq27510_battery_props;
428 di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
429 di->bat.get_property = bq27510_battery_get_property;
432 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
433 di->ac.properties = rk29_ac_props;
434 di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
435 di->ac.get_property = rk29_ac_get_property;
439 static void bq27510_battery_update_status(struct bq27510_device_info *di)
441 power_supply_changed(&di->bat);
444 static void bq27510_battery_work(struct work_struct *work)
446 struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work);
447 bq27510_battery_update_status(di);
448 /* reschedule for the next time */
449 schedule_delayed_work(&di->work, di->interval);
452 static int bq27510_battery_probe(struct i2c_client *client,
453 const struct i2c_device_id *id)
455 struct bq27510_device_info *di;
457 struct bq27510_platform_data *pdata;
459 #ifdef CONFIG_CHECK_BATT_CAPACITY
463 pdata = client->dev.platform_data;
465 di = kzalloc(sizeof(*di), GFP_KERNEL);
467 dev_err(&client->dev, "failed to allocate device info data\n");
471 i2c_set_clientdata(client, di);
472 di->dev = &client->dev;
473 di->bat.name = "bq27510-battery";
475 /* 4 seconds between monotor runs interval */
476 di->interval = msecs_to_jiffies(4 * 1000);
478 di->bat_num = pdata->bat_num;
479 di->dc_check_pin = pdata->dc_check_pin;
481 if (pdata->init_dc_check_pin)
482 pdata->init_dc_check_pin( );
484 bq27510_powersupply_init(di);
485 #ifdef CONFIG_CHECK_BATT_CAPACITY
488 bq27510_write(di->client,0x00,buf,2);
491 bq27510_write(di->client,0x00,buf,2);
493 retval = power_supply_register(&client->dev, &di->bat);
495 dev_err(&client->dev, "failed to register battery\n");
498 retval = power_supply_register(&client->dev, &di->ac);
500 dev_err(&client->dev, "failed to register ac\n");
504 INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
505 schedule_delayed_work(&di->work, di->interval);
506 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
515 static int bq27510_battery_remove(struct i2c_client *client)
517 struct bq27510_device_info *di = i2c_get_clientdata(client);
519 power_supply_unregister(&di->bat);
525 static const struct i2c_device_id bq27510_id[] = {
529 static struct i2c_driver bq27510_battery_driver = {
531 .name = "bq27510-battery",
533 .probe = bq27510_battery_probe,
534 .remove = bq27510_battery_remove,
535 .id_table = bq27510_id,
538 static int __init bq27510_battery_init(void)
541 struct proc_dir_entry * battery_proc_entry;
543 ret = i2c_add_driver(&bq27510_battery_driver);
545 printk(KERN_ERR "Unable to register BQ27510 driver\n");
547 battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
550 module_init(bq27510_battery_init);
552 static void __exit bq27510_battery_exit(void)
554 i2c_del_driver(&bq27510_battery_driver);
556 module_exit(bq27510_battery_exit);
558 MODULE_AUTHOR("Rockchip");
559 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
560 MODULE_LICENSE("GPL");