1 /* drivers/power/rt5025-battery.c
2 * I2C Driver for Richtek RT5025 PMIC
3 * Multi function device - multi functional baseband PMIC Battery part
6 * Author: Nick Hung <nick_hung@richtek.com>
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.
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>
31 #define VOLTAGE_ALERT 1
32 #define TEMPERATURE_ALERT 0
36 #define RT5025_TEST_WAKE_LOCK 1
38 u8 irq_thres[LAST_TYPE];
40 void rt5025_gauge_set_status(struct rt5025_battery_info *bi, int status)
43 if (status == POWER_SUPPLY_STATUS_FULL)
46 EXPORT_SYMBOL(rt5025_gauge_set_status);
48 void rt5025_gauge_set_online(struct rt5025_battery_info *bi, bool present)
52 EXPORT_SYMBOL(rt5025_gauge_set_online);
54 static int rt5025_read_reg(struct i2c_client *client,
55 u8 reg, u8 *data, u8 len)
58 return rt5025_reg_block_read(client, reg, len, data);
60 struct i2c_adapter *adap = client->adapter;
61 struct i2c_msg msgs[2];
64 msgs[0].addr = client->addr;
65 msgs[0].flags = client->flags;
69 msgs[1].addr = client->addr;
70 msgs[1].flags = client->flags | I2C_M_RD;
74 ret = i2c_transfer(adap, msgs, 2);
76 return (ret == 2)? len : ret;
80 static int rt5025_write_reg(struct i2c_client *client,
81 u8 reg, u8 *data, u8 len)
84 return rt5025_reg_block_write(client, reg, len, data);
86 struct i2c_adapter *adap = client->adapter;
89 char *tx_buf = (char *)kmalloc(len + 1, GFP_KERNEL);
94 memcpy(tx_buf+1, data, len);
96 msg.addr = client->addr;
97 msg.flags = client->flags;
99 msg.buf = (char *)tx_buf;
101 ret = i2c_transfer(adap, &msg, 1);
103 return (ret == 1) ? len : ret;
107 static void rt5025_gauge_alarm(struct alarm *alarm)
109 struct rt5025_battery_info *bi = (struct rt5025_battery_info *)container_of(alarm, struct rt5025_battery_info, wakeup_alarm);
111 wake_lock(&bi->monitor_wake_lock);
112 schedule_delayed_work(&bi->monitor_work, 0);
115 static void rt5025_program_alarm(struct rt5025_battery_info *bi)
117 ktime_t low_interval = ktime_set(bi->update_time, 0);
118 ktime_t slack = ktime_set(20, 0);
121 next = ktime_add(bi->last_poll, low_interval);
122 alarm_start_range(&bi->wakeup_alarm, next, ktime_add(next, slack));
126 static void rt5025_run_time(struct rt5025_battery_info *bi)
130 bi->time_to_empty = bi->rm / (bi->curr*(-1));
134 bi->time_to_full = (bi->fcc * 3600 - bi->rm) / bi->curr;
136 RTINFO("RTTF = %d\n",bi->time_to_full);
137 RTINFO("RTTE = %d\n",bi->time_to_empty);
141 static int rt5025_get_property(struct power_supply *psy,
142 enum power_supply_property psp,
143 union power_supply_propval *val)
145 struct rt5025_battery_info *bi = dev_get_drvdata(psy->dev->parent);
148 case POWER_SUPPLY_PROP_STATUS:
149 val->intval = bi->status;
150 //val->intval = POWER_SUPPLY_STATUS_CHARGING;
152 case POWER_SUPPLY_PROP_HEALTH:
153 val->intval = bi->health;
155 case POWER_SUPPLY_PROP_PRESENT:
156 val->intval = bi->present;
158 case POWER_SUPPLY_PROP_TEMP:
159 val->intval = bi->ext_temp;
161 case POWER_SUPPLY_PROP_ONLINE:
162 val->intval = bi->online;
164 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
165 val->intval = bi->vcell * 1000; //uV
167 case POWER_SUPPLY_PROP_CURRENT_NOW:
168 val->intval = bi->curr * 1000; //uA
170 case POWER_SUPPLY_PROP_CAPACITY:
171 val->intval = bi->soc;
173 if (val->intval > 100)
176 case POWER_SUPPLY_PROP_TECHNOLOGY:
177 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
180 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
181 val->intval = bi->time_to_empty;
183 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
184 val->intval = bi->time_to_full;
193 static void rt5025_get_vcell(struct rt5025_battery_info *bi)
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__);
202 bi->vcell = ((data[0] << 8) + data[1]) * 61 / 100;
204 bi->vcell = (bi->vcell + ((data[0] << 8) + data[1]) * 61 / 100) / 2;
206 bi->curr_offset = (15444 * bi->vcell - 27444000) / 10000;
208 if (37 * bi->vcell > 92000)
209 bi->curr_offset = (37 * bi->vcell - 92000) / 1000;
215 // if (!bi->avg_flag)
216 // pr_info("%d,%d,", bi->vcell, bi->curr_offset);
219 RTINFO("vcell_pre: %d, offset: %d\n", bi->vcell, bi->curr_offset);
221 RTINFO("vcell_avg: %d, offset: %d\n", bi->vcell, bi->curr_offset);
225 static void rt5025_get_current(struct rt5025_battery_info *bi)
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__);
235 temp = (data[0]<<8) | data[1];
236 bi->curr_raw = ((temp & 0x7FFF) * 3125) / 10000;
238 if (data[0] & (1 << 7)) {
240 temp = (((temp & 0x7FFF) * 3125) / 10 + bi->curr_offset) / 1000;
242 if ((temp * 3125) / 10 > bi->curr_offset)
243 temp = ((temp * 3125) / 10 - bi->curr_offset) / 1000;
254 temp = (data[0]<<8) | data[1];
255 if (data[0] & (1 << 7)) {
257 temp = temp & 0x7FFF;
258 if(temp > bi->curr_offset)
259 temp = temp - bi->curr_offset;
261 temp = temp + bi->curr_offset;
264 temp = (temp * 37375) / 100000; //Unit: 0.3125mA
275 bi->curr = (bi->curr + temp) / 2;
278 bi->internal_status = POWER_SUPPLY_STATUS_CHARGING;
280 bi->internal_status = POWER_SUPPLY_STATUS_DISCHARGING;
281 RTINFO("current=%d, internal_status=%d\n", bi->curr, bi->internal_status);
284 // if (!bi->avg_flag)
285 // pr_info("%d,",bi->curr);
288 RTINFO("current_pre: %d\n", bi->curr);
290 RTINFO("current_avg: %d\n", bi->curr);
294 static void rt5025_get_external_temp(struct rt5025_battery_info *bi)
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__);
302 bi->ain_volt = (data[0] * 256 + data[1]) * 61 / 100;
303 if(bi->ain_volt < 1150)
312 temp = (bi->ain_volt * (-91738) + 81521000) / 100000;
313 bi->ext_temp = (int)temp;
315 //bi->ext_temp = 250;
317 if (bi->ext_temp >= HIGH_TEMP_THRES) {
318 if (bi->health != POWER_SUPPLY_HEALTH_OVERHEAT)
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)
328 bi->temp_high_cnt = 0;
329 bi->temp_low_cnt = 0;
330 bi->temp_recover_cnt = 0;
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;
343 //RTINFO("external temperature: %d\n", bi->ext_temp);
346 static void rt5025_clear_cc(struct rt5025_battery_info *bi, operation_mode mode)
350 if (rt5025_read_reg(bi->client, RT5025_REG_CHANNEL_MSB, data, 2) < 0){
351 pr_err("%s: failed to read channel\n", __func__);
355 data[0] = data[0] | CHANNEL_H_BIT_CLRQCHG;
357 data[0] = data[0] | CHANNEL_H_BIT_CLRQDCHG;
359 if (rt5025_write_reg(bi->client, RT5025_REG_CHANNEL_MSB, data, 2) < 0){
360 pr_err("%s: failed to write channel\n", __func__);
364 static void rt5025_get_chg_cc(struct rt5025_battery_info *bi)
367 u32 qh_old,ql_old,qh_new,ql_new;
370 if (rt5025_read_reg(bi->client, RT5025_REG_QCHGH_MSB, data, 4) < 0){
371 pr_err("%s: Failed to read QCHG\n", __func__);
373 qh_old = (data[0]<<8) + data[1];
374 ql_old = (data[2]<<8) + data[3];
376 if (rt5025_read_reg(bi->client, RT5025_REG_QCHGH_MSB, data, 4) < 0){
377 pr_err("%s: Failed to read QCHG\n", __func__);
379 qh_new = (data[0]<<8) + data[1];
380 ql_new = (data[2]<<8) + data[3];
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;
389 cc_masec = (((qh_old<<16) + ql_old) * 50134) / 10;
393 offset = bi->curr_offset * bi->time_interval;
395 if (cc_masec > offset){
396 cc_masec = (cc_masec - offset) / 1000;
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;
405 cc_masec = (((qh_old<<16) + ql_old) * 5996) / 1000;
409 offset = (bi->curr_offset * bi->time_interval * 37375) / 100000;
412 cc_masec = cc_masec - offset;
415 if (cc_masec < (DEADBAND * bi->time_interval))
419 //pr_info("%d,\n", cc_masec);
421 RTINFO("chg_cc_mAsec: %d\n", cc_masec);
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);
430 static void rt5025_get_dchg_cc(struct rt5025_battery_info *bi)
433 u32 qh_old,ql_old,qh_new,ql_new;
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__);
439 qh_old = (data[0]<<8) + data[1];
440 ql_old = (data[2]<<8) + data[3];
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__);
445 qh_new = (data[0]<<8) + data[1];
446 ql_new = (data[2]<<8) + data[3];
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;
455 cc_masec = (((qh_old<<16) + ql_old) * 50134) / 10;
459 offset = bi->curr_offset * bi->time_interval;
462 cc_masec = (cc_masec + offset) / 1000;
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;
471 cc_masec = (((qh_old<<16) + ql_old) * 5996) / 1000;
475 offset = (bi->curr_offset * bi->time_interval * 37375) / 100000;
477 if (cc_masec > offset){
478 cc_masec = cc_masec - offset;
481 if (cc_masec < (DEADBAND * bi->time_interval))
485 //pr_info("%d,", cc_masec);
487 RTINFO("dchg_cc_mAsec: %d\n", cc_masec);
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);
496 static void rt5025_cycle_count(struct rt5025_battery_info *bi)
498 bi->acc_dchg_cap += bi->dchg_cc;
499 if(bi->acc_dchg_cap >= (bi->dc * 3600)){
501 bi->acc_dchg_cap -= (bi->dc * 3600);
505 static void rt5025_get_irq_flag(struct rt5025_battery_info *bi, u8 flag)
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__);
516 //RTINFO("IRQ_FLG 0x%x\n", bi->irq_flag);
519 static void rt5025_get_timer(struct rt5025_battery_info *bi)
525 if (rt5025_read_reg(bi->client, RT5025_REG_TIMER, data, 2) < 0){
526 pr_err("%s: Failed to read Timer\n", __func__);
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;
534 bi->time_interval = 65536 - bi->pre_gauge_timer + bi->gauge_timer;
537 bi->pre_gauge_timer = bi->gauge_timer;
539 // pr_info("%d,%d,", bi->gauge_timer,bi->time_interval);
541 RTINFO("timer %d , interval %d\n", bi->gauge_timer,bi->time_interval);
545 static void rt5025_alert_setting(struct rt5025_battery_info *bi, alert_type type, bool enable)
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__);
556 data[0] |= IRQ_CTL_BIT_TMX; //Enable max temperature alert
557 bi->max_temp_irq = true;
558 //RTDBG("Enable min temperature alert");
561 data[0] |= IRQ_CTL_BIT_TMN; //Enable min temperature alert
562 bi->min_temp_irq = true;
563 //RTDBG("Enable max temperature alert");
566 data[0] |= IRQ_CTL_BIT_VMX; //Enable max voltage alert
567 bi->max_volt_irq = true;
568 //RTDBG("Enable max voltage alert");
571 data[0] |= IRQ_CTL_BIT_VMN1; //Enable min1 voltage alert
572 bi->min_volt1_irq = true;
573 //RTDBG("Enable min1 voltage alert");
576 data[0] |= IRQ_CTL_BIT_VMN2; //Enable min2 voltage alert
577 bi->min_volt2_irq = true;
578 //RTDBG("Enable min2 voltage alert");
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");
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");
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");
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");
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");
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__);
618 static void rt5025_alert_threshold_init(struct i2c_client *client)
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__);
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__);
646 static void rt5025_alert_init(struct rt5025_battery_info *bi)
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);
655 void rt5025_gauge_irq_handler(struct rt5025_battery_info *bi, u8 irq_flag)
657 rt5025_get_irq_flag(bi, irq_flag);
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;
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;
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;
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;
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));
687 wake_lock(&bi->monitor_wake_lock);
688 schedule_delayed_work(&bi->monitor_work, 0);
690 EXPORT_SYMBOL(rt5025_gauge_irq_handler);
692 static void rt5025_convert_masec_to_permille(struct rt5025_battery_info *bi)
694 bi->permille = bi->rm / 3600 * 1000 / bi->fcc;
695 RTINFO("permille=%d\n", bi->permille);
699 static void rt5025_convert_permille_to_masec(struct rt5025_battery_info *bi)
701 bi->rm = bi->permille * bi->fcc / 1000 * 3600;
705 static void rt5025_init_capacity(struct rt5025_battery_info *bi)
709 int slope, const_term;
710 int delta_y, delta_x;
712 size = ARRAY_SIZE(rt5025_battery_param1);
713 while((bi->vcell < rt5025_battery_param1[i].x) &&
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);
721 slope = delta_y * 1000 / delta_x;
723 const_term = (rt5025_battery_param1[i].y) - ((rt5025_battery_param1[i].x * slope) / 1000);
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;
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;
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);
740 static void rt5025_smooth_soc(struct rt5025_battery_info *bi)
742 if ((bi->internal_status == POWER_SUPPLY_STATUS_CHARGING) &&
746 bi->rm = bi->fcc * bi->soc * 36;
747 rt5025_convert_masec_to_permille(bi);
749 else if ((bi->internal_status == POWER_SUPPLY_STATUS_DISCHARGING) &&
753 bi->rm = bi->fcc * bi->soc * 36;
754 rt5025_convert_masec_to_permille(bi);
758 bi->smooth_flag = false;
759 bi->update_time = NORMAL_POLL;
764 static void rt5025_soc_irreversible(struct rt5025_battery_info *bi)
767 //if (!bi->init_cap){
769 if (bi->internal_status == POWER_SUPPLY_STATUS_CHARGING)
771 if (bi->soc < bi->pre_soc)
772 bi->soc = bi->pre_soc;
774 else if ((bi->internal_status == POWER_SUPPLY_STATUS_DISCHARGING) &&
777 if (bi->soc > bi->pre_soc)
778 bi->soc = bi->pre_soc;
782 bi->init_once = false;
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);
788 static void rt5025_soc_lock(struct rt5025_battery_info *bi)
791 RTINFO("internal status=%d, tp_flag=%d, soc=%d\n", bi->internal_status, bi->tp_flag, bi->soc);
796 else if(bi->internal_status == POWER_SUPPLY_STATUS_CHARGING)
802 else if ((bi->soc < 99) && (bi->tp_flag))
804 bi->update_time = SMOOTH_POLL;
805 bi->smooth_flag = true;
806 rt5025_smooth_soc(bi);
814 if ((bi->soc <= 1) &&
815 (bi->internal_status == POWER_SUPPLY_STATUS_DISCHARGING)){
824 }else if ((bi->soc > 1) &&
825 (bi->internal_status == POWER_SUPPLY_STATUS_DISCHARGING) &&
827 bi->update_time = SMOOTH_POLL;
828 bi->smooth_flag = true;
829 rt5025_smooth_soc(bi);
831 bi->edv_flag = false;
835 static void rt5025_get_soc(struct rt5025_battery_info *bi)
837 if (bi->smooth_flag){
838 bi->smooth_flag = false;
839 bi->update_time = NORMAL_POLL;
841 RTINFO("before rm=%d\n", bi->rm);
842 if ((!bi->tp_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);
851 bi->temp_soc = bi->soc;
852 //pr_info("%d", bi->soc);
854 RTINFO("after rm=%d\n", bi->rm);
855 RTINFO("temp_soc=%d\n", bi->soc);
859 // pr_info("%d,%d,%d,%d,%d", bi->soc,bi->permille,bi->rm,bi->fcc,bi->smooth_flag);
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);
866 static void rt5025_soc_relearn_check(struct rt5025_battery_info *bi)
869 /* if ((bi->vcell >= TP_VOLT) &&
870 (bi->curr <= TP_CURR) &&
871 (bi->status == POWER_SUPPLY_STATUS_CHARGING) &&
874 bi->update_time = TP_POLL;
877 bi->update_time = NORMAL_POLL;
880 if (bi->tp_cnt == TP_TOTAL_COUNT){
883 bi->rm = bi->fcc * 3600;
884 rt5025_convert_masec_to_permille();
885 bi->update_time = NORMAL_POLL;
888 // if EOC happened, the tp_flag should be set 1.
889 if(bi->tp_flag == true)
891 bi->rm = bi->fcc * 3600;
892 rt5025_convert_masec_to_permille(bi);
893 bi->update_time = NORMAL_POLL;
896 if(rt5025_battery_param2[4].x < bi->vcell && bi->vcell <= rt5025_battery_param2[4].x+300)
898 bi->update_time = EDV_POLL;
899 bi->edv_detection = true;
901 else if((bi->vcell >= rt5025_battery_param2[4].x + 300 +EDV_HYS) && (bi->edv_detection == true))
903 bi->update_time = NORMAL_POLL;
904 bi->edv_detection = false;
906 else if((bi->vcell <= rt5025_battery_param2[4].x) && (bi->min_volt2_alert == true))
910 rt5025_convert_masec_to_permille(bi);
911 bi->edv_detection = false;
912 bi->update_time = NORMAL_POLL;
914 else if((bi->vcell > rt5025_battery_param2[4].x + EDV_HYS) && (bi->min_volt2_alert == true))
916 bi->min_volt2_alert = false;
917 bi->edv_flag = false;
921 if (bi->vcell <= EDV_VOLT){
922 if ((bi->curr <= EDV_CURR) ||
923 (bi->vcell <= rt5025_battery_param2[4].x))
929 bi->edv_detection = true;
930 bi->update_time = EDV_POLL;
931 }else if ((bi->vcell > (EDV_VOLT + EDV_HYS)) &&
932 (bi->edv_detection)) {
934 bi->edv_detection = false;
935 bi->edv_flag = false;
936 bi->update_time = NORMAL_POLL;
943 if (bi->edv_cnt == EDV_TOTAL_COUNT){
947 rt5025_convert_masec_to_permille();
948 bi->edv_detection = false;
949 bi->update_time = NORMAL_POLL;
951 if(bi->edv_detection)
953 bi->update_time = EDV_POLL;
955 if (bi->internal_status == POWER_SUPPLY_STATUS_CHARGING)
956 bi->edv_flag = false;
957 else if (bi->status != POWER_SUPPLY_STATUS_FULL)
961 //pr_err("%d,%d,%d,%d,%d",
962 // bi->tp_cnt,bi->tp_flag,bi->edv_detection,bi->edv_cnt,bi->edv_flag);
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);
971 static void rt5025_channel_cc(struct rt5025_battery_info *bi, bool enable)
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__);
980 data[0] = data[0] | 0x80;
982 data[0] = data[0] & 0x7F;
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__);
991 static void rt5025_pretrim(struct rt5025_battery_info *bi)
998 if (rt5025_write_reg(bi->client, 0xF0, data0, 2) < 0){
999 printk(KERN_ERR "%s: failed to write channel\n", __func__);
1002 if (rt5025_write_reg(bi->client, 0xF1, data0, 1) < 0){
1003 printk(KERN_ERR "%s: failed to write channel\n", __func__);
1005 // write trim data D0
1007 if (rt5025_write_reg(bi->client, 0xD0, data0, 1) < 0){
1008 printk(KERN_ERR "%s: failed to write channel\n", __func__);
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__);
1014 if (data1[0] != data0[0])
1015 printk(KERN_ERR "%s: 0xD0 write fail\n", __func__);
1016 // write trim data D1
1018 if (rt5025_write_reg(bi->client, 0xD1, data0, 1) < 0){
1019 printk(KERN_ERR "%s: failed to write channel\n", __func__);
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__);
1025 if (data1[0] != data0[0])
1026 printk(KERN_ERR "%s: 0xD1 write fail\n", __func__);
1027 // write trim data D2
1029 if (rt5025_write_reg(bi->client, 0xD2, data0, 1) < 0){
1030 printk(KERN_ERR "%s: failed to write channel\n", __func__);
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__);
1036 if(data1[0] != data0[0])
1037 printk(KERN_ERR "%s: 0xD2 write fail\n", __func__);
1038 // write trim data D3
1040 if (rt5025_write_reg(bi->client, 0xD3, data0, 1) < 0){
1041 printk(KERN_ERR "%s: failed to write channel\n", __func__);
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__);
1047 if(data1[0] != data0[0])
1048 printk(KERN_ERR "%s: 0xD3 write fail\n", __func__);
1049 // write trim data D4
1051 if (rt5025_write_reg(bi->client, 0xD4, data0, 1) < 0){
1052 printk(KERN_ERR "%s: failed to write channel\n", __func__);
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__);
1058 if (data1[0] != data0[0])
1059 printk(KERN_ERR "%s: 0xD4 write fail\n", __func__);
1063 if (rt5025_write_reg(bi->client, 0xF0, data0, 2) < 0){
1064 printk(KERN_ERR "%s: failed to write channel\n", __func__);
1069 static int rt5025_battery_parameter_backup(struct rt5025_battery_info *bi)
1073 //backup fcc_aging, rm, cycle_count, acc_dchg_cap
1075 data[0] = (bi->fcc_aging>>8)&0xff;
1076 data[1] = (bi->fcc_aging)&0xff;
1077 rt5025_write_reg(bi->client, 0x21, data, 2);
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);
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);
1091 data[0] = (bi->cycle_cnt)&0xff;
1092 rt5025_write_reg(bi->client, 0x2B, data, 1);
1096 static int rt5025_battery_parameter_restore(struct rt5025_battery_info *bi)
1100 //restore fcc_aging, rm ,cycle_count, acc_dchg_cap
1102 rt5025_read_reg(bi->client, 0x21, data, 2);
1103 bi->fcc = bi->fcc_aging = data[0]<<8 | data[1];
1105 rt5025_read_reg(bi->client, 0x23, data, 4);
1106 bi->rm = data[0]<<24 | data[1]<<16 | data[2]<<8 | data[3];
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];
1111 rt5025_read_reg(bi->client, 0x2B, data, 1);
1112 bi->cycle_cnt = data[0];
1118 // return value; 1-> initialized, 0-> no initial value
1119 static int rt5025_battery_parameter_initcheck(struct rt5025_battery_info *bi)
1125 if (rt5025_read_reg(bi->client, 0x21, data, 2) < 0)
1127 pr_err("%s: check initial value error\n", __func__);
1131 value = data[1]<<8 | data[0];
1135 RTINFO("initial check = %d\n", ret);
1140 static void rt5025_register_init(struct rt5025_battery_info *bi)
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__);
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__);
1153 /* set the alert threshold value */
1154 irq_thres[MINVOLT2] = VALRTMIN2_VALUE;
1155 irq_thres[VOLT_RLS] = VRLS_VALUE;
1157 bi->chg_cc_unuse = 0;
1158 bi->dchg_cc_unuse = 0;
1159 bi->pre_gauge_timer = 0;
1161 bi->status = bi->internal_status = POWER_SUPPLY_STATUS_DISCHARGING;
1162 bi->health = POWER_SUPPLY_HEALTH_GOOD;
1164 bi->init_cap = true;
1165 bi->avg_flag = true;
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;
1173 bi->edv_flag = false;
1174 bi->edv_detection = false;
1175 bi->init_once = true;
1178 bi->tp_flag = false;
1180 bi->acc_dchg_cap = 0;
1183 // if has initial data, rewrite to the stored data
1184 if (rt5025_battery_parameter_initcheck(bi))
1186 bi->init_cap = false;
1187 rt5025_battery_parameter_restore(bi);
1188 bi->soc = bi->rm/36/bi->fcc_aging;
1191 bi->update_time = NORMAL_POLL;
1192 bi->device_suspend = false;
1193 RTINFO("register initialized\n");
1196 static void rt5025_soc_aging(struct rt5025_battery_info *bi)
1198 if (bi->cycle_cnt >= rt5025_battery_param2[3].x)
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;
1204 RTINFO("fcc_aging=%d, rm=%d, cycle_cnt=%d\n", bi->fcc_aging, bi->rm, bi->cycle_cnt);
1207 static void rt5025_temp_comp(struct rt5025_battery_info *bi)
1211 int slope, const_term;
1212 int delta_y, delta_x;
1215 while((bi->ext_temp < rt5025_battery_param2[i].x) &&
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);
1223 slope = delta_y * 1000 / delta_x;
1225 const_term = (rt5025_battery_param2[i].y) - ((rt5025_battery_param2[i].x * slope) / 1000);
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;
1232 bi->tempcmp = (bi->ext_temp * slope) / 1000 + const_term;
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);
1245 static void rt5025_soc_temp_comp(struct rt5025_battery_info *bi)
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;
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;
1277 if((bi->temp_range_0_5 == true) && (bi->range_0_5_done == false))
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;
1290 else if((bi->temp_range_5_10 == true) && (bi->range_5_10_done == false))
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;
1303 else if((bi->temp_range_10_15 == true) && (bi->range_10_15_done == false))
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;
1316 else if((bi->temp_range_15_20 == true) && (bi->range_15_20_done == false))
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;
1329 else if((bi->temp_range_20_30 == true) && (bi->range_20_30_done == false))
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;
1343 else if((bi->temp_range_30_35 == true) && (bi->range_30_35_done == false))
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;
1356 else if((bi->temp_range_35_40 == true) && (bi->range_35_40_done == false))
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;
1369 else if((bi->temp_range_40_45 == true) && (bi->range_40_45_done == false))
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;
1382 else if((bi->temp_range_45_50 == true) && (bi->range_45_50_done == false))
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;
1395 RTINFO("soc->%d--\n", bi->soc);
1398 static void rt5025_update(struct rt5025_battery_info *bi)
1400 /* Update voltage */
1401 rt5025_get_vcell(bi);
1402 /* Update current */
1403 rt5025_get_current(bi);
1405 /* Update external temperature */
1406 rt5025_get_external_temp(bi);
1408 rt5025_get_timer(bi);
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 */
1419 rt5025_init_capacity(bi);
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");
1426 rt5025_soc_relearn_check(bi);
1428 rt5025_soc_temp_comp(bi);
1432 /* SOC Control Process */
1433 rt5025_soc_lock(bi);
1434 rt5025_soc_irreversible(bi);
1436 /* Update RTTF or RTTE */
1437 //rt5025_run_time(bi);
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);
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);
1459 if ((bi->min_volt2_irq == false) &&
1460 (bi->vcell > (rt5025_battery_param2[4].x + EDV_HYS))){
1461 rt5025_alert_setting(bi,MINVOLT2,true);
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);
1469 pr_info("[RT5025] update_time=%d\n",bi->update_time);
1474 static void rt5025_update_work(struct work_struct *work)
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;
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);
1488 wake_unlock(&bi->monitor_wake_lock);
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,
1502 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
1503 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
1507 static int rt5025_battery_suspend(struct platform_device *pdev, pm_message_t state)
1509 struct rt5025_battery_info *bi = platform_get_drvdata(pdev);
1510 //bi->last_event = ktime_get();
1511 bi->last_event = current_kernel_time();
1513 //cy add for battery parameter backup
1514 rt5025_battery_parameter_backup(bi);
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);
1525 static int rt5025_battery_resume(struct platform_device *pdev)
1527 struct rt5025_battery_info *bi = platform_get_drvdata(pdev);
1529 //struct timespec now = current_kernel_time();
1530 //struct timeval tv;
1531 //long time_interval;
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);
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);
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;
1550 static int rt5025_battery_remove(struct platform_device *pdev)
1552 struct rt5025_battery_info *bi = platform_get_drvdata(pdev);
1554 power_supply_unregister(&bi->battery);
1555 cancel_delayed_work(&bi->monitor_work);
1556 wake_lock_destroy(&bi->monitor_wake_lock);
1561 static int rt5025_battery_probe(struct platform_device *pdev)
1563 struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
1564 struct rt5025_battery_info *bi;
1567 bi = kzalloc(sizeof(*bi), GFP_KERNEL);
1571 bi->client = chip->i2c;
1575 bi->last_poll = alarm_get_elapsed_realtime();
1576 alarm_init(&bi->wakeup_alarm, ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP,
1577 rt5025_gauge_alarm);
1579 INIT_DELAYED_WORK(&bi->monitor_work, rt5025_update_work);
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");
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);
1593 /* register callback functions */
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;
1602 rt5025_register_gauge_callbacks(&chip->cb);
1605 platform_set_drvdata(pdev, bi);
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);
1613 ret = power_supply_register(&pdev->dev, &bi->battery);
1615 printk(KERN_ERR "[RT5025] power supply register failed\n");
1620 wake_lock(&bi->monitor_wake_lock);
1621 #if RT5025_TEST_WAKE_LOCK
1622 wake_lock(&bi->test_wake_lock);
1624 schedule_delayed_work(&bi->monitor_work, msecs_to_jiffies(INIT_POLL*MSEC_PER_SEC));
1625 chip->battery_info = bi;
1627 pr_info("rt5025-battery driver is successfully loaded\n");
1632 wake_lock_destroy(&bi->monitor_wake_lock);
1638 static void rt5025_battery_shutdown(struct platform_device *pdev)
1640 struct rt5025_battery_info *bi = platform_get_drvdata(pdev);
1643 rt5025_battery_parameter_backup(bi);
1646 static struct platform_driver rt5025_battery_driver =
1649 .name = RT5025_DEVICE_NAME "-battery",
1650 .owner = THIS_MODULE,
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,
1659 static int __init rt5025_battery_init(void)
1661 return platform_driver_register(&rt5025_battery_driver);
1663 module_init(rt5025_battery_init);
1665 static void __exit rt5025_battery_exit(void)
1667 platform_driver_unregister(&rt5025_battery_driver);
1669 module_exit(rt5025_battery_exit);
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");