xref: /OK3568_Linux_fs/kernel/Documentation/power/power_supply_class.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun========================
2*4882a593SmuzhiyunLinux power supply class
3*4882a593Smuzhiyun========================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunSynopsis
6*4882a593Smuzhiyun~~~~~~~~
7*4882a593SmuzhiyunPower supply class used to represent battery, UPS, AC or DC power supply
8*4882a593Smuzhiyunproperties to user-space.
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunIt defines core set of attributes, which should be applicable to (almost)
11*4882a593Smuzhiyunevery power supply out there. Attributes are available via sysfs and uevent
12*4882a593Smuzhiyuninterfaces.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunEach attribute has well defined meaning, up to unit of measure used. While
15*4882a593Smuzhiyunthe attributes provided are believed to be universally applicable to any
16*4882a593Smuzhiyunpower supply, specific monitoring hardware may not be able to provide them
17*4882a593Smuzhiyunall, so any of them may be skipped.
18*4882a593Smuzhiyun
19*4882a593SmuzhiyunPower supply class is extensible, and allows to define drivers own attributes.
20*4882a593SmuzhiyunThe core attribute set is subject to the standard Linux evolution (i.e.
21*4882a593Smuzhiyunif it will be found that some attribute is applicable to many power supply
22*4882a593Smuzhiyuntypes or their drivers, it can be added to the core set).
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunIt also integrates with LED framework, for the purpose of providing
25*4882a593Smuzhiyuntypically expected feedback of battery charging/fully charged status and
26*4882a593SmuzhiyunAC/USB power supply online status. (Note that specific details of the
27*4882a593Smuzhiyunindication (including whether to use it at all) are fully controllable by
28*4882a593Smuzhiyunuser and/or specific machine defaults, per design principles of LED
29*4882a593Smuzhiyunframework).
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun
32*4882a593SmuzhiyunAttributes/properties
33*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~
34*4882a593SmuzhiyunPower supply class has predefined set of attributes, this eliminates code
35*4882a593Smuzhiyunduplication across drivers. Power supply class insist on reusing its
36*4882a593Smuzhiyunpredefined attributes *and* their units.
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunSo, userspace gets predictable set of attributes and their units for any
39*4882a593Smuzhiyunkind of power supply, and can process/present them to a user in consistent
40*4882a593Smuzhiyunmanner. Results for different power supplies and machines are also directly
41*4882a593Smuzhiyuncomparable.
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunSee drivers/power/supply/ds2760_battery.c and drivers/power/supply/pda_power.c
44*4882a593Smuzhiyunfor the example how to declare and handle attributes.
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunUnits
48*4882a593Smuzhiyun~~~~~
49*4882a593SmuzhiyunQuoting include/linux/power_supply.h:
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun  All voltages, currents, charges, energies, time and temperatures in µV,
52*4882a593Smuzhiyun  µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
53*4882a593Smuzhiyun  stated. It's driver's job to convert its raw values to units in which
54*4882a593Smuzhiyun  this class operates.
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunAttributes/properties detailed
58*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun+--------------------------------------------------------------------------+
61*4882a593Smuzhiyun|               **Charge/Energy/Capacity - how to not confuse**            |
62*4882a593Smuzhiyun+--------------------------------------------------------------------------+
63*4882a593Smuzhiyun| **Because both "charge" (µAh) and "energy" (µWh) represents "capacity"   |
64*4882a593Smuzhiyun| of battery, this class distinguish these terms. Don't mix them!**        |
65*4882a593Smuzhiyun|                                                                          |
66*4882a593Smuzhiyun| - `CHARGE_*`                                                             |
67*4882a593Smuzhiyun|	attributes represents capacity in µAh only.                        |
68*4882a593Smuzhiyun| - `ENERGY_*`                                                             |
69*4882a593Smuzhiyun|	attributes represents capacity in µWh only.                        |
70*4882a593Smuzhiyun| - `CAPACITY`                                                             |
71*4882a593Smuzhiyun|	attribute represents capacity in *percents*, from 0 to 100.        |
72*4882a593Smuzhiyun+--------------------------------------------------------------------------+
73*4882a593Smuzhiyun
74*4882a593SmuzhiyunPostfixes:
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun_AVG
77*4882a593Smuzhiyun  *hardware* averaged value, use it if your hardware is really able to
78*4882a593Smuzhiyun  report averaged values.
79*4882a593Smuzhiyun_NOW
80*4882a593Smuzhiyun  momentary/instantaneous values.
81*4882a593Smuzhiyun
82*4882a593SmuzhiyunSTATUS
83*4882a593Smuzhiyun  this attribute represents operating status (charging, full,
84*4882a593Smuzhiyun  discharging (i.e. powering a load), etc.). This corresponds to
85*4882a593Smuzhiyun  `BATTERY_STATUS_*` values, as defined in battery.h.
86*4882a593Smuzhiyun
87*4882a593SmuzhiyunCHARGE_TYPE
88*4882a593Smuzhiyun  batteries can typically charge at different rates.
89*4882a593Smuzhiyun  This defines trickle and fast charges.  For batteries that
90*4882a593Smuzhiyun  are already charged or discharging, 'n/a' can be displayed (or
91*4882a593Smuzhiyun  'unknown', if the status is not known).
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunAUTHENTIC
94*4882a593Smuzhiyun  indicates the power supply (battery or charger) connected
95*4882a593Smuzhiyun  to the platform is authentic(1) or non authentic(0).
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunHEALTH
98*4882a593Smuzhiyun  represents health of the battery, values corresponds to
99*4882a593Smuzhiyun  POWER_SUPPLY_HEALTH_*, defined in battery.h.
100*4882a593Smuzhiyun
101*4882a593SmuzhiyunVOLTAGE_OCV
102*4882a593Smuzhiyun  open circuit voltage of the battery.
103*4882a593Smuzhiyun
104*4882a593SmuzhiyunVOLTAGE_MAX_DESIGN, VOLTAGE_MIN_DESIGN
105*4882a593Smuzhiyun  design values for maximal and minimal power supply voltages.
106*4882a593Smuzhiyun  Maximal/minimal means values of voltages when battery considered
107*4882a593Smuzhiyun  "full"/"empty" at normal conditions. Yes, there is no direct relation
108*4882a593Smuzhiyun  between voltage and battery capacity, but some dumb
109*4882a593Smuzhiyun  batteries use voltage for very approximated calculation of capacity.
110*4882a593Smuzhiyun  Battery driver also can use this attribute just to inform userspace
111*4882a593Smuzhiyun  about maximal and minimal voltage thresholds of a given battery.
112*4882a593Smuzhiyun
113*4882a593SmuzhiyunVOLTAGE_MAX, VOLTAGE_MIN
114*4882a593Smuzhiyun  same as _DESIGN voltage values except that these ones should be used
115*4882a593Smuzhiyun  if hardware could only guess (measure and retain) the thresholds of a
116*4882a593Smuzhiyun  given power supply.
117*4882a593Smuzhiyun
118*4882a593SmuzhiyunVOLTAGE_BOOT
119*4882a593Smuzhiyun  Reports the voltage measured during boot
120*4882a593Smuzhiyun
121*4882a593SmuzhiyunCURRENT_BOOT
122*4882a593Smuzhiyun  Reports the current measured during boot
123*4882a593Smuzhiyun
124*4882a593SmuzhiyunCHARGE_FULL_DESIGN, CHARGE_EMPTY_DESIGN
125*4882a593Smuzhiyun  design charge values, when battery considered full/empty.
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunENERGY_FULL_DESIGN, ENERGY_EMPTY_DESIGN
128*4882a593Smuzhiyun  same as above but for energy.
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunCHARGE_FULL, CHARGE_EMPTY
131*4882a593Smuzhiyun  These attributes means "last remembered value of charge when battery
132*4882a593Smuzhiyun  became full/empty". It also could mean "value of charge when battery
133*4882a593Smuzhiyun  considered full/empty at given conditions (temperature, age)".
134*4882a593Smuzhiyun  I.e. these attributes represents real thresholds, not design values.
135*4882a593Smuzhiyun
136*4882a593SmuzhiyunENERGY_FULL, ENERGY_EMPTY
137*4882a593Smuzhiyun  same as above but for energy.
138*4882a593Smuzhiyun
139*4882a593SmuzhiyunCHARGE_COUNTER
140*4882a593Smuzhiyun  the current charge counter (in µAh).  This could easily
141*4882a593Smuzhiyun  be negative; there is no empty or full value.  It is only useful for
142*4882a593Smuzhiyun  relative, time-based measurements.
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunPRECHARGE_CURRENT
145*4882a593Smuzhiyun  the maximum charge current during precharge phase of charge cycle
146*4882a593Smuzhiyun  (typically 20% of battery capacity).
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunCHARGE_TERM_CURRENT
149*4882a593Smuzhiyun  Charge termination current. The charge cycle terminates when battery
150*4882a593Smuzhiyun  voltage is above recharge threshold, and charge current is below
151*4882a593Smuzhiyun  this setting (typically 10% of battery capacity).
152*4882a593Smuzhiyun
153*4882a593SmuzhiyunCONSTANT_CHARGE_CURRENT
154*4882a593Smuzhiyun  constant charge current programmed by charger.
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun
157*4882a593SmuzhiyunCONSTANT_CHARGE_CURRENT_MAX
158*4882a593Smuzhiyun  maximum charge current supported by the power supply object.
159*4882a593Smuzhiyun
160*4882a593SmuzhiyunCONSTANT_CHARGE_VOLTAGE
161*4882a593Smuzhiyun  constant charge voltage programmed by charger.
162*4882a593SmuzhiyunCONSTANT_CHARGE_VOLTAGE_MAX
163*4882a593Smuzhiyun  maximum charge voltage supported by the power supply object.
164*4882a593Smuzhiyun
165*4882a593SmuzhiyunINPUT_CURRENT_LIMIT
166*4882a593Smuzhiyun  input current limit programmed by charger. Indicates
167*4882a593Smuzhiyun  the current drawn from a charging source.
168*4882a593SmuzhiyunINPUT_VOLTAGE_LIMIT
169*4882a593Smuzhiyun  input voltage limit programmed by charger. Indicates
170*4882a593Smuzhiyun  the voltage limit from a charging source.
171*4882a593SmuzhiyunINPUT_POWER_LIMIT
172*4882a593Smuzhiyun  input power limit programmed by charger. Indicates
173*4882a593Smuzhiyun  the power limit from a charging source.
174*4882a593Smuzhiyun
175*4882a593SmuzhiyunCHARGE_CONTROL_LIMIT
176*4882a593Smuzhiyun  current charge control limit setting
177*4882a593SmuzhiyunCHARGE_CONTROL_LIMIT_MAX
178*4882a593Smuzhiyun  maximum charge control limit setting
179*4882a593Smuzhiyun
180*4882a593SmuzhiyunCALIBRATE
181*4882a593Smuzhiyun  battery or coulomb counter calibration status
182*4882a593Smuzhiyun
183*4882a593SmuzhiyunCAPACITY
184*4882a593Smuzhiyun  capacity in percents.
185*4882a593SmuzhiyunCAPACITY_ALERT_MIN
186*4882a593Smuzhiyun  minimum capacity alert value in percents.
187*4882a593SmuzhiyunCAPACITY_ALERT_MAX
188*4882a593Smuzhiyun  maximum capacity alert value in percents.
189*4882a593SmuzhiyunCAPACITY_LEVEL
190*4882a593Smuzhiyun  capacity level. This corresponds to POWER_SUPPLY_CAPACITY_LEVEL_*.
191*4882a593Smuzhiyun
192*4882a593SmuzhiyunTEMP
193*4882a593Smuzhiyun  temperature of the power supply.
194*4882a593SmuzhiyunTEMP_ALERT_MIN
195*4882a593Smuzhiyun  minimum battery temperature alert.
196*4882a593SmuzhiyunTEMP_ALERT_MAX
197*4882a593Smuzhiyun  maximum battery temperature alert.
198*4882a593SmuzhiyunTEMP_AMBIENT
199*4882a593Smuzhiyun  ambient temperature.
200*4882a593SmuzhiyunTEMP_AMBIENT_ALERT_MIN
201*4882a593Smuzhiyun  minimum ambient temperature alert.
202*4882a593SmuzhiyunTEMP_AMBIENT_ALERT_MAX
203*4882a593Smuzhiyun  maximum ambient temperature alert.
204*4882a593SmuzhiyunTEMP_MIN
205*4882a593Smuzhiyun  minimum operatable temperature
206*4882a593SmuzhiyunTEMP_MAX
207*4882a593Smuzhiyun  maximum operatable temperature
208*4882a593Smuzhiyun
209*4882a593SmuzhiyunTIME_TO_EMPTY
210*4882a593Smuzhiyun  seconds left for battery to be considered empty
211*4882a593Smuzhiyun  (i.e. while battery powers a load)
212*4882a593SmuzhiyunTIME_TO_FULL
213*4882a593Smuzhiyun  seconds left for battery to be considered full
214*4882a593Smuzhiyun  (i.e. while battery is charging)
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun
217*4882a593SmuzhiyunBattery <-> external power supply interaction
218*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219*4882a593SmuzhiyunOften power supplies are acting as supplies and supplicants at the same
220*4882a593Smuzhiyuntime. Batteries are good example. So, batteries usually care if they're
221*4882a593Smuzhiyunexternally powered or not.
222*4882a593Smuzhiyun
223*4882a593SmuzhiyunFor that case, power supply class implements notification mechanism for
224*4882a593Smuzhiyunbatteries.
225*4882a593Smuzhiyun
226*4882a593SmuzhiyunExternal power supply (AC) lists supplicants (batteries) names in
227*4882a593Smuzhiyun"supplied_to" struct member, and each power_supply_changed() call
228*4882a593Smuzhiyunissued by external power supply will notify supplicants via
229*4882a593Smuzhiyunexternal_power_changed callback.
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun
232*4882a593SmuzhiyunDevicetree battery characteristics
233*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234*4882a593SmuzhiyunDrivers should call power_supply_get_battery_info() to obtain battery
235*4882a593Smuzhiyuncharacteristics from a devicetree battery node, defined in
236*4882a593SmuzhiyunDocumentation/devicetree/bindings/power/supply/battery.txt. This is
237*4882a593Smuzhiyunimplemented in drivers/power/supply/bq27xxx_battery.c.
238*4882a593Smuzhiyun
239*4882a593SmuzhiyunProperties in struct power_supply_battery_info and their counterparts in the
240*4882a593Smuzhiyunbattery node have names corresponding to elements in enum power_supply_property,
241*4882a593Smuzhiyunfor naming consistency between sysfs attributes and battery node properties.
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun
244*4882a593SmuzhiyunQA
245*4882a593Smuzhiyun~~
246*4882a593Smuzhiyun
247*4882a593SmuzhiyunQ:
248*4882a593Smuzhiyun   Where is POWER_SUPPLY_PROP_XYZ attribute?
249*4882a593SmuzhiyunA:
250*4882a593Smuzhiyun   If you cannot find attribute suitable for your driver needs, feel free
251*4882a593Smuzhiyun   to add it and send patch along with your driver.
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun   The attributes available currently are the ones currently provided by the
254*4882a593Smuzhiyun   drivers written.
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun   Good candidates to add in future: model/part#, cycle_time, manufacturer,
257*4882a593Smuzhiyun   etc.
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun
260*4882a593SmuzhiyunQ:
261*4882a593Smuzhiyun   I have some very specific attribute (e.g. battery color), should I add
262*4882a593Smuzhiyun   this attribute to standard ones?
263*4882a593SmuzhiyunA:
264*4882a593Smuzhiyun   Most likely, no. Such attribute can be placed in the driver itself, if
265*4882a593Smuzhiyun   it is useful. Of course, if the attribute in question applicable to
266*4882a593Smuzhiyun   large set of batteries, provided by many drivers, and/or comes from
267*4882a593Smuzhiyun   some general battery specification/standard, it may be a candidate to
268*4882a593Smuzhiyun   be added to the core attribute set.
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun
271*4882a593SmuzhiyunQ:
272*4882a593Smuzhiyun   Suppose, my battery monitoring chip/firmware does not provides capacity
273*4882a593Smuzhiyun   in percents, but provides charge_{now,full,empty}. Should I calculate
274*4882a593Smuzhiyun   percentage capacity manually, inside the driver, and register CAPACITY
275*4882a593Smuzhiyun   attribute? The same question about time_to_empty/time_to_full.
276*4882a593SmuzhiyunA:
277*4882a593Smuzhiyun   Most likely, no. This class is designed to export properties which are
278*4882a593Smuzhiyun   directly measurable by the specific hardware available.
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun   Inferring not available properties using some heuristics or mathematical
281*4882a593Smuzhiyun   model is not subject of work for a battery driver. Such functionality
282*4882a593Smuzhiyun   should be factored out, and in fact, apm_power, the driver to serve
283*4882a593Smuzhiyun   legacy APM API on top of power supply class, uses a simple heuristic of
284*4882a593Smuzhiyun   approximating remaining battery capacity based on its charge, current,
285*4882a593Smuzhiyun   voltage and so on. But full-fledged battery model is likely not subject
286*4882a593Smuzhiyun   for kernel at all, as it would require floating point calculation to deal
287*4882a593Smuzhiyun   with things like differential equations and Kalman filters. This is
288*4882a593Smuzhiyun   better be handled by batteryd/libbattery, yet to be written.
289