12d59c6f74b5390639ce735e9f2bd192356e6d12
[firefly-linux-kernel-4.4.55.git] / drivers / power / rt5025-battery.c
1 /* drivers/power/rt5025-battery.c
2  * I2C Driver for Richtek RT5025 PMIC
3  * Multi function device - multi functional baseband PMIC Battery part
4  *
5  * Copyright (C) 2013
6  * Author: Nick Hung <nick_hung@richtek.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/i2c.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/slab.h>
23 #include <linux/wakelock.h>
24 #include <linux/workqueue.h>
25 #include <linux/jiffies.h>
26 #include <linux/timer.h>
27 #include <linux/android_alarm.h>
28 #include <linux/mfd/rt5025.h>
29 #include <linux/power/rt5025-battery.h>
30
31 #define VOLTAGE_ALERT 1
32 #define TEMPERATURE_ALERT 0
33
34 #define RT5025_CSV 0
35 #define RT5025_B 1
36 #define RT5025_TEST_WAKE_LOCK 1
37
38 u8 irq_thres[LAST_TYPE];
39
40 void rt5025_gauge_set_status(struct rt5025_battery_info *bi, int status)
41 {
42   bi->status = status;
43   if (status == POWER_SUPPLY_STATUS_FULL)
44         bi->tp_flag = true;
45 }
46 EXPORT_SYMBOL(rt5025_gauge_set_status);
47
48 void rt5025_gauge_set_online(struct rt5025_battery_info *bi, bool present)
49 {
50   bi->online = present;
51 }
52 EXPORT_SYMBOL(rt5025_gauge_set_online);
53
54 static int rt5025_read_reg(struct i2c_client *client,
55                                 u8 reg, u8 *data, u8 len)
56 {
57         #if 1
58         return rt5025_reg_block_read(client, reg, len, data);
59         #else
60         struct i2c_adapter *adap = client->adapter;
61         struct i2c_msg msgs[2];
62         int ret;
63         
64         msgs[0].addr = client->addr;
65         msgs[0].flags = client->flags;
66         msgs[0].len = 1;
67         msgs[0].buf = &reg;
68
69         msgs[1].addr = client->addr;
70         msgs[1].flags = client->flags | I2C_M_RD;
71         msgs[1].len = len;
72         msgs[1].buf = data;
73         
74         ret = i2c_transfer(adap, msgs, 2);
75          
76         return (ret == 2)? len : ret;
77         #endif
78 }
79
80 static int rt5025_write_reg(struct i2c_client *client,
81                                 u8 reg, u8 *data, u8 len)
82 {
83         #if 1
84         return rt5025_reg_block_write(client, reg, len, data);
85         #else
86         struct i2c_adapter *adap = client->adapter;
87         struct i2c_msg msg;
88         int ret;
89         char *tx_buf = (char *)kmalloc(len + 1, GFP_KERNEL);
90         
91         if(!tx_buf)
92                 return -ENOMEM;
93         tx_buf[0] = reg;
94         memcpy(tx_buf+1, data, len);
95         
96         msg.addr = client->addr;
97         msg.flags = client->flags;
98         msg.len = len + 1;
99         msg.buf = (char *)tx_buf;
100
101         ret = i2c_transfer(adap, &msg, 1);
102         kfree(tx_buf);
103         return (ret == 1) ? len : ret;
104         #endif
105 }
106
107 static void rt5025_gauge_alarm(struct alarm *alarm)
108 {
109         struct rt5025_battery_info *bi = (struct rt5025_battery_info *)container_of(alarm, struct rt5025_battery_info, wakeup_alarm);
110
111         wake_lock(&bi->monitor_wake_lock);
112         schedule_delayed_work(&bi->monitor_work, 0);
113 }
114
115 static void rt5025_program_alarm(struct rt5025_battery_info *bi)
116 {
117         ktime_t low_interval = ktime_set(bi->update_time, 0);
118         ktime_t slack = ktime_set(20, 0);
119         ktime_t next;
120
121         next = ktime_add(bi->last_poll, low_interval);
122         alarm_start_range(&bi->wakeup_alarm, next, ktime_add(next, slack));
123 }
124
125 #if 0
126 static void rt5025_run_time(struct rt5025_battery_info *bi)
127 {
128         if(bi->curr <= 0)
129         {
130                 bi->time_to_empty = bi->rm / (bi->curr*(-1));
131         }
132         else
133         {
134                 bi->time_to_full = (bi->fcc * 3600 - bi->rm) / bi->curr;
135         }
136         RTINFO("RTTF = %d\n",bi->time_to_full);
137         RTINFO("RTTE = %d\n",bi->time_to_empty);
138 }
139 #endif
140
141 static int rt5025_get_property(struct power_supply *psy,
142                             enum power_supply_property psp,
143                             union power_supply_propval *val)
144 {
145   struct rt5025_battery_info *bi = dev_get_drvdata(psy->dev->parent);
146
147   switch (psp) {
148     case POWER_SUPPLY_PROP_STATUS:
149       val->intval = bi->status;
150       //val->intval = POWER_SUPPLY_STATUS_CHARGING;
151       break;
152     case POWER_SUPPLY_PROP_HEALTH:
153       val->intval = bi->health;
154       break;
155     case POWER_SUPPLY_PROP_PRESENT:
156       val->intval = bi->present;
157       break;
158     case POWER_SUPPLY_PROP_TEMP:
159       val->intval = bi->ext_temp;
160       break;
161     case POWER_SUPPLY_PROP_ONLINE:
162       val->intval = bi->online;
163       break;
164     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
165       val->intval = bi->vcell * 1000; //uV
166       break;
167     case POWER_SUPPLY_PROP_CURRENT_NOW:
168       val->intval = bi->curr * 1000; //uA
169       break;
170     case POWER_SUPPLY_PROP_CAPACITY:
171       val->intval = bi->soc;
172                         //val->intval = 50;
173       if (val->intval > 100)
174                                 val->intval = 100;
175       break;
176     case POWER_SUPPLY_PROP_TECHNOLOGY:
177       val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
178       break;
179     #if 0
180     case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
181                         val->intval = bi->time_to_empty;
182     break;
183     case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
184                         val->intval = bi->time_to_full;
185     #endif
186     break;
187     default:
188       return -EINVAL;
189   }
190   return 0;
191 }
192
193 static void rt5025_get_vcell(struct rt5025_battery_info *bi)
194 {
195   u8 data[2];
196         
197   if (rt5025_read_reg(bi->client, RT5025_REG_VCELL_MSB, data, 2) < 0){
198     printk(KERN_ERR "%s: Failed to read Voltage\n", __func__);
199   }
200
201   if (bi->avg_flag)
202     bi->vcell = ((data[0] << 8) + data[1]) * 61 / 100;
203   else 
204           bi->vcell = (bi->vcell + ((data[0] << 8) + data[1]) * 61 / 100) / 2;
205 #if RT5025_B
206         bi->curr_offset = (15444 * bi->vcell - 27444000) / 10000;
207 #else 
208   if (37 * bi->vcell > 92000)
209                 bi->curr_offset = (37 * bi->vcell - 92000) / 1000;
210         else
211                 bi->curr_offset = 0;
212 #endif
213                 
214 #if RT5025_CSV
215  // if (!bi->avg_flag)
216   //  pr_info("%d,%d,", bi->vcell, bi->curr_offset);
217 #else  
218   if (bi->avg_flag)
219                 RTINFO("vcell_pre: %d, offset: %d\n", bi->vcell, bi->curr_offset);
220   else
221                 RTINFO("vcell_avg: %d, offset: %d\n", bi->vcell, bi->curr_offset);
222 #endif
223 }
224
225 static void rt5025_get_current(struct rt5025_battery_info *bi)
226 {
227   u8 data[2];
228   s32 temp;
229   int sign = 0;
230
231   if (rt5025_read_reg(bi->client, RT5025_REG_CURRENT_MSB, data, 2) < 0) {
232     printk(KERN_ERR "%s: Failed to read CURRENT\n", __func__);
233   }
234 #if RT5025_B
235   temp = (data[0]<<8) | data[1];
236   bi->curr_raw = ((temp & 0x7FFF) * 3125) / 10000;
237     
238   if (data[0] & (1 << 7)) {
239     sign = 1;
240     temp = (((temp & 0x7FFF) * 3125) / 10 + bi->curr_offset) / 1000;
241   }else{
242                 if ((temp * 3125) / 10 > bi->curr_offset)
243                         temp = ((temp * 3125) / 10 - bi->curr_offset) / 1000;
244         }
245         
246   if (temp < DEADBAND)
247                 temp = 0;
248   
249   if (sign){
250     temp *= -1;
251     bi->curr_raw *= -1;
252         }
253 #else
254   temp = (data[0]<<8) | data[1];
255   if (data[0] & (1 << 7)) {
256     sign = 1;
257     temp = temp & 0x7FFF;
258     if(temp > bi->curr_offset)
259                         temp = temp - bi->curr_offset;
260   }else {
261     temp = temp + bi->curr_offset;
262         }
263
264   temp = (temp * 37375) / 100000; //Unit: 0.3125mA
265   if (temp < DEADBAND)
266                 temp = 0;
267   
268   if (sign)
269     temp *= -1;
270 #endif
271
272   if (bi->avg_flag) 
273     bi->curr = temp;
274   else
275           bi->curr = (bi->curr + temp) / 2;
276
277   if(bi->curr > 0)
278     bi->internal_status = POWER_SUPPLY_STATUS_CHARGING;
279   else
280     bi->internal_status = POWER_SUPPLY_STATUS_DISCHARGING;
281   RTINFO("current=%d, internal_status=%d\n", bi->curr, bi->internal_status);
282
283 #if RT5025_CSV
284  // if (!bi->avg_flag)
285   //  pr_info("%d,",bi->curr);
286 #else
287   if (bi->avg_flag)
288                 RTINFO("current_pre: %d\n", bi->curr);
289   else  
290                 RTINFO("current_avg: %d\n", bi->curr);
291 #endif
292 }
293
294 static void rt5025_get_external_temp(struct rt5025_battery_info *bi)
295 {
296   u8 data[2];
297   s32 temp;
298
299   if (rt5025_read_reg(bi->client, RT5025_REG_EXT_TEMPERATUE_MSB, data, 2) < 0) {
300     printk(KERN_ERR "%s: Failed to read TEMPERATURE\n", __func__);
301   }
302   bi->ain_volt = (data[0] * 256 + data[1]) * 61 / 100;
303   if(bi->ain_volt < 1150) 
304   {
305         bi->present = 1;
306   }
307   else
308   {
309         bi->present = 0;  
310   }
311   
312   temp =  (bi->ain_volt * (-91738) + 81521000) / 100000;
313   bi->ext_temp = (int)temp;
314         //test
315         //bi->ext_temp = 250;
316         
317         if (bi->ext_temp >= HIGH_TEMP_THRES) {
318                 if (bi->health != POWER_SUPPLY_HEALTH_OVERHEAT)
319                         bi->temp_high_cnt++;
320         } else if (bi->ext_temp <= HIGH_TEMP_RECOVER && bi->ext_temp >= LOW_TEMP_RECOVER) {
321                 if (bi->health == POWER_SUPPLY_HEALTH_OVERHEAT ||
322                     bi->health == POWER_SUPPLY_HEALTH_COLD)
323                         bi->temp_recover_cnt++;
324         } else if (bi->ext_temp <= LOW_TEMP_THRES) {
325                 if (bi->health != POWER_SUPPLY_HEALTH_COLD)
326                         bi->temp_low_cnt++;
327         }else {
328                 bi->temp_high_cnt = 0;
329                 bi->temp_low_cnt = 0;
330                 bi->temp_recover_cnt = 0;
331         }
332         
333         if (bi->temp_high_cnt >= TEMP_ABNORMAL_COUNT) {
334          bi->health = POWER_SUPPLY_HEALTH_OVERHEAT;
335          bi->temp_high_cnt = 0;
336         } else if (bi->temp_low_cnt >= TEMP_ABNORMAL_COUNT) {
337          bi->health = POWER_SUPPLY_HEALTH_COLD;
338          bi->temp_low_cnt = 0;
339         } else if (bi->temp_recover_cnt >= TEMP_ABNORMAL_COUNT) {
340          bi->health = POWER_SUPPLY_HEALTH_GOOD;
341          bi->temp_recover_cnt = 0;
342         }
343         //RTINFO("external temperature: %d\n", bi->ext_temp);
344 }
345
346 static void rt5025_clear_cc(struct rt5025_battery_info *bi, operation_mode mode)
347 {  
348   u8 data[2];
349         
350   if (rt5025_read_reg(bi->client, RT5025_REG_CHANNEL_MSB, data, 2) < 0){
351     pr_err("%s: failed to read channel\n", __func__);
352   }
353
354   if (mode == CHG)
355                 data[0] = data[0] | CHANNEL_H_BIT_CLRQCHG;
356         else
357                 data[0] = data[0] | CHANNEL_H_BIT_CLRQDCHG;
358                 
359   if (rt5025_write_reg(bi->client, RT5025_REG_CHANNEL_MSB, data, 2) < 0){
360     pr_err("%s: failed to write channel\n", __func__);
361   }
362 }
363
364 static void rt5025_get_chg_cc(struct rt5025_battery_info *bi)
365 {
366   u8 data[4];
367   u32 qh_old,ql_old,qh_new,ql_new;
368   u32 cc_masec,offset;
369   
370   if (rt5025_read_reg(bi->client, RT5025_REG_QCHGH_MSB, data, 4) < 0){
371     pr_err("%s: Failed to read QCHG\n", __func__);
372   }
373   qh_old = (data[0]<<8) + data[1];
374   ql_old = (data[2]<<8) + data[3];
375   
376   if (rt5025_read_reg(bi->client, RT5025_REG_QCHGH_MSB, data, 4) < 0){
377     pr_err("%s: Failed to read QCHG\n", __func__);
378   }
379   qh_new = (data[0]<<8) + data[1];
380   ql_new = (data[2]<<8) + data[3];
381
382 #if RT5025_B
383   if (qh_new > qh_old){
384      cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
385   }else if (qh_new == qh_old){
386     if (ql_new >= ql_old){
387       cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
388     }else {  
389       cc_masec = (((qh_old<<16) + ql_old) * 50134) / 10;
390                 }
391   }     
392   
393         offset = bi->curr_offset * bi->time_interval;
394                           
395   if (cc_masec > offset){
396                 cc_masec = (cc_masec - offset) / 1000;
397         }
398 #else           
399   if (qh_new > qh_old){
400      cc_masec = (((qh_new<<16) + ql_new) * 5996) / 1000;
401   }else if (qh_new == qh_old){
402     if (ql_new >= ql_old){
403       cc_masec = (((qh_new<<16) + ql_new) * 5996) / 1000;
404     }else {  
405       cc_masec = (((qh_old<<16) + ql_old) * 5996) / 1000;
406                 }
407   }     
408   
409         offset = (bi->curr_offset * bi->time_interval * 37375) / 100000;
410                           
411   if (cc_masec != 0){
412                 cc_masec = cc_masec - offset;
413         }
414 #endif
415         if (cc_masec < (DEADBAND * bi->time_interval))
416                 cc_masec = 0;
417
418 #if RT5025_CSV
419   //pr_info("%d,\n", cc_masec);
420 #else
421         RTINFO("chg_cc_mAsec: %d\n", cc_masec);
422 #endif
423
424         bi->chg_cc = cc_masec;
425         //bi->chg_cc = (cc_masec + bi->chg_cc_unuse) / 3600;
426   //bi->chg_cc_unuse = (cc_masec + bi->chg_cc_unuse) % 3600;
427   rt5025_clear_cc(bi, CHG);
428 }
429
430 static void rt5025_get_dchg_cc(struct rt5025_battery_info *bi)
431 {
432   u8 data[4];
433   u32 qh_old,ql_old,qh_new,ql_new;
434   u32 cc_masec,offset;
435   
436   if (rt5025_read_reg(bi->client, RT5025_REG_QDCHGH_MSB, data, 4) < 0){
437     printk(KERN_ERR "%s: Failed to read QDCHG\n", __func__);
438   }
439   qh_old = (data[0]<<8) + data[1];
440   ql_old = (data[2]<<8) + data[3];
441   
442   if (rt5025_read_reg(bi->client, RT5025_REG_QDCHGH_MSB, data, 4) < 0){
443     printk(KERN_ERR "%s: Failed to read QDCHG\n", __func__);
444   }
445   qh_new = (data[0]<<8) + data[1];
446   ql_new = (data[2]<<8) + data[3];
447   
448 #if RT5025_B
449   if (qh_new > qh_old){
450      cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
451   }else if (qh_new == qh_old){
452     if (ql_new >= ql_old){
453       cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
454     }else {  
455       cc_masec = (((qh_old<<16) + ql_old) * 50134) / 10;
456                 }
457   }     
458   
459         offset = bi->curr_offset * bi->time_interval;
460                           
461   if (cc_masec != 0){
462                 cc_masec = (cc_masec + offset) / 1000;
463         }
464 #else  
465   if (qh_new > qh_old){
466      cc_masec = (((qh_new<<16) + ql_new) * 5996) / 1000;
467   }else if (qh_new == qh_old){
468     if (ql_new >= ql_old){
469       cc_masec = (((qh_new<<16) + ql_new) * 5996) / 1000;
470     }else {  
471       cc_masec = (((qh_old<<16) + ql_old) * 5996) / 1000;
472                 }
473   }
474   
475         offset = (bi->curr_offset * bi->time_interval * 37375) / 100000;
476                           
477   if (cc_masec > offset){
478                 cc_masec = cc_masec - offset;
479         }
480 #endif
481         if (cc_masec < (DEADBAND * bi->time_interval))
482                 cc_masec = 0;
483                 
484 #if RT5025_CSV
485   //pr_info("%d,", cc_masec);
486 #else
487         RTINFO("dchg_cc_mAsec: %d\n", cc_masec);
488 #endif
489         bi->dchg_cc = cc_masec;
490         //bi->dchg_cc = (cc_masec + bi->dchg_cc_unuse) / 3600;
491   //bi->dchg_cc_unuse = (cc_masec + bi->dchg_cc_unuse) % 3600;
492         rt5025_clear_cc(bi, DCHG);
493         
494 }
495
496 static void rt5025_cycle_count(struct rt5025_battery_info *bi)
497 {
498         bi->acc_dchg_cap +=  bi->dchg_cc;
499         if(bi->acc_dchg_cap >= (bi->dc * 3600)){
500                 bi->cycle_cnt++;
501                 bi->acc_dchg_cap -= (bi->dc * 3600);
502         }
503 }
504
505 static void rt5025_get_irq_flag(struct rt5025_battery_info *bi, u8 flag)
506 {
507 #if 0
508   u8 data[1];
509
510   if (rt5025_read_reg(bi->client, RT5025_REG_IRQ_FLAG, data, 1) < 0){
511     pr_err("%s: Failed to read irq_flag\n", __func__);
512   }
513 #endif
514                 
515   bi->irq_flag = flag;
516         //RTINFO("IRQ_FLG 0x%x\n", bi->irq_flag);
517 }
518
519 static void rt5025_get_timer(struct rt5025_battery_info *bi)
520 {
521   u8 data[2];
522   //frankie
523         //u16 gauge_timer;
524         
525   if (rt5025_read_reg(bi->client, RT5025_REG_TIMER, data, 2) < 0){
526         pr_err("%s: Failed to read Timer\n", __func__);
527   }
528                 
529   bi->gauge_timer = (data[0] << 8) + data[1];
530   if (!bi->device_suspend){
531     if (bi->gauge_timer > bi->pre_gauge_timer)
532                   bi->time_interval = bi->gauge_timer - bi->pre_gauge_timer;
533           else  
534                   bi->time_interval = 65536 - bi->pre_gauge_timer + bi->gauge_timer;
535   }
536     
537   bi->pre_gauge_timer = bi->gauge_timer;
538 #if RT5025_CSV
539  // pr_info("%d,%d,", bi->gauge_timer,bi->time_interval);
540 #else
541         RTINFO("timer %d , interval %d\n", bi->gauge_timer,bi->time_interval);
542 #endif
543 }
544
545 static void rt5025_alert_setting(struct rt5025_battery_info *bi, alert_type type, bool enable)
546 {
547         u8 data[1];
548         
549         if (rt5025_read_reg(bi->client, RT5025_REG_IRQ_CTL, data, 1) < 0){
550                 printk(KERN_ERR "%s: Failed to read CONFIG\n", __func__);
551         }
552
553         if(enable){
554                 switch(type){
555                         case MAXTEMP:
556                                 data[0] |= IRQ_CTL_BIT_TMX; //Enable max temperature alert
557                                 bi->max_temp_irq = true;
558                                 //RTDBG("Enable min temperature alert");
559                                 break;
560                         case MINTEMP:
561                                 data[0] |= IRQ_CTL_BIT_TMN; //Enable min temperature alert
562                                 bi->min_temp_irq = true;  
563                                 //RTDBG("Enable max temperature alert");
564                                 break;
565                         case MAXVOLT:
566                                 data[0] |= IRQ_CTL_BIT_VMX; //Enable max voltage alert
567                                 bi->max_volt_irq = true;
568                                 //RTDBG("Enable max voltage alert");
569                                 break;
570                         case MINVOLT1:
571                                 data[0] |= IRQ_CTL_BIT_VMN1; //Enable min1 voltage alert        
572                                 bi->min_volt1_irq = true;
573                                 //RTDBG("Enable min1 voltage alert");
574                                 break;
575                         case MINVOLT2:
576                                 data[0] |= IRQ_CTL_BIT_VMN2; //Enable min2 voltage alert
577                                 bi->min_volt2_irq = true;
578                                 //RTDBG("Enable min2 voltage alert");
579                                 break;
580                         default:
581                                 break;
582                 }
583         }else{
584                 switch(type){
585                         case MAXTEMP:
586                                 data[0] = data[0] &~ IRQ_CTL_BIT_TMX; //Disable max temperature alert
587                                 bi->max_temp_irq = false;
588                                 //RTDBG("Disable min temperature alert");
589                                 break;
590                         case MINTEMP:
591                                 data[0] = data[0] &~ IRQ_CTL_BIT_TMN; //Disable min temperature alert
592                                 bi->min_temp_irq = false;
593                                 //RTDBG("Disable max temperature alert");
594                                 break;
595                         case MAXVOLT:
596                                 data[0] = data[0] &~ IRQ_CTL_BIT_VMX; //Disable max voltage alert
597                                 bi->max_volt_irq = false;
598                                 //RTDBG("Disable max voltage alert");
599                                 break;
600                         case MINVOLT1:
601                                 data[0] = data[0] &~ IRQ_CTL_BIT_VMN1; //Disable min1 voltage alert     
602                                 bi->min_volt1_irq = false;
603                                 //RTDBG("Disable min1 voltage alert");
604                                 break;
605                         case MINVOLT2:
606                                 data[0] = data[0] &~ IRQ_CTL_BIT_VMN2; //Disable min2 voltage alert
607                                 bi->min_volt2_irq = false;
608                                 //RTDBG("Disable min2 voltage alert");
609                                 break;
610                         default:
611                                 break;
612                 }
613         }
614         if (rt5025_write_reg(bi->client, RT5025_REG_IRQ_CTL, data, 1) < 0)
615                 pr_err("%s: failed to write IRQ control\n", __func__);
616 }
617
618 static void rt5025_alert_threshold_init(struct i2c_client *client)
619 {
620         u8 data[1];
621
622         #if 0 //change the operating right to jeita driver
623         /* TALRT MAX threshold setting */
624         data[0] = irq_thres[MAXTEMP];
625         if (rt5025_write_reg(client, RT5025_REG_TALRT_MAXTH, data, 1) < 0)
626                 printk(KERN_ERR "%s: failed to write TALRT MAX threshold\n", __func__); 
627         /* TALRT MIN threshold setting */
628         data[0] = irq_thres[MINTEMP];
629         if (rt5025_write_reg(client, RT5025_REG_TALRT_MINTH, data, 1) < 0)
630                 printk(KERN_ERR "%s: failed to write TALRT MIN threshold\n", __func__); 
631         #endif
632         /* VALRT MAX threshold setting */
633         data[0] = irq_thres[MAXVOLT];
634         if (rt5025_write_reg(client, RT5025_REG_VALRT_MAXTH, data, 1) < 0)
635                 printk(KERN_ERR "%s: failed to write VALRT MAX threshold\n", __func__); 
636         /* VALRT MIN1 threshold setting */
637         data[0] = irq_thres[MINVOLT1];
638         if (rt5025_write_reg(client, RT5025_REG_VALRT_MIN1TH, data, 1) < 0)
639                 printk(KERN_ERR "%s: failed to write VALRT MIN1 threshold\n", __func__);        
640         /* VALRT MIN2 threshold setting */
641         data[0] = irq_thres[MINVOLT2];
642         if (rt5025_write_reg(client, RT5025_REG_VALRT_MIN2TH, data, 1) < 0)
643                 printk(KERN_ERR "%s: failed to write VALRT MIN2 threshold\n", __func__);        
644 }
645
646 static void rt5025_alert_init(struct rt5025_battery_info *bi)
647 {
648
649         /* Set RT5025 gauge alert configuration */
650         rt5025_alert_threshold_init(bi->client);
651         /* Enable gauge alert function */
652         rt5025_alert_setting(bi, MINVOLT2,VOLTAGE_ALERT);       
653 }
654
655 void rt5025_gauge_irq_handler(struct rt5025_battery_info *bi, u8 irq_flag)
656 {
657   rt5025_get_irq_flag(bi, irq_flag);
658
659   if ((bi->irq_flag) & IRQ_FLG_BIT_TMX){        
660                 //printk(KERN_INFO "[RT5025]: Min temperature IRQ received\n");
661                 rt5025_alert_setting(bi,MAXTEMP,false);
662                 bi->max_temp_irq = false;
663         }
664   if ((bi->irq_flag) & IRQ_FLG_BIT_TMN){
665                 //printk(KERN_INFO "[RT5025]: Max temperature IRQ received\n");
666                 rt5025_alert_setting(bi,MINTEMP,false);
667                 bi->min_temp_irq = false; 
668         }
669   if ((bi->irq_flag) & IRQ_FLG_BIT_VMX){
670                 //printk(KERN_INFO "[RT5025]: Max voltage IRQ received\n");
671                 rt5025_alert_setting(bi,MAXVOLT,false);
672                 bi->max_volt_irq = false;
673         }
674   if ((bi->irq_flag) & IRQ_FLG_BIT_VMN1){
675                 //printk(KERN_INFO "[RT5025]: Min voltage1 IRQ received\n");
676                 rt5025_alert_setting(bi,MINVOLT1,false);
677                 bi->min_volt1_irq = false;
678         }
679   if ((bi->irq_flag) & IRQ_FLG_BIT_VMN2){
680                 //printk(KERN_INFO "[RT5025]: Min voltage2 IRQ received\n");
681                 rt5025_alert_setting(bi,MINVOLT2,false);
682                 bi->min_volt2_irq = false;
683                 bi->min_volt2_alert = true;
684                 wake_lock_timeout(&bi->low_battery_wake_lock, msecs_to_jiffies(LOW_BAT_WAKE_LOK_TIME*MSEC_PER_SEC));
685         }
686         
687         wake_lock(&bi->monitor_wake_lock);
688         schedule_delayed_work(&bi->monitor_work, 0);
689 }
690 EXPORT_SYMBOL(rt5025_gauge_irq_handler);
691
692 static void rt5025_convert_masec_to_permille(struct rt5025_battery_info *bi)
693 {
694   bi->permille = bi->rm / 3600 * 1000 / bi->fcc;
695   RTINFO("permille=%d\n", bi->permille);
696   return;
697 }
698
699 static void rt5025_convert_permille_to_masec(struct rt5025_battery_info *bi)
700
701   bi->rm = bi->permille * bi->fcc / 1000 * 3600;
702   return;
703 }
704
705 static void rt5025_init_capacity(struct rt5025_battery_info *bi)
706 {
707   int i = 1;
708   int size;
709   int slope, const_term;
710   int delta_y, delta_x;
711
712   size = ARRAY_SIZE(rt5025_battery_param1);
713   while((bi->vcell < rt5025_battery_param1[i].x) &&
714                                 (i < (size - 1))){
715     i++;
716   }
717
718   delta_x = rt5025_battery_param1[i-1].x - rt5025_battery_param1[i].x;
719   delta_y = (rt5025_battery_param1[i-1].y - rt5025_battery_param1[i].y);
720
721   slope = delta_y  * 1000 / delta_x;
722
723   const_term = (rt5025_battery_param1[i].y) - ((rt5025_battery_param1[i].x * slope) / 1000);
724
725   if (bi->vcell >= rt5025_battery_param1[0].x)
726     bi->permille = rt5025_battery_param1[0].y; 
727   else if (bi->vcell <= rt5025_battery_param1[size-1].x)
728     bi->permille = rt5025_battery_param1[size-1].y;
729   else
730     bi->permille = (bi->vcell * slope) / 1000 + const_term;
731   rt5025_convert_permille_to_masec(bi);
732   bi->soc = bi->rm /36/bi->fcc_aging;
733         bi->init_cap = false;
734
735   //pr_err("[rt5025] i=%d, delta_x=%d, delta_y=%d, slope=%d, const_term=%d\n", i, delta_x, delta_y, slope, const_term);
736         RTINFO("voltage=%d, permille=%d, soc=%d, rm=%d\n", bi->vcell, bi->permille, bi->soc, bi->rm);
737   return;               
738 }
739
740 static void rt5025_smooth_soc(struct rt5025_battery_info *bi)
741 {
742         if ((bi->internal_status == POWER_SUPPLY_STATUS_CHARGING) &&
743             (bi->soc < 100))
744         {
745                 bi->soc++;
746                 bi->rm = bi->fcc * bi->soc * 36;
747                 rt5025_convert_masec_to_permille(bi);
748         }
749         else if ((bi->internal_status == POWER_SUPPLY_STATUS_DISCHARGING) &&
750                   (bi->soc > 0))
751         {
752                 bi->soc--;
753                 bi->rm = bi->fcc * bi->soc * 36;
754                 rt5025_convert_masec_to_permille(bi);
755         }
756         else
757         {
758           bi->smooth_flag = false;
759           bi->update_time = NORMAL_POLL;
760   }
761 }
762
763
764 static void rt5025_soc_irreversible(struct rt5025_battery_info *bi)
765 {
766         // Prevent inverse
767         //if (!bi->init_cap){
768         if (!bi->init_once){
769                 if (bi->internal_status == POWER_SUPPLY_STATUS_CHARGING)
770                 {
771                         if (bi->soc < bi->pre_soc)
772                                 bi->soc = bi->pre_soc;
773                 }
774                 else if ((bi->internal_status == POWER_SUPPLY_STATUS_DISCHARGING) && 
775                                                 (bi->tp_flag == 0))
776                 {
777                         if (bi->soc > bi->pre_soc) 
778                                 bi->soc = bi->pre_soc;
779                 }
780         }
781         else
782                 bi->init_once = false;
783
784         bi->pre_soc = bi->soc;
785 //      RTINFO("pre_soc=%d, soc=%d, internal status=%d\n", bi->pre_soc,bi->soc,bi->internal_status);
786 }
787
788 static void rt5025_soc_lock(struct rt5025_battery_info *bi)
789 {
790          // lock 99%
791   RTINFO("internal status=%d, tp_flag=%d, soc=%d\n", bi->internal_status, bi->tp_flag, bi->soc);
792   if (bi->soc >= 99) 
793   {
794         if (bi->tp_flag)
795                 bi->soc = 100;
796                 else if(bi->internal_status == POWER_SUPPLY_STATUS_CHARGING)
797                 {
798                                 bi->soc = 99;
799                                 bi->pre_soc = 99;
800                 }
801   }
802   else if ((bi->soc < 99) && (bi->tp_flag))
803   {
804                 bi->update_time = SMOOTH_POLL;
805                 bi->smooth_flag = true;
806                 rt5025_smooth_soc(bi);
807   }
808   else
809   {
810                 bi->tp_flag = false;
811   }
812
813   // lock 1%   
814   if ((bi->soc <= 1) &&
815       (bi->internal_status == POWER_SUPPLY_STATUS_DISCHARGING)){
816     if (bi->edv_flag)
817       bi->soc = 0;
818     else
819     {
820                         bi->soc = 1;
821                         bi->pre_soc = 1; 
822                 }
823       
824         }else if ((bi->soc > 1) &&
825             (bi->internal_status == POWER_SUPPLY_STATUS_DISCHARGING) &&
826             (bi->edv_flag)){
827                 bi->update_time = SMOOTH_POLL;
828                 bi->smooth_flag = true;
829                 rt5025_smooth_soc(bi);
830         }else{
831                 bi->edv_flag = false;
832         }
833 }
834
835 static void rt5025_get_soc(struct rt5025_battery_info *bi)
836 {
837   if (bi->smooth_flag){
838     bi->smooth_flag = false;
839     bi->update_time = NORMAL_POLL;
840   }
841         RTINFO("before rm=%d\n", bi->rm);
842         if ((!bi->tp_flag) &&
843                         (!bi->edv_flag)){
844                 bi->rm = (bi->rm + bi->chg_cc) > bi->dchg_cc ? 
845                             bi->rm + bi->chg_cc - bi->dchg_cc : 0;
846                 if (bi->rm > (bi->fcc * 3600))
847                                         bi->rm = bi->fcc * 3600;
848                 rt5025_convert_masec_to_permille(bi);
849                 bi->soc = DIV_ROUND_UP(bi->permille, 10);
850 #if RT5025_CSV
851     bi->temp_soc = bi->soc;
852     //pr_info("%d", bi->soc);
853 #else
854         RTINFO("after rm=%d\n", bi->rm);
855                 RTINFO("temp_soc=%d\n", bi->soc);
856 #endif
857   }
858 #if RT5025_CSV
859  // pr_info("%d,%d,%d,%d,%d", bi->soc,bi->permille,bi->rm,bi->fcc,bi->smooth_flag);
860 #else  
861         RTINFO("soc=%d, permille=%d, rm=%d, fcc=%d, smooth_flag=%d\n", bi->soc,bi->permille,bi->rm,bi->fcc,bi->smooth_flag);
862 #endif
863   return;
864 }
865
866 static void rt5025_soc_relearn_check(struct rt5025_battery_info *bi)
867 {
868   // TP relearn
869 /*  if ((bi->vcell >= TP_VOLT) &&
870                         (bi->curr <= TP_CURR) &&
871                         (bi->status == POWER_SUPPLY_STATUS_CHARGING) &&
872                         (!bi->tp_flag)){
873                 bi->tp_cnt++;
874                 bi->update_time = TP_POLL;
875         }else {
876                 bi->tp_cnt = 0;
877                 bi->update_time = NORMAL_POLL;
878         }
879    
880   if (bi->tp_cnt == TP_TOTAL_COUNT){
881                 bi->tp_cnt = 0;
882                 bi->tp_flag = true;
883                 bi->rm = bi->fcc * 3600;
884                 rt5025_convert_masec_to_permille();
885                 bi->update_time = NORMAL_POLL;
886         }*/
887         
888         // if EOC happened, the tp_flag should be set 1.
889         if(bi->tp_flag == true)
890         {
891                 bi->rm = bi->fcc * 3600;
892                 rt5025_convert_masec_to_permille(bi);
893                 bi->update_time = NORMAL_POLL;
894         }
895         
896         if(rt5025_battery_param2[4].x < bi->vcell && bi->vcell <= rt5025_battery_param2[4].x+300)
897         {
898                 bi->update_time = EDV_POLL;
899                 bi->edv_detection = true;
900         }
901         else if((bi->vcell >= rt5025_battery_param2[4].x + 300 +EDV_HYS) && (bi->edv_detection == true))
902         {
903                  bi->update_time = NORMAL_POLL;
904                  bi->edv_detection = false;              
905         }
906         else if((bi->vcell <= rt5025_battery_param2[4].x) && (bi->min_volt2_alert == true))
907         {
908                 bi->edv_flag = true;
909                 bi->rm = 0;
910                 rt5025_convert_masec_to_permille(bi);
911                 bi->edv_detection = false;
912                 bi->update_time = NORMAL_POLL;
913         }
914         else if((bi->vcell > rt5025_battery_param2[4].x + EDV_HYS) && (bi->min_volt2_alert == true))
915         {
916                 bi->min_volt2_alert = false;
917                 bi->edv_flag = false;
918         }
919         
920         /*// EDV relearn
921   if (bi->vcell <= EDV_VOLT){
922                 if ((bi->curr <= EDV_CURR) ||
923                                 (bi->vcell <= rt5025_battery_param2[4].x))
924                         bi->edv_cnt++;
925                 else
926                 {
927                         bi->edv_cnt = 0;        
928                 }
929                 bi->edv_detection = true;
930                 bi->update_time = EDV_POLL;
931         }else if ((bi->vcell > (EDV_VOLT + EDV_HYS)) &&
932                   (bi->edv_detection)) {
933                 bi->edv_cnt = 0;
934                 bi->edv_detection = false;
935                 bi->edv_flag = false;
936                 bi->update_time = NORMAL_POLL;
937         }
938         else
939         {
940                 bi->edv_cnt = 0;
941         }
942    
943   if (bi->edv_cnt == EDV_TOTAL_COUNT){
944                 bi->edv_cnt = 0;
945                 bi->edv_flag = true;
946                 bi->rm = 0;
947                 rt5025_convert_masec_to_permille();
948                 bi->edv_detection = false;
949                 bi->update_time = NORMAL_POLL;
950         }
951   if(bi->edv_detection)
952   {
953         bi->update_time = EDV_POLL;
954   }*/
955   if (bi->internal_status == POWER_SUPPLY_STATUS_CHARGING) 
956           bi->edv_flag = false;
957   else if (bi->status != POWER_SUPPLY_STATUS_FULL)
958     bi->tp_flag = false;
959
960 #if RT5025_CSV
961   //pr_err("%d,%d,%d,%d,%d", 
962    // bi->tp_cnt,bi->tp_flag,bi->edv_detection,bi->edv_cnt,bi->edv_flag);
963 #else  
964         RTINFO("tp_cnt=%d, tp_flag=%d, edv_detection=%d, edv_cnt=%d, edv_flag=%d\n", 
965     bi->tp_cnt,bi->tp_flag,bi->edv_detection,bi->edv_cnt,bi->edv_flag);
966 #endif
967
968   return;
969 }
970
971 static void rt5025_channel_cc(struct rt5025_battery_info *bi, bool enable)
972 {
973   u8 data[1];
974         
975   if (rt5025_read_reg(bi->client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
976     printk(KERN_INFO "%s: failed to read channel\n", __func__);
977   }
978
979   if (enable){
980     data[0] = data[0] | 0x80;
981   }else { 
982     data[0] = data[0] & 0x7F;
983   }
984     
985   if (rt5025_write_reg(bi->client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
986     printk(KERN_INFO "%s: failed to write channel\n", __func__);
987   }
988 }
989
990 #if 0
991 static void rt5025_pretrim(struct rt5025_battery_info *bi)
992 {
993         u8 data0[2];
994         u8 data1[1];
995         
996         data0[0] = 0x55;
997         data0[1] = 0xAA;
998   if (rt5025_write_reg(bi->client, 0xF0, data0, 2) < 0){
999     printk(KERN_ERR "%s: failed to write channel\n", __func__);
1000   }
1001         data0[0] = 0x07;
1002   if (rt5025_write_reg(bi->client, 0xF1, data0, 1) < 0){
1003     printk(KERN_ERR "%s: failed to write channel\n", __func__);
1004   }
1005   // write trim data D0
1006         data0[0] = 0xDE;
1007   if (rt5025_write_reg(bi->client, 0xD0, data0, 1) < 0){
1008     printk(KERN_ERR "%s: failed to write channel\n", __func__);
1009   }
1010   // Read back to verify
1011   if (rt5025_read_reg(bi->client, 0xD0, data1, 1) < 0){
1012     printk(KERN_ERR "%s: failed to read channel\n", __func__);
1013   }
1014   if (data1[0] != data0[0])
1015                 printk(KERN_ERR "%s: 0xD0 write fail\n", __func__);
1016         // write trim data D1
1017         data0[0] = 0x01;
1018   if (rt5025_write_reg(bi->client, 0xD1, data0, 1) < 0){
1019     printk(KERN_ERR "%s: failed to write channel\n", __func__);
1020   }
1021   // Read back to verify
1022   if (rt5025_read_reg(bi->client, 0xD1, data1, 1) < 0){
1023     printk(KERN_ERR "%s: failed to read channel\n", __func__);
1024   }
1025   if (data1[0] != data0[0])
1026                 printk(KERN_ERR "%s: 0xD1 write fail\n", __func__);
1027   // write trim data D2
1028         data0[0] = 0x10;
1029   if (rt5025_write_reg(bi->client, 0xD2, data0, 1) < 0){
1030     printk(KERN_ERR "%s: failed to write channel\n", __func__);
1031   }
1032   // Read back to verify
1033   if (rt5025_read_reg(bi->client, 0xD2, data1, 1) < 0){
1034     printk(KERN_ERR "%s: failed to read channel\n", __func__);
1035   }
1036   if(data1[0] != data0[0])
1037                 printk(KERN_ERR "%s: 0xD2 write fail\n", __func__);
1038   // write trim data D3
1039         data0[0] = 0x89;
1040   if (rt5025_write_reg(bi->client, 0xD3, data0, 1) < 0){
1041     printk(KERN_ERR "%s: failed to write channel\n", __func__);
1042   }
1043   // Read back to verify
1044   if (rt5025_read_reg(bi->client, 0xD3, data1, 1) < 0){
1045     printk(KERN_ERR "%s: failed to read channel\n", __func__);
1046   }
1047   if(data1[0] != data0[0])
1048                 printk(KERN_ERR "%s: 0xD3 write fail\n", __func__);
1049   // write trim data D4
1050         data0[0] = 0xF2;
1051   if (rt5025_write_reg(bi->client, 0xD4, data0, 1) < 0){
1052     printk(KERN_ERR "%s: failed to write channel\n", __func__);
1053   }
1054   // Read back to verify
1055   if (rt5025_read_reg(bi->client, 0xD4, data1, 1) < 0){
1056     printk(KERN_ERR "%s: failed to read channel\n", __func__);
1057   }
1058   if (data1[0] != data0[0])
1059                 printk(KERN_ERR "%s: 0xD4 write fail\n", __func__);                             
1060                          
1061         data0[0] = 0x55;
1062         data0[1] = 0x55;
1063   if (rt5025_write_reg(bi->client, 0xF0, data0, 2) < 0){
1064     printk(KERN_ERR "%s: failed to write channel\n", __func__);
1065   }
1066 }
1067 #endif
1068
1069 static int rt5025_battery_parameter_backup(struct rt5025_battery_info *bi)
1070 {
1071         u8 data[4];
1072         RTINFO("\n");
1073         //backup fcc_aging, rm, cycle_count, acc_dchg_cap
1074         //fcc_aging
1075         data[0] = (bi->fcc_aging>>8)&0xff;
1076         data[1] = (bi->fcc_aging)&0xff;
1077         rt5025_write_reg(bi->client, 0x21, data, 2);
1078         //rm
1079         data[0] = (bi->rm>>24)&0xff;
1080         data[1] = (bi->rm>>16)&0xff;
1081         data[2] = (bi->rm>>8)&0xff;
1082         data[3] = (bi->rm)&0xff;
1083         rt5025_write_reg(bi->client, 0x23, data, 4);
1084         //acc_dchg_cap
1085         data[0] = (bi->acc_dchg_cap>>24)&0xff;
1086         data[1] = (bi->acc_dchg_cap>>16)&0xff;
1087         data[2] = (bi->acc_dchg_cap>>8)&0xff;
1088         data[3] = (bi->acc_dchg_cap)&0xff;
1089         rt5025_write_reg(bi->client, 0x27, data, 4);
1090         //cycle_count
1091         data[0] = (bi->cycle_cnt)&0xff;
1092         rt5025_write_reg(bi->client, 0x2B, data, 1);
1093         return 0;
1094 }
1095
1096 static int rt5025_battery_parameter_restore(struct rt5025_battery_info *bi)
1097 {
1098         u8 data[4];
1099         RTINFO("\n");
1100         //restore fcc_aging, rm ,cycle_count, acc_dchg_cap
1101         //fcc_aging
1102         rt5025_read_reg(bi->client, 0x21, data, 2);
1103         bi->fcc = bi->fcc_aging = data[0]<<8 | data[1];
1104         //rm
1105         rt5025_read_reg(bi->client, 0x23, data, 4);
1106         bi->rm = data[0]<<24 | data[1]<<16 | data[2]<<8 | data[3];
1107         //acc_dchg_cap
1108         rt5025_read_reg(bi->client, 0x27, data, 4);
1109         bi->acc_dchg_cap = data[0]<<24 | data[1]<<16 | data[2]<<8 | data[3];
1110         //cycle_count
1111         rt5025_read_reg(bi->client, 0x2B, data, 1);
1112         bi->cycle_cnt = data[0];
1113         
1114         return 0;
1115 }
1116
1117
1118 // return value; 1-> initialized, 0-> no initial value
1119 static int rt5025_battery_parameter_initcheck(struct rt5025_battery_info *bi)
1120 {
1121         u8 data[2];
1122         u16 value;
1123         int ret = 0;
1124
1125         if (rt5025_read_reg(bi->client, 0x21, data, 2) < 0)
1126         {
1127                 pr_err("%s: check initial value error\n", __func__);
1128         }
1129         else
1130         {
1131                 value = data[1]<<8 | data[0];
1132                 if (value)
1133                         ret = 1;
1134         }
1135         RTINFO("initial check = %d\n", ret);
1136
1137         return ret;
1138 }
1139
1140 static void rt5025_register_init(struct rt5025_battery_info *bi)
1141 {  
1142         u8 data[1];
1143         
1144         /* enable the channel of current,qc,ain,vbat and vadc */
1145         if (rt5025_read_reg(bi->client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
1146                 pr_err("%s: failed to read channel\n", __func__);
1147         }
1148         data[0] = data[0] | CHANNEL_L_BIT_CADC_EN | CHANNEL_L_BIT_AINCH | \
1149                 CHANNEL_L_BIT_VBATSCH | CHANNEL_L_BIT_VADC_EN;
1150         if (rt5025_write_reg(bi->client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
1151                 pr_err("%s: failed to write channel\n", __func__);
1152         }
1153         /* set the alert threshold value */
1154         irq_thres[MINVOLT2] = VALRTMIN2_VALUE;
1155         irq_thres[VOLT_RLS] = VRLS_VALUE;
1156
1157         bi->chg_cc_unuse = 0;
1158         bi->dchg_cc_unuse = 0;
1159         bi->pre_gauge_timer = 0;
1160         bi->online = 1;
1161         bi->status = bi->internal_status = POWER_SUPPLY_STATUS_DISCHARGING;
1162         bi->health = POWER_SUPPLY_HEALTH_GOOD;
1163         
1164         bi->init_cap = true;
1165         bi->avg_flag = true;
1166
1167         bi->fcc_aging = rt5025_battery_param2[4].y;
1168         bi->fcc = rt5025_battery_param2[4].y;
1169         bi->dc = rt5025_battery_param2[4].y;
1170         bi->rm = 0;
1171
1172         bi->edv_cnt = 0;
1173         bi->edv_flag = false;
1174         bi->edv_detection = false;
1175         bi->init_once = true;
1176
1177         bi->tp_cnt = 0;
1178         bi->tp_flag = false;
1179   
1180         bi->acc_dchg_cap = 0;
1181         bi->cycle_cnt = 0;
1182
1183         // if has initial data, rewrite to the stored data
1184         if (rt5025_battery_parameter_initcheck(bi))
1185         {
1186                 bi->init_cap = false;
1187                 rt5025_battery_parameter_restore(bi);
1188                 bi->soc = bi->rm/36/bi->fcc_aging;
1189         }
1190   
1191         bi->update_time = NORMAL_POLL;
1192         bi->device_suspend = false;
1193         RTINFO("register initialized\n");
1194 }
1195
1196 static void rt5025_soc_aging(struct rt5025_battery_info *bi)
1197 {
1198         if (bi->cycle_cnt >= rt5025_battery_param2[3].x)
1199         {
1200                 bi->fcc_aging = bi->fcc_aging*(1000-rt5025_battery_param2[3].y)/1000;
1201                 bi->rm = bi->rm*(1000-rt5025_battery_param2[3].y)/1000;
1202                 bi->cycle_cnt -= rt5025_battery_param2[3].x;
1203         }
1204         RTINFO("fcc_aging=%d, rm=%d, cycle_cnt=%d\n", bi->fcc_aging, bi->rm, bi->cycle_cnt);
1205 }
1206
1207 static void rt5025_temp_comp(struct rt5025_battery_info *bi)
1208 {
1209         int i = 1;
1210   int size;
1211   int slope, const_term;
1212   int delta_y, delta_x; 
1213   
1214   size = 3;
1215   while((bi->ext_temp < rt5025_battery_param2[i].x) &&
1216                                 (i < (size - 1))){
1217     i++;
1218   }
1219
1220   delta_x = rt5025_battery_param2[i-1].x - rt5025_battery_param2[i].x;
1221   delta_y = (rt5025_battery_param2[i-1].y - rt5025_battery_param2[i].y);
1222
1223   slope = delta_y  * 1000 / delta_x;
1224
1225   const_term = (rt5025_battery_param2[i].y) - ((rt5025_battery_param2[i].x * slope) / 1000);
1226
1227   if (bi->ext_temp >= rt5025_battery_param2[0].x)
1228     bi->tempcmp = rt5025_battery_param2[0].y; 
1229   else if (bi->ext_temp <= rt5025_battery_param2[size-1].x)
1230     bi->tempcmp = rt5025_battery_param2[size-1].y;
1231   else
1232     bi->tempcmp = (bi->ext_temp * slope) / 1000 + const_term;
1233         
1234   bi->fcc = bi->fcc_aging + bi->fcc_aging * bi->tempcmp /1000;
1235   if (bi->fcc >= (bi->dc*3>>1))
1236         bi->fcc = bi->dc*3>>1;
1237   if (bi->fcc <= (bi->dc>>1))
1238         bi->fcc = bi->dc>>1;
1239   bi->rm = bi->fcc * bi->soc * 36;
1240   //pr_err("[rt5025] i=%d, delta_x=%d, delta_y=%d, slope=%d, const_term=%d\n", i, delta_x, delta_y, slope, const_term);
1241   RTINFO("tempcmp=%d, ext_temp=%d, fcc=%d, rm=%d\n", bi->tempcmp, bi->ext_temp, bi->fcc, bi->rm);
1242   return;               
1243 }
1244
1245 static void rt5025_soc_temp_comp(struct rt5025_battery_info *bi)
1246 {
1247   RTINFO("soc->%d++\n", bi->soc);
1248   bi->temp_range_0_5 = false;
1249   bi->temp_range_5_10 = false;
1250   bi->temp_range_10_15 = false;
1251   bi->temp_range_15_20 = false;
1252   bi->temp_range_20_30 = false; 
1253   bi->temp_range_30_35 = false;
1254   bi->temp_range_35_40 = false;
1255   bi->temp_range_40_45 = false;
1256   bi->temp_range_45_50 = false;
1257   
1258   if (bi->ext_temp < 50)
1259         bi->temp_range_0_5 = true;
1260   else if (50 <= bi->ext_temp && bi->ext_temp < 100)
1261         bi->temp_range_5_10 = true;
1262   else if (100 <= bi->ext_temp && bi->ext_temp < 150)
1263         bi->temp_range_10_15 = true;
1264   else if (150 <= bi->ext_temp && bi->ext_temp < 200)
1265                 bi->temp_range_15_20 = true;
1266   else if (200 <= bi->ext_temp && bi->ext_temp <= 300)
1267         bi->temp_range_20_30 = true;
1268   else if (300 < bi->ext_temp && bi->ext_temp <= 350)
1269         bi->temp_range_30_35 = true;
1270   else if (350 < bi->ext_temp && bi->ext_temp <= 400)
1271         bi->temp_range_35_40 = true;
1272   else if (400 < bi->ext_temp && bi->ext_temp <= 450)
1273         bi->temp_range_40_45 = true;
1274   else if (450 < bi->ext_temp)
1275         bi->temp_range_45_50 = true;
1276         
1277   if((bi->temp_range_0_5 == true) && (bi->range_0_5_done == false))     
1278   {
1279         rt5025_temp_comp(bi);
1280         bi->range_0_5_done = true;
1281         bi->range_5_10_done = false;
1282         bi->range_10_15_done = false;
1283         bi->range_15_20_done = false;
1284         bi->range_20_30_done = false;
1285         bi->range_30_35_done = false;
1286         bi->range_35_40_done = false;
1287         bi->range_40_45_done = false;
1288         bi->range_45_50_done = false;
1289   }
1290         else if((bi->temp_range_5_10 == true) && (bi->range_5_10_done == false))        
1291   {
1292         rt5025_temp_comp(bi);
1293         bi->range_0_5_done = false;
1294         bi->range_5_10_done = true;
1295         bi->range_10_15_done = false;
1296         bi->range_15_20_done = false;
1297         bi->range_20_30_done = false;
1298         bi->range_30_35_done = false;
1299         bi->range_35_40_done = false;
1300         bi->range_40_45_done = false;
1301         bi->range_45_50_done = false;
1302   }
1303   else if((bi->temp_range_10_15 == true) && (bi->range_10_15_done == false))    
1304   {
1305     rt5025_temp_comp(bi);
1306     bi->range_0_5_done = false;
1307         bi->range_5_10_done = false;
1308         bi->range_10_15_done = true;
1309         bi->range_15_20_done = false;
1310         bi->range_20_30_done = false;
1311         bi->range_30_35_done = false;
1312         bi->range_35_40_done = false;
1313         bi->range_40_45_done = false;
1314         bi->range_45_50_done = false;
1315   }
1316   else if((bi->temp_range_15_20 == true) && (bi->range_15_20_done == false))    
1317   {
1318         rt5025_temp_comp(bi);
1319         bi->range_0_5_done = false;
1320         bi->range_5_10_done = false;
1321         bi->range_10_15_done = false;
1322         bi->range_15_20_done = true;
1323         bi->range_20_30_done = false;
1324         bi->range_30_35_done = false;
1325         bi->range_35_40_done = false;
1326         bi->range_40_45_done = false;
1327         bi->range_45_50_done = false;
1328   }
1329   else if((bi->temp_range_20_30 == true) && (bi->range_20_30_done == false))    
1330   {
1331         bi->fcc = bi->fcc_aging;
1332         bi->rm = bi->fcc*bi->soc*36;
1333         bi->range_0_5_done = false;
1334         bi->range_5_10_done = false;
1335         bi->range_10_15_done = false;
1336         bi->range_15_20_done = false;
1337         bi->range_20_30_done = true;
1338         bi->range_30_35_done = false;
1339         bi->range_35_40_done = false;
1340         bi->range_40_45_done = false;
1341         bi->range_45_50_done = false;
1342   }  
1343   else if((bi->temp_range_30_35 == true) && (bi->range_30_35_done == false))    
1344   {
1345         rt5025_temp_comp(bi);
1346         bi->range_0_5_done = false;
1347         bi->range_5_10_done = false;
1348         bi->range_10_15_done = false;
1349         bi->range_15_20_done = false;
1350         bi->range_20_30_done = false;
1351         bi->range_30_35_done = true;
1352         bi->range_35_40_done = false;
1353         bi->range_40_45_done = false;
1354         bi->range_45_50_done = false;
1355   }  
1356   else if((bi->temp_range_35_40 == true) && (bi->range_35_40_done == false))    
1357   {
1358         rt5025_temp_comp(bi);
1359         bi->range_0_5_done = false;
1360         bi->range_5_10_done = false;
1361         bi->range_10_15_done = false;
1362         bi->range_15_20_done = false;
1363         bi->range_20_30_done = false;
1364         bi->range_30_35_done = false;
1365         bi->range_35_40_done = true;
1366         bi->range_40_45_done = false;
1367         bi->range_45_50_done = false;
1368   }  
1369   else if((bi->temp_range_40_45 == true) && (bi->range_40_45_done == false))    
1370   {
1371         rt5025_temp_comp(bi);
1372         bi->range_0_5_done = false;
1373         bi->range_5_10_done = false;
1374         bi->range_10_15_done = false;
1375         bi->range_15_20_done = false;
1376         bi->range_20_30_done = false;
1377         bi->range_30_35_done = false;
1378         bi->range_35_40_done = false;
1379         bi->range_40_45_done = true;
1380         bi->range_45_50_done = false;
1381   }  
1382   else if((bi->temp_range_45_50 == true) && (bi->range_45_50_done == false))    
1383   {
1384         rt5025_temp_comp(bi);
1385         bi->range_0_5_done = false;
1386         bi->range_5_10_done = false;
1387         bi->range_10_15_done = false;
1388         bi->range_15_20_done = false;
1389         bi->range_20_30_done = false;
1390         bi->range_30_35_done = false;
1391         bi->range_35_40_done = false;
1392         bi->range_40_45_done = false;
1393         bi->range_45_50_done = true;
1394   } 
1395   RTINFO("soc->%d--\n", bi->soc);
1396 }
1397
1398 static void rt5025_update(struct rt5025_battery_info *bi)
1399 {
1400   /* Update voltage */
1401   rt5025_get_vcell(bi);
1402   /* Update current */
1403   rt5025_get_current(bi);
1404         
1405   /* Update external temperature */
1406   rt5025_get_external_temp(bi);
1407   /* Read timer */
1408   rt5025_get_timer(bi);
1409   /* Update chg cc */
1410   rt5025_get_chg_cc(bi);
1411   /* Update dchg cc */
1412   rt5025_get_dchg_cc(bi);
1413   /* Update cycle count check */
1414   rt5025_cycle_count(bi);
1415   /* Calculate cycle count */
1416   rt5025_soc_aging(bi);
1417   /* calculate initial soc */
1418   if (bi->init_cap){  
1419     rt5025_init_capacity(bi);
1420 #if RT5025_CSV
1421     pr_info("vcell,offset,current,timer,interval,QCHG,QDCHG,tp_cnt,tp_flag,edv_det,edv_cnt,edv_flag,soc,permille,RM,FCC,smooth_flag,acc_QD,cycle,update_time\n");
1422 #endif
1423   }
1424
1425         /* Relearn SOC */
1426         rt5025_soc_relearn_check(bi);
1427         /* SOC_Temp_Comp*/
1428         rt5025_soc_temp_comp(bi);
1429   /* Update SOC */
1430   rt5025_get_soc(bi);
1431   
1432   /* SOC Control Process */
1433   rt5025_soc_lock(bi);
1434   rt5025_soc_irreversible(bi);
1435   
1436   /* Update RTTF or RTTE */
1437   //rt5025_run_time(bi);
1438         
1439 #if TEMPERATURE_ALERT 
1440   if ((bi->max_temp_irq == false) &&
1441                   (((irq_thres[MAXTEMP] * IRQ_THRES_UNIT) / 100 - bi->ain_volt) > irq_thres[TEMP_RLS])){
1442                 rt5025_alert_setting(bi,MAXTEMP,true);
1443         }else if ((bi->min_temp_irq == false) &&
1444                                           ((bi->ain_volt - (irq_thres[MINTEMP] * IRQ_THRES_UNIT) / 100) > irq_thres[TEMP_RLS])){
1445                 rt5025_alert_setting(bi,MINTEMP,true);
1446   }
1447 #endif  
1448 #if 0           
1449         }else if ((bi->min_volt1_irq == false) &&
1450                                         ((bi->vcell - ((irq_thres[MINVOLT1] * IRQ_THRES_UNIT) / 100)) > irq_thres[VOLT_RLS])){
1451                 rt5025_alert_setting(bi,MINVOLT1,true);                         
1452         }else if ((bi->min_volt2_irq == false) &&
1453                                         ((bi->vcell - ((irq_thres[MINVOLT2] * IRQ_THRES_UNIT) / 100)) > irq_thres[VOLT_RLS])){
1454                 rt5025_alert_setting(bi,MINVOLT2,true);                                         
1455         }
1456 #endif
1457         
1458 #if VOLTAGE_ALERT
1459         if ((bi->min_volt2_irq == false) &&
1460                                 (bi->vcell > (rt5025_battery_param2[4].x + EDV_HYS))){  
1461                 rt5025_alert_setting(bi,MINVOLT2,true);                                         
1462         }
1463 #endif
1464 #if RT5025_CSV
1465   printk(KERN_INFO "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",bi->vcell,bi->curr_offset,bi->curr,bi->gauge_timer,bi->time_interval,bi->chg_cc,
1466                 bi->dchg_cc,bi->tp_cnt,bi->tp_flag,bi->edv_detection,bi->edv_cnt,bi->edv_flag,bi->soc,
1467                 bi->permille,bi->rm,bi->fcc,bi->smooth_flag,bi->acc_dchg_cap,bi->cycle_cnt,bi->update_time);
1468 #else
1469   pr_info("[RT5025] update_time=%d\n",bi->update_time);
1470   pr_info("\n");
1471 #endif
1472 }
1473
1474 static void rt5025_update_work(struct work_struct *work)
1475 {
1476         struct delayed_work *delayed_work = (struct delayed_work *)container_of(work, struct delayed_work, work);
1477         struct rt5025_battery_info *bi = (struct rt5025_battery_info *)container_of(delayed_work, struct rt5025_battery_info, monitor_work);
1478         unsigned long flags;
1479         
1480   rt5025_update(bi);
1481   
1482         /* prevent suspend before starting the alarm */
1483         local_irq_save(flags);
1484         bi->last_poll = alarm_get_elapsed_realtime();
1485         rt5025_program_alarm(bi);
1486         local_irq_restore(flags);
1487
1488         wake_unlock(&bi->monitor_wake_lock);
1489 }
1490
1491 static enum power_supply_property rt5025_battery_props[] = {
1492   POWER_SUPPLY_PROP_STATUS,
1493   POWER_SUPPLY_PROP_HEALTH,
1494   POWER_SUPPLY_PROP_PRESENT,
1495   POWER_SUPPLY_PROP_TEMP,
1496   POWER_SUPPLY_PROP_ONLINE,
1497   POWER_SUPPLY_PROP_VOLTAGE_NOW,
1498   POWER_SUPPLY_PROP_CURRENT_NOW,
1499   POWER_SUPPLY_PROP_CAPACITY,
1500   POWER_SUPPLY_PROP_TECHNOLOGY,
1501   #if 0
1502   POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
1503   POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
1504   #endif
1505 };
1506
1507 static int rt5025_battery_suspend(struct platform_device *pdev, pm_message_t state)
1508 {
1509         struct rt5025_battery_info *bi = platform_get_drvdata(pdev);
1510         //bi->last_event = ktime_get();
1511         bi->last_event = current_kernel_time();
1512
1513         //cy add for battery parameter backup
1514         rt5025_battery_parameter_backup(bi);
1515
1516         rt5025_channel_cc(bi, false);
1517         cancel_delayed_work(&bi->monitor_work);
1518         rt5025_get_timer(bi);
1519         bi->device_suspend = true;
1520         //bi->update_time = SUSPEND_POLL;
1521         RTINFO("RM=%d\n",bi->rm);
1522         return 0;
1523 }
1524
1525 static int rt5025_battery_resume(struct platform_device *pdev)
1526 {
1527         struct rt5025_battery_info *bi = platform_get_drvdata(pdev);
1528         //ktime_t now;
1529         //struct timespec now = current_kernel_time();
1530         //struct timeval tv;
1531         //long time_interval;
1532
1533         //now = ktime_get();
1534         //tv = ktime_to_timeval(ktime_sub(now, bi->last_event));
1535         //RTINFO("Sleep time = %d\n",(u32)tv.tv_sec);
1536         //bi->rm = bi->rm - ((u32)tv.tv_sec * SLEEP_CURRENT);
1537
1538         //time_interval = now.tv_sec - bi->last_event.tv_sec;
1539         //bi->rm = bi->rm - (time_interval * SLEEP_CURRENT);
1540         //RTINFO("Sleep time=%d, RM=%d",(int)time_interval,bi->rm);  
1541
1542         rt5025_channel_cc(bi, true);
1543         wake_lock(&bi->monitor_wake_lock);
1544         schedule_delayed_work(&bi->monitor_work, 0);
1545         bi->device_suspend = false;
1546         //RTINFO("\n");
1547         return 0;
1548 }
1549
1550 static int rt5025_battery_remove(struct platform_device *pdev)
1551 {
1552         struct rt5025_battery_info *bi = platform_get_drvdata(pdev);
1553
1554         power_supply_unregister(&bi->battery);
1555         cancel_delayed_work(&bi->monitor_work);
1556         wake_lock_destroy(&bi->monitor_wake_lock);
1557         kfree(bi);
1558         return 0;
1559 }
1560
1561 static int rt5025_battery_probe(struct platform_device *pdev)
1562 {
1563         struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
1564         struct rt5025_battery_info *bi;
1565         int ret;
1566
1567         bi = kzalloc(sizeof(*bi), GFP_KERNEL);
1568         if (!bi)
1569                 return -ENOMEM;
1570    
1571         bi->client = chip->i2c;
1572         bi->chip = chip;
1573
1574
1575         bi->last_poll = alarm_get_elapsed_realtime();
1576         alarm_init(&bi->wakeup_alarm, ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP,
1577                 rt5025_gauge_alarm);
1578
1579         INIT_DELAYED_WORK(&bi->monitor_work, rt5025_update_work);
1580         
1581         wake_lock_init(&bi->monitor_wake_lock, WAKE_LOCK_SUSPEND, "rt-battery-monitor");
1582         wake_lock_init(&bi->low_battery_wake_lock, WAKE_LOCK_SUSPEND, "low_battery_wake_lock");
1583 #if RT5025_TEST_WAKE_LOCK
1584         wake_lock_init(&bi->test_wake_lock, WAKE_LOCK_SUSPEND, "rt-test");
1585 #endif
1586         /* Write trimed data */
1587         //rt5025_pretrim(client);
1588         /* enable channel */
1589         rt5025_register_init(bi);
1590         /* enable gauge IRQ */
1591         rt5025_alert_init(bi);
1592   
1593         /* register callback functions */
1594         /*
1595         chip->cb.rt5025_gauge_irq_handler = rt5025_irq_handler;
1596         chip->cb.rt5025_gauge_set_status = rt5025_set_status;
1597         chip->cb.rt5025_gauge_set_online = rt5025_set_online;
1598         chip->cb.rt5025_gauge_suspend = rt5025_gauge_suspend;
1599         chip->cb.rt5025_gauge_resume = rt5025_gauge_resume;
1600         chip->cb.rt5025_gauge_remove = rt5025_gauge_remove;
1601         
1602         rt5025_register_gauge_callbacks(&chip->cb);
1603         */
1604
1605         platform_set_drvdata(pdev, bi);
1606
1607         bi->battery.name = "rt5025-battery";
1608         bi->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1609         bi->battery.get_property = rt5025_get_property;
1610         bi->battery.properties = rt5025_battery_props;
1611         bi->battery.num_properties = ARRAY_SIZE(rt5025_battery_props);
1612   
1613         ret = power_supply_register(&pdev->dev, &bi->battery);
1614         if (ret) {
1615                 printk(KERN_ERR "[RT5025] power supply register failed\n");
1616                 goto err_wake_lock;
1617         }
1618   
1619
1620         wake_lock(&bi->monitor_wake_lock);
1621 #if RT5025_TEST_WAKE_LOCK
1622         wake_lock(&bi->test_wake_lock);
1623 #endif
1624         schedule_delayed_work(&bi->monitor_work, msecs_to_jiffies(INIT_POLL*MSEC_PER_SEC));
1625         chip->battery_info = bi;
1626
1627         pr_info("rt5025-battery driver is successfully loaded\n");
1628         
1629   return 0;
1630
1631 err_wake_lock:
1632         wake_lock_destroy(&bi->monitor_wake_lock);
1633         kfree(bi);
1634
1635         return ret;
1636 }
1637
1638 static void rt5025_battery_shutdown(struct platform_device *pdev)
1639 {
1640         struct rt5025_battery_info *bi = platform_get_drvdata(pdev);
1641
1642         RTINFO("\n");
1643         rt5025_battery_parameter_backup(bi);
1644 }
1645
1646 static struct platform_driver rt5025_battery_driver = 
1647 {
1648         .driver = {
1649                 .name = RT5025_DEVICE_NAME "-battery",
1650                 .owner = THIS_MODULE,
1651         },
1652         .probe = rt5025_battery_probe,
1653         .remove = __devexit_p(rt5025_battery_remove),
1654         .shutdown = rt5025_battery_shutdown,
1655         .suspend = rt5025_battery_suspend,
1656         .resume = rt5025_battery_resume,
1657 };
1658
1659 static int __init rt5025_battery_init(void)
1660 {
1661         return platform_driver_register(&rt5025_battery_driver);
1662 }
1663 module_init(rt5025_battery_init);
1664
1665 static void __exit rt5025_battery_exit(void)
1666 {
1667         platform_driver_unregister(&rt5025_battery_driver);
1668 }
1669 module_exit(rt5025_battery_exit);
1670
1671
1672 MODULE_LICENSE("GPL v2");
1673 MODULE_AUTHOR("Nick Hung <nick_hung@richtek.com");
1674 MODULE_DESCRIPTION("battery gauge driver for RT5025");
1675 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-battery");