2 * Universal power supply monitor class
4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
5 * Copyright © 2004 Szabolcs Gyurko
6 * Copyright © 2003 Ian Molton <spyro@f2s.com>
8 * Modified: 2004, Oct Szabolcs Gyurko
10 * You may use this code as per GPL version 2
13 #ifndef __LINUX_POWER_SUPPLY_H__
14 #define __LINUX_POWER_SUPPLY_H__
16 #include <linux/device.h>
17 #include <linux/workqueue.h>
18 #include <linux/leds.h>
19 #include <linux/spinlock.h>
20 #include <linux/notifier.h>
21 #include <linux/types.h>
24 * All voltages, currents, charges, energies, time and temperatures in uV,
25 * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
26 * stated. It's driver's job to convert its raw values to units in which
27 * this class operates.
31 * For systems where the charger determines the maximum battery capacity
32 * the min and max fields should be used to present these values to user
33 * space. Unused/unknown fields will not appear in sysfs.
37 POWER_SUPPLY_STATUS_UNKNOWN
= 0,
38 POWER_SUPPLY_STATUS_CHARGING
,
39 POWER_SUPPLY_STATUS_DISCHARGING
,
40 POWER_SUPPLY_STATUS_NOT_CHARGING
,
41 POWER_SUPPLY_STATUS_FULL
,
45 POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
= 0,
46 POWER_SUPPLY_CHARGE_TYPE_NONE
,
47 POWER_SUPPLY_CHARGE_TYPE_TRICKLE
,
48 POWER_SUPPLY_CHARGE_TYPE_FAST
,
52 POWER_SUPPLY_HEALTH_UNKNOWN
= 0,
53 POWER_SUPPLY_HEALTH_GOOD
,
54 POWER_SUPPLY_HEALTH_OVERHEAT
,
55 POWER_SUPPLY_HEALTH_DEAD
,
56 POWER_SUPPLY_HEALTH_OVERVOLTAGE
,
57 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
,
58 POWER_SUPPLY_HEALTH_COLD
,
59 POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE
,
60 POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
,
61 POWER_SUPPLY_HEALTH_UNDERVOLTAGE
,
65 POWER_SUPPLY_TECHNOLOGY_UNKNOWN
= 0,
66 POWER_SUPPLY_TECHNOLOGY_NiMH
,
67 POWER_SUPPLY_TECHNOLOGY_LION
,
68 POWER_SUPPLY_TECHNOLOGY_LIPO
,
69 POWER_SUPPLY_TECHNOLOGY_LiFe
,
70 POWER_SUPPLY_TECHNOLOGY_NiCd
,
71 POWER_SUPPLY_TECHNOLOGY_LiMn
,
75 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
= 0,
76 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
,
77 POWER_SUPPLY_CAPACITY_LEVEL_LOW
,
78 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
,
79 POWER_SUPPLY_CAPACITY_LEVEL_HIGH
,
80 POWER_SUPPLY_CAPACITY_LEVEL_FULL
,
84 POWER_SUPPLY_SCOPE_UNKNOWN
= 0,
85 POWER_SUPPLY_SCOPE_SYSTEM
,
86 POWER_SUPPLY_SCOPE_DEVICE
,
89 enum power_supply_property
{
90 /* Properties of type `int' */
91 POWER_SUPPLY_PROP_STATUS
= 0,
92 POWER_SUPPLY_PROP_CHARGE_TYPE
,
93 POWER_SUPPLY_PROP_HEALTH
,
94 POWER_SUPPLY_PROP_PRESENT
,
95 POWER_SUPPLY_PROP_ONLINE
,
96 POWER_SUPPLY_PROP_AUTHENTIC
,
97 POWER_SUPPLY_PROP_CHARGING_ENABLED
,
98 POWER_SUPPLY_PROP_TECHNOLOGY
,
99 POWER_SUPPLY_PROP_CYCLE_COUNT
,
100 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
101 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
102 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
103 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
104 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
105 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
106 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
107 POWER_SUPPLY_PROP_VOLTAGE_BOOT
,
108 POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION
,
109 POWER_SUPPLY_PROP_CURRENT_MAX
,
110 POWER_SUPPLY_PROP_CURRENT_NOW
,
111 POWER_SUPPLY_PROP_CURRENT_AVG
,
112 POWER_SUPPLY_PROP_CURRENT_FULL
,
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_CHARGE_OTG_CONTROL
,
124 POWER_SUPPLY_PROP_CHARGE_POWERED_OTG_CONTROL
,
125 POWER_SUPPLY_PROP_CHARGE_TEMP
,
126 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
127 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
128 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
129 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
130 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
,
131 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
,
132 POWER_SUPPLY_PROP_INPUT_CURRENT_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_SCOPE
,
157 POWER_SUPPLY_PROP_PRECHARGE_CURRENT
,
158 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
159 POWER_SUPPLY_PROP_CALIBRATE
,
160 /* Local extensions */
161 POWER_SUPPLY_PROP_USB_HC
,
162 POWER_SUPPLY_PROP_USB_OTG
,
163 POWER_SUPPLY_PROP_CHARGE_ENABLED
,
164 POWER_SUPPLY_PROP_FUELGAUGE_RESET
,
165 POWER_SUPPLY_PROP_SOH
,
166 /* Local extensions of type int64_t */
167 POWER_SUPPLY_PROP_CHARGE_COUNTER_EXT
,
168 /* Properties of type `const char *' */
169 POWER_SUPPLY_PROP_MODEL_NAME
,
170 POWER_SUPPLY_PROP_MANUFACTURER
,
171 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
173 POWER_SUPPLY_PROP_VCHGIN
,
174 POWER_SUPPLY_PROP_VWCIN
,
175 POWER_SUPPLY_PROP_VBYP
,
176 POWER_SUPPLY_PROP_VSYS
,
177 POWER_SUPPLY_PROP_VBAT
,
178 POWER_SUPPLY_PROP_VGPADC
,
179 POWER_SUPPLY_PROP_VCC1
,
180 POWER_SUPPLY_PROP_VCC2
,
181 POWER_SUPPLY_PROP_ICHGIN
,
182 POWER_SUPPLY_PROP_IWCIN
,
183 POWER_SUPPLY_PROP_IOTG
,
184 POWER_SUPPLY_PROP_ITX
,
185 POWER_SUPPLY_PROP_CO_ENABLE
,
186 POWER_SUPPLY_PROP_RR_ENABLE
,
187 POWER_SUPPLY_PROP_USBPD_RESET
,
188 POWER_SUPPLY_PROP_USBPD_TEST_READ
,
191 enum power_supply_type
{
192 POWER_SUPPLY_TYPE_UNKNOWN
= 0,
193 POWER_SUPPLY_TYPE_BATTERY
,
194 POWER_SUPPLY_TYPE_UPS
,
195 POWER_SUPPLY_TYPE_MAINS
,
196 POWER_SUPPLY_TYPE_USB
, /* Standard Downstream Port */
197 POWER_SUPPLY_TYPE_USB_DCP
, /* Dedicated Charging Port */
198 POWER_SUPPLY_TYPE_USB_CDP
, /* Charging Downstream Port */
199 POWER_SUPPLY_TYPE_USB_ACA
, /* Accessory Charger Adapters */
200 POWER_SUPPLY_TYPE_USB_TYPE_C
, /* Type C Port */
201 POWER_SUPPLY_TYPE_USB_PD
, /* Power Delivery Port */
202 POWER_SUPPLY_TYPE_USB_PD_DRP
, /* PD Dual Role Port */
203 POWER_SUPPLY_TYPE_APPLE_BRICK_ID
, /* Apple Charging Method */
204 POWER_SUPPLY_TYPE_OTG
,
205 POWER_SUPPLY_TYPE_HV_MAINS
,
206 POWER_SUPPLY_TYPE_PREPARE_TA
,
207 POWER_SUPPLY_TYPE_SMART_NOTG
,
208 POWER_SUPPLY_TYPE_END
,
211 enum power_supply_notifier_events
{
212 PSY_EVENT_PROP_CHANGED
,
213 PSY_EVENT_PROP_ADDED
,
216 union power_supply_propval
{
225 /* Run-time specific power supply configuration */
226 struct power_supply_config
{
227 struct device_node
*of_node
;
228 /* Driver private data */
232 size_t num_supplicants
;
235 /* Description of power supply */
236 struct power_supply_desc
{
238 enum power_supply_type type
;
239 enum power_supply_property
*properties
;
240 size_t num_properties
;
243 * Functions for drivers implementing power supply class.
244 * These shouldn't be called directly by other drivers for accessing
245 * this power supply. Instead use power_supply_*() functions (for
246 * example power_supply_get_property()).
248 int (*get_property
)(struct power_supply
*psy
,
249 enum power_supply_property psp
,
250 union power_supply_propval
*val
);
251 int (*set_property
)(struct power_supply
*psy
,
252 enum power_supply_property psp
,
253 const union power_supply_propval
*val
);
255 * property_is_writeable() will be called during registration
256 * of power supply. If this happens during device probe then it must
257 * not access internal data of device (because probe did not end).
259 int (*property_is_writeable
)(struct power_supply
*psy
,
260 enum power_supply_property psp
);
261 void (*external_power_changed
)(struct power_supply
*psy
);
262 void (*set_charged
)(struct power_supply
*psy
);
265 * Set if thermal zone should not be created for this power supply.
266 * For example for virtual supplies forwarding calls to actual
267 * sensors or other supplies.
270 /* For APM emulation, think legacy userspace. */
274 struct power_supply
{
275 const struct power_supply_desc
*desc
;
278 size_t num_supplicants
;
280 char **supplied_from
;
282 struct device_node
*of_node
;
284 /* Driver private data */
289 struct work_struct changed_work
;
290 struct delayed_work deferred_register_work
;
291 spinlock_t changed_lock
;
296 #ifdef CONFIG_THERMAL
297 struct thermal_zone_device
*tzd
;
298 struct thermal_cooling_device
*tcd
;
301 #ifdef CONFIG_LEDS_TRIGGERS
302 struct led_trigger
*charging_full_trig
;
303 char *charging_full_trig_name
;
304 struct led_trigger
*charging_trig
;
305 char *charging_trig_name
;
306 struct led_trigger
*full_trig
;
307 char *full_trig_name
;
308 struct led_trigger
*online_trig
;
309 char *online_trig_name
;
310 struct led_trigger
*charging_blink_full_solid_trig
;
311 char *charging_blink_full_solid_trig_name
;
316 * This is recommended structure to specify static power supply parameters.
317 * Generic one, parametrizable for different power supplies. Power supply
318 * class itself does not use it, but that's what implementing most platform
319 * drivers, should try reuse for consistency.
322 struct power_supply_info
{
325 int voltage_max_design
;
326 int voltage_min_design
;
327 int charge_full_design
;
328 int charge_empty_design
;
329 int energy_full_design
;
330 int energy_empty_design
;
335 * This is the recommended struct to manage static battery parameters,
336 * populated by power_supply_get_battery_info(). Most platform drivers should
337 * use these for consistency.
338 * Its field names must correspond to elements in enum power_supply_property.
339 * The default field value is -EINVAL.
340 * Power supply class itself doesn't use this.
343 struct power_supply_battery_info
{
344 int energy_full_design_uwh
; /* microWatt-hours */
345 int charge_full_design_uah
; /* microAmp-hours */
346 int voltage_min_design_uv
; /* microVolts */
347 int precharge_current_ua
; /* microAmps */
348 int charge_term_current_ua
; /* microAmps */
349 int constant_charge_current_max_ua
; /* microAmps */
350 int constant_charge_voltage_max_uv
; /* microVolts */
353 extern struct atomic_notifier_head power_supply_notifier
;
354 extern int power_supply_reg_notifier(struct notifier_block
*nb
);
355 extern void power_supply_unreg_notifier(struct notifier_block
*nb
);
356 extern struct power_supply
*power_supply_get_by_name(const char *name
);
357 extern void power_supply_put(struct power_supply
*psy
);
359 extern struct power_supply
*power_supply_get_by_phandle(struct device_node
*np
,
360 const char *property
);
361 extern struct power_supply
*devm_power_supply_get_by_phandle(
362 struct device
*dev
, const char *property
);
363 #else /* !CONFIG_OF */
364 static inline struct power_supply
*
365 power_supply_get_by_phandle(struct device_node
*np
, const char *property
)
367 static inline struct power_supply
*
368 devm_power_supply_get_by_phandle(struct device
*dev
, const char *property
)
370 #endif /* CONFIG_OF */
372 extern int power_supply_get_battery_info(struct power_supply
*psy
,
373 struct power_supply_battery_info
*info
);
374 extern void power_supply_changed(struct power_supply
*psy
);
375 extern int power_supply_am_i_supplied(struct power_supply
*psy
);
376 extern int power_supply_set_input_current_limit_from_supplier(
377 struct power_supply
*psy
);
378 extern int power_supply_set_battery_charged(struct power_supply
*psy
);
380 #ifdef CONFIG_POWER_SUPPLY
381 extern int power_supply_is_system_supplied(void);
383 static inline int power_supply_is_system_supplied(void) { return -ENOSYS
; }
386 extern int power_supply_get_property(struct power_supply
*psy
,
387 enum power_supply_property psp
,
388 union power_supply_propval
*val
);
389 extern int power_supply_set_property(struct power_supply
*psy
,
390 enum power_supply_property psp
,
391 const union power_supply_propval
*val
);
392 extern int power_supply_property_is_writeable(struct power_supply
*psy
,
393 enum power_supply_property psp
);
394 extern void power_supply_external_power_changed(struct power_supply
*psy
);
396 extern struct power_supply
*__must_check
397 power_supply_register(struct device
*parent
,
398 const struct power_supply_desc
*desc
,
399 const struct power_supply_config
*cfg
);
400 extern struct power_supply
*__must_check
401 power_supply_register_no_ws(struct device
*parent
,
402 const struct power_supply_desc
*desc
,
403 const struct power_supply_config
*cfg
);
404 extern struct power_supply
*__must_check
405 devm_power_supply_register(struct device
*parent
,
406 const struct power_supply_desc
*desc
,
407 const struct power_supply_config
*cfg
);
408 extern struct power_supply
*__must_check
409 devm_power_supply_register_no_ws(struct device
*parent
,
410 const struct power_supply_desc
*desc
,
411 const struct power_supply_config
*cfg
);
412 extern void power_supply_unregister(struct power_supply
*psy
);
413 extern int power_supply_powers(struct power_supply
*psy
, struct device
*dev
);
415 extern void *power_supply_get_drvdata(struct power_supply
*psy
);
416 /* For APM emulation, think legacy userspace. */
417 extern struct class *power_supply_class
;
419 static inline bool power_supply_is_amp_property(enum power_supply_property psp
)
422 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
423 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN
:
424 case POWER_SUPPLY_PROP_CHARGE_FULL
:
425 case POWER_SUPPLY_PROP_CHARGE_EMPTY
:
426 case POWER_SUPPLY_PROP_CHARGE_NOW
:
427 case POWER_SUPPLY_PROP_CHARGE_AVG
:
428 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
429 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT
:
430 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
431 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
432 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
433 case POWER_SUPPLY_PROP_CURRENT_MAX
:
434 case POWER_SUPPLY_PROP_CURRENT_NOW
:
435 case POWER_SUPPLY_PROP_CURRENT_AVG
:
436 case POWER_SUPPLY_PROP_CURRENT_BOOT
:
445 static inline bool power_supply_is_watt_property(enum power_supply_property psp
)
448 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
449 case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN
:
450 case POWER_SUPPLY_PROP_ENERGY_FULL
:
451 case POWER_SUPPLY_PROP_ENERGY_EMPTY
:
452 case POWER_SUPPLY_PROP_ENERGY_NOW
:
453 case POWER_SUPPLY_PROP_ENERGY_AVG
:
454 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
455 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
456 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
457 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
458 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
459 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
460 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
461 case POWER_SUPPLY_PROP_VOLTAGE_BOOT
:
462 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
463 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
464 case POWER_SUPPLY_PROP_POWER_NOW
:
473 #endif /* __LINUX_POWER_SUPPLY_H__ */