rk31xx:RT5025:support pmic rt5025
[firefly-linux-kernel-4.4.55.git] / drivers / power / rt5025-gauge.c
1 /*
2  *  rt5025_gauge.c
3  *  fuel-gauge driver
4  *  revision 0.1
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/slab.h>
17 #include <linux/wakelock.h>
18 #include <linux/workqueue.h>
19 #include <linux/jiffies.h>
20 #include <linux/timer.h>
21 #include <linux/android_alarm.h>
22 #include <linux/mfd/rt5025.h>
23 #include <linux/power/rt5025-gauge.h>
24
25 #define RT5025_REG_IRQ_CTL                                              0x50
26 #define RT5025_REG_IRQ_FLAG                                             0x51
27 #define RT5025_REG_VALRT_MAXTH                          0x53
28 #define RT5025_REG_VALRT_MIN1TH                         0x54
29 #define RT5025_REG_VALRT_MIN2TH                         0x55
30 #define RT5025_REG_TALRT_MAXTH                          0x56
31 #define RT5025_REG_TALRT_MINTH                          0x57
32 #define RT5025_REG_VCELL_MSB                                    0x58
33 #define RT5025_REG_VCELL_LSB                                    0x59
34 #define RT5025_REG_INT_TEMPERATUE_MSB   0x5B
35 #define RT5025_REG_INT_TEMPERATUE_LSB 0x5C
36 #define RT5025_REG_EXT_TEMPERATUE_MSB   0x5E
37 #define RT5025_REG_EXT_TEMPERATUE_LSB   0x5F
38 #define RT5025_REG_TIMER                                                        0x60
39 #define RT5025_REG_CHANNEL_MSB          0x62
40 #define RT5025_REG_CHANNEL_LSB          0x63
41 #define RT5025_REG_CURRENT_MSB                          0x76
42 #define RT5025_REG_CURRENT_LSB                          0x77
43 #define RT5025_REG_QCHGH_MSB          0x78
44 #define RT5025_REG_QCHGH_LSB          0x79
45 #define RT5025_REG_QCHGL_MSB          0x7A
46 #define RT5025_REG_QCHGL_LSB          0x7B
47 #define RT5025_REG_QDCHGH_MSB                                   0x7C
48 #define RT5025_REG_QDCHGH_LSB                                   0x7D
49 #define RT5025_REG_QDCHGL_MSB                                   0x7E
50 #define RT5025_REG_QDCHGL_LSB                                   0x7F
51
52 #define IRQ_CTL_BIT_TMX  (1 << 5)
53 #define IRQ_CTL_BIT_TMN  (1 << 4)
54 #define IRQ_CTL_BIT_VMX  (1 << 2)
55 #define IRQ_CTL_BIT_VMN1 (1 << 1)
56 #define IRQ_CTL_BIT_VMN2 (1 << 0)
57
58 #define IRQ_FLG_BIT_TMX  (1 << 5)
59 #define IRQ_FLG_BIT_TMN  (1 << 4)
60 #define IRQ_FLG_BIT_VMX  (1 << 2)
61 #define IRQ_FLG_BIT_VMN1 (1 << 1)
62 #define IRQ_FLG_BIT_VMN2 (1 << 0)
63
64 #define CHANNEL_H_BIT_CLRQDCHG  (1 << 7)
65 #define CHANNEL_H_BIT_CLRQCHG   (1 << 6)
66
67 #define CHANNEL_L_BIT_CADC_EN   (1 << 7)
68 #define CHANNEL_L_BIT_INTEMPCH  (1 << 6)
69 #define CHANNEL_L_BIT_AINCH     (1 << 2)
70 #define CHANNEL_L_BIT_VBATSCH   (1 << 1)
71 #define CHANNEL_L_BIT_VADC_EN   (1 << 0)
72
73 #define NORMAL_POLL 10  /* 10 sec */
74 #define SUSPEND_POLL (30*60) /* 30 min */
75
76 #define HIGH_TEMP_THRES 650
77 #define HIGH_TEMP_RECOVER       430
78 #define LOW_TEMP_THRES (-30)
79 #define LOW_TEMP_RECOVER 0
80 #define TEMP_ABNORMAL_COUNT     3
81
82 #define TALRTMAX_VALUE  0x38 //65.39'C 0x9
83 #define TALRTMIN_VALUE  0x11 //-18.75'C 0x17
84 #define VALRTMAX_VALUE  0xDC //4297mV
85 #define VALRTMIN1_VALUE 0xB8 //3600mV
86 #define VALRTMIN2_VALUE 0x99 //3000mV
87 #define TRLS_VALUE      55   //5'C
88 #define VRLS_VALUE      100  //100mV
89
90 #define IRQ_THRES_UNIT 1953
91
92 struct rt5025_gauge_chip {
93   struct i2c_client *client;
94   struct rt5025_power_info *info;
95   struct rt5025_gauge_callbacks cb;
96
97         struct power_supply     battery;
98         
99         struct delayed_work monitor_work;
100         struct wake_lock monitor_wake_lock;
101         struct alarm wakeup_alarm;
102         
103         bool    suspend_poll;
104         ktime_t last_poll;
105         
106   /* battery voltage */
107   u16 vcell;
108   /* battery current */
109   s16 curr;
110   /* battery current offset */
111   u8 curr_offset;
112   /* AIN voltage */
113   u16 ain_volt;
114   /* battery external temperature */
115   s16 ext_temp;
116   /* charge coulomb counter */
117   u32 chg_cc;
118   u32 chg_cc_unuse;
119   /* discharge coulomb counter */
120   u32 dchg_cc;
121   u32 dchg_cc_unuse;
122   /* battery capacity */
123   u8 soc;
124   u16 time_interval;
125   u16 pre_gauge_timer;
126     
127   u8 online;
128   u8 status;
129   u8 health;
130
131   /* IRQ flag */
132   u8 irq_flag;
133    
134   /* max voltage IRQ flag */
135   bool max_volt_irq;
136   /* min voltage1 IRQ flag */
137   bool min_volt1_irq;  
138   /* min voltage2 IRQ flag */
139   bool min_volt2_irq;
140   /* max temperature IRQ flag */
141   bool max_temp_irq;
142   /* min temperature IRQ flag */
143   bool min_temp_irq;
144   
145         u8 temp_high_cnt;
146         u8 temp_low_cnt;
147         u8 temp_recover_cnt;
148 };
149
150 struct rt5025_gauge_chip *chip;
151 u8 irq_thres[LAST_TYPE];
152
153 void rt5025_set_status(int status)
154 {
155   chip->status = status; 
156 }
157
158 static int rt5025_read_reg(struct i2c_client *client,
159                                 u8 reg, u8 *data, u8 len)
160 {
161         struct i2c_adapter *adap = client->adapter;
162         struct i2c_msg msgs[2];
163         int ret;
164         
165         msgs[0].addr = client->addr;
166         msgs[0].flags = client->flags;
167         msgs[0].len = 1;
168         msgs[0].buf = &reg;
169         msgs[0].scl_rate = 200*1000;
170
171         msgs[1].addr = client->addr;
172         msgs[1].flags = client->flags | I2C_M_RD;
173         msgs[1].len = len;
174         msgs[1].buf = data;
175         msgs[1].scl_rate = 200*1000;
176         
177         ret = i2c_transfer(adap, msgs, 2);
178          
179         return (ret == 2)? len : ret;  
180 }
181
182 static int rt5025_write_reg(struct i2c_client *client,
183                                 u8 reg, u8 *data, u8 len)
184 {
185         struct i2c_adapter *adap = client->adapter;
186         struct i2c_msg msg;
187         int ret;
188         char *tx_buf = (char *)kmalloc(len + 1, GFP_KERNEL);
189         
190         if(!tx_buf)
191                 return -ENOMEM;
192         tx_buf[0] = reg;
193         memcpy(tx_buf+1, data, len);
194         
195         msg.addr = client->addr;
196         msg.flags = client->flags;
197         msg.len = len + 1;
198         msg.buf = (char *)tx_buf;
199         msg.scl_rate = 200*1000;        
200         ret = i2c_transfer(adap, &msg, 1);
201         kfree(tx_buf);
202         return (ret == 1) ? len : ret; 
203 }
204
205 static void rt5025_gauge_alarm(struct alarm *alarm)
206 {
207         wake_lock(&chip->monitor_wake_lock);
208         schedule_delayed_work(&chip->monitor_work, 0);
209 }
210
211 static void rt5025_program_alarm(int seconds)
212 {
213         ktime_t low_interval = ktime_set(seconds, 0);
214         ktime_t slack = ktime_set(20, 0);
215         ktime_t next;
216
217         next = ktime_add(chip->last_poll, low_interval);
218         alarm_start_range(&chip->wakeup_alarm, next, ktime_add(next, slack));
219 }
220
221 static int rt5025_get_property(struct power_supply *psy,
222                             enum power_supply_property psp,
223                             union power_supply_propval *val)
224 {
225   switch (psp) {
226     case POWER_SUPPLY_PROP_STATUS:
227       val->intval = chip->status;
228       break;
229     case POWER_SUPPLY_PROP_HEALTH:
230       val->intval = chip->health;
231       break;
232     case POWER_SUPPLY_PROP_PRESENT:
233       val->intval = chip->online;
234       break;
235     case POWER_SUPPLY_PROP_TEMP:
236       val->intval = chip->ext_temp;
237       break;
238     case POWER_SUPPLY_PROP_ONLINE:
239       val->intval = 1;
240       break;
241     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
242       val->intval = chip->vcell * 1000; //uV
243       break;
244     case POWER_SUPPLY_PROP_CURRENT_NOW:
245       val->intval = chip->curr * 1000; //uA
246       break;
247     case POWER_SUPPLY_PROP_CAPACITY:
248       val->intval = chip->soc;
249       if (val->intval > 100)
250                                 val->intval = 100;
251       break;
252     case POWER_SUPPLY_PROP_TECHNOLOGY:
253       val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
254       break;
255     default:
256       return -EINVAL;
257   }
258   return 0;
259 }
260
261 static void rt5025_get_vcell(struct i2c_client *client)
262 {
263   u8 data[2];
264         
265   if (rt5025_read_reg(client, RT5025_REG_VCELL_MSB, data, 2) < 0){
266     printk(KERN_ERR "%s: Failed to read Voltage\n", __func__);
267   }
268                 
269   chip->vcell = ((data[0] << 8) + data[1]) * 61 / 100;
270         chip->curr_offset = (15444 * chip->vcell - 27444000) / 10000;
271                 
272   RTINFO("[RT5025] vcell: %d, offset: %d\n", chip->vcell, chip->curr_offset);
273 }
274
275 static void rt5025_get_current(struct i2c_client *client)
276 {
277   u8 data[2];
278   u16 temp;
279   int sign = 0;
280
281   if (rt5025_read_reg(client, RT5025_REG_CURRENT_MSB, data, 2) < 0) {
282     printk(KERN_ERR "%s: Failed to read CURRENT\n", __func__);
283   }
284
285   temp = (data[0]<<8) | data[1];
286   if (data[0] & (1 << 7)) {
287     sign = 1;
288     temp = (((temp & 0x7FFF) * 3125) / 10 + chip->curr_offset) / 1000;
289   }else
290                 temp = ((temp * 3125) / 10 - chip->curr_offset) / 1000;
291
292   if (sign)
293     temp *= -1;
294
295         chip->curr = temp;
296   RTINFO("[RT5025] current: %d\n", chip->curr);
297 }
298
299 static void rt5025_get_external_temp(struct i2c_client *client)
300 {
301   u8 data[2];
302   long int temp;
303
304   if (rt5025_read_reg(client, RT5025_REG_EXT_TEMPERATUE_MSB, data, 2) < 0) {
305     printk(KERN_ERR "%s: Failed to read TEMPERATURE\n", __func__);
306   }
307   chip->ain_volt = (data[0] * 256 + data[1]) * 61 / 100;
308   /// Check battery present
309         if (chip->ain_volt < 1150)
310                 chip->online = true;
311         else
312                 chip->online = false;
313   temp =  (chip->ain_volt * (-91738) + 81521000) / 100000;
314   chip->ext_temp = (int)temp;
315         
316         if (chip->ext_temp >= HIGH_TEMP_THRES) {
317                 if (chip->health != POWER_SUPPLY_HEALTH_OVERHEAT)
318                         chip->temp_high_cnt++;
319         } else if (chip->ext_temp <= HIGH_TEMP_RECOVER && chip->ext_temp >= LOW_TEMP_RECOVER) {
320                 if (chip->health == POWER_SUPPLY_HEALTH_OVERHEAT ||
321                     chip->health == POWER_SUPPLY_HEALTH_COLD)
322                         chip->temp_recover_cnt++;
323         } else if (chip->ext_temp <= LOW_TEMP_THRES) {
324                 if (chip->health != POWER_SUPPLY_HEALTH_COLD)
325                         chip->temp_low_cnt++;
326         } else {
327                 chip->temp_high_cnt = 0;
328                 chip->temp_low_cnt = 0;
329                 chip->temp_recover_cnt = 0;
330         }
331         
332         if (chip->temp_high_cnt >= TEMP_ABNORMAL_COUNT) {
333          chip->health = POWER_SUPPLY_HEALTH_OVERHEAT;
334          chip->temp_high_cnt = 0;
335         } else if (chip->temp_low_cnt >= TEMP_ABNORMAL_COUNT) {
336          chip->health = POWER_SUPPLY_HEALTH_COLD;
337          chip->temp_low_cnt = 0;
338         } else if (chip->temp_recover_cnt >= TEMP_ABNORMAL_COUNT) {
339          chip->health = POWER_SUPPLY_HEALTH_GOOD;
340          chip->temp_recover_cnt = 0;
341         }
342   RTINFO("[RT5025] external temperature: %d\n", chip->ext_temp);
343 }
344
345 static void rt5025_clear_cc(operation_mode mode)
346 {  
347   u8 data[2];
348         
349   if (rt5025_read_reg(chip->client, RT5025_REG_CHANNEL_MSB, data, 2) < 0){
350     printk(KERN_ERR "%s: failed to read channel\n", __func__);
351   }
352
353   if (mode == CHG)
354                 data[0] = data[0] | CHANNEL_H_BIT_CLRQCHG;
355         else
356                 data[0] = data[0] | CHANNEL_H_BIT_CLRQDCHG;
357                 
358   if (rt5025_write_reg(chip->client, RT5025_REG_CHANNEL_MSB, data, 2) < 0){
359     printk(KERN_ERR "%s: failed to write channel\n", __func__);
360   }
361 }
362
363 static void rt5025_get_chg_cc(struct i2c_client *client)
364 {
365   u8 data[4];
366   u32 qh_old,ql_old,qh_new,ql_new;
367   u32 cc_masec,offset;
368   
369   if (rt5025_read_reg(client, RT5025_REG_QCHGH_MSB, data, 4) < 0){
370     printk(KERN_ERR "%s: Failed to read QCHG\n", __func__);
371   }
372   qh_old = (data[0]<<8) + data[1];
373   ql_old = (data[2]<<8) + data[3];
374   
375   if (rt5025_read_reg(client, RT5025_REG_QCHGH_MSB, data, 4) < 0){
376     printk(KERN_ERR "%s: Failed to read QCHG\n", __func__);
377   }
378   qh_new = (data[0]<<8) + data[1];
379   ql_new = (data[2]<<8) + data[3];
380         
381   if (qh_new > qh_old){
382      cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
383   }else if (qh_new == qh_old){
384     if (ql_new >= ql_old){
385       cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
386     }else {  
387       cc_masec = (((qh_old<<16) + ql_old) * 50134) / 10;
388                 }
389   }     
390   
391         offset = chip->curr_offset * chip->time_interval;
392                           
393   if (cc_masec != 0){
394                 cc_masec = (cc_masec - offset) / 1000;
395         }
396
397   RTINFO("[RT5025] chg_cc_mAsec: %d\n", cc_masec);
398
399         chip->chg_cc = (cc_masec + chip->chg_cc_unuse) / 3600;
400   chip->chg_cc_unuse = (cc_masec + chip->chg_cc_unuse) % 3600;
401   RTINFO("[RT5025] chg_cc_mAH: %d\n", chip->chg_cc);
402   rt5025_clear_cc(CHG);
403 }
404
405 static void rt5025_get_dchg_cc(struct i2c_client *client)
406 {
407   u8 data[4];
408   u32 qh_old,ql_old,qh_new,ql_new;
409   u32 cc_masec,offset;
410   
411   if (rt5025_read_reg(client, RT5025_REG_QDCHGH_MSB, data, 4) < 0){
412     printk(KERN_ERR "%s: Failed to read QDCHG\n", __func__);
413   }
414   qh_old = (data[0]<<8) + data[1];
415   ql_old = (data[2]<<8) + data[3];
416   
417   if (rt5025_read_reg(client, RT5025_REG_QDCHGH_MSB, data, 4) < 0){
418     printk(KERN_ERR "%s: Failed to read QDCHG\n", __func__);
419   }
420   qh_new = (data[0]<<8) + data[1];
421   ql_new = (data[2]<<8) + data[3];
422   
423   if (qh_new > qh_old){
424      cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
425   }else if (qh_new == qh_old){
426     if (ql_new >= ql_old){
427       cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
428     }else {  
429       cc_masec = (((qh_old<<16) + ql_old) * 50134) / 10;
430                 }
431   }     
432   
433         offset = chip->curr_offset * chip->time_interval;
434                           
435   if (cc_masec != 0){
436                 cc_masec = (cc_masec - offset) / 1000;
437         }
438
439   RTINFO("[RT5025] dchg_cc_mAsec: %d\n", cc_masec);
440
441         chip->dchg_cc = (cc_masec + chip->dchg_cc_unuse) / 3600;
442   chip->dchg_cc_unuse = (cc_masec + chip->dchg_cc_unuse) % 3600;
443   RTINFO("[RT5025] dchg_cc_mAH: %d\n", chip->dchg_cc);
444         rt5025_clear_cc(DCHG);
445 }
446
447 static void rt5025_get_irq_flag(struct i2c_client *client)
448 {
449   u8 data[1];
450
451   if (rt5025_read_reg(client, RT5025_REG_IRQ_FLAG, data, 1) < 0){
452     printk(KERN_ERR "%s: Failed to read irq_flag\n", __func__);
453   }
454                 
455   chip->irq_flag = data[0];
456   RTINFO("[RT5025] IRQ_FLG 0x%x\n", chip->irq_flag);
457 }
458
459 static void rt5025_get_timer(struct i2c_client *client)
460 {
461   u8 data[2];
462         u16 gauge_timer;
463         
464   if (rt5025_read_reg(client, RT5025_REG_TIMER, data, 2) < 0){
465     printk(KERN_ERR "%s: Failed to read Timer\n", __func__);
466   }
467                 
468   gauge_timer = (data[0] << 8) + data[1];
469   if (gauge_timer > chip->pre_gauge_timer)
470                 chip->time_interval = gauge_timer - chip->pre_gauge_timer;
471         else    
472                 chip->time_interval = 65536 - chip->pre_gauge_timer + gauge_timer;
473                 
474   chip->pre_gauge_timer = gauge_timer;
475   RTINFO("[RT5025] timer %d , interval %d\n", gauge_timer,chip->time_interval);
476 }
477
478 static void rt5025_get_soc(struct i2c_client *client)
479 {
480   chip->soc = 50;
481 }
482
483 static void rt5025_channel_cc(bool enable)
484 {
485   u8 data[1];
486         
487   if (rt5025_read_reg(chip->client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
488     printk(KERN_ERR "%s: failed to read channel\n", __func__);
489   }
490
491   if (enable){
492     data[0] = data[0] | 0x80;
493   }else { 
494     data[0] = data[0] & 0x7F;
495   }
496     
497   if (rt5025_write_reg(chip->client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
498     printk(KERN_ERR "%s: failed to write channel\n", __func__);
499   }
500 }
501
502 static void rt5025_register_init(struct i2c_client *client)
503 {  
504   u8 data[1];
505         
506   /* enable the channel of current,qc,ain,vbat and vadc */
507   if (rt5025_read_reg(client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
508     printk("%s: failed to read channel\n", __func__);
509   }
510   data[0] = data[0] |
511                                                 CHANNEL_L_BIT_CADC_EN |
512                                                 CHANNEL_L_BIT_AINCH |
513                                                 CHANNEL_L_BIT_VBATSCH |
514                                                 CHANNEL_L_BIT_VADC_EN;
515   if (rt5025_write_reg(client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
516     printk("%s: failed to write channel\n", __func__);
517   }
518         /* set the alert threshold value */
519         irq_thres[MAXTEMP]  = TALRTMAX_VALUE;
520         irq_thres[MINTEMP]  = TALRTMIN_VALUE;
521         irq_thres[MAXVOLT]  = VALRTMAX_VALUE;
522         irq_thres[MINVOLT1] = VALRTMIN1_VALUE;
523         irq_thres[MINVOLT2] = VALRTMIN2_VALUE;
524         irq_thres[TEMP_RLS] = TRLS_VALUE;
525         irq_thres[VOLT_RLS] = VRLS_VALUE;
526
527         chip->chg_cc_unuse = 0;
528         chip->dchg_cc_unuse = 0;
529         chip->pre_gauge_timer = 0;
530         chip->online = 1;
531         chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
532         chip->health = POWER_SUPPLY_HEALTH_GOOD;
533   RTINFO("[RT5025] register initialized\n");
534 }
535
536 static void rt5025_alert_setting(alert_type type, bool enable)
537 {
538         u8 data[1];
539         
540   if (rt5025_read_reg(chip->client, RT5025_REG_IRQ_CTL, data, 1) < 0){
541     printk(KERN_ERR "%s: Failed to read CONFIG\n", __func__);
542   }
543
544   if(enable){
545                 switch(type){
546                         case MAXTEMP:
547                                 data[0] |= IRQ_CTL_BIT_TMX; //Enable max temperature alert
548                                 chip->max_temp_irq = true;
549                                 RTINFO("Enable min temperature alert");
550                                 break;
551                         case MINTEMP:
552                                 data[0] |= IRQ_CTL_BIT_TMN; //Enable min temperature alert
553                                 chip->min_temp_irq = true;  
554                                 RTINFO("Enable max temperature alert");
555                                 break;
556                         case MAXVOLT:
557                                 data[0] |= IRQ_CTL_BIT_VMX; //Enable max voltage alert
558                                 chip->max_volt_irq = true;
559                                 RTINFO("Enable max voltage alert");
560                                 break;
561                         case MINVOLT1:
562                                 data[0] |= IRQ_CTL_BIT_VMN1; //Enable min1 voltage alert        
563                                 chip->min_volt1_irq = true;
564                                 RTINFO("Enable min1 voltage alert");
565                                 break;
566                         case MINVOLT2:
567                                 data[0] |= IRQ_CTL_BIT_VMN2; //Enable min2 voltage alert
568                                 chip->min_volt2_irq = true;
569                                 RTINFO("Enable min2 voltage alert");
570                                 break;
571                         default:
572                                 break;
573                 }
574         }else{
575                 switch(type){
576                         case MAXTEMP:
577                                 data[0] = data[0] &~ IRQ_CTL_BIT_TMX; //Disable max temperature alert
578                                 chip->max_temp_irq = false;
579                                 RTINFO("Disable min temperature alert");
580                                 break;
581                         case MINTEMP:
582                                 data[0] = data[0] &~ IRQ_CTL_BIT_TMN; //Disable min temperature alert
583                                 chip->min_temp_irq = false;
584                                 RTINFO("Disable max temperature alert");
585                                 break;
586                         case MAXVOLT:
587                                 data[0] = data[0] &~ IRQ_CTL_BIT_VMX; //Disable max voltage alert
588                                 chip->max_volt_irq = false;
589                                 RTINFO("Disable max voltage alert");
590                                 break;
591                         case MINVOLT1:
592                                 data[0] = data[0] &~ IRQ_CTL_BIT_VMN1; //Disable min1 voltage alert     
593                                 chip->min_volt1_irq = false;
594                                 RTINFO("Disable min1 voltage alert");
595                                 break;
596                         case MINVOLT2:
597                                 data[0] = data[0] &~ IRQ_CTL_BIT_VMN2; //Disable min2 voltage alert
598                                 chip->min_volt2_irq = false;
599                                 RTINFO("Disable min2 voltage alert");
600                                 break;
601                         default:
602                                 break;
603                 }
604         }
605   if (rt5025_write_reg(chip->client, RT5025_REG_IRQ_CTL, data, 1) < 0)
606                 printk(KERN_ERR "%s: failed to write IRQ control\n", __func__);
607 }       
608 static void rt5025_alert_threshold_init(struct i2c_client *client)
609 {
610   u8 data[1];
611
612   /* TALRT MAX threshold setting */
613   data[0] = irq_thres[MAXTEMP];
614   if (rt5025_write_reg(client, RT5025_REG_TALRT_MAXTH, data, 1) < 0)
615                 printk(KERN_ERR "%s: failed to write TALRT MAX threshold\n", __func__); 
616   /* TALRT MIN threshold setting */
617   data[0] = irq_thres[MINTEMP];
618   if (rt5025_write_reg(client, RT5025_REG_TALRT_MINTH, data, 1) < 0)
619                 printk(KERN_ERR "%s: failed to write TALRT MIN threshold\n", __func__); 
620   /* VALRT MAX threshold setting */
621   data[0] = irq_thres[MAXVOLT];
622   if (rt5025_write_reg(client, RT5025_REG_VALRT_MAXTH, data, 1) < 0)
623                 printk(KERN_ERR "%s: failed to write VALRT MAX threshold\n", __func__); 
624   /* VALRT MIN1 threshold setting */
625   data[0] = irq_thres[MINVOLT1];
626   if (rt5025_write_reg(client, RT5025_REG_VALRT_MIN1TH, data, 1) < 0)
627                 printk(KERN_ERR "%s: failed to write VALRT MIN1 threshold\n", __func__);        
628   /* VALRT MIN2 threshold setting */
629   data[0] = irq_thres[MINVOLT2];
630   if (rt5025_write_reg(client, RT5025_REG_VALRT_MIN2TH, data, 1) < 0)
631                 printk(KERN_ERR "%s: failed to write VALRT MIN2 threshold\n", __func__);        
632 }
633
634 static void rt5025_alert_init(struct i2c_client *client)
635 {
636   /* Set RT5025 gauge alert configuration */
637   rt5025_alert_threshold_init(client);
638         /* Enable gauge alert function */
639         rt5025_alert_setting(MAXTEMP,true);
640         rt5025_alert_setting(MINTEMP,true);
641         rt5025_alert_setting(MAXVOLT,true);
642         rt5025_alert_setting(MINVOLT1,true);
643         rt5025_alert_setting(MINVOLT2,true);    
644 }
645
646 void rt5025_irq_handler(void)
647 {
648   rt5025_get_irq_flag(chip->client);
649
650   if ((chip->irq_flag) & IRQ_FLG_BIT_TMX){      
651                 printk(KERN_INFO "[RT5025]: Min temperature IRQ received\n");
652                 rt5025_alert_setting(MAXTEMP,false);
653                 chip->max_temp_irq = false;
654         }
655   if ((chip->irq_flag) & IRQ_FLG_BIT_TMN){
656                 printk(KERN_INFO "[RT5025]: Max temperature IRQ received\n");
657                 rt5025_alert_setting(MINTEMP,false);
658                 chip->min_temp_irq = false; 
659         }
660   if ((chip->irq_flag) & IRQ_FLG_BIT_VMX){
661                 printk(KERN_INFO "[RT5025]: Max voltage IRQ received\n");
662                 rt5025_alert_setting(MAXVOLT,false);
663                 chip->max_volt_irq = false;
664         }
665   if ((chip->irq_flag) & IRQ_FLG_BIT_VMN1){
666                 printk(KERN_INFO "[RT5025]: Min voltage1 IRQ received\n");
667                 rt5025_alert_setting(MINVOLT1,false);
668                 chip->min_volt1_irq = false;
669         }
670   if ((chip->irq_flag) & IRQ_FLG_BIT_VMN2){
671                 printk(KERN_INFO "[RT5025]: Min voltage2 IRQ received\n");
672                 rt5025_alert_setting(MINVOLT2,false);
673                 chip->min_volt2_irq = false;
674         }
675         
676         wake_lock(&chip->monitor_wake_lock);
677         schedule_delayed_work(&chip->monitor_work, 0);
678 }
679
680 static void rt5025_update(struct i2c_client *client)
681 {
682   /* Update voltage */
683   rt5025_get_vcell(client);
684   /* Update current */
685   rt5025_get_current(client);
686   /* Update external temperature */
687   rt5025_get_external_temp(client);
688   /* Read timer */
689   rt5025_get_timer(client);
690   /* Update chg cc */
691   rt5025_get_chg_cc(client);
692   /* Update dchg cc */
693   rt5025_get_dchg_cc(client);
694   /* Update SOC */
695   rt5025_get_soc(client);
696
697   if ((chip->max_temp_irq == false) &&
698                  (((irq_thres[MAXTEMP] * IRQ_THRES_UNIT) / 100 - chip->ain_volt) > irq_thres[TEMP_RLS])){
699                 rt5025_alert_setting(MAXTEMP,true);
700         }else if ((chip->min_temp_irq == false) &&
701                                           ((chip->ain_volt - (irq_thres[MINTEMP] * IRQ_THRES_UNIT) / 100) > irq_thres[TEMP_RLS])){
702                 rt5025_alert_setting(MINTEMP,true);
703         }else if ((chip->max_volt_irq == false) &&
704                                         ((((irq_thres[MAXVOLT] * IRQ_THRES_UNIT) / 100) - chip->vcell) > irq_thres[VOLT_RLS])){
705                 rt5025_alert_setting(MAXVOLT,true);
706         }else if ((chip->min_volt1_irq == false) &&
707                                         ((chip->vcell - ((irq_thres[MINVOLT1] * IRQ_THRES_UNIT) / 100)) > irq_thres[VOLT_RLS])){
708                 rt5025_alert_setting(MINVOLT1,true);                            
709         }else if ((chip->min_volt2_irq == false) &&
710                                         ((chip->vcell - ((irq_thres[MINVOLT2] * IRQ_THRES_UNIT) / 100)) > irq_thres[VOLT_RLS])){
711                 rt5025_alert_setting(MINVOLT2,true);                                            
712         }
713 }
714
715 static void rt5025_update_work(struct work_struct *work)
716 {
717         unsigned long flags;
718         
719   rt5025_update(chip->client);
720
721   /* Update data to framework */
722   power_supply_changed(&chip->battery);
723   
724         /* prevent suspend before starting the alarm */
725         local_irq_save(flags);
726         chip->last_poll = alarm_get_elapsed_realtime();
727         rt5025_program_alarm(NORMAL_POLL);
728         local_irq_restore(flags);
729
730         wake_unlock(&chip->monitor_wake_lock);
731 }
732
733 static enum power_supply_property rt5025_battery_props[] = {
734   POWER_SUPPLY_PROP_STATUS,
735   POWER_SUPPLY_PROP_HEALTH,
736   POWER_SUPPLY_PROP_PRESENT,
737   POWER_SUPPLY_PROP_TEMP,
738   POWER_SUPPLY_PROP_ONLINE,
739   POWER_SUPPLY_PROP_VOLTAGE_NOW,
740   POWER_SUPPLY_PROP_CURRENT_NOW,
741   POWER_SUPPLY_PROP_CAPACITY,
742   POWER_SUPPLY_PROP_TECHNOLOGY,
743 };
744
745 void rt5025_gauge_suspend(void)
746 {
747         rt5025_channel_cc(false);
748         cancel_delayed_work(&chip->monitor_work);
749
750   RTINFO("\n");
751 }
752
753 void rt5025_gauge_resume(void)
754 {
755         rt5025_channel_cc(true);
756         wake_lock(&chip->monitor_wake_lock);
757         schedule_delayed_work(&chip->monitor_work, 0);
758   RTINFO("\n");
759 }
760
761 void rt5025_gauge_remove(void)
762 {
763         chip->info->event_callback = NULL;
764         power_supply_unregister(&chip->battery);
765         cancel_delayed_work(&chip->monitor_work);
766         wake_lock_destroy(&chip->monitor_wake_lock);
767         kfree(chip);
768 }
769
770 int rt5025_gauge_init(struct rt5025_power_info *info)
771 {
772         int ret;
773   chip = kzalloc(sizeof(*chip), GFP_KERNEL);
774   if (!chip)
775     return -ENOMEM;
776
777   chip->client = info->i2c;
778   chip->info = info;  
779   chip->battery.name = "rt5025-battery";
780   chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
781   chip->battery.get_property = rt5025_get_property;
782   chip->battery.properties = rt5025_battery_props;
783   chip->battery.num_properties = ARRAY_SIZE(rt5025_battery_props);
784
785   ret = power_supply_register(info->dev, &chip->battery);
786   if (ret) {
787     printk(KERN_ERR "[RT5025] power supply register failed\n");
788                 goto err_wake_lock;
789   }
790
791   chip->last_poll = alarm_get_elapsed_realtime();
792         alarm_init(&chip->wakeup_alarm, ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP,
793                 rt5025_gauge_alarm);
794
795         INIT_DELAYED_WORK(&chip->monitor_work, rt5025_update_work);
796         
797         wake_lock_init(&chip->monitor_wake_lock, WAKE_LOCK_SUSPEND,
798                         "rt-battery-monitor");
799   /* enable channel */
800   rt5025_register_init(info->i2c);
801
802         /* enable gauge IRQ */
803   rt5025_alert_init(info->i2c);
804
805         /* register callback functions */
806         chip->cb.rt5025_gauge_irq_handler = rt5025_irq_handler;
807         chip->cb.rt5025_gauge_set_status = rt5025_set_status;
808         chip->cb.rt5025_gauge_suspend = rt5025_gauge_suspend;
809         chip->cb.rt5025_gauge_resume = rt5025_gauge_resume;
810         chip->cb.rt5025_gauge_remove = rt5025_gauge_remove;
811         info->event_callback=&chip->cb;
812
813         //rt_register_gauge_callbacks(info->i2c, &chip->cb);
814         
815         wake_lock(&chip->monitor_wake_lock);
816         schedule_delayed_work(&chip->monitor_work, msecs_to_jiffies(1000));
817
818   return 0;
819
820 err_wake_lock:
821         wake_lock_destroy(&chip->monitor_wake_lock);
822         kfree(chip);
823
824         return ret;
825 }