Merge remote-tracking branch 'linux-2.6.32.y/master' into develop
[firefly-linux-kernel-4.4.55.git] / drivers / power / bq27510_battery.c
1 /*
2  * BQ27510 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 <mach/gpio.h>
25 #include <linux/proc_fs.h>
26 #include <asm/uaccess.h>
27 #include <mach/board.h>
28
29
30 #define DRIVER_VERSION                  "1.1.0"
31 #define BQ27x00_REG_TEMP                0x06
32 #define BQ27x00_REG_VOLT                0x08
33 #define BQ27x00_REG_AI                  0x14
34 #define BQ27x00_REG_FLAGS               0x0A
35 #define BQ27x00_REG_TTE                 0x16
36 #define BQ27x00_REG_TTF                 0x18
37 #define BQ27x00_REG_TTECP               0x26
38 #define BQ27000_REG_RSOC                0x0B /* Relative State-of-Charge */
39 #define BQ27500_REG_SOC                 0x2c
40
41 #define BQ27500_FLAG_DSC                BIT(0)
42 #define BQ27000_FLAG_CHGS               BIT(8)
43 #define BQ27500_FLAG_FC                 BIT(9)
44 #define BQ27500_FLAG_OTD                BIT(14)
45 #define BQ27500_FLAG_OTC                BIT(15)
46
47 #define BQ27510_SPEED                   300 * 1000
48 int  virtual_battery_enable = 0;
49 extern int dwc_vbus_status(void);
50 static void bq27510_set(void);
51
52 #if 0
53 #define DBG(x...) printk(KERN_INFO x)
54 #else
55 #define DBG(x...) do { } while (0)
56 #endif
57
58 /* If the system has several batteries we need a different name for each
59  * of them...
60  */
61 static DEFINE_MUTEX(battery_mutex);
62
63 struct bq27510_device_info {
64         struct device           *dev;
65         struct power_supply     bat;
66         struct power_supply     ac;
67         struct delayed_work work;
68         struct i2c_client       *client;
69         unsigned int interval;
70         unsigned int dc_check_pin;
71         unsigned int bat_num;
72 };
73
74 static struct bq27510_device_info *bq27510_di;
75 static enum power_supply_property bq27510_battery_props[] = {
76         POWER_SUPPLY_PROP_STATUS,
77         POWER_SUPPLY_PROP_PRESENT,
78         POWER_SUPPLY_PROP_VOLTAGE_NOW,
79         POWER_SUPPLY_PROP_CURRENT_NOW,
80         POWER_SUPPLY_PROP_CAPACITY,
81         POWER_SUPPLY_PROP_TEMP,
82         POWER_SUPPLY_PROP_TECHNOLOGY,
83         POWER_SUPPLY_PROP_HEALTH,
84         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
85         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
86         //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
87 };
88
89 static enum power_supply_property rk29_ac_props[] = {
90         POWER_SUPPLY_PROP_ONLINE,
91 };
92
93 static ssize_t battery_proc_write(struct file *file,const char __user *buffer,
94                          unsigned long count,void *data)
95 {
96         char c;
97         int rc;
98         printk("USER:\n");
99         printk("echo x >/proc/driver/power\n");
100         printk("x=1,means just print log ||x=2,means log and data ||x= other,means close log\n");
101
102         rc = get_user(c,buffer);
103         if(rc)
104                 return rc;
105         if(c == '1')
106                 virtual_battery_enable = 1;
107         else if(c == '2')
108                 virtual_battery_enable = 2;
109         else if(c == '3')
110                 virtual_battery_enable = 3;
111         else if(c == '9'){
112                 printk("%s:%d>>bq27510 set\n",__FUNCTION__,__LINE__);
113                 bq27510_set();
114         }
115         else 
116                 virtual_battery_enable = 0;
117         printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
118         return count;
119 }
120
121 static const struct file_operations battery_proc_fops = {
122         .owner          = THIS_MODULE, 
123         .write          = battery_proc_write,
124 }; 
125
126 /*
127  * Common code for BQ27510 devices read
128  */
129 static int bq27510_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
130 {
131         int ret;
132         ret = i2c_master_reg8_recv(client, reg, buf, len, BQ27510_SPEED);
133         return ret; 
134 }
135
136 static int bq27510_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
137 {
138         int ret; 
139         ret = i2c_master_reg8_send(client, reg, buf, (int)len, BQ27510_SPEED);
140         return ret;
141 }
142
143 /*
144  * Return the battery temperature in tenths of degree Celsius
145  * Or < 0 if something fails.
146  */
147 static int bq27510_battery_temperature(struct bq27510_device_info *di)
148 {
149         int ret;
150         int temp = 0;
151         u8 buf[2];
152
153         #if defined (CONFIG_NO_BATTERY_IC)
154         return 258;
155         #endif
156
157         if(virtual_battery_enable == 1)
158                 return 125/*258*/;
159         ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
160         if (ret<0) {
161                 dev_err(di->dev, "error reading temperature\n");
162                 return ret;
163         }
164         temp = get_unaligned_le16(buf);
165         temp = temp - 2731;
166         DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
167         return temp;
168 }
169
170 /*
171  * Return the battery Voltage in milivolts
172  * Or < 0 if something fails.
173  */
174 static int bq27510_battery_voltage(struct bq27510_device_info *di)
175 {
176         int ret;
177         u8 buf[2];
178         int volt = 0;
179
180         #if defined (CONFIG_NO_BATTERY_IC)
181                 return 4000000;
182         #endif
183         if(virtual_battery_enable == 1)
184                 return 2000000/*4000000*/;
185
186         ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2); 
187         if (ret<0) {
188                 dev_err(di->dev, "error reading voltage\n");
189                 return ret;
190         }
191         volt = get_unaligned_le16(buf);
192
193         //bp27510 can only measure one li-lion bat
194         if(di->bat_num == 2){
195                 volt = volt * 1000 * 2;
196         }else{
197                 volt = volt * 1000;
198         }
199
200         DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
201         return volt;
202 }
203
204 /*
205  * Return the battery average current
206  * Note that current can be negative signed as well
207  * Or 0 if something fails.
208  */
209 static int bq27510_battery_current(struct bq27510_device_info *di)
210 {
211         int ret;
212         int curr = 0;
213         u8 buf[2];
214
215         #if defined (CONFIG_NO_BATTERY_IC)
216                 return 22000;
217         #endif
218         if(virtual_battery_enable == 1)
219                 return 11000/*22000*/;
220         ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
221         if (ret<0) {
222                 dev_err(di->dev, "error reading current\n");
223                 return 0;
224         }
225
226         curr = get_unaligned_le16(buf);
227         DBG("curr = %x \n",curr);
228         if(curr>0x8000){
229                 curr = 0xFFFF^(curr-1);
230         }
231         curr = curr * 1000;
232         DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
233         return curr;
234 }
235
236 /*
237  * Return the battery Relative State-of-Charge
238  * Or < 0 if something fails.
239  */
240 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
241 {
242         int ret;
243         int rsoc = 0;
244         #if 0
245         int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
246         int art = 0, artte = 0, ai = 0, tte = 0, ttf = 0, si = 0;
247         int stte = 0, mli = 0, mltte = 0, ae = 0, ap = 0, ttecp = 0, cc = 0;
248         #endif
249         u8 buf[2];
250
251         #if defined (CONFIG_NO_BATTERY_IC)
252                 return 100;
253         #endif
254         if(virtual_battery_enable == 1)
255                 return 50/*100*/;
256         
257         ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2); 
258         if (ret<0) {
259                 dev_err(di->dev, "error reading relative State-of-Charge\n");
260                 return ret;
261         }
262         rsoc = get_unaligned_le16(buf);
263         DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
264
265         #if defined (CONFIG_NO_BATTERY_IC)
266         rsoc = 100;
267         #endif
268         #if 0     //other register information, for debug use
269         ret = bq27510_read(di->client,0x0c,buf,2);              //NominalAvailableCapacity
270         nvcap = get_unaligned_le16(buf);
271         DBG("\nEnter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
272         ret = bq27510_read(di->client,0x0e,buf,2);              //FullAvailableCapacity
273         facap = get_unaligned_le16(buf);
274         DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
275         ret = bq27510_read(di->client,0x10,buf,2);              //RemainingCapacity
276         remcap = get_unaligned_le16(buf);
277         DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
278         ret = bq27510_read(di->client,0x12,buf,2);              //FullChargeCapacity
279         fccap = get_unaligned_le16(buf);
280         DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
281         ret = bq27510_read(di->client,0x3c,buf,2);              //DesignCapacity
282         full = get_unaligned_le16(buf);
283         DBG("Enter:%s %d--DesignCapacity = %d\n",__FUNCTION__,__LINE__,full);
284         
285         buf[0] = 0x00;                                          //CONTROL_STATUS
286         buf[1] = 0x00;
287         bq27510_write(di->client,0x00,buf,2);
288         ret = bq27510_read(di->client,0x00,buf,2);
289         cnt = get_unaligned_le16(buf);
290         DBG("Enter:%s %d--Control status = %x\n",__FUNCTION__,__LINE__,cnt);
291
292         ret = bq27510_read(di->client,0x02,buf,2);              //AtRate
293         art = get_unaligned_le16(buf);
294         DBG("Enter:%s %d--AtRate = %d\n",__FUNCTION__,__LINE__,art);
295         ret = bq27510_read(di->client,0x04,buf,2);              //AtRateTimeToEmpty
296         artte = get_unaligned_le16(buf);
297         DBG("Enter:%s %d--AtRateTimeToEmpty = %d\n",__FUNCTION__,__LINE__,artte);
298         ret = bq27510_read(di->client,0x14,buf,2);              //AverageCurrent
299         ai = get_unaligned_le16(buf);
300         DBG("Enter:%s %d--AverageCurrent = %d\n",__FUNCTION__,__LINE__,ai);
301         ret = bq27510_read(di->client,0x16,buf,2);              //TimeToEmpty
302         tte = get_unaligned_le16(buf);
303         DBG("Enter:%s %d--TimeToEmpty = %d\n",__FUNCTION__,__LINE__,tte);
304         ret = bq27510_read(di->client,0x18,buf,2);              //TimeToFull
305         ttf = get_unaligned_le16(buf);
306         DBG("Enter:%s %d--TimeToFull = %d\n",__FUNCTION__,__LINE__,ttf);
307         ret = bq27510_read(di->client,0x1a,buf,2);              //StandbyCurrent
308         si = get_unaligned_le16(buf);
309         DBG("Enter:%s %d--StandbyCurrent = %d\n",__FUNCTION__,__LINE__,si);
310         ret = bq27510_read(di->client,0x1c,buf,2);              //StandbyTimeToEmpty
311         stte = get_unaligned_le16(buf);
312         DBG("Enter:%s %d--StandbyTimeToEmpty = %d\n",__FUNCTION__,__LINE__,stte);
313         ret = bq27510_read(di->client,0x1e,buf,2);              //MaxLoadCurrent
314         mli = get_unaligned_le16(buf);
315         DBG("Enter:%s %d--MaxLoadCurrent = %d\n",__FUNCTION__,__LINE__,mli);
316         ret = bq27510_read(di->client,0x20,buf,2);              //MaxLoadTimeToEmpty
317         mltte = get_unaligned_le16(buf);
318         DBG("Enter:%s %d--MaxLoadTimeToEmpty = %d\n",__FUNCTION__,__LINE__,mltte);
319         ret = bq27510_read(di->client,0x22,buf,2);              //AvailableEnergy
320         ae = get_unaligned_le16(buf);
321         DBG("Enter:%s %d--AvailableEnergy = %d\n",__FUNCTION__,__LINE__,ae);
322         ret = bq27510_read(di->client,0x24,buf,2);              //AveragePower
323         ap = get_unaligned_le16(buf);
324         DBG("Enter:%s %d--AveragePower = %d\n",__FUNCTION__,__LINE__,ap);
325         ret = bq27510_read(di->client,0x26,buf,2);              //TTEatConstantPower
326         ttecp = get_unaligned_le16(buf);
327         DBG("Enter:%s %d--TTEatConstantPower = %d\n",__FUNCTION__,__LINE__,ttecp);
328         ret = bq27510_read(di->client,0x2a,buf,2);              //CycleCount
329         cc = get_unaligned_le16(buf);
330         DBG("Enter:%s %d--CycleCount = %d\n",__FUNCTION__,__LINE__,cc);
331         #endif
332         return rsoc;
333 }
334
335 static int bq27510_battery_status(struct bq27510_device_info *di,
336                                   union power_supply_propval *val)
337 {
338         u8 buf[2];
339         int flags = 0;
340         int status;
341         int ret;
342
343         #if defined (CONFIG_NO_BATTERY_IC)
344                 val->intval = POWER_SUPPLY_STATUS_FULL;
345         return 0;
346         #endif
347
348         if(virtual_battery_enable == 1)
349         {
350                 val->intval = POWER_SUPPLY_STATUS_FULL;
351                 return 0;
352         }
353         ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
354         if (ret < 0) {
355                 dev_err(di->dev, "error reading flags\n");
356                 return ret;
357         }
358         flags = get_unaligned_le16(buf);
359         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
360         if (flags & BQ27500_FLAG_FC)
361                 status = POWER_SUPPLY_STATUS_FULL;
362         else if (flags & BQ27500_FLAG_DSC)
363                 status = POWER_SUPPLY_STATUS_DISCHARGING;
364         else
365                 status = POWER_SUPPLY_STATUS_CHARGING;
366
367         val->intval = status;
368         return 0;
369 }
370
371 static int bq27510_health_status(struct bq27510_device_info *di,
372                                   union power_supply_propval *val)
373 {
374         u8 buf[2];
375         int flags = 0;
376         int status;
377         int ret;
378         
379         #if defined (CONFIG_NO_BATTERY_IC)
380                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
381         return 0;
382         #endif
383
384         if(virtual_battery_enable == 1)
385         {
386                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
387                 return 0;
388         }
389         ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
390         if (ret < 0) {
391                 dev_err(di->dev, "error reading flags\n");
392                 return ret;
393         }
394         flags = get_unaligned_le16(buf);
395         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
396         if ((flags & BQ27500_FLAG_OTD)||(flags & BQ27500_FLAG_OTC))
397                 status = POWER_SUPPLY_HEALTH_OVERHEAT;
398         else
399                 status = POWER_SUPPLY_HEALTH_GOOD;
400
401         val->intval = status;
402         return 0;
403 }
404
405
406 /*
407  * Read a time register.
408  * Return < 0 if something fails.
409  */
410 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
411                                 union power_supply_propval *val)
412 {
413         u8 buf[2];
414         int tval = 0;
415         int ret;
416
417         ret = bq27510_read(di->client,reg,buf,2);
418         if (ret<0) {
419                 dev_err(di->dev, "error reading register %02x\n", reg);
420                 return ret;
421         }
422         tval = get_unaligned_le16(buf);
423         DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
424         if (tval == 65535)
425                 return -ENODATA;
426
427         val->intval = tval * 60;
428         DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
429         return 0;
430 }
431
432 #define to_bq27510_device_info(x) container_of((x), \
433                                 struct bq27510_device_info, bat);
434
435 static int bq27510_battery_get_property(struct power_supply *psy,
436                                         enum power_supply_property psp,
437                                         union power_supply_propval *val)
438 {
439         int ret = 0;
440         struct bq27510_device_info *di = to_bq27510_device_info(psy);
441         DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
442         
443         switch (psp) {
444         
445         case POWER_SUPPLY_PROP_STATUS:
446                 ret = bq27510_battery_status(di, val);
447                 break;
448         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
449         case POWER_SUPPLY_PROP_PRESENT:
450                 val->intval = bq27510_battery_voltage(di);
451                 if (psp == POWER_SUPPLY_PROP_PRESENT)
452                         val->intval = val->intval <= 0 ? 0 : 1;
453                 break;
454         case POWER_SUPPLY_PROP_CURRENT_NOW:
455                 val->intval = bq27510_battery_current(di);
456                 break;
457         case POWER_SUPPLY_PROP_CAPACITY:
458                 val->intval = bq27510_battery_rsoc(di);
459                 break;
460         case POWER_SUPPLY_PROP_TEMP:
461                 val->intval = bq27510_battery_temperature(di);
462                 break;
463         case POWER_SUPPLY_PROP_TECHNOLOGY:
464                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
465                 break;
466         case POWER_SUPPLY_PROP_HEALTH:
467                 ret = bq27510_health_status(di, val);
468                 break;
469         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
470                 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
471                 break;
472         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
473                 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
474                 break;
475         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
476                 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
477                 break;
478         default:
479                 return -EINVAL;
480         }
481
482         return ret;
483 }
484
485 static int rk29_ac_get_property(struct power_supply *psy,
486                         enum power_supply_property psp,
487                         union power_supply_propval *val)
488 {
489         int ret = 0;
490         struct bq27510_device_info *di = container_of(psy, struct bq27510_device_info, ac);
491         
492         switch (psp) {
493         case POWER_SUPPLY_PROP_ONLINE:
494                 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
495                         if(gpio_get_value(di->dc_check_pin))
496                                 val->intval = 0;        /*discharging*/
497                         else
498                                 val->intval = 1;        /*charging*/
499                 }
500                 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
501                 break;
502                 
503         default:
504                 ret = -EINVAL;
505                 break;
506         }
507         return ret;
508 }
509
510 static void bq27510_powersupply_init(struct bq27510_device_info *di)
511 {
512         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
513         di->bat.properties = bq27510_battery_props;
514         di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
515         di->bat.get_property = bq27510_battery_get_property;
516         
517         di->ac.name = "ac";
518         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
519         di->ac.properties = rk29_ac_props;
520         di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
521         di->ac.get_property = rk29_ac_get_property;
522 }
523
524
525 static void bq27510_battery_update_status(struct bq27510_device_info *di)
526 {
527         power_supply_changed(&di->bat);
528 }
529
530 static void bq27510_battery_work(struct work_struct *work)
531 {
532         struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work); 
533         bq27510_battery_update_status(di);
534         /* reschedule for the next time */
535         schedule_delayed_work(&di->work, di->interval);
536 }
537
538 static void bq27510_set(void)
539 {
540         struct bq27510_device_info *di;
541         int i = 0;
542         u8 buf[2];
543
544         di = bq27510_di;
545         printk("enter 0x41\n");
546         buf[0] = 0x41;
547         buf[1] = 0x00;
548         bq27510_write(di->client,0x00,buf,2);
549         
550         msleep(1500);
551                 
552         printk("enter 0x21\n");
553         buf[0] = 0x21;
554         buf[1] = 0x00;
555         bq27510_write(di->client,0x00,buf,2);
556
557         buf[0] = 0;
558         buf[1] = 0;
559         bq27510_read(di->client,0x00,buf,2);
560
561         // printk("%s: Enter:BUF[0]= 0X%x   BUF[1] = 0X%x\n",__FUNCTION__,buf[0],buf[1]);
562
563         while((buf[0] & 0x04)&&(i<5))   
564         {
565                 printk("enter more 0x21 times i = %d\n",i);
566                 mdelay(1000);
567                 buf[0] = 0x21;
568                 buf[1] = 0x00;
569                 bq27510_write(di->client,0x00,buf,2);
570
571                 buf[0] = 0;
572                 buf[1] = 0;
573                 bq27510_read(di->client,0x00,buf,2);
574                 i++;
575         }
576
577         if(i>5)
578                 printk("write 0x21 error\n");
579         else
580                 printk("bq27510 write 0x21 success\n");
581 }
582
583 static int bq27510_battery_probe(struct i2c_client *client,
584                                  const struct i2c_device_id *id)
585 {
586         struct bq27510_device_info *di;
587         int retval = 0;
588         struct bq27510_platform_data *pdata;
589         
590         pdata = client->dev.platform_data;
591         
592         di = kzalloc(sizeof(*di), GFP_KERNEL);
593         if (!di) {
594                 dev_err(&client->dev, "failed to allocate device info data\n");
595                 retval = -ENOMEM;
596                 goto batt_failed_2;
597         }
598         i2c_set_clientdata(client, di);
599         di->dev = &client->dev;
600         di->bat.name = "bq27510-battery";
601         di->client = client;
602         /* 4 seconds between monotor runs interval */
603         di->interval = msecs_to_jiffies(4 * 1000);
604         
605         di->bat_num = pdata->bat_num;
606         di->dc_check_pin = pdata->dc_check_pin;
607         
608         if (pdata->init_dc_check_pin)
609                 pdata->init_dc_check_pin( );
610         
611         bq27510_powersupply_init(di);
612         
613         retval = power_supply_register(&client->dev, &di->bat);
614         if (retval) {
615                 dev_err(&client->dev, "failed to register battery\n");
616                 goto batt_failed_4;
617         }
618         bq27510_di = di;
619         retval = power_supply_register(&client->dev, &di->ac);
620         if (retval) {
621                 dev_err(&client->dev, "failed to register ac\n");
622                 goto batt_failed_4;
623         }
624         INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
625         schedule_delayed_work(&di->work, di->interval);
626         dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
627         return 0;
628
629 batt_failed_4:
630         kfree(di);
631 batt_failed_2:
632         return retval;
633 }
634
635 static int bq27510_battery_remove(struct i2c_client *client)
636 {
637         struct bq27510_device_info *di = i2c_get_clientdata(client);
638
639         power_supply_unregister(&di->bat);
640         kfree(di->bat.name);
641         kfree(di);
642         return 0;
643 }
644
645 static const struct i2c_device_id bq27510_id[] = {
646         { "bq27510", 0 },
647 };
648
649 static struct i2c_driver bq27510_battery_driver = {
650         .driver = {
651                 .name = "bq27510",
652         },
653         .probe = bq27510_battery_probe,
654         .remove = bq27510_battery_remove,
655         .id_table = bq27510_id,
656 };
657
658 static int __init bq27510_battery_init(void)
659 {
660         int ret;
661         struct proc_dir_entry * battery_proc_entry;
662         
663         ret = i2c_add_driver(&bq27510_battery_driver);
664         if (ret)
665                 printk(KERN_ERR "Unable to register BQ27510 driver\n");
666         
667         battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
668         return ret;
669 }
670 module_init(bq27510_battery_init);
671
672 static void __exit bq27510_battery_exit(void)
673 {
674         i2c_del_driver(&bq27510_battery_driver);
675 }
676 module_exit(bq27510_battery_exit);
677
678 MODULE_AUTHOR("Rockchip");
679 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
680 MODULE_LICENSE("GPL");