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 BQ24257 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)  * Datasheets:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * https://www.ti.com/product/bq24250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * https://www.ti.com/product/bq24251
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * https://www.ti.com/product/bq24257
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define BQ24257_REG_1			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define BQ24257_REG_2			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define BQ24257_REG_3			0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define BQ24257_REG_4			0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define BQ24257_REG_5			0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define BQ24257_REG_6			0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define BQ24257_REG_7			0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define BQ24257_MANUFACTURER		"Texas Instruments"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define BQ24257_PG_GPIO			"pg"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define BQ24257_ILIM_SET_DELAY		1000	/* msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * When adding support for new devices make sure that enum bq2425x_chip and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * bq2425x_chip_name[] always stay in sync!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) enum bq2425x_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	BQ24250,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	BQ24251,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	BQ24257,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static const char *const bq2425x_chip_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	"bq24250",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	"bq24251",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	"bq24257",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) enum bq24257_fields {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	F_WD_FAULT, F_WD_EN, F_STAT, F_FAULT,			    /* REG 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	F_RESET, F_IILIMIT, F_EN_STAT, F_EN_TERM, F_CE, F_HZ_MODE,  /* REG 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	F_VBAT, F_USB_DET,					    /* REG 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	F_ICHG, F_ITERM,					    /* REG 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	F_LOOP_STATUS, F_LOW_CHG, F_DPDM_EN, F_CE_STATUS, F_VINDPM, /* REG 5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	F_X2_TMR_EN, F_TMR, F_SYSOFF, F_TS_EN, F_TS_STAT,	    /* REG 6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	F_VOVP, F_CLR_VDP, F_FORCE_BATDET, F_FORCE_PTM,		    /* REG 7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	F_MAX_FIELDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) /* initial field values, converted from uV/uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) struct bq24257_init_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	u8 ichg;	/* charge current      */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	u8 vbat;	/* regulation voltage  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	u8 iterm;	/* termination current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	u8 iilimit;	/* input current limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	u8 vovp;	/* over voltage protection voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	u8 vindpm;	/* VDMP input threshold voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) struct bq24257_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	u8 fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	bool power_good;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) struct bq24257_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	struct power_supply *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	enum bq2425x_chip chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	struct regmap *rmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	struct regmap_field *rmap_fields[F_MAX_FIELDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	struct gpio_desc *pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	struct delayed_work iilimit_setup_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	struct bq24257_init_data init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct bq24257_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	struct mutex lock; /* protect state data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	bool iilimit_autoset_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) static bool bq24257_is_volatile_reg(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	case BQ24257_REG_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	case BQ24257_REG_4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) static const struct regmap_config bq24257_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	.max_register = BQ24257_REG_7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	.cache_type = REGCACHE_RBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	.volatile_reg = bq24257_is_volatile_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static const struct reg_field bq24257_reg_fields[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	/* REG 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	[F_WD_FAULT]		= REG_FIELD(BQ24257_REG_1, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	[F_WD_EN]		= REG_FIELD(BQ24257_REG_1, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	[F_STAT]		= REG_FIELD(BQ24257_REG_1, 4, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	[F_FAULT]		= REG_FIELD(BQ24257_REG_1, 0, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	/* REG 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	[F_RESET]		= REG_FIELD(BQ24257_REG_2, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	[F_IILIMIT]		= REG_FIELD(BQ24257_REG_2, 4, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	[F_EN_STAT]		= REG_FIELD(BQ24257_REG_2, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	[F_EN_TERM]		= REG_FIELD(BQ24257_REG_2, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	[F_CE]			= REG_FIELD(BQ24257_REG_2, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	[F_HZ_MODE]		= REG_FIELD(BQ24257_REG_2, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	/* REG 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	[F_VBAT]		= REG_FIELD(BQ24257_REG_3, 2, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	[F_USB_DET]		= REG_FIELD(BQ24257_REG_3, 0, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	/* REG 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	[F_ICHG]		= REG_FIELD(BQ24257_REG_4, 3, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	[F_ITERM]		= REG_FIELD(BQ24257_REG_4, 0, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	/* REG 5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	[F_LOOP_STATUS]		= REG_FIELD(BQ24257_REG_5, 6, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	[F_LOW_CHG]		= REG_FIELD(BQ24257_REG_5, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	[F_DPDM_EN]		= REG_FIELD(BQ24257_REG_5, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	[F_CE_STATUS]		= REG_FIELD(BQ24257_REG_5, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	[F_VINDPM]		= REG_FIELD(BQ24257_REG_5, 0, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	/* REG 6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	[F_X2_TMR_EN]		= REG_FIELD(BQ24257_REG_6, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	[F_TMR]			= REG_FIELD(BQ24257_REG_6, 5, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	[F_SYSOFF]		= REG_FIELD(BQ24257_REG_6, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	[F_TS_EN]		= REG_FIELD(BQ24257_REG_6, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	[F_TS_STAT]		= REG_FIELD(BQ24257_REG_6, 0, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	/* REG 7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	[F_VOVP]		= REG_FIELD(BQ24257_REG_7, 5, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	[F_CLR_VDP]		= REG_FIELD(BQ24257_REG_7, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	[F_FORCE_BATDET]	= REG_FIELD(BQ24257_REG_7, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	[F_FORCE_PTM]		= REG_FIELD(BQ24257_REG_7, 2, 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) static const u32 bq24257_vbat_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	4300000, 4320000, 4340000, 4360000, 4380000, 4400000, 4420000, 4440000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) #define BQ24257_VBAT_MAP_SIZE		ARRAY_SIZE(bq24257_vbat_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) static const u32 bq24257_ichg_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	1750000, 1800000, 1850000, 1900000, 1950000, 2000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) #define BQ24257_ICHG_MAP_SIZE		ARRAY_SIZE(bq24257_ichg_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) static const u32 bq24257_iterm_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) #define BQ24257_ITERM_MAP_SIZE		ARRAY_SIZE(bq24257_iterm_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) static const u32 bq24257_iilimit_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	100000, 150000, 500000, 900000, 1500000, 2000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) #define BQ24257_IILIMIT_MAP_SIZE	ARRAY_SIZE(bq24257_iilimit_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static const u32 bq24257_vovp_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	6000000, 6500000, 7000000, 8000000, 9000000, 9500000, 10000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	10500000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) #define BQ24257_VOVP_MAP_SIZE		ARRAY_SIZE(bq24257_vovp_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) static const u32 bq24257_vindpm_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	4200000, 4280000, 4360000, 4440000, 4520000, 4600000, 4680000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	4760000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) #define BQ24257_VINDPM_MAP_SIZE		ARRAY_SIZE(bq24257_vindpm_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) static int bq24257_field_read(struct bq24257_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			      enum bq24257_fields field_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	ret = regmap_field_read(bq->rmap_fields[field_id], &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) static int bq24257_field_write(struct bq24257_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			       enum bq24257_fields field_id, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	return regmap_field_write(bq->rmap_fields[field_id], val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) static u8 bq24257_find_idx(u32 value, const u32 *map, u8 map_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	u8 idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	for (idx = 1; idx < map_size; idx++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		if (value < map[idx])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	return idx - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) enum bq24257_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	STATUS_READY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	STATUS_CHARGE_IN_PROGRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	STATUS_CHARGE_DONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	STATUS_FAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) enum bq24257_fault {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	FAULT_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	FAULT_INPUT_OVP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	FAULT_INPUT_UVLO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	FAULT_SLEEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	FAULT_BAT_TS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	FAULT_BAT_OVP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	FAULT_TS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	FAULT_TIMER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	FAULT_NO_BAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	FAULT_ISET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	FAULT_INPUT_LDO_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) static int bq24257_get_input_current_limit(struct bq24257_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 					   union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	ret = bq24257_field_read(bq, F_IILIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	 * The "External ILIM" and "Production & Test" modes are not exposed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	 * through this driver and not being covered by the lookup table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	 * Should such a mode have become active let's return an error rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	 * than exceeding the bounds of the lookup table and returning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	 * garbage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	if (ret >= BQ24257_IILIMIT_MAP_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	val->intval = bq24257_iilimit_map[ret];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	return 0;
^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) static int bq24257_set_input_current_limit(struct bq24257_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 					const union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	 * Address the case where the user manually sets an input current limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	 * while the charger auto-detection mechanism is is active. In this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	 * case we want to abort and go straight to the user-specified value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if (bq->iilimit_autoset_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		cancel_delayed_work_sync(&bq->iilimit_setup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	return bq24257_field_write(bq, F_IILIMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 				   bq24257_find_idx(val->intval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 						    bq24257_iilimit_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 						    BQ24257_IILIMIT_MAP_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) static int bq24257_power_supply_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 					     enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 					     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	struct bq24257_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	mutex_lock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	state = bq->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	mutex_unlock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		if (!state.power_good)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		else if (state.status == STATUS_READY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		else if (state.status == STATUS_CHARGE_IN_PROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		else if (state.status == STATUS_CHARGE_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	case POWER_SUPPLY_PROP_MANUFACTURER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		val->strval = BQ24257_MANUFACTURER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		val->strval = bq2425x_chip_name[bq->chip];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		val->intval = state.power_good;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		switch (state.fault) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		case FAULT_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		case FAULT_INPUT_OVP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		case FAULT_BAT_OVP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		case FAULT_TS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		case FAULT_BAT_TS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		case FAULT_TIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 			val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		val->intval = bq24257_ichg_map[bq->init_data.ichg];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		val->intval = bq24257_ichg_map[BQ24257_ICHG_MAP_SIZE - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		val->intval = bq24257_vbat_map[bq->init_data.vbat];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		val->intval = bq24257_vbat_map[BQ24257_VBAT_MAP_SIZE - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		val->intval = bq24257_iterm_map[bq->init_data.iterm];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		return bq24257_get_input_current_limit(bq, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) static int bq24257_power_supply_set_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 					enum power_supply_property prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 					const union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	switch (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		return bq24257_set_input_current_limit(bq, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) static int bq24257_power_supply_property_is_writeable(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 					enum power_supply_property psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) static int bq24257_get_chip_state(struct bq24257_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 				  struct bq24257_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	ret = bq24257_field_read(bq, F_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	state->status = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	ret = bq24257_field_read(bq, F_FAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	state->fault = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	if (bq->pg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		state->power_good = !gpiod_get_value_cansleep(bq->pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		 * If we have a chip without a dedicated power-good GPIO or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		 * some other explicit bit that would provide this information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		 * assume the power is good if there is no supply related
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		 * fault - and not good otherwise. There is a possibility for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		 * other errors to mask that power in fact is not good but this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		 * is probably the best we can do here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		switch (state->fault) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		case FAULT_INPUT_OVP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		case FAULT_INPUT_UVLO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		case FAULT_INPUT_LDO_LOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 			state->power_good = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			state->power_good = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) static bool bq24257_state_changed(struct bq24257_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 				  struct bq24257_state *new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	mutex_lock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	ret = (bq->state.status != new_state->status ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	       bq->state.fault != new_state->fault ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	       bq->state.power_good != new_state->power_good);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	mutex_unlock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) enum bq24257_loop_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	LOOP_STATUS_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	LOOP_STATUS_IN_DPM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	LOOP_STATUS_IN_CURRENT_LIMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	LOOP_STATUS_THERMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) enum bq24257_in_ilimit {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	IILIMIT_100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	IILIMIT_150,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	IILIMIT_500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	IILIMIT_900,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	IILIMIT_1500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	IILIMIT_2000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	IILIMIT_EXT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	IILIMIT_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) enum bq24257_vovp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	VOVP_6000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	VOVP_6500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	VOVP_7000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	VOVP_8000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	VOVP_9000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	VOVP_9500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	VOVP_10000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	VOVP_10500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) enum bq24257_vindpm {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	VINDPM_4200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	VINDPM_4280,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	VINDPM_4360,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	VINDPM_4440,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	VINDPM_4520,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	VINDPM_4600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	VINDPM_4680,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	VINDPM_4760
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) enum bq24257_port_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	PORT_TYPE_DCP,		/* Dedicated Charging Port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	PORT_TYPE_CDP,		/* Charging Downstream Port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	PORT_TYPE_SDP,		/* Standard Downstream Port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	PORT_TYPE_NON_STANDARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) enum bq24257_safety_timer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	SAFETY_TIMER_45,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	SAFETY_TIMER_360,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	SAFETY_TIMER_540,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	SAFETY_TIMER_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) static int bq24257_iilimit_autoset(struct bq24257_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	int loop_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	int iilimit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	int port_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	const u8 new_iilimit[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		[PORT_TYPE_DCP] = IILIMIT_2000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		[PORT_TYPE_CDP] = IILIMIT_2000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		[PORT_TYPE_SDP] = IILIMIT_500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		[PORT_TYPE_NON_STANDARD] = IILIMIT_500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	ret = bq24257_field_read(bq, F_LOOP_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	loop_status = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	ret = bq24257_field_read(bq, F_IILIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	iilimit = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	 * All USB ports should be able to handle 500mA. If not, DPM will lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	 * the charging current to accommodate the power source. No need to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	 * a lower IILIMIT value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	if (loop_status == LOOP_STATUS_IN_DPM && iilimit == IILIMIT_500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	ret = bq24257_field_read(bq, F_USB_DET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	port_type = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	ret = bq24257_field_write(bq, F_IILIMIT, new_iilimit[port_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	ret = bq24257_field_write(bq, F_TMR, SAFETY_TIMER_360);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	ret = bq24257_field_write(bq, F_CLR_VDP, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	dev_dbg(bq->dev, "port/loop = %d/%d -> iilimit = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		port_type, loop_status, new_iilimit[port_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) static void bq24257_iilimit_setup_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	struct bq24257_device *bq = container_of(work, struct bq24257_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 						 iilimit_setup_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	bq24257_iilimit_autoset(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) static void bq24257_handle_state_change(struct bq24257_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 					struct bq24257_state *new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	struct bq24257_state old_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	mutex_lock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	old_state = bq->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	mutex_unlock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	 * Handle BQ2425x state changes observing whether the D+/D- based input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	 * current limit autoset functionality is enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (!new_state->power_good) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		dev_dbg(bq->dev, "Power removed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		if (bq->iilimit_autoset_enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			cancel_delayed_work_sync(&bq->iilimit_setup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 			/* activate D+/D- port detection algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 			ret = bq24257_field_write(bq, F_DPDM_EN, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		 * When power is removed always return to the default input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		 * current limit as configured during probe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		ret = bq24257_field_write(bq, F_IILIMIT, bq->init_data.iilimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	} else if (!old_state.power_good) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		dev_dbg(bq->dev, "Power inserted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		if (bq->iilimit_autoset_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			/* configure input current limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			schedule_delayed_work(&bq->iilimit_setup_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 				      msecs_to_jiffies(BQ24257_ILIM_SET_DELAY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	} else if (new_state->fault == FAULT_NO_BAT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		dev_warn(bq->dev, "Battery removed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	} else if (new_state->fault == FAULT_TIMER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		dev_err(bq->dev, "Safety timer expired! Battery dead?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) static irqreturn_t bq24257_irq_handler_thread(int irq, void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	struct bq24257_device *bq = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	struct bq24257_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	ret = bq24257_get_chip_state(bq, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	if (!bq24257_state_changed(bq, &state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	dev_dbg(bq->dev, "irq(state changed): status/fault/pg = %d/%d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		state.status, state.fault, state.power_good);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	bq24257_handle_state_change(bq, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	mutex_lock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	bq->state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	mutex_unlock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	power_supply_changed(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) static int bq24257_hw_init(struct bq24257_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	struct bq24257_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		int field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	} init_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		{F_ICHG, bq->init_data.ichg},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		{F_VBAT, bq->init_data.vbat},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		{F_ITERM, bq->init_data.iterm},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		{F_VOVP, bq->init_data.vovp},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		{F_VINDPM, bq->init_data.vindpm},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	 * Disable the watchdog timer to prevent the IC from going back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	 * default settings after 50 seconds of I2C inactivity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	ret = bq24257_field_write(bq, F_WD_EN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	/* configure the charge currents and voltages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		ret = bq24257_field_write(bq, init_data[i].field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 					  init_data[i].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	ret = bq24257_get_chip_state(bq, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	mutex_lock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	bq->state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	mutex_unlock(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (!bq->iilimit_autoset_enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		dev_dbg(bq->dev, "manually setting iilimit = %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 			bq->init_data.iilimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		/* program fixed input current limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		ret = bq24257_field_write(bq, F_IILIMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 					  bq->init_data.iilimit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	} else if (!state.power_good)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		/* activate D+/D- detection algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		ret = bq24257_field_write(bq, F_DPDM_EN, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	else if (state.fault != FAULT_NO_BAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		ret = bq24257_iilimit_autoset(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) static enum power_supply_property bq24257_power_supply_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	POWER_SUPPLY_PROP_MANUFACTURER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) static char *bq24257_charger_supplied_to[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	"main-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) static const struct power_supply_desc bq24257_power_supply_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	.name = "bq24257-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	.type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	.properties = bq24257_power_supply_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	.num_properties = ARRAY_SIZE(bq24257_power_supply_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	.get_property = bq24257_power_supply_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	.set_property = bq24257_power_supply_set_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	.property_is_writeable = bq24257_power_supply_property_is_writeable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) static ssize_t bq24257_show_ovp_voltage(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 					char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	return scnprintf(buf, PAGE_SIZE, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			 bq24257_vovp_map[bq->init_data.vovp]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) static ssize_t bq24257_show_in_dpm_voltage(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 					   struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 					   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	return scnprintf(buf, PAGE_SIZE, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 			 bq24257_vindpm_map[bq->init_data.vindpm]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) static ssize_t bq24257_sysfs_show_enable(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 					 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 					 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		ret = bq24257_field_read(bq, F_HZ_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		ret = bq24257_field_read(bq, F_SYSOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) static ssize_t bq24257_sysfs_set_enable(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 					const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 					size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	if (kstrtol(buf, 10, &val) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		ret = bq24257_field_write(bq, F_HZ_MODE, (bool)val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		ret = bq24257_field_write(bq, F_SYSOFF, (bool)val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) static DEVICE_ATTR(ovp_voltage, S_IRUGO, bq24257_show_ovp_voltage, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) static DEVICE_ATTR(in_dpm_voltage, S_IRUGO, bq24257_show_in_dpm_voltage, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		   bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) static DEVICE_ATTR(sysoff_enable, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		   bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) static struct attribute *bq24257_charger_sysfs_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	&dev_attr_ovp_voltage.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	&dev_attr_in_dpm_voltage.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	&dev_attr_high_impedance_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	&dev_attr_sysoff_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) ATTRIBUTE_GROUPS(bq24257_charger_sysfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) static int bq24257_power_supply_init(struct bq24257_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	struct power_supply_config psy_cfg = { .drv_data = bq, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	psy_cfg.attr_grp = bq24257_charger_sysfs_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	psy_cfg.supplied_to = bq24257_charger_supplied_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	psy_cfg.num_supplicants = ARRAY_SIZE(bq24257_charger_supplied_to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	bq->charger = devm_power_supply_register(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 						 &bq24257_power_supply_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 						 &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	return PTR_ERR_OR_ZERO(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) static void bq24257_pg_gpio_probe(struct bq24257_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	bq->pg = devm_gpiod_get_optional(bq->dev, BQ24257_PG_GPIO, GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	if (PTR_ERR(bq->pg) == -EPROBE_DEFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		dev_info(bq->dev, "probe retry requested for PG pin\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	} else if (IS_ERR(bq->pg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		dev_err(bq->dev, "error probing PG pin\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		bq->pg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	if (bq->pg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		dev_dbg(bq->dev, "probed PG pin = %d\n", desc_to_gpio(bq->pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) static int bq24257_fw_probe(struct bq24257_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	u32 property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	/* Required properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	ret = device_property_read_u32(bq->dev, "ti,charge-current", &property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	bq->init_data.ichg = bq24257_find_idx(property, bq24257_ichg_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 					      BQ24257_ICHG_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	ret = device_property_read_u32(bq->dev, "ti,battery-regulation-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 				       &property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	bq->init_data.vbat = bq24257_find_idx(property, bq24257_vbat_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 					      BQ24257_VBAT_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	ret = device_property_read_u32(bq->dev, "ti,termination-current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 				       &property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	bq->init_data.iterm = bq24257_find_idx(property, bq24257_iterm_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 					       BQ24257_ITERM_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	/* Optional properties. If not provided use reasonable default. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	ret = device_property_read_u32(bq->dev, "ti,current-limit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 				       &property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		bq->iilimit_autoset_enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		 * Explicitly set a default value which will be needed for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		 * devices that don't support the automatic setting of the input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		 * current limit through the charger type detection mechanism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		bq->init_data.iilimit = IILIMIT_500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		bq->init_data.iilimit =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 				bq24257_find_idx(property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 						 bq24257_iilimit_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 						 BQ24257_IILIMIT_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	ret = device_property_read_u32(bq->dev, "ti,ovp-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 				       &property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		bq->init_data.vovp = VOVP_6500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		bq->init_data.vovp = bq24257_find_idx(property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 						      bq24257_vovp_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 						      BQ24257_VOVP_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	ret = device_property_read_u32(bq->dev, "ti,in-dpm-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 				       &property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		bq->init_data.vindpm = VINDPM_4360;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		bq->init_data.vindpm =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 				bq24257_find_idx(property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 						 bq24257_vindpm_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 						 BQ24257_VINDPM_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) static int bq24257_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			 const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	const struct acpi_device_id *acpi_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	struct bq24257_device *bq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	if (!bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	bq->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	bq->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	if (ACPI_HANDLE(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		acpi_id = acpi_match_device(dev->driver->acpi_match_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 					    &client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		if (!acpi_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			dev_err(dev, "Failed to match ACPI device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		bq->chip = (enum bq2425x_chip)acpi_id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		bq->chip = (enum bq2425x_chip)id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	mutex_init(&bq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	bq->rmap = devm_regmap_init_i2c(client, &bq24257_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (IS_ERR(bq->rmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		dev_err(dev, "failed to allocate register map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		return PTR_ERR(bq->rmap);
^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) 	for (i = 0; i < ARRAY_SIZE(bq24257_reg_fields); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		const struct reg_field *reg_fields = bq24257_reg_fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 							     reg_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		if (IS_ERR(bq->rmap_fields[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			dev_err(dev, "cannot allocate regmap field\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			return PTR_ERR(bq->rmap_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	i2c_set_clientdata(client, bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	if (!dev->platform_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		ret = bq24257_fw_probe(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			dev_err(dev, "Cannot read device properties.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	 * The BQ24250 doesn't support the D+/D- based charger type detection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	 * used for the automatic setting of the input current limit setting so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	 * explicitly disable that feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	if (bq->chip == BQ24250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		bq->iilimit_autoset_enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	if (bq->iilimit_autoset_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		INIT_DELAYED_WORK(&bq->iilimit_setup_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 				  bq24257_iilimit_setup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	 * The BQ24250 doesn't have a dedicated Power Good (PG) pin so let's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	 * not probe for it and instead use a SW-based approach to determine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	 * the PG state. We also use a SW-based approach for all other devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	 * if the PG pin is either not defined or can't be probed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	if (bq->chip != BQ24250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		bq24257_pg_gpio_probe(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	if (PTR_ERR(bq->pg) == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		return PTR_ERR(bq->pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	else if (!bq->pg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		dev_info(bq->dev, "using SW-based power-good detection\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	/* reset all registers to defaults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	ret = bq24257_field_write(bq, F_RESET, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	 * Put the RESET bit back to 0, in cache. For some reason the HW always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 * returns 1 on this bit, so this is the only way to avoid resetting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	 * chip every time we update another field in this register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	ret = bq24257_field_write(bq, F_RESET, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	ret = bq24257_hw_init(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		dev_err(dev, "Cannot initialize the chip.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	ret = bq24257_power_supply_init(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		dev_err(dev, "Failed to register power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 					bq24257_irq_handler_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 					IRQF_TRIGGER_FALLING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 					bq2425x_chip_name[bq->chip], bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		dev_err(dev, "Failed to request IRQ #%d\n", client->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) static int bq24257_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	struct bq24257_device *bq = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (bq->iilimit_autoset_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		cancel_delayed_work_sync(&bq->iilimit_setup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	bq24257_field_write(bq, F_RESET, 1); /* reset to defaults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) static int bq24257_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	struct bq24257_device *bq = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	if (bq->iilimit_autoset_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		cancel_delayed_work_sync(&bq->iilimit_setup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	/* reset all registers to default (and activate standalone mode) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	ret = bq24257_field_write(bq, F_RESET, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		dev_err(bq->dev, "Cannot reset chip to standalone mode.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) static int bq24257_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	struct bq24257_device *bq = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	ret = regcache_drop_region(bq->rmap, BQ24257_REG_1, BQ24257_REG_7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	ret = bq24257_field_write(bq, F_RESET, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	ret = bq24257_hw_init(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		dev_err(bq->dev, "Cannot init chip after resume.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	/* signal userspace, maybe state changed while suspended */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	power_supply_changed(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) static const struct dev_pm_ops bq24257_pm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend, bq24257_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) static const struct i2c_device_id bq24257_i2c_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	{ "bq24250", BQ24250 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	{ "bq24251", BQ24251 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	{ "bq24257", BQ24257 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) MODULE_DEVICE_TABLE(i2c, bq24257_i2c_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) static const struct of_device_id bq24257_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	{ .compatible = "ti,bq24250", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	{ .compatible = "ti,bq24251", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	{ .compatible = "ti,bq24257", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) MODULE_DEVICE_TABLE(of, bq24257_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) static const struct acpi_device_id bq24257_acpi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	{ "BQ242500", BQ24250 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	{ "BQ242510", BQ24251 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	{ "BQ242570", BQ24257 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) MODULE_DEVICE_TABLE(acpi, bq24257_acpi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) static struct i2c_driver bq24257_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		.name = "bq24257-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		.of_match_table = of_match_ptr(bq24257_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		.acpi_match_table = ACPI_PTR(bq24257_acpi_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		.pm = &bq24257_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	.probe = bq24257_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	.remove = bq24257_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	.id_table = bq24257_i2c_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) module_i2c_driver(bq24257_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) MODULE_DESCRIPTION("bq24257 charger driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) MODULE_LICENSE("GPL");