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> |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 20 | |
| 21 | enum data_source { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 22 | CM_BATTERY_PRESENT, |
| 23 | CM_NO_BATTERY, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 24 | CM_FUEL_GAUGE, |
| 25 | CM_CHARGER_STAT, |
| 26 | }; |
| 27 | |
| 28 | enum polling_modes { |
| 29 | CM_POLL_DISABLE = 0, |
| 30 | CM_POLL_ALWAYS, |
| 31 | CM_POLL_EXTERNAL_POWER_ONLY, |
| 32 | CM_POLL_CHARGING_ONLY, |
| 33 | }; |
| 34 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 35 | enum cm_event_types { |
| 36 | CM_EVENT_UNKNOWN = 0, |
| 37 | CM_EVENT_BATT_FULL, |
| 38 | CM_EVENT_BATT_IN, |
| 39 | CM_EVENT_BATT_OUT, |
| 40 | CM_EVENT_EXT_PWR_IN_OUT, |
| 41 | CM_EVENT_CHG_START_STOP, |
| 42 | CM_EVENT_OTHERS, |
| 43 | }; |
| 44 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 45 | /** |
| 46 | * struct charger_global_desc |
| 47 | * @rtc_name: the name of RTC used to wake up the system from suspend. |
| 48 | * @rtc_only_wakeup: |
| 49 | * If the system is woken up by waekup-sources other than the RTC or |
| 50 | * callbacks, Charger Manager should recognize with |
| 51 | * rtc_only_wakeup() returning false. |
| 52 | * If the RTC given to CM is the only wakeup reason, |
| 53 | * rtc_only_wakeup should return true. |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 54 | * @assume_timer_stops_in_suspend: |
| 55 | * Assume that the jiffy timer stops in suspend-to-RAM. |
| 56 | * When enabled, CM does not rely on jiffies value in |
| 57 | * suspend_again and assumes that jiffies value does not |
| 58 | * change during suspend. |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 59 | */ |
| 60 | struct charger_global_desc { |
| 61 | char *rtc_name; |
| 62 | |
| 63 | bool (*rtc_only_wakeup)(void); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 64 | |
| 65 | bool assume_timer_stops_in_suspend; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /** |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 69 | * struct charger_cable |
| 70 | * @extcon_name: the name of extcon device. |
| 71 | * @name: the name of charger cable(external connector). |
| 72 | * @extcon_dev: the extcon device. |
| 73 | * @wq: the workqueue to control charger according to the state of |
| 74 | * charger cable. If charger cable is attached, enable charger. |
| 75 | * But if charger cable is detached, disable charger. |
| 76 | * @nb: the notifier block to receive changed state from EXTCON |
| 77 | * (External Connector) when charger cable is attached/detached. |
| 78 | * @attached: the state of charger cable. |
| 79 | * true: the charger cable is attached |
| 80 | * false: the charger cable is detached |
| 81 | * @charger: the instance of struct charger_regulator. |
| 82 | * @cm: the Charger Manager representing the battery. |
| 83 | */ |
| 84 | struct charger_cable { |
| 85 | const char *extcon_name; |
| 86 | const char *name; |
| 87 | |
| 88 | /* The charger-manager use Exton framework*/ |
| 89 | struct extcon_specific_cable_nb extcon_dev; |
| 90 | struct work_struct wq; |
| 91 | struct notifier_block nb; |
| 92 | |
| 93 | /* The state of charger cable */ |
| 94 | bool attached; |
| 95 | |
| 96 | struct charger_regulator *charger; |
Chanwoo Choi | 45cd4fb | 2012-07-12 15:03:29 +0900 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Set min/max current of regulator to protect over-current issue |
| 100 | * according to a kind of charger cable when cable is attached. |
| 101 | */ |
| 102 | int min_uA; |
| 103 | int max_uA; |
| 104 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 105 | struct charger_manager *cm; |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * struct charger_regulator |
| 110 | * @regulator_name: the name of regulator for using charger. |
| 111 | * @consumer: the regulator consumer for the charger. |
| 112 | * @cables: |
| 113 | * the array of charger cables to enable/disable charger |
| 114 | * and set current limit according to constratint data of |
| 115 | * struct charger_cable if only charger cable included |
| 116 | * in the array of charger cables is attached/detached. |
| 117 | * @num_cables: the number of charger cables. |
| 118 | */ |
| 119 | struct charger_regulator { |
| 120 | /* The name of regulator for charging */ |
| 121 | const char *regulator_name; |
| 122 | struct regulator *consumer; |
| 123 | |
| 124 | /* |
| 125 | * Store constraint information related to current limit, |
| 126 | * each cable have different condition for charging. |
| 127 | */ |
| 128 | struct charger_cable *cables; |
| 129 | int num_cables; |
| 130 | }; |
| 131 | |
| 132 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 133 | * struct charger_desc |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 134 | * @psy_name: the name of power-supply-class for charger manager |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 135 | * @polling_mode: |
| 136 | * Determine which polling mode will be used |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 137 | * @fullbatt_vchkdrop_ms: |
| 138 | * @fullbatt_vchkdrop_uV: |
| 139 | * Check voltage drop after the battery is fully charged. |
| 140 | * If it has dropped more than fullbatt_vchkdrop_uV after |
| 141 | * fullbatt_vchkdrop_ms, CM will restart charging. |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 142 | * @fullbatt_uV: voltage in microvolt |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame^] | 143 | * If VBATT >= fullbatt_uV, it is assumed to be full. |
| 144 | * @fullbatt_soc: state of Charge in % |
| 145 | * If state of Charge >= fullbatt_soc, it is assumed to be full. |
| 146 | * @fullbatt_full_capacity: full capacity measure |
| 147 | * If full capacity of battery >= fullbatt_full_capacity, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 148 | * it is assumed to be full. |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 149 | * @polling_interval_ms: interval in millisecond at which |
| 150 | * charger manager will monitor battery health |
| 151 | * @battery_present: |
| 152 | * Specify where information for existance of battery can be obtained |
| 153 | * @psy_charger_stat: the names of power-supply for chargers |
| 154 | * @num_charger_regulator: the number of entries in charger_regulators |
Anton Vorontsov | c6b2744 | 2012-08-22 21:36:05 -0700 | [diff] [blame] | 155 | * @charger_regulators: array of charger regulators |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 156 | * @psy_fuel_gauge: the name of power-supply for fuel gauge |
| 157 | * @temperature_out_of_range: |
| 158 | * Determine whether the status is overheat or cold or normal. |
| 159 | * return_value > 0: overheat |
| 160 | * return_value == 0: normal |
| 161 | * return_value < 0: cold |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 162 | * @measure_battery_temp: |
| 163 | * true: measure battery temperature |
| 164 | * false: measure ambient temperature |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 165 | */ |
| 166 | struct charger_desc { |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 167 | char *psy_name; |
| 168 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 169 | enum polling_modes polling_mode; |
| 170 | unsigned int polling_interval_ms; |
| 171 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 172 | unsigned int fullbatt_vchkdrop_ms; |
| 173 | unsigned int fullbatt_vchkdrop_uV; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 174 | unsigned int fullbatt_uV; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame^] | 175 | unsigned int fullbatt_soc; |
| 176 | unsigned int fullbatt_full_capacity; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 177 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 178 | enum data_source battery_present; |
| 179 | |
| 180 | char **psy_charger_stat; |
| 181 | |
| 182 | int num_charger_regulators; |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 183 | struct charger_regulator *charger_regulators; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 184 | |
| 185 | char *psy_fuel_gauge; |
| 186 | |
| 187 | int (*temperature_out_of_range)(int *mC); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 188 | bool measure_battery_temp; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | #define PSY_NAME_MAX 30 |
| 192 | |
| 193 | /** |
| 194 | * struct charger_manager |
| 195 | * @entry: entry for list |
| 196 | * @dev: device pointer |
| 197 | * @desc: instance of charger_desc |
| 198 | * @fuel_gauge: power_supply for fuel gauge |
| 199 | * @charger_stat: array of power_supply for chargers |
| 200 | * @charger_enabled: the state of charger |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 201 | * @fullbatt_vchk_jiffies_at: |
| 202 | * jiffies at the time full battery check will occur. |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 203 | * @fullbatt_vchk_work: work queue for full battery check |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 204 | * @emergency_stop: |
| 205 | * When setting true, stop charging |
| 206 | * @last_temp_mC: the measured temperature in milli-Celsius |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 207 | * @psy_name_buf: the name of power-supply-class for charger manager |
| 208 | * @charger_psy: power_supply for charger manager |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 209 | * @status_save_ext_pwr_inserted: |
| 210 | * saved status of external power before entering suspend-to-RAM |
| 211 | * @status_save_batt: |
| 212 | * saved status of battery before entering suspend-to-RAM |
| 213 | */ |
| 214 | struct charger_manager { |
| 215 | struct list_head entry; |
| 216 | struct device *dev; |
| 217 | struct charger_desc *desc; |
| 218 | |
| 219 | struct power_supply *fuel_gauge; |
| 220 | struct power_supply **charger_stat; |
| 221 | |
| 222 | bool charger_enabled; |
| 223 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 224 | unsigned long fullbatt_vchk_jiffies_at; |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 225 | struct delayed_work fullbatt_vchk_work; |
| 226 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 227 | int emergency_stop; |
| 228 | int last_temp_mC; |
| 229 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 230 | char psy_name_buf[PSY_NAME_MAX + 1]; |
| 231 | struct power_supply charger_psy; |
| 232 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 233 | bool status_save_ext_pwr_inserted; |
| 234 | bool status_save_batt; |
| 235 | }; |
| 236 | |
| 237 | #ifdef CONFIG_CHARGER_MANAGER |
| 238 | extern int setup_charger_manager(struct charger_global_desc *gd); |
| 239 | extern bool cm_suspend_again(void); |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 240 | extern void cm_notify_event(struct power_supply *psy, |
| 241 | enum cm_event_types type, char *msg); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 242 | #else |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 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) { } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 248 | #endif |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 249 | #endif /* _CHARGER_MANAGER_H */ |