Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Battery driver for Marvell 88PM860x PMIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2012 Marvell International Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Author:	Jett Zhou <jtzhou@marvell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *		Haojian Zhuang <haojian.zhuang@marvell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/mfd/88pm860x.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) /* bit definitions of Status Query Interface 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #define STATUS2_CHG			(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define STATUS2_BAT			(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define STATUS2_VBUS			(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) /* bit definitions of Measurement Enable 1 Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define MEAS1_TINT			(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define MEAS1_GP1			(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) /* bit definitions of Measurement Enable 3 Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define MEAS3_IBAT			(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define MEAS3_BAT_DET			(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #define MEAS3_CC			(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) /* bit definitions of Measurement Off Time Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define MEAS_OFF_SLEEP_EN		(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) /* bit definitions of GPADC Bias Current 2 Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define GPBIAS2_GPADC1_SET		(2 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) /* GPADC1 Bias Current value in uA unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define GPBIAS2_GPADC1_UA		((GPBIAS2_GPADC1_SET >> 4) * 5 + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) /* bit definitions of GPADC Misc 1 Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define GPMISC1_GPADC_EN		(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) /* bit definitions of Charger Control 6 Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define CC6_BAT_DET_GPADC1		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) /* bit definitions of Coulomb Counter Reading Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define CCNT_AVG_SEL			(4 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) /* bit definitions of RTC miscellaneous Register1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define RTC_SOC_5LSB		(0x1F << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) /* bit definitions of RTC Register1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define RTC_SOC_3MSB		(0x7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) /* bit definitions of Power up Log register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #define BAT_WU_LOG			(1<<6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) /* coulomb counter index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define CCNT_POS1			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define CCNT_POS2			1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define CCNT_NEG1			2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define CCNT_NEG2			3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define CCNT_SPOS			4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define CCNT_SNEG			5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /* OCV -- Open Circuit Voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define OCV_MODE_ACTIVE			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define OCV_MODE_SLEEP			1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) /* Vbat range of CC for measuring Rbat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define LOW_BAT_THRESHOLD		3600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define VBATT_RESISTOR_MIN		3800
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define VBATT_RESISTOR_MAX		4100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) /* TBAT for batt, TINT for chip itself */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define PM860X_TEMP_TINT		(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define PM860X_TEMP_TBAT		(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * Battery temperature based on NTC resistor, defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * corresponding resistor value  -- Ohm / C degeree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define TBAT_NEG_25D		127773	/* -25 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define TBAT_NEG_10D		54564	/* -10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define TBAT_0D			32330	/* 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define TBAT_10D		19785	/* 10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #define TBAT_20D		12468	/* 20 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define TBAT_30D		8072	/* 30 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define TBAT_40D		5356	/* 40 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) struct pm860x_battery_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	struct pm860x_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	struct i2c_client *i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	struct power_supply *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	int irq_cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	int irq_batt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	int max_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	int resistor;		/* Battery Internal Resistor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	int last_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	int start_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	unsigned present:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	unsigned temp_type:1;	/* TINT or TBAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) struct ccnt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	unsigned long long int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	unsigned long long int neg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	unsigned int spos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	unsigned int sneg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	int total_chg;		/* mAh(3.6C) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	int total_dischg;	/* mAh(3.6C) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  * State of Charge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  * The first number is mAh(=3.6C), and the second number is percent point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) static int array_soc[][2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	{4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	{4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	{4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	{4013, 85}, {4006, 84}, {3995, 83}, {3987, 82}, {3982, 81},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	{3976, 80}, {3968, 79}, {3962, 78}, {3954, 77}, {3946, 76},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	{3941, 75}, {3934, 74}, {3929, 73}, {3922, 72}, {3916, 71},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	{3910, 70}, {3904, 69}, {3898, 68}, {3892, 67}, {3887, 66},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	{3880, 65}, {3874, 64}, {3868, 63}, {3862, 62}, {3854, 61},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	{3849, 60}, {3843, 59}, {3840, 58}, {3833, 57}, {3829, 56},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	{3824, 55}, {3818, 54}, {3815, 53}, {3810, 52}, {3808, 51},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	{3804, 50}, {3801, 49}, {3798, 48}, {3796, 47}, {3792, 46},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	{3789, 45}, {3785, 44}, {3784, 43}, {3782, 42}, {3780, 41},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	{3777, 40}, {3776, 39}, {3774, 38}, {3772, 37}, {3771, 36},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	{3769, 35}, {3768, 34}, {3764, 33}, {3763, 32}, {3760, 31},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	{3760, 30}, {3754, 29}, {3750, 28}, {3749, 27}, {3744, 26},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	{3740, 25}, {3734, 24}, {3732, 23}, {3728, 22}, {3726, 21},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	{3720, 20}, {3716, 19}, {3709, 18}, {3703, 17}, {3698, 16},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	{3692, 15}, {3683, 14}, {3675, 13}, {3670, 12}, {3665, 11},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	{3661, 10}, {3649, 9}, {3637, 8}, {3622, 7}, {3609, 6},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	{3580, 5}, {3558, 4}, {3540, 3}, {3510, 2}, {3429, 1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) static struct ccnt ccnt_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  * register 1 bit[7:0] -- bit[11:4] of measured value of voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * register 0 bit[3:0] -- bit[3:0] of measured value of voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) static int measure_12bit_voltage(struct pm860x_battery_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 				 int offset, int *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	unsigned char buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	ret = pm860x_bulk_read(info->i2c, offset, 2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	*data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	/* V_MEAS(mV) = data * 1.8 * 1000 / (2^12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	*data = ((*data & 0xfff) * 9 * 25) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) static int measure_vbatt(struct pm860x_battery_info *info, int state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 			 int *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	unsigned char buf[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	case OCV_MODE_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		ret = measure_12bit_voltage(info, PM8607_VBAT_MEAS1, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		/* V_BATT_MEAS(mV) = value * 3 * 1.8 * 1000 / (2^12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		*data *= 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	case OCV_MODE_SLEEP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		 * voltage value of VBATT in sleep mode is saved in different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		 * registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		 * bit[11:10] -- bit[7:6] of LDO9(0x18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		 * bit[9:8] -- bit[7:6] of LDO8(0x17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		 * bit[7:6] -- bit[7:6] of LDO7(0x16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		 * bit[5:4] -- bit[7:6] of LDO6(0x15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		 * bit[3:0] -- bit[7:4] of LDO5(0x14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		ret = pm860x_bulk_read(info->i2c, PM8607_LDO5, 5, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		ret = ((buf[4] >> 6) << 10) | ((buf[3] >> 6) << 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		    | ((buf[2] >> 6) << 6) | ((buf[1] >> 6) << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		    | (buf[0] >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		/* V_BATT_MEAS(mV) = data * 3 * 1.8 * 1000 / (2^12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		*data = ((*data & 0xff) * 27 * 25) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  * Return value is signed data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * Negative value means discharging, and positive value means charging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) static int measure_current(struct pm860x_battery_info *info, int *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	unsigned char buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	short s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	ret = pm860x_bulk_read(info->i2c, PM8607_IBAT_MEAS1, 2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	/* current(mA) = value * 0.125 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	*data = s >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) static int set_charger_current(struct pm860x_battery_info *info, int data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 			       int *old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	if (data < 50 || data > 1600 || !old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	data = ((data - 50) / 50) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	*old = pm860x_reg_read(info->i2c, PM8607_CHG_CTRL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	*old = (*old & 0x1f) * 50 + 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) static int read_ccnt(struct pm860x_battery_info *info, int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		     int *ccnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	unsigned char buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7, offset & 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	ret = pm860x_bulk_read(info->i2c, PM8607_CCNT_MEAS1, 2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	*ccnt = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) static int calc_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	unsigned int sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	ret = read_ccnt(info, CCNT_POS1, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	sum = data & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	ret = read_ccnt(info, CCNT_POS2, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	sum |= (data & 0xffff) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	ccnt->pos += sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	ret = read_ccnt(info, CCNT_NEG1, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	sum = data & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	ret = read_ccnt(info, CCNT_NEG2, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	sum |= (data & 0xffff) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	sum = ~sum + 1;		/* since it's negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	ccnt->neg += sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	ret = read_ccnt(info, CCNT_SPOS, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	ccnt->spos += data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	ret = read_ccnt(info, CCNT_SNEG, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	 * charge(mAh)  = count * 1.6984 * 1e(-8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	 *              = count * 16984 * 1.024 * 1.024 * 1.024 / (2 ^ 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	 *              = count * 18236 / (2 ^ 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	ccnt->total_chg = (int) ((ccnt->pos * 18236) >> 40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	ccnt->total_dischg = (int) ((ccnt->neg * 18236) >> 40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) static int clear_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	memset(ccnt, 0, sizeof(*ccnt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	/* read to clear ccnt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	read_ccnt(info, CCNT_POS1, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	read_ccnt(info, CCNT_POS2, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	read_ccnt(info, CCNT_NEG1, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	read_ccnt(info, CCNT_NEG2, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	read_ccnt(info, CCNT_SPOS, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	read_ccnt(info, CCNT_SNEG, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) /* Calculate Open Circuit Voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) static int calc_ocv(struct pm860x_battery_info *info, int *ocv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	int vbatt_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	int vbatt_sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	int ibatt_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	int ibatt_sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	if (!ocv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	for (i = 0, ibatt_sum = 0, vbatt_sum = 0; i < 10; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		vbatt_sum += data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		ret = measure_current(info, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		ibatt_sum += data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	vbatt_avg = vbatt_sum / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	ibatt_avg = ibatt_sum / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	mutex_lock(&info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	if (info->present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		*ocv = vbatt_avg - ibatt_avg * info->resistor / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		*ocv = vbatt_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	mutex_unlock(&info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	dev_dbg(info->dev, "VBAT average:%d, OCV:%d\n", vbatt_avg, *ocv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) /* Calculate State of Charge (percent points) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) static int calc_soc(struct pm860x_battery_info *info, int state, int *soc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	int ocv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	if (!soc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	case OCV_MODE_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		ret = calc_ocv(info, &ocv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	case OCV_MODE_SLEEP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	count = ARRAY_SIZE(array_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	if (ocv < array_soc[count - 1][0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		*soc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		if (ocv >= array_soc[i][0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			*soc = array_soc[i][1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) static irqreturn_t pm860x_coulomb_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	struct pm860x_battery_info *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	calc_ccnt(info, &ccnt_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) static irqreturn_t pm860x_batt_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	struct pm860x_battery_info *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	mutex_lock(&info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	if (ret & STATUS2_BAT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		info->present = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		info->temp_type = PM860X_TEMP_TBAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		info->present = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		info->temp_type = PM860X_TEMP_TINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	mutex_unlock(&info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	/* clear ccnt since battery is attached or dettached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	clear_ccnt(info, &ccnt_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) static void pm860x_init_battery(struct pm860x_battery_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	unsigned char buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	int bat_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	int soc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	/* measure enable on GPADC1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	data = MEAS1_GP1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	if (info->temp_type == PM860X_TEMP_TINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		data |= MEAS1_TINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN1, data, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	/* measure enable on IBAT, BAT_DET, CC. IBAT is depend on CC. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	data = MEAS3_IBAT | MEAS3_BAT_DET | MEAS3_CC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN3, data, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	/* measure disable CC in sleep time  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME1, 0x82);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME2, 0x6c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	/* enable GPADC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	ret = pm860x_set_bits(info->i2c, PM8607_GPADC_MISC1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 			    GPMISC1_GPADC_EN, GPMISC1_GPADC_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	/* detect battery via GPADC1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			    CC6_BAT_DET_GPADC1, CC6_BAT_DET_GPADC1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7 << 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			      CCNT_AVG_SEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	/* set GPADC1 bias */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	ret = pm860x_set_bits(info->i2c, PM8607_GP_BIAS2, 0xF << 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 			      GPBIAS2_GPADC1_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	/* check whether battery present) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	mutex_lock(&info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		mutex_unlock(&info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	if (ret & STATUS2_BAT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		info->present = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		info->temp_type = PM860X_TEMP_TBAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		info->present = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		info->temp_type = PM860X_TEMP_TINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	mutex_unlock(&info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	ret = calc_soc(info, OCV_MODE_ACTIVE, &soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	data = pm860x_reg_read(info->i2c, PM8607_POWER_UP_LOG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	bat_remove = data & BAT_WU_LOG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	dev_dbg(info->dev, "battery wake up? %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		bat_remove != 0 ? "yes" : "no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	/* restore SOC from RTC domain register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	if (bat_remove == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		buf[0] = pm860x_reg_read(info->i2c, PM8607_RTC_MISC2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		buf[1] = pm860x_reg_read(info->i2c, PM8607_RTC1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		data = ((buf[1] & 0x3) << 5) | ((buf[0] >> 3) & 0x1F);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		if (data > soc + 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			info->start_soc = soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		else if (data < soc - 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 			info->start_soc = soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 			info->start_soc = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		dev_dbg(info->dev, "soc_rtc %d, soc_ocv :%d\n", data, soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		pm860x_set_bits(info->i2c, PM8607_POWER_UP_LOG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 				BAT_WU_LOG, BAT_WU_LOG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		info->start_soc = soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	info->last_capacity = info->start_soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	dev_dbg(info->dev, "init soc : %d\n", info->last_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) static void set_temp_threshold(struct pm860x_battery_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 			       int min, int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	/* (tmp << 8) / 1800 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (min <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		data = (min << 8) / 1800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	pm860x_reg_write(info->i2c, PM8607_GPADC1_HIGHTH, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	dev_dbg(info->dev, "TEMP_HIGHTH : min: %d, 0x%x\n", min, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (max <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		data = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		data = (max << 8) / 1800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	pm860x_reg_write(info->i2c, PM8607_GPADC1_LOWTH, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	dev_dbg(info->dev, "TEMP_LOWTH:max : %d, 0x%x\n", max, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) static int measure_temp(struct pm860x_battery_info *info, int *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	int min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	int max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	if (info->temp_type == PM860X_TEMP_TINT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		ret = measure_12bit_voltage(info, PM8607_TINT_MEAS1, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		*data = (*data - 884) * 1000 / 3611;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		ret = measure_12bit_voltage(info, PM8607_GPADC1_MEAS1, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		/* meausered Vtbat(mV) / Ibias_current(11uA)*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		*data = (*data * 1000) / GPBIAS2_GPADC1_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		if (*data > TBAT_NEG_25D) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 			temp = -30;	/* over cold , suppose -30 roughly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 			max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			set_temp_threshold(info, 0, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		} else if (*data > TBAT_NEG_10D) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 			temp = -15;	/* -15 degree, code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 			set_temp_threshold(info, 0, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		} else if (*data > TBAT_0D) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 			temp = -5;	/* -5 degree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			set_temp_threshold(info, min, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		} else if (*data > TBAT_10D) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 			temp = 5;	/* in range of (0, 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 			set_temp_threshold(info, min, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		} else if (*data > TBAT_20D) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			temp = 15;	/* in range of (10, 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 			set_temp_threshold(info, min, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		} else if (*data > TBAT_30D) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			temp = 25;	/* in range of (20, 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 			set_temp_threshold(info, min, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		} else if (*data > TBAT_40D) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 			temp = 35;	/* in range of (30, 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 			set_temp_threshold(info, min, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			min = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 			set_temp_threshold(info, min, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			temp = 45;	/* over heat ,suppose 45 roughly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		dev_dbg(info->dev, "temp_C:%d C,temp_mv:%d mv\n", temp, *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		*data = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) static int calc_resistor(struct pm860x_battery_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	int vbatt_sum1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	int vbatt_sum2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	int chg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	int ibatt_sum1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	int ibatt_sum2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	ret = measure_current(info, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	/* make sure that charging is launched by data > 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (ret || data < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	/* calculate resistor only in CC charge mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (data < VBATT_RESISTOR_MIN || data > VBATT_RESISTOR_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	/* current is saved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (set_charger_current(info, 500, &chg_current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	 * set charge current as 500mA, wait about 500ms till charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	 * process is launched and stable with the newer charging current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	msleep(500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	for (i = 0, vbatt_sum1 = 0, ibatt_sum1 = 0; i < 10; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			goto out_meas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		vbatt_sum1 += data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		ret = measure_current(info, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			goto out_meas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		if (data < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 			ibatt_sum1 = ibatt_sum1 - data;	/* discharging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			ibatt_sum1 = ibatt_sum1 + data;	/* charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	if (set_charger_current(info, 100, &ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		goto out_meas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	 * set charge current as 100mA, wait about 500ms till charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	 * process is launched and stable with the newer charging current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	msleep(500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	for (i = 0, vbatt_sum2 = 0, ibatt_sum2 = 0; i < 10; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 			goto out_meas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		vbatt_sum2 += data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		ret = measure_current(info, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			goto out_meas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		if (data < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			ibatt_sum2 = ibatt_sum2 - data;	/* discharging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			ibatt_sum2 = ibatt_sum2 + data;	/* charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	/* restore current setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	if (set_charger_current(info, chg_current, &ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		goto out_meas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	if ((vbatt_sum1 > vbatt_sum2) && (ibatt_sum1 > ibatt_sum2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 			(ibatt_sum2 > 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		/* calculate resistor in discharging case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		data = 1000 * (vbatt_sum1 - vbatt_sum2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		    / (ibatt_sum1 - ibatt_sum2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		if ((data - info->resistor > 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 				(data - info->resistor < info->resistor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			info->resistor = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		if ((info->resistor - data > 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 				(info->resistor - data < data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			info->resistor = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) out_meas:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	set_charger_current(info, chg_current, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) static int calc_capacity(struct pm860x_battery_info *info, int *cap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	int ibat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	int cap_ocv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	int cap_cc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	ret = calc_ccnt(info, &ccnt_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) soc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	data = info->max_capacity * info->start_soc / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	if (ccnt_data.total_dischg - ccnt_data.total_chg <= data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		cap_cc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		    data + ccnt_data.total_chg - ccnt_data.total_dischg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		clear_ccnt(info, &ccnt_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		calc_soc(info, OCV_MODE_ACTIVE, &info->start_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		dev_dbg(info->dev, "restart soc = %d !\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			info->start_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		goto soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	cap_cc = cap_cc * 100 / info->max_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	if (cap_cc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		cap_cc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	else if (cap_cc > 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		cap_cc = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	dev_dbg(info->dev, "%s, last cap : %d", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		info->last_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	ret = measure_current(info, &ibat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	/* Calculate the capacity when discharging(ibat < 0) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	if (ibat < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		ret = calc_soc(info, OCV_MODE_ACTIVE, &cap_ocv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			cap_ocv = info->last_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		if (data <= LOW_BAT_THRESHOLD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			/* choose the lower capacity value to report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 			 * between vbat and CC when vbat < 3.6v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 			 * than 3.6v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 			*cap = min(cap_ocv, cap_cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			/* when detect vbat > 3.6v, but cap_cc < 15,and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			 * cap_ocv is 10% larger than cap_cc, we can think
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			 * CC have some accumulation error, switch to OCV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 			 * to estimate capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 			 * */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			if (cap_cc < 15 && cap_ocv - cap_cc > 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				*cap = cap_ocv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 				*cap = cap_cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		/* when discharging, make sure current capacity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		 * is lower than last*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		if (*cap > info->last_capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			*cap = info->last_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		*cap = cap_cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	info->last_capacity = *cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	dev_dbg(info->dev, "%s, cap_ocv:%d cap_cc:%d, cap:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		(ibat < 0) ? "discharging" : "charging",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		 cap_ocv, cap_cc, *cap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	 * store the current capacity to RTC domain register,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	 * after next power up , it will be restored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	pm860x_set_bits(info->i2c, PM8607_RTC_MISC2, RTC_SOC_5LSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			(*cap & 0x1F) << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	pm860x_set_bits(info->i2c, PM8607_RTC1, RTC_SOC_3MSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			((*cap >> 5) & 0x3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) static void pm860x_external_power_changed(struct power_supply *psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	calc_resistor(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) static int pm860x_batt_get_prop(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 				enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 				union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		val->intval = info->present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		ret = calc_capacity(info, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		if (data < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		else if (data > 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			data = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		/* return 100 if battery is not attached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		if (!info->present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			data = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		val->intval = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	case POWER_SUPPLY_PROP_TECHNOLOGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		/* return real vbatt Voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		val->intval = data * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		/* return Open Circuit Voltage (not measured voltage) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		ret = calc_ocv(info, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		val->intval = data * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		ret = measure_current(info, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		val->intval = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		if (info->present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			ret = measure_temp(info, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			data *= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			/* Fake Temp 25C Without Battery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			data = 250;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		val->intval = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) static int pm860x_batt_set_prop(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 				       enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 				       const union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	case POWER_SUPPLY_PROP_CHARGE_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		clear_ccnt(info, &ccnt_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		info->start_soc = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		dev_dbg(info->dev, "chg done, update soc = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			info->start_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) static enum power_supply_property pm860x_batt_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) static const struct power_supply_desc pm860x_battery_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	.name			= "battery-monitor",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	.type			= POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	.properties		= pm860x_batt_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	.num_properties		= ARRAY_SIZE(pm860x_batt_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	.get_property		= pm860x_batt_get_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	.set_property		= pm860x_batt_set_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	.external_power_changed	= pm860x_external_power_changed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) static int pm860x_battery_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	struct pm860x_battery_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	struct pm860x_power_pdata *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	info->irq_cc = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	if (info->irq_cc <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	info->irq_batt = platform_get_irq(pdev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	if (info->irq_batt <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	info->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	info->i2c =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	    (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	info->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	info->status = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	pdata = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	mutex_init(&info->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	platform_set_drvdata(pdev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	pm860x_init_battery(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	if (pdata && pdata->max_capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		info->max_capacity = pdata->max_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		info->max_capacity = 1500;	/* set default capacity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	if (pdata && pdata->resistor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		info->resistor = pdata->resistor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		info->resistor = 300;	/* set default internal resistor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	info->battery = devm_power_supply_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 						   &pm860x_battery_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 						   NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	if (IS_ERR(info->battery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		return PTR_ERR(info->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	info->battery->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	ret = devm_request_threaded_irq(chip->dev, info->irq_cc, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 					pm860x_coulomb_handler, IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 					"coulomb", info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			info->irq_cc, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	ret = devm_request_threaded_irq(chip->dev, info->irq_batt, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 					pm860x_batt_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 					IRQF_ONESHOT, "battery", info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			info->irq_batt, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) static int pm860x_battery_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		chip->wakeup_flag |= 1 << PM8607_IRQ_CC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) static int pm860x_battery_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		chip->wakeup_flag &= ~(1 << PM8607_IRQ_CC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) static SIMPLE_DEV_PM_OPS(pm860x_battery_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 			pm860x_battery_suspend, pm860x_battery_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) static struct platform_driver pm860x_battery_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		   .name = "88pm860x-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		   .pm = &pm860x_battery_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	.probe = pm860x_battery_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) module_platform_driver(pm860x_battery_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) MODULE_DESCRIPTION("Marvell 88PM860x Battery driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) MODULE_LICENSE("GPL");