1*d2912cb1SThomas 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 
34dfeccb12SChanwoo Choi enum cm_event_types {
35dfeccb12SChanwoo Choi 	CM_EVENT_UNKNOWN = 0,
36dfeccb12SChanwoo Choi 	CM_EVENT_BATT_FULL,
37dfeccb12SChanwoo Choi 	CM_EVENT_BATT_IN,
38dfeccb12SChanwoo Choi 	CM_EVENT_BATT_OUT,
395c49a625SJonghwa Lee 	CM_EVENT_BATT_OVERHEAT,
405c49a625SJonghwa Lee 	CM_EVENT_BATT_COLD,
41dfeccb12SChanwoo Choi 	CM_EVENT_EXT_PWR_IN_OUT,
42dfeccb12SChanwoo Choi 	CM_EVENT_CHG_START_STOP,
43dfeccb12SChanwoo Choi 	CM_EVENT_OTHERS,
44dfeccb12SChanwoo Choi };
45dfeccb12SChanwoo Choi 
463bb3dbbdSDonggeun Kim /**
47bee737bcSChanwoo Choi  * struct charger_cable
48bee737bcSChanwoo Choi  * @extcon_name: the name of extcon device.
49bee737bcSChanwoo Choi  * @name: the name of charger cable(external connector).
50bee737bcSChanwoo Choi  * @extcon_dev: the extcon device.
51bee737bcSChanwoo Choi  * @wq: the workqueue to control charger according to the state of
52bee737bcSChanwoo Choi  *	charger cable. If charger cable is attached, enable charger.
53bee737bcSChanwoo Choi  *	But if charger cable is detached, disable charger.
54bee737bcSChanwoo Choi  * @nb: the notifier block to receive changed state from EXTCON
55bee737bcSChanwoo Choi  *	(External Connector) when charger cable is attached/detached.
56bee737bcSChanwoo Choi  * @attached: the state of charger cable.
57bee737bcSChanwoo Choi  *	true: the charger cable is attached
58bee737bcSChanwoo Choi  *	false: the charger cable is detached
59bee737bcSChanwoo Choi  * @charger: the instance of struct charger_regulator.
60bee737bcSChanwoo Choi  * @cm: the Charger Manager representing the battery.
61bee737bcSChanwoo Choi  */
62bee737bcSChanwoo Choi struct charger_cable {
63bee737bcSChanwoo Choi 	const char *extcon_name;
64bee737bcSChanwoo Choi 	const char *name;
65bee737bcSChanwoo Choi 
6675ea8ca8SMarcel Ziswiler 	/* The charger-manager use Extcon framework */
67bee737bcSChanwoo Choi 	struct extcon_specific_cable_nb extcon_dev;
68bee737bcSChanwoo Choi 	struct work_struct wq;
69bee737bcSChanwoo Choi 	struct notifier_block nb;
70bee737bcSChanwoo Choi 
71bee737bcSChanwoo Choi 	/* The state of charger cable */
72bee737bcSChanwoo Choi 	bool attached;
73bee737bcSChanwoo Choi 
74bee737bcSChanwoo Choi 	struct charger_regulator *charger;
7545cd4fb2SChanwoo Choi 
7645cd4fb2SChanwoo Choi 	/*
7745cd4fb2SChanwoo Choi 	 * Set min/max current of regulator to protect over-current issue
7845cd4fb2SChanwoo Choi 	 * according to a kind of charger cable when cable is attached.
7945cd4fb2SChanwoo Choi 	 */
8045cd4fb2SChanwoo Choi 	int min_uA;
8145cd4fb2SChanwoo Choi 	int max_uA;
8245cd4fb2SChanwoo Choi 
83bee737bcSChanwoo Choi 	struct charger_manager *cm;
84bee737bcSChanwoo Choi };
85bee737bcSChanwoo Choi 
86bee737bcSChanwoo Choi /**
87bee737bcSChanwoo Choi  * struct charger_regulator
88bee737bcSChanwoo Choi  * @regulator_name: the name of regulator for using charger.
89bee737bcSChanwoo Choi  * @consumer: the regulator consumer for the charger.
903950c786SChanwoo Choi  * @externally_control:
913950c786SChanwoo Choi  *	Set if the charger-manager cannot control charger,
923950c786SChanwoo Choi  *	the charger will be maintained with disabled state.
93bee737bcSChanwoo Choi  * @cables:
94bee737bcSChanwoo Choi  *	the array of charger cables to enable/disable charger
9575ea8ca8SMarcel Ziswiler  *	and set current limit according to constraint data of
96bee737bcSChanwoo Choi  *	struct charger_cable if only charger cable included
97bee737bcSChanwoo Choi  *	in the array of charger cables is attached/detached.
98bee737bcSChanwoo Choi  * @num_cables: the number of charger cables.
993950c786SChanwoo Choi  * @attr_g: Attribute group for the charger(regulator)
1003950c786SChanwoo Choi  * @attr_name: "name" sysfs entry
1013950c786SChanwoo Choi  * @attr_state: "state" sysfs entry
1023950c786SChanwoo Choi  * @attr_externally_control: "externally_control" sysfs entry
1033950c786SChanwoo Choi  * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
104bee737bcSChanwoo Choi  */
105bee737bcSChanwoo Choi struct charger_regulator {
106bee737bcSChanwoo Choi 	/* The name of regulator for charging */
107bee737bcSChanwoo Choi 	const char *regulator_name;
108bee737bcSChanwoo Choi 	struct regulator *consumer;
109bee737bcSChanwoo Choi 
1103950c786SChanwoo Choi 	/* charger never on when system is on */
1113950c786SChanwoo Choi 	int externally_control;
1123950c786SChanwoo Choi 
113bee737bcSChanwoo Choi 	/*
114bee737bcSChanwoo Choi 	 * Store constraint information related to current limit,
115bee737bcSChanwoo Choi 	 * each cable have different condition for charging.
116bee737bcSChanwoo Choi 	 */
117bee737bcSChanwoo Choi 	struct charger_cable *cables;
118bee737bcSChanwoo Choi 	int num_cables;
1193950c786SChanwoo Choi 
120157ba1bbSSebastian Reichel 	struct attribute_group attr_grp;
1213950c786SChanwoo Choi 	struct device_attribute attr_name;
1223950c786SChanwoo Choi 	struct device_attribute attr_state;
1233950c786SChanwoo Choi 	struct device_attribute attr_externally_control;
1243950c786SChanwoo Choi 	struct attribute *attrs[4];
1253950c786SChanwoo Choi 
1263950c786SChanwoo Choi 	struct charger_manager *cm;
127bee737bcSChanwoo Choi };
128bee737bcSChanwoo Choi 
129bee737bcSChanwoo Choi /**
1303bb3dbbdSDonggeun Kim  * struct charger_desc
131ad3d13eeSDonggeun Kim  * @psy_name: the name of power-supply-class for charger manager
1323bb3dbbdSDonggeun Kim  * @polling_mode:
1333bb3dbbdSDonggeun Kim  *	Determine which polling mode will be used
134d829dc75SChanwoo Choi  * @fullbatt_vchkdrop_ms:
135d829dc75SChanwoo Choi  * @fullbatt_vchkdrop_uV:
136d829dc75SChanwoo Choi  *	Check voltage drop after the battery is fully charged.
137d829dc75SChanwoo Choi  *	If it has dropped more than fullbatt_vchkdrop_uV after
138d829dc75SChanwoo Choi  *	fullbatt_vchkdrop_ms, CM will restart charging.
139ad3d13eeSDonggeun Kim  * @fullbatt_uV: voltage in microvolt
1402ed9e9b6SChanwoo Choi  *	If VBATT >= fullbatt_uV, it is assumed to be full.
1412ed9e9b6SChanwoo Choi  * @fullbatt_soc: state of Charge in %
1422ed9e9b6SChanwoo Choi  *	If state of Charge >= fullbatt_soc, it is assumed to be full.
1432ed9e9b6SChanwoo Choi  * @fullbatt_full_capacity: full capacity measure
1442ed9e9b6SChanwoo Choi  *	If full capacity of battery >= fullbatt_full_capacity,
145ad3d13eeSDonggeun Kim  *	it is assumed to be full.
1463bb3dbbdSDonggeun Kim  * @polling_interval_ms: interval in millisecond at which
1473bb3dbbdSDonggeun Kim  *	charger manager will monitor battery health
1483bb3dbbdSDonggeun Kim  * @battery_present:
14975ea8ca8SMarcel Ziswiler  *	Specify where information for existence of battery can be obtained
1503bb3dbbdSDonggeun Kim  * @psy_charger_stat: the names of power-supply for chargers
1513bb3dbbdSDonggeun Kim  * @num_charger_regulator: the number of entries in charger_regulators
152c6b2744cSAnton Vorontsov  * @charger_regulators: array of charger regulators
1533bb3dbbdSDonggeun Kim  * @psy_fuel_gauge: the name of power-supply for fuel gauge
1545c49a625SJonghwa Lee  * @thermal_zone : the name of thermal zone for battery
1555c49a625SJonghwa Lee  * @temp_min : Minimum battery temperature for charging.
1565c49a625SJonghwa Lee  * @temp_max : Maximum battery temperature for charging.
15775ea8ca8SMarcel Ziswiler  * @temp_diff : Temperature difference to restart charging.
158ad3d13eeSDonggeun Kim  * @measure_battery_temp:
159ad3d13eeSDonggeun Kim  *	true: measure battery temperature
160ad3d13eeSDonggeun Kim  *	false: measure ambient temperature
1618fcfe088SChanwoo Choi  * @charging_max_duration_ms: Maximum possible duration for charging
1628fcfe088SChanwoo Choi  *	If whole charging duration exceed 'charging_max_duration_ms',
1638fcfe088SChanwoo Choi  *	cm stop charging.
1648fcfe088SChanwoo Choi  * @discharging_max_duration_ms:
1658fcfe088SChanwoo Choi  *	Maximum possible duration for discharging with charger cable
1668fcfe088SChanwoo Choi  *	after full-batt. If discharging duration exceed 'discharging
1678fcfe088SChanwoo Choi  *	max_duration_ms', cm start charging.
1683bb3dbbdSDonggeun Kim  */
1693bb3dbbdSDonggeun Kim struct charger_desc {
170856ee611SJonghwa Lee 	const char *psy_name;
171ad3d13eeSDonggeun Kim 
1723bb3dbbdSDonggeun Kim 	enum polling_modes polling_mode;
1733bb3dbbdSDonggeun Kim 	unsigned int polling_interval_ms;
1743bb3dbbdSDonggeun Kim 
175d829dc75SChanwoo Choi 	unsigned int fullbatt_vchkdrop_ms;
176d829dc75SChanwoo Choi 	unsigned int fullbatt_vchkdrop_uV;
177ad3d13eeSDonggeun Kim 	unsigned int fullbatt_uV;
1782ed9e9b6SChanwoo Choi 	unsigned int fullbatt_soc;
1792ed9e9b6SChanwoo Choi 	unsigned int fullbatt_full_capacity;
180ad3d13eeSDonggeun Kim 
1813bb3dbbdSDonggeun Kim 	enum data_source battery_present;
1823bb3dbbdSDonggeun Kim 
183856ee611SJonghwa Lee 	const char **psy_charger_stat;
1843bb3dbbdSDonggeun Kim 
1853bb3dbbdSDonggeun Kim 	int num_charger_regulators;
186bee737bcSChanwoo Choi 	struct charger_regulator *charger_regulators;
187157ba1bbSSebastian Reichel 	const struct attribute_group **sysfs_groups;
1883bb3dbbdSDonggeun Kim 
189856ee611SJonghwa Lee 	const char *psy_fuel_gauge;
1903bb3dbbdSDonggeun Kim 
191856ee611SJonghwa Lee 	const char *thermal_zone;
1925c49a625SJonghwa Lee 
1935c49a625SJonghwa Lee 	int temp_min;
1945c49a625SJonghwa Lee 	int temp_max;
1955c49a625SJonghwa Lee 	int temp_diff;
1965c49a625SJonghwa Lee 
197ad3d13eeSDonggeun Kim 	bool measure_battery_temp;
1988fcfe088SChanwoo Choi 
199856ee611SJonghwa Lee 	u32 charging_max_duration_ms;
200856ee611SJonghwa Lee 	u32 discharging_max_duration_ms;
2013bb3dbbdSDonggeun Kim };
2023bb3dbbdSDonggeun Kim 
2033bb3dbbdSDonggeun Kim #define PSY_NAME_MAX	30
2043bb3dbbdSDonggeun Kim 
2053bb3dbbdSDonggeun Kim /**
2063bb3dbbdSDonggeun Kim  * struct charger_manager
2073bb3dbbdSDonggeun Kim  * @entry: entry for list
2083bb3dbbdSDonggeun Kim  * @dev: device pointer
2093bb3dbbdSDonggeun Kim  * @desc: instance of charger_desc
2103bb3dbbdSDonggeun Kim  * @fuel_gauge: power_supply for fuel gauge
2113bb3dbbdSDonggeun Kim  * @charger_stat: array of power_supply for chargers
2125c49a625SJonghwa Lee  * @tzd_batt : thermal zone device for battery
2133bb3dbbdSDonggeun Kim  * @charger_enabled: the state of charger
214d829dc75SChanwoo Choi  * @fullbatt_vchk_jiffies_at:
215d829dc75SChanwoo Choi  *	jiffies at the time full battery check will occur.
216d829dc75SChanwoo Choi  * @fullbatt_vchk_work: work queue for full battery check
2173bb3dbbdSDonggeun Kim  * @emergency_stop:
2183bb3dbbdSDonggeun Kim  *	When setting true, stop charging
219ad3d13eeSDonggeun Kim  * @psy_name_buf: the name of power-supply-class for charger manager
220ad3d13eeSDonggeun Kim  * @charger_psy: power_supply for charger manager
2213bb3dbbdSDonggeun Kim  * @status_save_ext_pwr_inserted:
2223bb3dbbdSDonggeun Kim  *	saved status of external power before entering suspend-to-RAM
2233bb3dbbdSDonggeun Kim  * @status_save_batt:
2243bb3dbbdSDonggeun Kim  *	saved status of battery before entering suspend-to-RAM
2258fcfe088SChanwoo Choi  * @charging_start_time: saved start time of enabling charging
2268fcfe088SChanwoo Choi  * @charging_end_time: saved end time of disabling charging
2273bb3dbbdSDonggeun Kim  */
2283bb3dbbdSDonggeun Kim struct charger_manager {
2293bb3dbbdSDonggeun Kim 	struct list_head entry;
2303bb3dbbdSDonggeun Kim 	struct device *dev;
2313bb3dbbdSDonggeun Kim 	struct charger_desc *desc;
2323bb3dbbdSDonggeun Kim 
2335c49a625SJonghwa Lee #ifdef CONFIG_THERMAL
2345c49a625SJonghwa Lee 	struct thermal_zone_device *tzd_batt;
2355c49a625SJonghwa Lee #endif
2363bb3dbbdSDonggeun Kim 	bool charger_enabled;
2373bb3dbbdSDonggeun Kim 
238d829dc75SChanwoo Choi 	unsigned long fullbatt_vchk_jiffies_at;
239d829dc75SChanwoo Choi 	struct delayed_work fullbatt_vchk_work;
240d829dc75SChanwoo Choi 
2413bb3dbbdSDonggeun Kim 	int emergency_stop;
2423bb3dbbdSDonggeun Kim 
243ad3d13eeSDonggeun Kim 	char psy_name_buf[PSY_NAME_MAX + 1];
244297d716fSKrzysztof Kozlowski 	struct power_supply_desc charger_psy_desc;
245297d716fSKrzysztof Kozlowski 	struct power_supply *charger_psy;
246ad3d13eeSDonggeun Kim 
2478fcfe088SChanwoo Choi 	u64 charging_start_time;
2488fcfe088SChanwoo Choi 	u64 charging_end_time;
2493bb3dbbdSDonggeun Kim };
2503bb3dbbdSDonggeun Kim 
2513bb3dbbdSDonggeun Kim #ifdef CONFIG_CHARGER_MANAGER
252dfeccb12SChanwoo Choi extern void cm_notify_event(struct power_supply *psy,
253dfeccb12SChanwoo Choi 				enum cm_event_types type, char *msg);
2543bb3dbbdSDonggeun Kim #else
255dfeccb12SChanwoo Choi static inline void cm_notify_event(struct power_supply *psy,
256dfeccb12SChanwoo Choi 				enum cm_event_types type, char *msg) { }
2573bb3dbbdSDonggeun Kim #endif
2583bb3dbbdSDonggeun Kim #endif /* _CHARGER_MANAGER_H */
259