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)  * Driver for Richtek RT9455WSC battery charger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2015 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/of_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/usb/phy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #define RT9455_MANUFACTURER			"Richtek"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #define RT9455_MODEL_NAME			"RT9455"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define RT9455_DRIVER_NAME			"rt9455-charger"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #define RT9455_IRQ_NAME				"interrupt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define RT9455_PWR_RDY_DELAY			1 /* 1 second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define RT9455_MAX_CHARGING_TIME		21600 /* 6 hrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define RT9455_BATT_PRESENCE_DELAY		60 /* 60 seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define RT9455_CHARGE_MODE			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define RT9455_BOOST_MODE			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define RT9455_FAULT				0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define RT9455_IAICR_100MA			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define RT9455_IAICR_500MA			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define RT9455_IAICR_NO_LIMIT			0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define RT9455_CHARGE_DISABLE			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define RT9455_CHARGE_ENABLE			0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #define RT9455_PWR_FAULT			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define RT9455_PWR_GOOD				0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define RT9455_REG_CTRL1			0x00 /* CTRL1 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define RT9455_REG_CTRL2			0x01 /* CTRL2 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #define RT9455_REG_CTRL3			0x02 /* CTRL3 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #define RT9455_REG_DEV_ID			0x03 /* DEV_ID reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define RT9455_REG_CTRL4			0x04 /* CTRL4 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define RT9455_REG_CTRL5			0x05 /* CTRL5 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define RT9455_REG_CTRL6			0x06 /* CTRL6 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define RT9455_REG_CTRL7			0x07 /* CTRL7 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define RT9455_REG_IRQ1				0x08 /* IRQ1 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define RT9455_REG_IRQ2				0x09 /* IRQ2 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define RT9455_REG_IRQ3				0x0A /* IRQ3 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define RT9455_REG_MASK1			0x0B /* MASK1 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define RT9455_REG_MASK2			0x0C /* MASK2 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #define RT9455_REG_MASK3			0x0D /* MASK3 reg address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) enum rt9455_fields {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	F_STAT, F_BOOST, F_PWR_RDY, F_OTG_PIN_POLARITY, /* CTRL1 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	F_IAICR, F_TE_SHDN_EN, F_HIGHER_OCP, F_TE, F_IAICR_INT, F_HIZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	F_OPA_MODE, /* CTRL2 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	F_VOREG, F_OTG_PL, F_OTG_EN, /* CTRL3 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	F_VENDOR_ID, F_CHIP_REV, /* DEV_ID reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	F_RST, /* CTRL4 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	F_TMR_EN, F_MIVR, F_IPREC, F_IEOC_PERCENTAGE, /* CTRL5 reg fields*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	F_IAICR_SEL, F_ICHRG, F_VPREC, /* CTRL6 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	F_BATD_EN, F_CHG_EN, F_VMREG, /* CTRL7 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	F_TSDI, F_VINOVPI, F_BATAB, /* IRQ1 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	F_CHRVPI, F_CHBATOVI, F_CHTERMI, F_CHRCHGI, F_CH32MI, F_CHTREGI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	F_CHMIVRI, /* IRQ2 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	F_BSTBUSOVI, F_BSTOLI, F_BSTLOWVI, F_BST32SI, /* IRQ3 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	F_TSDM, F_VINOVPIM, F_BATABM, /* MASK1 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	F_CHRVPIM, F_CHBATOVIM, F_CHTERMIM, F_CHRCHGIM, F_CH32MIM, F_CHTREGIM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	F_CHMIVRIM, /* MASK2 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	F_BSTVINOVIM, F_BSTOLIM, F_BSTLOWVIM, F_BST32SIM, /* MASK3 reg fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	F_MAX_FIELDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) static const struct reg_field rt9455_reg_fields[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	[F_STAT]		= REG_FIELD(RT9455_REG_CTRL1, 4, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	[F_BOOST]		= REG_FIELD(RT9455_REG_CTRL1, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	[F_PWR_RDY]		= REG_FIELD(RT9455_REG_CTRL1, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	[F_OTG_PIN_POLARITY]	= REG_FIELD(RT9455_REG_CTRL1, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	[F_IAICR]		= REG_FIELD(RT9455_REG_CTRL2, 6, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	[F_TE_SHDN_EN]		= REG_FIELD(RT9455_REG_CTRL2, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	[F_HIGHER_OCP]		= REG_FIELD(RT9455_REG_CTRL2, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	[F_TE]			= REG_FIELD(RT9455_REG_CTRL2, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	[F_IAICR_INT]		= REG_FIELD(RT9455_REG_CTRL2, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	[F_HIZ]			= REG_FIELD(RT9455_REG_CTRL2, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	[F_OPA_MODE]		= REG_FIELD(RT9455_REG_CTRL2, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	[F_VOREG]		= REG_FIELD(RT9455_REG_CTRL3, 2, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	[F_OTG_PL]		= REG_FIELD(RT9455_REG_CTRL3, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	[F_OTG_EN]		= REG_FIELD(RT9455_REG_CTRL3, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	[F_VENDOR_ID]		= REG_FIELD(RT9455_REG_DEV_ID, 4, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	[F_CHIP_REV]		= REG_FIELD(RT9455_REG_DEV_ID, 0, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	[F_RST]			= REG_FIELD(RT9455_REG_CTRL4, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	[F_TMR_EN]		= REG_FIELD(RT9455_REG_CTRL5, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	[F_MIVR]		= REG_FIELD(RT9455_REG_CTRL5, 4, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	[F_IPREC]		= REG_FIELD(RT9455_REG_CTRL5, 2, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	[F_IEOC_PERCENTAGE]	= REG_FIELD(RT9455_REG_CTRL5, 0, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	[F_IAICR_SEL]		= REG_FIELD(RT9455_REG_CTRL6, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	[F_ICHRG]		= REG_FIELD(RT9455_REG_CTRL6, 4, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	[F_VPREC]		= REG_FIELD(RT9455_REG_CTRL6, 0, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	[F_BATD_EN]		= REG_FIELD(RT9455_REG_CTRL7, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	[F_CHG_EN]		= REG_FIELD(RT9455_REG_CTRL7, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	[F_VMREG]		= REG_FIELD(RT9455_REG_CTRL7, 0, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	[F_TSDI]		= REG_FIELD(RT9455_REG_IRQ1, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	[F_VINOVPI]		= REG_FIELD(RT9455_REG_IRQ1, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	[F_BATAB]		= REG_FIELD(RT9455_REG_IRQ1, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	[F_CHRVPI]		= REG_FIELD(RT9455_REG_IRQ2, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	[F_CHBATOVI]		= REG_FIELD(RT9455_REG_IRQ2, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	[F_CHTERMI]		= REG_FIELD(RT9455_REG_IRQ2, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	[F_CHRCHGI]		= REG_FIELD(RT9455_REG_IRQ2, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	[F_CH32MI]		= REG_FIELD(RT9455_REG_IRQ2, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	[F_CHTREGI]		= REG_FIELD(RT9455_REG_IRQ2, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	[F_CHMIVRI]		= REG_FIELD(RT9455_REG_IRQ2, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	[F_BSTBUSOVI]		= REG_FIELD(RT9455_REG_IRQ3, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	[F_BSTOLI]		= REG_FIELD(RT9455_REG_IRQ3, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	[F_BSTLOWVI]		= REG_FIELD(RT9455_REG_IRQ3, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	[F_BST32SI]		= REG_FIELD(RT9455_REG_IRQ3, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	[F_TSDM]		= REG_FIELD(RT9455_REG_MASK1, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	[F_VINOVPIM]		= REG_FIELD(RT9455_REG_MASK1, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	[F_BATABM]		= REG_FIELD(RT9455_REG_MASK1, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	[F_CHRVPIM]		= REG_FIELD(RT9455_REG_MASK2, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	[F_CHBATOVIM]		= REG_FIELD(RT9455_REG_MASK2, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	[F_CHTERMIM]		= REG_FIELD(RT9455_REG_MASK2, 4, 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	[F_CHRCHGIM]		= REG_FIELD(RT9455_REG_MASK2, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	[F_CH32MIM]		= REG_FIELD(RT9455_REG_MASK2, 2, 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	[F_CHTREGIM]		= REG_FIELD(RT9455_REG_MASK2, 1, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	[F_CHMIVRIM]		= REG_FIELD(RT9455_REG_MASK2, 0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	[F_BSTVINOVIM]		= REG_FIELD(RT9455_REG_MASK3, 7, 7),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	[F_BSTOLIM]		= REG_FIELD(RT9455_REG_MASK3, 6, 6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	[F_BSTLOWVIM]		= REG_FIELD(RT9455_REG_MASK3, 5, 5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	[F_BST32SIM]		= REG_FIELD(RT9455_REG_MASK3, 3, 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) #define GET_MASK(fid)	(BIT(rt9455_reg_fields[fid].msb + 1) - \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			 BIT(rt9455_reg_fields[fid].lsb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  * Each array initialised below shows the possible real-world values for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  * group of bits belonging to RT9455 registers. The arrays are sorted in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  * ascending order. The index of each real-world value represents the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173)  * that is encoded in the group of bits belonging to RT9455 registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) /* REG06[6:4] (ICHRG) in uAh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static const int rt9455_ichrg_values[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	 500000,  650000,  800000,  950000, 1100000, 1250000, 1400000, 1550000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * When the charger is in charge mode, REG02[7:2] represent battery regulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  * voltage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) /* REG02[7:2] (VOREG) in uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) static const int rt9455_voreg_values[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  * When the charger is in boost mode, REG02[7:2] represent boost output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * voltage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) /* REG02[7:2] (Boost output voltage) in uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) static const int rt9455_boost_voltage_values[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) /* REG07[3:0] (VMREG) in uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) static const int rt9455_vmreg_values[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) /* REG05[5:4] (IEOC_PERCENTAGE) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) static const int rt9455_ieoc_percentage_values[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	10, 30, 20, 30
^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) /* REG05[1:0] (MIVR) in uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) static const int rt9455_mivr_values[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	4000000, 4250000, 4500000, 5000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) /* REG05[1:0] (IAICR) in uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) static const int rt9455_iaicr_values[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	100000, 500000, 1000000, 2000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) struct rt9455_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	struct i2c_client		*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	struct regmap			*regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	struct regmap_field		*regmap_fields[F_MAX_FIELDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	struct power_supply		*charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	struct usb_phy			*usb_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	struct notifier_block		nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	struct delayed_work		pwr_rdy_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct delayed_work		max_charging_time_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct delayed_work		batt_presence_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	u32				voreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	u32				boost_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)  * Iterate through each element of the 'tbl' array until an element whose value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251)  * is greater than v is found. Return the index of the respective element,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252)  * or the index of the last element in the array, if no such element is found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	 * No need to iterate until the last index in the table because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	 * if no element greater than v is found in the table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	 * or if only the last element is greater than v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	 * function returns the index of the last element.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	for (i = 0; i < tbl_size - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		if (v <= tbl[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	return (tbl_size - 1);
^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) static int rt9455_get_field_val(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 				enum rt9455_fields field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 				const int tbl[], int tbl_size, int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	unsigned int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	ret = regmap_field_read(info->regmap_fields[field], &v);
^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) 	v = (v >= tbl_size) ? (tbl_size - 1) : v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	*val = tbl[v];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) static int rt9455_set_field_val(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 				enum rt9455_fields field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 				const int tbl[], int tbl_size, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	unsigned int idx = rt9455_find_idx(tbl, tbl_size, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	return regmap_field_write(info->regmap_fields[field], idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) static int rt9455_register_reset(struct rt9455_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	unsigned int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	int ret, limit = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	ret = regmap_field_write(info->regmap_fields[F_RST], 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		dev_err(dev, "Failed to set RST bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	}
^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) 	 * To make sure that reset operation has finished, loop until RST bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	 * is set to 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		ret = regmap_field_read(info->regmap_fields[F_RST], &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			dev_err(dev, "Failed to read RST bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		usleep_range(10, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	} while (--limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	if (!limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	return 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) /* Charger power supply property routines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) static enum power_supply_property rt9455_charger_properties[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	POWER_SUPPLY_PROP_SCOPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	POWER_SUPPLY_PROP_MANUFACTURER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) static char *rt9455_charger_supplied_to[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	"main-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) static int rt9455_charger_get_status(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 				     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	unsigned int v, pwr_rdy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	ret = regmap_field_read(info->regmap_fields[F_PWR_RDY],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				&pwr_rdy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	 * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	 * STAT bits value must be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (!pwr_rdy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		dev_err(&info->client->dev, "Failed to read STAT bits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	switch (v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		 * If PWR_RDY bit is set, but STAT bits value is 0, the charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		 * may be in one of the following cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		 * 1. CHG_EN bit is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		 * 2. CHG_EN bit is 1 but the battery is not connected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		 * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) static int rt9455_charger_get_health(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 				     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	unsigned int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	val->intval = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		dev_err(dev, "Failed to read IRQ1 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	if (v & GET_MASK(F_TSDI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	if (v & GET_MASK(F_VINOVPI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	if (v & GET_MASK(F_BATAB)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		dev_err(dev, "Failed to read IRQ2 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	if (v & GET_MASK(F_CHBATOVI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	if (v & GET_MASK(F_CH32MI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		return 0;
^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) 	ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		dev_err(dev, "Failed to read IRQ3 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	if (v & GET_MASK(F_BSTBUSOVI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	if (v & GET_MASK(F_BSTOLI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
^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) 	if (v & GET_MASK(F_BSTLOWVI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (v & GET_MASK(F_BST32SI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		dev_err(dev, "Failed to read STAT bits\n");
^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) 	if (v == RT9455_FAULT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) static int rt9455_charger_get_battery_presence(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 					       union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	unsigned int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	ret = regmap_field_read(info->regmap_fields[F_BATAB], &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		dev_err(&info->client->dev, "Failed to read BATAB bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	 * Since BATAB is 1 when battery is NOT present and 0 otherwise,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	 * !BATAB is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	val->intval = !v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	return 0;
^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) static int rt9455_charger_get_online(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 				     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	unsigned int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	val->intval = (int)v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) static int rt9455_charger_get_current(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 				      union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	int curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	ret = rt9455_get_field_val(info, F_ICHRG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 				   rt9455_ichrg_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 				   ARRAY_SIZE(rt9455_ichrg_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 				   &curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		dev_err(&info->client->dev, "Failed to read ICHRG value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	val->intval = curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) static int rt9455_charger_get_current_max(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 					  union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	val->intval = rt9455_ichrg_values[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) static int rt9455_charger_get_voltage(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 				      union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	int voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	ret = rt9455_get_field_val(info, F_VOREG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 				   rt9455_voreg_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 				   ARRAY_SIZE(rt9455_voreg_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 				   &voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		dev_err(&info->client->dev, "Failed to read VOREG value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	val->intval = voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) static int rt9455_charger_get_voltage_max(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 					  union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	val->intval = rt9455_vmreg_values[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) static int rt9455_charger_get_term_current(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 					   union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	int ichrg, ieoc_percentage, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	ret = rt9455_get_field_val(info, F_ICHRG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 				   rt9455_ichrg_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 				   ARRAY_SIZE(rt9455_ichrg_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 				   &ichrg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		dev_err(dev, "Failed to read ICHRG value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 				   rt9455_ieoc_percentage_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 				   ARRAY_SIZE(rt9455_ieoc_percentage_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 				   &ieoc_percentage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		dev_err(dev, "Failed to read IEOC value\n");
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	val->intval = ichrg * ieoc_percentage / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) static int rt9455_charger_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 				       enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 				       union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	struct rt9455_info *info = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		return rt9455_charger_get_status(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		return rt9455_charger_get_health(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		return rt9455_charger_get_battery_presence(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		return rt9455_charger_get_online(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		return rt9455_charger_get_current(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		return rt9455_charger_get_current_max(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		return rt9455_charger_get_voltage(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		return rt9455_charger_get_voltage_max(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	case POWER_SUPPLY_PROP_SCOPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		return rt9455_charger_get_term_current(info, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		val->strval = RT9455_MODEL_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	case POWER_SUPPLY_PROP_MANUFACTURER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		val->strval = RT9455_MANUFACTURER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		return -ENODATA;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			  u32 ieoc_percentage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			  u32 mivr, u32 iaicr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	int idx, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	ret = rt9455_register_reset(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		dev_err(dev, "Power On Reset failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	/* Set TE bit in order to enable end of charge detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	ret = regmap_field_write(info->regmap_fields[F_TE], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		dev_err(dev, "Failed to set TE bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	/* Set TE_SHDN_EN bit in order to enable end of charge detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		dev_err(dev, "Failed to set TE_SHDN_EN bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	 * Set BATD_EN bit in order to enable battery detection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	 * when charging is done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		dev_err(dev, "Failed to set BATD_EN bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	 * Disable Safety Timer. In charge mode, this timer terminates charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	 * if no read or write via I2C is done within 32 minutes. This timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	 * avoids overcharging the baterry when the OS is not loaded and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	 * charger is connected to a power source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	 * In boost mode, this timer triggers BST32SI interrupt if no read or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	 * write via I2C is done within 32 seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	 * When the OS is loaded and the charger driver is inserted, it is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	 * delayed_work, named max_charging_time_work, to avoid overcharging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	 * the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		dev_err(dev, "Failed to disable Safety Timer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	/* Set ICHRG to value retrieved from device-specific data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	ret = rt9455_set_field_val(info, F_ICHRG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 				   rt9455_ichrg_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 				   ARRAY_SIZE(rt9455_ichrg_values), ichrg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		dev_err(dev, "Failed to set ICHRG value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	/* Set IEOC Percentage to value retrieved from device-specific data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 				   rt9455_ieoc_percentage_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 				   ARRAY_SIZE(rt9455_ieoc_percentage_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 				   ieoc_percentage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		dev_err(dev, "Failed to set IEOC Percentage value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	/* Set VOREG to value retrieved from device-specific data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	ret = rt9455_set_field_val(info, F_VOREG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 				   rt9455_voreg_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 				   ARRAY_SIZE(rt9455_voreg_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 				   info->voreg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		dev_err(dev, "Failed to set VOREG value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	/* Set VMREG value to maximum (4.45V). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	ret = rt9455_set_field_val(info, F_VMREG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 				   rt9455_vmreg_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				   ARRAY_SIZE(rt9455_vmreg_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 				   rt9455_vmreg_values[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		dev_err(dev, "Failed to set VMREG value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	 * Set MIVR to value retrieved from device-specific data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	 * If no value is specified, default value for MIVR is 4.5V.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	if (mivr == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		mivr = 4500000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	ret = rt9455_set_field_val(info, F_MIVR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 				   rt9455_mivr_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 				   ARRAY_SIZE(rt9455_mivr_values), mivr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		dev_err(dev, "Failed to set MIVR value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	 * Set IAICR to value retrieved from device-specific data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	 * If no value is specified, default value for IAICR is 500 mA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	if (iaicr == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		iaicr = 500000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	ret = rt9455_set_field_val(info, F_IAICR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 				   rt9455_iaicr_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 				   ARRAY_SIZE(rt9455_iaicr_values), iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		dev_err(dev, "Failed to set IAICR value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		return ret;
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	 * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	 * and not by OTG pin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		dev_err(dev, "Failed to set IAICR_INT bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	}
^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) 	 * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	 * CHMIVRI is triggered, but there is no action to be taken by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	 * driver when CHMIVRI is triggered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		dev_err(dev, "Failed to mask CHMIVRI interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  * Before setting the charger into boost mode, boost output voltage is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)  * set. This is needed because boost output voltage may differ from battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802)  * regulation voltage. F_VOREG bits represent either battery regulation voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803)  * or boost output voltage, depending on the mode the charger is. Both battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  * regulation voltage and boost output voltage are read from DT/ACPI during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  * probe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	ret = rt9455_set_field_val(info, F_VOREG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 				   rt9455_boost_voltage_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 				   ARRAY_SIZE(rt9455_boost_voltage_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 				   info->boost_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		dev_err(dev, "Failed to set boost output voltage value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826)  * Before setting the charger into charge mode, battery regulation voltage is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827)  * set. This is needed because boost output voltage may differ from battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828)  * regulation voltage. F_VOREG bits represent either battery regulation voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829)  * or boost output voltage, depending on the mode the charger is. Both battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830)  * regulation voltage and boost output voltage are read from DT/ACPI during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831)  * probe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	ret = rt9455_set_field_val(info, F_VOREG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 				   rt9455_voreg_values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 				   ARRAY_SIZE(rt9455_voreg_values),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 				   info->voreg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		dev_err(dev, "Failed to set VOREG value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		return ret;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 						  bool *_is_battery_absent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 						  bool *_alert_userspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	unsigned int irq1, mask1, mask2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	bool is_battery_absent = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	bool alert_userspace = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		dev_err(dev, "Failed to read IRQ1 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		dev_err(dev, "Failed to read MASK1 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (irq1 & GET_MASK(F_TSDI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		dev_err(dev, "Thermal shutdown fault occurred\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		alert_userspace = true;
^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 (irq1 & GET_MASK(F_VINOVPI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		dev_err(dev, "Overvoltage input occurred\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (irq1 & GET_MASK(F_BATAB)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		dev_err(dev, "Battery absence occurred\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		is_battery_absent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		if ((mask1 & GET_MASK(F_BATABM)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			ret = regmap_field_write(info->regmap_fields[F_BATABM],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 						 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 				dev_err(dev, "Failed to mask BATAB interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			dev_err(dev, "Failed to read MASK2 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		if (mask2 & GET_MASK(F_CHTERMIM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 			ret = regmap_field_write(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 				info->regmap_fields[F_CHTERMIM], 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 				dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		if (mask2 & GET_MASK(F_CHRCHGIM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			ret = regmap_field_write(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 				info->regmap_fields[F_CHRCHGIM], 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 				dev_err(dev, "Failed to unmask CHRCHGI interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		 * When the battery is absent, max_charging_time_work is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		 * cancelled, since no charging is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		cancel_delayed_work_sync(&info->max_charging_time_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		 * Since no interrupt is triggered when the battery is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		 * reconnected, max_charging_time_work is not rescheduled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		 * Therefore, batt_presence_work is scheduled to check whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		 * the battery is still absent or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		queue_delayed_work(system_power_efficient_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 				   &info->batt_presence_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 				   RT9455_BATT_PRESENCE_DELAY * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	*_is_battery_absent = is_battery_absent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (alert_userspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		*_alert_userspace = alert_userspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 						  bool is_battery_absent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 						  bool *_alert_userspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	unsigned int irq2, mask2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	bool alert_userspace = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		dev_err(dev, "Failed to read IRQ2 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		dev_err(dev, "Failed to read MASK2 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		return ret;
^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) 	if (irq2 & GET_MASK(F_CHRVPI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		dev_dbg(dev, "Charger fault occurred\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		 * CHRVPI bit is set in 2 cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		 * 1. when the power source is connected to the charger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		 * 2. when the power source is disconnected from the charger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		 * To identify the case, PWR_RDY bit is checked. Because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		 * PWR_RDY bit is set / cleared after CHRVPI interrupt is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		 * triggered, it is used delayed_work to later read PWR_RDY bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		 * Also, do not set to true alert_userspace, because there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		 * need to notify userspace when CHRVPI interrupt has occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		 * Userspace will be notified after PWR_RDY bit is read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		queue_delayed_work(system_power_efficient_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 				   &info->pwr_rdy_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 				   RT9455_PWR_RDY_DELAY * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (irq2 & GET_MASK(F_CHBATOVI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		dev_err(dev, "Battery OVP occurred\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	if (irq2 & GET_MASK(F_CHTERMI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		dev_dbg(dev, "Charge terminated\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		if (!is_battery_absent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 				ret = regmap_field_write(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 					info->regmap_fields[F_CHTERMIM], 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 				if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 					dev_err(dev, "Failed to mask CHTERMI interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 				 * Update MASK2 value, since CHTERMIM bit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 				 * set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 				mask2 = mask2 | GET_MASK(F_CHTERMIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			cancel_delayed_work_sync(&info->max_charging_time_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	if (irq2 & GET_MASK(F_CHRCHGI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		dev_dbg(dev, "Recharge request\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 					 RT9455_CHARGE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			dev_err(dev, "Failed to enable charging\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		if (mask2 & GET_MASK(F_CHTERMIM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			ret = regmap_field_write(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 				info->regmap_fields[F_CHTERMIM], 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 				dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 			/* Update MASK2 value, since CHTERMIM bit is cleared. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			mask2 = mask2 & ~GET_MASK(F_CHTERMIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		if (!is_battery_absent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			 * No need to check whether the charger is connected to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 			 * power source when CHRCHGI is received, since CHRCHGI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 			 * is not triggered if the charger is not connected to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			 * the power source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 			queue_delayed_work(system_power_efficient_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 					   &info->max_charging_time_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 					   RT9455_MAX_CHARGING_TIME * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	if (irq2 & GET_MASK(F_CH32MI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		dev_err(dev, "Charger fault. 32 mins timeout occurred\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	if (irq2 & GET_MASK(F_CHTREGI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 			 "Charger warning. Thermal regulation loop active\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	if (irq2 & GET_MASK(F_CHMIVRI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		dev_dbg(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			"Charger warning. Input voltage MIVR loop active\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	if (alert_userspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		*_alert_userspace = alert_userspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 						  bool *_alert_userspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	unsigned int irq3, mask3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	bool alert_userspace = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		dev_err(dev, "Failed to read IRQ3 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3);
^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 read MASK3 register\n");
^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) 	if (irq3 & GET_MASK(F_BSTBUSOVI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		dev_err(dev, "Boost fault. Overvoltage input occurred\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (irq3 & GET_MASK(F_BSTOLI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		dev_err(dev, "Boost fault. Overload\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (irq3 & GET_MASK(F_BSTLOWVI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		dev_err(dev, "Boost fault. Battery voltage too low\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	if (irq3 & GET_MASK(F_BST32SI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		alert_userspace = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	if (alert_userspace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		ret = rt9455_set_voreg_before_charge_mode(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			dev_err(dev, "Failed to set VOREG before entering charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 					 RT9455_CHARGE_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			dev_err(dev, "Failed to set charger in charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		*_alert_userspace = alert_userspace;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) static irqreturn_t rt9455_irq_handler_thread(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	struct rt9455_info *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	bool alert_userspace = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	bool is_battery_absent = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	if (irq != info->client->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		dev_err(dev, "Interrupt is not for RT9455 charger\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	ret = regmap_field_read(info->regmap_fields[F_STAT], &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		dev_err(dev, "Failed to read STAT bits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	dev_dbg(dev, "Charger status is %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	 * Each function that processes an IRQ register receives as output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	 * parameter alert_userspace pointer. alert_userspace is set to true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	 * in such a function only if an interrupt has occurred in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	 * respective interrupt register. This way, it is avoided the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	 * case: interrupt occurs only in IRQ1 register,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	 * rt9455_irq_handler_check_irq1_register() function sets to true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	 * alert_userspace, but rt9455_irq_handler_check_irq2_register()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	 * and rt9455_irq_handler_check_irq3_register() functions set to false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	 * alert_userspace and power_supply_changed() is never called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 						     &alert_userspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		dev_err(dev, "Failed to handle IRQ1 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 						     &alert_userspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		dev_err(dev, "Failed to handle IRQ2 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		dev_err(dev, "Failed to handle IRQ3 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	if (alert_userspace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		 * Sometimes, an interrupt occurs while rt9455_probe() function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		 * is executing and power_supply_register() is not yet called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		 * Do not call power_supply_changed() in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		if (info->charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			power_supply_changed(info->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 				   u32 *ieoc_percentage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 				   u32 *mivr, u32 *iaicr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	if (!dev->of_node && !ACPI_HANDLE(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		dev_err(dev, "No support for either device tree or ACPI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	 * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	 * parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	ret = device_property_read_u32(dev, "richtek,output-charge-current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 				       ichrg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		dev_err(dev, "Error: missing \"output-charge-current\" property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 				       ieoc_percentage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	ret = device_property_read_u32(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 				       "richtek,battery-regulation-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 				       &info->voreg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	ret = device_property_read_u32(dev, "richtek,boost-output-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 				       &info->boost_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		dev_err(dev, "Error: missing \"boost-output-voltage\" property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	 * MIVR and IAICR are optional parameters. Do not return error if one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	 * them is not present in ACPI table or device tree specification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	device_property_read_u32(dev, "richtek,min-input-voltage-regulation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 				 mivr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	device_property_read_u32(dev, "richtek,avg-input-current-regulation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 				 iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) static int rt9455_usb_event_none(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 				 u8 opa_mode, u8 iaicr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	if (opa_mode == RT9455_BOOST_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		ret = rt9455_set_voreg_before_charge_mode(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			dev_err(dev, "Failed to set VOREG before entering charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		 * If the charger is in boost mode, and it has received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		 * USB_EVENT_NONE, this means the consumer device powered by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		 * charger is not connected anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		 * In this case, the charger goes into charge mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 					 RT9455_CHARGE_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			dev_err(dev, "Failed to set charger in charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	if (iaicr != RT9455_IAICR_100MA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		ret = regmap_field_write(info->regmap_fields[F_IAICR],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 					 RT9455_IAICR_100MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			dev_err(dev, "Failed to set IAICR value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) static int rt9455_usb_event_vbus(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 				 u8 opa_mode, u8 iaicr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	if (opa_mode == RT9455_BOOST_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		ret = rt9455_set_voreg_before_charge_mode(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			dev_err(dev, "Failed to set VOREG before entering charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		 * If the charger is in boost mode, and it has received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		 * USB_EVENT_VBUS, this means the consumer device powered by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		 * charger is not connected anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		 * In this case, the charger goes into charge mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 					 RT9455_CHARGE_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			dev_err(dev, "Failed to set charger in charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	if (iaicr != RT9455_IAICR_500MA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		ret = regmap_field_write(info->regmap_fields[F_IAICR],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 					 RT9455_IAICR_500MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			dev_err(dev, "Failed to set IAICR value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) static int rt9455_usb_event_id(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 			       u8 opa_mode, u8 iaicr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (opa_mode == RT9455_CHARGE_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		ret = rt9455_set_boost_voltage_before_boost_mode(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			dev_err(dev, "Failed to set boost output voltage before entering boost mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		 * If the charger is in charge mode, and it has received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		 * USB_EVENT_ID, this means a consumer device is connected and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		 * it should be powered by the charger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		 * In this case, the charger goes into boost mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 					 RT9455_BOOST_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			dev_err(dev, "Failed to set charger in boost mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	if (iaicr != RT9455_IAICR_100MA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		ret = regmap_field_write(info->regmap_fields[F_IAICR],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 					 RT9455_IAICR_100MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			dev_err(dev, "Failed to set IAICR value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) static int rt9455_usb_event_charger(struct rt9455_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 				    u8 opa_mode, u8 iaicr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	if (opa_mode == RT9455_BOOST_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		ret = rt9455_set_voreg_before_charge_mode(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			dev_err(dev, "Failed to set VOREG before entering charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		 * If the charger is in boost mode, and it has received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		 * USB_EVENT_CHARGER, this means the consumer device powered by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		 * the charger is not connected anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		 * In this case, the charger goes into charge mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 					 RT9455_CHARGE_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			dev_err(dev, "Failed to set charger in charge mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	if (iaicr != RT9455_IAICR_NO_LIMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		ret = regmap_field_write(info->regmap_fields[F_IAICR],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 					 RT9455_IAICR_NO_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			dev_err(dev, "Failed to set IAICR value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) static int rt9455_usb_event(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 			    unsigned long event, void *power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	struct rt9455_info *info = container_of(nb, struct rt9455_info, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	unsigned int opa_mode, iaicr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	 * Determine whether the charger is in charge mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	 * or in boost mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	ret = regmap_field_read(info->regmap_fields[F_OPA_MODE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 				&opa_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		dev_err(dev, "Failed to read OPA_MODE value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	ret = regmap_field_read(info->regmap_fields[F_IAICR],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 				&iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		dev_err(dev, "Failed to read IAICR value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	dev_dbg(dev, "Received USB event %lu\n", event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	case USB_EVENT_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		return rt9455_usb_event_none(info, opa_mode, iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	case USB_EVENT_VBUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		return rt9455_usb_event_vbus(info, opa_mode, iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	case USB_EVENT_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		return rt9455_usb_event_id(info, opa_mode, iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	case USB_EVENT_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		return rt9455_usb_event_charger(info, opa_mode, iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		dev_err(dev, "Unknown USB event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) static void rt9455_pwr_rdy_work_callback(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	struct rt9455_info *info = container_of(work, struct rt9455_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 						pwr_rdy_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	unsigned int pwr_rdy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		dev_err(dev, "Failed to read PWR_RDY bit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	switch (pwr_rdy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	case RT9455_PWR_FAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		dev_dbg(dev, "Charger disconnected from power source\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		cancel_delayed_work_sync(&info->max_charging_time_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	case RT9455_PWR_GOOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		dev_dbg(dev, "Charger connected to power source\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 					 RT9455_CHARGE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			dev_err(dev, "Failed to enable charging\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		queue_delayed_work(system_power_efficient_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 				   &info->max_charging_time_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 				   RT9455_MAX_CHARGING_TIME * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	 * Notify userspace that the charger has been either connected to or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	 * disconnected from the power source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	power_supply_changed(info->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) static void rt9455_max_charging_time_work_callback(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	struct rt9455_info *info = container_of(work, struct rt9455_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 						max_charging_time_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	dev_err(dev, "Battery has been charging for at least 6 hours and is not yet fully charged. Battery is dead, therefore charging is disabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 				 RT9455_CHARGE_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		dev_err(dev, "Failed to disable charging\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) static void rt9455_batt_presence_work_callback(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	struct rt9455_info *info = container_of(work, struct rt9455_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 						batt_presence_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	struct device *dev = &info->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	unsigned int irq1, mask1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		dev_err(dev, "Failed to read IRQ1 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	 * If the battery is still absent, batt_presence_work is rescheduled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	 * Otherwise, max_charging_time is scheduled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	if (irq1 & GET_MASK(F_BATAB)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		queue_delayed_work(system_power_efficient_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 				   &info->batt_presence_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 				   RT9455_BATT_PRESENCE_DELAY * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		queue_delayed_work(system_power_efficient_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 				   &info->max_charging_time_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 				   RT9455_MAX_CHARGING_TIME * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 			dev_err(dev, "Failed to read MASK1 register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		if (mask1 & GET_MASK(F_BATABM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 			ret = regmap_field_write(info->regmap_fields[F_BATABM],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 						 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 				dev_err(dev, "Failed to unmask BATAB interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		 * Notify userspace that the battery is now connected to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		 * charger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		power_supply_changed(info->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) static const struct power_supply_desc rt9455_charger_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	.name			= RT9455_DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	.type			= POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	.properties		= rt9455_charger_properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	.num_properties		= ARRAY_SIZE(rt9455_charger_properties),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	.get_property		= rt9455_charger_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	case RT9455_REG_DEV_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	case RT9455_REG_IRQ1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	case RT9455_REG_IRQ2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	case RT9455_REG_IRQ3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	case RT9455_REG_DEV_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	case RT9455_REG_CTRL5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	case RT9455_REG_CTRL6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) static const struct regmap_config rt9455_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	.reg_bits	= 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	.val_bits	= 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	.writeable_reg	= rt9455_is_writeable_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	.volatile_reg	= rt9455_is_volatile_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	.max_register	= RT9455_REG_MASK3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	.cache_type	= REGCACHE_RBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) static int rt9455_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 			const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	struct rt9455_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	struct power_supply_config rt9455_charger_config = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	 * Mandatory device-specific data values. Also, VOREG and boost output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	 * voltage are mandatory values, but they are stored in rt9455_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	 * structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	u32 ichrg, ieoc_percentage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	/* Optional device-specific data values. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	u32 mivr = -1, iaicr = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	info->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	i2c_set_clientdata(client, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	info->regmap = devm_regmap_init_i2c(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 					    &rt9455_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	if (IS_ERR(info->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		dev_err(dev, "Failed to initialize register map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	for (i = 0; i < F_MAX_FIELDS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		info->regmap_fields[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 			devm_regmap_field_alloc(dev, info->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 						rt9455_reg_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		if (IS_ERR(info->regmap_fields[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 			dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 				"Failed to allocate regmap field = %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 			return PTR_ERR(info->regmap_fields[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 				      &mivr, &iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		dev_err(dev, "Failed to discover charger\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	if (IS_ERR(info->usb_phy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		dev_err(dev, "Failed to get USB transceiver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		info->nb.notifier_call = rt9455_usb_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		ret = usb_register_notifier(info->usb_phy, &info->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 			dev_err(dev, "Failed to register USB notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 			 * If usb_register_notifier() fails, set notifier_call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 			 * to NULL, to avoid calling usb_unregister_notifier().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 			info->nb.notifier_call = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	INIT_DEFERRABLE_WORK(&info->max_charging_time_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 			     rt9455_max_charging_time_work_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	INIT_DEFERRABLE_WORK(&info->batt_presence_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			     rt9455_batt_presence_work_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	rt9455_charger_config.of_node		= dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	rt9455_charger_config.drv_data		= info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	rt9455_charger_config.supplied_to	= rt9455_charger_supplied_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	rt9455_charger_config.num_supplicants	=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 					ARRAY_SIZE(rt9455_charger_supplied_to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 					rt9455_irq_handler_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 					RT9455_DRIVER_NAME, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		dev_err(dev, "Failed to register IRQ handler\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		goto put_usb_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		dev_err(dev, "Failed to set charger to its default values\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		goto put_usb_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	info->charger = devm_power_supply_register(dev, &rt9455_charger_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 						   &rt9455_charger_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	if (IS_ERR(info->charger)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		dev_err(dev, "Failed to register charger\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		ret = PTR_ERR(info->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		goto put_usb_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) put_usb_notifier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	if (info->nb.notifier_call)  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		usb_unregister_notifier(info->usb_phy, &info->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		info->nb.notifier_call = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) static int rt9455_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	struct rt9455_info *info = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	ret = rt9455_register_reset(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		dev_err(&info->client->dev, "Failed to set charger to its default values\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	if (info->nb.notifier_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		usb_unregister_notifier(info->usb_phy, &info->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	cancel_delayed_work_sync(&info->pwr_rdy_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	cancel_delayed_work_sync(&info->max_charging_time_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	cancel_delayed_work_sync(&info->batt_presence_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) static const struct i2c_device_id rt9455_i2c_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	{ RT9455_DRIVER_NAME, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) static const struct of_device_id rt9455_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	{ .compatible = "richtek,rt9455", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) MODULE_DEVICE_TABLE(of, rt9455_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) static const struct acpi_device_id rt9455_i2c_acpi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	{ "RT945500", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) static struct i2c_driver rt9455_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	.probe		= rt9455_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	.remove		= rt9455_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	.id_table	= rt9455_i2c_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		.name		= RT9455_DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		.of_match_table	= of_match_ptr(rt9455_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		.acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) module_i2c_driver(rt9455_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");