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);
50 static void bq27510_set(void);
53 #define DBG(x...) printk(KERN_INFO x)
55 #define DBG(x...) do { } while (0)
58 /* If the system has several batteries we need a different name for each
61 static DEFINE_MUTEX(battery_mutex);
63 struct bq27510_device_info {
65 struct power_supply bat;
66 struct power_supply ac;
67 struct delayed_work work;
68 struct i2c_client *client;
69 unsigned int interval;
70 unsigned int dc_check_pin;
74 static struct bq27510_device_info *bq27510_di;
75 static enum power_supply_property bq27510_battery_props[] = {
76 POWER_SUPPLY_PROP_STATUS,
77 POWER_SUPPLY_PROP_PRESENT,
78 POWER_SUPPLY_PROP_VOLTAGE_NOW,
79 POWER_SUPPLY_PROP_CURRENT_NOW,
80 POWER_SUPPLY_PROP_CAPACITY,
81 POWER_SUPPLY_PROP_TEMP,
82 POWER_SUPPLY_PROP_TECHNOLOGY,
83 POWER_SUPPLY_PROP_HEALTH,
84 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
85 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
86 //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
89 static enum power_supply_property rk29_ac_props[] = {
90 POWER_SUPPLY_PROP_ONLINE,
93 static ssize_t battery_proc_write(struct file *file,const char __user *buffer,
94 size_t count, loff_t *ppos)
99 printk("echo x >/proc/driver/power\n");
100 printk("x=1,means just print log ||x=2,means log and data ||x= other,means close log\n");
102 rc = get_user(c,buffer);
106 //added by zwp,c='8' means check whether we need to download firmware to bq27xxx,return 0 means yes.
108 printk("%s,bq27510 need to download firmware\n",__FUNCTION__);
112 virtual_battery_enable = 1;
114 virtual_battery_enable = 2;
116 virtual_battery_enable = 3;
118 printk("%s:%d>>bq27510 set\n",__FUNCTION__,__LINE__);
122 virtual_battery_enable = 0;
123 printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
127 static const struct file_operations battery_proc_fops = {
128 .owner = THIS_MODULE,
129 .write = battery_proc_write,
133 * Common code for BQ27510 devices read
135 static int bq27510_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
138 ret = i2c_master_reg8_recv(client, reg, buf, len, BQ27510_SPEED);
142 static int bq27510_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
145 ret = i2c_master_reg8_send(client, reg, buf, (int)len, BQ27510_SPEED);
150 * Return the battery temperature in tenths of degree Celsius
151 * Or < 0 if something fails.
153 static int bq27510_battery_temperature(struct bq27510_device_info *di)
159 #if defined (CONFIG_NO_BATTERY_IC)
163 if(virtual_battery_enable == 1)
165 ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
167 dev_err(di->dev, "error reading temperature\n");
170 temp = get_unaligned_le16(buf);
172 DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
177 * Return the battery Voltage in milivolts
178 * Or < 0 if something fails.
180 static int bq27510_battery_voltage(struct bq27510_device_info *di)
186 #if defined (CONFIG_NO_BATTERY_IC)
189 if(virtual_battery_enable == 1)
190 return 2000000/*4000000*/;
192 ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2);
194 dev_err(di->dev, "error reading voltage\n");
197 volt = get_unaligned_le16(buf);
199 //bp27510 can only measure one li-lion bat
200 if(di->bat_num == 2){
201 volt = volt * 1000 * 2;
206 DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
211 * Return the battery average current
212 * Note that current can be negative signed as well
213 * Or 0 if something fails.
215 static int bq27510_battery_current(struct bq27510_device_info *di)
221 #if defined (CONFIG_NO_BATTERY_IC)
224 if(virtual_battery_enable == 1)
225 return 11000/*22000*/;
226 ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
228 dev_err(di->dev, "error reading current\n");
232 curr = get_unaligned_le16(buf);
233 DBG("curr = %x \n",curr);
235 curr = 0xFFFF^(curr-1);
238 DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
243 * Return the battery Relative State-of-Charge
244 * Or < 0 if something fails.
246 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
251 int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
252 int art = 0, artte = 0, ai = 0, tte = 0, ttf = 0, si = 0;
253 int stte = 0, mli = 0, mltte = 0, ae = 0, ap = 0, ttecp = 0, cc = 0;
257 #if defined (CONFIG_NO_BATTERY_IC)
260 if(virtual_battery_enable == 1)
263 ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2);
265 dev_err(di->dev, "error reading relative State-of-Charge\n");
268 rsoc = get_unaligned_le16(buf);
269 DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
271 #if defined (CONFIG_NO_BATTERY_IC)
274 #if 0 //other register information, for debug use
275 ret = bq27510_read(di->client,0x0c,buf,2); //NominalAvailableCapacity
276 nvcap = get_unaligned_le16(buf);
277 DBG("\nEnter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
278 ret = bq27510_read(di->client,0x0e,buf,2); //FullAvailableCapacity
279 facap = get_unaligned_le16(buf);
280 DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
281 ret = bq27510_read(di->client,0x10,buf,2); //RemainingCapacity
282 remcap = get_unaligned_le16(buf);
283 DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
284 ret = bq27510_read(di->client,0x12,buf,2); //FullChargeCapacity
285 fccap = get_unaligned_le16(buf);
286 DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
287 ret = bq27510_read(di->client,0x3c,buf,2); //DesignCapacity
288 full = get_unaligned_le16(buf);
289 DBG("Enter:%s %d--DesignCapacity = %d\n",__FUNCTION__,__LINE__,full);
291 buf[0] = 0x00; //CONTROL_STATUS
293 bq27510_write(di->client,0x00,buf,2);
294 ret = bq27510_read(di->client,0x00,buf,2);
295 cnt = get_unaligned_le16(buf);
296 DBG("Enter:%s %d--Control status = %x\n",__FUNCTION__,__LINE__,cnt);
298 ret = bq27510_read(di->client,0x02,buf,2); //AtRate
299 art = get_unaligned_le16(buf);
300 DBG("Enter:%s %d--AtRate = %d\n",__FUNCTION__,__LINE__,art);
301 ret = bq27510_read(di->client,0x04,buf,2); //AtRateTimeToEmpty
302 artte = get_unaligned_le16(buf);
303 DBG("Enter:%s %d--AtRateTimeToEmpty = %d\n",__FUNCTION__,__LINE__,artte);
304 ret = bq27510_read(di->client,0x14,buf,2); //AverageCurrent
305 ai = get_unaligned_le16(buf);
306 DBG("Enter:%s %d--AverageCurrent = %d\n",__FUNCTION__,__LINE__,ai);
307 ret = bq27510_read(di->client,0x16,buf,2); //TimeToEmpty
308 tte = get_unaligned_le16(buf);
309 DBG("Enter:%s %d--TimeToEmpty = %d\n",__FUNCTION__,__LINE__,tte);
310 ret = bq27510_read(di->client,0x18,buf,2); //TimeToFull
311 ttf = get_unaligned_le16(buf);
312 DBG("Enter:%s %d--TimeToFull = %d\n",__FUNCTION__,__LINE__,ttf);
313 ret = bq27510_read(di->client,0x1a,buf,2); //StandbyCurrent
314 si = get_unaligned_le16(buf);
315 DBG("Enter:%s %d--StandbyCurrent = %d\n",__FUNCTION__,__LINE__,si);
316 ret = bq27510_read(di->client,0x1c,buf,2); //StandbyTimeToEmpty
317 stte = get_unaligned_le16(buf);
318 DBG("Enter:%s %d--StandbyTimeToEmpty = %d\n",__FUNCTION__,__LINE__,stte);
319 ret = bq27510_read(di->client,0x1e,buf,2); //MaxLoadCurrent
320 mli = get_unaligned_le16(buf);
321 DBG("Enter:%s %d--MaxLoadCurrent = %d\n",__FUNCTION__,__LINE__,mli);
322 ret = bq27510_read(di->client,0x20,buf,2); //MaxLoadTimeToEmpty
323 mltte = get_unaligned_le16(buf);
324 DBG("Enter:%s %d--MaxLoadTimeToEmpty = %d\n",__FUNCTION__,__LINE__,mltte);
325 ret = bq27510_read(di->client,0x22,buf,2); //AvailableEnergy
326 ae = get_unaligned_le16(buf);
327 DBG("Enter:%s %d--AvailableEnergy = %d\n",__FUNCTION__,__LINE__,ae);
328 ret = bq27510_read(di->client,0x24,buf,2); //AveragePower
329 ap = get_unaligned_le16(buf);
330 DBG("Enter:%s %d--AveragePower = %d\n",__FUNCTION__,__LINE__,ap);
331 ret = bq27510_read(di->client,0x26,buf,2); //TTEatConstantPower
332 ttecp = get_unaligned_le16(buf);
333 DBG("Enter:%s %d--TTEatConstantPower = %d\n",__FUNCTION__,__LINE__,ttecp);
334 ret = bq27510_read(di->client,0x2a,buf,2); //CycleCount
335 cc = get_unaligned_le16(buf);
336 DBG("Enter:%s %d--CycleCount = %d\n",__FUNCTION__,__LINE__,cc);
341 static int bq27510_battery_status(struct bq27510_device_info *di,
342 union power_supply_propval *val)
349 #if defined (CONFIG_NO_BATTERY_IC)
350 val->intval = POWER_SUPPLY_STATUS_FULL;
354 if(virtual_battery_enable == 1)
356 val->intval = POWER_SUPPLY_STATUS_FULL;
359 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
361 dev_err(di->dev, "error reading flags\n");
364 flags = get_unaligned_le16(buf);
365 DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
366 if (flags & BQ27500_FLAG_FC)
367 status = POWER_SUPPLY_STATUS_FULL;
368 else if (flags & BQ27500_FLAG_DSC)
369 status = POWER_SUPPLY_STATUS_DISCHARGING;
371 status = POWER_SUPPLY_STATUS_CHARGING;
373 val->intval = status;
377 static int bq27510_health_status(struct bq27510_device_info *di,
378 union power_supply_propval *val)
385 #if defined (CONFIG_NO_BATTERY_IC)
386 val->intval = POWER_SUPPLY_HEALTH_GOOD;
390 if(virtual_battery_enable == 1)
392 val->intval = POWER_SUPPLY_HEALTH_GOOD;
395 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
397 dev_err(di->dev, "error reading flags\n");
400 flags = get_unaligned_le16(buf);
401 DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
402 if ((flags & BQ27500_FLAG_OTD)||(flags & BQ27500_FLAG_OTC))
403 status = POWER_SUPPLY_HEALTH_OVERHEAT;
405 status = POWER_SUPPLY_HEALTH_GOOD;
407 val->intval = status;
413 * Read a time register.
414 * Return < 0 if something fails.
416 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
417 union power_supply_propval *val)
423 ret = bq27510_read(di->client,reg,buf,2);
425 dev_err(di->dev, "error reading register %02x\n", reg);
428 tval = get_unaligned_le16(buf);
429 DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
433 val->intval = tval * 60;
434 DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
438 #define to_bq27510_device_info(x) container_of((x), \
439 struct bq27510_device_info, bat);
441 static int bq27510_battery_get_property(struct power_supply *psy,
442 enum power_supply_property psp,
443 union power_supply_propval *val)
446 struct bq27510_device_info *di = to_bq27510_device_info(psy);
447 DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
451 case POWER_SUPPLY_PROP_STATUS:
452 ret = bq27510_battery_status(di, val);
454 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
455 case POWER_SUPPLY_PROP_PRESENT:
456 val->intval = bq27510_battery_voltage(di);
457 if (psp == POWER_SUPPLY_PROP_PRESENT)
458 val->intval = val->intval <= 0 ? 0 : 1;
460 case POWER_SUPPLY_PROP_CURRENT_NOW:
461 val->intval = bq27510_battery_current(di);
463 case POWER_SUPPLY_PROP_CAPACITY:
464 val->intval = bq27510_battery_rsoc(di);
466 case POWER_SUPPLY_PROP_TEMP:
467 val->intval = bq27510_battery_temperature(di);
469 case POWER_SUPPLY_PROP_TECHNOLOGY:
470 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
472 case POWER_SUPPLY_PROP_HEALTH:
473 ret = bq27510_health_status(di, val);
475 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
476 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
478 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
479 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
481 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
482 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
491 static int rk29_ac_get_property(struct power_supply *psy,
492 enum power_supply_property psp,
493 union power_supply_propval *val)
496 struct bq27510_device_info *di = container_of(psy, struct bq27510_device_info, ac);
499 case POWER_SUPPLY_PROP_ONLINE:
500 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
501 if(gpio_get_value(di->dc_check_pin))
502 val->intval = 0; /*discharging*/
504 val->intval = 1; /*charging*/
506 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
516 static void bq27510_powersupply_init(struct bq27510_device_info *di)
518 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
519 di->bat.properties = bq27510_battery_props;
520 di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
521 di->bat.get_property = bq27510_battery_get_property;
524 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
525 di->ac.properties = rk29_ac_props;
526 di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
527 di->ac.get_property = rk29_ac_get_property;
531 static void bq27510_battery_update_status(struct bq27510_device_info *di)
533 power_supply_changed(&di->bat);
536 static void bq27510_battery_work(struct work_struct *work)
538 struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work);
539 bq27510_battery_update_status(di);
540 /* reschedule for the next time */
541 schedule_delayed_work(&di->work, di->interval);
544 static void bq27510_set(void)
546 struct bq27510_device_info *di;
551 printk("enter 0x41\n");
554 bq27510_write(di->client,0x00,buf,2);
558 printk("enter 0x21\n");
561 bq27510_write(di->client,0x00,buf,2);
565 bq27510_read(di->client,0x00,buf,2);
567 // printk("%s: Enter:BUF[0]= 0X%x BUF[1] = 0X%x\n",__FUNCTION__,buf[0],buf[1]);
569 while((buf[0] & 0x04)&&(i<5))
571 printk("enter more 0x21 times i = %d\n",i);
575 bq27510_write(di->client,0x00,buf,2);
579 bq27510_read(di->client,0x00,buf,2);
584 printk("write 0x21 error\n");
586 printk("bq27510 write 0x21 success\n");
589 static int bq27510_battery_probe(struct i2c_client *client,
590 const struct i2c_device_id *id)
592 struct bq27510_device_info *di;
594 struct bq27510_platform_data *pdata;
596 pdata = client->dev.platform_data;
598 di = kzalloc(sizeof(*di), GFP_KERNEL);
600 dev_err(&client->dev, "failed to allocate device info data\n");
604 i2c_set_clientdata(client, di);
605 di->dev = &client->dev;
606 di->bat.name = "bq27510-battery";
608 /* 4 seconds between monotor runs interval */
609 di->interval = msecs_to_jiffies(4 * 1000);
611 di->bat_num = pdata->bat_num;
612 di->dc_check_pin = pdata->dc_check_pin;
614 if (pdata->init_dc_check_pin)
615 pdata->init_dc_check_pin( );
617 bq27510_powersupply_init(di);
619 retval = power_supply_register(&client->dev, &di->bat);
621 dev_err(&client->dev, "failed to register battery\n");
625 retval = power_supply_register(&client->dev, &di->ac);
627 dev_err(&client->dev, "failed to register ac\n");
630 INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
631 schedule_delayed_work(&di->work, di->interval);
632 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
641 static int bq27510_battery_remove(struct i2c_client *client)
643 struct bq27510_device_info *di = i2c_get_clientdata(client);
645 power_supply_unregister(&di->bat);
651 static const struct i2c_device_id bq27510_id[] = {
655 static struct i2c_driver bq27510_battery_driver = {
659 .probe = bq27510_battery_probe,
660 .remove = bq27510_battery_remove,
661 .id_table = bq27510_id,
664 static int __init bq27510_battery_init(void)
667 struct proc_dir_entry * battery_proc_entry;
669 ret = i2c_add_driver(&bq27510_battery_driver);
671 printk(KERN_ERR "Unable to register BQ27510 driver\n");
673 battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
676 module_init(bq27510_battery_init);
678 static void __exit bq27510_battery_exit(void)
680 i2c_del_driver(&bq27510_battery_driver);
682 module_exit(bq27510_battery_exit);
684 MODULE_AUTHOR("Rockchip");
685 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
686 MODULE_LICENSE("GPL");