power_supply_class.rst 10 KB

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