Merge tag 'v5.7-rc1' into locking/kcsan, to resolve conflicts and refresh
[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 };
52
53 enum {
54         POWER_SUPPLY_HEALTH_UNKNOWN = 0,
55         POWER_SUPPLY_HEALTH_GOOD,
56         POWER_SUPPLY_HEALTH_OVERHEAT,
57         POWER_SUPPLY_HEALTH_DEAD,
58         POWER_SUPPLY_HEALTH_OVERVOLTAGE,
59         POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
60         POWER_SUPPLY_HEALTH_COLD,
61         POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE,
62         POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE,
63         POWER_SUPPLY_HEALTH_OVERCURRENT,
64 };
65
66 enum {
67         POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
68         POWER_SUPPLY_TECHNOLOGY_NiMH,
69         POWER_SUPPLY_TECHNOLOGY_LION,
70         POWER_SUPPLY_TECHNOLOGY_LIPO,
71         POWER_SUPPLY_TECHNOLOGY_LiFe,
72         POWER_SUPPLY_TECHNOLOGY_NiCd,
73         POWER_SUPPLY_TECHNOLOGY_LiMn,
74 };
75
76 enum {
77         POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
78         POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
79         POWER_SUPPLY_CAPACITY_LEVEL_LOW,
80         POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
81         POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
82         POWER_SUPPLY_CAPACITY_LEVEL_FULL,
83 };
84
85 enum {
86         POWER_SUPPLY_SCOPE_UNKNOWN = 0,
87         POWER_SUPPLY_SCOPE_SYSTEM,
88         POWER_SUPPLY_SCOPE_DEVICE,
89 };
90
91 enum power_supply_property {
92         /* Properties of type `int' */
93         POWER_SUPPLY_PROP_STATUS = 0,
94         POWER_SUPPLY_PROP_CHARGE_TYPE,
95         POWER_SUPPLY_PROP_HEALTH,
96         POWER_SUPPLY_PROP_PRESENT,
97         POWER_SUPPLY_PROP_ONLINE,
98         POWER_SUPPLY_PROP_AUTHENTIC,
99         POWER_SUPPLY_PROP_TECHNOLOGY,
100         POWER_SUPPLY_PROP_CYCLE_COUNT,
101         POWER_SUPPLY_PROP_VOLTAGE_MAX,
102         POWER_SUPPLY_PROP_VOLTAGE_MIN,
103         POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
104         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
105         POWER_SUPPLY_PROP_VOLTAGE_NOW,
106         POWER_SUPPLY_PROP_VOLTAGE_AVG,
107         POWER_SUPPLY_PROP_VOLTAGE_OCV,
108         POWER_SUPPLY_PROP_VOLTAGE_BOOT,
109         POWER_SUPPLY_PROP_CURRENT_MAX,
110         POWER_SUPPLY_PROP_CURRENT_NOW,
111         POWER_SUPPLY_PROP_CURRENT_AVG,
112         POWER_SUPPLY_PROP_CURRENT_BOOT,
113         POWER_SUPPLY_PROP_POWER_NOW,
114         POWER_SUPPLY_PROP_POWER_AVG,
115         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
116         POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
117         POWER_SUPPLY_PROP_CHARGE_FULL,
118         POWER_SUPPLY_PROP_CHARGE_EMPTY,
119         POWER_SUPPLY_PROP_CHARGE_NOW,
120         POWER_SUPPLY_PROP_CHARGE_AVG,
121         POWER_SUPPLY_PROP_CHARGE_COUNTER,
122         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
123         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
124         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
125         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
126         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
127         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
128         POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, /* in percents! */
129         POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, /* in percents! */
130         POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
131         POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
132         POWER_SUPPLY_PROP_INPUT_POWER_LIMIT,
133         POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
134         POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
135         POWER_SUPPLY_PROP_ENERGY_FULL,
136         POWER_SUPPLY_PROP_ENERGY_EMPTY,
137         POWER_SUPPLY_PROP_ENERGY_NOW,
138         POWER_SUPPLY_PROP_ENERGY_AVG,
139         POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
140         POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
141         POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
142         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
143         POWER_SUPPLY_PROP_TEMP,
144         POWER_SUPPLY_PROP_TEMP_MAX,
145         POWER_SUPPLY_PROP_TEMP_MIN,
146         POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
147         POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
148         POWER_SUPPLY_PROP_TEMP_AMBIENT,
149         POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
150         POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
151         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
152         POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
153         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
154         POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
155         POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
156         POWER_SUPPLY_PROP_USB_TYPE,
157         POWER_SUPPLY_PROP_SCOPE,
158         POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
159         POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
160         POWER_SUPPLY_PROP_CALIBRATE,
161         /* Properties of type `const char *' */
162         POWER_SUPPLY_PROP_MODEL_NAME,
163         POWER_SUPPLY_PROP_MANUFACTURER,
164         POWER_SUPPLY_PROP_SERIAL_NUMBER,
165 };
166
167 enum power_supply_type {
168         POWER_SUPPLY_TYPE_UNKNOWN = 0,
169         POWER_SUPPLY_TYPE_BATTERY,
170         POWER_SUPPLY_TYPE_UPS,
171         POWER_SUPPLY_TYPE_MAINS,
172         POWER_SUPPLY_TYPE_USB,                  /* Standard Downstream Port */
173         POWER_SUPPLY_TYPE_USB_DCP,              /* Dedicated Charging Port */
174         POWER_SUPPLY_TYPE_USB_CDP,              /* Charging Downstream Port */
175         POWER_SUPPLY_TYPE_USB_ACA,              /* Accessory Charger Adapters */
176         POWER_SUPPLY_TYPE_USB_TYPE_C,           /* Type C Port */
177         POWER_SUPPLY_TYPE_USB_PD,               /* Power Delivery Port */
178         POWER_SUPPLY_TYPE_USB_PD_DRP,           /* PD Dual Role Port */
179         POWER_SUPPLY_TYPE_APPLE_BRICK_ID,       /* Apple Charging Method */
180 };
181
182 enum power_supply_usb_type {
183         POWER_SUPPLY_USB_TYPE_UNKNOWN = 0,
184         POWER_SUPPLY_USB_TYPE_SDP,              /* Standard Downstream Port */
185         POWER_SUPPLY_USB_TYPE_DCP,              /* Dedicated Charging Port */
186         POWER_SUPPLY_USB_TYPE_CDP,              /* Charging Downstream Port */
187         POWER_SUPPLY_USB_TYPE_ACA,              /* Accessory Charger Adapters */
188         POWER_SUPPLY_USB_TYPE_C,                /* Type C Port */
189         POWER_SUPPLY_USB_TYPE_PD,               /* Power Delivery Port */
190         POWER_SUPPLY_USB_TYPE_PD_DRP,           /* PD Dual Role Port */
191         POWER_SUPPLY_USB_TYPE_PD_PPS,           /* PD Programmable Power Supply */
192         POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID,   /* Apple Charging Method */
193 };
194
195 enum power_supply_notifier_events {
196         PSY_EVENT_PROP_CHANGED,
197 };
198
199 union power_supply_propval {
200         int intval;
201         const char *strval;
202 };
203
204 struct device_node;
205 struct power_supply;
206
207 /* Run-time specific power supply configuration */
208 struct power_supply_config {
209         struct device_node *of_node;
210         struct fwnode_handle *fwnode;
211
212         /* Driver private data */
213         void *drv_data;
214
215         /* Device specific sysfs attributes */
216         const struct attribute_group **attr_grp;
217
218         char **supplied_to;
219         size_t num_supplicants;
220 };
221
222 /* Description of power supply */
223 struct power_supply_desc {
224         const char *name;
225         enum power_supply_type type;
226         enum power_supply_usb_type *usb_types;
227         size_t num_usb_types;
228         enum power_supply_property *properties;
229         size_t num_properties;
230
231         /*
232          * Functions for drivers implementing power supply class.
233          * These shouldn't be called directly by other drivers for accessing
234          * this power supply. Instead use power_supply_*() functions (for
235          * example power_supply_get_property()).
236          */
237         int (*get_property)(struct power_supply *psy,
238                             enum power_supply_property psp,
239                             union power_supply_propval *val);
240         int (*set_property)(struct power_supply *psy,
241                             enum power_supply_property psp,
242                             const union power_supply_propval *val);
243         /*
244          * property_is_writeable() will be called during registration
245          * of power supply. If this happens during device probe then it must
246          * not access internal data of device (because probe did not end).
247          */
248         int (*property_is_writeable)(struct power_supply *psy,
249                                      enum power_supply_property psp);
250         void (*external_power_changed)(struct power_supply *psy);
251         void (*set_charged)(struct power_supply *psy);
252
253         /*
254          * Set if thermal zone should not be created for this power supply.
255          * For example for virtual supplies forwarding calls to actual
256          * sensors or other supplies.
257          */
258         bool no_thermal;
259         /* For APM emulation, think legacy userspace. */
260         int use_for_apm;
261 };
262
263 struct power_supply {
264         const struct power_supply_desc *desc;
265
266         char **supplied_to;
267         size_t num_supplicants;
268
269         char **supplied_from;
270         size_t num_supplies;
271         struct device_node *of_node;
272
273         /* Driver private data */
274         void *drv_data;
275
276         /* private */
277         struct device dev;
278         struct work_struct changed_work;
279         struct delayed_work deferred_register_work;
280         spinlock_t changed_lock;
281         bool changed;
282         bool initialized;
283         bool removing;
284         atomic_t use_cnt;
285 #ifdef CONFIG_THERMAL
286         struct thermal_zone_device *tzd;
287         struct thermal_cooling_device *tcd;
288 #endif
289
290 #ifdef CONFIG_LEDS_TRIGGERS
291         struct led_trigger *charging_full_trig;
292         char *charging_full_trig_name;
293         struct led_trigger *charging_trig;
294         char *charging_trig_name;
295         struct led_trigger *full_trig;
296         char *full_trig_name;
297         struct led_trigger *online_trig;
298         char *online_trig_name;
299         struct led_trigger *charging_blink_full_solid_trig;
300         char *charging_blink_full_solid_trig_name;
301 #endif
302 };
303
304 /*
305  * This is recommended structure to specify static power supply parameters.
306  * Generic one, parametrizable for different power supplies. Power supply
307  * class itself does not use it, but that's what implementing most platform
308  * drivers, should try reuse for consistency.
309  */
310
311 struct power_supply_info {
312         const char *name;
313         int technology;
314         int voltage_max_design;
315         int voltage_min_design;
316         int charge_full_design;
317         int charge_empty_design;
318         int energy_full_design;
319         int energy_empty_design;
320         int use_for_apm;
321 };
322
323 struct power_supply_battery_ocv_table {
324         int ocv;        /* microVolts */
325         int capacity;   /* percent */
326 };
327
328 struct power_supply_resistance_temp_table {
329         int temp;       /* celsius */
330         int resistance; /* internal resistance percent */
331 };
332
333 #define POWER_SUPPLY_OCV_TEMP_MAX 20
334
335 /*
336  * This is the recommended struct to manage static battery parameters,
337  * populated by power_supply_get_battery_info(). Most platform drivers should
338  * use these for consistency.
339  * Its field names must correspond to elements in enum power_supply_property.
340  * The default field value is -EINVAL.
341  * Power supply class itself doesn't use this.
342  */
343
344 struct power_supply_battery_info {
345         int energy_full_design_uwh;         /* microWatt-hours */
346         int charge_full_design_uah;         /* microAmp-hours */
347         int voltage_min_design_uv;          /* microVolts */
348         int voltage_max_design_uv;          /* microVolts */
349         int precharge_current_ua;           /* microAmps */
350         int charge_term_current_ua;         /* microAmps */
351         int constant_charge_current_max_ua; /* microAmps */
352         int constant_charge_voltage_max_uv; /* microVolts */
353         int factory_internal_resistance_uohm;   /* microOhms */
354         int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];/* celsius */
355         struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX];
356         int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX];
357         struct power_supply_resistance_temp_table *resist_table;
358         int resist_table_size;
359 };
360
361 extern struct atomic_notifier_head power_supply_notifier;
362 extern int power_supply_reg_notifier(struct notifier_block *nb);
363 extern void power_supply_unreg_notifier(struct notifier_block *nb);
364 extern struct power_supply *power_supply_get_by_name(const char *name);
365 extern void power_supply_put(struct power_supply *psy);
366 #ifdef CONFIG_OF
367 extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
368                                                         const char *property);
369 extern struct power_supply *devm_power_supply_get_by_phandle(
370                                     struct device *dev, const char *property);
371 #else /* !CONFIG_OF */
372 static inline struct power_supply *
373 power_supply_get_by_phandle(struct device_node *np, const char *property)
374 { return NULL; }
375 static inline struct power_supply *
376 devm_power_supply_get_by_phandle(struct device *dev, const char *property)
377 { return NULL; }
378 #endif /* CONFIG_OF */
379
380 extern int power_supply_get_battery_info(struct power_supply *psy,
381                                          struct power_supply_battery_info *info);
382 extern void power_supply_put_battery_info(struct power_supply *psy,
383                                           struct power_supply_battery_info *info);
384 extern int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
385                                        int table_len, int ocv);
386 extern struct power_supply_battery_ocv_table *
387 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
388                                 int temp, int *table_len);
389 extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
390                                         int ocv, int temp);
391 extern int
392 power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
393                                 int table_len, int temp);
394 extern void power_supply_changed(struct power_supply *psy);
395 extern int power_supply_am_i_supplied(struct power_supply *psy);
396 extern int power_supply_set_input_current_limit_from_supplier(
397                                          struct power_supply *psy);
398 extern int power_supply_set_battery_charged(struct power_supply *psy);
399
400 #ifdef CONFIG_POWER_SUPPLY
401 extern int power_supply_is_system_supplied(void);
402 #else
403 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
404 #endif
405
406 extern int power_supply_get_property(struct power_supply *psy,
407                             enum power_supply_property psp,
408                             union power_supply_propval *val);
409 extern int power_supply_set_property(struct power_supply *psy,
410                             enum power_supply_property psp,
411                             const union power_supply_propval *val);
412 extern int power_supply_property_is_writeable(struct power_supply *psy,
413                                         enum power_supply_property psp);
414 extern void power_supply_external_power_changed(struct power_supply *psy);
415
416 extern struct power_supply *__must_check
417 power_supply_register(struct device *parent,
418                                  const struct power_supply_desc *desc,
419                                  const struct power_supply_config *cfg);
420 extern struct power_supply *__must_check
421 power_supply_register_no_ws(struct device *parent,
422                                  const struct power_supply_desc *desc,
423                                  const struct power_supply_config *cfg);
424 extern struct power_supply *__must_check
425 devm_power_supply_register(struct device *parent,
426                                  const struct power_supply_desc *desc,
427                                  const struct power_supply_config *cfg);
428 extern struct power_supply *__must_check
429 devm_power_supply_register_no_ws(struct device *parent,
430                                  const struct power_supply_desc *desc,
431                                  const struct power_supply_config *cfg);
432 extern void power_supply_unregister(struct power_supply *psy);
433 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
434
435 #define to_power_supply(device) container_of(device, struct power_supply, dev)
436
437 extern void *power_supply_get_drvdata(struct power_supply *psy);
438 /* For APM emulation, think legacy userspace. */
439 extern struct class *power_supply_class;
440
441 static inline bool power_supply_is_amp_property(enum power_supply_property psp)
442 {
443         switch (psp) {
444         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
445         case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
446         case POWER_SUPPLY_PROP_CHARGE_FULL:
447         case POWER_SUPPLY_PROP_CHARGE_EMPTY:
448         case POWER_SUPPLY_PROP_CHARGE_NOW:
449         case POWER_SUPPLY_PROP_CHARGE_AVG:
450         case POWER_SUPPLY_PROP_CHARGE_COUNTER:
451         case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
452         case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
453         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
454         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
455         case POWER_SUPPLY_PROP_CURRENT_MAX:
456         case POWER_SUPPLY_PROP_CURRENT_NOW:
457         case POWER_SUPPLY_PROP_CURRENT_AVG:
458         case POWER_SUPPLY_PROP_CURRENT_BOOT:
459                 return 1;
460         default:
461                 break;
462         }
463
464         return 0;
465 }
466
467 static inline bool power_supply_is_watt_property(enum power_supply_property psp)
468 {
469         switch (psp) {
470         case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
471         case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
472         case POWER_SUPPLY_PROP_ENERGY_FULL:
473         case POWER_SUPPLY_PROP_ENERGY_EMPTY:
474         case POWER_SUPPLY_PROP_ENERGY_NOW:
475         case POWER_SUPPLY_PROP_ENERGY_AVG:
476         case POWER_SUPPLY_PROP_VOLTAGE_MAX:
477         case POWER_SUPPLY_PROP_VOLTAGE_MIN:
478         case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
479         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
480         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
481         case POWER_SUPPLY_PROP_VOLTAGE_AVG:
482         case POWER_SUPPLY_PROP_VOLTAGE_OCV:
483         case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
484         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
485         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
486         case POWER_SUPPLY_PROP_POWER_NOW:
487                 return 1;
488         default:
489                 break;
490         }
491
492         return 0;
493 }
494
495 #ifdef CONFIG_POWER_SUPPLY_HWMON
496 int power_supply_add_hwmon_sysfs(struct power_supply *psy);
497 void power_supply_remove_hwmon_sysfs(struct power_supply *psy);
498 #else
499 static inline int power_supply_add_hwmon_sysfs(struct power_supply *psy)
500 {
501         return 0;
502 }
503
504 static inline
505 void power_supply_remove_hwmon_sysfs(struct power_supply *psy) {}
506 #endif
507
508 #endif /* __LINUX_POWER_SUPPLY_H__ */