Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Fuel gauge driver for CellWise 2017
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2012, RockChip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Authors: Shunqing Chen <csq@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/bits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define CW2017_SIZE_BATINFO		80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define CW2017_REG_VERSION		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define CW2017_REG_VCELL_H		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define CW2017_REG_VCELL_L		0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define CW2017_REG_SOC_INT		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define CW2017_REG_SOC_DECIMAL		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define CW2017_REG_TEMP			0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define CW2017_REG_MODE_CONFIG		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define CW2017_REG_INT_CONFIG		0x0A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define CW2017_REG_SOC_ALERT		0x0B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define CW2017_REG_TEMP_MAX		0x0C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define CW2017_REG_TEMP_MIN		0x0D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define CW2017_REG_VOLT_ID_H		0x0E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define CW2017_REG_VOLT_ID_L		0x0F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define CW2017_REG_BATINFO		0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define CW2017_MODE_SLEEP		0x30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define CW2017_MODE_NORMAL		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define CW2017_MODE_DEFAULT		0xF0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define CW2017_CONFIG_UPDATE_FLG	0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define NO_START_VERSION		160
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define	TEMP_MAX_ALERT			0xFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define	TEMP_MIN_ALERT			0xFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define TEMP_ALERT_DISABLE		0xFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define INT_CONFIG_MIN_TEMP_MARK        BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define INT_CONFIG_MAX_TEMP_MARK        BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define INT_CONFIG_SOC_CHANGE_MARK      BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define DEF_DESIGN_CAPACITY		4000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define CW2017_MASK_ATHD		GENMASK(7, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) /* reset gauge of no valid state of charge could be polled for 40s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define CW2017_BAT_SOC_ERROR_MS		(40 * MSEC_PER_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) /* reset gauge if state of charge stuck for half an hour during charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define CW2017_BAT_CHARGING_STUCK_MS	(1800 * MSEC_PER_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) /* poll interval from CellWise GPL Android driver example */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define CW2017_DEFAULT_POLL_INTERVAL_MS	8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) struct cw_battery {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct workqueue_struct *battery_workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct delayed_work battery_delay_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct power_supply *rk_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct power_supply_battery_info battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u8 *bat_profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	bool charger_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	bool battery_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	int soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	int charge_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int design_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u32 poll_interval_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	u32 alert_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	int temp_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	int temp_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	int temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	bool dual_cell;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	unsigned int read_errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	unsigned int charge_stuck_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static int cw_read_word(struct cw_battery *cw_bat, u8 reg, u16 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	__be16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	*val = be16_to_cpu(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return 0;
^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) static void cw2017_enable(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	unsigned char reg_val = CW2017_MODE_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	regmap_write(cw_bat->regmap, CW2017_REG_MODE_CONFIG, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	reg_val = CW2017_MODE_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	regmap_write(cw_bat->regmap, CW2017_REG_MODE_CONFIG, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	reg_val = CW2017_MODE_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	regmap_write(cw_bat->regmap, CW2017_REG_MODE_CONFIG, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int cw_update_profile(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	unsigned int reg_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unsigned char int_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/* write new battery info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	ret = regmap_raw_write(cw_bat->regmap, CW2017_REG_BATINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			       cw_bat->bat_profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			       CW2017_SIZE_BATINFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	/* set config update flag  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	reg_val |= CW2017_CONFIG_UPDATE_FLG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	reg_val &= ~CW2017_MASK_ATHD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	reg_val |= cw_bat->alert_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	regmap_write(cw_bat->regmap, CW2017_REG_SOC_ALERT, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (cw_bat->alert_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		int_mask |= INT_CONFIG_SOC_CHANGE_MARK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	cw_bat->temp_max = TEMP_MAX_ALERT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	cw_bat->temp_min = TEMP_MIN_ALERT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (cw_bat->temp_max != TEMP_ALERT_DISABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		int_mask |= INT_CONFIG_MAX_TEMP_MARK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		reg_val = (cw_bat->temp_max + 40) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		regmap_write(cw_bat->regmap, CW2017_REG_TEMP_MAX, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (cw_bat->temp_min != TEMP_ALERT_DISABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		int_mask |= INT_CONFIG_MIN_TEMP_MARK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		reg_val = (cw_bat->temp_min + 40) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		regmap_write(cw_bat->regmap, CW2017_REG_TEMP_MIN, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	regmap_write(cw_bat->regmap, CW2017_REG_INT_CONFIG, int_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	/* wait for gauge to reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/* wait for gauge to become ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	ret = regmap_read_poll_timeout(cw_bat->regmap, CW2017_REG_SOC_INT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 				       reg_val, reg_val <= 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				       10 * USEC_PER_MSEC, 10 * USEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		dev_err(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			"Gauge did not become ready after profile upload\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		dev_dbg(cw_bat->dev, "Battery profile updated\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	cw2017_enable(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	dev_dbg(cw_bat->dev, "Battery profile configured\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int cw_init(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	unsigned int config_flg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	regmap_read(cw_bat->regmap, CW2017_REG_MODE_CONFIG, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	regmap_read(cw_bat->regmap, CW2017_REG_SOC_ALERT, &config_flg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (reg_val != CW2017_MODE_NORMAL || !(config_flg & CW2017_CONFIG_UPDATE_FLG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		dev_dbg(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			"Battery profile not present, uploading battery profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (cw_bat->bat_profile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			ret = cw_update_profile(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 				dev_err(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 					"Failed to upload battery profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 				 "No profile specified, continuing without profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	} else if (cw_bat->bat_profile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		u8 bat_info[CW2017_SIZE_BATINFO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		ret = regmap_raw_read(cw_bat->regmap, CW2017_REG_BATINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				      bat_info, CW2017_SIZE_BATINFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			dev_err(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				"Failed to read stored battery profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (memcmp(bat_info, cw_bat->bat_profile, CW2017_SIZE_BATINFO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			dev_warn(cw_bat->dev, "Replacing stored battery profile\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			ret = cw_update_profile(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			 "Can't check current battery profile, no profile provided\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return 0;
^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) #define HYSTERESIS(current, previous, up, down) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	(((current) < (previous) + (up)) && ((current) > (previous) - (down)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int cw_get_soc(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	unsigned int soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	regmap_read(cw_bat->regmap, CW2017_REG_SOC_INT, &soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (soc > 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		int max_error_cycles =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			CW2017_BAT_SOC_ERROR_MS / cw_bat->poll_interval_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		dev_err(cw_bat->dev, "Invalid SoC %d%%\n", soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		cw_bat->read_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (cw_bat->read_errors > max_error_cycles) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				 "Too many invalid SoC reports, resetting gauge\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			cw_bat->read_errors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return cw_bat->soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	cw_bat->read_errors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	/* Reset gauge if stuck while charging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (cw_bat->status == POWER_SUPPLY_STATUS_CHARGING && soc == cw_bat->soc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		int max_stuck_cycles =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			CW2017_BAT_CHARGING_STUCK_MS / cw_bat->poll_interval_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		cw_bat->charge_stuck_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (cw_bat->charge_stuck_cnt > max_stuck_cycles) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 				 "SoC stuck @%u%%, resetting gauge\n", soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			cw_bat->charge_stuck_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		cw_bat->charge_stuck_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	/* Ignore voltage dips during charge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 0, 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		soc = cw_bat->soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	/* Ignore voltage spikes during discharge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (!cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 3, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		soc = cw_bat->soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	return soc;
^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 cw_get_voltage(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	int voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	u16 reg_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	cw_read_word(cw_bat, CW2017_REG_VCELL_H, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	reg_val &= 0x3fff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	voltage_mv = reg_val * 5 / 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (cw_bat->dual_cell)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		voltage_mv *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	dev_dbg(cw_bat->dev, "Read voltage: %d mV, raw=0x%04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		voltage_mv, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	return voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static void cw_update_charge_status(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	ret = power_supply_am_i_supplied(cw_bat->rk_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		dev_warn(cw_bat->dev, "Failed to get supply state: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		bool charger_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		charger_attached = !!ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (cw_bat->charger_attached != charger_attached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			cw_bat->battery_changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			if (charger_attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 				cw_bat->charge_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		cw_bat->charger_attached = charger_attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static void cw_update_soc(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	int soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	soc = cw_get_soc(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (soc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		dev_err(cw_bat->dev, "Failed to get SoC from gauge: %d\n", soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	else if (cw_bat->soc != soc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		cw_bat->soc = soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		cw_bat->battery_changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static void cw_update_voltage(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	int voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	voltage_mv = cw_get_voltage(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (voltage_mv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		dev_err(cw_bat->dev, "Failed to get voltage from gauge: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			voltage_mv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		cw_bat->voltage_mv = voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int cw_update_temp(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	unsigned int val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	regmap_read(cw_bat->regmap, CW2017_REG_TEMP, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	cw_bat->temp = val * 10 / 2 - 400;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return cw_bat->temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static void cw_update_status(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	int status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (cw_bat->charger_attached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		if (cw_bat->soc >= 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			status = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (cw_bat->status != status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		cw_bat->battery_changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	cw_bat->status = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static void cw_bat_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct delayed_work *delay_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	struct cw_battery *cw_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	delay_work = to_delayed_work(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	cw_bat = container_of(delay_work, struct cw_battery, battery_delay_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	cw_update_soc(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	cw_update_voltage(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	cw_update_charge_status(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	cw_update_temp(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	cw_update_status(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	dev_dbg(cw_bat->dev, "charger_attached = %d\n", cw_bat->charger_attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	dev_dbg(cw_bat->dev, "status = %d\n", cw_bat->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	dev_dbg(cw_bat->dev, "soc = %d%%\n", cw_bat->soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	dev_dbg(cw_bat->dev, "voltage = %dmV\n", cw_bat->voltage_mv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (cw_bat->battery_changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		power_supply_changed(cw_bat->rk_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	cw_bat->battery_changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	queue_delayed_work(cw_bat->battery_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			   &cw_bat->battery_delay_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			   msecs_to_jiffies(cw_bat->poll_interval_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static int cw_battery_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 				   enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 				   union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	struct cw_battery *cw_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	cw_bat = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		val->intval = cw_bat->soc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		val->intval = cw_bat->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		val->intval = !!cw_bat->voltage_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		val->intval = cw_bat->voltage_mv * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		val->intval = 0;
^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) 	case POWER_SUPPLY_PROP_TECHNOLOGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		val->intval = cw_bat->charge_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		val->intval = cw_bat->temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	case POWER_SUPPLY_PROP_CHARGE_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		if (cw_bat->battery.charge_full_design_uah > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			val->intval = cw_bat->battery.charge_full_design_uah;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			val->intval = cw_bat->design_capacity * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) static enum power_supply_property cw_battery_properties[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	POWER_SUPPLY_PROP_CHARGE_FULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static const struct power_supply_desc cw2017_bat_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	.name		= "cw2017-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	.type		= POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	.properties	= cw_battery_properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	.num_properties	= ARRAY_SIZE(cw_battery_properties),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	.get_property	= cw_battery_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static int cw2017_parse_properties(struct cw_battery *cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	struct device *dev = cw_bat->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	length = device_property_count_u8(dev, "cellwise,battery-profile");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (length < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			 "No battery-profile found, using current flash contents\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	} else if (length != CW2017_SIZE_BATINFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		dev_err(cw_bat->dev, "battery-profile must be %d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			CW2017_SIZE_BATINFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	cw_bat->bat_profile = devm_kzalloc(dev, length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (!cw_bat->bat_profile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	ret = device_property_read_u8_array(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 					    "cellwise,battery-profile",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 					    cw_bat->bat_profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 					    length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	ret = device_property_read_u32(dev, "cellwise,design-capacity-amh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 				       &cw_bat->design_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		dev_info(cw_bat->dev, "Missing design capacity\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		cw_bat->design_capacity = DEF_DESIGN_CAPACITY;
^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) 	device_property_read_u32(dev, "cellwise,alert-level",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 				 &cw_bat->alert_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	cw_bat->dual_cell = device_property_read_bool(dev, "cellwise,dual-cell");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	ret = device_property_read_u32(dev, "cellwise,monitor-interval-ms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 				       &cw_bat->poll_interval_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		dev_dbg(cw_bat->dev, "Using default poll interval\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		cw_bat->poll_interval_ms = CW2017_DEFAULT_POLL_INTERVAL_MS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static const struct regmap_range regmap_ranges_rd_yes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	regmap_reg_range(CW2017_REG_VERSION, CW2017_REG_VERSION),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	regmap_reg_range(CW2017_REG_VCELL_H, CW2017_REG_TEMP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	regmap_reg_range(CW2017_REG_MODE_CONFIG, CW2017_REG_MODE_CONFIG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	regmap_reg_range(CW2017_REG_INT_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			 CW2017_REG_BATINFO + CW2017_SIZE_BATINFO - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) static const struct regmap_access_table regmap_rd_table = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	.yes_ranges = regmap_ranges_rd_yes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	.n_yes_ranges = ARRAY_SIZE(regmap_ranges_rd_yes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static const struct regmap_range regmap_ranges_wr_yes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	regmap_reg_range(CW2017_REG_MODE_CONFIG, CW2017_REG_MODE_CONFIG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	regmap_reg_range(CW2017_REG_INT_CONFIG, CW2017_REG_TEMP_MIN),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	regmap_reg_range(CW2017_REG_BATINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			 CW2017_REG_BATINFO + CW2017_SIZE_BATINFO - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static const struct regmap_access_table regmap_wr_table = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	.yes_ranges = regmap_ranges_wr_yes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	.n_yes_ranges = ARRAY_SIZE(regmap_ranges_wr_yes),
^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) static const struct regmap_range regmap_ranges_vol_yes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	regmap_reg_range(CW2017_REG_VCELL_H, CW2017_REG_TEMP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static const struct regmap_access_table regmap_vol_table = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	.yes_ranges = regmap_ranges_vol_yes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	.n_yes_ranges = ARRAY_SIZE(regmap_ranges_vol_yes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) static const struct regmap_config cw2017_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	.rd_table = &regmap_rd_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	.wr_table = &regmap_wr_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	.volatile_table = &regmap_vol_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	.max_register = CW2017_REG_BATINFO + CW2017_SIZE_BATINFO - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static int cw_bat_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	struct cw_battery *cw_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	struct power_supply_config psy_cfg = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	if (!cw_bat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	i2c_set_clientdata(client, cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	cw_bat->dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	cw_bat->soc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	ret = cw2017_parse_properties(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		dev_err(cw_bat->dev, "Failed to parse cw2017 properties\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	cw_bat->regmap = devm_regmap_init_i2c(client, &cw2017_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	if (IS_ERR(cw_bat->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		dev_err(cw_bat->dev, "Failed to allocate regmap: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			PTR_ERR(cw_bat->regmap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		return PTR_ERR(cw_bat->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	ret = cw_init(cw_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		dev_err(cw_bat->dev, "Init failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	psy_cfg.drv_data = cw_bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	psy_cfg.fwnode = dev_fwnode(cw_bat->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	cw_bat->rk_bat = devm_power_supply_register(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 						    &cw2017_bat_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 						    &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	if (IS_ERR(cw_bat->rk_bat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		dev_err(cw_bat->dev, "Failed to register power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		return PTR_ERR(cw_bat->rk_bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	ret = power_supply_get_battery_info(cw_bat->rk_bat, &cw_bat->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		dev_warn(cw_bat->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 			 "No monitored battery, some properties will be missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	queue_delayed_work(cw_bat->battery_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 			   &cw_bat->battery_delay_work, msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static int __maybe_unused cw_bat_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	struct cw_battery *cw_bat = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	cancel_delayed_work_sync(&cw_bat->battery_delay_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static int __maybe_unused cw_bat_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	struct cw_battery *cw_bat = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	queue_delayed_work(cw_bat->battery_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 			   &cw_bat->battery_delay_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static SIMPLE_DEV_PM_OPS(cw_bat_pm_ops, cw_bat_suspend, cw_bat_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static int cw_bat_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	struct cw_battery *cw_bat = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	cancel_delayed_work_sync(&cw_bat->battery_delay_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	power_supply_put_battery_info(cw_bat->rk_bat, &cw_bat->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) static const struct i2c_device_id cw_bat_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	{ "cw2017", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static const struct of_device_id cw2017_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	{ .compatible = "cellwise,cw2017" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) MODULE_DEVICE_TABLE(of, cw2017_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) static struct i2c_driver cw_bat_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		.name = "cw2017",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		.of_match_table = cw2017_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		.pm = &cw_bat_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	.probe_new = cw_bat_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	.remove = cw_bat_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	.id_table = cw_bat_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) module_i2c_driver(cw_bat_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) MODULE_AUTHOR("Shunqing Chen<csq@rock-chips.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) MODULE_DESCRIPTION("cw2017 battery driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) MODULE_LICENSE("GPL");