charger-manager.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  3. * MyungJoo.Ham <myungjoo.ham@samsung.com>
  4. *
  5. * Charger Manager.
  6. * This framework enables to control and multiple chargers and to
  7. * monitor charging even in the context of suspend-to-RAM with
  8. * an interface combining the chargers.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. **/
  14. #ifndef _CHARGER_MANAGER_H
  15. #define _CHARGER_MANAGER_H
  16. #include <linux/power_supply.h>
  17. #include <linux/extcon.h>
  18. enum data_source {
  19. CM_BATTERY_PRESENT,
  20. CM_NO_BATTERY,
  21. CM_FUEL_GAUGE,
  22. CM_CHARGER_STAT,
  23. };
  24. enum polling_modes {
  25. CM_POLL_DISABLE = 0,
  26. CM_POLL_ALWAYS,
  27. CM_POLL_EXTERNAL_POWER_ONLY,
  28. CM_POLL_CHARGING_ONLY,
  29. };
  30. enum cm_event_types {
  31. CM_EVENT_UNKNOWN = 0,
  32. CM_EVENT_BATT_FULL,
  33. CM_EVENT_BATT_IN,
  34. CM_EVENT_BATT_OUT,
  35. CM_EVENT_BATT_OVERHEAT,
  36. CM_EVENT_BATT_COLD,
  37. CM_EVENT_EXT_PWR_IN_OUT,
  38. CM_EVENT_CHG_START_STOP,
  39. CM_EVENT_OTHERS,
  40. };
  41. /**
  42. * struct charger_global_desc
  43. * @rtc_name: the name of RTC used to wake up the system from suspend.
  44. * @rtc_only_wakeup:
  45. * If the system is woken up by waekup-sources other than the RTC or
  46. * callbacks, Charger Manager should recognize with
  47. * rtc_only_wakeup() returning false.
  48. * If the RTC given to CM is the only wakeup reason,
  49. * rtc_only_wakeup should return true.
  50. * @assume_timer_stops_in_suspend:
  51. * Assume that the jiffy timer stops in suspend-to-RAM.
  52. * When enabled, CM does not rely on jiffies value in
  53. * suspend_again and assumes that jiffies value does not
  54. * change during suspend.
  55. */
  56. struct charger_global_desc {
  57. char *rtc_name;
  58. bool (*rtc_only_wakeup)(void);
  59. bool assume_timer_stops_in_suspend;
  60. };
  61. /**
  62. * struct charger_cable
  63. * @extcon_name: the name of extcon device.
  64. * @name: the name of charger cable(external connector).
  65. * @extcon_dev: the extcon device.
  66. * @wq: the workqueue to control charger according to the state of
  67. * charger cable. If charger cable is attached, enable charger.
  68. * But if charger cable is detached, disable charger.
  69. * @nb: the notifier block to receive changed state from EXTCON
  70. * (External Connector) when charger cable is attached/detached.
  71. * @attached: the state of charger cable.
  72. * true: the charger cable is attached
  73. * false: the charger cable is detached
  74. * @charger: the instance of struct charger_regulator.
  75. * @cm: the Charger Manager representing the battery.
  76. */
  77. struct charger_cable {
  78. const char *extcon_name;
  79. const char *name;
  80. /* The charger-manager use Exton framework*/
  81. struct extcon_specific_cable_nb extcon_dev;
  82. struct work_struct wq;
  83. struct notifier_block nb;
  84. /* The state of charger cable */
  85. bool attached;
  86. struct charger_regulator *charger;
  87. /*
  88. * Set min/max current of regulator to protect over-current issue
  89. * according to a kind of charger cable when cable is attached.
  90. */
  91. int min_uA;
  92. int max_uA;
  93. struct charger_manager *cm;
  94. };
  95. /**
  96. * struct charger_regulator
  97. * @regulator_name: the name of regulator for using charger.
  98. * @consumer: the regulator consumer for the charger.
  99. * @externally_control:
  100. * Set if the charger-manager cannot control charger,
  101. * the charger will be maintained with disabled state.
  102. * @cables:
  103. * the array of charger cables to enable/disable charger
  104. * and set current limit according to constratint data of
  105. * struct charger_cable if only charger cable included
  106. * in the array of charger cables is attached/detached.
  107. * @num_cables: the number of charger cables.
  108. * @attr_g: Attribute group for the charger(regulator)
  109. * @attr_name: "name" sysfs entry
  110. * @attr_state: "state" sysfs entry
  111. * @attr_externally_control: "externally_control" sysfs entry
  112. * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
  113. */
  114. struct charger_regulator {
  115. /* The name of regulator for charging */
  116. const char *regulator_name;
  117. struct regulator *consumer;
  118. /* charger never on when system is on */
  119. int externally_control;
  120. /*
  121. * Store constraint information related to current limit,
  122. * each cable have different condition for charging.
  123. */
  124. struct charger_cable *cables;
  125. int num_cables;
  126. struct attribute_group attr_g;
  127. struct device_attribute attr_name;
  128. struct device_attribute attr_state;
  129. struct device_attribute attr_externally_control;
  130. struct attribute *attrs[4];
  131. struct charger_manager *cm;
  132. };
  133. /**
  134. * struct charger_desc
  135. * @psy_name: the name of power-supply-class for charger manager
  136. * @polling_mode:
  137. * Determine which polling mode will be used
  138. * @fullbatt_vchkdrop_ms:
  139. * @fullbatt_vchkdrop_uV:
  140. * Check voltage drop after the battery is fully charged.
  141. * If it has dropped more than fullbatt_vchkdrop_uV after
  142. * fullbatt_vchkdrop_ms, CM will restart charging.
  143. * @fullbatt_uV: voltage in microvolt
  144. * If VBATT >= fullbatt_uV, it is assumed to be full.
  145. * @fullbatt_soc: state of Charge in %
  146. * If state of Charge >= fullbatt_soc, it is assumed to be full.
  147. * @fullbatt_full_capacity: full capacity measure
  148. * If full capacity of battery >= fullbatt_full_capacity,
  149. * it is assumed to be full.
  150. * @polling_interval_ms: interval in millisecond at which
  151. * charger manager will monitor battery health
  152. * @battery_present:
  153. * Specify where information for existance of battery can be obtained
  154. * @psy_charger_stat: the names of power-supply for chargers
  155. * @num_charger_regulator: the number of entries in charger_regulators
  156. * @charger_regulators: array of charger regulators
  157. * @psy_fuel_gauge: the name of power-supply for fuel gauge
  158. * @thermal_zone : the name of thermal zone for battery
  159. * @temp_min : Minimum battery temperature for charging.
  160. * @temp_max : Maximum battery temperature for charging.
  161. * @temp_diff : Temperature diffential to restart charging.
  162. * @measure_battery_temp:
  163. * true: measure battery temperature
  164. * false: measure ambient temperature
  165. * @charging_max_duration_ms: Maximum possible duration for charging
  166. * If whole charging duration exceed 'charging_max_duration_ms',
  167. * cm stop charging.
  168. * @discharging_max_duration_ms:
  169. * Maximum possible duration for discharging with charger cable
  170. * after full-batt. If discharging duration exceed 'discharging
  171. * max_duration_ms', cm start charging.
  172. */
  173. struct charger_desc {
  174. const char *psy_name;
  175. enum polling_modes polling_mode;
  176. unsigned int polling_interval_ms;
  177. unsigned int fullbatt_vchkdrop_ms;
  178. unsigned int fullbatt_vchkdrop_uV;
  179. unsigned int fullbatt_uV;
  180. unsigned int fullbatt_soc;
  181. unsigned int fullbatt_full_capacity;
  182. enum data_source battery_present;
  183. const char **psy_charger_stat;
  184. int num_charger_regulators;
  185. struct charger_regulator *charger_regulators;
  186. const char *psy_fuel_gauge;
  187. const char *thermal_zone;
  188. int temp_min;
  189. int temp_max;
  190. int temp_diff;
  191. bool measure_battery_temp;
  192. u32 charging_max_duration_ms;
  193. u32 discharging_max_duration_ms;
  194. };
  195. #define PSY_NAME_MAX 30
  196. /**
  197. * struct charger_manager
  198. * @entry: entry for list
  199. * @dev: device pointer
  200. * @desc: instance of charger_desc
  201. * @fuel_gauge: power_supply for fuel gauge
  202. * @charger_stat: array of power_supply for chargers
  203. * @tzd_batt : thermal zone device for battery
  204. * @charger_enabled: the state of charger
  205. * @fullbatt_vchk_jiffies_at:
  206. * jiffies at the time full battery check will occur.
  207. * @fullbatt_vchk_work: work queue for full battery check
  208. * @emergency_stop:
  209. * When setting true, stop charging
  210. * @psy_name_buf: the name of power-supply-class for charger manager
  211. * @charger_psy: power_supply for charger manager
  212. * @status_save_ext_pwr_inserted:
  213. * saved status of external power before entering suspend-to-RAM
  214. * @status_save_batt:
  215. * saved status of battery before entering suspend-to-RAM
  216. * @charging_start_time: saved start time of enabling charging
  217. * @charging_end_time: saved end time of disabling charging
  218. */
  219. struct charger_manager {
  220. struct list_head entry;
  221. struct device *dev;
  222. struct charger_desc *desc;
  223. #ifdef CONFIG_THERMAL
  224. struct thermal_zone_device *tzd_batt;
  225. #endif
  226. bool charger_enabled;
  227. unsigned long fullbatt_vchk_jiffies_at;
  228. struct delayed_work fullbatt_vchk_work;
  229. int emergency_stop;
  230. char psy_name_buf[PSY_NAME_MAX + 1];
  231. struct power_supply charger_psy;
  232. bool status_save_ext_pwr_inserted;
  233. bool status_save_batt;
  234. u64 charging_start_time;
  235. u64 charging_end_time;
  236. };
  237. #ifdef CONFIG_CHARGER_MANAGER
  238. extern int setup_charger_manager(struct charger_global_desc *gd);
  239. extern bool cm_suspend_again(void);
  240. extern void cm_notify_event(struct power_supply *psy,
  241. enum cm_event_types type, char *msg);
  242. #else
  243. static inline int setup_charger_manager(struct charger_global_desc *gd)
  244. { return 0; }
  245. static inline bool cm_suspend_again(void) { return false; }
  246. static inline void cm_notify_event(struct power_supply *psy,
  247. enum cm_event_types type, char *msg) { }
  248. #endif
  249. #endif /* _CHARGER_MANAGER_H */