add bq27541
[firefly-linux-kernel-4.4.55.git] / drivers / power / bq27541_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                   100 * 1000
48 int  virtual_battery_enable = 0;
49 extern int dwc_vbus_status(void);
50 static void bq27541_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 bq27541_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 bq27541_device_info *bq27541_di;
75 static enum power_supply_property bq27541_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>>bq27541 set\n",__FUNCTION__,__LINE__);
113                 bq27541_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 bq27541_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 bq27541_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 bq27541_battery_temperature(struct bq27541_device_info *di)
148 {
149         int ret;
150         int temp = 0;
151         u8 buf[2] ={0};
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 = bq27541_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;  //K
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 bq27541_battery_voltage(struct bq27541_device_info *di)
175 {
176         int ret;
177         u8 buf[2] = {0};
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 = bq27541_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 bq27541_battery_current(struct bq27541_device_info *di)
210 {
211         int ret;
212         int curr = 0;
213         u8 buf[2] = {0};
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 = bq27541_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 bq27541_battery_rsoc(struct bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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         bq27541_write(di->client,0x00,buf,2);
288         ret = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 = bq27541_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 bq27541_battery_status(struct bq27541_device_info *di,
336                                   union power_supply_propval *val)
337 {
338         u8 buf[2] = {0};
339         int flags = 0;
340         int status = 0;
341         int ret = 0;
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 = bq27541_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 bq27541_health_status(struct bq27541_device_info *di,
372                                   union power_supply_propval *val)
373 {
374         u8 buf[2] = {0};
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 = bq27541_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         
397         if ((flags & BQ27500_FLAG_OTD)||(flags & BQ27500_FLAG_OTC))
398                 status = POWER_SUPPLY_HEALTH_OVERHEAT;
399         else
400                 status = POWER_SUPPLY_HEALTH_GOOD;
401
402
403
404         val->intval = status;
405         return 0;
406 }
407
408
409 /*
410  * Read a time register.
411  * Return < 0 if something fails.
412  */
413 static int bq27541_battery_time(struct bq27541_device_info *di, int reg,
414                                 union power_supply_propval *val)
415 {
416         u8 buf[2] = {0};
417         int tval = 0;
418         int ret;
419
420         ret = bq27541_read(di->client,reg,buf,2);
421         if (ret<0) {
422                 dev_err(di->dev, "error reading register %02x\n", reg);
423                 return ret;
424         }
425         tval = get_unaligned_le16(buf);
426         DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
427         if (tval == 65535)
428                 return -ENODATA;
429
430         val->intval = tval * 60;
431         DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
432         return 0;
433 }
434
435 #define to_bq27541_device_info(x) container_of((x), \
436                                 struct bq27541_device_info, bat);
437    
438 static int bq27541_battery_get_property(struct power_supply *psy,
439                                         enum power_supply_property psp,
440                                         union power_supply_propval *val)
441 {
442         int ret = 0;
443         struct bq27541_device_info *di = to_bq27541_device_info(psy);
444         DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
445         
446         switch (psp) {
447
448         case POWER_SUPPLY_PROP_STATUS:
449                 ret = bq27541_battery_status(di, val);
450                 break;
451         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
452         case POWER_SUPPLY_PROP_PRESENT:
453                 val->intval = bq27541_battery_voltage(di);
454                 if (psp == POWER_SUPPLY_PROP_PRESENT)
455                         val->intval = val->intval <= 0 ? 0 : 1;
456                 break;
457         case POWER_SUPPLY_PROP_CURRENT_NOW:
458                 val->intval = bq27541_battery_current(di);
459                 break;
460         case POWER_SUPPLY_PROP_CAPACITY:
461                 val->intval = bq27541_battery_rsoc(di);
462                 break;
463
464         case POWER_SUPPLY_PROP_TEMP:
465                 val->intval = bq27541_battery_temperature(di);
466                 break;
467
468         case POWER_SUPPLY_PROP_TECHNOLOGY:
469                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
470                 break;
471         case POWER_SUPPLY_PROP_HEALTH:
472                 ret = bq27541_health_status(di, val);
473                 break;
474         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
475                 ret = bq27541_battery_time(di, BQ27x00_REG_TTE, val);
476                 break;
477         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
478                 ret = bq27541_battery_time(di, BQ27x00_REG_TTECP, val);
479                 break;
480         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
481                 ret = bq27541_battery_time(di, BQ27x00_REG_TTF, val);
482                 break;
483         
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 bq27541_device_info *di = container_of(psy, struct bq27541_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 bq27541_powersupply_init(struct bq27541_device_info *di)
517 {
518         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
519         di->bat.properties = bq27541_battery_props;
520         di->bat.num_properties = ARRAY_SIZE(bq27541_battery_props);
521         di->bat.get_property = bq27541_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 bq27541_battery_update_status(struct bq27541_device_info *di)
532 {
533         power_supply_changed(&di->bat);
534 }
535
536 static void bq27541_battery_work(struct work_struct *work)
537 {
538         struct bq27541_device_info *di = container_of(work, struct bq27541_device_info, work.work); 
539         bq27541_battery_update_status(di);
540         /* reschedule for the next time */
541         schedule_delayed_work(&di->work, di->interval);
542 }
543
544 static void bq27541_set(void)
545 {
546         struct bq27541_device_info *di;
547         int i = 0;
548         u8 buf[2];
549
550         di = bq27541_di;
551         printk("enter 0x41\n");
552         buf[0] = 0x41;
553         buf[1] = 0x00;
554         bq27541_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         bq27541_write(di->client,0x00,buf,2);
562
563         buf[0] = 0;
564         buf[1] = 0;
565         bq27541_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                 bq27541_write(di->client,0x00,buf,2);
576
577                 buf[0] = 0;
578                 buf[1] = 0;
579                 bq27541_read(di->client,0x00,buf,2);
580                 i++;
581         }
582
583         if(i>5)
584                 printk("write 0x21 error\n");
585         else
586                 printk("bq27541 write 0x21 success\n");
587 }
588
589 static int bq27541_battery_probe(struct i2c_client *client,
590                                  const struct i2c_device_id *id)
591 {
592         struct bq27541_device_info *di;
593         int retval = 0;
594         struct bq27541_platform_data *pdata;
595
596         DBG("**********  bq27541_battery_probe**************  ");
597         pdata = client->dev.platform_data;
598         
599         di = kzalloc(sizeof(*di), GFP_KERNEL);
600         if (!di) {
601                 dev_err(&client->dev, "failed to allocate device info data\n");
602                 retval = -ENOMEM;
603                 goto batt_failed_2;
604         }
605         i2c_set_clientdata(client, di);
606         di->dev = &client->dev;
607         di->bat.name = "bq27541-battery";
608         di->client = client;
609         /* 4 seconds between monotor runs interval */
610         di->interval = msecs_to_jiffies(4 * 1000);
611         
612         di->bat_num = pdata->bat_num;
613         di->dc_check_pin = pdata->dc_check_pin;
614         
615         if (pdata->init_dc_check_pin)
616                 pdata->init_dc_check_pin( );
617         
618         bq27541_powersupply_init(di);
619         
620         retval = power_supply_register(&client->dev, &di->bat);
621         if (retval) {
622                 dev_err(&client->dev, "failed to register battery\n");
623                 goto batt_failed_4;
624         }
625         bq27541_di = di;
626         retval = power_supply_register(&client->dev, &di->ac);
627         if (retval) {
628                 dev_err(&client->dev, "failed to register ac\n");
629                 goto batt_failed_4;
630         }
631         INIT_DELAYED_WORK(&di->work, bq27541_battery_work);
632         schedule_delayed_work(&di->work, di->interval);
633         dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
634         return 0;
635
636 batt_failed_4:
637         kfree(di);
638 batt_failed_2:
639         return retval;
640 }
641
642 static int bq27541_battery_remove(struct i2c_client *client)
643 {
644         struct bq27541_device_info *di = i2c_get_clientdata(client);
645
646         power_supply_unregister(&di->bat);
647         kfree(di->bat.name);
648         kfree(di);
649         return 0;
650 }
651
652 static const struct i2c_device_id bq27541_id[] = {
653         { "bq27541", 0 },
654 };
655
656 static struct i2c_driver bq27541_battery_driver = {
657         .driver = {
658                 .name = "bq27541",
659         },
660         .probe = bq27541_battery_probe,
661         .remove = bq27541_battery_remove,
662         .id_table = bq27541_id,
663 };
664
665 static int __init bq27541_battery_init(void)
666 {
667         int ret;
668         struct proc_dir_entry * battery_proc_entry;
669         
670         ret = i2c_add_driver(&bq27541_battery_driver);
671         if (ret)
672                 printk(KERN_ERR "Unable to register BQ27541 driver\n");
673         
674         battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
675         return ret;
676 }
677 module_init(bq27541_battery_init);
678
679 static void __exit bq27541_battery_exit(void)
680 {
681         i2c_del_driver(&bq27541_battery_driver);
682 }
683 module_exit(bq27541_battery_exit);
684
685 MODULE_AUTHOR("clb");
686 MODULE_DESCRIPTION("BQ27541 battery monitor driver");
687 MODULE_LICENSE("GPL");