power_supply.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/wakelock.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/leds.h>
  17. struct device;
  18. /*
  19. * All voltages, currents, charges, energies, time and temperatures in uV,
  20. * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
  21. * stated. It's driver's job to convert its raw values to units in which
  22. * this class operates.
  23. */
  24. /*
  25. * For systems where the charger determines the maximum battery capacity
  26. * the min and max fields should be used to present these values to user
  27. * space. Unused/unknown fields will not appear in sysfs.
  28. */
  29. enum {
  30. POWER_SUPPLY_STATUS_UNKNOWN = 0,
  31. POWER_SUPPLY_STATUS_CHARGING,
  32. POWER_SUPPLY_STATUS_DISCHARGING,
  33. POWER_SUPPLY_STATUS_NOT_CHARGING,
  34. POWER_SUPPLY_STATUS_FULL,
  35. };
  36. enum {
  37. POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,
  38. POWER_SUPPLY_CHARGE_TYPE_NONE,
  39. POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
  40. POWER_SUPPLY_CHARGE_TYPE_FAST,
  41. POWER_SUPPLY_CHARGE_TYPE_TAPER,
  42. POWER_SUPPLY_CHARGE_TYPE_SLOW,
  43. };
  44. enum {
  45. POWER_SUPPLY_HEALTH_UNKNOWN = 0,
  46. POWER_SUPPLY_HEALTH_GOOD,
  47. POWER_SUPPLY_HEALTH_OVERHEAT,
  48. POWER_SUPPLY_HEALTH_WARM,
  49. POWER_SUPPLY_HEALTH_DEAD,
  50. POWER_SUPPLY_HEALTH_OVERVOLTAGE,
  51. POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
  52. POWER_SUPPLY_HEALTH_COLD,
  53. POWER_SUPPLY_HEALTH_COOL,
  54. POWER_SUPPLY_HEALTH_UNDERVOLTAGE,
  55. POWER_SUPPLY_HEALTH_OVERHEATLIMIT,
  56. };
  57. enum {
  58. POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
  59. POWER_SUPPLY_TECHNOLOGY_NiMH,
  60. POWER_SUPPLY_TECHNOLOGY_LION,
  61. POWER_SUPPLY_TECHNOLOGY_LIPO,
  62. POWER_SUPPLY_TECHNOLOGY_LiFe,
  63. POWER_SUPPLY_TECHNOLOGY_NiCd,
  64. POWER_SUPPLY_TECHNOLOGY_LiMn,
  65. };
  66. enum {
  67. POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
  68. POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
  69. POWER_SUPPLY_CAPACITY_LEVEL_LOW,
  70. POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
  71. POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
  72. POWER_SUPPLY_CAPACITY_LEVEL_FULL,
  73. };
  74. enum {
  75. POWER_SUPPLY_SCOPE_UNKNOWN = 0,
  76. POWER_SUPPLY_SCOPE_SYSTEM,
  77. POWER_SUPPLY_SCOPE_DEVICE,
  78. };
  79. enum power_supply_property {
  80. /* Properties of type `int' */
  81. POWER_SUPPLY_PROP_STATUS = 0,
  82. POWER_SUPPLY_PROP_CHARGE_TYPE,
  83. POWER_SUPPLY_PROP_HEALTH,
  84. POWER_SUPPLY_PROP_PRESENT,
  85. POWER_SUPPLY_PROP_ONLINE,
  86. POWER_SUPPLY_PROP_AUTHENTIC,
  87. POWER_SUPPLY_PROP_CHARGING_ENABLED,
  88. POWER_SUPPLY_PROP_TECHNOLOGY,
  89. POWER_SUPPLY_PROP_CYCLE_COUNT,
  90. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  91. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  92. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  93. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  94. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  95. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  96. POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION,
  97. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  98. POWER_SUPPLY_PROP_CURRENT_MAX,
  99. POWER_SUPPLY_PROP_INPUT_CURRENT_MAX,
  100. POWER_SUPPLY_PROP_INPUT_CURRENT_TRIM,
  101. POWER_SUPPLY_PROP_INPUT_CURRENT_SETTLED,
  102. POWER_SUPPLY_PROP_VCHG_LOOP_DBC_BYPASS,
  103. POWER_SUPPLY_PROP_CURRENT_NOW,
  104. POWER_SUPPLY_PROP_CURRENT_AVG,
  105. POWER_SUPPLY_PROP_POWER_NOW,
  106. POWER_SUPPLY_PROP_POWER_AVG,
  107. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  108. POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  109. POWER_SUPPLY_PROP_CHARGE_FULL,
  110. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  111. POWER_SUPPLY_PROP_CHARGE_NOW,
  112. POWER_SUPPLY_PROP_CHARGE_AVG,
  113. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  114. POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW,
  115. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  116. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  117. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  118. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  119. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
  120. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
  121. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  122. POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
  123. POWER_SUPPLY_PROP_ENERGY_FULL,
  124. POWER_SUPPLY_PROP_ENERGY_EMPTY,
  125. POWER_SUPPLY_PROP_ENERGY_NOW,
  126. POWER_SUPPLY_PROP_ENERGY_AVG,
  127. POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
  128. POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
  129. POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
  130. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  131. POWER_SUPPLY_PROP_TEMP,
  132. POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
  133. POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
  134. POWER_SUPPLY_PROP_COOL_TEMP,
  135. POWER_SUPPLY_PROP_WARM_TEMP,
  136. POWER_SUPPLY_PROP_TEMP_AMBIENT,
  137. POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
  138. POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
  139. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  140. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  141. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  142. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  143. POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
  144. POWER_SUPPLY_PROP_SCOPE,
  145. POWER_SUPPLY_PROP_SYSTEM_TEMP_LEVEL,
  146. POWER_SUPPLY_PROP_RESISTANCE,
  147. POWER_SUPPLY_PROP_RESISTANCE_CAPACITIVE,
  148. /* Local extensions */
  149. POWER_SUPPLY_PROP_USB_HC,
  150. POWER_SUPPLY_PROP_USB_OTG,
  151. POWER_SUPPLY_PROP_CHARGE_ENABLED,
  152. /* Properties of type `const char *' */
  153. POWER_SUPPLY_PROP_MODEL_NAME,
  154. POWER_SUPPLY_PROP_MANUFACTURER,
  155. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  156. POWER_SUPPLY_PROP_BATFET,
  157. POWER_SUPPLY_PROP_BATTERY_TYPE,
  158. };
  159. enum power_supply_type {
  160. POWER_SUPPLY_TYPE_UNKNOWN = 0,
  161. POWER_SUPPLY_TYPE_BATTERY, /* 1 */
  162. POWER_SUPPLY_TYPE_UPS, /* 2 */
  163. POWER_SUPPLY_TYPE_MAINS, /* 3 */
  164. POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port (4) */
  165. POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port (5) */
  166. POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port (6) */
  167. POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters (7) */
  168. POWER_SUPPLY_TYPE_BMS, /* Battery Monitor System (8) */
  169. POWER_SUPPLY_TYPE_MISC, /* 9 */
  170. POWER_SUPPLY_TYPE_WIRELESS, /* 10 */
  171. POWER_SUPPLY_TYPE_CARDOCK, /* 11 */
  172. POWER_SUPPLY_TYPE_UARTOFF, /* 12 */
  173. POWER_SUPPLY_TYPE_OTG, /* 13 */
  174. POWER_SUPPLY_TYPE_LAN_HUB, /* 14 */
  175. POWER_SUPPLY_TYPE_MHL_500, /* 15 */
  176. POWER_SUPPLY_TYPE_MHL_900, /* 16 */
  177. POWER_SUPPLY_TYPE_MHL_1500, /* 17 */
  178. POWER_SUPPLY_TYPE_MHL_USB, /* 18 */
  179. POWER_SUPPLY_TYPE_SMART_OTG, /* 19 */
  180. POWER_SUPPLY_TYPE_SMART_NOTG, /* 20 */
  181. POWER_SUPPLY_TYPE_POWER_SHARING, /* power sharing cable (21) */
  182. POWER_SUPPLY_TYPE_HV_MAINS, /* Adaptive Charger (22) */
  183. POWER_SUPPLY_TYPE_POGODOCK, /* POGO Dock Charger (23) */
  184. POWER_SUPPLY_TYPE_MHL_2000, /* 24 */
  185. POWER_SUPPLY_TYPE_MHL_USB_100, /* 25 */
  186. POWER_SUPPLY_TYPE_HV_PREPARE_MAINS, /* Prepare Adaptive Charger (26) */
  187. POWER_SUPPLY_TYPE_HV_ERR, /* Adaptive Charger Err (27) */
  188. POWER_SUPPLY_TYPE_HV_UNKNOWN, /* Adaptive Charger Unknown (28) */
  189. POWER_SUPPLY_TYPE_MDOCK_TA, /* MMdock charger (29) */
  190. POWER_SUPPLY_TYPE_MDOCK_USB, /* MMdock + USB (30) */
  191. POWER_SUPPLY_TYPE_MAX,
  192. };
  193. union power_supply_propval {
  194. int intval;
  195. const char *strval;
  196. };
  197. struct power_supply {
  198. const char *name;
  199. enum power_supply_type type;
  200. enum power_supply_property *properties;
  201. size_t num_properties;
  202. char **supplied_to;
  203. size_t num_supplicants;
  204. char **supplied_from;
  205. size_t num_supplies;
  206. #ifdef CONFIG_OF
  207. struct device_node *of_node;
  208. #endif
  209. int (*get_property)(struct power_supply *psy,
  210. enum power_supply_property psp,
  211. union power_supply_propval *val);
  212. int (*set_property)(struct power_supply *psy,
  213. enum power_supply_property psp,
  214. const union power_supply_propval *val);
  215. int (*property_is_writeable)(struct power_supply *psy,
  216. enum power_supply_property psp);
  217. void (*external_power_changed)(struct power_supply *psy);
  218. void (*set_charged)(struct power_supply *psy);
  219. /* For APM emulation, think legacy userspace. */
  220. int use_for_apm;
  221. /* private */
  222. struct device *dev;
  223. struct work_struct changed_work;
  224. spinlock_t changed_lock;
  225. bool changed;
  226. #ifdef CONFIG_THERMAL
  227. struct thermal_zone_device *tzd;
  228. struct thermal_cooling_device *tcd;
  229. #endif
  230. struct wake_lock work_wake_lock;
  231. #ifdef CONFIG_LEDS_TRIGGERS
  232. struct led_trigger *charging_full_trig;
  233. char *charging_full_trig_name;
  234. struct led_trigger *charging_trig;
  235. char *charging_trig_name;
  236. struct led_trigger *full_trig;
  237. char *full_trig_name;
  238. struct led_trigger *online_trig;
  239. char *online_trig_name;
  240. struct led_trigger *charging_blink_full_solid_trig;
  241. char *charging_blink_full_solid_trig_name;
  242. #endif
  243. };
  244. /*
  245. * This is recommended structure to specify static power supply parameters.
  246. * Generic one, parametrizable for different power supplies. Power supply
  247. * class itself does not use it, but that's what implementing most platform
  248. * drivers, should try reuse for consistency.
  249. */
  250. struct power_supply_info {
  251. const char *name;
  252. int technology;
  253. int voltage_max_design;
  254. int voltage_min_design;
  255. int charge_full_design;
  256. int charge_empty_design;
  257. int energy_full_design;
  258. int energy_empty_design;
  259. int use_for_apm;
  260. };
  261. #if defined(CONFIG_POWER_SUPPLY) || defined(CONFIG_POWER_SUPPLY_MODULE)
  262. extern struct power_supply *power_supply_get_by_name(char *name);
  263. extern void power_supply_changed(struct power_supply *psy);
  264. extern int power_supply_am_i_supplied(struct power_supply *psy);
  265. extern int power_supply_set_battery_charged(struct power_supply *psy);
  266. extern int power_supply_set_current_limit(struct power_supply *psy, int limit);
  267. extern int power_supply_set_voltage_limit(struct power_supply *psy, int limit);
  268. extern int power_supply_set_online(struct power_supply *psy, bool enable);
  269. extern int power_supply_set_health_state(struct power_supply *psy, int health);
  270. extern int power_supply_set_present(struct power_supply *psy, bool enable);
  271. extern int power_supply_set_scope(struct power_supply *psy, int scope);
  272. extern int power_supply_set_charge_type(struct power_supply *psy, int type);
  273. extern int power_supply_set_supply_type(struct power_supply *psy,
  274. enum power_supply_type supply_type);
  275. extern int power_supply_is_system_supplied(void);
  276. extern int power_supply_register(struct device *parent,
  277. struct power_supply *psy);
  278. extern void power_supply_unregister(struct power_supply *psy);
  279. extern int power_supply_powers(struct power_supply *psy, struct device *dev);
  280. #else
  281. static inline struct power_supply *power_supply_get_by_name(char *name)
  282. { return NULL; }
  283. static inline void power_supply_changed(struct power_supply *psy) { }
  284. static inline int power_supply_am_i_supplied(struct power_supply *psy)
  285. { return -ENOSYS; }
  286. static inline int power_supply_set_battery_charged(struct power_supply *psy)
  287. { return -ENOSYS; }
  288. static inline int power_supply_set_voltage_limit(struct power_supply *psy,
  289. int limit)
  290. { return -ENOSYS; }
  291. static inline int power_supply_set_current_limit(struct power_supply *psy,
  292. int limit)
  293. { return -ENOSYS; }
  294. static inline int power_supply_set_online(struct power_supply *psy,
  295. bool enable)
  296. { return -ENOSYS; }
  297. static inline int power_supply_set_health_state(struct power_supply *psy,
  298. int health)
  299. { return -ENOSYS; }
  300. static inline int power_supply_set_present(struct power_supply *psy,
  301. bool enable)
  302. { return -ENOSYS; }
  303. static inline int power_supply_set_scope(struct power_supply *psy,
  304. int scope)
  305. { return -ENOSYS; }
  306. static inline int power_supply_set_charge_type(struct power_supply *psy,
  307. int type)
  308. { return -ENOSYS; }
  309. static inline int power_supply_set_supply_type(struct power_supply *psy,
  310. enum power_supply_type supply_type)
  311. { return -ENOSYS; }
  312. static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
  313. static inline int power_supply_register(struct device *parent,
  314. struct power_supply *psy)
  315. { return -ENOSYS; }
  316. static inline void power_supply_unregister(struct power_supply *psy) { }
  317. static inline int power_supply_powers(struct power_supply *psy,
  318. struct device *dev)
  319. { return -ENOSYS; }
  320. #endif
  321. /* For APM emulation, think legacy userspace. */
  322. extern struct class *power_supply_class;
  323. static inline bool power_supply_is_amp_property(enum power_supply_property psp)
  324. {
  325. switch (psp) {
  326. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  327. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  328. case POWER_SUPPLY_PROP_CHARGE_FULL:
  329. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  330. case POWER_SUPPLY_PROP_CHARGE_NOW:
  331. case POWER_SUPPLY_PROP_CHARGE_AVG:
  332. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  333. case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
  334. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  335. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  336. case POWER_SUPPLY_PROP_CURRENT_MAX:
  337. case POWER_SUPPLY_PROP_INPUT_CURRENT_MAX:
  338. case POWER_SUPPLY_PROP_CURRENT_NOW:
  339. case POWER_SUPPLY_PROP_CURRENT_AVG:
  340. return 1;
  341. default:
  342. break;
  343. }
  344. return 0;
  345. }
  346. static inline bool power_supply_is_watt_property(enum power_supply_property psp)
  347. {
  348. switch (psp) {
  349. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  350. case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
  351. case POWER_SUPPLY_PROP_ENERGY_FULL:
  352. case POWER_SUPPLY_PROP_ENERGY_EMPTY:
  353. case POWER_SUPPLY_PROP_ENERGY_NOW:
  354. case POWER_SUPPLY_PROP_ENERGY_AVG:
  355. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  356. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  357. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  358. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  359. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  360. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  361. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  362. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  363. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  364. case POWER_SUPPLY_PROP_POWER_NOW:
  365. return 1;
  366. default:
  367. break;
  368. }
  369. return 0;
  370. }
  371. #endif /* __LINUX_POWER_SUPPLY_H__ */