1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Universal power supply monitor class
5 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
6 * Copyright © 2004 Szabolcs Gyurko
7 * Copyright © 2003 Ian Molton <spyro@f2s.com>
9 * Modified: 2004, Oct Szabolcs Gyurko
12 #ifndef __LINUX_POWER_SUPPLY_H__
13 #define __LINUX_POWER_SUPPLY_H__
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>
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.
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.
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
,
42 /* What algorithm is the charger using? */
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 */
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 POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED
,
68 POWER_SUPPLY_TECHNOLOGY_UNKNOWN
= 0,
69 POWER_SUPPLY_TECHNOLOGY_NiMH
,
70 POWER_SUPPLY_TECHNOLOGY_LION
,
71 POWER_SUPPLY_TECHNOLOGY_LIPO
,
72 POWER_SUPPLY_TECHNOLOGY_LiFe
,
73 POWER_SUPPLY_TECHNOLOGY_NiCd
,
74 POWER_SUPPLY_TECHNOLOGY_LiMn
,
78 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
= 0,
79 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
,
80 POWER_SUPPLY_CAPACITY_LEVEL_LOW
,
81 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
,
82 POWER_SUPPLY_CAPACITY_LEVEL_HIGH
,
83 POWER_SUPPLY_CAPACITY_LEVEL_FULL
,
87 POWER_SUPPLY_SCOPE_UNKNOWN
= 0,
88 POWER_SUPPLY_SCOPE_SYSTEM
,
89 POWER_SUPPLY_SCOPE_DEVICE
,
92 enum power_supply_property
{
93 /* Properties of type `int' */
94 POWER_SUPPLY_PROP_STATUS
= 0,
95 POWER_SUPPLY_PROP_CHARGE_TYPE
,
96 POWER_SUPPLY_PROP_HEALTH
,
97 POWER_SUPPLY_PROP_PRESENT
,
98 POWER_SUPPLY_PROP_ONLINE
,
99 POWER_SUPPLY_PROP_AUTHENTIC
,
100 POWER_SUPPLY_PROP_TECHNOLOGY
,
101 POWER_SUPPLY_PROP_CYCLE_COUNT
,
102 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
103 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
104 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
105 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
106 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
107 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
108 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
109 POWER_SUPPLY_PROP_VOLTAGE_BOOT
,
110 POWER_SUPPLY_PROP_CURRENT_MAX
,
111 POWER_SUPPLY_PROP_CURRENT_NOW
,
112 POWER_SUPPLY_PROP_CURRENT_AVG
,
113 POWER_SUPPLY_PROP_CURRENT_BOOT
,
114 POWER_SUPPLY_PROP_POWER_NOW
,
115 POWER_SUPPLY_PROP_POWER_AVG
,
116 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
117 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
,
118 POWER_SUPPLY_PROP_CHARGE_FULL
,
119 POWER_SUPPLY_PROP_CHARGE_EMPTY
,
120 POWER_SUPPLY_PROP_CHARGE_NOW
,
121 POWER_SUPPLY_PROP_CHARGE_AVG
,
122 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
123 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
124 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
125 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
126 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
127 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
,
128 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
,
129 POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD
, /* in percents! */
130 POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD
, /* in percents! */
131 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
132 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT
,
133 POWER_SUPPLY_PROP_INPUT_POWER_LIMIT
,
134 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
135 POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN
,
136 POWER_SUPPLY_PROP_ENERGY_FULL
,
137 POWER_SUPPLY_PROP_ENERGY_EMPTY
,
138 POWER_SUPPLY_PROP_ENERGY_NOW
,
139 POWER_SUPPLY_PROP_ENERGY_AVG
,
140 POWER_SUPPLY_PROP_CAPACITY
, /* in percents! */
141 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
, /* in percents! */
142 POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX
, /* in percents! */
143 POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN
, /* in percents! */
144 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
145 POWER_SUPPLY_PROP_TEMP
,
146 POWER_SUPPLY_PROP_TEMP_MAX
,
147 POWER_SUPPLY_PROP_TEMP_MIN
,
148 POWER_SUPPLY_PROP_TEMP_ALERT_MIN
,
149 POWER_SUPPLY_PROP_TEMP_ALERT_MAX
,
150 POWER_SUPPLY_PROP_TEMP_AMBIENT
,
151 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN
,
152 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX
,
153 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
154 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
155 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
156 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
,
157 POWER_SUPPLY_PROP_TYPE
, /* use power_supply.type instead */
158 POWER_SUPPLY_PROP_USB_TYPE
,
159 POWER_SUPPLY_PROP_SCOPE
,
160 POWER_SUPPLY_PROP_PRECHARGE_CURRENT
,
161 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
162 POWER_SUPPLY_PROP_CALIBRATE
,
163 POWER_SUPPLY_PROP_MANUFACTURE_YEAR
,
164 POWER_SUPPLY_PROP_MANUFACTURE_MONTH
,
165 POWER_SUPPLY_PROP_MANUFACTURE_DAY
,
166 /* Properties of type `const char *' */
167 POWER_SUPPLY_PROP_MODEL_NAME
,
168 POWER_SUPPLY_PROP_MANUFACTURER
,
169 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
172 enum power_supply_type
{
173 POWER_SUPPLY_TYPE_UNKNOWN
= 0,
174 POWER_SUPPLY_TYPE_BATTERY
,
175 POWER_SUPPLY_TYPE_UPS
,
176 POWER_SUPPLY_TYPE_MAINS
,
177 POWER_SUPPLY_TYPE_USB
, /* Standard Downstream Port */
178 POWER_SUPPLY_TYPE_USB_DCP
, /* Dedicated Charging Port */
179 POWER_SUPPLY_TYPE_USB_CDP
, /* Charging Downstream Port */
180 POWER_SUPPLY_TYPE_USB_ACA
, /* Accessory Charger Adapters */
181 POWER_SUPPLY_TYPE_USB_TYPE_C
, /* Type C Port */
182 POWER_SUPPLY_TYPE_USB_PD
, /* Power Delivery Port */
183 POWER_SUPPLY_TYPE_USB_PD_DRP
, /* PD Dual Role Port */
184 POWER_SUPPLY_TYPE_APPLE_BRICK_ID
, /* Apple Charging Method */
187 enum power_supply_usb_type
{
188 POWER_SUPPLY_USB_TYPE_UNKNOWN
= 0,
189 POWER_SUPPLY_USB_TYPE_SDP
, /* Standard Downstream Port */
190 POWER_SUPPLY_USB_TYPE_DCP
, /* Dedicated Charging Port */
191 POWER_SUPPLY_USB_TYPE_CDP
, /* Charging Downstream Port */
192 POWER_SUPPLY_USB_TYPE_ACA
, /* Accessory Charger Adapters */
193 POWER_SUPPLY_USB_TYPE_C
, /* Type C Port */
194 POWER_SUPPLY_USB_TYPE_PD
, /* Power Delivery Port */
195 POWER_SUPPLY_USB_TYPE_PD_DRP
, /* PD Dual Role Port */
196 POWER_SUPPLY_USB_TYPE_PD_PPS
, /* PD Programmable Power Supply */
197 POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
, /* Apple Charging Method */
200 enum power_supply_notifier_events
{
201 PSY_EVENT_PROP_CHANGED
,
204 union power_supply_propval
{
212 /* Run-time specific power supply configuration */
213 struct power_supply_config
{
214 struct device_node
*of_node
;
215 struct fwnode_handle
*fwnode
;
217 /* Driver private data */
220 /* Device specific sysfs attributes */
221 const struct attribute_group
**attr_grp
;
224 size_t num_supplicants
;
227 /* Description of power supply */
228 struct power_supply_desc
{
230 enum power_supply_type type
;
231 const enum power_supply_usb_type
*usb_types
;
232 size_t num_usb_types
;
233 const enum power_supply_property
*properties
;
234 size_t num_properties
;
237 * Functions for drivers implementing power supply class.
238 * These shouldn't be called directly by other drivers for accessing
239 * this power supply. Instead use power_supply_*() functions (for
240 * example power_supply_get_property()).
242 int (*get_property
)(struct power_supply
*psy
,
243 enum power_supply_property psp
,
244 union power_supply_propval
*val
);
245 int (*set_property
)(struct power_supply
*psy
,
246 enum power_supply_property psp
,
247 const union power_supply_propval
*val
);
249 * property_is_writeable() will be called during registration
250 * of power supply. If this happens during device probe then it must
251 * not access internal data of device (because probe did not end).
253 int (*property_is_writeable
)(struct power_supply
*psy
,
254 enum power_supply_property psp
);
255 void (*external_power_changed
)(struct power_supply
*psy
);
256 void (*set_charged
)(struct power_supply
*psy
);
259 * Set if thermal zone should not be created for this power supply.
260 * For example for virtual supplies forwarding calls to actual
261 * sensors or other supplies.
264 /* For APM emulation, think legacy userspace. */
268 struct power_supply
{
269 const struct power_supply_desc
*desc
;
272 size_t num_supplicants
;
274 char **supplied_from
;
276 struct device_node
*of_node
;
278 /* Driver private data */
283 struct work_struct changed_work
;
284 struct delayed_work deferred_register_work
;
285 spinlock_t changed_lock
;
290 #ifdef CONFIG_THERMAL
291 struct thermal_zone_device
*tzd
;
292 struct thermal_cooling_device
*tcd
;
295 #ifdef CONFIG_LEDS_TRIGGERS
296 struct led_trigger
*charging_full_trig
;
297 char *charging_full_trig_name
;
298 struct led_trigger
*charging_trig
;
299 char *charging_trig_name
;
300 struct led_trigger
*full_trig
;
301 char *full_trig_name
;
302 struct led_trigger
*online_trig
;
303 char *online_trig_name
;
304 struct led_trigger
*charging_blink_full_solid_trig
;
305 char *charging_blink_full_solid_trig_name
;
310 * This is recommended structure to specify static power supply parameters.
311 * Generic one, parametrizable for different power supplies. Power supply
312 * class itself does not use it, but that's what implementing most platform
313 * drivers, should try reuse for consistency.
316 struct power_supply_info
{
319 int voltage_max_design
;
320 int voltage_min_design
;
321 int charge_full_design
;
322 int charge_empty_design
;
323 int energy_full_design
;
324 int energy_empty_design
;
328 struct power_supply_battery_ocv_table
{
329 int ocv
; /* microVolts */
330 int capacity
; /* percent */
333 struct power_supply_resistance_temp_table
{
334 int temp
; /* celsius */
335 int resistance
; /* internal resistance percent */
338 #define POWER_SUPPLY_OCV_TEMP_MAX 20
341 * This is the recommended struct to manage static battery parameters,
342 * populated by power_supply_get_battery_info(). Most platform drivers should
343 * use these for consistency.
344 * Its field names must correspond to elements in enum power_supply_property.
345 * The default field value is -EINVAL.
346 * Power supply class itself doesn't use this.
349 struct power_supply_battery_info
{
350 int energy_full_design_uwh
; /* microWatt-hours */
351 int charge_full_design_uah
; /* microAmp-hours */
352 int voltage_min_design_uv
; /* microVolts */
353 int voltage_max_design_uv
; /* microVolts */
354 int tricklecharge_current_ua
; /* microAmps */
355 int precharge_current_ua
; /* microAmps */
356 int precharge_voltage_max_uv
; /* microVolts */
357 int charge_term_current_ua
; /* microAmps */
358 int charge_restart_voltage_uv
; /* microVolts */
359 int overvoltage_limit_uv
; /* microVolts */
360 int constant_charge_current_max_ua
; /* microAmps */
361 int constant_charge_voltage_max_uv
; /* microVolts */
362 int factory_internal_resistance_uohm
; /* microOhms */
363 int ocv_temp
[POWER_SUPPLY_OCV_TEMP_MAX
];/* celsius */
364 struct power_supply_battery_ocv_table
*ocv_table
[POWER_SUPPLY_OCV_TEMP_MAX
];
365 int ocv_table_size
[POWER_SUPPLY_OCV_TEMP_MAX
];
366 struct power_supply_resistance_temp_table
*resist_table
;
367 int resist_table_size
;
370 extern struct atomic_notifier_head power_supply_notifier
;
371 extern int power_supply_reg_notifier(struct notifier_block
*nb
);
372 extern void power_supply_unreg_notifier(struct notifier_block
*nb
);
373 extern struct power_supply
*power_supply_get_by_name(const char *name
);
374 extern void power_supply_put(struct power_supply
*psy
);
376 extern struct power_supply
*power_supply_get_by_phandle(struct device_node
*np
,
377 const char *property
);
378 extern struct power_supply
*devm_power_supply_get_by_phandle(
379 struct device
*dev
, const char *property
);
380 #else /* !CONFIG_OF */
381 static inline struct power_supply
*
382 power_supply_get_by_phandle(struct device_node
*np
, const char *property
)
384 static inline struct power_supply
*
385 devm_power_supply_get_by_phandle(struct device
*dev
, const char *property
)
387 #endif /* CONFIG_OF */
389 extern int power_supply_get_battery_info(struct power_supply
*psy
,
390 struct power_supply_battery_info
*info
);
391 extern void power_supply_put_battery_info(struct power_supply
*psy
,
392 struct power_supply_battery_info
*info
);
393 extern int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table
*table
,
394 int table_len
, int ocv
);
395 extern struct power_supply_battery_ocv_table
*
396 power_supply_find_ocv2cap_table(struct power_supply_battery_info
*info
,
397 int temp
, int *table_len
);
398 extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info
*info
,
401 power_supply_temp2resist_simple(struct power_supply_resistance_temp_table
*table
,
402 int table_len
, int temp
);
403 extern void power_supply_changed(struct power_supply
*psy
);
404 extern int power_supply_am_i_supplied(struct power_supply
*psy
);
405 extern int power_supply_set_input_current_limit_from_supplier(
406 struct power_supply
*psy
);
407 extern int power_supply_set_battery_charged(struct power_supply
*psy
);
409 #ifdef CONFIG_POWER_SUPPLY
410 extern int power_supply_is_system_supplied(void);
412 static inline int power_supply_is_system_supplied(void) { return -ENOSYS
; }
415 extern int power_supply_get_property(struct power_supply
*psy
,
416 enum power_supply_property psp
,
417 union power_supply_propval
*val
);
418 extern int power_supply_set_property(struct power_supply
*psy
,
419 enum power_supply_property psp
,
420 const union power_supply_propval
*val
);
421 extern int power_supply_property_is_writeable(struct power_supply
*psy
,
422 enum power_supply_property psp
);
423 extern void power_supply_external_power_changed(struct power_supply
*psy
);
425 extern struct power_supply
*__must_check
426 power_supply_register(struct device
*parent
,
427 const struct power_supply_desc
*desc
,
428 const struct power_supply_config
*cfg
);
429 extern struct power_supply
*__must_check
430 power_supply_register_no_ws(struct device
*parent
,
431 const struct power_supply_desc
*desc
,
432 const struct power_supply_config
*cfg
);
433 extern struct power_supply
*__must_check
434 devm_power_supply_register(struct device
*parent
,
435 const struct power_supply_desc
*desc
,
436 const struct power_supply_config
*cfg
);
437 extern struct power_supply
*__must_check
438 devm_power_supply_register_no_ws(struct device
*parent
,
439 const struct power_supply_desc
*desc
,
440 const struct power_supply_config
*cfg
);
441 extern void power_supply_unregister(struct power_supply
*psy
);
442 extern int power_supply_powers(struct power_supply
*psy
, struct device
*dev
);
444 #define to_power_supply(device) container_of(device, struct power_supply, dev)
446 extern void *power_supply_get_drvdata(struct power_supply
*psy
);
447 /* For APM emulation, think legacy userspace. */
448 extern struct class *power_supply_class
;
450 static inline bool power_supply_is_amp_property(enum power_supply_property psp
)
453 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
454 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
455 case POWER_SUPPLY_PROP_CHARGE_FULL
:
456 case POWER_SUPPLY_PROP_CHARGE_EMPTY
:
457 case POWER_SUPPLY_PROP_CHARGE_NOW
:
458 case POWER_SUPPLY_PROP_CHARGE_AVG
:
459 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
460 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT
:
461 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
462 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
463 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
464 case POWER_SUPPLY_PROP_CURRENT_MAX
:
465 case POWER_SUPPLY_PROP_CURRENT_NOW
:
466 case POWER_SUPPLY_PROP_CURRENT_AVG
:
467 case POWER_SUPPLY_PROP_CURRENT_BOOT
:
476 static inline bool power_supply_is_watt_property(enum power_supply_property psp
)
479 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
480 case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN
:
481 case POWER_SUPPLY_PROP_ENERGY_FULL
:
482 case POWER_SUPPLY_PROP_ENERGY_EMPTY
:
483 case POWER_SUPPLY_PROP_ENERGY_NOW
:
484 case POWER_SUPPLY_PROP_ENERGY_AVG
:
485 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
486 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
487 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
488 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
489 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
490 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
491 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
492 case POWER_SUPPLY_PROP_VOLTAGE_BOOT
:
493 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
494 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
495 case POWER_SUPPLY_PROP_POWER_NOW
:
504 #ifdef CONFIG_POWER_SUPPLY_HWMON
505 int power_supply_add_hwmon_sysfs(struct power_supply
*psy
);
506 void power_supply_remove_hwmon_sysfs(struct power_supply
*psy
);
508 static inline int power_supply_add_hwmon_sysfs(struct power_supply
*psy
)
514 void power_supply_remove_hwmon_sysfs(struct power_supply
*psy
) {}
517 #endif /* __LINUX_POWER_SUPPLY_H__ */