1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * Battery driver for Marvell 88PM860x PMIC
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  * Copyright (c) 2012 Marvell International Ltd.
68c0984e5SSebastian Reichel  * Author:	Jett Zhou <jtzhou@marvell.com>
78c0984e5SSebastian Reichel  *		Haojian Zhuang <haojian.zhuang@marvell.com>
88c0984e5SSebastian Reichel  */
98c0984e5SSebastian Reichel 
108c0984e5SSebastian Reichel #include <linux/kernel.h>
118c0984e5SSebastian Reichel #include <linux/module.h>
128c0984e5SSebastian Reichel #include <linux/platform_device.h>
138c0984e5SSebastian Reichel #include <linux/slab.h>
148c0984e5SSebastian Reichel #include <linux/mutex.h>
158c0984e5SSebastian Reichel #include <linux/string.h>
168c0984e5SSebastian Reichel #include <linux/power_supply.h>
178c0984e5SSebastian Reichel #include <linux/mfd/88pm860x.h>
188c0984e5SSebastian Reichel #include <linux/delay.h>
198c0984e5SSebastian Reichel 
208c0984e5SSebastian Reichel /* bit definitions of Status Query Interface 2 */
218c0984e5SSebastian Reichel #define STATUS2_CHG			(1 << 2)
228c0984e5SSebastian Reichel #define STATUS2_BAT			(1 << 3)
238c0984e5SSebastian Reichel #define STATUS2_VBUS			(1 << 4)
248c0984e5SSebastian Reichel 
258c0984e5SSebastian Reichel /* bit definitions of Measurement Enable 1 Register */
268c0984e5SSebastian Reichel #define MEAS1_TINT			(1 << 3)
278c0984e5SSebastian Reichel #define MEAS1_GP1			(1 << 5)
288c0984e5SSebastian Reichel 
298c0984e5SSebastian Reichel /* bit definitions of Measurement Enable 3 Register */
308c0984e5SSebastian Reichel #define MEAS3_IBAT			(1 << 0)
318c0984e5SSebastian Reichel #define MEAS3_BAT_DET			(1 << 1)
328c0984e5SSebastian Reichel #define MEAS3_CC			(1 << 2)
338c0984e5SSebastian Reichel 
348c0984e5SSebastian Reichel /* bit definitions of Measurement Off Time Register */
358c0984e5SSebastian Reichel #define MEAS_OFF_SLEEP_EN		(1 << 1)
368c0984e5SSebastian Reichel 
378c0984e5SSebastian Reichel /* bit definitions of GPADC Bias Current 2 Register */
388c0984e5SSebastian Reichel #define GPBIAS2_GPADC1_SET		(2 << 4)
398c0984e5SSebastian Reichel /* GPADC1 Bias Current value in uA unit */
408c0984e5SSebastian Reichel #define GPBIAS2_GPADC1_UA		((GPBIAS2_GPADC1_SET >> 4) * 5 + 1)
418c0984e5SSebastian Reichel 
428c0984e5SSebastian Reichel /* bit definitions of GPADC Misc 1 Register */
438c0984e5SSebastian Reichel #define GPMISC1_GPADC_EN		(1 << 0)
448c0984e5SSebastian Reichel 
458c0984e5SSebastian Reichel /* bit definitions of Charger Control 6 Register */
468c0984e5SSebastian Reichel #define CC6_BAT_DET_GPADC1		1
478c0984e5SSebastian Reichel 
488c0984e5SSebastian Reichel /* bit definitions of Coulomb Counter Reading Register */
498c0984e5SSebastian Reichel #define CCNT_AVG_SEL			(4 << 3)
508c0984e5SSebastian Reichel 
518c0984e5SSebastian Reichel /* bit definitions of RTC miscellaneous Register1 */
528c0984e5SSebastian Reichel #define RTC_SOC_5LSB		(0x1F << 3)
538c0984e5SSebastian Reichel 
548c0984e5SSebastian Reichel /* bit definitions of RTC Register1 */
558c0984e5SSebastian Reichel #define RTC_SOC_3MSB		(0x7)
568c0984e5SSebastian Reichel 
578c0984e5SSebastian Reichel /* bit definitions of Power up Log register */
588c0984e5SSebastian Reichel #define BAT_WU_LOG			(1<<6)
598c0984e5SSebastian Reichel 
608c0984e5SSebastian Reichel /* coulomb counter index */
618c0984e5SSebastian Reichel #define CCNT_POS1			0
628c0984e5SSebastian Reichel #define CCNT_POS2			1
638c0984e5SSebastian Reichel #define CCNT_NEG1			2
648c0984e5SSebastian Reichel #define CCNT_NEG2			3
658c0984e5SSebastian Reichel #define CCNT_SPOS			4
668c0984e5SSebastian Reichel #define CCNT_SNEG			5
678c0984e5SSebastian Reichel 
688c0984e5SSebastian Reichel /* OCV -- Open Circuit Voltage */
698c0984e5SSebastian Reichel #define OCV_MODE_ACTIVE			0
708c0984e5SSebastian Reichel #define OCV_MODE_SLEEP			1
718c0984e5SSebastian Reichel 
728c0984e5SSebastian Reichel /* Vbat range of CC for measuring Rbat */
738c0984e5SSebastian Reichel #define LOW_BAT_THRESHOLD		3600
748c0984e5SSebastian Reichel #define VBATT_RESISTOR_MIN		3800
758c0984e5SSebastian Reichel #define VBATT_RESISTOR_MAX		4100
768c0984e5SSebastian Reichel 
778c0984e5SSebastian Reichel /* TBAT for batt, TINT for chip itself */
788c0984e5SSebastian Reichel #define PM860X_TEMP_TINT		(0)
798c0984e5SSebastian Reichel #define PM860X_TEMP_TBAT		(1)
808c0984e5SSebastian Reichel 
818c0984e5SSebastian Reichel /*
828c0984e5SSebastian Reichel  * Battery temperature based on NTC resistor, defined
838c0984e5SSebastian Reichel  * corresponding resistor value  -- Ohm / C degeree.
848c0984e5SSebastian Reichel  */
858c0984e5SSebastian Reichel #define TBAT_NEG_25D		127773	/* -25 */
868c0984e5SSebastian Reichel #define TBAT_NEG_10D		54564	/* -10 */
878c0984e5SSebastian Reichel #define TBAT_0D			32330	/* 0 */
888c0984e5SSebastian Reichel #define TBAT_10D		19785	/* 10 */
898c0984e5SSebastian Reichel #define TBAT_20D		12468	/* 20 */
908c0984e5SSebastian Reichel #define TBAT_30D		8072	/* 30 */
918c0984e5SSebastian Reichel #define TBAT_40D		5356	/* 40 */
928c0984e5SSebastian Reichel 
938c0984e5SSebastian Reichel struct pm860x_battery_info {
948c0984e5SSebastian Reichel 	struct pm860x_chip *chip;
958c0984e5SSebastian Reichel 	struct i2c_client *i2c;
968c0984e5SSebastian Reichel 	struct device *dev;
978c0984e5SSebastian Reichel 
988c0984e5SSebastian Reichel 	struct power_supply *battery;
998c0984e5SSebastian Reichel 	struct mutex lock;
1008c0984e5SSebastian Reichel 	int status;
1018c0984e5SSebastian Reichel 	int irq_cc;
1028c0984e5SSebastian Reichel 	int irq_batt;
1038c0984e5SSebastian Reichel 	int max_capacity;
1048c0984e5SSebastian Reichel 	int resistor;		/* Battery Internal Resistor */
1058c0984e5SSebastian Reichel 	int last_capacity;
1068c0984e5SSebastian Reichel 	int start_soc;
1078c0984e5SSebastian Reichel 	unsigned present:1;
1088c0984e5SSebastian Reichel 	unsigned temp_type:1;	/* TINT or TBAT */
1098c0984e5SSebastian Reichel };
1108c0984e5SSebastian Reichel 
1118c0984e5SSebastian Reichel struct ccnt {
112*33ae8b03SMilan Djurovic 	unsigned long long pos;
113*33ae8b03SMilan Djurovic 	unsigned long long neg;
1148c0984e5SSebastian Reichel 	unsigned int spos;
1158c0984e5SSebastian Reichel 	unsigned int sneg;
1168c0984e5SSebastian Reichel 
1178c0984e5SSebastian Reichel 	int total_chg;		/* mAh(3.6C) */
1188c0984e5SSebastian Reichel 	int total_dischg;	/* mAh(3.6C) */
1198c0984e5SSebastian Reichel };
1208c0984e5SSebastian Reichel 
1218c0984e5SSebastian Reichel /*
1228c0984e5SSebastian Reichel  * State of Charge.
1238c0984e5SSebastian Reichel  * The first number is mAh(=3.6C), and the second number is percent point.
1248c0984e5SSebastian Reichel  */
1258c0984e5SSebastian Reichel static int array_soc[][2] = {
1268c0984e5SSebastian Reichel 	{4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96},
1278c0984e5SSebastian Reichel 	{4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91},
1288c0984e5SSebastian Reichel 	{4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86},
1298c0984e5SSebastian Reichel 	{4013, 85}, {4006, 84}, {3995, 83}, {3987, 82}, {3982, 81},
1308c0984e5SSebastian Reichel 	{3976, 80}, {3968, 79}, {3962, 78}, {3954, 77}, {3946, 76},
1318c0984e5SSebastian Reichel 	{3941, 75}, {3934, 74}, {3929, 73}, {3922, 72}, {3916, 71},
1328c0984e5SSebastian Reichel 	{3910, 70}, {3904, 69}, {3898, 68}, {3892, 67}, {3887, 66},
1338c0984e5SSebastian Reichel 	{3880, 65}, {3874, 64}, {3868, 63}, {3862, 62}, {3854, 61},
1348c0984e5SSebastian Reichel 	{3849, 60}, {3843, 59}, {3840, 58}, {3833, 57}, {3829, 56},
1358c0984e5SSebastian Reichel 	{3824, 55}, {3818, 54}, {3815, 53}, {3810, 52}, {3808, 51},
1368c0984e5SSebastian Reichel 	{3804, 50}, {3801, 49}, {3798, 48}, {3796, 47}, {3792, 46},
1378c0984e5SSebastian Reichel 	{3789, 45}, {3785, 44}, {3784, 43}, {3782, 42}, {3780, 41},
1388c0984e5SSebastian Reichel 	{3777, 40}, {3776, 39}, {3774, 38}, {3772, 37}, {3771, 36},
1398c0984e5SSebastian Reichel 	{3769, 35}, {3768, 34}, {3764, 33}, {3763, 32}, {3760, 31},
1408c0984e5SSebastian Reichel 	{3760, 30}, {3754, 29}, {3750, 28}, {3749, 27}, {3744, 26},
1418c0984e5SSebastian Reichel 	{3740, 25}, {3734, 24}, {3732, 23}, {3728, 22}, {3726, 21},
1428c0984e5SSebastian Reichel 	{3720, 20}, {3716, 19}, {3709, 18}, {3703, 17}, {3698, 16},
1438c0984e5SSebastian Reichel 	{3692, 15}, {3683, 14}, {3675, 13}, {3670, 12}, {3665, 11},
1448c0984e5SSebastian Reichel 	{3661, 10}, {3649, 9}, {3637, 8}, {3622, 7}, {3609, 6},
1458c0984e5SSebastian Reichel 	{3580, 5}, {3558, 4}, {3540, 3}, {3510, 2}, {3429, 1},
1468c0984e5SSebastian Reichel };
1478c0984e5SSebastian Reichel 
1488c0984e5SSebastian Reichel static struct ccnt ccnt_data;
1498c0984e5SSebastian Reichel 
1508c0984e5SSebastian Reichel /*
1518c0984e5SSebastian Reichel  * register 1 bit[7:0] -- bit[11:4] of measured value of voltage
1528c0984e5SSebastian Reichel  * register 0 bit[3:0] -- bit[3:0] of measured value of voltage
1538c0984e5SSebastian Reichel  */
measure_12bit_voltage(struct pm860x_battery_info * info,int offset,int * data)1548c0984e5SSebastian Reichel static int measure_12bit_voltage(struct pm860x_battery_info *info,
1558c0984e5SSebastian Reichel 				 int offset, int *data)
1568c0984e5SSebastian Reichel {
1578c0984e5SSebastian Reichel 	unsigned char buf[2];
1588c0984e5SSebastian Reichel 	int ret;
1598c0984e5SSebastian Reichel 
1608c0984e5SSebastian Reichel 	ret = pm860x_bulk_read(info->i2c, offset, 2, buf);
1618c0984e5SSebastian Reichel 	if (ret < 0)
1628c0984e5SSebastian Reichel 		return ret;
1638c0984e5SSebastian Reichel 
1648c0984e5SSebastian Reichel 	*data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
1658c0984e5SSebastian Reichel 	/* V_MEAS(mV) = data * 1.8 * 1000 / (2^12) */
1668c0984e5SSebastian Reichel 	*data = ((*data & 0xfff) * 9 * 25) >> 9;
1678c0984e5SSebastian Reichel 	return 0;
1688c0984e5SSebastian Reichel }
1698c0984e5SSebastian Reichel 
measure_vbatt(struct pm860x_battery_info * info,int state,int * data)1708c0984e5SSebastian Reichel static int measure_vbatt(struct pm860x_battery_info *info, int state,
1718c0984e5SSebastian Reichel 			 int *data)
1728c0984e5SSebastian Reichel {
1738c0984e5SSebastian Reichel 	unsigned char buf[5];
1748c0984e5SSebastian Reichel 	int ret;
1758c0984e5SSebastian Reichel 
1768c0984e5SSebastian Reichel 	switch (state) {
1778c0984e5SSebastian Reichel 	case OCV_MODE_ACTIVE:
1788c0984e5SSebastian Reichel 		ret = measure_12bit_voltage(info, PM8607_VBAT_MEAS1, data);
1798c0984e5SSebastian Reichel 		if (ret)
1808c0984e5SSebastian Reichel 			return ret;
1818c0984e5SSebastian Reichel 		/* V_BATT_MEAS(mV) = value * 3 * 1.8 * 1000 / (2^12) */
1828c0984e5SSebastian Reichel 		*data *= 3;
1838c0984e5SSebastian Reichel 		break;
1848c0984e5SSebastian Reichel 	case OCV_MODE_SLEEP:
1858c0984e5SSebastian Reichel 		/*
1868c0984e5SSebastian Reichel 		 * voltage value of VBATT in sleep mode is saved in different
1878c0984e5SSebastian Reichel 		 * registers.
1888c0984e5SSebastian Reichel 		 * bit[11:10] -- bit[7:6] of LDO9(0x18)
1898c0984e5SSebastian Reichel 		 * bit[9:8] -- bit[7:6] of LDO8(0x17)
1908c0984e5SSebastian Reichel 		 * bit[7:6] -- bit[7:6] of LDO7(0x16)
1918c0984e5SSebastian Reichel 		 * bit[5:4] -- bit[7:6] of LDO6(0x15)
1928c0984e5SSebastian Reichel 		 * bit[3:0] -- bit[7:4] of LDO5(0x14)
1938c0984e5SSebastian Reichel 		 */
1948c0984e5SSebastian Reichel 		ret = pm860x_bulk_read(info->i2c, PM8607_LDO5, 5, buf);
1958c0984e5SSebastian Reichel 		if (ret < 0)
1968c0984e5SSebastian Reichel 			return ret;
1978c0984e5SSebastian Reichel 		ret = ((buf[4] >> 6) << 10) | ((buf[3] >> 6) << 8)
1988c0984e5SSebastian Reichel 		    | ((buf[2] >> 6) << 6) | ((buf[1] >> 6) << 4)
1998c0984e5SSebastian Reichel 		    | (buf[0] >> 4);
2008c0984e5SSebastian Reichel 		/* V_BATT_MEAS(mV) = data * 3 * 1.8 * 1000 / (2^12) */
2018c0984e5SSebastian Reichel 		*data = ((*data & 0xff) * 27 * 25) >> 9;
2028c0984e5SSebastian Reichel 		break;
2038c0984e5SSebastian Reichel 	default:
2048c0984e5SSebastian Reichel 		return -EINVAL;
2058c0984e5SSebastian Reichel 	}
2068c0984e5SSebastian Reichel 	return 0;
2078c0984e5SSebastian Reichel }
2088c0984e5SSebastian Reichel 
2098c0984e5SSebastian Reichel /*
2108c0984e5SSebastian Reichel  * Return value is signed data.
2118c0984e5SSebastian Reichel  * Negative value means discharging, and positive value means charging.
2128c0984e5SSebastian Reichel  */
measure_current(struct pm860x_battery_info * info,int * data)2138c0984e5SSebastian Reichel static int measure_current(struct pm860x_battery_info *info, int *data)
2148c0984e5SSebastian Reichel {
2158c0984e5SSebastian Reichel 	unsigned char buf[2];
2168c0984e5SSebastian Reichel 	short s;
2178c0984e5SSebastian Reichel 	int ret;
2188c0984e5SSebastian Reichel 
2198c0984e5SSebastian Reichel 	ret = pm860x_bulk_read(info->i2c, PM8607_IBAT_MEAS1, 2, buf);
2208c0984e5SSebastian Reichel 	if (ret < 0)
2218c0984e5SSebastian Reichel 		return ret;
2228c0984e5SSebastian Reichel 
2238c0984e5SSebastian Reichel 	s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
2248c0984e5SSebastian Reichel 	/* current(mA) = value * 0.125 */
2258c0984e5SSebastian Reichel 	*data = s >> 3;
2268c0984e5SSebastian Reichel 	return 0;
2278c0984e5SSebastian Reichel }
2288c0984e5SSebastian Reichel 
set_charger_current(struct pm860x_battery_info * info,int data,int * old)2298c0984e5SSebastian Reichel static int set_charger_current(struct pm860x_battery_info *info, int data,
2308c0984e5SSebastian Reichel 			       int *old)
2318c0984e5SSebastian Reichel {
2328c0984e5SSebastian Reichel 	int ret;
2338c0984e5SSebastian Reichel 
2348c0984e5SSebastian Reichel 	if (data < 50 || data > 1600 || !old)
2358c0984e5SSebastian Reichel 		return -EINVAL;
2368c0984e5SSebastian Reichel 
2378c0984e5SSebastian Reichel 	data = ((data - 50) / 50) & 0x1f;
2388c0984e5SSebastian Reichel 	*old = pm860x_reg_read(info->i2c, PM8607_CHG_CTRL2);
2398c0984e5SSebastian Reichel 	*old = (*old & 0x1f) * 50 + 50;
2408c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, data);
2418c0984e5SSebastian Reichel 	if (ret < 0)
2428c0984e5SSebastian Reichel 		return ret;
2438c0984e5SSebastian Reichel 	return 0;
2448c0984e5SSebastian Reichel }
2458c0984e5SSebastian Reichel 
read_ccnt(struct pm860x_battery_info * info,int offset,int * ccnt)2468c0984e5SSebastian Reichel static int read_ccnt(struct pm860x_battery_info *info, int offset,
2478c0984e5SSebastian Reichel 		     int *ccnt)
2488c0984e5SSebastian Reichel {
2498c0984e5SSebastian Reichel 	unsigned char buf[2];
2508c0984e5SSebastian Reichel 	int ret;
2518c0984e5SSebastian Reichel 
2528c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7, offset & 7);
2538c0984e5SSebastian Reichel 	if (ret < 0)
2548c0984e5SSebastian Reichel 		goto out;
2558c0984e5SSebastian Reichel 	ret = pm860x_bulk_read(info->i2c, PM8607_CCNT_MEAS1, 2, buf);
2568c0984e5SSebastian Reichel 	if (ret < 0)
2578c0984e5SSebastian Reichel 		goto out;
2588c0984e5SSebastian Reichel 	*ccnt = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
2598c0984e5SSebastian Reichel 	return 0;
2608c0984e5SSebastian Reichel out:
2618c0984e5SSebastian Reichel 	return ret;
2628c0984e5SSebastian Reichel }
2638c0984e5SSebastian Reichel 
calc_ccnt(struct pm860x_battery_info * info,struct ccnt * ccnt)2648c0984e5SSebastian Reichel static int calc_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
2658c0984e5SSebastian Reichel {
2668c0984e5SSebastian Reichel 	unsigned int sum;
2678c0984e5SSebastian Reichel 	int ret;
2688c0984e5SSebastian Reichel 	int data;
2698c0984e5SSebastian Reichel 
2708c0984e5SSebastian Reichel 	ret = read_ccnt(info, CCNT_POS1, &data);
2718c0984e5SSebastian Reichel 	if (ret)
2728c0984e5SSebastian Reichel 		goto out;
2738c0984e5SSebastian Reichel 	sum = data & 0xffff;
2748c0984e5SSebastian Reichel 	ret = read_ccnt(info, CCNT_POS2, &data);
2758c0984e5SSebastian Reichel 	if (ret)
2768c0984e5SSebastian Reichel 		goto out;
2778c0984e5SSebastian Reichel 	sum |= (data & 0xffff) << 16;
2788c0984e5SSebastian Reichel 	ccnt->pos += sum;
2798c0984e5SSebastian Reichel 
2808c0984e5SSebastian Reichel 	ret = read_ccnt(info, CCNT_NEG1, &data);
2818c0984e5SSebastian Reichel 	if (ret)
2828c0984e5SSebastian Reichel 		goto out;
2838c0984e5SSebastian Reichel 	sum = data & 0xffff;
2848c0984e5SSebastian Reichel 	ret = read_ccnt(info, CCNT_NEG2, &data);
2858c0984e5SSebastian Reichel 	if (ret)
2868c0984e5SSebastian Reichel 		goto out;
2878c0984e5SSebastian Reichel 	sum |= (data & 0xffff) << 16;
2888c0984e5SSebastian Reichel 	sum = ~sum + 1;		/* since it's negative */
2898c0984e5SSebastian Reichel 	ccnt->neg += sum;
2908c0984e5SSebastian Reichel 
2918c0984e5SSebastian Reichel 	ret = read_ccnt(info, CCNT_SPOS, &data);
2928c0984e5SSebastian Reichel 	if (ret)
2938c0984e5SSebastian Reichel 		goto out;
2948c0984e5SSebastian Reichel 	ccnt->spos += data;
2958c0984e5SSebastian Reichel 	ret = read_ccnt(info, CCNT_SNEG, &data);
2968c0984e5SSebastian Reichel 	if (ret)
2978c0984e5SSebastian Reichel 		goto out;
2988c0984e5SSebastian Reichel 
2998c0984e5SSebastian Reichel 	/*
3008c0984e5SSebastian Reichel 	 * charge(mAh)  = count * 1.6984 * 1e(-8)
3018c0984e5SSebastian Reichel 	 *              = count * 16984 * 1.024 * 1.024 * 1.024 / (2 ^ 40)
3028c0984e5SSebastian Reichel 	 *              = count * 18236 / (2 ^ 40)
3038c0984e5SSebastian Reichel 	 */
3048c0984e5SSebastian Reichel 	ccnt->total_chg = (int) ((ccnt->pos * 18236) >> 40);
3058c0984e5SSebastian Reichel 	ccnt->total_dischg = (int) ((ccnt->neg * 18236) >> 40);
3068c0984e5SSebastian Reichel 	return 0;
3078c0984e5SSebastian Reichel out:
3088c0984e5SSebastian Reichel 	return ret;
3098c0984e5SSebastian Reichel }
3108c0984e5SSebastian Reichel 
clear_ccnt(struct pm860x_battery_info * info,struct ccnt * ccnt)3118c0984e5SSebastian Reichel static int clear_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
3128c0984e5SSebastian Reichel {
3138c0984e5SSebastian Reichel 	int data;
3148c0984e5SSebastian Reichel 
3158c0984e5SSebastian Reichel 	memset(ccnt, 0, sizeof(*ccnt));
3168c0984e5SSebastian Reichel 	/* read to clear ccnt */
3178c0984e5SSebastian Reichel 	read_ccnt(info, CCNT_POS1, &data);
3188c0984e5SSebastian Reichel 	read_ccnt(info, CCNT_POS2, &data);
3198c0984e5SSebastian Reichel 	read_ccnt(info, CCNT_NEG1, &data);
3208c0984e5SSebastian Reichel 	read_ccnt(info, CCNT_NEG2, &data);
3218c0984e5SSebastian Reichel 	read_ccnt(info, CCNT_SPOS, &data);
3228c0984e5SSebastian Reichel 	read_ccnt(info, CCNT_SNEG, &data);
3238c0984e5SSebastian Reichel 	return 0;
3248c0984e5SSebastian Reichel }
3258c0984e5SSebastian Reichel 
3268c0984e5SSebastian Reichel /* Calculate Open Circuit Voltage */
calc_ocv(struct pm860x_battery_info * info,int * ocv)3278c0984e5SSebastian Reichel static int calc_ocv(struct pm860x_battery_info *info, int *ocv)
3288c0984e5SSebastian Reichel {
3298c0984e5SSebastian Reichel 	int ret;
3308c0984e5SSebastian Reichel 	int i;
3318c0984e5SSebastian Reichel 	int data;
3328c0984e5SSebastian Reichel 	int vbatt_avg;
3338c0984e5SSebastian Reichel 	int vbatt_sum;
3348c0984e5SSebastian Reichel 	int ibatt_avg;
3358c0984e5SSebastian Reichel 	int ibatt_sum;
3368c0984e5SSebastian Reichel 
3378c0984e5SSebastian Reichel 	if (!ocv)
3388c0984e5SSebastian Reichel 		return -EINVAL;
3398c0984e5SSebastian Reichel 
3408c0984e5SSebastian Reichel 	for (i = 0, ibatt_sum = 0, vbatt_sum = 0; i < 10; i++) {
3418c0984e5SSebastian Reichel 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
3428c0984e5SSebastian Reichel 		if (ret)
3438c0984e5SSebastian Reichel 			goto out;
3448c0984e5SSebastian Reichel 		vbatt_sum += data;
3458c0984e5SSebastian Reichel 		ret = measure_current(info, &data);
3468c0984e5SSebastian Reichel 		if (ret)
3478c0984e5SSebastian Reichel 			goto out;
3488c0984e5SSebastian Reichel 		ibatt_sum += data;
3498c0984e5SSebastian Reichel 	}
3508c0984e5SSebastian Reichel 	vbatt_avg = vbatt_sum / 10;
3518c0984e5SSebastian Reichel 	ibatt_avg = ibatt_sum / 10;
3528c0984e5SSebastian Reichel 
3538c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
3548c0984e5SSebastian Reichel 	if (info->present)
3558c0984e5SSebastian Reichel 		*ocv = vbatt_avg - ibatt_avg * info->resistor / 1000;
3568c0984e5SSebastian Reichel 	else
3578c0984e5SSebastian Reichel 		*ocv = vbatt_avg;
3588c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
3598c0984e5SSebastian Reichel 	dev_dbg(info->dev, "VBAT average:%d, OCV:%d\n", vbatt_avg, *ocv);
3608c0984e5SSebastian Reichel 	return 0;
3618c0984e5SSebastian Reichel out:
3628c0984e5SSebastian Reichel 	return ret;
3638c0984e5SSebastian Reichel }
3648c0984e5SSebastian Reichel 
3658c0984e5SSebastian Reichel /* Calculate State of Charge (percent points) */
calc_soc(struct pm860x_battery_info * info,int state,int * soc)3668c0984e5SSebastian Reichel static int calc_soc(struct pm860x_battery_info *info, int state, int *soc)
3678c0984e5SSebastian Reichel {
3688c0984e5SSebastian Reichel 	int i;
3698c0984e5SSebastian Reichel 	int ocv;
3708c0984e5SSebastian Reichel 	int count;
3718c0984e5SSebastian Reichel 	int ret = -EINVAL;
3728c0984e5SSebastian Reichel 
3738c0984e5SSebastian Reichel 	if (!soc)
3748c0984e5SSebastian Reichel 		return -EINVAL;
3758c0984e5SSebastian Reichel 
3768c0984e5SSebastian Reichel 	switch (state) {
3778c0984e5SSebastian Reichel 	case OCV_MODE_ACTIVE:
3788c0984e5SSebastian Reichel 		ret = calc_ocv(info, &ocv);
3798c0984e5SSebastian Reichel 		break;
3808c0984e5SSebastian Reichel 	case OCV_MODE_SLEEP:
3818c0984e5SSebastian Reichel 		ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv);
3828c0984e5SSebastian Reichel 		break;
3838c0984e5SSebastian Reichel 	}
3848c0984e5SSebastian Reichel 	if (ret)
3858c0984e5SSebastian Reichel 		return ret;
3868c0984e5SSebastian Reichel 
3878c0984e5SSebastian Reichel 	count = ARRAY_SIZE(array_soc);
3888c0984e5SSebastian Reichel 	if (ocv < array_soc[count - 1][0]) {
3898c0984e5SSebastian Reichel 		*soc = 0;
3908c0984e5SSebastian Reichel 		return 0;
3918c0984e5SSebastian Reichel 	}
3928c0984e5SSebastian Reichel 
3938c0984e5SSebastian Reichel 	for (i = 0; i < count; i++) {
3948c0984e5SSebastian Reichel 		if (ocv >= array_soc[i][0]) {
3958c0984e5SSebastian Reichel 			*soc = array_soc[i][1];
3968c0984e5SSebastian Reichel 			break;
3978c0984e5SSebastian Reichel 		}
3988c0984e5SSebastian Reichel 	}
3998c0984e5SSebastian Reichel 	return 0;
4008c0984e5SSebastian Reichel }
4018c0984e5SSebastian Reichel 
pm860x_coulomb_handler(int irq,void * data)4028c0984e5SSebastian Reichel static irqreturn_t pm860x_coulomb_handler(int irq, void *data)
4038c0984e5SSebastian Reichel {
4048c0984e5SSebastian Reichel 	struct pm860x_battery_info *info = data;
4058c0984e5SSebastian Reichel 
4068c0984e5SSebastian Reichel 	calc_ccnt(info, &ccnt_data);
4078c0984e5SSebastian Reichel 	return IRQ_HANDLED;
4088c0984e5SSebastian Reichel }
4098c0984e5SSebastian Reichel 
pm860x_batt_handler(int irq,void * data)4108c0984e5SSebastian Reichel static irqreturn_t pm860x_batt_handler(int irq, void *data)
4118c0984e5SSebastian Reichel {
4128c0984e5SSebastian Reichel 	struct pm860x_battery_info *info = data;
4138c0984e5SSebastian Reichel 	int ret;
4148c0984e5SSebastian Reichel 
4158c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
4168c0984e5SSebastian Reichel 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
4178c0984e5SSebastian Reichel 	if (ret & STATUS2_BAT) {
4188c0984e5SSebastian Reichel 		info->present = 1;
4198c0984e5SSebastian Reichel 		info->temp_type = PM860X_TEMP_TBAT;
4208c0984e5SSebastian Reichel 	} else {
4218c0984e5SSebastian Reichel 		info->present = 0;
4228c0984e5SSebastian Reichel 		info->temp_type = PM860X_TEMP_TINT;
4238c0984e5SSebastian Reichel 	}
4248c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
4258c0984e5SSebastian Reichel 	/* clear ccnt since battery is attached or dettached */
4268c0984e5SSebastian Reichel 	clear_ccnt(info, &ccnt_data);
4278c0984e5SSebastian Reichel 	return IRQ_HANDLED;
4288c0984e5SSebastian Reichel }
4298c0984e5SSebastian Reichel 
pm860x_init_battery(struct pm860x_battery_info * info)4308c0984e5SSebastian Reichel static void pm860x_init_battery(struct pm860x_battery_info *info)
4318c0984e5SSebastian Reichel {
4328c0984e5SSebastian Reichel 	unsigned char buf[2];
4338c0984e5SSebastian Reichel 	int ret;
4348c0984e5SSebastian Reichel 	int data;
4358c0984e5SSebastian Reichel 	int bat_remove;
436ccf193deSTom Rix 	int soc = 0;
4378c0984e5SSebastian Reichel 
4388c0984e5SSebastian Reichel 	/* measure enable on GPADC1 */
4398c0984e5SSebastian Reichel 	data = MEAS1_GP1;
4408c0984e5SSebastian Reichel 	if (info->temp_type == PM860X_TEMP_TINT)
4418c0984e5SSebastian Reichel 		data |= MEAS1_TINT;
4428c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN1, data, data);
4438c0984e5SSebastian Reichel 	if (ret)
4448c0984e5SSebastian Reichel 		goto out;
4458c0984e5SSebastian Reichel 
4468c0984e5SSebastian Reichel 	/* measure enable on IBAT, BAT_DET, CC. IBAT is depend on CC. */
4478c0984e5SSebastian Reichel 	data = MEAS3_IBAT | MEAS3_BAT_DET | MEAS3_CC;
4488c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN3, data, data);
4498c0984e5SSebastian Reichel 	if (ret)
4508c0984e5SSebastian Reichel 		goto out;
4518c0984e5SSebastian Reichel 
4528c0984e5SSebastian Reichel 	/* measure disable CC in sleep time  */
4538c0984e5SSebastian Reichel 	ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME1, 0x82);
4548c0984e5SSebastian Reichel 	if (ret)
4558c0984e5SSebastian Reichel 		goto out;
4568c0984e5SSebastian Reichel 	ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME2, 0x6c);
4578c0984e5SSebastian Reichel 	if (ret)
4588c0984e5SSebastian Reichel 		goto out;
4598c0984e5SSebastian Reichel 
4608c0984e5SSebastian Reichel 	/* enable GPADC */
4618c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_GPADC_MISC1,
4628c0984e5SSebastian Reichel 			    GPMISC1_GPADC_EN, GPMISC1_GPADC_EN);
4638c0984e5SSebastian Reichel 	if (ret < 0)
4648c0984e5SSebastian Reichel 		goto out;
4658c0984e5SSebastian Reichel 
4668c0984e5SSebastian Reichel 	/* detect battery via GPADC1 */
4678c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6,
4688c0984e5SSebastian Reichel 			    CC6_BAT_DET_GPADC1, CC6_BAT_DET_GPADC1);
4698c0984e5SSebastian Reichel 	if (ret < 0)
4708c0984e5SSebastian Reichel 		goto out;
4718c0984e5SSebastian Reichel 
4728c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7 << 3,
4738c0984e5SSebastian Reichel 			      CCNT_AVG_SEL);
4748c0984e5SSebastian Reichel 	if (ret < 0)
4758c0984e5SSebastian Reichel 		goto out;
4768c0984e5SSebastian Reichel 
4778c0984e5SSebastian Reichel 	/* set GPADC1 bias */
4788c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_GP_BIAS2, 0xF << 4,
4798c0984e5SSebastian Reichel 			      GPBIAS2_GPADC1_SET);
4808c0984e5SSebastian Reichel 	if (ret < 0)
4818c0984e5SSebastian Reichel 		goto out;
4828c0984e5SSebastian Reichel 
4838c0984e5SSebastian Reichel 	/* check whether battery present) */
4848c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
4858c0984e5SSebastian Reichel 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
4868c0984e5SSebastian Reichel 	if (ret < 0) {
4878c0984e5SSebastian Reichel 		mutex_unlock(&info->lock);
4888c0984e5SSebastian Reichel 		goto out;
4898c0984e5SSebastian Reichel 	}
4908c0984e5SSebastian Reichel 	if (ret & STATUS2_BAT) {
4918c0984e5SSebastian Reichel 		info->present = 1;
4928c0984e5SSebastian Reichel 		info->temp_type = PM860X_TEMP_TBAT;
4938c0984e5SSebastian Reichel 	} else {
4948c0984e5SSebastian Reichel 		info->present = 0;
4958c0984e5SSebastian Reichel 		info->temp_type = PM860X_TEMP_TINT;
4968c0984e5SSebastian Reichel 	}
4978c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
4988c0984e5SSebastian Reichel 
499ccf193deSTom Rix 	ret = calc_soc(info, OCV_MODE_ACTIVE, &soc);
500ccf193deSTom Rix 	if (ret < 0)
501ccf193deSTom Rix 		goto out;
5028c0984e5SSebastian Reichel 
5038c0984e5SSebastian Reichel 	data = pm860x_reg_read(info->i2c, PM8607_POWER_UP_LOG);
5048c0984e5SSebastian Reichel 	bat_remove = data & BAT_WU_LOG;
5058c0984e5SSebastian Reichel 
5068c0984e5SSebastian Reichel 	dev_dbg(info->dev, "battery wake up? %s\n",
5078c0984e5SSebastian Reichel 		bat_remove != 0 ? "yes" : "no");
5088c0984e5SSebastian Reichel 
5098c0984e5SSebastian Reichel 	/* restore SOC from RTC domain register */
5108c0984e5SSebastian Reichel 	if (bat_remove == 0) {
5118c0984e5SSebastian Reichel 		buf[0] = pm860x_reg_read(info->i2c, PM8607_RTC_MISC2);
5128c0984e5SSebastian Reichel 		buf[1] = pm860x_reg_read(info->i2c, PM8607_RTC1);
5138c0984e5SSebastian Reichel 		data = ((buf[1] & 0x3) << 5) | ((buf[0] >> 3) & 0x1F);
5148c0984e5SSebastian Reichel 		if (data > soc + 15)
5158c0984e5SSebastian Reichel 			info->start_soc = soc;
5168c0984e5SSebastian Reichel 		else if (data < soc - 15)
5178c0984e5SSebastian Reichel 			info->start_soc = soc;
5188c0984e5SSebastian Reichel 		else
5198c0984e5SSebastian Reichel 			info->start_soc = data;
5208c0984e5SSebastian Reichel 		dev_dbg(info->dev, "soc_rtc %d, soc_ocv :%d\n", data, soc);
5218c0984e5SSebastian Reichel 	} else {
5228c0984e5SSebastian Reichel 		pm860x_set_bits(info->i2c, PM8607_POWER_UP_LOG,
5238c0984e5SSebastian Reichel 				BAT_WU_LOG, BAT_WU_LOG);
5248c0984e5SSebastian Reichel 		info->start_soc = soc;
5258c0984e5SSebastian Reichel 	}
5268c0984e5SSebastian Reichel 	info->last_capacity = info->start_soc;
5278c0984e5SSebastian Reichel 	dev_dbg(info->dev, "init soc : %d\n", info->last_capacity);
5288c0984e5SSebastian Reichel out:
5298c0984e5SSebastian Reichel 	return;
5308c0984e5SSebastian Reichel }
5318c0984e5SSebastian Reichel 
set_temp_threshold(struct pm860x_battery_info * info,int min,int max)5328c0984e5SSebastian Reichel static void set_temp_threshold(struct pm860x_battery_info *info,
5338c0984e5SSebastian Reichel 			       int min, int max)
5348c0984e5SSebastian Reichel {
5358c0984e5SSebastian Reichel 	int data;
5368c0984e5SSebastian Reichel 
5378c0984e5SSebastian Reichel 	/* (tmp << 8) / 1800 */
5388c0984e5SSebastian Reichel 	if (min <= 0)
5398c0984e5SSebastian Reichel 		data = 0;
5408c0984e5SSebastian Reichel 	else
5418c0984e5SSebastian Reichel 		data = (min << 8) / 1800;
5428c0984e5SSebastian Reichel 	pm860x_reg_write(info->i2c, PM8607_GPADC1_HIGHTH, data);
5438c0984e5SSebastian Reichel 	dev_dbg(info->dev, "TEMP_HIGHTH : min: %d, 0x%x\n", min, data);
5448c0984e5SSebastian Reichel 
5458c0984e5SSebastian Reichel 	if (max <= 0)
5468c0984e5SSebastian Reichel 		data = 0xff;
5478c0984e5SSebastian Reichel 	else
5488c0984e5SSebastian Reichel 		data = (max << 8) / 1800;
5498c0984e5SSebastian Reichel 	pm860x_reg_write(info->i2c, PM8607_GPADC1_LOWTH, data);
5508c0984e5SSebastian Reichel 	dev_dbg(info->dev, "TEMP_LOWTH:max : %d, 0x%x\n", max, data);
5518c0984e5SSebastian Reichel }
5528c0984e5SSebastian Reichel 
measure_temp(struct pm860x_battery_info * info,int * data)5538c0984e5SSebastian Reichel static int measure_temp(struct pm860x_battery_info *info, int *data)
5548c0984e5SSebastian Reichel {
5558c0984e5SSebastian Reichel 	int ret;
5568c0984e5SSebastian Reichel 	int temp;
5578c0984e5SSebastian Reichel 	int min;
5588c0984e5SSebastian Reichel 	int max;
5598c0984e5SSebastian Reichel 
5608c0984e5SSebastian Reichel 	if (info->temp_type == PM860X_TEMP_TINT) {
5618c0984e5SSebastian Reichel 		ret = measure_12bit_voltage(info, PM8607_TINT_MEAS1, data);
5628c0984e5SSebastian Reichel 		if (ret)
5638c0984e5SSebastian Reichel 			return ret;
5648c0984e5SSebastian Reichel 		*data = (*data - 884) * 1000 / 3611;
5658c0984e5SSebastian Reichel 	} else {
5668c0984e5SSebastian Reichel 		ret = measure_12bit_voltage(info, PM8607_GPADC1_MEAS1, data);
5678c0984e5SSebastian Reichel 		if (ret)
5688c0984e5SSebastian Reichel 			return ret;
5698c0984e5SSebastian Reichel 		/* meausered Vtbat(mV) / Ibias_current(11uA)*/
5708c0984e5SSebastian Reichel 		*data = (*data * 1000) / GPBIAS2_GPADC1_UA;
5718c0984e5SSebastian Reichel 
5728c0984e5SSebastian Reichel 		if (*data > TBAT_NEG_25D) {
5738c0984e5SSebastian Reichel 			temp = -30;	/* over cold , suppose -30 roughly */
5748c0984e5SSebastian Reichel 			max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
5758c0984e5SSebastian Reichel 			set_temp_threshold(info, 0, max);
5768c0984e5SSebastian Reichel 		} else if (*data > TBAT_NEG_10D) {
5778c0984e5SSebastian Reichel 			temp = -15;	/* -15 degree, code */
5788c0984e5SSebastian Reichel 			max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
5798c0984e5SSebastian Reichel 			set_temp_threshold(info, 0, max);
5808c0984e5SSebastian Reichel 		} else if (*data > TBAT_0D) {
5818c0984e5SSebastian Reichel 			temp = -5;	/* -5 degree */
5828c0984e5SSebastian Reichel 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
5838c0984e5SSebastian Reichel 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
5848c0984e5SSebastian Reichel 			set_temp_threshold(info, min, max);
5858c0984e5SSebastian Reichel 		} else if (*data > TBAT_10D) {
5868c0984e5SSebastian Reichel 			temp = 5;	/* in range of (0, 10) */
5878c0984e5SSebastian Reichel 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
5888c0984e5SSebastian Reichel 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
5898c0984e5SSebastian Reichel 			set_temp_threshold(info, min, max);
5908c0984e5SSebastian Reichel 		} else if (*data > TBAT_20D) {
5918c0984e5SSebastian Reichel 			temp = 15;	/* in range of (10, 20) */
5928c0984e5SSebastian Reichel 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
5938c0984e5SSebastian Reichel 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
5948c0984e5SSebastian Reichel 			set_temp_threshold(info, min, max);
5958c0984e5SSebastian Reichel 		} else if (*data > TBAT_30D) {
5968c0984e5SSebastian Reichel 			temp = 25;	/* in range of (20, 30) */
5978c0984e5SSebastian Reichel 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
5988c0984e5SSebastian Reichel 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
5998c0984e5SSebastian Reichel 			set_temp_threshold(info, min, max);
6008c0984e5SSebastian Reichel 		} else if (*data > TBAT_40D) {
6018c0984e5SSebastian Reichel 			temp = 35;	/* in range of (30, 40) */
6028c0984e5SSebastian Reichel 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
6038c0984e5SSebastian Reichel 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
6048c0984e5SSebastian Reichel 			set_temp_threshold(info, min, max);
6058c0984e5SSebastian Reichel 		} else {
6068c0984e5SSebastian Reichel 			min = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
6078c0984e5SSebastian Reichel 			set_temp_threshold(info, min, 0);
6088c0984e5SSebastian Reichel 			temp = 45;	/* over heat ,suppose 45 roughly */
6098c0984e5SSebastian Reichel 		}
6108c0984e5SSebastian Reichel 
6118c0984e5SSebastian Reichel 		dev_dbg(info->dev, "temp_C:%d C,temp_mv:%d mv\n", temp, *data);
6128c0984e5SSebastian Reichel 		*data = temp;
6138c0984e5SSebastian Reichel 	}
6148c0984e5SSebastian Reichel 	return 0;
6158c0984e5SSebastian Reichel }
6168c0984e5SSebastian Reichel 
calc_resistor(struct pm860x_battery_info * info)6178c0984e5SSebastian Reichel static int calc_resistor(struct pm860x_battery_info *info)
6188c0984e5SSebastian Reichel {
6198c0984e5SSebastian Reichel 	int vbatt_sum1;
6208c0984e5SSebastian Reichel 	int vbatt_sum2;
6218c0984e5SSebastian Reichel 	int chg_current;
6228c0984e5SSebastian Reichel 	int ibatt_sum1;
6238c0984e5SSebastian Reichel 	int ibatt_sum2;
6248c0984e5SSebastian Reichel 	int data;
6258c0984e5SSebastian Reichel 	int ret;
6268c0984e5SSebastian Reichel 	int i;
6278c0984e5SSebastian Reichel 
6288c0984e5SSebastian Reichel 	ret = measure_current(info, &data);
6298c0984e5SSebastian Reichel 	/* make sure that charging is launched by data > 0 */
6308c0984e5SSebastian Reichel 	if (ret || data < 0)
6318c0984e5SSebastian Reichel 		goto out;
6328c0984e5SSebastian Reichel 
6338c0984e5SSebastian Reichel 	ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
6348c0984e5SSebastian Reichel 	if (ret)
6358c0984e5SSebastian Reichel 		goto out;
6368c0984e5SSebastian Reichel 	/* calculate resistor only in CC charge mode */
6378c0984e5SSebastian Reichel 	if (data < VBATT_RESISTOR_MIN || data > VBATT_RESISTOR_MAX)
6388c0984e5SSebastian Reichel 		goto out;
6398c0984e5SSebastian Reichel 
6408c0984e5SSebastian Reichel 	/* current is saved */
6418c0984e5SSebastian Reichel 	if (set_charger_current(info, 500, &chg_current))
6428c0984e5SSebastian Reichel 		goto out;
6438c0984e5SSebastian Reichel 
6448c0984e5SSebastian Reichel 	/*
6458c0984e5SSebastian Reichel 	 * set charge current as 500mA, wait about 500ms till charging
6468c0984e5SSebastian Reichel 	 * process is launched and stable with the newer charging current.
6478c0984e5SSebastian Reichel 	 */
6488c0984e5SSebastian Reichel 	msleep(500);
6498c0984e5SSebastian Reichel 
6508c0984e5SSebastian Reichel 	for (i = 0, vbatt_sum1 = 0, ibatt_sum1 = 0; i < 10; i++) {
6518c0984e5SSebastian Reichel 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
6528c0984e5SSebastian Reichel 		if (ret)
6538c0984e5SSebastian Reichel 			goto out_meas;
6548c0984e5SSebastian Reichel 		vbatt_sum1 += data;
6558c0984e5SSebastian Reichel 		ret = measure_current(info, &data);
6568c0984e5SSebastian Reichel 		if (ret)
6578c0984e5SSebastian Reichel 			goto out_meas;
6588c0984e5SSebastian Reichel 
6598c0984e5SSebastian Reichel 		if (data < 0)
6608c0984e5SSebastian Reichel 			ibatt_sum1 = ibatt_sum1 - data;	/* discharging */
6618c0984e5SSebastian Reichel 		else
6628c0984e5SSebastian Reichel 			ibatt_sum1 = ibatt_sum1 + data;	/* charging */
6638c0984e5SSebastian Reichel 	}
6648c0984e5SSebastian Reichel 
6658c0984e5SSebastian Reichel 	if (set_charger_current(info, 100, &ret))
6668c0984e5SSebastian Reichel 		goto out_meas;
6678c0984e5SSebastian Reichel 	/*
6688c0984e5SSebastian Reichel 	 * set charge current as 100mA, wait about 500ms till charging
6698c0984e5SSebastian Reichel 	 * process is launched and stable with the newer charging current.
6708c0984e5SSebastian Reichel 	 */
6718c0984e5SSebastian Reichel 	msleep(500);
6728c0984e5SSebastian Reichel 
6738c0984e5SSebastian Reichel 	for (i = 0, vbatt_sum2 = 0, ibatt_sum2 = 0; i < 10; i++) {
6748c0984e5SSebastian Reichel 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
6758c0984e5SSebastian Reichel 		if (ret)
6768c0984e5SSebastian Reichel 			goto out_meas;
6778c0984e5SSebastian Reichel 		vbatt_sum2 += data;
6788c0984e5SSebastian Reichel 		ret = measure_current(info, &data);
6798c0984e5SSebastian Reichel 		if (ret)
6808c0984e5SSebastian Reichel 			goto out_meas;
6818c0984e5SSebastian Reichel 
6828c0984e5SSebastian Reichel 		if (data < 0)
6838c0984e5SSebastian Reichel 			ibatt_sum2 = ibatt_sum2 - data;	/* discharging */
6848c0984e5SSebastian Reichel 		else
6858c0984e5SSebastian Reichel 			ibatt_sum2 = ibatt_sum2 + data;	/* charging */
6868c0984e5SSebastian Reichel 	}
6878c0984e5SSebastian Reichel 
6888c0984e5SSebastian Reichel 	/* restore current setting */
6898c0984e5SSebastian Reichel 	if (set_charger_current(info, chg_current, &ret))
6908c0984e5SSebastian Reichel 		goto out_meas;
6918c0984e5SSebastian Reichel 
6928c0984e5SSebastian Reichel 	if ((vbatt_sum1 > vbatt_sum2) && (ibatt_sum1 > ibatt_sum2) &&
6938c0984e5SSebastian Reichel 			(ibatt_sum2 > 0)) {
6948c0984e5SSebastian Reichel 		/* calculate resistor in discharging case */
6958c0984e5SSebastian Reichel 		data = 1000 * (vbatt_sum1 - vbatt_sum2)
6968c0984e5SSebastian Reichel 		    / (ibatt_sum1 - ibatt_sum2);
6978c0984e5SSebastian Reichel 		if ((data - info->resistor > 0) &&
6988c0984e5SSebastian Reichel 				(data - info->resistor < info->resistor))
6998c0984e5SSebastian Reichel 			info->resistor = data;
7008c0984e5SSebastian Reichel 		if ((info->resistor - data > 0) &&
7018c0984e5SSebastian Reichel 				(info->resistor - data < data))
7028c0984e5SSebastian Reichel 			info->resistor = data;
7038c0984e5SSebastian Reichel 	}
7048c0984e5SSebastian Reichel 	return 0;
7058c0984e5SSebastian Reichel 
7068c0984e5SSebastian Reichel out_meas:
7078c0984e5SSebastian Reichel 	set_charger_current(info, chg_current, &ret);
7088c0984e5SSebastian Reichel out:
7098c0984e5SSebastian Reichel 	return -EINVAL;
7108c0984e5SSebastian Reichel }
7118c0984e5SSebastian Reichel 
calc_capacity(struct pm860x_battery_info * info,int * cap)7128c0984e5SSebastian Reichel static int calc_capacity(struct pm860x_battery_info *info, int *cap)
7138c0984e5SSebastian Reichel {
7148c0984e5SSebastian Reichel 	int ret;
7158c0984e5SSebastian Reichel 	int data;
7168c0984e5SSebastian Reichel 	int ibat;
7178c0984e5SSebastian Reichel 	int cap_ocv = 0;
7188c0984e5SSebastian Reichel 	int cap_cc = 0;
7198c0984e5SSebastian Reichel 
7208c0984e5SSebastian Reichel 	ret = calc_ccnt(info, &ccnt_data);
7218c0984e5SSebastian Reichel 	if (ret)
7228c0984e5SSebastian Reichel 		goto out;
7238c0984e5SSebastian Reichel soc:
7248c0984e5SSebastian Reichel 	data = info->max_capacity * info->start_soc / 100;
7258c0984e5SSebastian Reichel 	if (ccnt_data.total_dischg - ccnt_data.total_chg <= data) {
7268c0984e5SSebastian Reichel 		cap_cc =
7278c0984e5SSebastian Reichel 		    data + ccnt_data.total_chg - ccnt_data.total_dischg;
7288c0984e5SSebastian Reichel 	} else {
7298c0984e5SSebastian Reichel 		clear_ccnt(info, &ccnt_data);
7308c0984e5SSebastian Reichel 		calc_soc(info, OCV_MODE_ACTIVE, &info->start_soc);
7318c0984e5SSebastian Reichel 		dev_dbg(info->dev, "restart soc = %d !\n",
7328c0984e5SSebastian Reichel 			info->start_soc);
7338c0984e5SSebastian Reichel 		goto soc;
7348c0984e5SSebastian Reichel 	}
7358c0984e5SSebastian Reichel 
7368c0984e5SSebastian Reichel 	cap_cc = cap_cc * 100 / info->max_capacity;
7378c0984e5SSebastian Reichel 	if (cap_cc < 0)
7388c0984e5SSebastian Reichel 		cap_cc = 0;
7398c0984e5SSebastian Reichel 	else if (cap_cc > 100)
7408c0984e5SSebastian Reichel 		cap_cc = 100;
7418c0984e5SSebastian Reichel 
7428c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, last cap : %d", __func__,
7438c0984e5SSebastian Reichel 		info->last_capacity);
7448c0984e5SSebastian Reichel 
7458c0984e5SSebastian Reichel 	ret = measure_current(info, &ibat);
7468c0984e5SSebastian Reichel 	if (ret)
7478c0984e5SSebastian Reichel 		goto out;
7488c0984e5SSebastian Reichel 	/* Calculate the capacity when discharging(ibat < 0) */
7498c0984e5SSebastian Reichel 	if (ibat < 0) {
7508c0984e5SSebastian Reichel 		ret = calc_soc(info, OCV_MODE_ACTIVE, &cap_ocv);
7518c0984e5SSebastian Reichel 		if (ret)
7528c0984e5SSebastian Reichel 			cap_ocv = info->last_capacity;
7538c0984e5SSebastian Reichel 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
7548c0984e5SSebastian Reichel 		if (ret)
7558c0984e5SSebastian Reichel 			goto out;
7568c0984e5SSebastian Reichel 		if (data <= LOW_BAT_THRESHOLD) {
7578c0984e5SSebastian Reichel 			/* choose the lower capacity value to report
7588c0984e5SSebastian Reichel 			 * between vbat and CC when vbat < 3.6v;
7598c0984e5SSebastian Reichel 			 * than 3.6v;
7608c0984e5SSebastian Reichel 			 */
7618c0984e5SSebastian Reichel 			*cap = min(cap_ocv, cap_cc);
7628c0984e5SSebastian Reichel 		} else {
7638c0984e5SSebastian Reichel 			/* when detect vbat > 3.6v, but cap_cc < 15,and
7648c0984e5SSebastian Reichel 			 * cap_ocv is 10% larger than cap_cc, we can think
7658c0984e5SSebastian Reichel 			 * CC have some accumulation error, switch to OCV
7668c0984e5SSebastian Reichel 			 * to estimate capacity;
7678c0984e5SSebastian Reichel 			 * */
7688c0984e5SSebastian Reichel 			if (cap_cc < 15 && cap_ocv - cap_cc > 10)
7698c0984e5SSebastian Reichel 				*cap = cap_ocv;
7708c0984e5SSebastian Reichel 			else
7718c0984e5SSebastian Reichel 				*cap = cap_cc;
7728c0984e5SSebastian Reichel 		}
7738c0984e5SSebastian Reichel 		/* when discharging, make sure current capacity
7748c0984e5SSebastian Reichel 		 * is lower than last*/
7758c0984e5SSebastian Reichel 		if (*cap > info->last_capacity)
7768c0984e5SSebastian Reichel 			*cap = info->last_capacity;
7778c0984e5SSebastian Reichel 	} else {
7788c0984e5SSebastian Reichel 		*cap = cap_cc;
7798c0984e5SSebastian Reichel 	}
7808c0984e5SSebastian Reichel 	info->last_capacity = *cap;
7818c0984e5SSebastian Reichel 
7828c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, cap_ocv:%d cap_cc:%d, cap:%d\n",
7838c0984e5SSebastian Reichel 		(ibat < 0) ? "discharging" : "charging",
7848c0984e5SSebastian Reichel 		 cap_ocv, cap_cc, *cap);
7858c0984e5SSebastian Reichel 	/*
7868c0984e5SSebastian Reichel 	 * store the current capacity to RTC domain register,
7878c0984e5SSebastian Reichel 	 * after next power up , it will be restored.
7888c0984e5SSebastian Reichel 	 */
7898c0984e5SSebastian Reichel 	pm860x_set_bits(info->i2c, PM8607_RTC_MISC2, RTC_SOC_5LSB,
7908c0984e5SSebastian Reichel 			(*cap & 0x1F) << 3);
7918c0984e5SSebastian Reichel 	pm860x_set_bits(info->i2c, PM8607_RTC1, RTC_SOC_3MSB,
7928c0984e5SSebastian Reichel 			((*cap >> 5) & 0x3));
7938c0984e5SSebastian Reichel 	return 0;
7948c0984e5SSebastian Reichel out:
7958c0984e5SSebastian Reichel 	return ret;
7968c0984e5SSebastian Reichel }
7978c0984e5SSebastian Reichel 
pm860x_external_power_changed(struct power_supply * psy)7988c0984e5SSebastian Reichel static void pm860x_external_power_changed(struct power_supply *psy)
7998c0984e5SSebastian Reichel {
8008c0984e5SSebastian Reichel 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
8018c0984e5SSebastian Reichel 
8028c0984e5SSebastian Reichel 	calc_resistor(info);
8038c0984e5SSebastian Reichel }
8048c0984e5SSebastian Reichel 
pm860x_batt_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)8058c0984e5SSebastian Reichel static int pm860x_batt_get_prop(struct power_supply *psy,
8068c0984e5SSebastian Reichel 				enum power_supply_property psp,
8078c0984e5SSebastian Reichel 				union power_supply_propval *val)
8088c0984e5SSebastian Reichel {
8098c0984e5SSebastian Reichel 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
8108c0984e5SSebastian Reichel 	int data;
8118c0984e5SSebastian Reichel 	int ret;
8128c0984e5SSebastian Reichel 
8138c0984e5SSebastian Reichel 	switch (psp) {
8148c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
8158c0984e5SSebastian Reichel 		val->intval = info->present;
8168c0984e5SSebastian Reichel 		break;
8178c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
8188c0984e5SSebastian Reichel 		ret = calc_capacity(info, &data);
8198c0984e5SSebastian Reichel 		if (ret)
8208c0984e5SSebastian Reichel 			return ret;
8218c0984e5SSebastian Reichel 		if (data < 0)
8228c0984e5SSebastian Reichel 			data = 0;
8238c0984e5SSebastian Reichel 		else if (data > 100)
8248c0984e5SSebastian Reichel 			data = 100;
8258c0984e5SSebastian Reichel 		/* return 100 if battery is not attached */
8268c0984e5SSebastian Reichel 		if (!info->present)
8278c0984e5SSebastian Reichel 			data = 100;
8288c0984e5SSebastian Reichel 		val->intval = data;
8298c0984e5SSebastian Reichel 		break;
8308c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
8318c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
8328c0984e5SSebastian Reichel 		break;
8338c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
8348c0984e5SSebastian Reichel 		/* return real vbatt Voltage */
8358c0984e5SSebastian Reichel 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
8368c0984e5SSebastian Reichel 		if (ret)
8378c0984e5SSebastian Reichel 			return ret;
8388c0984e5SSebastian Reichel 		val->intval = data * 1000;
8398c0984e5SSebastian Reichel 		break;
8408c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
8418c0984e5SSebastian Reichel 		/* return Open Circuit Voltage (not measured voltage) */
8428c0984e5SSebastian Reichel 		ret = calc_ocv(info, &data);
8438c0984e5SSebastian Reichel 		if (ret)
8448c0984e5SSebastian Reichel 			return ret;
8458c0984e5SSebastian Reichel 		val->intval = data * 1000;
8468c0984e5SSebastian Reichel 		break;
8478c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
8488c0984e5SSebastian Reichel 		ret = measure_current(info, &data);
8498c0984e5SSebastian Reichel 		if (ret)
8508c0984e5SSebastian Reichel 			return ret;
8518c0984e5SSebastian Reichel 		val->intval = data;
8528c0984e5SSebastian Reichel 		break;
8538c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
8548c0984e5SSebastian Reichel 		if (info->present) {
8558c0984e5SSebastian Reichel 			ret = measure_temp(info, &data);
8568c0984e5SSebastian Reichel 			if (ret)
8578c0984e5SSebastian Reichel 				return ret;
8588c0984e5SSebastian Reichel 			data *= 10;
8598c0984e5SSebastian Reichel 		} else {
8608c0984e5SSebastian Reichel 			/* Fake Temp 25C Without Battery */
8618c0984e5SSebastian Reichel 			data = 250;
8628c0984e5SSebastian Reichel 		}
8638c0984e5SSebastian Reichel 		val->intval = data;
8648c0984e5SSebastian Reichel 		break;
8658c0984e5SSebastian Reichel 	default:
8668c0984e5SSebastian Reichel 		return -ENODEV;
8678c0984e5SSebastian Reichel 	}
8688c0984e5SSebastian Reichel 	return 0;
8698c0984e5SSebastian Reichel }
8708c0984e5SSebastian Reichel 
pm860x_batt_set_prop(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)8718c0984e5SSebastian Reichel static int pm860x_batt_set_prop(struct power_supply *psy,
8728c0984e5SSebastian Reichel 				       enum power_supply_property psp,
8738c0984e5SSebastian Reichel 				       const union power_supply_propval *val)
8748c0984e5SSebastian Reichel {
8758c0984e5SSebastian Reichel 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
8768c0984e5SSebastian Reichel 
8778c0984e5SSebastian Reichel 	switch (psp) {
8788c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
8798c0984e5SSebastian Reichel 		clear_ccnt(info, &ccnt_data);
8808c0984e5SSebastian Reichel 		info->start_soc = 100;
8818c0984e5SSebastian Reichel 		dev_dbg(info->dev, "chg done, update soc = %d\n",
8828c0984e5SSebastian Reichel 			info->start_soc);
8838c0984e5SSebastian Reichel 		break;
8848c0984e5SSebastian Reichel 	default:
8858c0984e5SSebastian Reichel 		return -EPERM;
8868c0984e5SSebastian Reichel 	}
8878c0984e5SSebastian Reichel 
8888c0984e5SSebastian Reichel 	return 0;
8898c0984e5SSebastian Reichel }
8908c0984e5SSebastian Reichel 
8918c0984e5SSebastian Reichel 
8928c0984e5SSebastian Reichel static enum power_supply_property pm860x_batt_props[] = {
8938c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
8948c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
8958c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
8968c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
8978c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
8988c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
8998c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
9008c0984e5SSebastian Reichel };
9018c0984e5SSebastian Reichel 
9028c0984e5SSebastian Reichel static const struct power_supply_desc pm860x_battery_desc = {
9038c0984e5SSebastian Reichel 	.name			= "battery-monitor",
9048c0984e5SSebastian Reichel 	.type			= POWER_SUPPLY_TYPE_BATTERY,
9058c0984e5SSebastian Reichel 	.properties		= pm860x_batt_props,
9068c0984e5SSebastian Reichel 	.num_properties		= ARRAY_SIZE(pm860x_batt_props),
9078c0984e5SSebastian Reichel 	.get_property		= pm860x_batt_get_prop,
9088c0984e5SSebastian Reichel 	.set_property		= pm860x_batt_set_prop,
9098c0984e5SSebastian Reichel 	.external_power_changed	= pm860x_external_power_changed,
9108c0984e5SSebastian Reichel };
9118c0984e5SSebastian Reichel 
pm860x_battery_probe(struct platform_device * pdev)9128c0984e5SSebastian Reichel static int pm860x_battery_probe(struct platform_device *pdev)
9138c0984e5SSebastian Reichel {
9148c0984e5SSebastian Reichel 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
9158c0984e5SSebastian Reichel 	struct pm860x_battery_info *info;
9168c0984e5SSebastian Reichel 	struct pm860x_power_pdata *pdata;
9178c0984e5SSebastian Reichel 	int ret;
9188c0984e5SSebastian Reichel 
9198c0984e5SSebastian Reichel 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
9208c0984e5SSebastian Reichel 	if (!info)
9218c0984e5SSebastian Reichel 		return -ENOMEM;
9228c0984e5SSebastian Reichel 
9238c0984e5SSebastian Reichel 	info->irq_cc = platform_get_irq(pdev, 0);
924164eaf6bSTang Bin 	if (info->irq_cc <= 0)
9258c0984e5SSebastian Reichel 		return -EINVAL;
9268c0984e5SSebastian Reichel 
9278c0984e5SSebastian Reichel 	info->irq_batt = platform_get_irq(pdev, 1);
928164eaf6bSTang Bin 	if (info->irq_batt <= 0)
9298c0984e5SSebastian Reichel 		return -EINVAL;
9308c0984e5SSebastian Reichel 
9318c0984e5SSebastian Reichel 	info->chip = chip;
9328c0984e5SSebastian Reichel 	info->i2c =
9338c0984e5SSebastian Reichel 	    (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
9348c0984e5SSebastian Reichel 	info->dev = &pdev->dev;
9358c0984e5SSebastian Reichel 	info->status = POWER_SUPPLY_STATUS_UNKNOWN;
9368c0984e5SSebastian Reichel 	pdata = pdev->dev.platform_data;
9378c0984e5SSebastian Reichel 
9388c0984e5SSebastian Reichel 	mutex_init(&info->lock);
9398c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, info);
9408c0984e5SSebastian Reichel 
9418c0984e5SSebastian Reichel 	pm860x_init_battery(info);
9428c0984e5SSebastian Reichel 
9438c0984e5SSebastian Reichel 	if (pdata && pdata->max_capacity)
9448c0984e5SSebastian Reichel 		info->max_capacity = pdata->max_capacity;
9458c0984e5SSebastian Reichel 	else
9468c0984e5SSebastian Reichel 		info->max_capacity = 1500;	/* set default capacity */
9478c0984e5SSebastian Reichel 	if (pdata && pdata->resistor)
9488c0984e5SSebastian Reichel 		info->resistor = pdata->resistor;
9498c0984e5SSebastian Reichel 	else
9508c0984e5SSebastian Reichel 		info->resistor = 300;	/* set default internal resistor */
9518c0984e5SSebastian Reichel 
9528c0984e5SSebastian Reichel 	info->battery = devm_power_supply_register(&pdev->dev,
9538c0984e5SSebastian Reichel 						   &pm860x_battery_desc,
9548c0984e5SSebastian Reichel 						   NULL);
9558c0984e5SSebastian Reichel 	if (IS_ERR(info->battery))
9568c0984e5SSebastian Reichel 		return PTR_ERR(info->battery);
9578c0984e5SSebastian Reichel 	info->battery->dev.parent = &pdev->dev;
9588c0984e5SSebastian Reichel 
9598c0984e5SSebastian Reichel 	ret = devm_request_threaded_irq(chip->dev, info->irq_cc, NULL,
9608c0984e5SSebastian Reichel 					pm860x_coulomb_handler, IRQF_ONESHOT,
9618c0984e5SSebastian Reichel 					"coulomb", info);
9628c0984e5SSebastian Reichel 	if (ret < 0) {
9638c0984e5SSebastian Reichel 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
9648c0984e5SSebastian Reichel 			info->irq_cc, ret);
9658c0984e5SSebastian Reichel 		return ret;
9668c0984e5SSebastian Reichel 	}
9678c0984e5SSebastian Reichel 
9688c0984e5SSebastian Reichel 	ret = devm_request_threaded_irq(chip->dev, info->irq_batt, NULL,
9698c0984e5SSebastian Reichel 					pm860x_batt_handler,
9708c0984e5SSebastian Reichel 					IRQF_ONESHOT, "battery", info);
9718c0984e5SSebastian Reichel 	if (ret < 0) {
9728c0984e5SSebastian Reichel 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
9738c0984e5SSebastian Reichel 			info->irq_batt, ret);
9748c0984e5SSebastian Reichel 		return ret;
9758c0984e5SSebastian Reichel 	}
9768c0984e5SSebastian Reichel 
9778c0984e5SSebastian Reichel 
9788c0984e5SSebastian Reichel 	return 0;
9798c0984e5SSebastian Reichel }
9808c0984e5SSebastian Reichel 
9818c0984e5SSebastian Reichel #ifdef CONFIG_PM_SLEEP
pm860x_battery_suspend(struct device * dev)9828c0984e5SSebastian Reichel static int pm860x_battery_suspend(struct device *dev)
9838c0984e5SSebastian Reichel {
9848c0984e5SSebastian Reichel 	struct platform_device *pdev = to_platform_device(dev);
9858c0984e5SSebastian Reichel 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
9868c0984e5SSebastian Reichel 
9878c0984e5SSebastian Reichel 	if (device_may_wakeup(dev))
9888c0984e5SSebastian Reichel 		chip->wakeup_flag |= 1 << PM8607_IRQ_CC;
9898c0984e5SSebastian Reichel 	return 0;
9908c0984e5SSebastian Reichel }
9918c0984e5SSebastian Reichel 
pm860x_battery_resume(struct device * dev)9928c0984e5SSebastian Reichel static int pm860x_battery_resume(struct device *dev)
9938c0984e5SSebastian Reichel {
9948c0984e5SSebastian Reichel 	struct platform_device *pdev = to_platform_device(dev);
9958c0984e5SSebastian Reichel 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
9968c0984e5SSebastian Reichel 
9978c0984e5SSebastian Reichel 	if (device_may_wakeup(dev))
9988c0984e5SSebastian Reichel 		chip->wakeup_flag &= ~(1 << PM8607_IRQ_CC);
9998c0984e5SSebastian Reichel 	return 0;
10008c0984e5SSebastian Reichel }
10018c0984e5SSebastian Reichel #endif
10028c0984e5SSebastian Reichel 
10038c0984e5SSebastian Reichel static SIMPLE_DEV_PM_OPS(pm860x_battery_pm_ops,
10048c0984e5SSebastian Reichel 			pm860x_battery_suspend, pm860x_battery_resume);
10058c0984e5SSebastian Reichel 
10068c0984e5SSebastian Reichel static struct platform_driver pm860x_battery_driver = {
10078c0984e5SSebastian Reichel 	.driver = {
10088c0984e5SSebastian Reichel 		   .name = "88pm860x-battery",
10098c0984e5SSebastian Reichel 		   .pm = &pm860x_battery_pm_ops,
10108c0984e5SSebastian Reichel 	},
10118c0984e5SSebastian Reichel 	.probe = pm860x_battery_probe,
10128c0984e5SSebastian Reichel };
10138c0984e5SSebastian Reichel module_platform_driver(pm860x_battery_driver);
10148c0984e5SSebastian Reichel 
10158c0984e5SSebastian Reichel MODULE_DESCRIPTION("Marvell 88PM860x Battery driver");
10168c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
1017