1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *  Universal power supply monitor class
4  *
5  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
6  *  Copyright © 2004  Szabolcs Gyurko
7  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
8  *
9  *  Modified: 2004, Oct     Szabolcs Gyurko
10  */
11 
12 #ifndef __LINUX_POWER_SUPPLY_H__
13 #define __LINUX_POWER_SUPPLY_H__
14 
15 #include <linux/device.h>
16 #include <linux/workqueue.h>
17 #include <linux/leds.h>
18 #include <linux/spinlock.h>
19 #include <linux/notifier.h>
20 
21 /*
22  * All voltages, currents, charges, energies, time and temperatures in uV,
23  * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
24  * stated. It's driver's job to convert its raw values to units in which
25  * this class operates.
26  */
27 
28 /*
29  * For systems where the charger determines the maximum battery capacity
30  * the min and max fields should be used to present these values to user
31  * space. Unused/unknown fields will not appear in sysfs.
32  */
33 
34 enum {
35 	POWER_SUPPLY_STATUS_UNKNOWN = 0,
36 	POWER_SUPPLY_STATUS_CHARGING,
37 	POWER_SUPPLY_STATUS_DISCHARGING,
38 	POWER_SUPPLY_STATUS_NOT_CHARGING,
39 	POWER_SUPPLY_STATUS_FULL,
40 };
41 
42 /* What algorithm is the charger using? */
43 enum {
44 	POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,
45 	POWER_SUPPLY_CHARGE_TYPE_NONE,
46 	POWER_SUPPLY_CHARGE_TYPE_TRICKLE,	/* slow speed */
47 	POWER_SUPPLY_CHARGE_TYPE_FAST,		/* fast speed */
48 	POWER_SUPPLY_CHARGE_TYPE_STANDARD,	/* normal speed */
49 	POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE,	/* dynamically adjusted speed */
50 	POWER_SUPPLY_CHARGE_TYPE_CUSTOM,	/* use CHARGE_CONTROL_* props */
51 	POWER_SUPPLY_CHARGE_TYPE_LONGLIFE,	/* slow speed, longer life */
52 	POWER_SUPPLY_CHARGE_TYPE_BYPASS,	/* bypassing the charger */
53 };
54 
55 enum {
56 	POWER_SUPPLY_HEALTH_UNKNOWN = 0,
57 	POWER_SUPPLY_HEALTH_GOOD,
58 	POWER_SUPPLY_HEALTH_OVERHEAT,
59 	POWER_SUPPLY_HEALTH_DEAD,
60 	POWER_SUPPLY_HEALTH_OVERVOLTAGE,
61 	POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
62 	POWER_SUPPLY_HEALTH_COLD,
63 	POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE,
64 	POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE,
65 	POWER_SUPPLY_HEALTH_OVERCURRENT,
66 	POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED,
67 	POWER_SUPPLY_HEALTH_WARM,
68 	POWER_SUPPLY_HEALTH_COOL,
69 	POWER_SUPPLY_HEALTH_HOT,
70 	POWER_SUPPLY_HEALTH_NO_BATTERY,
71 };
72 
73 enum {
74 	POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
75 	POWER_SUPPLY_TECHNOLOGY_NiMH,
76 	POWER_SUPPLY_TECHNOLOGY_LION,
77 	POWER_SUPPLY_TECHNOLOGY_LIPO,
78 	POWER_SUPPLY_TECHNOLOGY_LiFe,
79 	POWER_SUPPLY_TECHNOLOGY_NiCd,
80 	POWER_SUPPLY_TECHNOLOGY_LiMn,
81 };
82 
83 enum {
84 	POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
85 	POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
86 	POWER_SUPPLY_CAPACITY_LEVEL_LOW,
87 	POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
88 	POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
89 	POWER_SUPPLY_CAPACITY_LEVEL_FULL,
90 };
91 
92 enum {
93 	POWER_SUPPLY_SCOPE_UNKNOWN = 0,
94 	POWER_SUPPLY_SCOPE_SYSTEM,
95 	POWER_SUPPLY_SCOPE_DEVICE,
96 };
97 
98 enum power_supply_property {
99 	/* Properties of type `int' */
100 	POWER_SUPPLY_PROP_STATUS = 0,
101 	POWER_SUPPLY_PROP_CHARGE_TYPE,
102 	POWER_SUPPLY_PROP_HEALTH,
103 	POWER_SUPPLY_PROP_PRESENT,
104 	POWER_SUPPLY_PROP_ONLINE,
105 	POWER_SUPPLY_PROP_AUTHENTIC,
106 	POWER_SUPPLY_PROP_TECHNOLOGY,
107 	POWER_SUPPLY_PROP_CYCLE_COUNT,
108 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
109 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
110 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
111 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
112 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
113 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
114 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
115 	POWER_SUPPLY_PROP_VOLTAGE_BOOT,
116 	POWER_SUPPLY_PROP_CURRENT_MAX,
117 	POWER_SUPPLY_PROP_CURRENT_NOW,
118 	POWER_SUPPLY_PROP_CURRENT_AVG,
119 	POWER_SUPPLY_PROP_CURRENT_BOOT,
120 	POWER_SUPPLY_PROP_POWER_NOW,
121 	POWER_SUPPLY_PROP_POWER_AVG,
122 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
123 	POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
124 	POWER_SUPPLY_PROP_CHARGE_FULL,
125 	POWER_SUPPLY_PROP_CHARGE_EMPTY,
126 	POWER_SUPPLY_PROP_CHARGE_NOW,
127 	POWER_SUPPLY_PROP_CHARGE_AVG,
128 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
129 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
130 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
131 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
132 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
133 	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
134 	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
135 	POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, /* in percents! */
136 	POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, /* in percents! */
137 	POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
138 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
139 	POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
140 	POWER_SUPPLY_PROP_INPUT_POWER_LIMIT,
141 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
142 	POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
143 	POWER_SUPPLY_PROP_ENERGY_FULL,
144 	POWER_SUPPLY_PROP_ENERGY_EMPTY,
145 	POWER_SUPPLY_PROP_ENERGY_NOW,
146 	POWER_SUPPLY_PROP_ENERGY_AVG,
147 	POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
148 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
149 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
150 	POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, /* in percents! */
151 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
152 	POWER_SUPPLY_PROP_TEMP,
153 	POWER_SUPPLY_PROP_TEMP_MAX,
154 	POWER_SUPPLY_PROP_TEMP_MIN,
155 	POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
156 	POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
157 	POWER_SUPPLY_PROP_TEMP_AMBIENT,
158 	POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
159 	POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
160 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
161 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
162 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
163 	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
164 	POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
165 	POWER_SUPPLY_PROP_USB_TYPE,
166 	POWER_SUPPLY_PROP_SCOPE,
167 	POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
168 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
169 	POWER_SUPPLY_PROP_CALIBRATE,
170 	POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
171 	POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
172 	POWER_SUPPLY_PROP_MANUFACTURE_DAY,
173 	/* Properties of type `const char *' */
174 	POWER_SUPPLY_PROP_MODEL_NAME,
175 	POWER_SUPPLY_PROP_MANUFACTURER,
176 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
177 };
178 
179 enum power_supply_type {
180 	POWER_SUPPLY_TYPE_UNKNOWN = 0,
181 	POWER_SUPPLY_TYPE_BATTERY,
182 	POWER_SUPPLY_TYPE_UPS,
183 	POWER_SUPPLY_TYPE_MAINS,
184 	POWER_SUPPLY_TYPE_USB,			/* Standard Downstream Port */
185 	POWER_SUPPLY_TYPE_USB_DCP,		/* Dedicated Charging Port */
186 	POWER_SUPPLY_TYPE_USB_CDP,		/* Charging Downstream Port */
187 	POWER_SUPPLY_TYPE_USB_ACA,		/* Accessory Charger Adapters */
188 	POWER_SUPPLY_TYPE_USB_TYPE_C,		/* Type C Port */
189 	POWER_SUPPLY_TYPE_USB_PD,		/* Power Delivery Port */
190 	POWER_SUPPLY_TYPE_USB_PD_DRP,		/* PD Dual Role Port */
191 	POWER_SUPPLY_TYPE_APPLE_BRICK_ID,	/* Apple Charging Method */
192 	POWER_SUPPLY_TYPE_WIRELESS,		/* Wireless */
193 };
194 
195 enum power_supply_usb_type {
196 	POWER_SUPPLY_USB_TYPE_UNKNOWN = 0,
197 	POWER_SUPPLY_USB_TYPE_SDP,		/* Standard Downstream Port */
198 	POWER_SUPPLY_USB_TYPE_DCP,		/* Dedicated Charging Port */
199 	POWER_SUPPLY_USB_TYPE_CDP,		/* Charging Downstream Port */
200 	POWER_SUPPLY_USB_TYPE_ACA,		/* Accessory Charger Adapters */
201 	POWER_SUPPLY_USB_TYPE_C,		/* Type C Port */
202 	POWER_SUPPLY_USB_TYPE_PD,		/* Power Delivery Port */
203 	POWER_SUPPLY_USB_TYPE_PD_DRP,		/* PD Dual Role Port */
204 	POWER_SUPPLY_USB_TYPE_PD_PPS,		/* PD Programmable Power Supply */
205 	POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID,	/* Apple Charging Method */
206 };
207 
208 enum power_supply_charge_behaviour {
209 	POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO = 0,
210 	POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE,
211 	POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE,
212 };
213 
214 enum power_supply_notifier_events {
215 	PSY_EVENT_PROP_CHANGED,
216 };
217 
218 union power_supply_propval {
219 	int intval;
220 	const char *strval;
221 };
222 
223 struct device_node;
224 struct power_supply;
225 
226 /* Run-time specific power supply configuration */
227 struct power_supply_config {
228 	struct device_node *of_node;
229 	struct fwnode_handle *fwnode;
230 
231 	/* Driver private data */
232 	void *drv_data;
233 
234 	/* Device specific sysfs attributes */
235 	const struct attribute_group **attr_grp;
236 
237 	char **supplied_to;
238 	size_t num_supplicants;
239 };
240 
241 /* Description of power supply */
242 struct power_supply_desc {
243 	const char *name;
244 	enum power_supply_type type;
245 	const enum power_supply_usb_type *usb_types;
246 	size_t num_usb_types;
247 	const enum power_supply_property *properties;
248 	size_t num_properties;
249 
250 	/*
251 	 * Functions for drivers implementing power supply class.
252 	 * These shouldn't be called directly by other drivers for accessing
253 	 * this power supply. Instead use power_supply_*() functions (for
254 	 * example power_supply_get_property()).
255 	 */
256 	int (*get_property)(struct power_supply *psy,
257 			    enum power_supply_property psp,
258 			    union power_supply_propval *val);
259 	int (*set_property)(struct power_supply *psy,
260 			    enum power_supply_property psp,
261 			    const union power_supply_propval *val);
262 	/*
263 	 * property_is_writeable() will be called during registration
264 	 * of power supply. If this happens during device probe then it must
265 	 * not access internal data of device (because probe did not end).
266 	 */
267 	int (*property_is_writeable)(struct power_supply *psy,
268 				     enum power_supply_property psp);
269 	void (*external_power_changed)(struct power_supply *psy);
270 	void (*set_charged)(struct power_supply *psy);
271 
272 	/*
273 	 * Set if thermal zone should not be created for this power supply.
274 	 * For example for virtual supplies forwarding calls to actual
275 	 * sensors or other supplies.
276 	 */
277 	bool no_thermal;
278 	/* For APM emulation, think legacy userspace. */
279 	int use_for_apm;
280 };
281 
282 struct power_supply {
283 	const struct power_supply_desc *desc;
284 
285 	char **supplied_to;
286 	size_t num_supplicants;
287 
288 	char **supplied_from;
289 	size_t num_supplies;
290 	struct device_node *of_node;
291 
292 	/* Driver private data */
293 	void *drv_data;
294 
295 	/* private */
296 	struct device dev;
297 	struct work_struct changed_work;
298 	struct delayed_work deferred_register_work;
299 	spinlock_t changed_lock;
300 	bool changed;
301 	bool initialized;
302 	bool removing;
303 	atomic_t use_cnt;
304 #ifdef CONFIG_THERMAL
305 	struct thermal_zone_device *tzd;
306 	struct thermal_cooling_device *tcd;
307 #endif
308 
309 #ifdef CONFIG_LEDS_TRIGGERS
310 	struct led_trigger *charging_full_trig;
311 	char *charging_full_trig_name;
312 	struct led_trigger *charging_trig;
313 	char *charging_trig_name;
314 	struct led_trigger *full_trig;
315 	char *full_trig_name;
316 	struct led_trigger *online_trig;
317 	char *online_trig_name;
318 	struct led_trigger *charging_blink_full_solid_trig;
319 	char *charging_blink_full_solid_trig_name;
320 #endif
321 };
322 
323 /*
324  * This is recommended structure to specify static power supply parameters.
325  * Generic one, parametrizable for different power supplies. Power supply
326  * class itself does not use it, but that's what implementing most platform
327  * drivers, should try reuse for consistency.
328  */
329 
330 struct power_supply_info {
331 	const char *name;
332 	int technology;
333 	int voltage_max_design;
334 	int voltage_min_design;
335 	int charge_full_design;
336 	int charge_empty_design;
337 	int energy_full_design;
338 	int energy_empty_design;
339 	int use_for_apm;
340 };
341 
342 struct power_supply_battery_ocv_table {
343 	int ocv;	/* microVolts */
344 	int capacity;	/* percent */
345 };
346 
347 struct power_supply_resistance_temp_table {
348 	int temp;	/* celsius */
349 	int resistance;	/* internal resistance percent */
350 };
351 
352 struct power_supply_vbat_ri_table {
353 	int vbat_uv;	/* Battery voltage in microvolt */
354 	int ri_uohm;	/* Internal resistance in microohm */
355 };
356 
357 /**
358  * struct power_supply_maintenance_charge_table - setting for maintenace charging
359  * @charge_current_max_ua: maintenance charging current that is used to keep
360  *   the charge of the battery full as current is consumed after full charging.
361  *   The corresponding charge_voltage_max_uv is used as a safeguard: when we
362  *   reach this voltage the maintenance charging current is turned off. It is
363  *   turned back on if we fall below this voltage.
364  * @charge_voltage_max_uv: maintenance charging voltage that is usually a bit
365  *   lower than the constant_charge_voltage_max_uv. We can apply this settings
366  *   charge_current_max_ua until we get back up to this voltage.
367  * @safety_timer_minutes: maintenance charging safety timer, with an expiry
368  *   time in minutes. We will only use maintenance charging in this setting
369  *   for a certain amount of time, then we will first move to the next
370  *   maintenance charge current and voltage pair in respective array and wait
371  *   for the next safety timer timeout, or, if we reached the last maintencance
372  *   charging setting, disable charging until we reach
373  *   charge_restart_voltage_uv and restart ordinary CC/CV charging from there.
374  *   These timers should be chosen to align with the typical discharge curve
375  *   for the battery.
376  *
377  * When the main CC/CV charging is complete the battery can optionally be
378  * maintenance charged at the voltages from this table: a table of settings is
379  * traversed using a slightly lower current and voltage than what is used for
380  * CC/CV charging. The maintenance charging will for safety reasons not go on
381  * indefinately: we lower the current and voltage with successive maintenance
382  * settings, then disable charging completely after we reach the last one,
383  * and after that we do not restart charging until we reach
384  * charge_restart_voltage_uv (see struct power_supply_battery_info) and restart
385  * ordinary CC/CV charging from there.
386  *
387  * As an example, a Samsung EB425161LA Lithium-Ion battery is CC/CV charged
388  * at 900mA to 4340mV, then maintenance charged at 600mA and 4150mV for
389  * 60 hours, then maintenance charged at 600mA and 4100mV for 200 hours.
390  * After this the charge cycle is restarted waiting for
391  * charge_restart_voltage_uv.
392  *
393  * For most mobile electronics this type of maintenance charging is enough for
394  * the user to disconnect the device and make use of it before both maintenance
395  * charging cycles are complete.
396  */
397 struct power_supply_maintenance_charge_table {
398 	int charge_current_max_ua;
399 	int charge_voltage_max_uv;
400 	int charge_safety_timer_minutes;
401 };
402 
403 #define POWER_SUPPLY_OCV_TEMP_MAX 20
404 
405 /**
406  * struct power_supply_battery_info - information about batteries
407  * @technology: from the POWER_SUPPLY_TECHNOLOGY_* enum
408  * @energy_full_design_uwh: energy content when fully charged in microwatt
409  *   hours
410  * @charge_full_design_uah: charge content when fully charged in microampere
411  *   hours
412  * @voltage_min_design_uv: minimum voltage across the poles when the battery
413  *   is at minimum voltage level in microvolts. If the voltage drops below this
414  *   level the battery will need precharging when using CC/CV charging.
415  * @voltage_max_design_uv: voltage across the poles when the battery is fully
416  *   charged in microvolts. This is the "nominal voltage" i.e. the voltage
417  *   printed on the label of the battery.
418  * @tricklecharge_current_ua: the tricklecharge current used when trickle
419  *   charging the battery in microamperes. This is the charging phase when the
420  *   battery is completely empty and we need to carefully trickle in some
421  *   charge until we reach the precharging voltage.
422  * @precharge_current_ua: current to use in the precharge phase in microamperes,
423  *   the precharge rate is limited by limiting the current to this value.
424  * @precharge_voltage_max_uv: the maximum voltage allowed when precharging in
425  *   microvolts. When we pass this voltage we will nominally switch over to the
426  *   CC (constant current) charging phase defined by constant_charge_current_ua
427  *   and constant_charge_voltage_max_uv.
428  * @charge_term_current_ua: when the current in the CV (constant voltage)
429  *   charging phase drops below this value in microamperes the charging will
430  *   terminate completely and not restart until the voltage over the battery
431  *   poles reach charge_restart_voltage_uv unless we use maintenance charging.
432  * @charge_restart_voltage_uv: when the battery has been fully charged by
433  *   CC/CV charging and charging has been disabled, and the voltage subsequently
434  *   drops below this value in microvolts, the charging will be restarted
435  *   (typically using CV charging).
436  * @overvoltage_limit_uv: If the voltage exceeds the nominal voltage
437  *   voltage_max_design_uv and we reach this voltage level, all charging must
438  *   stop and emergency procedures take place, such as shutting down the system
439  *   in some cases.
440  * @constant_charge_current_max_ua: current in microamperes to use in the CC
441  *   (constant current) charging phase. The charging rate is limited
442  *   by this current. This is the main charging phase and as the current is
443  *   constant into the battery the voltage slowly ascends to
444  *   constant_charge_voltage_max_uv.
445  * @constant_charge_voltage_max_uv: voltage in microvolts signifying the end of
446  *   the CC (constant current) charging phase and the beginning of the CV
447  *   (constant voltage) charging phase.
448  * @maintenance_charge: an array of maintenance charging settings to be used
449  *   after the main CC/CV charging phase is complete.
450  * @maintenance_charge_size: the number of maintenance charging settings in
451  *   maintenance_charge.
452  * @alert_low_temp_charge_current_ua: The charging current to use if the battery
453  *   enters low alert temperature, i.e. if the internal temperature is between
454  *   temp_alert_min and temp_min. No matter the charging phase, this
455  *   and alert_high_temp_charge_voltage_uv will be applied.
456  * @alert_low_temp_charge_voltage_uv: Same as alert_low_temp_charge_current_ua,
457  *   but for the charging voltage.
458  * @alert_high_temp_charge_current_ua: The charging current to use if the
459  *   battery enters high alert temperature, i.e. if the internal temperature is
460  *   between temp_alert_max and temp_max. No matter the charging phase, this
461  *   and alert_high_temp_charge_voltage_uv will be applied, usually lowering
462  *   the charging current as an evasive manouver.
463  * @alert_high_temp_charge_voltage_uv: Same as
464  *   alert_high_temp_charge_current_ua, but for the charging voltage.
465  * @factory_internal_resistance_uohm: the internal resistance of the battery
466  *   at fabrication time, expressed in microohms. This resistance will vary
467  *   depending on the lifetime and charge of the battery, so this is just a
468  *   nominal ballpark figure. This internal resistance is given for the state
469  *   when the battery is discharging.
470  * @factory_internal_resistance_charging_uohm: the internal resistance of the
471  *   battery at fabrication time while charging, expressed in microohms.
472  *   The charging process will affect the internal resistance of the battery
473  *   so this value provides a better resistance under these circumstances.
474  *   This resistance will vary depending on the lifetime and charge of the
475  *   battery, so this is just a nominal ballpark figure.
476  * @ocv_temp: array indicating the open circuit voltage (OCV) capacity
477  *   temperature indices. This is an array of temperatures in degrees Celsius
478  *   indicating which capacity table to use for a certain temperature, since
479  *   the capacity for reasons of chemistry will be different at different
480  *   temperatures. Determining capacity is a multivariate problem and the
481  *   temperature is the first variable we determine.
482  * @temp_ambient_alert_min: the battery will go outside of operating conditions
483  *   when the ambient temperature goes below this temperature in degrees
484  *   Celsius.
485  * @temp_ambient_alert_max: the battery will go outside of operating conditions
486  *   when the ambient temperature goes above this temperature in degrees
487  *   Celsius.
488  * @temp_alert_min: the battery should issue an alert if the internal
489  *   temperature goes below this temperature in degrees Celsius.
490  * @temp_alert_max: the battery should issue an alert if the internal
491  *   temperature goes above this temperature in degrees Celsius.
492  * @temp_min: the battery will go outside of operating conditions when
493  *   the internal temperature goes below this temperature in degrees Celsius.
494  *   Normally this means the system should shut down.
495  * @temp_max: the battery will go outside of operating conditions when
496  *   the internal temperature goes above this temperature in degrees Celsius.
497  *   Normally this means the system should shut down.
498  * @ocv_table: for each entry in ocv_temp there is a corresponding entry in
499  *   ocv_table and a size for each entry in ocv_table_size. These arrays
500  *   determine the capacity in percent in relation to the voltage in microvolts
501  *   at the indexed temperature.
502  * @ocv_table_size: for each entry in ocv_temp this array is giving the size of
503  *   each entry in the array of capacity arrays in ocv_table.
504  * @resist_table: this is a table that correlates a battery temperature to the
505  *   expected internal resistance at this temperature. The resistance is given
506  *   as a percentage of factory_internal_resistance_uohm. Knowing the
507  *   resistance of the battery is usually necessary for calculating the open
508  *   circuit voltage (OCV) that is then used with the ocv_table to calculate
509  *   the capacity of the battery. The resist_table must be ordered descending
510  *   by temperature: highest temperature with lowest resistance first, lowest
511  *   temperature with highest resistance last.
512  * @resist_table_size: the number of items in the resist_table.
513  * @vbat2ri_discharging: this is a table that correlates Battery voltage (VBAT)
514  *   to internal resistance (Ri). The resistance is given in microohm for the
515  *   corresponding voltage in microvolts. The internal resistance is used to
516  *   determine the open circuit voltage so that we can determine the capacity
517  *   of the battery. These voltages to resistance tables apply when the battery
518  *   is discharging. The table must be ordered descending by voltage: highest
519  *   voltage first.
520  * @vbat2ri_discharging_size: the number of items in the vbat2ri_discharging
521  *   table.
522  * @vbat2ri_charging: same function as vbat2ri_discharging but for the state
523  *   when the battery is charging. Being under charge changes the battery's
524  *   internal resistance characteristics so a separate table is needed.*
525  *   The table must be ordered descending by voltage: highest voltage first.
526  * @vbat2ri_charging_size: the number of items in the vbat2ri_charging
527  *   table.
528  * @bti_resistance_ohm: The Battery Type Indicator (BIT) nominal resistance
529  *   in ohms for this battery, if an identification resistor is mounted
530  *   between a third battery terminal and ground. This scheme is used by a lot
531  *   of mobile device batteries.
532  * @bti_resistance_tolerance: The tolerance in percent of the BTI resistance,
533  *   for example 10 for +/- 10%, if the bti_resistance is set to 7000 and the
534  *   tolerance is 10% we will detect a proper battery if the BTI resistance
535  *   is between 6300 and 7700 Ohm.
536  *
537  * This is the recommended struct to manage static battery parameters,
538  * populated by power_supply_get_battery_info(). Most platform drivers should
539  * use these for consistency.
540  *
541  * Its field names must correspond to elements in enum power_supply_property.
542  * The default field value is -EINVAL or NULL for pointers.
543  *
544  * CC/CV CHARGING:
545  *
546  * The charging parameters here assume a CC/CV charging scheme. This method
547  * is most common with Lithium Ion batteries (other methods are possible) and
548  * looks as follows:
549  *
550  * ^ Battery voltage
551  * |                                               --- overvoltage_limit_uv
552  * |
553  * |                    ...................................................
554  * |                 .. constant_charge_voltage_max_uv
555  * |              ..
556  * |             .
557  * |            .
558  * |           .
559  * |          .
560  * |         .
561  * |     .. precharge_voltage_max_uv
562  * |  ..
563  * |. (trickle charging)
564  * +------------------------------------------------------------------> time
565  *
566  * ^ Current into the battery
567  * |
568  * |      ............. constant_charge_current_max_ua
569  * |      .            .
570  * |      .             .
571  * |      .              .
572  * |      .               .
573  * |      .                ..
574  * |      .                  ....
575  * |      .                       .....
576  * |    ... precharge_current_ua       .......  charge_term_current_ua
577  * |    .                                    .
578  * |    .                                    .
579  * |.... tricklecharge_current_ua            .
580  * |                                         .
581  * +-----------------------------------------------------------------> time
582  *
583  * These diagrams are synchronized on time and the voltage and current
584  * follow each other.
585  *
586  * With CC/CV charging commence over time like this for an empty battery:
587  *
588  * 1. When the battery is completely empty it may need to be charged with
589  *    an especially small current so that electrons just "trickle in",
590  *    this is the tricklecharge_current_ua.
591  *
592  * 2. Next a small initial pre-charge current (precharge_current_ua)
593  *    is applied if the voltage is below precharge_voltage_max_uv until we
594  *    reach precharge_voltage_max_uv. CAUTION: in some texts this is referred
595  *    to as "trickle charging" but the use in the Linux kernel is different
596  *    see below!
597  *
598  * 3. Then the main charging current is applied, which is called the constant
599  *    current (CC) phase. A current regulator is set up to allow
600  *    constant_charge_current_max_ua of current to flow into the battery.
601  *    The chemical reaction in the battery will make the voltage go up as
602  *    charge goes into the battery. This current is applied until we reach
603  *    the constant_charge_voltage_max_uv voltage.
604  *
605  * 4. At this voltage we switch over to the constant voltage (CV) phase. This
606  *    means we allow current to go into the battery, but we keep the voltage
607  *    fixed. This current will continue to charge the battery while keeping
608  *    the voltage the same. A chemical reaction in the battery goes on
609  *    storing energy without affecting the voltage. Over time the current
610  *    will slowly drop and when we reach charge_term_current_ua we will
611  *    end the constant voltage phase.
612  *
613  * After this the battery is fully charged, and if we do not support maintenance
614  * charging, the charging will not restart until power dissipation makes the
615  * voltage fall so that we reach charge_restart_voltage_uv and at this point
616  * we restart charging at the appropriate phase, usually this will be inside
617  * the CV phase.
618  *
619  * If we support maintenance charging the voltage is however kept high after
620  * the CV phase with a very low current. This is meant to let the same charge
621  * go in for usage while the charger is still connected, mainly for
622  * dissipation for the power consuming entity while connected to the
623  * charger.
624  *
625  * All charging MUST terminate if the overvoltage_limit_uv is ever reached.
626  * Overcharging Lithium Ion cells can be DANGEROUS and lead to fire or
627  * explosions.
628  *
629  * DETERMINING BATTERY CAPACITY:
630  *
631  * Several members of the struct deal with trying to determine the remaining
632  * capacity in the battery, usually as a percentage of charge. In practice
633  * many chargers uses a so-called fuel gauge or coloumb counter that measure
634  * how much charge goes into the battery and how much goes out (+/- leak
635  * consumption). This does not help if we do not know how much capacity the
636  * battery has to begin with, such as when it is first used or was taken out
637  * and charged in a separate charger. Therefore many capacity algorithms use
638  * the open circuit voltage with a look-up table to determine the rough
639  * capacity of the battery. The open circuit voltage can be conceptualized
640  * with an ideal voltage source (V) in series with an internal resistance (Ri)
641  * like this:
642  *
643  *      +-------> IBAT >----------------+
644  *      |                    ^          |
645  *     [ ] Ri                |          |
646  *      |                    | VBAT     |
647  *      o <----------        |          |
648  *     +|           ^        |         [ ] Rload
649  *    .---.         |        |          |
650  *    | V |         | OCV    |          |
651  *    '---'         |        |          |
652  *      |           |        |          |
653  *  GND +-------------------------------+
654  *
655  * If we disconnect the load (here simplified as a fixed resistance Rload)
656  * and measure VBAT with a infinite impedance voltage meter we will get
657  * VBAT = OCV and this assumption is sometimes made even under load, assuming
658  * Rload is insignificant. However this will be of dubious quality because the
659  * load is rarely that small and Ri is strongly nonlinear depending on
660  * temperature and how much capacity is left in the battery due to the
661  * chemistry involved.
662  *
663  * In many practical applications we cannot just disconnect the battery from
664  * the load, so instead we often try to measure the instantaneous IBAT (the
665  * current out from the battery), estimate the Ri and thus calculate the
666  * voltage drop over Ri and compensate like this:
667  *
668  *   OCV = VBAT - (IBAT * Ri)
669  *
670  * The tables vbat2ri_discharging and vbat2ri_charging are used to determine
671  * (by interpolation) the Ri from the VBAT under load. These curves are highly
672  * nonlinear and may need many datapoints but can be found in datasheets for
673  * some batteries. This gives the compensated open circuit voltage (OCV) for
674  * the battery even under load. Using this method will also compensate for
675  * temperature changes in the environment: this will also make the internal
676  * resistance change, and it will affect the VBAT under load, so correlating
677  * VBAT to Ri takes both remaining capacity and temperature into consideration.
678  *
679  * Alternatively a manufacturer can specify how the capacity of the battery
680  * is dependent on the battery temperature which is the main factor affecting
681  * Ri. As we know all checmical reactions are faster when it is warm and slower
682  * when it is cold. You can put in 1500mAh and only get 800mAh out before the
683  * voltage drops too low for example. This effect is also highly nonlinear and
684  * the purpose of the table resist_table: this will take a temperature and
685  * tell us how big percentage of Ri the specified temperature correlates to.
686  * Usually we have 100% of the factory_internal_resistance_uohm at 25 degrees
687  * Celsius.
688  *
689  * The power supply class itself doesn't use this struct as of now.
690  */
691 
692 struct power_supply_battery_info {
693 	unsigned int technology;
694 	int energy_full_design_uwh;
695 	int charge_full_design_uah;
696 	int voltage_min_design_uv;
697 	int voltage_max_design_uv;
698 	int tricklecharge_current_ua;
699 	int precharge_current_ua;
700 	int precharge_voltage_max_uv;
701 	int charge_term_current_ua;
702 	int charge_restart_voltage_uv;
703 	int overvoltage_limit_uv;
704 	int constant_charge_current_max_ua;
705 	int constant_charge_voltage_max_uv;
706 	struct power_supply_maintenance_charge_table *maintenance_charge;
707 	int maintenance_charge_size;
708 	int alert_low_temp_charge_current_ua;
709 	int alert_low_temp_charge_voltage_uv;
710 	int alert_high_temp_charge_current_ua;
711 	int alert_high_temp_charge_voltage_uv;
712 	int factory_internal_resistance_uohm;
713 	int factory_internal_resistance_charging_uohm;
714 	int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];
715 	int temp_ambient_alert_min;
716 	int temp_ambient_alert_max;
717 	int temp_alert_min;
718 	int temp_alert_max;
719 	int temp_min;
720 	int temp_max;
721 	struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX];
722 	int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX];
723 	struct power_supply_resistance_temp_table *resist_table;
724 	int resist_table_size;
725 	struct power_supply_vbat_ri_table *vbat2ri_discharging;
726 	int vbat2ri_discharging_size;
727 	struct power_supply_vbat_ri_table *vbat2ri_charging;
728 	int vbat2ri_charging_size;
729 	int bti_resistance_ohm;
730 	int bti_resistance_tolerance;
731 };
732 
733 extern struct atomic_notifier_head power_supply_notifier;
734 extern int power_supply_reg_notifier(struct notifier_block *nb);
735 extern void power_supply_unreg_notifier(struct notifier_block *nb);
736 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
737 extern struct power_supply *power_supply_get_by_name(const char *name);
738 extern void power_supply_put(struct power_supply *psy);
739 #else
power_supply_put(struct power_supply * psy)740 static inline void power_supply_put(struct power_supply *psy) {}
power_supply_get_by_name(const char * name)741 static inline struct power_supply *power_supply_get_by_name(const char *name)
742 { return NULL; }
743 #endif
744 #ifdef CONFIG_OF
745 extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
746 							const char *property);
747 extern struct power_supply *devm_power_supply_get_by_phandle(
748 				    struct device *dev, const char *property);
749 #else /* !CONFIG_OF */
750 static inline struct power_supply *
power_supply_get_by_phandle(struct device_node * np,const char * property)751 power_supply_get_by_phandle(struct device_node *np, const char *property)
752 { return NULL; }
753 static inline struct power_supply *
devm_power_supply_get_by_phandle(struct device * dev,const char * property)754 devm_power_supply_get_by_phandle(struct device *dev, const char *property)
755 { return NULL; }
756 #endif /* CONFIG_OF */
757 
758 extern int power_supply_get_battery_info(struct power_supply *psy,
759 					 struct power_supply_battery_info **info_out);
760 extern void power_supply_put_battery_info(struct power_supply *psy,
761 					  struct power_supply_battery_info *info);
762 extern int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
763 				       int table_len, int ocv);
764 extern struct power_supply_battery_ocv_table *
765 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
766 				int temp, int *table_len);
767 extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
768 					int ocv, int temp);
769 extern int
770 power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
771 				int table_len, int temp);
772 extern int power_supply_vbat2ri(struct power_supply_battery_info *info,
773 				int vbat_uv, bool charging);
774 extern struct power_supply_maintenance_charge_table *
775 power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index);
776 extern bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
777 					      int resistance);
778 extern void power_supply_changed(struct power_supply *psy);
779 extern int power_supply_am_i_supplied(struct power_supply *psy);
780 int power_supply_get_property_from_supplier(struct power_supply *psy,
781 					    enum power_supply_property psp,
782 					    union power_supply_propval *val);
783 extern int power_supply_set_battery_charged(struct power_supply *psy);
784 
785 static inline bool
power_supply_supports_maintenance_charging(struct power_supply_battery_info * info)786 power_supply_supports_maintenance_charging(struct power_supply_battery_info *info)
787 {
788 	struct power_supply_maintenance_charge_table *mt;
789 
790 	mt = power_supply_get_maintenance_charging_setting(info, 0);
791 
792 	return (mt != NULL);
793 }
794 
795 static inline bool
power_supply_supports_vbat2ri(struct power_supply_battery_info * info)796 power_supply_supports_vbat2ri(struct power_supply_battery_info *info)
797 {
798 	return ((info->vbat2ri_discharging != NULL) &&
799 		info->vbat2ri_discharging_size > 0);
800 }
801 
802 static inline bool
power_supply_supports_temp2ri(struct power_supply_battery_info * info)803 power_supply_supports_temp2ri(struct power_supply_battery_info *info)
804 {
805 	return ((info->resist_table != NULL) &&
806 		info->resist_table_size > 0);
807 }
808 
809 #ifdef CONFIG_POWER_SUPPLY
810 extern int power_supply_is_system_supplied(void);
811 #else
power_supply_is_system_supplied(void)812 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
813 #endif
814 
815 extern int power_supply_get_property(struct power_supply *psy,
816 			    enum power_supply_property psp,
817 			    union power_supply_propval *val);
818 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
819 extern int power_supply_set_property(struct power_supply *psy,
820 			    enum power_supply_property psp,
821 			    const union power_supply_propval *val);
822 #else
power_supply_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)823 static inline int power_supply_set_property(struct power_supply *psy,
824 			    enum power_supply_property psp,
825 			    const union power_supply_propval *val)
826 { return 0; }
827 #endif
828 extern int power_supply_property_is_writeable(struct power_supply *psy,
829 					enum power_supply_property psp);
830 extern void power_supply_external_power_changed(struct power_supply *psy);
831 
832 extern struct power_supply *__must_check
833 power_supply_register(struct device *parent,
834 				 const struct power_supply_desc *desc,
835 				 const struct power_supply_config *cfg);
836 extern struct power_supply *__must_check
837 power_supply_register_no_ws(struct device *parent,
838 				 const struct power_supply_desc *desc,
839 				 const struct power_supply_config *cfg);
840 extern struct power_supply *__must_check
841 devm_power_supply_register(struct device *parent,
842 				 const struct power_supply_desc *desc,
843 				 const struct power_supply_config *cfg);
844 extern struct power_supply *__must_check
845 devm_power_supply_register_no_ws(struct device *parent,
846 				 const struct power_supply_desc *desc,
847 				 const struct power_supply_config *cfg);
848 extern void power_supply_unregister(struct power_supply *psy);
849 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
850 
851 #define to_power_supply(device) container_of(device, struct power_supply, dev)
852 
853 extern void *power_supply_get_drvdata(struct power_supply *psy);
854 /* For APM emulation, think legacy userspace. */
855 extern struct class *power_supply_class;
856 
power_supply_is_amp_property(enum power_supply_property psp)857 static inline bool power_supply_is_amp_property(enum power_supply_property psp)
858 {
859 	switch (psp) {
860 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
861 	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
862 	case POWER_SUPPLY_PROP_CHARGE_FULL:
863 	case POWER_SUPPLY_PROP_CHARGE_EMPTY:
864 	case POWER_SUPPLY_PROP_CHARGE_NOW:
865 	case POWER_SUPPLY_PROP_CHARGE_AVG:
866 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
867 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
868 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
869 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
870 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
871 	case POWER_SUPPLY_PROP_CURRENT_MAX:
872 	case POWER_SUPPLY_PROP_CURRENT_NOW:
873 	case POWER_SUPPLY_PROP_CURRENT_AVG:
874 	case POWER_SUPPLY_PROP_CURRENT_BOOT:
875 		return true;
876 	default:
877 		break;
878 	}
879 
880 	return false;
881 }
882 
power_supply_is_watt_property(enum power_supply_property psp)883 static inline bool power_supply_is_watt_property(enum power_supply_property psp)
884 {
885 	switch (psp) {
886 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
887 	case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
888 	case POWER_SUPPLY_PROP_ENERGY_FULL:
889 	case POWER_SUPPLY_PROP_ENERGY_EMPTY:
890 	case POWER_SUPPLY_PROP_ENERGY_NOW:
891 	case POWER_SUPPLY_PROP_ENERGY_AVG:
892 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
893 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
894 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
895 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
896 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
897 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
898 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
899 	case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
900 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
901 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
902 	case POWER_SUPPLY_PROP_POWER_NOW:
903 		return true;
904 	default:
905 		break;
906 	}
907 
908 	return false;
909 }
910 
911 #ifdef CONFIG_POWER_SUPPLY_HWMON
912 int power_supply_add_hwmon_sysfs(struct power_supply *psy);
913 void power_supply_remove_hwmon_sysfs(struct power_supply *psy);
914 #else
power_supply_add_hwmon_sysfs(struct power_supply * psy)915 static inline int power_supply_add_hwmon_sysfs(struct power_supply *psy)
916 {
917 	return 0;
918 }
919 
920 static inline
power_supply_remove_hwmon_sysfs(struct power_supply * psy)921 void power_supply_remove_hwmon_sysfs(struct power_supply *psy) {}
922 #endif
923 
924 #ifdef CONFIG_SYSFS
925 ssize_t power_supply_charge_behaviour_show(struct device *dev,
926 					   unsigned int available_behaviours,
927 					   enum power_supply_charge_behaviour behaviour,
928 					   char *buf);
929 
930 int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf);
931 #else
932 static inline
power_supply_charge_behaviour_show(struct device * dev,unsigned int available_behaviours,enum power_supply_charge_behaviour behaviour,char * buf)933 ssize_t power_supply_charge_behaviour_show(struct device *dev,
934 					   unsigned int available_behaviours,
935 					   enum power_supply_charge_behaviour behaviour,
936 					   char *buf)
937 {
938 	return -EOPNOTSUPP;
939 }
940 
power_supply_charge_behaviour_parse(unsigned int available_behaviours,const char * buf)941 static inline int power_supply_charge_behaviour_parse(unsigned int available_behaviours,
942 						      const char *buf)
943 {
944 	return -EOPNOTSUPP;
945 }
946 #endif
947 
948 #endif /* __LINUX_POWER_SUPPLY_H__ */
949