2 * BQ27320 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 <linux/proc_fs.h>
25 #include <linux/uaccess.h>
26 #include <linux/fcntl.h>
28 #include <linux/ctype.h>
29 #include <linux/vmalloc.h>
31 #include <linux/interrupt.h>
32 #include <linux/module.h>
33 #include <linux/of_irq.h>
34 #include <linux/of_gpio.h>
36 #include <linux/of_device.h>
38 #define DRIVER_VERSION "1.0.0"
39 #define BQ27x00_REG_TEMP 0x06
40 #define BQ27x00_REG_VOLT 0x08
41 #define BQ27x00_REG_AI 0x14
42 #define BQ27x00_REG_BATTERYSTATUS 0x0A
43 #define BQ27x00_REG_TTE 0x16
44 #define BQ27x00_REG_TTF 0x18
45 #define BQ27320_REG_SOC 0x2c
47 #define BQ27320_BATTERYSTATUS_DSC BIT(0)
48 #define BQ27320_BATTERYSTATUS_SYSDOWN BIT(1)
49 //#define BQ27320_BATTERYSTATUS_CHGS BIT(8)
50 #define BQ27320_BATTERYSTATUS_FC BIT(9)
51 #define BQ27320_BATTERYSTATUS_OTD BIT(10)
52 #define BQ27320_BATTERYSTATUS_OTC BIT(11)
53 #define BQ27320_CURRENT BIT(15)
55 #define BQ27320_SPEED 100 * 1000
57 /*define for firmware update*/
58 #define BSP_I2C_MAX_TRANSFER_LEN 128
59 #define BSP_MAX_ASC_PER_LINE 400
60 #define BSP_ENTER_ROM_MODE_CMD 0x00
61 #define BSP_ENTER_ROM_MODE_DATA 0x0F00
62 #define BSP_ROM_MODE_I2C_ADDR 0x0B
63 #define BSP_NORMAL_MODE_I2C_ADDR 0x55
64 #define BSP_FIRMWARE_FILE_SIZE (3301*400)
66 /*define for power detect*/
67 #define BATTERY_LOW_CAPACITY 2
68 #define BATTERY_LOW_VOLTAGE 3500000
69 #define BATTERY_RECHARGER_CAPACITY 97
70 #define BATTERY_LOW_TEMPRETURE 0
71 #define BATTERY_HIGH_TEMPRETURE 650
73 struct bq27320_device_info {
75 struct power_supply bat;
76 struct power_supply usb;
77 struct power_supply ac;
78 struct delayed_work work;
79 struct i2c_client *client;
80 unsigned int interval;
81 unsigned int dc_check_pin;
84 unsigned usb_charging;
92 struct workqueue_struct *workqueue;
93 struct delayed_work chg_down_work;
94 struct mutex battery_mutex;
97 struct bq27320_board {
99 struct device_node *of_node;
102 struct i2c_client* g_bq27320_i2c_client = NULL;
103 static struct i2c_driver bq27320_battery_driver;
105 int virtual_battery_enable = 0;
106 extern int dwc_vbus_status(void);
107 //extern int get_gadget_connect_flag(void);
108 extern int dwc_otg_check_dpdm(bool wait);
111 #define DBG(x...) printk(KERN_INFO x)
113 #define DBG(x...) do { } while (0)
116 /* If the system has several batteries we need a different name for each
119 static DEFINE_MUTEX(battery_mutex);
121 static struct bq27320_device_info *bq27320_di;
122 static enum power_supply_property bq27320_battery_props[] = {
123 POWER_SUPPLY_PROP_STATUS,
124 POWER_SUPPLY_PROP_ONLINE,
125 POWER_SUPPLY_PROP_PRESENT,
126 POWER_SUPPLY_PROP_VOLTAGE_NOW,
127 POWER_SUPPLY_PROP_CURRENT_NOW,
128 POWER_SUPPLY_PROP_CAPACITY,
129 POWER_SUPPLY_PROP_TEMP,
130 // POWER_SUPPLY_PROP_TECHNOLOGY,
131 // POWER_SUPPLY_PROP_HEALTH,
132 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
133 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
134 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
137 static enum power_supply_property rk3190_ac_props[] = {
138 POWER_SUPPLY_PROP_ONLINE,
141 static enum power_supply_property rk3190_usb_props[] = {
142 POWER_SUPPLY_PROP_ONLINE,
145 static ssize_t battery_proc_write(struct file *file,const char __user *buffer,
146 size_t count, loff_t *ppos)
151 printk("echo x >/proc/driver/power\n");
152 printk("x=1,means just print log ||x=2,means log and data ||x= other,means close log\n");
154 rc = get_user(c,buffer);
159 virtual_battery_enable = 1;
161 virtual_battery_enable = 2;
163 virtual_battery_enable = 3;
165 virtual_battery_enable = 0;
166 printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
170 static const struct file_operations battery_proc_fops = {
171 .owner = THIS_MODULE,
172 .write = battery_proc_write,
176 * Common code for BQ27320 devices read
179 int bq27320_i2c_master_reg8_read(const struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
181 struct i2c_adapter *adap=client->adapter;
182 struct i2c_msg msgs[2];
186 msgs[0].addr = client->addr;
187 msgs[0].flags = client->flags;
189 msgs[0].buf = ®_buf;
190 msgs[0].scl_rate = scl_rate;
191 // msgs[0].udelay = client->udelay;
193 msgs[1].addr = client->addr;
194 msgs[1].flags = client->flags | I2C_M_RD;
196 msgs[1].buf = (char *)buf;
197 msgs[1].scl_rate = scl_rate;
198 // msgs[1].udelay = client->udelay;
200 ret = i2c_transfer(adap, msgs, 2);
201 return (ret == 2)? count : ret;
203 EXPORT_SYMBOL(bq27320_i2c_master_reg8_read);
205 int bq27320_i2c_master_reg8_write(const struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
207 struct i2c_adapter *adap=client->adapter;
210 char *tx_buf = (char *)kmalloc(count + 1, GFP_KERNEL);
214 memcpy(tx_buf+1, buf, count);
216 msg.addr = client->addr;
217 msg.flags = client->flags;
219 msg.buf = (char *)tx_buf;
220 msg.scl_rate = scl_rate;
221 // msg.udelay = client->udelay;
223 ret = i2c_transfer(adap, &msg, 1);
225 return (ret == 1) ? count : ret;
228 EXPORT_SYMBOL(bq27320_i2c_master_reg8_write);
229 static int bq27320_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
232 mutex_lock(&battery_mutex);
233 ret = bq27320_i2c_master_reg8_read(client, reg, buf, len, BQ27320_SPEED);
234 mutex_unlock(&battery_mutex);
238 static int bq27320_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
241 mutex_lock(&battery_mutex);
242 ret = bq27320_i2c_master_reg8_write(client, reg, buf, (int)len, BQ27320_SPEED);
243 mutex_unlock(&battery_mutex);
247 static int bq27320_read_and_compare(struct i2c_client *client, u8 reg, u8 *pSrcBuf, u8 *pDstBuf, u16 len)
251 i2c_ret = bq27320_read(client, reg, pSrcBuf, len);
254 printk(KERN_ERR "[%s,%d,%08x] bq27320_read failed\n",__FUNCTION__,__LINE__,reg);
258 i2c_ret = strncmp(pDstBuf, pSrcBuf, len);
263 static int bq27320_atoi(const char *s)
268 while (*s != '\0' && *s >= '0' && *s <= '9') {
269 k = 10 * k + (*s - '0');
275 static unsigned long bq27320_strtoul(const char *cp, unsigned int base)
277 unsigned long result = 0,value;
279 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
280 ? toupper(*cp) : *cp)-'A'+10) < base)
282 result = result*base + value;
289 static int bq27320_firmware_program(struct i2c_client *client, const unsigned char *pgm_data, unsigned int filelen)
291 unsigned int i = 0, j = 0, ulDelay = 0, ulReadNum = 0;
292 unsigned int ulCounter = 0, ulLineLen = 0;
293 unsigned char temp = 0;
294 unsigned char *p_cur;
295 unsigned char pBuf[BSP_MAX_ASC_PER_LINE] = { 0 };
296 unsigned char p_src[BSP_I2C_MAX_TRANSFER_LEN] = { 0 };
297 unsigned char p_dst[BSP_I2C_MAX_TRANSFER_LEN] = { 0 };
298 unsigned char ucTmpBuf[16] = { 0 };
300 bq275x0_firmware_program_begin:
306 p_cur = (unsigned char *)pgm_data;
310 if((p_cur - pgm_data) >= filelen)
312 printk("Download success\n");
316 while (*p_cur == '\r' || *p_cur == '\n')
324 memset(p_src, 0x00, sizeof(p_src));
325 memset(p_dst, 0x00, sizeof(p_dst));
326 memset(pBuf, 0x00, sizeof(pBuf));
328 /*»ñÈ¡Ò»ÐÐÊý¾Ý£¬È¥³ý¿Õ¸ñ*/
329 while(i < BSP_MAX_ASC_PER_LINE)
333 if(('\r' == temp) || ('\n' == temp))
339 pBuf[ulLineLen++] = temp;
347 if(('W' == p_src[0]) || ('C' == p_src[0]))
349 for(i=2,j=0; i<ulLineLen; i+=2,j++)
351 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
352 memcpy(ucTmpBuf, pBuf+i, 2);
353 p_src[2+j] = bq27320_strtoul(ucTmpBuf, 16);
356 temp = (ulLineLen -2)/2;
357 ulLineLen = temp + 2;
359 else if('X' == p_src[0])
361 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
362 memcpy(ucTmpBuf, pBuf+2, ulLineLen-2);
363 ulDelay = bq27320_atoi(ucTmpBuf);
365 else if('R' == p_src[0])
367 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
368 memcpy(ucTmpBuf, pBuf+2, 2);
369 p_src[2] = bq27320_strtoul(ucTmpBuf, 16);
370 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
371 memcpy(ucTmpBuf, pBuf+4, 2);
372 p_src[3] = bq27320_strtoul(ucTmpBuf, 16);
373 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
374 memcpy(ucTmpBuf, pBuf+6, ulLineLen-6);
375 ulReadNum = bq27320_atoi(ucTmpBuf);
386 for(i=0; i<ulLineLen-4; i++)
388 printk("%x ", p_src[4+i]);
390 printk(KERN_ERR "\n");
393 if(bq27320_write(client, p_src[3], &p_src[4], ulLineLen-4) < 0)
395 printk(KERN_ERR "[%s,%d] bq27320_write failed len=%d reg+ %08x\n",__FUNCTION__,__LINE__,ulLineLen-4,p_src[3]);
401 if(bq27320_read(client, p_src[3], p_dst, ulReadNum) < 0)
403 printk(KERN_ERR "[%s,%d] bq275x0_i2c_bytes_read failed\n",__FUNCTION__,__LINE__);
408 if(bq27320_read_and_compare(client, p_src[3], p_dst, &p_src[4], ulLineLen-4))
411 printk(KERN_ERR "[%s,%d] bq275x0_i2c_bytes_read_and_compare failed\n",__FUNCTION__,__LINE__);
412 goto bq275x0_firmware_program_begin;
431 static int bq27320_firmware_download(struct i2c_client *client, const unsigned char *pgm_data, unsigned int len)
434 unsigned char ucTmpBuf[2] = { 0 };
436 ucTmpBuf[0] = BSP_ENTER_ROM_MODE_DATA & 0x00ff;
437 ucTmpBuf[1] = (BSP_ENTER_ROM_MODE_DATA>>8) & 0x00ff;
440 iRet = bq27320_write(client, BSP_ENTER_ROM_MODE_CMD, &ucTmpBuf[0], 2);
443 printk(KERN_ERR "[%s,%d] bq27320_write failed\n",__FUNCTION__,__LINE__);
448 g_bq27320_i2c_client->addr = BSP_ROM_MODE_I2C_ADDR;
451 iRet = bq27320_firmware_program(g_bq27320_i2c_client, pgm_data, len);
454 printk(KERN_ERR "[%s,%d] bq275x0_firmware_program failed\n",__FUNCTION__,__LINE__);
458 g_bq27320_i2c_client->addr = BSP_NORMAL_MODE_I2C_ADDR;
464 static int bq27320_update_firmware(struct i2c_client *client, const char *pFilePath)
468 struct inode *inode = NULL;
476 filp = filp_open(pFilePath, O_RDONLY, S_IRUSR);
479 printk(KERN_ERR "[%s,%d] filp_open failed\n",__FUNCTION__,__LINE__);
486 printk(KERN_ERR "[%s,%d] File Operation Method Error\n",__FUNCTION__,__LINE__);
487 filp_close(filp, NULL);
492 inode = filp->f_path.dentry->d_inode;
495 printk(KERN_ERR "[%s,%d] Get inode from filp failed\n",__FUNCTION__,__LINE__);
496 filp_close(filp, NULL);
502 length = i_size_read(inode->i_mapping->host);
503 printk("bq27320 firmware image size is %d \n",length);
504 if (!( length > 0 && length < BSP_FIRMWARE_FILE_SIZE))
506 printk(KERN_ERR "[%s,%d] Get file size error\n",__FUNCTION__,__LINE__);
507 filp_close(filp, NULL);
512 /* allocation buff size */
513 buf = vmalloc(length+(length%2)); /* buf size if even */
516 printk(KERN_ERR "[%s,%d] Alloctation memory failed\n",__FUNCTION__,__LINE__);
517 filp_close(filp, NULL);
523 if (filp->f_op->read(filp, buf, length, &filp->f_pos) != length)
525 printk(KERN_ERR "[%s,%d] File read error\n",__FUNCTION__,__LINE__);
526 filp_close(filp, NULL);
527 filp_close(filp, NULL);
533 ret = bq27320_firmware_download(client, (const char*)buf, length);
538 filp_close(filp, NULL);
545 static u8 get_child_version(void)
550 if(bq27320_write(g_bq27320_i2c_client, 0x3e, data, 1) < 0)
555 if(bq27320_write(g_bq27320_i2c_client, 0x3f, data, 1) < 0)
559 bq27320_read(g_bq27320_i2c_client, 0x40, data, 8);
564 static ssize_t bq27320_attr_store(struct device_driver *driver,const char *buf, size_t count)
567 unsigned char path_image[255];
569 if(NULL == buf || count >255 || count == 0 || strnchr(buf, count, 0x20))
571 memcpy (path_image, buf, count);
572 /* replace '\n' with '\0' */
573 if((path_image[count-1]) == '\n')
574 path_image[count-1] = '\0';
576 path_image[count] = '\0';
578 /*enter firmware bqfs download*/
579 virtual_battery_enable = 1;
580 iRet = bq27320_update_firmware(g_bq27320_i2c_client, path_image);
582 virtual_battery_enable = 0;
585 pr_err("Update firemware finish, then update battery status...");
592 static ssize_t bq27320_attr_show(struct device_driver *driver, char *buf)
601 ver = get_child_version();
605 return sprintf(buf, "%s", "Coulometer Damaged or Firmware Error");
610 return sprintf(buf, "download firemware is %x", ver);
615 static DRIVER_ATTR(state, 0664, bq27320_attr_show, bq27320_attr_store);
620 * Return the battery temperature in tenths of degree Celsius
621 * Or < 0 if something fails.
623 static int bq27320_battery_temperature(struct bq27320_device_info *di)
629 #if defined (CONFIG_NO_BATTERY_IC)
633 if(virtual_battery_enable == 1)
635 ret = bq27320_read(di->client,BQ27x00_REG_TEMP,buf,2);
637 dev_err(di->dev, "error reading temperature\n");
640 temp = get_unaligned_le16(buf);
642 DBG("Enter:%s--temp = %d\n",__FUNCTION__,temp);
643 di ->bat_tempreture = temp;
648 * Return the battery Voltage in milivolts
649 * Or < 0 if something fails.
651 static int bq27320_battery_voltage(struct bq27320_device_info *di)
657 #if defined (CONFIG_NO_BATTERY_IC)
660 if(virtual_battery_enable == 1)
661 return 2000000/*4000000*/;
663 ret = bq27320_read(di->client,BQ27x00_REG_VOLT,buf,2);
665 dev_err(di->dev, "error reading voltage\n");
668 volt = get_unaligned_le16(buf);
670 //bp27510 can only measure one li-lion bat
671 if(di->bat_num == 2){
672 volt = volt * 1000 * 2;
677 DBG("Enter:%s--volt = %d\n",__FUNCTION__,volt);
682 * Return the battery average current
683 * Note that current can be negative signed as well
684 * Or 0 if something fails.
686 static int bq27320_battery_current(struct bq27320_device_info *di)
692 #if defined (CONFIG_NO_BATTERY_IC)
695 if(virtual_battery_enable == 1)
696 return 11000/*22000*/;
697 ret = bq27320_read(di->client,BQ27x00_REG_AI,buf,2);
699 dev_err(di->dev, "error reading current\n");
703 curr = get_unaligned_le16(buf);
704 DBG("curr = %x \n",curr);
706 curr = 0xFFFF^(curr-1);
707 DBG("curr = -%d \n",curr*1000);
710 DBG("curr = %d \n",curr*1000);
716 * Return the battery Relative State-of-Charge
717 * Or < 0 if something fails.
720 static int bq27320_battery_rsoc(struct bq27320_device_info *di)
725 int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
726 int art = 0, artte = 0, ai = 0, tte = 0, ttf = 0, si = 0;
727 int stte = 0, mli = 0, mltte = 0, ae = 0, ap = 0, ttecp = 0, cc = 0;
731 #if defined (CONFIG_NO_BATTERY_IC)
734 if(virtual_battery_enable == 1)
737 ret = bq27320_read(di->client,BQ27320_REG_SOC,buf,2);
739 dev_err(di->dev, "error reading relative State-of-Charge\n");
742 rsoc = get_unaligned_le16(buf);
743 DBG("Enter:%s --rsoc = %d\n",__FUNCTION__,rsoc);
745 #if defined (CONFIG_NO_BATTERY_IC)
748 #if 0 //other register information, for debug use
749 ret = bq27320_read(di->client,0x0c,buf,2); //NominalAvailableCapacity
750 nvcap = get_unaligned_le16(buf);
751 DBG("\nEnter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
752 ret = bq27320_read(di->client,0x0e,buf,2); //FullAvailableCapacity
753 facap = get_unaligned_le16(buf);
754 DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
755 ret = bq27320_read(di->client,0x10,buf,2); //RemainingCapacity
756 remcap = get_unaligned_le16(buf);
757 DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
758 ret = bq27320_read(di->client,0x12,buf,2); //FullChargeCapacity
759 fccap = get_unaligned_le16(buf);
760 DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
761 ret = bq27320_read(di->client,0x3c,buf,2); //DesignCapacity
762 full = get_unaligned_le16(buf);
763 DBG("Enter:%s %d--DesignCapacity = %d\n",__FUNCTION__,__LINE__,full);
765 buf[0] = 0x00; //CONTROL_STATUS
767 bq27320_write(di->client,0x00,buf,2);
768 ret = bq27320_read(di->client,0x00,buf,2);
769 cnt = get_unaligned_le16(buf);
770 DBG("Enter:%s %d--Control status = %x\n",__FUNCTION__,__LINE__,cnt);
772 ret = bq27320_read(di->client,0x02,buf,2); //AtRate
773 art = get_unaligned_le16(buf);
774 DBG("Enter:%s %d--AtRate = %d\n",__FUNCTION__,__LINE__,art);
775 ret = bq27320_read(di->client,0x04,buf,2); //AtRateTimeToEmpty
776 artte = get_unaligned_le16(buf);
777 DBG("Enter:%s %d--AtRateTimeToEmpty = %d\n",__FUNCTION__,__LINE__,artte);
778 ret = bq27320_read(di->client,0x14,buf,2); //AverageCurrent
779 ai = get_unaligned_le16(buf);
780 DBG("Enter:%s %d--AverageCurrent = %d\n",__FUNCTION__,__LINE__,ai);
781 ret = bq27320_read(di->client,0x16,buf,2); //TimeToEmpty
782 tte = get_unaligned_le16(buf);
783 DBG("Enter:%s %d--TimeToEmpty = %d\n",__FUNCTION__,__LINE__,tte);
784 ret = bq27320_read(di->client,0x18,buf,2); //TimeToFull
785 ttf = get_unaligned_le16(buf);
786 DBG("Enter:%s %d--TimeToFull = %d\n",__FUNCTION__,__LINE__,ttf);
787 ret = bq27320_read(di->client,0x1a,buf,2); //StandbyCurrent
788 si = get_unaligned_le16(buf);
789 DBG("Enter:%s %d--StandbyCurrent = %d\n",__FUNCTION__,__LINE__,si);
790 ret = bq27320_read(di->client,0x1c,buf,2); //StandbyTimeToEmpty
791 stte = get_unaligned_le16(buf);
792 DBG("Enter:%s %d--StandbyTimeToEmpty = %d\n",__FUNCTION__,__LINE__,stte);
793 ret = bq27320_read(di->client,0x1e,buf,2); //MaxLoadCurrent
794 mli = get_unaligned_le16(buf);
795 DBG("Enter:%s %d--MaxLoadCurrent = %d\n",__FUNCTION__,__LINE__,mli);
796 ret = bq27320_read(di->client,0x20,buf,2); //MaxLoadTimeToEmpty
797 mltte = get_unaligned_le16(buf);
798 DBG("Enter:%s %d--MaxLoadTimeToEmpty = %d\n",__FUNCTION__,__LINE__,mltte);
799 ret = bq27320_read(di->client,0x22,buf,2); //AvailableEnergy
800 ae = get_unaligned_le16(buf);
801 DBG("Enter:%s %d--AvailableEnergy = %d\n",__FUNCTION__,__LINE__,ae);
802 ret = bq27320_read(di->client,0x24,buf,2); //AveragePower
803 ap = get_unaligned_le16(buf);
804 DBG("Enter:%s %d--AveragePower = %d\n",__FUNCTION__,__LINE__,ap);
805 ret = bq27320_read(di->client,0x26,buf,2); //TTEatConstantPower
806 ttecp = get_unaligned_le16(buf);
807 DBG("Enter:%s %d--TTEatConstantPower = %d\n",__FUNCTION__,__LINE__,ttecp);
808 ret = bq27320_read(di->client,0x2a,buf,2); //CycleCount
809 cc = get_unaligned_le16(buf);
810 DBG("Enter:%s %d--CycleCount = %d\n",__FUNCTION__,__LINE__,cc);
815 static int bq27320_battery_status(struct bq27320_device_info *di,
816 union power_supply_propval *val)
823 #if defined (CONFIG_NO_BATTERY_IC)
824 val->intval = POWER_SUPPLY_STATUS_FULL;
828 if(virtual_battery_enable == 1)
830 val->intval = POWER_SUPPLY_STATUS_FULL;
834 ret = bq27320_read(di->client,BQ27x00_REG_BATTERYSTATUS, buf, 2);
836 dev_err(di->dev, "error reading flags\n");
840 flags = get_unaligned_le16(buf);
841 DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
843 if ((flags & BQ27320_BATTERYSTATUS_FC) ||(bq27320_di ->rsoc ==100)){
844 status = POWER_SUPPLY_STATUS_FULL;
846 DBG("status =POWER_SUPPLY_STATUS_FULL \n");
848 else if (flags & BQ27320_BATTERYSTATUS_DSC){
849 status = POWER_SUPPLY_STATUS_DISCHARGING;
850 DBG("status =POWER_SUPPLY_STATUS_DISCHARGING \n");
853 status = POWER_SUPPLY_STATUS_CHARGING;
854 DBG("status =POWER_SUPPLY_STATUS_CHARGING \n");
857 if (((status==POWER_SUPPLY_STATUS_FULL)||(status==POWER_SUPPLY_STATUS_CHARGING))
858 && ((bq27320_di->ac_charging ==0) && (bq27320_di->usb_charging ==0) ))
859 status = POWER_SUPPLY_STATUS_DISCHARGING;
860 else if ((status==POWER_SUPPLY_STATUS_DISCHARGING) && ((bq27320_di->ac_charging ==1) || (bq27320_di->usb_charging ==1) ))
861 status = POWER_SUPPLY_STATUS_CHARGING;
863 di ->bat_status = status;
864 val->intval = status;
868 static int bq27320_health_status(struct bq27320_device_info *di,
869 union power_supply_propval *val)
876 #if defined (CONFIG_NO_BATTERY_IC)
877 val->intval = POWER_SUPPLY_HEALTH_GOOD;
881 if(virtual_battery_enable == 1)
883 val->intval = POWER_SUPPLY_HEALTH_GOOD;
886 ret = bq27320_read(di->client,BQ27x00_REG_BATTERYSTATUS, buf, 2);
888 dev_err(di->dev, "error reading flags\n");
891 flags = get_unaligned_le16(buf);
892 DBG("Enter:%s--health status = %x\n",__FUNCTION__,flags);
893 if ((flags & BQ27320_BATTERYSTATUS_OTD)||(flags & BQ27320_BATTERYSTATUS_OTC)){
894 status = POWER_SUPPLY_HEALTH_OVERHEAT;
895 DBG("health =POWER_SUPPLY_HEALTH_OVERHEAT \n");
898 status = POWER_SUPPLY_HEALTH_GOOD;
899 DBG("health =POWER_SUPPLY_HEALTH_GOOD \n");
902 val->intval = status;
908 * Read a time register.
909 * Return < 0 if something fails.
911 static int bq27320_battery_time(struct bq27320_device_info *di, int reg,
912 union power_supply_propval *val)
918 ret = bq27320_read(di->client,reg,buf,2);
920 dev_err(di->dev, "error reading register %02x\n", reg);
923 tval = get_unaligned_le16(buf);
924 DBG("Enter:%s--tval=%d\n",__FUNCTION__,tval);
928 val->intval = tval * 60;
929 DBG("Enter:%s val->intval = %d\n",__FUNCTION__,val->intval);
933 #define to_bq27320_device_info(x) container_of((x), \
934 struct bq27320_device_info, bat);
936 static int bq27320_battery_get_property(struct power_supply *psy,
937 enum power_supply_property psp,
938 union power_supply_propval *val)
941 struct bq27320_device_info *di = to_bq27320_device_info(psy);
942 DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
946 case POWER_SUPPLY_PROP_STATUS:
947 ret = bq27320_battery_status(di, val);
949 case POWER_SUPPLY_PROP_ONLINE:
950 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
951 val->intval = bq27320_di ->ac_charging;
952 else if (psy->type == POWER_SUPPLY_TYPE_USB)
953 val->intval = bq27320_di ->usb_charging;
955 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
956 val->intval = bq27320_battery_voltage(di);
958 case POWER_SUPPLY_PROP_PRESENT:
959 val->intval = bq27320_battery_voltage(di);
960 val->intval = val->intval <= 0 ? 0 : 1;
961 di->bat_present =val->intval;
963 case POWER_SUPPLY_PROP_CURRENT_NOW:
964 val->intval = bq27320_battery_current(di);
966 case POWER_SUPPLY_PROP_CAPACITY:
967 val->intval = bq27320_battery_rsoc(di);
969 case POWER_SUPPLY_PROP_TEMP:
970 val->intval = bq27320_battery_temperature(di);
972 case POWER_SUPPLY_PROP_TECHNOLOGY:
973 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
975 case POWER_SUPPLY_PROP_HEALTH:
976 ret = bq27320_health_status(di, val);
978 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
979 ret = bq27320_battery_time(di, BQ27x00_REG_TTE, val);
981 // case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
982 // ret = bq27320_battery_time(di, BQ27x00_REG_TTECP, val);
984 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
985 ret = bq27320_battery_time(di, BQ27x00_REG_TTF, val);
994 static int bq27320_get_usb_status(void){
995 int usb_status = 0; // 0--dischage ,1 ---usb charge, 2 ---ac charge
996 int vbus_status = dwc_vbus_status();
998 if (1 == vbus_status) {
999 // if (0 == get_gadget_connect_flag()){
1000 // usb_status = 2; // non-standard AC charger
1002 usb_status = 1; // connect to pc
1004 if (2 == vbus_status)
1005 usb_status = 2; //standard AC charger
1011 static int bq27320_battery_get_status(void)
1014 int usb_ac_charging = 0;
1016 if(dwc_otg_check_dpdm(0) == 0){
1017 bq27320_di->usb_charging = 0;
1018 bq27320_di->ac_charging = 0;
1019 }else if(dwc_otg_check_dpdm(0) == 1){
1020 bq27320_di->usb_charging = 1;
1021 bq27320_di->ac_charging = 0;
1022 }else if(dwc_otg_check_dpdm(0) == 2 || dwc_otg_check_dpdm(0) == 3){
1023 bq27320_di->usb_charging = 0;
1024 bq27320_di->ac_charging = 1;
1026 if(( 1 == bq27320_di->usb_charging)||(1 == bq27320_di ->ac_charging))
1029 if (charge_on == 0){
1030 usb_ac_charging = bq27320_get_usb_status(); //0 --discharge, 1---usb charging,2----AC charging;
1031 if(1 == usb_ac_charging){
1032 bq27320_di->usb_charging = 1;
1033 bq27320_di->ac_charging = 0;
1035 else if(2 == usb_ac_charging){
1036 bq27320_di->usb_charging = 0;
1037 bq27320_di->ac_charging = 1;
1040 bq27320_di->usb_charging = 0;
1041 bq27320_di->ac_charging = 0;
1047 static int rk3190_ac_get_property(struct power_supply *psy,
1048 enum power_supply_property psp,
1049 union power_supply_propval *val)
1054 case POWER_SUPPLY_PROP_ONLINE:
1055 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
1056 val->intval = bq27320_di ->ac_charging;
1063 bq27320_di->online =val->intval;
1064 DBG("%s:rk3190_ac_get_property %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
1069 static int rk3190_usb_get_property(struct power_supply *psy,
1070 enum power_supply_property psp,
1071 union power_supply_propval *val)
1076 case POWER_SUPPLY_PROP_ONLINE:
1077 if (psy->type == POWER_SUPPLY_TYPE_USB)
1078 val->intval = bq27320_di ->usb_charging;
1085 bq27320_di->online =val->intval;
1086 DBG("%s:%d rk3190_usb_get_property val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
1091 static void bq27320_powersupply_init(struct bq27320_device_info *di)
1093 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1094 di->bat.properties = bq27320_battery_props;
1095 di->bat.num_properties = ARRAY_SIZE(bq27320_battery_props);
1096 di->bat.get_property = bq27320_battery_get_property;
1098 di->usb.name = "bq27320-usb";
1099 di->usb.type = POWER_SUPPLY_TYPE_USB;
1100 di->usb.properties = rk3190_usb_props;
1101 di->usb.num_properties = ARRAY_SIZE(rk3190_usb_props);
1102 di->usb.get_property = rk3190_usb_get_property;
1104 di->ac.name = "bq27320-ac";
1105 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1106 di->ac.properties = rk3190_ac_props;
1107 di->ac.num_properties = ARRAY_SIZE(rk3190_ac_props);
1108 di->ac.get_property =rk3190_ac_get_property;
1112 static void bq27320_battery_update_status(struct bq27320_device_info *di)
1115 bq27320_battery_get_status();
1116 power_supply_changed(&di->bat);
1117 power_supply_changed(&di->usb);
1118 power_supply_changed(&di->ac);
1120 #ifdef CONFIG_CHARGER_BQ24161
1121 #include <linux/power/bq24296_charger.h>
1122 static void bq27320_for_charging(struct bq27320_device_info *di)
1124 if ((bq27320_di->usb_charging || bq27320_di->ac_charging) && (di->bat_present == 1)) {//ÓгäµçÆ÷½ÓÈ룬ÇÒµç³Ø´æÔÚ
1125 //tempreture out of safe range, do not charge
1126 if ((di->bat_tempreture < BATTERY_LOW_TEMPRETURE)
1127 || (di->bat_tempreture > BATTERY_HIGH_TEMPRETURE)) {
1128 printk(KERN_INFO "battery tempreture is out of safe range\n");
1130 bq24296_charge_otg_en(0, 0);//disable charging
1134 if (otg_is_host_mode() || mhl_vbus_power_on()) {
1135 printk("**********usb is otg mode, otg lock***********\n");
1136 bq24296_charge_otg_en(1, 1, 1);
1140 bq24296_charge_otg_en(1, 1, 0);
1142 if (di->bat_status==POWER_SUPPLY_STATUS_FULL) {//³äÂú
1144 printk(KERN_INFO "**********charger ok*********\n");
1146 else if ((di->soc_full==1) && (di->rsoc<=BATTERY_RECHARGER_CAPACITY)) {//ÒѳäÂú¹ý£¬ÇÒµçÁ¿Ð¡ÓÚ95%£¬ÐèÒªÐø³ä
1147 bq24296_charge_otg_en(0, 0);
1149 bq24296_charge_otg_en(1, 0);
1151 printk(KERN_INFO "**********recharger*********\n");
1155 else if (otg_is_host_mode() || mhl_vbus_power_on()) {
1156 bq24296_charge_otg_en(1, 0, 1);
1160 bq24296_charge_otg_en();
1168 static void bq27320_battery_work(struct work_struct *work)
1170 struct bq27320_device_info *di = container_of(work, struct bq27320_device_info, work.work);
1171 bq27320_battery_update_status(di);
1172 /* reschedule for the next time */
1173 #ifdef CONFIG_CHARGER_BQ24296
1174 bq27320_for_charging(di);
1176 schedule_delayed_work(&di->work, 1*HZ);
1179 static void bq27320_set(void)
1181 struct bq27320_device_info *di;
1186 printk("enter 0x41\n");
1189 bq27320_write(di->client,0x00,buf,2);
1193 printk("enter 0x21\n");
1196 bq27320_write(di->client,0x00,buf,2);
1200 bq27320_read(di->client,0x00,buf,2);
1202 // printk("%s: Enter:BUF[0]= 0X%x BUF[1] = 0X%x\n",__FUNCTION__,buf[0],buf[1]);
1204 while((buf[0] & 0x04)&&(i<5))
1206 printk("enter more 0x21 times i = %d\n",i);
1210 bq27320_write(di->client,0x00,buf,2);
1214 bq27320_read(di->client,0x00,buf,2);
1219 printk("write 0x21 error\n");
1221 printk("bq27320 write 0x21 success\n");
1225 static int bq27320_battery_suspend(struct i2c_client *client, pm_message_t mesg)
1227 cancel_delayed_work_sync(&bq27320_di->work);
1231 static int bq27320_battery_resume(struct i2c_client *client)
1233 schedule_delayed_work(&bq27320_di->work, msecs_to_jiffies(50));
1237 static int bq27320_is_in_rom_mode(void)
1240 unsigned char data = 0x0f;
1242 bq27320_di->client->addr = BSP_ROM_MODE_I2C_ADDR;
1243 ret = bq27320_write(bq27320_di->client, 0x00, &data, 1);
1244 bq27320_di->client->addr = BSP_NORMAL_MODE_I2C_ADDR;
1253 static struct of_device_id bq27320_battery_of_match[] = {
1254 { .compatible = "ti,bq27320"},
1257 MODULE_DEVICE_TABLE(of, bq27320_battery_of_match);
1260 static int bq27320_battery_probe(struct i2c_client *client,
1261 const struct i2c_device_id *id)
1263 struct bq27320_device_info *di;
1265 struct device_node *bq27320_node;
1268 DBG("%s,line=%d\n", __func__,__LINE__);
1270 bq27320_node = of_node_get(client->dev.of_node);
1271 if (!bq27320_node) {
1272 printk("could not find bq27320-node\n");
1275 di = devm_kzalloc(&client->dev,sizeof(*di), GFP_KERNEL);
1277 dev_err(&client->dev, "failed to allocate device info data\n");
1281 i2c_set_clientdata(client, di);
1282 di->dev = &client->dev;
1283 di->bat.name = "bq27320-battery";
1284 di->client = client;
1285 /* 4 seconds between monotor runs interval */
1286 di->interval = msecs_to_jiffies(4 * 1000);
1287 di->ac_charging = 1;
1288 di->usb_charging =1;
1293 mutex_init(&di->battery_mutex);
1295 retval = bq27320_read(di->client,0x00,buf,2);
1297 printk("The device is not bq27320 %d\n",retval);
1301 bq27320_powersupply_init(di);
1302 retval = power_supply_register(&client->dev, &di->bat);
1304 dev_err(&client->dev, "failed to register battery\n");
1308 retval = power_supply_register(&client->dev, &di->usb);
1310 dev_err(&client->dev, "failed to register ac\n");
1314 retval = power_supply_register(&client->dev, &di->ac);
1316 dev_err(&client->dev, "failed to register ac\n");
1320 g_bq27320_i2c_client = client;
1322 retval = driver_create_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1325 printk("failed to create sysfs entry(state): %d\n", retval);
1329 INIT_DELAYED_WORK(&di->work, bq27320_battery_work);
1330 // schedule_delayed_work(&di->work, di->interval);
1331 schedule_delayed_work(&di->work, 1*HZ);
1332 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
1339 driver_remove_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1345 static int bq27320_battery_remove(struct i2c_client *client)
1347 struct bq27320_device_info *di = i2c_get_clientdata(client);
1349 driver_remove_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1351 power_supply_unregister(&di->bat);
1352 power_supply_unregister(&di->usb);
1353 power_supply_unregister(&di->ac);
1354 kfree(di->bat.name);
1355 kfree(di->usb.name);
1361 static const struct i2c_device_id bq27320_id[] = {
1365 static struct i2c_driver bq27320_battery_driver = {
1368 .of_match_table =of_match_ptr(bq27320_battery_of_match),
1370 .probe = bq27320_battery_probe,
1371 .remove = bq27320_battery_remove,
1372 .suspend = bq27320_battery_suspend,
1373 .resume = bq27320_battery_resume,
1374 .id_table = bq27320_id,
1377 static int __init bq27320_battery_init(void)
1380 struct proc_dir_entry * battery_proc_entry;
1382 ret = i2c_add_driver(&bq27320_battery_driver);
1384 printk(KERN_ERR "Unable to register BQ27320 driver\n");
1386 battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
1389 module_init(bq27320_battery_init);
1391 static void __exit bq27320_battery_exit(void)
1393 i2c_del_driver(&bq27320_battery_driver);
1395 module_exit(bq27320_battery_exit);
1397 MODULE_AUTHOR("Rockchip");
1398 MODULE_DESCRIPTION("BQ27320 battery monitor driver");
1399 MODULE_LICENSE("GPL");