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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * rk818 charger driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2016 Rockchip Electronics Co., Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * chenjh <chenjh@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * This program is free software; you can redistribute it and/or modify it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * under the terms and conditions of the GNU General Public License,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * version 2, as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * This program is distributed in the hope it will be useful, but WITHOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *
^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/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/extcon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/irqdomain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/mfd/rk808.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/power/rk_usbbc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/rk_keys.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include "rk818_battery.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static int dbg_enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) module_param_named(dbg_level, dbg_enable, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #define DBG(args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 		if (dbg_enable) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 			pr_info(args); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 		} \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define CG_INFO(fmt, args...) pr_info("rk818-charger: "fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define DEFAULT_CHRG_CURRENT	1400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define DEFAULT_INPUT_CURRENT	2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define DEFAULT_CHRG_VOLTAGE	4200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #define SAMPLE_RES_10MR		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define SAMPLE_RES_20MR		20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define SAMPLE_RES_DIV1		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define SAMPLE_RES_DIV2		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) /* RK818_USB_CTRL_REG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #define INPUT_CUR450MA		(0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define INPUT_CUR80MA		(0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define INPUT_CUR850MA		(0x02)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define INPUT_CUR1500MA		(0x05)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define INPUT_CUR_MSK		(0x0f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) /* RK818_CHRG_CTRL_REG3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define CHRG_FINISH_MODE_MSK	BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define CHRG_FINISH_ANA_SIGNAL	(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define CHRG_FINISH_DIG_SIGNAL	BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) /* RK818_SUP_STS_REG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #define BAT_EXS			BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define USB_VLIMIT_EN		BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define USB_CLIMIT_EN		BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) /* RK818_CHRG_CTRL_REG1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define CHRG_EN			BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define CHRG_CUR_MSK		(0x0f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) /* RK818_INT_STS_MSK_REG2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define CHRG_CVTLMT_INT_MSK	BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define PLUG_OUT_MSK		BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define PLUG_IN_MSK		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) /* RK818_VB_MON_REG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define PLUG_IN_STS		BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) /* RK818_TS_CTRL_REG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define GG_EN			BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define TS2_FUN_ADC		BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) /* RK818_ADC_CTRL_REG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define ADC_TS2_EN		BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define CG_DRIVER_VERSION		"2.0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define DEFAULT_TS2_THRESHOLD_VOL      4350
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define DEFAULT_TS2_VALID_VOL          1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define DEFAULT_TS2_VOL_MULTI          0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define DEFAULT_TS2_CHECK_CNT          5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) enum charger_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	USB_TYPE_UNKNOWN_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	USB_TYPE_NONE_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	USB_TYPE_USB_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	USB_TYPE_AC_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	USB_TYPE_CDP_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	DC_TYPE_DC_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	DC_TYPE_NONE_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	OFFLINE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	ONLINE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) struct temp_chrg_table {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	int temp_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	int temp_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	u32 chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	u8 set_chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) struct charger_platform_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	u32 max_input_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	u32 max_chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	u32 max_chrg_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	u32 pwroff_vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	u32 power_dc2otg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	u32 dc_det_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	int dc_det_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	bool support_dc_det;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	int virtual_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	int sample_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	int otg5v_suspend_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	bool extcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	int ts2_vol_multi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct temp_chrg_table *tc_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	u32 tc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) struct rk818_charger {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	struct rk808 *rk818;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct power_supply *ac_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	struct power_supply *usb_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	struct power_supply *bat_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	struct extcon_dev *cable_edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	struct charger_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	struct workqueue_struct *usb_charger_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	struct workqueue_struct *dc_charger_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	struct workqueue_struct *finish_sig_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	struct workqueue_struct *ts2_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	struct delayed_work dc_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	struct delayed_work usb_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	struct delayed_work host_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	struct delayed_work discnt_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	struct delayed_work finish_sig_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	struct delayed_work irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	struct delayed_work ts2_vol_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	struct notifier_block bc_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	struct notifier_block cable_cg_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	struct notifier_block cable_host_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	struct notifier_block cable_discnt_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	struct notifier_block temp_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	unsigned int bc_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	enum charger_t usb_charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	enum charger_t dc_charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	struct regulator *otg5v_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	u8 ac_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	u8 usb_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	u8 otg_in;	/* OTG device attached status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	u8 otg_pmic5v;	/* OTG device power supply from PMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	u8 dc_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	u8 prop_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	u8 chrg_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	u8 chrg_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	u8 chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	u8 res_div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	u8 sleep_set_off_reg1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	u8 plugin_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	u8 plugout_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	int plugin_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	int plugout_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	int charger_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) static int rk818_reg_read(struct rk818_charger *cg, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	int ret, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	ret = regmap_read(cg->regmap, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		dev_err(cg->dev, "i2c read reg: 0x%2x failed\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) static int rk818_reg_write(struct rk818_charger *cg, u8 reg, u8 buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	ret = regmap_write(cg->regmap, reg, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		dev_err(cg->dev, "i2c write reg: 0x%2x failed\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) static int rk818_reg_set_bits(struct rk818_charger *cg, u8 reg, u8 mask, u8 buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	ret = regmap_update_bits(cg->regmap, reg, mask, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		dev_err(cg->dev, "i2c set reg: 0x%2x failed\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	return ret;
^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) static int rk818_reg_clear_bits(struct rk818_charger *cg, u8 reg, u8 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	ret = regmap_update_bits(cg->regmap, reg, mask, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		dev_err(cg->dev, "i2c clr reg: 0x%02x failed\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) static int rk818_cg_online(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	return (cg->ac_in | cg->usb_in | cg->dc_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) static int rk818_cg_get_dsoc(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	return rk818_reg_read(cg, RK818_SOC_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) static int rk818_cg_get_avg_current(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	int cur, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	val |= rk818_reg_read(cg, RK818_BAT_CUR_AVG_REGL) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	val |= rk818_reg_read(cg, RK818_BAT_CUR_AVG_REGH) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	if (val & 0x800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		val -= 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	cur = val * cg->res_div * 1506 / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	return cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) static int rk818_cg_get_ts2_voltage(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	u32 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	int voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	val |= rk818_reg_read(cg, RK818_TS2_ADC_REGL) << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	val |= rk818_reg_read(cg, RK818_TS2_ADC_REGH) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	/* refer voltage 2.2V, 12bit adc accuracy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	voltage = val * 2200 * cg->pdata->ts2_vol_multi / 4095;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	DBG("********* ts2 adc=%d, vol=%d\n", val, voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	return voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) static u64 get_boot_sec(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	ktime_get_boottime_ts64(&ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	return ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) static int rk818_cg_lowpwr_check(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	static u64 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	int current_avg, dsoc, fake_offline = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	buf = rk818_reg_read(cg, RK818_TS_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	if (!(buf & GG_EN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		return fake_offline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	dsoc = rk818_cg_get_dsoc(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	current_avg = rk818_cg_get_avg_current(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	if ((current_avg < 0) && (dsoc == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		if (!time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			time = get_boot_sec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		if ((get_boot_sec() - time) >= 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 			fake_offline = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			CG_INFO("low power....soc=%d, current=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 				dsoc, current_avg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		fake_offline = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	DBG("<%s>. t=%lld, dsoc=%d, current=%d, fake_offline=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	    __func__, get_boot_sec() - time, dsoc, current_avg, fake_offline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	return fake_offline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) static int rk818_cg_get_bat_psy(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	struct rk818_charger *cg = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	struct power_supply *psy = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		cg->bat_psy = psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) static void rk818_cg_get_psy(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (!cg->bat_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		class_for_each_device(power_supply_class, NULL, (void *)cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 				      rk818_cg_get_bat_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) static int rk818_cg_get_bat_max_cur(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	rk818_cg_get_psy(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	if (!cg->bat_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		return cg->pdata->max_chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	ret = cg->bat_psy->desc->get_property(cg->bat_psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 					      POWER_SUPPLY_PROP_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 					      &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	if (!ret && val.intval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		return val.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	return cg->pdata->max_chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) static int rk818_cg_get_bat_max_vol(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	rk818_cg_get_psy(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	if (!cg->bat_psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		return cg->pdata->max_chrg_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	ret = cg->bat_psy->desc->get_property(cg->bat_psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 					      POWER_SUPPLY_PROP_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 					      &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (!ret && val.intval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		return val.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	return cg->pdata->max_chrg_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) static enum power_supply_property rk818_ac_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	POWER_SUPPLY_PROP_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) static enum power_supply_property rk818_usb_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	POWER_SUPPLY_PROP_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) static int rk818_cg_ac_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 				    enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 				    union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	struct rk818_charger *cg = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	int fake_offline = 0, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	if (rk818_cg_online(cg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		fake_offline = rk818_cg_lowpwr_check(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		if (cg->pdata->virtual_power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 			val->intval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		else if (fake_offline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			val->intval = (cg->ac_in | cg->dc_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		DBG("report online: %d\n", val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		if (cg->pdata->virtual_power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		else if (fake_offline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 			val->intval = cg->prop_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		DBG("report prop: %d\n", val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		val->intval = rk818_cg_get_bat_max_vol(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	case POWER_SUPPLY_PROP_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		val->intval = rk818_cg_get_bat_max_cur(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) static int rk818_cg_usb_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 				     enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 				     union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	struct rk818_charger *cg = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	int fake_offline = 0, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (rk818_cg_online(cg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		fake_offline = rk818_cg_lowpwr_check(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		if (cg->pdata->virtual_power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			val->intval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		else if (fake_offline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			val->intval = cg->usb_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		DBG("report online: %d\n", val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		if (cg->pdata->virtual_power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		else if (fake_offline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 			val->intval = cg->prop_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		DBG("report prop: %d\n", val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		val->intval = rk818_cg_get_bat_max_vol(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	case POWER_SUPPLY_PROP_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		val->intval = rk818_cg_get_bat_max_cur(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) static const struct power_supply_desc rk818_ac_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	.name		= "ac",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	.type		= POWER_SUPPLY_TYPE_MAINS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	.properties	= rk818_ac_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	.num_properties	= ARRAY_SIZE(rk818_ac_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	.get_property	= rk818_cg_ac_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) static const struct power_supply_desc rk818_usb_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	.name		= "usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	.type		= POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	.properties	= rk818_usb_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	.num_properties	= ARRAY_SIZE(rk818_usb_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	.get_property	= rk818_cg_usb_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) static int rk818_cg_init_power_supply(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	struct power_supply_config psy_cfg = { .drv_data = cg, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	cg->usb_psy = devm_power_supply_register(cg->dev, &rk818_usb_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 						 &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (IS_ERR(cg->usb_psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		dev_err(cg->dev, "register usb power supply fail\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		return PTR_ERR(cg->usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	cg->ac_psy = devm_power_supply_register(cg->dev, &rk818_ac_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 						&psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	if (IS_ERR(cg->ac_psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		dev_err(cg->dev, "register ac power supply fail\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		return PTR_ERR(cg->ac_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) static void rk818_cg_pr_info(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	u8 usb_ctrl, chrg_ctrl1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	usb_ctrl = rk818_reg_read(cg, RK818_USB_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	chrg_ctrl1 = rk818_reg_read(cg, RK818_CHRG_CTRL_REG1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	CG_INFO("ac=%d usb=%d dc=%d otg=%d 5v=%d, v=%d chrg=%d input=%d virt=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		cg->ac_in, cg->usb_in, cg->dc_in, cg->otg_in, cg->otg_pmic5v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		chrg_vol_sel_array[(chrg_ctrl1 & 0x70) >> 4],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		chrg_cur_sel_array[chrg_ctrl1 & 0x0f] * cg->res_div,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		chrg_cur_input_array[usb_ctrl & 0x0f],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		cg->pdata->virtual_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) static bool is_battery_exist(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	return (rk818_reg_read(cg, RK818_SUP_STS_REG) & BAT_EXS) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) static void rk818_cg_set_chrg_current(struct rk818_charger *cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 				      u8 chrg_current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	u8 chrg_ctrl_reg1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	chrg_ctrl_reg1 = rk818_reg_read(cg, RK818_CHRG_CTRL_REG1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	chrg_ctrl_reg1 &= ~CHRG_CUR_MSK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	chrg_ctrl_reg1 |= (chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	rk818_reg_write(cg, RK818_CHRG_CTRL_REG1, chrg_ctrl_reg1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) static void rk818_cg_set_input_current(struct rk818_charger *cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 				       int input_current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	u8 usb_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	if (cg->pdata->virtual_power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		CG_INFO("warning: virtual power mode...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		input_current = cg->chrg_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	usb_ctrl = rk818_reg_read(cg, RK818_USB_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	usb_ctrl &= ~INPUT_CUR_MSK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	usb_ctrl |= (input_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	rk818_reg_write(cg, RK818_USB_CTRL_REG, usb_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) static void rk818_cg_set_finish_sig(struct rk818_charger *cg, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	buf = rk818_reg_read(cg, RK818_CHRG_CTRL_REG3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	buf &= ~CHRG_FINISH_MODE_MSK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	buf |= mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	rk818_reg_write(cg, RK818_CHRG_CTRL_REG3, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) static void rk818_cg_finish_sig_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	struct rk818_charger *cg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	cg = container_of(work, struct rk818_charger, finish_sig_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	if (rk818_cg_online(cg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		rk818_cg_set_finish_sig(cg, CHRG_FINISH_DIG_SIGNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		rk818_cg_set_finish_sig(cg, CHRG_FINISH_ANA_SIGNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) static void rk818_cg_set_chrg_param(struct rk818_charger *cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 				    enum charger_t charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	switch (charger) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	case USB_TYPE_NONE_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		cg->usb_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		cg->ac_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		if (cg->dc_in == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 			cg->prop_status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			rk818_cg_set_input_current(cg, INPUT_CUR450MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		power_supply_changed(cg->usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		power_supply_changed(cg->ac_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	case USB_TYPE_USB_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		cg->usb_in = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		cg->ac_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		cg->prop_status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		if (cg->dc_in == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			rk818_cg_set_input_current(cg, INPUT_CUR450MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		power_supply_changed(cg->usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		power_supply_changed(cg->ac_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	case USB_TYPE_CDP_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		cg->usb_in = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		cg->ac_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		cg->prop_status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		if (cg->dc_in == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 			rk818_cg_set_input_current(cg, INPUT_CUR1500MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		power_supply_changed(cg->usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		power_supply_changed(cg->ac_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	case USB_TYPE_AC_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		cg->ac_in = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		cg->usb_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		cg->prop_status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		if (charger == USB_TYPE_AC_CHARGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			if (cg->pdata->ts2_vol_multi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 				rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 				rk818_cg_set_input_current(cg, INPUT_CUR450MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 				queue_delayed_work(cg->ts2_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 						   &cg->ts2_vol_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 						   msecs_to_jiffies(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 				rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 				rk818_cg_set_input_current(cg, cg->chrg_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 			rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			rk818_cg_set_input_current(cg, INPUT_CUR1500MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		power_supply_changed(cg->usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		power_supply_changed(cg->ac_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	case DC_TYPE_DC_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		cg->dc_in = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		cg->prop_status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		if (cg->pdata->ts2_vol_multi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 			rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			rk818_cg_set_input_current(cg, INPUT_CUR450MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			queue_delayed_work(cg->ts2_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 					   &cg->ts2_vol_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 					   msecs_to_jiffies(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			rk818_cg_set_input_current(cg, cg->chrg_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		power_supply_changed(cg->usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		power_supply_changed(cg->ac_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	case DC_TYPE_NONE_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		cg->dc_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		buf = rk818_reg_read(cg, RK818_VB_MON_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		if ((buf & PLUG_IN_STS) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			cg->ac_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 			cg->usb_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			cg->prop_status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			rk818_cg_set_input_current(cg, INPUT_CUR450MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		} else if (cg->usb_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 			rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			rk818_cg_set_input_current(cg, INPUT_CUR450MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			cg->prop_status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		power_supply_changed(cg->usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		power_supply_changed(cg->ac_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		cg->prop_status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	cg->charger_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	if (rk818_cg_online(cg) && rk818_cg_get_dsoc(cg) == 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		cg->prop_status = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	if (cg->finish_sig_wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		queue_delayed_work(cg->finish_sig_wq, &cg->finish_sig_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 				   msecs_to_jiffies(1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) static void rk818_cg_set_otg_in(struct rk818_charger *cg, int online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	cg->otg_in = online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) static void rk818_cg_set_otg_power(struct rk818_charger *cg, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	case USB_OTG_POWER_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		if (cg->otg_pmic5v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 			CG_INFO("otg5v is on yet, ignore..\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 			if (IS_ERR(cg->otg5v_rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 				CG_INFO("not get otg_switch regulator!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			if (!regulator_is_enabled(cg->otg5v_rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 				ret = regulator_enable(cg->otg5v_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 				if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 					CG_INFO("enable otg5v failed:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 						ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 					return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 			cg->otg_pmic5v = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 			disable_irq(cg->plugin_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			disable_irq(cg->plugout_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			CG_INFO("enable otg5v\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	case USB_OTG_POWER_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		if (!cg->otg_pmic5v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			CG_INFO("otg5v is off yet, ignore..\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			if (IS_ERR(cg->otg5v_rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 				CG_INFO("not get otg_switch regulator!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			if (regulator_is_enabled(cg->otg5v_rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 				ret = regulator_disable(cg->otg5v_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 				if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 					CG_INFO("disable otg5v failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 						ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 					return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			cg->otg_pmic5v = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			enable_irq(cg->plugin_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 			enable_irq(cg->plugout_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			CG_INFO("disable otg5v\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		dev_err(cg->dev, "error otg type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) static enum charger_t rk818_cg_get_dc_state(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	if (!gpio_is_valid(cg->pdata->dc_det_pin))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		return DC_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	level = gpio_get_value(cg->pdata->dc_det_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	return (level == cg->pdata->dc_det_level) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		DC_TYPE_DC_CHARGER : DC_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) static void rk818_cg_dc_det_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	enum charger_t charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	struct rk818_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			struct rk818_charger, dc_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	charger = rk818_cg_get_dc_state(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	if (charger == DC_TYPE_DC_CHARGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		CG_INFO("detect dc charger in..\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		rk818_cg_set_chrg_param(cg, DC_TYPE_DC_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		/* check otg supply */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		if (cg->otg_in && cg->pdata->power_dc2otg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			CG_INFO("otg power from dc adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			rk818_cg_set_otg_power(cg, USB_OTG_POWER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		CG_INFO("detect dc charger out..\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		rk818_cg_set_chrg_param(cg, DC_TYPE_NONE_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		/* check otg supply, power on anyway */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		if (cg->otg_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			rk818_cg_set_otg_power(cg, USB_OTG_POWER_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	rk_send_wakeup_key();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	rk818_cg_pr_info(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) static u8 rk818_cg_decode_chrg_vol(struct rk818_charger *cg, u32 chrg_vol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	u8 val = 0, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	for (index = 0; index < ARRAY_SIZE(chrg_vol_sel_array); index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		if (chrg_vol < chrg_vol_sel_array[index])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		val = index << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) static u8 rk818_cg_decode_input_current(struct rk818_charger *cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 					u32 input_current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	u8 val = 0, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	for (index = 2; index < ARRAY_SIZE(chrg_cur_input_array); index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		if (input_current < 850 && input_current > 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 			val = 0x0;	/* 450mA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		} else if (input_current <= 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			val = 0x1;	/* 80mA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			if (input_current < chrg_cur_input_array[index])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			val = index <<  0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) static u8 rk818_cg_decode_chrg_current(struct rk818_charger *cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 				       u32 chrg_current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	u8 val = 0, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	if (cg->pdata->sample_res == SAMPLE_RES_10MR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		if (chrg_current > 2000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			chrg_current /= cg->res_div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 			chrg_current = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	for (index = 0; index < ARRAY_SIZE(chrg_cur_sel_array); index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		if (chrg_current < chrg_cur_sel_array[index])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		val = index << 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) static void rk818_cg_init_config(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	u8 usb_ctrl, sup_sts, chrg_ctrl1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	cg->chrg_voltage = rk818_cg_decode_chrg_vol(cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 				cg->pdata->max_chrg_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	cg->chrg_current = rk818_cg_decode_chrg_current(cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 				cg->pdata->max_chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	cg->chrg_input = rk818_cg_decode_input_current(cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 				cg->pdata->max_input_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	sup_sts = rk818_reg_read(cg, RK818_SUP_STS_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	usb_ctrl = rk818_reg_read(cg, RK818_USB_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	/* set charge current and voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	usb_ctrl &= ~INPUT_CUR_MSK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	usb_ctrl |= cg->chrg_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	chrg_ctrl1 = (CHRG_EN | cg->chrg_voltage | cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	/* disable voltage limit and enable input current limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	sup_sts &= ~USB_VLIMIT_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	sup_sts |= USB_CLIMIT_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	rk818_reg_write(cg, RK818_SUP_STS_REG, sup_sts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	rk818_reg_write(cg, RK818_USB_CTRL_REG, usb_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	rk818_reg_write(cg, RK818_CHRG_CTRL_REG1, chrg_ctrl1);
^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) static void rk818_ts2_vol_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	struct rk818_charger *cg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	int ts2_vol, input_current, invalid_cnt = 0, confirm_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	cg = container_of(work, struct rk818_charger, ts2_vol_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	input_current = INPUT_CUR80MA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	while (input_current < cg->chrg_input) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		msleep(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		ts2_vol = rk818_cg_get_ts2_voltage(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		/* filter invalid voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		if (ts2_vol <= DEFAULT_TS2_VALID_VOL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			invalid_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 			DBG("%s: invalid ts2 voltage: %d\n, cnt=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			    __func__, ts2_vol, invalid_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			if (invalid_cnt < DEFAULT_TS2_CHECK_CNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			/* if fail, set max input current as default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			input_current = cg->chrg_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			rk818_cg_set_input_current(cg, input_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		/* update input current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		if (ts2_vol >= DEFAULT_TS2_THRESHOLD_VOL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			/* update input current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			input_current++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			rk818_cg_set_input_current(cg, input_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			DBG("********* input=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 			    chrg_cur_input_array[input_current & 0x0f]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 			/* confirm lower threshold voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 			confirm_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			if (confirm_cnt < DEFAULT_TS2_CHECK_CNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 				DBG("%s: confirm ts2 voltage: %d\n, cnt=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 				    __func__, ts2_vol, confirm_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			/* trigger threshold, so roll back 1 step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			input_current--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			if (input_current == INPUT_CUR80MA ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 			    input_current < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 				input_current = INPUT_CUR450MA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			rk818_cg_set_input_current(cg, input_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (input_current != cg->chrg_input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		CG_INFO("adjust input current: %dma\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			chrg_cur_input_array[input_current & 0x0f]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) static int rk818_cg_charger_evt_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 					 unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	struct rk818_charger *cg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		container_of(nb, struct rk818_charger, cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	queue_delayed_work(cg->usb_charger_wq, &cg->usb_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) static int rk818_cg_discnt_evt_notfier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 				       unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	struct rk818_charger *cg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		container_of(nb, struct rk818_charger, cable_discnt_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	queue_delayed_work(cg->usb_charger_wq, &cg->discnt_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) static int rk818_cg_host_evt_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 				      unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	struct rk818_charger *cg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		container_of(nb, struct rk818_charger, cable_host_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	queue_delayed_work(cg->usb_charger_wq, &cg->host_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) static int rk818_cg_bc_evt_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 				    unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	struct rk818_charger *cg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		container_of(nb, struct rk818_charger, bc_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	cg->bc_event = event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	queue_delayed_work(cg->usb_charger_wq, &cg->usb_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) static void rk818_cg_bc_evt_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	struct rk818_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 					struct rk818_charger, usb_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	const char *event_name[] = {"DISCNT", "USB", "AC", "CDP1.5A",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 				    "UNKNOWN", "OTG ON", "OTG OFF"};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	switch (cg->bc_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	case USB_BC_TYPE_DISCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		rk818_cg_set_chrg_param(cg, USB_TYPE_NONE_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	case USB_BC_TYPE_SDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		rk818_cg_set_chrg_param(cg, USB_TYPE_USB_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	case USB_BC_TYPE_DCP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		rk818_cg_set_chrg_param(cg, USB_TYPE_AC_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	case USB_BC_TYPE_CDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		rk818_cg_set_chrg_param(cg, USB_TYPE_CDP_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	case USB_OTG_POWER_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		rk818_cg_set_otg_in(cg, ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		if (cg->pdata->power_dc2otg && cg->dc_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			CG_INFO("otg power from dc adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			rk818_cg_set_otg_power(cg, USB_OTG_POWER_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	case USB_OTG_POWER_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		rk818_cg_set_otg_in(cg, OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		rk818_cg_set_otg_power(cg, USB_OTG_POWER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	CG_INFO("receive bc notifier event: %s..\n", event_name[cg->bc_event]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	rk818_cg_pr_info(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) static void rk818_cg_irq_delay_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	struct rk818_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			struct rk818_charger, irq_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	if (cg->plugin_trigger) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		CG_INFO("pmic: plug in\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		cg->plugin_trigger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		rk_send_wakeup_key();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		if (cg->pdata->extcon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 			queue_delayed_work(cg->usb_charger_wq, &cg->usb_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 					   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	} else if (cg->plugout_trigger) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		CG_INFO("pmic: plug out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		cg->plugout_trigger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		rk818_cg_set_chrg_param(cg, USB_TYPE_NONE_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		rk818_cg_set_chrg_param(cg, DC_TYPE_NONE_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		rk_send_wakeup_key();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		rk818_cg_pr_info(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		CG_INFO("pmic: unknown irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) static irqreturn_t rk818_plug_in_isr(int irq, void *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	struct rk818_charger *icg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	icg = (struct rk818_charger *)cg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	icg->plugin_trigger = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	queue_delayed_work(icg->usb_charger_wq, &icg->irq_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 			   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) static irqreturn_t rk818_plug_out_isr(int irq, void *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct rk818_charger *icg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	icg = (struct rk818_charger *)cg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	icg->plugout_trigger = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	queue_delayed_work(icg->usb_charger_wq, &icg->irq_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) static irqreturn_t rk818_dc_det_isr(int irq, void *charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	struct rk818_charger *cg = (struct rk818_charger *)charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	if (gpio_get_value(cg->pdata->dc_det_pin))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		irq_set_irq_type(irq, IRQF_TRIGGER_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		irq_set_irq_type(irq, IRQF_TRIGGER_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	queue_delayed_work(cg->dc_charger_wq, &cg->dc_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) static int rk818_cg_init_irqs(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	struct rk808 *rk818 = cg->rk818;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	struct platform_device *pdev = cg->pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	int ret, plug_in_irq, plug_out_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	plug_in_irq = regmap_irq_get_virq(rk818->irq_data, RK818_IRQ_PLUG_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (plug_in_irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		dev_err(cg->dev, "plug_in_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		return plug_in_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	plug_out_irq = regmap_irq_get_virq(rk818->irq_data, RK818_IRQ_PLUG_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	if (plug_out_irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		dev_err(cg->dev, "plug_out_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		return plug_out_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	ret = devm_request_threaded_irq(cg->dev, plug_in_irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 					rk818_plug_in_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 					IRQF_TRIGGER_RISING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 					"rk818_plug_in", cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		dev_err(&pdev->dev, "plug_in_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	ret = devm_request_threaded_irq(cg->dev, plug_out_irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 					rk818_plug_out_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 					IRQF_TRIGGER_FALLING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 					"rk818_plug_out", cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		dev_err(&pdev->dev, "plug_out_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	cg->plugin_irq = plug_in_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	cg->plugout_irq = plug_out_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	INIT_DELAYED_WORK(&cg->irq_work, rk818_cg_irq_delay_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) static int rk818_cg_init_dc(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	int ret, level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	unsigned int dc_det_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	cg->dc_charger_wq = alloc_ordered_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 				WQ_MEM_RECLAIM | WQ_FREEZABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 				"rk818-dc-wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	INIT_DELAYED_WORK(&cg->dc_work, rk818_cg_dc_det_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	cg->dc_charger = DC_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	if (!cg->pdata->support_dc_det)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	ret = devm_gpio_request(cg->dev, cg->pdata->dc_det_pin, "rk818_dc_det");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		dev_err(cg->dev, "failed to request gpio %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 			cg->pdata->dc_det_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	ret = gpio_direction_input(cg->pdata->dc_det_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		dev_err(cg->dev, "failed to set gpio input\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	level = gpio_get_value(cg->pdata->dc_det_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	if (level == cg->pdata->dc_det_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		cg->dc_charger = DC_TYPE_DC_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		cg->dc_charger = DC_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	if (level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		irq_flags = IRQF_TRIGGER_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		irq_flags = IRQF_TRIGGER_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	dc_det_irq = gpio_to_irq(cg->pdata->dc_det_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	ret = devm_request_irq(cg->dev, dc_det_irq, rk818_dc_det_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			       irq_flags, "rk818_dc_det", cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		dev_err(cg->dev, "rk818_dc_det_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	enable_irq_wake(dc_det_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) static void rk818_cg_discnt_evt_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	struct rk818_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			struct rk818_charger, discnt_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	if (extcon_get_state(cg->cable_edev, EXTCON_USB) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		CG_INFO("receive type-c notifier event: DISCNT...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		rk818_cg_set_chrg_param(cg, USB_TYPE_NONE_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		rk818_cg_pr_info(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) static void rk818_cg_host_evt_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	struct rk818_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			struct rk818_charger, host_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	struct extcon_dev *edev = cg->cable_edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	/* Determine charger type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	if (extcon_get_state(edev, EXTCON_USB_VBUS_EN) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		rk818_cg_set_otg_in(cg, ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		CG_INFO("receive type-c notifier event: OTG ON...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		if (cg->dc_in && cg->pdata->power_dc2otg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 			CG_INFO("otg power from dc adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			rk818_cg_set_otg_power(cg, USB_OTG_POWER_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	} else if (extcon_get_state(edev, EXTCON_USB_VBUS_EN) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		CG_INFO("receive type-c notifier event: OTG OFF...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		rk818_cg_set_otg_in(cg, OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		rk818_cg_set_otg_power(cg, USB_OTG_POWER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	rk818_cg_pr_info(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) static void rk818_cg_charger_evt_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	struct rk818_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 				struct rk818_charger, usb_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	struct extcon_dev *edev = cg->cable_edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	enum charger_t charger = USB_TYPE_UNKNOWN_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	const char *event[] = {"UN", "NONE", "USB", "AC", "CDP1.5A"};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	/* Determine charger type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	if (extcon_get_state(edev, EXTCON_CHG_USB_SDP) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		charger = USB_TYPE_USB_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	else if (extcon_get_state(edev, EXTCON_CHG_USB_DCP) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		charger = USB_TYPE_AC_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	else if (extcon_get_state(edev, EXTCON_CHG_USB_CDP) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		charger = USB_TYPE_CDP_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	if (charger != USB_TYPE_UNKNOWN_CHARGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		CG_INFO("receive type-c notifier event: %s...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			event[charger]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		cg->usb_charger = charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		rk818_cg_set_chrg_param(cg, charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		rk818_cg_pr_info(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) static long rk818_cg_init_usb(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	enum charger_t charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	enum bc_port_type bc_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	struct extcon_dev *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	struct device *dev = cg->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	cg->usb_charger_wq = alloc_ordered_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 				WQ_MEM_RECLAIM | WQ_FREEZABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 				"rk818-usb-wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	cg->usb_charger = USB_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	/* type-C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	if (cg->pdata->extcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		edev = extcon_get_edev_by_phandle(dev->parent, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		if (IS_ERR(edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			if (PTR_ERR(edev) != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 				dev_err(dev, "Invalid or missing extcon\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 			return PTR_ERR(edev);
^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) 		/* Register chargers  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		INIT_DELAYED_WORK(&cg->usb_work, rk818_cg_charger_evt_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		cg->cable_cg_nb.notifier_call = rk818_cg_charger_evt_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		ret = extcon_register_notifier(edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 					       &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			dev_err(dev, "failed to register notifier for SDP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		ret = extcon_register_notifier(edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 					       &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			dev_err(dev, "failed to register notifier for DCP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		ret = extcon_register_notifier(edev, EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 					       &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 			dev_err(dev, "failed to register notifier for CDP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		/* Register host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		INIT_DELAYED_WORK(&cg->host_work, rk818_cg_host_evt_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		cg->cable_host_nb.notifier_call = rk818_cg_host_evt_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		ret = extcon_register_notifier(edev, EXTCON_USB_VBUS_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 					       &cg->cable_host_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			dev_err(dev, "failed to register notifier for HOST\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		/* Register discnt usb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		INIT_DELAYED_WORK(&cg->discnt_work, rk818_cg_discnt_evt_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		cg->cable_discnt_nb.notifier_call = rk818_cg_discnt_evt_notfier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		ret = extcon_register_notifier(edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 					       &cg->cable_discnt_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			dev_err(dev, "failed to register notifier for HOST\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			extcon_unregister_notifier(edev, EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 						   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			extcon_unregister_notifier(edev, EXTCON_USB_VBUS_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 						   &cg->cable_host_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		cg->cable_edev = edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		schedule_delayed_work(&cg->host_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		schedule_delayed_work(&cg->usb_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		CG_INFO("register typec extcon evt notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		INIT_DELAYED_WORK(&cg->usb_work, rk818_cg_bc_evt_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		cg->bc_nb.notifier_call = rk818_cg_bc_evt_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		ret = rk_bc_detect_notifier_register(&cg->bc_nb, &bc_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			dev_err(dev, "failed to register notifier for bc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		switch (bc_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		case USB_BC_TYPE_DISCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			charger = USB_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		case USB_BC_TYPE_SDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		case USB_BC_TYPE_CDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			charger = USB_TYPE_USB_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		case USB_BC_TYPE_DCP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 			charger = USB_TYPE_AC_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			charger = USB_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		cg->usb_charger = charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		CG_INFO("register bc evt notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) static void rk818_cg_init_finish_sig(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	if (rk818_cg_online(cg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		rk818_cg_set_finish_sig(cg, CHRG_FINISH_DIG_SIGNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		rk818_cg_set_finish_sig(cg, CHRG_FINISH_ANA_SIGNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	cg->finish_sig_wq = alloc_ordered_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 				WQ_MEM_RECLAIM | WQ_FREEZABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 				"rk818-finish-sig-wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	INIT_DELAYED_WORK(&cg->finish_sig_work, rk818_cg_finish_sig_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) static void rk818_cg_init_ts2_detect(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	u8 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	cg->ts2_wq = alloc_ordered_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 				WQ_MEM_RECLAIM | WQ_FREEZABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 				"rk818-ts2-wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	INIT_DELAYED_WORK(&cg->ts2_vol_work, rk818_ts2_vol_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	if (!cg->pdata->ts2_vol_multi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	/* TS2 adc mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	buf = rk818_reg_read(cg, RK818_TS_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	buf |= TS2_FUN_ADC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	rk818_reg_write(cg, RK818_TS_CTRL_REG, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	/* TS2 adc enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	buf = rk818_reg_read(cg, RK818_ADC_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	buf |= ADC_TS2_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	rk818_reg_write(cg, RK818_ADC_CTRL_REG, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	CG_INFO("enable ts2 voltage detect, multi=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		cg->pdata->ts2_vol_multi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) static void rk818_cg_init_charger_state(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	rk818_cg_init_config(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	rk818_cg_init_finish_sig(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	rk818_cg_set_chrg_param(cg, cg->dc_charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	rk818_cg_set_chrg_param(cg, cg->usb_charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	if (cg->otg_in && cg->dc_in && cg->pdata->power_dc2otg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		CG_INFO("otg power from dc adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		rk818_cg_set_otg_power(cg, USB_OTG_POWER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	CG_INFO("ac=%d, usb=%d, dc=%d, otg=%d, 5v=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		cg->ac_in, cg->usb_in, cg->dc_in, cg->otg_in, cg->otg_pmic5v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) static int rk818_cg_temperature_notifier_call(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 					      unsigned long temp, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	struct rk818_charger *cg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		container_of(nb, struct rk818_charger, temp_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	static int temp_triggered, config_index = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	int i, up_temp, down_temp, cfg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	int now_temp = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	u8 usb_ctrl, chrg_ctrl1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	DBG("%s: receive notify temperature = %d\n", __func__, now_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	for (i = 0; i < cg->pdata->tc_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		up_temp = cg->pdata->tc_table[i].temp_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		down_temp = cg->pdata->tc_table[i].temp_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		cfg_current = cg->pdata->tc_table[i].chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		if (now_temp >= down_temp && now_temp <= up_temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			/* Temp range or charger are not update, return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 			if (config_index == i && !cg->charger_changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 				return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 			config_index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 			cg->charger_changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 			temp_triggered = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			if (cg->pdata->tc_table[i].set_chrg_current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 				rk818_cg_set_chrg_current(cg, cfg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 				CG_INFO("temperature = %d'C[%d~%d'C], "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 					"chrg current = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 					now_temp, down_temp, up_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 					chrg_cur_sel_array[cfg_current] *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 					cg->res_div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 				rk818_cg_set_input_current(cg, cfg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 				CG_INFO("temperature = %d'C[%d~%d'C], "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 					"input current = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 					now_temp, down_temp, up_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 					chrg_cur_input_array[cfg_current]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 			return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	 * means: current temperature not covers above case, temperature rolls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	 * back to normal range, so restore default value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	if (temp_triggered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		temp_triggered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		config_index = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		rk818_cg_set_chrg_current(cg, cg->chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		if (cg->ac_in || cg->dc_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			rk818_cg_set_input_current(cg, cg->chrg_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 			rk818_cg_set_input_current(cg, INPUT_CUR450MA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		usb_ctrl = rk818_reg_read(cg, RK818_USB_CTRL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		chrg_ctrl1 = rk818_reg_read(cg, RK818_CHRG_CTRL_REG1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		CG_INFO("roll back temp %d'C, current chrg = %d, input = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 			now_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 			chrg_cur_sel_array[(chrg_ctrl1 & 0x0f)] * cg->res_div,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 			chrg_cur_input_array[(usb_ctrl & 0x0f)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) static int parse_temperature_chrg_table(struct rk818_charger *cg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 					struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	int size, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	int i, chrg_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	const __be32 *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (!of_find_property(np, "temperature_chrg_table_v2", &size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	list = of_get_property(np, "temperature_chrg_table_v2", &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	size /= sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	if (!size || (size % 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		dev_err(cg->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 			"invalid temperature_chrg_table: size=%d\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	count = size / 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	cg->pdata->tc_count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	cg->pdata->tc_table = devm_kzalloc(cg->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 					   count * sizeof(*cg->pdata->tc_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 					   GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	if (!cg->pdata->tc_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		/* temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		cg->pdata->tc_table[i].temp_down = be32_to_cpu(*list++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		cg->pdata->tc_table[i].temp_up = be32_to_cpu(*list++);
^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) 		 * because charge current lowest level is 1000mA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		 * higher than or equal 1000ma, select charge current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		 * lower than 1000ma, must select input current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		chrg_current = be32_to_cpu(*list++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		if (chrg_current >= 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 			cg->pdata->tc_table[i].set_chrg_current = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 			cg->pdata->tc_table[i].chrg_current =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 				rk818_cg_decode_chrg_current(cg, chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 			cg->pdata->tc_table[i].chrg_current =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 				rk818_cg_decode_input_current(cg, chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		DBG("temp%d: [%d, %d], chrg_current=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		    i, cg->pdata->tc_table[i].temp_down,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		    cg->pdata->tc_table[i].temp_up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		    cg->pdata->tc_table[i].chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) static int rk818_cg_register_temp_notifier(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (!cg->pdata->tc_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	cg->temp_nb.notifier_call = rk818_cg_temperature_notifier_call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	ret = rk818_bat_temp_notifier_register(&cg->temp_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		dev_err(cg->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			"battery temperature notify register failed:%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	CG_INFO("enable set charge current by temperature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) static int rk818_cg_get_otg5v_regulator(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	/* not necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	cg->otg5v_rdev = devm_regulator_get(cg->dev, "otg_switch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	if (IS_ERR(cg->otg5v_rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		ret = PTR_ERR(cg->otg5v_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		dev_warn(cg->dev, "failed to get otg regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) static int rk818_cg_parse_dt(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	struct charger_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	enum of_gpio_flags flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	struct device *dev = cg->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	np = of_find_node_by_name(cg->pdev->dev.of_node, "battery");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		dev_err(dev, "battery node not found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	cg->pdata = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	pdata->max_chrg_current = DEFAULT_CHRG_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	pdata->max_input_current = DEFAULT_INPUT_CURRENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	pdata->max_chrg_voltage = DEFAULT_CHRG_VOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	pdata->extcon = device_property_read_bool(dev->parent, "extcon");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	ret = of_property_read_u32(np, "max_chrg_current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 				   &pdata->max_chrg_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		dev_err(dev, "max_chrg_current missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	ret = of_property_read_u32(np, "max_input_current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 				   &pdata->max_input_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		dev_err(dev, "max_input_current missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	ret = of_property_read_u32(np, "max_chrg_voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 				   &pdata->max_chrg_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		dev_err(dev, "max_chrg_voltage missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	ret = of_property_read_u32(np, "virtual_power", &pdata->virtual_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		dev_err(dev, "virtual_power missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	ret = of_property_read_u32(np, "power_dc2otg", &pdata->power_dc2otg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		dev_err(dev, "power_dc2otg missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	ret = of_property_read_u32(np, "sample_res", &pdata->sample_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		pdata->sample_res = SAMPLE_RES_20MR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		dev_err(dev, "sample_res missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	ret = of_property_read_u32(np, "otg5v_suspend_enable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 				   &pdata->otg5v_suspend_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		pdata->otg5v_suspend_enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		dev_err(dev, "otg5v_suspend_enable missing!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	ret = of_property_read_u32(np, "ts2_vol_multi",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 				   &pdata->ts2_vol_multi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	if (!is_battery_exist(cg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		pdata->virtual_power = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	cg->res_div = (cg->pdata->sample_res == SAMPLE_RES_20MR) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		       SAMPLE_RES_DIV1 : SAMPLE_RES_DIV2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	if (!of_find_property(np, "dc_det_gpio", &ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		pdata->support_dc_det = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		CG_INFO("not support dc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 		pdata->support_dc_det = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		pdata->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 							    0, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		if (gpio_is_valid(pdata->dc_det_pin)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 			CG_INFO("support dc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 			pdata->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 					       0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 			dev_err(dev, "invalid dc det gpio!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	ret = parse_temperature_chrg_table(cg, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	DBG("input_current:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	    "chrg_current:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	    "chrg_voltage:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	    "sample_res:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	    "extcon:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	    "ts2_vol_multi:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	    "virtual_power:%d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	    "power_dc2otg:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	    pdata->max_input_current, pdata->max_chrg_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	    pdata->max_chrg_voltage, pdata->sample_res, pdata->extcon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	    pdata->ts2_vol_multi, pdata->virtual_power, pdata->power_dc2otg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) static int rk818_cg_parse_dt(struct rk818_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) static int rk818_charger_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	struct rk808 *rk818 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	struct rk818_charger *cg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	cg = devm_kzalloc(&pdev->dev, sizeof(*cg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	if (!cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	cg->rk818 = rk818;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	cg->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	cg->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	cg->regmap = rk818->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	platform_set_drvdata(pdev, cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	ret = rk818_cg_parse_dt(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		dev_err(cg->dev, "parse dt failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	rk818_cg_init_ts2_detect(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	rk818_cg_get_otg5v_regulator(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	ret = rk818_cg_init_dc(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		dev_err(cg->dev, "init dc failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	ret = rk818_cg_init_usb(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		dev_err(cg->dev, "init usb failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	ret = rk818_cg_init_power_supply(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		dev_err(cg->dev, "init power supply fail!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	rk818_cg_init_charger_state(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	ret = rk818_cg_register_temp_notifier(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		dev_err(cg->dev, "register temp notify failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		goto notify_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	ret = rk818_cg_init_irqs(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		dev_err(cg->dev, "init irqs failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		goto irq_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	CG_INFO("driver version: %s\n", CG_DRIVER_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) irq_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	rk818_bat_temp_notifier_unregister(&cg->temp_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) notify_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	/* type-c only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	if (cg->pdata->extcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		cancel_delayed_work_sync(&cg->host_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		cancel_delayed_work_sync(&cg->discnt_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	cancel_delayed_work_sync(&cg->usb_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	cancel_delayed_work_sync(&cg->dc_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	cancel_delayed_work_sync(&cg->finish_sig_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	cancel_delayed_work_sync(&cg->irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	cancel_delayed_work_sync(&cg->ts2_vol_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	destroy_workqueue(cg->ts2_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	destroy_workqueue(cg->usb_charger_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	destroy_workqueue(cg->dc_charger_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	destroy_workqueue(cg->finish_sig_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	if (cg->pdata->extcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 					   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 					   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 					   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_USB_VBUS_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 					   &cg->cable_host_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 					   &cg->cable_discnt_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 		rk_bc_detect_notifier_unregister(&cg->bc_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) static void rk818_charger_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	struct rk818_charger *cg = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	/* type-c only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	if (cg->pdata->extcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		cancel_delayed_work_sync(&cg->host_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		cancel_delayed_work_sync(&cg->discnt_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	rk818_cg_set_otg_power(cg, USB_OTG_POWER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	disable_irq(cg->plugin_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	disable_irq(cg->plugout_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	cancel_delayed_work_sync(&cg->usb_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	cancel_delayed_work_sync(&cg->dc_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	cancel_delayed_work_sync(&cg->finish_sig_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	cancel_delayed_work_sync(&cg->irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	cancel_delayed_work_sync(&cg->ts2_vol_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	flush_workqueue(cg->ts2_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	flush_workqueue(cg->usb_charger_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	flush_workqueue(cg->dc_charger_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	flush_workqueue(cg->finish_sig_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	if (cg->pdata->extcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 					   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 					   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 					   &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_USB_VBUS_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 					   &cg->cable_host_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		extcon_unregister_notifier(cg->cable_edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 					   &cg->cable_discnt_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		rk_bc_detect_notifier_unregister(&cg->bc_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	rk818_bat_temp_notifier_unregister(&cg->temp_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	rk818_cg_set_finish_sig(cg, CHRG_FINISH_ANA_SIGNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	CG_INFO("shutdown: ac=%d usb=%d dc=%d otg=%d 5v=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		cg->ac_in, cg->usb_in, cg->dc_in, cg->otg_in, cg->otg_pmic5v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) static int rk818_charger_suspend(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 				 pm_message_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	struct rk818_charger *cg = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	cg->sleep_set_off_reg1 = rk818_reg_read(cg, RK818_SLEEP_SET_OFF_REG1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	/* enable sleep boost5v and otg5v */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	if (cg->pdata->otg5v_suspend_enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		if ((cg->otg_in && !cg->dc_in) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		    (cg->otg_in && cg->dc_in && !cg->pdata->power_dc2otg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 			rk818_reg_clear_bits(cg, RK818_SLEEP_SET_OFF_REG1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 					     OTG_BOOST_SLP_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			CG_INFO("suspend: otg 5v on\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	/* disable sleep otg5v */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	rk818_reg_set_bits(cg, RK818_SLEEP_SET_OFF_REG1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 			   OTG_SLP_SET_OFF, OTG_SLP_SET_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	CG_INFO("suspend: otg 5v off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) static int rk818_charger_resume(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	struct rk818_charger *cg = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	/* resume sleep boost5v and otg5v */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	rk818_reg_set_bits(cg, RK818_SLEEP_SET_OFF_REG1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 			   OTG_BOOST_SLP_OFF, cg->sleep_set_off_reg1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) static struct platform_driver rk818_charger_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	.probe = rk818_charger_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	.suspend = rk818_charger_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	.resume = rk818_charger_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	.shutdown = rk818_charger_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		.name	= "rk818-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) static int __init charger_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	return platform_driver_register(&rk818_charger_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) module_init(charger_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) static void __exit charger_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	platform_driver_unregister(&rk818_charger_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) module_exit(charger_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) MODULE_ALIAS("platform:rk818-charger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) MODULE_AUTHOR("chenjh<chenjh@rock-chips.com>");