f09f215a30dba039c9f9c62c92ed534c798edf8c
[firefly-linux-kernel-4.4.55.git] / drivers / power / cw2015_battery.c
1 /*
2  * RockChip ADC Battery Driver 
3  * Copyright (C) 2012, RockChip
4  *
5  * Authors: xuhuicong <xhc@rock-chips.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
15 #include <linux/workqueue.h>
16 #include <linux/kernel.h>
17 #include <linux/i2c.h>
18 #include <mach/gpio.h>
19 #include <linux/delay.h>
20 #include <linux/power/cw2015_battery.h>
21 #include <linux/time.h>
22
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <mach/board.h>
26
27 #define REG_VERSION             0x0
28 #define REG_VCELL               0x2
29 #define REG_SOC                 0x4
30 #define REG_RRT_ALERT           0x6
31 #define REG_CONFIG              0x8
32 #define REG_MODE                0xA
33 #define REG_BATINFO             0x10
34
35 #define MODE_SLEEP_MASK         (0x3<<6)
36 #define MODE_SLEEP              (0x3<<6)
37 #define MODE_NORMAL             (0x0<<6)
38 #define MODE_QUICK_START        (0x3<<4)
39 #define MODE_RESTART            (0xf<<0)
40
41 #define CONFIG_UPDATE_FLG       (0x1<<1)
42 #define ATHD                    (0xa<<3)        //ATHD =10%
43
44 #define CW_I2C_SPEED            100000          // default i2c speed set 100khz
45
46 #define BATTERY_UP_MAX_CHANGE   600             // the max time allow battery change quantity
47 #define BATTERY_DOWN_MIN_CHANGE_RUN 30          // the min time allow battery change quantity when run
48 #define BATTERY_DOWN_MIN_CHANGE_SLEEP 1800      // the min time allow battery change quantity when run 30min
49
50 #define BATTERY_DOWN_MAX_CHANGE_RUN_AC_ONLINE 1800
51
52 #define Enable_BATDRV_LOG       0
53
54 #if Enable_BATDRV_LOG
55 #define xprintk(format, arg...)  printk("func: %s\n" format,  __FUNCTION__, ##arg)
56 #else
57 #define xprintk(format, ...)
58 #endif
59
60 struct cw_battery {
61         struct i2c_client *client;
62         struct workqueue_struct *battery_workqueue;
63         struct delayed_work battery_delay_work;
64         struct delayed_work dc_wakeup_work;
65         const struct cw_bat_platform_data *plat_data;
66
67         struct power_supply rk_bat;
68         struct power_supply rk_ac;
69         struct power_supply rk_usb;
70
71         long sleep_time_capacity;      // the sleep time from capacity change to present, it will set 0 when capacity change 
72         long run_time_capacity;
73
74         long sleep_time_ac_online;      // the sleep time from insert ac to present, it will set 0 when insert ac
75         long run_time_ac_online;
76
77         int dc_online;
78         int usb_online;
79         int capacity;
80         int voltage;
81         int status;
82         int time_to_empty;
83
84         int bat_change;
85 };
86
87 static int cw_read(struct i2c_client *client, u8 reg, u8 buf[])
88 {
89         int ret;
90         ret = i2c_master_reg8_recv(client, reg, buf, 1, CW_I2C_SPEED);
91         return ret;
92 }
93
94 static int cw_write(struct i2c_client *client, u8 reg, u8 const buf[])
95 {
96         int ret;
97         ret = i2c_master_reg8_send(client, reg, buf, 1, CW_I2C_SPEED);
98         return ret;
99 }
100
101 static int cw_update_config_info(struct cw_battery *cw_bat)
102 {
103         int ret;
104         u8 reg_val;
105         int i;
106         u8 reset_val;
107
108         printk("func: %s-------\n", __func__);
109         /* make sure no in sleep mode */
110         ret = cw_read(cw_bat->client, REG_MODE, &reg_val);
111         if (ret < 0)
112                 return ret;
113
114         reset_val = reg_val;
115         if((reg_val & MODE_SLEEP_MASK) == MODE_SLEEP) {
116                 printk("Error, device in sleep mode, cannot update battery info\n");
117                 return -1;
118         }
119
120         /* update new battery info */
121
122         for (i = 0; i < SIZE_BATINFO; i++) {
123                 printk("cw_bat->plat_data->cw_bat_config_info[%d] = 0x%x\n", i, \
124                                 cw_bat->plat_data->cw_bat_config_info[i]);
125                 ret = cw_write(cw_bat->client, REG_BATINFO + i, &cw_bat->plat_data->cw_bat_config_info[i]);
126
127                 if (ret < 0) 
128                         return ret;
129         }
130
131         /* readback & check */
132         for (i = 0; i < SIZE_BATINFO; i++) {
133                 ret = cw_read(cw_bat->client, REG_BATINFO + i, &reg_val);
134                 if (reg_val != cw_bat->plat_data->cw_bat_config_info[i])
135                         return -1;
136         }
137
138         
139         /* set cw2015 to use new battery info */
140         ret = cw_read(cw_bat->client, REG_CONFIG, &reg_val);
141         if (ret < 0)
142                 return ret;
143
144         reg_val |= CONFIG_UPDATE_FLG;/* set UPDATE_FLAG */
145         reg_val &= 0x07;    // clear ATHD
146         reg_val |= ATHD;    // set ATHD
147         ret = cw_write(cw_bat->client, REG_CONFIG, &reg_val);
148         if (ret < 0)
149                 return ret;
150
151         /* check 2015 for ATHD & update_flag */ 
152         ret = cw_read(cw_bat->client, REG_CONFIG, &reg_val);
153         if (ret < 0)
154                 return ret;
155         
156         if (!(reg_val & CONFIG_UPDATE_FLG)) {
157                 printk("update flag for new battery info have not set..\n");
158         }
159
160         if ((reg_val & 0xf8) != ATHD) {
161                 printk("the new ATHD have not set..\n");
162         }
163
164         /* reset */
165         reset_val &= ~(MODE_RESTART);
166         reg_val = reset_val | MODE_RESTART;
167         ret = cw_write(cw_bat->client, REG_MODE, &reg_val);
168         if (ret < 0)
169                 return ret;
170
171         msleep(10);
172         ret = cw_write(cw_bat->client, REG_MODE, &reset_val);
173         if (ret < 0)
174                 return ret;
175         
176         return 0;
177 }
178
179 static int cw_init(struct cw_battery *cw_bat)
180 {
181         int ret;
182         int i;
183         u8 reg_val = MODE_SLEEP;
184 #if 0
185         ret = cw_read(cw_bat->client, REG_MODE, &reg_val);
186         if (ret < 0)
187                 return ret;
188 #endif
189         if ((reg_val & MODE_SLEEP_MASK) == MODE_SLEEP) {
190                 reg_val = MODE_NORMAL;
191                 ret = cw_write(cw_bat->client, REG_MODE, &reg_val);
192                 if (ret < 0) 
193                         return ret;
194         }
195
196         ret = cw_read(cw_bat->client, REG_CONFIG, &reg_val);
197         if (ret < 0)
198                 return ret;
199
200         if ((reg_val & 0xf8) != ATHD) {
201                 printk("the new ATHD have not set\n");
202                 reg_val &= 0x07;    // clear ATHD
203                 reg_val |= ATHD;    // set ATHD
204                 ret = cw_write(cw_bat->client, REG_CONFIG, &reg_val);
205                 if (ret < 0)
206                         return ret;
207         }
208
209         ret = cw_read(cw_bat->client, REG_CONFIG, &reg_val);
210         if (ret < 0) 
211                 return ret;
212
213         if (!(reg_val & CONFIG_UPDATE_FLG)) {
214                 printk("update flag for new battery info have not set\n");
215                 ret = cw_update_config_info(cw_bat);
216                 if (ret < 0)
217                         return ret;
218         } else {
219                 for(i = 0; i < SIZE_BATINFO; i++) { 
220                         ret = cw_read(cw_bat->client, (REG_BATINFO + i), &reg_val);
221                         if (ret < 0)
222                                 return ret;
223                         
224                         if (cw_bat->plat_data->cw_bat_config_info[i] != reg_val)
225                                 break;
226                 }
227
228                 if (i != SIZE_BATINFO) {
229                         printk("update flag for new battery info have not set\n"); 
230                         ret = cw_update_config_info(cw_bat);
231                         if (ret < 0)
232                                 return ret;
233                 }
234         }
235
236         for (i = 0; i < 30; i++) {
237                 ret = cw_read(cw_bat->client, REG_SOC, &reg_val);
238                 if (ret < 0)
239                         return ret;
240                 else if (ret != 0xff) 
241                         break;
242                 
243                 msleep(100);
244                 if (i > 25)
245                         printk("cw2015 input unvalid power error\n");
246
247         }
248         
249         return 0;
250 }
251
252 static void cw_update_time_member_ac_online(struct cw_battery *cw_bat)
253 {
254         struct timespec ts;
255         int new_run_time;
256         int new_sleep_time;
257
258         ktime_get_ts(&ts);
259         new_run_time = ts.tv_sec;
260
261         get_monotonic_boottime(&ts);
262         new_sleep_time = ts.tv_sec - new_run_time;
263
264         cw_bat->run_time_ac_online = new_run_time;
265         cw_bat->sleep_time_ac_online = new_sleep_time; 
266 }
267
268 static void cw_update_time_member_capacity(struct cw_battery *cw_bat)
269 {
270         struct timespec ts;
271         int new_run_time;
272         int new_sleep_time;
273
274         ktime_get_ts(&ts);
275         new_run_time = ts.tv_sec;
276
277         get_monotonic_boottime(&ts);
278         new_sleep_time = ts.tv_sec - new_run_time;
279
280         cw_bat->run_time_capacity = new_run_time;
281         cw_bat->sleep_time_capacity = new_sleep_time; 
282 }
283
284 static int cw_quickstart(struct cw_battery *cw_bat)
285 {
286         int ret = 0;
287         u8 reg_val = MODE_QUICK_START | MODE_NORMAL;
288
289         ret = cw_write(cw_bat->client, REG_MODE, &reg_val);     //(MODE_QUICK_START | MODE_NORMAL));  // 0x30
290         if(ret < 0) {
291                 printk("Error quick start1\n");
292                 return ret;
293         }
294         
295         reg_val = MODE_NORMAL;
296         ret = cw_write(cw_bat->client, REG_MODE, &reg_val);
297         if(ret < 0) {
298                 printk("Error quick start2\n");
299                 return ret;
300         }
301         return 1;
302 }
303
304 static int cw_get_capacity(struct cw_battery *cw_bat)
305 {
306         int cw_capacity;
307         int ret;
308         u8 reg_val;
309
310         struct timespec ts;
311         long new_run_time;
312         long new_sleep_time;
313         long capacity_or_aconline_time;
314         int allow_change;
315         int allow_capacity;
316         static int if_quickstart = 0;
317
318
319         ret = cw_read(cw_bat->client, REG_SOC, &reg_val);
320         if (ret < 0)
321                 return ret;
322
323         cw_capacity = reg_val;
324         if ((cw_capacity < 0) || (cw_capacity > 100)) {
325                 printk("get cw_capacity error; cw_capacity = %d\n", cw_capacity);
326                 return cw_capacity;
327         } 
328
329         if (cw_capacity == 0) 
330                 xprintk("the cw201x capacity is 0 !!!!!!!, funciton: %s, line: %d\n", __func__, __LINE__);
331         else 
332                 xprintk("the cw201x capacity is %d, funciton: %s\n", cw_capacity, __func__);
333
334         ret = cw_read(cw_bat->client, REG_SOC + 1, &reg_val);
335
336         ktime_get_ts(&ts);
337         new_run_time = ts.tv_sec;
338
339         get_monotonic_boottime(&ts);
340         new_sleep_time = ts.tv_sec - new_run_time;
341
342         if ((cw_bat->dc_online == 1) && (cw_capacity >= 95) && (cw_capacity <= cw_bat->capacity)) {     // avoid no charge full
343
344                 capacity_or_aconline_time = (cw_bat->sleep_time_capacity > cw_bat->sleep_time_ac_online) ? cw_bat->sleep_time_capacity : cw_bat->sleep_time_ac_online;
345                 capacity_or_aconline_time += (cw_bat->run_time_capacity > cw_bat->run_time_ac_online) ? cw_bat->run_time_capacity : cw_bat->run_time_ac_online;
346                 allow_change = (new_sleep_time + new_run_time - capacity_or_aconline_time) / BATTERY_UP_MAX_CHANGE;
347                 if (allow_change > 0) {
348                         allow_capacity = cw_bat->capacity + allow_change; 
349                         cw_capacity = (allow_capacity <= 100) ? allow_capacity : 100;
350                 }
351
352         } else if (((cw_bat->dc_online == 1) && (cw_capacity == (cw_bat->capacity - 1)))
353                         || ((cw_bat->dc_online == 0) && (cw_capacity == (cw_bat->capacity + 1)))) {             // modify battery level swing
354
355                 if (!(cw_capacity == 0 && cw_bat->capacity == 1)) {                     
356                         cw_capacity = cw_bat->capacity;
357                 }
358                                 
359
360         } else if ((cw_capacity == 0) && (cw_bat->capacity > 1)) {              // avoid battery level jump to 0% at a moment from more than 2%
361                 allow_change = ((new_run_time - cw_bat->run_time_capacity) / BATTERY_DOWN_MIN_CHANGE_RUN);
362                 allow_change += ((new_sleep_time - cw_bat->sleep_time_capacity) / BATTERY_DOWN_MIN_CHANGE_SLEEP);
363
364                 allow_capacity = cw_bat->capacity - allow_change;
365                 cw_capacity = (allow_capacity >= cw_capacity) ? allow_capacity: cw_capacity;
366         } 
367  
368 #if 1   
369         if((cw_bat->dc_online == 1) &&(cw_capacity == 0))
370         {                 
371                 if (((new_sleep_time + new_run_time - cw_bat->sleep_time_ac_online - cw_bat->run_time_ac_online) > BATTERY_DOWN_MAX_CHANGE_RUN_AC_ONLINE) && (if_quickstart == 0)) {
372                         cw_quickstart(cw_bat);      // if the cw_capacity = 0 the cw2015 will qstrt
373                         if_quickstart = 1;
374                 } else if (if_quickstart == 1) {
375                         if_quickstart = 0;
376                 }
377         } else if (if_quickstart == 1) {
378                 if_quickstart = 0;
379         }
380 #endif
381
382         if((cw_capacity == 100) && (gpio_get_value(cw_bat->plat_data->chg_ok_pin) != cw_bat->plat_data->chg_ok_level))
383                 cw_capacity = 99;
384
385         return cw_capacity;
386 }
387
388 static int cw_get_vol(struct cw_battery *cw_bat)
389 {
390         int ret;
391         u8 reg_val;
392         u16 value16, value16_1, value16_2, value16_3;
393         int voltage;
394
395         ret = cw_read(cw_bat->client, REG_VCELL, &reg_val);
396         if (ret < 0)
397                 return ret;
398
399         value16 = reg_val;
400
401         ret = cw_read(cw_bat->client, REG_VCELL + 1, &reg_val);
402         if (ret < 0)
403                 return ret;
404
405         value16 = (value16 << 8) + reg_val;
406         
407         ret = cw_read(cw_bat->client, REG_VCELL, &reg_val);
408         if (ret < 0)
409                 return ret;
410
411         value16_1 = reg_val;
412
413         ret = cw_read(cw_bat->client, REG_VCELL + 1, &reg_val);
414         if (ret < 0)
415                 return ret;
416
417         value16_1 = (value16_1 << 8) + reg_val;
418         
419         ret = cw_read(cw_bat->client, REG_VCELL, &reg_val);
420         if (ret < 0)
421                 return ret;
422
423         value16_2 = reg_val;
424
425         ret = cw_read(cw_bat->client, REG_VCELL + 1, &reg_val);
426         if (ret < 0)
427                 return ret;
428
429         value16_2 = (value16_2 << 8) + reg_val;
430                 
431                 
432         if(value16 > value16_1)
433             {    
434                 value16_3 = value16;
435                     value16 = value16_1;
436                     value16_1 = value16_3;
437         }
438                 
439         if(value16_1 > value16_2)
440             {
441                 value16_3 =value16_1;
442                         value16_1 =value16_2;
443                         value16_2 =value16_3;
444             }
445                         
446         if(value16 >value16_1)
447             {    
448                 value16_3 =value16;
449                         value16 =value16_1;
450                         value16_1 =value16_3;
451         }                       
452
453         voltage = value16_1 * 312 / 1024;
454
455         return voltage;
456 }
457
458 static int cw_get_time_to_empty(struct cw_battery *cw_bat)
459 {
460         int ret;
461         u8 reg_val;
462         u16 value16;
463
464         ret = cw_read(cw_bat->client, REG_RRT_ALERT, &reg_val);
465         if (ret < 0)
466                 return ret;
467
468         value16 = reg_val;
469
470         ret = cw_read(cw_bat->client, REG_RRT_ALERT + 1, &reg_val);
471         if (ret < 0)
472                 return ret;
473
474         value16 = ((value16 << 8) + reg_val) & 0x1fff;
475         return value16;
476 }
477
478 static void rk_bat_update_capacity(struct cw_battery *cw_bat)
479 {
480         int cw_capacity;
481
482         cw_capacity = cw_get_capacity(cw_bat);
483         if ((cw_capacity >= 0) && (cw_capacity <= 100) && (cw_bat->capacity != cw_capacity)) {
484                 cw_bat->capacity = cw_capacity;
485                 cw_bat->bat_change = 1;
486                 cw_update_time_member_capacity(cw_bat);
487
488                 if (cw_bat->capacity == 0)
489                         printk("report battery capacity 0 and will shutdown if no changing");
490
491         }
492 }
493
494
495
496 static void rk_bat_update_vol(struct cw_battery *cw_bat)
497 {
498         int ret;
499         ret = cw_get_vol(cw_bat);
500         if ((ret >= 0) && (cw_bat->voltage != ret)) {
501                 cw_bat->voltage = ret;
502                 cw_bat->bat_change = 1;
503         }
504 }
505
506 static void rk_bat_update_status(struct cw_battery *cw_bat)
507 {
508         int status;
509
510
511         if ((cw_bat->dc_online == 1) || (cw_bat->usb_online == 1)) {
512                 if (cw_bat->capacity >= 100) 
513                         status=POWER_SUPPLY_STATUS_FULL;
514                 else
515                         status=POWER_SUPPLY_STATUS_CHARGING;
516         } else {
517                 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
518         }
519
520         if (cw_bat->status != status) {
521                 cw_bat->status = status;
522                 cw_bat->bat_change = 1;
523        
524         } 
525 }
526
527 static void rk_bat_update_time_to_empty(struct cw_battery *cw_bat)
528 {
529         int ret;
530         ret = cw_get_time_to_empty(cw_bat);
531         if ((ret >= 0) && (cw_bat->time_to_empty != ret)) {
532                 cw_bat->time_to_empty = ret;
533                 cw_bat->bat_change = 1;
534         }
535         
536 }
537
538 static int rk_ac_update_online(struct cw_battery *cw_bat)
539 {
540         if (gpio_get_value(cw_bat->plat_data->dc_det_pin) == cw_bat->plat_data->dc_det_level) {
541                 if (cw_bat->dc_online != 1) {
542                         cw_update_time_member_ac_online(cw_bat);
543                         cw_bat->dc_online = 1;
544                         return 1;
545                 }
546         } else {
547                 if (cw_bat->dc_online != 0) {
548                         cw_update_time_member_ac_online(cw_bat);
549                         cw_bat->dc_online = 0;
550                         return 1;
551                 }
552         }
553
554         return 0;
555 }
556
557 static int rk_usb_update_online(struct cw_battery *cw_bat)
558 {
559         cw_bat->usb_online = 0;
560         return 0;
561 }
562
563 static void cw_bat_work(struct work_struct *work)
564 {
565         struct delayed_work *delay_work;
566         struct cw_battery *cw_bat;
567         int ret;
568
569         delay_work = container_of(work, struct delayed_work, work);
570         cw_bat = container_of(delay_work, struct cw_battery, battery_delay_work);
571
572         
573
574         ret = rk_ac_update_online(cw_bat);
575         if (ret == 1) {
576                 power_supply_changed(&cw_bat->rk_ac);
577         }
578
579         ret = rk_usb_update_online(cw_bat);
580         if (ret == 1) {
581                 power_supply_changed(&cw_bat->rk_usb);
582         }
583
584         rk_bat_update_status(cw_bat);
585         rk_bat_update_capacity(cw_bat);
586         rk_bat_update_vol(cw_bat);
587         rk_bat_update_time_to_empty(cw_bat);
588
589         if (cw_bat->bat_change) {
590                 power_supply_changed(&cw_bat->rk_bat);
591                 cw_bat->bat_change = 0;
592         }
593
594         queue_delayed_work(cw_bat->battery_workqueue, &cw_bat->battery_delay_work, msecs_to_jiffies(1000));
595
596         xprintk("cw_bat->bat_change = %d, cw_bat->time_to_empty = %d, cw_bat->capacity = %d, cw_bat->voltage = %d, cw_bat->dc_online = %d, cw_bat->usb_online = %d\n",\
597                         cw_bat->bat_change, cw_bat->time_to_empty, cw_bat->capacity, cw_bat->voltage, cw_bat->dc_online, cw_bat->usb_online);
598 }
599
600 static int rk_usb_get_property (struct power_supply *psy,
601                 enum power_supply_property psp,
602                 union power_supply_propval *val)
603 {
604         int ret = 0;
605         struct cw_battery *cw_bat;
606
607         cw_bat = container_of(psy, struct cw_battery, rk_usb);
608         switch (psp) {
609         case POWER_SUPPLY_PROP_ONLINE:
610                 val->intval = cw_bat->usb_online;
611                 break;
612         default:
613                 break;
614         }
615         return ret;
616 }
617
618 static enum power_supply_property rk_usb_properties[] = {
619         POWER_SUPPLY_PROP_ONLINE,
620 };
621
622
623 static int rk_ac_get_property (struct power_supply *psy,
624                 enum power_supply_property psp,
625                 union power_supply_propval *val)
626 {
627         int ret = 0;
628         struct cw_battery *cw_bat;
629
630         cw_bat = container_of(psy, struct cw_battery, rk_ac);
631         switch (psp) {
632         case POWER_SUPPLY_PROP_ONLINE:
633                 val->intval = cw_bat->dc_online;
634                 break;
635         default:
636                 break;
637         }
638         return ret;
639 }
640
641 static enum power_supply_property rk_ac_properties[] = {
642         POWER_SUPPLY_PROP_ONLINE,
643 };
644
645 static int rk_battery_get_property(struct power_supply *psy,
646                 enum power_supply_property psp,
647                 union power_supply_propval *val)
648 {
649         int ret = 0;
650         struct cw_battery *cw_bat;
651
652         cw_bat = container_of(psy, struct cw_battery, rk_bat); 
653         switch (psp) {
654         case POWER_SUPPLY_PROP_CAPACITY:
655                 val->intval = cw_bat->capacity;
656                 break;
657         case POWER_SUPPLY_PROP_STATUS:
658                 val->intval = cw_bat->status;
659                 break;
660                 
661         case POWER_SUPPLY_PROP_HEALTH:
662                 val->intval= POWER_SUPPLY_HEALTH_GOOD;
663                 break;
664         case POWER_SUPPLY_PROP_PRESENT:
665                 val->intval = cw_bat->voltage <= 0 ? 0 : 1;
666                 break;
667                 
668         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
669                 val->intval = cw_bat->voltage;
670                 break;
671                 
672         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
673                 val->intval = cw_bat->time_to_empty;                    
674                 break;
675             
676         case POWER_SUPPLY_PROP_TECHNOLOGY:
677                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
678                 break;
679
680         default:
681                 break;
682         }
683         return ret;
684 }
685
686 static enum power_supply_property rk_battery_properties[] = {
687         POWER_SUPPLY_PROP_CAPACITY,
688         POWER_SUPPLY_PROP_STATUS,
689         POWER_SUPPLY_PROP_HEALTH,
690         POWER_SUPPLY_PROP_PRESENT,
691         POWER_SUPPLY_PROP_VOLTAGE_NOW,
692         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
693         POWER_SUPPLY_PROP_TECHNOLOGY,
694 };
695
696 static int cw_bat_gpio_init(struct cw_battery *cw_bat)
697 {
698
699         int ret;
700         gpio_free(cw_bat->plat_data->dc_det_pin);
701         if (cw_bat->plat_data->dc_det_pin != INVALID_GPIO) {
702                 ret = gpio_request(cw_bat->plat_data->dc_det_pin, NULL);
703                 if (ret) {
704                         printk("failed to request dc_det_pin gpio\n");
705                         goto request_dc_det_pin_fail;
706                 }
707
708                 gpio_pull_updown(cw_bat->plat_data->dc_det_pin, GPIOPullUp);
709                 ret = gpio_direction_input(cw_bat->plat_data->dc_det_pin);
710                 if (ret) {
711                         printk("failed to set dc_det_pin input\n");
712                         goto request_bat_low_pin_fail;
713                 }
714         }
715         if (cw_bat->plat_data->bat_low_pin != INVALID_GPIO) {
716                 ret = gpio_request(cw_bat->plat_data->bat_low_pin, NULL);
717                 if (ret) {
718                         printk("failed to request bat_low_pin gpio\n");
719                         goto request_bat_low_pin_fail;
720                 }
721
722                 gpio_pull_updown(cw_bat->plat_data->bat_low_pin, GPIOPullUp);
723                 ret = gpio_direction_input(cw_bat->plat_data->bat_low_pin);
724                 if (ret) {
725                         printk("failed to set bat_low_pin input\n");
726                         goto request_chg_ok_pin_fail;
727                 }
728         }
729         if (cw_bat->plat_data->chg_ok_pin != INVALID_GPIO) {
730                 ret = gpio_request(cw_bat->plat_data->chg_ok_pin, NULL);
731                 if (ret) {
732                         printk("failed to request chg_ok_pin gpio\n");
733                         goto request_chg_ok_pin_fail;
734                 }
735
736                 gpio_pull_updown(cw_bat->plat_data->chg_ok_pin, GPIOPullUp);
737                 ret = gpio_direction_input(cw_bat->plat_data->chg_ok_pin);
738                 if (ret) {
739                         printk("failed to set chg_ok_pin input\n");
740                         gpio_free(cw_bat->plat_data->chg_ok_pin); 
741                         goto request_chg_ok_pin_fail;
742                 }
743         }
744         return 0;
745
746         
747 request_chg_ok_pin_fail:
748         if (cw_bat->plat_data->bat_low_pin != INVALID_GPIO)
749                 gpio_free(cw_bat->plat_data->bat_low_pin);
750
751 request_bat_low_pin_fail:
752         if (cw_bat->plat_data->dc_det_pin != INVALID_GPIO) 
753                 gpio_free(cw_bat->plat_data->dc_det_pin);
754
755 request_dc_det_pin_fail:
756         return ret;
757
758 }
759
760
761 static void dc_detect_do_wakeup(struct work_struct *work)
762 {
763         int ret;
764         int irq;
765         unsigned int type;
766
767         struct delayed_work *delay_work;
768         struct cw_battery *cw_bat;
769
770         delay_work = container_of(work, struct delayed_work, work);
771         cw_bat = container_of(delay_work, struct cw_battery, dc_wakeup_work);
772
773         rk28_send_wakeup_key();
774
775         irq = gpio_to_irq(cw_bat->plat_data->dc_det_pin);
776         type = gpio_get_value(cw_bat->plat_data->dc_det_pin) ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
777         ret = irq_set_irq_type(irq, type);
778         if (ret < 0) {
779                 pr_err("%s: irq_set_irq_type(%d, %d) failed\n", __func__, irq, type);
780         }
781         enable_irq(irq);
782 }
783
784 static irqreturn_t dc_detect_irq_handler(int irq, void *dev_id)
785 {
786         struct cw_battery *cw_bat = dev_id;
787         disable_irq_nosync(irq); // for irq debounce
788         //wake_lock_timeout(&usb_wakelock, WAKE_LOCK_TIMEOUT);
789         //schedule_delayed_work(&wakeup_work, HZ / 10);
790         queue_delayed_work(cw_bat->battery_workqueue, &cw_bat->dc_wakeup_work, msecs_to_jiffies(20));
791         return IRQ_HANDLED;
792 }
793
794
795 static int cw_bat_probe(struct i2c_client *client, const struct i2c_device_id *id)
796 {
797         struct cw_battery *cw_bat;
798         int ret;
799         int irq;
800         int irq_flags;
801
802         cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL);
803         if (!cw_bat) {
804                 printk("fail to allocate memory\n");
805                 return -ENOMEM;
806         }
807
808         i2c_set_clientdata(client, cw_bat);
809         cw_bat->plat_data = client->dev.platform_data;
810         ret = cw_bat_gpio_init(cw_bat);
811         if (ret) {
812                 printk("cw_bat_gpio_init error\n");
813                 return ret;
814         }
815         
816         cw_bat->client = client;
817         ret = cw_init(cw_bat);
818         if (ret) 
819                 return ret;
820         
821         cw_bat->rk_bat.name = "rk-bat";
822         cw_bat->rk_bat.type = POWER_SUPPLY_TYPE_BATTERY;
823         cw_bat->rk_bat.properties = rk_battery_properties;
824         cw_bat->rk_bat.num_properties = ARRAY_SIZE(rk_battery_properties);
825         cw_bat->rk_bat.get_property = rk_battery_get_property;
826         ret = power_supply_register(&client->dev, &cw_bat->rk_bat);
827         if(ret < 0) {
828                 printk("power supply register rk_bat error\n");
829                 goto rk_bat_register_fail;
830         }
831
832         cw_bat->rk_ac.name = "rk-ac";
833         cw_bat->rk_ac.type = POWER_SUPPLY_TYPE_MAINS;
834         cw_bat->rk_ac.properties = rk_ac_properties;
835         cw_bat->rk_ac.num_properties = ARRAY_SIZE(rk_ac_properties);
836         cw_bat->rk_ac.get_property = rk_ac_get_property;
837         ret = power_supply_register(&client->dev, &cw_bat->rk_ac);
838         if(ret < 0) {
839                 printk("power supply register rk_ac error\n");
840                 goto rk_ac_register_fail;
841         }
842
843         cw_bat->rk_usb.name = "rk-usb";
844         cw_bat->rk_usb.type = POWER_SUPPLY_TYPE_USB;
845         cw_bat->rk_usb.properties = rk_usb_properties;
846         cw_bat->rk_usb.num_properties = ARRAY_SIZE(rk_usb_properties);
847         cw_bat->rk_usb.get_property = rk_usb_get_property;
848         ret = power_supply_register(&client->dev, &cw_bat->rk_usb);
849         if(ret < 0) {
850                 printk("power supply register rk_ac error\n");
851                 goto rk_usb_register_fail;
852         }
853
854         cw_bat->dc_online = 0;
855         cw_bat->usb_online = 0;
856         cw_bat->capacity = 2;
857         cw_bat->voltage = 0;
858         cw_bat->status = 0;
859         cw_bat->time_to_empty = 0;
860         cw_bat->bat_change = 0;
861
862         cw_update_time_member_capacity(cw_bat);
863         cw_update_time_member_ac_online(cw_bat);
864
865         cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery");
866         INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work);
867         INIT_DELAYED_WORK(&cw_bat->dc_wakeup_work, dc_detect_do_wakeup);
868         queue_delayed_work(cw_bat->battery_workqueue, &cw_bat->battery_delay_work, msecs_to_jiffies(10));
869         
870         irq = gpio_to_irq(cw_bat->plat_data->dc_det_pin);
871         irq_flags = gpio_get_value(cw_bat->plat_data->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
872         ret = request_irq(irq, dc_detect_irq_handler, irq_flags, "usb_detect", cw_bat);
873         if (ret < 0) {
874                 pr_err("%s: request_irq(%d) failed\n", __func__, irq);
875         }
876         enable_irq_wake(irq);
877
878         printk("cw2015 driver v1.0 probe sucess\n");
879         return 0;
880
881 rk_usb_register_fail:
882         power_supply_unregister(&cw_bat->rk_bat);
883 rk_ac_register_fail:
884         power_supply_unregister(&cw_bat->rk_ac);
885 rk_bat_register_fail:
886         printk("cw2015 driver v1.0 probe error!!!!\n");
887         return ret;
888 }
889
890 static int cw_bat_remove(struct i2c_client *client)
891 {
892         struct cw_battery *cw_bat = i2c_get_clientdata(client);
893         xprintk();
894         cancel_delayed_work(&cw_bat->battery_delay_work);
895         return 0;
896 }
897
898 #ifdef CONFIG_PM
899 static int cw_bat_suspend(struct device *dev)
900 {
901         struct i2c_client *client = to_i2c_client(dev);
902         struct cw_battery *cw_bat = i2c_get_clientdata(client);
903         xprintk();
904         cancel_delayed_work(&cw_bat->battery_delay_work);
905         return 0;
906 }
907
908 static int cw_bat_resume(struct device *dev)
909 {
910         struct i2c_client *client = to_i2c_client(dev);
911         struct cw_battery *cw_bat = i2c_get_clientdata(client);
912         xprintk();
913         queue_delayed_work(cw_bat->battery_workqueue, &cw_bat->battery_delay_work, msecs_to_jiffies(100));
914         return 0;
915 }
916
917 static const struct i2c_device_id cw_id[] = {
918         { "cw201x", 0 },
919 };
920 MODULE_DEVICE_TABLE(i2c, cw_id);
921
922 static const struct dev_pm_ops cw_bat_pm_ops = {
923         .suspend  = cw_bat_suspend,
924         .resume   = cw_bat_resume,
925 };
926 #endif
927
928 static struct i2c_driver cw_bat_driver = {
929         .driver         = {
930                 .name   = "cw201x",
931 #ifdef CONFIG_PM
932                 .pm     = &cw_bat_pm_ops,
933 #endif
934         },
935         
936         .probe          = cw_bat_probe,
937         .remove         = cw_bat_remove,
938         .id_table       = cw_id,
939 };
940
941 static int __init cw_bat_init(void)
942 {
943         xprintk();
944         return i2c_add_driver(&cw_bat_driver);
945 }
946
947 static void __exit cw_bat_exit(void)
948 {
949         xprintk();
950         i2c_del_driver(&cw_bat_driver);
951 }
952
953 fs_initcall(cw_bat_init);
954 module_exit(cw_bat_exit);
955
956 MODULE_AUTHOR("xhc<xhc@rock-chips.com>");
957 MODULE_DESCRIPTION("cw2015 battery driver");
958 MODULE_LICENSE("GPL");
959