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