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