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 (3290*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(void);
110 static void bq27320_set(void);
113 #define DBG(x...) printk(KERN_INFO x)
115 #define DBG(x...) do { } while (0)
118 /* If the system has several batteries we need a different name for each
121 static DEFINE_MUTEX(battery_mutex);
123 static struct bq27320_device_info *bq27320_di;
124 static enum power_supply_property bq27320_battery_props[] = {
125 POWER_SUPPLY_PROP_STATUS,
126 POWER_SUPPLY_PROP_ONLINE,
127 POWER_SUPPLY_PROP_PRESENT,
128 POWER_SUPPLY_PROP_VOLTAGE_NOW,
129 POWER_SUPPLY_PROP_CURRENT_NOW,
130 POWER_SUPPLY_PROP_CAPACITY,
131 POWER_SUPPLY_PROP_TEMP,
132 // POWER_SUPPLY_PROP_TECHNOLOGY,
133 // POWER_SUPPLY_PROP_HEALTH,
134 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
135 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
136 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
139 static enum power_supply_property rk3190_ac_props[] = {
140 POWER_SUPPLY_PROP_ONLINE,
143 static enum power_supply_property rk3190_usb_props[] = {
144 POWER_SUPPLY_PROP_ONLINE,
147 static ssize_t battery_proc_write(struct file *file,const char __user *buffer,
148 size_t count, loff_t *ppos)
153 printk("echo x >/proc/driver/power\n");
154 printk("x=1,means just print log ||x=2,means log and data ||x= other,means close log\n");
156 rc = get_user(c,buffer);
161 virtual_battery_enable = 1;
163 virtual_battery_enable = 2;
165 virtual_battery_enable = 3;
167 virtual_battery_enable = 0;
168 printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
172 static const struct file_operations battery_proc_fops = {
173 .owner = THIS_MODULE,
174 .write = battery_proc_write,
178 * Common code for BQ27320 devices read
181 int bq27320_i2c_master_reg8_read(const struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
183 struct i2c_adapter *adap=client->adapter;
184 struct i2c_msg msgs[2];
188 msgs[0].addr = client->addr;
189 msgs[0].flags = client->flags;
191 msgs[0].buf = ®_buf;
192 msgs[0].scl_rate = scl_rate;
193 // msgs[0].udelay = client->udelay;
195 msgs[1].addr = client->addr;
196 msgs[1].flags = client->flags | I2C_M_RD;
198 msgs[1].buf = (char *)buf;
199 msgs[1].scl_rate = scl_rate;
200 // msgs[1].udelay = client->udelay;
202 ret = i2c_transfer(adap, msgs, 2);
203 return (ret == 2)? count : ret;
205 EXPORT_SYMBOL(bq27320_i2c_master_reg8_read);
207 int bq27320_i2c_master_reg8_write(const struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
209 struct i2c_adapter *adap=client->adapter;
212 char *tx_buf = (char *)kmalloc(count + 1, GFP_KERNEL);
216 memcpy(tx_buf+1, buf, count);
218 msg.addr = client->addr;
219 msg.flags = client->flags;
221 msg.buf = (char *)tx_buf;
222 msg.scl_rate = scl_rate;
223 // msg.udelay = client->udelay;
225 ret = i2c_transfer(adap, &msg, 1);
227 return (ret == 1) ? count : ret;
230 EXPORT_SYMBOL(bq27320_i2c_master_reg8_write);
231 static int bq27320_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
234 mutex_lock(&battery_mutex);
235 ret = bq27320_i2c_master_reg8_read(client, reg, buf, len, BQ27320_SPEED);
236 mutex_unlock(&battery_mutex);
240 static int bq27320_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
243 mutex_lock(&battery_mutex);
244 ret = bq27320_i2c_master_reg8_write(client, reg, buf, (int)len, BQ27320_SPEED);
245 mutex_unlock(&battery_mutex);
249 static int bq27320_read_and_compare(struct i2c_client *client, u8 reg, u8 *pSrcBuf, u8 *pDstBuf, u16 len)
253 i2c_ret = bq27320_read(client, reg, pSrcBuf, len);
256 printk(KERN_ERR "[%s,%d,%08x] bq27320_read failed\n",__FUNCTION__,__LINE__,reg);
260 i2c_ret = strncmp(pDstBuf, pSrcBuf, len);
265 static int bq27320_atoi(const char *s)
270 while (*s != '\0' && *s >= '0' && *s <= '9') {
271 k = 10 * k + (*s - '0');
277 static unsigned long bq27320_strtoul(const char *cp, unsigned int base)
279 unsigned long result = 0,value;
281 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
282 ? toupper(*cp) : *cp)-'A'+10) < base)
284 result = result*base + value;
291 static int bq27320_firmware_program(struct i2c_client *client, const unsigned char *pgm_data, unsigned int filelen)
293 unsigned int i = 0, j = 0, ulDelay = 0, ulReadNum = 0;
294 unsigned int ulCounter = 0, ulLineLen = 0;
295 unsigned char temp = 0;
296 unsigned char *p_cur;
297 unsigned char pBuf[BSP_MAX_ASC_PER_LINE] = { 0 };
298 unsigned char p_src[BSP_I2C_MAX_TRANSFER_LEN] = { 0 };
299 unsigned char p_dst[BSP_I2C_MAX_TRANSFER_LEN] = { 0 };
300 unsigned char ucTmpBuf[16] = { 0 };
302 bq275x0_firmware_program_begin:
308 p_cur = (unsigned char *)pgm_data;
312 if((p_cur - pgm_data) >= filelen)
314 printk("Download success\n");
318 while (*p_cur == '\r' || *p_cur == '\n')
326 memset(p_src, 0x00, sizeof(p_src));
327 memset(p_dst, 0x00, sizeof(p_dst));
328 memset(pBuf, 0x00, sizeof(pBuf));
330 /*»ñÈ¡Ò»ÐÐÊý¾Ý£¬È¥³ý¿Õ¸ñ*/
331 while(i < BSP_MAX_ASC_PER_LINE)
335 if(('\r' == temp) || ('\n' == temp))
341 pBuf[ulLineLen++] = temp;
349 if(('W' == p_src[0]) || ('C' == p_src[0]))
351 for(i=2,j=0; i<ulLineLen; i+=2,j++)
353 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
354 memcpy(ucTmpBuf, pBuf+i, 2);
355 p_src[2+j] = bq27320_strtoul(ucTmpBuf, 16);
358 temp = (ulLineLen -2)/2;
359 ulLineLen = temp + 2;
361 else if('X' == p_src[0])
363 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
364 memcpy(ucTmpBuf, pBuf+2, ulLineLen-2);
365 ulDelay = bq27320_atoi(ucTmpBuf);
367 else if('R' == p_src[0])
369 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
370 memcpy(ucTmpBuf, pBuf+2, 2);
371 p_src[2] = bq27320_strtoul(ucTmpBuf, 16);
372 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
373 memcpy(ucTmpBuf, pBuf+4, 2);
374 p_src[3] = bq27320_strtoul(ucTmpBuf, 16);
375 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
376 memcpy(ucTmpBuf, pBuf+6, ulLineLen-6);
377 ulReadNum = bq27320_atoi(ucTmpBuf);
388 for(i=0; i<ulLineLen-4; i++)
390 printk("%x ", p_src[4+i]);
392 printk(KERN_ERR "\n");
395 if(bq27320_write(client, p_src[3], &p_src[4], ulLineLen-4) < 0)
397 printk(KERN_ERR "[%s,%d] bq27320_write failed len=%d reg+ %08x\n",__FUNCTION__,__LINE__,ulLineLen-4,p_src[3]);
403 if(bq27320_read(client, p_src[3], p_dst, ulReadNum) < 0)
405 printk(KERN_ERR "[%s,%d] bq275x0_i2c_bytes_read failed\n",__FUNCTION__,__LINE__);
410 if(bq27320_read_and_compare(client, p_src[3], p_dst, &p_src[4], ulLineLen-4))
413 printk(KERN_ERR "[%s,%d] bq275x0_i2c_bytes_read_and_compare failed\n",__FUNCTION__,__LINE__);
414 goto bq275x0_firmware_program_begin;
433 static int bq27320_firmware_download(struct i2c_client *client, const unsigned char *pgm_data, unsigned int len)
436 unsigned char ucTmpBuf[2] = { 0 };
438 ucTmpBuf[0] = BSP_ENTER_ROM_MODE_DATA & 0x00ff;
439 ucTmpBuf[1] = (BSP_ENTER_ROM_MODE_DATA>>8) & 0x00ff;
442 iRet = bq27320_write(client, BSP_ENTER_ROM_MODE_CMD, &ucTmpBuf[0], 2);
445 printk(KERN_ERR "[%s,%d] bq27320_write failed\n",__FUNCTION__,__LINE__);
450 g_bq27320_i2c_client->addr = BSP_ROM_MODE_I2C_ADDR;
453 iRet = bq27320_firmware_program(g_bq27320_i2c_client, pgm_data, len);
456 printk(KERN_ERR "[%s,%d] bq275x0_firmware_program failed\n",__FUNCTION__,__LINE__);
460 g_bq27320_i2c_client->addr = BSP_NORMAL_MODE_I2C_ADDR;
466 static int bq27320_update_firmware(struct i2c_client *client, const char *pFilePath)
470 struct inode *inode = NULL;
478 filp = filp_open(pFilePath, O_RDONLY, S_IRUSR);
481 printk(KERN_ERR "[%s,%d] filp_open failed\n",__FUNCTION__,__LINE__);
488 printk(KERN_ERR "[%s,%d] File Operation Method Error\n",__FUNCTION__,__LINE__);
489 filp_close(filp, NULL);
494 inode = filp->f_path.dentry->d_inode;
497 printk(KERN_ERR "[%s,%d] Get inode from filp failed\n",__FUNCTION__,__LINE__);
498 filp_close(filp, NULL);
504 length = i_size_read(inode->i_mapping->host);
505 printk("bq27320 firmware image size is %d \n",length);
506 if (!( length > 0 && length < BSP_FIRMWARE_FILE_SIZE))
508 printk(KERN_ERR "[%s,%d] Get file size error\n",__FUNCTION__,__LINE__);
509 filp_close(filp, NULL);
514 /* allocation buff size */
515 buf = vmalloc(length+(length%2)); /* buf size if even */
518 printk(KERN_ERR "[%s,%d] Alloctation memory failed\n",__FUNCTION__,__LINE__);
519 filp_close(filp, NULL);
525 if (filp->f_op->read(filp, buf, length, &filp->f_pos) != length)
527 printk(KERN_ERR "[%s,%d] File read error\n",__FUNCTION__,__LINE__);
528 filp_close(filp, NULL);
529 filp_close(filp, NULL);
535 ret = bq27320_firmware_download(client, (const char*)buf, length);
540 filp_close(filp, NULL);
547 static u8 get_child_version(void)
552 if(bq27320_write(g_bq27320_i2c_client, 0x3e, data, 1) < 0)
557 if(bq27320_write(g_bq27320_i2c_client, 0x3f, data, 1) < 0)
562 if(bq27320_write(g_bq27320_i2c_client, 0x61, data, 1) < 0)
566 bq27320_read(g_bq27320_i2c_client, 0x60, data, 1);
569 bq27320_read(g_bq27320_i2c_client, 0x40, data, 32);
574 static ssize_t bq27320_attr_store(struct device_driver *driver,const char *buf, size_t count)
577 unsigned char path_image[255];
579 if(NULL == buf || count >255 || count == 0 || strnchr(buf, count, 0x20))
581 memcpy (path_image, buf, count);
582 /* replace '\n' with '\0' */
583 if((path_image[count-1]) == '\n')
584 path_image[count-1] = '\0';
586 path_image[count] = '\0';
588 /*enter firmware bqfs download*/
589 virtual_battery_enable = 1;
590 iRet = bq27320_update_firmware(g_bq27320_i2c_client, path_image);
592 virtual_battery_enable = 0;
595 pr_err("Update firemware finish, then update battery status...");
602 static ssize_t bq27320_attr_show(struct device_driver *driver, char *buf)
611 ver = get_child_version();
615 return sprintf(buf, "%s", "Coulometer Damaged or Firmware Error");
620 return sprintf(buf, "%x", ver);
625 static DRIVER_ATTR(state, 0664, bq27320_attr_show, bq27320_attr_store);
630 * Return the battery temperature in tenths of degree Celsius
631 * Or < 0 if something fails.
633 static int bq27320_battery_temperature(struct bq27320_device_info *di)
639 #if defined (CONFIG_NO_BATTERY_IC)
643 if(virtual_battery_enable == 1)
645 ret = bq27320_read(di->client,BQ27x00_REG_TEMP,buf,2);
647 dev_err(di->dev, "error reading temperature\n");
650 temp = get_unaligned_le16(buf);
652 DBG("Enter:%s--temp = %d\n",__FUNCTION__,temp);
653 di ->bat_tempreture = temp;
658 * Return the battery Voltage in milivolts
659 * Or < 0 if something fails.
661 static int bq27320_battery_voltage(struct bq27320_device_info *di)
667 #if defined (CONFIG_NO_BATTERY_IC)
670 if(virtual_battery_enable == 1)
671 return 2000000/*4000000*/;
673 ret = bq27320_read(di->client,BQ27x00_REG_VOLT,buf,2);
675 dev_err(di->dev, "error reading voltage\n");
678 volt = get_unaligned_le16(buf);
680 //bp27510 can only measure one li-lion bat
681 if(di->bat_num == 2){
682 volt = volt * 1000 * 2;
687 DBG("Enter:%s--volt = %d\n",__FUNCTION__,volt);
692 * Return the battery average current
693 * Note that current can be negative signed as well
694 * Or 0 if something fails.
696 static int bq27320_battery_current(struct bq27320_device_info *di)
702 #if defined (CONFIG_NO_BATTERY_IC)
705 if(virtual_battery_enable == 1)
706 return 11000/*22000*/;
707 ret = bq27320_read(di->client,BQ27x00_REG_AI,buf,2);
709 dev_err(di->dev, "error reading current\n");
713 curr = get_unaligned_le16(buf);
714 DBG("curr = %x \n",curr);
716 curr = 0xFFFF^(curr-1);
717 DBG("curr = -%d \n",curr*1000);
720 DBG("curr = %d \n",curr*1000);
726 * Return the battery Relative State-of-Charge
727 * Or < 0 if something fails.
730 static int bq27320_battery_rsoc(struct bq27320_device_info *di)
735 int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
736 int art = 0, artte = 0, ai = 0, tte = 0, ttf = 0, si = 0;
737 int stte = 0, mli = 0, mltte = 0, ae = 0, ap = 0, ttecp = 0, cc = 0;
741 #if defined (CONFIG_NO_BATTERY_IC)
744 if(virtual_battery_enable == 1)
747 ret = bq27320_read(di->client,BQ27320_REG_SOC,buf,2);
749 dev_err(di->dev, "error reading relative State-of-Charge\n");
752 rsoc = get_unaligned_le16(buf);
753 DBG("Enter:%s --rsoc = %d\n",__FUNCTION__,rsoc);
755 #if defined (CONFIG_NO_BATTERY_IC)
758 #if 0 //other register information, for debug use
759 ret = bq27320_read(di->client,0x0c,buf,2); //NominalAvailableCapacity
760 nvcap = get_unaligned_le16(buf);
761 DBG("\nEnter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
762 ret = bq27320_read(di->client,0x0e,buf,2); //FullAvailableCapacity
763 facap = get_unaligned_le16(buf);
764 DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
765 ret = bq27320_read(di->client,0x10,buf,2); //RemainingCapacity
766 remcap = get_unaligned_le16(buf);
767 DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
768 ret = bq27320_read(di->client,0x12,buf,2); //FullChargeCapacity
769 fccap = get_unaligned_le16(buf);
770 DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
771 ret = bq27320_read(di->client,0x3c,buf,2); //DesignCapacity
772 full = get_unaligned_le16(buf);
773 DBG("Enter:%s %d--DesignCapacity = %d\n",__FUNCTION__,__LINE__,full);
775 buf[0] = 0x00; //CONTROL_STATUS
777 bq27320_write(di->client,0x00,buf,2);
778 ret = bq27320_read(di->client,0x00,buf,2);
779 cnt = get_unaligned_le16(buf);
780 DBG("Enter:%s %d--Control status = %x\n",__FUNCTION__,__LINE__,cnt);
782 ret = bq27320_read(di->client,0x02,buf,2); //AtRate
783 art = get_unaligned_le16(buf);
784 DBG("Enter:%s %d--AtRate = %d\n",__FUNCTION__,__LINE__,art);
785 ret = bq27320_read(di->client,0x04,buf,2); //AtRateTimeToEmpty
786 artte = get_unaligned_le16(buf);
787 DBG("Enter:%s %d--AtRateTimeToEmpty = %d\n",__FUNCTION__,__LINE__,artte);
788 ret = bq27320_read(di->client,0x14,buf,2); //AverageCurrent
789 ai = get_unaligned_le16(buf);
790 DBG("Enter:%s %d--AverageCurrent = %d\n",__FUNCTION__,__LINE__,ai);
791 ret = bq27320_read(di->client,0x16,buf,2); //TimeToEmpty
792 tte = get_unaligned_le16(buf);
793 DBG("Enter:%s %d--TimeToEmpty = %d\n",__FUNCTION__,__LINE__,tte);
794 ret = bq27320_read(di->client,0x18,buf,2); //TimeToFull
795 ttf = get_unaligned_le16(buf);
796 DBG("Enter:%s %d--TimeToFull = %d\n",__FUNCTION__,__LINE__,ttf);
797 ret = bq27320_read(di->client,0x1a,buf,2); //StandbyCurrent
798 si = get_unaligned_le16(buf);
799 DBG("Enter:%s %d--StandbyCurrent = %d\n",__FUNCTION__,__LINE__,si);
800 ret = bq27320_read(di->client,0x1c,buf,2); //StandbyTimeToEmpty
801 stte = get_unaligned_le16(buf);
802 DBG("Enter:%s %d--StandbyTimeToEmpty = %d\n",__FUNCTION__,__LINE__,stte);
803 ret = bq27320_read(di->client,0x1e,buf,2); //MaxLoadCurrent
804 mli = get_unaligned_le16(buf);
805 DBG("Enter:%s %d--MaxLoadCurrent = %d\n",__FUNCTION__,__LINE__,mli);
806 ret = bq27320_read(di->client,0x20,buf,2); //MaxLoadTimeToEmpty
807 mltte = get_unaligned_le16(buf);
808 DBG("Enter:%s %d--MaxLoadTimeToEmpty = %d\n",__FUNCTION__,__LINE__,mltte);
809 ret = bq27320_read(di->client,0x22,buf,2); //AvailableEnergy
810 ae = get_unaligned_le16(buf);
811 DBG("Enter:%s %d--AvailableEnergy = %d\n",__FUNCTION__,__LINE__,ae);
812 ret = bq27320_read(di->client,0x24,buf,2); //AveragePower
813 ap = get_unaligned_le16(buf);
814 DBG("Enter:%s %d--AveragePower = %d\n",__FUNCTION__,__LINE__,ap);
815 ret = bq27320_read(di->client,0x26,buf,2); //TTEatConstantPower
816 ttecp = get_unaligned_le16(buf);
817 DBG("Enter:%s %d--TTEatConstantPower = %d\n",__FUNCTION__,__LINE__,ttecp);
818 ret = bq27320_read(di->client,0x2a,buf,2); //CycleCount
819 cc = get_unaligned_le16(buf);
820 DBG("Enter:%s %d--CycleCount = %d\n",__FUNCTION__,__LINE__,cc);
825 static int bq27320_battery_status(struct bq27320_device_info *di,
826 union power_supply_propval *val)
833 #if defined (CONFIG_NO_BATTERY_IC)
834 val->intval = POWER_SUPPLY_STATUS_FULL;
838 if(virtual_battery_enable == 1)
840 val->intval = POWER_SUPPLY_STATUS_FULL;
844 ret = bq27320_read(di->client,BQ27x00_REG_BATTERYSTATUS, buf, 2);
846 dev_err(di->dev, "error reading flags\n");
850 flags = get_unaligned_le16(buf);
851 DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
853 if ((flags & BQ27320_BATTERYSTATUS_FC) ||(bq27320_di ->rsoc ==100)){
854 status = POWER_SUPPLY_STATUS_FULL;
856 DBG("status =POWER_SUPPLY_STATUS_FULL \n");
858 else if (flags & BQ27320_BATTERYSTATUS_DSC){
859 status = POWER_SUPPLY_STATUS_DISCHARGING;
860 DBG("status =POWER_SUPPLY_STATUS_DISCHARGING \n");
863 status = POWER_SUPPLY_STATUS_CHARGING;
864 DBG("status =POWER_SUPPLY_STATUS_CHARGING \n");
867 if (((status==POWER_SUPPLY_STATUS_FULL)||(status==POWER_SUPPLY_STATUS_CHARGING))
868 && ((bq27320_di->ac_charging ==0) && (bq27320_di->usb_charging ==0) ))
869 status = POWER_SUPPLY_STATUS_DISCHARGING;
870 else if ((status==POWER_SUPPLY_STATUS_DISCHARGING) && ((bq27320_di->ac_charging ==1) || (bq27320_di->usb_charging ==1) ))
871 status = POWER_SUPPLY_STATUS_CHARGING;
873 di ->bat_status = status;
874 val->intval = status;
878 static int bq27320_health_status(struct bq27320_device_info *di,
879 union power_supply_propval *val)
886 #if defined (CONFIG_NO_BATTERY_IC)
887 val->intval = POWER_SUPPLY_HEALTH_GOOD;
891 if(virtual_battery_enable == 1)
893 val->intval = POWER_SUPPLY_HEALTH_GOOD;
896 ret = bq27320_read(di->client,BQ27x00_REG_BATTERYSTATUS, buf, 2);
898 dev_err(di->dev, "error reading flags\n");
901 flags = get_unaligned_le16(buf);
902 DBG("Enter:%s--health status = %x\n",__FUNCTION__,flags);
903 if ((flags & BQ27320_BATTERYSTATUS_OTD)||(flags & BQ27320_BATTERYSTATUS_OTC)){
904 status = POWER_SUPPLY_HEALTH_OVERHEAT;
905 DBG("health =POWER_SUPPLY_HEALTH_OVERHEAT \n");
908 status = POWER_SUPPLY_HEALTH_GOOD;
909 DBG("health =POWER_SUPPLY_HEALTH_GOOD \n");
912 val->intval = status;
918 * Read a time register.
919 * Return < 0 if something fails.
921 static int bq27320_battery_time(struct bq27320_device_info *di, int reg,
922 union power_supply_propval *val)
928 ret = bq27320_read(di->client,reg,buf,2);
930 dev_err(di->dev, "error reading register %02x\n", reg);
933 tval = get_unaligned_le16(buf);
934 DBG("Enter:%s--tval=%d\n",__FUNCTION__,tval);
938 val->intval = tval * 60;
939 DBG("Enter:%s val->intval = %d\n",__FUNCTION__,val->intval);
943 #define to_bq27320_device_info(x) container_of((x), \
944 struct bq27320_device_info, bat);
946 static int bq27320_battery_get_property(struct power_supply *psy,
947 enum power_supply_property psp,
948 union power_supply_propval *val)
951 struct bq27320_device_info *di = to_bq27320_device_info(psy);
952 DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
956 case POWER_SUPPLY_PROP_STATUS:
957 ret = bq27320_battery_status(di, val);
959 case POWER_SUPPLY_PROP_ONLINE:
960 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
961 val->intval = bq27320_di ->ac_charging;
962 else if (psy->type == POWER_SUPPLY_TYPE_USB)
963 val->intval = bq27320_di ->usb_charging;
965 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
966 val->intval = bq27320_battery_voltage(di);
968 case POWER_SUPPLY_PROP_PRESENT:
969 val->intval = bq27320_battery_voltage(di);
970 val->intval = val->intval <= 0 ? 0 : 1;
971 di->bat_present =val->intval;
973 case POWER_SUPPLY_PROP_CURRENT_NOW:
974 val->intval = bq27320_battery_current(di);
976 case POWER_SUPPLY_PROP_CAPACITY:
977 val->intval = bq27320_battery_rsoc(di);
979 case POWER_SUPPLY_PROP_TEMP:
980 val->intval = bq27320_battery_temperature(di);
982 case POWER_SUPPLY_PROP_TECHNOLOGY:
983 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
985 case POWER_SUPPLY_PROP_HEALTH:
986 ret = bq27320_health_status(di, val);
988 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
989 ret = bq27320_battery_time(di, BQ27x00_REG_TTE, val);
991 // case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
992 // ret = bq27320_battery_time(di, BQ27x00_REG_TTECP, val);
994 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
995 ret = bq27320_battery_time(di, BQ27x00_REG_TTF, val);
1004 static int bq27320_get_usb_status(void){
1005 int usb_status = 0; // 0--dischage ,1 ---usb charge, 2 ---ac charge
1006 int vbus_status = dwc_vbus_status();
1008 if (1 == vbus_status) {
1009 // if (0 == get_gadget_connect_flag()){
1010 // usb_status = 2; // non-standard AC charger
1012 usb_status = 1; // connect to pc
1014 if (2 == vbus_status)
1015 usb_status = 2; //standard AC charger
1021 static int bq27320_battery_get_status(void)
1024 int usb_ac_charging = 0;
1026 if(dwc_otg_check_dpdm() == 0){
1027 bq27320_di->usb_charging = 0;
1028 bq27320_di->ac_charging = 0;
1029 }else if(dwc_otg_check_dpdm() == 1){
1030 bq27320_di->usb_charging = 1;
1031 bq27320_di->ac_charging = 0;
1032 }else if(dwc_otg_check_dpdm() == 2 || dwc_otg_check_dpdm() == 3){
1033 bq27320_di->usb_charging = 0;
1034 bq27320_di->ac_charging = 1;
1036 if(( 1 == bq27320_di->usb_charging)||(1 == bq27320_di ->ac_charging))
1039 if (charge_on == 0){
1040 usb_ac_charging = bq27320_get_usb_status(); //0 --discharge, 1---usb charging,2----AC charging;
1041 if(1 == usb_ac_charging){
1042 bq27320_di->usb_charging = 1;
1043 bq27320_di->ac_charging = 0;
1045 else if(2 == usb_ac_charging){
1046 bq27320_di->usb_charging = 0;
1047 bq27320_di->ac_charging = 1;
1050 bq27320_di->usb_charging = 0;
1051 bq27320_di->ac_charging = 0;
1057 static int rk3190_ac_get_property(struct power_supply *psy,
1058 enum power_supply_property psp,
1059 union power_supply_propval *val)
1064 case POWER_SUPPLY_PROP_ONLINE:
1065 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
1066 val->intval = bq27320_di ->ac_charging;
1073 bq27320_di->online =val->intval;
1074 DBG("%s:rk3190_ac_get_property %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
1079 static int rk3190_usb_get_property(struct power_supply *psy,
1080 enum power_supply_property psp,
1081 union power_supply_propval *val)
1086 case POWER_SUPPLY_PROP_ONLINE:
1087 if (psy->type == POWER_SUPPLY_TYPE_USB)
1088 val->intval = bq27320_di ->usb_charging;
1095 bq27320_di->online =val->intval;
1096 DBG("%s:%d rk3190_usb_get_property val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
1101 static void bq27320_powersupply_init(struct bq27320_device_info *di)
1103 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1104 di->bat.properties = bq27320_battery_props;
1105 di->bat.num_properties = ARRAY_SIZE(bq27320_battery_props);
1106 di->bat.get_property = bq27320_battery_get_property;
1108 di->usb.name = "bq27320-usb";
1109 di->usb.type = POWER_SUPPLY_TYPE_USB;
1110 di->usb.properties = rk3190_usb_props;
1111 di->usb.num_properties = ARRAY_SIZE(rk3190_usb_props);
1112 di->usb.get_property = rk3190_usb_get_property;
1114 di->ac.name = "bq27320-ac";
1115 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1116 di->ac.properties = rk3190_ac_props;
1117 di->ac.num_properties = ARRAY_SIZE(rk3190_ac_props);
1118 di->ac.get_property =rk3190_ac_get_property;
1122 static void bq27320_battery_update_status(struct bq27320_device_info *di)
1125 bq27320_battery_get_status();
1126 power_supply_changed(&di->bat);
1127 power_supply_changed(&di->usb);
1128 power_supply_changed(&di->ac);
1130 #ifdef CONFIG_CHARGER_BQ24161
1131 #include <linux/power/bq24296_charger.h>
1132 static void bq27320_for_charging(struct bq27320_device_info *di)
1134 if ((bq27320_di->usb_charging || bq27320_di->ac_charging) && (di->bat_present == 1)) {//ÓгäµçÆ÷½ÓÈ룬ÇÒµç³Ø´æÔÚ
1135 //tempreture out of safe range, do not charge
1136 if ((di->bat_tempreture < BATTERY_LOW_TEMPRETURE)
1137 || (di->bat_tempreture > BATTERY_HIGH_TEMPRETURE)) {
1138 printk(KERN_INFO "battery tempreture is out of safe range\n");
1140 bq24296_charge_otg_en(0, 0);//disable charging
1144 if (otg_is_host_mode() || mhl_vbus_power_on()) {
1145 printk("**********usb is otg mode, otg lock***********\n");
1146 bq24296_charge_otg_en(1, 1, 1);
1150 bq24296_charge_otg_en(1, 1, 0);
1152 if (di->bat_status==POWER_SUPPLY_STATUS_FULL) {//³äÂú
1154 printk(KERN_INFO "**********charger ok*********\n");
1156 else if ((di->soc_full==1) && (di->rsoc<=BATTERY_RECHARGER_CAPACITY)) {//ÒѳäÂú¹ý£¬ÇÒµçÁ¿Ð¡ÓÚ95%£¬ÐèÒªÐø³ä
1157 bq24296_charge_otg_en(0, 0);
1159 bq24296_charge_otg_en(1, 0);
1161 printk(KERN_INFO "**********recharger*********\n");
1165 else if (otg_is_host_mode() || mhl_vbus_power_on()) {
1166 bq24296_charge_otg_en(1, 0, 1);
1170 bq24296_charge_otg_en();
1178 static void bq27320_battery_work(struct work_struct *work)
1180 struct bq27320_device_info *di = container_of(work, struct bq27320_device_info, work.work);
1181 bq27320_battery_update_status(di);
1182 /* reschedule for the next time */
1183 #ifdef CONFIG_CHARGER_BQ24296
1184 bq27320_for_charging(di);
1186 schedule_delayed_work(&di->work, 1*HZ);
1189 static void bq27320_set(void)
1191 struct bq27320_device_info *di;
1196 printk("enter 0x41\n");
1199 bq27320_write(di->client,0x00,buf,2);
1203 printk("enter 0x21\n");
1206 bq27320_write(di->client,0x00,buf,2);
1210 bq27320_read(di->client,0x00,buf,2);
1212 // printk("%s: Enter:BUF[0]= 0X%x BUF[1] = 0X%x\n",__FUNCTION__,buf[0],buf[1]);
1214 while((buf[0] & 0x04)&&(i<5))
1216 printk("enter more 0x21 times i = %d\n",i);
1220 bq27320_write(di->client,0x00,buf,2);
1224 bq27320_read(di->client,0x00,buf,2);
1229 printk("write 0x21 error\n");
1231 printk("bq27320 write 0x21 success\n");
1235 static int bq27320_battery_suspend(struct i2c_client *client, pm_message_t mesg)
1237 cancel_delayed_work_sync(&bq27320_di->work);
1241 static int bq27320_battery_resume(struct i2c_client *client)
1243 schedule_delayed_work(&bq27320_di->work, msecs_to_jiffies(50));
1246 static int bq27320_is_in_rom_mode(void)
1249 unsigned char data = 0x0f;
1251 bq27320_di->client->addr = BSP_ROM_MODE_I2C_ADDR;
1252 ret = bq27320_write(bq27320_di->client, 0x00, &data, 1);
1253 bq27320_di->client->addr = BSP_NORMAL_MODE_I2C_ADDR;
1262 static struct of_device_id bq27320_battery_of_match[] = {
1263 { .compatible = "ti,bq27320"},
1266 MODULE_DEVICE_TABLE(of, bq27320_battery_of_match);
1269 static int bq27320_battery_probe(struct i2c_client *client,
1270 const struct i2c_device_id *id)
1272 struct bq27320_device_info *di;
1274 struct bq27320_board *pdev;
1275 struct device_node *bq27320_node;
1277 DBG("%s,line=%d\n", __func__,__LINE__);
1279 bq27320_node = of_node_get(client->dev.of_node);
1280 if (!bq27320_node) {
1281 printk("could not find bq27320-node\n");
1284 di = devm_kzalloc(&client->dev,sizeof(*di), GFP_KERNEL);
1286 dev_err(&client->dev, "failed to allocate device info data\n");
1290 i2c_set_clientdata(client, di);
1291 di->dev = &client->dev;
1292 di->bat.name = "bq27320-battery";
1293 di->client = client;
1294 /* 4 seconds between monotor runs interval */
1295 di->interval = msecs_to_jiffies(4 * 1000);
1296 di->ac_charging = 1;
1297 di->usb_charging =1;
1302 mutex_init(&di->battery_mutex);
1304 bq27320_powersupply_init(di);
1305 retval = power_supply_register(&client->dev, &di->bat);
1307 dev_err(&client->dev, "failed to register battery\n");
1311 retval = power_supply_register(&client->dev, &di->usb);
1313 dev_err(&client->dev, "failed to register ac\n");
1317 retval = power_supply_register(&client->dev, &di->ac);
1319 dev_err(&client->dev, "failed to register ac\n");
1323 g_bq27320_i2c_client = client;
1325 retval = driver_create_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1328 printk("failed to create sysfs entry(state): %d\n", retval);
1332 INIT_DELAYED_WORK(&di->work, bq27320_battery_work);
1333 // schedule_delayed_work(&di->work, di->interval);
1334 schedule_delayed_work(&di->work, 1*HZ);
1335 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
1342 driver_remove_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1348 static int bq27320_battery_remove(struct i2c_client *client)
1350 struct bq27320_device_info *di = i2c_get_clientdata(client);
1352 driver_remove_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1354 power_supply_unregister(&di->bat);
1355 power_supply_unregister(&di->usb);
1356 power_supply_unregister(&di->ac);
1357 kfree(di->bat.name);
1358 kfree(di->usb.name);
1364 static const struct i2c_device_id bq27320_id[] = {
1368 static struct i2c_driver bq27320_battery_driver = {
1371 .of_match_table =of_match_ptr(bq27320_battery_of_match),
1373 .probe = bq27320_battery_probe,
1374 .remove = bq27320_battery_remove,
1375 .suspend = bq27320_battery_suspend,
1376 .resume = bq27320_battery_resume,
1377 .id_table = bq27320_id,
1380 static int __init bq27320_battery_init(void)
1383 struct proc_dir_entry * battery_proc_entry;
1385 ret = i2c_add_driver(&bq27320_battery_driver);
1387 printk(KERN_ERR "Unable to register BQ27320 driver\n");
1389 battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
1392 module_init(bq27320_battery_init);
1394 static void __exit bq27320_battery_exit(void)
1396 i2c_del_driver(&bq27320_battery_driver);
1398 module_exit(bq27320_battery_exit);
1400 MODULE_AUTHOR("Rockchip");
1401 MODULE_DESCRIPTION("BQ27320 battery monitor driver");
1402 MODULE_LICENSE("GPL");