18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// BQ2515X Battery Charger Driver 38c2ecf20Sopenharmony_ci// Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com/ 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <linux/err.h> 68c2ecf20Sopenharmony_ci#include <linux/i2c.h> 78c2ecf20Sopenharmony_ci#include <linux/init.h> 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 118c2ecf20Sopenharmony_ci#include <linux/power_supply.h> 128c2ecf20Sopenharmony_ci#include <linux/regmap.h> 138c2ecf20Sopenharmony_ci#include <linux/types.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define BQ2515X_MANUFACTURER "Texas Instruments" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define BQ2515X_STAT0 0x00 188c2ecf20Sopenharmony_ci#define BQ2515X_STAT1 0x01 198c2ecf20Sopenharmony_ci#define BQ2515X_STAT2 0x02 208c2ecf20Sopenharmony_ci#define BQ2515X_FLAG0 0x03 218c2ecf20Sopenharmony_ci#define BQ2515X_FLAG1 0x04 228c2ecf20Sopenharmony_ci#define BQ2515X_FLAG2 0x05 238c2ecf20Sopenharmony_ci#define BQ2515X_FLAG3 0x06 248c2ecf20Sopenharmony_ci#define BQ2515X_MASK0 0x07 258c2ecf20Sopenharmony_ci#define BQ2515X_MASK1 0x08 268c2ecf20Sopenharmony_ci#define BQ2515X_MASK2 0x09 278c2ecf20Sopenharmony_ci#define BQ2515X_MASK3 0x0a 288c2ecf20Sopenharmony_ci#define BQ2515X_VBAT_CTRL 0x12 298c2ecf20Sopenharmony_ci#define BQ2515X_ICHG_CTRL 0x13 308c2ecf20Sopenharmony_ci#define BQ2515X_PCHRGCTRL 0x14 318c2ecf20Sopenharmony_ci#define BQ2515X_TERMCTRL 0x15 328c2ecf20Sopenharmony_ci#define BQ2515X_BUVLO 0x16 338c2ecf20Sopenharmony_ci#define BQ2515X_CHARGERCTRL0 0x17 348c2ecf20Sopenharmony_ci#define BQ2515X_CHARGERCTRL1 0x18 358c2ecf20Sopenharmony_ci#define BQ2515X_ILIMCTRL 0x19 368c2ecf20Sopenharmony_ci#define BQ2515X_LDOCTRL 0x1d 378c2ecf20Sopenharmony_ci#define BQ2515X_MRCTRL 0x30 388c2ecf20Sopenharmony_ci#define BQ2515X_ICCTRL0 0x35 398c2ecf20Sopenharmony_ci#define BQ2515X_ICCTRL1 0x36 408c2ecf20Sopenharmony_ci#define BQ2515X_ICCTRL2 0x37 418c2ecf20Sopenharmony_ci#define BQ2515X_ADCCTRL0 0x40 428c2ecf20Sopenharmony_ci#define BQ2515X_ADCCTRL1 0x41 438c2ecf20Sopenharmony_ci#define BQ2515X_ADC_VBAT_M 0x42 448c2ecf20Sopenharmony_ci#define BQ2515X_ADC_VBAT_L 0x43 458c2ecf20Sopenharmony_ci#define BQ2515X_ADC_TS_M 0x44 468c2ecf20Sopenharmony_ci#define BQ2515X_ADC_TS_L 0x45 478c2ecf20Sopenharmony_ci#define BQ2515X_ADC_ICHG_M 0x46 488c2ecf20Sopenharmony_ci#define BQ2515X_ADC_ICHG_L 0x47 498c2ecf20Sopenharmony_ci#define BQ2515X_ADC_ADCIN_M 0x48 508c2ecf20Sopenharmony_ci#define BQ2515X_ADC_ADCIN_L 0x49 518c2ecf20Sopenharmony_ci#define BQ2515X_ADC_VIN_M 0x4a 528c2ecf20Sopenharmony_ci#define BQ2515X_ADC_VIN_L 0x4b 538c2ecf20Sopenharmony_ci#define BQ2515X_ADC_PMID_M 0x4c 548c2ecf20Sopenharmony_ci#define BQ2515X_ADC_PMID_L 0x4d 558c2ecf20Sopenharmony_ci#define BQ2515X_ADC_IIN_M 0x4e 568c2ecf20Sopenharmony_ci#define BQ2515X_ADC_IIN_L 0x4f 578c2ecf20Sopenharmony_ci#define BQ2515X_ADC_COMP1_M 0x52 588c2ecf20Sopenharmony_ci#define BQ2515X_ADC_COMP1_L 0X53 598c2ecf20Sopenharmony_ci#define BQ2515X_ADC_COMP2_M 0X54 608c2ecf20Sopenharmony_ci#define BQ2515X_ADC_COMP2_L 0x55 618c2ecf20Sopenharmony_ci#define BQ2515X_ADC_COMP3_M 0x56 628c2ecf20Sopenharmony_ci#define BQ2515X_ADC_COMP3_L 0x57 638c2ecf20Sopenharmony_ci#define BQ2515X_ADC_READ_EN 0x58 648c2ecf20Sopenharmony_ci#define BQ2515X_TS_FASTCHGCTRL 0x61 658c2ecf20Sopenharmony_ci#define BQ2515X_TS_COLD 0x62 668c2ecf20Sopenharmony_ci#define BQ2515X_TS_COOL 0x63 678c2ecf20Sopenharmony_ci#define BQ2515X_TS_WARM 0x64 688c2ecf20Sopenharmony_ci#define BQ2515X_TS_HOT 0x65 698c2ecf20Sopenharmony_ci#define BQ2515X_DEVICE_ID 0x6f 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci#define BQ2515X_DEFAULT_ICHG_UA 10000 728c2ecf20Sopenharmony_ci#define BQ25150_DEFAULT_ILIM_UA 100000 738c2ecf20Sopenharmony_ci#define BQ25155_DEFAULT_ILIM_UA 500000 748c2ecf20Sopenharmony_ci#define BQ2515X_DEFAULT_VBAT_REG_UV 4200000 758c2ecf20Sopenharmony_ci#define BQ2515X_DEFAULT_IPRECHARGE_UA 2500 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci#define BQ2515X_DIVISOR 65536 788c2ecf20Sopenharmony_ci#define BQ2515X_VBAT_BASE_VOLT 3600000 798c2ecf20Sopenharmony_ci#define BQ2515X_VBAT_REG_MAX 4600000 808c2ecf20Sopenharmony_ci#define BQ2515X_VBAT_REG_MIN 3600000 818c2ecf20Sopenharmony_ci#define BQ2515X_VBAT_STEP_UV 10000 828c2ecf20Sopenharmony_ci#define BQ2515X_UV_FACTOR 1000000 838c2ecf20Sopenharmony_ci#define BQ2515X_VBAT_MULTIPLIER 6 848c2ecf20Sopenharmony_ci#define BQ2515X_ICHG_DIVISOR 52429 858c2ecf20Sopenharmony_ci#define BQ2515X_ICHG_CURR_STEP_THRESH_UA 318750 868c2ecf20Sopenharmony_ci#define BQ2515X_ICHG_MIN_UA 0 878c2ecf20Sopenharmony_ci#define BQ2515X_ICHG_MAX_UA 500000 888c2ecf20Sopenharmony_ci#define BQ2515X_ICHG_RNG_1B0_UA 1250 898c2ecf20Sopenharmony_ci#define BQ2515X_ICHG_RNG_1B1_UA 2500 908c2ecf20Sopenharmony_ci#define BQ2515X_VLOWV_SEL_1B0_UV 3000000 918c2ecf20Sopenharmony_ci#define BQ2515X_VLOWV_SEL_1B1_UV 2800000 928c2ecf20Sopenharmony_ci#define BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA 18750 938c2ecf20Sopenharmony_ci#define BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA 37500 948c2ecf20Sopenharmony_ci#define BQ2515X_TWAKE2_MIN_US 1700000 958c2ecf20Sopenharmony_ci#define BQ2515X_TWAKE2_MAX_US 2300000 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci#define BQ2515X_ILIM_150MA 0x2 988c2ecf20Sopenharmony_ci#define BQ2515X_ILIM_MASK 0x7 998c2ecf20Sopenharmony_ci#define BQ2515X_ILIM_MIN 50000 1008c2ecf20Sopenharmony_ci#define BQ2515X_ILIM_MAX 600000 1018c2ecf20Sopenharmony_ci#define BQ2515X_HEALTH_MASK 0xf 1028c2ecf20Sopenharmony_ci#define BQ2515X_ICHGRNG_MASK 0x80 1038c2ecf20Sopenharmony_ci#define BQ2515X_STAT0_MASK 0x0f 1048c2ecf20Sopenharmony_ci#define BQ2515X_STAT1_MASK 0x1f 1058c2ecf20Sopenharmony_ci#define BQ2515X_PRECHARGE_MASK 0x1f 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci#define BQ2515X_TS_HOT_STAT BIT(0) 1088c2ecf20Sopenharmony_ci#define BQ2515X_TS_WARM_STAT BIT(1) 1098c2ecf20Sopenharmony_ci#define BQ2515X_TS_COOL_STAT BIT(2) 1108c2ecf20Sopenharmony_ci#define BQ2515X_TS_COLD_STAT BIT(3) 1118c2ecf20Sopenharmony_ci#define BQ2515X_SAFETY_TIMER_EXP BIT(5) 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci#define BQ2515X_EN_VBAT_READ BIT(3) 1148c2ecf20Sopenharmony_ci#define BQ2515X_EN_ICHG_READ BIT(5) 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci#define BQ2515X_VIN_GOOD BIT(0) 1178c2ecf20Sopenharmony_ci#define BQ2515X_CHRG_DONE BIT(5) 1188c2ecf20Sopenharmony_ci#define BQ2515X_CV_CHRG_MODE BIT(6) 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci#define BQ2515X_VIN_OVP_FAULT_STAT BIT(7) 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci#define BQ2515X_WATCHDOG_DISABLE BIT(4) 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci#define BQ2515X_ICHARGE_RANGE BIT(7) 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci#define BQ2515X_VLOWV_SEL BIT(5) 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci#define BQ2515X_CHARGER_DISABLE BIT(0) 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci#define BQ2515X_HWRESET_14S_WD BIT(1) 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic const int bq2515x_ilim_lvl_values[] = { 1338c2ecf20Sopenharmony_ci 50000, 100000, 150000, 200000, 300000, 400000, 500000, 600000 1348c2ecf20Sopenharmony_ci}; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci/** 1378c2ecf20Sopenharmony_ci * struct bq2515x_init_data - 1388c2ecf20Sopenharmony_ci * @ilim: input current limit 1398c2ecf20Sopenharmony_ci * @ichg: fast charge current 1408c2ecf20Sopenharmony_ci * @vbatreg: battery regulation voltage 1418c2ecf20Sopenharmony_ci * @iprechg: precharge current 1428c2ecf20Sopenharmony_ci */ 1438c2ecf20Sopenharmony_cistruct bq2515x_init_data { 1448c2ecf20Sopenharmony_ci int ilim; 1458c2ecf20Sopenharmony_ci int ichg; 1468c2ecf20Sopenharmony_ci int vbatreg; 1478c2ecf20Sopenharmony_ci int iprechg; 1488c2ecf20Sopenharmony_ci}; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cienum bq2515x_id { 1518c2ecf20Sopenharmony_ci BQ25150, 1528c2ecf20Sopenharmony_ci BQ25155, 1538c2ecf20Sopenharmony_ci}; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci/** 1568c2ecf20Sopenharmony_ci * struct bq2515x_device - 1578c2ecf20Sopenharmony_ci * @mains: mains properties 1588c2ecf20Sopenharmony_ci * @battery: battery properties 1598c2ecf20Sopenharmony_ci * @regmap: register map structure 1608c2ecf20Sopenharmony_ci * @dev: device structure 1618c2ecf20Sopenharmony_ci * 1628c2ecf20Sopenharmony_ci * @reset_gpio: manual reset (MR) pin 1638c2ecf20Sopenharmony_ci * @powerdown_gpio: low power mode pin 1648c2ecf20Sopenharmony_ci * @ac_detect_gpio: power good (PG) pin 1658c2ecf20Sopenharmony_ci * @ce_gpio: charge enable (CE) pin 1668c2ecf20Sopenharmony_ci * 1678c2ecf20Sopenharmony_ci * @model_name: string value describing device model 1688c2ecf20Sopenharmony_ci * @device_id: value of device_id 1698c2ecf20Sopenharmony_ci * @mains_online: boolean value indicating power supply online 1708c2ecf20Sopenharmony_ci * 1718c2ecf20Sopenharmony_ci * @init_data: charger initialization data structure 1728c2ecf20Sopenharmony_ci */ 1738c2ecf20Sopenharmony_cistruct bq2515x_device { 1748c2ecf20Sopenharmony_ci struct power_supply *mains; 1758c2ecf20Sopenharmony_ci struct power_supply *battery; 1768c2ecf20Sopenharmony_ci struct regmap *regmap; 1778c2ecf20Sopenharmony_ci struct device *dev; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci struct gpio_desc *reset_gpio; 1808c2ecf20Sopenharmony_ci struct gpio_desc *powerdown_gpio; 1818c2ecf20Sopenharmony_ci struct gpio_desc *ac_detect_gpio; 1828c2ecf20Sopenharmony_ci struct gpio_desc *ce_gpio; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci char model_name[I2C_NAME_SIZE]; 1858c2ecf20Sopenharmony_ci int device_id; 1868c2ecf20Sopenharmony_ci bool mains_online; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci struct bq2515x_init_data init_data; 1898c2ecf20Sopenharmony_ci}; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cistatic const struct reg_default bq25150_reg_defaults[] = { 1928c2ecf20Sopenharmony_ci {BQ2515X_FLAG0, 0x0}, 1938c2ecf20Sopenharmony_ci {BQ2515X_FLAG1, 0x0}, 1948c2ecf20Sopenharmony_ci {BQ2515X_FLAG2, 0x0}, 1958c2ecf20Sopenharmony_ci {BQ2515X_FLAG3, 0x0}, 1968c2ecf20Sopenharmony_ci {BQ2515X_MASK0, 0x0}, 1978c2ecf20Sopenharmony_ci {BQ2515X_MASK1, 0x0}, 1988c2ecf20Sopenharmony_ci {BQ2515X_MASK2, 0x71}, 1998c2ecf20Sopenharmony_ci {BQ2515X_MASK3, 0x0}, 2008c2ecf20Sopenharmony_ci {BQ2515X_VBAT_CTRL, 0x3C}, 2018c2ecf20Sopenharmony_ci {BQ2515X_ICHG_CTRL, 0x8}, 2028c2ecf20Sopenharmony_ci {BQ2515X_PCHRGCTRL, 0x2}, 2038c2ecf20Sopenharmony_ci {BQ2515X_TERMCTRL, 0x14}, 2048c2ecf20Sopenharmony_ci {BQ2515X_BUVLO, 0x0}, 2058c2ecf20Sopenharmony_ci {BQ2515X_CHARGERCTRL0, 0x82}, 2068c2ecf20Sopenharmony_ci {BQ2515X_CHARGERCTRL1, 0x42}, 2078c2ecf20Sopenharmony_ci {BQ2515X_ILIMCTRL, 0x1}, 2088c2ecf20Sopenharmony_ci {BQ2515X_LDOCTRL, 0xB0}, 2098c2ecf20Sopenharmony_ci {BQ2515X_MRCTRL, 0x2A}, 2108c2ecf20Sopenharmony_ci {BQ2515X_ICCTRL0, 0x10}, 2118c2ecf20Sopenharmony_ci {BQ2515X_ICCTRL1, 0x0}, 2128c2ecf20Sopenharmony_ci {BQ2515X_ICCTRL2, 0x0}, 2138c2ecf20Sopenharmony_ci {BQ2515X_ADCCTRL0, 0x2}, 2148c2ecf20Sopenharmony_ci {BQ2515X_ADCCTRL1, 0x40}, 2158c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP1_M, 0x23}, 2168c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP1_L, 0x20}, 2178c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP2_M, 0x38}, 2188c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP2_L, 0x90}, 2198c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP3_M, 0x0}, 2208c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP3_L, 0x0}, 2218c2ecf20Sopenharmony_ci {BQ2515X_ADC_READ_EN, 0x0}, 2228c2ecf20Sopenharmony_ci {BQ2515X_TS_FASTCHGCTRL, 0x34}, 2238c2ecf20Sopenharmony_ci {BQ2515X_TS_COLD, 0x7C}, 2248c2ecf20Sopenharmony_ci {BQ2515X_TS_COOL, 0x6D}, 2258c2ecf20Sopenharmony_ci {BQ2515X_TS_WARM, 0x38}, 2268c2ecf20Sopenharmony_ci {BQ2515X_TS_HOT, 0x27}, 2278c2ecf20Sopenharmony_ci {BQ2515X_DEVICE_ID, 0x20}, 2288c2ecf20Sopenharmony_ci}; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistatic const struct reg_default bq25155_reg_defaults[] = { 2318c2ecf20Sopenharmony_ci {BQ2515X_FLAG0, 0x0}, 2328c2ecf20Sopenharmony_ci {BQ2515X_FLAG1, 0x0}, 2338c2ecf20Sopenharmony_ci {BQ2515X_FLAG2, 0x0}, 2348c2ecf20Sopenharmony_ci {BQ2515X_FLAG3, 0x0}, 2358c2ecf20Sopenharmony_ci {BQ2515X_MASK0, 0x0}, 2368c2ecf20Sopenharmony_ci {BQ2515X_MASK1, 0x0}, 2378c2ecf20Sopenharmony_ci {BQ2515X_MASK2, 0x71}, 2388c2ecf20Sopenharmony_ci {BQ2515X_MASK3, 0x0}, 2398c2ecf20Sopenharmony_ci {BQ2515X_VBAT_CTRL, 0x3C}, 2408c2ecf20Sopenharmony_ci {BQ2515X_ICHG_CTRL, 0x8}, 2418c2ecf20Sopenharmony_ci {BQ2515X_PCHRGCTRL, 0x2}, 2428c2ecf20Sopenharmony_ci {BQ2515X_TERMCTRL, 0x14}, 2438c2ecf20Sopenharmony_ci {BQ2515X_BUVLO, 0x0}, 2448c2ecf20Sopenharmony_ci {BQ2515X_CHARGERCTRL0, 0x82}, 2458c2ecf20Sopenharmony_ci {BQ2515X_CHARGERCTRL1, 0xC2}, 2468c2ecf20Sopenharmony_ci {BQ2515X_ILIMCTRL, 0x6}, 2478c2ecf20Sopenharmony_ci {BQ2515X_LDOCTRL, 0xB0}, 2488c2ecf20Sopenharmony_ci {BQ2515X_MRCTRL, 0x2A}, 2498c2ecf20Sopenharmony_ci {BQ2515X_ICCTRL0, 0x10}, 2508c2ecf20Sopenharmony_ci {BQ2515X_ICCTRL1, 0x0}, 2518c2ecf20Sopenharmony_ci {BQ2515X_ICCTRL2, 0x40}, 2528c2ecf20Sopenharmony_ci {BQ2515X_ADCCTRL0, 0x2}, 2538c2ecf20Sopenharmony_ci {BQ2515X_ADCCTRL1, 0x40}, 2548c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP1_M, 0x23}, 2558c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP1_L, 0x20}, 2568c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP2_M, 0x38}, 2578c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP2_L, 0x90}, 2588c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP3_M, 0x0}, 2598c2ecf20Sopenharmony_ci {BQ2515X_ADC_COMP3_L, 0x0}, 2608c2ecf20Sopenharmony_ci {BQ2515X_ADC_READ_EN, 0x0}, 2618c2ecf20Sopenharmony_ci {BQ2515X_TS_FASTCHGCTRL, 0x34}, 2628c2ecf20Sopenharmony_ci {BQ2515X_TS_COLD, 0x7C}, 2638c2ecf20Sopenharmony_ci {BQ2515X_TS_COOL, 0x6D}, 2648c2ecf20Sopenharmony_ci {BQ2515X_TS_WARM, 0x38}, 2658c2ecf20Sopenharmony_ci {BQ2515X_TS_HOT, 0x27}, 2668c2ecf20Sopenharmony_ci {BQ2515X_DEVICE_ID, 0x35}, 2678c2ecf20Sopenharmony_ci}; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic int bq2515x_wake_up(struct bq2515x_device *bq2515x) 2708c2ecf20Sopenharmony_ci{ 2718c2ecf20Sopenharmony_ci int ret; 2728c2ecf20Sopenharmony_ci int val; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci /* Read the STAT register if we can read it then the device is out 2758c2ecf20Sopenharmony_ci * of ship mode. If the register cannot be read then attempt to wake 2768c2ecf20Sopenharmony_ci * it up and enable the ADC. 2778c2ecf20Sopenharmony_ci */ 2788c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &val); 2798c2ecf20Sopenharmony_ci if (ret) 2808c2ecf20Sopenharmony_ci return ret; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci /* Need to toggle LP and bring device out of ship mode. The device 2838c2ecf20Sopenharmony_ci * will exit the ship mode when the MR pin is held low for at least 2848c2ecf20Sopenharmony_ci * t_WAKE2 as shown in section 8.3.7.1 of the datasheet. 2858c2ecf20Sopenharmony_ci */ 2868c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(bq2515x->powerdown_gpio, 0); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(bq2515x->reset_gpio, 0); 2898c2ecf20Sopenharmony_ci usleep_range(BQ2515X_TWAKE2_MIN_US, BQ2515X_TWAKE2_MAX_US); 2908c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(bq2515x->reset_gpio, 1); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci return regmap_write(bq2515x->regmap, BQ2515X_ADC_READ_EN, 2938c2ecf20Sopenharmony_ci (BQ2515X_EN_VBAT_READ | BQ2515X_EN_ICHG_READ)); 2948c2ecf20Sopenharmony_ci} 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_cistatic int bq2515x_update_ps_status(struct bq2515x_device *bq2515x) 2978c2ecf20Sopenharmony_ci{ 2988c2ecf20Sopenharmony_ci bool dc = false; 2998c2ecf20Sopenharmony_ci unsigned int val; 3008c2ecf20Sopenharmony_ci int ret; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci if (bq2515x->ac_detect_gpio) 3038c2ecf20Sopenharmony_ci val = gpiod_get_value_cansleep(bq2515x->ac_detect_gpio); 3048c2ecf20Sopenharmony_ci else { 3058c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &val); 3068c2ecf20Sopenharmony_ci if (ret) 3078c2ecf20Sopenharmony_ci return ret; 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci dc = val & BQ2515X_VIN_GOOD; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci ret = bq2515x->mains_online != dc; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci bq2515x->mains_online = dc; 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci return ret; 3178c2ecf20Sopenharmony_ci} 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_cistatic int bq2515x_disable_watchdog_timers(struct bq2515x_device *bq2515x) 3208c2ecf20Sopenharmony_ci{ 3218c2ecf20Sopenharmony_ci int ret; 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci ret = regmap_update_bits(bq2515x->regmap, BQ2515X_CHARGERCTRL0, 3248c2ecf20Sopenharmony_ci BQ2515X_WATCHDOG_DISABLE, BQ2515X_WATCHDOG_DISABLE); 3258c2ecf20Sopenharmony_ci if (ret) 3268c2ecf20Sopenharmony_ci return ret; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci return regmap_update_bits(bq2515x->regmap, BQ2515X_ICCTRL2, 3298c2ecf20Sopenharmony_ci BQ2515X_HWRESET_14S_WD, 0); 3308c2ecf20Sopenharmony_ci} 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_cistatic int bq2515x_get_battery_voltage_now(struct bq2515x_device *bq2515x) 3338c2ecf20Sopenharmony_ci{ 3348c2ecf20Sopenharmony_ci int ret; 3358c2ecf20Sopenharmony_ci int vbat_msb; 3368c2ecf20Sopenharmony_ci int vbat_lsb; 3378c2ecf20Sopenharmony_ci uint32_t vbat_measurement; 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci if (!bq2515x->mains_online) 3408c2ecf20Sopenharmony_ci bq2515x_wake_up(bq2515x); 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_VBAT_M, &vbat_msb); 3438c2ecf20Sopenharmony_ci if (ret) 3448c2ecf20Sopenharmony_ci return ret; 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_VBAT_L, &vbat_lsb); 3478c2ecf20Sopenharmony_ci if (ret) 3488c2ecf20Sopenharmony_ci return ret; 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci vbat_measurement = (vbat_msb << 8) | vbat_lsb; 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci return vbat_measurement * (BQ2515X_UV_FACTOR / BQ2515X_DIVISOR) * 3538c2ecf20Sopenharmony_ci BQ2515X_VBAT_MULTIPLIER; 3548c2ecf20Sopenharmony_ci} 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_cistatic int bq2515x_get_battery_current_now(struct bq2515x_device *bq2515x) 3578c2ecf20Sopenharmony_ci{ 3588c2ecf20Sopenharmony_ci int ret; 3598c2ecf20Sopenharmony_ci int ichg_msb; 3608c2ecf20Sopenharmony_ci int ichg_lsb; 3618c2ecf20Sopenharmony_ci uint32_t ichg_measurement; 3628c2ecf20Sopenharmony_ci u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA; 3638c2ecf20Sopenharmony_ci unsigned int ichg_reg_code, reg_code; 3648c2ecf20Sopenharmony_ci unsigned int icharge_range = 0, pchrgctrl; 3658c2ecf20Sopenharmony_ci unsigned int buvlo, vlowv_sel, vlowv = BQ2515X_VLOWV_SEL_1B0_UV; 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci if (!bq2515x->mains_online) 3688c2ecf20Sopenharmony_ci return -ENODATA; 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_ICHG_M, &ichg_msb); 3718c2ecf20Sopenharmony_ci if (ret) 3728c2ecf20Sopenharmony_ci return ret; 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_ICHG_L, &ichg_lsb); 3758c2ecf20Sopenharmony_ci if (ret) 3768c2ecf20Sopenharmony_ci return ret; 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci ichg_measurement = (ichg_msb << 8) | ichg_lsb; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_BUVLO, &buvlo); 3818c2ecf20Sopenharmony_ci if (ret) 3828c2ecf20Sopenharmony_ci return ret; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci vlowv_sel = buvlo & BQ2515X_VLOWV_SEL; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci if (vlowv_sel) 3878c2ecf20Sopenharmony_ci vlowv = BQ2515X_VLOWV_SEL_1B1_UV; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci if (bq2515x_get_battery_voltage_now(bq2515x) < vlowv) { 3908c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, 3918c2ecf20Sopenharmony_ci &pchrgctrl); 3928c2ecf20Sopenharmony_ci if (ret) 3938c2ecf20Sopenharmony_ci return ret; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci reg_code = pchrgctrl & BQ2515X_PRECHARGE_MASK; 3968c2ecf20Sopenharmony_ci } else { 3978c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_ICHG_CTRL, 3988c2ecf20Sopenharmony_ci &ichg_reg_code); 3998c2ecf20Sopenharmony_ci if (ret) 4008c2ecf20Sopenharmony_ci return ret; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci reg_code = ichg_reg_code; 4038c2ecf20Sopenharmony_ci } 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl); 4068c2ecf20Sopenharmony_ci if (ret) 4078c2ecf20Sopenharmony_ci return ret; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci if (icharge_range) 4128c2ecf20Sopenharmony_ci ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci return reg_code * (ichg_multiplier * ichg_measurement / 4158c2ecf20Sopenharmony_ci BQ2515X_ICHG_DIVISOR); 4168c2ecf20Sopenharmony_ci} 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_cistatic bool bq2515x_get_charge_disable(struct bq2515x_device *bq2515x) 4198c2ecf20Sopenharmony_ci{ 4208c2ecf20Sopenharmony_ci int ret; 4218c2ecf20Sopenharmony_ci int ce_pin; 4228c2ecf20Sopenharmony_ci int icctrl2; 4238c2ecf20Sopenharmony_ci int charger_disable; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci ce_pin = gpiod_get_value_cansleep(bq2515x->ce_gpio); 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_ICCTRL2, &icctrl2); 4288c2ecf20Sopenharmony_ci if (ret) 4298c2ecf20Sopenharmony_ci return ret; 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci charger_disable = icctrl2 & BQ2515X_CHARGER_DISABLE; 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci if (charger_disable || ce_pin) 4348c2ecf20Sopenharmony_ci return true; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci return false; 4378c2ecf20Sopenharmony_ci} 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_cistatic int bq2515x_set_charge_disable(struct bq2515x_device *bq2515x, int val) 4408c2ecf20Sopenharmony_ci{ 4418c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(bq2515x->ce_gpio, val); 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ci return regmap_update_bits(bq2515x->regmap, BQ2515X_ICCTRL2, 4448c2ecf20Sopenharmony_ci BQ2515X_CHARGER_DISABLE, val); 4458c2ecf20Sopenharmony_ci} 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_cistatic int bq2515x_get_const_charge_current(struct bq2515x_device *bq2515x) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci int ret; 4508c2ecf20Sopenharmony_ci u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA; 4518c2ecf20Sopenharmony_ci unsigned int ichg_reg_code; 4528c2ecf20Sopenharmony_ci unsigned int pchrgctrl; 4538c2ecf20Sopenharmony_ci unsigned int icharge_range; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_ICHG_CTRL, &ichg_reg_code); 4568c2ecf20Sopenharmony_ci if (ret) 4578c2ecf20Sopenharmony_ci return ret; 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl); 4608c2ecf20Sopenharmony_ci if (ret) 4618c2ecf20Sopenharmony_ci return ret; 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci if (icharge_range) 4668c2ecf20Sopenharmony_ci ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci return ichg_reg_code * ichg_multiplier; 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_cistatic int bq2515x_set_const_charge_current(struct bq2515x_device *bq2515x, 4728c2ecf20Sopenharmony_ci int val) 4738c2ecf20Sopenharmony_ci{ 4748c2ecf20Sopenharmony_ci int ret; 4758c2ecf20Sopenharmony_ci unsigned int ichg_reg_code; 4768c2ecf20Sopenharmony_ci u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA; 4778c2ecf20Sopenharmony_ci unsigned int icharge_range = 0; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci if (val > BQ2515X_ICHG_MAX_UA || val < BQ2515X_ICHG_MIN_UA) 4808c2ecf20Sopenharmony_ci return -EINVAL; 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci if (val > BQ2515X_ICHG_CURR_STEP_THRESH_UA) { 4838c2ecf20Sopenharmony_ci ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA; 4848c2ecf20Sopenharmony_ci icharge_range = BQ2515X_ICHARGE_RANGE; 4858c2ecf20Sopenharmony_ci } 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci bq2515x_set_charge_disable(bq2515x, 1); 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci ret = regmap_update_bits(bq2515x->regmap, BQ2515X_PCHRGCTRL, 4908c2ecf20Sopenharmony_ci BQ2515X_ICHARGE_RANGE, icharge_range); 4918c2ecf20Sopenharmony_ci if (ret) 4928c2ecf20Sopenharmony_ci return ret; 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci ichg_reg_code = val / ichg_multiplier; 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci ret = regmap_write(bq2515x->regmap, BQ2515X_ICHG_CTRL, ichg_reg_code); 4978c2ecf20Sopenharmony_ci if (ret) 4988c2ecf20Sopenharmony_ci return ret; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci return bq2515x_set_charge_disable(bq2515x, 0); 5018c2ecf20Sopenharmony_ci} 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_cistatic int bq2515x_get_precharge_current(struct bq2515x_device *bq2515x) 5048c2ecf20Sopenharmony_ci{ 5058c2ecf20Sopenharmony_ci int ret; 5068c2ecf20Sopenharmony_ci unsigned int pchrgctrl; 5078c2ecf20Sopenharmony_ci unsigned int icharge_range; 5088c2ecf20Sopenharmony_ci u16 precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA; 5098c2ecf20Sopenharmony_ci unsigned int precharge_reg_code; 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl); 5128c2ecf20Sopenharmony_ci if (ret) 5138c2ecf20Sopenharmony_ci return ret; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci if (icharge_range) 5188c2ecf20Sopenharmony_ci precharge_multiplier = BQ2515X_ICHG_RNG_1B1_UA; 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci precharge_reg_code = pchrgctrl & BQ2515X_PRECHARGE_MASK; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci return precharge_reg_code * precharge_multiplier; 5238c2ecf20Sopenharmony_ci} 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_cistatic int bq2515x_set_precharge_current(struct bq2515x_device *bq2515x, 5268c2ecf20Sopenharmony_ci int val) 5278c2ecf20Sopenharmony_ci{ 5288c2ecf20Sopenharmony_ci int ret; 5298c2ecf20Sopenharmony_ci unsigned int pchrgctrl; 5308c2ecf20Sopenharmony_ci unsigned int icharge_range; 5318c2ecf20Sopenharmony_ci unsigned int precharge_reg_code; 5328c2ecf20Sopenharmony_ci unsigned int precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA; 5338c2ecf20Sopenharmony_ci unsigned int precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA; 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl); 5368c2ecf20Sopenharmony_ci if (ret) 5378c2ecf20Sopenharmony_ci return ret; 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE; 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_ci if (icharge_range) { 5428c2ecf20Sopenharmony_ci precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA; 5438c2ecf20Sopenharmony_ci precharge_multiplier = BQ2515X_ICHG_RNG_1B1_UA; 5448c2ecf20Sopenharmony_ci } else { 5458c2ecf20Sopenharmony_ci precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA; 5468c2ecf20Sopenharmony_ci precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA; 5478c2ecf20Sopenharmony_ci } 5488c2ecf20Sopenharmony_ci if (val > precharge_max_ua || val < BQ2515X_ICHG_MIN_UA) 5498c2ecf20Sopenharmony_ci return -EINVAL; 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci precharge_reg_code = val / precharge_multiplier; 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci ret = bq2515x_set_charge_disable(bq2515x, 1); 5548c2ecf20Sopenharmony_ci if (ret) 5558c2ecf20Sopenharmony_ci return ret; 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_ci ret = regmap_update_bits(bq2515x->regmap, BQ2515X_PCHRGCTRL, 5588c2ecf20Sopenharmony_ci BQ2515X_PRECHARGE_MASK, precharge_reg_code); 5598c2ecf20Sopenharmony_ci if (ret) 5608c2ecf20Sopenharmony_ci return ret; 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci return bq2515x_set_charge_disable(bq2515x, 0); 5638c2ecf20Sopenharmony_ci} 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_cistatic int bq2515x_charging_status(struct bq2515x_device *bq2515x, 5668c2ecf20Sopenharmony_ci union power_supply_propval *val) 5678c2ecf20Sopenharmony_ci{ 5688c2ecf20Sopenharmony_ci bool status0_no_fault; 5698c2ecf20Sopenharmony_ci bool status1_no_fault; 5708c2ecf20Sopenharmony_ci bool ce_status; 5718c2ecf20Sopenharmony_ci bool charge_done; 5728c2ecf20Sopenharmony_ci unsigned int status; 5738c2ecf20Sopenharmony_ci int ret; 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci if (!bq2515x->mains_online) { 5768c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 5778c2ecf20Sopenharmony_ci return 0; 5788c2ecf20Sopenharmony_ci } 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &status); 5818c2ecf20Sopenharmony_ci if (ret) 5828c2ecf20Sopenharmony_ci return ret; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci /* 5858c2ecf20Sopenharmony_ci * The code block below is used to determine if any faults from the 5868c2ecf20Sopenharmony_ci * STAT0 register are disbaling charging or if the charge has completed 5878c2ecf20Sopenharmony_ci * according to the CHARGE_DONE_STAT bit. 5888c2ecf20Sopenharmony_ci */ 5898c2ecf20Sopenharmony_ci if (((status & BQ2515X_STAT0_MASK) == true) & 5908c2ecf20Sopenharmony_ci ((status & BQ2515X_CHRG_DONE) == false)) { 5918c2ecf20Sopenharmony_ci status0_no_fault = true; 5928c2ecf20Sopenharmony_ci charge_done = false; 5938c2ecf20Sopenharmony_ci } else if (status & BQ2515X_CHRG_DONE) { 5948c2ecf20Sopenharmony_ci charge_done = true; 5958c2ecf20Sopenharmony_ci status0_no_fault = false; 5968c2ecf20Sopenharmony_ci } else { 5978c2ecf20Sopenharmony_ci status0_no_fault = false; 5988c2ecf20Sopenharmony_ci charge_done = false; 5998c2ecf20Sopenharmony_ci } 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_STAT1, &status); 6028c2ecf20Sopenharmony_ci if (ret) 6038c2ecf20Sopenharmony_ci return ret; 6048c2ecf20Sopenharmony_ci /* 6058c2ecf20Sopenharmony_ci * The code block below is used to determine if any faults from the 6068c2ecf20Sopenharmony_ci * STAT1 register are disbaling charging 6078c2ecf20Sopenharmony_ci */ 6088c2ecf20Sopenharmony_ci if ((status & BQ2515X_STAT1_MASK) == false) 6098c2ecf20Sopenharmony_ci status1_no_fault = true; 6108c2ecf20Sopenharmony_ci else 6118c2ecf20Sopenharmony_ci status1_no_fault = false; 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci ce_status = (!bq2515x_get_charge_disable(bq2515x)); 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci /* 6168c2ecf20Sopenharmony_ci * If there are no faults and charging is enabled, then status is 6178c2ecf20Sopenharmony_ci * charging. Otherwise, if charging is complete, then status is full. 6188c2ecf20Sopenharmony_ci * Otherwise, if a fault exists or charging is disabled, then status is 6198c2ecf20Sopenharmony_ci * not charging 6208c2ecf20Sopenharmony_ci */ 6218c2ecf20Sopenharmony_ci if (status0_no_fault & status1_no_fault & ce_status) 6228c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_CHARGING; 6238c2ecf20Sopenharmony_ci else if (charge_done) 6248c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_FULL; 6258c2ecf20Sopenharmony_ci else if (!(status0_no_fault & status1_no_fault & ce_status)) 6268c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci return 0; 6298c2ecf20Sopenharmony_ci} 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_cistatic int bq2515x_get_batt_reg(struct bq2515x_device *bq2515x) 6328c2ecf20Sopenharmony_ci{ 6338c2ecf20Sopenharmony_ci int vbat_reg_code; 6348c2ecf20Sopenharmony_ci int ret; 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_VBAT_CTRL, &vbat_reg_code); 6378c2ecf20Sopenharmony_ci if (ret) 6388c2ecf20Sopenharmony_ci return ret; 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci return BQ2515X_VBAT_BASE_VOLT + vbat_reg_code * BQ2515X_VBAT_STEP_UV; 6418c2ecf20Sopenharmony_ci} 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_cistatic int bq2515x_set_batt_reg(struct bq2515x_device *bq2515x, int val) 6448c2ecf20Sopenharmony_ci{ 6458c2ecf20Sopenharmony_ci int vbat_reg_code; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci if (val > BQ2515X_VBAT_REG_MAX || val < BQ2515X_VBAT_REG_MIN) 6488c2ecf20Sopenharmony_ci return -EINVAL; 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci vbat_reg_code = (val - BQ2515X_VBAT_BASE_VOLT) / BQ2515X_VBAT_STEP_UV; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci return regmap_write(bq2515x->regmap, BQ2515X_VBAT_CTRL, vbat_reg_code); 6538c2ecf20Sopenharmony_ci} 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_cistatic int bq2515x_get_ilim_lvl(struct bq2515x_device *bq2515x) 6568c2ecf20Sopenharmony_ci{ 6578c2ecf20Sopenharmony_ci int ret; 6588c2ecf20Sopenharmony_ci int ilimctrl; 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_ILIMCTRL, &ilimctrl); 6618c2ecf20Sopenharmony_ci if (ret) 6628c2ecf20Sopenharmony_ci return ret; 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci return bq2515x_ilim_lvl_values[ilimctrl & BQ2515X_ILIM_MASK]; 6658c2ecf20Sopenharmony_ci} 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_cistatic int bq2515x_set_ilim_lvl(struct bq2515x_device *bq2515x, int val) 6688c2ecf20Sopenharmony_ci{ 6698c2ecf20Sopenharmony_ci int i = 0; 6708c2ecf20Sopenharmony_ci unsigned int array_size = ARRAY_SIZE(bq2515x_ilim_lvl_values); 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci for (i = array_size - 1; i > 0; i--) { 6738c2ecf20Sopenharmony_ci if (val >= bq2515x_ilim_lvl_values[i]) 6748c2ecf20Sopenharmony_ci break; 6758c2ecf20Sopenharmony_ci } 6768c2ecf20Sopenharmony_ci return regmap_write(bq2515x->regmap, BQ2515X_ILIMCTRL, i); 6778c2ecf20Sopenharmony_ci} 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_cistatic int bq2515x_power_supply_property_is_writeable(struct power_supply *psy, 6808c2ecf20Sopenharmony_ci enum power_supply_property prop) 6818c2ecf20Sopenharmony_ci{ 6828c2ecf20Sopenharmony_ci switch (prop) { 6838c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 6848c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 6858c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 6868c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 6878c2ecf20Sopenharmony_ci return true; 6888c2ecf20Sopenharmony_ci default: 6898c2ecf20Sopenharmony_ci return false; 6908c2ecf20Sopenharmony_ci } 6918c2ecf20Sopenharmony_ci} 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_cistatic int bq2515x_charger_get_health(struct bq2515x_device *bq2515x, 6948c2ecf20Sopenharmony_ci union power_supply_propval *val) 6958c2ecf20Sopenharmony_ci{ 6968c2ecf20Sopenharmony_ci int health = POWER_SUPPLY_HEALTH_GOOD; 6978c2ecf20Sopenharmony_ci int ret; 6988c2ecf20Sopenharmony_ci unsigned int stat1; 6998c2ecf20Sopenharmony_ci unsigned int flag3; 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_ci if (!bq2515x->mains_online) 7028c2ecf20Sopenharmony_ci bq2515x_wake_up(bq2515x); 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_FLAG3, &flag3); 7058c2ecf20Sopenharmony_ci if (ret) 7068c2ecf20Sopenharmony_ci return ret; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci ret = regmap_read(bq2515x->regmap, BQ2515X_STAT1, &stat1); 7098c2ecf20Sopenharmony_ci if (ret) 7108c2ecf20Sopenharmony_ci return ret; 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci if (stat1 & BQ2515X_HEALTH_MASK) { 7138c2ecf20Sopenharmony_ci switch (stat1 & BQ2515X_HEALTH_MASK) { 7148c2ecf20Sopenharmony_ci case BQ2515X_TS_HOT_STAT: 7158c2ecf20Sopenharmony_ci health = POWER_SUPPLY_HEALTH_HOT; 7168c2ecf20Sopenharmony_ci break; 7178c2ecf20Sopenharmony_ci case BQ2515X_TS_WARM_STAT: 7188c2ecf20Sopenharmony_ci health = POWER_SUPPLY_HEALTH_WARM; 7198c2ecf20Sopenharmony_ci break; 7208c2ecf20Sopenharmony_ci case BQ2515X_TS_COOL_STAT: 7218c2ecf20Sopenharmony_ci health = POWER_SUPPLY_HEALTH_COOL; 7228c2ecf20Sopenharmony_ci break; 7238c2ecf20Sopenharmony_ci case BQ2515X_TS_COLD_STAT: 7248c2ecf20Sopenharmony_ci health = POWER_SUPPLY_HEALTH_COLD; 7258c2ecf20Sopenharmony_ci break; 7268c2ecf20Sopenharmony_ci default: 7278c2ecf20Sopenharmony_ci health = POWER_SUPPLY_HEALTH_UNKNOWN; 7288c2ecf20Sopenharmony_ci break; 7298c2ecf20Sopenharmony_ci } 7308c2ecf20Sopenharmony_ci } 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci if (stat1 & BQ2515X_VIN_OVP_FAULT_STAT) 7338c2ecf20Sopenharmony_ci health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_ci if (flag3 & BQ2515X_SAFETY_TIMER_EXP) 7368c2ecf20Sopenharmony_ci health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_ci val->intval = health; 7398c2ecf20Sopenharmony_ci return 0; 7408c2ecf20Sopenharmony_ci} 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_cistatic int bq2515x_mains_set_property(struct power_supply *psy, 7438c2ecf20Sopenharmony_ci enum power_supply_property prop, 7448c2ecf20Sopenharmony_ci const union power_supply_propval *val) 7458c2ecf20Sopenharmony_ci{ 7468c2ecf20Sopenharmony_ci struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy); 7478c2ecf20Sopenharmony_ci int ret; 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci switch (prop) { 7508c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 7518c2ecf20Sopenharmony_ci ret = bq2515x_set_batt_reg(bq2515x, val->intval); 7528c2ecf20Sopenharmony_ci break; 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 7558c2ecf20Sopenharmony_ci ret = bq2515x_set_const_charge_current(bq2515x, val->intval); 7568c2ecf20Sopenharmony_ci break; 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 7598c2ecf20Sopenharmony_ci ret = bq2515x_set_ilim_lvl(bq2515x, val->intval); 7608c2ecf20Sopenharmony_ci break; 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 7638c2ecf20Sopenharmony_ci ret = bq2515x_set_precharge_current(bq2515x, val->intval); 7648c2ecf20Sopenharmony_ci break; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci default: 7678c2ecf20Sopenharmony_ci return -EINVAL; 7688c2ecf20Sopenharmony_ci } 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci return ret; 7718c2ecf20Sopenharmony_ci} 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_cistatic int bq2515x_mains_get_property(struct power_supply *psy, 7748c2ecf20Sopenharmony_ci enum power_supply_property prop, 7758c2ecf20Sopenharmony_ci union power_supply_propval *val) 7768c2ecf20Sopenharmony_ci{ 7778c2ecf20Sopenharmony_ci struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy); 7788c2ecf20Sopenharmony_ci int ret = 0; 7798c2ecf20Sopenharmony_ci 7808c2ecf20Sopenharmony_ci switch (prop) { 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 7838c2ecf20Sopenharmony_ci ret = bq2515x_get_const_charge_current(bq2515x); 7848c2ecf20Sopenharmony_ci if (ret < 0) 7858c2ecf20Sopenharmony_ci return ret; 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ci val->intval = ret; 7888c2ecf20Sopenharmony_ci break; 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 7918c2ecf20Sopenharmony_ci ret = bq2515x_get_batt_reg(bq2515x); 7928c2ecf20Sopenharmony_ci if (ret < 0) 7938c2ecf20Sopenharmony_ci return ret; 7948c2ecf20Sopenharmony_ci val->intval = ret; 7958c2ecf20Sopenharmony_ci break; 7968c2ecf20Sopenharmony_ci 7978c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 7988c2ecf20Sopenharmony_ci ret = bq2515x_get_precharge_current(bq2515x); 7998c2ecf20Sopenharmony_ci if (ret < 0) 8008c2ecf20Sopenharmony_ci return ret; 8018c2ecf20Sopenharmony_ci val->intval = ret; 8028c2ecf20Sopenharmony_ci break; 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ONLINE: 8058c2ecf20Sopenharmony_ci val->intval = bq2515x->mains_online; 8068c2ecf20Sopenharmony_ci break; 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_HEALTH: 8098c2ecf20Sopenharmony_ci ret = bq2515x_charger_get_health(bq2515x, val); 8108c2ecf20Sopenharmony_ci if (ret) 8118c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; 8128c2ecf20Sopenharmony_ci break; 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 8158c2ecf20Sopenharmony_ci ret = bq2515x_get_ilim_lvl(bq2515x); 8168c2ecf20Sopenharmony_ci if (ret < 0) 8178c2ecf20Sopenharmony_ci return ret; 8188c2ecf20Sopenharmony_ci val->intval = ret; 8198c2ecf20Sopenharmony_ci break; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MODEL_NAME: 8228c2ecf20Sopenharmony_ci val->strval = bq2515x->model_name; 8238c2ecf20Sopenharmony_ci break; 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURER: 8268c2ecf20Sopenharmony_ci val->strval = BQ2515X_MANUFACTURER; 8278c2ecf20Sopenharmony_ci break; 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_STATUS: 8308c2ecf20Sopenharmony_ci ret = bq2515x_charging_status(bq2515x, val); 8318c2ecf20Sopenharmony_ci if (ret) 8328c2ecf20Sopenharmony_ci return ret; 8338c2ecf20Sopenharmony_ci break; 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_ci default: 8368c2ecf20Sopenharmony_ci return -EINVAL; 8378c2ecf20Sopenharmony_ci } 8388c2ecf20Sopenharmony_ci 8398c2ecf20Sopenharmony_ci return ret; 8408c2ecf20Sopenharmony_ci} 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_cistatic int bq2515x_battery_get_property(struct power_supply *psy, 8438c2ecf20Sopenharmony_ci enum power_supply_property prop, 8448c2ecf20Sopenharmony_ci union power_supply_propval *val) 8458c2ecf20Sopenharmony_ci{ 8468c2ecf20Sopenharmony_ci struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy); 8478c2ecf20Sopenharmony_ci int ret; 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci ret = bq2515x_update_ps_status(bq2515x); 8508c2ecf20Sopenharmony_ci if (ret) 8518c2ecf20Sopenharmony_ci return ret; 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci switch (prop) { 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 8568c2ecf20Sopenharmony_ci ret = bq2515x->init_data.vbatreg; 8578c2ecf20Sopenharmony_ci if (ret < 0) 8588c2ecf20Sopenharmony_ci return ret; 8598c2ecf20Sopenharmony_ci val->intval = ret; 8608c2ecf20Sopenharmony_ci break; 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 8638c2ecf20Sopenharmony_ci ret = bq2515x->init_data.ichg; 8648c2ecf20Sopenharmony_ci if (ret < 0) 8658c2ecf20Sopenharmony_ci return ret; 8668c2ecf20Sopenharmony_ci val->intval = ret; 8678c2ecf20Sopenharmony_ci break; 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_NOW: 8708c2ecf20Sopenharmony_ci ret = bq2515x_get_battery_voltage_now(bq2515x); 8718c2ecf20Sopenharmony_ci if (ret < 0) 8728c2ecf20Sopenharmony_ci return ret; 8738c2ecf20Sopenharmony_ci val->intval = ret; 8748c2ecf20Sopenharmony_ci break; 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_NOW: 8778c2ecf20Sopenharmony_ci ret = bq2515x_get_battery_current_now(bq2515x); 8788c2ecf20Sopenharmony_ci if (ret < 0) 8798c2ecf20Sopenharmony_ci return ret; 8808c2ecf20Sopenharmony_ci val->intval = ret; 8818c2ecf20Sopenharmony_ci break; 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ci default: 8848c2ecf20Sopenharmony_ci return -EINVAL; 8858c2ecf20Sopenharmony_ci } 8868c2ecf20Sopenharmony_ci return 0; 8878c2ecf20Sopenharmony_ci} 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_cistatic const enum power_supply_property bq2515x_battery_properties[] = { 8908c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_NOW, 8918c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_NOW, 8928c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 8938c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 8948c2ecf20Sopenharmony_ci}; 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_cistatic const enum power_supply_property bq2515x_mains_properties[] = { 8978c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_ONLINE, 8988c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_STATUS, 8998c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_HEALTH, 9008c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 9018c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MODEL_NAME, 9028c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MANUFACTURER, 9038c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 9048c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 9058c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_PRECHARGE_CURRENT, 9068c2ecf20Sopenharmony_ci}; 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_cistatic const struct power_supply_desc bq2515x_mains_desc = { 9098c2ecf20Sopenharmony_ci .name = "bq2515x-mains", 9108c2ecf20Sopenharmony_ci .type = POWER_SUPPLY_TYPE_MAINS, 9118c2ecf20Sopenharmony_ci .get_property = bq2515x_mains_get_property, 9128c2ecf20Sopenharmony_ci .set_property = bq2515x_mains_set_property, 9138c2ecf20Sopenharmony_ci .properties = bq2515x_mains_properties, 9148c2ecf20Sopenharmony_ci .num_properties = ARRAY_SIZE(bq2515x_mains_properties), 9158c2ecf20Sopenharmony_ci .property_is_writeable = bq2515x_power_supply_property_is_writeable, 9168c2ecf20Sopenharmony_ci}; 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_cistatic const struct power_supply_desc bq2515x_battery_desc = { 9198c2ecf20Sopenharmony_ci .name = "bq2515x-battery", 9208c2ecf20Sopenharmony_ci .type = POWER_SUPPLY_TYPE_BATTERY, 9218c2ecf20Sopenharmony_ci .get_property = bq2515x_battery_get_property, 9228c2ecf20Sopenharmony_ci .properties = bq2515x_battery_properties, 9238c2ecf20Sopenharmony_ci .num_properties = ARRAY_SIZE(bq2515x_battery_properties), 9248c2ecf20Sopenharmony_ci .property_is_writeable = bq2515x_power_supply_property_is_writeable, 9258c2ecf20Sopenharmony_ci}; 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_cistatic int bq2515x_power_supply_register(struct bq2515x_device *bq2515x, 9288c2ecf20Sopenharmony_ci struct device *dev, struct power_supply_config psy_cfg) 9298c2ecf20Sopenharmony_ci{ 9308c2ecf20Sopenharmony_ci bq2515x->mains = devm_power_supply_register(bq2515x->dev, 9318c2ecf20Sopenharmony_ci &bq2515x_mains_desc, 9328c2ecf20Sopenharmony_ci &psy_cfg); 9338c2ecf20Sopenharmony_ci if (IS_ERR(bq2515x->mains)) 9348c2ecf20Sopenharmony_ci return -EINVAL; 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci bq2515x->battery = devm_power_supply_register(bq2515x->dev, 9378c2ecf20Sopenharmony_ci &bq2515x_battery_desc, 9388c2ecf20Sopenharmony_ci &psy_cfg); 9398c2ecf20Sopenharmony_ci if (IS_ERR(bq2515x->battery)) 9408c2ecf20Sopenharmony_ci return -EINVAL; 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_ci return 0; 9438c2ecf20Sopenharmony_ci} 9448c2ecf20Sopenharmony_ci 9458c2ecf20Sopenharmony_cistatic int bq2515x_hw_init(struct bq2515x_device *bq2515x) 9468c2ecf20Sopenharmony_ci{ 9478c2ecf20Sopenharmony_ci int ret; 9488c2ecf20Sopenharmony_ci struct power_supply_battery_info bat_info = { }; 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci ret = bq2515x_disable_watchdog_timers(bq2515x); 9518c2ecf20Sopenharmony_ci if (ret) 9528c2ecf20Sopenharmony_ci return ret; 9538c2ecf20Sopenharmony_ci 9548c2ecf20Sopenharmony_ci if (bq2515x->init_data.ilim) { 9558c2ecf20Sopenharmony_ci ret = bq2515x_set_ilim_lvl(bq2515x, bq2515x->init_data.ilim); 9568c2ecf20Sopenharmony_ci if (ret) 9578c2ecf20Sopenharmony_ci return ret; 9588c2ecf20Sopenharmony_ci } 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci ret = power_supply_get_battery_info(bq2515x->mains, &bat_info); 9618c2ecf20Sopenharmony_ci if (ret) { 9628c2ecf20Sopenharmony_ci dev_warn(bq2515x->dev, "battery info missing, default values will be applied\n"); 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci bq2515x->init_data.ichg = BQ2515X_DEFAULT_ICHG_UA; 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci bq2515x->init_data.vbatreg = BQ2515X_DEFAULT_VBAT_REG_UV; 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_ci bq2515x->init_data.iprechg = BQ2515X_DEFAULT_IPRECHARGE_UA; 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_ci } else { 9718c2ecf20Sopenharmony_ci bq2515x->init_data.ichg = 9728c2ecf20Sopenharmony_ci bat_info.constant_charge_current_max_ua; 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_ci bq2515x->init_data.vbatreg = 9758c2ecf20Sopenharmony_ci bat_info.constant_charge_voltage_max_uv; 9768c2ecf20Sopenharmony_ci 9778c2ecf20Sopenharmony_ci bq2515x->init_data.iprechg = 9788c2ecf20Sopenharmony_ci bat_info.precharge_current_ua; 9798c2ecf20Sopenharmony_ci } 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_ci ret = bq2515x_set_const_charge_current(bq2515x, 9828c2ecf20Sopenharmony_ci bq2515x->init_data.ichg); 9838c2ecf20Sopenharmony_ci if (ret) 9848c2ecf20Sopenharmony_ci return ret; 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ci ret = bq2515x_set_batt_reg(bq2515x, bq2515x->init_data.vbatreg); 9878c2ecf20Sopenharmony_ci if (ret) 9888c2ecf20Sopenharmony_ci return ret; 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_ci return bq2515x_set_precharge_current(bq2515x, 9918c2ecf20Sopenharmony_ci bq2515x->init_data.iprechg); 9928c2ecf20Sopenharmony_ci} 9938c2ecf20Sopenharmony_ci 9948c2ecf20Sopenharmony_cistatic int bq2515x_read_properties(struct bq2515x_device *bq2515x) 9958c2ecf20Sopenharmony_ci{ 9968c2ecf20Sopenharmony_ci int ret; 9978c2ecf20Sopenharmony_ci 9988c2ecf20Sopenharmony_ci ret = device_property_read_u32(bq2515x->dev, 9998c2ecf20Sopenharmony_ci "input-current-limit-microamp", 10008c2ecf20Sopenharmony_ci &bq2515x->init_data.ilim); 10018c2ecf20Sopenharmony_ci if (ret) { 10028c2ecf20Sopenharmony_ci switch (bq2515x->device_id) { 10038c2ecf20Sopenharmony_ci case BQ25150: 10048c2ecf20Sopenharmony_ci bq2515x->init_data.ilim = BQ25150_DEFAULT_ILIM_UA; 10058c2ecf20Sopenharmony_ci break; 10068c2ecf20Sopenharmony_ci case BQ25155: 10078c2ecf20Sopenharmony_ci bq2515x->init_data.ilim = BQ25155_DEFAULT_ILIM_UA; 10088c2ecf20Sopenharmony_ci break; 10098c2ecf20Sopenharmony_ci } 10108c2ecf20Sopenharmony_ci } 10118c2ecf20Sopenharmony_ci 10128c2ecf20Sopenharmony_ci bq2515x->ac_detect_gpio = devm_gpiod_get_optional(bq2515x->dev, 10138c2ecf20Sopenharmony_ci "ac-detect", GPIOD_IN); 10148c2ecf20Sopenharmony_ci if (IS_ERR(bq2515x->ac_detect_gpio)) { 10158c2ecf20Sopenharmony_ci ret = PTR_ERR(bq2515x->ac_detect_gpio); 10168c2ecf20Sopenharmony_ci dev_err(bq2515x->dev, "Failed to get ac detect"); 10178c2ecf20Sopenharmony_ci return ret; 10188c2ecf20Sopenharmony_ci } 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci bq2515x->reset_gpio = devm_gpiod_get_optional(bq2515x->dev, 10218c2ecf20Sopenharmony_ci "reset", GPIOD_OUT_LOW); 10228c2ecf20Sopenharmony_ci if (IS_ERR(bq2515x->reset_gpio)) { 10238c2ecf20Sopenharmony_ci ret = PTR_ERR(bq2515x->reset_gpio); 10248c2ecf20Sopenharmony_ci dev_err(bq2515x->dev, "Failed to get reset"); 10258c2ecf20Sopenharmony_ci return ret; 10268c2ecf20Sopenharmony_ci } 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ci bq2515x->powerdown_gpio = devm_gpiod_get_optional(bq2515x->dev, 10298c2ecf20Sopenharmony_ci "powerdown", GPIOD_OUT_LOW); 10308c2ecf20Sopenharmony_ci if (IS_ERR(bq2515x->powerdown_gpio)) { 10318c2ecf20Sopenharmony_ci ret = PTR_ERR(bq2515x->powerdown_gpio); 10328c2ecf20Sopenharmony_ci dev_err(bq2515x->dev, "Failed to get powerdown"); 10338c2ecf20Sopenharmony_ci return ret; 10348c2ecf20Sopenharmony_ci } 10358c2ecf20Sopenharmony_ci 10368c2ecf20Sopenharmony_ci bq2515x->ce_gpio = devm_gpiod_get_optional(bq2515x->dev, 10378c2ecf20Sopenharmony_ci "charge-enable", 10388c2ecf20Sopenharmony_ci GPIOD_OUT_LOW); 10398c2ecf20Sopenharmony_ci if (IS_ERR(bq2515x->ce_gpio)) { 10408c2ecf20Sopenharmony_ci ret = PTR_ERR(bq2515x->ce_gpio); 10418c2ecf20Sopenharmony_ci dev_err(bq2515x->dev, "Failed to get ce"); 10428c2ecf20Sopenharmony_ci return ret; 10438c2ecf20Sopenharmony_ci } 10448c2ecf20Sopenharmony_ci 10458c2ecf20Sopenharmony_ci return 0; 10468c2ecf20Sopenharmony_ci} 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_cistatic bool bq2515x_volatile_register(struct device *dev, unsigned int reg) 10498c2ecf20Sopenharmony_ci{ 10508c2ecf20Sopenharmony_ci switch (reg) { 10518c2ecf20Sopenharmony_ci case BQ2515X_STAT0 ... BQ2515X_FLAG3: 10528c2ecf20Sopenharmony_ci case BQ2515X_ADC_VBAT_M ... BQ2515X_ADC_IIN_L: 10538c2ecf20Sopenharmony_ci return true; 10548c2ecf20Sopenharmony_ci default: 10558c2ecf20Sopenharmony_ci return false; 10568c2ecf20Sopenharmony_ci } 10578c2ecf20Sopenharmony_ci} 10588c2ecf20Sopenharmony_ci 10598c2ecf20Sopenharmony_cistatic const struct regmap_config bq25150_regmap_config = { 10608c2ecf20Sopenharmony_ci .reg_bits = 8, 10618c2ecf20Sopenharmony_ci .val_bits = 8, 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_ci .max_register = BQ2515X_DEVICE_ID, 10648c2ecf20Sopenharmony_ci .reg_defaults = bq25150_reg_defaults, 10658c2ecf20Sopenharmony_ci .num_reg_defaults = ARRAY_SIZE(bq25150_reg_defaults), 10668c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 10678c2ecf20Sopenharmony_ci .volatile_reg = bq2515x_volatile_register, 10688c2ecf20Sopenharmony_ci}; 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_cistatic const struct regmap_config bq25155_regmap_config = { 10718c2ecf20Sopenharmony_ci .reg_bits = 8, 10728c2ecf20Sopenharmony_ci .val_bits = 8, 10738c2ecf20Sopenharmony_ci 10748c2ecf20Sopenharmony_ci .max_register = BQ2515X_DEVICE_ID, 10758c2ecf20Sopenharmony_ci .reg_defaults = bq25155_reg_defaults, 10768c2ecf20Sopenharmony_ci .num_reg_defaults = ARRAY_SIZE(bq25155_reg_defaults), 10778c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 10788c2ecf20Sopenharmony_ci .volatile_reg = bq2515x_volatile_register, 10798c2ecf20Sopenharmony_ci}; 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_cistatic int bq2515x_probe(struct i2c_client *client, 10828c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 10838c2ecf20Sopenharmony_ci{ 10848c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 10858c2ecf20Sopenharmony_ci struct bq2515x_device *bq2515x; 10868c2ecf20Sopenharmony_ci struct power_supply_config charger_cfg = {}; 10878c2ecf20Sopenharmony_ci int ret; 10888c2ecf20Sopenharmony_ci 10898c2ecf20Sopenharmony_ci bq2515x = devm_kzalloc(dev, sizeof(*bq2515x), GFP_KERNEL); 10908c2ecf20Sopenharmony_ci if (!bq2515x) 10918c2ecf20Sopenharmony_ci return -ENOMEM; 10928c2ecf20Sopenharmony_ci 10938c2ecf20Sopenharmony_ci bq2515x->dev = dev; 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci strncpy(bq2515x->model_name, id->name, I2C_NAME_SIZE); 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci bq2515x->device_id = id->driver_data; 10988c2ecf20Sopenharmony_ci 10998c2ecf20Sopenharmony_ci switch (bq2515x->device_id) { 11008c2ecf20Sopenharmony_ci case BQ25150: 11018c2ecf20Sopenharmony_ci bq2515x->regmap = devm_regmap_init_i2c(client, 11028c2ecf20Sopenharmony_ci &bq25150_regmap_config); 11038c2ecf20Sopenharmony_ci break; 11048c2ecf20Sopenharmony_ci case BQ25155: 11058c2ecf20Sopenharmony_ci bq2515x->regmap = devm_regmap_init_i2c(client, 11068c2ecf20Sopenharmony_ci &bq25155_regmap_config); 11078c2ecf20Sopenharmony_ci break; 11088c2ecf20Sopenharmony_ci } 11098c2ecf20Sopenharmony_ci 11108c2ecf20Sopenharmony_ci if (IS_ERR(bq2515x->regmap)) { 11118c2ecf20Sopenharmony_ci dev_err(dev, "failed to allocate register map\n"); 11128c2ecf20Sopenharmony_ci return PTR_ERR(bq2515x->regmap); 11138c2ecf20Sopenharmony_ci } 11148c2ecf20Sopenharmony_ci 11158c2ecf20Sopenharmony_ci i2c_set_clientdata(client, bq2515x); 11168c2ecf20Sopenharmony_ci 11178c2ecf20Sopenharmony_ci charger_cfg.drv_data = bq2515x; 11188c2ecf20Sopenharmony_ci charger_cfg.of_node = dev->of_node; 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_ci ret = bq2515x_read_properties(bq2515x); 11218c2ecf20Sopenharmony_ci if (ret) { 11228c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read device tree properties %d\n", 11238c2ecf20Sopenharmony_ci ret); 11248c2ecf20Sopenharmony_ci return ret; 11258c2ecf20Sopenharmony_ci } 11268c2ecf20Sopenharmony_ci 11278c2ecf20Sopenharmony_ci ret = bq2515x_power_supply_register(bq2515x, dev, charger_cfg); 11288c2ecf20Sopenharmony_ci if (ret) { 11298c2ecf20Sopenharmony_ci dev_err(dev, "failed to register power supply\n"); 11308c2ecf20Sopenharmony_ci return ret; 11318c2ecf20Sopenharmony_ci } 11328c2ecf20Sopenharmony_ci 11338c2ecf20Sopenharmony_ci ret = bq2515x_hw_init(bq2515x); 11348c2ecf20Sopenharmony_ci if (ret) { 11358c2ecf20Sopenharmony_ci dev_err(dev, "Cannot initialize the chip\n"); 11368c2ecf20Sopenharmony_ci return ret; 11378c2ecf20Sopenharmony_ci } 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_ci return 0; 11408c2ecf20Sopenharmony_ci} 11418c2ecf20Sopenharmony_ci 11428c2ecf20Sopenharmony_cistatic const struct i2c_device_id bq2515x_i2c_ids[] = { 11438c2ecf20Sopenharmony_ci { "bq25150", BQ25150, }, 11448c2ecf20Sopenharmony_ci { "bq25155", BQ25155, }, 11458c2ecf20Sopenharmony_ci {}, 11468c2ecf20Sopenharmony_ci}; 11478c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, bq2515x_i2c_ids); 11488c2ecf20Sopenharmony_ci 11498c2ecf20Sopenharmony_cistatic const struct of_device_id bq2515x_of_match[] = { 11508c2ecf20Sopenharmony_ci { .compatible = "ti,bq25150", }, 11518c2ecf20Sopenharmony_ci { .compatible = "ti,bq25155", }, 11528c2ecf20Sopenharmony_ci { }, 11538c2ecf20Sopenharmony_ci}; 11548c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bq2515x_of_match); 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_cistatic struct i2c_driver bq2515x_driver = { 11578c2ecf20Sopenharmony_ci .driver = { 11588c2ecf20Sopenharmony_ci .name = "bq2515x-charger", 11598c2ecf20Sopenharmony_ci .of_match_table = bq2515x_of_match, 11608c2ecf20Sopenharmony_ci }, 11618c2ecf20Sopenharmony_ci .probe = bq2515x_probe, 11628c2ecf20Sopenharmony_ci .id_table = bq2515x_i2c_ids, 11638c2ecf20Sopenharmony_ci}; 11648c2ecf20Sopenharmony_cimodule_i2c_driver(bq2515x_driver); 11658c2ecf20Sopenharmony_ci 11668c2ecf20Sopenharmony_ciMODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); 11678c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>"); 11688c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("BQ2515X charger driver"); 11698c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1170