power_supply_class.txt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. Linux power supply class
  2. ========================
  3. Synopsis
  4. ~~~~~~~~
  5. Power supply class used to represent battery, UPS, AC or DC power supply
  6. properties to user-space.
  7. It defines core set of attributes, which should be applicable to (almost)
  8. every power supply out there. Attributes are available via sysfs and uevent
  9. interfaces.
  10. Each attribute has well defined meaning, up to unit of measure used. While
  11. the attributes provided are believed to be universally applicable to any
  12. power supply, specific monitoring hardware may not be able to provide them
  13. all, so any of them may be skipped.
  14. Power supply class is extensible, and allows to define drivers own attributes.
  15. The core attribute set is subject to the standard Linux evolution (i.e.
  16. if it will be found that some attribute is applicable to many power supply
  17. types or their drivers, it can be added to the core set).
  18. It also integrates with LED framework, for the purpose of providing
  19. typically expected feedback of battery charging/fully charged status and
  20. AC/USB power supply online status. (Note that specific details of the
  21. indication (including whether to use it at all) are fully controllable by
  22. user and/or specific machine defaults, per design principles of LED
  23. framework).
  24. Attributes/properties
  25. ~~~~~~~~~~~~~~~~~~~~~
  26. Power supply class has predefined set of attributes, this eliminates code
  27. duplication across drivers. Power supply class insist on reusing its
  28. predefined attributes *and* their units.
  29. So, userspace gets predictable set of attributes and their units for any
  30. kind of power supply, and can process/present them to a user in consistent
  31. manner. Results for different power supplies and machines are also directly
  32. comparable.
  33. See drivers/power/supply/ds2760_battery.c and drivers/power/supply/pda_power.c
  34. for the example how to declare and handle attributes.
  35. Units
  36. ~~~~~
  37. Quoting include/linux/power_supply.h:
  38. All voltages, currents, charges, energies, time and temperatures in µV,
  39. µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
  40. stated. It's driver's job to convert its raw values to units in which
  41. this class operates.
  42. Attributes/properties detailed
  43. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44. ~ ~ ~ ~ ~ ~ ~ Charge/Energy/Capacity - how to not confuse ~ ~ ~ ~ ~ ~ ~
  45. ~ ~
  46. ~ Because both "charge" (µAh) and "energy" (µWh) represents "capacity" ~
  47. ~ of battery, this class distinguish these terms. Don't mix them! ~
  48. ~ ~
  49. ~ CHARGE_* attributes represents capacity in µAh only. ~
  50. ~ ENERGY_* attributes represents capacity in µWh only. ~
  51. ~ CAPACITY attribute represents capacity in *percents*, from 0 to 100. ~
  52. ~ ~
  53. ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  54. Postfixes:
  55. _AVG - *hardware* averaged value, use it if your hardware is really able to
  56. report averaged values.
  57. _NOW - momentary/instantaneous values.
  58. STATUS - this attribute represents operating status (charging, full,
  59. discharging (i.e. powering a load), etc.). This corresponds to
  60. BATTERY_STATUS_* values, as defined in battery.h.
  61. CHARGE_TYPE - batteries can typically charge at different rates.
  62. This defines trickle and fast charges. For batteries that
  63. are already charged or discharging, 'n/a' can be displayed (or
  64. 'unknown', if the status is not known).
  65. AUTHENTIC - indicates the power supply (battery or charger) connected
  66. to the platform is authentic(1) or non authentic(0).
  67. HEALTH - represents health of the battery, values corresponds to
  68. POWER_SUPPLY_HEALTH_*, defined in battery.h.
  69. VOLTAGE_OCV - open circuit voltage of the battery.
  70. VOLTAGE_MAX_DESIGN, VOLTAGE_MIN_DESIGN - design values for maximal and
  71. minimal power supply voltages. Maximal/minimal means values of voltages
  72. when battery considered "full"/"empty" at normal conditions. Yes, there is
  73. no direct relation between voltage and battery capacity, but some dumb
  74. batteries use voltage for very approximated calculation of capacity.
  75. Battery driver also can use this attribute just to inform userspace
  76. about maximal and minimal voltage thresholds of a given battery.
  77. VOLTAGE_MAX, VOLTAGE_MIN - same as _DESIGN voltage values except that
  78. these ones should be used if hardware could only guess (measure and
  79. retain) the thresholds of a given power supply.
  80. VOLTAGE_BOOT - Reports the voltage measured during boot
  81. CURRENT_BOOT - Reports the current measured during boot
  82. CHARGE_FULL_DESIGN, CHARGE_EMPTY_DESIGN - design charge values, when
  83. battery considered full/empty.
  84. ENERGY_FULL_DESIGN, ENERGY_EMPTY_DESIGN - same as above but for energy.
  85. CHARGE_FULL, CHARGE_EMPTY - These attributes means "last remembered value
  86. of charge when battery became full/empty". It also could mean "value of
  87. charge when battery considered full/empty at given conditions (temperature,
  88. age)". I.e. these attributes represents real thresholds, not design values.
  89. CHARGE_COUNTER - the current charge counter (in µAh). This could easily
  90. be negative; there is no empty or full value. It is only useful for
  91. relative, time-based measurements.
  92. CONSTANT_CHARGE_CURRENT - constant charge current programmed by charger.
  93. CONSTANT_CHARGE_CURRENT_MAX - maximum charge current supported by the
  94. power supply object.
  95. INPUT_CURRENT_LIMIT - input current limit programmed by charger. Indicates
  96. the current drawn from a charging source.
  97. CHARGE_TERM_CURRENT - Charge termination current used to detect the end of charge
  98. condition.
  99. CALIBRATE - battery or coulomb counter calibration status
  100. CONSTANT_CHARGE_VOLTAGE - constant charge voltage programmed by charger.
  101. CONSTANT_CHARGE_VOLTAGE_MAX - maximum charge voltage supported by the
  102. power supply object.
  103. CHARGE_CONTROL_LIMIT - current charge control limit setting
  104. CHARGE_CONTROL_LIMIT_MAX - maximum charge control limit setting
  105. ENERGY_FULL, ENERGY_EMPTY - same as above but for energy.
  106. CAPACITY - capacity in percents.
  107. CAPACITY_ALERT_MIN - minimum capacity alert value in percents.
  108. CAPACITY_ALERT_MAX - maximum capacity alert value in percents.
  109. CAPACITY_LEVEL - capacity level. This corresponds to
  110. POWER_SUPPLY_CAPACITY_LEVEL_*.
  111. TEMP - temperature of the power supply.
  112. TEMP_ALERT_MIN - minimum battery temperature alert.
  113. TEMP_ALERT_MAX - maximum battery temperature alert.
  114. TEMP_AMBIENT - ambient temperature.
  115. TEMP_AMBIENT_ALERT_MIN - minimum ambient temperature alert.
  116. TEMP_AMBIENT_ALERT_MAX - maximum ambient temperature alert.
  117. TEMP_MIN - minimum operatable temperature
  118. TEMP_MAX - maximum operatable temperature
  119. TIME_TO_EMPTY - seconds left for battery to be considered empty (i.e.
  120. while battery powers a load)
  121. TIME_TO_FULL - seconds left for battery to be considered full (i.e.
  122. while battery is charging)
  123. Battery <-> external power supply interaction
  124. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  125. Often power supplies are acting as supplies and supplicants at the same
  126. time. Batteries are good example. So, batteries usually care if they're
  127. externally powered or not.
  128. For that case, power supply class implements notification mechanism for
  129. batteries.
  130. External power supply (AC) lists supplicants (batteries) names in
  131. "supplied_to" struct member, and each power_supply_changed() call
  132. issued by external power supply will notify supplicants via
  133. external_power_changed callback.
  134. QA
  135. ~~
  136. Q: Where is POWER_SUPPLY_PROP_XYZ attribute?
  137. A: If you cannot find attribute suitable for your driver needs, feel free
  138. to add it and send patch along with your driver.
  139. The attributes available currently are the ones currently provided by the
  140. drivers written.
  141. Good candidates to add in future: model/part#, cycle_time, manufacturer,
  142. etc.
  143. Q: I have some very specific attribute (e.g. battery color), should I add
  144. this attribute to standard ones?
  145. A: Most likely, no. Such attribute can be placed in the driver itself, if
  146. it is useful. Of course, if the attribute in question applicable to
  147. large set of batteries, provided by many drivers, and/or comes from
  148. some general battery specification/standard, it may be a candidate to
  149. be added to the core attribute set.
  150. Q: Suppose, my battery monitoring chip/firmware does not provides capacity
  151. in percents, but provides charge_{now,full,empty}. Should I calculate
  152. percentage capacity manually, inside the driver, and register CAPACITY
  153. attribute? The same question about time_to_empty/time_to_full.
  154. A: Most likely, no. This class is designed to export properties which are
  155. directly measurable by the specific hardware available.
  156. Inferring not available properties using some heuristics or mathematical
  157. model is not subject of work for a battery driver. Such functionality
  158. should be factored out, and in fact, apm_power, the driver to serve
  159. legacy APM API on top of power supply class, uses a simple heuristic of
  160. approximating remaining battery capacity based on its charge, current,
  161. voltage and so on. But full-fledged battery model is likely not subject
  162. for kernel at all, as it would require floating point calculation to deal
  163. with things like differential equations and Kalman filters. This is
  164. better be handled by batteryd/libbattery, yet to be written.