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