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_batt_temp {
35 	CM_BATT_OK = 0,
36 	CM_BATT_OVERHEAT,
37 	CM_BATT_COLD,
38 };
39 
40 /**
41  * struct charger_cable
42  * @extcon_name: the name of extcon device.
43  * @name: the name of the cable connector
44  * @extcon_dev: the extcon device.
45  * @wq: the workqueue to control charger according to the state of
46  *	charger cable. If charger cable is attached, enable charger.
47  *	But if charger cable is detached, disable charger.
48  * @nb: the notifier block to receive changed state from EXTCON
49  *	(External Connector) when charger cable is attached/detached.
50  * @attached: the state of charger cable.
51  *	true: the charger cable is attached
52  *	false: the charger cable is detached
53  * @charger: the instance of struct charger_regulator.
54  * @cm: the Charger Manager representing the battery.
55  */
56 struct charger_cable {
57 	const char *extcon_name;
58 	const char *name;
59 	struct extcon_dev *extcon_dev;
60 	u64 extcon_type;
61 
62 	/* The charger-manager use Extcon framework */
63 	struct work_struct wq;
64 	struct notifier_block nb;
65 
66 	/* The state of charger cable */
67 	bool attached;
68 
69 	struct charger_regulator *charger;
70 
71 	/*
72 	 * Set min/max current of regulator to protect over-current issue
73 	 * according to a kind of charger cable when cable is attached.
74 	 */
75 	int min_uA;
76 	int max_uA;
77 
78 	struct charger_manager *cm;
79 };
80 
81 /**
82  * struct charger_regulator
83  * @regulator_name: the name of regulator for using charger.
84  * @consumer: the regulator consumer for the charger.
85  * @externally_control:
86  *	Set if the charger-manager cannot control charger,
87  *	the charger will be maintained with disabled state.
88  * @cables:
89  *	the array of charger cables to enable/disable charger
90  *	and set current limit according to constraint data of
91  *	struct charger_cable if only charger cable included
92  *	in the array of charger cables is attached/detached.
93  * @num_cables: the number of charger cables.
94  * @attr_g: Attribute group for the charger(regulator)
95  * @attr_name: "name" sysfs entry
96  * @attr_state: "state" sysfs entry
97  * @attr_externally_control: "externally_control" sysfs entry
98  * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
99  */
100 struct charger_regulator {
101 	/* The name of regulator for charging */
102 	const char *regulator_name;
103 	struct regulator *consumer;
104 
105 	/* charger never on when system is on */
106 	int externally_control;
107 
108 	/*
109 	 * Store constraint information related to current limit,
110 	 * each cable have different condition for charging.
111 	 */
112 	struct charger_cable *cables;
113 	int num_cables;
114 
115 	struct attribute_group attr_grp;
116 	struct device_attribute attr_name;
117 	struct device_attribute attr_state;
118 	struct device_attribute attr_externally_control;
119 	struct attribute *attrs[4];
120 
121 	struct charger_manager *cm;
122 };
123 
124 /**
125  * struct charger_desc
126  * @psy_name: the name of power-supply-class for charger manager
127  * @polling_mode:
128  *	Determine which polling mode will be used
129  * @fullbatt_vchkdrop_uV:
130  *	Check voltage drop after the battery is fully charged.
131  *	If it has dropped more than fullbatt_vchkdrop_uV
132  *	CM will restart charging.
133  * @fullbatt_uV: voltage in microvolt
134  *	If VBATT >= fullbatt_uV, it is assumed to be full.
135  * @fullbatt_soc: state of Charge in %
136  *	If state of Charge >= fullbatt_soc, it is assumed to be full.
137  * @fullbatt_full_capacity: full capacity measure
138  *	If full capacity of battery >= fullbatt_full_capacity,
139  *	it is assumed to be full.
140  * @polling_interval_ms: interval in millisecond at which
141  *	charger manager will monitor battery health
142  * @battery_present:
143  *	Specify where information for existence of battery can be obtained
144  * @psy_charger_stat: the names of power-supply for chargers
145  * @num_charger_regulator: the number of entries in charger_regulators
146  * @charger_regulators: array of charger regulators
147  * @psy_fuel_gauge: the name of power-supply for fuel gauge
148  * @thermal_zone : the name of thermal zone for battery
149  * @temp_min : Minimum battery temperature for charging.
150  * @temp_max : Maximum battery temperature for charging.
151  * @temp_diff : Temperature difference to restart charging.
152  * @measure_battery_temp:
153  *	true: measure battery temperature
154  *	false: measure ambient temperature
155  * @charging_max_duration_ms: Maximum possible duration for charging
156  *	If whole charging duration exceed 'charging_max_duration_ms',
157  *	cm stop charging.
158  * @discharging_max_duration_ms:
159  *	Maximum possible duration for discharging with charger cable
160  *	after full-batt. If discharging duration exceed 'discharging
161  *	max_duration_ms', cm start charging.
162  */
163 struct charger_desc {
164 	const char *psy_name;
165 
166 	enum polling_modes polling_mode;
167 	unsigned int polling_interval_ms;
168 
169 	unsigned int fullbatt_vchkdrop_uV;
170 	unsigned int fullbatt_uV;
171 	unsigned int fullbatt_soc;
172 	unsigned int fullbatt_full_capacity;
173 
174 	enum data_source battery_present;
175 
176 	const char **psy_charger_stat;
177 
178 	int num_charger_regulators;
179 	struct charger_regulator *charger_regulators;
180 	const struct attribute_group **sysfs_groups;
181 
182 	const char *psy_fuel_gauge;
183 
184 	const char *thermal_zone;
185 
186 	int temp_min;
187 	int temp_max;
188 	int temp_diff;
189 
190 	bool measure_battery_temp;
191 
192 	u32 charging_max_duration_ms;
193 	u32 discharging_max_duration_ms;
194 };
195 
196 #define PSY_NAME_MAX	30
197 
198 /**
199  * struct charger_manager
200  * @entry: entry for list
201  * @dev: device pointer
202  * @desc: instance of charger_desc
203  * @fuel_gauge: power_supply for fuel gauge
204  * @charger_stat: array of power_supply for chargers
205  * @tzd_batt : thermal zone device for battery
206  * @charger_enabled: the state of charger
207  * @emergency_stop:
208  *	When setting true, stop charging
209  * @psy_name_buf: the name of power-supply-class for charger manager
210  * @charger_psy: power_supply for charger manager
211  * @status_save_ext_pwr_inserted:
212  *	saved status of external power before entering suspend-to-RAM
213  * @status_save_batt:
214  *	saved status of battery before entering suspend-to-RAM
215  * @charging_start_time: saved start time of enabling charging
216  * @charging_end_time: saved end time of disabling charging
217  * @battery_status: Current battery status
218  */
219 struct charger_manager {
220 	struct list_head entry;
221 	struct device *dev;
222 	struct charger_desc *desc;
223 
224 #ifdef CONFIG_THERMAL
225 	struct thermal_zone_device *tzd_batt;
226 #endif
227 	bool charger_enabled;
228 
229 	int emergency_stop;
230 
231 	char psy_name_buf[PSY_NAME_MAX + 1];
232 	struct power_supply_desc charger_psy_desc;
233 	struct power_supply *charger_psy;
234 
235 	u64 charging_start_time;
236 	u64 charging_end_time;
237 
238 	int battery_status;
239 };
240 
241 #endif /* _CHARGER_MANAGER_H */
242