power_supply.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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_USB_TYPE,
  138. POWER_SUPPLY_PROP_SCOPE,
  139. POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
  140. POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
  141. POWER_SUPPLY_PROP_CALIBRATE,
  142. /* Properties of type `const char *' */
  143. POWER_SUPPLY_PROP_MODEL_NAME,
  144. POWER_SUPPLY_PROP_MANUFACTURER,
  145. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  146. };
  147. enum power_supply_type {
  148. POWER_SUPPLY_TYPE_UNKNOWN = 0,
  149. POWER_SUPPLY_TYPE_BATTERY,
  150. POWER_SUPPLY_TYPE_UPS,
  151. POWER_SUPPLY_TYPE_MAINS,
  152. POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port */
  153. POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */
  154. POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */
  155. POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */
  156. POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */
  157. POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */
  158. POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */
  159. POWER_SUPPLY_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */
  160. };
  161. enum power_supply_usb_type {
  162. POWER_SUPPLY_USB_TYPE_UNKNOWN = 0,
  163. POWER_SUPPLY_USB_TYPE_SDP, /* Standard Downstream Port */
  164. POWER_SUPPLY_USB_TYPE_DCP, /* Dedicated Charging Port */
  165. POWER_SUPPLY_USB_TYPE_CDP, /* Charging Downstream Port */
  166. POWER_SUPPLY_USB_TYPE_ACA, /* Accessory Charger Adapters */
  167. POWER_SUPPLY_USB_TYPE_C, /* Type C Port */
  168. POWER_SUPPLY_USB_TYPE_PD, /* Power Delivery Port */
  169. POWER_SUPPLY_USB_TYPE_PD_DRP, /* PD Dual Role Port */
  170. POWER_SUPPLY_USB_TYPE_PD_PPS, /* PD Programmable Power Supply */
  171. POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */
  172. };
  173. enum power_supply_notifier_events {
  174. PSY_EVENT_PROP_CHANGED,
  175. };
  176. union power_supply_propval {
  177. int intval;
  178. const char *strval;
  179. };
  180. struct device_node;
  181. struct power_supply;
  182. /* Run-time specific power supply configuration */
  183. struct power_supply_config {
  184. struct device_node *of_node;
  185. struct fwnode_handle *fwnode;
  186. /* Driver private data */
  187. void *drv_data;
  188. char **supplied_to;
  189. size_t num_supplicants;
  190. };
  191. /* Description of power supply */
  192. struct power_supply_desc {
  193. const char *name;
  194. enum power_supply_type type;
  195. enum power_supply_usb_type *usb_types;
  196. size_t num_usb_types;
  197. enum power_supply_property *properties;
  198. size_t num_properties;
  199. /*
  200. * Functions for drivers implementing power supply class.
  201. * These shouldn't be called directly by other drivers for accessing
  202. * this power supply. Instead use power_supply_*() functions (for
  203. * example power_supply_get_property()).
  204. */
  205. int (*get_property)(struct power_supply *psy,
  206. enum power_supply_property psp,
  207. union power_supply_propval *val);
  208. int (*set_property)(struct power_supply *psy,
  209. enum power_supply_property psp,
  210. const union power_supply_propval *val);
  211. /*
  212. * property_is_writeable() will be called during registration
  213. * of power supply. If this happens during device probe then it must
  214. * not access internal data of device (because probe did not end).
  215. */
  216. int (*property_is_writeable)(struct power_supply *psy,
  217. enum power_supply_property psp);
  218. void (*external_power_changed)(struct power_supply *psy);
  219. void (*set_charged)(struct power_supply *psy);
  220. /*
  221. * Set if thermal zone should not be created for this power supply.
  222. * For example for virtual supplies forwarding calls to actual
  223. * sensors or other supplies.
  224. */
  225. bool no_thermal;
  226. /* For APM emulation, think legacy userspace. */
  227. int use_for_apm;
  228. };
  229. struct power_supply {
  230. const struct power_supply_desc *desc;
  231. char **supplied_to;
  232. size_t num_supplicants;
  233. char **supplied_from;
  234. size_t num_supplies;
  235. struct device_node *of_node;
  236. /* Driver private data */
  237. void *drv_data;
  238. /* private */
  239. struct device dev;
  240. struct work_struct changed_work;
  241. struct delayed_work deferred_register_work;
  242. spinlock_t changed_lock;
  243. bool changed;
  244. bool initialized;
  245. bool removing;
  246. atomic_t use_cnt;
  247. #ifdef CONFIG_THERMAL
  248. struct thermal_zone_device *tzd;
  249. struct thermal_cooling_device *tcd;
  250. #endif
  251. #ifdef CONFIG_LEDS_TRIGGERS
  252. struct led_trigger *charging_full_trig;
  253. char *charging_full_trig_name;
  254. struct led_trigger *charging_trig;
  255. char *charging_trig_name;
  256. struct led_trigger *full_trig;
  257. char *full_trig_name;
  258. struct led_trigger *online_trig;
  259. char *online_trig_name;
  260. struct led_trigger *charging_blink_full_solid_trig;
  261. char *charging_blink_full_solid_trig_name;
  262. #endif
  263. };
  264. /*
  265. * This is recommended structure to specify static power supply parameters.
  266. * Generic one, parametrizable for different power supplies. Power supply
  267. * class itself does not use it, but that's what implementing most platform
  268. * drivers, should try reuse for consistency.
  269. */
  270. struct power_supply_info {
  271. const char *name;
  272. int technology;
  273. int voltage_max_design;
  274. int voltage_min_design;
  275. int charge_full_design;
  276. int charge_empty_design;
  277. int energy_full_design;
  278. int energy_empty_design;
  279. int use_for_apm;
  280. };
  281. /*
  282. * This is the recommended struct to manage static battery parameters,
  283. * populated by power_supply_get_battery_info(). Most platform drivers should
  284. * use these for consistency.
  285. * Its field names must correspond to elements in enum power_supply_property.
  286. * The default field value is -EINVAL.
  287. * Power supply class itself doesn't use this.
  288. */
  289. struct power_supply_battery_info {
  290. int energy_full_design_uwh; /* microWatt-hours */
  291. int charge_full_design_uah; /* microAmp-hours */
  292. int voltage_min_design_uv; /* microVolts */
  293. int precharge_current_ua; /* microAmps */
  294. int charge_term_current_ua; /* microAmps */
  295. int constant_charge_current_max_ua; /* microAmps */
  296. int constant_charge_voltage_max_uv; /* microVolts */
  297. };
  298. extern struct atomic_notifier_head power_supply_notifier;
  299. extern int power_supply_reg_notifier(struct notifier_block *nb);
  300. extern void power_supply_unreg_notifier(struct notifier_block *nb);
  301. extern struct power_supply *power_supply_get_by_name(const char *name);
  302. extern void power_supply_put(struct power_supply *psy);
  303. #ifdef CONFIG_OF
  304. extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
  305. const char *property);
  306. extern struct power_supply *devm_power_supply_get_by_phandle(
  307. struct device *dev, const char *property);
  308. #else /* !CONFIG_OF */
  309. static inline struct power_supply *
  310. power_supply_get_by_phandle(struct device_node *np, const char *property)
  311. { return NULL; }
  312. static inline struct power_supply *
  313. devm_power_supply_get_by_phandle(struct device *dev, const char *property)
  314. { return NULL; }
  315. #endif /* CONFIG_OF */
  316. extern int power_supply_get_battery_info(struct power_supply *psy,
  317. struct power_supply_battery_info *info);
  318. extern void power_supply_changed(struct power_supply *psy);
  319. extern int power_supply_am_i_supplied(struct power_supply *psy);
  320. extern int power_supply_set_input_current_limit_from_supplier(
  321. struct power_supply *psy);
  322. extern int power_supply_set_battery_charged(struct power_supply *psy);
  323. #ifdef CONFIG_POWER_SUPPLY
  324. extern int power_supply_is_system_supplied(void);
  325. #else
  326. static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
  327. #endif
  328. extern int power_supply_get_property(struct power_supply *psy,
  329. enum power_supply_property psp,
  330. union power_supply_propval *val);
  331. extern int power_supply_set_property(struct power_supply *psy,
  332. enum power_supply_property psp,
  333. const union power_supply_propval *val);
  334. extern int power_supply_property_is_writeable(struct power_supply *psy,
  335. enum power_supply_property psp);
  336. extern void power_supply_external_power_changed(struct power_supply *psy);
  337. extern struct power_supply *__must_check
  338. power_supply_register(struct device *parent,
  339. const struct power_supply_desc *desc,
  340. const struct power_supply_config *cfg);
  341. extern struct power_supply *__must_check
  342. power_supply_register_no_ws(struct device *parent,
  343. const struct power_supply_desc *desc,
  344. const struct power_supply_config *cfg);
  345. extern struct power_supply *__must_check
  346. devm_power_supply_register(struct device *parent,
  347. const struct power_supply_desc *desc,
  348. const struct power_supply_config *cfg);
  349. extern struct power_supply *__must_check
  350. devm_power_supply_register_no_ws(struct device *parent,
  351. const struct power_supply_desc *desc,
  352. const struct power_supply_config *cfg);
  353. extern void power_supply_unregister(struct power_supply *psy);
  354. extern int power_supply_powers(struct power_supply *psy, struct device *dev);
  355. #define to_power_supply(device) container_of(device, struct power_supply, dev)
  356. extern void *power_supply_get_drvdata(struct power_supply *psy);
  357. /* For APM emulation, think legacy userspace. */
  358. extern struct class *power_supply_class;
  359. static inline bool power_supply_is_amp_property(enum power_supply_property psp)
  360. {
  361. switch (psp) {
  362. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  363. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  364. case POWER_SUPPLY_PROP_CHARGE_FULL:
  365. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  366. case POWER_SUPPLY_PROP_CHARGE_NOW:
  367. case POWER_SUPPLY_PROP_CHARGE_AVG:
  368. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  369. case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
  370. case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
  371. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  372. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  373. case POWER_SUPPLY_PROP_CURRENT_MAX:
  374. case POWER_SUPPLY_PROP_CURRENT_NOW:
  375. case POWER_SUPPLY_PROP_CURRENT_AVG:
  376. case POWER_SUPPLY_PROP_CURRENT_BOOT:
  377. return 1;
  378. default:
  379. break;
  380. }
  381. return 0;
  382. }
  383. static inline bool power_supply_is_watt_property(enum power_supply_property psp)
  384. {
  385. switch (psp) {
  386. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  387. case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
  388. case POWER_SUPPLY_PROP_ENERGY_FULL:
  389. case POWER_SUPPLY_PROP_ENERGY_EMPTY:
  390. case POWER_SUPPLY_PROP_ENERGY_NOW:
  391. case POWER_SUPPLY_PROP_ENERGY_AVG:
  392. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  393. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  394. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  395. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  396. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  397. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  398. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  399. case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
  400. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  401. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  402. case POWER_SUPPLY_PROP_POWER_NOW:
  403. return 1;
  404. default:
  405. break;
  406. }
  407. return 0;
  408. }
  409. #endif /* __LINUX_POWER_SUPPLY_H__ */