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)  * bq2415x charger driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2011-2013  Pali Rohár <pali@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Datasheets:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * https://www.ti.com/product/bq24150
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * https://www.ti.com/product/bq24150a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * https://www.ti.com/product/bq24152
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * https://www.ti.com/product/bq24153
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * https://www.ti.com/product/bq24153a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * https://www.ti.com/product/bq24155
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * https://www.ti.com/product/bq24157s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * https://www.ti.com/product/bq24158
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/power/bq2415x_charger.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) /* timeout for resetting chip timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define BQ2415X_TIMER_TIMEOUT		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define BQ2415X_REG_STATUS		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define BQ2415X_REG_CONTROL		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define BQ2415X_REG_VOLTAGE		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define BQ2415X_REG_VENDER		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define BQ2415X_REG_CURRENT		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) /* reset state for all registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define BQ2415X_RESET_STATUS		BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define BQ2415X_RESET_CONTROL		(BIT(4)|BIT(5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define BQ2415X_RESET_VOLTAGE		(BIT(1)|BIT(3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define BQ2415X_RESET_CURRENT		(BIT(0)|BIT(3)|BIT(7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) /* status register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define BQ2415X_BIT_TMR_RST		7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define BQ2415X_BIT_OTG			7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define BQ2415X_BIT_EN_STAT		6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define BQ2415X_MASK_STAT		(BIT(4)|BIT(5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define BQ2415X_SHIFT_STAT		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define BQ2415X_BIT_BOOST		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define BQ2415X_MASK_FAULT		(BIT(0)|BIT(1)|BIT(2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define BQ2415X_SHIFT_FAULT		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) /* control register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define BQ2415X_MASK_LIMIT		(BIT(6)|BIT(7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #define BQ2415X_SHIFT_LIMIT		6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define BQ2415X_MASK_VLOWV		(BIT(4)|BIT(5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define BQ2415X_SHIFT_VLOWV		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define BQ2415X_BIT_TE			3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define BQ2415X_BIT_CE			2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define BQ2415X_BIT_HZ_MODE		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define BQ2415X_BIT_OPA_MODE		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /* voltage register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define BQ2415X_MASK_VO		(BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define BQ2415X_SHIFT_VO		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define BQ2415X_BIT_OTG_PL		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define BQ2415X_BIT_OTG_EN		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) /* vender register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define BQ2415X_MASK_VENDER		(BIT(5)|BIT(6)|BIT(7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define BQ2415X_SHIFT_VENDER		5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define BQ2415X_MASK_PN			(BIT(3)|BIT(4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define BQ2415X_SHIFT_PN		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define BQ2415X_MASK_REVISION		(BIT(0)|BIT(1)|BIT(2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define BQ2415X_SHIFT_REVISION		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) /* current register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define BQ2415X_MASK_RESET		BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define BQ2415X_MASK_VI_CHRG		(BIT(4)|BIT(5)|BIT(6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define BQ2415X_SHIFT_VI_CHRG		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) /* N/A					BIT(3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define BQ2415X_MASK_VI_TERM		(BIT(0)|BIT(1)|BIT(2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define BQ2415X_SHIFT_VI_TERM		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) enum bq2415x_command {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	BQ2415X_TIMER_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	BQ2415X_OTG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	BQ2415X_STAT_PIN_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	BQ2415X_STAT_PIN_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	BQ2415X_STAT_PIN_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	BQ2415X_CHARGE_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	BQ2415X_BOOST_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	BQ2415X_FAULT_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	BQ2415X_CHARGE_TERMINATION_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	BQ2415X_CHARGE_TERMINATION_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	BQ2415X_CHARGE_TERMINATION_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	BQ2415X_CHARGER_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	BQ2415X_CHARGER_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	BQ2415X_CHARGER_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	BQ2415X_HIGH_IMPEDANCE_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	BQ2415X_HIGH_IMPEDANCE_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	BQ2415X_HIGH_IMPEDANCE_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	BQ2415X_BOOST_MODE_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	BQ2415X_BOOST_MODE_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	BQ2415X_BOOST_MODE_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	BQ2415X_OTG_LEVEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	BQ2415X_OTG_ACTIVATE_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	BQ2415X_OTG_ACTIVATE_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	BQ2415X_OTG_PIN_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	BQ2415X_OTG_PIN_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	BQ2415X_OTG_PIN_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	BQ2415X_VENDER_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	BQ2415X_PART_NUMBER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	BQ2415X_REVISION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) enum bq2415x_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	BQUNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	BQ24150,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	BQ24150A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	BQ24151,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	BQ24151A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	BQ24152,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	BQ24153,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	BQ24153A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	BQ24155,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	BQ24156,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	BQ24156A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	BQ24157S,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	BQ24158,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) static char *bq2415x_chip_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	"unknown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	"bq24150",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	"bq24150a",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	"bq24151",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	"bq24151a",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	"bq24152",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	"bq24153",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	"bq24153a",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	"bq24155",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	"bq24156",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	"bq24156a",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	"bq24157s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	"bq24158",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) struct bq2415x_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	struct bq2415x_platform_data init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	struct power_supply *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	struct power_supply_desc charger_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	struct delayed_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	struct device_node *notify_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	struct notifier_block nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	enum bq2415x_mode reported_mode;/* mode reported by hook function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	enum bq2415x_mode mode;		/* currently configured mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	enum bq2415x_chip chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	const char *timer_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	char *model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	int autotimer;	/* 1 - if driver automatically reset timer, 0 - not */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	int automode;	/* 1 - enabled, 0 - disabled; -1 - not supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) /* each registered chip must have unique id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) static DEFINE_IDR(bq2415x_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) static DEFINE_MUTEX(bq2415x_id_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) static DEFINE_MUTEX(bq2415x_timer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) static DEFINE_MUTEX(bq2415x_i2c_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) /**** i2c read functions ****/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) /* read value from register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	struct i2c_client *client = to_i2c_client(bq->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	struct i2c_msg msg[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	if (!client->adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	msg[0].addr = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	msg[0].flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	msg[0].buf = &reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	msg[0].len = sizeof(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	msg[1].addr = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	msg[1].flags = I2C_M_RD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	msg[1].buf = &val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	msg[1].len = sizeof(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	mutex_lock(&bq2415x_i2c_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	mutex_unlock(&bq2415x_i2c_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) /* read value from register, apply mask and right shift it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 				 u8 mask, u8 shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	if (shift > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	ret = bq2415x_i2c_read(bq, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	return (ret & mask) >> shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) /* read value from register and return one specified bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	if (bit > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) /**** i2c write functions ****/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) /* write value to register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct i2c_client *client = to_i2c_client(bq->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	struct i2c_msg msg[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	u8 data[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	data[0] = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	data[1] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	msg[0].addr = client->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	msg[0].flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	msg[0].buf = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	msg[0].len = ARRAY_SIZE(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	mutex_lock(&bq2415x_i2c_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	mutex_unlock(&bq2415x_i2c_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	/* i2c_transfer returns number of messages transferred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	else if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) /* read value from register, change it with mask left shifted and write back */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 				  u8 mask, u8 shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	if (shift > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	ret = bq2415x_i2c_read(bq, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	ret &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	ret |= val << shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	return bq2415x_i2c_write(bq, reg, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) /* change only one bit in register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 				 bool val, u8 bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if (bit > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) /**** global functions ****/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) /* exec command function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) static int bq2415x_exec_command(struct bq2415x_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 				enum bq2415x_command command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	switch (command) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	case BQ2415X_TIMER_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 				1, BQ2415X_BIT_TMR_RST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	case BQ2415X_OTG_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 				BQ2415X_BIT_OTG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	case BQ2415X_STAT_PIN_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 				BQ2415X_BIT_EN_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	case BQ2415X_STAT_PIN_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 				BQ2415X_BIT_EN_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	case BQ2415X_STAT_PIN_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 				BQ2415X_BIT_EN_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	case BQ2415X_CHARGE_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 				BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	case BQ2415X_BOOST_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 				BQ2415X_BIT_BOOST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	case BQ2415X_FAULT_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 			BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	case BQ2415X_CHARGE_TERMINATION_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 				BQ2415X_BIT_TE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	case BQ2415X_CHARGE_TERMINATION_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 				1, BQ2415X_BIT_TE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	case BQ2415X_CHARGE_TERMINATION_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 				0, BQ2415X_BIT_TE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	case BQ2415X_CHARGER_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			BQ2415X_BIT_CE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		return ret > 0 ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	case BQ2415X_CHARGER_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 				0, BQ2415X_BIT_CE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	case BQ2415X_CHARGER_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 				1, BQ2415X_BIT_CE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	case BQ2415X_HIGH_IMPEDANCE_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 				BQ2415X_BIT_HZ_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	case BQ2415X_HIGH_IMPEDANCE_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 				1, BQ2415X_BIT_HZ_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	case BQ2415X_HIGH_IMPEDANCE_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 				0, BQ2415X_BIT_HZ_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	case BQ2415X_BOOST_MODE_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 				BQ2415X_BIT_OPA_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	case BQ2415X_BOOST_MODE_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 				1, BQ2415X_BIT_OPA_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	case BQ2415X_BOOST_MODE_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 				0, BQ2415X_BIT_OPA_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	case BQ2415X_OTG_LEVEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 				BQ2415X_BIT_OTG_PL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	case BQ2415X_OTG_ACTIVATE_HIGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 				1, BQ2415X_BIT_OTG_PL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	case BQ2415X_OTG_ACTIVATE_LOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 				0, BQ2415X_BIT_OTG_PL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	case BQ2415X_OTG_PIN_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 				BQ2415X_BIT_OTG_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	case BQ2415X_OTG_PIN_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 				1, BQ2415X_BIT_OTG_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	case BQ2415X_OTG_PIN_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 				0, BQ2415X_BIT_OTG_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	case BQ2415X_VENDER_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	case BQ2415X_PART_NUMBER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 				BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	case BQ2415X_REVISION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	return -EINVAL;
^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) /* detect chip type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	struct i2c_client *client = to_i2c_client(bq->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	switch (client->addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	case 0x6b:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			if (bq->chip == BQ24151A)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 				return bq->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			return BQ24151;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 			if (bq->chip == BQ24150A ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 				bq->chip == BQ24152 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 				bq->chip == BQ24155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 				return bq->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			return BQ24150;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			if (bq->chip == BQ24153A)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 				return bq->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			return BQ24153;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 			return BQUNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	case 0x6a:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			if (bq->chip == BQ24156A)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 				return bq->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 			return BQ24156;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			if (bq->chip == BQ24157S)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 				return bq->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			return BQ24158;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			return BQUNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	return BQUNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) /* detect chip revision */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) static int bq2415x_detect_revision(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	int chip = bq2415x_detect_chip(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	if (ret < 0 || chip < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	switch (chip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	case BQ24150:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	case BQ24150A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	case BQ24151:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	case BQ24151A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	case BQ24152:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		if (ret >= 0 && ret <= 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	case BQ24153:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	case BQ24153A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	case BQ24156:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	case BQ24156A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	case BQ24157S:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	case BQ24158:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		if (ret == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		else if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	case BQ24155:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		if (ret == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			return 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	case BQUNKNOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) /* return chip vender code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) static int bq2415x_get_vender_code(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	/* convert to binary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	return (ret & 0x1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	       ((ret >> 1) & 0x1) * 10 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	       ((ret >> 2) & 0x1) * 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) /* reset all chip registers to default state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) static void bq2415x_reset_chip(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	bq->timer_error = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) /**** properties functions ****/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) /* set current limit in mA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if (mA <= 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	else if (mA <= 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	else if (mA <= 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		val = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		val = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) /* get current limit in mA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) static int bq2415x_get_current_limit(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	else if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		return 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	else if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		return 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	else if (ret == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		return 800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	else if (ret == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		return 1800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) /* set weak battery voltage in mV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	/* round to 100mV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	if (mV <= 3400 + 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	else if (mV <= 3500 + 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	else if (mV <= 3600 + 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		val = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		val = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) /* get weak battery voltage in mV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	return 100 * (34 + ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) /* set battery regulation voltage in mV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 						  int mV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	int val = (mV/10 - 350) / 2;
^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) 	 * According to datasheet, maximum battery regulation voltage is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	 * 4440mV which is b101111 = 47.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	else if (val > 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) /* get battery regulation voltage in mV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	return 10 * (350 + 2*ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) /* set charge current in mA (platform data must provide resistor sense) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	if (bq->init_data.resistor_sense <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	else if (val > 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		val = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			BQ2415X_SHIFT_VI_CHRG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) /* get charge current in mA (platform data must provide resistor sense) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) static int bq2415x_get_charge_current(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	if (bq->init_data.resistor_sense <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return (37400 + 6800*ret) / bq->init_data.resistor_sense;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) /* set termination current in mA (platform data must provide resistor sense) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	if (bq->init_data.resistor_sense <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	else if (val > 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		val = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			BQ2415X_SHIFT_VI_TERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) /* get termination current in mA (platform data must provide resistor sense) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) static int bq2415x_get_termination_current(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (bq->init_data.resistor_sense <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 			BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	return (3400 + 3400*ret) / bq->init_data.resistor_sense;
^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) /* set default value of property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) #define bq2415x_set_default_value(bq, prop) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		int ret = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		if (bq->init_data.prop != -1) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 			ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		if (ret < 0) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 			return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) /* set default values of all properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) static int bq2415x_set_defaults(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	bq2415x_set_default_value(bq, current_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	bq2415x_set_default_value(bq, weak_battery_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	bq2415x_set_default_value(bq, battery_regulation_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (bq->init_data.resistor_sense > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		bq2415x_set_default_value(bq, charge_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		bq2415x_set_default_value(bq, termination_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) /**** charger mode functions ****/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) /* set charger mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	int charger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	int boost = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if (mode == BQ2415X_MODE_BOOST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		boost = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	else if (mode != BQ2415X_MODE_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		charger = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	if (!charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	if (!boost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	case BQ2415X_MODE_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		dev_dbg(bq->dev, "changing mode to: Offline\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		ret = bq2415x_set_current_limit(bq, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	case BQ2415X_MODE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		dev_dbg(bq->dev, "changing mode to: N/A\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		ret = bq2415x_set_current_limit(bq, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	case BQ2415X_MODE_HOST_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		ret = bq2415x_set_current_limit(bq, 500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	case BQ2415X_MODE_DEDICATED_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		ret = bq2415x_set_current_limit(bq, 1800);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	case BQ2415X_MODE_BOOST: /* Boost mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		dev_dbg(bq->dev, "changing mode to: Boost\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		ret = bq2415x_set_current_limit(bq, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	else if (boost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	bq2415x_set_default_value(bq, weak_battery_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	bq2415x_set_default_value(bq, battery_regulation_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	bq->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	sysfs_notify(&bq->charger->dev.kobj, NULL, "mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	return 0;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) static bool bq2415x_update_reported_mode(struct bq2415x_device *bq, int mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	enum bq2415x_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	if (mA == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		mode = BQ2415X_MODE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	else if (mA < 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		mode = BQ2415X_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	else if (mA < 1800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		mode = BQ2415X_MODE_HOST_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		mode = BQ2415X_MODE_DEDICATED_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	if (bq->reported_mode == mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	bq->reported_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) static int bq2415x_notifier_call(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		unsigned long val, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	struct bq2415x_device *bq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		container_of(nb, struct bq2415x_device, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	struct power_supply *psy = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	union power_supply_propval prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	if (val != PSY_EVENT_PROP_CHANGED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	/* Ignore event if it was not send by notify_node/notify_device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (bq->notify_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		if (!psy->dev.parent ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		    psy->dev.parent->of_node != bq->notify_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	} else if (bq->init_data.notify_device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		if (strcmp(psy->desc->name, bq->init_data.notify_device) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 			return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	dev_dbg(bq->dev, "notifier call was called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			&prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (!bq2415x_update_reported_mode(bq, prop.intval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	/* if automode is not enabled do not tell about reported_mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	if (bq->automode < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	schedule_delayed_work(&bq->work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	return NOTIFY_OK;
^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) /**** timer functions ****/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) /* enable/disable auto resetting chip timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	mutex_lock(&bq2415x_timer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (bq->autotimer == state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		mutex_unlock(&bq2415x_timer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	bq->autotimer = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		bq->timer_error = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		cancel_delayed_work_sync(&bq->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	mutex_unlock(&bq2415x_timer_mutex);
^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) /* called by bq2415x_timer_work on timer error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	bq->timer_error = msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	sysfs_notify(&bq->charger->dev.kobj, NULL, "timer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	dev_err(bq->dev, "%s\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		bq->automode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	bq2415x_set_autotimer(bq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) /* delayed work function for auto resetting chip timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) static void bq2415x_timer_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 						 work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	int boost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	if (bq->automode > 0 && (bq->reported_mode != bq->mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		bq2415x_set_mode(bq, bq->reported_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	if (!bq->autotimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		bq2415x_timer_error(bq, "Resetting timer failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	if (boost < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		bq2415x_timer_error(bq, "Unknown error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		bq2415x_timer_error(bq, "Unknown error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		return;
^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) 	if (boost) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		switch (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		/* Non fatal errors, chip is OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		case 0: /* No error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		case 6: /* Timer expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			dev_err(bq->dev, "Timer expired\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		case 3: /* Battery voltage too low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			dev_err(bq->dev, "Battery voltage to low\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		/* Fatal errors, disable and reset chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		case 1: /* Overvoltage protection (chip fried) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			bq2415x_timer_error(bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 				"Overvoltage protection (chip fried)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		case 2: /* Overload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			bq2415x_timer_error(bq, "Overload");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		case 4: /* Battery overvoltage protection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 			bq2415x_timer_error(bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 				"Battery overvoltage protection");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		case 5: /* Thermal shutdown (too hot) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			bq2415x_timer_error(bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 					"Thermal shutdown (too hot)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		case 7: /* N/A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			bq2415x_timer_error(bq, "Unknown error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		switch (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		/* Non fatal errors, chip is OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		case 0: /* No error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		case 2: /* Sleep mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			dev_err(bq->dev, "Sleep mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		case 3: /* Poor input source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			dev_err(bq->dev, "Poor input source\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		case 6: /* Timer expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			dev_err(bq->dev, "Timer expired\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		case 7: /* No battery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			dev_err(bq->dev, "No battery\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		/* Fatal errors, disable and reset chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		case 1: /* Overvoltage protection (chip fried) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			bq2415x_timer_error(bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 				"Overvoltage protection (chip fried)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		case 4: /* Battery overvoltage protection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			bq2415x_timer_error(bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 				"Battery overvoltage protection");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		case 5: /* Thermal shutdown (too hot) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			bq2415x_timer_error(bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 				"Thermal shutdown (too hot)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) /**** power supply interface code ****/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) static enum power_supply_property bq2415x_power_supply_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	/* TODO: maybe add more power supply properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) static int bq2415x_power_supply_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 					     enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 					     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		else if (ret == 0) /* Ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		else if (ret == 1) /* Charge in progress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		else if (ret == 2) /* Charge done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		val->strval = bq->model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	bq->autotimer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		bq->automode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	cancel_delayed_work_sync(&bq->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	power_supply_unregister(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	kfree(bq->model);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) /**** additional sysfs entries for power supply interface ****/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) /* show *_status entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) static ssize_t bq2415x_sysfs_show_status(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 					 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 					 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	enum bq2415x_command command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	if (strcmp(attr->attr.name, "otg_status") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		command = BQ2415X_OTG_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	else if (strcmp(attr->attr.name, "charge_status") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		command = BQ2415X_CHARGE_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	else if (strcmp(attr->attr.name, "boost_status") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		command = BQ2415X_BOOST_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	else if (strcmp(attr->attr.name, "fault_status") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		command = BQ2415X_FAULT_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	ret = bq2415x_exec_command(bq, command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	return sprintf(buf, "%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)  * set timer entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)  *    auto - enable auto mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)  *    off - disable auto mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)  *    (other values) - reset chip timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 				       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 				       const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 				       size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	if (strncmp(buf, "auto", 4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		bq2415x_set_autotimer(bq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	else if (strncmp(buf, "off", 3) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		bq2415x_set_autotimer(bq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /* show timer entry (auto or off) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 					char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	if (bq->timer_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		return sprintf(buf, "%s\n", bq->timer_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	if (bq->autotimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		return sprintf(buf, "auto\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	return sprintf(buf, "off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)  * set mode entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)  *    auto - if automode is supported, enable it and set mode to reported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)  *    none - disable charger and boost mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)  *    host - charging mode for host/hub chargers (current limit 500mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)  *    dedicated - charging mode for dedicated chargers (unlimited current limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)  *    boost - disable charger and enable boost mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 				      struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 				      const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 				      size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	enum bq2415x_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	if (strncmp(buf, "auto", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		if (bq->automode < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		bq->automode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		mode = bq->reported_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	} else if (strncmp(buf, "off", 3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 			bq->automode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		mode = BQ2415X_MODE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	} else if (strncmp(buf, "none", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			bq->automode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		mode = BQ2415X_MODE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	} else if (strncmp(buf, "host", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			bq->automode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		mode = BQ2415X_MODE_HOST_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	} else if (strncmp(buf, "dedicated", 9) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			bq->automode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		mode = BQ2415X_MODE_DEDICATED_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	} else if (strncmp(buf, "boost", 5) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			bq->automode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		mode = BQ2415X_MODE_BOOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	} else if (strncmp(buf, "reset", 5) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		bq2415x_reset_chip(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		bq2415x_set_defaults(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		if (bq->automode <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		bq->automode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		mode = bq->reported_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	ret = bq2415x_set_mode(bq, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) /* show mode entry (auto, none, host, dedicated or boost) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 				       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 				       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		ret += sprintf(buf+ret, "auto (");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	switch (bq->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	case BQ2415X_MODE_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		ret += sprintf(buf+ret, "off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	case BQ2415X_MODE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		ret += sprintf(buf+ret, "none");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	case BQ2415X_MODE_HOST_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		ret += sprintf(buf+ret, "host");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	case BQ2415X_MODE_DEDICATED_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		ret += sprintf(buf+ret, "dedicated");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	case BQ2415X_MODE_BOOST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		ret += sprintf(buf+ret, "boost");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	if (bq->automode > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		ret += sprintf(buf+ret, ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	ret += sprintf(buf+ret, "\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) /* show reported_mode entry (none, host, dedicated or boost) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 						struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 						char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	if (bq->automode < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	switch (bq->reported_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	case BQ2415X_MODE_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		return sprintf(buf, "off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	case BQ2415X_MODE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		return sprintf(buf, "none\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	case BQ2415X_MODE_HOST_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		return sprintf(buf, "host\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	case BQ2415X_MODE_DEDICATED_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		return sprintf(buf, "dedicated\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	case BQ2415X_MODE_BOOST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		return sprintf(buf, "boost\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) /* directly set raw value to chip register, format: 'register value' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 					   struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 					   const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 					   size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	unsigned int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	if (sscanf(buf, "%x %x", &reg, &val) != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	if (reg > 4 || val > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	ret = bq2415x_i2c_write(bq, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) /* print value of chip register, format: 'register=value' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 				       u8 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 				       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	int ret = bq2415x_i2c_read(bq, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		return sprintf(buf, "%#.2x=error %d\n", reg, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) /* show all raw values of chip register, format per line: 'register=value' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 					    struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 					    char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) /* set current and voltage limit entries (in mA or mV) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 				       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 				       const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 				       size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	if (kstrtol(buf, 10, &val) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	if (strcmp(attr->attr.name, "current_limit") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		ret = bq2415x_set_current_limit(bq, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		ret = bq2415x_set_weak_battery_voltage(bq, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		ret = bq2415x_set_battery_regulation_voltage(bq, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	else if (strcmp(attr->attr.name, "charge_current") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		ret = bq2415x_set_charge_current(bq, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	else if (strcmp(attr->attr.name, "termination_current") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		ret = bq2415x_set_termination_current(bq, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	return count;
^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) /* show current and voltage limit entries (in mA or mV) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 					char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^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 (strcmp(attr->attr.name, "current_limit") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		ret = bq2415x_get_current_limit(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		ret = bq2415x_get_weak_battery_voltage(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		ret = bq2415x_get_battery_regulation_voltage(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	else if (strcmp(attr->attr.name, "charge_current") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		ret = bq2415x_get_charge_current(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	else if (strcmp(attr->attr.name, "termination_current") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		ret = bq2415x_get_termination_current(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	return sprintf(buf, "%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) /* set *_enable entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 					const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 					size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	enum bq2415x_command command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	if (kstrtol(buf, 10, &val) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			BQ2415X_CHARGE_TERMINATION_DISABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			BQ2415X_HIGH_IMPEDANCE_DISABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		command = val ? BQ2415X_OTG_PIN_ENABLE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			BQ2415X_OTG_PIN_DISABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		command = val ? BQ2415X_STAT_PIN_ENABLE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			BQ2415X_STAT_PIN_DISABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	ret = bq2415x_exec_command(bq, command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) /* show *_enable entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 					 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 					 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	enum bq2415x_command command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		command = BQ2415X_CHARGE_TERMINATION_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		command = BQ2415X_HIGH_IMPEDANCE_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		command = BQ2415X_OTG_PIN_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		command = BQ2415X_STAT_PIN_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	ret = bq2415x_exec_command(bq, command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	return sprintf(buf, "%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) static DEVICE_ATTR(reported_mode, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		bq2415x_sysfs_show_reported_mode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) static struct attribute *bq2415x_sysfs_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	 * TODO: some (appropriate) of these attrs should be switched to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	 * use power supply class props.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	&dev_attr_current_limit.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	&dev_attr_weak_battery_voltage.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	&dev_attr_battery_regulation_voltage.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	&dev_attr_charge_current.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	&dev_attr_termination_current.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	&dev_attr_charge_termination_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	&dev_attr_high_impedance_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	&dev_attr_otg_pin_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	&dev_attr_stat_pin_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	&dev_attr_reported_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	&dev_attr_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	&dev_attr_timer.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	&dev_attr_registers.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	&dev_attr_otg_status.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	&dev_attr_charge_status.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	&dev_attr_boost_status.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	&dev_attr_fault_status.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) ATTRIBUTE_GROUPS(bq2415x_sysfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) static int bq2415x_power_supply_init(struct bq2415x_device *bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	int chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	char revstr[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	struct power_supply_config psy_cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		.drv_data = bq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		.of_node = bq->dev->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		.attr_grp = bq2415x_sysfs_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	bq->charger_desc.name = bq->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	bq->charger_desc.type = POWER_SUPPLY_TYPE_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	bq->charger_desc.properties = bq2415x_power_supply_props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	bq->charger_desc.num_properties =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 			ARRAY_SIZE(bq2415x_power_supply_props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	bq->charger_desc.get_property = bq2415x_power_supply_get_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	ret = bq2415x_detect_chip(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		chip = BQUNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		chip = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	ret = bq2415x_detect_revision(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		strcpy(revstr, "unknown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		sprintf(revstr, "1.%d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	bq->model = kasprintf(GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 				"chip %s, revision %s, vender code %.3d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 				bq2415x_chip_name[chip], revstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 				bq2415x_get_vender_code(bq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	if (!bq->model) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		dev_err(bq->dev, "failed to allocate model name\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	bq->charger = power_supply_register(bq->dev, &bq->charger_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 					    &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	if (IS_ERR(bq->charger)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		kfree(bq->model);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		return PTR_ERR(bq->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) /* main bq2415x probe function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) static int bq2415x_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 			 const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	struct bq2415x_device *bq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	struct device_node *np = client->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	struct bq2415x_platform_data *pdata = client->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	const struct acpi_device_id *acpi_id = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	struct power_supply *notify_psy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	union power_supply_propval prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	if (!np && !pdata && !ACPI_HANDLE(&client->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		dev_err(&client->dev, "Neither devicetree, nor platform data, nor ACPI support\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		return -ENODEV;
^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) 	/* Get new ID for the new device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	mutex_lock(&bq2415x_id_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	mutex_unlock(&bq2415x_id_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	if (num < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	if (id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	} else if (ACPI_HANDLE(&client->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		acpi_id =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 			acpi_match_device(client->dev.driver->acpi_match_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 					  &client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		if (!acpi_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 			dev_err(&client->dev, "failed to match device name\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 			ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 			goto error_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		name = kasprintf(GFP_KERNEL, "%s-%d", acpi_id->id, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		dev_err(&client->dev, "failed to allocate device name\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		goto error_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	if (!bq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		goto error_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	i2c_set_clientdata(client, bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	bq->id = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	bq->dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	if (id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		bq->chip = id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	else if (ACPI_HANDLE(bq->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		bq->chip = acpi_id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	bq->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	bq->mode = BQ2415X_MODE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	bq->reported_mode = BQ2415X_MODE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	bq->autotimer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	bq->automode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	if (np || ACPI_HANDLE(bq->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		ret = device_property_read_u32(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 					       "ti,current-limit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 					       &bq->init_data.current_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 			goto error_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		ret = device_property_read_u32(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 					"ti,weak-battery-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 					&bq->init_data.weak_battery_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 			goto error_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		ret = device_property_read_u32(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 				"ti,battery-regulation-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 				&bq->init_data.battery_regulation_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 			goto error_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		ret = device_property_read_u32(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 					       "ti,charge-current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 					       &bq->init_data.charge_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 			goto error_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		ret = device_property_read_u32(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 				"ti,termination-current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 				&bq->init_data.termination_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 			goto error_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		ret = device_property_read_u32(bq->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 					       "ti,resistor-sense",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 					       &bq->init_data.resistor_sense);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 			goto error_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		if (np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 			bq->notify_node = of_parse_phandle(np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 						"ti,usb-charger-detection", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	bq2415x_reset_chip(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	ret = bq2415x_power_supply_init(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		dev_err(bq->dev, "failed to register power supply: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		goto error_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	ret = bq2415x_set_defaults(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		dev_err(bq->dev, "failed to set default values: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		goto error_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	if (bq->notify_node || bq->init_data.notify_device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		bq->nb.notifier_call = bq2415x_notifier_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		ret = power_supply_reg_notifier(&bq->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 			dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 			goto error_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		bq->automode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		dev_info(bq->dev, "automode supported, waiting for events\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		bq->automode = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		dev_info(bq->dev, "automode not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	/* Query for initial reported_mode and set it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	if (bq->nb.notifier_call) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 			notify_psy = power_supply_get_by_phandle(np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 						"ti,usb-charger-detection");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			if (IS_ERR(notify_psy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 				notify_psy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		} else if (bq->init_data.notify_device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			notify_psy = power_supply_get_by_name(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 						bq->init_data.notify_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	if (notify_psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		ret = power_supply_get_property(notify_psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 					POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		power_supply_put(notify_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 			bq2415x_update_reported_mode(bq, prop.intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 			bq2415x_set_mode(bq, bq->reported_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	bq2415x_set_autotimer(bq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	dev_info(bq->dev, "driver registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) error_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	bq2415x_power_supply_exit(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) error_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	if (bq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		of_node_put(bq->notify_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) error_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	mutex_lock(&bq2415x_id_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	idr_remove(&bq2415x_id, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	mutex_unlock(&bq2415x_id_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) /* main bq2415x remove function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) static int bq2415x_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	struct bq2415x_device *bq = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	if (bq->nb.notifier_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		power_supply_unreg_notifier(&bq->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	of_node_put(bq->notify_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	bq2415x_power_supply_exit(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	bq2415x_reset_chip(bq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	mutex_lock(&bq2415x_id_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	idr_remove(&bq2415x_id, bq->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	mutex_unlock(&bq2415x_id_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	dev_info(bq->dev, "driver unregistered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	kfree(bq->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	return 0;
^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 bq2415x_i2c_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	{ "bq2415x", BQUNKNOWN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	{ "bq24150", BQ24150 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	{ "bq24150a", BQ24150A },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	{ "bq24151", BQ24151 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	{ "bq24151a", BQ24151A },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	{ "bq24152", BQ24152 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	{ "bq24153", BQ24153 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	{ "bq24153a", BQ24153A },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	{ "bq24155", BQ24155 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	{ "bq24156", BQ24156 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	{ "bq24156a", BQ24156A },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	{ "bq24157s", BQ24157S },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	{ "bq24158", BQ24158 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) static const struct acpi_device_id bq2415x_i2c_acpi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	{ "BQ2415X", BQUNKNOWN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	{ "BQ241500", BQ24150 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	{ "BQA24150", BQ24150A },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	{ "BQ241510", BQ24151 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	{ "BQA24151", BQ24151A },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	{ "BQ241520", BQ24152 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	{ "BQ241530", BQ24153 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	{ "BQA24153", BQ24153A },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	{ "BQ241550", BQ24155 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	{ "BQ241560", BQ24156 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	{ "BQA24156", BQ24156A },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	{ "BQS24157", BQ24157S },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	{ "BQ241580", BQ24158 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) MODULE_DEVICE_TABLE(acpi, bq2415x_i2c_acpi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) static const struct of_device_id bq2415x_of_match_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	{ .compatible = "ti,bq24150" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	{ .compatible = "ti,bq24150a" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	{ .compatible = "ti,bq24151" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	{ .compatible = "ti,bq24151a" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	{ .compatible = "ti,bq24152" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	{ .compatible = "ti,bq24153" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	{ .compatible = "ti,bq24153a" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	{ .compatible = "ti,bq24155" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	{ .compatible = "ti,bq24156" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	{ .compatible = "ti,bq24156a" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	{ .compatible = "ti,bq24157s" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	{ .compatible = "ti,bq24158" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) MODULE_DEVICE_TABLE(of, bq2415x_of_match_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) static struct i2c_driver bq2415x_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 		.name = "bq2415x-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		.of_match_table = of_match_ptr(bq2415x_of_match_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		.acpi_match_table = ACPI_PTR(bq2415x_i2c_acpi_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	.probe = bq2415x_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	.remove = bq2415x_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	.id_table = bq2415x_i2c_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) module_i2c_driver(bq2415x_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) MODULE_DESCRIPTION("bq2415x charger driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) MODULE_LICENSE("GPL");