power_supply.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/workqueue.h>
  15. #include <linux/leds.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/notifier.h>
  18. #include <linux/types.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. POWER_SUPPLY_STATUS_CMD_DISCHARGING,
  37. };
  38. enum {
  39. POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,
  40. POWER_SUPPLY_CHARGE_TYPE_NONE,
  41. POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
  42. POWER_SUPPLY_CHARGE_TYPE_FAST,
  43. };
  44. enum {
  45. POWER_SUPPLY_HEALTH_UNKNOWN = 0,
  46. POWER_SUPPLY_HEALTH_GOOD,
  47. POWER_SUPPLY_HEALTH_OVERHEAT,
  48. POWER_SUPPLY_HEALTH_DEAD,
  49. POWER_SUPPLY_HEALTH_OVERVOLTAGE,
  50. POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
  51. POWER_SUPPLY_HEALTH_COLD,
  52. POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE,
  53. POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE,
  54. };
  55. enum {
  56. POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
  57. POWER_SUPPLY_TECHNOLOGY_NiMH,
  58. POWER_SUPPLY_TECHNOLOGY_LION,
  59. POWER_SUPPLY_TECHNOLOGY_LIPO,
  60. POWER_SUPPLY_TECHNOLOGY_LiFe,
  61. POWER_SUPPLY_TECHNOLOGY_NiCd,
  62. POWER_SUPPLY_TECHNOLOGY_LiMn,
  63. };
  64. enum {
  65. POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
  66. POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
  67. POWER_SUPPLY_CAPACITY_LEVEL_LOW,
  68. POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
  69. POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
  70. POWER_SUPPLY_CAPACITY_LEVEL_FULL,
  71. };
  72. enum {
  73. POWER_SUPPLY_SCOPE_UNKNOWN = 0,
  74. POWER_SUPPLY_SCOPE_SYSTEM,
  75. POWER_SUPPLY_SCOPE_DEVICE,
  76. };
  77. enum power_supply_property {
  78. /* Properties of type `int' */
  79. POWER_SUPPLY_PROP_STATUS = 0,
  80. POWER_SUPPLY_PROP_CHARGE_TYPE,
  81. POWER_SUPPLY_PROP_HEALTH,
  82. POWER_SUPPLY_PROP_PRESENT,
  83. POWER_SUPPLY_PROP_ONLINE,
  84. POWER_SUPPLY_PROP_AUTHENTIC,
  85. POWER_SUPPLY_PROP_TECHNOLOGY,
  86. POWER_SUPPLY_PROP_CYCLE_COUNT,
  87. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  88. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  89. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  90. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  91. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  92. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  93. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  94. POWER_SUPPLY_PROP_VOLTAGE_BOOT,
  95. POWER_SUPPLY_PROP_CURRENT_MAX,
  96. POWER_SUPPLY_PROP_CURRENT_NOW,
  97. POWER_SUPPLY_PROP_CURRENT_AVG,
  98. POWER_SUPPLY_PROP_CURRENT_BOOT,
  99. POWER_SUPPLY_PROP_POWER_NOW,
  100. POWER_SUPPLY_PROP_POWER_AVG,
  101. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  102. POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  103. POWER_SUPPLY_PROP_CHARGE_FULL,
  104. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  105. POWER_SUPPLY_PROP_CHARGE_NOW,
  106. POWER_SUPPLY_PROP_CHARGE_AVG,
  107. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  108. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  109. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  110. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  111. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  112. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
  113. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
  114. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  115. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  116. POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
  117. POWER_SUPPLY_PROP_ENERGY_FULL,
  118. POWER_SUPPLY_PROP_ENERGY_EMPTY,
  119. POWER_SUPPLY_PROP_ENERGY_NOW,
  120. POWER_SUPPLY_PROP_ENERGY_AVG,
  121. POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
  122. POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
  123. POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
  124. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  125. POWER_SUPPLY_PROP_TEMP,
  126. POWER_SUPPLY_PROP_TEMP_MAX,
  127. POWER_SUPPLY_PROP_TEMP_MIN,
  128. POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
  129. POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
  130. POWER_SUPPLY_PROP_TEMP_AMBIENT,
  131. POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
  132. POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
  133. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  134. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  135. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  136. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  137. POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
  138. POWER_SUPPLY_PROP_SCOPE,
  139. POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
  140. POWER_SUPPLY_PROP_CALIBRATE,
  141. /* Local extensions */
  142. POWER_SUPPLY_PROP_USB_HC,
  143. POWER_SUPPLY_PROP_USB_OTG,
  144. POWER_SUPPLY_PROP_CHARGE_ENABLED,
  145. /* Local extensions of type int64_t */
  146. POWER_SUPPLY_PROP_CHARGE_COUNTER_EXT,
  147. /* Add for battery voltage/temp */
  148. POWER_SUPPLY_PROP_batt_vol,
  149. POWER_SUPPLY_PROP_batt_temp,
  150. /* Add for EM */
  151. POWER_SUPPLY_PROP_TemperatureR,
  152. POWER_SUPPLY_PROP_TempBattVoltage,
  153. POWER_SUPPLY_PROP_InstatVolt,
  154. POWER_SUPPLY_PROP_BatteryAverageCurrent,
  155. POWER_SUPPLY_PROP_BatterySenseVoltage,
  156. POWER_SUPPLY_PROP_ISenseVoltage,
  157. POWER_SUPPLY_PROP_ChargerVoltage,
  158. /* Dual battery */
  159. POWER_SUPPLY_PROP_status_smb,
  160. POWER_SUPPLY_PROP_capacity_smb,
  161. POWER_SUPPLY_PROP_present_smb,
  162. /* ADB CMD Discharging */
  163. POWER_SUPPLY_PROP_adjust_power,
  164. /* Properties of type `const char *' */
  165. POWER_SUPPLY_PROP_MODEL_NAME,
  166. POWER_SUPPLY_PROP_MANUFACTURER,
  167. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  168. };
  169. enum power_supply_type {
  170. POWER_SUPPLY_TYPE_UNKNOWN = 0,
  171. POWER_SUPPLY_TYPE_BATTERY,
  172. POWER_SUPPLY_TYPE_UPS,
  173. POWER_SUPPLY_TYPE_MAINS,
  174. POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port */
  175. POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */
  176. POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */
  177. POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */
  178. POWER_SUPPLY_TYPE_WIRELESS, /* Wireless Charger */
  179. };
  180. enum power_supply_notifier_events {
  181. PSY_EVENT_PROP_CHANGED,
  182. };
  183. union power_supply_propval {
  184. int intval;
  185. const char *strval;
  186. int64_t int64val;
  187. };
  188. struct device;
  189. struct device_node;
  190. struct power_supply {
  191. const char *name;
  192. enum power_supply_type type;
  193. enum power_supply_property *properties;
  194. size_t num_properties;
  195. char **supplied_to;
  196. size_t num_supplicants;
  197. char **supplied_from;
  198. size_t num_supplies;
  199. struct device_node *of_node;
  200. int (*get_property)(struct power_supply *psy,
  201. enum power_supply_property psp,
  202. union power_supply_propval *val);
  203. int (*set_property)(struct power_supply *psy,
  204. enum power_supply_property psp,
  205. const union power_supply_propval *val);
  206. int (*property_is_writeable)(struct power_supply *psy,
  207. enum power_supply_property psp);
  208. void (*external_power_changed)(struct power_supply *psy);
  209. void (*set_charged)(struct power_supply *psy);
  210. /*
  211. * Set if thermal zone should not be created for this power supply.
  212. * For example for virtual supplies forwarding calls to actual
  213. * sensors or other supplies.
  214. */
  215. bool no_thermal;
  216. /* For APM emulation, think legacy userspace. */
  217. int use_for_apm;
  218. /* private */
  219. struct device *dev;
  220. struct work_struct changed_work;
  221. spinlock_t changed_lock;
  222. bool changed;
  223. #ifdef CONFIG_THERMAL
  224. struct thermal_zone_device *tzd;
  225. struct thermal_cooling_device *tcd;
  226. #endif
  227. #ifdef CONFIG_LEDS_TRIGGERS
  228. struct led_trigger *charging_full_trig;
  229. char *charging_full_trig_name;
  230. struct led_trigger *charging_trig;
  231. char *charging_trig_name;
  232. struct led_trigger *full_trig;
  233. char *full_trig_name;
  234. struct led_trigger *online_trig;
  235. char *online_trig_name;
  236. struct led_trigger *charging_blink_full_solid_trig;
  237. char *charging_blink_full_solid_trig_name;
  238. #endif
  239. };
  240. /*
  241. * This is recommended structure to specify static power supply parameters.
  242. * Generic one, parametrizable for different power supplies. Power supply
  243. * class itself does not use it, but that's what implementing most platform
  244. * drivers, should try reuse for consistency.
  245. */
  246. struct power_supply_info {
  247. const char *name;
  248. int technology;
  249. int voltage_max_design;
  250. int voltage_min_design;
  251. int charge_full_design;
  252. int charge_empty_design;
  253. int energy_full_design;
  254. int energy_empty_design;
  255. int use_for_apm;
  256. };
  257. extern struct atomic_notifier_head power_supply_notifier;
  258. extern int power_supply_reg_notifier(struct notifier_block *nb);
  259. extern void power_supply_unreg_notifier(struct notifier_block *nb);
  260. extern struct power_supply *power_supply_get_by_name(const char *name);
  261. #ifdef CONFIG_OF
  262. extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
  263. const char *property);
  264. #else /* !CONFIG_OF */
  265. static inline struct power_supply *
  266. power_supply_get_by_phandle(struct device_node *np, const char *property)
  267. { return NULL; }
  268. #endif /* CONFIG_OF */
  269. extern void power_supply_changed(struct power_supply *psy);
  270. extern int power_supply_am_i_supplied(struct power_supply *psy);
  271. extern int power_supply_set_battery_charged(struct power_supply *psy);
  272. #ifdef CONFIG_POWER_SUPPLY
  273. extern int power_supply_is_system_supplied(void);
  274. #else
  275. static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
  276. #endif
  277. extern int power_supply_register(struct device *parent,
  278. struct power_supply *psy);
  279. extern int power_supply_register_no_ws(struct device *parent,
  280. struct power_supply *psy);
  281. extern void power_supply_unregister(struct power_supply *psy);
  282. extern int power_supply_powers(struct power_supply *psy, struct device *dev);
  283. /* For APM emulation, think legacy userspace. */
  284. extern struct class *power_supply_class;
  285. static inline bool power_supply_is_amp_property(enum power_supply_property psp)
  286. {
  287. switch (psp) {
  288. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  289. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  290. case POWER_SUPPLY_PROP_CHARGE_FULL:
  291. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  292. case POWER_SUPPLY_PROP_CHARGE_NOW:
  293. case POWER_SUPPLY_PROP_CHARGE_AVG:
  294. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  295. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  296. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  297. case POWER_SUPPLY_PROP_CURRENT_MAX:
  298. case POWER_SUPPLY_PROP_CURRENT_NOW:
  299. case POWER_SUPPLY_PROP_CURRENT_AVG:
  300. case POWER_SUPPLY_PROP_CURRENT_BOOT:
  301. return 1;
  302. default:
  303. break;
  304. }
  305. return 0;
  306. }
  307. static inline bool power_supply_is_watt_property(enum power_supply_property psp)
  308. {
  309. switch (psp) {
  310. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  311. case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
  312. case POWER_SUPPLY_PROP_ENERGY_FULL:
  313. case POWER_SUPPLY_PROP_ENERGY_EMPTY:
  314. case POWER_SUPPLY_PROP_ENERGY_NOW:
  315. case POWER_SUPPLY_PROP_ENERGY_AVG:
  316. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  317. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  318. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  319. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  320. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  321. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  322. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  323. case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
  324. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  325. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  326. case POWER_SUPPLY_PROP_POWER_NOW:
  327. return 1;
  328. default:
  329. break;
  330. }
  331. return 0;
  332. }
  333. #endif /* __LINUX_POWER_SUPPLY_H__ */