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/rwsem.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/notifier.h>
22
23 /*
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.
28 */
29
30 /*
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.
34 */
35
36 enum {
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,
42 };
43
44 /* What algorithm is the charger using? */
45 enum power_supply_charge_type {
46 POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,
47 POWER_SUPPLY_CHARGE_TYPE_NONE,
48 POWER_SUPPLY_CHARGE_TYPE_TRICKLE, /* slow speed */
49 POWER_SUPPLY_CHARGE_TYPE_FAST, /* fast speed */
50 POWER_SUPPLY_CHARGE_TYPE_STANDARD, /* normal speed */
51 POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE, /* dynamically adjusted speed */
52 POWER_SUPPLY_CHARGE_TYPE_CUSTOM, /* use CHARGE_CONTROL_* props */
53 POWER_SUPPLY_CHARGE_TYPE_LONGLIFE, /* slow speed, longer life */
54 POWER_SUPPLY_CHARGE_TYPE_BYPASS, /* bypassing the charger */
55 };
56
57 enum {
58 POWER_SUPPLY_HEALTH_UNKNOWN = 0,
59 POWER_SUPPLY_HEALTH_GOOD,
60 POWER_SUPPLY_HEALTH_OVERHEAT,
61 POWER_SUPPLY_HEALTH_DEAD,
62 POWER_SUPPLY_HEALTH_OVERVOLTAGE,
63 POWER_SUPPLY_HEALTH_UNDERVOLTAGE,
64 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
65 POWER_SUPPLY_HEALTH_COLD,
66 POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE,
67 POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE,
68 POWER_SUPPLY_HEALTH_OVERCURRENT,
69 POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED,
70 POWER_SUPPLY_HEALTH_WARM,
71 POWER_SUPPLY_HEALTH_COOL,
72 POWER_SUPPLY_HEALTH_HOT,
73 POWER_SUPPLY_HEALTH_NO_BATTERY,
74 POWER_SUPPLY_HEALTH_BLOWN_FUSE,
75 POWER_SUPPLY_HEALTH_CELL_IMBALANCE,
76 };
77
78 enum {
79 POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
80 POWER_SUPPLY_TECHNOLOGY_NiMH,
81 POWER_SUPPLY_TECHNOLOGY_LION,
82 POWER_SUPPLY_TECHNOLOGY_LIPO,
83 POWER_SUPPLY_TECHNOLOGY_LiFe,
84 POWER_SUPPLY_TECHNOLOGY_NiCd,
85 POWER_SUPPLY_TECHNOLOGY_LiMn,
86 };
87
88 enum {
89 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
90 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
91 POWER_SUPPLY_CAPACITY_LEVEL_LOW,
92 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
93 POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
94 POWER_SUPPLY_CAPACITY_LEVEL_FULL,
95 };
96
97 enum {
98 POWER_SUPPLY_SCOPE_UNKNOWN = 0,
99 POWER_SUPPLY_SCOPE_SYSTEM,
100 POWER_SUPPLY_SCOPE_DEVICE,
101 };
102
103 enum power_supply_property {
104 /* Properties of type `int' */
105 POWER_SUPPLY_PROP_STATUS = 0,
106 POWER_SUPPLY_PROP_CHARGE_TYPE,
107 POWER_SUPPLY_PROP_CHARGE_TYPES,
108 POWER_SUPPLY_PROP_HEALTH,
109 POWER_SUPPLY_PROP_PRESENT,
110 POWER_SUPPLY_PROP_ONLINE,
111 POWER_SUPPLY_PROP_AUTHENTIC,
112 POWER_SUPPLY_PROP_TECHNOLOGY,
113 POWER_SUPPLY_PROP_CYCLE_COUNT,
114 POWER_SUPPLY_PROP_VOLTAGE_MAX,
115 POWER_SUPPLY_PROP_VOLTAGE_MIN,
116 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
117 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
118 POWER_SUPPLY_PROP_VOLTAGE_NOW,
119 POWER_SUPPLY_PROP_VOLTAGE_AVG,
120 POWER_SUPPLY_PROP_VOLTAGE_OCV,
121 POWER_SUPPLY_PROP_VOLTAGE_BOOT,
122 POWER_SUPPLY_PROP_CURRENT_MAX,
123 POWER_SUPPLY_PROP_CURRENT_NOW,
124 POWER_SUPPLY_PROP_CURRENT_AVG,
125 POWER_SUPPLY_PROP_CURRENT_BOOT,
126 POWER_SUPPLY_PROP_POWER_NOW,
127 POWER_SUPPLY_PROP_POWER_AVG,
128 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
129 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
130 POWER_SUPPLY_PROP_CHARGE_FULL,
131 POWER_SUPPLY_PROP_CHARGE_EMPTY,
132 POWER_SUPPLY_PROP_CHARGE_NOW,
133 POWER_SUPPLY_PROP_CHARGE_AVG,
134 POWER_SUPPLY_PROP_CHARGE_COUNTER,
135 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
136 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
137 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
138 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
139 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
140 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
141 POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, /* in percents! */
142 POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, /* in percents! */
143 POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
144 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
145 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
146 POWER_SUPPLY_PROP_INPUT_POWER_LIMIT,
147 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
148 POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
149 POWER_SUPPLY_PROP_ENERGY_FULL,
150 POWER_SUPPLY_PROP_ENERGY_EMPTY,
151 POWER_SUPPLY_PROP_ENERGY_NOW,
152 POWER_SUPPLY_PROP_ENERGY_AVG,
153 POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
154 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
155 POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
156 POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, /* in percents! */
157 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
158 POWER_SUPPLY_PROP_TEMP,
159 POWER_SUPPLY_PROP_TEMP_MAX,
160 POWER_SUPPLY_PROP_TEMP_MIN,
161 POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
162 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
163 POWER_SUPPLY_PROP_TEMP_AMBIENT,
164 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
165 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
166 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
167 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
168 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
169 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
170 POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
171 POWER_SUPPLY_PROP_USB_TYPE,
172 POWER_SUPPLY_PROP_SCOPE,
173 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
174 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
175 POWER_SUPPLY_PROP_CALIBRATE,
176 POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
177 POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
178 POWER_SUPPLY_PROP_MANUFACTURE_DAY,
179 /* Properties of type `const char *' */
180 POWER_SUPPLY_PROP_MODEL_NAME,
181 POWER_SUPPLY_PROP_MANUFACTURER,
182 POWER_SUPPLY_PROP_SERIAL_NUMBER,
183 };
184
185 enum power_supply_type {
186 POWER_SUPPLY_TYPE_UNKNOWN = 0,
187 POWER_SUPPLY_TYPE_BATTERY,
188 POWER_SUPPLY_TYPE_UPS,
189 POWER_SUPPLY_TYPE_MAINS,
190 POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port */
191 POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */
192 POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */
193 POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */
194 POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */
195 POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */
196 POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */
197 POWER_SUPPLY_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */
198 POWER_SUPPLY_TYPE_WIRELESS, /* Wireless */
199 };
200
201 enum power_supply_usb_type {
202 POWER_SUPPLY_USB_TYPE_UNKNOWN = 0,
203 POWER_SUPPLY_USB_TYPE_SDP, /* Standard Downstream Port */
204 POWER_SUPPLY_USB_TYPE_DCP, /* Dedicated Charging Port */
205 POWER_SUPPLY_USB_TYPE_CDP, /* Charging Downstream Port */
206 POWER_SUPPLY_USB_TYPE_ACA, /* Accessory Charger Adapters */
207 POWER_SUPPLY_USB_TYPE_C, /* Type C Port */
208 POWER_SUPPLY_USB_TYPE_PD, /* Power Delivery Port */
209 POWER_SUPPLY_USB_TYPE_PD_DRP, /* PD Dual Role Port */
210 POWER_SUPPLY_USB_TYPE_PD_PPS, /* PD Programmable Power Supply */
211 POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */
212 };
213
214 enum power_supply_charge_behaviour {
215 POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO = 0,
216 POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE,
217 POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE_AWAKE,
218 POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE,
219 };
220
221 enum power_supply_notifier_events {
222 PSY_EVENT_PROP_CHANGED,
223 };
224
225 union power_supply_propval {
226 int intval;
227 const char *strval;
228 };
229
230 struct device_node;
231 struct power_supply;
232
233 /* Run-time specific power supply configuration */
234 struct power_supply_config {
235 struct fwnode_handle *fwnode;
236
237 /* Driver private data */
238 void *drv_data;
239
240 /* Device specific sysfs attributes */
241 const struct attribute_group **attr_grp;
242
243 char **supplied_to;
244 size_t num_supplicants;
245
246 bool no_wakeup_source;
247 };
248
249 /* Description of power supply */
250 struct power_supply_desc {
251 const char *name;
252 enum power_supply_type type;
253 u8 charge_behaviours;
254 u32 charge_types;
255 u32 usb_types;
256 const enum power_supply_property *properties;
257 size_t num_properties;
258
259 /*
260 * Functions for drivers implementing power supply class.
261 * These shouldn't be called directly by other drivers for accessing
262 * this power supply. Instead use power_supply_*() functions (for
263 * example power_supply_get_property()).
264 */
265 int (*get_property)(struct power_supply *psy,
266 enum power_supply_property psp,
267 union power_supply_propval *val);
268 int (*set_property)(struct power_supply *psy,
269 enum power_supply_property psp,
270 const union power_supply_propval *val);
271 /*
272 * property_is_writeable() will be called during registration
273 * of power supply. If this happens during device probe then it must
274 * not access internal data of device (because probe did not end).
275 */
276 int (*property_is_writeable)(struct power_supply *psy,
277 enum power_supply_property psp);
278 void (*external_power_changed)(struct power_supply *psy);
279
280 /*
281 * Set if thermal zone should not be created for this power supply.
282 * For example for virtual supplies forwarding calls to actual
283 * sensors or other supplies.
284 */
285 bool no_thermal;
286 /* For APM emulation, think legacy userspace. */
287 int use_for_apm;
288 };
289
290 struct power_supply_ext {
291 const char *const name;
292 u8 charge_behaviours;
293 u32 charge_types;
294 const enum power_supply_property *properties;
295 size_t num_properties;
296
297 int (*get_property)(struct power_supply *psy,
298 const struct power_supply_ext *ext,
299 void *data,
300 enum power_supply_property psp,
301 union power_supply_propval *val);
302 int (*set_property)(struct power_supply *psy,
303 const struct power_supply_ext *ext,
304 void *data,
305 enum power_supply_property psp,
306 const union power_supply_propval *val);
307 int (*property_is_writeable)(struct power_supply *psy,
308 const struct power_supply_ext *ext,
309 void *data,
310 enum power_supply_property psp);
311 };
312
313 struct power_supply {
314 const struct power_supply_desc *desc;
315
316 char **supplied_to;
317 size_t num_supplicants;
318
319 char **supplied_from;
320 size_t num_supplies;
321
322 /* Driver private data */
323 void *drv_data;
324
325 /* private */
326 struct device dev;
327 struct work_struct changed_work;
328 struct delayed_work deferred_register_work;
329 spinlock_t changed_lock;
330 bool changed;
331 bool update_groups;
332 bool initialized;
333 bool removing;
334 atomic_t use_cnt;
335 struct power_supply_battery_info *battery_info;
336 struct rw_semaphore extensions_sem; /* protects "extensions" */
337 struct list_head extensions;
338 #ifdef CONFIG_THERMAL
339 struct thermal_zone_device *tzd;
340 struct thermal_cooling_device *tcd;
341 #endif
342
343 #ifdef CONFIG_LEDS_TRIGGERS
344 struct led_trigger *trig;
345 struct led_trigger *charging_trig;
346 struct led_trigger *full_trig;
347 struct led_trigger *charging_blink_full_solid_trig;
348 struct led_trigger *charging_orange_full_green_trig;
349 #endif
350 };
351
352 #define dev_to_psy(__dev) container_of_const(__dev, struct power_supply, dev)
353
354 /*
355 * This is recommended structure to specify static power supply parameters.
356 * Generic one, parametrizable for different power supplies. Power supply
357 * class itself does not use it, but that's what implementing most platform
358 * drivers, should try reuse for consistency.
359 */
360
361 struct power_supply_info {
362 const char *name;
363 int technology;
364 int voltage_max_design;
365 int voltage_min_design;
366 int charge_full_design;
367 int charge_empty_design;
368 int energy_full_design;
369 int energy_empty_design;
370 int use_for_apm;
371 };
372
373 struct power_supply_battery_ocv_table {
374 int ocv; /* microVolts */
375 int capacity; /* percent */
376 };
377
378 struct power_supply_resistance_temp_table {
379 int temp; /* celsius */
380 int resistance; /* internal resistance percent */
381 };
382
383 struct power_supply_vbat_ri_table {
384 int vbat_uv; /* Battery voltage in microvolt */
385 int ri_uohm; /* Internal resistance in microohm */
386 };
387
388 /**
389 * struct power_supply_maintenance_charge_table - setting for maintenace charging
390 * @charge_current_max_ua: maintenance charging current that is used to keep
391 * the charge of the battery full as current is consumed after full charging.
392 * The corresponding charge_voltage_max_uv is used as a safeguard: when we
393 * reach this voltage the maintenance charging current is turned off. It is
394 * turned back on if we fall below this voltage.
395 * @charge_voltage_max_uv: maintenance charging voltage that is usually a bit
396 * lower than the constant_charge_voltage_max_uv. We can apply this settings
397 * charge_current_max_ua until we get back up to this voltage.
398 * @safety_timer_minutes: maintenance charging safety timer, with an expiry
399 * time in minutes. We will only use maintenance charging in this setting
400 * for a certain amount of time, then we will first move to the next
401 * maintenance charge current and voltage pair in respective array and wait
402 * for the next safety timer timeout, or, if we reached the last maintencance
403 * charging setting, disable charging until we reach
404 * charge_restart_voltage_uv and restart ordinary CC/CV charging from there.
405 * These timers should be chosen to align with the typical discharge curve
406 * for the battery.
407 *
408 * Ordinary CC/CV charging will stop charging when the charge current goes
409 * below charge_term_current_ua, and then restart it (if the device is still
410 * plugged into the charger) at charge_restart_voltage_uv. This happens in most
411 * consumer products because the power usage while connected to a charger is
412 * not zero, and devices are not manufactured to draw power directly from the
413 * charger: instead they will at all times dissipate the battery a little, like
414 * the power used in standby mode. This will over time give a charge graph
415 * such as this:
416 *
417 * Energy
418 * ^ ... ... ... ... ... ... ...
419 * | . . . . . . . . . . . . .
420 * | .. . .. . .. . .. . .. . .. . ..
421 * |. .. .. .. .. .. ..
422 * +-------------------------------------------------------------------> t
423 *
424 * Practically this means that the Li-ions are wandering back and forth in the
425 * battery and this causes degeneration of the battery anode and cathode.
426 * To prolong the life of the battery, maintenance charging is applied after
427 * reaching charge_term_current_ua to hold up the charge in the battery while
428 * consuming power, thus lowering the wear on the battery:
429 *
430 * Energy
431 * ^ .......................................
432 * | . ......................
433 * | ..
434 * |.
435 * +-------------------------------------------------------------------> t
436 *
437 * Maintenance charging uses the voltages from this table: a table of settings
438 * is traversed using a slightly lower current and voltage than what is used for
439 * CC/CV charging. The maintenance charging will for safety reasons not go on
440 * indefinately: we lower the current and voltage with successive maintenance
441 * settings, then disable charging completely after we reach the last one,
442 * and after that we do not restart charging until we reach
443 * charge_restart_voltage_uv (see struct power_supply_battery_info) and restart
444 * ordinary CC/CV charging from there.
445 *
446 * As an example, a Samsung EB425161LA Lithium-Ion battery is CC/CV charged
447 * at 900mA to 4340mV, then maintenance charged at 600mA and 4150mV for up to
448 * 60 hours, then maintenance charged at 600mA and 4100mV for up to 200 hours.
449 * After this the charge cycle is restarted waiting for
450 * charge_restart_voltage_uv.
451 *
452 * For most mobile electronics this type of maintenance charging is enough for
453 * the user to disconnect the device and make use of it before both maintenance
454 * charging cycles are complete, if the current and voltage has been chosen
455 * appropriately. These need to be determined from battery discharge curves
456 * and expected standby current.
457 *
458 * If the voltage anyway drops to charge_restart_voltage_uv during maintenance
459 * charging, ordinary CC/CV charging is restarted. This can happen if the
460 * device is e.g. actively used during charging, so more current is drawn than
461 * the expected stand-by current. Also overvoltage protection will be applied
462 * as usual.
463 */
464 struct power_supply_maintenance_charge_table {
465 int charge_current_max_ua;
466 int charge_voltage_max_uv;
467 int charge_safety_timer_minutes;
468 };
469
470 #define POWER_SUPPLY_OCV_TEMP_MAX 20
471
472 /**
473 * struct power_supply_battery_info - information about batteries
474 * @technology: from the POWER_SUPPLY_TECHNOLOGY_* enum
475 * @energy_full_design_uwh: energy content when fully charged in microwatt
476 * hours
477 * @charge_full_design_uah: charge content when fully charged in microampere
478 * hours
479 * @voltage_min_design_uv: minimum voltage across the poles when the battery
480 * is at minimum voltage level in microvolts. If the voltage drops below this
481 * level the battery will need precharging when using CC/CV charging.
482 * @voltage_max_design_uv: voltage across the poles when the battery is fully
483 * charged in microvolts. This is the "nominal voltage" i.e. the voltage
484 * printed on the label of the battery.
485 * @tricklecharge_current_ua: the tricklecharge current used when trickle
486 * charging the battery in microamperes. This is the charging phase when the
487 * battery is completely empty and we need to carefully trickle in some
488 * charge until we reach the precharging voltage.
489 * @precharge_current_ua: current to use in the precharge phase in microamperes,
490 * the precharge rate is limited by limiting the current to this value.
491 * @precharge_voltage_max_uv: the maximum voltage allowed when precharging in
492 * microvolts. When we pass this voltage we will nominally switch over to the
493 * CC (constant current) charging phase defined by constant_charge_current_ua
494 * and constant_charge_voltage_max_uv.
495 * @charge_term_current_ua: when the current in the CV (constant voltage)
496 * charging phase drops below this value in microamperes the charging will
497 * terminate completely and not restart until the voltage over the battery
498 * poles reach charge_restart_voltage_uv unless we use maintenance charging.
499 * @charge_restart_voltage_uv: when the battery has been fully charged by
500 * CC/CV charging and charging has been disabled, and the voltage subsequently
501 * drops below this value in microvolts, the charging will be restarted
502 * (typically using CV charging).
503 * @overvoltage_limit_uv: If the voltage exceeds the nominal voltage
504 * voltage_max_design_uv and we reach this voltage level, all charging must
505 * stop and emergency procedures take place, such as shutting down the system
506 * in some cases.
507 * @constant_charge_current_max_ua: current in microamperes to use in the CC
508 * (constant current) charging phase. The charging rate is limited
509 * by this current. This is the main charging phase and as the current is
510 * constant into the battery the voltage slowly ascends to
511 * constant_charge_voltage_max_uv.
512 * @constant_charge_voltage_max_uv: voltage in microvolts signifying the end of
513 * the CC (constant current) charging phase and the beginning of the CV
514 * (constant voltage) charging phase.
515 * @maintenance_charge: an array of maintenance charging settings to be used
516 * after the main CC/CV charging phase is complete.
517 * @maintenance_charge_size: the number of maintenance charging settings in
518 * maintenance_charge.
519 * @alert_low_temp_charge_current_ua: The charging current to use if the battery
520 * enters low alert temperature, i.e. if the internal temperature is between
521 * temp_alert_min and temp_min. No matter the charging phase, this
522 * and alert_high_temp_charge_voltage_uv will be applied.
523 * @alert_low_temp_charge_voltage_uv: Same as alert_low_temp_charge_current_ua,
524 * but for the charging voltage.
525 * @alert_high_temp_charge_current_ua: The charging current to use if the
526 * battery enters high alert temperature, i.e. if the internal temperature is
527 * between temp_alert_max and temp_max. No matter the charging phase, this
528 * and alert_high_temp_charge_voltage_uv will be applied, usually lowering
529 * the charging current as an evasive manouver.
530 * @alert_high_temp_charge_voltage_uv: Same as
531 * alert_high_temp_charge_current_ua, but for the charging voltage.
532 * @factory_internal_resistance_uohm: the internal resistance of the battery
533 * at fabrication time, expressed in microohms. This resistance will vary
534 * depending on the lifetime and charge of the battery, so this is just a
535 * nominal ballpark figure. This internal resistance is given for the state
536 * when the battery is discharging.
537 * @factory_internal_resistance_charging_uohm: the internal resistance of the
538 * battery at fabrication time while charging, expressed in microohms.
539 * The charging process will affect the internal resistance of the battery
540 * so this value provides a better resistance under these circumstances.
541 * This resistance will vary depending on the lifetime and charge of the
542 * battery, so this is just a nominal ballpark figure.
543 * @ocv_temp: array indicating the open circuit voltage (OCV) capacity
544 * temperature indices. This is an array of temperatures in degrees Celsius
545 * indicating which capacity table to use for a certain temperature, since
546 * the capacity for reasons of chemistry will be different at different
547 * temperatures. Determining capacity is a multivariate problem and the
548 * temperature is the first variable we determine.
549 * @temp_ambient_alert_min: the battery will go outside of operating conditions
550 * when the ambient temperature goes below this temperature in degrees
551 * Celsius.
552 * @temp_ambient_alert_max: the battery will go outside of operating conditions
553 * when the ambient temperature goes above this temperature in degrees
554 * Celsius.
555 * @temp_alert_min: the battery should issue an alert if the internal
556 * temperature goes below this temperature in degrees Celsius.
557 * @temp_alert_max: the battery should issue an alert if the internal
558 * temperature goes above this temperature in degrees Celsius.
559 * @temp_min: the battery will go outside of operating conditions when
560 * the internal temperature goes below this temperature in degrees Celsius.
561 * Normally this means the system should shut down.
562 * @temp_max: the battery will go outside of operating conditions when
563 * the internal temperature goes above this temperature in degrees Celsius.
564 * Normally this means the system should shut down.
565 * @ocv_table: for each entry in ocv_temp there is a corresponding entry in
566 * ocv_table and a size for each entry in ocv_table_size. These arrays
567 * determine the capacity in percent in relation to the voltage in microvolts
568 * at the indexed temperature.
569 * @ocv_table_size: for each entry in ocv_temp this array is giving the size of
570 * each entry in the array of capacity arrays in ocv_table.
571 * @resist_table: this is a table that correlates a battery temperature to the
572 * expected internal resistance at this temperature. The resistance is given
573 * as a percentage of factory_internal_resistance_uohm. Knowing the
574 * resistance of the battery is usually necessary for calculating the open
575 * circuit voltage (OCV) that is then used with the ocv_table to calculate
576 * the capacity of the battery. The resist_table must be ordered descending
577 * by temperature: highest temperature with lowest resistance first, lowest
578 * temperature with highest resistance last.
579 * @resist_table_size: the number of items in the resist_table.
580 * @vbat2ri_discharging: this is a table that correlates Battery voltage (VBAT)
581 * to internal resistance (Ri). The resistance is given in microohm for the
582 * corresponding voltage in microvolts. The internal resistance is used to
583 * determine the open circuit voltage so that we can determine the capacity
584 * of the battery. These voltages to resistance tables apply when the battery
585 * is discharging. The table must be ordered descending by voltage: highest
586 * voltage first.
587 * @vbat2ri_discharging_size: the number of items in the vbat2ri_discharging
588 * table.
589 * @vbat2ri_charging: same function as vbat2ri_discharging but for the state
590 * when the battery is charging. Being under charge changes the battery's
591 * internal resistance characteristics so a separate table is needed.*
592 * The table must be ordered descending by voltage: highest voltage first.
593 * @vbat2ri_charging_size: the number of items in the vbat2ri_charging
594 * table.
595 * @bti_resistance_ohm: The Battery Type Indicator (BIT) nominal resistance
596 * in ohms for this battery, if an identification resistor is mounted
597 * between a third battery terminal and ground. This scheme is used by a lot
598 * of mobile device batteries.
599 * @bti_resistance_tolerance: The tolerance in percent of the BTI resistance,
600 * for example 10 for +/- 10%, if the bti_resistance is set to 7000 and the
601 * tolerance is 10% we will detect a proper battery if the BTI resistance
602 * is between 6300 and 7700 Ohm.
603 *
604 * This is the recommended struct to manage static battery parameters,
605 * populated by power_supply_get_battery_info(). Most platform drivers should
606 * use these for consistency.
607 *
608 * Its field names must correspond to elements in enum power_supply_property.
609 * The default field value is -EINVAL or NULL for pointers.
610 *
611 * CC/CV CHARGING:
612 *
613 * The charging parameters here assume a CC/CV charging scheme. This method
614 * is most common with Lithium Ion batteries (other methods are possible) and
615 * looks as follows:
616 *
617 * ^ Battery voltage
618 * | --- overvoltage_limit_uv
619 * |
620 * | ...................................................
621 * | .. constant_charge_voltage_max_uv
622 * | ..
623 * | .
624 * | .
625 * | .
626 * | .
627 * | .
628 * | .. precharge_voltage_max_uv
629 * | ..
630 * |. (trickle charging)
631 * +------------------------------------------------------------------> time
632 *
633 * ^ Current into the battery
634 * |
635 * | ............. constant_charge_current_max_ua
636 * | . .
637 * | . .
638 * | . .
639 * | . .
640 * | . ..
641 * | . ....
642 * | . .....
643 * | ... precharge_current_ua ....... charge_term_current_ua
644 * | . .
645 * | . .
646 * |.... tricklecharge_current_ua .
647 * | .
648 * +-----------------------------------------------------------------> time
649 *
650 * These diagrams are synchronized on time and the voltage and current
651 * follow each other.
652 *
653 * With CC/CV charging commence over time like this for an empty battery:
654 *
655 * 1. When the battery is completely empty it may need to be charged with
656 * an especially small current so that electrons just "trickle in",
657 * this is the tricklecharge_current_ua.
658 *
659 * 2. Next a small initial pre-charge current (precharge_current_ua)
660 * is applied if the voltage is below precharge_voltage_max_uv until we
661 * reach precharge_voltage_max_uv. CAUTION: in some texts this is referred
662 * to as "trickle charging" but the use in the Linux kernel is different
663 * see below!
664 *
665 * 3. Then the main charging current is applied, which is called the constant
666 * current (CC) phase. A current regulator is set up to allow
667 * constant_charge_current_max_ua of current to flow into the battery.
668 * The chemical reaction in the battery will make the voltage go up as
669 * charge goes into the battery. This current is applied until we reach
670 * the constant_charge_voltage_max_uv voltage.
671 *
672 * 4. At this voltage we switch over to the constant voltage (CV) phase. This
673 * means we allow current to go into the battery, but we keep the voltage
674 * fixed. This current will continue to charge the battery while keeping
675 * the voltage the same. A chemical reaction in the battery goes on
676 * storing energy without affecting the voltage. Over time the current
677 * will slowly drop and when we reach charge_term_current_ua we will
678 * end the constant voltage phase.
679 *
680 * After this the battery is fully charged, and if we do not support maintenance
681 * charging, the charging will not restart until power dissipation makes the
682 * voltage fall so that we reach charge_restart_voltage_uv and at this point
683 * we restart charging at the appropriate phase, usually this will be inside
684 * the CV phase.
685 *
686 * If we support maintenance charging the voltage is however kept high after
687 * the CV phase with a very low current. This is meant to let the same charge
688 * go in for usage while the charger is still connected, mainly for
689 * dissipation for the power consuming entity while connected to the
690 * charger.
691 *
692 * All charging MUST terminate if the overvoltage_limit_uv is ever reached.
693 * Overcharging Lithium Ion cells can be DANGEROUS and lead to fire or
694 * explosions.
695 *
696 * DETERMINING BATTERY CAPACITY:
697 *
698 * Several members of the struct deal with trying to determine the remaining
699 * capacity in the battery, usually as a percentage of charge. In practice
700 * many chargers uses a so-called fuel gauge or coloumb counter that measure
701 * how much charge goes into the battery and how much goes out (+/- leak
702 * consumption). This does not help if we do not know how much capacity the
703 * battery has to begin with, such as when it is first used or was taken out
704 * and charged in a separate charger. Therefore many capacity algorithms use
705 * the open circuit voltage with a look-up table to determine the rough
706 * capacity of the battery. The open circuit voltage can be conceptualized
707 * with an ideal voltage source (V) in series with an internal resistance (Ri)
708 * like this:
709 *
710 * +-------> IBAT >----------------+
711 * | ^ |
712 * [ ] Ri | |
713 * | | VBAT |
714 * o <---------- | |
715 * +| ^ | [ ] Rload
716 * .---. | | |
717 * | V | | OCV | |
718 * '---' | | |
719 * | | | |
720 * GND +-------------------------------+
721 *
722 * If we disconnect the load (here simplified as a fixed resistance Rload)
723 * and measure VBAT with a infinite impedance voltage meter we will get
724 * VBAT = OCV and this assumption is sometimes made even under load, assuming
725 * Rload is insignificant. However this will be of dubious quality because the
726 * load is rarely that small and Ri is strongly nonlinear depending on
727 * temperature and how much capacity is left in the battery due to the
728 * chemistry involved.
729 *
730 * In many practical applications we cannot just disconnect the battery from
731 * the load, so instead we often try to measure the instantaneous IBAT (the
732 * current out from the battery), estimate the Ri and thus calculate the
733 * voltage drop over Ri and compensate like this:
734 *
735 * OCV = VBAT - (IBAT * Ri)
736 *
737 * The tables vbat2ri_discharging and vbat2ri_charging are used to determine
738 * (by interpolation) the Ri from the VBAT under load. These curves are highly
739 * nonlinear and may need many datapoints but can be found in datasheets for
740 * some batteries. This gives the compensated open circuit voltage (OCV) for
741 * the battery even under load. Using this method will also compensate for
742 * temperature changes in the environment: this will also make the internal
743 * resistance change, and it will affect the VBAT under load, so correlating
744 * VBAT to Ri takes both remaining capacity and temperature into consideration.
745 *
746 * Alternatively a manufacturer can specify how the capacity of the battery
747 * is dependent on the battery temperature which is the main factor affecting
748 * Ri. As we know all checmical reactions are faster when it is warm and slower
749 * when it is cold. You can put in 1500mAh and only get 800mAh out before the
750 * voltage drops too low for example. This effect is also highly nonlinear and
751 * the purpose of the table resist_table: this will take a temperature and
752 * tell us how big percentage of Ri the specified temperature correlates to.
753 * Usually we have 100% of the factory_internal_resistance_uohm at 25 degrees
754 * Celsius.
755 *
756 * The power supply class itself doesn't use this struct as of now.
757 */
758
759 struct power_supply_battery_info {
760 unsigned int technology;
761 int energy_full_design_uwh;
762 int charge_full_design_uah;
763 int voltage_min_design_uv;
764 int voltage_max_design_uv;
765 int tricklecharge_current_ua;
766 int precharge_current_ua;
767 int precharge_voltage_max_uv;
768 int charge_term_current_ua;
769 int charge_restart_voltage_uv;
770 int overvoltage_limit_uv;
771 int constant_charge_current_max_ua;
772 int constant_charge_voltage_max_uv;
773 const struct power_supply_maintenance_charge_table *maintenance_charge;
774 int maintenance_charge_size;
775 int alert_low_temp_charge_current_ua;
776 int alert_low_temp_charge_voltage_uv;
777 int alert_high_temp_charge_current_ua;
778 int alert_high_temp_charge_voltage_uv;
779 int factory_internal_resistance_uohm;
780 int factory_internal_resistance_charging_uohm;
781 int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];
782 int temp_ambient_alert_min;
783 int temp_ambient_alert_max;
784 int temp_alert_min;
785 int temp_alert_max;
786 int temp_min;
787 int temp_max;
788 const struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX];
789 int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX];
790 const struct power_supply_resistance_temp_table *resist_table;
791 int resist_table_size;
792 const struct power_supply_vbat_ri_table *vbat2ri_discharging;
793 int vbat2ri_discharging_size;
794 const struct power_supply_vbat_ri_table *vbat2ri_charging;
795 int vbat2ri_charging_size;
796 int bti_resistance_ohm;
797 int bti_resistance_tolerance;
798 };
799
800 extern int power_supply_reg_notifier(struct notifier_block *nb);
801 extern void power_supply_unreg_notifier(struct notifier_block *nb);
802 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
803 extern struct power_supply *power_supply_get_by_name(const char *name);
804 extern void power_supply_put(struct power_supply *psy);
805 #else
power_supply_put(struct power_supply * psy)806 static inline void power_supply_put(struct power_supply *psy) {}
power_supply_get_by_name(const char * name)807 static inline struct power_supply *power_supply_get_by_name(const char *name)
808 { return NULL; }
809 #endif
810 extern struct power_supply *power_supply_get_by_reference(struct fwnode_handle *fwnode,
811 const char *property);
812 extern struct power_supply *devm_power_supply_get_by_reference(
813 struct device *dev, const char *property);
814
815 extern const enum power_supply_property power_supply_battery_info_properties[];
816 extern const size_t power_supply_battery_info_properties_size;
817 extern int power_supply_get_battery_info(struct power_supply *psy,
818 struct power_supply_battery_info **info_out);
819 extern void power_supply_put_battery_info(struct power_supply *psy,
820 struct power_supply_battery_info *info);
821 extern bool power_supply_battery_info_has_prop(struct power_supply_battery_info *info,
822 enum power_supply_property psp);
823 extern int power_supply_battery_info_get_prop(struct power_supply_battery_info *info,
824 enum power_supply_property psp,
825 union power_supply_propval *val);
826 extern int power_supply_ocv2cap_simple(const struct power_supply_battery_ocv_table *table,
827 int table_len, int ocv);
828 extern const struct power_supply_battery_ocv_table *
829 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
830 int temp, int *table_len);
831 extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
832 int ocv, int temp);
833 extern int
834 power_supply_temp2resist_simple(const struct power_supply_resistance_temp_table *table,
835 int table_len, int temp);
836 extern int power_supply_vbat2ri(struct power_supply_battery_info *info,
837 int vbat_uv, bool charging);
838 extern const struct power_supply_maintenance_charge_table *
839 power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index);
840 extern bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
841 int resistance);
842 extern void power_supply_changed(struct power_supply *psy);
843 extern int power_supply_am_i_supplied(struct power_supply *psy);
844 int power_supply_get_property_from_supplier(struct power_supply *psy,
845 enum power_supply_property psp,
846 union power_supply_propval *val);
847
848 static inline bool
power_supply_supports_maintenance_charging(struct power_supply_battery_info * info)849 power_supply_supports_maintenance_charging(struct power_supply_battery_info *info)
850 {
851 const struct power_supply_maintenance_charge_table *mt;
852
853 mt = power_supply_get_maintenance_charging_setting(info, 0);
854
855 return (mt != NULL);
856 }
857
858 static inline bool
power_supply_supports_vbat2ri(struct power_supply_battery_info * info)859 power_supply_supports_vbat2ri(struct power_supply_battery_info *info)
860 {
861 return ((info->vbat2ri_discharging != NULL) &&
862 info->vbat2ri_discharging_size > 0);
863 }
864
865 static inline bool
power_supply_supports_temp2ri(struct power_supply_battery_info * info)866 power_supply_supports_temp2ri(struct power_supply_battery_info *info)
867 {
868 return ((info->resist_table != NULL) &&
869 info->resist_table_size > 0);
870 }
871
872 #ifdef CONFIG_POWER_SUPPLY
873 extern int power_supply_is_system_supplied(void);
874 #else
power_supply_is_system_supplied(void)875 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
876 #endif
877
878 extern int power_supply_get_property(struct power_supply *psy,
879 enum power_supply_property psp,
880 union power_supply_propval *val);
881 int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
882 union power_supply_propval *val);
883 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
884 extern int power_supply_set_property(struct power_supply *psy,
885 enum power_supply_property psp,
886 const union power_supply_propval *val);
887 int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
888 const union power_supply_propval *val);
889 #else
power_supply_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)890 static inline int power_supply_set_property(struct power_supply *psy,
891 enum power_supply_property psp,
892 const union power_supply_propval *val)
893 { return 0; }
power_supply_set_property_direct(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)894 static inline int power_supply_set_property_direct(struct power_supply *psy,
895 enum power_supply_property psp,
896 const union power_supply_propval *val)
897 { return 0; }
898 #endif
899 extern void power_supply_external_power_changed(struct power_supply *psy);
900
901 extern struct power_supply *__must_check
902 power_supply_register(struct device *parent,
903 const struct power_supply_desc *desc,
904 const struct power_supply_config *cfg);
905 extern struct power_supply *__must_check
906 devm_power_supply_register(struct device *parent,
907 const struct power_supply_desc *desc,
908 const struct power_supply_config *cfg);
909 extern void power_supply_unregister(struct power_supply *psy);
910 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
911
912 extern int __must_check
913 power_supply_register_extension(struct power_supply *psy,
914 const struct power_supply_ext *ext,
915 struct device *dev,
916 void *data);
917 extern void power_supply_unregister_extension(struct power_supply *psy,
918 const struct power_supply_ext *ext);
919
920 #define to_power_supply(device) container_of(device, struct power_supply, dev)
921
922 extern void *power_supply_get_drvdata(struct power_supply *psy);
923 extern int power_supply_for_each_psy(void *data, int (*fn)(struct power_supply *psy, void *data));
924
power_supply_is_amp_property(enum power_supply_property psp)925 static inline bool power_supply_is_amp_property(enum power_supply_property psp)
926 {
927 switch (psp) {
928 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
929 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
930 case POWER_SUPPLY_PROP_CHARGE_FULL:
931 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
932 case POWER_SUPPLY_PROP_CHARGE_NOW:
933 case POWER_SUPPLY_PROP_CHARGE_AVG:
934 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
935 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
936 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
937 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
938 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
939 case POWER_SUPPLY_PROP_CURRENT_MAX:
940 case POWER_SUPPLY_PROP_CURRENT_NOW:
941 case POWER_SUPPLY_PROP_CURRENT_AVG:
942 case POWER_SUPPLY_PROP_CURRENT_BOOT:
943 return true;
944 default:
945 break;
946 }
947
948 return false;
949 }
950
power_supply_is_watt_property(enum power_supply_property psp)951 static inline bool power_supply_is_watt_property(enum power_supply_property psp)
952 {
953 switch (psp) {
954 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
955 case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
956 case POWER_SUPPLY_PROP_ENERGY_FULL:
957 case POWER_SUPPLY_PROP_ENERGY_EMPTY:
958 case POWER_SUPPLY_PROP_ENERGY_NOW:
959 case POWER_SUPPLY_PROP_ENERGY_AVG:
960 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
961 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
962 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
963 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
964 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
965 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
966 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
967 case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
968 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
969 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
970 case POWER_SUPPLY_PROP_POWER_NOW:
971 return true;
972 default:
973 break;
974 }
975
976 return false;
977 }
978
979 #ifdef CONFIG_SYSFS
980 ssize_t power_supply_charge_behaviour_show(struct device *dev,
981 unsigned int available_behaviours,
982 enum power_supply_charge_behaviour behaviour,
983 char *buf);
984
985 int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf);
986 ssize_t power_supply_charge_types_show(struct device *dev,
987 unsigned int available_types,
988 enum power_supply_charge_type current_type,
989 char *buf);
990 int power_supply_charge_types_parse(unsigned int available_types, const char *buf);
991 #else
992 static inline
power_supply_charge_behaviour_show(struct device * dev,unsigned int available_behaviours,enum power_supply_charge_behaviour behaviour,char * buf)993 ssize_t power_supply_charge_behaviour_show(struct device *dev,
994 unsigned int available_behaviours,
995 enum power_supply_charge_behaviour behaviour,
996 char *buf)
997 {
998 return -EOPNOTSUPP;
999 }
1000
power_supply_charge_behaviour_parse(unsigned int available_behaviours,const char * buf)1001 static inline int power_supply_charge_behaviour_parse(unsigned int available_behaviours,
1002 const char *buf)
1003 {
1004 return -EOPNOTSUPP;
1005 }
1006
1007 static inline
power_supply_charge_types_show(struct device * dev,unsigned int available_types,enum power_supply_charge_type current_type,char * buf)1008 ssize_t power_supply_charge_types_show(struct device *dev,
1009 unsigned int available_types,
1010 enum power_supply_charge_type current_type,
1011 char *buf)
1012 {
1013 return -EOPNOTSUPP;
1014 }
1015
power_supply_charge_types_parse(unsigned int available_types,const char * buf)1016 static inline int power_supply_charge_types_parse(unsigned int available_types, const char *buf)
1017 {
1018 return -EOPNOTSUPP;
1019 }
1020 #endif
1021
1022 #endif /* __LINUX_POWER_SUPPLY_H__ */
1023