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