power_supply.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Universal power supply monitor class
  3. *
  4. * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
  5. * Copyright © 2004 Szabolcs Gyurko
  6. * Copyright © 2003 Ian Molton <spyro@f2s.com>
  7. *
  8. * Modified: 2004, Oct Szabolcs Gyurko
  9. *
  10. * You may use this code as per GPL version 2
  11. */
  12. #ifndef __LINUX_POWER_SUPPLY_H__
  13. #define __LINUX_POWER_SUPPLY_H__
  14. #include <linux/device.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/leds.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/notifier.h>
  19. /*
  20. * All voltages, currents, charges, energies, time and temperatures in uV,
  21. * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
  22. * stated. It's driver's job to convert its raw values to units in which
  23. * this class operates.
  24. */
  25. /*
  26. * For systems where the charger determines the maximum battery capacity
  27. * the min and max fields should be used to present these values to user
  28. * space. Unused/unknown fields will not appear in sysfs.
  29. */
  30. enum {
  31. POWER_SUPPLY_STATUS_UNKNOWN = 0,
  32. POWER_SUPPLY_STATUS_CHARGING,
  33. POWER_SUPPLY_STATUS_DISCHARGING,
  34. POWER_SUPPLY_STATUS_NOT_CHARGING,
  35. POWER_SUPPLY_STATUS_FULL,
  36. };
  37. enum {
  38. POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,
  39. POWER_SUPPLY_CHARGE_TYPE_NONE,
  40. POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
  41. POWER_SUPPLY_CHARGE_TYPE_FAST,
  42. };
  43. enum {
  44. POWER_SUPPLY_HEALTH_UNKNOWN = 0,
  45. POWER_SUPPLY_HEALTH_GOOD,
  46. POWER_SUPPLY_HEALTH_OVERHEAT,
  47. POWER_SUPPLY_HEALTH_DEAD,
  48. POWER_SUPPLY_HEALTH_OVERVOLTAGE,
  49. POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
  50. POWER_SUPPLY_HEALTH_COLD,
  51. POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE,
  52. POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE,
  53. };
  54. enum {
  55. POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
  56. POWER_SUPPLY_TECHNOLOGY_NiMH,
  57. POWER_SUPPLY_TECHNOLOGY_LION,
  58. POWER_SUPPLY_TECHNOLOGY_LIPO,
  59. POWER_SUPPLY_TECHNOLOGY_LiFe,
  60. POWER_SUPPLY_TECHNOLOGY_NiCd,
  61. POWER_SUPPLY_TECHNOLOGY_LiMn,
  62. };
  63. enum {
  64. POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
  65. POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
  66. POWER_SUPPLY_CAPACITY_LEVEL_LOW,
  67. POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
  68. POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
  69. POWER_SUPPLY_CAPACITY_LEVEL_FULL,
  70. };
  71. enum {
  72. POWER_SUPPLY_SCOPE_UNKNOWN = 0,
  73. POWER_SUPPLY_SCOPE_SYSTEM,
  74. POWER_SUPPLY_SCOPE_DEVICE,
  75. };
  76. enum power_supply_property {
  77. /* Properties of type `int' */
  78. POWER_SUPPLY_PROP_STATUS = 0,
  79. POWER_SUPPLY_PROP_CHARGE_TYPE,
  80. POWER_SUPPLY_PROP_HEALTH,
  81. POWER_SUPPLY_PROP_PRESENT,
  82. POWER_SUPPLY_PROP_ONLINE,
  83. POWER_SUPPLY_PROP_AUTHENTIC,
  84. POWER_SUPPLY_PROP_TECHNOLOGY,
  85. POWER_SUPPLY_PROP_CYCLE_COUNT,
  86. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  87. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  88. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  89. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  90. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  91. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  92. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  93. POWER_SUPPLY_PROP_VOLTAGE_BOOT,
  94. POWER_SUPPLY_PROP_CURRENT_MAX,
  95. POWER_SUPPLY_PROP_CURRENT_NOW,
  96. POWER_SUPPLY_PROP_CURRENT_AVG,
  97. POWER_SUPPLY_PROP_CURRENT_BOOT,
  98. POWER_SUPPLY_PROP_POWER_NOW,
  99. POWER_SUPPLY_PROP_POWER_AVG,
  100. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  101. POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  102. POWER_SUPPLY_PROP_CHARGE_FULL,
  103. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  104. POWER_SUPPLY_PROP_CHARGE_NOW,
  105. POWER_SUPPLY_PROP_CHARGE_AVG,
  106. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  107. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  108. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  109. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  110. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  111. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
  112. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
  113. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  114. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  115. POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
  116. POWER_SUPPLY_PROP_ENERGY_FULL,
  117. POWER_SUPPLY_PROP_ENERGY_EMPTY,
  118. POWER_SUPPLY_PROP_ENERGY_NOW,
  119. POWER_SUPPLY_PROP_ENERGY_AVG,
  120. POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
  121. POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
  122. POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
  123. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  124. POWER_SUPPLY_PROP_TEMP,
  125. POWER_SUPPLY_PROP_TEMP_MAX,
  126. POWER_SUPPLY_PROP_TEMP_MIN,
  127. POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
  128. POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
  129. POWER_SUPPLY_PROP_TEMP_AMBIENT,
  130. POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
  131. POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
  132. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  133. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  134. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  135. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  136. POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
  137. POWER_SUPPLY_PROP_SCOPE,
  138. POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
  139. POWER_SUPPLY_PROP_CALIBRATE,
  140. /* Properties of type `const char *' */
  141. POWER_SUPPLY_PROP_MODEL_NAME,
  142. POWER_SUPPLY_PROP_MANUFACTURER,
  143. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  144. };
  145. enum power_supply_type {
  146. POWER_SUPPLY_TYPE_UNKNOWN = 0,
  147. POWER_SUPPLY_TYPE_BATTERY,
  148. POWER_SUPPLY_TYPE_UPS,
  149. POWER_SUPPLY_TYPE_MAINS,
  150. POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port */
  151. POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */
  152. POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */
  153. POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */
  154. POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */
  155. POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */
  156. POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */
  157. };
  158. enum power_supply_notifier_events {
  159. PSY_EVENT_PROP_CHANGED,
  160. };
  161. union power_supply_propval {
  162. int intval;
  163. const char *strval;
  164. };
  165. struct device_node;
  166. struct power_supply;
  167. /* Run-time specific power supply configuration */
  168. struct power_supply_config {
  169. struct device_node *of_node;
  170. /* Driver private data */
  171. void *drv_data;
  172. char **supplied_to;
  173. size_t num_supplicants;
  174. };
  175. /* Description of power supply */
  176. struct power_supply_desc {
  177. const char *name;
  178. enum power_supply_type type;
  179. enum power_supply_property *properties;
  180. size_t num_properties;
  181. /*
  182. * Functions for drivers implementing power supply class.
  183. * These shouldn't be called directly by other drivers for accessing
  184. * this power supply. Instead use power_supply_*() functions (for
  185. * example power_supply_get_property()).
  186. */
  187. int (*get_property)(struct power_supply *psy,
  188. enum power_supply_property psp,
  189. union power_supply_propval *val);
  190. int (*set_property)(struct power_supply *psy,
  191. enum power_supply_property psp,
  192. const union power_supply_propval *val);
  193. /*
  194. * property_is_writeable() will be called during registration
  195. * of power supply. If this happens during device probe then it must
  196. * not access internal data of device (because probe did not end).
  197. */
  198. int (*property_is_writeable)(struct power_supply *psy,
  199. enum power_supply_property psp);
  200. void (*external_power_changed)(struct power_supply *psy);
  201. void (*set_charged)(struct power_supply *psy);
  202. /*
  203. * Set if thermal zone should not be created for this power supply.
  204. * For example for virtual supplies forwarding calls to actual
  205. * sensors or other supplies.
  206. */
  207. bool no_thermal;
  208. /* For APM emulation, think legacy userspace. */
  209. int use_for_apm;
  210. };
  211. struct power_supply {
  212. const struct power_supply_desc *desc;
  213. char **supplied_to;
  214. size_t num_supplicants;
  215. char **supplied_from;
  216. size_t num_supplies;
  217. struct device_node *of_node;
  218. /* Driver private data */
  219. void *drv_data;
  220. /* private */
  221. struct device dev;
  222. struct work_struct changed_work;
  223. struct delayed_work deferred_register_work;
  224. spinlock_t changed_lock;
  225. bool changed;
  226. bool initialized;
  227. atomic_t use_cnt;
  228. #ifdef CONFIG_THERMAL
  229. struct thermal_zone_device *tzd;
  230. struct thermal_cooling_device *tcd;
  231. #endif
  232. #ifdef CONFIG_LEDS_TRIGGERS
  233. struct led_trigger *charging_full_trig;
  234. char *charging_full_trig_name;
  235. struct led_trigger *charging_trig;
  236. char *charging_trig_name;
  237. struct led_trigger *full_trig;
  238. char *full_trig_name;
  239. struct led_trigger *online_trig;
  240. char *online_trig_name;
  241. struct led_trigger *charging_blink_full_solid_trig;
  242. char *charging_blink_full_solid_trig_name;
  243. #endif
  244. };
  245. /*
  246. * This is recommended structure to specify static power supply parameters.
  247. * Generic one, parametrizable for different power supplies. Power supply
  248. * class itself does not use it, but that's what implementing most platform
  249. * drivers, should try reuse for consistency.
  250. */
  251. struct power_supply_info {
  252. const char *name;
  253. int technology;
  254. int voltage_max_design;
  255. int voltage_min_design;
  256. int charge_full_design;
  257. int charge_empty_design;
  258. int energy_full_design;
  259. int energy_empty_design;
  260. int use_for_apm;
  261. };
  262. extern struct atomic_notifier_head power_supply_notifier;
  263. extern int power_supply_reg_notifier(struct notifier_block *nb);
  264. extern void power_supply_unreg_notifier(struct notifier_block *nb);
  265. extern struct power_supply *power_supply_get_by_name(const char *name);
  266. extern void power_supply_put(struct power_supply *psy);
  267. #ifdef CONFIG_OF
  268. extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
  269. const char *property);
  270. extern struct power_supply *devm_power_supply_get_by_phandle(
  271. struct device *dev, const char *property);
  272. #else /* !CONFIG_OF */
  273. static inline struct power_supply *
  274. power_supply_get_by_phandle(struct device_node *np, const char *property)
  275. { return NULL; }
  276. static inline struct power_supply *
  277. devm_power_supply_get_by_phandle(struct device *dev, const char *property)
  278. { return NULL; }
  279. #endif /* CONFIG_OF */
  280. extern void power_supply_changed(struct power_supply *psy);
  281. extern int power_supply_am_i_supplied(struct power_supply *psy);
  282. extern int power_supply_set_battery_charged(struct power_supply *psy);
  283. #ifdef CONFIG_POWER_SUPPLY
  284. extern int power_supply_is_system_supplied(void);
  285. #else
  286. static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
  287. #endif
  288. extern int power_supply_get_property(struct power_supply *psy,
  289. enum power_supply_property psp,
  290. union power_supply_propval *val);
  291. extern int power_supply_set_property(struct power_supply *psy,
  292. enum power_supply_property psp,
  293. const union power_supply_propval *val);
  294. extern int power_supply_property_is_writeable(struct power_supply *psy,
  295. enum power_supply_property psp);
  296. extern void power_supply_external_power_changed(struct power_supply *psy);
  297. extern struct power_supply *__must_check
  298. power_supply_register(struct device *parent,
  299. const struct power_supply_desc *desc,
  300. const struct power_supply_config *cfg);
  301. extern struct power_supply *__must_check
  302. power_supply_register_no_ws(struct device *parent,
  303. const struct power_supply_desc *desc,
  304. const struct power_supply_config *cfg);
  305. extern struct power_supply *__must_check
  306. devm_power_supply_register(struct device *parent,
  307. const struct power_supply_desc *desc,
  308. const struct power_supply_config *cfg);
  309. extern struct power_supply *__must_check
  310. devm_power_supply_register_no_ws(struct device *parent,
  311. const struct power_supply_desc *desc,
  312. const struct power_supply_config *cfg);
  313. extern void power_supply_unregister(struct power_supply *psy);
  314. extern int power_supply_powers(struct power_supply *psy, struct device *dev);
  315. extern void *power_supply_get_drvdata(struct power_supply *psy);
  316. /* For APM emulation, think legacy userspace. */
  317. extern struct class *power_supply_class;
  318. static inline bool power_supply_is_amp_property(enum power_supply_property psp)
  319. {
  320. switch (psp) {
  321. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  322. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  323. case POWER_SUPPLY_PROP_CHARGE_FULL:
  324. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  325. case POWER_SUPPLY_PROP_CHARGE_NOW:
  326. case POWER_SUPPLY_PROP_CHARGE_AVG:
  327. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  328. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  329. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  330. case POWER_SUPPLY_PROP_CURRENT_MAX:
  331. case POWER_SUPPLY_PROP_CURRENT_NOW:
  332. case POWER_SUPPLY_PROP_CURRENT_AVG:
  333. case POWER_SUPPLY_PROP_CURRENT_BOOT:
  334. return 1;
  335. default:
  336. break;
  337. }
  338. return 0;
  339. }
  340. static inline bool power_supply_is_watt_property(enum power_supply_property psp)
  341. {
  342. switch (psp) {
  343. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  344. case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
  345. case POWER_SUPPLY_PROP_ENERGY_FULL:
  346. case POWER_SUPPLY_PROP_ENERGY_EMPTY:
  347. case POWER_SUPPLY_PROP_ENERGY_NOW:
  348. case POWER_SUPPLY_PROP_ENERGY_AVG:
  349. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  350. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  351. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  352. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  353. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  354. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  355. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  356. case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
  357. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  358. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  359. case POWER_SUPPLY_PROP_POWER_NOW:
  360. return 1;
  361. default:
  362. break;
  363. }
  364. return 0;
  365. }
  366. #endif /* __LINUX_POWER_SUPPLY_H__ */