18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * TI BQ25890 charger driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Intel Corporation 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci#include <linux/i2c.h> 108c2ecf20Sopenharmony_ci#include <linux/power_supply.h> 118c2ecf20Sopenharmony_ci#include <linux/regmap.h> 128c2ecf20Sopenharmony_ci#include <linux/types.h> 138c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 148c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 158c2ecf20Sopenharmony_ci#include <linux/delay.h> 168c2ecf20Sopenharmony_ci#include <linux/usb/phy.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/acpi.h> 198c2ecf20Sopenharmony_ci#include <linux/of.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#define BQ25890_MANUFACTURER "Texas Instruments" 228c2ecf20Sopenharmony_ci#define BQ25890_IRQ_PIN "bq25890_irq" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define BQ25890_ID 3 258c2ecf20Sopenharmony_ci#define BQ25895_ID 7 268c2ecf20Sopenharmony_ci#define BQ25896_ID 0 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cienum bq25890_chip_version { 298c2ecf20Sopenharmony_ci BQ25890, 308c2ecf20Sopenharmony_ci BQ25892, 318c2ecf20Sopenharmony_ci BQ25895, 328c2ecf20Sopenharmony_ci BQ25896, 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic const char *const bq25890_chip_name[] = { 368c2ecf20Sopenharmony_ci "BQ25890", 378c2ecf20Sopenharmony_ci "BQ25892", 388c2ecf20Sopenharmony_ci "BQ25895", 398c2ecf20Sopenharmony_ci "BQ25896", 408c2ecf20Sopenharmony_ci}; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cienum bq25890_fields { 438c2ecf20Sopenharmony_ci F_EN_HIZ, F_EN_ILIM, F_IILIM, /* Reg00 */ 448c2ecf20Sopenharmony_ci F_BHOT, F_BCOLD, F_VINDPM_OFS, /* Reg01 */ 458c2ecf20Sopenharmony_ci F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN, 468c2ecf20Sopenharmony_ci F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN, /* Reg02 */ 478c2ecf20Sopenharmony_ci F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN, 488c2ecf20Sopenharmony_ci F_MIN_VBAT_SEL, /* Reg03 */ 498c2ecf20Sopenharmony_ci F_PUMPX_EN, F_ICHG, /* Reg04 */ 508c2ecf20Sopenharmony_ci F_IPRECHG, F_ITERM, /* Reg05 */ 518c2ecf20Sopenharmony_ci F_VREG, F_BATLOWV, F_VRECHG, /* Reg06 */ 528c2ecf20Sopenharmony_ci F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR, 538c2ecf20Sopenharmony_ci F_JEITA_ISET, /* Reg07 */ 548c2ecf20Sopenharmony_ci F_BATCMP, F_VCLAMP, F_TREG, /* Reg08 */ 558c2ecf20Sopenharmony_ci F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET, 568c2ecf20Sopenharmony_ci F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN, /* Reg09 */ 578c2ecf20Sopenharmony_ci F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI, /* Reg0A */ 588c2ecf20Sopenharmony_ci F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD, 598c2ecf20Sopenharmony_ci F_VSYS_STAT, /* Reg0B */ 608c2ecf20Sopenharmony_ci F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT, 618c2ecf20Sopenharmony_ci F_NTC_FAULT, /* Reg0C */ 628c2ecf20Sopenharmony_ci F_FORCE_VINDPM, F_VINDPM, /* Reg0D */ 638c2ecf20Sopenharmony_ci F_THERM_STAT, F_BATV, /* Reg0E */ 648c2ecf20Sopenharmony_ci F_SYSV, /* Reg0F */ 658c2ecf20Sopenharmony_ci F_TSPCT, /* Reg10 */ 668c2ecf20Sopenharmony_ci F_VBUS_GD, F_VBUSV, /* Reg11 */ 678c2ecf20Sopenharmony_ci F_ICHGR, /* Reg12 */ 688c2ecf20Sopenharmony_ci F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM, /* Reg13 */ 698c2ecf20Sopenharmony_ci F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV, /* Reg14 */ 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci F_MAX_FIELDS 728c2ecf20Sopenharmony_ci}; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci/* initial field values, converted to register values */ 758c2ecf20Sopenharmony_cistruct bq25890_init_data { 768c2ecf20Sopenharmony_ci u8 ichg; /* charge current */ 778c2ecf20Sopenharmony_ci u8 vreg; /* regulation voltage */ 788c2ecf20Sopenharmony_ci u8 iterm; /* termination current */ 798c2ecf20Sopenharmony_ci u8 iprechg; /* precharge current */ 808c2ecf20Sopenharmony_ci u8 sysvmin; /* minimum system voltage limit */ 818c2ecf20Sopenharmony_ci u8 boostv; /* boost regulation voltage */ 828c2ecf20Sopenharmony_ci u8 boosti; /* boost current limit */ 838c2ecf20Sopenharmony_ci u8 boostf; /* boost frequency */ 848c2ecf20Sopenharmony_ci u8 ilim_en; /* enable ILIM pin */ 858c2ecf20Sopenharmony_ci u8 treg; /* thermal regulation threshold */ 868c2ecf20Sopenharmony_ci u8 rbatcomp; /* IBAT sense resistor value */ 878c2ecf20Sopenharmony_ci u8 vclamp; /* IBAT compensation voltage limit */ 888c2ecf20Sopenharmony_ci}; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistruct bq25890_state { 918c2ecf20Sopenharmony_ci u8 online; 928c2ecf20Sopenharmony_ci u8 chrg_status; 938c2ecf20Sopenharmony_ci u8 chrg_fault; 948c2ecf20Sopenharmony_ci u8 vsys_status; 958c2ecf20Sopenharmony_ci u8 boost_fault; 968c2ecf20Sopenharmony_ci u8 bat_fault; 978c2ecf20Sopenharmony_ci}; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistruct bq25890_device { 1008c2ecf20Sopenharmony_ci struct i2c_client *client; 1018c2ecf20Sopenharmony_ci struct device *dev; 1028c2ecf20Sopenharmony_ci struct power_supply *charger; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci struct usb_phy *usb_phy; 1058c2ecf20Sopenharmony_ci struct notifier_block usb_nb; 1068c2ecf20Sopenharmony_ci struct work_struct usb_work; 1078c2ecf20Sopenharmony_ci unsigned long usb_event; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci struct regmap *rmap; 1108c2ecf20Sopenharmony_ci struct regmap_field *rmap_fields[F_MAX_FIELDS]; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci enum bq25890_chip_version chip_version; 1138c2ecf20Sopenharmony_ci struct bq25890_init_data init_data; 1148c2ecf20Sopenharmony_ci struct bq25890_state state; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci struct mutex lock; /* protect state data */ 1178c2ecf20Sopenharmony_ci}; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic const struct regmap_range bq25890_readonly_reg_ranges[] = { 1208c2ecf20Sopenharmony_ci regmap_reg_range(0x0b, 0x0c), 1218c2ecf20Sopenharmony_ci regmap_reg_range(0x0e, 0x13), 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic const struct regmap_access_table bq25890_writeable_regs = { 1258c2ecf20Sopenharmony_ci .no_ranges = bq25890_readonly_reg_ranges, 1268c2ecf20Sopenharmony_ci .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges), 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic const struct regmap_range bq25890_volatile_reg_ranges[] = { 1308c2ecf20Sopenharmony_ci regmap_reg_range(0x00, 0x00), 1318c2ecf20Sopenharmony_ci regmap_reg_range(0x02, 0x02), 1328c2ecf20Sopenharmony_ci regmap_reg_range(0x09, 0x09), 1338c2ecf20Sopenharmony_ci regmap_reg_range(0x0b, 0x14), 1348c2ecf20Sopenharmony_ci}; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic const struct regmap_access_table bq25890_volatile_regs = { 1378c2ecf20Sopenharmony_ci .yes_ranges = bq25890_volatile_reg_ranges, 1388c2ecf20Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges), 1398c2ecf20Sopenharmony_ci}; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic const struct regmap_config bq25890_regmap_config = { 1428c2ecf20Sopenharmony_ci .reg_bits = 8, 1438c2ecf20Sopenharmony_ci .val_bits = 8, 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci .max_register = 0x14, 1468c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci .wr_table = &bq25890_writeable_regs, 1498c2ecf20Sopenharmony_ci .volatile_table = &bq25890_volatile_regs, 1508c2ecf20Sopenharmony_ci}; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic const struct reg_field bq25890_reg_fields[] = { 1538c2ecf20Sopenharmony_ci /* REG00 */ 1548c2ecf20Sopenharmony_ci [F_EN_HIZ] = REG_FIELD(0x00, 7, 7), 1558c2ecf20Sopenharmony_ci [F_EN_ILIM] = REG_FIELD(0x00, 6, 6), 1568c2ecf20Sopenharmony_ci [F_IILIM] = REG_FIELD(0x00, 0, 5), 1578c2ecf20Sopenharmony_ci /* REG01 */ 1588c2ecf20Sopenharmony_ci [F_BHOT] = REG_FIELD(0x01, 6, 7), 1598c2ecf20Sopenharmony_ci [F_BCOLD] = REG_FIELD(0x01, 5, 5), 1608c2ecf20Sopenharmony_ci [F_VINDPM_OFS] = REG_FIELD(0x01, 0, 4), 1618c2ecf20Sopenharmony_ci /* REG02 */ 1628c2ecf20Sopenharmony_ci [F_CONV_START] = REG_FIELD(0x02, 7, 7), 1638c2ecf20Sopenharmony_ci [F_CONV_RATE] = REG_FIELD(0x02, 6, 6), 1648c2ecf20Sopenharmony_ci [F_BOOSTF] = REG_FIELD(0x02, 5, 5), 1658c2ecf20Sopenharmony_ci [F_ICO_EN] = REG_FIELD(0x02, 4, 4), 1668c2ecf20Sopenharmony_ci [F_HVDCP_EN] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896 1678c2ecf20Sopenharmony_ci [F_MAXC_EN] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896 1688c2ecf20Sopenharmony_ci [F_FORCE_DPM] = REG_FIELD(0x02, 1, 1), 1698c2ecf20Sopenharmony_ci [F_AUTO_DPDM_EN] = REG_FIELD(0x02, 0, 0), 1708c2ecf20Sopenharmony_ci /* REG03 */ 1718c2ecf20Sopenharmony_ci [F_BAT_LOAD_EN] = REG_FIELD(0x03, 7, 7), 1728c2ecf20Sopenharmony_ci [F_WD_RST] = REG_FIELD(0x03, 6, 6), 1738c2ecf20Sopenharmony_ci [F_OTG_CFG] = REG_FIELD(0x03, 5, 5), 1748c2ecf20Sopenharmony_ci [F_CHG_CFG] = REG_FIELD(0x03, 4, 4), 1758c2ecf20Sopenharmony_ci [F_SYSVMIN] = REG_FIELD(0x03, 1, 3), 1768c2ecf20Sopenharmony_ci [F_MIN_VBAT_SEL] = REG_FIELD(0x03, 0, 0), // BQ25896 only 1778c2ecf20Sopenharmony_ci /* REG04 */ 1788c2ecf20Sopenharmony_ci [F_PUMPX_EN] = REG_FIELD(0x04, 7, 7), 1798c2ecf20Sopenharmony_ci [F_ICHG] = REG_FIELD(0x04, 0, 6), 1808c2ecf20Sopenharmony_ci /* REG05 */ 1818c2ecf20Sopenharmony_ci [F_IPRECHG] = REG_FIELD(0x05, 4, 7), 1828c2ecf20Sopenharmony_ci [F_ITERM] = REG_FIELD(0x05, 0, 3), 1838c2ecf20Sopenharmony_ci /* REG06 */ 1848c2ecf20Sopenharmony_ci [F_VREG] = REG_FIELD(0x06, 2, 7), 1858c2ecf20Sopenharmony_ci [F_BATLOWV] = REG_FIELD(0x06, 1, 1), 1868c2ecf20Sopenharmony_ci [F_VRECHG] = REG_FIELD(0x06, 0, 0), 1878c2ecf20Sopenharmony_ci /* REG07 */ 1888c2ecf20Sopenharmony_ci [F_TERM_EN] = REG_FIELD(0x07, 7, 7), 1898c2ecf20Sopenharmony_ci [F_STAT_DIS] = REG_FIELD(0x07, 6, 6), 1908c2ecf20Sopenharmony_ci [F_WD] = REG_FIELD(0x07, 4, 5), 1918c2ecf20Sopenharmony_ci [F_TMR_EN] = REG_FIELD(0x07, 3, 3), 1928c2ecf20Sopenharmony_ci [F_CHG_TMR] = REG_FIELD(0x07, 1, 2), 1938c2ecf20Sopenharmony_ci [F_JEITA_ISET] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895 1948c2ecf20Sopenharmony_ci /* REG08 */ 1958c2ecf20Sopenharmony_ci [F_BATCMP] = REG_FIELD(0x08, 5, 7), 1968c2ecf20Sopenharmony_ci [F_VCLAMP] = REG_FIELD(0x08, 2, 4), 1978c2ecf20Sopenharmony_ci [F_TREG] = REG_FIELD(0x08, 0, 1), 1988c2ecf20Sopenharmony_ci /* REG09 */ 1998c2ecf20Sopenharmony_ci [F_FORCE_ICO] = REG_FIELD(0x09, 7, 7), 2008c2ecf20Sopenharmony_ci [F_TMR2X_EN] = REG_FIELD(0x09, 6, 6), 2018c2ecf20Sopenharmony_ci [F_BATFET_DIS] = REG_FIELD(0x09, 5, 5), 2028c2ecf20Sopenharmony_ci [F_JEITA_VSET] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895 2038c2ecf20Sopenharmony_ci [F_BATFET_DLY] = REG_FIELD(0x09, 3, 3), 2048c2ecf20Sopenharmony_ci [F_BATFET_RST_EN] = REG_FIELD(0x09, 2, 2), 2058c2ecf20Sopenharmony_ci [F_PUMPX_UP] = REG_FIELD(0x09, 1, 1), 2068c2ecf20Sopenharmony_ci [F_PUMPX_DN] = REG_FIELD(0x09, 0, 0), 2078c2ecf20Sopenharmony_ci /* REG0A */ 2088c2ecf20Sopenharmony_ci [F_BOOSTV] = REG_FIELD(0x0A, 4, 7), 2098c2ecf20Sopenharmony_ci [F_BOOSTI] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895 2108c2ecf20Sopenharmony_ci [F_PFM_OTG_DIS] = REG_FIELD(0x0A, 3, 3), // BQ25896 only 2118c2ecf20Sopenharmony_ci /* REG0B */ 2128c2ecf20Sopenharmony_ci [F_VBUS_STAT] = REG_FIELD(0x0B, 5, 7), 2138c2ecf20Sopenharmony_ci [F_CHG_STAT] = REG_FIELD(0x0B, 3, 4), 2148c2ecf20Sopenharmony_ci [F_PG_STAT] = REG_FIELD(0x0B, 2, 2), 2158c2ecf20Sopenharmony_ci [F_SDP_STAT] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896 2168c2ecf20Sopenharmony_ci [F_VSYS_STAT] = REG_FIELD(0x0B, 0, 0), 2178c2ecf20Sopenharmony_ci /* REG0C */ 2188c2ecf20Sopenharmony_ci [F_WD_FAULT] = REG_FIELD(0x0C, 7, 7), 2198c2ecf20Sopenharmony_ci [F_BOOST_FAULT] = REG_FIELD(0x0C, 6, 6), 2208c2ecf20Sopenharmony_ci [F_CHG_FAULT] = REG_FIELD(0x0C, 4, 5), 2218c2ecf20Sopenharmony_ci [F_BAT_FAULT] = REG_FIELD(0x0C, 3, 3), 2228c2ecf20Sopenharmony_ci [F_NTC_FAULT] = REG_FIELD(0x0C, 0, 2), 2238c2ecf20Sopenharmony_ci /* REG0D */ 2248c2ecf20Sopenharmony_ci [F_FORCE_VINDPM] = REG_FIELD(0x0D, 7, 7), 2258c2ecf20Sopenharmony_ci [F_VINDPM] = REG_FIELD(0x0D, 0, 6), 2268c2ecf20Sopenharmony_ci /* REG0E */ 2278c2ecf20Sopenharmony_ci [F_THERM_STAT] = REG_FIELD(0x0E, 7, 7), 2288c2ecf20Sopenharmony_ci [F_BATV] = REG_FIELD(0x0E, 0, 6), 2298c2ecf20Sopenharmony_ci /* REG0F */ 2308c2ecf20Sopenharmony_ci [F_SYSV] = REG_FIELD(0x0F, 0, 6), 2318c2ecf20Sopenharmony_ci /* REG10 */ 2328c2ecf20Sopenharmony_ci [F_TSPCT] = REG_FIELD(0x10, 0, 6), 2338c2ecf20Sopenharmony_ci /* REG11 */ 2348c2ecf20Sopenharmony_ci [F_VBUS_GD] = REG_FIELD(0x11, 7, 7), 2358c2ecf20Sopenharmony_ci [F_VBUSV] = REG_FIELD(0x11, 0, 6), 2368c2ecf20Sopenharmony_ci /* REG12 */ 2378c2ecf20Sopenharmony_ci [F_ICHGR] = REG_FIELD(0x12, 0, 6), 2388c2ecf20Sopenharmony_ci /* REG13 */ 2398c2ecf20Sopenharmony_ci [F_VDPM_STAT] = REG_FIELD(0x13, 7, 7), 2408c2ecf20Sopenharmony_ci [F_IDPM_STAT] = REG_FIELD(0x13, 6, 6), 2418c2ecf20Sopenharmony_ci [F_IDPM_LIM] = REG_FIELD(0x13, 0, 5), 2428c2ecf20Sopenharmony_ci /* REG14 */ 2438c2ecf20Sopenharmony_ci [F_REG_RST] = REG_FIELD(0x14, 7, 7), 2448c2ecf20Sopenharmony_ci [F_ICO_OPTIMIZED] = REG_FIELD(0x14, 6, 6), 2458c2ecf20Sopenharmony_ci [F_PN] = REG_FIELD(0x14, 3, 5), 2468c2ecf20Sopenharmony_ci [F_TS_PROFILE] = REG_FIELD(0x14, 2, 2), 2478c2ecf20Sopenharmony_ci [F_DEV_REV] = REG_FIELD(0x14, 0, 1) 2488c2ecf20Sopenharmony_ci}; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci/* 2518c2ecf20Sopenharmony_ci * Most of the val -> idx conversions can be computed, given the minimum, 2528c2ecf20Sopenharmony_ci * maximum and the step between values. For the rest of conversions, we use 2538c2ecf20Sopenharmony_ci * lookup tables. 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_cienum bq25890_table_ids { 2568c2ecf20Sopenharmony_ci /* range tables */ 2578c2ecf20Sopenharmony_ci TBL_ICHG, 2588c2ecf20Sopenharmony_ci TBL_ITERM, 2598c2ecf20Sopenharmony_ci TBL_IILIM, 2608c2ecf20Sopenharmony_ci TBL_VREG, 2618c2ecf20Sopenharmony_ci TBL_BOOSTV, 2628c2ecf20Sopenharmony_ci TBL_SYSVMIN, 2638c2ecf20Sopenharmony_ci TBL_VBATCOMP, 2648c2ecf20Sopenharmony_ci TBL_RBATCOMP, 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci /* lookup tables */ 2678c2ecf20Sopenharmony_ci TBL_TREG, 2688c2ecf20Sopenharmony_ci TBL_BOOSTI, 2698c2ecf20Sopenharmony_ci}; 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci/* Thermal Regulation Threshold lookup table, in degrees Celsius */ 2728c2ecf20Sopenharmony_cistatic const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 }; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci#define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl) 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci/* Boost mode current limit lookup table, in uA */ 2778c2ecf20Sopenharmony_cistatic const u32 bq25890_boosti_tbl[] = { 2788c2ecf20Sopenharmony_ci 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000 2798c2ecf20Sopenharmony_ci}; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci#define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl) 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_cistruct bq25890_range { 2848c2ecf20Sopenharmony_ci u32 min; 2858c2ecf20Sopenharmony_ci u32 max; 2868c2ecf20Sopenharmony_ci u32 step; 2878c2ecf20Sopenharmony_ci}; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_cistruct bq25890_lookup { 2908c2ecf20Sopenharmony_ci const u32 *tbl; 2918c2ecf20Sopenharmony_ci u32 size; 2928c2ecf20Sopenharmony_ci}; 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_cistatic const union { 2958c2ecf20Sopenharmony_ci struct bq25890_range rt; 2968c2ecf20Sopenharmony_ci struct bq25890_lookup lt; 2978c2ecf20Sopenharmony_ci} bq25890_tables[] = { 2988c2ecf20Sopenharmony_ci /* range tables */ 2998c2ecf20Sopenharmony_ci /* TODO: BQ25896 has max ICHG 3008 mA */ 3008c2ecf20Sopenharmony_ci [TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */ 3018c2ecf20Sopenharmony_ci [TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */ 3028c2ecf20Sopenharmony_ci [TBL_IILIM] = { .rt = {100000, 3250000, 50000} }, /* uA */ 3038c2ecf20Sopenharmony_ci [TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */ 3048c2ecf20Sopenharmony_ci [TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */ 3058c2ecf20Sopenharmony_ci [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */ 3068c2ecf20Sopenharmony_ci [TBL_VBATCOMP] ={ .rt = {0, 224000, 32000} }, /* uV */ 3078c2ecf20Sopenharmony_ci [TBL_RBATCOMP] ={ .rt = {0, 140000, 20000} }, /* uOhm */ 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci /* lookup tables */ 3108c2ecf20Sopenharmony_ci [TBL_TREG] = { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} }, 3118c2ecf20Sopenharmony_ci [TBL_BOOSTI] = { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} } 3128c2ecf20Sopenharmony_ci}; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_cistatic int bq25890_field_read(struct bq25890_device *bq, 3158c2ecf20Sopenharmony_ci enum bq25890_fields field_id) 3168c2ecf20Sopenharmony_ci{ 3178c2ecf20Sopenharmony_ci int ret; 3188c2ecf20Sopenharmony_ci int val; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci ret = regmap_field_read(bq->rmap_fields[field_id], &val); 3218c2ecf20Sopenharmony_ci if (ret < 0) 3228c2ecf20Sopenharmony_ci return ret; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci return val; 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic int bq25890_field_write(struct bq25890_device *bq, 3288c2ecf20Sopenharmony_ci enum bq25890_fields field_id, u8 val) 3298c2ecf20Sopenharmony_ci{ 3308c2ecf20Sopenharmony_ci return regmap_field_write(bq->rmap_fields[field_id], val); 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_cistatic u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id) 3348c2ecf20Sopenharmony_ci{ 3358c2ecf20Sopenharmony_ci u8 idx; 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci if (id >= TBL_TREG) { 3388c2ecf20Sopenharmony_ci const u32 *tbl = bq25890_tables[id].lt.tbl; 3398c2ecf20Sopenharmony_ci u32 tbl_size = bq25890_tables[id].lt.size; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++) 3428c2ecf20Sopenharmony_ci ; 3438c2ecf20Sopenharmony_ci } else { 3448c2ecf20Sopenharmony_ci const struct bq25890_range *rtbl = &bq25890_tables[id].rt; 3458c2ecf20Sopenharmony_ci u8 rtbl_size; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci for (idx = 1; 3508c2ecf20Sopenharmony_ci idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value); 3518c2ecf20Sopenharmony_ci idx++) 3528c2ecf20Sopenharmony_ci ; 3538c2ecf20Sopenharmony_ci } 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci return idx - 1; 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_cistatic u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id) 3598c2ecf20Sopenharmony_ci{ 3608c2ecf20Sopenharmony_ci const struct bq25890_range *rtbl; 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci /* lookup table? */ 3638c2ecf20Sopenharmony_ci if (id >= TBL_TREG) 3648c2ecf20Sopenharmony_ci return bq25890_tables[id].lt.tbl[idx]; 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci /* range table */ 3678c2ecf20Sopenharmony_ci rtbl = &bq25890_tables[id].rt; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci return (rtbl->min + idx * rtbl->step); 3708c2ecf20Sopenharmony_ci} 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_cienum bq25890_status { 3738c2ecf20Sopenharmony_ci STATUS_NOT_CHARGING, 3748c2ecf20Sopenharmony_ci STATUS_PRE_CHARGING, 3758c2ecf20Sopenharmony_ci STATUS_FAST_CHARGING, 3768c2ecf20Sopenharmony_ci STATUS_TERMINATION_DONE, 3778c2ecf20Sopenharmony_ci}; 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_cienum bq25890_chrg_fault { 3808c2ecf20Sopenharmony_ci CHRG_FAULT_NORMAL, 3818c2ecf20Sopenharmony_ci CHRG_FAULT_INPUT, 3828c2ecf20Sopenharmony_ci CHRG_FAULT_THERMAL_SHUTDOWN, 3838c2ecf20Sopenharmony_ci CHRG_FAULT_TIMER_EXPIRED, 3848c2ecf20Sopenharmony_ci}; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_cistatic bool bq25890_is_adc_property(enum power_supply_property psp) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci switch (psp) { 3898c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_NOW: 3908c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_NOW: 3918c2ecf20Sopenharmony_ci return true; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci default: 3948c2ecf20Sopenharmony_ci return false; 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_cistatic irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq); 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_cistatic int bq25890_power_supply_get_property(struct power_supply *psy, 4018c2ecf20Sopenharmony_ci enum power_supply_property psp, 4028c2ecf20Sopenharmony_ci union power_supply_propval *val) 4038c2ecf20Sopenharmony_ci{ 4048c2ecf20Sopenharmony_ci struct bq25890_device *bq = power_supply_get_drvdata(psy); 4058c2ecf20Sopenharmony_ci struct bq25890_state state; 4068c2ecf20Sopenharmony_ci bool do_adc_conv; 4078c2ecf20Sopenharmony_ci int ret; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci mutex_lock(&bq->lock); 4108c2ecf20Sopenharmony_ci /* update state in case we lost an interrupt */ 4118c2ecf20Sopenharmony_ci __bq25890_handle_irq(bq); 4128c2ecf20Sopenharmony_ci state = bq->state; 4138c2ecf20Sopenharmony_ci do_adc_conv = !state.online && bq25890_is_adc_property(psp); 4148c2ecf20Sopenharmony_ci if (do_adc_conv) 4158c2ecf20Sopenharmony_ci bq25890_field_write(bq, F_CONV_START, 1); 4168c2ecf20Sopenharmony_ci mutex_unlock(&bq->lock); 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci if (do_adc_conv) 4198c2ecf20Sopenharmony_ci regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START], 4208c2ecf20Sopenharmony_ci ret, !ret, 25000, 1000000); 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci switch (psp) { 4238c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_STATUS: 4248c2ecf20Sopenharmony_ci if (!state.online) 4258c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 4268c2ecf20Sopenharmony_ci else if (state.chrg_status == STATUS_NOT_CHARGING) 4278c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 4288c2ecf20Sopenharmony_ci else if (state.chrg_status == STATUS_PRE_CHARGING || 4298c2ecf20Sopenharmony_ci state.chrg_status == STATUS_FAST_CHARGING) 4308c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_CHARGING; 4318c2ecf20Sopenharmony_ci else if (state.chrg_status == STATUS_TERMINATION_DONE) 4328c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_FULL; 4338c2ecf20Sopenharmony_ci else 4348c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci break; 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_TYPE: 4398c2ecf20Sopenharmony_ci if (!state.online || state.chrg_status == STATUS_NOT_CHARGING || 4408c2ecf20Sopenharmony_ci state.chrg_status == STATUS_TERMINATION_DONE) 4418c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; 4428c2ecf20Sopenharmony_ci else if (state.chrg_status == STATUS_PRE_CHARGING) 4438c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD; 4448c2ecf20Sopenharmony_ci else if (state.chrg_status == STATUS_FAST_CHARGING) 4458c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; 4468c2ecf20Sopenharmony_ci else /* unreachable */ 4478c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; 4488c2ecf20Sopenharmony_ci break; 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MANUFACTURER: 4518c2ecf20Sopenharmony_ci val->strval = BQ25890_MANUFACTURER; 4528c2ecf20Sopenharmony_ci break; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_MODEL_NAME: 4558c2ecf20Sopenharmony_ci val->strval = bq25890_chip_name[bq->chip_version]; 4568c2ecf20Sopenharmony_ci break; 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ONLINE: 4598c2ecf20Sopenharmony_ci val->intval = state.online; 4608c2ecf20Sopenharmony_ci break; 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_HEALTH: 4638c2ecf20Sopenharmony_ci if (!state.chrg_fault && !state.bat_fault && !state.boost_fault) 4648c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_GOOD; 4658c2ecf20Sopenharmony_ci else if (state.bat_fault) 4668c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 4678c2ecf20Sopenharmony_ci else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED) 4688c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 4698c2ecf20Sopenharmony_ci else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN) 4708c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 4718c2ecf20Sopenharmony_ci else 4728c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 4738c2ecf20Sopenharmony_ci break; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 4768c2ecf20Sopenharmony_ci val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG); 4778c2ecf20Sopenharmony_ci break; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 4808c2ecf20Sopenharmony_ci if (!state.online) { 4818c2ecf20Sopenharmony_ci val->intval = 0; 4828c2ecf20Sopenharmony_ci break; 4838c2ecf20Sopenharmony_ci } 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci ret = bq25890_field_read(bq, F_BATV); /* read measured value */ 4868c2ecf20Sopenharmony_ci if (ret < 0) 4878c2ecf20Sopenharmony_ci return ret; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */ 4908c2ecf20Sopenharmony_ci val->intval = 2304000 + ret * 20000; 4918c2ecf20Sopenharmony_ci break; 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 4948c2ecf20Sopenharmony_ci val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG); 4958c2ecf20Sopenharmony_ci break; 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 4988c2ecf20Sopenharmony_ci val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM); 4998c2ecf20Sopenharmony_ci break; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 5028c2ecf20Sopenharmony_ci val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM); 5038c2ecf20Sopenharmony_ci break; 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 5068c2ecf20Sopenharmony_ci ret = bq25890_field_read(bq, F_IILIM); 5078c2ecf20Sopenharmony_ci if (ret < 0) 5088c2ecf20Sopenharmony_ci return ret; 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci val->intval = bq25890_find_val(ret, TBL_IILIM); 5118c2ecf20Sopenharmony_ci break; 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_NOW: 5148c2ecf20Sopenharmony_ci ret = bq25890_field_read(bq, F_SYSV); /* read measured value */ 5158c2ecf20Sopenharmony_ci if (ret < 0) 5168c2ecf20Sopenharmony_ci return ret; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */ 5198c2ecf20Sopenharmony_ci val->intval = 2304000 + ret * 20000; 5208c2ecf20Sopenharmony_ci break; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_NOW: 5238c2ecf20Sopenharmony_ci ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */ 5248c2ecf20Sopenharmony_ci if (ret < 0) 5258c2ecf20Sopenharmony_ci return ret; 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci /* converted_val = ADC_val * 50mA (table 10.3.19) */ 5288c2ecf20Sopenharmony_ci val->intval = ret * -50000; 5298c2ecf20Sopenharmony_ci break; 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci default: 5328c2ecf20Sopenharmony_ci return -EINVAL; 5338c2ecf20Sopenharmony_ci } 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci return 0; 5368c2ecf20Sopenharmony_ci} 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_cistatic int bq25890_get_chip_state(struct bq25890_device *bq, 5398c2ecf20Sopenharmony_ci struct bq25890_state *state) 5408c2ecf20Sopenharmony_ci{ 5418c2ecf20Sopenharmony_ci int i, ret; 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci struct { 5448c2ecf20Sopenharmony_ci enum bq25890_fields id; 5458c2ecf20Sopenharmony_ci u8 *data; 5468c2ecf20Sopenharmony_ci } state_fields[] = { 5478c2ecf20Sopenharmony_ci {F_CHG_STAT, &state->chrg_status}, 5488c2ecf20Sopenharmony_ci {F_PG_STAT, &state->online}, 5498c2ecf20Sopenharmony_ci {F_VSYS_STAT, &state->vsys_status}, 5508c2ecf20Sopenharmony_ci {F_BOOST_FAULT, &state->boost_fault}, 5518c2ecf20Sopenharmony_ci {F_BAT_FAULT, &state->bat_fault}, 5528c2ecf20Sopenharmony_ci {F_CHG_FAULT, &state->chrg_fault} 5538c2ecf20Sopenharmony_ci }; 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(state_fields); i++) { 5568c2ecf20Sopenharmony_ci ret = bq25890_field_read(bq, state_fields[i].id); 5578c2ecf20Sopenharmony_ci if (ret < 0) 5588c2ecf20Sopenharmony_ci return ret; 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci *state_fields[i].data = ret; 5618c2ecf20Sopenharmony_ci } 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n", 5648c2ecf20Sopenharmony_ci state->chrg_status, state->online, state->vsys_status, 5658c2ecf20Sopenharmony_ci state->chrg_fault, state->boost_fault, state->bat_fault); 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci return 0; 5688c2ecf20Sopenharmony_ci} 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_cistatic irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq) 5718c2ecf20Sopenharmony_ci{ 5728c2ecf20Sopenharmony_ci struct bq25890_state new_state; 5738c2ecf20Sopenharmony_ci int ret; 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci ret = bq25890_get_chip_state(bq, &new_state); 5768c2ecf20Sopenharmony_ci if (ret < 0) 5778c2ecf20Sopenharmony_ci return IRQ_NONE; 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci if (!memcmp(&bq->state, &new_state, sizeof(new_state))) 5808c2ecf20Sopenharmony_ci return IRQ_NONE; 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci if (!new_state.online && bq->state.online) { /* power removed */ 5838c2ecf20Sopenharmony_ci /* disable ADC */ 5848c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, F_CONV_RATE, 0); 5858c2ecf20Sopenharmony_ci if (ret < 0) 5868c2ecf20Sopenharmony_ci goto error; 5878c2ecf20Sopenharmony_ci } else if (new_state.online && !bq->state.online) { /* power inserted */ 5888c2ecf20Sopenharmony_ci /* enable ADC, to have control of charge current/voltage */ 5898c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, F_CONV_RATE, 1); 5908c2ecf20Sopenharmony_ci if (ret < 0) 5918c2ecf20Sopenharmony_ci goto error; 5928c2ecf20Sopenharmony_ci } 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci bq->state = new_state; 5958c2ecf20Sopenharmony_ci power_supply_changed(bq->charger); 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci return IRQ_HANDLED; 5988c2ecf20Sopenharmony_cierror: 5998c2ecf20Sopenharmony_ci dev_err(bq->dev, "Error communicating with the chip: %pe\n", 6008c2ecf20Sopenharmony_ci ERR_PTR(ret)); 6018c2ecf20Sopenharmony_ci return IRQ_HANDLED; 6028c2ecf20Sopenharmony_ci} 6038c2ecf20Sopenharmony_ci 6048c2ecf20Sopenharmony_cistatic irqreturn_t bq25890_irq_handler_thread(int irq, void *private) 6058c2ecf20Sopenharmony_ci{ 6068c2ecf20Sopenharmony_ci struct bq25890_device *bq = private; 6078c2ecf20Sopenharmony_ci irqreturn_t ret; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci mutex_lock(&bq->lock); 6108c2ecf20Sopenharmony_ci ret = __bq25890_handle_irq(bq); 6118c2ecf20Sopenharmony_ci mutex_unlock(&bq->lock); 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci return ret; 6148c2ecf20Sopenharmony_ci} 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_cistatic int bq25890_chip_reset(struct bq25890_device *bq) 6178c2ecf20Sopenharmony_ci{ 6188c2ecf20Sopenharmony_ci int ret; 6198c2ecf20Sopenharmony_ci int rst_check_counter = 10; 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, F_REG_RST, 1); 6228c2ecf20Sopenharmony_ci if (ret < 0) 6238c2ecf20Sopenharmony_ci return ret; 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_ci do { 6268c2ecf20Sopenharmony_ci ret = bq25890_field_read(bq, F_REG_RST); 6278c2ecf20Sopenharmony_ci if (ret < 0) 6288c2ecf20Sopenharmony_ci return ret; 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci usleep_range(5, 10); 6318c2ecf20Sopenharmony_ci } while (ret == 1 && --rst_check_counter); 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci if (!rst_check_counter) 6348c2ecf20Sopenharmony_ci return -ETIMEDOUT; 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci return 0; 6378c2ecf20Sopenharmony_ci} 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_cistatic int bq25890_hw_init(struct bq25890_device *bq) 6408c2ecf20Sopenharmony_ci{ 6418c2ecf20Sopenharmony_ci int ret; 6428c2ecf20Sopenharmony_ci int i; 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci const struct { 6458c2ecf20Sopenharmony_ci enum bq25890_fields id; 6468c2ecf20Sopenharmony_ci u32 value; 6478c2ecf20Sopenharmony_ci } init_data[] = { 6488c2ecf20Sopenharmony_ci {F_ICHG, bq->init_data.ichg}, 6498c2ecf20Sopenharmony_ci {F_VREG, bq->init_data.vreg}, 6508c2ecf20Sopenharmony_ci {F_ITERM, bq->init_data.iterm}, 6518c2ecf20Sopenharmony_ci {F_IPRECHG, bq->init_data.iprechg}, 6528c2ecf20Sopenharmony_ci {F_SYSVMIN, bq->init_data.sysvmin}, 6538c2ecf20Sopenharmony_ci {F_BOOSTV, bq->init_data.boostv}, 6548c2ecf20Sopenharmony_ci {F_BOOSTI, bq->init_data.boosti}, 6558c2ecf20Sopenharmony_ci {F_BOOSTF, bq->init_data.boostf}, 6568c2ecf20Sopenharmony_ci {F_EN_ILIM, bq->init_data.ilim_en}, 6578c2ecf20Sopenharmony_ci {F_TREG, bq->init_data.treg}, 6588c2ecf20Sopenharmony_ci {F_BATCMP, bq->init_data.rbatcomp}, 6598c2ecf20Sopenharmony_ci {F_VCLAMP, bq->init_data.vclamp}, 6608c2ecf20Sopenharmony_ci }; 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci ret = bq25890_chip_reset(bq); 6638c2ecf20Sopenharmony_ci if (ret < 0) { 6648c2ecf20Sopenharmony_ci dev_dbg(bq->dev, "Reset failed %d\n", ret); 6658c2ecf20Sopenharmony_ci return ret; 6668c2ecf20Sopenharmony_ci } 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci /* disable watchdog */ 6698c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, F_WD, 0); 6708c2ecf20Sopenharmony_ci if (ret < 0) { 6718c2ecf20Sopenharmony_ci dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret); 6728c2ecf20Sopenharmony_ci return ret; 6738c2ecf20Sopenharmony_ci } 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci /* initialize currents/voltages and other parameters */ 6768c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(init_data); i++) { 6778c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, init_data[i].id, 6788c2ecf20Sopenharmony_ci init_data[i].value); 6798c2ecf20Sopenharmony_ci if (ret < 0) { 6808c2ecf20Sopenharmony_ci dev_dbg(bq->dev, "Writing init data failed %d\n", ret); 6818c2ecf20Sopenharmony_ci return ret; 6828c2ecf20Sopenharmony_ci } 6838c2ecf20Sopenharmony_ci } 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci /* Configure ADC for continuous conversions when charging */ 6868c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online); 6878c2ecf20Sopenharmony_ci if (ret < 0) { 6888c2ecf20Sopenharmony_ci dev_dbg(bq->dev, "Config ADC failed %d\n", ret); 6898c2ecf20Sopenharmony_ci return ret; 6908c2ecf20Sopenharmony_ci } 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci ret = bq25890_get_chip_state(bq, &bq->state); 6938c2ecf20Sopenharmony_ci if (ret < 0) { 6948c2ecf20Sopenharmony_ci dev_dbg(bq->dev, "Get state failed %d\n", ret); 6958c2ecf20Sopenharmony_ci return ret; 6968c2ecf20Sopenharmony_ci } 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci return 0; 6998c2ecf20Sopenharmony_ci} 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_cistatic const enum power_supply_property bq25890_power_supply_props[] = { 7028c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MANUFACTURER, 7038c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_MODEL_NAME, 7048c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_STATUS, 7058c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_TYPE, 7068c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_ONLINE, 7078c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_HEALTH, 7088c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 7098c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 7108c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 7118c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_PRECHARGE_CURRENT, 7128c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 7138c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 7148c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_NOW, 7158c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_NOW, 7168c2ecf20Sopenharmony_ci}; 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_cistatic char *bq25890_charger_supplied_to[] = { 7198c2ecf20Sopenharmony_ci "main-battery", 7208c2ecf20Sopenharmony_ci}; 7218c2ecf20Sopenharmony_ci 7228c2ecf20Sopenharmony_cistatic const struct power_supply_desc bq25890_power_supply_desc = { 7238c2ecf20Sopenharmony_ci .name = "bq25890-charger", 7248c2ecf20Sopenharmony_ci .type = POWER_SUPPLY_TYPE_USB, 7258c2ecf20Sopenharmony_ci .properties = bq25890_power_supply_props, 7268c2ecf20Sopenharmony_ci .num_properties = ARRAY_SIZE(bq25890_power_supply_props), 7278c2ecf20Sopenharmony_ci .get_property = bq25890_power_supply_get_property, 7288c2ecf20Sopenharmony_ci}; 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_cistatic int bq25890_power_supply_init(struct bq25890_device *bq) 7318c2ecf20Sopenharmony_ci{ 7328c2ecf20Sopenharmony_ci struct power_supply_config psy_cfg = { .drv_data = bq, }; 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci psy_cfg.supplied_to = bq25890_charger_supplied_to; 7358c2ecf20Sopenharmony_ci psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to); 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci bq->charger = power_supply_register(bq->dev, &bq25890_power_supply_desc, 7388c2ecf20Sopenharmony_ci &psy_cfg); 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(bq->charger); 7418c2ecf20Sopenharmony_ci} 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_cistatic void bq25890_usb_work(struct work_struct *data) 7448c2ecf20Sopenharmony_ci{ 7458c2ecf20Sopenharmony_ci int ret; 7468c2ecf20Sopenharmony_ci struct bq25890_device *bq = 7478c2ecf20Sopenharmony_ci container_of(data, struct bq25890_device, usb_work); 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci switch (bq->usb_event) { 7508c2ecf20Sopenharmony_ci case USB_EVENT_ID: 7518c2ecf20Sopenharmony_ci /* Enable boost mode */ 7528c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, F_OTG_CFG, 1); 7538c2ecf20Sopenharmony_ci if (ret < 0) 7548c2ecf20Sopenharmony_ci goto error; 7558c2ecf20Sopenharmony_ci break; 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci case USB_EVENT_NONE: 7588c2ecf20Sopenharmony_ci /* Disable boost mode */ 7598c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, F_OTG_CFG, 0); 7608c2ecf20Sopenharmony_ci if (ret < 0) 7618c2ecf20Sopenharmony_ci goto error; 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci power_supply_changed(bq->charger); 7648c2ecf20Sopenharmony_ci break; 7658c2ecf20Sopenharmony_ci } 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_ci return; 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_cierror: 7708c2ecf20Sopenharmony_ci dev_err(bq->dev, "Error switching to boost/charger mode.\n"); 7718c2ecf20Sopenharmony_ci} 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_cistatic int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val, 7748c2ecf20Sopenharmony_ci void *priv) 7758c2ecf20Sopenharmony_ci{ 7768c2ecf20Sopenharmony_ci struct bq25890_device *bq = 7778c2ecf20Sopenharmony_ci container_of(nb, struct bq25890_device, usb_nb); 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci bq->usb_event = val; 7808c2ecf20Sopenharmony_ci queue_work(system_power_efficient_wq, &bq->usb_work); 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci return NOTIFY_OK; 7838c2ecf20Sopenharmony_ci} 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_cistatic int bq25890_get_chip_version(struct bq25890_device *bq) 7868c2ecf20Sopenharmony_ci{ 7878c2ecf20Sopenharmony_ci int id, rev; 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_ci id = bq25890_field_read(bq, F_PN); 7908c2ecf20Sopenharmony_ci if (id < 0) { 7918c2ecf20Sopenharmony_ci dev_err(bq->dev, "Cannot read chip ID.\n"); 7928c2ecf20Sopenharmony_ci return id; 7938c2ecf20Sopenharmony_ci } 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci rev = bq25890_field_read(bq, F_DEV_REV); 7968c2ecf20Sopenharmony_ci if (rev < 0) { 7978c2ecf20Sopenharmony_ci dev_err(bq->dev, "Cannot read chip revision.\n"); 7988c2ecf20Sopenharmony_ci return rev; 7998c2ecf20Sopenharmony_ci } 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci switch (id) { 8028c2ecf20Sopenharmony_ci case BQ25890_ID: 8038c2ecf20Sopenharmony_ci bq->chip_version = BQ25890; 8048c2ecf20Sopenharmony_ci break; 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci /* BQ25892 and BQ25896 share same ID 0 */ 8078c2ecf20Sopenharmony_ci case BQ25896_ID: 8088c2ecf20Sopenharmony_ci switch (rev) { 8098c2ecf20Sopenharmony_ci case 2: 8108c2ecf20Sopenharmony_ci bq->chip_version = BQ25896; 8118c2ecf20Sopenharmony_ci break; 8128c2ecf20Sopenharmony_ci case 1: 8138c2ecf20Sopenharmony_ci bq->chip_version = BQ25892; 8148c2ecf20Sopenharmony_ci break; 8158c2ecf20Sopenharmony_ci default: 8168c2ecf20Sopenharmony_ci dev_err(bq->dev, 8178c2ecf20Sopenharmony_ci "Unknown device revision %d, assume BQ25892\n", 8188c2ecf20Sopenharmony_ci rev); 8198c2ecf20Sopenharmony_ci bq->chip_version = BQ25892; 8208c2ecf20Sopenharmony_ci } 8218c2ecf20Sopenharmony_ci break; 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci case BQ25895_ID: 8248c2ecf20Sopenharmony_ci bq->chip_version = BQ25895; 8258c2ecf20Sopenharmony_ci break; 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci default: 8288c2ecf20Sopenharmony_ci dev_err(bq->dev, "Unknown chip ID %d\n", id); 8298c2ecf20Sopenharmony_ci return -ENODEV; 8308c2ecf20Sopenharmony_ci } 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci return 0; 8338c2ecf20Sopenharmony_ci} 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_cistatic int bq25890_irq_probe(struct bq25890_device *bq) 8368c2ecf20Sopenharmony_ci{ 8378c2ecf20Sopenharmony_ci struct gpio_desc *irq; 8388c2ecf20Sopenharmony_ci 8398c2ecf20Sopenharmony_ci irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN); 8408c2ecf20Sopenharmony_ci if (IS_ERR(irq)) { 8418c2ecf20Sopenharmony_ci dev_err(bq->dev, "Could not probe irq pin.\n"); 8428c2ecf20Sopenharmony_ci return PTR_ERR(irq); 8438c2ecf20Sopenharmony_ci } 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci return gpiod_to_irq(irq); 8468c2ecf20Sopenharmony_ci} 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_cistatic int bq25890_fw_read_u32_props(struct bq25890_device *bq) 8498c2ecf20Sopenharmony_ci{ 8508c2ecf20Sopenharmony_ci int ret; 8518c2ecf20Sopenharmony_ci u32 property; 8528c2ecf20Sopenharmony_ci int i; 8538c2ecf20Sopenharmony_ci struct bq25890_init_data *init = &bq->init_data; 8548c2ecf20Sopenharmony_ci struct { 8558c2ecf20Sopenharmony_ci char *name; 8568c2ecf20Sopenharmony_ci bool optional; 8578c2ecf20Sopenharmony_ci enum bq25890_table_ids tbl_id; 8588c2ecf20Sopenharmony_ci u8 *conv_data; /* holds converted value from given property */ 8598c2ecf20Sopenharmony_ci } props[] = { 8608c2ecf20Sopenharmony_ci /* required properties */ 8618c2ecf20Sopenharmony_ci {"ti,charge-current", false, TBL_ICHG, &init->ichg}, 8628c2ecf20Sopenharmony_ci {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg}, 8638c2ecf20Sopenharmony_ci {"ti,termination-current", false, TBL_ITERM, &init->iterm}, 8648c2ecf20Sopenharmony_ci {"ti,precharge-current", false, TBL_ITERM, &init->iprechg}, 8658c2ecf20Sopenharmony_ci {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin}, 8668c2ecf20Sopenharmony_ci {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv}, 8678c2ecf20Sopenharmony_ci {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti}, 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ci /* optional properties */ 8708c2ecf20Sopenharmony_ci {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg}, 8718c2ecf20Sopenharmony_ci {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp}, 8728c2ecf20Sopenharmony_ci {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp}, 8738c2ecf20Sopenharmony_ci }; 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci /* initialize data for optional properties */ 8768c2ecf20Sopenharmony_ci init->treg = 3; /* 120 degrees Celsius */ 8778c2ecf20Sopenharmony_ci init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */ 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(props); i++) { 8808c2ecf20Sopenharmony_ci ret = device_property_read_u32(bq->dev, props[i].name, 8818c2ecf20Sopenharmony_ci &property); 8828c2ecf20Sopenharmony_ci if (ret < 0) { 8838c2ecf20Sopenharmony_ci if (props[i].optional) 8848c2ecf20Sopenharmony_ci continue; 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci dev_err(bq->dev, "Unable to read property %d %s\n", ret, 8878c2ecf20Sopenharmony_ci props[i].name); 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci return ret; 8908c2ecf20Sopenharmony_ci } 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci *props[i].conv_data = bq25890_find_idx(property, 8938c2ecf20Sopenharmony_ci props[i].tbl_id); 8948c2ecf20Sopenharmony_ci } 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_ci return 0; 8978c2ecf20Sopenharmony_ci} 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_cistatic int bq25890_fw_probe(struct bq25890_device *bq) 9008c2ecf20Sopenharmony_ci{ 9018c2ecf20Sopenharmony_ci int ret; 9028c2ecf20Sopenharmony_ci struct bq25890_init_data *init = &bq->init_data; 9038c2ecf20Sopenharmony_ci 9048c2ecf20Sopenharmony_ci ret = bq25890_fw_read_u32_props(bq); 9058c2ecf20Sopenharmony_ci if (ret < 0) 9068c2ecf20Sopenharmony_ci return ret; 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin"); 9098c2ecf20Sopenharmony_ci init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq"); 9108c2ecf20Sopenharmony_ci 9118c2ecf20Sopenharmony_ci return 0; 9128c2ecf20Sopenharmony_ci} 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_cistatic int bq25890_probe(struct i2c_client *client, 9158c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 9168c2ecf20Sopenharmony_ci{ 9178c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 9188c2ecf20Sopenharmony_ci struct bq25890_device *bq; 9198c2ecf20Sopenharmony_ci int ret; 9208c2ecf20Sopenharmony_ci int i; 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_ci bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL); 9238c2ecf20Sopenharmony_ci if (!bq) 9248c2ecf20Sopenharmony_ci return -ENOMEM; 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci bq->client = client; 9278c2ecf20Sopenharmony_ci bq->dev = dev; 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_ci mutex_init(&bq->lock); 9308c2ecf20Sopenharmony_ci 9318c2ecf20Sopenharmony_ci bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config); 9328c2ecf20Sopenharmony_ci if (IS_ERR(bq->rmap)) { 9338c2ecf20Sopenharmony_ci dev_err(dev, "failed to allocate register map\n"); 9348c2ecf20Sopenharmony_ci return PTR_ERR(bq->rmap); 9358c2ecf20Sopenharmony_ci } 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) { 9388c2ecf20Sopenharmony_ci const struct reg_field *reg_fields = bq25890_reg_fields; 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_ci bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap, 9418c2ecf20Sopenharmony_ci reg_fields[i]); 9428c2ecf20Sopenharmony_ci if (IS_ERR(bq->rmap_fields[i])) { 9438c2ecf20Sopenharmony_ci dev_err(dev, "cannot allocate regmap field\n"); 9448c2ecf20Sopenharmony_ci return PTR_ERR(bq->rmap_fields[i]); 9458c2ecf20Sopenharmony_ci } 9468c2ecf20Sopenharmony_ci } 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci i2c_set_clientdata(client, bq); 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci ret = bq25890_get_chip_version(bq); 9518c2ecf20Sopenharmony_ci if (ret) { 9528c2ecf20Sopenharmony_ci dev_err(dev, "Cannot read chip ID or unknown chip.\n"); 9538c2ecf20Sopenharmony_ci return ret; 9548c2ecf20Sopenharmony_ci } 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_ci if (!dev->platform_data) { 9578c2ecf20Sopenharmony_ci ret = bq25890_fw_probe(bq); 9588c2ecf20Sopenharmony_ci if (ret < 0) { 9598c2ecf20Sopenharmony_ci dev_err(dev, "Cannot read device properties.\n"); 9608c2ecf20Sopenharmony_ci return ret; 9618c2ecf20Sopenharmony_ci } 9628c2ecf20Sopenharmony_ci } else { 9638c2ecf20Sopenharmony_ci return -ENODEV; 9648c2ecf20Sopenharmony_ci } 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci ret = bq25890_hw_init(bq); 9678c2ecf20Sopenharmony_ci if (ret < 0) { 9688c2ecf20Sopenharmony_ci dev_err(dev, "Cannot initialize the chip.\n"); 9698c2ecf20Sopenharmony_ci return ret; 9708c2ecf20Sopenharmony_ci } 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci if (client->irq <= 0) 9738c2ecf20Sopenharmony_ci client->irq = bq25890_irq_probe(bq); 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci if (client->irq < 0) { 9768c2ecf20Sopenharmony_ci dev_err(dev, "No irq resource found.\n"); 9778c2ecf20Sopenharmony_ci return client->irq; 9788c2ecf20Sopenharmony_ci } 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci /* OTG reporting */ 9818c2ecf20Sopenharmony_ci bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); 9828c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(bq->usb_phy)) { 9838c2ecf20Sopenharmony_ci INIT_WORK(&bq->usb_work, bq25890_usb_work); 9848c2ecf20Sopenharmony_ci bq->usb_nb.notifier_call = bq25890_usb_notifier; 9858c2ecf20Sopenharmony_ci usb_register_notifier(bq->usb_phy, &bq->usb_nb); 9868c2ecf20Sopenharmony_ci } 9878c2ecf20Sopenharmony_ci 9888c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(dev, client->irq, NULL, 9898c2ecf20Sopenharmony_ci bq25890_irq_handler_thread, 9908c2ecf20Sopenharmony_ci IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 9918c2ecf20Sopenharmony_ci BQ25890_IRQ_PIN, bq); 9928c2ecf20Sopenharmony_ci if (ret) 9938c2ecf20Sopenharmony_ci goto irq_fail; 9948c2ecf20Sopenharmony_ci 9958c2ecf20Sopenharmony_ci ret = bq25890_power_supply_init(bq); 9968c2ecf20Sopenharmony_ci if (ret < 0) { 9978c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register power supply\n"); 9988c2ecf20Sopenharmony_ci goto irq_fail; 9998c2ecf20Sopenharmony_ci } 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ci return 0; 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_ciirq_fail: 10048c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(bq->usb_phy)) 10058c2ecf20Sopenharmony_ci usb_unregister_notifier(bq->usb_phy, &bq->usb_nb); 10068c2ecf20Sopenharmony_ci 10078c2ecf20Sopenharmony_ci return ret; 10088c2ecf20Sopenharmony_ci} 10098c2ecf20Sopenharmony_ci 10108c2ecf20Sopenharmony_cistatic int bq25890_remove(struct i2c_client *client) 10118c2ecf20Sopenharmony_ci{ 10128c2ecf20Sopenharmony_ci struct bq25890_device *bq = i2c_get_clientdata(client); 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_ci power_supply_unregister(bq->charger); 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(bq->usb_phy)) 10178c2ecf20Sopenharmony_ci usb_unregister_notifier(bq->usb_phy, &bq->usb_nb); 10188c2ecf20Sopenharmony_ci 10198c2ecf20Sopenharmony_ci /* reset all registers to default values */ 10208c2ecf20Sopenharmony_ci bq25890_chip_reset(bq); 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci return 0; 10238c2ecf20Sopenharmony_ci} 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 10268c2ecf20Sopenharmony_cistatic int bq25890_suspend(struct device *dev) 10278c2ecf20Sopenharmony_ci{ 10288c2ecf20Sopenharmony_ci struct bq25890_device *bq = dev_get_drvdata(dev); 10298c2ecf20Sopenharmony_ci 10308c2ecf20Sopenharmony_ci /* 10318c2ecf20Sopenharmony_ci * If charger is removed, while in suspend, make sure ADC is diabled 10328c2ecf20Sopenharmony_ci * since it consumes slightly more power. 10338c2ecf20Sopenharmony_ci */ 10348c2ecf20Sopenharmony_ci return bq25890_field_write(bq, F_CONV_RATE, 0); 10358c2ecf20Sopenharmony_ci} 10368c2ecf20Sopenharmony_ci 10378c2ecf20Sopenharmony_cistatic int bq25890_resume(struct device *dev) 10388c2ecf20Sopenharmony_ci{ 10398c2ecf20Sopenharmony_ci int ret; 10408c2ecf20Sopenharmony_ci struct bq25890_device *bq = dev_get_drvdata(dev); 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci mutex_lock(&bq->lock); 10438c2ecf20Sopenharmony_ci 10448c2ecf20Sopenharmony_ci ret = bq25890_get_chip_state(bq, &bq->state); 10458c2ecf20Sopenharmony_ci if (ret < 0) 10468c2ecf20Sopenharmony_ci goto unlock; 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ci /* Re-enable ADC only if charger is plugged in. */ 10498c2ecf20Sopenharmony_ci if (bq->state.online) { 10508c2ecf20Sopenharmony_ci ret = bq25890_field_write(bq, F_CONV_RATE, 1); 10518c2ecf20Sopenharmony_ci if (ret < 0) 10528c2ecf20Sopenharmony_ci goto unlock; 10538c2ecf20Sopenharmony_ci } 10548c2ecf20Sopenharmony_ci 10558c2ecf20Sopenharmony_ci /* signal userspace, maybe state changed while suspended */ 10568c2ecf20Sopenharmony_ci power_supply_changed(bq->charger); 10578c2ecf20Sopenharmony_ci 10588c2ecf20Sopenharmony_ciunlock: 10598c2ecf20Sopenharmony_ci mutex_unlock(&bq->lock); 10608c2ecf20Sopenharmony_ci 10618c2ecf20Sopenharmony_ci return ret; 10628c2ecf20Sopenharmony_ci} 10638c2ecf20Sopenharmony_ci#endif 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_cistatic const struct dev_pm_ops bq25890_pm = { 10668c2ecf20Sopenharmony_ci SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume) 10678c2ecf20Sopenharmony_ci}; 10688c2ecf20Sopenharmony_ci 10698c2ecf20Sopenharmony_cistatic const struct i2c_device_id bq25890_i2c_ids[] = { 10708c2ecf20Sopenharmony_ci { "bq25890", 0 }, 10718c2ecf20Sopenharmony_ci { "bq25892", 0 }, 10728c2ecf20Sopenharmony_ci { "bq25895", 0 }, 10738c2ecf20Sopenharmony_ci { "bq25896", 0 }, 10748c2ecf20Sopenharmony_ci {}, 10758c2ecf20Sopenharmony_ci}; 10768c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids); 10778c2ecf20Sopenharmony_ci 10788c2ecf20Sopenharmony_cistatic const struct of_device_id bq25890_of_match[] = { 10798c2ecf20Sopenharmony_ci { .compatible = "ti,bq25890", }, 10808c2ecf20Sopenharmony_ci { .compatible = "ti,bq25892", }, 10818c2ecf20Sopenharmony_ci { .compatible = "ti,bq25895", }, 10828c2ecf20Sopenharmony_ci { .compatible = "ti,bq25896", }, 10838c2ecf20Sopenharmony_ci { }, 10848c2ecf20Sopenharmony_ci}; 10858c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bq25890_of_match); 10868c2ecf20Sopenharmony_ci 10878c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI 10888c2ecf20Sopenharmony_cistatic const struct acpi_device_id bq25890_acpi_match[] = { 10898c2ecf20Sopenharmony_ci {"BQ258900", 0}, 10908c2ecf20Sopenharmony_ci {}, 10918c2ecf20Sopenharmony_ci}; 10928c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, bq25890_acpi_match); 10938c2ecf20Sopenharmony_ci#endif 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_cistatic struct i2c_driver bq25890_driver = { 10968c2ecf20Sopenharmony_ci .driver = { 10978c2ecf20Sopenharmony_ci .name = "bq25890-charger", 10988c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(bq25890_of_match), 10998c2ecf20Sopenharmony_ci .acpi_match_table = ACPI_PTR(bq25890_acpi_match), 11008c2ecf20Sopenharmony_ci .pm = &bq25890_pm, 11018c2ecf20Sopenharmony_ci }, 11028c2ecf20Sopenharmony_ci .probe = bq25890_probe, 11038c2ecf20Sopenharmony_ci .remove = bq25890_remove, 11048c2ecf20Sopenharmony_ci .id_table = bq25890_i2c_ids, 11058c2ecf20Sopenharmony_ci}; 11068c2ecf20Sopenharmony_cimodule_i2c_driver(bq25890_driver); 11078c2ecf20Sopenharmony_ci 11088c2ecf20Sopenharmony_ciMODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>"); 11098c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("bq25890 charger driver"); 11108c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1111