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 | * This driver enables to monitor battery health and control charger |
| 6 | * during suspend-to-mem. |
| 7 | * Charger manager depends on other devices. register this later than |
| 8 | * the depending devices. |
| 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 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/irq.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/rtc.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/power/charger-manager.h> |
| 26 | #include <linux/regulator/consumer.h> |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 27 | #include <linux/sysfs.h> |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 28 | #include <linux/of.h> |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 29 | #include <linux/thermal.h> |
| 30 | |
| 31 | /* |
| 32 | * Default termperature threshold for charging. |
| 33 | * Every temperature units are in tenth of centigrade. |
| 34 | */ |
| 35 | #define CM_DEFAULT_RECHARGE_TEMP_DIFF 50 |
| 36 | #define CM_DEFAULT_CHARGE_TEMP_MAX 500 |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 37 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 38 | static const char * const default_event_names[] = { |
| 39 | [CM_EVENT_UNKNOWN] = "Unknown", |
| 40 | [CM_EVENT_BATT_FULL] = "Battery Full", |
| 41 | [CM_EVENT_BATT_IN] = "Battery Inserted", |
| 42 | [CM_EVENT_BATT_OUT] = "Battery Pulled Out", |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 43 | [CM_EVENT_BATT_OVERHEAT] = "Battery Overheat", |
| 44 | [CM_EVENT_BATT_COLD] = "Battery Cold", |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 45 | [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach", |
| 46 | [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop", |
| 47 | [CM_EVENT_OTHERS] = "Other battery events" |
| 48 | }; |
| 49 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 50 | /* |
| 51 | * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for |
| 52 | * delayed works so that we can run delayed works with CM_JIFFIES_SMALL |
| 53 | * without any delays. |
| 54 | */ |
| 55 | #define CM_JIFFIES_SMALL (2) |
| 56 | |
| 57 | /* If y is valid (> 0) and smaller than x, do x = y */ |
| 58 | #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x)) |
| 59 | |
| 60 | /* |
| 61 | * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking |
| 62 | * rtc alarm. It should be 2 or larger |
| 63 | */ |
| 64 | #define CM_RTC_SMALL (2) |
| 65 | |
| 66 | #define UEVENT_BUF_SIZE 32 |
| 67 | |
| 68 | static LIST_HEAD(cm_list); |
| 69 | static DEFINE_MUTEX(cm_list_mtx); |
| 70 | |
| 71 | /* About in-suspend (suspend-again) monitoring */ |
| 72 | static struct rtc_device *rtc_dev; |
| 73 | /* |
| 74 | * Backup RTC alarm |
| 75 | * Save the wakeup alarm before entering suspend-to-RAM |
| 76 | */ |
| 77 | static struct rtc_wkalrm rtc_wkalarm_save; |
| 78 | /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */ |
| 79 | static unsigned long rtc_wkalarm_save_time; |
| 80 | static bool cm_suspended; |
| 81 | static bool cm_rtc_set; |
| 82 | static unsigned long cm_suspend_duration_ms; |
| 83 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 84 | /* About normal (not suspended) monitoring */ |
| 85 | static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */ |
| 86 | static unsigned long next_polling; /* Next appointed polling time */ |
| 87 | static struct workqueue_struct *cm_wq; /* init at driver add */ |
| 88 | static struct delayed_work cm_monitor_work; /* init at driver add */ |
| 89 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 90 | /* Global charger-manager description */ |
| 91 | static struct charger_global_desc *g_desc; /* init with setup_charger_manager */ |
| 92 | |
| 93 | /** |
| 94 | * is_batt_present - See if the battery presents in place. |
| 95 | * @cm: the Charger Manager representing the battery. |
| 96 | */ |
| 97 | static bool is_batt_present(struct charger_manager *cm) |
| 98 | { |
| 99 | union power_supply_propval val; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 100 | struct power_supply *psy; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 101 | bool present = false; |
| 102 | int i, ret; |
| 103 | |
| 104 | switch (cm->desc->battery_present) { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 105 | case CM_BATTERY_PRESENT: |
| 106 | present = true; |
| 107 | break; |
| 108 | case CM_NO_BATTERY: |
| 109 | break; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 110 | case CM_FUEL_GAUGE: |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 111 | psy = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 112 | if (!psy) |
| 113 | break; |
| 114 | |
| 115 | ret = psy->get_property(psy, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 116 | POWER_SUPPLY_PROP_PRESENT, &val); |
| 117 | if (ret == 0 && val.intval) |
| 118 | present = true; |
| 119 | break; |
| 120 | case CM_CHARGER_STAT: |
| 121 | for (i = 0; cm->charger_stat[i]; i++) { |
| 122 | ret = cm->charger_stat[i]->get_property( |
| 123 | cm->charger_stat[i], |
| 124 | POWER_SUPPLY_PROP_PRESENT, &val); |
| 125 | if (ret == 0 && val.intval) { |
| 126 | present = true; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | return present; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * is_ext_pwr_online - See if an external power source is attached to charge |
| 138 | * @cm: the Charger Manager representing the battery. |
| 139 | * |
| 140 | * Returns true if at least one of the chargers of the battery has an external |
| 141 | * power source attached to charge the battery regardless of whether it is |
| 142 | * actually charging or not. |
| 143 | */ |
| 144 | static bool is_ext_pwr_online(struct charger_manager *cm) |
| 145 | { |
| 146 | union power_supply_propval val; |
| 147 | bool online = false; |
| 148 | int i, ret; |
| 149 | |
| 150 | /* If at least one of them has one, it's yes. */ |
| 151 | for (i = 0; cm->charger_stat[i]; i++) { |
| 152 | ret = cm->charger_stat[i]->get_property( |
| 153 | cm->charger_stat[i], |
| 154 | POWER_SUPPLY_PROP_ONLINE, &val); |
| 155 | if (ret == 0 && val.intval) { |
| 156 | online = true; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return online; |
| 162 | } |
| 163 | |
| 164 | /** |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 165 | * get_batt_uV - Get the voltage level of the battery |
| 166 | * @cm: the Charger Manager representing the battery. |
| 167 | * @uV: the voltage level returned. |
| 168 | * |
| 169 | * Returns 0 if there is no error. |
| 170 | * Returns a negative value on error. |
| 171 | */ |
| 172 | static int get_batt_uV(struct charger_manager *cm, int *uV) |
| 173 | { |
| 174 | union power_supply_propval val; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 175 | struct power_supply *fuel_gauge; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 176 | int ret; |
| 177 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 178 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 179 | if (!fuel_gauge) |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 180 | return -ENODEV; |
| 181 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 182 | ret = fuel_gauge->get_property(fuel_gauge, |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 183 | POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 184 | if (ret) |
| 185 | return ret; |
| 186 | |
| 187 | *uV = val.intval; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 192 | * is_charging - Returns true if the battery is being charged. |
| 193 | * @cm: the Charger Manager representing the battery. |
| 194 | */ |
| 195 | static bool is_charging(struct charger_manager *cm) |
| 196 | { |
| 197 | int i, ret; |
| 198 | bool charging = false; |
| 199 | union power_supply_propval val; |
| 200 | |
| 201 | /* If there is no battery, it cannot be charged */ |
| 202 | if (!is_batt_present(cm)) |
| 203 | return false; |
| 204 | |
| 205 | /* If at least one of the charger is charging, return yes */ |
| 206 | for (i = 0; cm->charger_stat[i]; i++) { |
| 207 | /* 1. The charger sholuld not be DISABLED */ |
| 208 | if (cm->emergency_stop) |
| 209 | continue; |
| 210 | if (!cm->charger_enabled) |
| 211 | continue; |
| 212 | |
| 213 | /* 2. The charger should be online (ext-power) */ |
| 214 | ret = cm->charger_stat[i]->get_property( |
| 215 | cm->charger_stat[i], |
| 216 | POWER_SUPPLY_PROP_ONLINE, &val); |
| 217 | if (ret) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 218 | dev_warn(cm->dev, "Cannot read ONLINE value from %s\n", |
| 219 | cm->desc->psy_charger_stat[i]); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 220 | continue; |
| 221 | } |
| 222 | if (val.intval == 0) |
| 223 | continue; |
| 224 | |
| 225 | /* |
| 226 | * 3. The charger should not be FULL, DISCHARGING, |
| 227 | * or NOT_CHARGING. |
| 228 | */ |
| 229 | ret = cm->charger_stat[i]->get_property( |
| 230 | cm->charger_stat[i], |
| 231 | POWER_SUPPLY_PROP_STATUS, &val); |
| 232 | if (ret) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 233 | dev_warn(cm->dev, "Cannot read STATUS value from %s\n", |
| 234 | cm->desc->psy_charger_stat[i]); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 235 | continue; |
| 236 | } |
| 237 | if (val.intval == POWER_SUPPLY_STATUS_FULL || |
| 238 | val.intval == POWER_SUPPLY_STATUS_DISCHARGING || |
| 239 | val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) |
| 240 | continue; |
| 241 | |
| 242 | /* Then, this is charging. */ |
| 243 | charging = true; |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | return charging; |
| 248 | } |
| 249 | |
| 250 | /** |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 251 | * is_full_charged - Returns true if the battery is fully charged. |
| 252 | * @cm: the Charger Manager representing the battery. |
| 253 | */ |
| 254 | static bool is_full_charged(struct charger_manager *cm) |
| 255 | { |
| 256 | struct charger_desc *desc = cm->desc; |
| 257 | union power_supply_propval val; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 258 | struct power_supply *fuel_gauge; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 259 | int ret = 0; |
| 260 | int uV; |
| 261 | |
| 262 | /* If there is no battery, it cannot be charged */ |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 263 | if (!is_batt_present(cm)) |
| 264 | return false; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 265 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 266 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 267 | if (!fuel_gauge) |
| 268 | return false; |
| 269 | |
| 270 | if (desc->fullbatt_full_capacity > 0) { |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 271 | val.intval = 0; |
| 272 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 273 | /* Not full if capacity of fuel gauge isn't full */ |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 274 | ret = fuel_gauge->get_property(fuel_gauge, |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 275 | POWER_SUPPLY_PROP_CHARGE_FULL, &val); |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 276 | if (!ret && val.intval > desc->fullbatt_full_capacity) |
| 277 | return true; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* Full, if it's over the fullbatt voltage */ |
| 281 | if (desc->fullbatt_uV > 0) { |
| 282 | ret = get_batt_uV(cm, &uV); |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 283 | if (!ret && uV >= desc->fullbatt_uV) |
| 284 | return true; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* Full, if the capacity is more than fullbatt_soc */ |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 288 | if (desc->fullbatt_soc > 0) { |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 289 | val.intval = 0; |
| 290 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 291 | ret = fuel_gauge->get_property(fuel_gauge, |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 292 | POWER_SUPPLY_PROP_CAPACITY, &val); |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 293 | if (!ret && val.intval >= desc->fullbatt_soc) |
| 294 | return true; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 295 | } |
| 296 | |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 297 | return false; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 301 | * is_polling_required - Return true if need to continue polling for this CM. |
| 302 | * @cm: the Charger Manager representing the battery. |
| 303 | */ |
| 304 | static bool is_polling_required(struct charger_manager *cm) |
| 305 | { |
| 306 | switch (cm->desc->polling_mode) { |
| 307 | case CM_POLL_DISABLE: |
| 308 | return false; |
| 309 | case CM_POLL_ALWAYS: |
| 310 | return true; |
| 311 | case CM_POLL_EXTERNAL_POWER_ONLY: |
| 312 | return is_ext_pwr_online(cm); |
| 313 | case CM_POLL_CHARGING_ONLY: |
| 314 | return is_charging(cm); |
| 315 | default: |
| 316 | dev_warn(cm->dev, "Incorrect polling_mode (%d)\n", |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 317 | cm->desc->polling_mode); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * try_charger_enable - Enable/Disable chargers altogether |
| 325 | * @cm: the Charger Manager representing the battery. |
| 326 | * @enable: true: enable / false: disable |
| 327 | * |
| 328 | * Note that Charger Manager keeps the charger enabled regardless whether |
| 329 | * the charger is charging or not (because battery is full or no external |
| 330 | * power source exists) except when CM needs to disable chargers forcibly |
| 331 | * bacause of emergency causes; when the battery is overheated or too cold. |
| 332 | */ |
| 333 | static int try_charger_enable(struct charger_manager *cm, bool enable) |
| 334 | { |
| 335 | int err = 0, i; |
| 336 | struct charger_desc *desc = cm->desc; |
| 337 | |
| 338 | /* Ignore if it's redundent command */ |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 339 | if (enable == cm->charger_enabled) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 340 | return 0; |
| 341 | |
| 342 | if (enable) { |
| 343 | if (cm->emergency_stop) |
| 344 | return -EAGAIN; |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 345 | |
| 346 | /* |
| 347 | * Save start time of charging to limit |
| 348 | * maximum possible charging time. |
| 349 | */ |
| 350 | cm->charging_start_time = ktime_to_ms(ktime_get()); |
| 351 | cm->charging_end_time = 0; |
| 352 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 353 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 354 | if (desc->charger_regulators[i].externally_control) |
| 355 | continue; |
| 356 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 357 | err = regulator_enable(desc->charger_regulators[i].consumer); |
| 358 | if (err < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 359 | dev_warn(cm->dev, "Cannot enable %s regulator\n", |
| 360 | desc->charger_regulators[i].regulator_name); |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 361 | } |
| 362 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 363 | } else { |
| 364 | /* |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 365 | * Save end time of charging to maintain fully charged state |
| 366 | * of battery after full-batt. |
| 367 | */ |
| 368 | cm->charging_start_time = 0; |
| 369 | cm->charging_end_time = ktime_to_ms(ktime_get()); |
| 370 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 371 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 372 | if (desc->charger_regulators[i].externally_control) |
| 373 | continue; |
| 374 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 375 | err = regulator_disable(desc->charger_regulators[i].consumer); |
| 376 | if (err < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 377 | dev_warn(cm->dev, "Cannot disable %s regulator\n", |
| 378 | desc->charger_regulators[i].regulator_name); |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 382 | /* |
| 383 | * Abnormal battery state - Stop charging forcibly, |
| 384 | * even if charger was enabled at the other places |
| 385 | */ |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 386 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 387 | if (regulator_is_enabled( |
| 388 | desc->charger_regulators[i].consumer)) { |
| 389 | regulator_force_disable( |
| 390 | desc->charger_regulators[i].consumer); |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 391 | dev_warn(cm->dev, "Disable regulator(%s) forcibly\n", |
| 392 | desc->charger_regulators[i].regulator_name); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (!err) |
| 398 | cm->charger_enabled = enable; |
| 399 | |
| 400 | return err; |
| 401 | } |
| 402 | |
| 403 | /** |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 404 | * try_charger_restart - Restart charging. |
| 405 | * @cm: the Charger Manager representing the battery. |
| 406 | * |
| 407 | * Restart charging by turning off and on the charger. |
| 408 | */ |
| 409 | static int try_charger_restart(struct charger_manager *cm) |
| 410 | { |
| 411 | int err; |
| 412 | |
| 413 | if (cm->emergency_stop) |
| 414 | return -EAGAIN; |
| 415 | |
| 416 | err = try_charger_enable(cm, false); |
| 417 | if (err) |
| 418 | return err; |
| 419 | |
| 420 | return try_charger_enable(cm, true); |
| 421 | } |
| 422 | |
| 423 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 424 | * uevent_notify - Let users know something has changed. |
| 425 | * @cm: the Charger Manager representing the battery. |
| 426 | * @event: the event string. |
| 427 | * |
| 428 | * If @event is null, it implies that uevent_notify is called |
| 429 | * by resume function. When called in the resume function, cm_suspended |
| 430 | * should be already reset to false in order to let uevent_notify |
| 431 | * notify the recent event during the suspend to users. While |
| 432 | * suspended, uevent_notify does not notify users, but tracks |
| 433 | * events so that uevent_notify can notify users later after resumed. |
| 434 | */ |
| 435 | static void uevent_notify(struct charger_manager *cm, const char *event) |
| 436 | { |
| 437 | static char env_str[UEVENT_BUF_SIZE + 1] = ""; |
| 438 | static char env_str_save[UEVENT_BUF_SIZE + 1] = ""; |
| 439 | |
| 440 | if (cm_suspended) { |
| 441 | /* Nothing in suspended-event buffer */ |
| 442 | if (env_str_save[0] == 0) { |
| 443 | if (!strncmp(env_str, event, UEVENT_BUF_SIZE)) |
| 444 | return; /* status not changed */ |
| 445 | strncpy(env_str_save, event, UEVENT_BUF_SIZE); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE)) |
| 450 | return; /* Duplicated. */ |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 451 | strncpy(env_str_save, event, UEVENT_BUF_SIZE); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 452 | return; |
| 453 | } |
| 454 | |
| 455 | if (event == NULL) { |
| 456 | /* No messages pending */ |
| 457 | if (!env_str_save[0]) |
| 458 | return; |
| 459 | |
| 460 | strncpy(env_str, env_str_save, UEVENT_BUF_SIZE); |
| 461 | kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE); |
| 462 | env_str_save[0] = 0; |
| 463 | |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | /* status not changed */ |
| 468 | if (!strncmp(env_str, event, UEVENT_BUF_SIZE)) |
| 469 | return; |
| 470 | |
| 471 | /* save the status and notify the update */ |
| 472 | strncpy(env_str, event, UEVENT_BUF_SIZE); |
| 473 | kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE); |
| 474 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 475 | dev_info(cm->dev, "%s\n", event); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 479 | * fullbatt_vchk - Check voltage drop some times after "FULL" event. |
| 480 | * @work: the work_struct appointing the function |
| 481 | * |
| 482 | * If a user has designated "fullbatt_vchkdrop_ms/uV" values with |
| 483 | * charger_desc, Charger Manager checks voltage drop after the battery |
| 484 | * "FULL" event. It checks whether the voltage has dropped more than |
| 485 | * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms. |
| 486 | */ |
| 487 | static void fullbatt_vchk(struct work_struct *work) |
| 488 | { |
| 489 | struct delayed_work *dwork = to_delayed_work(work); |
| 490 | struct charger_manager *cm = container_of(dwork, |
| 491 | struct charger_manager, fullbatt_vchk_work); |
| 492 | struct charger_desc *desc = cm->desc; |
| 493 | int batt_uV, err, diff; |
| 494 | |
| 495 | /* remove the appointment for fullbatt_vchk */ |
| 496 | cm->fullbatt_vchk_jiffies_at = 0; |
| 497 | |
| 498 | if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms) |
| 499 | return; |
| 500 | |
| 501 | err = get_batt_uV(cm, &batt_uV); |
| 502 | if (err) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 503 | dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 504 | return; |
| 505 | } |
| 506 | |
Chanwoo Choi | f36b9dd | 2012-11-22 16:53:51 +0900 | [diff] [blame] | 507 | diff = desc->fullbatt_uV - batt_uV; |
| 508 | if (diff < 0) |
| 509 | return; |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 510 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 511 | dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 512 | |
| 513 | if (diff > desc->fullbatt_vchkdrop_uV) { |
| 514 | try_charger_restart(cm); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 515 | uevent_notify(cm, "Recharging"); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 520 | * check_charging_duration - Monitor charging/discharging duration |
| 521 | * @cm: the Charger Manager representing the battery. |
| 522 | * |
| 523 | * If whole charging duration exceed 'charging_max_duration_ms', |
| 524 | * cm stop charging to prevent overcharge/overheat. If discharging |
| 525 | * duration exceed 'discharging _max_duration_ms', charger cable is |
| 526 | * attached, after full-batt, cm start charging to maintain fully |
| 527 | * charged state for battery. |
| 528 | */ |
| 529 | static int check_charging_duration(struct charger_manager *cm) |
| 530 | { |
| 531 | struct charger_desc *desc = cm->desc; |
| 532 | u64 curr = ktime_to_ms(ktime_get()); |
| 533 | u64 duration; |
| 534 | int ret = false; |
| 535 | |
| 536 | if (!desc->charging_max_duration_ms && |
| 537 | !desc->discharging_max_duration_ms) |
| 538 | return ret; |
| 539 | |
| 540 | if (cm->charger_enabled) { |
| 541 | duration = curr - cm->charging_start_time; |
| 542 | |
| 543 | if (duration > desc->charging_max_duration_ms) { |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 544 | dev_info(cm->dev, "Charging duration exceed %ums\n", |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 545 | desc->charging_max_duration_ms); |
| 546 | uevent_notify(cm, "Discharging"); |
| 547 | try_charger_enable(cm, false); |
| 548 | ret = true; |
| 549 | } |
| 550 | } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) { |
| 551 | duration = curr - cm->charging_end_time; |
| 552 | |
| 553 | if (duration > desc->charging_max_duration_ms && |
| 554 | is_ext_pwr_online(cm)) { |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 555 | dev_info(cm->dev, "Discharging duration exceed %ums\n", |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 556 | desc->discharging_max_duration_ms); |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 557 | uevent_notify(cm, "Recharging"); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 558 | try_charger_enable(cm, true); |
| 559 | ret = true; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 566 | static int cm_get_battery_temperature_by_psy(struct charger_manager *cm, |
| 567 | int *temp) |
| 568 | { |
| 569 | struct power_supply *fuel_gauge; |
| 570 | |
| 571 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 572 | if (!fuel_gauge) |
| 573 | return -ENODEV; |
| 574 | |
| 575 | return fuel_gauge->get_property(fuel_gauge, |
| 576 | POWER_SUPPLY_PROP_TEMP, |
| 577 | (union power_supply_propval *)temp); |
| 578 | } |
| 579 | |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 580 | static int cm_get_battery_temperature(struct charger_manager *cm, |
| 581 | int *temp) |
| 582 | { |
| 583 | int ret; |
| 584 | |
| 585 | if (!cm->desc->measure_battery_temp) |
| 586 | return -ENODEV; |
| 587 | |
| 588 | #ifdef CONFIG_THERMAL |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 589 | if (cm->tzd_batt) { |
| 590 | ret = thermal_zone_get_temp(cm->tzd_batt, (unsigned long *)temp); |
| 591 | if (!ret) |
| 592 | /* Calibrate temperature unit */ |
| 593 | *temp /= 100; |
| 594 | } else |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 595 | #endif |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 596 | { |
| 597 | /* if-else continued from CONFIG_THERMAL */ |
| 598 | ret = cm_get_battery_temperature_by_psy(cm, temp); |
| 599 | } |
| 600 | |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static int cm_check_thermal_status(struct charger_manager *cm) |
| 605 | { |
| 606 | struct charger_desc *desc = cm->desc; |
| 607 | int temp, upper_limit, lower_limit; |
| 608 | int ret = 0; |
| 609 | |
| 610 | ret = cm_get_battery_temperature(cm, &temp); |
| 611 | if (ret) { |
| 612 | /* FIXME: |
| 613 | * No information of battery temperature might |
| 614 | * occur hazadous result. We have to handle it |
| 615 | * depending on battery type. |
| 616 | */ |
| 617 | dev_err(cm->dev, "Failed to get battery temperature\n"); |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | upper_limit = desc->temp_max; |
| 622 | lower_limit = desc->temp_min; |
| 623 | |
| 624 | if (cm->emergency_stop) { |
| 625 | upper_limit -= desc->temp_diff; |
| 626 | lower_limit += desc->temp_diff; |
| 627 | } |
| 628 | |
| 629 | if (temp > upper_limit) |
| 630 | ret = CM_EVENT_BATT_OVERHEAT; |
| 631 | else if (temp < lower_limit) |
| 632 | ret = CM_EVENT_BATT_COLD; |
| 633 | |
| 634 | return ret; |
| 635 | } |
| 636 | |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 637 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 638 | * _cm_monitor - Monitor the temperature and return true for exceptions. |
| 639 | * @cm: the Charger Manager representing the battery. |
| 640 | * |
| 641 | * Returns true if there is an event to notify for the battery. |
| 642 | * (True if the status of "emergency_stop" changes) |
| 643 | */ |
| 644 | static bool _cm_monitor(struct charger_manager *cm) |
| 645 | { |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 646 | int temp_alrt; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 647 | |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 648 | temp_alrt = cm_check_thermal_status(cm); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 649 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 650 | /* It has been stopped already */ |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 651 | if (temp_alrt && cm->emergency_stop) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 652 | return false; |
| 653 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 654 | /* |
| 655 | * Check temperature whether overheat or cold. |
| 656 | * If temperature is out of range normal state, stop charging. |
| 657 | */ |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 658 | if (temp_alrt) { |
| 659 | cm->emergency_stop = temp_alrt; |
| 660 | if (!try_charger_enable(cm, false)) |
| 661 | uevent_notify(cm, default_event_names[temp_alrt]); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 662 | |
| 663 | /* |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 664 | * Check whole charging duration and discharing duration |
| 665 | * after full-batt. |
| 666 | */ |
| 667 | } else if (!cm->emergency_stop && check_charging_duration(cm)) { |
| 668 | dev_dbg(cm->dev, |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 669 | "Charging/Discharging duration is out of range\n"); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 670 | /* |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 671 | * Check dropped voltage of battery. If battery voltage is more |
| 672 | * dropped than fullbatt_vchkdrop_uV after fully charged state, |
| 673 | * charger-manager have to recharge battery. |
| 674 | */ |
| 675 | } else if (!cm->emergency_stop && is_ext_pwr_online(cm) && |
| 676 | !cm->charger_enabled) { |
| 677 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
| 678 | |
| 679 | /* |
| 680 | * Check whether fully charged state to protect overcharge |
| 681 | * if charger-manager is charging for battery. |
| 682 | */ |
| 683 | } else if (!cm->emergency_stop && is_full_charged(cm) && |
| 684 | cm->charger_enabled) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 685 | dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n"); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 686 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); |
| 687 | |
| 688 | try_charger_enable(cm, false); |
| 689 | |
| 690 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 691 | } else { |
| 692 | cm->emergency_stop = 0; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 693 | if (is_ext_pwr_online(cm)) { |
| 694 | if (!try_charger_enable(cm, true)) |
| 695 | uevent_notify(cm, "CHARGING"); |
| 696 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | return true; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * cm_monitor - Monitor every battery. |
| 704 | * |
| 705 | * Returns true if there is an event to notify from any of the batteries. |
| 706 | * (True if the status of "emergency_stop" changes) |
| 707 | */ |
| 708 | static bool cm_monitor(void) |
| 709 | { |
| 710 | bool stop = false; |
| 711 | struct charger_manager *cm; |
| 712 | |
| 713 | mutex_lock(&cm_list_mtx); |
| 714 | |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 715 | list_for_each_entry(cm, &cm_list, entry) { |
| 716 | if (_cm_monitor(cm)) |
| 717 | stop = true; |
| 718 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 719 | |
| 720 | mutex_unlock(&cm_list_mtx); |
| 721 | |
| 722 | return stop; |
| 723 | } |
| 724 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 725 | /** |
| 726 | * _setup_polling - Setup the next instance of polling. |
| 727 | * @work: work_struct of the function _setup_polling. |
| 728 | */ |
| 729 | static void _setup_polling(struct work_struct *work) |
| 730 | { |
| 731 | unsigned long min = ULONG_MAX; |
| 732 | struct charger_manager *cm; |
| 733 | bool keep_polling = false; |
| 734 | unsigned long _next_polling; |
| 735 | |
| 736 | mutex_lock(&cm_list_mtx); |
| 737 | |
| 738 | list_for_each_entry(cm, &cm_list, entry) { |
| 739 | if (is_polling_required(cm) && cm->desc->polling_interval_ms) { |
| 740 | keep_polling = true; |
| 741 | |
| 742 | if (min > cm->desc->polling_interval_ms) |
| 743 | min = cm->desc->polling_interval_ms; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | polling_jiffy = msecs_to_jiffies(min); |
| 748 | if (polling_jiffy <= CM_JIFFIES_SMALL) |
| 749 | polling_jiffy = CM_JIFFIES_SMALL + 1; |
| 750 | |
| 751 | if (!keep_polling) |
| 752 | polling_jiffy = ULONG_MAX; |
| 753 | if (polling_jiffy == ULONG_MAX) |
| 754 | goto out; |
| 755 | |
| 756 | WARN(cm_wq == NULL, "charger-manager: workqueue not initialized" |
| 757 | ". try it later. %s\n", __func__); |
| 758 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 759 | /* |
| 760 | * Use mod_delayed_work() iff the next polling interval should |
| 761 | * occur before the currently scheduled one. If @cm_monitor_work |
| 762 | * isn't active, the end result is the same, so no need to worry |
| 763 | * about stale @next_polling. |
| 764 | */ |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 765 | _next_polling = jiffies + polling_jiffy; |
| 766 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 767 | if (time_before(_next_polling, next_polling)) { |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 768 | mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy); |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 769 | next_polling = _next_polling; |
| 770 | } else { |
| 771 | if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy)) |
| 772 | next_polling = _next_polling; |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 773 | } |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 774 | out: |
| 775 | mutex_unlock(&cm_list_mtx); |
| 776 | } |
| 777 | static DECLARE_WORK(setup_polling, _setup_polling); |
| 778 | |
| 779 | /** |
| 780 | * cm_monitor_poller - The Monitor / Poller. |
| 781 | * @work: work_struct of the function cm_monitor_poller |
| 782 | * |
| 783 | * During non-suspended state, cm_monitor_poller is used to poll and monitor |
| 784 | * the batteries. |
| 785 | */ |
| 786 | static void cm_monitor_poller(struct work_struct *work) |
| 787 | { |
| 788 | cm_monitor(); |
| 789 | schedule_work(&setup_polling); |
| 790 | } |
| 791 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 792 | /** |
| 793 | * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL |
| 794 | * @cm: the Charger Manager representing the battery. |
| 795 | */ |
| 796 | static void fullbatt_handler(struct charger_manager *cm) |
| 797 | { |
| 798 | struct charger_desc *desc = cm->desc; |
| 799 | |
| 800 | if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms) |
| 801 | goto out; |
| 802 | |
| 803 | if (cm_suspended) |
| 804 | device_set_wakeup_capable(cm->dev, true); |
| 805 | |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 806 | mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work, |
| 807 | msecs_to_jiffies(desc->fullbatt_vchkdrop_ms)); |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 808 | cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies( |
| 809 | desc->fullbatt_vchkdrop_ms); |
| 810 | |
| 811 | if (cm->fullbatt_vchk_jiffies_at == 0) |
| 812 | cm->fullbatt_vchk_jiffies_at = 1; |
| 813 | |
| 814 | out: |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 815 | dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n"); |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 816 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * battout_handler - Event handler for CM_EVENT_BATT_OUT |
| 821 | * @cm: the Charger Manager representing the battery. |
| 822 | */ |
| 823 | static void battout_handler(struct charger_manager *cm) |
| 824 | { |
| 825 | if (cm_suspended) |
| 826 | device_set_wakeup_capable(cm->dev, true); |
| 827 | |
| 828 | if (!is_batt_present(cm)) { |
| 829 | dev_emerg(cm->dev, "Battery Pulled Out!\n"); |
| 830 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]); |
| 831 | } else { |
| 832 | uevent_notify(cm, "Battery Reinserted?"); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * misc_event_handler - Handler for other evnets |
| 838 | * @cm: the Charger Manager representing the battery. |
| 839 | * @type: the Charger Manager representing the battery. |
| 840 | */ |
| 841 | static void misc_event_handler(struct charger_manager *cm, |
| 842 | enum cm_event_types type) |
| 843 | { |
| 844 | if (cm_suspended) |
| 845 | device_set_wakeup_capable(cm->dev, true); |
| 846 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 847 | if (is_polling_required(cm) && cm->desc->polling_interval_ms) |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 848 | schedule_work(&setup_polling); |
| 849 | uevent_notify(cm, default_event_names[type]); |
| 850 | } |
| 851 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 852 | static int charger_get_property(struct power_supply *psy, |
| 853 | enum power_supply_property psp, |
| 854 | union power_supply_propval *val) |
| 855 | { |
| 856 | struct charger_manager *cm = container_of(psy, |
| 857 | struct charger_manager, charger_psy); |
| 858 | struct charger_desc *desc = cm->desc; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 859 | struct power_supply *fuel_gauge; |
Anton Vorontsov | df58c04 | 2012-03-15 21:01:28 +0400 | [diff] [blame] | 860 | int ret = 0; |
| 861 | int uV; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 862 | |
| 863 | switch (psp) { |
| 864 | case POWER_SUPPLY_PROP_STATUS: |
| 865 | if (is_charging(cm)) |
| 866 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 867 | else if (is_ext_pwr_online(cm)) |
| 868 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 869 | else |
| 870 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 871 | break; |
| 872 | case POWER_SUPPLY_PROP_HEALTH: |
| 873 | if (cm->emergency_stop > 0) |
| 874 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 875 | else if (cm->emergency_stop < 0) |
| 876 | val->intval = POWER_SUPPLY_HEALTH_COLD; |
| 877 | else |
| 878 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 879 | break; |
| 880 | case POWER_SUPPLY_PROP_PRESENT: |
| 881 | if (is_batt_present(cm)) |
| 882 | val->intval = 1; |
| 883 | else |
| 884 | val->intval = 0; |
| 885 | break; |
| 886 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Anton Vorontsov | df58c04 | 2012-03-15 21:01:28 +0400 | [diff] [blame] | 887 | ret = get_batt_uV(cm, &val->intval); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 888 | break; |
| 889 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 890 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 891 | if (!fuel_gauge) { |
| 892 | ret = -ENODEV; |
| 893 | break; |
| 894 | } |
| 895 | ret = fuel_gauge->get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 896 | POWER_SUPPLY_PROP_CURRENT_NOW, val); |
| 897 | break; |
| 898 | case POWER_SUPPLY_PROP_TEMP: |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 899 | case POWER_SUPPLY_PROP_TEMP_AMBIENT: |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 900 | return cm_get_battery_temperature(cm, &val->intval); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 901 | case POWER_SUPPLY_PROP_CAPACITY: |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 902 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 903 | if (!fuel_gauge) { |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 904 | ret = -ENODEV; |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | if (!is_batt_present(cm)) { |
| 909 | /* There is no battery. Assume 100% */ |
| 910 | val->intval = 100; |
| 911 | break; |
| 912 | } |
| 913 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 914 | ret = fuel_gauge->get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 915 | POWER_SUPPLY_PROP_CAPACITY, val); |
| 916 | if (ret) |
| 917 | break; |
| 918 | |
| 919 | if (val->intval > 100) { |
| 920 | val->intval = 100; |
| 921 | break; |
| 922 | } |
| 923 | if (val->intval < 0) |
| 924 | val->intval = 0; |
| 925 | |
| 926 | /* Do not adjust SOC when charging: voltage is overrated */ |
| 927 | if (is_charging(cm)) |
| 928 | break; |
| 929 | |
| 930 | /* |
| 931 | * If the capacity value is inconsistent, calibrate it base on |
| 932 | * the battery voltage values and the thresholds given as desc |
| 933 | */ |
| 934 | ret = get_batt_uV(cm, &uV); |
| 935 | if (ret) { |
| 936 | /* Voltage information not available. No calibration */ |
| 937 | ret = 0; |
| 938 | break; |
| 939 | } |
| 940 | |
| 941 | if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV && |
| 942 | !is_charging(cm)) { |
| 943 | val->intval = 100; |
| 944 | break; |
| 945 | } |
| 946 | |
| 947 | break; |
| 948 | case POWER_SUPPLY_PROP_ONLINE: |
| 949 | if (is_ext_pwr_online(cm)) |
| 950 | val->intval = 1; |
| 951 | else |
| 952 | val->intval = 0; |
| 953 | break; |
| 954 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 955 | if (is_full_charged(cm)) |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 956 | val->intval = 1; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 957 | else |
| 958 | val->intval = 0; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 959 | ret = 0; |
| 960 | break; |
| 961 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 962 | if (is_charging(cm)) { |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 963 | fuel_gauge = power_supply_get_by_name( |
| 964 | cm->desc->psy_fuel_gauge); |
| 965 | if (!fuel_gauge) { |
| 966 | ret = -ENODEV; |
| 967 | break; |
| 968 | } |
| 969 | |
| 970 | ret = fuel_gauge->get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 971 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 972 | val); |
| 973 | if (ret) { |
| 974 | val->intval = 1; |
| 975 | ret = 0; |
| 976 | } else { |
| 977 | /* If CHARGE_NOW is supplied, use it */ |
| 978 | val->intval = (val->intval > 0) ? |
| 979 | val->intval : 1; |
| 980 | } |
| 981 | } else { |
| 982 | val->intval = 0; |
| 983 | } |
| 984 | break; |
| 985 | default: |
| 986 | return -EINVAL; |
| 987 | } |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | #define NUM_CHARGER_PSY_OPTIONAL (4) |
| 992 | static enum power_supply_property default_charger_props[] = { |
| 993 | /* Guaranteed to provide */ |
| 994 | POWER_SUPPLY_PROP_STATUS, |
| 995 | POWER_SUPPLY_PROP_HEALTH, |
| 996 | POWER_SUPPLY_PROP_PRESENT, |
| 997 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 998 | POWER_SUPPLY_PROP_CAPACITY, |
| 999 | POWER_SUPPLY_PROP_ONLINE, |
| 1000 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 1001 | /* |
| 1002 | * Optional properties are: |
| 1003 | * POWER_SUPPLY_PROP_CHARGE_NOW, |
| 1004 | * POWER_SUPPLY_PROP_CURRENT_NOW, |
| 1005 | * POWER_SUPPLY_PROP_TEMP, and |
| 1006 | * POWER_SUPPLY_PROP_TEMP_AMBIENT, |
| 1007 | */ |
| 1008 | }; |
| 1009 | |
| 1010 | static struct power_supply psy_default = { |
| 1011 | .name = "battery", |
| 1012 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 1013 | .properties = default_charger_props, |
| 1014 | .num_properties = ARRAY_SIZE(default_charger_props), |
| 1015 | .get_property = charger_get_property, |
Krzysztof Kozlowski | ba9c918 | 2014-10-07 17:47:37 +0200 | [diff] [blame] | 1016 | .no_thermal = true, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1017 | }; |
| 1018 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1019 | /** |
| 1020 | * cm_setup_timer - For in-suspend monitoring setup wakeup alarm |
| 1021 | * for suspend_again. |
| 1022 | * |
| 1023 | * Returns true if the alarm is set for Charger Manager to use. |
| 1024 | * Returns false if |
| 1025 | * cm_setup_timer fails to set an alarm, |
| 1026 | * cm_setup_timer does not need to set an alarm for Charger Manager, |
| 1027 | * or an alarm previously configured is to be used. |
| 1028 | */ |
| 1029 | static bool cm_setup_timer(void) |
| 1030 | { |
| 1031 | struct charger_manager *cm; |
| 1032 | unsigned int wakeup_ms = UINT_MAX; |
| 1033 | bool ret = false; |
| 1034 | |
| 1035 | mutex_lock(&cm_list_mtx); |
| 1036 | |
| 1037 | list_for_each_entry(cm, &cm_list, entry) { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1038 | unsigned int fbchk_ms = 0; |
| 1039 | |
| 1040 | /* fullbatt_vchk is required. setup timer for that */ |
| 1041 | if (cm->fullbatt_vchk_jiffies_at) { |
| 1042 | fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at |
| 1043 | - jiffies); |
| 1044 | if (time_is_before_eq_jiffies( |
| 1045 | cm->fullbatt_vchk_jiffies_at) || |
| 1046 | msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) { |
| 1047 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
| 1048 | fbchk_ms = 0; |
| 1049 | } |
| 1050 | } |
| 1051 | CM_MIN_VALID(wakeup_ms, fbchk_ms); |
| 1052 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1053 | /* Skip if polling is not required for this CM */ |
| 1054 | if (!is_polling_required(cm) && !cm->emergency_stop) |
| 1055 | continue; |
| 1056 | if (cm->desc->polling_interval_ms == 0) |
| 1057 | continue; |
| 1058 | CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms); |
| 1059 | } |
| 1060 | |
| 1061 | mutex_unlock(&cm_list_mtx); |
| 1062 | |
| 1063 | if (wakeup_ms < UINT_MAX && wakeup_ms > 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1064 | pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1065 | if (rtc_dev) { |
| 1066 | struct rtc_wkalrm tmp; |
| 1067 | unsigned long time, now; |
| 1068 | unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000); |
| 1069 | |
| 1070 | /* |
| 1071 | * Set alarm with the polling interval (wakeup_ms) |
| 1072 | * except when rtc_wkalarm_save comes first. |
| 1073 | * However, the alarm time should be NOW + |
| 1074 | * CM_RTC_SMALL or later. |
| 1075 | */ |
| 1076 | tmp.enabled = 1; |
| 1077 | rtc_read_time(rtc_dev, &tmp.time); |
| 1078 | rtc_tm_to_time(&tmp.time, &now); |
| 1079 | if (add < CM_RTC_SMALL) |
| 1080 | add = CM_RTC_SMALL; |
| 1081 | time = now + add; |
| 1082 | |
| 1083 | ret = true; |
| 1084 | |
| 1085 | if (rtc_wkalarm_save.enabled && |
| 1086 | rtc_wkalarm_save_time && |
| 1087 | rtc_wkalarm_save_time < time) { |
| 1088 | if (rtc_wkalarm_save_time < now + CM_RTC_SMALL) |
| 1089 | time = now + CM_RTC_SMALL; |
| 1090 | else |
| 1091 | time = rtc_wkalarm_save_time; |
| 1092 | |
| 1093 | /* The timer is not appointed by CM */ |
| 1094 | ret = false; |
| 1095 | } |
| 1096 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1097 | pr_info("Waking up after %lu secs\n", time - now); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1098 | |
| 1099 | rtc_time_to_tm(time, &tmp.time); |
| 1100 | rtc_set_alarm(rtc_dev, &tmp); |
| 1101 | cm_suspend_duration_ms += wakeup_ms; |
| 1102 | return ret; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | if (rtc_dev) |
| 1107 | rtc_set_alarm(rtc_dev, &rtc_wkalarm_save); |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1111 | static void _cm_fbchk_in_suspend(struct charger_manager *cm) |
| 1112 | { |
| 1113 | unsigned long jiffy_now = jiffies; |
| 1114 | |
| 1115 | if (!cm->fullbatt_vchk_jiffies_at) |
| 1116 | return; |
| 1117 | |
| 1118 | if (g_desc && g_desc->assume_timer_stops_in_suspend) |
| 1119 | jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms); |
| 1120 | |
| 1121 | /* Execute now if it's going to be executed not too long after */ |
| 1122 | jiffy_now += CM_JIFFIES_SMALL; |
| 1123 | |
| 1124 | if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at)) |
| 1125 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
| 1126 | } |
| 1127 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1128 | /** |
| 1129 | * cm_suspend_again - Determine whether suspend again or not |
| 1130 | * |
| 1131 | * Returns true if the system should be suspended again |
| 1132 | * Returns false if the system should be woken up |
| 1133 | */ |
| 1134 | bool cm_suspend_again(void) |
| 1135 | { |
| 1136 | struct charger_manager *cm; |
| 1137 | bool ret = false; |
| 1138 | |
| 1139 | if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() || |
| 1140 | !cm_rtc_set) |
| 1141 | return false; |
| 1142 | |
| 1143 | if (cm_monitor()) |
| 1144 | goto out; |
| 1145 | |
| 1146 | ret = true; |
| 1147 | mutex_lock(&cm_list_mtx); |
| 1148 | list_for_each_entry(cm, &cm_list, entry) { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1149 | _cm_fbchk_in_suspend(cm); |
| 1150 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1151 | if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) || |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1152 | cm->status_save_batt != is_batt_present(cm)) { |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1153 | ret = false; |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1154 | break; |
| 1155 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1156 | } |
| 1157 | mutex_unlock(&cm_list_mtx); |
| 1158 | |
| 1159 | cm_rtc_set = cm_setup_timer(); |
| 1160 | out: |
| 1161 | /* It's about the time when the non-CM appointed timer goes off */ |
| 1162 | if (rtc_wkalarm_save.enabled) { |
| 1163 | unsigned long now; |
| 1164 | struct rtc_time tmp; |
| 1165 | |
| 1166 | rtc_read_time(rtc_dev, &tmp); |
| 1167 | rtc_tm_to_time(&tmp, &now); |
| 1168 | |
| 1169 | if (rtc_wkalarm_save_time && |
| 1170 | now + CM_RTC_SMALL >= rtc_wkalarm_save_time) |
| 1171 | return false; |
| 1172 | } |
| 1173 | return ret; |
| 1174 | } |
| 1175 | EXPORT_SYMBOL_GPL(cm_suspend_again); |
| 1176 | |
| 1177 | /** |
| 1178 | * setup_charger_manager - initialize charger_global_desc data |
| 1179 | * @gd: pointer to instance of charger_global_desc |
| 1180 | */ |
| 1181 | int setup_charger_manager(struct charger_global_desc *gd) |
| 1182 | { |
| 1183 | if (!gd) |
| 1184 | return -EINVAL; |
| 1185 | |
| 1186 | if (rtc_dev) |
| 1187 | rtc_class_close(rtc_dev); |
| 1188 | rtc_dev = NULL; |
| 1189 | g_desc = NULL; |
| 1190 | |
| 1191 | if (!gd->rtc_only_wakeup) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1192 | pr_err("The callback rtc_only_wakeup is not given\n"); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1193 | return -EINVAL; |
| 1194 | } |
| 1195 | |
| 1196 | if (gd->rtc_name) { |
| 1197 | rtc_dev = rtc_class_open(gd->rtc_name); |
| 1198 | if (IS_ERR_OR_NULL(rtc_dev)) { |
| 1199 | rtc_dev = NULL; |
| 1200 | /* Retry at probe. RTC may be not registered yet */ |
| 1201 | } |
| 1202 | } else { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1203 | pr_warn("No wakeup timer is given for charger manager. " |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1204 | "In-suspend monitoring won't work.\n"); |
| 1205 | } |
| 1206 | |
| 1207 | g_desc = gd; |
| 1208 | return 0; |
| 1209 | } |
| 1210 | EXPORT_SYMBOL_GPL(setup_charger_manager); |
| 1211 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1212 | /** |
| 1213 | * charger_extcon_work - enable/diable charger according to the state |
| 1214 | * of charger cable |
| 1215 | * |
| 1216 | * @work: work_struct of the function charger_extcon_work. |
| 1217 | */ |
| 1218 | static void charger_extcon_work(struct work_struct *work) |
| 1219 | { |
| 1220 | struct charger_cable *cable = |
| 1221 | container_of(work, struct charger_cable, wq); |
Chanwoo Choi | 45cd4fb | 2012-07-12 15:03:29 +0900 | [diff] [blame] | 1222 | int ret; |
| 1223 | |
| 1224 | if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) { |
| 1225 | ret = regulator_set_current_limit(cable->charger->consumer, |
| 1226 | cable->min_uA, cable->max_uA); |
| 1227 | if (ret < 0) { |
| 1228 | pr_err("Cannot set current limit of %s (%s)\n", |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1229 | cable->charger->regulator_name, cable->name); |
Chanwoo Choi | 45cd4fb | 2012-07-12 15:03:29 +0900 | [diff] [blame] | 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | pr_info("Set current limit of %s : %duA ~ %duA\n", |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1234 | cable->charger->regulator_name, |
| 1235 | cable->min_uA, cable->max_uA); |
Chanwoo Choi | 45cd4fb | 2012-07-12 15:03:29 +0900 | [diff] [blame] | 1236 | } |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1237 | |
| 1238 | try_charger_enable(cable->cm, cable->attached); |
| 1239 | } |
| 1240 | |
| 1241 | /** |
| 1242 | * charger_extcon_notifier - receive the state of charger cable |
| 1243 | * when registered cable is attached or detached. |
| 1244 | * |
| 1245 | * @self: the notifier block of the charger_extcon_notifier. |
| 1246 | * @event: the cable state. |
| 1247 | * @ptr: the data pointer of notifier block. |
| 1248 | */ |
| 1249 | static int charger_extcon_notifier(struct notifier_block *self, |
| 1250 | unsigned long event, void *ptr) |
| 1251 | { |
| 1252 | struct charger_cable *cable = |
| 1253 | container_of(self, struct charger_cable, nb); |
| 1254 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1255 | /* |
| 1256 | * The newly state of charger cable. |
| 1257 | * If cable is attached, cable->attached is true. |
| 1258 | */ |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1259 | cable->attached = event; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1260 | |
| 1261 | /* |
| 1262 | * Setup monitoring to check battery state |
| 1263 | * when charger cable is attached. |
| 1264 | */ |
| 1265 | if (cable->attached && is_polling_required(cable->cm)) { |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 1266 | cancel_work_sync(&setup_polling); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1267 | schedule_work(&setup_polling); |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * Setup work for controlling charger(regulator) |
| 1272 | * according to charger cable. |
| 1273 | */ |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1274 | schedule_work(&cable->wq); |
| 1275 | |
| 1276 | return NOTIFY_DONE; |
| 1277 | } |
| 1278 | |
| 1279 | /** |
| 1280 | * charger_extcon_init - register external connector to use it |
| 1281 | * as the charger cable |
| 1282 | * |
| 1283 | * @cm: the Charger Manager representing the battery. |
| 1284 | * @cable: the Charger cable representing the external connector. |
| 1285 | */ |
| 1286 | static int charger_extcon_init(struct charger_manager *cm, |
| 1287 | struct charger_cable *cable) |
| 1288 | { |
| 1289 | int ret = 0; |
| 1290 | |
| 1291 | /* |
| 1292 | * Charger manager use Extcon framework to identify |
| 1293 | * the charger cable among various external connector |
| 1294 | * cable (e.g., TA, USB, MHL, Dock). |
| 1295 | */ |
| 1296 | INIT_WORK(&cable->wq, charger_extcon_work); |
| 1297 | cable->nb.notifier_call = charger_extcon_notifier; |
| 1298 | ret = extcon_register_interest(&cable->extcon_dev, |
| 1299 | cable->extcon_name, cable->name, &cable->nb); |
| 1300 | if (ret < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1301 | pr_info("Cannot register extcon_dev for %s(cable: %s)\n", |
| 1302 | cable->extcon_name, cable->name); |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1303 | ret = -EINVAL; |
| 1304 | } |
| 1305 | |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1309 | /** |
| 1310 | * charger_manager_register_extcon - Register extcon device to recevie state |
| 1311 | * of charger cable. |
| 1312 | * @cm: the Charger Manager representing the battery. |
| 1313 | * |
| 1314 | * This function support EXTCON(External Connector) subsystem to detect the |
| 1315 | * state of charger cables for enabling or disabling charger(regulator) and |
| 1316 | * select the charger cable for charging among a number of external cable |
| 1317 | * according to policy of H/W board. |
| 1318 | */ |
| 1319 | static int charger_manager_register_extcon(struct charger_manager *cm) |
| 1320 | { |
| 1321 | struct charger_desc *desc = cm->desc; |
| 1322 | struct charger_regulator *charger; |
| 1323 | int ret = 0; |
| 1324 | int i; |
| 1325 | int j; |
| 1326 | |
| 1327 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1328 | charger = &desc->charger_regulators[i]; |
| 1329 | |
| 1330 | charger->consumer = regulator_get(cm->dev, |
| 1331 | charger->regulator_name); |
Jonghwa Lee | 5a6c220 | 2013-06-25 14:18:14 +0900 | [diff] [blame] | 1332 | if (IS_ERR(charger->consumer)) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1333 | dev_err(cm->dev, "Cannot find charger(%s)\n", |
| 1334 | charger->regulator_name); |
Jonghwa Lee | 5a6c220 | 2013-06-25 14:18:14 +0900 | [diff] [blame] | 1335 | return PTR_ERR(charger->consumer); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1336 | } |
| 1337 | charger->cm = cm; |
| 1338 | |
| 1339 | for (j = 0; j < charger->num_cables; j++) { |
| 1340 | struct charger_cable *cable = &charger->cables[j]; |
| 1341 | |
| 1342 | ret = charger_extcon_init(cm, cable); |
| 1343 | if (ret < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1344 | dev_err(cm->dev, "Cannot initialize charger(%s)\n", |
| 1345 | charger->regulator_name); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1346 | goto err; |
| 1347 | } |
| 1348 | cable->charger = charger; |
| 1349 | cable->cm = cm; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | err: |
| 1354 | return ret; |
| 1355 | } |
| 1356 | |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1357 | /* help function of sysfs node to control charger(regulator) */ |
| 1358 | static ssize_t charger_name_show(struct device *dev, |
| 1359 | struct device_attribute *attr, char *buf) |
| 1360 | { |
| 1361 | struct charger_regulator *charger |
| 1362 | = container_of(attr, struct charger_regulator, attr_name); |
| 1363 | |
| 1364 | return sprintf(buf, "%s\n", charger->regulator_name); |
| 1365 | } |
| 1366 | |
| 1367 | static ssize_t charger_state_show(struct device *dev, |
| 1368 | struct device_attribute *attr, char *buf) |
| 1369 | { |
| 1370 | struct charger_regulator *charger |
| 1371 | = container_of(attr, struct charger_regulator, attr_state); |
| 1372 | int state = 0; |
| 1373 | |
| 1374 | if (!charger->externally_control) |
| 1375 | state = regulator_is_enabled(charger->consumer); |
| 1376 | |
| 1377 | return sprintf(buf, "%s\n", state ? "enabled" : "disabled"); |
| 1378 | } |
| 1379 | |
| 1380 | static ssize_t charger_externally_control_show(struct device *dev, |
| 1381 | struct device_attribute *attr, char *buf) |
| 1382 | { |
| 1383 | struct charger_regulator *charger = container_of(attr, |
| 1384 | struct charger_regulator, attr_externally_control); |
| 1385 | |
| 1386 | return sprintf(buf, "%d\n", charger->externally_control); |
| 1387 | } |
| 1388 | |
| 1389 | static ssize_t charger_externally_control_store(struct device *dev, |
| 1390 | struct device_attribute *attr, const char *buf, |
| 1391 | size_t count) |
| 1392 | { |
| 1393 | struct charger_regulator *charger |
| 1394 | = container_of(attr, struct charger_regulator, |
| 1395 | attr_externally_control); |
| 1396 | struct charger_manager *cm = charger->cm; |
| 1397 | struct charger_desc *desc = cm->desc; |
| 1398 | int i; |
| 1399 | int ret; |
| 1400 | int externally_control; |
| 1401 | int chargers_externally_control = 1; |
| 1402 | |
| 1403 | ret = sscanf(buf, "%d", &externally_control); |
| 1404 | if (ret == 0) { |
| 1405 | ret = -EINVAL; |
| 1406 | return ret; |
| 1407 | } |
| 1408 | |
| 1409 | if (!externally_control) { |
| 1410 | charger->externally_control = 0; |
| 1411 | return count; |
| 1412 | } |
| 1413 | |
| 1414 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1415 | if (&desc->charger_regulators[i] != charger && |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1416 | !desc->charger_regulators[i].externally_control) { |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1417 | /* |
| 1418 | * At least, one charger is controlled by |
| 1419 | * charger-manager |
| 1420 | */ |
| 1421 | chargers_externally_control = 0; |
| 1422 | break; |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | if (!chargers_externally_control) { |
| 1427 | if (cm->charger_enabled) { |
| 1428 | try_charger_enable(charger->cm, false); |
| 1429 | charger->externally_control = externally_control; |
| 1430 | try_charger_enable(charger->cm, true); |
| 1431 | } else { |
| 1432 | charger->externally_control = externally_control; |
| 1433 | } |
| 1434 | } else { |
| 1435 | dev_warn(cm->dev, |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1436 | "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n", |
| 1437 | charger->regulator_name); |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | return count; |
| 1441 | } |
| 1442 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1443 | /** |
| 1444 | * charger_manager_register_sysfs - Register sysfs entry for each charger |
| 1445 | * @cm: the Charger Manager representing the battery. |
| 1446 | * |
| 1447 | * This function add sysfs entry for charger(regulator) to control charger from |
| 1448 | * user-space. If some development board use one more chargers for charging |
| 1449 | * but only need one charger on specific case which is dependent on user |
| 1450 | * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/ |
| 1451 | * class/power_supply/battery/charger.[index]/externally_control'. For example, |
| 1452 | * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/ |
| 1453 | * externally_control, this charger isn't controlled from charger-manager and |
| 1454 | * always stay off state of regulator. |
| 1455 | */ |
| 1456 | static int charger_manager_register_sysfs(struct charger_manager *cm) |
| 1457 | { |
| 1458 | struct charger_desc *desc = cm->desc; |
| 1459 | struct charger_regulator *charger; |
| 1460 | int chargers_externally_control = 1; |
| 1461 | char buf[11]; |
| 1462 | char *str; |
| 1463 | int ret = 0; |
| 1464 | int i; |
| 1465 | |
| 1466 | /* Create sysfs entry to control charger(regulator) */ |
| 1467 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1468 | charger = &desc->charger_regulators[i]; |
| 1469 | |
| 1470 | snprintf(buf, 10, "charger.%d", i); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1471 | str = devm_kzalloc(cm->dev, |
| 1472 | sizeof(char) * (strlen(buf) + 1), GFP_KERNEL); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1473 | if (!str) { |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1474 | ret = -ENOMEM; |
| 1475 | goto err; |
| 1476 | } |
| 1477 | strcpy(str, buf); |
| 1478 | |
| 1479 | charger->attrs[0] = &charger->attr_name.attr; |
| 1480 | charger->attrs[1] = &charger->attr_state.attr; |
| 1481 | charger->attrs[2] = &charger->attr_externally_control.attr; |
| 1482 | charger->attrs[3] = NULL; |
| 1483 | charger->attr_g.name = str; |
| 1484 | charger->attr_g.attrs = charger->attrs; |
| 1485 | |
| 1486 | sysfs_attr_init(&charger->attr_name.attr); |
| 1487 | charger->attr_name.attr.name = "name"; |
| 1488 | charger->attr_name.attr.mode = 0444; |
| 1489 | charger->attr_name.show = charger_name_show; |
| 1490 | |
| 1491 | sysfs_attr_init(&charger->attr_state.attr); |
| 1492 | charger->attr_state.attr.name = "state"; |
| 1493 | charger->attr_state.attr.mode = 0444; |
| 1494 | charger->attr_state.show = charger_state_show; |
| 1495 | |
| 1496 | sysfs_attr_init(&charger->attr_externally_control.attr); |
| 1497 | charger->attr_externally_control.attr.name |
| 1498 | = "externally_control"; |
| 1499 | charger->attr_externally_control.attr.mode = 0644; |
| 1500 | charger->attr_externally_control.show |
| 1501 | = charger_externally_control_show; |
| 1502 | charger->attr_externally_control.store |
| 1503 | = charger_externally_control_store; |
| 1504 | |
| 1505 | if (!desc->charger_regulators[i].externally_control || |
| 1506 | !chargers_externally_control) |
| 1507 | chargers_externally_control = 0; |
| 1508 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1509 | dev_info(cm->dev, "'%s' regulator's externally_control is %d\n", |
| 1510 | charger->regulator_name, charger->externally_control); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1511 | |
| 1512 | ret = sysfs_create_group(&cm->charger_psy.dev->kobj, |
| 1513 | &charger->attr_g); |
| 1514 | if (ret < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1515 | dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n", |
| 1516 | charger->regulator_name); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1517 | ret = -EINVAL; |
| 1518 | goto err; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | if (chargers_externally_control) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1523 | dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n"); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1524 | ret = -EINVAL; |
| 1525 | goto err; |
| 1526 | } |
| 1527 | |
| 1528 | err: |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 1532 | static int cm_init_thermal_data(struct charger_manager *cm, |
| 1533 | struct power_supply *fuel_gauge) |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1534 | { |
| 1535 | struct charger_desc *desc = cm->desc; |
| 1536 | union power_supply_propval val; |
| 1537 | int ret; |
| 1538 | |
| 1539 | /* Verify whether fuel gauge provides battery temperature */ |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 1540 | ret = fuel_gauge->get_property(fuel_gauge, |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1541 | POWER_SUPPLY_PROP_TEMP, &val); |
| 1542 | |
| 1543 | if (!ret) { |
| 1544 | cm->charger_psy.properties[cm->charger_psy.num_properties] = |
| 1545 | POWER_SUPPLY_PROP_TEMP; |
| 1546 | cm->charger_psy.num_properties++; |
| 1547 | cm->desc->measure_battery_temp = true; |
| 1548 | } |
| 1549 | #ifdef CONFIG_THERMAL |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1550 | if (ret && desc->thermal_zone) { |
| 1551 | cm->tzd_batt = |
| 1552 | thermal_zone_get_zone_by_name(desc->thermal_zone); |
| 1553 | if (IS_ERR(cm->tzd_batt)) |
| 1554 | return PTR_ERR(cm->tzd_batt); |
| 1555 | |
| 1556 | /* Use external thermometer */ |
| 1557 | cm->charger_psy.properties[cm->charger_psy.num_properties] = |
| 1558 | POWER_SUPPLY_PROP_TEMP_AMBIENT; |
| 1559 | cm->charger_psy.num_properties++; |
| 1560 | cm->desc->measure_battery_temp = true; |
| 1561 | ret = 0; |
| 1562 | } |
| 1563 | #endif |
| 1564 | if (cm->desc->measure_battery_temp) { |
| 1565 | /* NOTICE : Default allowable minimum charge temperature is 0 */ |
| 1566 | if (!desc->temp_max) |
| 1567 | desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX; |
| 1568 | if (!desc->temp_diff) |
| 1569 | desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF; |
| 1570 | } |
| 1571 | |
| 1572 | return ret; |
| 1573 | } |
| 1574 | |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1575 | static struct of_device_id charger_manager_match[] = { |
| 1576 | { |
| 1577 | .compatible = "charger-manager", |
| 1578 | }, |
| 1579 | {}, |
| 1580 | }; |
| 1581 | |
Anton Vorontsov | 434a09f | 2013-12-23 18:17:05 -0800 | [diff] [blame] | 1582 | static struct charger_desc *of_cm_parse_desc(struct device *dev) |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1583 | { |
| 1584 | struct charger_desc *desc; |
| 1585 | struct device_node *np = dev->of_node; |
| 1586 | u32 poll_mode = CM_POLL_DISABLE; |
| 1587 | u32 battery_stat = CM_NO_BATTERY; |
| 1588 | int num_chgs = 0; |
| 1589 | |
| 1590 | desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); |
| 1591 | if (!desc) |
| 1592 | return ERR_PTR(-ENOMEM); |
| 1593 | |
| 1594 | of_property_read_string(np, "cm-name", &desc->psy_name); |
| 1595 | |
| 1596 | of_property_read_u32(np, "cm-poll-mode", &poll_mode); |
| 1597 | desc->polling_mode = poll_mode; |
| 1598 | |
| 1599 | of_property_read_u32(np, "cm-poll-interval", |
| 1600 | &desc->polling_interval_ms); |
| 1601 | |
| 1602 | of_property_read_u32(np, "cm-fullbatt-vchkdrop-ms", |
| 1603 | &desc->fullbatt_vchkdrop_ms); |
| 1604 | of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt", |
| 1605 | &desc->fullbatt_vchkdrop_uV); |
| 1606 | of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV); |
| 1607 | of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc); |
| 1608 | of_property_read_u32(np, "cm-fullbatt-capacity", |
| 1609 | &desc->fullbatt_full_capacity); |
| 1610 | |
| 1611 | of_property_read_u32(np, "cm-battery-stat", &battery_stat); |
| 1612 | desc->battery_present = battery_stat; |
| 1613 | |
| 1614 | /* chargers */ |
| 1615 | of_property_read_u32(np, "cm-num-chargers", &num_chgs); |
| 1616 | if (num_chgs) { |
| 1617 | /* Allocate empty bin at the tail of array */ |
| 1618 | desc->psy_charger_stat = devm_kzalloc(dev, sizeof(char *) |
| 1619 | * (num_chgs + 1), GFP_KERNEL); |
| 1620 | if (desc->psy_charger_stat) { |
| 1621 | int i; |
| 1622 | for (i = 0; i < num_chgs; i++) |
| 1623 | of_property_read_string_index(np, "cm-chargers", |
| 1624 | i, &desc->psy_charger_stat[i]); |
| 1625 | } else { |
| 1626 | return ERR_PTR(-ENOMEM); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge); |
| 1631 | |
| 1632 | of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone); |
| 1633 | |
| 1634 | of_property_read_u32(np, "cm-battery-cold", &desc->temp_min); |
| 1635 | if (of_get_property(np, "cm-battery-cold-in-minus", NULL)) |
| 1636 | desc->temp_min *= -1; |
| 1637 | of_property_read_u32(np, "cm-battery-hot", &desc->temp_max); |
| 1638 | of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff); |
| 1639 | |
| 1640 | of_property_read_u32(np, "cm-charging-max", |
| 1641 | &desc->charging_max_duration_ms); |
| 1642 | of_property_read_u32(np, "cm-discharging-max", |
| 1643 | &desc->discharging_max_duration_ms); |
| 1644 | |
| 1645 | /* battery charger regualtors */ |
| 1646 | desc->num_charger_regulators = of_get_child_count(np); |
| 1647 | if (desc->num_charger_regulators) { |
| 1648 | struct charger_regulator *chg_regs; |
| 1649 | struct device_node *child; |
| 1650 | |
| 1651 | chg_regs = devm_kzalloc(dev, sizeof(*chg_regs) |
| 1652 | * desc->num_charger_regulators, |
| 1653 | GFP_KERNEL); |
| 1654 | if (!chg_regs) |
| 1655 | return ERR_PTR(-ENOMEM); |
| 1656 | |
| 1657 | desc->charger_regulators = chg_regs; |
| 1658 | |
| 1659 | for_each_child_of_node(np, child) { |
| 1660 | struct charger_cable *cables; |
| 1661 | struct device_node *_child; |
| 1662 | |
| 1663 | of_property_read_string(child, "cm-regulator-name", |
| 1664 | &chg_regs->regulator_name); |
| 1665 | |
| 1666 | /* charger cables */ |
| 1667 | chg_regs->num_cables = of_get_child_count(child); |
| 1668 | if (chg_regs->num_cables) { |
| 1669 | cables = devm_kzalloc(dev, sizeof(*cables) |
| 1670 | * chg_regs->num_cables, |
| 1671 | GFP_KERNEL); |
| 1672 | if (!cables) |
| 1673 | return ERR_PTR(-ENOMEM); |
| 1674 | |
| 1675 | chg_regs->cables = cables; |
| 1676 | |
| 1677 | for_each_child_of_node(child, _child) { |
| 1678 | of_property_read_string(_child, |
| 1679 | "cm-cable-name", &cables->name); |
| 1680 | of_property_read_string(_child, |
| 1681 | "cm-cable-extcon", |
| 1682 | &cables->extcon_name); |
| 1683 | of_property_read_u32(_child, |
| 1684 | "cm-cable-min", |
| 1685 | &cables->min_uA); |
| 1686 | of_property_read_u32(_child, |
| 1687 | "cm-cable-max", |
| 1688 | &cables->max_uA); |
| 1689 | cables++; |
| 1690 | } |
| 1691 | } |
| 1692 | chg_regs++; |
| 1693 | } |
| 1694 | } |
| 1695 | return desc; |
| 1696 | } |
| 1697 | |
| 1698 | static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev) |
| 1699 | { |
| 1700 | if (pdev->dev.of_node) |
| 1701 | return of_cm_parse_desc(&pdev->dev); |
Jingoo Han | 86515b7 | 2014-08-29 12:45:27 +0900 | [diff] [blame] | 1702 | return dev_get_platdata(&pdev->dev); |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1703 | } |
| 1704 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1705 | static int charger_manager_probe(struct platform_device *pdev) |
| 1706 | { |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1707 | struct charger_desc *desc = cm_get_drv_data(pdev); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1708 | struct charger_manager *cm; |
| 1709 | int ret = 0, i = 0; |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1710 | int j = 0; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1711 | union power_supply_propval val; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 1712 | struct power_supply *fuel_gauge; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1713 | |
| 1714 | if (g_desc && !rtc_dev && g_desc->rtc_name) { |
| 1715 | rtc_dev = rtc_class_open(g_desc->rtc_name); |
| 1716 | if (IS_ERR_OR_NULL(rtc_dev)) { |
| 1717 | rtc_dev = NULL; |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1718 | dev_err(&pdev->dev, "Cannot get RTC %s\n", |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1719 | g_desc->rtc_name); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1720 | return -ENODEV; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
Chanwoo Choi | c6738d0 | 2014-08-26 13:41:38 +0900 | [diff] [blame] | 1724 | if (IS_ERR(desc)) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1725 | dev_err(&pdev->dev, "No platform data (desc) found\n"); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1726 | return -ENODEV; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1727 | } |
| 1728 | |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1729 | cm = devm_kzalloc(&pdev->dev, |
| 1730 | sizeof(struct charger_manager), GFP_KERNEL); |
| 1731 | if (!cm) |
| 1732 | return -ENOMEM; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1733 | |
| 1734 | /* Basic Values. Unspecified are Null or 0 */ |
| 1735 | cm->dev = &pdev->dev; |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1736 | cm->desc = desc; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1737 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1738 | /* |
| 1739 | * The following two do not need to be errors. |
| 1740 | * Users may intentionally ignore those two features. |
| 1741 | */ |
| 1742 | if (desc->fullbatt_uV == 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1743 | dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n"); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1744 | } |
| 1745 | if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1746 | dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n"); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1747 | desc->fullbatt_vchkdrop_ms = 0; |
| 1748 | desc->fullbatt_vchkdrop_uV = 0; |
| 1749 | } |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1750 | if (desc->fullbatt_soc == 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1751 | dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n"); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1752 | } |
| 1753 | if (desc->fullbatt_full_capacity == 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1754 | dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n"); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1755 | } |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1756 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1757 | if (!desc->charger_regulators || desc->num_charger_regulators < 1) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1758 | dev_err(&pdev->dev, "charger_regulators undefined\n"); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1759 | return -EINVAL; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1763 | dev_err(&pdev->dev, "No power supply defined\n"); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1764 | return -EINVAL; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1765 | } |
| 1766 | |
Krzysztof Kozlowski | 661a888 | 2014-09-26 13:27:03 +0200 | [diff] [blame] | 1767 | if (!desc->psy_fuel_gauge) { |
| 1768 | dev_err(&pdev->dev, "No fuel gauge power supply defined\n"); |
| 1769 | return -EINVAL; |
| 1770 | } |
| 1771 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1772 | /* Counting index only */ |
| 1773 | while (desc->psy_charger_stat[i]) |
| 1774 | i++; |
| 1775 | |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1776 | cm->charger_stat = devm_kzalloc(&pdev->dev, |
| 1777 | sizeof(struct power_supply *) * i, GFP_KERNEL); |
| 1778 | if (!cm->charger_stat) |
| 1779 | return -ENOMEM; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1780 | |
| 1781 | for (i = 0; desc->psy_charger_stat[i]; i++) { |
| 1782 | cm->charger_stat[i] = power_supply_get_by_name( |
| 1783 | desc->psy_charger_stat[i]); |
| 1784 | if (!cm->charger_stat[i]) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1785 | dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n", |
| 1786 | desc->psy_charger_stat[i]); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1787 | return -ENODEV; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 1791 | fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge); |
| 1792 | if (!fuel_gauge) { |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1793 | dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n", |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1794 | desc->psy_fuel_gauge); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1795 | return -ENODEV; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | if (desc->polling_interval_ms == 0 || |
| 1799 | msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) { |
| 1800 | dev_err(&pdev->dev, "polling_interval_ms is too small\n"); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1801 | return -EINVAL; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1802 | } |
| 1803 | |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 1804 | if (!desc->charging_max_duration_ms || |
| 1805 | !desc->discharging_max_duration_ms) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1806 | dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n"); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 1807 | desc->charging_max_duration_ms = 0; |
| 1808 | desc->discharging_max_duration_ms = 0; |
| 1809 | } |
| 1810 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1811 | platform_set_drvdata(pdev, cm); |
| 1812 | |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1813 | memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default)); |
| 1814 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1815 | if (!desc->psy_name) |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1816 | strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1817 | else |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1818 | strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1819 | cm->charger_psy.name = cm->psy_name_buf; |
| 1820 | |
| 1821 | /* Allocate for psy properties because they may vary */ |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1822 | cm->charger_psy.properties = devm_kzalloc(&pdev->dev, |
| 1823 | sizeof(enum power_supply_property) |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1824 | * (ARRAY_SIZE(default_charger_props) + |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1825 | NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL); |
| 1826 | if (!cm->charger_psy.properties) |
| 1827 | return -ENOMEM; |
| 1828 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1829 | memcpy(cm->charger_psy.properties, default_charger_props, |
| 1830 | sizeof(enum power_supply_property) * |
| 1831 | ARRAY_SIZE(default_charger_props)); |
| 1832 | cm->charger_psy.num_properties = psy_default.num_properties; |
| 1833 | |
| 1834 | /* Find which optional psy-properties are available */ |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 1835 | if (!fuel_gauge->get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1836 | POWER_SUPPLY_PROP_CHARGE_NOW, &val)) { |
| 1837 | cm->charger_psy.properties[cm->charger_psy.num_properties] = |
| 1838 | POWER_SUPPLY_PROP_CHARGE_NOW; |
| 1839 | cm->charger_psy.num_properties++; |
| 1840 | } |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 1841 | if (!fuel_gauge->get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1842 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 1843 | &val)) { |
| 1844 | cm->charger_psy.properties[cm->charger_psy.num_properties] = |
| 1845 | POWER_SUPPLY_PROP_CURRENT_NOW; |
| 1846 | cm->charger_psy.num_properties++; |
| 1847 | } |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1848 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame^] | 1849 | ret = cm_init_thermal_data(cm, fuel_gauge); |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1850 | if (ret) { |
| 1851 | dev_err(&pdev->dev, "Failed to initialize thermal data\n"); |
| 1852 | cm->desc->measure_battery_temp = false; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1853 | } |
| 1854 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1855 | INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk); |
| 1856 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1857 | ret = power_supply_register(NULL, &cm->charger_psy); |
| 1858 | if (ret) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1859 | dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n", |
| 1860 | cm->charger_psy.name); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1861 | return ret; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1862 | } |
| 1863 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1864 | /* Register extcon device for charger cable */ |
| 1865 | ret = charger_manager_register_extcon(cm); |
| 1866 | if (ret < 0) { |
| 1867 | dev_err(&pdev->dev, "Cannot initialize extcon device\n"); |
| 1868 | goto err_reg_extcon; |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1869 | } |
| 1870 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1871 | /* Register sysfs entry for charger(regulator) */ |
| 1872 | ret = charger_manager_register_sysfs(cm); |
| 1873 | if (ret < 0) { |
| 1874 | dev_err(&pdev->dev, |
| 1875 | "Cannot initialize sysfs entry of regulator\n"); |
| 1876 | goto err_reg_sysfs; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | /* Add to the list */ |
| 1880 | mutex_lock(&cm_list_mtx); |
| 1881 | list_add(&cm->entry, &cm_list); |
| 1882 | mutex_unlock(&cm_list_mtx); |
| 1883 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1884 | /* |
| 1885 | * Charger-manager is capable of waking up the systme from sleep |
| 1886 | * when event is happend through cm_notify_event() |
| 1887 | */ |
| 1888 | device_init_wakeup(&pdev->dev, true); |
| 1889 | device_set_wakeup_capable(&pdev->dev, false); |
| 1890 | |
Chanwoo Choi | b1022e2 | 2014-08-26 13:41:39 +0900 | [diff] [blame] | 1891 | /* |
| 1892 | * Charger-manager have to check the charging state right after |
| 1893 | * tialization of charger-manager and then update current charging |
| 1894 | * state. |
| 1895 | */ |
| 1896 | cm_monitor(); |
| 1897 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1898 | schedule_work(&setup_polling); |
| 1899 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1900 | return 0; |
| 1901 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1902 | err_reg_sysfs: |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1903 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1904 | struct charger_regulator *charger; |
| 1905 | |
| 1906 | charger = &desc->charger_regulators[i]; |
| 1907 | sysfs_remove_group(&cm->charger_psy.dev->kobj, |
| 1908 | &charger->attr_g); |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1909 | } |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1910 | err_reg_extcon: |
| 1911 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1912 | struct charger_regulator *charger; |
| 1913 | |
| 1914 | charger = &desc->charger_regulators[i]; |
| 1915 | for (j = 0; j < charger->num_cables; j++) { |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1916 | struct charger_cable *cable = &charger->cables[j]; |
Jonghwa Lee | 3cc9d269 | 2013-06-25 14:02:49 +0900 | [diff] [blame] | 1917 | /* Remove notifier block if only edev exists */ |
| 1918 | if (cable->extcon_dev.edev) |
| 1919 | extcon_unregister_interest(&cable->extcon_dev); |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1920 | } |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1921 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1922 | regulator_put(desc->charger_regulators[i].consumer); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1923 | } |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1924 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1925 | power_supply_unregister(&cm->charger_psy); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1926 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1927 | return ret; |
| 1928 | } |
| 1929 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 1930 | static int charger_manager_remove(struct platform_device *pdev) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1931 | { |
| 1932 | struct charger_manager *cm = platform_get_drvdata(pdev); |
| 1933 | struct charger_desc *desc = cm->desc; |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1934 | int i = 0; |
| 1935 | int j = 0; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1936 | |
| 1937 | /* Remove from the list */ |
| 1938 | mutex_lock(&cm_list_mtx); |
| 1939 | list_del(&cm->entry); |
| 1940 | mutex_unlock(&cm_list_mtx); |
| 1941 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 1942 | cancel_work_sync(&setup_polling); |
| 1943 | cancel_delayed_work_sync(&cm_monitor_work); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1944 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1945 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
| 1946 | struct charger_regulator *charger |
| 1947 | = &desc->charger_regulators[i]; |
| 1948 | for (j = 0 ; j < charger->num_cables ; j++) { |
| 1949 | struct charger_cable *cable = &charger->cables[j]; |
| 1950 | extcon_unregister_interest(&cable->extcon_dev); |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | for (i = 0 ; i < desc->num_charger_regulators ; i++) |
| 1955 | regulator_put(desc->charger_regulators[i].consumer); |
| 1956 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1957 | power_supply_unregister(&cm->charger_psy); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1958 | |
| 1959 | try_charger_enable(cm, false); |
| 1960 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1961 | return 0; |
| 1962 | } |
| 1963 | |
Axel Lin | 1bbe24d | 2012-01-11 17:19:45 +0800 | [diff] [blame] | 1964 | static const struct platform_device_id charger_manager_id[] = { |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1965 | { "charger-manager", 0 }, |
| 1966 | { }, |
| 1967 | }; |
Axel Lin | 1bbe24d | 2012-01-11 17:19:45 +0800 | [diff] [blame] | 1968 | MODULE_DEVICE_TABLE(platform, charger_manager_id); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1969 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1970 | static int cm_suspend_noirq(struct device *dev) |
| 1971 | { |
| 1972 | int ret = 0; |
| 1973 | |
| 1974 | if (device_may_wakeup(dev)) { |
| 1975 | device_set_wakeup_capable(dev, false); |
| 1976 | ret = -EAGAIN; |
| 1977 | } |
| 1978 | |
| 1979 | return ret; |
| 1980 | } |
| 1981 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1982 | static int cm_suspend_prepare(struct device *dev) |
| 1983 | { |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1984 | struct charger_manager *cm = dev_get_drvdata(dev); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1985 | |
| 1986 | if (!cm_suspended) { |
| 1987 | if (rtc_dev) { |
| 1988 | struct rtc_time tmp; |
| 1989 | unsigned long now; |
| 1990 | |
| 1991 | rtc_read_alarm(rtc_dev, &rtc_wkalarm_save); |
| 1992 | rtc_read_time(rtc_dev, &tmp); |
| 1993 | |
| 1994 | if (rtc_wkalarm_save.enabled) { |
| 1995 | rtc_tm_to_time(&rtc_wkalarm_save.time, |
| 1996 | &rtc_wkalarm_save_time); |
| 1997 | rtc_tm_to_time(&tmp, &now); |
| 1998 | if (now > rtc_wkalarm_save_time) |
| 1999 | rtc_wkalarm_save_time = 0; |
| 2000 | } else { |
| 2001 | rtc_wkalarm_save_time = 0; |
| 2002 | } |
| 2003 | } |
| 2004 | cm_suspended = true; |
| 2005 | } |
| 2006 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 2007 | cancel_delayed_work(&cm->fullbatt_vchk_work); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2008 | cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm); |
| 2009 | cm->status_save_batt = is_batt_present(cm); |
| 2010 | |
| 2011 | if (!cm_rtc_set) { |
| 2012 | cm_suspend_duration_ms = 0; |
| 2013 | cm_rtc_set = cm_setup_timer(); |
| 2014 | } |
| 2015 | |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
| 2019 | static void cm_suspend_complete(struct device *dev) |
| 2020 | { |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 2021 | struct charger_manager *cm = dev_get_drvdata(dev); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2022 | |
| 2023 | if (cm_suspended) { |
| 2024 | if (rtc_dev) { |
| 2025 | struct rtc_wkalrm tmp; |
| 2026 | |
| 2027 | rtc_read_alarm(rtc_dev, &tmp); |
| 2028 | rtc_wkalarm_save.pending = tmp.pending; |
| 2029 | rtc_set_alarm(rtc_dev, &rtc_wkalarm_save); |
| 2030 | } |
| 2031 | cm_suspended = false; |
| 2032 | cm_rtc_set = false; |
| 2033 | } |
| 2034 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 2035 | /* Re-enqueue delayed work (fullbatt_vchk_work) */ |
| 2036 | if (cm->fullbatt_vchk_jiffies_at) { |
| 2037 | unsigned long delay = 0; |
| 2038 | unsigned long now = jiffies + CM_JIFFIES_SMALL; |
| 2039 | |
| 2040 | if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) { |
| 2041 | delay = (unsigned long)((long)now |
| 2042 | - (long)(cm->fullbatt_vchk_jiffies_at)); |
| 2043 | delay = jiffies_to_msecs(delay); |
| 2044 | } else { |
| 2045 | delay = 0; |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * Account for cm_suspend_duration_ms if |
| 2050 | * assume_timer_stops_in_suspend is active |
| 2051 | */ |
| 2052 | if (g_desc && g_desc->assume_timer_stops_in_suspend) { |
| 2053 | if (delay > cm_suspend_duration_ms) |
| 2054 | delay -= cm_suspend_duration_ms; |
| 2055 | else |
| 2056 | delay = 0; |
| 2057 | } |
| 2058 | |
| 2059 | queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work, |
| 2060 | msecs_to_jiffies(delay)); |
| 2061 | } |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 2062 | device_set_wakeup_capable(cm->dev, false); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2063 | uevent_notify(cm, NULL); |
| 2064 | } |
| 2065 | |
| 2066 | static const struct dev_pm_ops charger_manager_pm = { |
| 2067 | .prepare = cm_suspend_prepare, |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 2068 | .suspend_noirq = cm_suspend_noirq, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2069 | .complete = cm_suspend_complete, |
| 2070 | }; |
| 2071 | |
| 2072 | static struct platform_driver charger_manager_driver = { |
| 2073 | .driver = { |
| 2074 | .name = "charger-manager", |
| 2075 | .owner = THIS_MODULE, |
| 2076 | .pm = &charger_manager_pm, |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 2077 | .of_match_table = charger_manager_match, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2078 | }, |
| 2079 | .probe = charger_manager_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 2080 | .remove = charger_manager_remove, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2081 | .id_table = charger_manager_id, |
| 2082 | }; |
| 2083 | |
| 2084 | static int __init charger_manager_init(void) |
| 2085 | { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 2086 | cm_wq = create_freezable_workqueue("charger_manager"); |
| 2087 | INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller); |
| 2088 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2089 | return platform_driver_register(&charger_manager_driver); |
| 2090 | } |
| 2091 | late_initcall(charger_manager_init); |
| 2092 | |
| 2093 | static void __exit charger_manager_cleanup(void) |
| 2094 | { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 2095 | destroy_workqueue(cm_wq); |
| 2096 | cm_wq = NULL; |
| 2097 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2098 | platform_driver_unregister(&charger_manager_driver); |
| 2099 | } |
| 2100 | module_exit(charger_manager_cleanup); |
| 2101 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 2102 | /** |
| 2103 | * find_power_supply - find the associated power_supply of charger |
| 2104 | * @cm: the Charger Manager representing the battery |
| 2105 | * @psy: pointer to instance of charger's power_supply |
| 2106 | */ |
| 2107 | static bool find_power_supply(struct charger_manager *cm, |
| 2108 | struct power_supply *psy) |
| 2109 | { |
| 2110 | int i; |
| 2111 | bool found = false; |
| 2112 | |
| 2113 | for (i = 0; cm->charger_stat[i]; i++) { |
| 2114 | if (psy == cm->charger_stat[i]) { |
| 2115 | found = true; |
| 2116 | break; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | return found; |
| 2121 | } |
| 2122 | |
| 2123 | /** |
| 2124 | * cm_notify_event - charger driver notify Charger Manager of charger event |
| 2125 | * @psy: pointer to instance of charger's power_supply |
| 2126 | * @type: type of charger event |
| 2127 | * @msg: optional message passed to uevent_notify fuction |
| 2128 | */ |
| 2129 | void cm_notify_event(struct power_supply *psy, enum cm_event_types type, |
| 2130 | char *msg) |
| 2131 | { |
| 2132 | struct charger_manager *cm; |
| 2133 | bool found_power_supply = false; |
| 2134 | |
| 2135 | if (psy == NULL) |
| 2136 | return; |
| 2137 | |
| 2138 | mutex_lock(&cm_list_mtx); |
| 2139 | list_for_each_entry(cm, &cm_list, entry) { |
| 2140 | found_power_supply = find_power_supply(cm, psy); |
| 2141 | if (found_power_supply) |
| 2142 | break; |
| 2143 | } |
| 2144 | mutex_unlock(&cm_list_mtx); |
| 2145 | |
| 2146 | if (!found_power_supply) |
| 2147 | return; |
| 2148 | |
| 2149 | switch (type) { |
| 2150 | case CM_EVENT_BATT_FULL: |
| 2151 | fullbatt_handler(cm); |
| 2152 | break; |
| 2153 | case CM_EVENT_BATT_OUT: |
| 2154 | battout_handler(cm); |
| 2155 | break; |
| 2156 | case CM_EVENT_BATT_IN: |
| 2157 | case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP: |
| 2158 | misc_event_handler(cm, type); |
| 2159 | break; |
| 2160 | case CM_EVENT_UNKNOWN: |
| 2161 | case CM_EVENT_OTHERS: |
| 2162 | uevent_notify(cm, msg ? msg : default_event_names[type]); |
| 2163 | break; |
| 2164 | default: |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 2165 | dev_err(cm->dev, "%s: type not specified\n", __func__); |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 2166 | break; |
| 2167 | } |
| 2168 | } |
| 2169 | EXPORT_SYMBOL_GPL(cm_notify_event); |
| 2170 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2171 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 2172 | MODULE_DESCRIPTION("Charger Manager"); |
| 2173 | MODULE_LICENSE("GPL"); |