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