rk31:linux3.10:support bq27320 fg and bq24296 charger ic
[firefly-linux-kernel-4.4.55.git] / drivers / power / bq27320_battery.c
1 /*
2  * BQ27320 battery driver
3  *
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.
7  *
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.
11  *
12  */
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>
27 #include <linux/fs.h>
28 #include <linux/ctype.h>
29 #include <linux/vmalloc.h>
30
31 #include <linux/interrupt.h>
32 #include <linux/module.h>
33 #include <linux/of_irq.h>
34 #include <linux/of_gpio.h>
35 #include <linux/of.h>
36 #include <linux/of_device.h>
37
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
46
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)
54
55 #define BQ27320_SPEED                   100 * 1000
56
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)
65
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
72
73 struct bq27320_device_info {
74         struct device           *dev;
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;
82         unsigned int bat_num;
83         unsigned                ac_charging;
84         unsigned                usb_charging;
85         unsigned                online;
86         unsigned int irq_pin;
87         int soc_full;
88         int rsoc;
89         int bat_tempreture;
90         int bat_status;
91         int bat_present;
92         struct workqueue_struct *workqueue;     
93         struct delayed_work     chg_down_work;
94         struct mutex    battery_mutex;
95         
96 };
97 struct bq27320_board {
98         unsigned int irq_pin;
99         struct device_node *of_node;
100 };
101
102 struct i2c_client* g_bq27320_i2c_client = NULL;
103 static struct i2c_driver bq27320_battery_driver;
104
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);
109
110 static void bq27320_set(void);
111
112 #if 0
113 #define DBG(x...) printk(KERN_INFO x)
114 #else
115 #define DBG(x...) do { } while (0)
116 #endif
117
118 /* If the system has several batteries we need a different name for each
119  * of them...
120  */
121 static DEFINE_MUTEX(battery_mutex);
122
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,
137 };
138
139 static enum power_supply_property rk3190_ac_props[] = {
140         POWER_SUPPLY_PROP_ONLINE,
141 };
142
143 static enum power_supply_property rk3190_usb_props[] = {
144         POWER_SUPPLY_PROP_ONLINE,
145 };
146
147 static ssize_t battery_proc_write(struct file *file,const char __user *buffer,
148                          size_t count, loff_t *ppos)
149 {
150         char c;
151         int rc;
152         printk("USER:\n");
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");
155
156         rc = get_user(c,buffer);
157         if(rc)
158                 return rc;
159                 
160         if(c == '1')
161                 virtual_battery_enable = 1;
162         else if(c == '2')
163                 virtual_battery_enable = 2;
164         else if(c == '3')
165                 virtual_battery_enable = 3;
166         else 
167                 virtual_battery_enable = 0;
168         printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
169         return count;
170 }
171
172 static const struct file_operations battery_proc_fops = {
173         .owner          = THIS_MODULE, 
174         .write          = battery_proc_write,
175 }; 
176
177 /*
178  * Common code for BQ27320 devices read
179  */
180
181  int bq27320_i2c_master_reg8_read(const struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
182 {
183         struct i2c_adapter *adap=client->adapter;
184         struct i2c_msg msgs[2];
185         int ret;
186         char reg_buf = reg;
187         
188         msgs[0].addr = client->addr;
189         msgs[0].flags = client->flags;
190         msgs[0].len = 1;
191         msgs[0].buf = &reg_buf;
192         msgs[0].scl_rate = scl_rate;
193 //      msgs[0].udelay = client->udelay;
194
195         msgs[1].addr = client->addr;
196         msgs[1].flags = client->flags | I2C_M_RD;
197         msgs[1].len = count;
198         msgs[1].buf = (char *)buf;
199         msgs[1].scl_rate = scl_rate;
200 //      msgs[1].udelay = client->udelay;
201
202         ret = i2c_transfer(adap, msgs, 2);
203         return (ret == 2)? count : ret;
204 }
205 EXPORT_SYMBOL(bq27320_i2c_master_reg8_read);
206
207 int bq27320_i2c_master_reg8_write(const struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
208 {
209         struct i2c_adapter *adap=client->adapter;
210         struct i2c_msg msg;
211         int ret;
212         char *tx_buf = (char *)kmalloc(count + 1, GFP_KERNEL);
213         if(!tx_buf)
214                 return -ENOMEM;
215         tx_buf[0] = reg;
216         memcpy(tx_buf+1, buf, count); 
217
218         msg.addr = client->addr;
219         msg.flags = client->flags;
220         msg.len = count + 1;
221         msg.buf = (char *)tx_buf;
222         msg.scl_rate = scl_rate;
223 //      msg.udelay = client->udelay;
224
225         ret = i2c_transfer(adap, &msg, 1);
226         kfree(tx_buf);
227         return (ret == 1) ? count : ret;
228
229 }
230 EXPORT_SYMBOL(bq27320_i2c_master_reg8_write);
231 static int bq27320_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
232 {
233         int ret;
234         mutex_lock(&battery_mutex);
235         ret = bq27320_i2c_master_reg8_read(client, reg, buf, len, BQ27320_SPEED);
236         mutex_unlock(&battery_mutex);
237         return ret; 
238 }
239
240 static int bq27320_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
241 {
242         int ret; 
243         mutex_lock(&battery_mutex);
244         ret = bq27320_i2c_master_reg8_write(client, reg, buf, (int)len, BQ27320_SPEED);
245         mutex_unlock(&battery_mutex);
246         return ret;
247 }
248 #if 1
249 static int bq27320_read_and_compare(struct i2c_client *client, u8 reg, u8 *pSrcBuf, u8 *pDstBuf, u16 len)
250 {
251         int i2c_ret;
252
253         i2c_ret = bq27320_read(client, reg, pSrcBuf, len);
254         if(i2c_ret < 0)
255         {
256                 printk(KERN_ERR "[%s,%d,%08x] bq27320_read failed\n",__FUNCTION__,__LINE__,reg);
257                 return i2c_ret;
258         }
259
260         i2c_ret = strncmp(pDstBuf, pSrcBuf, len);
261
262         return i2c_ret;
263 }
264
265 static int bq27320_atoi(const char *s)
266 {
267         int k = 0;
268
269         k = 0;
270         while (*s != '\0' && *s >= '0' && *s <= '9') {
271                 k = 10 * k + (*s - '0');
272                 s++;
273         }
274         return k;
275 }
276
277 static unsigned long bq27320_strtoul(const char *cp, unsigned int base)
278 {
279         unsigned long result = 0,value;
280
281         while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
282                 ? toupper(*cp) : *cp)-'A'+10) < base) 
283         {
284                 result = result*base + value;
285                 cp++;
286         }
287
288         return result;
289 }
290
291 static int bq27320_firmware_program(struct i2c_client *client, const unsigned char *pgm_data, unsigned int filelen)
292 {
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 };
301
302 bq275x0_firmware_program_begin:
303         if(ulCounter > 10)
304         {
305                 return -1;
306         }
307         
308         p_cur = (unsigned char *)pgm_data;               
309
310         while(1)
311         {
312                 if((p_cur - pgm_data) >= filelen)
313                 {
314                         printk("Download success\n");
315                         break;
316                 }
317                         
318                 while (*p_cur == '\r' || *p_cur == '\n')
319                 {
320                         p_cur++;
321                 }
322                 
323                 i = 0;
324                 ulLineLen = 0;
325
326                 memset(p_src, 0x00, sizeof(p_src));
327                 memset(p_dst, 0x00, sizeof(p_dst));
328                 memset(pBuf, 0x00, sizeof(pBuf));
329
330                 /*»ñÈ¡Ò»ÐÐÊý¾Ý£¬È¥³ý¿Õ¸ñ*/
331                 while(i < BSP_MAX_ASC_PER_LINE)
332                 {
333                         temp = *p_cur++;          
334                         i++;
335                         if(('\r' == temp) || ('\n' == temp))
336                         {
337                                 break;  
338                         }
339                         if(' ' != temp)
340                         {
341                                 pBuf[ulLineLen++] = temp;
342                         }
343                 }
344
345                 
346                 p_src[0] = pBuf[0];
347                 p_src[1] = pBuf[1];
348
349                 if(('W' == p_src[0]) || ('C' == p_src[0]))
350                 {
351                         for(i=2,j=0; i<ulLineLen; i+=2,j++)
352                         {
353                                 memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
354                                 memcpy(ucTmpBuf, pBuf+i, 2);
355                                 p_src[2+j] = bq27320_strtoul(ucTmpBuf, 16);
356                         }
357
358                         temp = (ulLineLen -2)/2;
359                         ulLineLen = temp + 2;
360                 }
361                 else if('X' == p_src[0])
362                 {
363                         memset(ucTmpBuf, 0x00, sizeof(ucTmpBuf));
364                         memcpy(ucTmpBuf, pBuf+2, ulLineLen-2);
365                         ulDelay = bq27320_atoi(ucTmpBuf);
366                 }
367                 else if('R' == p_src[0])
368                 {
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);
378                 }
379
380                 if(':' == p_src[1])
381                 {
382                         switch(p_src[0])
383                         {
384                                 case 'W' :
385
386                                         #if 0
387                                         printk("W: ");
388                                         for(i=0; i<ulLineLen-4; i++)
389                                         {
390                                                 printk("%x ", p_src[4+i]);
391                                         }
392                                         printk(KERN_ERR "\n");
393                                         #endif                                    
394
395                                         if(bq27320_write(client, p_src[3], &p_src[4], ulLineLen-4) < 0)
396                                         {
397                                                  printk(KERN_ERR "[%s,%d] bq27320_write failed len=%d reg+ %08x\n",__FUNCTION__,__LINE__,ulLineLen-4,p_src[3]);                                           
398                                         }
399
400                                         break;
401                                 
402                                 case 'R' :
403                                         if(bq27320_read(client, p_src[3], p_dst, ulReadNum) < 0)
404                                         {
405                                                 printk(KERN_ERR "[%s,%d] bq275x0_i2c_bytes_read failed\n",__FUNCTION__,__LINE__);
406                                         }
407                                         break;
408                                         
409                                 case 'C' :
410                                         if(bq27320_read_and_compare(client, p_src[3], p_dst, &p_src[4], ulLineLen-4))
411                                         {
412                                                 ulCounter++;
413                                                 printk(KERN_ERR "[%s,%d] bq275x0_i2c_bytes_read_and_compare failed\n",__FUNCTION__,__LINE__);
414                                                 goto bq275x0_firmware_program_begin;
415                                         }
416                                         break;
417                                         
418                                 case 'X' :                                        
419                                         mdelay(ulDelay);
420                                         break;
421                                   
422                                 default:
423                                         return 0;
424                         }
425                 }
426           
427         }
428
429         return 0;
430         
431 }
432
433 static int bq27320_firmware_download(struct i2c_client *client, const unsigned char *pgm_data, unsigned int len)
434 {
435         int iRet;
436         unsigned char ucTmpBuf[2] = { 0 };
437
438         ucTmpBuf[0] = BSP_ENTER_ROM_MODE_DATA & 0x00ff;
439         ucTmpBuf[1] = (BSP_ENTER_ROM_MODE_DATA>>8) & 0x00ff;
440         
441         /*Enter Rom Mode */
442         iRet = bq27320_write(client, BSP_ENTER_ROM_MODE_CMD, &ucTmpBuf[0], 2);
443         if(0 > iRet)
444         {
445                 printk(KERN_ERR "[%s,%d] bq27320_write failed\n",__FUNCTION__,__LINE__);
446         }
447         mdelay(10);
448
449         /*change i2c addr*/
450         g_bq27320_i2c_client->addr = BSP_ROM_MODE_I2C_ADDR;
451
452         /*program bqfs*/
453         iRet = bq27320_firmware_program(g_bq27320_i2c_client, pgm_data, len);
454         if(0 != iRet)
455         {
456                 printk(KERN_ERR "[%s,%d] bq275x0_firmware_program failed\n",__FUNCTION__,__LINE__);
457         }
458
459         /*change i2c addr*/
460         g_bq27320_i2c_client->addr = BSP_NORMAL_MODE_I2C_ADDR;
461
462         return iRet;
463         
464 }
465
466 static int bq27320_update_firmware(struct i2c_client *client, const char *pFilePath) 
467 {
468         char *buf;
469         struct file *filp;
470         struct inode *inode = NULL;
471         mm_segment_t oldfs;
472         unsigned int length;
473         int ret = 0;
474
475         /* open file */
476         oldfs = get_fs();
477         set_fs(KERNEL_DS);
478         filp = filp_open(pFilePath, O_RDONLY, S_IRUSR);
479         if (IS_ERR(filp)) 
480         {
481                 printk(KERN_ERR "[%s,%d] filp_open failed\n",__FUNCTION__,__LINE__);
482                 set_fs(oldfs);
483                 return -1;
484         }
485
486         if (!filp->f_op) 
487         {
488                 printk(KERN_ERR "[%s,%d] File Operation Method Error\n",__FUNCTION__,__LINE__);            
489                 filp_close(filp, NULL);
490                 set_fs(oldfs);
491                 return -1;
492         }
493
494         inode = filp->f_path.dentry->d_inode;
495         if (!inode) 
496         {
497                 printk(KERN_ERR "[%s,%d] Get inode from filp failed\n",__FUNCTION__,__LINE__);                  
498                 filp_close(filp, NULL);
499                 set_fs(oldfs);
500                 return -1;
501         }
502
503         /* file's size */
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))
507         {
508                 printk(KERN_ERR "[%s,%d] Get file size error\n",__FUNCTION__,__LINE__);
509                 filp_close(filp, NULL);
510                 set_fs(oldfs);
511                 return -1;
512         }
513
514         /* allocation buff size */
515         buf = vmalloc(length+(length%2));               /* buf size if even */
516         if (!buf) 
517         {
518                 printk(KERN_ERR "[%s,%d] Alloctation memory failed\n",__FUNCTION__,__LINE__);
519                 filp_close(filp, NULL);
520                 set_fs(oldfs);
521                 return -1;
522         }
523
524         /* read data */
525         if (filp->f_op->read(filp, buf, length, &filp->f_pos) != length) 
526         {
527                 printk(KERN_ERR "[%s,%d] File read error\n",__FUNCTION__,__LINE__);
528                 filp_close(filp, NULL);
529                 filp_close(filp, NULL);
530                 set_fs(oldfs);
531                 vfree(buf);
532                 return -1;
533         }
534
535         ret = bq27320_firmware_download(client, (const char*)buf, length);
536
537         //if(0 == ret)
538                 //ret = 1;
539
540         filp_close(filp, NULL);
541         set_fs(oldfs);
542         vfree(buf);
543         
544         return ret;
545 }
546
547 static u8 get_child_version(void)
548 {
549         u8 data[32];
550         
551         data[0] = 0x39;
552         if(bq27320_write(g_bq27320_i2c_client, 0x3e, data, 1) < 0)
553                 return -1;
554         mdelay(2);
555
556         data[0] = 0x00;
557         if(bq27320_write(g_bq27320_i2c_client, 0x3f, data, 1) < 0)
558                 return -1;
559         mdelay(2);
560
561         data[0] = 0x00;
562         if(bq27320_write(g_bq27320_i2c_client, 0x61, data, 1) < 0)
563                 return -1;
564         mdelay(2);
565
566         bq27320_read(g_bq27320_i2c_client, 0x60, data, 1);
567         mdelay(2);
568
569         bq27320_read(g_bq27320_i2c_client, 0x40, data, 32);
570         
571         return data[0];
572 }
573
574 static ssize_t bq27320_attr_store(struct device_driver *driver,const char *buf, size_t count)
575 {
576         int iRet = 0;
577         unsigned char path_image[255];
578
579         if(NULL == buf || count >255 || count == 0 || strnchr(buf, count, 0x20))
580                 return -1;
581         memcpy (path_image, buf,  count);
582         /* replace '\n' with  '\0'      */ 
583         if((path_image[count-1]) == '\n')
584                 path_image[count-1] = '\0'; 
585         else
586                 path_image[count] = '\0';               
587
588         /*enter firmware bqfs download*/
589         virtual_battery_enable = 1;
590         iRet = bq27320_update_firmware(g_bq27320_i2c_client, path_image);               
591         msleep(3000);
592         virtual_battery_enable = 0;
593
594         if (iRet == 0) {
595                 pr_err("Update firemware finish, then update battery status...");               
596                 return count;
597         }
598
599         return iRet;
600 }
601
602 static ssize_t bq27320_attr_show(struct device_driver *driver, char *buf)
603 {
604         u8 ver;
605         
606         if(NULL == buf)
607         {
608                 return -1;
609         }
610
611         ver = get_child_version();
612
613         if(ver < 0)
614         {
615                 return sprintf(buf, "%s", "Coulometer Damaged or Firmware Error");
616         }
617         else
618         {        
619         
620                 return sprintf(buf, "%x", ver);
621         }
622
623 }
624
625 static DRIVER_ATTR(state, 0664, bq27320_attr_show, bq27320_attr_store);
626
627
628 #endif
629 /*
630  * Return the battery temperature in tenths of degree Celsius
631  * Or < 0 if something fails.
632  */
633 static int bq27320_battery_temperature(struct bq27320_device_info *di)
634 {
635         int ret;
636         int temp = 0;
637         u8 buf[2];
638
639         #if defined (CONFIG_NO_BATTERY_IC)
640         return 258;
641         #endif
642
643         if(virtual_battery_enable == 1)
644                 return 125/*258*/;
645         ret = bq27320_read(di->client,BQ27x00_REG_TEMP,buf,2);
646         if (ret<0) {
647                 dev_err(di->dev, "error reading temperature\n");
648                 return ret;
649         }
650         temp = get_unaligned_le16(buf);
651         temp = temp - 2731;
652         DBG("Enter:%s--temp = %d\n",__FUNCTION__,temp);
653         di ->bat_tempreture = temp;
654         return temp;
655 }
656
657 /*
658  * Return the battery Voltage in milivolts
659  * Or < 0 if something fails.
660  */
661 static int bq27320_battery_voltage(struct bq27320_device_info *di)
662 {
663         int ret;
664         u8 buf[2];
665         int volt = 0;
666
667         #if defined (CONFIG_NO_BATTERY_IC)
668                 return 4000000;
669         #endif
670         if(virtual_battery_enable == 1)
671                 return 2000000/*4000000*/;
672
673         ret = bq27320_read(di->client,BQ27x00_REG_VOLT,buf,2); 
674         if (ret<0) {
675                 dev_err(di->dev, "error reading voltage\n");
676                 return ret;
677         }
678         volt = get_unaligned_le16(buf);
679
680         //bp27510 can only measure one li-lion bat
681         if(di->bat_num == 2){
682                 volt = volt * 1000 * 2;
683         }else{
684                 volt = volt * 1000;
685         }
686
687         DBG("Enter:%s--volt = %d\n",__FUNCTION__,volt);
688         return volt;
689 }
690
691 /*
692  * Return the battery average current
693  * Note that current can be negative signed as well
694  * Or 0 if something fails.
695  */
696 static int bq27320_battery_current(struct bq27320_device_info *di)
697 {
698         int ret;
699         int curr = 0;
700         u8 buf[2];
701
702         #if defined (CONFIG_NO_BATTERY_IC)
703                 return 22000;
704         #endif
705         if(virtual_battery_enable == 1)
706                 return 11000/*22000*/;
707         ret = bq27320_read(di->client,BQ27x00_REG_AI,buf,2);
708         if (ret<0) {
709                 dev_err(di->dev, "error reading current\n");
710                 return 0;
711         }
712
713         curr = get_unaligned_le16(buf);
714         DBG("curr = %x \n",curr);
715         if(curr>0x8000){
716                 curr = 0xFFFF^(curr-1);
717                 DBG("curr = -%d \n",curr*1000);
718         }
719         else
720                 DBG("curr = %d \n",curr*1000);
721         curr = curr * 1000;
722         return curr;
723 }
724
725 /*
726  * Return the battery Relative State-of-Charge
727  * Or < 0 if something fails.
728  
729  */
730 static int bq27320_battery_rsoc(struct bq27320_device_info *di)
731 {
732         int ret;
733         int rsoc = 0;
734         #if 0
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;
738         #endif
739         u8 buf[2];
740
741         #if defined (CONFIG_NO_BATTERY_IC)
742                 return 50;
743         #endif
744         if(virtual_battery_enable == 1)
745                 return 50/*100*/;
746         
747         ret = bq27320_read(di->client,BQ27320_REG_SOC,buf,2); 
748         if (ret<0) {
749                 dev_err(di->dev, "error reading relative State-of-Charge\n");
750                 return ret;
751         }
752         rsoc = get_unaligned_le16(buf);
753         DBG("Enter:%s --rsoc = %d\n",__FUNCTION__,rsoc);
754
755         #if defined (CONFIG_NO_BATTERY_IC)
756         rsoc = 100;
757         #endif
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);
774         
775         buf[0] = 0x00;                                          //CONTROL_STATUS
776         buf[1] = 0x00;
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);
781
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);
821         #endif
822         return rsoc;
823 }
824
825 static int bq27320_battery_status(struct bq27320_device_info *di,
826                                   union power_supply_propval *val)
827 {
828         u8 buf[2];
829         int flags = 0;
830         int status;
831         int ret;
832
833         #if defined (CONFIG_NO_BATTERY_IC)
834                 val->intval = POWER_SUPPLY_STATUS_FULL;
835         return 0;
836         #endif
837
838         if(virtual_battery_enable == 1)
839         {
840                 val->intval = POWER_SUPPLY_STATUS_FULL;
841                 return 0;
842         }
843         
844         ret = bq27320_read(di->client,BQ27x00_REG_BATTERYSTATUS, buf, 2);
845         if (ret < 0) {
846                 dev_err(di->dev, "error reading flags\n");
847                 return ret;
848         }
849         
850         flags = get_unaligned_le16(buf);
851         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
852         
853         if ((flags & BQ27320_BATTERYSTATUS_FC) ||(bq27320_di ->rsoc ==100)){
854                 status = POWER_SUPPLY_STATUS_FULL;
855                 di->soc_full = 1;
856                 DBG("status =POWER_SUPPLY_STATUS_FULL \n");
857         }
858         else if (flags & BQ27320_BATTERYSTATUS_DSC){
859                 status = POWER_SUPPLY_STATUS_DISCHARGING;
860                 DBG("status =POWER_SUPPLY_STATUS_DISCHARGING \n");
861         }
862         else {
863                 status = POWER_SUPPLY_STATUS_CHARGING;
864                 DBG("status =POWER_SUPPLY_STATUS_CHARGING \n");
865         }       
866
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;
872
873         di ->bat_status = status;
874         val->intval = status;
875         return 0;
876 }
877
878 static int bq27320_health_status(struct bq27320_device_info *di,
879                                   union power_supply_propval *val)
880 {
881         u8 buf[2];
882         int flags = 0;
883         int status;
884         int ret;
885         
886         #if defined (CONFIG_NO_BATTERY_IC)
887                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
888         return 0;
889         #endif
890
891         if(virtual_battery_enable == 1)
892         {
893                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
894                 return 0;
895         }
896         ret = bq27320_read(di->client,BQ27x00_REG_BATTERYSTATUS, buf, 2);
897         if (ret < 0) {
898                 dev_err(di->dev, "error reading flags\n");
899                 return ret;
900         }
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");
906         }
907         else{
908                 status = POWER_SUPPLY_HEALTH_GOOD;
909                 DBG("health =POWER_SUPPLY_HEALTH_GOOD \n");
910         }
911
912         val->intval = status;
913         return 0;
914 }
915
916
917 /*
918  * Read a time register.
919  * Return < 0 if something fails.
920  */
921 static int bq27320_battery_time(struct bq27320_device_info *di, int reg,
922                                 union power_supply_propval *val)
923 {
924         u8 buf[2];
925         int tval = 0;
926         int ret;
927
928         ret = bq27320_read(di->client,reg,buf,2);
929         if (ret<0) {
930                 dev_err(di->dev, "error reading register %02x\n", reg);
931                 return ret;
932         }
933         tval = get_unaligned_le16(buf);
934         DBG("Enter:%s--tval=%d\n",__FUNCTION__,tval);
935         if (tval == 65535)
936                 return -ENODATA;
937
938         val->intval = tval * 60;
939         DBG("Enter:%s val->intval = %d\n",__FUNCTION__,val->intval);
940         return 0;
941 }
942
943 #define to_bq27320_device_info(x) container_of((x), \
944                                 struct bq27320_device_info, bat);
945
946 static int bq27320_battery_get_property(struct power_supply *psy,
947                                         enum power_supply_property psp,
948                                         union power_supply_propval *val)
949 {
950         int ret = 0;
951         struct bq27320_device_info *di = to_bq27320_device_info(psy);
952         DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
953         
954         switch (psp) {
955         
956         case POWER_SUPPLY_PROP_STATUS:
957                 ret = bq27320_battery_status(di, val);
958                 break;
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;        
964                 break;
965         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
966                 val->intval = bq27320_battery_voltage(di);
967                 break;
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;
972                 break;
973         case POWER_SUPPLY_PROP_CURRENT_NOW:
974                 val->intval = bq27320_battery_current(di);
975                 break;
976         case POWER_SUPPLY_PROP_CAPACITY:
977                 val->intval = bq27320_battery_rsoc(di);
978                 break;
979         case POWER_SUPPLY_PROP_TEMP:
980                 val->intval = bq27320_battery_temperature(di);
981                 break;
982         case POWER_SUPPLY_PROP_TECHNOLOGY:
983                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
984                 break;
985         case POWER_SUPPLY_PROP_HEALTH:
986                 ret = bq27320_health_status(di, val);
987                 break;
988         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
989                 ret = bq27320_battery_time(di, BQ27x00_REG_TTE, val);
990                 break;
991 //      case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
992 //              ret = bq27320_battery_time(di, BQ27x00_REG_TTECP, val);
993 //              break;
994         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
995                 ret = bq27320_battery_time(di, BQ27x00_REG_TTF, val);
996                 break;
997         default:
998                 return -EINVAL;
999         }
1000
1001         return ret;
1002 }
1003
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();
1007         
1008         if (1 == vbus_status) {
1009         //      if (0 == get_gadget_connect_flag()){ 
1010 //                      usb_status = 2; // non-standard AC charger
1011 //              }else
1012                         usb_status = 1; // connect to pc        
1013         }else{
1014                 if (2 == vbus_status) 
1015                         usb_status = 2; //standard AC charger
1016                 else
1017                         usb_status = 0; 
1018         }
1019         return usb_status;
1020 }
1021 static int bq27320_battery_get_status(void)
1022 {
1023         int charge_on = 0;
1024         int usb_ac_charging = 0;
1025 /*      
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;
1035         }
1036         if(( 1 == bq27320_di->usb_charging)||(1 == bq27320_di ->ac_charging))
1037                 charge_on =1;
1038 */      
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;
1044                 }
1045                 else if(2 == usb_ac_charging){
1046                         bq27320_di->usb_charging = 0;
1047                         bq27320_di->ac_charging = 1;    
1048                 }
1049                 else{
1050                         bq27320_di->usb_charging = 0;
1051                         bq27320_di->ac_charging = 0;    
1052                 }
1053         }
1054         return 0;
1055
1056 }
1057 static int rk3190_ac_get_property(struct power_supply *psy,
1058                         enum power_supply_property psp,
1059                         union power_supply_propval *val)
1060 {
1061         int ret = 0;
1062         
1063         switch (psp) {
1064         case POWER_SUPPLY_PROP_ONLINE:
1065                 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
1066                         val->intval = bq27320_di ->ac_charging; 
1067                 else
1068                         return -EINVAL;
1069                 break;
1070         default:
1071                 return -EINVAL;
1072         }
1073         bq27320_di->online =val->intval;
1074         DBG("%s:rk3190_ac_get_property %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
1075
1076         return ret;
1077 }
1078
1079 static int rk3190_usb_get_property(struct power_supply *psy,
1080                         enum power_supply_property psp,
1081                         union power_supply_propval *val)
1082 {
1083         int ret = 0;
1084         
1085         switch (psp) {
1086         case POWER_SUPPLY_PROP_ONLINE:
1087          if (psy->type == POWER_SUPPLY_TYPE_USB)
1088                         val->intval = bq27320_di ->usb_charging;        
1089                 else
1090                         return -EINVAL;
1091                 break;
1092         default:
1093                 return -EINVAL;
1094         }
1095         bq27320_di->online =val->intval;
1096         DBG("%s:%d rk3190_usb_get_property  val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
1097
1098         return ret;
1099 }
1100
1101 static void bq27320_powersupply_init(struct bq27320_device_info *di)
1102 {
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;
1107         
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;
1113
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;
1119 }
1120
1121
1122 static void bq27320_battery_update_status(struct bq27320_device_info *di)
1123 {
1124
1125         bq27320_battery_get_status();   
1126         power_supply_changed(&di->bat);
1127         power_supply_changed(&di->usb);
1128         power_supply_changed(&di->ac);  
1129 }
1130 #ifdef CONFIG_CHARGER_BQ24161
1131 #include <linux/power/bq24296_charger.h>
1132 static void bq27320_for_charging(struct bq27320_device_info *di)
1133 {                       
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");
1139                         di->soc_full = 0;
1140                         bq24296_charge_otg_en(0, 0);//disable charging
1141                         return ;
1142                 }
1143 /*
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);
1147                 }
1148                 
1149                 else 
1150                         bq24296_charge_otg_en(1, 1, 0);
1151 */
1152                 if (di->bat_status==POWER_SUPPLY_STATUS_FULL) {//³äÂú
1153                         di->soc_full = 1;
1154                         printk(KERN_INFO "**********charger ok*********\n");
1155                 }
1156                 else if ((di->soc_full==1) && (di->rsoc<=BATTERY_RECHARGER_CAPACITY)) {//ÒѳäÂú¹ý£¬ÇÒµçÁ¿Ð¡ÓÚ95%£¬ÐèÒªÐø³ä
1157                         bq24296_charge_otg_en(0, 0);
1158                         msleep(1000);
1159                         bq24296_charge_otg_en(1, 0);
1160                         di->soc_full = 0;
1161                         printk(KERN_INFO "**********recharger*********\n");
1162                 }
1163         }
1164         /*
1165         else if (otg_is_host_mode() || mhl_vbus_power_on()) {
1166                 bq24296_charge_otg_en(1, 0, 1);
1167         }
1168         else {
1169                 di->bat_full = 0;
1170                 bq24296_charge_otg_en();
1171         }
1172         */
1173 }
1174
1175 #endif
1176
1177
1178 static void bq27320_battery_work(struct work_struct *work)
1179 {
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);
1185         #endif
1186         schedule_delayed_work(&di->work, 1*HZ);
1187 }
1188
1189 static void bq27320_set(void)
1190 {
1191         struct bq27320_device_info *di;
1192         int i = 0;
1193         u8 buf[2];
1194
1195         di = bq27320_di;
1196         printk("enter 0x41\n");
1197         buf[0] = 0x41;
1198         buf[1] = 0x00;
1199         bq27320_write(di->client,0x00,buf,2);
1200         
1201         msleep(1500);
1202                 
1203         printk("enter 0x21\n");
1204         buf[0] = 0x21;
1205         buf[1] = 0x00;
1206         bq27320_write(di->client,0x00,buf,2);
1207
1208         buf[0] = 0;
1209         buf[1] = 0;
1210         bq27320_read(di->client,0x00,buf,2);
1211
1212         // printk("%s: Enter:BUF[0]= 0X%x   BUF[1] = 0X%x\n",__FUNCTION__,buf[0],buf[1]);
1213
1214         while((buf[0] & 0x04)&&(i<5))   
1215         {
1216                 printk("enter more 0x21 times i = %d\n",i);
1217                 mdelay(1000);
1218                 buf[0] = 0x21;
1219                 buf[1] = 0x00;
1220                 bq27320_write(di->client,0x00,buf,2);
1221
1222                 buf[0] = 0;
1223                 buf[1] = 0;
1224                 bq27320_read(di->client,0x00,buf,2);
1225                 i++;
1226         }
1227
1228         if(i>5)
1229                 printk("write 0x21 error\n");
1230         else
1231                 printk("bq27320 write 0x21 success\n");
1232 }
1233
1234
1235 static int bq27320_battery_suspend(struct i2c_client *client, pm_message_t mesg)
1236 {
1237         cancel_delayed_work_sync(&bq27320_di->work);
1238         return 0;
1239 }
1240
1241 static int bq27320_battery_resume(struct i2c_client *client)
1242 {
1243         schedule_delayed_work(&bq27320_di->work, msecs_to_jiffies(50));
1244         return 0;
1245 }
1246 static int bq27320_is_in_rom_mode(void)
1247 {
1248         int ret = 0;
1249         unsigned char data = 0x0f;
1250         
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;
1254
1255         if (ret == 1)
1256                 return 1;
1257         else 
1258                 return 0;
1259 }
1260
1261 #ifdef CONFIG_OF
1262 static struct of_device_id bq27320_battery_of_match[] = {
1263         { .compatible = "ti,bq27320"},
1264         { },
1265 };
1266 MODULE_DEVICE_TABLE(of, bq27320_battery_of_match);
1267 #endif
1268
1269 static int bq27320_battery_probe(struct i2c_client *client,
1270                                  const struct i2c_device_id *id)
1271 {
1272         struct bq27320_device_info *di;
1273         int retval = 0;
1274         struct bq27320_board *pdev;
1275         struct device_node *bq27320_node;
1276
1277          DBG("%s,line=%d\n", __func__,__LINE__);
1278          
1279          bq27320_node = of_node_get(client->dev.of_node);
1280         if (!bq27320_node) {
1281                 printk("could not find bq27320-node\n");
1282         }
1283
1284         di = devm_kzalloc(&client->dev,sizeof(*di), GFP_KERNEL);
1285         if (!di) {
1286                 dev_err(&client->dev, "failed to allocate device info data\n");
1287                 retval = -ENOMEM;
1288                 goto batt_failed_2;
1289         }
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;
1298         di->online =1;
1299         di->soc_full = 0;
1300         bq27320_di = di;
1301         
1302         mutex_init(&di->battery_mutex);
1303         
1304         bq27320_powersupply_init(di);
1305         retval = power_supply_register(&client->dev, &di->bat);
1306         if (retval) {
1307                 dev_err(&client->dev, "failed to register battery\n");
1308                 goto batt_failed_4;
1309         }
1310         
1311         retval = power_supply_register(&client->dev, &di->usb);
1312         if (retval) {
1313                 dev_err(&client->dev, "failed to register ac\n");
1314                 goto batt_failed_4;
1315         }
1316
1317         retval = power_supply_register(&client->dev, &di->ac);
1318         if (retval) {
1319                 dev_err(&client->dev, "failed to register ac\n");
1320                 goto batt_failed_4;
1321         }
1322
1323          g_bq27320_i2c_client = client;
1324                 
1325         retval = driver_create_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1326         if (0 != retval)
1327         {
1328                 printk("failed to create sysfs entry(state): %d\n", retval);
1329                 goto batt_failed_3;
1330         }
1331          
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);
1336         
1337         return 0;
1338
1339 batt_failed_4:
1340         kfree(di);
1341 batt_failed_3:
1342         driver_remove_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1343 batt_failed_2:
1344         return retval;
1345
1346 }
1347
1348 static int bq27320_battery_remove(struct i2c_client *client)
1349 {
1350         struct bq27320_device_info *di = i2c_get_clientdata(client);
1351
1352         driver_remove_file(&(bq27320_battery_driver.driver), &driver_attr_state);
1353
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);
1359         kfree(di->ac.name);
1360         kfree(di);
1361         return 0;
1362 }
1363
1364 static const struct i2c_device_id bq27320_id[] = {
1365         { "bq27320", 0 },
1366 };
1367
1368 static struct i2c_driver bq27320_battery_driver = {
1369         .driver = {
1370                 .name = "bq27320",
1371                 .of_match_table =of_match_ptr(bq27320_battery_of_match),
1372         },
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,
1378 };
1379
1380 static int __init bq27320_battery_init(void)
1381 {
1382         int ret;
1383         struct proc_dir_entry * battery_proc_entry;
1384         
1385         ret = i2c_add_driver(&bq27320_battery_driver);
1386         if (ret)
1387                 printk(KERN_ERR "Unable to register BQ27320 driver\n");
1388         
1389         battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
1390         return ret;
1391 }
1392 module_init(bq27320_battery_init);
1393
1394 static void __exit bq27320_battery_exit(void)
1395 {
1396         i2c_del_driver(&bq27320_battery_driver);
1397 }
1398 module_exit(bq27320_battery_exit);
1399
1400 MODULE_AUTHOR("Rockchip");
1401 MODULE_DESCRIPTION("BQ27320 battery monitor driver");
1402 MODULE_LICENSE("GPL");