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