xref: /linux/include/linux/power_supply.h (revision 448ecd5771e255629bef0fb16c9b78c4bbd7bd56)
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 	bool no_wakeup_source;
241 };
242 
243 /* Description of power supply */
244 struct power_supply_desc {
245 	const char *name;
246 	enum power_supply_type type;
247 	u8 charge_behaviours;
248 	u32 usb_types;
249 	const enum power_supply_property *properties;
250 	size_t num_properties;
251 
252 	/*
253 	 * Functions for drivers implementing power supply class.
254 	 * These shouldn't be called directly by other drivers for accessing
255 	 * this power supply. Instead use power_supply_*() functions (for
256 	 * example power_supply_get_property()).
257 	 */
258 	int (*get_property)(struct power_supply *psy,
259 			    enum power_supply_property psp,
260 			    union power_supply_propval *val);
261 	int (*set_property)(struct power_supply *psy,
262 			    enum power_supply_property psp,
263 			    const union power_supply_propval *val);
264 	/*
265 	 * property_is_writeable() will be called during registration
266 	 * of power supply. If this happens during device probe then it must
267 	 * not access internal data of device (because probe did not end).
268 	 */
269 	int (*property_is_writeable)(struct power_supply *psy,
270 				     enum power_supply_property psp);
271 	void (*external_power_changed)(struct power_supply *psy);
272 	void (*set_charged)(struct power_supply *psy);
273 
274 	/*
275 	 * Set if thermal zone should not be created for this power supply.
276 	 * For example for virtual supplies forwarding calls to actual
277 	 * sensors or other supplies.
278 	 */
279 	bool no_thermal;
280 	/* For APM emulation, think legacy userspace. */
281 	int use_for_apm;
282 };
283 
284 struct power_supply {
285 	const struct power_supply_desc *desc;
286 
287 	char **supplied_to;
288 	size_t num_supplicants;
289 
290 	char **supplied_from;
291 	size_t num_supplies;
292 	struct device_node *of_node;
293 
294 	/* Driver private data */
295 	void *drv_data;
296 
297 	/* private */
298 	struct device dev;
299 	struct work_struct changed_work;
300 	struct delayed_work deferred_register_work;
301 	spinlock_t changed_lock;
302 	bool changed;
303 	bool initialized;
304 	bool removing;
305 	atomic_t use_cnt;
306 	struct power_supply_battery_info *battery_info;
307 #ifdef CONFIG_THERMAL
308 	struct thermal_zone_device *tzd;
309 	struct thermal_cooling_device *tcd;
310 #endif
311 
312 #ifdef CONFIG_LEDS_TRIGGERS
313 	struct led_trigger *trig;
314 	struct led_trigger *charging_trig;
315 	struct led_trigger *full_trig;
316 	struct led_trigger *charging_blink_full_solid_trig;
317 	struct led_trigger *charging_orange_full_green_trig;
318 #endif
319 };
320 
321 /*
322  * This is recommended structure to specify static power supply parameters.
323  * Generic one, parametrizable for different power supplies. Power supply
324  * class itself does not use it, but that's what implementing most platform
325  * drivers, should try reuse for consistency.
326  */
327 
328 struct power_supply_info {
329 	const char *name;
330 	int technology;
331 	int voltage_max_design;
332 	int voltage_min_design;
333 	int charge_full_design;
334 	int charge_empty_design;
335 	int energy_full_design;
336 	int energy_empty_design;
337 	int use_for_apm;
338 };
339 
340 struct power_supply_battery_ocv_table {
341 	int ocv;	/* microVolts */
342 	int capacity;	/* percent */
343 };
344 
345 struct power_supply_resistance_temp_table {
346 	int temp;	/* celsius */
347 	int resistance;	/* internal resistance percent */
348 };
349 
350 struct power_supply_vbat_ri_table {
351 	int vbat_uv;	/* Battery voltage in microvolt */
352 	int ri_uohm;	/* Internal resistance in microohm */
353 };
354 
355 /**
356  * struct power_supply_maintenance_charge_table - setting for maintenace charging
357  * @charge_current_max_ua: maintenance charging current that is used to keep
358  *   the charge of the battery full as current is consumed after full charging.
359  *   The corresponding charge_voltage_max_uv is used as a safeguard: when we
360  *   reach this voltage the maintenance charging current is turned off. It is
361  *   turned back on if we fall below this voltage.
362  * @charge_voltage_max_uv: maintenance charging voltage that is usually a bit
363  *   lower than the constant_charge_voltage_max_uv. We can apply this settings
364  *   charge_current_max_ua until we get back up to this voltage.
365  * @safety_timer_minutes: maintenance charging safety timer, with an expiry
366  *   time in minutes. We will only use maintenance charging in this setting
367  *   for a certain amount of time, then we will first move to the next
368  *   maintenance charge current and voltage pair in respective array and wait
369  *   for the next safety timer timeout, or, if we reached the last maintencance
370  *   charging setting, disable charging until we reach
371  *   charge_restart_voltage_uv and restart ordinary CC/CV charging from there.
372  *   These timers should be chosen to align with the typical discharge curve
373  *   for the battery.
374  *
375  * Ordinary CC/CV charging will stop charging when the charge current goes
376  * below charge_term_current_ua, and then restart it (if the device is still
377  * plugged into the charger) at charge_restart_voltage_uv. This happens in most
378  * consumer products because the power usage while connected to a charger is
379  * not zero, and devices are not manufactured to draw power directly from the
380  * charger: instead they will at all times dissipate the battery a little, like
381  * the power used in standby mode. This will over time give a charge graph
382  * such as this:
383  *
384  * Energy
385  *  ^      ...        ...      ...      ...      ...      ...      ...
386  *  |    .   .       .  .     .  .     .  .     .  .     .  .     .
387  *  |  ..     .   ..     .  ..    .  ..    .  ..    .  ..    .  ..
388  *  |.          ..        ..       ..       ..       ..       ..
389  *  +-------------------------------------------------------------------> t
390  *
391  * Practically this means that the Li-ions are wandering back and forth in the
392  * battery and this causes degeneration of the battery anode and cathode.
393  * To prolong the life of the battery, maintenance charging is applied after
394  * reaching charge_term_current_ua to hold up the charge in the battery while
395  * consuming power, thus lowering the wear on the battery:
396  *
397  * Energy
398  *  ^      .......................................
399  *  |    .                                        ......................
400  *  |  ..
401  *  |.
402  *  +-------------------------------------------------------------------> t
403  *
404  * Maintenance charging uses the voltages from this table: a table of settings
405  * is traversed using a slightly lower current and voltage than what is used for
406  * CC/CV charging. The maintenance charging will for safety reasons not go on
407  * indefinately: we lower the current and voltage with successive maintenance
408  * settings, then disable charging completely after we reach the last one,
409  * and after that we do not restart charging until we reach
410  * charge_restart_voltage_uv (see struct power_supply_battery_info) and restart
411  * ordinary CC/CV charging from there.
412  *
413  * As an example, a Samsung EB425161LA Lithium-Ion battery is CC/CV charged
414  * at 900mA to 4340mV, then maintenance charged at 600mA and 4150mV for up to
415  * 60 hours, then maintenance charged at 600mA and 4100mV for up to 200 hours.
416  * After this the charge cycle is restarted waiting for
417  * charge_restart_voltage_uv.
418  *
419  * For most mobile electronics this type of maintenance charging is enough for
420  * the user to disconnect the device and make use of it before both maintenance
421  * charging cycles are complete, if the current and voltage has been chosen
422  * appropriately. These need to be determined from battery discharge curves
423  * and expected standby current.
424  *
425  * If the voltage anyway drops to charge_restart_voltage_uv during maintenance
426  * charging, ordinary CC/CV charging is restarted. This can happen if the
427  * device is e.g. actively used during charging, so more current is drawn than
428  * the expected stand-by current. Also overvoltage protection will be applied
429  * as usual.
430  */
431 struct power_supply_maintenance_charge_table {
432 	int charge_current_max_ua;
433 	int charge_voltage_max_uv;
434 	int charge_safety_timer_minutes;
435 };
436 
437 #define POWER_SUPPLY_OCV_TEMP_MAX 20
438 
439 /**
440  * struct power_supply_battery_info - information about batteries
441  * @technology: from the POWER_SUPPLY_TECHNOLOGY_* enum
442  * @energy_full_design_uwh: energy content when fully charged in microwatt
443  *   hours
444  * @charge_full_design_uah: charge content when fully charged in microampere
445  *   hours
446  * @voltage_min_design_uv: minimum voltage across the poles when the battery
447  *   is at minimum voltage level in microvolts. If the voltage drops below this
448  *   level the battery will need precharging when using CC/CV charging.
449  * @voltage_max_design_uv: voltage across the poles when the battery is fully
450  *   charged in microvolts. This is the "nominal voltage" i.e. the voltage
451  *   printed on the label of the battery.
452  * @tricklecharge_current_ua: the tricklecharge current used when trickle
453  *   charging the battery in microamperes. This is the charging phase when the
454  *   battery is completely empty and we need to carefully trickle in some
455  *   charge until we reach the precharging voltage.
456  * @precharge_current_ua: current to use in the precharge phase in microamperes,
457  *   the precharge rate is limited by limiting the current to this value.
458  * @precharge_voltage_max_uv: the maximum voltage allowed when precharging in
459  *   microvolts. When we pass this voltage we will nominally switch over to the
460  *   CC (constant current) charging phase defined by constant_charge_current_ua
461  *   and constant_charge_voltage_max_uv.
462  * @charge_term_current_ua: when the current in the CV (constant voltage)
463  *   charging phase drops below this value in microamperes the charging will
464  *   terminate completely and not restart until the voltage over the battery
465  *   poles reach charge_restart_voltage_uv unless we use maintenance charging.
466  * @charge_restart_voltage_uv: when the battery has been fully charged by
467  *   CC/CV charging and charging has been disabled, and the voltage subsequently
468  *   drops below this value in microvolts, the charging will be restarted
469  *   (typically using CV charging).
470  * @overvoltage_limit_uv: If the voltage exceeds the nominal voltage
471  *   voltage_max_design_uv and we reach this voltage level, all charging must
472  *   stop and emergency procedures take place, such as shutting down the system
473  *   in some cases.
474  * @constant_charge_current_max_ua: current in microamperes to use in the CC
475  *   (constant current) charging phase. The charging rate is limited
476  *   by this current. This is the main charging phase and as the current is
477  *   constant into the battery the voltage slowly ascends to
478  *   constant_charge_voltage_max_uv.
479  * @constant_charge_voltage_max_uv: voltage in microvolts signifying the end of
480  *   the CC (constant current) charging phase and the beginning of the CV
481  *   (constant voltage) charging phase.
482  * @maintenance_charge: an array of maintenance charging settings to be used
483  *   after the main CC/CV charging phase is complete.
484  * @maintenance_charge_size: the number of maintenance charging settings in
485  *   maintenance_charge.
486  * @alert_low_temp_charge_current_ua: The charging current to use if the battery
487  *   enters low alert temperature, i.e. if the internal temperature is between
488  *   temp_alert_min and temp_min. No matter the charging phase, this
489  *   and alert_high_temp_charge_voltage_uv will be applied.
490  * @alert_low_temp_charge_voltage_uv: Same as alert_low_temp_charge_current_ua,
491  *   but for the charging voltage.
492  * @alert_high_temp_charge_current_ua: The charging current to use if the
493  *   battery enters high alert temperature, i.e. if the internal temperature is
494  *   between temp_alert_max and temp_max. No matter the charging phase, this
495  *   and alert_high_temp_charge_voltage_uv will be applied, usually lowering
496  *   the charging current as an evasive manouver.
497  * @alert_high_temp_charge_voltage_uv: Same as
498  *   alert_high_temp_charge_current_ua, but for the charging voltage.
499  * @factory_internal_resistance_uohm: the internal resistance of the battery
500  *   at fabrication time, expressed in microohms. This resistance will vary
501  *   depending on the lifetime and charge of the battery, so this is just a
502  *   nominal ballpark figure. This internal resistance is given for the state
503  *   when the battery is discharging.
504  * @factory_internal_resistance_charging_uohm: the internal resistance of the
505  *   battery at fabrication time while charging, expressed in microohms.
506  *   The charging process will affect the internal resistance of the battery
507  *   so this value provides a better resistance under these circumstances.
508  *   This resistance will vary depending on the lifetime and charge of the
509  *   battery, so this is just a nominal ballpark figure.
510  * @ocv_temp: array indicating the open circuit voltage (OCV) capacity
511  *   temperature indices. This is an array of temperatures in degrees Celsius
512  *   indicating which capacity table to use for a certain temperature, since
513  *   the capacity for reasons of chemistry will be different at different
514  *   temperatures. Determining capacity is a multivariate problem and the
515  *   temperature is the first variable we determine.
516  * @temp_ambient_alert_min: the battery will go outside of operating conditions
517  *   when the ambient temperature goes below this temperature in degrees
518  *   Celsius.
519  * @temp_ambient_alert_max: the battery will go outside of operating conditions
520  *   when the ambient temperature goes above this temperature in degrees
521  *   Celsius.
522  * @temp_alert_min: the battery should issue an alert if the internal
523  *   temperature goes below this temperature in degrees Celsius.
524  * @temp_alert_max: the battery should issue an alert if the internal
525  *   temperature goes above this temperature in degrees Celsius.
526  * @temp_min: the battery will go outside of operating conditions when
527  *   the internal temperature goes below this temperature in degrees Celsius.
528  *   Normally this means the system should shut down.
529  * @temp_max: the battery will go outside of operating conditions when
530  *   the internal temperature goes above this temperature in degrees Celsius.
531  *   Normally this means the system should shut down.
532  * @ocv_table: for each entry in ocv_temp there is a corresponding entry in
533  *   ocv_table and a size for each entry in ocv_table_size. These arrays
534  *   determine the capacity in percent in relation to the voltage in microvolts
535  *   at the indexed temperature.
536  * @ocv_table_size: for each entry in ocv_temp this array is giving the size of
537  *   each entry in the array of capacity arrays in ocv_table.
538  * @resist_table: this is a table that correlates a battery temperature to the
539  *   expected internal resistance at this temperature. The resistance is given
540  *   as a percentage of factory_internal_resistance_uohm. Knowing the
541  *   resistance of the battery is usually necessary for calculating the open
542  *   circuit voltage (OCV) that is then used with the ocv_table to calculate
543  *   the capacity of the battery. The resist_table must be ordered descending
544  *   by temperature: highest temperature with lowest resistance first, lowest
545  *   temperature with highest resistance last.
546  * @resist_table_size: the number of items in the resist_table.
547  * @vbat2ri_discharging: this is a table that correlates Battery voltage (VBAT)
548  *   to internal resistance (Ri). The resistance is given in microohm for the
549  *   corresponding voltage in microvolts. The internal resistance is used to
550  *   determine the open circuit voltage so that we can determine the capacity
551  *   of the battery. These voltages to resistance tables apply when the battery
552  *   is discharging. The table must be ordered descending by voltage: highest
553  *   voltage first.
554  * @vbat2ri_discharging_size: the number of items in the vbat2ri_discharging
555  *   table.
556  * @vbat2ri_charging: same function as vbat2ri_discharging but for the state
557  *   when the battery is charging. Being under charge changes the battery's
558  *   internal resistance characteristics so a separate table is needed.*
559  *   The table must be ordered descending by voltage: highest voltage first.
560  * @vbat2ri_charging_size: the number of items in the vbat2ri_charging
561  *   table.
562  * @bti_resistance_ohm: The Battery Type Indicator (BIT) nominal resistance
563  *   in ohms for this battery, if an identification resistor is mounted
564  *   between a third battery terminal and ground. This scheme is used by a lot
565  *   of mobile device batteries.
566  * @bti_resistance_tolerance: The tolerance in percent of the BTI resistance,
567  *   for example 10 for +/- 10%, if the bti_resistance is set to 7000 and the
568  *   tolerance is 10% we will detect a proper battery if the BTI resistance
569  *   is between 6300 and 7700 Ohm.
570  *
571  * This is the recommended struct to manage static battery parameters,
572  * populated by power_supply_get_battery_info(). Most platform drivers should
573  * use these for consistency.
574  *
575  * Its field names must correspond to elements in enum power_supply_property.
576  * The default field value is -EINVAL or NULL for pointers.
577  *
578  * CC/CV CHARGING:
579  *
580  * The charging parameters here assume a CC/CV charging scheme. This method
581  * is most common with Lithium Ion batteries (other methods are possible) and
582  * looks as follows:
583  *
584  * ^ Battery voltage
585  * |                                               --- overvoltage_limit_uv
586  * |
587  * |                    ...................................................
588  * |                 .. constant_charge_voltage_max_uv
589  * |              ..
590  * |             .
591  * |            .
592  * |           .
593  * |          .
594  * |         .
595  * |     .. precharge_voltage_max_uv
596  * |  ..
597  * |. (trickle charging)
598  * +------------------------------------------------------------------> time
599  *
600  * ^ Current into the battery
601  * |
602  * |      ............. constant_charge_current_max_ua
603  * |      .            .
604  * |      .             .
605  * |      .              .
606  * |      .               .
607  * |      .                ..
608  * |      .                  ....
609  * |      .                       .....
610  * |    ... precharge_current_ua       .......  charge_term_current_ua
611  * |    .                                    .
612  * |    .                                    .
613  * |.... tricklecharge_current_ua            .
614  * |                                         .
615  * +-----------------------------------------------------------------> time
616  *
617  * These diagrams are synchronized on time and the voltage and current
618  * follow each other.
619  *
620  * With CC/CV charging commence over time like this for an empty battery:
621  *
622  * 1. When the battery is completely empty it may need to be charged with
623  *    an especially small current so that electrons just "trickle in",
624  *    this is the tricklecharge_current_ua.
625  *
626  * 2. Next a small initial pre-charge current (precharge_current_ua)
627  *    is applied if the voltage is below precharge_voltage_max_uv until we
628  *    reach precharge_voltage_max_uv. CAUTION: in some texts this is referred
629  *    to as "trickle charging" but the use in the Linux kernel is different
630  *    see below!
631  *
632  * 3. Then the main charging current is applied, which is called the constant
633  *    current (CC) phase. A current regulator is set up to allow
634  *    constant_charge_current_max_ua of current to flow into the battery.
635  *    The chemical reaction in the battery will make the voltage go up as
636  *    charge goes into the battery. This current is applied until we reach
637  *    the constant_charge_voltage_max_uv voltage.
638  *
639  * 4. At this voltage we switch over to the constant voltage (CV) phase. This
640  *    means we allow current to go into the battery, but we keep the voltage
641  *    fixed. This current will continue to charge the battery while keeping
642  *    the voltage the same. A chemical reaction in the battery goes on
643  *    storing energy without affecting the voltage. Over time the current
644  *    will slowly drop and when we reach charge_term_current_ua we will
645  *    end the constant voltage phase.
646  *
647  * After this the battery is fully charged, and if we do not support maintenance
648  * charging, the charging will not restart until power dissipation makes the
649  * voltage fall so that we reach charge_restart_voltage_uv and at this point
650  * we restart charging at the appropriate phase, usually this will be inside
651  * the CV phase.
652  *
653  * If we support maintenance charging the voltage is however kept high after
654  * the CV phase with a very low current. This is meant to let the same charge
655  * go in for usage while the charger is still connected, mainly for
656  * dissipation for the power consuming entity while connected to the
657  * charger.
658  *
659  * All charging MUST terminate if the overvoltage_limit_uv is ever reached.
660  * Overcharging Lithium Ion cells can be DANGEROUS and lead to fire or
661  * explosions.
662  *
663  * DETERMINING BATTERY CAPACITY:
664  *
665  * Several members of the struct deal with trying to determine the remaining
666  * capacity in the battery, usually as a percentage of charge. In practice
667  * many chargers uses a so-called fuel gauge or coloumb counter that measure
668  * how much charge goes into the battery and how much goes out (+/- leak
669  * consumption). This does not help if we do not know how much capacity the
670  * battery has to begin with, such as when it is first used or was taken out
671  * and charged in a separate charger. Therefore many capacity algorithms use
672  * the open circuit voltage with a look-up table to determine the rough
673  * capacity of the battery. The open circuit voltage can be conceptualized
674  * with an ideal voltage source (V) in series with an internal resistance (Ri)
675  * like this:
676  *
677  *      +-------> IBAT >----------------+
678  *      |                    ^          |
679  *     [ ] Ri                |          |
680  *      |                    | VBAT     |
681  *      o <----------        |          |
682  *     +|           ^        |         [ ] Rload
683  *    .---.         |        |          |
684  *    | V |         | OCV    |          |
685  *    '---'         |        |          |
686  *      |           |        |          |
687  *  GND +-------------------------------+
688  *
689  * If we disconnect the load (here simplified as a fixed resistance Rload)
690  * and measure VBAT with a infinite impedance voltage meter we will get
691  * VBAT = OCV and this assumption is sometimes made even under load, assuming
692  * Rload is insignificant. However this will be of dubious quality because the
693  * load is rarely that small and Ri is strongly nonlinear depending on
694  * temperature and how much capacity is left in the battery due to the
695  * chemistry involved.
696  *
697  * In many practical applications we cannot just disconnect the battery from
698  * the load, so instead we often try to measure the instantaneous IBAT (the
699  * current out from the battery), estimate the Ri and thus calculate the
700  * voltage drop over Ri and compensate like this:
701  *
702  *   OCV = VBAT - (IBAT * Ri)
703  *
704  * The tables vbat2ri_discharging and vbat2ri_charging are used to determine
705  * (by interpolation) the Ri from the VBAT under load. These curves are highly
706  * nonlinear and may need many datapoints but can be found in datasheets for
707  * some batteries. This gives the compensated open circuit voltage (OCV) for
708  * the battery even under load. Using this method will also compensate for
709  * temperature changes in the environment: this will also make the internal
710  * resistance change, and it will affect the VBAT under load, so correlating
711  * VBAT to Ri takes both remaining capacity and temperature into consideration.
712  *
713  * Alternatively a manufacturer can specify how the capacity of the battery
714  * is dependent on the battery temperature which is the main factor affecting
715  * Ri. As we know all checmical reactions are faster when it is warm and slower
716  * when it is cold. You can put in 1500mAh and only get 800mAh out before the
717  * voltage drops too low for example. This effect is also highly nonlinear and
718  * the purpose of the table resist_table: this will take a temperature and
719  * tell us how big percentage of Ri the specified temperature correlates to.
720  * Usually we have 100% of the factory_internal_resistance_uohm at 25 degrees
721  * Celsius.
722  *
723  * The power supply class itself doesn't use this struct as of now.
724  */
725 
726 struct power_supply_battery_info {
727 	unsigned int technology;
728 	int energy_full_design_uwh;
729 	int charge_full_design_uah;
730 	int voltage_min_design_uv;
731 	int voltage_max_design_uv;
732 	int tricklecharge_current_ua;
733 	int precharge_current_ua;
734 	int precharge_voltage_max_uv;
735 	int charge_term_current_ua;
736 	int charge_restart_voltage_uv;
737 	int overvoltage_limit_uv;
738 	int constant_charge_current_max_ua;
739 	int constant_charge_voltage_max_uv;
740 	const struct power_supply_maintenance_charge_table *maintenance_charge;
741 	int maintenance_charge_size;
742 	int alert_low_temp_charge_current_ua;
743 	int alert_low_temp_charge_voltage_uv;
744 	int alert_high_temp_charge_current_ua;
745 	int alert_high_temp_charge_voltage_uv;
746 	int factory_internal_resistance_uohm;
747 	int factory_internal_resistance_charging_uohm;
748 	int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];
749 	int temp_ambient_alert_min;
750 	int temp_ambient_alert_max;
751 	int temp_alert_min;
752 	int temp_alert_max;
753 	int temp_min;
754 	int temp_max;
755 	const struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX];
756 	int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX];
757 	const struct power_supply_resistance_temp_table *resist_table;
758 	int resist_table_size;
759 	const struct power_supply_vbat_ri_table *vbat2ri_discharging;
760 	int vbat2ri_discharging_size;
761 	const struct power_supply_vbat_ri_table *vbat2ri_charging;
762 	int vbat2ri_charging_size;
763 	int bti_resistance_ohm;
764 	int bti_resistance_tolerance;
765 };
766 
767 extern int power_supply_reg_notifier(struct notifier_block *nb);
768 extern void power_supply_unreg_notifier(struct notifier_block *nb);
769 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
770 extern struct power_supply *power_supply_get_by_name(const char *name);
771 extern void power_supply_put(struct power_supply *psy);
772 #else
power_supply_put(struct power_supply * psy)773 static inline void power_supply_put(struct power_supply *psy) {}
power_supply_get_by_name(const char * name)774 static inline struct power_supply *power_supply_get_by_name(const char *name)
775 { return NULL; }
776 #endif
777 #ifdef CONFIG_OF
778 extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
779 							const char *property);
780 extern struct power_supply *devm_power_supply_get_by_phandle(
781 				    struct device *dev, const char *property);
782 #else /* !CONFIG_OF */
783 static inline struct power_supply *
power_supply_get_by_phandle(struct device_node * np,const char * property)784 power_supply_get_by_phandle(struct device_node *np, const char *property)
785 { return NULL; }
786 static inline struct power_supply *
devm_power_supply_get_by_phandle(struct device * dev,const char * property)787 devm_power_supply_get_by_phandle(struct device *dev, const char *property)
788 { return NULL; }
789 #endif /* CONFIG_OF */
790 
791 extern const enum power_supply_property power_supply_battery_info_properties[];
792 extern const size_t power_supply_battery_info_properties_size;
793 extern int power_supply_get_battery_info(struct power_supply *psy,
794 					 struct power_supply_battery_info **info_out);
795 extern void power_supply_put_battery_info(struct power_supply *psy,
796 					  struct power_supply_battery_info *info);
797 extern bool power_supply_battery_info_has_prop(struct power_supply_battery_info *info,
798 					       enum power_supply_property psp);
799 extern int power_supply_battery_info_get_prop(struct power_supply_battery_info *info,
800 					      enum power_supply_property psp,
801 					      union power_supply_propval *val);
802 extern int power_supply_ocv2cap_simple(const struct power_supply_battery_ocv_table *table,
803 				       int table_len, int ocv);
804 extern const struct power_supply_battery_ocv_table *
805 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
806 				int temp, int *table_len);
807 extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
808 					int ocv, int temp);
809 extern int
810 power_supply_temp2resist_simple(const struct power_supply_resistance_temp_table *table,
811 				int table_len, int temp);
812 extern int power_supply_vbat2ri(struct power_supply_battery_info *info,
813 				int vbat_uv, bool charging);
814 extern const struct power_supply_maintenance_charge_table *
815 power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index);
816 extern bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
817 					      int resistance);
818 extern void power_supply_changed(struct power_supply *psy);
819 extern int power_supply_am_i_supplied(struct power_supply *psy);
820 int power_supply_get_property_from_supplier(struct power_supply *psy,
821 					    enum power_supply_property psp,
822 					    union power_supply_propval *val);
823 extern int power_supply_set_battery_charged(struct power_supply *psy);
824 
825 static inline bool
power_supply_supports_maintenance_charging(struct power_supply_battery_info * info)826 power_supply_supports_maintenance_charging(struct power_supply_battery_info *info)
827 {
828 	const struct power_supply_maintenance_charge_table *mt;
829 
830 	mt = power_supply_get_maintenance_charging_setting(info, 0);
831 
832 	return (mt != NULL);
833 }
834 
835 static inline bool
power_supply_supports_vbat2ri(struct power_supply_battery_info * info)836 power_supply_supports_vbat2ri(struct power_supply_battery_info *info)
837 {
838 	return ((info->vbat2ri_discharging != NULL) &&
839 		info->vbat2ri_discharging_size > 0);
840 }
841 
842 static inline bool
power_supply_supports_temp2ri(struct power_supply_battery_info * info)843 power_supply_supports_temp2ri(struct power_supply_battery_info *info)
844 {
845 	return ((info->resist_table != NULL) &&
846 		info->resist_table_size > 0);
847 }
848 
849 #ifdef CONFIG_POWER_SUPPLY
850 extern int power_supply_is_system_supplied(void);
851 #else
power_supply_is_system_supplied(void)852 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
853 #endif
854 
855 extern int power_supply_get_property(struct power_supply *psy,
856 			    enum power_supply_property psp,
857 			    union power_supply_propval *val);
858 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
859 extern int power_supply_set_property(struct power_supply *psy,
860 			    enum power_supply_property psp,
861 			    const union power_supply_propval *val);
862 #else
power_supply_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)863 static inline int power_supply_set_property(struct power_supply *psy,
864 			    enum power_supply_property psp,
865 			    const union power_supply_propval *val)
866 { return 0; }
867 #endif
868 extern void power_supply_external_power_changed(struct power_supply *psy);
869 
870 extern struct power_supply *__must_check
871 power_supply_register(struct device *parent,
872 				 const struct power_supply_desc *desc,
873 				 const struct power_supply_config *cfg);
874 extern struct power_supply *__must_check
875 devm_power_supply_register(struct device *parent,
876 				 const struct power_supply_desc *desc,
877 				 const struct power_supply_config *cfg);
878 extern void power_supply_unregister(struct power_supply *psy);
879 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
880 
881 #define to_power_supply(device) container_of(device, struct power_supply, dev)
882 
883 extern void *power_supply_get_drvdata(struct power_supply *psy);
884 extern int power_supply_for_each_device(void *data, int (*fn)(struct device *dev, void *data));
885 
power_supply_is_amp_property(enum power_supply_property psp)886 static inline bool power_supply_is_amp_property(enum power_supply_property psp)
887 {
888 	switch (psp) {
889 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
890 	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
891 	case POWER_SUPPLY_PROP_CHARGE_FULL:
892 	case POWER_SUPPLY_PROP_CHARGE_EMPTY:
893 	case POWER_SUPPLY_PROP_CHARGE_NOW:
894 	case POWER_SUPPLY_PROP_CHARGE_AVG:
895 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
896 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
897 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
898 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
899 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
900 	case POWER_SUPPLY_PROP_CURRENT_MAX:
901 	case POWER_SUPPLY_PROP_CURRENT_NOW:
902 	case POWER_SUPPLY_PROP_CURRENT_AVG:
903 	case POWER_SUPPLY_PROP_CURRENT_BOOT:
904 		return true;
905 	default:
906 		break;
907 	}
908 
909 	return false;
910 }
911 
power_supply_is_watt_property(enum power_supply_property psp)912 static inline bool power_supply_is_watt_property(enum power_supply_property psp)
913 {
914 	switch (psp) {
915 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
916 	case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
917 	case POWER_SUPPLY_PROP_ENERGY_FULL:
918 	case POWER_SUPPLY_PROP_ENERGY_EMPTY:
919 	case POWER_SUPPLY_PROP_ENERGY_NOW:
920 	case POWER_SUPPLY_PROP_ENERGY_AVG:
921 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
922 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
923 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
924 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
925 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
926 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
927 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
928 	case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
929 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
930 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
931 	case POWER_SUPPLY_PROP_POWER_NOW:
932 		return true;
933 	default:
934 		break;
935 	}
936 
937 	return false;
938 }
939 
940 #ifdef CONFIG_SYSFS
941 ssize_t power_supply_charge_behaviour_show(struct device *dev,
942 					   unsigned int available_behaviours,
943 					   enum power_supply_charge_behaviour behaviour,
944 					   char *buf);
945 
946 int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf);
947 #else
948 static inline
power_supply_charge_behaviour_show(struct device * dev,unsigned int available_behaviours,enum power_supply_charge_behaviour behaviour,char * buf)949 ssize_t power_supply_charge_behaviour_show(struct device *dev,
950 					   unsigned int available_behaviours,
951 					   enum power_supply_charge_behaviour behaviour,
952 					   char *buf)
953 {
954 	return -EOPNOTSUPP;
955 }
956 
power_supply_charge_behaviour_parse(unsigned int available_behaviours,const char * buf)957 static inline int power_supply_charge_behaviour_parse(unsigned int available_behaviours,
958 						      const char *buf)
959 {
960 	return -EOPNOTSUPP;
961 }
962 #endif
963 
964 #endif /* __LINUX_POWER_SUPPLY_H__ */
965