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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) // BQ2515X Battery Charger Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3) // Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/regmap.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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #define BQ2515X_MANUFACTURER "Texas Instruments"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #define BQ2515X_STAT0		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #define BQ2515X_STAT1		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #define BQ2515X_STAT2		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #define BQ2515X_FLAG0		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #define BQ2515X_FLAG1		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define BQ2515X_FLAG2		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define BQ2515X_FLAG3		0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #define BQ2515X_MASK0		0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define BQ2515X_MASK1		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define BQ2515X_MASK2		0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define BQ2515X_MASK3		0x0a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define BQ2515X_VBAT_CTRL	0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define BQ2515X_ICHG_CTRL	0x13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define BQ2515X_PCHRGCTRL	0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define BQ2515X_TERMCTRL	0x15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #define BQ2515X_BUVLO		0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define BQ2515X_CHARGERCTRL0	0x17
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define BQ2515X_CHARGERCTRL1	0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define BQ2515X_ILIMCTRL	0x19
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define BQ2515X_LDOCTRL		0x1d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define BQ2515X_MRCTRL		0x30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define BQ2515X_ICCTRL0		0x35
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define BQ2515X_ICCTRL1		0x36
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define BQ2515X_ICCTRL2		0x37
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #define BQ2515X_ADCCTRL0	0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #define BQ2515X_ADCCTRL1	0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define BQ2515X_ADC_VBAT_M	0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define BQ2515X_ADC_VBAT_L	0x43
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define BQ2515X_ADC_TS_M	0x44
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define BQ2515X_ADC_TS_L	0x45
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #define BQ2515X_ADC_ICHG_M	0x46
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #define BQ2515X_ADC_ICHG_L	0x47
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define BQ2515X_ADC_ADCIN_M	0x48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define BQ2515X_ADC_ADCIN_L	0x49
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define BQ2515X_ADC_VIN_M	0x4a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define BQ2515X_ADC_VIN_L	0x4b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define BQ2515X_ADC_PMID_M	0x4c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define BQ2515X_ADC_PMID_L	0x4d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define BQ2515X_ADC_IIN_M	0x4e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define BQ2515X_ADC_IIN_L	0x4f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define BQ2515X_ADC_COMP1_M	0x52
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #define BQ2515X_ADC_COMP1_L	0X53
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define BQ2515X_ADC_COMP2_M	0X54
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #define BQ2515X_ADC_COMP2_L	0x55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define BQ2515X_ADC_COMP3_M	0x56
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define BQ2515X_ADC_COMP3_L	0x57
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define BQ2515X_ADC_READ_EN	0x58
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define BQ2515X_TS_FASTCHGCTRL	0x61
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define BQ2515X_TS_COLD		0x62
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define BQ2515X_TS_COOL		0x63
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define BQ2515X_TS_WARM		0x64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define BQ2515X_TS_HOT		0x65
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define BQ2515X_DEVICE_ID	0x6f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define BQ2515X_DEFAULT_ICHG_UA		10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define BQ25150_DEFAULT_ILIM_UA		100000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define BQ25155_DEFAULT_ILIM_UA		500000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define BQ2515X_DEFAULT_VBAT_REG_UV	4200000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define BQ2515X_DEFAULT_IPRECHARGE_UA	2500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define BQ2515X_DIVISOR				65536
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define BQ2515X_VBAT_BASE_VOLT			3600000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define BQ2515X_VBAT_REG_MAX			4600000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define BQ2515X_VBAT_REG_MIN			3600000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define BQ2515X_VBAT_STEP_UV			10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define BQ2515X_UV_FACTOR			1000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define BQ2515X_VBAT_MULTIPLIER			6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define BQ2515X_ICHG_DIVISOR			52429
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define BQ2515X_ICHG_CURR_STEP_THRESH_UA	318750
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define BQ2515X_ICHG_MIN_UA			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define BQ2515X_ICHG_MAX_UA			500000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define BQ2515X_ICHG_RNG_1B0_UA			1250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #define BQ2515X_ICHG_RNG_1B1_UA			2500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define BQ2515X_VLOWV_SEL_1B0_UV		3000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define BQ2515X_VLOWV_SEL_1B1_UV		2800000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA	18750
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA	37500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) #define BQ2515X_TWAKE2_MIN_US			1700000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define BQ2515X_TWAKE2_MAX_US			2300000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define BQ2515X_ILIM_150MA	0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #define BQ2515X_ILIM_MASK	0x7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define BQ2515X_ILIM_MIN	50000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) #define BQ2515X_ILIM_MAX	600000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) #define BQ2515X_HEALTH_MASK	0xf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #define BQ2515X_ICHGRNG_MASK	0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) #define BQ2515X_STAT0_MASK	0x0f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) #define BQ2515X_STAT1_MASK	0x1f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) #define BQ2515X_PRECHARGE_MASK	0x1f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) #define BQ2515X_TS_HOT_STAT		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) #define BQ2515X_TS_WARM_STAT		BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) #define BQ2515X_TS_COOL_STAT		BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) #define BQ2515X_TS_COLD_STAT		BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) #define BQ2515X_SAFETY_TIMER_EXP	BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) #define BQ2515X_EN_VBAT_READ		BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define BQ2515X_EN_ICHG_READ		BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define BQ2515X_VIN_GOOD		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define BQ2515X_CHRG_DONE		BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #define BQ2515X_CV_CHRG_MODE		BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #define BQ2515X_VIN_OVP_FAULT_STAT	BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define BQ2515X_WATCHDOG_DISABLE	BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) #define BQ2515X_ICHARGE_RANGE		BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define BQ2515X_VLOWV_SEL		BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) #define BQ2515X_CHARGER_DISABLE		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) #define BQ2515X_HWRESET_14S_WD		BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) static const int bq2515x_ilim_lvl_values[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	50000, 100000, 150000, 200000, 300000, 400000, 500000, 600000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) };
^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)  * struct bq2515x_init_data -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  * @ilim: input current limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  * @ichg: fast charge current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * @vbatreg: battery regulation voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  * @iprechg: precharge current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) struct bq2515x_init_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	int ilim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	int ichg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	int vbatreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	int iprechg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) enum bq2515x_id {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	BQ25150,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	BQ25155,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  * struct bq2515x_device -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  * @mains: mains properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158)  * @battery: battery properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  * @regmap: register map structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  * @dev: device structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  * @reset_gpio: manual reset (MR) pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  * @powerdown_gpio: low power mode pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * @ac_detect_gpio: power good (PG) pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  * @ce_gpio: charge enable (CE) pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * @model_name: string value describing device model
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  * @device_id: value of device_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  * @mains_online: boolean value indicating power supply online
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  * @init_data: charger initialization data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) struct bq2515x_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	struct power_supply *mains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	struct power_supply *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	struct gpio_desc *reset_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	struct gpio_desc *powerdown_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct gpio_desc *ac_detect_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	struct gpio_desc *ce_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	char model_name[I2C_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	int device_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	bool mains_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	struct bq2515x_init_data init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) static const struct reg_default bq25150_reg_defaults[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	{BQ2515X_FLAG0, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	{BQ2515X_FLAG1, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	{BQ2515X_FLAG2, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	{BQ2515X_FLAG3, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	{BQ2515X_MASK0, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	{BQ2515X_MASK1, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	{BQ2515X_MASK2, 0x71},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	{BQ2515X_MASK3, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	{BQ2515X_VBAT_CTRL, 0x3C},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	{BQ2515X_ICHG_CTRL, 0x8},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	{BQ2515X_PCHRGCTRL, 0x2},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	{BQ2515X_TERMCTRL, 0x14},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	{BQ2515X_BUVLO, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	{BQ2515X_CHARGERCTRL0, 0x82},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	{BQ2515X_CHARGERCTRL1, 0x42},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	{BQ2515X_ILIMCTRL, 0x1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	{BQ2515X_LDOCTRL, 0xB0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	{BQ2515X_MRCTRL, 0x2A},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	{BQ2515X_ICCTRL0, 0x10},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	{BQ2515X_ICCTRL1, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	{BQ2515X_ICCTRL2, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	{BQ2515X_ADCCTRL0, 0x2},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	{BQ2515X_ADCCTRL1, 0x40},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	{BQ2515X_ADC_COMP1_M, 0x23},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	{BQ2515X_ADC_COMP1_L, 0x20},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	{BQ2515X_ADC_COMP2_M, 0x38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	{BQ2515X_ADC_COMP2_L, 0x90},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	{BQ2515X_ADC_COMP3_M, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	{BQ2515X_ADC_COMP3_L, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	{BQ2515X_ADC_READ_EN, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	{BQ2515X_TS_FASTCHGCTRL, 0x34},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	{BQ2515X_TS_COLD, 0x7C},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	{BQ2515X_TS_COOL, 0x6D},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	{BQ2515X_TS_WARM, 0x38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	{BQ2515X_TS_HOT, 0x27},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	{BQ2515X_DEVICE_ID, 0x20},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) static const struct reg_default bq25155_reg_defaults[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	{BQ2515X_FLAG0, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	{BQ2515X_FLAG1, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	{BQ2515X_FLAG2, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	{BQ2515X_FLAG3, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	{BQ2515X_MASK0, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	{BQ2515X_MASK1, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	{BQ2515X_MASK2, 0x71},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	{BQ2515X_MASK3, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	{BQ2515X_VBAT_CTRL, 0x3C},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	{BQ2515X_ICHG_CTRL, 0x8},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	{BQ2515X_PCHRGCTRL, 0x2},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	{BQ2515X_TERMCTRL, 0x14},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	{BQ2515X_BUVLO, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	{BQ2515X_CHARGERCTRL0, 0x82},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	{BQ2515X_CHARGERCTRL1, 0xC2},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	{BQ2515X_ILIMCTRL, 0x6},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	{BQ2515X_LDOCTRL, 0xB0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	{BQ2515X_MRCTRL, 0x2A},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	{BQ2515X_ICCTRL0, 0x10},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	{BQ2515X_ICCTRL1, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	{BQ2515X_ICCTRL2, 0x40},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	{BQ2515X_ADCCTRL0, 0x2},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	{BQ2515X_ADCCTRL1, 0x40},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	{BQ2515X_ADC_COMP1_M, 0x23},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	{BQ2515X_ADC_COMP1_L, 0x20},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	{BQ2515X_ADC_COMP2_M, 0x38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	{BQ2515X_ADC_COMP2_L, 0x90},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	{BQ2515X_ADC_COMP3_M, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	{BQ2515X_ADC_COMP3_L, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	{BQ2515X_ADC_READ_EN, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	{BQ2515X_TS_FASTCHGCTRL, 0x34},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	{BQ2515X_TS_COLD, 0x7C},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	{BQ2515X_TS_COOL, 0x6D},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	{BQ2515X_TS_WARM, 0x38},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	{BQ2515X_TS_HOT, 0x27},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	{BQ2515X_DEVICE_ID, 0x35},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) static int bq2515x_wake_up(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	/* Read the STAT register if we can read it then the device is out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	 * of ship mode.  If the register cannot be read then attempt to wake
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	 * it up and enable the ADC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	/* Need to toggle LP and bring device out of ship mode. The device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	 * will exit the ship mode when the MR pin is held low for at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	 * t_WAKE2 as shown in section 8.3.7.1 of the datasheet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	gpiod_set_value_cansleep(bq2515x->powerdown_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	gpiod_set_value_cansleep(bq2515x->reset_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	usleep_range(BQ2515X_TWAKE2_MIN_US, BQ2515X_TWAKE2_MAX_US);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	gpiod_set_value_cansleep(bq2515x->reset_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	return regmap_write(bq2515x->regmap, BQ2515X_ADC_READ_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 				(BQ2515X_EN_VBAT_READ | BQ2515X_EN_ICHG_READ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) static int bq2515x_update_ps_status(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	bool dc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	if (bq2515x->ac_detect_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		val = gpiod_get_value_cansleep(bq2515x->ac_detect_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	dc = val & BQ2515X_VIN_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	ret = bq2515x->mains_online != dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	bq2515x->mains_online = dc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) static int bq2515x_disable_watchdog_timers(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	ret = regmap_update_bits(bq2515x->regmap, BQ2515X_CHARGERCTRL0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			BQ2515X_WATCHDOG_DISABLE, BQ2515X_WATCHDOG_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	return regmap_update_bits(bq2515x->regmap, BQ2515X_ICCTRL2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 						BQ2515X_HWRESET_14S_WD, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) static int bq2515x_get_battery_voltage_now(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	int vbat_msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	int vbat_lsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	uint32_t vbat_measurement;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	if (!bq2515x->mains_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		bq2515x_wake_up(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_VBAT_M, &vbat_msb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_VBAT_L, &vbat_lsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	vbat_measurement = (vbat_msb << 8) | vbat_lsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	return vbat_measurement * (BQ2515X_UV_FACTOR / BQ2515X_DIVISOR) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 						BQ2515X_VBAT_MULTIPLIER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) static int bq2515x_get_battery_current_now(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	int ichg_msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	int ichg_lsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	uint32_t ichg_measurement;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	unsigned int ichg_reg_code, reg_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	unsigned int icharge_range = 0, pchrgctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	unsigned int buvlo, vlowv_sel, vlowv = BQ2515X_VLOWV_SEL_1B0_UV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	if (!bq2515x->mains_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_ICHG_M, &ichg_msb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_ICHG_L, &ichg_lsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	ichg_measurement = (ichg_msb << 8) | ichg_lsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	ret = regmap_read(bq2515x->regmap, BQ2515X_BUVLO, &buvlo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	vlowv_sel = buvlo & BQ2515X_VLOWV_SEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	if (vlowv_sel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		vlowv = BQ2515X_VLOWV_SEL_1B1_UV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (bq2515x_get_battery_voltage_now(bq2515x) < vlowv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 								&pchrgctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		reg_code = pchrgctrl & BQ2515X_PRECHARGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		ret = regmap_read(bq2515x->regmap, BQ2515X_ICHG_CTRL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 							&ichg_reg_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		reg_code = ichg_reg_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	if (icharge_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	return reg_code * (ichg_multiplier * ichg_measurement /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 							BQ2515X_ICHG_DIVISOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) static bool bq2515x_get_charge_disable(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	int ce_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	int icctrl2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	int charger_disable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	ce_pin = gpiod_get_value_cansleep(bq2515x->ce_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	ret = regmap_read(bq2515x->regmap, BQ2515X_ICCTRL2, &icctrl2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	charger_disable = icctrl2 & BQ2515X_CHARGER_DISABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (charger_disable || ce_pin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) static int bq2515x_set_charge_disable(struct bq2515x_device *bq2515x, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	gpiod_set_value_cansleep(bq2515x->ce_gpio, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	return regmap_update_bits(bq2515x->regmap, BQ2515X_ICCTRL2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 					BQ2515X_CHARGER_DISABLE, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) static int bq2515x_get_const_charge_current(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	unsigned int ichg_reg_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	unsigned int pchrgctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	unsigned int icharge_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	ret = regmap_read(bq2515x->regmap, BQ2515X_ICHG_CTRL, &ichg_reg_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (icharge_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	return ichg_reg_code * ichg_multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static int bq2515x_set_const_charge_current(struct bq2515x_device *bq2515x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 								int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	unsigned int ichg_reg_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	unsigned int icharge_range = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	if (val > BQ2515X_ICHG_MAX_UA || val < BQ2515X_ICHG_MIN_UA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	if (val > BQ2515X_ICHG_CURR_STEP_THRESH_UA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		icharge_range = BQ2515X_ICHARGE_RANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	bq2515x_set_charge_disable(bq2515x, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	ret = regmap_update_bits(bq2515x->regmap, BQ2515X_PCHRGCTRL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 					BQ2515X_ICHARGE_RANGE, icharge_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	ichg_reg_code = val / ichg_multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	ret = regmap_write(bq2515x->regmap, BQ2515X_ICHG_CTRL, ichg_reg_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	return bq2515x_set_charge_disable(bq2515x, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) static int bq2515x_get_precharge_current(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	unsigned int pchrgctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	unsigned int icharge_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	u16 precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	unsigned int precharge_reg_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	if (icharge_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		precharge_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	precharge_reg_code = pchrgctrl & BQ2515X_PRECHARGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	return precharge_reg_code * precharge_multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) static int bq2515x_set_precharge_current(struct bq2515x_device *bq2515x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 					int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	unsigned int pchrgctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	unsigned int icharge_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	unsigned int precharge_reg_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	unsigned int precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	unsigned int precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	if (icharge_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		precharge_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (val > precharge_max_ua || val < BQ2515X_ICHG_MIN_UA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	precharge_reg_code = val / precharge_multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	ret = bq2515x_set_charge_disable(bq2515x, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	ret = regmap_update_bits(bq2515x->regmap, BQ2515X_PCHRGCTRL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 				BQ2515X_PRECHARGE_MASK, precharge_reg_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	return bq2515x_set_charge_disable(bq2515x, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) static int bq2515x_charging_status(struct bq2515x_device *bq2515x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 				   union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	bool status0_no_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	bool status1_no_fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	bool ce_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	bool charge_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	if (!bq2515x->mains_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		return ret;
^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) 	 * The code block below is used to determine if any faults from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	 * STAT0 register are disbaling charging or if the charge has completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	 * according to the CHARGE_DONE_STAT bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	if (((status & BQ2515X_STAT0_MASK) == true) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			((status & BQ2515X_CHRG_DONE) == false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		status0_no_fault = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		charge_done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	} else if (status & BQ2515X_CHRG_DONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		charge_done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		status0_no_fault = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		status0_no_fault = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		charge_done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	ret = regmap_read(bq2515x->regmap, BQ2515X_STAT1, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	 * The code block below is used to determine if any faults from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	 * STAT1 register are disbaling charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	if ((status & BQ2515X_STAT1_MASK) == false)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		status1_no_fault = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		status1_no_fault = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	ce_status = (!bq2515x_get_charge_disable(bq2515x));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	 * If there are no faults and charging is enabled, then status is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	 * charging. Otherwise, if charging is complete, then status is full.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	 * Otherwise, if a fault exists or charging is disabled, then status is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	 * not charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	if (status0_no_fault & status1_no_fault & ce_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	else if (charge_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	else if (!(status0_no_fault & status1_no_fault & ce_status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	return 0;
^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 bq2515x_get_batt_reg(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	int vbat_reg_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	ret = regmap_read(bq2515x->regmap, BQ2515X_VBAT_CTRL, &vbat_reg_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (ret)
^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) 	return BQ2515X_VBAT_BASE_VOLT + vbat_reg_code * BQ2515X_VBAT_STEP_UV;
^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) static int bq2515x_set_batt_reg(struct bq2515x_device *bq2515x, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	int vbat_reg_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (val > BQ2515X_VBAT_REG_MAX || val < BQ2515X_VBAT_REG_MIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	vbat_reg_code = (val - BQ2515X_VBAT_BASE_VOLT) / BQ2515X_VBAT_STEP_UV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	return regmap_write(bq2515x->regmap, BQ2515X_VBAT_CTRL, vbat_reg_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) static int bq2515x_get_ilim_lvl(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	int ilimctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	ret = regmap_read(bq2515x->regmap, BQ2515X_ILIMCTRL, &ilimctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	return bq2515x_ilim_lvl_values[ilimctrl & BQ2515X_ILIM_MASK];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) static int bq2515x_set_ilim_lvl(struct bq2515x_device *bq2515x, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	unsigned int array_size = ARRAY_SIZE(bq2515x_ilim_lvl_values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	for (i = array_size - 1; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		if (val >= bq2515x_ilim_lvl_values[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	return regmap_write(bq2515x->regmap, BQ2515X_ILIMCTRL, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) static int bq2515x_power_supply_property_is_writeable(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 					enum power_supply_property prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	switch (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	}
^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) static int bq2515x_charger_get_health(struct bq2515x_device *bq2515x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 				      union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	int health = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	unsigned int stat1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	unsigned int flag3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	if (!bq2515x->mains_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		bq2515x_wake_up(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	ret = regmap_read(bq2515x->regmap, BQ2515X_FLAG3, &flag3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	if (ret)
^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) 	ret = regmap_read(bq2515x->regmap, BQ2515X_STAT1, &stat1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (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) 	if (stat1 & BQ2515X_HEALTH_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		switch (stat1 & BQ2515X_HEALTH_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		case BQ2515X_TS_HOT_STAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			health = POWER_SUPPLY_HEALTH_HOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		case BQ2515X_TS_WARM_STAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			health = POWER_SUPPLY_HEALTH_WARM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		case BQ2515X_TS_COOL_STAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 			health = POWER_SUPPLY_HEALTH_COOL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		case BQ2515X_TS_COLD_STAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			health = POWER_SUPPLY_HEALTH_COLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			health = POWER_SUPPLY_HEALTH_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if (stat1 & BQ2515X_VIN_OVP_FAULT_STAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	if (flag3 & BQ2515X_SAFETY_TIMER_EXP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	val->intval = health;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) static int bq2515x_mains_set_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		enum power_supply_property prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		const union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	switch (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		ret = bq2515x_set_batt_reg(bq2515x, val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		ret = bq2515x_set_const_charge_current(bq2515x, val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		ret = bq2515x_set_ilim_lvl(bq2515x, val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		ret = bq2515x_set_precharge_current(bq2515x, val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) static int bq2515x_mains_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 				     enum power_supply_property prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	switch (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		ret = bq2515x_get_const_charge_current(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		val->intval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		ret = bq2515x_get_batt_reg(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		val->intval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		ret = bq2515x_get_precharge_current(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		val->intval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		val->intval = bq2515x->mains_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		ret = bq2515x_charger_get_health(bq2515x, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		ret = bq2515x_get_ilim_lvl(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		val->intval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		val->strval = bq2515x->model_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	case POWER_SUPPLY_PROP_MANUFACTURER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		val->strval = BQ2515X_MANUFACTURER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		ret = bq2515x_charging_status(bq2515x, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) static int bq2515x_battery_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 				       enum power_supply_property prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 				       union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	ret = bq2515x_update_ps_status(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	switch (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		ret = bq2515x->init_data.vbatreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		val->intval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		ret = bq2515x->init_data.ichg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		val->intval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		ret = bq2515x_get_battery_voltage_now(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		val->intval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		ret = bq2515x_get_battery_current_now(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		val->intval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) static const enum power_supply_property bq2515x_battery_properties[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) static const enum power_supply_property bq2515x_mains_properties[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	POWER_SUPPLY_PROP_MANUFACTURER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) static const struct power_supply_desc bq2515x_mains_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	.name			= "bq2515x-mains",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	.type			= POWER_SUPPLY_TYPE_MAINS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	.get_property		= bq2515x_mains_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	.set_property		= bq2515x_mains_set_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	.properties		= bq2515x_mains_properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	.num_properties		= ARRAY_SIZE(bq2515x_mains_properties),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	.property_is_writeable	= bq2515x_power_supply_property_is_writeable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) static const struct power_supply_desc bq2515x_battery_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	.name			= "bq2515x-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	.type			= POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	.get_property		= bq2515x_battery_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	.properties		= bq2515x_battery_properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	.num_properties		= ARRAY_SIZE(bq2515x_battery_properties),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	.property_is_writeable	= bq2515x_power_supply_property_is_writeable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) static int bq2515x_power_supply_register(struct bq2515x_device *bq2515x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		struct device *dev, struct power_supply_config psy_cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	bq2515x->mains = devm_power_supply_register(bq2515x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 						    &bq2515x_mains_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 						    &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	if (IS_ERR(bq2515x->mains))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	bq2515x->battery = devm_power_supply_register(bq2515x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 						      &bq2515x_battery_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 						      &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (IS_ERR(bq2515x->battery))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) static int bq2515x_hw_init(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	struct power_supply_battery_info bat_info = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	ret = bq2515x_disable_watchdog_timers(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	if (bq2515x->init_data.ilim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		ret = bq2515x_set_ilim_lvl(bq2515x, bq2515x->init_data.ilim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	ret = power_supply_get_battery_info(bq2515x->mains, &bat_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		dev_warn(bq2515x->dev, "battery info missing, default values will be applied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		bq2515x->init_data.ichg = BQ2515X_DEFAULT_ICHG_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		bq2515x->init_data.vbatreg = BQ2515X_DEFAULT_VBAT_REG_UV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		bq2515x->init_data.iprechg = BQ2515X_DEFAULT_IPRECHARGE_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		bq2515x->init_data.ichg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 				bat_info.constant_charge_current_max_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		bq2515x->init_data.vbatreg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 				bat_info.constant_charge_voltage_max_uv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		bq2515x->init_data.iprechg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 				bat_info.precharge_current_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	ret = bq2515x_set_const_charge_current(bq2515x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 						bq2515x->init_data.ichg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	ret = bq2515x_set_batt_reg(bq2515x, bq2515x->init_data.vbatreg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	return bq2515x_set_precharge_current(bq2515x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 						bq2515x->init_data.iprechg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) static int bq2515x_read_properties(struct bq2515x_device *bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	ret = device_property_read_u32(bq2515x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 				      "input-current-limit-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 				      &bq2515x->init_data.ilim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		switch (bq2515x->device_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		case BQ25150:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 			bq2515x->init_data.ilim = BQ25150_DEFAULT_ILIM_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		case BQ25155:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			bq2515x->init_data.ilim = BQ25155_DEFAULT_ILIM_UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	bq2515x->ac_detect_gpio = devm_gpiod_get_optional(bq2515x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 						   "ac-detect", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	if (IS_ERR(bq2515x->ac_detect_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		ret = PTR_ERR(bq2515x->ac_detect_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		dev_err(bq2515x->dev, "Failed to get ac detect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	bq2515x->reset_gpio = devm_gpiod_get_optional(bq2515x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 						   "reset", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (IS_ERR(bq2515x->reset_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		ret = PTR_ERR(bq2515x->reset_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		dev_err(bq2515x->dev, "Failed to get reset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		return ret;
^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) 	bq2515x->powerdown_gpio = devm_gpiod_get_optional(bq2515x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 						"powerdown", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	if (IS_ERR(bq2515x->powerdown_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		ret = PTR_ERR(bq2515x->powerdown_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		dev_err(bq2515x->dev, "Failed to get powerdown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	bq2515x->ce_gpio = devm_gpiod_get_optional(bq2515x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 						   "charge-enable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 						   GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	if (IS_ERR(bq2515x->ce_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		ret = PTR_ERR(bq2515x->ce_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		dev_err(bq2515x->dev, "Failed to get ce");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) static bool bq2515x_volatile_register(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	case BQ2515X_STAT0 ... BQ2515X_FLAG3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	case BQ2515X_ADC_VBAT_M ... BQ2515X_ADC_IIN_L:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) static const struct regmap_config bq25150_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	.max_register		= BQ2515X_DEVICE_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	.reg_defaults		= bq25150_reg_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	.num_reg_defaults	= ARRAY_SIZE(bq25150_reg_defaults),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	.cache_type		= REGCACHE_RBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	.volatile_reg		= bq2515x_volatile_register,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) static const struct regmap_config bq25155_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	.max_register		= BQ2515X_DEVICE_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	.reg_defaults		= bq25155_reg_defaults,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	.num_reg_defaults	= ARRAY_SIZE(bq25155_reg_defaults),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	.cache_type		= REGCACHE_RBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	.volatile_reg		= bq2515x_volatile_register,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) static int bq2515x_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			 const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	struct bq2515x_device *bq2515x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	struct power_supply_config charger_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	bq2515x = devm_kzalloc(dev, sizeof(*bq2515x), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (!bq2515x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	bq2515x->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	strncpy(bq2515x->model_name, id->name, I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	bq2515x->device_id = id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	switch (bq2515x->device_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	case BQ25150:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		bq2515x->regmap = devm_regmap_init_i2c(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 						&bq25150_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	case BQ25155:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		bq2515x->regmap = devm_regmap_init_i2c(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 						&bq25155_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	if (IS_ERR(bq2515x->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		dev_err(dev, "failed to allocate register map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		return PTR_ERR(bq2515x->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	i2c_set_clientdata(client, bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	charger_cfg.drv_data = bq2515x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	charger_cfg.of_node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	ret = bq2515x_read_properties(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		dev_err(dev, "Failed to read device tree properties %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 									ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	ret = bq2515x_power_supply_register(bq2515x, dev, charger_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		dev_err(dev, "failed to register power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	ret = bq2515x_hw_init(bq2515x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		dev_err(dev, "Cannot initialize the chip\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		return ret;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) static const struct i2c_device_id bq2515x_i2c_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	{ "bq25150", BQ25150, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	{ "bq25155", BQ25155, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) MODULE_DEVICE_TABLE(i2c, bq2515x_i2c_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) static const struct of_device_id bq2515x_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	{ .compatible = "ti,bq25150", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	{ .compatible = "ti,bq25155", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) MODULE_DEVICE_TABLE(of, bq2515x_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) static struct i2c_driver bq2515x_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		.name = "bq2515x-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		.of_match_table = bq2515x_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	.probe = bq2515x_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	.id_table = bq2515x_i2c_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) module_i2c_driver(bq2515x_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) MODULE_DESCRIPTION("BQ2515X charger driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) MODULE_LICENSE("GPL v2");