1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
23bb3dbbdSDonggeun Kim /*
33bb3dbbdSDonggeun Kim  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
43bb3dbbdSDonggeun Kim  * MyungJoo.Ham <myungjoo.ham@samsung.com>
53bb3dbbdSDonggeun Kim  *
63bb3dbbdSDonggeun Kim  * Charger Manager.
73bb3dbbdSDonggeun Kim  * This framework enables to control and multiple chargers and to
83bb3dbbdSDonggeun Kim  * monitor charging even in the context of suspend-to-RAM with
93bb3dbbdSDonggeun Kim  * an interface combining the chargers.
103bb3dbbdSDonggeun Kim  *
113bb3dbbdSDonggeun Kim **/
123bb3dbbdSDonggeun Kim 
133bb3dbbdSDonggeun Kim #ifndef _CHARGER_MANAGER_H
143bb3dbbdSDonggeun Kim #define _CHARGER_MANAGER_H
153bb3dbbdSDonggeun Kim 
163bb3dbbdSDonggeun Kim #include <linux/power_supply.h>
17bee737bcSChanwoo Choi #include <linux/extcon.h>
18c1155c64SJonghwa Lee #include <linux/alarmtimer.h>
193bb3dbbdSDonggeun Kim 
203bb3dbbdSDonggeun Kim enum data_source {
21d829dc75SChanwoo Choi 	CM_BATTERY_PRESENT,
22d829dc75SChanwoo Choi 	CM_NO_BATTERY,
233bb3dbbdSDonggeun Kim 	CM_FUEL_GAUGE,
243bb3dbbdSDonggeun Kim 	CM_CHARGER_STAT,
253bb3dbbdSDonggeun Kim };
263bb3dbbdSDonggeun Kim 
273bb3dbbdSDonggeun Kim enum polling_modes {
283bb3dbbdSDonggeun Kim 	CM_POLL_DISABLE = 0,
293bb3dbbdSDonggeun Kim 	CM_POLL_ALWAYS,
303bb3dbbdSDonggeun Kim 	CM_POLL_EXTERNAL_POWER_ONLY,
313bb3dbbdSDonggeun Kim 	CM_POLL_CHARGING_ONLY,
323bb3dbbdSDonggeun Kim };
333bb3dbbdSDonggeun Kim 
349584051fSJonghwa Lee enum cm_batt_temp {
359584051fSJonghwa Lee 	CM_BATT_OK = 0,
369584051fSJonghwa Lee 	CM_BATT_OVERHEAT,
379584051fSJonghwa Lee 	CM_BATT_COLD,
38dfeccb12SChanwoo Choi };
39dfeccb12SChanwoo Choi 
403bb3dbbdSDonggeun Kim /**
41bee737bcSChanwoo Choi  * struct charger_cable
42bee737bcSChanwoo Choi  * @extcon_name: the name of extcon device.
43c1f73028SJonathan Bakker  * @name: the name of the cable connector
44bee737bcSChanwoo Choi  * @extcon_dev: the extcon device.
45bee737bcSChanwoo Choi  * @wq: the workqueue to control charger according to the state of
46bee737bcSChanwoo Choi  *	charger cable. If charger cable is attached, enable charger.
47bee737bcSChanwoo Choi  *	But if charger cable is detached, disable charger.
48bee737bcSChanwoo Choi  * @nb: the notifier block to receive changed state from EXTCON
49bee737bcSChanwoo Choi  *	(External Connector) when charger cable is attached/detached.
50bee737bcSChanwoo Choi  * @attached: the state of charger cable.
51bee737bcSChanwoo Choi  *	true: the charger cable is attached
52bee737bcSChanwoo Choi  *	false: the charger cable is detached
53bee737bcSChanwoo Choi  * @charger: the instance of struct charger_regulator.
54bee737bcSChanwoo Choi  * @cm: the Charger Manager representing the battery.
55bee737bcSChanwoo Choi  */
56bee737bcSChanwoo Choi struct charger_cable {
57bee737bcSChanwoo Choi 	const char *extcon_name;
58bee737bcSChanwoo Choi 	const char *name;
59c1f73028SJonathan Bakker 	struct extcon_dev *extcon_dev;
60c1f73028SJonathan Bakker 	u64 extcon_type;
61bee737bcSChanwoo Choi 
6275ea8ca8SMarcel Ziswiler 	/* The charger-manager use Extcon framework */
63bee737bcSChanwoo Choi 	struct work_struct wq;
64bee737bcSChanwoo Choi 	struct notifier_block nb;
65bee737bcSChanwoo Choi 
66bee737bcSChanwoo Choi 	/* The state of charger cable */
67bee737bcSChanwoo Choi 	bool attached;
68bee737bcSChanwoo Choi 
69bee737bcSChanwoo Choi 	struct charger_regulator *charger;
7045cd4fb2SChanwoo Choi 
7145cd4fb2SChanwoo Choi 	/*
7245cd4fb2SChanwoo Choi 	 * Set min/max current of regulator to protect over-current issue
7345cd4fb2SChanwoo Choi 	 * according to a kind of charger cable when cable is attached.
7445cd4fb2SChanwoo Choi 	 */
7545cd4fb2SChanwoo Choi 	int min_uA;
7645cd4fb2SChanwoo Choi 	int max_uA;
7745cd4fb2SChanwoo Choi 
78bee737bcSChanwoo Choi 	struct charger_manager *cm;
79bee737bcSChanwoo Choi };
80bee737bcSChanwoo Choi 
81bee737bcSChanwoo Choi /**
82bee737bcSChanwoo Choi  * struct charger_regulator
83bee737bcSChanwoo Choi  * @regulator_name: the name of regulator for using charger.
84bee737bcSChanwoo Choi  * @consumer: the regulator consumer for the charger.
853950c786SChanwoo Choi  * @externally_control:
863950c786SChanwoo Choi  *	Set if the charger-manager cannot control charger,
873950c786SChanwoo Choi  *	the charger will be maintained with disabled state.
88bee737bcSChanwoo Choi  * @cables:
89bee737bcSChanwoo Choi  *	the array of charger cables to enable/disable charger
9075ea8ca8SMarcel Ziswiler  *	and set current limit according to constraint data of
91bee737bcSChanwoo Choi  *	struct charger_cable if only charger cable included
92bee737bcSChanwoo Choi  *	in the array of charger cables is attached/detached.
93bee737bcSChanwoo Choi  * @num_cables: the number of charger cables.
943950c786SChanwoo Choi  * @attr_g: Attribute group for the charger(regulator)
953950c786SChanwoo Choi  * @attr_name: "name" sysfs entry
963950c786SChanwoo Choi  * @attr_state: "state" sysfs entry
973950c786SChanwoo Choi  * @attr_externally_control: "externally_control" sysfs entry
983950c786SChanwoo Choi  * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
99bee737bcSChanwoo Choi  */
100bee737bcSChanwoo Choi struct charger_regulator {
101bee737bcSChanwoo Choi 	/* The name of regulator for charging */
102bee737bcSChanwoo Choi 	const char *regulator_name;
103bee737bcSChanwoo Choi 	struct regulator *consumer;
104bee737bcSChanwoo Choi 
1053950c786SChanwoo Choi 	/* charger never on when system is on */
1063950c786SChanwoo Choi 	int externally_control;
1073950c786SChanwoo Choi 
108bee737bcSChanwoo Choi 	/*
109bee737bcSChanwoo Choi 	 * Store constraint information related to current limit,
110bee737bcSChanwoo Choi 	 * each cable have different condition for charging.
111bee737bcSChanwoo Choi 	 */
112bee737bcSChanwoo Choi 	struct charger_cable *cables;
113bee737bcSChanwoo Choi 	int num_cables;
1143950c786SChanwoo Choi 
115157ba1bbSSebastian Reichel 	struct attribute_group attr_grp;
1163950c786SChanwoo Choi 	struct device_attribute attr_name;
1173950c786SChanwoo Choi 	struct device_attribute attr_state;
1183950c786SChanwoo Choi 	struct device_attribute attr_externally_control;
1193950c786SChanwoo Choi 	struct attribute *attrs[4];
1203950c786SChanwoo Choi 
1213950c786SChanwoo Choi 	struct charger_manager *cm;
122bee737bcSChanwoo Choi };
123bee737bcSChanwoo Choi 
124bee737bcSChanwoo Choi /**
1253bb3dbbdSDonggeun Kim  * struct charger_desc
126ad3d13eeSDonggeun Kim  * @psy_name: the name of power-supply-class for charger manager
1273bb3dbbdSDonggeun Kim  * @polling_mode:
1283bb3dbbdSDonggeun Kim  *	Determine which polling mode will be used
129d829dc75SChanwoo Choi  * @fullbatt_vchkdrop_uV:
130d829dc75SChanwoo Choi  *	Check voltage drop after the battery is fully charged.
1319584051fSJonghwa Lee  *	If it has dropped more than fullbatt_vchkdrop_uV
1329584051fSJonghwa Lee  *	CM will restart charging.
133ad3d13eeSDonggeun Kim  * @fullbatt_uV: voltage in microvolt
1342ed9e9b6SChanwoo Choi  *	If VBATT >= fullbatt_uV, it is assumed to be full.
1352ed9e9b6SChanwoo Choi  * @fullbatt_soc: state of Charge in %
1362ed9e9b6SChanwoo Choi  *	If state of Charge >= fullbatt_soc, it is assumed to be full.
1372ed9e9b6SChanwoo Choi  * @fullbatt_full_capacity: full capacity measure
1382ed9e9b6SChanwoo Choi  *	If full capacity of battery >= fullbatt_full_capacity,
139ad3d13eeSDonggeun Kim  *	it is assumed to be full.
1403bb3dbbdSDonggeun Kim  * @polling_interval_ms: interval in millisecond at which
1413bb3dbbdSDonggeun Kim  *	charger manager will monitor battery health
1423bb3dbbdSDonggeun Kim  * @battery_present:
14375ea8ca8SMarcel Ziswiler  *	Specify where information for existence of battery can be obtained
1443bb3dbbdSDonggeun Kim  * @psy_charger_stat: the names of power-supply for chargers
1453bb3dbbdSDonggeun Kim  * @num_charger_regulator: the number of entries in charger_regulators
146c6b2744cSAnton Vorontsov  * @charger_regulators: array of charger regulators
1473bb3dbbdSDonggeun Kim  * @psy_fuel_gauge: the name of power-supply for fuel gauge
1485c49a625SJonghwa Lee  * @thermal_zone : the name of thermal zone for battery
1495c49a625SJonghwa Lee  * @temp_min : Minimum battery temperature for charging.
1505c49a625SJonghwa Lee  * @temp_max : Maximum battery temperature for charging.
15175ea8ca8SMarcel Ziswiler  * @temp_diff : Temperature difference to restart charging.
152ad3d13eeSDonggeun Kim  * @measure_battery_temp:
153ad3d13eeSDonggeun Kim  *	true: measure battery temperature
154ad3d13eeSDonggeun Kim  *	false: measure ambient temperature
1558fcfe088SChanwoo Choi  * @charging_max_duration_ms: Maximum possible duration for charging
1568fcfe088SChanwoo Choi  *	If whole charging duration exceed 'charging_max_duration_ms',
1578fcfe088SChanwoo Choi  *	cm stop charging.
1588fcfe088SChanwoo Choi  * @discharging_max_duration_ms:
1598fcfe088SChanwoo Choi  *	Maximum possible duration for discharging with charger cable
1608fcfe088SChanwoo Choi  *	after full-batt. If discharging duration exceed 'discharging
1618fcfe088SChanwoo Choi  *	max_duration_ms', cm start charging.
1623bb3dbbdSDonggeun Kim  */
1633bb3dbbdSDonggeun Kim struct charger_desc {
164856ee611SJonghwa Lee 	const char *psy_name;
165ad3d13eeSDonggeun Kim 
1663bb3dbbdSDonggeun Kim 	enum polling_modes polling_mode;
1673bb3dbbdSDonggeun Kim 	unsigned int polling_interval_ms;
1683bb3dbbdSDonggeun Kim 
169d829dc75SChanwoo Choi 	unsigned int fullbatt_vchkdrop_uV;
170ad3d13eeSDonggeun Kim 	unsigned int fullbatt_uV;
1712ed9e9b6SChanwoo Choi 	unsigned int fullbatt_soc;
1722ed9e9b6SChanwoo Choi 	unsigned int fullbatt_full_capacity;
173ad3d13eeSDonggeun Kim 
1743bb3dbbdSDonggeun Kim 	enum data_source battery_present;
1753bb3dbbdSDonggeun Kim 
176856ee611SJonghwa Lee 	const char **psy_charger_stat;
1773bb3dbbdSDonggeun Kim 
1783bb3dbbdSDonggeun Kim 	int num_charger_regulators;
179bee737bcSChanwoo Choi 	struct charger_regulator *charger_regulators;
180157ba1bbSSebastian Reichel 	const struct attribute_group **sysfs_groups;
1813bb3dbbdSDonggeun Kim 
182856ee611SJonghwa Lee 	const char *psy_fuel_gauge;
1833bb3dbbdSDonggeun Kim 
184856ee611SJonghwa Lee 	const char *thermal_zone;
1855c49a625SJonghwa Lee 
1865c49a625SJonghwa Lee 	int temp_min;
1875c49a625SJonghwa Lee 	int temp_max;
1885c49a625SJonghwa Lee 	int temp_diff;
1895c49a625SJonghwa Lee 
190ad3d13eeSDonggeun Kim 	bool measure_battery_temp;
1918fcfe088SChanwoo Choi 
192856ee611SJonghwa Lee 	u32 charging_max_duration_ms;
193856ee611SJonghwa Lee 	u32 discharging_max_duration_ms;
1943bb3dbbdSDonggeun Kim };
1953bb3dbbdSDonggeun Kim 
1963bb3dbbdSDonggeun Kim #define PSY_NAME_MAX	30
1973bb3dbbdSDonggeun Kim 
1983bb3dbbdSDonggeun Kim /**
1993bb3dbbdSDonggeun Kim  * struct charger_manager
2003bb3dbbdSDonggeun Kim  * @entry: entry for list
2013bb3dbbdSDonggeun Kim  * @dev: device pointer
2023bb3dbbdSDonggeun Kim  * @desc: instance of charger_desc
2033bb3dbbdSDonggeun Kim  * @fuel_gauge: power_supply for fuel gauge
2043bb3dbbdSDonggeun Kim  * @charger_stat: array of power_supply for chargers
2055c49a625SJonghwa Lee  * @tzd_batt : thermal zone device for battery
2063bb3dbbdSDonggeun Kim  * @charger_enabled: the state of charger
2073bb3dbbdSDonggeun Kim  * @emergency_stop:
2083bb3dbbdSDonggeun Kim  *	When setting true, stop charging
209ad3d13eeSDonggeun Kim  * @psy_name_buf: the name of power-supply-class for charger manager
210ad3d13eeSDonggeun Kim  * @charger_psy: power_supply for charger manager
2113bb3dbbdSDonggeun Kim  * @status_save_ext_pwr_inserted:
2123bb3dbbdSDonggeun Kim  *	saved status of external power before entering suspend-to-RAM
2133bb3dbbdSDonggeun Kim  * @status_save_batt:
2143bb3dbbdSDonggeun Kim  *	saved status of battery before entering suspend-to-RAM
2158fcfe088SChanwoo Choi  * @charging_start_time: saved start time of enabling charging
2168fcfe088SChanwoo Choi  * @charging_end_time: saved end time of disabling charging
217e132fc6bSJonghwa Lee  * @battery_status: Current battery status
2183bb3dbbdSDonggeun Kim  */
2193bb3dbbdSDonggeun Kim struct charger_manager {
2203bb3dbbdSDonggeun Kim 	struct list_head entry;
2213bb3dbbdSDonggeun Kim 	struct device *dev;
2223bb3dbbdSDonggeun Kim 	struct charger_desc *desc;
2233bb3dbbdSDonggeun Kim 
2245c49a625SJonghwa Lee #ifdef CONFIG_THERMAL
2255c49a625SJonghwa Lee 	struct thermal_zone_device *tzd_batt;
2265c49a625SJonghwa Lee #endif
2273bb3dbbdSDonggeun Kim 	bool charger_enabled;
2283bb3dbbdSDonggeun Kim 
2293bb3dbbdSDonggeun Kim 	int emergency_stop;
2303bb3dbbdSDonggeun Kim 
231ad3d13eeSDonggeun Kim 	char psy_name_buf[PSY_NAME_MAX + 1];
232297d716fSKrzysztof Kozlowski 	struct power_supply_desc charger_psy_desc;
233297d716fSKrzysztof Kozlowski 	struct power_supply *charger_psy;
234ad3d13eeSDonggeun Kim 
2358fcfe088SChanwoo Choi 	u64 charging_start_time;
2368fcfe088SChanwoo Choi 	u64 charging_end_time;
237e132fc6bSJonghwa Lee 
238e132fc6bSJonghwa Lee 	int battery_status;
2393bb3dbbdSDonggeun Kim };
2403bb3dbbdSDonggeun Kim 
2413bb3dbbdSDonggeun Kim #endif /* _CHARGER_MANAGER_H */
242