charger-manager.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include <linux/alarmtimer.h>
  19. enum data_source {
  20. CM_BATTERY_PRESENT,
  21. CM_NO_BATTERY,
  22. CM_FUEL_GAUGE,
  23. CM_CHARGER_STAT,
  24. };
  25. enum polling_modes {
  26. CM_POLL_DISABLE = 0,
  27. CM_POLL_ALWAYS,
  28. CM_POLL_EXTERNAL_POWER_ONLY,
  29. CM_POLL_CHARGING_ONLY,
  30. };
  31. enum cm_event_types {
  32. CM_EVENT_UNKNOWN = 0,
  33. CM_EVENT_BATT_FULL,
  34. CM_EVENT_BATT_IN,
  35. CM_EVENT_BATT_OUT,
  36. CM_EVENT_BATT_OVERHEAT,
  37. CM_EVENT_BATT_COLD,
  38. CM_EVENT_EXT_PWR_IN_OUT,
  39. CM_EVENT_CHG_START_STOP,
  40. CM_EVENT_OTHERS,
  41. };
  42. /**
  43. * struct charger_cable
  44. * @extcon_name: the name of extcon device.
  45. * @name: the name of charger cable(external connector).
  46. * @extcon_dev: the extcon device.
  47. * @wq: the workqueue to control charger according to the state of
  48. * charger cable. If charger cable is attached, enable charger.
  49. * But if charger cable is detached, disable charger.
  50. * @nb: the notifier block to receive changed state from EXTCON
  51. * (External Connector) when charger cable is attached/detached.
  52. * @attached: the state of charger cable.
  53. * true: the charger cable is attached
  54. * false: the charger cable is detached
  55. * @charger: the instance of struct charger_regulator.
  56. * @cm: the Charger Manager representing the battery.
  57. */
  58. struct charger_cable {
  59. const char *extcon_name;
  60. const char *name;
  61. /* The charger-manager use Extcon framework */
  62. struct extcon_specific_cable_nb extcon_dev;
  63. struct work_struct wq;
  64. struct notifier_block nb;
  65. /* The state of charger cable */
  66. bool attached;
  67. struct charger_regulator *charger;
  68. /*
  69. * Set min/max current of regulator to protect over-current issue
  70. * according to a kind of charger cable when cable is attached.
  71. */
  72. int min_uA;
  73. int max_uA;
  74. struct charger_manager *cm;
  75. };
  76. /**
  77. * struct charger_regulator
  78. * @regulator_name: the name of regulator for using charger.
  79. * @consumer: the regulator consumer for the charger.
  80. * @externally_control:
  81. * Set if the charger-manager cannot control charger,
  82. * the charger will be maintained with disabled state.
  83. * @cables:
  84. * the array of charger cables to enable/disable charger
  85. * and set current limit according to constraint data of
  86. * struct charger_cable if only charger cable included
  87. * in the array of charger cables is attached/detached.
  88. * @num_cables: the number of charger cables.
  89. * @attr_g: Attribute group for the charger(regulator)
  90. * @attr_name: "name" sysfs entry
  91. * @attr_state: "state" sysfs entry
  92. * @attr_externally_control: "externally_control" sysfs entry
  93. * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
  94. */
  95. struct charger_regulator {
  96. /* The name of regulator for charging */
  97. const char *regulator_name;
  98. struct regulator *consumer;
  99. /* charger never on when system is on */
  100. int externally_control;
  101. /*
  102. * Store constraint information related to current limit,
  103. * each cable have different condition for charging.
  104. */
  105. struct charger_cable *cables;
  106. int num_cables;
  107. struct attribute_group attr_g;
  108. struct device_attribute attr_name;
  109. struct device_attribute attr_state;
  110. struct device_attribute attr_externally_control;
  111. struct attribute *attrs[4];
  112. struct charger_manager *cm;
  113. };
  114. /**
  115. * struct charger_desc
  116. * @psy_name: the name of power-supply-class for charger manager
  117. * @polling_mode:
  118. * Determine which polling mode will be used
  119. * @fullbatt_vchkdrop_ms:
  120. * @fullbatt_vchkdrop_uV:
  121. * Check voltage drop after the battery is fully charged.
  122. * If it has dropped more than fullbatt_vchkdrop_uV after
  123. * fullbatt_vchkdrop_ms, CM will restart charging.
  124. * @fullbatt_uV: voltage in microvolt
  125. * If VBATT >= fullbatt_uV, it is assumed to be full.
  126. * @fullbatt_soc: state of Charge in %
  127. * If state of Charge >= fullbatt_soc, it is assumed to be full.
  128. * @fullbatt_full_capacity: full capacity measure
  129. * If full capacity of battery >= fullbatt_full_capacity,
  130. * it is assumed to be full.
  131. * @polling_interval_ms: interval in millisecond at which
  132. * charger manager will monitor battery health
  133. * @battery_present:
  134. * Specify where information for existence of battery can be obtained
  135. * @psy_charger_stat: the names of power-supply for chargers
  136. * @num_charger_regulator: the number of entries in charger_regulators
  137. * @charger_regulators: array of charger regulators
  138. * @psy_fuel_gauge: the name of power-supply for fuel gauge
  139. * @thermal_zone : the name of thermal zone for battery
  140. * @temp_min : Minimum battery temperature for charging.
  141. * @temp_max : Maximum battery temperature for charging.
  142. * @temp_diff : Temperature difference to restart charging.
  143. * @measure_battery_temp:
  144. * true: measure battery temperature
  145. * false: measure ambient temperature
  146. * @charging_max_duration_ms: Maximum possible duration for charging
  147. * If whole charging duration exceed 'charging_max_duration_ms',
  148. * cm stop charging.
  149. * @discharging_max_duration_ms:
  150. * Maximum possible duration for discharging with charger cable
  151. * after full-batt. If discharging duration exceed 'discharging
  152. * max_duration_ms', cm start charging.
  153. */
  154. struct charger_desc {
  155. const char *psy_name;
  156. enum polling_modes polling_mode;
  157. unsigned int polling_interval_ms;
  158. unsigned int fullbatt_vchkdrop_ms;
  159. unsigned int fullbatt_vchkdrop_uV;
  160. unsigned int fullbatt_uV;
  161. unsigned int fullbatt_soc;
  162. unsigned int fullbatt_full_capacity;
  163. enum data_source battery_present;
  164. const char **psy_charger_stat;
  165. int num_charger_regulators;
  166. struct charger_regulator *charger_regulators;
  167. const char *psy_fuel_gauge;
  168. const char *thermal_zone;
  169. int temp_min;
  170. int temp_max;
  171. int temp_diff;
  172. bool measure_battery_temp;
  173. u32 charging_max_duration_ms;
  174. u32 discharging_max_duration_ms;
  175. };
  176. #define PSY_NAME_MAX 30
  177. /**
  178. * struct charger_manager
  179. * @entry: entry for list
  180. * @dev: device pointer
  181. * @desc: instance of charger_desc
  182. * @fuel_gauge: power_supply for fuel gauge
  183. * @charger_stat: array of power_supply for chargers
  184. * @tzd_batt : thermal zone device for battery
  185. * @charger_enabled: the state of charger
  186. * @fullbatt_vchk_jiffies_at:
  187. * jiffies at the time full battery check will occur.
  188. * @fullbatt_vchk_work: work queue for full battery check
  189. * @emergency_stop:
  190. * When setting true, stop charging
  191. * @psy_name_buf: the name of power-supply-class for charger manager
  192. * @charger_psy: power_supply for charger manager
  193. * @status_save_ext_pwr_inserted:
  194. * saved status of external power before entering suspend-to-RAM
  195. * @status_save_batt:
  196. * saved status of battery before entering suspend-to-RAM
  197. * @charging_start_time: saved start time of enabling charging
  198. * @charging_end_time: saved end time of disabling charging
  199. */
  200. struct charger_manager {
  201. struct list_head entry;
  202. struct device *dev;
  203. struct charger_desc *desc;
  204. #ifdef CONFIG_THERMAL
  205. struct thermal_zone_device *tzd_batt;
  206. #endif
  207. bool charger_enabled;
  208. unsigned long fullbatt_vchk_jiffies_at;
  209. struct delayed_work fullbatt_vchk_work;
  210. int emergency_stop;
  211. char psy_name_buf[PSY_NAME_MAX + 1];
  212. struct power_supply_desc charger_psy_desc;
  213. struct power_supply *charger_psy;
  214. u64 charging_start_time;
  215. u64 charging_end_time;
  216. };
  217. #ifdef CONFIG_CHARGER_MANAGER
  218. extern void cm_notify_event(struct power_supply *psy,
  219. enum cm_event_types type, char *msg);
  220. #else
  221. static inline void cm_notify_event(struct power_supply *psy,
  222. enum cm_event_types type, char *msg) { }
  223. #endif
  224. #endif /* _CHARGER_MANAGER_H */