7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/slab.h>
17 #include <linux/wakelock.h>
18 #include <linux/workqueue.h>
19 #include <linux/jiffies.h>
20 #include <linux/timer.h>
21 #include <linux/android_alarm.h>
22 #include <linux/mfd/rt5025.h>
23 #include <linux/power/rt5025-gauge.h>
25 #define RT5025_REG_IRQ_CTL 0x50
26 #define RT5025_REG_IRQ_FLAG 0x51
27 #define RT5025_REG_VALRT_MAXTH 0x53
28 #define RT5025_REG_VALRT_MIN1TH 0x54
29 #define RT5025_REG_VALRT_MIN2TH 0x55
30 #define RT5025_REG_TALRT_MAXTH 0x56
31 #define RT5025_REG_TALRT_MINTH 0x57
32 #define RT5025_REG_VCELL_MSB 0x58
33 #define RT5025_REG_VCELL_LSB 0x59
34 #define RT5025_REG_INT_TEMPERATUE_MSB 0x5B
35 #define RT5025_REG_INT_TEMPERATUE_LSB 0x5C
36 #define RT5025_REG_EXT_TEMPERATUE_MSB 0x5E
37 #define RT5025_REG_EXT_TEMPERATUE_LSB 0x5F
38 #define RT5025_REG_TIMER 0x60
39 #define RT5025_REG_CHANNEL_MSB 0x62
40 #define RT5025_REG_CHANNEL_LSB 0x63
41 #define RT5025_REG_CURRENT_MSB 0x76
42 #define RT5025_REG_CURRENT_LSB 0x77
43 #define RT5025_REG_QCHGH_MSB 0x78
44 #define RT5025_REG_QCHGH_LSB 0x79
45 #define RT5025_REG_QCHGL_MSB 0x7A
46 #define RT5025_REG_QCHGL_LSB 0x7B
47 #define RT5025_REG_QDCHGH_MSB 0x7C
48 #define RT5025_REG_QDCHGH_LSB 0x7D
49 #define RT5025_REG_QDCHGL_MSB 0x7E
50 #define RT5025_REG_QDCHGL_LSB 0x7F
52 #define IRQ_CTL_BIT_TMX (1 << 5)
53 #define IRQ_CTL_BIT_TMN (1 << 4)
54 #define IRQ_CTL_BIT_VMX (1 << 2)
55 #define IRQ_CTL_BIT_VMN1 (1 << 1)
56 #define IRQ_CTL_BIT_VMN2 (1 << 0)
58 #define IRQ_FLG_BIT_TMX (1 << 5)
59 #define IRQ_FLG_BIT_TMN (1 << 4)
60 #define IRQ_FLG_BIT_VMX (1 << 2)
61 #define IRQ_FLG_BIT_VMN1 (1 << 1)
62 #define IRQ_FLG_BIT_VMN2 (1 << 0)
64 #define CHANNEL_H_BIT_CLRQDCHG (1 << 7)
65 #define CHANNEL_H_BIT_CLRQCHG (1 << 6)
67 #define CHANNEL_L_BIT_CADC_EN (1 << 7)
68 #define CHANNEL_L_BIT_INTEMPCH (1 << 6)
69 #define CHANNEL_L_BIT_AINCH (1 << 2)
70 #define CHANNEL_L_BIT_VBATSCH (1 << 1)
71 #define CHANNEL_L_BIT_VADC_EN (1 << 0)
73 #define NORMAL_POLL 10 /* 10 sec */
74 #define SUSPEND_POLL (30*60) /* 30 min */
76 #define HIGH_TEMP_THRES 650
77 #define HIGH_TEMP_RECOVER 430
78 #define LOW_TEMP_THRES (-30)
79 #define LOW_TEMP_RECOVER 0
80 #define TEMP_ABNORMAL_COUNT 3
82 #define TALRTMAX_VALUE 0x38 //65.39'C 0x9
83 #define TALRTMIN_VALUE 0x11 //-18.75'C 0x17
84 #define VALRTMAX_VALUE 0xDC //4297mV
85 #define VALRTMIN1_VALUE 0xB8 //3600mV
86 #define VALRTMIN2_VALUE 0x99 //3000mV
87 #define TRLS_VALUE 55 //5'C
88 #define VRLS_VALUE 100 //100mV
90 #define IRQ_THRES_UNIT 1953
92 struct rt5025_gauge_chip {
93 struct i2c_client *client;
94 struct rt5025_power_info *info;
95 struct rt5025_gauge_callbacks cb;
97 struct power_supply battery;
99 struct delayed_work monitor_work;
100 struct wake_lock monitor_wake_lock;
101 struct alarm wakeup_alarm;
106 /* battery voltage */
108 /* battery current */
110 /* battery current offset */
114 /* battery external temperature */
116 /* charge coulomb counter */
119 /* discharge coulomb counter */
122 /* battery capacity */
134 /* max voltage IRQ flag */
136 /* min voltage1 IRQ flag */
138 /* min voltage2 IRQ flag */
140 /* max temperature IRQ flag */
142 /* min temperature IRQ flag */
150 struct rt5025_gauge_chip *chip;
151 u8 irq_thres[LAST_TYPE];
153 void rt5025_set_status(int status)
155 chip->status = status;
158 static int rt5025_read_reg(struct i2c_client *client,
159 u8 reg, u8 *data, u8 len)
161 struct i2c_adapter *adap = client->adapter;
162 struct i2c_msg msgs[2];
165 msgs[0].addr = client->addr;
166 msgs[0].flags = client->flags;
169 msgs[0].scl_rate = 200*1000;
171 msgs[1].addr = client->addr;
172 msgs[1].flags = client->flags | I2C_M_RD;
175 msgs[1].scl_rate = 200*1000;
177 ret = i2c_transfer(adap, msgs, 2);
179 return (ret == 2)? len : ret;
182 static int rt5025_write_reg(struct i2c_client *client,
183 u8 reg, u8 *data, u8 len)
185 struct i2c_adapter *adap = client->adapter;
188 char *tx_buf = (char *)kmalloc(len + 1, GFP_KERNEL);
193 memcpy(tx_buf+1, data, len);
195 msg.addr = client->addr;
196 msg.flags = client->flags;
198 msg.buf = (char *)tx_buf;
199 msg.scl_rate = 200*1000;
200 ret = i2c_transfer(adap, &msg, 1);
202 return (ret == 1) ? len : ret;
205 static void rt5025_gauge_alarm(struct alarm *alarm)
207 wake_lock(&chip->monitor_wake_lock);
208 schedule_delayed_work(&chip->monitor_work, 0);
211 static void rt5025_program_alarm(int seconds)
213 ktime_t low_interval = ktime_set(seconds, 0);
214 ktime_t slack = ktime_set(20, 0);
217 next = ktime_add(chip->last_poll, low_interval);
218 alarm_start_range(&chip->wakeup_alarm, next, ktime_add(next, slack));
221 static int rt5025_get_property(struct power_supply *psy,
222 enum power_supply_property psp,
223 union power_supply_propval *val)
226 case POWER_SUPPLY_PROP_STATUS:
227 val->intval = chip->status;
229 case POWER_SUPPLY_PROP_HEALTH:
230 val->intval = chip->health;
232 case POWER_SUPPLY_PROP_PRESENT:
233 val->intval = chip->online;
235 case POWER_SUPPLY_PROP_TEMP:
236 val->intval = chip->ext_temp;
238 case POWER_SUPPLY_PROP_ONLINE:
241 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
242 val->intval = chip->vcell * 1000; //uV
244 case POWER_SUPPLY_PROP_CURRENT_NOW:
245 val->intval = chip->curr * 1000; //uA
247 case POWER_SUPPLY_PROP_CAPACITY:
248 val->intval = chip->soc;
249 if (val->intval > 100)
252 case POWER_SUPPLY_PROP_TECHNOLOGY:
253 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
261 static void rt5025_get_vcell(struct i2c_client *client)
265 if (rt5025_read_reg(client, RT5025_REG_VCELL_MSB, data, 2) < 0){
266 printk(KERN_ERR "%s: Failed to read Voltage\n", __func__);
269 chip->vcell = ((data[0] << 8) + data[1]) * 61 / 100;
270 chip->curr_offset = (15444 * chip->vcell - 27444000) / 10000;
272 RTINFO("[RT5025] vcell: %d, offset: %d\n", chip->vcell, chip->curr_offset);
275 static void rt5025_get_current(struct i2c_client *client)
281 if (rt5025_read_reg(client, RT5025_REG_CURRENT_MSB, data, 2) < 0) {
282 printk(KERN_ERR "%s: Failed to read CURRENT\n", __func__);
285 temp = (data[0]<<8) | data[1];
286 if (data[0] & (1 << 7)) {
288 temp = (((temp & 0x7FFF) * 3125) / 10 + chip->curr_offset) / 1000;
290 temp = ((temp * 3125) / 10 - chip->curr_offset) / 1000;
296 RTINFO("[RT5025] current: %d\n", chip->curr);
299 static void rt5025_get_external_temp(struct i2c_client *client)
304 if (rt5025_read_reg(client, RT5025_REG_EXT_TEMPERATUE_MSB, data, 2) < 0) {
305 printk(KERN_ERR "%s: Failed to read TEMPERATURE\n", __func__);
307 chip->ain_volt = (data[0] * 256 + data[1]) * 61 / 100;
308 /// Check battery present
309 if (chip->ain_volt < 1150)
312 chip->online = false;
313 temp = (chip->ain_volt * (-91738) + 81521000) / 100000;
314 chip->ext_temp = (int)temp;
316 if (chip->ext_temp >= HIGH_TEMP_THRES) {
317 if (chip->health != POWER_SUPPLY_HEALTH_OVERHEAT)
318 chip->temp_high_cnt++;
319 } else if (chip->ext_temp <= HIGH_TEMP_RECOVER && chip->ext_temp >= LOW_TEMP_RECOVER) {
320 if (chip->health == POWER_SUPPLY_HEALTH_OVERHEAT ||
321 chip->health == POWER_SUPPLY_HEALTH_COLD)
322 chip->temp_recover_cnt++;
323 } else if (chip->ext_temp <= LOW_TEMP_THRES) {
324 if (chip->health != POWER_SUPPLY_HEALTH_COLD)
325 chip->temp_low_cnt++;
327 chip->temp_high_cnt = 0;
328 chip->temp_low_cnt = 0;
329 chip->temp_recover_cnt = 0;
332 if (chip->temp_high_cnt >= TEMP_ABNORMAL_COUNT) {
333 chip->health = POWER_SUPPLY_HEALTH_OVERHEAT;
334 chip->temp_high_cnt = 0;
335 } else if (chip->temp_low_cnt >= TEMP_ABNORMAL_COUNT) {
336 chip->health = POWER_SUPPLY_HEALTH_COLD;
337 chip->temp_low_cnt = 0;
338 } else if (chip->temp_recover_cnt >= TEMP_ABNORMAL_COUNT) {
339 chip->health = POWER_SUPPLY_HEALTH_GOOD;
340 chip->temp_recover_cnt = 0;
342 RTINFO("[RT5025] external temperature: %d\n", chip->ext_temp);
345 static void rt5025_clear_cc(operation_mode mode)
349 if (rt5025_read_reg(chip->client, RT5025_REG_CHANNEL_MSB, data, 2) < 0){
350 printk(KERN_ERR "%s: failed to read channel\n", __func__);
354 data[0] = data[0] | CHANNEL_H_BIT_CLRQCHG;
356 data[0] = data[0] | CHANNEL_H_BIT_CLRQDCHG;
358 if (rt5025_write_reg(chip->client, RT5025_REG_CHANNEL_MSB, data, 2) < 0){
359 printk(KERN_ERR "%s: failed to write channel\n", __func__);
363 static void rt5025_get_chg_cc(struct i2c_client *client)
366 u32 qh_old,ql_old,qh_new,ql_new;
369 if (rt5025_read_reg(client, RT5025_REG_QCHGH_MSB, data, 4) < 0){
370 printk(KERN_ERR "%s: Failed to read QCHG\n", __func__);
372 qh_old = (data[0]<<8) + data[1];
373 ql_old = (data[2]<<8) + data[3];
375 if (rt5025_read_reg(client, RT5025_REG_QCHGH_MSB, data, 4) < 0){
376 printk(KERN_ERR "%s: Failed to read QCHG\n", __func__);
378 qh_new = (data[0]<<8) + data[1];
379 ql_new = (data[2]<<8) + data[3];
381 if (qh_new > qh_old){
382 cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
383 }else if (qh_new == qh_old){
384 if (ql_new >= ql_old){
385 cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
387 cc_masec = (((qh_old<<16) + ql_old) * 50134) / 10;
391 offset = chip->curr_offset * chip->time_interval;
394 cc_masec = (cc_masec - offset) / 1000;
397 RTINFO("[RT5025] chg_cc_mAsec: %d\n", cc_masec);
399 chip->chg_cc = (cc_masec + chip->chg_cc_unuse) / 3600;
400 chip->chg_cc_unuse = (cc_masec + chip->chg_cc_unuse) % 3600;
401 RTINFO("[RT5025] chg_cc_mAH: %d\n", chip->chg_cc);
402 rt5025_clear_cc(CHG);
405 static void rt5025_get_dchg_cc(struct i2c_client *client)
408 u32 qh_old,ql_old,qh_new,ql_new;
411 if (rt5025_read_reg(client, RT5025_REG_QDCHGH_MSB, data, 4) < 0){
412 printk(KERN_ERR "%s: Failed to read QDCHG\n", __func__);
414 qh_old = (data[0]<<8) + data[1];
415 ql_old = (data[2]<<8) + data[3];
417 if (rt5025_read_reg(client, RT5025_REG_QDCHGH_MSB, data, 4) < 0){
418 printk(KERN_ERR "%s: Failed to read QDCHG\n", __func__);
420 qh_new = (data[0]<<8) + data[1];
421 ql_new = (data[2]<<8) + data[3];
423 if (qh_new > qh_old){
424 cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
425 }else if (qh_new == qh_old){
426 if (ql_new >= ql_old){
427 cc_masec = (((qh_new<<16) + ql_new) * 50134) / 10;
429 cc_masec = (((qh_old<<16) + ql_old) * 50134) / 10;
433 offset = chip->curr_offset * chip->time_interval;
436 cc_masec = (cc_masec - offset) / 1000;
439 RTINFO("[RT5025] dchg_cc_mAsec: %d\n", cc_masec);
441 chip->dchg_cc = (cc_masec + chip->dchg_cc_unuse) / 3600;
442 chip->dchg_cc_unuse = (cc_masec + chip->dchg_cc_unuse) % 3600;
443 RTINFO("[RT5025] dchg_cc_mAH: %d\n", chip->dchg_cc);
444 rt5025_clear_cc(DCHG);
447 static void rt5025_get_irq_flag(struct i2c_client *client)
451 if (rt5025_read_reg(client, RT5025_REG_IRQ_FLAG, data, 1) < 0){
452 printk(KERN_ERR "%s: Failed to read irq_flag\n", __func__);
455 chip->irq_flag = data[0];
456 RTINFO("[RT5025] IRQ_FLG 0x%x\n", chip->irq_flag);
459 static void rt5025_get_timer(struct i2c_client *client)
464 if (rt5025_read_reg(client, RT5025_REG_TIMER, data, 2) < 0){
465 printk(KERN_ERR "%s: Failed to read Timer\n", __func__);
468 gauge_timer = (data[0] << 8) + data[1];
469 if (gauge_timer > chip->pre_gauge_timer)
470 chip->time_interval = gauge_timer - chip->pre_gauge_timer;
472 chip->time_interval = 65536 - chip->pre_gauge_timer + gauge_timer;
474 chip->pre_gauge_timer = gauge_timer;
475 RTINFO("[RT5025] timer %d , interval %d\n", gauge_timer,chip->time_interval);
478 static void rt5025_get_soc(struct i2c_client *client)
483 static void rt5025_channel_cc(bool enable)
487 if (rt5025_read_reg(chip->client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
488 printk(KERN_ERR "%s: failed to read channel\n", __func__);
492 data[0] = data[0] | 0x80;
494 data[0] = data[0] & 0x7F;
497 if (rt5025_write_reg(chip->client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
498 printk(KERN_ERR "%s: failed to write channel\n", __func__);
502 static void rt5025_register_init(struct i2c_client *client)
506 /* enable the channel of current,qc,ain,vbat and vadc */
507 if (rt5025_read_reg(client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
508 printk("%s: failed to read channel\n", __func__);
511 CHANNEL_L_BIT_CADC_EN |
512 CHANNEL_L_BIT_AINCH |
513 CHANNEL_L_BIT_VBATSCH |
514 CHANNEL_L_BIT_VADC_EN;
515 if (rt5025_write_reg(client, RT5025_REG_CHANNEL_LSB, data, 1) < 0){
516 printk("%s: failed to write channel\n", __func__);
518 /* set the alert threshold value */
519 irq_thres[MAXTEMP] = TALRTMAX_VALUE;
520 irq_thres[MINTEMP] = TALRTMIN_VALUE;
521 irq_thres[MAXVOLT] = VALRTMAX_VALUE;
522 irq_thres[MINVOLT1] = VALRTMIN1_VALUE;
523 irq_thres[MINVOLT2] = VALRTMIN2_VALUE;
524 irq_thres[TEMP_RLS] = TRLS_VALUE;
525 irq_thres[VOLT_RLS] = VRLS_VALUE;
527 chip->chg_cc_unuse = 0;
528 chip->dchg_cc_unuse = 0;
529 chip->pre_gauge_timer = 0;
531 chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
532 chip->health = POWER_SUPPLY_HEALTH_GOOD;
533 RTINFO("[RT5025] register initialized\n");
536 static void rt5025_alert_setting(alert_type type, bool enable)
540 if (rt5025_read_reg(chip->client, RT5025_REG_IRQ_CTL, data, 1) < 0){
541 printk(KERN_ERR "%s: Failed to read CONFIG\n", __func__);
547 data[0] |= IRQ_CTL_BIT_TMX; //Enable max temperature alert
548 chip->max_temp_irq = true;
549 RTINFO("Enable min temperature alert");
552 data[0] |= IRQ_CTL_BIT_TMN; //Enable min temperature alert
553 chip->min_temp_irq = true;
554 RTINFO("Enable max temperature alert");
557 data[0] |= IRQ_CTL_BIT_VMX; //Enable max voltage alert
558 chip->max_volt_irq = true;
559 RTINFO("Enable max voltage alert");
562 data[0] |= IRQ_CTL_BIT_VMN1; //Enable min1 voltage alert
563 chip->min_volt1_irq = true;
564 RTINFO("Enable min1 voltage alert");
567 data[0] |= IRQ_CTL_BIT_VMN2; //Enable min2 voltage alert
568 chip->min_volt2_irq = true;
569 RTINFO("Enable min2 voltage alert");
577 data[0] = data[0] &~ IRQ_CTL_BIT_TMX; //Disable max temperature alert
578 chip->max_temp_irq = false;
579 RTINFO("Disable min temperature alert");
582 data[0] = data[0] &~ IRQ_CTL_BIT_TMN; //Disable min temperature alert
583 chip->min_temp_irq = false;
584 RTINFO("Disable max temperature alert");
587 data[0] = data[0] &~ IRQ_CTL_BIT_VMX; //Disable max voltage alert
588 chip->max_volt_irq = false;
589 RTINFO("Disable max voltage alert");
592 data[0] = data[0] &~ IRQ_CTL_BIT_VMN1; //Disable min1 voltage alert
593 chip->min_volt1_irq = false;
594 RTINFO("Disable min1 voltage alert");
597 data[0] = data[0] &~ IRQ_CTL_BIT_VMN2; //Disable min2 voltage alert
598 chip->min_volt2_irq = false;
599 RTINFO("Disable min2 voltage alert");
605 if (rt5025_write_reg(chip->client, RT5025_REG_IRQ_CTL, data, 1) < 0)
606 printk(KERN_ERR "%s: failed to write IRQ control\n", __func__);
608 static void rt5025_alert_threshold_init(struct i2c_client *client)
612 /* TALRT MAX threshold setting */
613 data[0] = irq_thres[MAXTEMP];
614 if (rt5025_write_reg(client, RT5025_REG_TALRT_MAXTH, data, 1) < 0)
615 printk(KERN_ERR "%s: failed to write TALRT MAX threshold\n", __func__);
616 /* TALRT MIN threshold setting */
617 data[0] = irq_thres[MINTEMP];
618 if (rt5025_write_reg(client, RT5025_REG_TALRT_MINTH, data, 1) < 0)
619 printk(KERN_ERR "%s: failed to write TALRT MIN threshold\n", __func__);
620 /* VALRT MAX threshold setting */
621 data[0] = irq_thres[MAXVOLT];
622 if (rt5025_write_reg(client, RT5025_REG_VALRT_MAXTH, data, 1) < 0)
623 printk(KERN_ERR "%s: failed to write VALRT MAX threshold\n", __func__);
624 /* VALRT MIN1 threshold setting */
625 data[0] = irq_thres[MINVOLT1];
626 if (rt5025_write_reg(client, RT5025_REG_VALRT_MIN1TH, data, 1) < 0)
627 printk(KERN_ERR "%s: failed to write VALRT MIN1 threshold\n", __func__);
628 /* VALRT MIN2 threshold setting */
629 data[0] = irq_thres[MINVOLT2];
630 if (rt5025_write_reg(client, RT5025_REG_VALRT_MIN2TH, data, 1) < 0)
631 printk(KERN_ERR "%s: failed to write VALRT MIN2 threshold\n", __func__);
634 static void rt5025_alert_init(struct i2c_client *client)
636 /* Set RT5025 gauge alert configuration */
637 rt5025_alert_threshold_init(client);
638 /* Enable gauge alert function */
639 rt5025_alert_setting(MAXTEMP,true);
640 rt5025_alert_setting(MINTEMP,true);
641 rt5025_alert_setting(MAXVOLT,true);
642 rt5025_alert_setting(MINVOLT1,true);
643 rt5025_alert_setting(MINVOLT2,true);
646 void rt5025_irq_handler(void)
648 rt5025_get_irq_flag(chip->client);
650 if ((chip->irq_flag) & IRQ_FLG_BIT_TMX){
651 printk(KERN_INFO "[RT5025]: Min temperature IRQ received\n");
652 rt5025_alert_setting(MAXTEMP,false);
653 chip->max_temp_irq = false;
655 if ((chip->irq_flag) & IRQ_FLG_BIT_TMN){
656 printk(KERN_INFO "[RT5025]: Max temperature IRQ received\n");
657 rt5025_alert_setting(MINTEMP,false);
658 chip->min_temp_irq = false;
660 if ((chip->irq_flag) & IRQ_FLG_BIT_VMX){
661 printk(KERN_INFO "[RT5025]: Max voltage IRQ received\n");
662 rt5025_alert_setting(MAXVOLT,false);
663 chip->max_volt_irq = false;
665 if ((chip->irq_flag) & IRQ_FLG_BIT_VMN1){
666 printk(KERN_INFO "[RT5025]: Min voltage1 IRQ received\n");
667 rt5025_alert_setting(MINVOLT1,false);
668 chip->min_volt1_irq = false;
670 if ((chip->irq_flag) & IRQ_FLG_BIT_VMN2){
671 printk(KERN_INFO "[RT5025]: Min voltage2 IRQ received\n");
672 rt5025_alert_setting(MINVOLT2,false);
673 chip->min_volt2_irq = false;
676 wake_lock(&chip->monitor_wake_lock);
677 schedule_delayed_work(&chip->monitor_work, 0);
680 static void rt5025_update(struct i2c_client *client)
683 rt5025_get_vcell(client);
685 rt5025_get_current(client);
686 /* Update external temperature */
687 rt5025_get_external_temp(client);
689 rt5025_get_timer(client);
691 rt5025_get_chg_cc(client);
693 rt5025_get_dchg_cc(client);
695 rt5025_get_soc(client);
697 if ((chip->max_temp_irq == false) &&
698 (((irq_thres[MAXTEMP] * IRQ_THRES_UNIT) / 100 - chip->ain_volt) > irq_thres[TEMP_RLS])){
699 rt5025_alert_setting(MAXTEMP,true);
700 }else if ((chip->min_temp_irq == false) &&
701 ((chip->ain_volt - (irq_thres[MINTEMP] * IRQ_THRES_UNIT) / 100) > irq_thres[TEMP_RLS])){
702 rt5025_alert_setting(MINTEMP,true);
703 }else if ((chip->max_volt_irq == false) &&
704 ((((irq_thres[MAXVOLT] * IRQ_THRES_UNIT) / 100) - chip->vcell) > irq_thres[VOLT_RLS])){
705 rt5025_alert_setting(MAXVOLT,true);
706 }else if ((chip->min_volt1_irq == false) &&
707 ((chip->vcell - ((irq_thres[MINVOLT1] * IRQ_THRES_UNIT) / 100)) > irq_thres[VOLT_RLS])){
708 rt5025_alert_setting(MINVOLT1,true);
709 }else if ((chip->min_volt2_irq == false) &&
710 ((chip->vcell - ((irq_thres[MINVOLT2] * IRQ_THRES_UNIT) / 100)) > irq_thres[VOLT_RLS])){
711 rt5025_alert_setting(MINVOLT2,true);
715 static void rt5025_update_work(struct work_struct *work)
719 rt5025_update(chip->client);
721 /* Update data to framework */
722 power_supply_changed(&chip->battery);
724 /* prevent suspend before starting the alarm */
725 local_irq_save(flags);
726 chip->last_poll = alarm_get_elapsed_realtime();
727 rt5025_program_alarm(NORMAL_POLL);
728 local_irq_restore(flags);
730 wake_unlock(&chip->monitor_wake_lock);
733 static enum power_supply_property rt5025_battery_props[] = {
734 POWER_SUPPLY_PROP_STATUS,
735 POWER_SUPPLY_PROP_HEALTH,
736 POWER_SUPPLY_PROP_PRESENT,
737 POWER_SUPPLY_PROP_TEMP,
738 POWER_SUPPLY_PROP_ONLINE,
739 POWER_SUPPLY_PROP_VOLTAGE_NOW,
740 POWER_SUPPLY_PROP_CURRENT_NOW,
741 POWER_SUPPLY_PROP_CAPACITY,
742 POWER_SUPPLY_PROP_TECHNOLOGY,
745 void rt5025_gauge_suspend(void)
747 rt5025_channel_cc(false);
748 cancel_delayed_work(&chip->monitor_work);
753 void rt5025_gauge_resume(void)
755 rt5025_channel_cc(true);
756 wake_lock(&chip->monitor_wake_lock);
757 schedule_delayed_work(&chip->monitor_work, 0);
761 void rt5025_gauge_remove(void)
763 chip->info->event_callback = NULL;
764 power_supply_unregister(&chip->battery);
765 cancel_delayed_work(&chip->monitor_work);
766 wake_lock_destroy(&chip->monitor_wake_lock);
770 int rt5025_gauge_init(struct rt5025_power_info *info)
773 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
777 chip->client = info->i2c;
779 chip->battery.name = "rt5025-battery";
780 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
781 chip->battery.get_property = rt5025_get_property;
782 chip->battery.properties = rt5025_battery_props;
783 chip->battery.num_properties = ARRAY_SIZE(rt5025_battery_props);
785 ret = power_supply_register(info->dev, &chip->battery);
787 printk(KERN_ERR "[RT5025] power supply register failed\n");
791 chip->last_poll = alarm_get_elapsed_realtime();
792 alarm_init(&chip->wakeup_alarm, ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP,
795 INIT_DELAYED_WORK(&chip->monitor_work, rt5025_update_work);
797 wake_lock_init(&chip->monitor_wake_lock, WAKE_LOCK_SUSPEND,
798 "rt-battery-monitor");
800 rt5025_register_init(info->i2c);
802 /* enable gauge IRQ */
803 rt5025_alert_init(info->i2c);
805 /* register callback functions */
806 chip->cb.rt5025_gauge_irq_handler = rt5025_irq_handler;
807 chip->cb.rt5025_gauge_set_status = rt5025_set_status;
808 chip->cb.rt5025_gauge_suspend = rt5025_gauge_suspend;
809 chip->cb.rt5025_gauge_resume = rt5025_gauge_resume;
810 chip->cb.rt5025_gauge_remove = rt5025_gauge_remove;
811 info->event_callback=&chip->cb;
813 //rt_register_gauge_callbacks(info->i2c, &chip->cb);
815 wake_lock(&chip->monitor_wake_lock);
816 schedule_delayed_work(&chip->monitor_work, msecs_to_jiffies(1000));
821 wake_lock_destroy(&chip->monitor_wake_lock);