UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[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 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30
31
32
33 #define DRIVER_VERSION                  "1.1.0"
34 #define BQ27x00_REG_TEMP                0x06
35 #define BQ27x00_REG_VOLT                0x08
36 #define BQ27x00_REG_AI                  0x14
37 #define BQ27x00_REG_FLAGS               0x0A
38 #define BQ27x00_REG_TTE                 0x16
39 #define BQ27x00_REG_TTF                 0x18
40 #define BQ27x00_REG_TTECP               0x26
41 #define BQ27000_REG_RSOC                0x0B /* Relative State-of-Charge */
42 #define BQ27500_REG_SOC                 0x2c
43
44 #define BQ27500_FLAG_DSC                BIT(0)
45 #define BQ27000_FLAG_CHGS               BIT(8)
46 #define BQ27500_FLAG_FC                 BIT(9)
47 #define BQ27500_FLAG_OTD                BIT(14)
48 #define BQ27500_FLAG_OTC                BIT(15)
49
50 #define BQ27510_SPEED                   100 * 1000
51 #define POWER_ON_PIN    RK29_PIN4_PA4
52 //#define CHG_OK RK29_PIN4_PA3
53
54 //#define BAT_LOW       RK29_PIN4_PA2
55
56
57 int  virtual_battery_enable = 0;
58 extern int dwc_vbus_status(void);
59 static void bq27541_set(void);
60
61
62 #if 0
63 #define DBG(x...) printk(KERN_INFO x)
64 #else
65 #define DBG(x...) do { } while (0)
66 #endif
67
68 /* If the system has several batteries we need a different name for each
69  * of them...
70  */
71 static DEFINE_MUTEX(battery_mutex);
72
73 struct bq27541_device_info {
74         struct device           *dev;
75         struct power_supply     bat;
76         struct power_supply     ac;
77         struct delayed_work work;
78         struct delayed_work wakeup_work;
79         struct i2c_client       *client;
80         int wake_irq;
81         unsigned int interval;
82         unsigned int dc_check_pin;
83         unsigned int bat_check_pin;
84         unsigned int bat_num;
85         int power_down;
86 };
87   
88 static struct bq27541_device_info *bq27541_di;
89 static enum power_supply_property bq27541_battery_props[] = {
90         POWER_SUPPLY_PROP_STATUS,
91         POWER_SUPPLY_PROP_PRESENT,
92         POWER_SUPPLY_PROP_VOLTAGE_NOW,
93         POWER_SUPPLY_PROP_CURRENT_NOW,
94         POWER_SUPPLY_PROP_CAPACITY,
95         POWER_SUPPLY_PROP_TEMP,
96         POWER_SUPPLY_PROP_TECHNOLOGY,
97         POWER_SUPPLY_PROP_HEALTH,
98         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
99         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
100         //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
101 };
102
103 static enum power_supply_property rk29_ac_props[] = {
104         POWER_SUPPLY_PROP_ONLINE,
105 };
106
107 static ssize_t battery_proc_write(struct file *file,const char __user *buffer,
108                          unsigned long count,void *data)
109 {
110         char c;
111         int rc;
112         printk("USER:\n");
113         printk("echo x >/proc/driver/power\n");
114         printk("x=1,means just print log ||x=2,means log and data ||x= other,means close log\n");
115
116         rc = get_user(c,buffer);
117         if(rc)
118                 return rc;
119         
120         //added by zwp,c='8' means check whether we need to download firmware to bq27xxx,return 0 means yes.
121         if(c == '8'){
122                 printk("%s,bq27541 don't need to download firmware\n",__FUNCTION__);
123                 return -1;//bq27541 don't need to download firmware.
124         }
125         if(c == '1')
126                 virtual_battery_enable = 1;
127         else if(c == '2')
128                 virtual_battery_enable = 2;
129         else if(c == '3')
130                 virtual_battery_enable = 3;
131         else if(c == '9'){
132                 printk("%s:%d>>bq27541 set\n",__FUNCTION__,__LINE__);
133                 bq27541_set();
134         }
135         else 
136                 virtual_battery_enable = 0;
137         printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
138         return count;
139 }
140
141 static const struct file_operations battery_proc_fops = {
142         .owner          = THIS_MODULE, 
143         .write          = battery_proc_write,
144 }; 
145
146 /*
147  * Common code for BQ27510 devices read
148  */
149 static int bq27541_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
150 {
151         int ret;
152         ret = i2c_master_reg8_recv(client, reg, buf, len, BQ27510_SPEED);
153         return ret; 
154 }
155
156 static int bq27541_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
157 {
158         int ret; 
159         ret = i2c_master_reg8_send(client, reg, buf, (int)len, BQ27510_SPEED);
160         return ret;
161 }
162
163 /*
164  * Return the battery temperature in tenths of degree Celsius
165  * Or < 0 if something fails.
166  */
167  
168 static irqreturn_t bq27541_bat_wakeup(int irq, void *dev_id)
169 {       
170         struct bq27541_device_info *di = (struct bq27541_device_info *)dev_id;
171
172         printk("!!!  bq27541 bat_low irq low vol !!!\n\n\n");
173         disable_irq_wake(di->wake_irq);
174
175         schedule_delayed_work(&di->wakeup_work, msecs_to_jiffies(0));   
176         return IRQ_HANDLED;
177 }
178
179 static void bq27541_battery_wake_work(struct work_struct *work)
180 {
181     int ret;
182     struct bq27541_device_info *di = 
183                 (struct bq27541_device_info *)container_of(work, struct bq27541_device_info, wakeup_work.work);
184                 
185     rk28_send_wakeup_key();
186     
187     free_irq(di->wake_irq, di);
188         di->wake_irq = gpio_to_irq(di->bat_check_pin);
189         
190         ret = request_irq(di->wake_irq, bq27541_bat_wakeup, IRQF_TRIGGER_FALLING, "bq27541_battery", di);
191         if (ret) {
192                 printk("request faild!\n");
193                 return;
194         }
195         enable_irq_wake(di->wake_irq);
196 }
197
198
199 static int bq27541_battery_temperature(struct bq27541_device_info *di)
200 {
201         int ret;
202         int temp = 0;
203         u8 buf[2] ={0};
204
205         #if defined (CONFIG_NO_BATTERY_IC)
206         return 258;
207         #endif
208
209         if(virtual_battery_enable == 1)
210                 return 125/*258*/;
211         ret = bq27541_read(di->client,BQ27x00_REG_TEMP,buf,2);
212         if (ret<0) {
213                 dev_err(di->dev, "error reading temperature\n");
214                 return ret;
215         }
216         temp = get_unaligned_le16(buf);
217         temp = temp - 2731;  //K
218         DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
219
220 //      rk29_pm_power_off();
221         return temp;
222 }
223
224 /*
225  * Return the battery Voltage in milivolts
226  * Or < 0 if something fails.
227  */
228
229
230
231 static int bq27541_battery_voltage(struct bq27541_device_info *di)
232 {
233         int ret;
234         u8 buf[2] = {0};
235         int volt = 0;
236
237         #if defined (CONFIG_NO_BATTERY_IC)
238                 return 4000000;
239         #endif
240         if(virtual_battery_enable == 1)
241                 return 2000000/*4000000*/;
242
243         ret = bq27541_read(di->client,BQ27x00_REG_VOLT,buf,2); 
244         if (ret<0) {
245                 dev_err(di->dev, "error reading voltage\n");
246 //              gpio_set_value(POWER_ON_PIN, GPIO_LOW);
247                 return ret;
248         }
249         volt = get_unaligned_le16(buf);
250
251         //bp27510 can only measure one li-lion bat
252         if(di->bat_num == 2){
253                 volt = volt * 1000 * 2;
254         }else{
255                 volt = volt * 1000;
256         }
257                 
258
259
260         
261         if ((volt <= 3400000)  && (volt > 0) && gpio_get_value(di->dc_check_pin)){
262                 printk("vol smaller then 3.4V, report to android!");
263                 di->power_down = 1;
264         }else{
265                 di->power_down = 0;
266         }
267
268         
269         DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
270         return volt;
271 }
272
273 /*
274  * Return the battery average current
275  * Note that current can be negative signed as well
276  * Or 0 if something fails.
277  */
278 static int bq27541_battery_current(struct bq27541_device_info *di)
279 {
280         int ret;
281         int curr = 0;
282         u8 buf[2] = {0};
283
284         #if defined (CONFIG_NO_BATTERY_IC)
285                 return 22000;
286         #endif
287         if(virtual_battery_enable == 1)
288                 return 11000/*22000*/;
289         ret = bq27541_read(di->client,BQ27x00_REG_AI,buf,2);
290         if (ret<0) {
291                 dev_err(di->dev, "error reading current\n");
292                 return 0;
293         }
294
295         curr = get_unaligned_le16(buf);
296         DBG("curr = %x \n",curr);
297         if(curr>0x8000){
298                 curr = 0xFFFF^(curr-1);
299         }
300         curr = curr * 1000;
301         DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
302         return curr;
303 }
304
305 /*
306  * Return the battery Relative State-of-Charge
307  * Or < 0 if something fails.
308  */
309 static int bq27541_battery_rsoc(struct bq27541_device_info *di)
310 {
311         int ret;
312         int rsoc = 0;
313         #if 0
314         int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
315         int art = 0, artte = 0, ai = 0, tte = 0, ttf = 0, si = 0;
316         int stte = 0, mli = 0, mltte = 0, ae = 0, ap = 0, ttecp = 0, cc = 0;
317         #endif
318         u8 buf[2];
319
320         #if defined (CONFIG_NO_BATTERY_IC)
321                 return 100;
322         #endif
323         if(virtual_battery_enable == 1)
324                 return 50/*100*/;
325         
326         ret = bq27541_read(di->client,BQ27500_REG_SOC,buf,2); 
327         if (ret<0) {
328                 dev_err(di->dev, "error reading relative State-of-Charge\n");
329                 return ret;
330         }
331         rsoc = get_unaligned_le16(buf);
332         DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
333
334         #if defined (CONFIG_NO_BATTERY_IC)
335         rsoc = 100;
336         #endif
337         #if 0     //other register information, for debug use
338         ret = bq27541_read(di->client,0x0c,buf,2);              //NominalAvailableCapacity
339         nvcap = get_unaligned_le16(buf);
340         DBG("\nEnter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
341         ret = bq27541_read(di->client,0x0e,buf,2);              //FullAvailableCapacity
342         facap = get_unaligned_le16(buf);
343         DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
344         ret = bq27541_read(di->client,0x10,buf,2);              //RemainingCapacity
345         remcap = get_unaligned_le16(buf);
346         DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
347         ret = bq27541_read(di->client,0x12,buf,2);              //FullChargeCapacity
348         fccap = get_unaligned_le16(buf);
349         DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
350         ret = bq27541_read(di->client,0x3c,buf,2);              //DesignCapacity
351         full = get_unaligned_le16(buf);
352         DBG("Enter:%s %d--DesignCapacity = %d\n",__FUNCTION__,__LINE__,full);
353         
354         buf[0] = 0x00;                                          //CONTROL_STATUS
355         buf[1] = 0x00;
356         bq27541_write(di->client,0x00,buf,2);
357         ret = bq27541_read(di->client,0x00,buf,2);
358         cnt = get_unaligned_le16(buf);
359         DBG("Enter:%s %d--Control status = %x\n",__FUNCTION__,__LINE__,cnt);
360
361         ret = bq27541_read(di->client,0x02,buf,2);              //AtRate
362         art = get_unaligned_le16(buf);
363         DBG("Enter:%s %d--AtRate = %d\n",__FUNCTION__,__LINE__,art);
364         ret = bq27541_read(di->client,0x04,buf,2);              //AtRateTimeToEmpty
365         artte = get_unaligned_le16(buf);
366         DBG("Enter:%s %d--AtRateTimeToEmpty = %d\n",__FUNCTION__,__LINE__,artte);
367         ret = bq27541_read(di->client,0x14,buf,2);              //AverageCurrent
368         ai = get_unaligned_le16(buf);
369         DBG("Enter:%s %d--AverageCurrent = %d\n",__FUNCTION__,__LINE__,ai);
370         ret = bq27541_read(di->client,0x16,buf,2);              //TimeToEmpty
371         tte = get_unaligned_le16(buf);
372         DBG("Enter:%s %d--TimeToEmpty = %d\n",__FUNCTION__,__LINE__,tte);
373         ret = bq27541_read(di->client,0x18,buf,2);              //TimeToFull
374         ttf = get_unaligned_le16(buf);
375         DBG("Enter:%s %d--TimeToFull = %d\n",__FUNCTION__,__LINE__,ttf);
376         ret = bq27541_read(di->client,0x1a,buf,2);              //StandbyCurrent
377         si = get_unaligned_le16(buf);
378         DBG("Enter:%s %d--StandbyCurrent = %d\n",__FUNCTION__,__LINE__,si);
379         ret = bq27541_read(di->client,0x1c,buf,2);              //StandbyTimeToEmpty
380         stte = get_unaligned_le16(buf);
381         DBG("Enter:%s %d--StandbyTimeToEmpty = %d\n",__FUNCTION__,__LINE__,stte);
382         ret = bq27541_read(di->client,0x1e,buf,2);              //MaxLoadCurrent
383         mli = get_unaligned_le16(buf);
384         DBG("Enter:%s %d--MaxLoadCurrent = %d\n",__FUNCTION__,__LINE__,mli);
385         ret = bq27541_read(di->client,0x20,buf,2);              //MaxLoadTimeToEmpty
386         mltte = get_unaligned_le16(buf);
387         DBG("Enter:%s %d--MaxLoadTimeToEmpty = %d\n",__FUNCTION__,__LINE__,mltte);
388         ret = bq27541_read(di->client,0x22,buf,2);              //AvailableEnergy
389         ae = get_unaligned_le16(buf);
390         DBG("Enter:%s %d--AvailableEnergy = %d\n",__FUNCTION__,__LINE__,ae);
391         ret = bq27541_read(di->client,0x24,buf,2);              //AveragePower
392         ap = get_unaligned_le16(buf);
393         DBG("Enter:%s %d--AveragePower = %d\n",__FUNCTION__,__LINE__,ap);
394         ret = bq27541_read(di->client,0x26,buf,2);              //TTEatConstantPower
395         ttecp = get_unaligned_le16(buf);
396         DBG("Enter:%s %d--TTEatConstantPower = %d\n",__FUNCTION__,__LINE__,ttecp);
397         ret = bq27541_read(di->client,0x2a,buf,2);              //CycleCount
398         cc = get_unaligned_le16(buf);
399         DBG("Enter:%s %d--CycleCount = %d\n",__FUNCTION__,__LINE__,cc);
400         #endif
401         return rsoc;
402 }
403
404 static int bq27541_battery_status(struct bq27541_device_info *di,
405                                   union power_supply_propval *val)
406 {
407         u8 buf[2] = {0};
408         int flags = 0;
409         int status = 0;
410         int ret = 0;
411
412         #if defined (CONFIG_NO_BATTERY_IC)
413                 val->intval = POWER_SUPPLY_STATUS_FULL;
414         return 0;
415         #endif
416
417         if(virtual_battery_enable == 1)
418         {
419                 val->intval = POWER_SUPPLY_STATUS_FULL;
420                 return 0;
421         }
422         ret = bq27541_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
423         if (ret < 0) {
424                 dev_err(di->dev, "error reading flags\n");
425                 return ret;
426         }
427         flags = get_unaligned_le16(buf);
428         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
429         if (flags & BQ27500_FLAG_FC)
430                 status = POWER_SUPPLY_STATUS_FULL;
431         else if (flags & BQ27500_FLAG_DSC)
432                 status = POWER_SUPPLY_STATUS_DISCHARGING;
433         else
434                 status = POWER_SUPPLY_STATUS_CHARGING;
435
436         val->intval = status;
437         return 0;
438 }
439
440 static int bq27541_health_status(struct bq27541_device_info *di,
441                                   union power_supply_propval *val)
442 {
443         u8 buf[2] = {0};
444         int flags = 0;
445         int status;
446         int ret;
447         
448         #if defined (CONFIG_NO_BATTERY_IC)
449                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
450         return 0;
451         #endif
452
453         if(virtual_battery_enable == 1)
454         {
455                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
456                 return 0;
457         }
458         ret = bq27541_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
459         if (ret < 0) {
460                 dev_err(di->dev, "error reading flags\n");
461                 return ret;
462         }
463         flags = get_unaligned_le16(buf);
464         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
465         
466         if ((flags & BQ27500_FLAG_OTD)||(flags & BQ27500_FLAG_OTC))
467                 status = POWER_SUPPLY_HEALTH_OVERHEAT;
468         else
469                 status = POWER_SUPPLY_HEALTH_GOOD;
470
471
472
473         val->intval = status;
474         return 0;
475 }
476
477
478 /*
479  * Read a time register.
480  * Return < 0 if something fails.
481  */
482 static int bq27541_battery_time(struct bq27541_device_info *di, int reg,
483                                 union power_supply_propval *val)
484 {
485         u8 buf[2] = {0};
486         int tval = 0;
487         int ret;
488
489         ret = bq27541_read(di->client,reg,buf,2);
490         if (ret<0) {
491                 dev_err(di->dev, "error reading register %02x\n", reg);
492                 return ret;
493         }
494         tval = get_unaligned_le16(buf);
495         DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
496         if (tval == 65535)
497                 return -ENODATA;
498
499         val->intval = tval * 60;
500         DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
501         return 0;
502 }
503
504 #define to_bq27541_device_info(x) container_of((x), \
505                                 struct bq27541_device_info, bat);
506    
507 static int bq27541_battery_get_property(struct power_supply *psy,
508                                         enum power_supply_property psp,
509                                         union power_supply_propval *val)
510 {
511         int ret = 0;
512         
513         struct bq27541_device_info *di = to_bq27541_device_info(psy);
514         DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
515         
516         switch (psp) {
517
518         case POWER_SUPPLY_PROP_STATUS:
519                 ret = bq27541_battery_status(di, val);
520                 break;
521         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
522         case POWER_SUPPLY_PROP_PRESENT:
523                 val->intval = bq27541_battery_voltage(di);
524                 if (psp == POWER_SUPPLY_PROP_PRESENT){
525                         val->intval = val->intval <= 0 ? 0 : 1;
526                 }
527                 break;
528         case POWER_SUPPLY_PROP_CURRENT_NOW:
529                 val->intval = bq27541_battery_current(di);
530                 break;
531         case POWER_SUPPLY_PROP_CAPACITY:
532                 if (di->power_down == 1){      // < 3.4V    power down ,capacity = 0;
533                         val->intval = 0;
534                 }else {
535                         val->intval = bq27541_battery_rsoc(di);
536                 }
537                 break;
538
539         case POWER_SUPPLY_PROP_TEMP:
540                 val->intval = bq27541_battery_temperature(di);
541                 break;
542
543         case POWER_SUPPLY_PROP_TECHNOLOGY:
544                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
545                 break;
546         case POWER_SUPPLY_PROP_HEALTH:
547                 ret = bq27541_health_status(di, val);
548                 break;
549         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
550                 ret = bq27541_battery_time(di, BQ27x00_REG_TTE, val);
551                 break;
552         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
553                 ret = bq27541_battery_time(di, BQ27x00_REG_TTECP, val);
554                 break;
555         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
556                 ret = bq27541_battery_time(di, BQ27x00_REG_TTF, val);
557                 break;
558         
559         default:
560                 return -EINVAL;
561         }
562
563         return ret;
564 }
565
566 static int rk29_ac_get_property(struct power_supply *psy,
567                         enum power_supply_property psp,
568                         union power_supply_propval *val)
569 {
570         int ret = 0;
571         struct bq27541_device_info *di = container_of(psy, struct bq27541_device_info, ac);
572         
573         switch (psp) {
574         case POWER_SUPPLY_PROP_ONLINE:
575                 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
576                         if(gpio_get_value(di->dc_check_pin))
577                                 val->intval = 0;        /*discharging*/
578                         else
579                                 val->intval = 1;        /*charging*/
580                 }
581                 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
582                 break;
583                 
584         default:
585                 ret = -EINVAL;
586                 break;
587         }
588         return ret;
589 }
590
591 static void bq27541_powersupply_init(struct bq27541_device_info *di)
592 {
593         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
594         di->bat.properties = bq27541_battery_props;
595         di->bat.num_properties = ARRAY_SIZE(bq27541_battery_props);
596         di->bat.get_property = bq27541_battery_get_property;
597         di->power_down = 0;
598         
599         di->ac.name = "ac";
600         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
601         di->ac.properties = rk29_ac_props;
602         di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
603         di->ac.get_property = rk29_ac_get_property;
604 }
605
606
607 static void bq27541_battery_update_status(struct bq27541_device_info *di)
608 {
609         power_supply_changed(&di->bat);
610 }
611
612 static void bq27541_battery_work(struct work_struct *work)
613 {
614         struct bq27541_device_info *di = container_of(work, struct bq27541_device_info, work.work); 
615         bq27541_battery_update_status(di);
616         /* reschedule for the next time */
617         schedule_delayed_work(&di->work, di->interval);
618 }
619
620 static void bq27541_set(void)
621 {
622         struct bq27541_device_info *di;
623         int i = 0;
624         u8 buf[2];
625
626         di = bq27541_di;
627         printk("enter 0x41\n");
628         buf[0] = 0x41;
629         buf[1] = 0x00;
630         bq27541_write(di->client,0x00,buf,2);
631         
632         msleep(1500);
633                 
634         printk("enter 0x21\n");
635         buf[0] = 0x21;
636         buf[1] = 0x00;
637         bq27541_write(di->client,0x00,buf,2);
638
639         buf[0] = 0;
640         buf[1] = 0;
641         bq27541_read(di->client,0x00,buf,2);
642
643         // printk("%s: Enter:BUF[0]= 0X%x   BUF[1] = 0X%x\n",__FUNCTION__,buf[0],buf[1]);
644
645         while((buf[0] & 0x04)&&(i<5))   
646         {
647                 printk("enter more 0x21 times i = %d\n",i);
648                 mdelay(1000);
649                 buf[0] = 0x21;
650                 buf[1] = 0x00;
651                 bq27541_write(di->client,0x00,buf,2);
652
653                 buf[0] = 0;
654                 buf[1] = 0;
655                 bq27541_read(di->client,0x00,buf,2);
656                 i++;
657         }
658
659         if(i>5)
660                 printk("write 0x21 error\n");
661         else
662                 printk("bq27541 write 0x21 success\n");
663 }
664
665 static int bq27541_battery_probe(struct i2c_client *client,
666                                  const struct i2c_device_id *id)
667 {
668         struct bq27541_device_info *di;
669         int retval = 0;
670         struct bq27541_platform_data *pdata;
671         int val =0;
672         char buf[2];
673         int volt;
674         DBG("**********  bq27541_battery_probe**************  ");
675         pdata = client->dev.platform_data;
676         
677         di = kzalloc(sizeof(*di), GFP_KERNEL);
678         if (!di) {
679                 dev_err(&client->dev, "failed to allocate device info data\n");
680                 retval = -ENOMEM;
681                 goto batt_failed_2;
682         }
683         i2c_set_clientdata(client, di);
684         di->dev = &client->dev;
685         di->bat.name = "bq27541-battery";
686         di->client = client;
687         /* 4 seconds between monotor runs interval */
688         di->interval = msecs_to_jiffies(4 * 1000);
689         
690         di->bat_num = pdata->bat_num;
691         di->dc_check_pin = pdata->dc_check_pin;
692         di->bat_check_pin = pdata->bat_check_pin;
693         
694         if (pdata->init_dc_check_pin)
695                 pdata->init_dc_check_pin( );
696         
697         bq27541_powersupply_init(di);
698
699         
700         
701         retval = power_supply_register(&client->dev, &di->bat);
702         if (retval) {
703                 dev_err(&client->dev, "failed to register battery\n");
704                 goto batt_failed_4;
705         }
706         bq27541_di = di;
707         retval = power_supply_register(&client->dev, &di->ac);
708         if (retval) {
709                 dev_err(&client->dev, "failed to register ac\n");
710                 goto batt_failed_4;
711         }
712         INIT_DELAYED_WORK(&di->work, bq27541_battery_work);
713         schedule_delayed_work(&di->work, di->interval);
714         dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
715
716 #if  !defined (CONFIG_NO_BATTERY_IC)
717
718         // no battery  , no power up
719         gpio_request(POWER_ON_PIN, "poweronpin");
720         gpio_request(pdata->bat_check_pin, NULL);
721         gpio_direction_input(pdata->bat_check_pin);
722         gpio_request(pdata->chgok_check_pin, "CHG_OK");
723         gpio_direction_input(pdata->chgok_check_pin);
724
725         val = gpio_get_value(pdata->bat_check_pin);
726         if (val == 1){
727                 printk("\n\n!!! bat_low  high !!!\n\n");
728                 val = bq27541_read(di->client,BQ27x00_REG_VOLT,buf,2);
729                 if (val < 0){
730                         printk("\n\n!!! bq i2c err! no battery,  power down\n!!!\n\n");
731                         gpio_direction_output(POWER_ON_PIN, GPIO_LOW);  
732                         while(1){
733                                 gpio_set_value(POWER_ON_PIN, GPIO_LOW);
734                                 mdelay(100);
735                         }
736                 }
737
738         }else{
739                         
740                 printk("\n\n!!! bat_low  low !!!\n\n");
741                 val = gpio_get_value(pdata->chgok_check_pin);
742                 if (val == 1){
743                         printk("no battery, power down \n");
744                         gpio_direction_output(POWER_ON_PIN, GPIO_LOW);
745                         while(1){
746                                 gpio_set_value(POWER_ON_PIN, GPIO_LOW);
747                                 mdelay(100);
748                         }
749                 }else{
750                         mdelay(1000);
751                         val = gpio_get_value(pdata->chgok_check_pin);
752                         if (val == 1){
753                                 printk("no battery, power down \n");
754                                 gpio_direction_output(POWER_ON_PIN, GPIO_LOW);                  
755                                 while(1){
756                                         gpio_set_value(POWER_ON_PIN, GPIO_LOW);
757                                         mdelay(100);
758                                 }
759                         }
760                 }
761
762         }
763
764 //      gpio_free(POWER_ON_PIN);
765 //      gpio_free(pdata->bat_check_pin);
766         gpio_free(pdata->chgok_check_pin);
767
768
769         //smaller  3.4V , no power up
770         if (gpio_get_value(di->dc_check_pin) && (gpio_get_value(pdata->bat_check_pin) == 0)){
771                         printk("no AC && battery low ,so power down \n");
772                         gpio_direction_output(POWER_ON_PIN, GPIO_LOW);                  
773                         while(1){
774                                 printk("no AC && battery low ,so power down \n");
775                                 gpio_set_value(POWER_ON_PIN, GPIO_LOW);
776                                 mdelay(100);
777                         }
778         }       
779
780         
781         // battery low irq
782         di->wake_irq = gpio_to_irq(pdata->bat_check_pin);
783         retval = request_irq(di->wake_irq, bq27541_bat_wakeup, IRQF_TRIGGER_FALLING, "bq27541_battery", di);
784         if (retval) {
785                 printk("failed to request bat det irq\n");
786                 goto err_batirq_failed;
787         }
788         
789         INIT_DELAYED_WORK(&di->wakeup_work, bq27541_battery_wake_work);
790         enable_irq_wake(di->wake_irq);
791
792
793 #endif
794         
795         return 0;
796
797 batt_failed_4:
798         kfree(di);
799 batt_failed_2:
800
801 err_batirq_failed:
802         gpio_free(pdata->bat_check_pin);
803
804         return retval;
805 }
806
807 static int bq27541_battery_remove(struct i2c_client *client)
808 {
809         struct bq27541_device_info *di = i2c_get_clientdata(client);
810
811         power_supply_unregister(&di->bat);
812         kfree(di->bat.name);
813         kfree(di);
814         return 0;
815 }
816
817 static const struct i2c_device_id bq27541_id[] = {
818         { "bq27541", 0 },
819 };
820
821 static struct i2c_driver bq27541_battery_driver = {
822         .driver = {
823                 .name = "bq27541",
824         },
825         .probe = bq27541_battery_probe,
826         .remove = bq27541_battery_remove,
827         .id_table = bq27541_id,
828 };
829
830 static int __init bq27541_battery_init(void)
831 {
832         int ret;
833         
834         struct proc_dir_entry * battery_proc_entry;
835         
836         ret = i2c_add_driver(&bq27541_battery_driver);
837         if (ret)
838                 printk(KERN_ERR "Unable to register BQ27541 driver\n");
839         
840         battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
841
842         return ret;
843 }
844
845 //module_init(bq27541_battery_init);
846 //fs_initcall_sync(bq27541_battery_init);
847
848 fs_initcall(bq27541_battery_init);
849 //arch_initcall(bq27541_battery_init);
850
851 static void __exit bq27541_battery_exit(void)
852 {
853         i2c_del_driver(&bq27541_battery_driver);
854 }
855 module_exit(bq27541_battery_exit);
856
857 MODULE_AUTHOR("clb");
858 MODULE_DESCRIPTION("BQ27541 battery monitor driver");
859 MODULE_LICENSE("GPL");