Orange Pi5 kernel

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

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * TI BQ25890 charger driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2015 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/usb/phy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define BQ25890_MANUFACTURER		"Texas Instruments"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define BQ25890_IRQ_PIN			"bq25890_irq"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define BQ25890_ID			3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define BQ25895_ID			7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define BQ25896_ID			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define SY6970_ID			1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) enum bq25890_chip_version {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	BQ25890,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	BQ25892,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	BQ25895,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	BQ25896,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	SY6970,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) static const char *const bq25890_chip_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	"BQ25890",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	"BQ25892",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	"BQ25895",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	"BQ25896",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	"SY6970",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) enum bq25890_fields {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	F_EN_HIZ, F_EN_ILIM, F_IILIM,				     /* Reg00 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	F_BHOT, F_BCOLD, F_VINDPM_OFS,				     /* Reg01 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN,	     /* Reg02 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	F_MIN_VBAT_SEL,						     /* Reg03 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	F_PUMPX_EN, F_ICHG,					     /* Reg04 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	F_IPRECHG, F_ITERM,					     /* Reg05 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	F_VREG, F_BATLOWV, F_VRECHG,				     /* Reg06 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	F_JEITA_ISET,						     /* Reg07 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	F_BATCMP, F_VCLAMP, F_TREG,				     /* Reg08 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN,	     /* Reg09 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI,			     /* Reg0A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	F_VSYS_STAT,						     /* Reg0B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	F_NTC_FAULT,						     /* Reg0C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	F_FORCE_VINDPM, F_VINDPM,				     /* Reg0D */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	F_THERM_STAT, F_BATV,					     /* Reg0E */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	F_SYSV,							     /* Reg0F */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	F_TSPCT,						     /* Reg10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	F_VBUS_GD, F_VBUSV,					     /* Reg11 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	F_ICHGR,						     /* Reg12 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM,			     /* Reg13 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV,   /* Reg14 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	F_MAX_FIELDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) /* initial field values, converted to register values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) struct bq25890_init_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	u8 ichg;	/* charge current		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	u8 vreg;	/* regulation voltage		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	u8 iterm;	/* termination current		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	u8 iprechg;	/* precharge current		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	u8 sysvmin;	/* minimum system voltage limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	u8 boostv;	/* boost regulation voltage	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	u8 boosti;	/* boost current limit		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	u8 boostf;	/* boost frequency		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	u8 ilim_en;	/* enable ILIM pin		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	u8 treg;	/* thermal regulation threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	u8 rbatcomp;	/* IBAT sense resistor value    */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	u8 vclamp;	/* IBAT compensation voltage limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) struct bq25890_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	u8 online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	u8 chrg_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	u8 chrg_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	u8 vsys_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	u8 boost_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	u8 bat_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) struct bq25890_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct power_supply *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	struct usb_phy *usb_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct notifier_block usb_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	struct work_struct usb_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	unsigned long usb_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	struct gpio_desc *otg_mode_en_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	struct regulator_dev *otg_vbus_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	struct regmap *rmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	struct regmap_field *rmap_fields[F_MAX_FIELDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	enum bq25890_chip_version chip_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	struct bq25890_init_data init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	struct bq25890_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct workqueue_struct	*charger_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	struct delayed_work pd_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	struct notifier_block nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	struct device_node *notify_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	int pd_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	int pd_cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	struct mutex lock; /* protect state data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) static const struct regmap_range bq25890_readonly_reg_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	regmap_reg_range(0x0b, 0x0c),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	regmap_reg_range(0x0e, 0x13),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) static const struct regmap_access_table bq25890_writeable_regs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	.no_ranges = bq25890_readonly_reg_ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	.n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) static const struct regmap_range bq25890_volatile_reg_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	regmap_reg_range(0x00, 0x00),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	regmap_reg_range(0x02, 0x02),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	regmap_reg_range(0x09, 0x09),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	regmap_reg_range(0x0b, 0x14),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) static const struct regmap_access_table bq25890_volatile_regs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	.yes_ranges = bq25890_volatile_reg_ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	.n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) static const struct regmap_config bq25890_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	.max_register = 0x14,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	.cache_type = REGCACHE_RBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	.wr_table = &bq25890_writeable_regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	.volatile_table = &bq25890_volatile_regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) static const struct reg_field bq25890_reg_fields[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	/* REG00 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	[F_EN_HIZ]		= REG_FIELD(0x00, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	[F_EN_ILIM]		= REG_FIELD(0x00, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	[F_IILIM]		= REG_FIELD(0x00, 0, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	/* REG01 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	[F_BHOT]		= REG_FIELD(0x01, 6, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	[F_BCOLD]		= REG_FIELD(0x01, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	[F_VINDPM_OFS]		= REG_FIELD(0x01, 0, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	/* REG02 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	[F_CONV_START]		= REG_FIELD(0x02, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	[F_CONV_RATE]		= REG_FIELD(0x02, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	[F_BOOSTF]		= REG_FIELD(0x02, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	[F_ICO_EN]		= REG_FIELD(0x02, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	[F_HVDCP_EN]		= REG_FIELD(0x02, 3, 3),  // reserved on BQ25896
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	[F_MAXC_EN]		= REG_FIELD(0x02, 2, 2),  // reserved on BQ25896
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	[F_FORCE_DPM]		= REG_FIELD(0x02, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	[F_AUTO_DPDM_EN]	= REG_FIELD(0x02, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	/* REG03 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	[F_BAT_LOAD_EN]		= REG_FIELD(0x03, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	[F_WD_RST]		= REG_FIELD(0x03, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	[F_OTG_CFG]		= REG_FIELD(0x03, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	[F_CHG_CFG]		= REG_FIELD(0x03, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	[F_SYSVMIN]		= REG_FIELD(0x03, 1, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	[F_MIN_VBAT_SEL]	= REG_FIELD(0x03, 0, 0), // BQ25896 only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	/* REG04 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	[F_PUMPX_EN]		= REG_FIELD(0x04, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	[F_ICHG]		= REG_FIELD(0x04, 0, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	/* REG05 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	[F_IPRECHG]		= REG_FIELD(0x05, 4, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	[F_ITERM]		= REG_FIELD(0x05, 0, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	/* REG06 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	[F_VREG]		= REG_FIELD(0x06, 2, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	[F_BATLOWV]		= REG_FIELD(0x06, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	[F_VRECHG]		= REG_FIELD(0x06, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	/* REG07 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	[F_TERM_EN]		= REG_FIELD(0x07, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	[F_STAT_DIS]		= REG_FIELD(0x07, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	[F_WD]			= REG_FIELD(0x07, 4, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	[F_TMR_EN]		= REG_FIELD(0x07, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	[F_CHG_TMR]		= REG_FIELD(0x07, 1, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	[F_JEITA_ISET]		= REG_FIELD(0x07, 0, 0), // reserved on BQ25895
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	/* REG08 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	[F_BATCMP]		= REG_FIELD(0x08, 5, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	[F_VCLAMP]		= REG_FIELD(0x08, 2, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	[F_TREG]		= REG_FIELD(0x08, 0, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	/* REG09 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	[F_FORCE_ICO]		= REG_FIELD(0x09, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	[F_TMR2X_EN]		= REG_FIELD(0x09, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	[F_BATFET_DIS]		= REG_FIELD(0x09, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	[F_JEITA_VSET]		= REG_FIELD(0x09, 4, 4), // reserved on BQ25895
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	[F_BATFET_DLY]		= REG_FIELD(0x09, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	[F_BATFET_RST_EN]	= REG_FIELD(0x09, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	[F_PUMPX_UP]		= REG_FIELD(0x09, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	[F_PUMPX_DN]		= REG_FIELD(0x09, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	/* REG0A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	[F_BOOSTV]		= REG_FIELD(0x0A, 4, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	[F_BOOSTI]		= REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	[F_PFM_OTG_DIS]		= REG_FIELD(0x0A, 3, 3), // BQ25896 only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	/* REG0B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	[F_VBUS_STAT]		= REG_FIELD(0x0B, 5, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	[F_CHG_STAT]		= REG_FIELD(0x0B, 3, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	[F_PG_STAT]		= REG_FIELD(0x0B, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	[F_SDP_STAT]		= REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	[F_VSYS_STAT]		= REG_FIELD(0x0B, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	/* REG0C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	[F_WD_FAULT]		= REG_FIELD(0x0C, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	[F_BOOST_FAULT]		= REG_FIELD(0x0C, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	[F_CHG_FAULT]		= REG_FIELD(0x0C, 4, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	[F_BAT_FAULT]		= REG_FIELD(0x0C, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	[F_NTC_FAULT]		= REG_FIELD(0x0C, 0, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	/* REG0D */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	[F_FORCE_VINDPM]	= REG_FIELD(0x0D, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	[F_VINDPM]		= REG_FIELD(0x0D, 0, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	/* REG0E */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	[F_THERM_STAT]		= REG_FIELD(0x0E, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	[F_BATV]		= REG_FIELD(0x0E, 0, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	/* REG0F */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	[F_SYSV]		= REG_FIELD(0x0F, 0, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	/* REG10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	[F_TSPCT]		= REG_FIELD(0x10, 0, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	/* REG11 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	[F_VBUS_GD]		= REG_FIELD(0x11, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	[F_VBUSV]		= REG_FIELD(0x11, 0, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	/* REG12 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	[F_ICHGR]		= REG_FIELD(0x12, 0, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	/* REG13 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	[F_VDPM_STAT]		= REG_FIELD(0x13, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	[F_IDPM_STAT]		= REG_FIELD(0x13, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	[F_IDPM_LIM]		= REG_FIELD(0x13, 0, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	/* REG14 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	[F_REG_RST]		= REG_FIELD(0x14, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	[F_ICO_OPTIMIZED]	= REG_FIELD(0x14, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	[F_PN]			= REG_FIELD(0x14, 3, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	[F_TS_PROFILE]		= REG_FIELD(0x14, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	[F_DEV_REV]		= REG_FIELD(0x14, 0, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264)  * Most of the val -> idx conversions can be computed, given the minimum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  * maximum and the step between values. For the rest of conversions, we use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * lookup tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) enum bq25890_table_ids {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	/* range tables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	TBL_ICHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	TBL_ITERM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	TBL_IILIM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	TBL_VREG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	TBL_BOOSTV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	TBL_SYSVMIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	TBL_VBATCOMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	TBL_RBATCOMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	TBL_VINDPM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	/* lookup tables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	TBL_TREG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	TBL_BOOSTI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) /* Thermal Regulation Threshold lookup table, in degrees Celsius */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) #define BQ25890_TREG_TBL_SIZE		ARRAY_SIZE(bq25890_treg_tbl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) /* Boost mode current limit lookup table, in uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) static const u32 bq25890_boosti_tbl[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) #define BQ25890_BOOSTI_TBL_SIZE		ARRAY_SIZE(bq25890_boosti_tbl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) struct bq25890_range {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	u32 min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	u32 max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	u32 step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) struct bq25890_lookup {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	const u32 *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) static const union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	struct bq25890_range  rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	struct bq25890_lookup lt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) } bq25890_tables[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	/* range tables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	/* TODO: BQ25896 has max ICHG 3008 mA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	[TBL_ICHG] =	{ .rt = {0,	  5056000, 64000} },	 /* uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	[TBL_ITERM] =	{ .rt = {64000,   1024000, 64000} },	 /* uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	[TBL_IILIM] =   { .rt = {100000,  3250000, 50000} },	 /* uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	[TBL_VREG] =	{ .rt = {3840000, 4608000, 16000} },	 /* uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	[TBL_BOOSTV] =	{ .rt = {4550000, 5510000, 64000} },	 /* uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	[TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} },	 /* uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	[TBL_VBATCOMP] ={ .rt = {0,        224000, 32000} },	 /* uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	[TBL_RBATCOMP] ={ .rt = {0,        140000, 20000} },	 /* uOhm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	[TBL_VINDPM] =  { .rt = {100000,  3100000, 100000} },	 /* uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	/* lookup tables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	[TBL_TREG] =	{ .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	[TBL_BOOSTI] =	{ .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static int bq25890_field_read(struct bq25890_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 			      enum bq25890_fields field_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	ret = regmap_field_read(bq->rmap_fields[field_id], &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) static int bq25890_field_write(struct bq25890_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			       enum bq25890_fields field_id, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	return regmap_field_write(bq->rmap_fields[field_id], val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	u8 idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	if (id >= TBL_TREG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		const u32 *tbl = bq25890_tables[id].lt.tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		u32 tbl_size = bq25890_tables[id].lt.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		u8 rtbl_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		for (idx = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		     idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		     idx++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	return idx - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	const struct bq25890_range *rtbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	/* lookup table? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	if (id >= TBL_TREG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		return bq25890_tables[id].lt.tbl[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	/* range table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	rtbl = &bq25890_tables[id].rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	return (rtbl->min + idx * rtbl->step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) enum bq25890_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	STATUS_NOT_CHARGING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	STATUS_PRE_CHARGING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	STATUS_FAST_CHARGING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	STATUS_TERMINATION_DONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) enum bq25890_chrg_fault {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	CHRG_FAULT_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	CHRG_FAULT_INPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	CHRG_FAULT_THERMAL_SHUTDOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	CHRG_FAULT_TIMER_EXPIRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) static bool bq25890_is_adc_property(enum power_supply_property psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) static int bq25890_power_supply_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 					     enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 					     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	struct bq25890_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	struct bq25890_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	bool do_adc_conv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	mutex_lock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	/* update state in case we lost an interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	__bq25890_handle_irq(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	state = bq->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	do_adc_conv = !state.online && bq25890_is_adc_property(psp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	if (do_adc_conv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		bq25890_field_write(bq, F_CONV_START, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	mutex_unlock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (do_adc_conv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			ret, !ret, 25000, 1000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		if (!state.online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		else if (state.chrg_status == STATUS_NOT_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		else if (state.chrg_status == STATUS_PRE_CHARGING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			 state.chrg_status == STATUS_FAST_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		else if (state.chrg_status == STATUS_TERMINATION_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		    state.chrg_status == STATUS_TERMINATION_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		else if (state.chrg_status == STATUS_PRE_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		else if (state.chrg_status == STATUS_FAST_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		else /* unreachable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 			val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	case POWER_SUPPLY_PROP_MANUFACTURER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		val->strval = BQ25890_MANUFACTURER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		val->strval = bq25890_chip_name[bq->chip_version];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		val->intval = state.online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		else if (state.bat_fault)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		if (!state.online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 			val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		ret = bq25890_field_read(bq, F_BATV); /* read measured value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		val->intval = 2304000 + ret * 20000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		ret = bq25890_field_read(bq, F_IILIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		val->intval = bq25890_find_val(ret, TBL_IILIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		val->intval = 2304000 + ret * 20000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		/* converted_val = ADC_val * 50mA (table 10.3.19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		val->intval = ret * -50000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) static int bq25890_get_chip_state(struct bq25890_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 				  struct bq25890_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		enum bq25890_fields id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	} state_fields[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		{F_CHG_STAT,	&state->chrg_status},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		{F_PG_STAT,	&state->online},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		{F_VSYS_STAT,	&state->vsys_status},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		{F_BOOST_FAULT, &state->boost_fault},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		{F_BAT_FAULT,	&state->bat_fault},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		{F_CHG_FAULT,	&state->chrg_fault}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		ret = bq25890_field_read(bq, state_fields[i].id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		*state_fields[i].data = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		state->chrg_status, state->online, state->vsys_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		state->chrg_fault, state->boost_fault, state->bat_fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	struct bq25890_state new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	ret = bq25890_get_chip_state(bq, &new_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	if (!new_state.online && bq->state.online) {	    /* power removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		/* disable ADC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		ret = bq25890_field_write(bq, F_CONV_RATE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	} else if (new_state.online && !bq->state.online) { /* power inserted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		/* enable ADC, to have control of charge current/voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		ret = bq25890_field_write(bq, F_CONV_RATE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	bq->state = new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	power_supply_changed(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	dev_err(bq->dev, "Error communicating with the chip: %pe\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		ERR_PTR(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	struct bq25890_device *bq = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	irqreturn_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	mutex_lock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	ret = __bq25890_handle_irq(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	mutex_unlock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) static int bq25890_chip_reset(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	int rst_check_counter = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	ret = bq25890_field_write(bq, F_REG_RST, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		ret = bq25890_field_read(bq, F_REG_RST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		usleep_range(5, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	} while (ret == 1 && --rst_check_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	if (!rst_check_counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) static int bq25890_hw_init(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		enum bq25890_fields id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	} init_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		{F_ICHG,	 bq->init_data.ichg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		{F_VREG,	 bq->init_data.vreg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		{F_ITERM,	 bq->init_data.iterm},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		{F_IPRECHG,	 bq->init_data.iprechg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		{F_SYSVMIN,	 bq->init_data.sysvmin},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		{F_BOOSTV,	 bq->init_data.boostv},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		{F_BOOSTI,	 bq->init_data.boosti},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		{F_BOOSTF,	 bq->init_data.boostf},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		{F_EN_ILIM,	 bq->init_data.ilim_en},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		{F_TREG,	 bq->init_data.treg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		{F_BATCMP,	 bq->init_data.rbatcomp},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		{F_VCLAMP,	 bq->init_data.vclamp},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	ret = bq25890_chip_reset(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		dev_dbg(bq->dev, "Reset failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	/* disable watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	ret = bq25890_field_write(bq, F_WD, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	/* initialize currents/voltages and other parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		ret = bq25890_field_write(bq, init_data[i].id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 					  init_data[i].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			dev_dbg(bq->dev, "Writing init data failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	/* Configure ADC for continuous conversions when charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	ret = bq25890_get_chip_state(bq, &bq->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		dev_dbg(bq->dev, "Get state failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) static const enum power_supply_property bq25890_power_supply_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	POWER_SUPPLY_PROP_MANUFACTURER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	POWER_SUPPLY_PROP_CHARGE_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) static char *bq25890_charger_supplied_to[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	"main-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) static const struct power_supply_desc bq25890_power_supply_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	.name = "bq25890-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	.type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	.properties = bq25890_power_supply_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	.num_properties = ARRAY_SIZE(bq25890_power_supply_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	.get_property = bq25890_power_supply_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) static int bq25890_power_supply_init(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	struct power_supply_config psy_cfg = { .drv_data = bq, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	psy_cfg.supplied_to = bq25890_charger_supplied_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	psy_cfg.of_node = bq->dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	bq->charger = power_supply_register(bq->dev, &bq25890_power_supply_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 					    &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	return PTR_ERR_OR_ZERO(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) static void bq25890_usb_work(struct work_struct *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	struct bq25890_device *bq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			container_of(data, struct bq25890_device, usb_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	switch (bq->usb_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	case USB_EVENT_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		/* Enable boost mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		ret = bq25890_field_write(bq, F_OTG_CFG, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	case USB_EVENT_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		/* Disable boost mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		ret = bq25890_field_write(bq, F_OTG_CFG, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		power_supply_changed(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	dev_err(bq->dev, "Error switching to boost/charger mode.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 				void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	struct bq25890_device *bq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			container_of(nb, struct bq25890_device, usb_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	bq->usb_event = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	queue_work(system_power_efficient_wq, &bq->usb_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) static int bq25890_get_chip_version(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	int id, rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	id = bq25890_field_read(bq, F_PN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		dev_err(bq->dev, "Cannot read chip ID.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	rev = bq25890_field_read(bq, F_DEV_REV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	if (rev < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		dev_err(bq->dev, "Cannot read chip revision.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		return rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	switch (id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	case BQ25890_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		bq->chip_version = BQ25890;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	/* BQ25892 and BQ25896 share same ID 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	case BQ25896_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		switch (rev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			bq->chip_version = BQ25896;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 			bq->chip_version = BQ25892;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			dev_err(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 				"Unknown device revision %d, assume BQ25892\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 				rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			bq->chip_version = BQ25892;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	case BQ25895_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		bq->chip_version = BQ25895;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	case SY6970_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		bq->chip_version = SY6970;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		dev_err(bq->dev, "Unknown chip ID %d\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) static int bq25890_irq_probe(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	struct gpio_desc *irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	if (IS_ERR(irq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		dev_err(bq->dev, "Could not probe irq pin.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		return PTR_ERR(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	return gpiod_to_irq(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	u32 property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	struct bq25890_init_data *init = &bq->init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		bool optional;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		enum bq25890_table_ids tbl_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		u8 *conv_data; /* holds converted value from given property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	} props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		/* required properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		{"ti,charge-current", false, TBL_ICHG, &init->ichg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		{"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		{"ti,termination-current", false, TBL_ITERM, &init->iterm},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		{"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		{"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		{"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		{"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		/* optional properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		{"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		{"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		{"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	/* initialize data for optional properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	init->treg = 3; /* 120 degrees Celsius */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	for (i = 0; i < ARRAY_SIZE(props); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		ret = device_property_read_u32(bq->dev, props[i].name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 					       &property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 			if (props[i].optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			dev_err(bq->dev, "Unable to read property %d %s\n", ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 				props[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		*props[i].conv_data = bq25890_find_idx(property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 						       props[i].tbl_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) static int bq25890_fw_probe(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	struct bq25890_init_data *init = &bq->init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	ret = bq25890_fw_read_u32_props(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	bq->notify_node = of_parse_phandle(bq->dev->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 					   "ti,usb-charger-detection", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	bq->otg_mode_en_io =  devm_gpiod_get_optional(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 						      "otg-mode-en",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 						      GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (!IS_ERR_OR_NULL(bq->otg_mode_en_io))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		gpiod_direction_output(bq->otg_mode_en_io, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) static void bq25890_set_pd_param(struct bq25890_device *bq, int vol, int cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	int vindpm, iilim, ichg, vol_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	iilim = bq25890_find_idx(cur, TBL_IILIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	ichg = bq25890_find_idx(cur, TBL_ICHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	vol_limit = vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	if (vol < 5000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		vol_limit = 5000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	vol_limit = vol_limit - 1280000 - 3200000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	if (vol > 6000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		vol_limit /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	vindpm = bq25890_find_idx(vol_limit, TBL_VINDPM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	while (!bq25890_field_read(bq, F_PG_STAT) && i < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		msleep(500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	bq25890_field_write(bq, F_IILIM, iilim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	bq25890_field_write(bq, F_VINDPM_OFS, vindpm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	bq25890_field_write(bq, F_ICHG, ichg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	dev_info(bq->dev, "vol=%d cur=%d  INPUT_CURRENT:%x, INPUT_VOLTAGE:%x, CHARGE_CURRENT:%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		 vol, cur, iilim, vindpm, ichg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	bq25890_get_chip_state(bq, &bq->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	power_supply_changed(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) static int bq25890_pd_notifier_call(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 				    unsigned long val, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	struct bq25890_device *bq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		container_of(nb, struct bq25890_device, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	struct power_supply *psy = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	union power_supply_propval prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (val != PSY_EVENT_PROP_CHANGED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	/* Ignore event if it was not send by notify_node/notify_device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	if (bq->notify_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		if (!psy->dev.parent ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		    psy->dev.parent->of_node != bq->notify_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, &prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	/* online=0: USB out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	if (prop.intval == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		bq->pd_cur = 450000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		bq->pd_vol = 5000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		queue_delayed_work(bq->charger_wq, &bq->pd_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 				   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_NOW, &prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	bq->pd_cur = prop.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	if (bq->pd_cur > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 						&prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		bq->pd_vol = prop.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		queue_delayed_work(bq->charger_wq, &bq->pd_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 				   msecs_to_jiffies(100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) static void bq25890_pd_evt_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	struct bq25890_device *bq = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 						 struct bq25890_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 						 pd_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	bq25890_set_pd_param(bq, bq->pd_vol, bq->pd_cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) static int bq25890_register_pd_psy(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	struct power_supply *notify_psy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	union power_supply_propval prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	if (!bq->notify_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	bq->charger_wq = alloc_ordered_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 						 WQ_MEM_RECLAIM |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 						 WQ_FREEZABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 						 "bq25890-charge-wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	INIT_DELAYED_WORK(&bq->pd_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			  bq25890_pd_evt_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	bq->nb.notifier_call = bq25890_pd_notifier_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	ret = power_supply_reg_notifier(&bq->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	bq25890_field_write(bq, F_AUTO_DPDM_EN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (bq->nb.notifier_call) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		notify_psy = power_supply_get_by_phandle(bq->dev->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 						"ti,usb-charger-detection");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		if (IS_ERR_OR_NULL(notify_psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			dev_info(bq->dev, "bq25700 notify_psy is error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			notify_psy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	if (notify_psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		ret = power_supply_get_property(notify_psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 						POWER_SUPPLY_PROP_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 						&prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		bq->pd_cur = prop.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		ret = power_supply_get_property(notify_psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 						POWER_SUPPLY_PROP_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 						&prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		bq->pd_vol = prop.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		queue_delayed_work(bq->charger_wq, &bq->pd_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 				   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) static void bq25890_set_otg_vbus(struct bq25890_device *bq, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	if (!IS_ERR_OR_NULL(bq->otg_mode_en_io))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		gpiod_direction_output(bq->otg_mode_en_io, enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	bq25890_field_write(bq, F_OTG_CFG, enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) static int bq25890_otg_vbus_enable(struct regulator_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	struct bq25890_device *bq = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	bq25890_set_otg_vbus(bq, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) static int bq25890_otg_vbus_disable(struct regulator_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	struct bq25890_device *bq = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	bq25890_set_otg_vbus(bq, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) static int bq25890_otg_vbus_is_enabled(struct regulator_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	struct bq25890_device *bq = rdev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	int gpio_status = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	val = bq25890_field_read(bq, F_OTG_CFG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	if (!IS_ERR_OR_NULL(bq->otg_mode_en_io))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		gpio_status = gpiod_get_value(bq->otg_mode_en_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	return val && gpio_status ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) static const struct regulator_ops bq25890_otg_vbus_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	.enable = bq25890_otg_vbus_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	.disable = bq25890_otg_vbus_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	.is_enabled = bq25890_otg_vbus_is_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) static const struct regulator_desc bq25890_otg_vbus_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	.name = "otg-vbus",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	.of_match = "otg-vbus",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	.regulators_node = of_match_ptr("regulators"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	.ops = &bq25890_otg_vbus_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	.type = REGULATOR_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	.fixed_uV = 5000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	.n_voltages = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) static int bq25890_register_otg_vbus_regulator(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	struct regulator_config config = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	np = of_get_child_by_name(bq->dev->of_node, "regulators");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		dev_warn(bq->dev, "cannot find regulators node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	config.dev = bq->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	config.driver_data = bq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	bq->otg_vbus_reg = devm_regulator_register(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 						   &bq25890_otg_vbus_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 						   &config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	if (IS_ERR(bq->otg_vbus_reg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		return PTR_ERR(bq->otg_vbus_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) static int bq25890_otg_register(struct bq25890_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	/* OTG reporting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	bq->usb_phy = devm_usb_get_phy(bq->dev, USB_PHY_TYPE_USB2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	if (!IS_ERR_OR_NULL(bq->usb_phy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		INIT_WORK(&bq->usb_work, bq25890_usb_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		bq->usb_nb.notifier_call = bq25890_usb_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		usb_register_notifier(bq->usb_phy, &bq->usb_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	ret = bq25890_register_otg_vbus_regulator(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		dev_warn(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			 "Cannot register otg vbus regulator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		bq->otg_vbus_reg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) static int bq25890_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			 const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	struct bq25890_device *bq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	if (!bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	bq->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	bq->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	mutex_init(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	if (IS_ERR(bq->rmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		dev_err(dev, "failed to allocate register map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		return PTR_ERR(bq->rmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		const struct reg_field *reg_fields = bq25890_reg_fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 							     reg_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		if (IS_ERR(bq->rmap_fields[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			dev_err(dev, "cannot allocate regmap field\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			return PTR_ERR(bq->rmap_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	i2c_set_clientdata(client, bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	ret = bq25890_get_chip_version(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		dev_err(dev, "Cannot read chip ID or unknown chip.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (!dev->platform_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		ret = bq25890_fw_probe(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			dev_err(dev, "Cannot read device properties.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	ret = bq25890_hw_init(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		dev_err(dev, "Cannot initialize the chip.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	if (client->irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		client->irq = bq25890_irq_probe(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	if (client->irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		dev_err(dev, "No irq resource found.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		return client->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	bq25890_otg_register(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 					bq25890_irq_handler_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 					BQ25890_IRQ_PIN, bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		goto irq_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	ret = bq25890_power_supply_init(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		dev_err(dev, "Failed to register power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		goto irq_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	bq25890_register_pd_psy(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) irq_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	if (!IS_ERR_OR_NULL(bq->usb_phy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) static int bq25890_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	struct bq25890_device *bq = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	power_supply_unregister(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	if (!IS_ERR_OR_NULL(bq->usb_phy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	/* reset all registers to default values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	bq25890_chip_reset(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) static int bq25890_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	struct bq25890_device *bq = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	 * If charger is removed, while in suspend, make sure ADC is diabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	 * since it consumes slightly more power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	return bq25890_field_write(bq, F_CONV_RATE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) static int bq25890_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	struct bq25890_device *bq = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	mutex_lock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	ret = bq25890_get_chip_state(bq, &bq->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	/* Re-enable ADC only if charger is plugged in. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	if (bq->state.online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		ret = bq25890_field_write(bq, F_CONV_RATE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	/* signal userspace, maybe state changed while suspended */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	power_supply_changed(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	mutex_unlock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) static const struct dev_pm_ops bq25890_pm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) static const struct i2c_device_id bq25890_i2c_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	{ "bq25890", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	{ "bq25892", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	{ "bq25895", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	{ "bq25896", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	{ "sy6970", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) static const struct of_device_id bq25890_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	{ .compatible = "ti,bq25890", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	{ .compatible = "ti,bq25892", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	{ .compatible = "ti,bq25895", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	{ .compatible = "ti,bq25896", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	{ .compatible = "sy,sy6970", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) MODULE_DEVICE_TABLE(of, bq25890_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) static const struct acpi_device_id bq25890_acpi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	{"BQ258900", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) static struct i2c_driver bq25890_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		.name = "bq25890-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		.of_match_table = of_match_ptr(bq25890_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		.acpi_match_table = ACPI_PTR(bq25890_acpi_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		.pm = &bq25890_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	.probe = bq25890_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	.remove = bq25890_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	.id_table = bq25890_i2c_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) module_i2c_driver(bq25890_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) MODULE_DESCRIPTION("bq25890 charger driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) MODULE_LICENSE("GPL");