1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
4  * MyungJoo.Ham <myungjoo.ham@samsung.com>
5  *
6  * Charger Manager.
7  * This framework enables to control and multiple chargers and to
8  * monitor charging even in the context of suspend-to-RAM with
9  * an interface combining the chargers.
10  *
11 **/
12 
13 #ifndef _CHARGER_MANAGER_H
14 #define _CHARGER_MANAGER_H
15 
16 #include <linux/power_supply.h>
17 #include <linux/extcon.h>
18 #include <linux/alarmtimer.h>
19 
20 enum data_source {
21 	CM_BATTERY_PRESENT,
22 	CM_NO_BATTERY,
23 	CM_FUEL_GAUGE,
24 	CM_CHARGER_STAT,
25 };
26 
27 enum polling_modes {
28 	CM_POLL_DISABLE = 0,
29 	CM_POLL_ALWAYS,
30 	CM_POLL_EXTERNAL_POWER_ONLY,
31 	CM_POLL_CHARGING_ONLY,
32 };
33 
34 enum cm_event_types {
35 	CM_EVENT_UNKNOWN = 0,
36 	CM_EVENT_BATT_FULL,
37 	CM_EVENT_BATT_IN,
38 	CM_EVENT_BATT_OUT,
39 	CM_EVENT_BATT_OVERHEAT,
40 	CM_EVENT_BATT_COLD,
41 	CM_EVENT_EXT_PWR_IN_OUT,
42 	CM_EVENT_CHG_START_STOP,
43 	CM_EVENT_OTHERS,
44 };
45 
46 /**
47  * struct charger_cable
48  * @extcon_name: the name of extcon device.
49  * @name: the name of charger cable(external connector).
50  * @extcon_dev: the extcon device.
51  * @wq: the workqueue to control charger according to the state of
52  *	charger cable. If charger cable is attached, enable charger.
53  *	But if charger cable is detached, disable charger.
54  * @nb: the notifier block to receive changed state from EXTCON
55  *	(External Connector) when charger cable is attached/detached.
56  * @attached: the state of charger cable.
57  *	true: the charger cable is attached
58  *	false: the charger cable is detached
59  * @charger: the instance of struct charger_regulator.
60  * @cm: the Charger Manager representing the battery.
61  */
62 struct charger_cable {
63 	const char *extcon_name;
64 	const char *name;
65 
66 	/* The charger-manager use Extcon framework */
67 	struct extcon_specific_cable_nb extcon_dev;
68 	struct work_struct wq;
69 	struct notifier_block nb;
70 
71 	/* The state of charger cable */
72 	bool attached;
73 
74 	struct charger_regulator *charger;
75 
76 	/*
77 	 * Set min/max current of regulator to protect over-current issue
78 	 * according to a kind of charger cable when cable is attached.
79 	 */
80 	int min_uA;
81 	int max_uA;
82 
83 	struct charger_manager *cm;
84 };
85 
86 /**
87  * struct charger_regulator
88  * @regulator_name: the name of regulator for using charger.
89  * @consumer: the regulator consumer for the charger.
90  * @externally_control:
91  *	Set if the charger-manager cannot control charger,
92  *	the charger will be maintained with disabled state.
93  * @cables:
94  *	the array of charger cables to enable/disable charger
95  *	and set current limit according to constraint data of
96  *	struct charger_cable if only charger cable included
97  *	in the array of charger cables is attached/detached.
98  * @num_cables: the number of charger cables.
99  * @attr_g: Attribute group for the charger(regulator)
100  * @attr_name: "name" sysfs entry
101  * @attr_state: "state" sysfs entry
102  * @attr_externally_control: "externally_control" sysfs entry
103  * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
104  */
105 struct charger_regulator {
106 	/* The name of regulator for charging */
107 	const char *regulator_name;
108 	struct regulator *consumer;
109 
110 	/* charger never on when system is on */
111 	int externally_control;
112 
113 	/*
114 	 * Store constraint information related to current limit,
115 	 * each cable have different condition for charging.
116 	 */
117 	struct charger_cable *cables;
118 	int num_cables;
119 
120 	struct attribute_group attr_grp;
121 	struct device_attribute attr_name;
122 	struct device_attribute attr_state;
123 	struct device_attribute attr_externally_control;
124 	struct attribute *attrs[4];
125 
126 	struct charger_manager *cm;
127 };
128 
129 /**
130  * struct charger_desc
131  * @psy_name: the name of power-supply-class for charger manager
132  * @polling_mode:
133  *	Determine which polling mode will be used
134  * @fullbatt_vchkdrop_ms:
135  * @fullbatt_vchkdrop_uV:
136  *	Check voltage drop after the battery is fully charged.
137  *	If it has dropped more than fullbatt_vchkdrop_uV after
138  *	fullbatt_vchkdrop_ms, CM will restart charging.
139  * @fullbatt_uV: voltage in microvolt
140  *	If VBATT >= fullbatt_uV, it is assumed to be full.
141  * @fullbatt_soc: state of Charge in %
142  *	If state of Charge >= fullbatt_soc, it is assumed to be full.
143  * @fullbatt_full_capacity: full capacity measure
144  *	If full capacity of battery >= fullbatt_full_capacity,
145  *	it is assumed to be full.
146  * @polling_interval_ms: interval in millisecond at which
147  *	charger manager will monitor battery health
148  * @battery_present:
149  *	Specify where information for existence of battery can be obtained
150  * @psy_charger_stat: the names of power-supply for chargers
151  * @num_charger_regulator: the number of entries in charger_regulators
152  * @charger_regulators: array of charger regulators
153  * @psy_fuel_gauge: the name of power-supply for fuel gauge
154  * @thermal_zone : the name of thermal zone for battery
155  * @temp_min : Minimum battery temperature for charging.
156  * @temp_max : Maximum battery temperature for charging.
157  * @temp_diff : Temperature difference to restart charging.
158  * @measure_battery_temp:
159  *	true: measure battery temperature
160  *	false: measure ambient temperature
161  * @charging_max_duration_ms: Maximum possible duration for charging
162  *	If whole charging duration exceed 'charging_max_duration_ms',
163  *	cm stop charging.
164  * @discharging_max_duration_ms:
165  *	Maximum possible duration for discharging with charger cable
166  *	after full-batt. If discharging duration exceed 'discharging
167  *	max_duration_ms', cm start charging.
168  */
169 struct charger_desc {
170 	const char *psy_name;
171 
172 	enum polling_modes polling_mode;
173 	unsigned int polling_interval_ms;
174 
175 	unsigned int fullbatt_vchkdrop_ms;
176 	unsigned int fullbatt_vchkdrop_uV;
177 	unsigned int fullbatt_uV;
178 	unsigned int fullbatt_soc;
179 	unsigned int fullbatt_full_capacity;
180 
181 	enum data_source battery_present;
182 
183 	const char **psy_charger_stat;
184 
185 	int num_charger_regulators;
186 	struct charger_regulator *charger_regulators;
187 	const struct attribute_group **sysfs_groups;
188 
189 	const char *psy_fuel_gauge;
190 
191 	const char *thermal_zone;
192 
193 	int temp_min;
194 	int temp_max;
195 	int temp_diff;
196 
197 	bool measure_battery_temp;
198 
199 	u32 charging_max_duration_ms;
200 	u32 discharging_max_duration_ms;
201 };
202 
203 #define PSY_NAME_MAX	30
204 
205 /**
206  * struct charger_manager
207  * @entry: entry for list
208  * @dev: device pointer
209  * @desc: instance of charger_desc
210  * @fuel_gauge: power_supply for fuel gauge
211  * @charger_stat: array of power_supply for chargers
212  * @tzd_batt : thermal zone device for battery
213  * @charger_enabled: the state of charger
214  * @fullbatt_vchk_jiffies_at:
215  *	jiffies at the time full battery check will occur.
216  * @fullbatt_vchk_work: work queue for full battery check
217  * @emergency_stop:
218  *	When setting true, stop charging
219  * @psy_name_buf: the name of power-supply-class for charger manager
220  * @charger_psy: power_supply for charger manager
221  * @status_save_ext_pwr_inserted:
222  *	saved status of external power before entering suspend-to-RAM
223  * @status_save_batt:
224  *	saved status of battery before entering suspend-to-RAM
225  * @charging_start_time: saved start time of enabling charging
226  * @charging_end_time: saved end time of disabling charging
227  */
228 struct charger_manager {
229 	struct list_head entry;
230 	struct device *dev;
231 	struct charger_desc *desc;
232 
233 #ifdef CONFIG_THERMAL
234 	struct thermal_zone_device *tzd_batt;
235 #endif
236 	bool charger_enabled;
237 
238 	unsigned long fullbatt_vchk_jiffies_at;
239 	struct delayed_work fullbatt_vchk_work;
240 
241 	int emergency_stop;
242 
243 	char psy_name_buf[PSY_NAME_MAX + 1];
244 	struct power_supply_desc charger_psy_desc;
245 	struct power_supply *charger_psy;
246 
247 	u64 charging_start_time;
248 	u64 charging_end_time;
249 };
250 
251 #ifdef CONFIG_CHARGER_MANAGER
252 extern void cm_notify_event(struct power_supply *psy,
253 				enum cm_event_types type, char *msg);
254 #else
255 static inline void cm_notify_event(struct power_supply *psy,
256 				enum cm_event_types type, char *msg) { }
257 #endif
258 #endif /* _CHARGER_MANAGER_H */
259