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