charger-manager.h 6.9 KB

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