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 unsigned long count,void *data)
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 virtual_battery_enable = 1;
108 virtual_battery_enable = 2;
110 virtual_battery_enable = 3;
112 printk("%s:%d>>bq27510 set\n",__FUNCTION__,__LINE__);
116 virtual_battery_enable = 0;
117 printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
121 static const struct file_operations battery_proc_fops = {
122 .owner = THIS_MODULE,
123 .write = battery_proc_write,
127 * Common code for BQ27510 devices read
129 static int bq27510_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
132 ret = i2c_master_reg8_recv(client, reg, buf, len, BQ27510_SPEED);
136 static int bq27510_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
139 ret = i2c_master_reg8_send(client, reg, buf, (int)len, BQ27510_SPEED);
144 * Return the battery temperature in tenths of degree Celsius
145 * Or < 0 if something fails.
147 static int bq27510_battery_temperature(struct bq27510_device_info *di)
153 #if defined (CONFIG_NO_BATTERY_IC)
157 if(virtual_battery_enable == 1)
159 ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
161 dev_err(di->dev, "error reading temperature\n");
164 temp = get_unaligned_le16(buf);
166 DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
171 * Return the battery Voltage in milivolts
172 * Or < 0 if something fails.
174 static int bq27510_battery_voltage(struct bq27510_device_info *di)
180 #if defined (CONFIG_NO_BATTERY_IC)
183 if(virtual_battery_enable == 1)
184 return 2000000/*4000000*/;
186 ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2);
188 dev_err(di->dev, "error reading voltage\n");
191 volt = get_unaligned_le16(buf);
193 //bp27510 can only measure one li-lion bat
194 if(di->bat_num == 2){
195 volt = volt * 1000 * 2;
200 DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
205 * Return the battery average current
206 * Note that current can be negative signed as well
207 * Or 0 if something fails.
209 static int bq27510_battery_current(struct bq27510_device_info *di)
215 #if defined (CONFIG_NO_BATTERY_IC)
218 if(virtual_battery_enable == 1)
219 return 11000/*22000*/;
220 ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
222 dev_err(di->dev, "error reading current\n");
226 curr = get_unaligned_le16(buf);
227 DBG("curr = %x \n",curr);
229 curr = 0xFFFF^(curr-1);
232 DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
237 * Return the battery Relative State-of-Charge
238 * Or < 0 if something fails.
240 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
245 int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
246 int art = 0, artte = 0, ai = 0, tte = 0, ttf = 0, si = 0;
247 int stte = 0, mli = 0, mltte = 0, ae = 0, ap = 0, ttecp = 0, cc = 0;
251 #if defined (CONFIG_NO_BATTERY_IC)
254 if(virtual_battery_enable == 1)
257 ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2);
259 dev_err(di->dev, "error reading relative State-of-Charge\n");
262 rsoc = get_unaligned_le16(buf);
263 DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
265 #if defined (CONFIG_NO_BATTERY_IC)
268 #if 0 //other register information, for debug use
269 ret = bq27510_read(di->client,0x0c,buf,2); //NominalAvailableCapacity
270 nvcap = get_unaligned_le16(buf);
271 DBG("\nEnter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
272 ret = bq27510_read(di->client,0x0e,buf,2); //FullAvailableCapacity
273 facap = get_unaligned_le16(buf);
274 DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
275 ret = bq27510_read(di->client,0x10,buf,2); //RemainingCapacity
276 remcap = get_unaligned_le16(buf);
277 DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
278 ret = bq27510_read(di->client,0x12,buf,2); //FullChargeCapacity
279 fccap = get_unaligned_le16(buf);
280 DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
281 ret = bq27510_read(di->client,0x3c,buf,2); //DesignCapacity
282 full = get_unaligned_le16(buf);
283 DBG("Enter:%s %d--DesignCapacity = %d\n",__FUNCTION__,__LINE__,full);
285 buf[0] = 0x00; //CONTROL_STATUS
287 bq27510_write(di->client,0x00,buf,2);
288 ret = bq27510_read(di->client,0x00,buf,2);
289 cnt = get_unaligned_le16(buf);
290 DBG("Enter:%s %d--Control status = %x\n",__FUNCTION__,__LINE__,cnt);
292 ret = bq27510_read(di->client,0x02,buf,2); //AtRate
293 art = get_unaligned_le16(buf);
294 DBG("Enter:%s %d--AtRate = %d\n",__FUNCTION__,__LINE__,art);
295 ret = bq27510_read(di->client,0x04,buf,2); //AtRateTimeToEmpty
296 artte = get_unaligned_le16(buf);
297 DBG("Enter:%s %d--AtRateTimeToEmpty = %d\n",__FUNCTION__,__LINE__,artte);
298 ret = bq27510_read(di->client,0x14,buf,2); //AverageCurrent
299 ai = get_unaligned_le16(buf);
300 DBG("Enter:%s %d--AverageCurrent = %d\n",__FUNCTION__,__LINE__,ai);
301 ret = bq27510_read(di->client,0x16,buf,2); //TimeToEmpty
302 tte = get_unaligned_le16(buf);
303 DBG("Enter:%s %d--TimeToEmpty = %d\n",__FUNCTION__,__LINE__,tte);
304 ret = bq27510_read(di->client,0x18,buf,2); //TimeToFull
305 ttf = get_unaligned_le16(buf);
306 DBG("Enter:%s %d--TimeToFull = %d\n",__FUNCTION__,__LINE__,ttf);
307 ret = bq27510_read(di->client,0x1a,buf,2); //StandbyCurrent
308 si = get_unaligned_le16(buf);
309 DBG("Enter:%s %d--StandbyCurrent = %d\n",__FUNCTION__,__LINE__,si);
310 ret = bq27510_read(di->client,0x1c,buf,2); //StandbyTimeToEmpty
311 stte = get_unaligned_le16(buf);
312 DBG("Enter:%s %d--StandbyTimeToEmpty = %d\n",__FUNCTION__,__LINE__,stte);
313 ret = bq27510_read(di->client,0x1e,buf,2); //MaxLoadCurrent
314 mli = get_unaligned_le16(buf);
315 DBG("Enter:%s %d--MaxLoadCurrent = %d\n",__FUNCTION__,__LINE__,mli);
316 ret = bq27510_read(di->client,0x20,buf,2); //MaxLoadTimeToEmpty
317 mltte = get_unaligned_le16(buf);
318 DBG("Enter:%s %d--MaxLoadTimeToEmpty = %d\n",__FUNCTION__,__LINE__,mltte);
319 ret = bq27510_read(di->client,0x22,buf,2); //AvailableEnergy
320 ae = get_unaligned_le16(buf);
321 DBG("Enter:%s %d--AvailableEnergy = %d\n",__FUNCTION__,__LINE__,ae);
322 ret = bq27510_read(di->client,0x24,buf,2); //AveragePower
323 ap = get_unaligned_le16(buf);
324 DBG("Enter:%s %d--AveragePower = %d\n",__FUNCTION__,__LINE__,ap);
325 ret = bq27510_read(di->client,0x26,buf,2); //TTEatConstantPower
326 ttecp = get_unaligned_le16(buf);
327 DBG("Enter:%s %d--TTEatConstantPower = %d\n",__FUNCTION__,__LINE__,ttecp);
328 ret = bq27510_read(di->client,0x2a,buf,2); //CycleCount
329 cc = get_unaligned_le16(buf);
330 DBG("Enter:%s %d--CycleCount = %d\n",__FUNCTION__,__LINE__,cc);
335 static int bq27510_battery_status(struct bq27510_device_info *di,
336 union power_supply_propval *val)
343 #if defined (CONFIG_NO_BATTERY_IC)
344 val->intval = POWER_SUPPLY_STATUS_FULL;
348 if(virtual_battery_enable == 1)
350 val->intval = POWER_SUPPLY_STATUS_FULL;
353 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
355 dev_err(di->dev, "error reading flags\n");
358 flags = get_unaligned_le16(buf);
359 DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
360 if (flags & BQ27500_FLAG_FC)
361 status = POWER_SUPPLY_STATUS_FULL;
362 else if (flags & BQ27500_FLAG_DSC)
363 status = POWER_SUPPLY_STATUS_DISCHARGING;
365 status = POWER_SUPPLY_STATUS_CHARGING;
367 val->intval = status;
371 static int bq27510_health_status(struct bq27510_device_info *di,
372 union power_supply_propval *val)
379 #if defined (CONFIG_NO_BATTERY_IC)
380 val->intval = POWER_SUPPLY_HEALTH_GOOD;
384 if(virtual_battery_enable == 1)
386 val->intval = POWER_SUPPLY_HEALTH_GOOD;
389 ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
391 dev_err(di->dev, "error reading flags\n");
394 flags = get_unaligned_le16(buf);
395 DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
396 if ((flags & BQ27500_FLAG_OTD)||(flags & BQ27500_FLAG_OTC))
397 status = POWER_SUPPLY_HEALTH_OVERHEAT;
399 status = POWER_SUPPLY_HEALTH_GOOD;
401 val->intval = status;
407 * Read a time register.
408 * Return < 0 if something fails.
410 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
411 union power_supply_propval *val)
417 ret = bq27510_read(di->client,reg,buf,2);
419 dev_err(di->dev, "error reading register %02x\n", reg);
422 tval = get_unaligned_le16(buf);
423 DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
427 val->intval = tval * 60;
428 DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
432 #define to_bq27510_device_info(x) container_of((x), \
433 struct bq27510_device_info, bat);
435 static int bq27510_battery_get_property(struct power_supply *psy,
436 enum power_supply_property psp,
437 union power_supply_propval *val)
440 struct bq27510_device_info *di = to_bq27510_device_info(psy);
441 DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
445 case POWER_SUPPLY_PROP_STATUS:
446 ret = bq27510_battery_status(di, val);
448 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
449 case POWER_SUPPLY_PROP_PRESENT:
450 val->intval = bq27510_battery_voltage(di);
451 if (psp == POWER_SUPPLY_PROP_PRESENT)
452 val->intval = val->intval <= 0 ? 0 : 1;
454 case POWER_SUPPLY_PROP_CURRENT_NOW:
455 val->intval = bq27510_battery_current(di);
457 case POWER_SUPPLY_PROP_CAPACITY:
458 val->intval = bq27510_battery_rsoc(di);
460 case POWER_SUPPLY_PROP_TEMP:
461 val->intval = bq27510_battery_temperature(di);
463 case POWER_SUPPLY_PROP_TECHNOLOGY:
464 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
466 case POWER_SUPPLY_PROP_HEALTH:
467 ret = bq27510_health_status(di, val);
469 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
470 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
472 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
473 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
475 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
476 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
485 static int rk29_ac_get_property(struct power_supply *psy,
486 enum power_supply_property psp,
487 union power_supply_propval *val)
490 struct bq27510_device_info *di = container_of(psy, struct bq27510_device_info, ac);
493 case POWER_SUPPLY_PROP_ONLINE:
494 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
495 if(gpio_get_value(di->dc_check_pin))
496 val->intval = 0; /*discharging*/
498 val->intval = 1; /*charging*/
500 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
510 static void bq27510_powersupply_init(struct bq27510_device_info *di)
512 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
513 di->bat.properties = bq27510_battery_props;
514 di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
515 di->bat.get_property = bq27510_battery_get_property;
518 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
519 di->ac.properties = rk29_ac_props;
520 di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
521 di->ac.get_property = rk29_ac_get_property;
525 static void bq27510_battery_update_status(struct bq27510_device_info *di)
527 power_supply_changed(&di->bat);
530 static void bq27510_battery_work(struct work_struct *work)
532 struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work);
533 bq27510_battery_update_status(di);
534 /* reschedule for the next time */
535 schedule_delayed_work(&di->work, di->interval);
538 static void bq27510_set(void)
540 struct bq27510_device_info *di;
545 printk("enter 0x41\n");
548 bq27510_write(di->client,0x00,buf,2);
552 printk("enter 0x21\n");
555 bq27510_write(di->client,0x00,buf,2);
559 bq27510_read(di->client,0x00,buf,2);
561 // printk("%s: Enter:BUF[0]= 0X%x BUF[1] = 0X%x\n",__FUNCTION__,buf[0],buf[1]);
563 while((buf[0] & 0x04)&&(i<5))
565 printk("enter more 0x21 times i = %d\n",i);
569 bq27510_write(di->client,0x00,buf,2);
573 bq27510_read(di->client,0x00,buf,2);
578 printk("write 0x21 error\n");
580 printk("bq27510 write 0x21 success\n");
583 static int bq27510_battery_probe(struct i2c_client *client,
584 const struct i2c_device_id *id)
586 struct bq27510_device_info *di;
588 struct bq27510_platform_data *pdata;
590 pdata = client->dev.platform_data;
592 di = kzalloc(sizeof(*di), GFP_KERNEL);
594 dev_err(&client->dev, "failed to allocate device info data\n");
598 i2c_set_clientdata(client, di);
599 di->dev = &client->dev;
600 di->bat.name = "bq27510-battery";
602 /* 4 seconds between monotor runs interval */
603 di->interval = msecs_to_jiffies(4 * 1000);
605 di->bat_num = pdata->bat_num;
606 di->dc_check_pin = pdata->dc_check_pin;
608 if (pdata->init_dc_check_pin)
609 pdata->init_dc_check_pin( );
611 bq27510_powersupply_init(di);
613 retval = power_supply_register(&client->dev, &di->bat);
615 dev_err(&client->dev, "failed to register battery\n");
619 retval = power_supply_register(&client->dev, &di->ac);
621 dev_err(&client->dev, "failed to register ac\n");
624 INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
625 schedule_delayed_work(&di->work, di->interval);
626 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
635 static int bq27510_battery_remove(struct i2c_client *client)
637 struct bq27510_device_info *di = i2c_get_clientdata(client);
639 power_supply_unregister(&di->bat);
645 static const struct i2c_device_id bq27510_id[] = {
649 static struct i2c_driver bq27510_battery_driver = {
653 .probe = bq27510_battery_probe,
654 .remove = bq27510_battery_remove,
655 .id_table = bq27510_id,
658 static int __init bq27510_battery_init(void)
661 struct proc_dir_entry * battery_proc_entry;
663 ret = i2c_add_driver(&bq27510_battery_driver);
665 printk(KERN_ERR "Unable to register BQ27510 driver\n");
667 battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
670 module_init(bq27510_battery_init);
672 static void __exit bq27510_battery_exit(void)
674 i2c_del_driver(&bq27510_battery_driver);
676 module_exit(bq27510_battery_exit);
678 MODULE_AUTHOR("Rockchip");
679 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
680 MODULE_LICENSE("GPL");