18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for Richtek RT9455WSC battery charger. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Intel Corporation 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 108c2ecf20Sopenharmony_ci#include <linux/delay.h> 118c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 128c2ecf20Sopenharmony_ci#include <linux/of_device.h> 138c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 148c2ecf20Sopenharmony_ci#include <linux/power_supply.h> 158c2ecf20Sopenharmony_ci#include <linux/i2c.h> 168c2ecf20Sopenharmony_ci#include <linux/acpi.h> 178c2ecf20Sopenharmony_ci#include <linux/usb/phy.h> 188c2ecf20Sopenharmony_ci#include <linux/regmap.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define RT9455_MANUFACTURER "Richtek" 218c2ecf20Sopenharmony_ci#define RT9455_MODEL_NAME "RT9455" 228c2ecf20Sopenharmony_ci#define RT9455_DRIVER_NAME "rt9455-charger" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define RT9455_IRQ_NAME "interrupt" 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define RT9455_PWR_RDY_DELAY 1 /* 1 second */ 278c2ecf20Sopenharmony_ci#define RT9455_MAX_CHARGING_TIME 21600 /* 6 hrs */ 288c2ecf20Sopenharmony_ci#define RT9455_BATT_PRESENCE_DELAY 60 /* 60 seconds */ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define RT9455_CHARGE_MODE 0x00 318c2ecf20Sopenharmony_ci#define RT9455_BOOST_MODE 0x01 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define RT9455_FAULT 0x03 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#define RT9455_IAICR_100MA 0x00 368c2ecf20Sopenharmony_ci#define RT9455_IAICR_500MA 0x01 378c2ecf20Sopenharmony_ci#define RT9455_IAICR_NO_LIMIT 0x03 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define RT9455_CHARGE_DISABLE 0x00 408c2ecf20Sopenharmony_ci#define RT9455_CHARGE_ENABLE 0x01 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define RT9455_PWR_FAULT 0x00 438c2ecf20Sopenharmony_ci#define RT9455_PWR_GOOD 0x01 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define RT9455_REG_CTRL1 0x00 /* CTRL1 reg address */ 468c2ecf20Sopenharmony_ci#define RT9455_REG_CTRL2 0x01 /* CTRL2 reg address */ 478c2ecf20Sopenharmony_ci#define RT9455_REG_CTRL3 0x02 /* CTRL3 reg address */ 488c2ecf20Sopenharmony_ci#define RT9455_REG_DEV_ID 0x03 /* DEV_ID reg address */ 498c2ecf20Sopenharmony_ci#define RT9455_REG_CTRL4 0x04 /* CTRL4 reg address */ 508c2ecf20Sopenharmony_ci#define RT9455_REG_CTRL5 0x05 /* CTRL5 reg address */ 518c2ecf20Sopenharmony_ci#define RT9455_REG_CTRL6 0x06 /* CTRL6 reg address */ 528c2ecf20Sopenharmony_ci#define RT9455_REG_CTRL7 0x07 /* CTRL7 reg address */ 538c2ecf20Sopenharmony_ci#define RT9455_REG_IRQ1 0x08 /* IRQ1 reg address */ 548c2ecf20Sopenharmony_ci#define RT9455_REG_IRQ2 0x09 /* IRQ2 reg address */ 558c2ecf20Sopenharmony_ci#define RT9455_REG_IRQ3 0x0A /* IRQ3 reg address */ 568c2ecf20Sopenharmony_ci#define RT9455_REG_MASK1 0x0B /* MASK1 reg address */ 578c2ecf20Sopenharmony_ci#define RT9455_REG_MASK2 0x0C /* MASK2 reg address */ 588c2ecf20Sopenharmony_ci#define RT9455_REG_MASK3 0x0D /* MASK3 reg address */ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cienum rt9455_fields { 618c2ecf20Sopenharmony_ci F_STAT, F_BOOST, F_PWR_RDY, F_OTG_PIN_POLARITY, /* CTRL1 reg fields */ 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci F_IAICR, F_TE_SHDN_EN, F_HIGHER_OCP, F_TE, F_IAICR_INT, F_HIZ, 648c2ecf20Sopenharmony_ci F_OPA_MODE, /* CTRL2 reg fields */ 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci F_VOREG, F_OTG_PL, F_OTG_EN, /* CTRL3 reg fields */ 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci F_VENDOR_ID, F_CHIP_REV, /* DEV_ID reg fields */ 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci F_RST, /* CTRL4 reg fields */ 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci F_TMR_EN, F_MIVR, F_IPREC, F_IEOC_PERCENTAGE, /* CTRL5 reg fields*/ 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci F_IAICR_SEL, F_ICHRG, F_VPREC, /* CTRL6 reg fields */ 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci F_BATD_EN, F_CHG_EN, F_VMREG, /* CTRL7 reg fields */ 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci F_TSDI, F_VINOVPI, F_BATAB, /* IRQ1 reg fields */ 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci F_CHRVPI, F_CHBATOVI, F_CHTERMI, F_CHRCHGI, F_CH32MI, F_CHTREGI, 818c2ecf20Sopenharmony_ci F_CHMIVRI, /* IRQ2 reg fields */ 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci F_BSTBUSOVI, F_BSTOLI, F_BSTLOWVI, F_BST32SI, /* IRQ3 reg fields */ 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci F_TSDM, F_VINOVPIM, F_BATABM, /* MASK1 reg fields */ 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci F_CHRVPIM, F_CHBATOVIM, F_CHTERMIM, F_CHRCHGIM, F_CH32MIM, F_CHTREGIM, 888c2ecf20Sopenharmony_ci F_CHMIVRIM, /* MASK2 reg fields */ 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci F_BSTVINOVIM, F_BSTOLIM, F_BSTLOWVIM, F_BST32SIM, /* MASK3 reg fields */ 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci F_MAX_FIELDS 938c2ecf20Sopenharmony_ci}; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic const struct reg_field rt9455_reg_fields[] = { 968c2ecf20Sopenharmony_ci [F_STAT] = REG_FIELD(RT9455_REG_CTRL1, 4, 5), 978c2ecf20Sopenharmony_ci [F_BOOST] = REG_FIELD(RT9455_REG_CTRL1, 3, 3), 988c2ecf20Sopenharmony_ci [F_PWR_RDY] = REG_FIELD(RT9455_REG_CTRL1, 2, 2), 998c2ecf20Sopenharmony_ci [F_OTG_PIN_POLARITY] = REG_FIELD(RT9455_REG_CTRL1, 1, 1), 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci [F_IAICR] = REG_FIELD(RT9455_REG_CTRL2, 6, 7), 1028c2ecf20Sopenharmony_ci [F_TE_SHDN_EN] = REG_FIELD(RT9455_REG_CTRL2, 5, 5), 1038c2ecf20Sopenharmony_ci [F_HIGHER_OCP] = REG_FIELD(RT9455_REG_CTRL2, 4, 4), 1048c2ecf20Sopenharmony_ci [F_TE] = REG_FIELD(RT9455_REG_CTRL2, 3, 3), 1058c2ecf20Sopenharmony_ci [F_IAICR_INT] = REG_FIELD(RT9455_REG_CTRL2, 2, 2), 1068c2ecf20Sopenharmony_ci [F_HIZ] = REG_FIELD(RT9455_REG_CTRL2, 1, 1), 1078c2ecf20Sopenharmony_ci [F_OPA_MODE] = REG_FIELD(RT9455_REG_CTRL2, 0, 0), 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci [F_VOREG] = REG_FIELD(RT9455_REG_CTRL3, 2, 7), 1108c2ecf20Sopenharmony_ci [F_OTG_PL] = REG_FIELD(RT9455_REG_CTRL3, 1, 1), 1118c2ecf20Sopenharmony_ci [F_OTG_EN] = REG_FIELD(RT9455_REG_CTRL3, 0, 0), 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci [F_VENDOR_ID] = REG_FIELD(RT9455_REG_DEV_ID, 4, 7), 1148c2ecf20Sopenharmony_ci [F_CHIP_REV] = REG_FIELD(RT9455_REG_DEV_ID, 0, 3), 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci [F_RST] = REG_FIELD(RT9455_REG_CTRL4, 7, 7), 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci [F_TMR_EN] = REG_FIELD(RT9455_REG_CTRL5, 7, 7), 1198c2ecf20Sopenharmony_ci [F_MIVR] = REG_FIELD(RT9455_REG_CTRL5, 4, 5), 1208c2ecf20Sopenharmony_ci [F_IPREC] = REG_FIELD(RT9455_REG_CTRL5, 2, 3), 1218c2ecf20Sopenharmony_ci [F_IEOC_PERCENTAGE] = REG_FIELD(RT9455_REG_CTRL5, 0, 1), 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci [F_IAICR_SEL] = REG_FIELD(RT9455_REG_CTRL6, 7, 7), 1248c2ecf20Sopenharmony_ci [F_ICHRG] = REG_FIELD(RT9455_REG_CTRL6, 4, 6), 1258c2ecf20Sopenharmony_ci [F_VPREC] = REG_FIELD(RT9455_REG_CTRL6, 0, 2), 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci [F_BATD_EN] = REG_FIELD(RT9455_REG_CTRL7, 6, 6), 1288c2ecf20Sopenharmony_ci [F_CHG_EN] = REG_FIELD(RT9455_REG_CTRL7, 4, 4), 1298c2ecf20Sopenharmony_ci [F_VMREG] = REG_FIELD(RT9455_REG_CTRL7, 0, 3), 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci [F_TSDI] = REG_FIELD(RT9455_REG_IRQ1, 7, 7), 1328c2ecf20Sopenharmony_ci [F_VINOVPI] = REG_FIELD(RT9455_REG_IRQ1, 6, 6), 1338c2ecf20Sopenharmony_ci [F_BATAB] = REG_FIELD(RT9455_REG_IRQ1, 0, 0), 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci [F_CHRVPI] = REG_FIELD(RT9455_REG_IRQ2, 7, 7), 1368c2ecf20Sopenharmony_ci [F_CHBATOVI] = REG_FIELD(RT9455_REG_IRQ2, 5, 5), 1378c2ecf20Sopenharmony_ci [F_CHTERMI] = REG_FIELD(RT9455_REG_IRQ2, 4, 4), 1388c2ecf20Sopenharmony_ci [F_CHRCHGI] = REG_FIELD(RT9455_REG_IRQ2, 3, 3), 1398c2ecf20Sopenharmony_ci [F_CH32MI] = REG_FIELD(RT9455_REG_IRQ2, 2, 2), 1408c2ecf20Sopenharmony_ci [F_CHTREGI] = REG_FIELD(RT9455_REG_IRQ2, 1, 1), 1418c2ecf20Sopenharmony_ci [F_CHMIVRI] = REG_FIELD(RT9455_REG_IRQ2, 0, 0), 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci [F_BSTBUSOVI] = REG_FIELD(RT9455_REG_IRQ3, 7, 7), 1448c2ecf20Sopenharmony_ci [F_BSTOLI] = REG_FIELD(RT9455_REG_IRQ3, 6, 6), 1458c2ecf20Sopenharmony_ci [F_BSTLOWVI] = REG_FIELD(RT9455_REG_IRQ3, 5, 5), 1468c2ecf20Sopenharmony_ci [F_BST32SI] = REG_FIELD(RT9455_REG_IRQ3, 3, 3), 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci [F_TSDM] = REG_FIELD(RT9455_REG_MASK1, 7, 7), 1498c2ecf20Sopenharmony_ci [F_VINOVPIM] = REG_FIELD(RT9455_REG_MASK1, 6, 6), 1508c2ecf20Sopenharmony_ci [F_BATABM] = REG_FIELD(RT9455_REG_MASK1, 0, 0), 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci [F_CHRVPIM] = REG_FIELD(RT9455_REG_MASK2, 7, 7), 1538c2ecf20Sopenharmony_ci [F_CHBATOVIM] = REG_FIELD(RT9455_REG_MASK2, 5, 5), 1548c2ecf20Sopenharmony_ci [F_CHTERMIM] = REG_FIELD(RT9455_REG_MASK2, 4, 4), 1558c2ecf20Sopenharmony_ci [F_CHRCHGIM] = REG_FIELD(RT9455_REG_MASK2, 3, 3), 1568c2ecf20Sopenharmony_ci [F_CH32MIM] = REG_FIELD(RT9455_REG_MASK2, 2, 2), 1578c2ecf20Sopenharmony_ci [F_CHTREGIM] = REG_FIELD(RT9455_REG_MASK2, 1, 1), 1588c2ecf20Sopenharmony_ci [F_CHMIVRIM] = REG_FIELD(RT9455_REG_MASK2, 0, 0), 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci [F_BSTVINOVIM] = REG_FIELD(RT9455_REG_MASK3, 7, 7), 1618c2ecf20Sopenharmony_ci [F_BSTOLIM] = REG_FIELD(RT9455_REG_MASK3, 6, 6), 1628c2ecf20Sopenharmony_ci [F_BSTLOWVIM] = REG_FIELD(RT9455_REG_MASK3, 5, 5), 1638c2ecf20Sopenharmony_ci [F_BST32SIM] = REG_FIELD(RT9455_REG_MASK3, 3, 3), 1648c2ecf20Sopenharmony_ci}; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci#define GET_MASK(fid) (BIT(rt9455_reg_fields[fid].msb + 1) - \ 1678c2ecf20Sopenharmony_ci BIT(rt9455_reg_fields[fid].lsb)) 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci/* 1708c2ecf20Sopenharmony_ci * Each array initialised below shows the possible real-world values for a 1718c2ecf20Sopenharmony_ci * group of bits belonging to RT9455 registers. The arrays are sorted in 1728c2ecf20Sopenharmony_ci * ascending order. The index of each real-world value represents the value 1738c2ecf20Sopenharmony_ci * that is encoded in the group of bits belonging to RT9455 registers. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ci/* REG06[6:4] (ICHRG) in uAh */ 1768c2ecf20Sopenharmony_cistatic const int rt9455_ichrg_values[] = { 1778c2ecf20Sopenharmony_ci 500000, 650000, 800000, 950000, 1100000, 1250000, 1400000, 1550000 1788c2ecf20Sopenharmony_ci}; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci/* 1818c2ecf20Sopenharmony_ci * When the charger is in charge mode, REG02[7:2] represent battery regulation 1828c2ecf20Sopenharmony_ci * voltage. 1838c2ecf20Sopenharmony_ci */ 1848c2ecf20Sopenharmony_ci/* REG02[7:2] (VOREG) in uV */ 1858c2ecf20Sopenharmony_cistatic const int rt9455_voreg_values[] = { 1868c2ecf20Sopenharmony_ci 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000, 1878c2ecf20Sopenharmony_ci 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000, 1888c2ecf20Sopenharmony_ci 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000, 1898c2ecf20Sopenharmony_ci 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000, 1908c2ecf20Sopenharmony_ci 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000, 1918c2ecf20Sopenharmony_ci 4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000, 1928c2ecf20Sopenharmony_ci 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 1938c2ecf20Sopenharmony_ci 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000 1948c2ecf20Sopenharmony_ci}; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci/* 1978c2ecf20Sopenharmony_ci * When the charger is in boost mode, REG02[7:2] represent boost output 1988c2ecf20Sopenharmony_ci * voltage. 1998c2ecf20Sopenharmony_ci */ 2008c2ecf20Sopenharmony_ci/* REG02[7:2] (Boost output voltage) in uV */ 2018c2ecf20Sopenharmony_cistatic const int rt9455_boost_voltage_values[] = { 2028c2ecf20Sopenharmony_ci 4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000, 2038c2ecf20Sopenharmony_ci 4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000, 2048c2ecf20Sopenharmony_ci 4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000, 2058c2ecf20Sopenharmony_ci 5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000, 2068c2ecf20Sopenharmony_ci 5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000, 2078c2ecf20Sopenharmony_ci 5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000, 2088c2ecf20Sopenharmony_ci 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 2098c2ecf20Sopenharmony_ci 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 2108c2ecf20Sopenharmony_ci}; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci/* REG07[3:0] (VMREG) in uV */ 2138c2ecf20Sopenharmony_cistatic const int rt9455_vmreg_values[] = { 2148c2ecf20Sopenharmony_ci 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000, 2158c2ecf20Sopenharmony_ci 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000 2168c2ecf20Sopenharmony_ci}; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci/* REG05[5:4] (IEOC_PERCENTAGE) */ 2198c2ecf20Sopenharmony_cistatic const int rt9455_ieoc_percentage_values[] = { 2208c2ecf20Sopenharmony_ci 10, 30, 20, 30 2218c2ecf20Sopenharmony_ci}; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci/* REG05[1:0] (MIVR) in uV */ 2248c2ecf20Sopenharmony_cistatic const int rt9455_mivr_values[] = { 2258c2ecf20Sopenharmony_ci 4000000, 4250000, 4500000, 5000000 2268c2ecf20Sopenharmony_ci}; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci/* REG05[1:0] (IAICR) in uA */ 2298c2ecf20Sopenharmony_cistatic const int rt9455_iaicr_values[] = { 2308c2ecf20Sopenharmony_ci 100000, 500000, 1000000, 2000000 2318c2ecf20Sopenharmony_ci}; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_cistruct rt9455_info { 2348c2ecf20Sopenharmony_ci struct i2c_client *client; 2358c2ecf20Sopenharmony_ci struct regmap *regmap; 2368c2ecf20Sopenharmony_ci struct regmap_field *regmap_fields[F_MAX_FIELDS]; 2378c2ecf20Sopenharmony_ci struct power_supply *charger; 2388c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_PHY) 2398c2ecf20Sopenharmony_ci struct usb_phy *usb_phy; 2408c2ecf20Sopenharmony_ci struct notifier_block nb; 2418c2ecf20Sopenharmony_ci#endif 2428c2ecf20Sopenharmony_ci struct delayed_work pwr_rdy_work; 2438c2ecf20Sopenharmony_ci struct delayed_work max_charging_time_work; 2448c2ecf20Sopenharmony_ci struct delayed_work batt_presence_work; 2458c2ecf20Sopenharmony_ci u32 voreg; 2468c2ecf20Sopenharmony_ci u32 boost_voltage; 2478c2ecf20Sopenharmony_ci}; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci/* 2508c2ecf20Sopenharmony_ci * Iterate through each element of the 'tbl' array until an element whose value 2518c2ecf20Sopenharmony_ci * is greater than v is found. Return the index of the respective element, 2528c2ecf20Sopenharmony_ci * or the index of the last element in the array, if no such element is found. 2538c2ecf20Sopenharmony_ci */ 2548c2ecf20Sopenharmony_cistatic unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v) 2558c2ecf20Sopenharmony_ci{ 2568c2ecf20Sopenharmony_ci int i; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci /* 2598c2ecf20Sopenharmony_ci * No need to iterate until the last index in the table because 2608c2ecf20Sopenharmony_ci * if no element greater than v is found in the table, 2618c2ecf20Sopenharmony_ci * or if only the last element is greater than v, 2628c2ecf20Sopenharmony_ci * function returns the index of the last element. 2638c2ecf20Sopenharmony_ci */ 2648c2ecf20Sopenharmony_ci for (i = 0; i < tbl_size - 1; i++) 2658c2ecf20Sopenharmony_ci if (v <= tbl[i]) 2668c2ecf20Sopenharmony_ci return i; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci return (tbl_size - 1); 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_cistatic int rt9455_get_field_val(struct rt9455_info *info, 2728c2ecf20Sopenharmony_ci enum rt9455_fields field, 2738c2ecf20Sopenharmony_ci const int tbl[], int tbl_size, int *val) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci unsigned int v; 2768c2ecf20Sopenharmony_ci int ret; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[field], &v); 2798c2ecf20Sopenharmony_ci if (ret) 2808c2ecf20Sopenharmony_ci return ret; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci v = (v >= tbl_size) ? (tbl_size - 1) : v; 2838c2ecf20Sopenharmony_ci *val = tbl[v]; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci return 0; 2868c2ecf20Sopenharmony_ci} 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_cistatic int rt9455_set_field_val(struct rt9455_info *info, 2898c2ecf20Sopenharmony_ci enum rt9455_fields field, 2908c2ecf20Sopenharmony_ci const int tbl[], int tbl_size, int val) 2918c2ecf20Sopenharmony_ci{ 2928c2ecf20Sopenharmony_ci unsigned int idx = rt9455_find_idx(tbl, tbl_size, val); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci return regmap_field_write(info->regmap_fields[field], idx); 2958c2ecf20Sopenharmony_ci} 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic int rt9455_register_reset(struct rt9455_info *info) 2988c2ecf20Sopenharmony_ci{ 2998c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 3008c2ecf20Sopenharmony_ci unsigned int v; 3018c2ecf20Sopenharmony_ci int ret, limit = 100; 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_RST], 0x01); 3048c2ecf20Sopenharmony_ci if (ret) { 3058c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set RST bit\n"); 3068c2ecf20Sopenharmony_ci return ret; 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci /* 3108c2ecf20Sopenharmony_ci * To make sure that reset operation has finished, loop until RST bit 3118c2ecf20Sopenharmony_ci * is set to 0. 3128c2ecf20Sopenharmony_ci */ 3138c2ecf20Sopenharmony_ci do { 3148c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_RST], &v); 3158c2ecf20Sopenharmony_ci if (ret) { 3168c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read RST bit\n"); 3178c2ecf20Sopenharmony_ci return ret; 3188c2ecf20Sopenharmony_ci } 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci if (!v) 3218c2ecf20Sopenharmony_ci break; 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci usleep_range(10, 100); 3248c2ecf20Sopenharmony_ci } while (--limit); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci if (!limit) 3278c2ecf20Sopenharmony_ci return -EIO; 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci return 0; 3308c2ecf20Sopenharmony_ci} 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci/* Charger power supply property routines */ 3338c2ecf20Sopenharmony_cistatic enum power_supply_property rt9455_charger_properties[] = { 3348c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_STATUS, 3358c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_HEALTH, 3368c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_PRESENT, 3378c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_ONLINE, 3388c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 3398c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 3408c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 3418c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 3428c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_SCOPE, 3438c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 3448c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MODEL_NAME, 3458c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MANUFACTURER, 3468c2ecf20Sopenharmony_ci}; 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_cistatic char *rt9455_charger_supplied_to[] = { 3498c2ecf20Sopenharmony_ci "main-battery", 3508c2ecf20Sopenharmony_ci}; 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_cistatic int rt9455_charger_get_status(struct rt9455_info *info, 3538c2ecf20Sopenharmony_ci union power_supply_propval *val) 3548c2ecf20Sopenharmony_ci{ 3558c2ecf20Sopenharmony_ci unsigned int v, pwr_rdy; 3568c2ecf20Sopenharmony_ci int ret; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], 3598c2ecf20Sopenharmony_ci &pwr_rdy); 3608c2ecf20Sopenharmony_ci if (ret) { 3618c2ecf20Sopenharmony_ci dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n"); 3628c2ecf20Sopenharmony_ci return ret; 3638c2ecf20Sopenharmony_ci } 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci /* 3668c2ecf20Sopenharmony_ci * If PWR_RDY bit is unset, the battery is discharging. Otherwise, 3678c2ecf20Sopenharmony_ci * STAT bits value must be checked. 3688c2ecf20Sopenharmony_ci */ 3698c2ecf20Sopenharmony_ci if (!pwr_rdy) { 3708c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 3718c2ecf20Sopenharmony_ci return 0; 3728c2ecf20Sopenharmony_ci } 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_STAT], &v); 3758c2ecf20Sopenharmony_ci if (ret) { 3768c2ecf20Sopenharmony_ci dev_err(&info->client->dev, "Failed to read STAT bits\n"); 3778c2ecf20Sopenharmony_ci return ret; 3788c2ecf20Sopenharmony_ci } 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci switch (v) { 3818c2ecf20Sopenharmony_ci case 0: 3828c2ecf20Sopenharmony_ci /* 3838c2ecf20Sopenharmony_ci * If PWR_RDY bit is set, but STAT bits value is 0, the charger 3848c2ecf20Sopenharmony_ci * may be in one of the following cases: 3858c2ecf20Sopenharmony_ci * 1. CHG_EN bit is 0. 3868c2ecf20Sopenharmony_ci * 2. CHG_EN bit is 1 but the battery is not connected. 3878c2ecf20Sopenharmony_ci * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is 3888c2ecf20Sopenharmony_ci * returned. 3898c2ecf20Sopenharmony_ci */ 3908c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 3918c2ecf20Sopenharmony_ci return 0; 3928c2ecf20Sopenharmony_ci case 1: 3938c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_CHARGING; 3948c2ecf20Sopenharmony_ci return 0; 3958c2ecf20Sopenharmony_ci case 2: 3968c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_FULL; 3978c2ecf20Sopenharmony_ci return 0; 3988c2ecf20Sopenharmony_ci default: 3998c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 4008c2ecf20Sopenharmony_ci return 0; 4018c2ecf20Sopenharmony_ci } 4028c2ecf20Sopenharmony_ci} 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_cistatic int rt9455_charger_get_health(struct rt9455_info *info, 4058c2ecf20Sopenharmony_ci union power_supply_propval *val) 4068c2ecf20Sopenharmony_ci{ 4078c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 4088c2ecf20Sopenharmony_ci unsigned int v; 4098c2ecf20Sopenharmony_ci int ret; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_GOOD; 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v); 4148c2ecf20Sopenharmony_ci if (ret) { 4158c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IRQ1 register\n"); 4168c2ecf20Sopenharmony_ci return ret; 4178c2ecf20Sopenharmony_ci } 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci if (v & GET_MASK(F_TSDI)) { 4208c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 4218c2ecf20Sopenharmony_ci return 0; 4228c2ecf20Sopenharmony_ci } 4238c2ecf20Sopenharmony_ci if (v & GET_MASK(F_VINOVPI)) { 4248c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 4258c2ecf20Sopenharmony_ci return 0; 4268c2ecf20Sopenharmony_ci } 4278c2ecf20Sopenharmony_ci if (v & GET_MASK(F_BATAB)) { 4288c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 4298c2ecf20Sopenharmony_ci return 0; 4308c2ecf20Sopenharmony_ci } 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v); 4338c2ecf20Sopenharmony_ci if (ret) { 4348c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IRQ2 register\n"); 4358c2ecf20Sopenharmony_ci return ret; 4368c2ecf20Sopenharmony_ci } 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci if (v & GET_MASK(F_CHBATOVI)) { 4398c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 4408c2ecf20Sopenharmony_ci return 0; 4418c2ecf20Sopenharmony_ci } 4428c2ecf20Sopenharmony_ci if (v & GET_MASK(F_CH32MI)) { 4438c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 4448c2ecf20Sopenharmony_ci return 0; 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v); 4488c2ecf20Sopenharmony_ci if (ret) { 4498c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IRQ3 register\n"); 4508c2ecf20Sopenharmony_ci return ret; 4518c2ecf20Sopenharmony_ci } 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci if (v & GET_MASK(F_BSTBUSOVI)) { 4548c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 4558c2ecf20Sopenharmony_ci return 0; 4568c2ecf20Sopenharmony_ci } 4578c2ecf20Sopenharmony_ci if (v & GET_MASK(F_BSTOLI)) { 4588c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 4598c2ecf20Sopenharmony_ci return 0; 4608c2ecf20Sopenharmony_ci } 4618c2ecf20Sopenharmony_ci if (v & GET_MASK(F_BSTLOWVI)) { 4628c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 4638c2ecf20Sopenharmony_ci return 0; 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci if (v & GET_MASK(F_BST32SI)) { 4668c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 4678c2ecf20Sopenharmony_ci return 0; 4688c2ecf20Sopenharmony_ci } 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_STAT], &v); 4718c2ecf20Sopenharmony_ci if (ret) { 4728c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read STAT bits\n"); 4738c2ecf20Sopenharmony_ci return ret; 4748c2ecf20Sopenharmony_ci } 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci if (v == RT9455_FAULT) { 4778c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 4788c2ecf20Sopenharmony_ci return 0; 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci return 0; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistatic int rt9455_charger_get_battery_presence(struct rt9455_info *info, 4858c2ecf20Sopenharmony_ci union power_supply_propval *val) 4868c2ecf20Sopenharmony_ci{ 4878c2ecf20Sopenharmony_ci unsigned int v; 4888c2ecf20Sopenharmony_ci int ret; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_BATAB], &v); 4918c2ecf20Sopenharmony_ci if (ret) { 4928c2ecf20Sopenharmony_ci dev_err(&info->client->dev, "Failed to read BATAB bit\n"); 4938c2ecf20Sopenharmony_ci return ret; 4948c2ecf20Sopenharmony_ci } 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci /* 4978c2ecf20Sopenharmony_ci * Since BATAB is 1 when battery is NOT present and 0 otherwise, 4988c2ecf20Sopenharmony_ci * !BATAB is returned. 4998c2ecf20Sopenharmony_ci */ 5008c2ecf20Sopenharmony_ci val->intval = !v; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci return 0; 5038c2ecf20Sopenharmony_ci} 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_cistatic int rt9455_charger_get_online(struct rt9455_info *info, 5068c2ecf20Sopenharmony_ci union power_supply_propval *val) 5078c2ecf20Sopenharmony_ci{ 5088c2ecf20Sopenharmony_ci unsigned int v; 5098c2ecf20Sopenharmony_ci int ret; 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v); 5128c2ecf20Sopenharmony_ci if (ret) { 5138c2ecf20Sopenharmony_ci dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n"); 5148c2ecf20Sopenharmony_ci return ret; 5158c2ecf20Sopenharmony_ci } 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci val->intval = (int)v; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci return 0; 5208c2ecf20Sopenharmony_ci} 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_cistatic int rt9455_charger_get_current(struct rt9455_info *info, 5238c2ecf20Sopenharmony_ci union power_supply_propval *val) 5248c2ecf20Sopenharmony_ci{ 5258c2ecf20Sopenharmony_ci int curr; 5268c2ecf20Sopenharmony_ci int ret; 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci ret = rt9455_get_field_val(info, F_ICHRG, 5298c2ecf20Sopenharmony_ci rt9455_ichrg_values, 5308c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_ichrg_values), 5318c2ecf20Sopenharmony_ci &curr); 5328c2ecf20Sopenharmony_ci if (ret) { 5338c2ecf20Sopenharmony_ci dev_err(&info->client->dev, "Failed to read ICHRG value\n"); 5348c2ecf20Sopenharmony_ci return ret; 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci val->intval = curr; 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci return 0; 5408c2ecf20Sopenharmony_ci} 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_cistatic int rt9455_charger_get_current_max(struct rt9455_info *info, 5438c2ecf20Sopenharmony_ci union power_supply_propval *val) 5448c2ecf20Sopenharmony_ci{ 5458c2ecf20Sopenharmony_ci int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci val->intval = rt9455_ichrg_values[idx]; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci return 0; 5508c2ecf20Sopenharmony_ci} 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_cistatic int rt9455_charger_get_voltage(struct rt9455_info *info, 5538c2ecf20Sopenharmony_ci union power_supply_propval *val) 5548c2ecf20Sopenharmony_ci{ 5558c2ecf20Sopenharmony_ci int voltage; 5568c2ecf20Sopenharmony_ci int ret; 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci ret = rt9455_get_field_val(info, F_VOREG, 5598c2ecf20Sopenharmony_ci rt9455_voreg_values, 5608c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_voreg_values), 5618c2ecf20Sopenharmony_ci &voltage); 5628c2ecf20Sopenharmony_ci if (ret) { 5638c2ecf20Sopenharmony_ci dev_err(&info->client->dev, "Failed to read VOREG value\n"); 5648c2ecf20Sopenharmony_ci return ret; 5658c2ecf20Sopenharmony_ci } 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci val->intval = voltage; 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci return 0; 5708c2ecf20Sopenharmony_ci} 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_cistatic int rt9455_charger_get_voltage_max(struct rt9455_info *info, 5738c2ecf20Sopenharmony_ci union power_supply_propval *val) 5748c2ecf20Sopenharmony_ci{ 5758c2ecf20Sopenharmony_ci int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1; 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci val->intval = rt9455_vmreg_values[idx]; 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci return 0; 5808c2ecf20Sopenharmony_ci} 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_cistatic int rt9455_charger_get_term_current(struct rt9455_info *info, 5838c2ecf20Sopenharmony_ci union power_supply_propval *val) 5848c2ecf20Sopenharmony_ci{ 5858c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 5868c2ecf20Sopenharmony_ci int ichrg, ieoc_percentage, ret; 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci ret = rt9455_get_field_val(info, F_ICHRG, 5898c2ecf20Sopenharmony_ci rt9455_ichrg_values, 5908c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_ichrg_values), 5918c2ecf20Sopenharmony_ci &ichrg); 5928c2ecf20Sopenharmony_ci if (ret) { 5938c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read ICHRG value\n"); 5948c2ecf20Sopenharmony_ci return ret; 5958c2ecf20Sopenharmony_ci } 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE, 5988c2ecf20Sopenharmony_ci rt9455_ieoc_percentage_values, 5998c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_ieoc_percentage_values), 6008c2ecf20Sopenharmony_ci &ieoc_percentage); 6018c2ecf20Sopenharmony_ci if (ret) { 6028c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IEOC value\n"); 6038c2ecf20Sopenharmony_ci return ret; 6048c2ecf20Sopenharmony_ci } 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci val->intval = ichrg * ieoc_percentage / 100; 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci return 0; 6098c2ecf20Sopenharmony_ci} 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_cistatic int rt9455_charger_get_property(struct power_supply *psy, 6128c2ecf20Sopenharmony_ci enum power_supply_property psp, 6138c2ecf20Sopenharmony_ci union power_supply_propval *val) 6148c2ecf20Sopenharmony_ci{ 6158c2ecf20Sopenharmony_ci struct rt9455_info *info = power_supply_get_drvdata(psy); 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci switch (psp) { 6188c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_STATUS: 6198c2ecf20Sopenharmony_ci return rt9455_charger_get_status(info, val); 6208c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_HEALTH: 6218c2ecf20Sopenharmony_ci return rt9455_charger_get_health(info, val); 6228c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRESENT: 6238c2ecf20Sopenharmony_ci return rt9455_charger_get_battery_presence(info, val); 6248c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ONLINE: 6258c2ecf20Sopenharmony_ci return rt9455_charger_get_online(info, val); 6268c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 6278c2ecf20Sopenharmony_ci return rt9455_charger_get_current(info, val); 6288c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 6298c2ecf20Sopenharmony_ci return rt9455_charger_get_current_max(info, val); 6308c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 6318c2ecf20Sopenharmony_ci return rt9455_charger_get_voltage(info, val); 6328c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 6338c2ecf20Sopenharmony_ci return rt9455_charger_get_voltage_max(info, val); 6348c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_SCOPE: 6358c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_SCOPE_SYSTEM; 6368c2ecf20Sopenharmony_ci return 0; 6378c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 6388c2ecf20Sopenharmony_ci return rt9455_charger_get_term_current(info, val); 6398c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MODEL_NAME: 6408c2ecf20Sopenharmony_ci val->strval = RT9455_MODEL_NAME; 6418c2ecf20Sopenharmony_ci return 0; 6428c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURER: 6438c2ecf20Sopenharmony_ci val->strval = RT9455_MANUFACTURER; 6448c2ecf20Sopenharmony_ci return 0; 6458c2ecf20Sopenharmony_ci default: 6468c2ecf20Sopenharmony_ci return -ENODATA; 6478c2ecf20Sopenharmony_ci } 6488c2ecf20Sopenharmony_ci} 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_cistatic int rt9455_hw_init(struct rt9455_info *info, u32 ichrg, 6518c2ecf20Sopenharmony_ci u32 ieoc_percentage, 6528c2ecf20Sopenharmony_ci u32 mivr, u32 iaicr) 6538c2ecf20Sopenharmony_ci{ 6548c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 6558c2ecf20Sopenharmony_ci int idx, ret; 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci ret = rt9455_register_reset(info); 6588c2ecf20Sopenharmony_ci if (ret) { 6598c2ecf20Sopenharmony_ci dev_err(dev, "Power On Reset failed\n"); 6608c2ecf20Sopenharmony_ci return ret; 6618c2ecf20Sopenharmony_ci } 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci /* Set TE bit in order to enable end of charge detection */ 6648c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_TE], 1); 6658c2ecf20Sopenharmony_ci if (ret) { 6668c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set TE bit\n"); 6678c2ecf20Sopenharmony_ci return ret; 6688c2ecf20Sopenharmony_ci } 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci /* Set TE_SHDN_EN bit in order to enable end of charge detection */ 6718c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1); 6728c2ecf20Sopenharmony_ci if (ret) { 6738c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set TE_SHDN_EN bit\n"); 6748c2ecf20Sopenharmony_ci return ret; 6758c2ecf20Sopenharmony_ci } 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci /* 6788c2ecf20Sopenharmony_ci * Set BATD_EN bit in order to enable battery detection 6798c2ecf20Sopenharmony_ci * when charging is done 6808c2ecf20Sopenharmony_ci */ 6818c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1); 6828c2ecf20Sopenharmony_ci if (ret) { 6838c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set BATD_EN bit\n"); 6848c2ecf20Sopenharmony_ci return ret; 6858c2ecf20Sopenharmony_ci } 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci /* 6888c2ecf20Sopenharmony_ci * Disable Safety Timer. In charge mode, this timer terminates charging 6898c2ecf20Sopenharmony_ci * if no read or write via I2C is done within 32 minutes. This timer 6908c2ecf20Sopenharmony_ci * avoids overcharging the baterry when the OS is not loaded and the 6918c2ecf20Sopenharmony_ci * charger is connected to a power source. 6928c2ecf20Sopenharmony_ci * In boost mode, this timer triggers BST32SI interrupt if no read or 6938c2ecf20Sopenharmony_ci * write via I2C is done within 32 seconds. 6948c2ecf20Sopenharmony_ci * When the OS is loaded and the charger driver is inserted, it is used 6958c2ecf20Sopenharmony_ci * delayed_work, named max_charging_time_work, to avoid overcharging 6968c2ecf20Sopenharmony_ci * the battery. 6978c2ecf20Sopenharmony_ci */ 6988c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00); 6998c2ecf20Sopenharmony_ci if (ret) { 7008c2ecf20Sopenharmony_ci dev_err(dev, "Failed to disable Safety Timer\n"); 7018c2ecf20Sopenharmony_ci return ret; 7028c2ecf20Sopenharmony_ci } 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_ci /* Set ICHRG to value retrieved from device-specific data */ 7058c2ecf20Sopenharmony_ci ret = rt9455_set_field_val(info, F_ICHRG, 7068c2ecf20Sopenharmony_ci rt9455_ichrg_values, 7078c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_ichrg_values), ichrg); 7088c2ecf20Sopenharmony_ci if (ret) { 7098c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set ICHRG value\n"); 7108c2ecf20Sopenharmony_ci return ret; 7118c2ecf20Sopenharmony_ci } 7128c2ecf20Sopenharmony_ci 7138c2ecf20Sopenharmony_ci /* Set IEOC Percentage to value retrieved from device-specific data */ 7148c2ecf20Sopenharmony_ci ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE, 7158c2ecf20Sopenharmony_ci rt9455_ieoc_percentage_values, 7168c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_ieoc_percentage_values), 7178c2ecf20Sopenharmony_ci ieoc_percentage); 7188c2ecf20Sopenharmony_ci if (ret) { 7198c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set IEOC Percentage value\n"); 7208c2ecf20Sopenharmony_ci return ret; 7218c2ecf20Sopenharmony_ci } 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci /* Set VOREG to value retrieved from device-specific data */ 7248c2ecf20Sopenharmony_ci ret = rt9455_set_field_val(info, F_VOREG, 7258c2ecf20Sopenharmony_ci rt9455_voreg_values, 7268c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_voreg_values), 7278c2ecf20Sopenharmony_ci info->voreg); 7288c2ecf20Sopenharmony_ci if (ret) { 7298c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set VOREG value\n"); 7308c2ecf20Sopenharmony_ci return ret; 7318c2ecf20Sopenharmony_ci } 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_ci /* Set VMREG value to maximum (4.45V). */ 7348c2ecf20Sopenharmony_ci idx = ARRAY_SIZE(rt9455_vmreg_values) - 1; 7358c2ecf20Sopenharmony_ci ret = rt9455_set_field_val(info, F_VMREG, 7368c2ecf20Sopenharmony_ci rt9455_vmreg_values, 7378c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_vmreg_values), 7388c2ecf20Sopenharmony_ci rt9455_vmreg_values[idx]); 7398c2ecf20Sopenharmony_ci if (ret) { 7408c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set VMREG value\n"); 7418c2ecf20Sopenharmony_ci return ret; 7428c2ecf20Sopenharmony_ci } 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci /* 7458c2ecf20Sopenharmony_ci * Set MIVR to value retrieved from device-specific data. 7468c2ecf20Sopenharmony_ci * If no value is specified, default value for MIVR is 4.5V. 7478c2ecf20Sopenharmony_ci */ 7488c2ecf20Sopenharmony_ci if (mivr == -1) 7498c2ecf20Sopenharmony_ci mivr = 4500000; 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_ci ret = rt9455_set_field_val(info, F_MIVR, 7528c2ecf20Sopenharmony_ci rt9455_mivr_values, 7538c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_mivr_values), mivr); 7548c2ecf20Sopenharmony_ci if (ret) { 7558c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set MIVR value\n"); 7568c2ecf20Sopenharmony_ci return ret; 7578c2ecf20Sopenharmony_ci } 7588c2ecf20Sopenharmony_ci 7598c2ecf20Sopenharmony_ci /* 7608c2ecf20Sopenharmony_ci * Set IAICR to value retrieved from device-specific data. 7618c2ecf20Sopenharmony_ci * If no value is specified, default value for IAICR is 500 mA. 7628c2ecf20Sopenharmony_ci */ 7638c2ecf20Sopenharmony_ci if (iaicr == -1) 7648c2ecf20Sopenharmony_ci iaicr = 500000; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci ret = rt9455_set_field_val(info, F_IAICR, 7678c2ecf20Sopenharmony_ci rt9455_iaicr_values, 7688c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_iaicr_values), iaicr); 7698c2ecf20Sopenharmony_ci if (ret) { 7708c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set IAICR value\n"); 7718c2ecf20Sopenharmony_ci return ret; 7728c2ecf20Sopenharmony_ci } 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci /* 7758c2ecf20Sopenharmony_ci * Set IAICR_INT bit so that IAICR value is determined by IAICR bits 7768c2ecf20Sopenharmony_ci * and not by OTG pin. 7778c2ecf20Sopenharmony_ci */ 7788c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01); 7798c2ecf20Sopenharmony_ci if (ret) { 7808c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set IAICR_INT bit\n"); 7818c2ecf20Sopenharmony_ci return ret; 7828c2ecf20Sopenharmony_ci } 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci /* 7858c2ecf20Sopenharmony_ci * Disable CHMIVRI interrupt. Because the driver sets MIVR value, 7868c2ecf20Sopenharmony_ci * CHMIVRI is triggered, but there is no action to be taken by the 7878c2ecf20Sopenharmony_ci * driver when CHMIVRI is triggered. 7888c2ecf20Sopenharmony_ci */ 7898c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01); 7908c2ecf20Sopenharmony_ci if (ret) { 7918c2ecf20Sopenharmony_ci dev_err(dev, "Failed to mask CHMIVRI interrupt\n"); 7928c2ecf20Sopenharmony_ci return ret; 7938c2ecf20Sopenharmony_ci } 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci return 0; 7968c2ecf20Sopenharmony_ci} 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_PHY) 7998c2ecf20Sopenharmony_ci/* 8008c2ecf20Sopenharmony_ci * Before setting the charger into boost mode, boost output voltage is 8018c2ecf20Sopenharmony_ci * set. This is needed because boost output voltage may differ from battery 8028c2ecf20Sopenharmony_ci * regulation voltage. F_VOREG bits represent either battery regulation voltage 8038c2ecf20Sopenharmony_ci * or boost output voltage, depending on the mode the charger is. Both battery 8048c2ecf20Sopenharmony_ci * regulation voltage and boost output voltage are read from DT/ACPI during 8058c2ecf20Sopenharmony_ci * probe. 8068c2ecf20Sopenharmony_ci */ 8078c2ecf20Sopenharmony_cistatic int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info) 8088c2ecf20Sopenharmony_ci{ 8098c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 8108c2ecf20Sopenharmony_ci int ret; 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci ret = rt9455_set_field_val(info, F_VOREG, 8138c2ecf20Sopenharmony_ci rt9455_boost_voltage_values, 8148c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_boost_voltage_values), 8158c2ecf20Sopenharmony_ci info->boost_voltage); 8168c2ecf20Sopenharmony_ci if (ret) { 8178c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set boost output voltage value\n"); 8188c2ecf20Sopenharmony_ci return ret; 8198c2ecf20Sopenharmony_ci } 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci return 0; 8228c2ecf20Sopenharmony_ci} 8238c2ecf20Sopenharmony_ci#endif 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci/* 8268c2ecf20Sopenharmony_ci * Before setting the charger into charge mode, battery regulation voltage is 8278c2ecf20Sopenharmony_ci * set. This is needed because boost output voltage may differ from battery 8288c2ecf20Sopenharmony_ci * regulation voltage. F_VOREG bits represent either battery regulation voltage 8298c2ecf20Sopenharmony_ci * or boost output voltage, depending on the mode the charger is. Both battery 8308c2ecf20Sopenharmony_ci * regulation voltage and boost output voltage are read from DT/ACPI during 8318c2ecf20Sopenharmony_ci * probe. 8328c2ecf20Sopenharmony_ci */ 8338c2ecf20Sopenharmony_cistatic int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info) 8348c2ecf20Sopenharmony_ci{ 8358c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 8368c2ecf20Sopenharmony_ci int ret; 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci ret = rt9455_set_field_val(info, F_VOREG, 8398c2ecf20Sopenharmony_ci rt9455_voreg_values, 8408c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_voreg_values), 8418c2ecf20Sopenharmony_ci info->voreg); 8428c2ecf20Sopenharmony_ci if (ret) { 8438c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set VOREG value\n"); 8448c2ecf20Sopenharmony_ci return ret; 8458c2ecf20Sopenharmony_ci } 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci return 0; 8488c2ecf20Sopenharmony_ci} 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_cistatic int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info, 8518c2ecf20Sopenharmony_ci bool *_is_battery_absent, 8528c2ecf20Sopenharmony_ci bool *_alert_userspace) 8538c2ecf20Sopenharmony_ci{ 8548c2ecf20Sopenharmony_ci unsigned int irq1, mask1, mask2; 8558c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 8568c2ecf20Sopenharmony_ci bool is_battery_absent = false; 8578c2ecf20Sopenharmony_ci bool alert_userspace = false; 8588c2ecf20Sopenharmony_ci int ret; 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1); 8618c2ecf20Sopenharmony_ci if (ret) { 8628c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IRQ1 register\n"); 8638c2ecf20Sopenharmony_ci return ret; 8648c2ecf20Sopenharmony_ci } 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1); 8678c2ecf20Sopenharmony_ci if (ret) { 8688c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read MASK1 register\n"); 8698c2ecf20Sopenharmony_ci return ret; 8708c2ecf20Sopenharmony_ci } 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ci if (irq1 & GET_MASK(F_TSDI)) { 8738c2ecf20Sopenharmony_ci dev_err(dev, "Thermal shutdown fault occurred\n"); 8748c2ecf20Sopenharmony_ci alert_userspace = true; 8758c2ecf20Sopenharmony_ci } 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci if (irq1 & GET_MASK(F_VINOVPI)) { 8788c2ecf20Sopenharmony_ci dev_err(dev, "Overvoltage input occurred\n"); 8798c2ecf20Sopenharmony_ci alert_userspace = true; 8808c2ecf20Sopenharmony_ci } 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ci if (irq1 & GET_MASK(F_BATAB)) { 8838c2ecf20Sopenharmony_ci dev_err(dev, "Battery absence occurred\n"); 8848c2ecf20Sopenharmony_ci is_battery_absent = true; 8858c2ecf20Sopenharmony_ci alert_userspace = true; 8868c2ecf20Sopenharmony_ci 8878c2ecf20Sopenharmony_ci if ((mask1 & GET_MASK(F_BATABM)) == 0) { 8888c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_BATABM], 8898c2ecf20Sopenharmony_ci 0x01); 8908c2ecf20Sopenharmony_ci if (ret) { 8918c2ecf20Sopenharmony_ci dev_err(dev, "Failed to mask BATAB interrupt\n"); 8928c2ecf20Sopenharmony_ci return ret; 8938c2ecf20Sopenharmony_ci } 8948c2ecf20Sopenharmony_ci } 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2); 8978c2ecf20Sopenharmony_ci if (ret) { 8988c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read MASK2 register\n"); 8998c2ecf20Sopenharmony_ci return ret; 9008c2ecf20Sopenharmony_ci } 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci if (mask2 & GET_MASK(F_CHTERMIM)) { 9038c2ecf20Sopenharmony_ci ret = regmap_field_write( 9048c2ecf20Sopenharmony_ci info->regmap_fields[F_CHTERMIM], 0x00); 9058c2ecf20Sopenharmony_ci if (ret) { 9068c2ecf20Sopenharmony_ci dev_err(dev, "Failed to unmask CHTERMI interrupt\n"); 9078c2ecf20Sopenharmony_ci return ret; 9088c2ecf20Sopenharmony_ci } 9098c2ecf20Sopenharmony_ci } 9108c2ecf20Sopenharmony_ci 9118c2ecf20Sopenharmony_ci if (mask2 & GET_MASK(F_CHRCHGIM)) { 9128c2ecf20Sopenharmony_ci ret = regmap_field_write( 9138c2ecf20Sopenharmony_ci info->regmap_fields[F_CHRCHGIM], 0x00); 9148c2ecf20Sopenharmony_ci if (ret) { 9158c2ecf20Sopenharmony_ci dev_err(dev, "Failed to unmask CHRCHGI interrupt\n"); 9168c2ecf20Sopenharmony_ci return ret; 9178c2ecf20Sopenharmony_ci } 9188c2ecf20Sopenharmony_ci } 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci /* 9218c2ecf20Sopenharmony_ci * When the battery is absent, max_charging_time_work is 9228c2ecf20Sopenharmony_ci * cancelled, since no charging is done. 9238c2ecf20Sopenharmony_ci */ 9248c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&info->max_charging_time_work); 9258c2ecf20Sopenharmony_ci /* 9268c2ecf20Sopenharmony_ci * Since no interrupt is triggered when the battery is 9278c2ecf20Sopenharmony_ci * reconnected, max_charging_time_work is not rescheduled. 9288c2ecf20Sopenharmony_ci * Therefore, batt_presence_work is scheduled to check whether 9298c2ecf20Sopenharmony_ci * the battery is still absent or not. 9308c2ecf20Sopenharmony_ci */ 9318c2ecf20Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 9328c2ecf20Sopenharmony_ci &info->batt_presence_work, 9338c2ecf20Sopenharmony_ci RT9455_BATT_PRESENCE_DELAY * HZ); 9348c2ecf20Sopenharmony_ci } 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci *_is_battery_absent = is_battery_absent; 9378c2ecf20Sopenharmony_ci 9388c2ecf20Sopenharmony_ci if (alert_userspace) 9398c2ecf20Sopenharmony_ci *_alert_userspace = alert_userspace; 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci return 0; 9428c2ecf20Sopenharmony_ci} 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_cistatic int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info, 9458c2ecf20Sopenharmony_ci bool is_battery_absent, 9468c2ecf20Sopenharmony_ci bool *_alert_userspace) 9478c2ecf20Sopenharmony_ci{ 9488c2ecf20Sopenharmony_ci unsigned int irq2, mask2; 9498c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 9508c2ecf20Sopenharmony_ci bool alert_userspace = false; 9518c2ecf20Sopenharmony_ci int ret; 9528c2ecf20Sopenharmony_ci 9538c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2); 9548c2ecf20Sopenharmony_ci if (ret) { 9558c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IRQ2 register\n"); 9568c2ecf20Sopenharmony_ci return ret; 9578c2ecf20Sopenharmony_ci } 9588c2ecf20Sopenharmony_ci 9598c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2); 9608c2ecf20Sopenharmony_ci if (ret) { 9618c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read MASK2 register\n"); 9628c2ecf20Sopenharmony_ci return ret; 9638c2ecf20Sopenharmony_ci } 9648c2ecf20Sopenharmony_ci 9658c2ecf20Sopenharmony_ci if (irq2 & GET_MASK(F_CHRVPI)) { 9668c2ecf20Sopenharmony_ci dev_dbg(dev, "Charger fault occurred\n"); 9678c2ecf20Sopenharmony_ci /* 9688c2ecf20Sopenharmony_ci * CHRVPI bit is set in 2 cases: 9698c2ecf20Sopenharmony_ci * 1. when the power source is connected to the charger. 9708c2ecf20Sopenharmony_ci * 2. when the power source is disconnected from the charger. 9718c2ecf20Sopenharmony_ci * To identify the case, PWR_RDY bit is checked. Because 9728c2ecf20Sopenharmony_ci * PWR_RDY bit is set / cleared after CHRVPI interrupt is 9738c2ecf20Sopenharmony_ci * triggered, it is used delayed_work to later read PWR_RDY bit. 9748c2ecf20Sopenharmony_ci * Also, do not set to true alert_userspace, because there is no 9758c2ecf20Sopenharmony_ci * need to notify userspace when CHRVPI interrupt has occurred. 9768c2ecf20Sopenharmony_ci * Userspace will be notified after PWR_RDY bit is read. 9778c2ecf20Sopenharmony_ci */ 9788c2ecf20Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 9798c2ecf20Sopenharmony_ci &info->pwr_rdy_work, 9808c2ecf20Sopenharmony_ci RT9455_PWR_RDY_DELAY * HZ); 9818c2ecf20Sopenharmony_ci } 9828c2ecf20Sopenharmony_ci if (irq2 & GET_MASK(F_CHBATOVI)) { 9838c2ecf20Sopenharmony_ci dev_err(dev, "Battery OVP occurred\n"); 9848c2ecf20Sopenharmony_ci alert_userspace = true; 9858c2ecf20Sopenharmony_ci } 9868c2ecf20Sopenharmony_ci if (irq2 & GET_MASK(F_CHTERMI)) { 9878c2ecf20Sopenharmony_ci dev_dbg(dev, "Charge terminated\n"); 9888c2ecf20Sopenharmony_ci if (!is_battery_absent) { 9898c2ecf20Sopenharmony_ci if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) { 9908c2ecf20Sopenharmony_ci ret = regmap_field_write( 9918c2ecf20Sopenharmony_ci info->regmap_fields[F_CHTERMIM], 0x01); 9928c2ecf20Sopenharmony_ci if (ret) { 9938c2ecf20Sopenharmony_ci dev_err(dev, "Failed to mask CHTERMI interrupt\n"); 9948c2ecf20Sopenharmony_ci return ret; 9958c2ecf20Sopenharmony_ci } 9968c2ecf20Sopenharmony_ci /* 9978c2ecf20Sopenharmony_ci * Update MASK2 value, since CHTERMIM bit is 9988c2ecf20Sopenharmony_ci * set. 9998c2ecf20Sopenharmony_ci */ 10008c2ecf20Sopenharmony_ci mask2 = mask2 | GET_MASK(F_CHTERMIM); 10018c2ecf20Sopenharmony_ci } 10028c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&info->max_charging_time_work); 10038c2ecf20Sopenharmony_ci alert_userspace = true; 10048c2ecf20Sopenharmony_ci } 10058c2ecf20Sopenharmony_ci } 10068c2ecf20Sopenharmony_ci if (irq2 & GET_MASK(F_CHRCHGI)) { 10078c2ecf20Sopenharmony_ci dev_dbg(dev, "Recharge request\n"); 10088c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_CHG_EN], 10098c2ecf20Sopenharmony_ci RT9455_CHARGE_ENABLE); 10108c2ecf20Sopenharmony_ci if (ret) { 10118c2ecf20Sopenharmony_ci dev_err(dev, "Failed to enable charging\n"); 10128c2ecf20Sopenharmony_ci return ret; 10138c2ecf20Sopenharmony_ci } 10148c2ecf20Sopenharmony_ci if (mask2 & GET_MASK(F_CHTERMIM)) { 10158c2ecf20Sopenharmony_ci ret = regmap_field_write( 10168c2ecf20Sopenharmony_ci info->regmap_fields[F_CHTERMIM], 0x00); 10178c2ecf20Sopenharmony_ci if (ret) { 10188c2ecf20Sopenharmony_ci dev_err(dev, "Failed to unmask CHTERMI interrupt\n"); 10198c2ecf20Sopenharmony_ci return ret; 10208c2ecf20Sopenharmony_ci } 10218c2ecf20Sopenharmony_ci /* Update MASK2 value, since CHTERMIM bit is cleared. */ 10228c2ecf20Sopenharmony_ci mask2 = mask2 & ~GET_MASK(F_CHTERMIM); 10238c2ecf20Sopenharmony_ci } 10248c2ecf20Sopenharmony_ci if (!is_battery_absent) { 10258c2ecf20Sopenharmony_ci /* 10268c2ecf20Sopenharmony_ci * No need to check whether the charger is connected to 10278c2ecf20Sopenharmony_ci * power source when CHRCHGI is received, since CHRCHGI 10288c2ecf20Sopenharmony_ci * is not triggered if the charger is not connected to 10298c2ecf20Sopenharmony_ci * the power source. 10308c2ecf20Sopenharmony_ci */ 10318c2ecf20Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 10328c2ecf20Sopenharmony_ci &info->max_charging_time_work, 10338c2ecf20Sopenharmony_ci RT9455_MAX_CHARGING_TIME * HZ); 10348c2ecf20Sopenharmony_ci alert_userspace = true; 10358c2ecf20Sopenharmony_ci } 10368c2ecf20Sopenharmony_ci } 10378c2ecf20Sopenharmony_ci if (irq2 & GET_MASK(F_CH32MI)) { 10388c2ecf20Sopenharmony_ci dev_err(dev, "Charger fault. 32 mins timeout occurred\n"); 10398c2ecf20Sopenharmony_ci alert_userspace = true; 10408c2ecf20Sopenharmony_ci } 10418c2ecf20Sopenharmony_ci if (irq2 & GET_MASK(F_CHTREGI)) { 10428c2ecf20Sopenharmony_ci dev_warn(dev, 10438c2ecf20Sopenharmony_ci "Charger warning. Thermal regulation loop active\n"); 10448c2ecf20Sopenharmony_ci alert_userspace = true; 10458c2ecf20Sopenharmony_ci } 10468c2ecf20Sopenharmony_ci if (irq2 & GET_MASK(F_CHMIVRI)) { 10478c2ecf20Sopenharmony_ci dev_dbg(dev, 10488c2ecf20Sopenharmony_ci "Charger warning. Input voltage MIVR loop active\n"); 10498c2ecf20Sopenharmony_ci } 10508c2ecf20Sopenharmony_ci 10518c2ecf20Sopenharmony_ci if (alert_userspace) 10528c2ecf20Sopenharmony_ci *_alert_userspace = alert_userspace; 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_ci return 0; 10558c2ecf20Sopenharmony_ci} 10568c2ecf20Sopenharmony_ci 10578c2ecf20Sopenharmony_cistatic int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info, 10588c2ecf20Sopenharmony_ci bool *_alert_userspace) 10598c2ecf20Sopenharmony_ci{ 10608c2ecf20Sopenharmony_ci unsigned int irq3, mask3; 10618c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 10628c2ecf20Sopenharmony_ci bool alert_userspace = false; 10638c2ecf20Sopenharmony_ci int ret; 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3); 10668c2ecf20Sopenharmony_ci if (ret) { 10678c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IRQ3 register\n"); 10688c2ecf20Sopenharmony_ci return ret; 10698c2ecf20Sopenharmony_ci } 10708c2ecf20Sopenharmony_ci 10718c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3); 10728c2ecf20Sopenharmony_ci if (ret) { 10738c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read MASK3 register\n"); 10748c2ecf20Sopenharmony_ci return ret; 10758c2ecf20Sopenharmony_ci } 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci if (irq3 & GET_MASK(F_BSTBUSOVI)) { 10788c2ecf20Sopenharmony_ci dev_err(dev, "Boost fault. Overvoltage input occurred\n"); 10798c2ecf20Sopenharmony_ci alert_userspace = true; 10808c2ecf20Sopenharmony_ci } 10818c2ecf20Sopenharmony_ci if (irq3 & GET_MASK(F_BSTOLI)) { 10828c2ecf20Sopenharmony_ci dev_err(dev, "Boost fault. Overload\n"); 10838c2ecf20Sopenharmony_ci alert_userspace = true; 10848c2ecf20Sopenharmony_ci } 10858c2ecf20Sopenharmony_ci if (irq3 & GET_MASK(F_BSTLOWVI)) { 10868c2ecf20Sopenharmony_ci dev_err(dev, "Boost fault. Battery voltage too low\n"); 10878c2ecf20Sopenharmony_ci alert_userspace = true; 10888c2ecf20Sopenharmony_ci } 10898c2ecf20Sopenharmony_ci if (irq3 & GET_MASK(F_BST32SI)) { 10908c2ecf20Sopenharmony_ci dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n"); 10918c2ecf20Sopenharmony_ci alert_userspace = true; 10928c2ecf20Sopenharmony_ci } 10938c2ecf20Sopenharmony_ci 10948c2ecf20Sopenharmony_ci if (alert_userspace) { 10958c2ecf20Sopenharmony_ci dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n"); 10968c2ecf20Sopenharmony_ci ret = rt9455_set_voreg_before_charge_mode(info); 10978c2ecf20Sopenharmony_ci if (ret) { 10988c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set VOREG before entering charge mode\n"); 10998c2ecf20Sopenharmony_ci return ret; 11008c2ecf20Sopenharmony_ci } 11018c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 11028c2ecf20Sopenharmony_ci RT9455_CHARGE_MODE); 11038c2ecf20Sopenharmony_ci if (ret) { 11048c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set charger in charge mode\n"); 11058c2ecf20Sopenharmony_ci return ret; 11068c2ecf20Sopenharmony_ci } 11078c2ecf20Sopenharmony_ci *_alert_userspace = alert_userspace; 11088c2ecf20Sopenharmony_ci } 11098c2ecf20Sopenharmony_ci 11108c2ecf20Sopenharmony_ci return 0; 11118c2ecf20Sopenharmony_ci} 11128c2ecf20Sopenharmony_ci 11138c2ecf20Sopenharmony_cistatic irqreturn_t rt9455_irq_handler_thread(int irq, void *data) 11148c2ecf20Sopenharmony_ci{ 11158c2ecf20Sopenharmony_ci struct rt9455_info *info = data; 11168c2ecf20Sopenharmony_ci struct device *dev; 11178c2ecf20Sopenharmony_ci bool alert_userspace = false; 11188c2ecf20Sopenharmony_ci bool is_battery_absent = false; 11198c2ecf20Sopenharmony_ci unsigned int status; 11208c2ecf20Sopenharmony_ci int ret; 11218c2ecf20Sopenharmony_ci 11228c2ecf20Sopenharmony_ci if (!info) 11238c2ecf20Sopenharmony_ci return IRQ_NONE; 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_ci dev = &info->client->dev; 11268c2ecf20Sopenharmony_ci 11278c2ecf20Sopenharmony_ci if (irq != info->client->irq) { 11288c2ecf20Sopenharmony_ci dev_err(dev, "Interrupt is not for RT9455 charger\n"); 11298c2ecf20Sopenharmony_ci return IRQ_NONE; 11308c2ecf20Sopenharmony_ci } 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_STAT], &status); 11338c2ecf20Sopenharmony_ci if (ret) { 11348c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read STAT bits\n"); 11358c2ecf20Sopenharmony_ci return IRQ_HANDLED; 11368c2ecf20Sopenharmony_ci } 11378c2ecf20Sopenharmony_ci dev_dbg(dev, "Charger status is %d\n", status); 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_ci /* 11408c2ecf20Sopenharmony_ci * Each function that processes an IRQ register receives as output 11418c2ecf20Sopenharmony_ci * parameter alert_userspace pointer. alert_userspace is set to true 11428c2ecf20Sopenharmony_ci * in such a function only if an interrupt has occurred in the 11438c2ecf20Sopenharmony_ci * respective interrupt register. This way, it is avoided the following 11448c2ecf20Sopenharmony_ci * case: interrupt occurs only in IRQ1 register, 11458c2ecf20Sopenharmony_ci * rt9455_irq_handler_check_irq1_register() function sets to true 11468c2ecf20Sopenharmony_ci * alert_userspace, but rt9455_irq_handler_check_irq2_register() 11478c2ecf20Sopenharmony_ci * and rt9455_irq_handler_check_irq3_register() functions set to false 11488c2ecf20Sopenharmony_ci * alert_userspace and power_supply_changed() is never called. 11498c2ecf20Sopenharmony_ci */ 11508c2ecf20Sopenharmony_ci ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent, 11518c2ecf20Sopenharmony_ci &alert_userspace); 11528c2ecf20Sopenharmony_ci if (ret) { 11538c2ecf20Sopenharmony_ci dev_err(dev, "Failed to handle IRQ1 register\n"); 11548c2ecf20Sopenharmony_ci return IRQ_HANDLED; 11558c2ecf20Sopenharmony_ci } 11568c2ecf20Sopenharmony_ci 11578c2ecf20Sopenharmony_ci ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent, 11588c2ecf20Sopenharmony_ci &alert_userspace); 11598c2ecf20Sopenharmony_ci if (ret) { 11608c2ecf20Sopenharmony_ci dev_err(dev, "Failed to handle IRQ2 register\n"); 11618c2ecf20Sopenharmony_ci return IRQ_HANDLED; 11628c2ecf20Sopenharmony_ci } 11638c2ecf20Sopenharmony_ci 11648c2ecf20Sopenharmony_ci ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace); 11658c2ecf20Sopenharmony_ci if (ret) { 11668c2ecf20Sopenharmony_ci dev_err(dev, "Failed to handle IRQ3 register\n"); 11678c2ecf20Sopenharmony_ci return IRQ_HANDLED; 11688c2ecf20Sopenharmony_ci } 11698c2ecf20Sopenharmony_ci 11708c2ecf20Sopenharmony_ci if (alert_userspace) { 11718c2ecf20Sopenharmony_ci /* 11728c2ecf20Sopenharmony_ci * Sometimes, an interrupt occurs while rt9455_probe() function 11738c2ecf20Sopenharmony_ci * is executing and power_supply_register() is not yet called. 11748c2ecf20Sopenharmony_ci * Do not call power_supply_changed() in this case. 11758c2ecf20Sopenharmony_ci */ 11768c2ecf20Sopenharmony_ci if (info->charger) 11778c2ecf20Sopenharmony_ci power_supply_changed(info->charger); 11788c2ecf20Sopenharmony_ci } 11798c2ecf20Sopenharmony_ci 11808c2ecf20Sopenharmony_ci return IRQ_HANDLED; 11818c2ecf20Sopenharmony_ci} 11828c2ecf20Sopenharmony_ci 11838c2ecf20Sopenharmony_cistatic int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg, 11848c2ecf20Sopenharmony_ci u32 *ieoc_percentage, 11858c2ecf20Sopenharmony_ci u32 *mivr, u32 *iaicr) 11868c2ecf20Sopenharmony_ci{ 11878c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 11888c2ecf20Sopenharmony_ci int ret; 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_ci if (!dev->of_node && !ACPI_HANDLE(dev)) { 11918c2ecf20Sopenharmony_ci dev_err(dev, "No support for either device tree or ACPI\n"); 11928c2ecf20Sopenharmony_ci return -EINVAL; 11938c2ecf20Sopenharmony_ci } 11948c2ecf20Sopenharmony_ci /* 11958c2ecf20Sopenharmony_ci * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory 11968c2ecf20Sopenharmony_ci * parameters. 11978c2ecf20Sopenharmony_ci */ 11988c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, "richtek,output-charge-current", 11998c2ecf20Sopenharmony_ci ichrg); 12008c2ecf20Sopenharmony_ci if (ret) { 12018c2ecf20Sopenharmony_ci dev_err(dev, "Error: missing \"output-charge-current\" property\n"); 12028c2ecf20Sopenharmony_ci return ret; 12038c2ecf20Sopenharmony_ci } 12048c2ecf20Sopenharmony_ci 12058c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage", 12068c2ecf20Sopenharmony_ci ieoc_percentage); 12078c2ecf20Sopenharmony_ci if (ret) { 12088c2ecf20Sopenharmony_ci dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n"); 12098c2ecf20Sopenharmony_ci return ret; 12108c2ecf20Sopenharmony_ci } 12118c2ecf20Sopenharmony_ci 12128c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, 12138c2ecf20Sopenharmony_ci "richtek,battery-regulation-voltage", 12148c2ecf20Sopenharmony_ci &info->voreg); 12158c2ecf20Sopenharmony_ci if (ret) { 12168c2ecf20Sopenharmony_ci dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n"); 12178c2ecf20Sopenharmony_ci return ret; 12188c2ecf20Sopenharmony_ci } 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, "richtek,boost-output-voltage", 12218c2ecf20Sopenharmony_ci &info->boost_voltage); 12228c2ecf20Sopenharmony_ci if (ret) { 12238c2ecf20Sopenharmony_ci dev_err(dev, "Error: missing \"boost-output-voltage\" property\n"); 12248c2ecf20Sopenharmony_ci return ret; 12258c2ecf20Sopenharmony_ci } 12268c2ecf20Sopenharmony_ci 12278c2ecf20Sopenharmony_ci /* 12288c2ecf20Sopenharmony_ci * MIVR and IAICR are optional parameters. Do not return error if one of 12298c2ecf20Sopenharmony_ci * them is not present in ACPI table or device tree specification. 12308c2ecf20Sopenharmony_ci */ 12318c2ecf20Sopenharmony_ci device_property_read_u32(dev, "richtek,min-input-voltage-regulation", 12328c2ecf20Sopenharmony_ci mivr); 12338c2ecf20Sopenharmony_ci device_property_read_u32(dev, "richtek,avg-input-current-regulation", 12348c2ecf20Sopenharmony_ci iaicr); 12358c2ecf20Sopenharmony_ci 12368c2ecf20Sopenharmony_ci return 0; 12378c2ecf20Sopenharmony_ci} 12388c2ecf20Sopenharmony_ci 12398c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_PHY) 12408c2ecf20Sopenharmony_cistatic int rt9455_usb_event_none(struct rt9455_info *info, 12418c2ecf20Sopenharmony_ci u8 opa_mode, u8 iaicr) 12428c2ecf20Sopenharmony_ci{ 12438c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 12448c2ecf20Sopenharmony_ci int ret; 12458c2ecf20Sopenharmony_ci 12468c2ecf20Sopenharmony_ci if (opa_mode == RT9455_BOOST_MODE) { 12478c2ecf20Sopenharmony_ci ret = rt9455_set_voreg_before_charge_mode(info); 12488c2ecf20Sopenharmony_ci if (ret) { 12498c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set VOREG before entering charge mode\n"); 12508c2ecf20Sopenharmony_ci return ret; 12518c2ecf20Sopenharmony_ci } 12528c2ecf20Sopenharmony_ci /* 12538c2ecf20Sopenharmony_ci * If the charger is in boost mode, and it has received 12548c2ecf20Sopenharmony_ci * USB_EVENT_NONE, this means the consumer device powered by the 12558c2ecf20Sopenharmony_ci * charger is not connected anymore. 12568c2ecf20Sopenharmony_ci * In this case, the charger goes into charge mode. 12578c2ecf20Sopenharmony_ci */ 12588c2ecf20Sopenharmony_ci dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n"); 12598c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 12608c2ecf20Sopenharmony_ci RT9455_CHARGE_MODE); 12618c2ecf20Sopenharmony_ci if (ret) { 12628c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set charger in charge mode\n"); 12638c2ecf20Sopenharmony_ci return NOTIFY_DONE; 12648c2ecf20Sopenharmony_ci } 12658c2ecf20Sopenharmony_ci } 12668c2ecf20Sopenharmony_ci 12678c2ecf20Sopenharmony_ci dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n"); 12688c2ecf20Sopenharmony_ci if (iaicr != RT9455_IAICR_100MA) { 12698c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_IAICR], 12708c2ecf20Sopenharmony_ci RT9455_IAICR_100MA); 12718c2ecf20Sopenharmony_ci if (ret) { 12728c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set IAICR value\n"); 12738c2ecf20Sopenharmony_ci return NOTIFY_DONE; 12748c2ecf20Sopenharmony_ci } 12758c2ecf20Sopenharmony_ci } 12768c2ecf20Sopenharmony_ci 12778c2ecf20Sopenharmony_ci return NOTIFY_OK; 12788c2ecf20Sopenharmony_ci} 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_cistatic int rt9455_usb_event_vbus(struct rt9455_info *info, 12818c2ecf20Sopenharmony_ci u8 opa_mode, u8 iaicr) 12828c2ecf20Sopenharmony_ci{ 12838c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 12848c2ecf20Sopenharmony_ci int ret; 12858c2ecf20Sopenharmony_ci 12868c2ecf20Sopenharmony_ci if (opa_mode == RT9455_BOOST_MODE) { 12878c2ecf20Sopenharmony_ci ret = rt9455_set_voreg_before_charge_mode(info); 12888c2ecf20Sopenharmony_ci if (ret) { 12898c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set VOREG before entering charge mode\n"); 12908c2ecf20Sopenharmony_ci return ret; 12918c2ecf20Sopenharmony_ci } 12928c2ecf20Sopenharmony_ci /* 12938c2ecf20Sopenharmony_ci * If the charger is in boost mode, and it has received 12948c2ecf20Sopenharmony_ci * USB_EVENT_VBUS, this means the consumer device powered by the 12958c2ecf20Sopenharmony_ci * charger is not connected anymore. 12968c2ecf20Sopenharmony_ci * In this case, the charger goes into charge mode. 12978c2ecf20Sopenharmony_ci */ 12988c2ecf20Sopenharmony_ci dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n"); 12998c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 13008c2ecf20Sopenharmony_ci RT9455_CHARGE_MODE); 13018c2ecf20Sopenharmony_ci if (ret) { 13028c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set charger in charge mode\n"); 13038c2ecf20Sopenharmony_ci return NOTIFY_DONE; 13048c2ecf20Sopenharmony_ci } 13058c2ecf20Sopenharmony_ci } 13068c2ecf20Sopenharmony_ci 13078c2ecf20Sopenharmony_ci dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n"); 13088c2ecf20Sopenharmony_ci if (iaicr != RT9455_IAICR_500MA) { 13098c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_IAICR], 13108c2ecf20Sopenharmony_ci RT9455_IAICR_500MA); 13118c2ecf20Sopenharmony_ci if (ret) { 13128c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set IAICR value\n"); 13138c2ecf20Sopenharmony_ci return NOTIFY_DONE; 13148c2ecf20Sopenharmony_ci } 13158c2ecf20Sopenharmony_ci } 13168c2ecf20Sopenharmony_ci 13178c2ecf20Sopenharmony_ci return NOTIFY_OK; 13188c2ecf20Sopenharmony_ci} 13198c2ecf20Sopenharmony_ci 13208c2ecf20Sopenharmony_cistatic int rt9455_usb_event_id(struct rt9455_info *info, 13218c2ecf20Sopenharmony_ci u8 opa_mode, u8 iaicr) 13228c2ecf20Sopenharmony_ci{ 13238c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 13248c2ecf20Sopenharmony_ci int ret; 13258c2ecf20Sopenharmony_ci 13268c2ecf20Sopenharmony_ci if (opa_mode == RT9455_CHARGE_MODE) { 13278c2ecf20Sopenharmony_ci ret = rt9455_set_boost_voltage_before_boost_mode(info); 13288c2ecf20Sopenharmony_ci if (ret) { 13298c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set boost output voltage before entering boost mode\n"); 13308c2ecf20Sopenharmony_ci return ret; 13318c2ecf20Sopenharmony_ci } 13328c2ecf20Sopenharmony_ci /* 13338c2ecf20Sopenharmony_ci * If the charger is in charge mode, and it has received 13348c2ecf20Sopenharmony_ci * USB_EVENT_ID, this means a consumer device is connected and 13358c2ecf20Sopenharmony_ci * it should be powered by the charger. 13368c2ecf20Sopenharmony_ci * In this case, the charger goes into boost mode. 13378c2ecf20Sopenharmony_ci */ 13388c2ecf20Sopenharmony_ci dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n"); 13398c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 13408c2ecf20Sopenharmony_ci RT9455_BOOST_MODE); 13418c2ecf20Sopenharmony_ci if (ret) { 13428c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set charger in boost mode\n"); 13438c2ecf20Sopenharmony_ci return NOTIFY_DONE; 13448c2ecf20Sopenharmony_ci } 13458c2ecf20Sopenharmony_ci } 13468c2ecf20Sopenharmony_ci 13478c2ecf20Sopenharmony_ci dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n"); 13488c2ecf20Sopenharmony_ci if (iaicr != RT9455_IAICR_100MA) { 13498c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_IAICR], 13508c2ecf20Sopenharmony_ci RT9455_IAICR_100MA); 13518c2ecf20Sopenharmony_ci if (ret) { 13528c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set IAICR value\n"); 13538c2ecf20Sopenharmony_ci return NOTIFY_DONE; 13548c2ecf20Sopenharmony_ci } 13558c2ecf20Sopenharmony_ci } 13568c2ecf20Sopenharmony_ci 13578c2ecf20Sopenharmony_ci return NOTIFY_OK; 13588c2ecf20Sopenharmony_ci} 13598c2ecf20Sopenharmony_ci 13608c2ecf20Sopenharmony_cistatic int rt9455_usb_event_charger(struct rt9455_info *info, 13618c2ecf20Sopenharmony_ci u8 opa_mode, u8 iaicr) 13628c2ecf20Sopenharmony_ci{ 13638c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 13648c2ecf20Sopenharmony_ci int ret; 13658c2ecf20Sopenharmony_ci 13668c2ecf20Sopenharmony_ci if (opa_mode == RT9455_BOOST_MODE) { 13678c2ecf20Sopenharmony_ci ret = rt9455_set_voreg_before_charge_mode(info); 13688c2ecf20Sopenharmony_ci if (ret) { 13698c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set VOREG before entering charge mode\n"); 13708c2ecf20Sopenharmony_ci return ret; 13718c2ecf20Sopenharmony_ci } 13728c2ecf20Sopenharmony_ci /* 13738c2ecf20Sopenharmony_ci * If the charger is in boost mode, and it has received 13748c2ecf20Sopenharmony_ci * USB_EVENT_CHARGER, this means the consumer device powered by 13758c2ecf20Sopenharmony_ci * the charger is not connected anymore. 13768c2ecf20Sopenharmony_ci * In this case, the charger goes into charge mode. 13778c2ecf20Sopenharmony_ci */ 13788c2ecf20Sopenharmony_ci dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n"); 13798c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_OPA_MODE], 13808c2ecf20Sopenharmony_ci RT9455_CHARGE_MODE); 13818c2ecf20Sopenharmony_ci if (ret) { 13828c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set charger in charge mode\n"); 13838c2ecf20Sopenharmony_ci return NOTIFY_DONE; 13848c2ecf20Sopenharmony_ci } 13858c2ecf20Sopenharmony_ci } 13868c2ecf20Sopenharmony_ci 13878c2ecf20Sopenharmony_ci dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n"); 13888c2ecf20Sopenharmony_ci if (iaicr != RT9455_IAICR_NO_LIMIT) { 13898c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_IAICR], 13908c2ecf20Sopenharmony_ci RT9455_IAICR_NO_LIMIT); 13918c2ecf20Sopenharmony_ci if (ret) { 13928c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set IAICR value\n"); 13938c2ecf20Sopenharmony_ci return NOTIFY_DONE; 13948c2ecf20Sopenharmony_ci } 13958c2ecf20Sopenharmony_ci } 13968c2ecf20Sopenharmony_ci 13978c2ecf20Sopenharmony_ci return NOTIFY_OK; 13988c2ecf20Sopenharmony_ci} 13998c2ecf20Sopenharmony_ci 14008c2ecf20Sopenharmony_cistatic int rt9455_usb_event(struct notifier_block *nb, 14018c2ecf20Sopenharmony_ci unsigned long event, void *power) 14028c2ecf20Sopenharmony_ci{ 14038c2ecf20Sopenharmony_ci struct rt9455_info *info = container_of(nb, struct rt9455_info, nb); 14048c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 14058c2ecf20Sopenharmony_ci unsigned int opa_mode, iaicr; 14068c2ecf20Sopenharmony_ci int ret; 14078c2ecf20Sopenharmony_ci 14088c2ecf20Sopenharmony_ci /* 14098c2ecf20Sopenharmony_ci * Determine whether the charger is in charge mode 14108c2ecf20Sopenharmony_ci * or in boost mode. 14118c2ecf20Sopenharmony_ci */ 14128c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_OPA_MODE], 14138c2ecf20Sopenharmony_ci &opa_mode); 14148c2ecf20Sopenharmony_ci if (ret) { 14158c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read OPA_MODE value\n"); 14168c2ecf20Sopenharmony_ci return NOTIFY_DONE; 14178c2ecf20Sopenharmony_ci } 14188c2ecf20Sopenharmony_ci 14198c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_IAICR], 14208c2ecf20Sopenharmony_ci &iaicr); 14218c2ecf20Sopenharmony_ci if (ret) { 14228c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IAICR value\n"); 14238c2ecf20Sopenharmony_ci return NOTIFY_DONE; 14248c2ecf20Sopenharmony_ci } 14258c2ecf20Sopenharmony_ci 14268c2ecf20Sopenharmony_ci dev_dbg(dev, "Received USB event %lu\n", event); 14278c2ecf20Sopenharmony_ci switch (event) { 14288c2ecf20Sopenharmony_ci case USB_EVENT_NONE: 14298c2ecf20Sopenharmony_ci return rt9455_usb_event_none(info, opa_mode, iaicr); 14308c2ecf20Sopenharmony_ci case USB_EVENT_VBUS: 14318c2ecf20Sopenharmony_ci return rt9455_usb_event_vbus(info, opa_mode, iaicr); 14328c2ecf20Sopenharmony_ci case USB_EVENT_ID: 14338c2ecf20Sopenharmony_ci return rt9455_usb_event_id(info, opa_mode, iaicr); 14348c2ecf20Sopenharmony_ci case USB_EVENT_CHARGER: 14358c2ecf20Sopenharmony_ci return rt9455_usb_event_charger(info, opa_mode, iaicr); 14368c2ecf20Sopenharmony_ci default: 14378c2ecf20Sopenharmony_ci dev_err(dev, "Unknown USB event\n"); 14388c2ecf20Sopenharmony_ci } 14398c2ecf20Sopenharmony_ci return NOTIFY_DONE; 14408c2ecf20Sopenharmony_ci} 14418c2ecf20Sopenharmony_ci#endif 14428c2ecf20Sopenharmony_ci 14438c2ecf20Sopenharmony_cistatic void rt9455_pwr_rdy_work_callback(struct work_struct *work) 14448c2ecf20Sopenharmony_ci{ 14458c2ecf20Sopenharmony_ci struct rt9455_info *info = container_of(work, struct rt9455_info, 14468c2ecf20Sopenharmony_ci pwr_rdy_work.work); 14478c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 14488c2ecf20Sopenharmony_ci unsigned int pwr_rdy; 14498c2ecf20Sopenharmony_ci int ret; 14508c2ecf20Sopenharmony_ci 14518c2ecf20Sopenharmony_ci ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy); 14528c2ecf20Sopenharmony_ci if (ret) { 14538c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read PWR_RDY bit\n"); 14548c2ecf20Sopenharmony_ci return; 14558c2ecf20Sopenharmony_ci } 14568c2ecf20Sopenharmony_ci switch (pwr_rdy) { 14578c2ecf20Sopenharmony_ci case RT9455_PWR_FAULT: 14588c2ecf20Sopenharmony_ci dev_dbg(dev, "Charger disconnected from power source\n"); 14598c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&info->max_charging_time_work); 14608c2ecf20Sopenharmony_ci break; 14618c2ecf20Sopenharmony_ci case RT9455_PWR_GOOD: 14628c2ecf20Sopenharmony_ci dev_dbg(dev, "Charger connected to power source\n"); 14638c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_CHG_EN], 14648c2ecf20Sopenharmony_ci RT9455_CHARGE_ENABLE); 14658c2ecf20Sopenharmony_ci if (ret) { 14668c2ecf20Sopenharmony_ci dev_err(dev, "Failed to enable charging\n"); 14678c2ecf20Sopenharmony_ci return; 14688c2ecf20Sopenharmony_ci } 14698c2ecf20Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 14708c2ecf20Sopenharmony_ci &info->max_charging_time_work, 14718c2ecf20Sopenharmony_ci RT9455_MAX_CHARGING_TIME * HZ); 14728c2ecf20Sopenharmony_ci break; 14738c2ecf20Sopenharmony_ci } 14748c2ecf20Sopenharmony_ci /* 14758c2ecf20Sopenharmony_ci * Notify userspace that the charger has been either connected to or 14768c2ecf20Sopenharmony_ci * disconnected from the power source. 14778c2ecf20Sopenharmony_ci */ 14788c2ecf20Sopenharmony_ci power_supply_changed(info->charger); 14798c2ecf20Sopenharmony_ci} 14808c2ecf20Sopenharmony_ci 14818c2ecf20Sopenharmony_cistatic void rt9455_max_charging_time_work_callback(struct work_struct *work) 14828c2ecf20Sopenharmony_ci{ 14838c2ecf20Sopenharmony_ci struct rt9455_info *info = container_of(work, struct rt9455_info, 14848c2ecf20Sopenharmony_ci max_charging_time_work.work); 14858c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 14868c2ecf20Sopenharmony_ci int ret; 14878c2ecf20Sopenharmony_ci 14888c2ecf20Sopenharmony_ci dev_err(dev, "Battery has been charging for at least 6 hours and is not yet fully charged. Battery is dead, therefore charging is disabled.\n"); 14898c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_CHG_EN], 14908c2ecf20Sopenharmony_ci RT9455_CHARGE_DISABLE); 14918c2ecf20Sopenharmony_ci if (ret) 14928c2ecf20Sopenharmony_ci dev_err(dev, "Failed to disable charging\n"); 14938c2ecf20Sopenharmony_ci} 14948c2ecf20Sopenharmony_ci 14958c2ecf20Sopenharmony_cistatic void rt9455_batt_presence_work_callback(struct work_struct *work) 14968c2ecf20Sopenharmony_ci{ 14978c2ecf20Sopenharmony_ci struct rt9455_info *info = container_of(work, struct rt9455_info, 14988c2ecf20Sopenharmony_ci batt_presence_work.work); 14998c2ecf20Sopenharmony_ci struct device *dev = &info->client->dev; 15008c2ecf20Sopenharmony_ci unsigned int irq1, mask1; 15018c2ecf20Sopenharmony_ci int ret; 15028c2ecf20Sopenharmony_ci 15038c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1); 15048c2ecf20Sopenharmony_ci if (ret) { 15058c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read IRQ1 register\n"); 15068c2ecf20Sopenharmony_ci return; 15078c2ecf20Sopenharmony_ci } 15088c2ecf20Sopenharmony_ci 15098c2ecf20Sopenharmony_ci /* 15108c2ecf20Sopenharmony_ci * If the battery is still absent, batt_presence_work is rescheduled. 15118c2ecf20Sopenharmony_ci * Otherwise, max_charging_time is scheduled. 15128c2ecf20Sopenharmony_ci */ 15138c2ecf20Sopenharmony_ci if (irq1 & GET_MASK(F_BATAB)) { 15148c2ecf20Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 15158c2ecf20Sopenharmony_ci &info->batt_presence_work, 15168c2ecf20Sopenharmony_ci RT9455_BATT_PRESENCE_DELAY * HZ); 15178c2ecf20Sopenharmony_ci } else { 15188c2ecf20Sopenharmony_ci queue_delayed_work(system_power_efficient_wq, 15198c2ecf20Sopenharmony_ci &info->max_charging_time_work, 15208c2ecf20Sopenharmony_ci RT9455_MAX_CHARGING_TIME * HZ); 15218c2ecf20Sopenharmony_ci 15228c2ecf20Sopenharmony_ci ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1); 15238c2ecf20Sopenharmony_ci if (ret) { 15248c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read MASK1 register\n"); 15258c2ecf20Sopenharmony_ci return; 15268c2ecf20Sopenharmony_ci } 15278c2ecf20Sopenharmony_ci 15288c2ecf20Sopenharmony_ci if (mask1 & GET_MASK(F_BATABM)) { 15298c2ecf20Sopenharmony_ci ret = regmap_field_write(info->regmap_fields[F_BATABM], 15308c2ecf20Sopenharmony_ci 0x00); 15318c2ecf20Sopenharmony_ci if (ret) 15328c2ecf20Sopenharmony_ci dev_err(dev, "Failed to unmask BATAB interrupt\n"); 15338c2ecf20Sopenharmony_ci } 15348c2ecf20Sopenharmony_ci /* 15358c2ecf20Sopenharmony_ci * Notify userspace that the battery is now connected to the 15368c2ecf20Sopenharmony_ci * charger. 15378c2ecf20Sopenharmony_ci */ 15388c2ecf20Sopenharmony_ci power_supply_changed(info->charger); 15398c2ecf20Sopenharmony_ci } 15408c2ecf20Sopenharmony_ci} 15418c2ecf20Sopenharmony_ci 15428c2ecf20Sopenharmony_cistatic const struct power_supply_desc rt9455_charger_desc = { 15438c2ecf20Sopenharmony_ci .name = RT9455_DRIVER_NAME, 15448c2ecf20Sopenharmony_ci .type = POWER_SUPPLY_TYPE_USB, 15458c2ecf20Sopenharmony_ci .properties = rt9455_charger_properties, 15468c2ecf20Sopenharmony_ci .num_properties = ARRAY_SIZE(rt9455_charger_properties), 15478c2ecf20Sopenharmony_ci .get_property = rt9455_charger_get_property, 15488c2ecf20Sopenharmony_ci}; 15498c2ecf20Sopenharmony_ci 15508c2ecf20Sopenharmony_cistatic bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg) 15518c2ecf20Sopenharmony_ci{ 15528c2ecf20Sopenharmony_ci switch (reg) { 15538c2ecf20Sopenharmony_ci case RT9455_REG_DEV_ID: 15548c2ecf20Sopenharmony_ci case RT9455_REG_IRQ1: 15558c2ecf20Sopenharmony_ci case RT9455_REG_IRQ2: 15568c2ecf20Sopenharmony_ci case RT9455_REG_IRQ3: 15578c2ecf20Sopenharmony_ci return false; 15588c2ecf20Sopenharmony_ci default: 15598c2ecf20Sopenharmony_ci return true; 15608c2ecf20Sopenharmony_ci } 15618c2ecf20Sopenharmony_ci} 15628c2ecf20Sopenharmony_ci 15638c2ecf20Sopenharmony_cistatic bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg) 15648c2ecf20Sopenharmony_ci{ 15658c2ecf20Sopenharmony_ci switch (reg) { 15668c2ecf20Sopenharmony_ci case RT9455_REG_DEV_ID: 15678c2ecf20Sopenharmony_ci case RT9455_REG_CTRL5: 15688c2ecf20Sopenharmony_ci case RT9455_REG_CTRL6: 15698c2ecf20Sopenharmony_ci return false; 15708c2ecf20Sopenharmony_ci default: 15718c2ecf20Sopenharmony_ci return true; 15728c2ecf20Sopenharmony_ci } 15738c2ecf20Sopenharmony_ci} 15748c2ecf20Sopenharmony_ci 15758c2ecf20Sopenharmony_cistatic const struct regmap_config rt9455_regmap_config = { 15768c2ecf20Sopenharmony_ci .reg_bits = 8, 15778c2ecf20Sopenharmony_ci .val_bits = 8, 15788c2ecf20Sopenharmony_ci .writeable_reg = rt9455_is_writeable_reg, 15798c2ecf20Sopenharmony_ci .volatile_reg = rt9455_is_volatile_reg, 15808c2ecf20Sopenharmony_ci .max_register = RT9455_REG_MASK3, 15818c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 15828c2ecf20Sopenharmony_ci}; 15838c2ecf20Sopenharmony_ci 15848c2ecf20Sopenharmony_cistatic int rt9455_probe(struct i2c_client *client, 15858c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 15868c2ecf20Sopenharmony_ci{ 15878c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 15888c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 15898c2ecf20Sopenharmony_ci struct rt9455_info *info; 15908c2ecf20Sopenharmony_ci struct power_supply_config rt9455_charger_config = {}; 15918c2ecf20Sopenharmony_ci /* 15928c2ecf20Sopenharmony_ci * Mandatory device-specific data values. Also, VOREG and boost output 15938c2ecf20Sopenharmony_ci * voltage are mandatory values, but they are stored in rt9455_info 15948c2ecf20Sopenharmony_ci * structure. 15958c2ecf20Sopenharmony_ci */ 15968c2ecf20Sopenharmony_ci u32 ichrg, ieoc_percentage; 15978c2ecf20Sopenharmony_ci /* Optional device-specific data values. */ 15988c2ecf20Sopenharmony_ci u32 mivr = -1, iaicr = -1; 15998c2ecf20Sopenharmony_ci int i, ret; 16008c2ecf20Sopenharmony_ci 16018c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 16028c2ecf20Sopenharmony_ci dev_err(dev, "No support for SMBUS_BYTE_DATA\n"); 16038c2ecf20Sopenharmony_ci return -ENODEV; 16048c2ecf20Sopenharmony_ci } 16058c2ecf20Sopenharmony_ci info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 16068c2ecf20Sopenharmony_ci if (!info) 16078c2ecf20Sopenharmony_ci return -ENOMEM; 16088c2ecf20Sopenharmony_ci 16098c2ecf20Sopenharmony_ci info->client = client; 16108c2ecf20Sopenharmony_ci i2c_set_clientdata(client, info); 16118c2ecf20Sopenharmony_ci 16128c2ecf20Sopenharmony_ci info->regmap = devm_regmap_init_i2c(client, 16138c2ecf20Sopenharmony_ci &rt9455_regmap_config); 16148c2ecf20Sopenharmony_ci if (IS_ERR(info->regmap)) { 16158c2ecf20Sopenharmony_ci dev_err(dev, "Failed to initialize register map\n"); 16168c2ecf20Sopenharmony_ci return -EINVAL; 16178c2ecf20Sopenharmony_ci } 16188c2ecf20Sopenharmony_ci 16198c2ecf20Sopenharmony_ci for (i = 0; i < F_MAX_FIELDS; i++) { 16208c2ecf20Sopenharmony_ci info->regmap_fields[i] = 16218c2ecf20Sopenharmony_ci devm_regmap_field_alloc(dev, info->regmap, 16228c2ecf20Sopenharmony_ci rt9455_reg_fields[i]); 16238c2ecf20Sopenharmony_ci if (IS_ERR(info->regmap_fields[i])) { 16248c2ecf20Sopenharmony_ci dev_err(dev, 16258c2ecf20Sopenharmony_ci "Failed to allocate regmap field = %d\n", i); 16268c2ecf20Sopenharmony_ci return PTR_ERR(info->regmap_fields[i]); 16278c2ecf20Sopenharmony_ci } 16288c2ecf20Sopenharmony_ci } 16298c2ecf20Sopenharmony_ci 16308c2ecf20Sopenharmony_ci ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage, 16318c2ecf20Sopenharmony_ci &mivr, &iaicr); 16328c2ecf20Sopenharmony_ci if (ret) { 16338c2ecf20Sopenharmony_ci dev_err(dev, "Failed to discover charger\n"); 16348c2ecf20Sopenharmony_ci return ret; 16358c2ecf20Sopenharmony_ci } 16368c2ecf20Sopenharmony_ci 16378c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_PHY) 16388c2ecf20Sopenharmony_ci info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); 16398c2ecf20Sopenharmony_ci if (IS_ERR(info->usb_phy)) { 16408c2ecf20Sopenharmony_ci dev_err(dev, "Failed to get USB transceiver\n"); 16418c2ecf20Sopenharmony_ci } else { 16428c2ecf20Sopenharmony_ci info->nb.notifier_call = rt9455_usb_event; 16438c2ecf20Sopenharmony_ci ret = usb_register_notifier(info->usb_phy, &info->nb); 16448c2ecf20Sopenharmony_ci if (ret) { 16458c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register USB notifier\n"); 16468c2ecf20Sopenharmony_ci /* 16478c2ecf20Sopenharmony_ci * If usb_register_notifier() fails, set notifier_call 16488c2ecf20Sopenharmony_ci * to NULL, to avoid calling usb_unregister_notifier(). 16498c2ecf20Sopenharmony_ci */ 16508c2ecf20Sopenharmony_ci info->nb.notifier_call = NULL; 16518c2ecf20Sopenharmony_ci } 16528c2ecf20Sopenharmony_ci } 16538c2ecf20Sopenharmony_ci#endif 16548c2ecf20Sopenharmony_ci 16558c2ecf20Sopenharmony_ci INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback); 16568c2ecf20Sopenharmony_ci INIT_DEFERRABLE_WORK(&info->max_charging_time_work, 16578c2ecf20Sopenharmony_ci rt9455_max_charging_time_work_callback); 16588c2ecf20Sopenharmony_ci INIT_DEFERRABLE_WORK(&info->batt_presence_work, 16598c2ecf20Sopenharmony_ci rt9455_batt_presence_work_callback); 16608c2ecf20Sopenharmony_ci 16618c2ecf20Sopenharmony_ci rt9455_charger_config.of_node = dev->of_node; 16628c2ecf20Sopenharmony_ci rt9455_charger_config.drv_data = info; 16638c2ecf20Sopenharmony_ci rt9455_charger_config.supplied_to = rt9455_charger_supplied_to; 16648c2ecf20Sopenharmony_ci rt9455_charger_config.num_supplicants = 16658c2ecf20Sopenharmony_ci ARRAY_SIZE(rt9455_charger_supplied_to); 16668c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(dev, client->irq, NULL, 16678c2ecf20Sopenharmony_ci rt9455_irq_handler_thread, 16688c2ecf20Sopenharmony_ci IRQF_TRIGGER_LOW | IRQF_ONESHOT, 16698c2ecf20Sopenharmony_ci RT9455_DRIVER_NAME, info); 16708c2ecf20Sopenharmony_ci if (ret) { 16718c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register IRQ handler\n"); 16728c2ecf20Sopenharmony_ci goto put_usb_notifier; 16738c2ecf20Sopenharmony_ci } 16748c2ecf20Sopenharmony_ci 16758c2ecf20Sopenharmony_ci ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr); 16768c2ecf20Sopenharmony_ci if (ret) { 16778c2ecf20Sopenharmony_ci dev_err(dev, "Failed to set charger to its default values\n"); 16788c2ecf20Sopenharmony_ci goto put_usb_notifier; 16798c2ecf20Sopenharmony_ci } 16808c2ecf20Sopenharmony_ci 16818c2ecf20Sopenharmony_ci info->charger = devm_power_supply_register(dev, &rt9455_charger_desc, 16828c2ecf20Sopenharmony_ci &rt9455_charger_config); 16838c2ecf20Sopenharmony_ci if (IS_ERR(info->charger)) { 16848c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register charger\n"); 16858c2ecf20Sopenharmony_ci ret = PTR_ERR(info->charger); 16868c2ecf20Sopenharmony_ci goto put_usb_notifier; 16878c2ecf20Sopenharmony_ci } 16888c2ecf20Sopenharmony_ci 16898c2ecf20Sopenharmony_ci return 0; 16908c2ecf20Sopenharmony_ci 16918c2ecf20Sopenharmony_ciput_usb_notifier: 16928c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_PHY) 16938c2ecf20Sopenharmony_ci if (info->nb.notifier_call) { 16948c2ecf20Sopenharmony_ci usb_unregister_notifier(info->usb_phy, &info->nb); 16958c2ecf20Sopenharmony_ci info->nb.notifier_call = NULL; 16968c2ecf20Sopenharmony_ci } 16978c2ecf20Sopenharmony_ci#endif 16988c2ecf20Sopenharmony_ci return ret; 16998c2ecf20Sopenharmony_ci} 17008c2ecf20Sopenharmony_ci 17018c2ecf20Sopenharmony_cistatic int rt9455_remove(struct i2c_client *client) 17028c2ecf20Sopenharmony_ci{ 17038c2ecf20Sopenharmony_ci int ret; 17048c2ecf20Sopenharmony_ci struct rt9455_info *info = i2c_get_clientdata(client); 17058c2ecf20Sopenharmony_ci 17068c2ecf20Sopenharmony_ci ret = rt9455_register_reset(info); 17078c2ecf20Sopenharmony_ci if (ret) 17088c2ecf20Sopenharmony_ci dev_err(&info->client->dev, "Failed to set charger to its default values\n"); 17098c2ecf20Sopenharmony_ci 17108c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_PHY) 17118c2ecf20Sopenharmony_ci if (info->nb.notifier_call) 17128c2ecf20Sopenharmony_ci usb_unregister_notifier(info->usb_phy, &info->nb); 17138c2ecf20Sopenharmony_ci#endif 17148c2ecf20Sopenharmony_ci 17158c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&info->pwr_rdy_work); 17168c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&info->max_charging_time_work); 17178c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&info->batt_presence_work); 17188c2ecf20Sopenharmony_ci 17198c2ecf20Sopenharmony_ci return ret; 17208c2ecf20Sopenharmony_ci} 17218c2ecf20Sopenharmony_ci 17228c2ecf20Sopenharmony_cistatic const struct i2c_device_id rt9455_i2c_id_table[] = { 17238c2ecf20Sopenharmony_ci { RT9455_DRIVER_NAME, 0 }, 17248c2ecf20Sopenharmony_ci { }, 17258c2ecf20Sopenharmony_ci}; 17268c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table); 17278c2ecf20Sopenharmony_ci 17288c2ecf20Sopenharmony_cistatic const struct of_device_id rt9455_of_match[] = { 17298c2ecf20Sopenharmony_ci { .compatible = "richtek,rt9455", }, 17308c2ecf20Sopenharmony_ci { }, 17318c2ecf20Sopenharmony_ci}; 17328c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, rt9455_of_match); 17338c2ecf20Sopenharmony_ci 17348c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI 17358c2ecf20Sopenharmony_cistatic const struct acpi_device_id rt9455_i2c_acpi_match[] = { 17368c2ecf20Sopenharmony_ci { "RT945500", 0 }, 17378c2ecf20Sopenharmony_ci { } 17388c2ecf20Sopenharmony_ci}; 17398c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match); 17408c2ecf20Sopenharmony_ci#endif 17418c2ecf20Sopenharmony_ci 17428c2ecf20Sopenharmony_cistatic struct i2c_driver rt9455_driver = { 17438c2ecf20Sopenharmony_ci .probe = rt9455_probe, 17448c2ecf20Sopenharmony_ci .remove = rt9455_remove, 17458c2ecf20Sopenharmony_ci .id_table = rt9455_i2c_id_table, 17468c2ecf20Sopenharmony_ci .driver = { 17478c2ecf20Sopenharmony_ci .name = RT9455_DRIVER_NAME, 17488c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(rt9455_of_match), 17498c2ecf20Sopenharmony_ci .acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match), 17508c2ecf20Sopenharmony_ci }, 17518c2ecf20Sopenharmony_ci}; 17528c2ecf20Sopenharmony_cimodule_i2c_driver(rt9455_driver); 17538c2ecf20Sopenharmony_ci 17548c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 17558c2ecf20Sopenharmony_ciMODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>"); 17568c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Richtek RT9455 Charger Driver"); 1757