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