13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/slab.h>
18 #include <linux/string.h>
24 #define STATUS2_CHG (1 << 2)
25 #define STATUS2_BAT (1 << 3)
26 #define STATUS2_VBUS (1 << 4)
29 #define MEAS1_TINT (1 << 3)
30 #define MEAS1_GP1 (1 << 5)
33 #define MEAS3_IBAT (1 << 0)
34 #define MEAS3_BAT_DET (1 << 1)
35 #define MEAS3_CC (1 << 2)
38 #define MEAS_OFF_SLEEP_EN (1 << 1)
41 #define GPBIAS2_GPADC1_SET (2 << 4)
43 #define GPBIAS2_GPADC1_UA ((GPBIAS2_GPADC1_SET >> 4) * 5 + 1)
46 #define GPMISC1_GPADC_EN (1 << 0)
49 #define CC6_BAT_DET_GPADC1 1
52 #define CCNT_AVG_SEL (4 << 3)
55 #define RTC_SOC_5LSB (0x1F << 3)
58 #define RTC_SOC_3MSB (0x7)
61 #define BAT_WU_LOG (1<<6)
72 #define OCV_MODE_ACTIVE 0
73 #define OCV_MODE_SLEEP 1
76 #define LOW_BAT_THRESHOLD 3600
77 #define VBATT_RESISTOR_MIN 3800
78 #define VBATT_RESISTOR_MAX 4100
81 #define PM860X_TEMP_TINT (0)
82 #define PM860X_TEMP_TBAT (1)
88 #define TBAT_NEG_25D 127773
89 #define TBAT_NEG_10D 54564
91 #define TBAT_10D 19785
92 #define TBAT_20D 12468
115 unsigned long long int pos;
116 unsigned long long int neg;
128 static int array_soc[][2] = {
129 {4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96},
130 {4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91},
131 {4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86},
132 {4013, 85}, {4006, 84}, {3995, 83}, {3987, 82}, {3982, 81},
133 {3976, 80}, {3968, 79}, {3962, 78}, {3954, 77}, {3946, 76},
134 {3941, 75}, {3934, 74}, {3929, 73}, {3922, 72}, {3916, 71},
135 {3910, 70}, {3904, 69}, {3898, 68}, {3892, 67}, {3887, 66},
136 {3880, 65}, {3874, 64}, {3868, 63}, {3862, 62}, {3854, 61},
137 {3849, 60}, {3843, 59}, {3840, 58}, {3833, 57}, {3829, 56},
138 {3824, 55}, {3818, 54}, {3815, 53}, {3810, 52}, {3808, 51},
139 {3804, 50}, {3801, 49}, {3798, 48}, {3796, 47}, {3792, 46},
140 {3789, 45}, {3785, 44}, {3784, 43}, {3782, 42}, {3780, 41},
141 {3777, 40}, {3776, 39}, {3774, 38}, {3772, 37}, {3771, 36},
142 {3769, 35}, {3768, 34}, {3764, 33}, {3763, 32}, {3760, 31},
143 {3760, 30}, {3754, 29}, {3750, 28}, {3749, 27}, {3744, 26},
144 {3740, 25}, {3734, 24}, {3732, 23}, {3728, 22}, {3726, 21},
145 {3720, 20}, {3716, 19}, {3709, 18}, {3703, 17}, {3698, 16},
146 {3692, 15}, {3683, 14}, {3675, 13}, {3670, 12}, {3665, 11},
147 {3661, 10}, {3649, 9}, {3637, 8}, {3622, 7}, {3609, 6},
148 {3580, 5}, {3558, 4}, {3540, 3}, {3510, 2}, {3429, 1},
151 static struct ccnt ccnt_data;
160 unsigned char buf[2];
167 *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
169 *data = ((*data & 0xfff) * 9 * 25) >> 9;
176 unsigned char buf[5];
200 ret = ((buf[4] >> 6) << 10) | ((buf[3] >> 6) << 8)
201 | ((buf[2] >> 6) << 6) | ((buf[1] >> 6) << 4)
204 *data = ((*data & 0xff) * 27 * 25) >> 9;
218 unsigned char buf[2];
226 s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
240 data = ((data - 50) / 50) & 0x1f;
242 *old = (*old & 0x1f) * 50 + 50;
252 unsigned char buf[2];
261 *ccnt = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
280 sum |= (data & 0xffff) << 16;
290 sum |= (data & 0xffff) << 16;
318 memset(ccnt, 0,
sizeof(*ccnt));
343 for (i = 0, ibatt_sum = 0, vbatt_sum = 0; i < 10; i++) {
348 ret = measure_current(info, &data);
353 vbatt_avg = vbatt_sum / 10;
354 ibatt_avg = ibatt_sum / 10;
358 *ocv = vbatt_avg - ibatt_avg * info->
resistor / 1000;
362 dev_dbg(info->
dev,
"VBAT average:%d, OCV:%d\n", vbatt_avg, *ocv);
381 ret = calc_ocv(info, &ocv);
391 if (ocv < array_soc[count - 1][0]) {
396 for (i = 0; i <
count; i++) {
397 if (ocv >= array_soc[i][0]) {
398 *soc = array_soc[
i][1];
405 static irqreturn_t pm860x_coulomb_handler(
int irq,
void *data)
409 calc_ccnt(info, &ccnt_data);
413 static irqreturn_t pm860x_batt_handler(
int irq,
void *data)
429 clear_ccnt(info, &ccnt_data);
435 unsigned char buf[2];
508 bat_remove != 0 ?
"yes" :
"no");
511 if (bat_remove == 0) {
514 data = ((buf[1] & 0x3) << 5) | ((buf[0] >> 3) & 0x1F);
517 else if (data < soc - 15)
521 dev_dbg(info->
dev,
"soc_rtc %d, soc_ocv :%d\n", data, soc);
524 BAT_WU_LOG, BAT_WU_LOG);
542 data = (min << 8) / 1800;
544 dev_dbg(info->
dev,
"TEMP_HIGHTH : min: %d, 0x%x\n", min, data);
549 data = (max << 8) / 1800;
551 dev_dbg(info->
dev,
"TEMP_LOWTH:max : %d, 0x%x\n", max, data);
565 *data = (*data - 884) * 1000 / 3611;
576 set_temp_threshold(info, 0, max);
580 set_temp_threshold(info, 0, max);
585 set_temp_threshold(info, min, max);
590 set_temp_threshold(info, min, max);
595 set_temp_threshold(info, min, max);
600 set_temp_threshold(info, min, max);
605 set_temp_threshold(info, min, max);
608 set_temp_threshold(info, min, 0);
612 dev_dbg(info->
dev,
"temp_C:%d C,temp_mv:%d mv\n", temp, *data);
629 ret = measure_current(info, &data);
642 if (set_charger_current(info, 500, &chg_current))
651 for (i = 0, vbatt_sum1 = 0, ibatt_sum1 = 0; i < 10; i++) {
656 ret = measure_current(info, &data);
661 ibatt_sum1 = ibatt_sum1 -
data;
663 ibatt_sum1 = ibatt_sum1 +
data;
666 if (set_charger_current(info, 100, &ret))
674 for (i = 0, vbatt_sum2 = 0, ibatt_sum2 = 0; i < 10; i++) {
679 ret = measure_current(info, &data);
684 ibatt_sum2 = ibatt_sum2 -
data;
686 ibatt_sum2 = ibatt_sum2 +
data;
690 if (set_charger_current(info, chg_current, &ret))
693 if ((vbatt_sum1 > vbatt_sum2) && (ibatt_sum1 > ibatt_sum2) &&
696 data = 1000 * (vbatt_sum1 - vbatt_sum2)
697 / (ibatt_sum1 - ibatt_sum2);
708 set_charger_current(info, chg_current, &ret);
721 ret = calc_ccnt(info, &ccnt_data);
726 if (ccnt_data.total_dischg - ccnt_data.total_chg <= data) {
728 data + ccnt_data.total_chg - ccnt_data.total_dischg;
730 clear_ccnt(info, &ccnt_data);
740 else if (cap_cc > 100)
743 dev_dbg(info->
dev,
"%s, last cap : %d", __func__,
746 ret = measure_current(info, &ibat);
762 *cap =
min(cap_ocv, cap_cc);
769 if (cap_cc < 15 && cap_ocv - cap_cc > 10)
783 dev_dbg(info->
dev,
"%s, cap_ocv:%d cap_cc:%d, cap:%d\n",
784 (ibat < 0) ?
"discharging" :
"charging",
785 cap_ocv, cap_cc, *cap);
793 ((*cap >> 5) & 0x3));
799 static void pm860x_external_power_changed(
struct power_supply *psy)
807 static int pm860x_batt_get_prop(
struct power_supply *psy,
820 ret = calc_capacity(info, &data);
840 val->
intval = data * 1000;
844 ret = calc_ocv(info, &data);
847 val->
intval = data * 1000;
850 ret = measure_current(info, &data);
857 ret = measure_temp(info, &data);
873 static int pm860x_batt_set_prop(
struct power_supply *psy,
881 clear_ccnt(info, &ccnt_data);
883 dev_dbg(info->
dev,
"chg done, update soc = %d\n",
934 pdata = pdev->
dev.platform_data;
937 platform_set_drvdata(pdev, info);
939 pm860x_init_battery(info);
941 info->
battery.name =
"battery-monitor";
943 info->
battery.properties = pm860x_batt_props;
945 info->
battery.get_property = pm860x_batt_get_prop;
946 info->
battery.set_property = pm860x_batt_set_prop;
947 info->
battery.external_power_changed = pm860x_external_power_changed;
967 dev_err(chip->
dev,
"Failed to request IRQ: #%d: %d\n",
975 dev_err(chip->
dev,
"Failed to request IRQ: #%d: %d\n",
1000 platform_set_drvdata(pdev,
NULL);
1004 #ifdef CONFIG_PM_SLEEP
1005 static int pm860x_battery_suspend(
struct device *
dev)
1010 if (device_may_wakeup(dev))
1015 static int pm860x_battery_resume(
struct device *dev)
1020 if (device_may_wakeup(dev))
1027 pm860x_battery_suspend, pm860x_battery_resume);
1031 .name =
"88pm860x-battery",
1033 .pm = &pm860x_battery_pm_ops,
1035 .probe = pm860x_battery_probe,