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.
43bee737bcSChanwoo Choi  * @name: the name of charger cable(external 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;
59bee737bcSChanwoo Choi 
6075ea8ca8SMarcel Ziswiler 	/* The charger-manager use Extcon framework */
61bee737bcSChanwoo Choi 	struct extcon_specific_cable_nb extcon_dev;
62bee737bcSChanwoo Choi 	struct work_struct wq;
63bee737bcSChanwoo Choi 	struct notifier_block nb;
64bee737bcSChanwoo Choi 
65bee737bcSChanwoo Choi 	/* The state of charger cable */
66bee737bcSChanwoo Choi 	bool attached;
67bee737bcSChanwoo Choi 
68bee737bcSChanwoo Choi 	struct charger_regulator *charger;
6945cd4fb2SChanwoo Choi 
7045cd4fb2SChanwoo Choi 	/*
7145cd4fb2SChanwoo Choi 	 * Set min/max current of regulator to protect over-current issue
7245cd4fb2SChanwoo Choi 	 * according to a kind of charger cable when cable is attached.
7345cd4fb2SChanwoo Choi 	 */
7445cd4fb2SChanwoo Choi 	int min_uA;
7545cd4fb2SChanwoo Choi 	int max_uA;
7645cd4fb2SChanwoo Choi 
77bee737bcSChanwoo Choi 	struct charger_manager *cm;
78bee737bcSChanwoo Choi };
79bee737bcSChanwoo Choi 
80bee737bcSChanwoo Choi /**
81bee737bcSChanwoo Choi  * struct charger_regulator
82bee737bcSChanwoo Choi  * @regulator_name: the name of regulator for using charger.
83bee737bcSChanwoo Choi  * @consumer: the regulator consumer for the charger.
843950c786SChanwoo Choi  * @externally_control:
853950c786SChanwoo Choi  *	Set if the charger-manager cannot control charger,
863950c786SChanwoo Choi  *	the charger will be maintained with disabled state.
87bee737bcSChanwoo Choi  * @cables:
88bee737bcSChanwoo Choi  *	the array of charger cables to enable/disable charger
8975ea8ca8SMarcel Ziswiler  *	and set current limit according to constraint data of
90bee737bcSChanwoo Choi  *	struct charger_cable if only charger cable included
91bee737bcSChanwoo Choi  *	in the array of charger cables is attached/detached.
92bee737bcSChanwoo Choi  * @num_cables: the number of charger cables.
933950c786SChanwoo Choi  * @attr_g: Attribute group for the charger(regulator)
943950c786SChanwoo Choi  * @attr_name: "name" sysfs entry
953950c786SChanwoo Choi  * @attr_state: "state" sysfs entry
963950c786SChanwoo Choi  * @attr_externally_control: "externally_control" sysfs entry
973950c786SChanwoo Choi  * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
98bee737bcSChanwoo Choi  */
99bee737bcSChanwoo Choi struct charger_regulator {
100bee737bcSChanwoo Choi 	/* The name of regulator for charging */
101bee737bcSChanwoo Choi 	const char *regulator_name;
102bee737bcSChanwoo Choi 	struct regulator *consumer;
103bee737bcSChanwoo Choi 
1043950c786SChanwoo Choi 	/* charger never on when system is on */
1053950c786SChanwoo Choi 	int externally_control;
1063950c786SChanwoo Choi 
107bee737bcSChanwoo Choi 	/*
108bee737bcSChanwoo Choi 	 * Store constraint information related to current limit,
109bee737bcSChanwoo Choi 	 * each cable have different condition for charging.
110bee737bcSChanwoo Choi 	 */
111bee737bcSChanwoo Choi 	struct charger_cable *cables;
112bee737bcSChanwoo Choi 	int num_cables;
1133950c786SChanwoo Choi 
114157ba1bbSSebastian Reichel 	struct attribute_group attr_grp;
1153950c786SChanwoo Choi 	struct device_attribute attr_name;
1163950c786SChanwoo Choi 	struct device_attribute attr_state;
1173950c786SChanwoo Choi 	struct device_attribute attr_externally_control;
1183950c786SChanwoo Choi 	struct attribute *attrs[4];
1193950c786SChanwoo Choi 
1203950c786SChanwoo Choi 	struct charger_manager *cm;
121bee737bcSChanwoo Choi };
122bee737bcSChanwoo Choi 
123bee737bcSChanwoo Choi /**
1243bb3dbbdSDonggeun Kim  * struct charger_desc
125ad3d13eeSDonggeun Kim  * @psy_name: the name of power-supply-class for charger manager
1263bb3dbbdSDonggeun Kim  * @polling_mode:
1273bb3dbbdSDonggeun Kim  *	Determine which polling mode will be used
128d829dc75SChanwoo Choi  * @fullbatt_vchkdrop_uV:
129d829dc75SChanwoo Choi  *	Check voltage drop after the battery is fully charged.
1309584051fSJonghwa Lee  *	If it has dropped more than fullbatt_vchkdrop_uV
1319584051fSJonghwa Lee  *	CM will restart charging.
132ad3d13eeSDonggeun Kim  * @fullbatt_uV: voltage in microvolt
1332ed9e9b6SChanwoo Choi  *	If VBATT >= fullbatt_uV, it is assumed to be full.
1342ed9e9b6SChanwoo Choi  * @fullbatt_soc: state of Charge in %
1352ed9e9b6SChanwoo Choi  *	If state of Charge >= fullbatt_soc, it is assumed to be full.
1362ed9e9b6SChanwoo Choi  * @fullbatt_full_capacity: full capacity measure
1372ed9e9b6SChanwoo Choi  *	If full capacity of battery >= fullbatt_full_capacity,
138ad3d13eeSDonggeun Kim  *	it is assumed to be full.
1393bb3dbbdSDonggeun Kim  * @polling_interval_ms: interval in millisecond at which
1403bb3dbbdSDonggeun Kim  *	charger manager will monitor battery health
1413bb3dbbdSDonggeun Kim  * @battery_present:
14275ea8ca8SMarcel Ziswiler  *	Specify where information for existence of battery can be obtained
1433bb3dbbdSDonggeun Kim  * @psy_charger_stat: the names of power-supply for chargers
1443bb3dbbdSDonggeun Kim  * @num_charger_regulator: the number of entries in charger_regulators
145c6b2744cSAnton Vorontsov  * @charger_regulators: array of charger regulators
1463bb3dbbdSDonggeun Kim  * @psy_fuel_gauge: the name of power-supply for fuel gauge
1475c49a625SJonghwa Lee  * @thermal_zone : the name of thermal zone for battery
1485c49a625SJonghwa Lee  * @temp_min : Minimum battery temperature for charging.
1495c49a625SJonghwa Lee  * @temp_max : Maximum battery temperature for charging.
15075ea8ca8SMarcel Ziswiler  * @temp_diff : Temperature difference to restart charging.
151ad3d13eeSDonggeun Kim  * @measure_battery_temp:
152ad3d13eeSDonggeun Kim  *	true: measure battery temperature
153ad3d13eeSDonggeun Kim  *	false: measure ambient temperature
1548fcfe088SChanwoo Choi  * @charging_max_duration_ms: Maximum possible duration for charging
1558fcfe088SChanwoo Choi  *	If whole charging duration exceed 'charging_max_duration_ms',
1568fcfe088SChanwoo Choi  *	cm stop charging.
1578fcfe088SChanwoo Choi  * @discharging_max_duration_ms:
1588fcfe088SChanwoo Choi  *	Maximum possible duration for discharging with charger cable
1598fcfe088SChanwoo Choi  *	after full-batt. If discharging duration exceed 'discharging
1608fcfe088SChanwoo Choi  *	max_duration_ms', cm start charging.
1613bb3dbbdSDonggeun Kim  */
1623bb3dbbdSDonggeun Kim struct charger_desc {
163856ee611SJonghwa Lee 	const char *psy_name;
164ad3d13eeSDonggeun Kim 
1653bb3dbbdSDonggeun Kim 	enum polling_modes polling_mode;
1663bb3dbbdSDonggeun Kim 	unsigned int polling_interval_ms;
1673bb3dbbdSDonggeun Kim 
168d829dc75SChanwoo Choi 	unsigned int fullbatt_vchkdrop_uV;
169ad3d13eeSDonggeun Kim 	unsigned int fullbatt_uV;
1702ed9e9b6SChanwoo Choi 	unsigned int fullbatt_soc;
1712ed9e9b6SChanwoo Choi 	unsigned int fullbatt_full_capacity;
172ad3d13eeSDonggeun Kim 
1733bb3dbbdSDonggeun Kim 	enum data_source battery_present;
1743bb3dbbdSDonggeun Kim 
175856ee611SJonghwa Lee 	const char **psy_charger_stat;
1763bb3dbbdSDonggeun Kim 
1773bb3dbbdSDonggeun Kim 	int num_charger_regulators;
178bee737bcSChanwoo Choi 	struct charger_regulator *charger_regulators;
179157ba1bbSSebastian Reichel 	const struct attribute_group **sysfs_groups;
1803bb3dbbdSDonggeun Kim 
181856ee611SJonghwa Lee 	const char *psy_fuel_gauge;
1823bb3dbbdSDonggeun Kim 
183856ee611SJonghwa Lee 	const char *thermal_zone;
1845c49a625SJonghwa Lee 
1855c49a625SJonghwa Lee 	int temp_min;
1865c49a625SJonghwa Lee 	int temp_max;
1875c49a625SJonghwa Lee 	int temp_diff;
1885c49a625SJonghwa Lee 
189ad3d13eeSDonggeun Kim 	bool measure_battery_temp;
1908fcfe088SChanwoo Choi 
191856ee611SJonghwa Lee 	u32 charging_max_duration_ms;
192856ee611SJonghwa Lee 	u32 discharging_max_duration_ms;
1933bb3dbbdSDonggeun Kim };
1943bb3dbbdSDonggeun Kim 
1953bb3dbbdSDonggeun Kim #define PSY_NAME_MAX	30
1963bb3dbbdSDonggeun Kim 
1973bb3dbbdSDonggeun Kim /**
1983bb3dbbdSDonggeun Kim  * struct charger_manager
1993bb3dbbdSDonggeun Kim  * @entry: entry for list
2003bb3dbbdSDonggeun Kim  * @dev: device pointer
2013bb3dbbdSDonggeun Kim  * @desc: instance of charger_desc
2023bb3dbbdSDonggeun Kim  * @fuel_gauge: power_supply for fuel gauge
2033bb3dbbdSDonggeun Kim  * @charger_stat: array of power_supply for chargers
2045c49a625SJonghwa Lee  * @tzd_batt : thermal zone device for battery
2053bb3dbbdSDonggeun Kim  * @charger_enabled: the state of charger
2063bb3dbbdSDonggeun Kim  * @emergency_stop:
2073bb3dbbdSDonggeun Kim  *	When setting true, stop charging
208ad3d13eeSDonggeun Kim  * @psy_name_buf: the name of power-supply-class for charger manager
209ad3d13eeSDonggeun Kim  * @charger_psy: power_supply for charger manager
2103bb3dbbdSDonggeun Kim  * @status_save_ext_pwr_inserted:
2113bb3dbbdSDonggeun Kim  *	saved status of external power before entering suspend-to-RAM
2123bb3dbbdSDonggeun Kim  * @status_save_batt:
2133bb3dbbdSDonggeun Kim  *	saved status of battery before entering suspend-to-RAM
2148fcfe088SChanwoo Choi  * @charging_start_time: saved start time of enabling charging
2158fcfe088SChanwoo Choi  * @charging_end_time: saved end time of disabling charging
216*e132fc6bSJonghwa Lee  * @battery_status: Current battery status
2173bb3dbbdSDonggeun Kim  */
2183bb3dbbdSDonggeun Kim struct charger_manager {
2193bb3dbbdSDonggeun Kim 	struct list_head entry;
2203bb3dbbdSDonggeun Kim 	struct device *dev;
2213bb3dbbdSDonggeun Kim 	struct charger_desc *desc;
2223bb3dbbdSDonggeun Kim 
2235c49a625SJonghwa Lee #ifdef CONFIG_THERMAL
2245c49a625SJonghwa Lee 	struct thermal_zone_device *tzd_batt;
2255c49a625SJonghwa Lee #endif
2263bb3dbbdSDonggeun Kim 	bool charger_enabled;
2273bb3dbbdSDonggeun Kim 
2283bb3dbbdSDonggeun Kim 	int emergency_stop;
2293bb3dbbdSDonggeun Kim 
230ad3d13eeSDonggeun Kim 	char psy_name_buf[PSY_NAME_MAX + 1];
231297d716fSKrzysztof Kozlowski 	struct power_supply_desc charger_psy_desc;
232297d716fSKrzysztof Kozlowski 	struct power_supply *charger_psy;
233ad3d13eeSDonggeun Kim 
2348fcfe088SChanwoo Choi 	u64 charging_start_time;
2358fcfe088SChanwoo Choi 	u64 charging_end_time;
236*e132fc6bSJonghwa Lee 
237*e132fc6bSJonghwa Lee 	int battery_status;
2383bb3dbbdSDonggeun Kim };
2393bb3dbbdSDonggeun Kim 
2403bb3dbbdSDonggeun Kim #endif /* _CHARGER_MANAGER_H */
241