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 | |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/rtc.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/power/charger-manager.h> |
| 24 | #include <linux/regulator/consumer.h> |
| 25 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 26 | static const char * const default_event_names[] = { |
| 27 | [CM_EVENT_UNKNOWN] = "Unknown", |
| 28 | [CM_EVENT_BATT_FULL] = "Battery Full", |
| 29 | [CM_EVENT_BATT_IN] = "Battery Inserted", |
| 30 | [CM_EVENT_BATT_OUT] = "Battery Pulled Out", |
| 31 | [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach", |
| 32 | [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop", |
| 33 | [CM_EVENT_OTHERS] = "Other battery events" |
| 34 | }; |
| 35 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 36 | /* |
| 37 | * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for |
| 38 | * delayed works so that we can run delayed works with CM_JIFFIES_SMALL |
| 39 | * without any delays. |
| 40 | */ |
| 41 | #define CM_JIFFIES_SMALL (2) |
| 42 | |
| 43 | /* If y is valid (> 0) and smaller than x, do x = y */ |
| 44 | #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x)) |
| 45 | |
| 46 | /* |
| 47 | * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking |
| 48 | * rtc alarm. It should be 2 or larger |
| 49 | */ |
| 50 | #define CM_RTC_SMALL (2) |
| 51 | |
| 52 | #define UEVENT_BUF_SIZE 32 |
| 53 | |
| 54 | static LIST_HEAD(cm_list); |
| 55 | static DEFINE_MUTEX(cm_list_mtx); |
| 56 | |
| 57 | /* About in-suspend (suspend-again) monitoring */ |
| 58 | static struct rtc_device *rtc_dev; |
| 59 | /* |
| 60 | * Backup RTC alarm |
| 61 | * Save the wakeup alarm before entering suspend-to-RAM |
| 62 | */ |
| 63 | static struct rtc_wkalrm rtc_wkalarm_save; |
| 64 | /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */ |
| 65 | static unsigned long rtc_wkalarm_save_time; |
| 66 | static bool cm_suspended; |
| 67 | static bool cm_rtc_set; |
| 68 | static unsigned long cm_suspend_duration_ms; |
| 69 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 70 | /* About normal (not suspended) monitoring */ |
| 71 | static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */ |
| 72 | static unsigned long next_polling; /* Next appointed polling time */ |
| 73 | static struct workqueue_struct *cm_wq; /* init at driver add */ |
| 74 | static struct delayed_work cm_monitor_work; /* init at driver add */ |
| 75 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 76 | /* Global charger-manager description */ |
| 77 | static struct charger_global_desc *g_desc; /* init with setup_charger_manager */ |
| 78 | |
| 79 | /** |
| 80 | * is_batt_present - See if the battery presents in place. |
| 81 | * @cm: the Charger Manager representing the battery. |
| 82 | */ |
| 83 | static bool is_batt_present(struct charger_manager *cm) |
| 84 | { |
| 85 | union power_supply_propval val; |
| 86 | bool present = false; |
| 87 | int i, ret; |
| 88 | |
| 89 | switch (cm->desc->battery_present) { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 90 | case CM_BATTERY_PRESENT: |
| 91 | present = true; |
| 92 | break; |
| 93 | case CM_NO_BATTERY: |
| 94 | break; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 95 | case CM_FUEL_GAUGE: |
| 96 | ret = cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 97 | POWER_SUPPLY_PROP_PRESENT, &val); |
| 98 | if (ret == 0 && val.intval) |
| 99 | present = true; |
| 100 | break; |
| 101 | case CM_CHARGER_STAT: |
| 102 | for (i = 0; cm->charger_stat[i]; i++) { |
| 103 | ret = cm->charger_stat[i]->get_property( |
| 104 | cm->charger_stat[i], |
| 105 | POWER_SUPPLY_PROP_PRESENT, &val); |
| 106 | if (ret == 0 && val.intval) { |
| 107 | present = true; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | return present; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * is_ext_pwr_online - See if an external power source is attached to charge |
| 119 | * @cm: the Charger Manager representing the battery. |
| 120 | * |
| 121 | * Returns true if at least one of the chargers of the battery has an external |
| 122 | * power source attached to charge the battery regardless of whether it is |
| 123 | * actually charging or not. |
| 124 | */ |
| 125 | static bool is_ext_pwr_online(struct charger_manager *cm) |
| 126 | { |
| 127 | union power_supply_propval val; |
| 128 | bool online = false; |
| 129 | int i, ret; |
| 130 | |
| 131 | /* If at least one of them has one, it's yes. */ |
| 132 | for (i = 0; cm->charger_stat[i]; i++) { |
| 133 | ret = cm->charger_stat[i]->get_property( |
| 134 | cm->charger_stat[i], |
| 135 | POWER_SUPPLY_PROP_ONLINE, &val); |
| 136 | if (ret == 0 && val.intval) { |
| 137 | online = true; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return online; |
| 143 | } |
| 144 | |
| 145 | /** |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 146 | * get_batt_uV - Get the voltage level of the battery |
| 147 | * @cm: the Charger Manager representing the battery. |
| 148 | * @uV: the voltage level returned. |
| 149 | * |
| 150 | * Returns 0 if there is no error. |
| 151 | * Returns a negative value on error. |
| 152 | */ |
| 153 | static int get_batt_uV(struct charger_manager *cm, int *uV) |
| 154 | { |
| 155 | union power_supply_propval val; |
| 156 | int ret; |
| 157 | |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 158 | if (!cm->fuel_gauge) |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 159 | return -ENODEV; |
| 160 | |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 161 | ret = cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 162 | POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 163 | if (ret) |
| 164 | return ret; |
| 165 | |
| 166 | *uV = val.intval; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 171 | * is_charging - Returns true if the battery is being charged. |
| 172 | * @cm: the Charger Manager representing the battery. |
| 173 | */ |
| 174 | static bool is_charging(struct charger_manager *cm) |
| 175 | { |
| 176 | int i, ret; |
| 177 | bool charging = false; |
| 178 | union power_supply_propval val; |
| 179 | |
| 180 | /* If there is no battery, it cannot be charged */ |
| 181 | if (!is_batt_present(cm)) |
| 182 | return false; |
| 183 | |
| 184 | /* If at least one of the charger is charging, return yes */ |
| 185 | for (i = 0; cm->charger_stat[i]; i++) { |
| 186 | /* 1. The charger sholuld not be DISABLED */ |
| 187 | if (cm->emergency_stop) |
| 188 | continue; |
| 189 | if (!cm->charger_enabled) |
| 190 | continue; |
| 191 | |
| 192 | /* 2. The charger should be online (ext-power) */ |
| 193 | ret = cm->charger_stat[i]->get_property( |
| 194 | cm->charger_stat[i], |
| 195 | POWER_SUPPLY_PROP_ONLINE, &val); |
| 196 | if (ret) { |
| 197 | dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n", |
| 198 | cm->desc->psy_charger_stat[i]); |
| 199 | continue; |
| 200 | } |
| 201 | if (val.intval == 0) |
| 202 | continue; |
| 203 | |
| 204 | /* |
| 205 | * 3. The charger should not be FULL, DISCHARGING, |
| 206 | * or NOT_CHARGING. |
| 207 | */ |
| 208 | ret = cm->charger_stat[i]->get_property( |
| 209 | cm->charger_stat[i], |
| 210 | POWER_SUPPLY_PROP_STATUS, &val); |
| 211 | if (ret) { |
| 212 | dev_warn(cm->dev, "Cannot read STATUS value from %s.\n", |
| 213 | cm->desc->psy_charger_stat[i]); |
| 214 | continue; |
| 215 | } |
| 216 | if (val.intval == POWER_SUPPLY_STATUS_FULL || |
| 217 | val.intval == POWER_SUPPLY_STATUS_DISCHARGING || |
| 218 | val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) |
| 219 | continue; |
| 220 | |
| 221 | /* Then, this is charging. */ |
| 222 | charging = true; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | return charging; |
| 227 | } |
| 228 | |
| 229 | /** |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 230 | * is_full_charged - Returns true if the battery is fully charged. |
| 231 | * @cm: the Charger Manager representing the battery. |
| 232 | */ |
| 233 | static bool is_full_charged(struct charger_manager *cm) |
| 234 | { |
| 235 | struct charger_desc *desc = cm->desc; |
| 236 | union power_supply_propval val; |
| 237 | int ret = 0; |
| 238 | int uV; |
| 239 | |
| 240 | /* If there is no battery, it cannot be charged */ |
| 241 | if (!is_batt_present(cm)) { |
| 242 | val.intval = 0; |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) { |
| 247 | /* Not full if capacity of fuel gauge isn't full */ |
| 248 | ret = cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 249 | POWER_SUPPLY_PROP_CHARGE_FULL, &val); |
| 250 | if (!ret && val.intval > desc->fullbatt_full_capacity) { |
| 251 | val.intval = 1; |
| 252 | goto out; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* Full, if it's over the fullbatt voltage */ |
| 257 | if (desc->fullbatt_uV > 0) { |
| 258 | ret = get_batt_uV(cm, &uV); |
| 259 | if (!ret && uV >= desc->fullbatt_uV) { |
| 260 | val.intval = 1; |
| 261 | goto out; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* Full, if the capacity is more than fullbatt_soc */ |
| 266 | if (cm->fuel_gauge && desc->fullbatt_soc > 0) { |
| 267 | ret = cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 268 | POWER_SUPPLY_PROP_CAPACITY, &val); |
| 269 | if (!ret && val.intval >= desc->fullbatt_soc) { |
| 270 | val.intval = 1; |
| 271 | goto out; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | val.intval = 0; |
| 276 | |
| 277 | out: |
| 278 | return val.intval ? true : false; |
| 279 | } |
| 280 | |
| 281 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 282 | * is_polling_required - Return true if need to continue polling for this CM. |
| 283 | * @cm: the Charger Manager representing the battery. |
| 284 | */ |
| 285 | static bool is_polling_required(struct charger_manager *cm) |
| 286 | { |
| 287 | switch (cm->desc->polling_mode) { |
| 288 | case CM_POLL_DISABLE: |
| 289 | return false; |
| 290 | case CM_POLL_ALWAYS: |
| 291 | return true; |
| 292 | case CM_POLL_EXTERNAL_POWER_ONLY: |
| 293 | return is_ext_pwr_online(cm); |
| 294 | case CM_POLL_CHARGING_ONLY: |
| 295 | return is_charging(cm); |
| 296 | default: |
| 297 | dev_warn(cm->dev, "Incorrect polling_mode (%d)\n", |
| 298 | cm->desc->polling_mode); |
| 299 | } |
| 300 | |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * try_charger_enable - Enable/Disable chargers altogether |
| 306 | * @cm: the Charger Manager representing the battery. |
| 307 | * @enable: true: enable / false: disable |
| 308 | * |
| 309 | * Note that Charger Manager keeps the charger enabled regardless whether |
| 310 | * the charger is charging or not (because battery is full or no external |
| 311 | * power source exists) except when CM needs to disable chargers forcibly |
| 312 | * bacause of emergency causes; when the battery is overheated or too cold. |
| 313 | */ |
| 314 | static int try_charger_enable(struct charger_manager *cm, bool enable) |
| 315 | { |
| 316 | int err = 0, i; |
| 317 | struct charger_desc *desc = cm->desc; |
| 318 | |
| 319 | /* Ignore if it's redundent command */ |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 320 | if (enable == cm->charger_enabled) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 321 | return 0; |
| 322 | |
| 323 | if (enable) { |
| 324 | if (cm->emergency_stop) |
| 325 | return -EAGAIN; |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame^] | 326 | |
| 327 | /* |
| 328 | * Save start time of charging to limit |
| 329 | * maximum possible charging time. |
| 330 | */ |
| 331 | cm->charging_start_time = ktime_to_ms(ktime_get()); |
| 332 | cm->charging_end_time = 0; |
| 333 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 334 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
| 335 | err = regulator_enable(desc->charger_regulators[i].consumer); |
| 336 | if (err < 0) { |
| 337 | dev_warn(cm->dev, |
| 338 | "Cannot enable %s regulator\n", |
| 339 | desc->charger_regulators[i].regulator_name); |
| 340 | } |
| 341 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 342 | } else { |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame^] | 343 | /* |
| 344 | * Save end time of charging to maintain fully charged state |
| 345 | * of battery after full-batt. |
| 346 | */ |
| 347 | cm->charging_start_time = 0; |
| 348 | cm->charging_end_time = ktime_to_ms(ktime_get()); |
| 349 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 350 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
| 351 | err = regulator_disable(desc->charger_regulators[i].consumer); |
| 352 | if (err < 0) { |
| 353 | dev_warn(cm->dev, |
| 354 | "Cannot disable %s regulator\n", |
| 355 | desc->charger_regulators[i].regulator_name); |
| 356 | } |
| 357 | } |
| 358 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 359 | /* |
| 360 | * Abnormal battery state - Stop charging forcibly, |
| 361 | * even if charger was enabled at the other places |
| 362 | */ |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 363 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 364 | if (regulator_is_enabled( |
| 365 | desc->charger_regulators[i].consumer)) { |
| 366 | regulator_force_disable( |
| 367 | desc->charger_regulators[i].consumer); |
| 368 | dev_warn(cm->dev, |
| 369 | "Disable regulator(%s) forcibly.\n", |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 370 | desc->charger_regulators[i].regulator_name); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (!err) |
| 376 | cm->charger_enabled = enable; |
| 377 | |
| 378 | return err; |
| 379 | } |
| 380 | |
| 381 | /** |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 382 | * try_charger_restart - Restart charging. |
| 383 | * @cm: the Charger Manager representing the battery. |
| 384 | * |
| 385 | * Restart charging by turning off and on the charger. |
| 386 | */ |
| 387 | static int try_charger_restart(struct charger_manager *cm) |
| 388 | { |
| 389 | int err; |
| 390 | |
| 391 | if (cm->emergency_stop) |
| 392 | return -EAGAIN; |
| 393 | |
| 394 | err = try_charger_enable(cm, false); |
| 395 | if (err) |
| 396 | return err; |
| 397 | |
| 398 | return try_charger_enable(cm, true); |
| 399 | } |
| 400 | |
| 401 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 402 | * uevent_notify - Let users know something has changed. |
| 403 | * @cm: the Charger Manager representing the battery. |
| 404 | * @event: the event string. |
| 405 | * |
| 406 | * If @event is null, it implies that uevent_notify is called |
| 407 | * by resume function. When called in the resume function, cm_suspended |
| 408 | * should be already reset to false in order to let uevent_notify |
| 409 | * notify the recent event during the suspend to users. While |
| 410 | * suspended, uevent_notify does not notify users, but tracks |
| 411 | * events so that uevent_notify can notify users later after resumed. |
| 412 | */ |
| 413 | static void uevent_notify(struct charger_manager *cm, const char *event) |
| 414 | { |
| 415 | static char env_str[UEVENT_BUF_SIZE + 1] = ""; |
| 416 | static char env_str_save[UEVENT_BUF_SIZE + 1] = ""; |
| 417 | |
| 418 | if (cm_suspended) { |
| 419 | /* Nothing in suspended-event buffer */ |
| 420 | if (env_str_save[0] == 0) { |
| 421 | if (!strncmp(env_str, event, UEVENT_BUF_SIZE)) |
| 422 | return; /* status not changed */ |
| 423 | strncpy(env_str_save, event, UEVENT_BUF_SIZE); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE)) |
| 428 | return; /* Duplicated. */ |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 429 | strncpy(env_str_save, event, UEVENT_BUF_SIZE); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 430 | return; |
| 431 | } |
| 432 | |
| 433 | if (event == NULL) { |
| 434 | /* No messages pending */ |
| 435 | if (!env_str_save[0]) |
| 436 | return; |
| 437 | |
| 438 | strncpy(env_str, env_str_save, UEVENT_BUF_SIZE); |
| 439 | kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE); |
| 440 | env_str_save[0] = 0; |
| 441 | |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | /* status not changed */ |
| 446 | if (!strncmp(env_str, event, UEVENT_BUF_SIZE)) |
| 447 | return; |
| 448 | |
| 449 | /* save the status and notify the update */ |
| 450 | strncpy(env_str, event, UEVENT_BUF_SIZE); |
| 451 | kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE); |
| 452 | |
| 453 | dev_info(cm->dev, event); |
| 454 | } |
| 455 | |
| 456 | /** |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 457 | * fullbatt_vchk - Check voltage drop some times after "FULL" event. |
| 458 | * @work: the work_struct appointing the function |
| 459 | * |
| 460 | * If a user has designated "fullbatt_vchkdrop_ms/uV" values with |
| 461 | * charger_desc, Charger Manager checks voltage drop after the battery |
| 462 | * "FULL" event. It checks whether the voltage has dropped more than |
| 463 | * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms. |
| 464 | */ |
| 465 | static void fullbatt_vchk(struct work_struct *work) |
| 466 | { |
| 467 | struct delayed_work *dwork = to_delayed_work(work); |
| 468 | struct charger_manager *cm = container_of(dwork, |
| 469 | struct charger_manager, fullbatt_vchk_work); |
| 470 | struct charger_desc *desc = cm->desc; |
| 471 | int batt_uV, err, diff; |
| 472 | |
| 473 | /* remove the appointment for fullbatt_vchk */ |
| 474 | cm->fullbatt_vchk_jiffies_at = 0; |
| 475 | |
| 476 | if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms) |
| 477 | return; |
| 478 | |
| 479 | err = get_batt_uV(cm, &batt_uV); |
| 480 | if (err) { |
| 481 | dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err); |
| 482 | return; |
| 483 | } |
| 484 | |
Chanwoo Choi | fd65ee5 | 2012-07-27 14:01:37 +0900 | [diff] [blame] | 485 | diff = desc->fullbatt_uV; |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 486 | diff -= batt_uV; |
| 487 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 488 | dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 489 | |
| 490 | if (diff > desc->fullbatt_vchkdrop_uV) { |
| 491 | try_charger_restart(cm); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame^] | 492 | uevent_notify(cm, "Recharging"); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | /** |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame^] | 497 | * check_charging_duration - Monitor charging/discharging duration |
| 498 | * @cm: the Charger Manager representing the battery. |
| 499 | * |
| 500 | * If whole charging duration exceed 'charging_max_duration_ms', |
| 501 | * cm stop charging to prevent overcharge/overheat. If discharging |
| 502 | * duration exceed 'discharging _max_duration_ms', charger cable is |
| 503 | * attached, after full-batt, cm start charging to maintain fully |
| 504 | * charged state for battery. |
| 505 | */ |
| 506 | static int check_charging_duration(struct charger_manager *cm) |
| 507 | { |
| 508 | struct charger_desc *desc = cm->desc; |
| 509 | u64 curr = ktime_to_ms(ktime_get()); |
| 510 | u64 duration; |
| 511 | int ret = false; |
| 512 | |
| 513 | if (!desc->charging_max_duration_ms && |
| 514 | !desc->discharging_max_duration_ms) |
| 515 | return ret; |
| 516 | |
| 517 | if (cm->charger_enabled) { |
| 518 | duration = curr - cm->charging_start_time; |
| 519 | |
| 520 | if (duration > desc->charging_max_duration_ms) { |
| 521 | dev_info(cm->dev, "Charging duration exceed %lldms", |
| 522 | desc->charging_max_duration_ms); |
| 523 | uevent_notify(cm, "Discharging"); |
| 524 | try_charger_enable(cm, false); |
| 525 | ret = true; |
| 526 | } |
| 527 | } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) { |
| 528 | duration = curr - cm->charging_end_time; |
| 529 | |
| 530 | if (duration > desc->charging_max_duration_ms && |
| 531 | is_ext_pwr_online(cm)) { |
| 532 | dev_info(cm->dev, "DisCharging duration exceed %lldms", |
| 533 | desc->discharging_max_duration_ms); |
| 534 | uevent_notify(cm, "Recharing"); |
| 535 | try_charger_enable(cm, true); |
| 536 | ret = true; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 544 | * _cm_monitor - Monitor the temperature and return true for exceptions. |
| 545 | * @cm: the Charger Manager representing the battery. |
| 546 | * |
| 547 | * Returns true if there is an event to notify for the battery. |
| 548 | * (True if the status of "emergency_stop" changes) |
| 549 | */ |
| 550 | static bool _cm_monitor(struct charger_manager *cm) |
| 551 | { |
| 552 | struct charger_desc *desc = cm->desc; |
| 553 | int temp = desc->temperature_out_of_range(&cm->last_temp_mC); |
| 554 | |
| 555 | dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n", |
| 556 | cm->last_temp_mC / 1000, cm->last_temp_mC % 1000); |
| 557 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 558 | /* It has been stopped already */ |
| 559 | if (temp && cm->emergency_stop) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 560 | return false; |
| 561 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 562 | /* |
| 563 | * Check temperature whether overheat or cold. |
| 564 | * If temperature is out of range normal state, stop charging. |
| 565 | */ |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 566 | if (temp) { |
| 567 | cm->emergency_stop = temp; |
| 568 | if (!try_charger_enable(cm, false)) { |
| 569 | if (temp > 0) |
| 570 | uevent_notify(cm, "OVERHEAT"); |
| 571 | else |
| 572 | uevent_notify(cm, "COLD"); |
| 573 | } |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 574 | |
| 575 | /* |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame^] | 576 | * Check whole charging duration and discharing duration |
| 577 | * after full-batt. |
| 578 | */ |
| 579 | } else if (!cm->emergency_stop && check_charging_duration(cm)) { |
| 580 | dev_dbg(cm->dev, |
| 581 | "Charging/Discharging duration is out of range"); |
| 582 | /* |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 583 | * Check dropped voltage of battery. If battery voltage is more |
| 584 | * dropped than fullbatt_vchkdrop_uV after fully charged state, |
| 585 | * charger-manager have to recharge battery. |
| 586 | */ |
| 587 | } else if (!cm->emergency_stop && is_ext_pwr_online(cm) && |
| 588 | !cm->charger_enabled) { |
| 589 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
| 590 | |
| 591 | /* |
| 592 | * Check whether fully charged state to protect overcharge |
| 593 | * if charger-manager is charging for battery. |
| 594 | */ |
| 595 | } else if (!cm->emergency_stop && is_full_charged(cm) && |
| 596 | cm->charger_enabled) { |
| 597 | dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n"); |
| 598 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); |
| 599 | |
| 600 | try_charger_enable(cm, false); |
| 601 | |
| 602 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 603 | } else { |
| 604 | cm->emergency_stop = 0; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 605 | if (is_ext_pwr_online(cm)) { |
| 606 | if (!try_charger_enable(cm, true)) |
| 607 | uevent_notify(cm, "CHARGING"); |
| 608 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * cm_monitor - Monitor every battery. |
| 616 | * |
| 617 | * Returns true if there is an event to notify from any of the batteries. |
| 618 | * (True if the status of "emergency_stop" changes) |
| 619 | */ |
| 620 | static bool cm_monitor(void) |
| 621 | { |
| 622 | bool stop = false; |
| 623 | struct charger_manager *cm; |
| 624 | |
| 625 | mutex_lock(&cm_list_mtx); |
| 626 | |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 627 | list_for_each_entry(cm, &cm_list, entry) { |
| 628 | if (_cm_monitor(cm)) |
| 629 | stop = true; |
| 630 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 631 | |
| 632 | mutex_unlock(&cm_list_mtx); |
| 633 | |
| 634 | return stop; |
| 635 | } |
| 636 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 637 | /** |
| 638 | * _setup_polling - Setup the next instance of polling. |
| 639 | * @work: work_struct of the function _setup_polling. |
| 640 | */ |
| 641 | static void _setup_polling(struct work_struct *work) |
| 642 | { |
| 643 | unsigned long min = ULONG_MAX; |
| 644 | struct charger_manager *cm; |
| 645 | bool keep_polling = false; |
| 646 | unsigned long _next_polling; |
| 647 | |
| 648 | mutex_lock(&cm_list_mtx); |
| 649 | |
| 650 | list_for_each_entry(cm, &cm_list, entry) { |
| 651 | if (is_polling_required(cm) && cm->desc->polling_interval_ms) { |
| 652 | keep_polling = true; |
| 653 | |
| 654 | if (min > cm->desc->polling_interval_ms) |
| 655 | min = cm->desc->polling_interval_ms; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | polling_jiffy = msecs_to_jiffies(min); |
| 660 | if (polling_jiffy <= CM_JIFFIES_SMALL) |
| 661 | polling_jiffy = CM_JIFFIES_SMALL + 1; |
| 662 | |
| 663 | if (!keep_polling) |
| 664 | polling_jiffy = ULONG_MAX; |
| 665 | if (polling_jiffy == ULONG_MAX) |
| 666 | goto out; |
| 667 | |
| 668 | WARN(cm_wq == NULL, "charger-manager: workqueue not initialized" |
| 669 | ". try it later. %s\n", __func__); |
| 670 | |
| 671 | _next_polling = jiffies + polling_jiffy; |
| 672 | |
| 673 | if (!delayed_work_pending(&cm_monitor_work) || |
| 674 | (delayed_work_pending(&cm_monitor_work) && |
| 675 | time_after(next_polling, _next_polling))) { |
| 676 | cancel_delayed_work_sync(&cm_monitor_work); |
| 677 | next_polling = jiffies + polling_jiffy; |
| 678 | queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy); |
| 679 | } |
| 680 | |
| 681 | out: |
| 682 | mutex_unlock(&cm_list_mtx); |
| 683 | } |
| 684 | static DECLARE_WORK(setup_polling, _setup_polling); |
| 685 | |
| 686 | /** |
| 687 | * cm_monitor_poller - The Monitor / Poller. |
| 688 | * @work: work_struct of the function cm_monitor_poller |
| 689 | * |
| 690 | * During non-suspended state, cm_monitor_poller is used to poll and monitor |
| 691 | * the batteries. |
| 692 | */ |
| 693 | static void cm_monitor_poller(struct work_struct *work) |
| 694 | { |
| 695 | cm_monitor(); |
| 696 | schedule_work(&setup_polling); |
| 697 | } |
| 698 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 699 | /** |
| 700 | * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL |
| 701 | * @cm: the Charger Manager representing the battery. |
| 702 | */ |
| 703 | static void fullbatt_handler(struct charger_manager *cm) |
| 704 | { |
| 705 | struct charger_desc *desc = cm->desc; |
| 706 | |
| 707 | if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms) |
| 708 | goto out; |
| 709 | |
| 710 | if (cm_suspended) |
| 711 | device_set_wakeup_capable(cm->dev, true); |
| 712 | |
| 713 | if (delayed_work_pending(&cm->fullbatt_vchk_work)) |
| 714 | cancel_delayed_work(&cm->fullbatt_vchk_work); |
| 715 | queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work, |
| 716 | msecs_to_jiffies(desc->fullbatt_vchkdrop_ms)); |
| 717 | cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies( |
| 718 | desc->fullbatt_vchkdrop_ms); |
| 719 | |
| 720 | if (cm->fullbatt_vchk_jiffies_at == 0) |
| 721 | cm->fullbatt_vchk_jiffies_at = 1; |
| 722 | |
| 723 | out: |
| 724 | dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n"); |
| 725 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * battout_handler - Event handler for CM_EVENT_BATT_OUT |
| 730 | * @cm: the Charger Manager representing the battery. |
| 731 | */ |
| 732 | static void battout_handler(struct charger_manager *cm) |
| 733 | { |
| 734 | if (cm_suspended) |
| 735 | device_set_wakeup_capable(cm->dev, true); |
| 736 | |
| 737 | if (!is_batt_present(cm)) { |
| 738 | dev_emerg(cm->dev, "Battery Pulled Out!\n"); |
| 739 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]); |
| 740 | } else { |
| 741 | uevent_notify(cm, "Battery Reinserted?"); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * misc_event_handler - Handler for other evnets |
| 747 | * @cm: the Charger Manager representing the battery. |
| 748 | * @type: the Charger Manager representing the battery. |
| 749 | */ |
| 750 | static void misc_event_handler(struct charger_manager *cm, |
| 751 | enum cm_event_types type) |
| 752 | { |
| 753 | if (cm_suspended) |
| 754 | device_set_wakeup_capable(cm->dev, true); |
| 755 | |
| 756 | if (!delayed_work_pending(&cm_monitor_work) && |
| 757 | is_polling_required(cm) && cm->desc->polling_interval_ms) |
| 758 | schedule_work(&setup_polling); |
| 759 | uevent_notify(cm, default_event_names[type]); |
| 760 | } |
| 761 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 762 | static int charger_get_property(struct power_supply *psy, |
| 763 | enum power_supply_property psp, |
| 764 | union power_supply_propval *val) |
| 765 | { |
| 766 | struct charger_manager *cm = container_of(psy, |
| 767 | struct charger_manager, charger_psy); |
| 768 | struct charger_desc *desc = cm->desc; |
Anton Vorontsov | df58c04 | 2012-03-15 21:01:28 +0400 | [diff] [blame] | 769 | int ret = 0; |
| 770 | int uV; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 771 | |
| 772 | switch (psp) { |
| 773 | case POWER_SUPPLY_PROP_STATUS: |
| 774 | if (is_charging(cm)) |
| 775 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 776 | else if (is_ext_pwr_online(cm)) |
| 777 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 778 | else |
| 779 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 780 | break; |
| 781 | case POWER_SUPPLY_PROP_HEALTH: |
| 782 | if (cm->emergency_stop > 0) |
| 783 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 784 | else if (cm->emergency_stop < 0) |
| 785 | val->intval = POWER_SUPPLY_HEALTH_COLD; |
| 786 | else |
| 787 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 788 | break; |
| 789 | case POWER_SUPPLY_PROP_PRESENT: |
| 790 | if (is_batt_present(cm)) |
| 791 | val->intval = 1; |
| 792 | else |
| 793 | val->intval = 0; |
| 794 | break; |
| 795 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Anton Vorontsov | df58c04 | 2012-03-15 21:01:28 +0400 | [diff] [blame] | 796 | ret = get_batt_uV(cm, &val->intval); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 797 | break; |
| 798 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 799 | ret = cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 800 | POWER_SUPPLY_PROP_CURRENT_NOW, val); |
| 801 | break; |
| 802 | case POWER_SUPPLY_PROP_TEMP: |
| 803 | /* in thenth of centigrade */ |
| 804 | if (cm->last_temp_mC == INT_MIN) |
| 805 | desc->temperature_out_of_range(&cm->last_temp_mC); |
| 806 | val->intval = cm->last_temp_mC / 100; |
| 807 | if (!desc->measure_battery_temp) |
| 808 | ret = -ENODEV; |
| 809 | break; |
| 810 | case POWER_SUPPLY_PROP_TEMP_AMBIENT: |
| 811 | /* in thenth of centigrade */ |
| 812 | if (cm->last_temp_mC == INT_MIN) |
| 813 | desc->temperature_out_of_range(&cm->last_temp_mC); |
| 814 | val->intval = cm->last_temp_mC / 100; |
| 815 | if (desc->measure_battery_temp) |
| 816 | ret = -ENODEV; |
| 817 | break; |
| 818 | case POWER_SUPPLY_PROP_CAPACITY: |
| 819 | if (!cm->fuel_gauge) { |
| 820 | ret = -ENODEV; |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | if (!is_batt_present(cm)) { |
| 825 | /* There is no battery. Assume 100% */ |
| 826 | val->intval = 100; |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | ret = cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 831 | POWER_SUPPLY_PROP_CAPACITY, val); |
| 832 | if (ret) |
| 833 | break; |
| 834 | |
| 835 | if (val->intval > 100) { |
| 836 | val->intval = 100; |
| 837 | break; |
| 838 | } |
| 839 | if (val->intval < 0) |
| 840 | val->intval = 0; |
| 841 | |
| 842 | /* Do not adjust SOC when charging: voltage is overrated */ |
| 843 | if (is_charging(cm)) |
| 844 | break; |
| 845 | |
| 846 | /* |
| 847 | * If the capacity value is inconsistent, calibrate it base on |
| 848 | * the battery voltage values and the thresholds given as desc |
| 849 | */ |
| 850 | ret = get_batt_uV(cm, &uV); |
| 851 | if (ret) { |
| 852 | /* Voltage information not available. No calibration */ |
| 853 | ret = 0; |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV && |
| 858 | !is_charging(cm)) { |
| 859 | val->intval = 100; |
| 860 | break; |
| 861 | } |
| 862 | |
| 863 | break; |
| 864 | case POWER_SUPPLY_PROP_ONLINE: |
| 865 | if (is_ext_pwr_online(cm)) |
| 866 | val->intval = 1; |
| 867 | else |
| 868 | val->intval = 0; |
| 869 | break; |
| 870 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 871 | if (is_full_charged(cm)) |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 872 | val->intval = 1; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 873 | else |
| 874 | val->intval = 0; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 875 | ret = 0; |
| 876 | break; |
| 877 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 878 | if (is_charging(cm)) { |
| 879 | ret = cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 880 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 881 | val); |
| 882 | if (ret) { |
| 883 | val->intval = 1; |
| 884 | ret = 0; |
| 885 | } else { |
| 886 | /* If CHARGE_NOW is supplied, use it */ |
| 887 | val->intval = (val->intval > 0) ? |
| 888 | val->intval : 1; |
| 889 | } |
| 890 | } else { |
| 891 | val->intval = 0; |
| 892 | } |
| 893 | break; |
| 894 | default: |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | return ret; |
| 898 | } |
| 899 | |
| 900 | #define NUM_CHARGER_PSY_OPTIONAL (4) |
| 901 | static enum power_supply_property default_charger_props[] = { |
| 902 | /* Guaranteed to provide */ |
| 903 | POWER_SUPPLY_PROP_STATUS, |
| 904 | POWER_SUPPLY_PROP_HEALTH, |
| 905 | POWER_SUPPLY_PROP_PRESENT, |
| 906 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 907 | POWER_SUPPLY_PROP_CAPACITY, |
| 908 | POWER_SUPPLY_PROP_ONLINE, |
| 909 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 910 | /* |
| 911 | * Optional properties are: |
| 912 | * POWER_SUPPLY_PROP_CHARGE_NOW, |
| 913 | * POWER_SUPPLY_PROP_CURRENT_NOW, |
| 914 | * POWER_SUPPLY_PROP_TEMP, and |
| 915 | * POWER_SUPPLY_PROP_TEMP_AMBIENT, |
| 916 | */ |
| 917 | }; |
| 918 | |
| 919 | static struct power_supply psy_default = { |
| 920 | .name = "battery", |
| 921 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 922 | .properties = default_charger_props, |
| 923 | .num_properties = ARRAY_SIZE(default_charger_props), |
| 924 | .get_property = charger_get_property, |
| 925 | }; |
| 926 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 927 | /** |
| 928 | * cm_setup_timer - For in-suspend monitoring setup wakeup alarm |
| 929 | * for suspend_again. |
| 930 | * |
| 931 | * Returns true if the alarm is set for Charger Manager to use. |
| 932 | * Returns false if |
| 933 | * cm_setup_timer fails to set an alarm, |
| 934 | * cm_setup_timer does not need to set an alarm for Charger Manager, |
| 935 | * or an alarm previously configured is to be used. |
| 936 | */ |
| 937 | static bool cm_setup_timer(void) |
| 938 | { |
| 939 | struct charger_manager *cm; |
| 940 | unsigned int wakeup_ms = UINT_MAX; |
| 941 | bool ret = false; |
| 942 | |
| 943 | mutex_lock(&cm_list_mtx); |
| 944 | |
| 945 | list_for_each_entry(cm, &cm_list, entry) { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 946 | unsigned int fbchk_ms = 0; |
| 947 | |
| 948 | /* fullbatt_vchk is required. setup timer for that */ |
| 949 | if (cm->fullbatt_vchk_jiffies_at) { |
| 950 | fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at |
| 951 | - jiffies); |
| 952 | if (time_is_before_eq_jiffies( |
| 953 | cm->fullbatt_vchk_jiffies_at) || |
| 954 | msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) { |
| 955 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
| 956 | fbchk_ms = 0; |
| 957 | } |
| 958 | } |
| 959 | CM_MIN_VALID(wakeup_ms, fbchk_ms); |
| 960 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 961 | /* Skip if polling is not required for this CM */ |
| 962 | if (!is_polling_required(cm) && !cm->emergency_stop) |
| 963 | continue; |
| 964 | if (cm->desc->polling_interval_ms == 0) |
| 965 | continue; |
| 966 | CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms); |
| 967 | } |
| 968 | |
| 969 | mutex_unlock(&cm_list_mtx); |
| 970 | |
| 971 | if (wakeup_ms < UINT_MAX && wakeup_ms > 0) { |
| 972 | pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms); |
| 973 | if (rtc_dev) { |
| 974 | struct rtc_wkalrm tmp; |
| 975 | unsigned long time, now; |
| 976 | unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000); |
| 977 | |
| 978 | /* |
| 979 | * Set alarm with the polling interval (wakeup_ms) |
| 980 | * except when rtc_wkalarm_save comes first. |
| 981 | * However, the alarm time should be NOW + |
| 982 | * CM_RTC_SMALL or later. |
| 983 | */ |
| 984 | tmp.enabled = 1; |
| 985 | rtc_read_time(rtc_dev, &tmp.time); |
| 986 | rtc_tm_to_time(&tmp.time, &now); |
| 987 | if (add < CM_RTC_SMALL) |
| 988 | add = CM_RTC_SMALL; |
| 989 | time = now + add; |
| 990 | |
| 991 | ret = true; |
| 992 | |
| 993 | if (rtc_wkalarm_save.enabled && |
| 994 | rtc_wkalarm_save_time && |
| 995 | rtc_wkalarm_save_time < time) { |
| 996 | if (rtc_wkalarm_save_time < now + CM_RTC_SMALL) |
| 997 | time = now + CM_RTC_SMALL; |
| 998 | else |
| 999 | time = rtc_wkalarm_save_time; |
| 1000 | |
| 1001 | /* The timer is not appointed by CM */ |
| 1002 | ret = false; |
| 1003 | } |
| 1004 | |
| 1005 | pr_info("Waking up after %lu secs.\n", |
| 1006 | time - now); |
| 1007 | |
| 1008 | rtc_time_to_tm(time, &tmp.time); |
| 1009 | rtc_set_alarm(rtc_dev, &tmp); |
| 1010 | cm_suspend_duration_ms += wakeup_ms; |
| 1011 | return ret; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (rtc_dev) |
| 1016 | rtc_set_alarm(rtc_dev, &rtc_wkalarm_save); |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1020 | static void _cm_fbchk_in_suspend(struct charger_manager *cm) |
| 1021 | { |
| 1022 | unsigned long jiffy_now = jiffies; |
| 1023 | |
| 1024 | if (!cm->fullbatt_vchk_jiffies_at) |
| 1025 | return; |
| 1026 | |
| 1027 | if (g_desc && g_desc->assume_timer_stops_in_suspend) |
| 1028 | jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms); |
| 1029 | |
| 1030 | /* Execute now if it's going to be executed not too long after */ |
| 1031 | jiffy_now += CM_JIFFIES_SMALL; |
| 1032 | |
| 1033 | if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at)) |
| 1034 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
| 1035 | } |
| 1036 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1037 | /** |
| 1038 | * cm_suspend_again - Determine whether suspend again or not |
| 1039 | * |
| 1040 | * Returns true if the system should be suspended again |
| 1041 | * Returns false if the system should be woken up |
| 1042 | */ |
| 1043 | bool cm_suspend_again(void) |
| 1044 | { |
| 1045 | struct charger_manager *cm; |
| 1046 | bool ret = false; |
| 1047 | |
| 1048 | if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() || |
| 1049 | !cm_rtc_set) |
| 1050 | return false; |
| 1051 | |
| 1052 | if (cm_monitor()) |
| 1053 | goto out; |
| 1054 | |
| 1055 | ret = true; |
| 1056 | mutex_lock(&cm_list_mtx); |
| 1057 | list_for_each_entry(cm, &cm_list, entry) { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1058 | _cm_fbchk_in_suspend(cm); |
| 1059 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1060 | if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) || |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1061 | cm->status_save_batt != is_batt_present(cm)) { |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1062 | ret = false; |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1063 | break; |
| 1064 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1065 | } |
| 1066 | mutex_unlock(&cm_list_mtx); |
| 1067 | |
| 1068 | cm_rtc_set = cm_setup_timer(); |
| 1069 | out: |
| 1070 | /* It's about the time when the non-CM appointed timer goes off */ |
| 1071 | if (rtc_wkalarm_save.enabled) { |
| 1072 | unsigned long now; |
| 1073 | struct rtc_time tmp; |
| 1074 | |
| 1075 | rtc_read_time(rtc_dev, &tmp); |
| 1076 | rtc_tm_to_time(&tmp, &now); |
| 1077 | |
| 1078 | if (rtc_wkalarm_save_time && |
| 1079 | now + CM_RTC_SMALL >= rtc_wkalarm_save_time) |
| 1080 | return false; |
| 1081 | } |
| 1082 | return ret; |
| 1083 | } |
| 1084 | EXPORT_SYMBOL_GPL(cm_suspend_again); |
| 1085 | |
| 1086 | /** |
| 1087 | * setup_charger_manager - initialize charger_global_desc data |
| 1088 | * @gd: pointer to instance of charger_global_desc |
| 1089 | */ |
| 1090 | int setup_charger_manager(struct charger_global_desc *gd) |
| 1091 | { |
| 1092 | if (!gd) |
| 1093 | return -EINVAL; |
| 1094 | |
| 1095 | if (rtc_dev) |
| 1096 | rtc_class_close(rtc_dev); |
| 1097 | rtc_dev = NULL; |
| 1098 | g_desc = NULL; |
| 1099 | |
| 1100 | if (!gd->rtc_only_wakeup) { |
| 1101 | pr_err("The callback rtc_only_wakeup is not given.\n"); |
| 1102 | return -EINVAL; |
| 1103 | } |
| 1104 | |
| 1105 | if (gd->rtc_name) { |
| 1106 | rtc_dev = rtc_class_open(gd->rtc_name); |
| 1107 | if (IS_ERR_OR_NULL(rtc_dev)) { |
| 1108 | rtc_dev = NULL; |
| 1109 | /* Retry at probe. RTC may be not registered yet */ |
| 1110 | } |
| 1111 | } else { |
| 1112 | pr_warn("No wakeup timer is given for charger manager." |
| 1113 | "In-suspend monitoring won't work.\n"); |
| 1114 | } |
| 1115 | |
| 1116 | g_desc = gd; |
| 1117 | return 0; |
| 1118 | } |
| 1119 | EXPORT_SYMBOL_GPL(setup_charger_manager); |
| 1120 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1121 | /** |
| 1122 | * charger_extcon_work - enable/diable charger according to the state |
| 1123 | * of charger cable |
| 1124 | * |
| 1125 | * @work: work_struct of the function charger_extcon_work. |
| 1126 | */ |
| 1127 | static void charger_extcon_work(struct work_struct *work) |
| 1128 | { |
| 1129 | struct charger_cable *cable = |
| 1130 | container_of(work, struct charger_cable, wq); |
Chanwoo Choi | 45cd4fb | 2012-07-12 15:03:29 +0900 | [diff] [blame] | 1131 | int ret; |
| 1132 | |
| 1133 | if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) { |
| 1134 | ret = regulator_set_current_limit(cable->charger->consumer, |
| 1135 | cable->min_uA, cable->max_uA); |
| 1136 | if (ret < 0) { |
| 1137 | pr_err("Cannot set current limit of %s (%s)\n", |
| 1138 | cable->charger->regulator_name, cable->name); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | pr_info("Set current limit of %s : %duA ~ %duA\n", |
| 1143 | cable->charger->regulator_name, |
| 1144 | cable->min_uA, cable->max_uA); |
| 1145 | } |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1146 | |
| 1147 | try_charger_enable(cable->cm, cable->attached); |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * charger_extcon_notifier - receive the state of charger cable |
| 1152 | * when registered cable is attached or detached. |
| 1153 | * |
| 1154 | * @self: the notifier block of the charger_extcon_notifier. |
| 1155 | * @event: the cable state. |
| 1156 | * @ptr: the data pointer of notifier block. |
| 1157 | */ |
| 1158 | static int charger_extcon_notifier(struct notifier_block *self, |
| 1159 | unsigned long event, void *ptr) |
| 1160 | { |
| 1161 | struct charger_cable *cable = |
| 1162 | container_of(self, struct charger_cable, nb); |
| 1163 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1164 | /* |
| 1165 | * The newly state of charger cable. |
| 1166 | * If cable is attached, cable->attached is true. |
| 1167 | */ |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1168 | cable->attached = event; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1169 | |
| 1170 | /* |
| 1171 | * Setup monitoring to check battery state |
| 1172 | * when charger cable is attached. |
| 1173 | */ |
| 1174 | if (cable->attached && is_polling_required(cable->cm)) { |
| 1175 | if (work_pending(&setup_polling)) |
| 1176 | cancel_work_sync(&setup_polling); |
| 1177 | schedule_work(&setup_polling); |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * Setup work for controlling charger(regulator) |
| 1182 | * according to charger cable. |
| 1183 | */ |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1184 | schedule_work(&cable->wq); |
| 1185 | |
| 1186 | return NOTIFY_DONE; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * charger_extcon_init - register external connector to use it |
| 1191 | * as the charger cable |
| 1192 | * |
| 1193 | * @cm: the Charger Manager representing the battery. |
| 1194 | * @cable: the Charger cable representing the external connector. |
| 1195 | */ |
| 1196 | static int charger_extcon_init(struct charger_manager *cm, |
| 1197 | struct charger_cable *cable) |
| 1198 | { |
| 1199 | int ret = 0; |
| 1200 | |
| 1201 | /* |
| 1202 | * Charger manager use Extcon framework to identify |
| 1203 | * the charger cable among various external connector |
| 1204 | * cable (e.g., TA, USB, MHL, Dock). |
| 1205 | */ |
| 1206 | INIT_WORK(&cable->wq, charger_extcon_work); |
| 1207 | cable->nb.notifier_call = charger_extcon_notifier; |
| 1208 | ret = extcon_register_interest(&cable->extcon_dev, |
| 1209 | cable->extcon_name, cable->name, &cable->nb); |
| 1210 | if (ret < 0) { |
| 1211 | pr_info("Cannot register extcon_dev for %s(cable: %s).\n", |
| 1212 | cable->extcon_name, |
| 1213 | cable->name); |
| 1214 | ret = -EINVAL; |
| 1215 | } |
| 1216 | |
| 1217 | return ret; |
| 1218 | } |
| 1219 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1220 | static int charger_manager_probe(struct platform_device *pdev) |
| 1221 | { |
| 1222 | struct charger_desc *desc = dev_get_platdata(&pdev->dev); |
| 1223 | struct charger_manager *cm; |
| 1224 | int ret = 0, i = 0; |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1225 | int j = 0; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1226 | union power_supply_propval val; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1227 | |
| 1228 | if (g_desc && !rtc_dev && g_desc->rtc_name) { |
| 1229 | rtc_dev = rtc_class_open(g_desc->rtc_name); |
| 1230 | if (IS_ERR_OR_NULL(rtc_dev)) { |
| 1231 | rtc_dev = NULL; |
| 1232 | dev_err(&pdev->dev, "Cannot get RTC %s.\n", |
| 1233 | g_desc->rtc_name); |
| 1234 | ret = -ENODEV; |
| 1235 | goto err_alloc; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | if (!desc) { |
| 1240 | dev_err(&pdev->dev, "No platform data (desc) found.\n"); |
| 1241 | ret = -ENODEV; |
| 1242 | goto err_alloc; |
| 1243 | } |
| 1244 | |
| 1245 | cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL); |
| 1246 | if (!cm) { |
| 1247 | dev_err(&pdev->dev, "Cannot allocate memory.\n"); |
| 1248 | ret = -ENOMEM; |
| 1249 | goto err_alloc; |
| 1250 | } |
| 1251 | |
| 1252 | /* Basic Values. Unspecified are Null or 0 */ |
| 1253 | cm->dev = &pdev->dev; |
| 1254 | cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL); |
| 1255 | if (!cm->desc) { |
| 1256 | dev_err(&pdev->dev, "Cannot allocate memory.\n"); |
| 1257 | ret = -ENOMEM; |
| 1258 | goto err_alloc_desc; |
| 1259 | } |
| 1260 | memcpy(cm->desc, desc, sizeof(struct charger_desc)); |
| 1261 | cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */ |
| 1262 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1263 | /* |
| 1264 | * The following two do not need to be errors. |
| 1265 | * Users may intentionally ignore those two features. |
| 1266 | */ |
| 1267 | if (desc->fullbatt_uV == 0) { |
| 1268 | dev_info(&pdev->dev, "Ignoring full-battery voltage threshold" |
| 1269 | " as it is not supplied."); |
| 1270 | } |
| 1271 | if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) { |
| 1272 | dev_info(&pdev->dev, "Disabling full-battery voltage drop " |
| 1273 | "checking mechanism as it is not supplied."); |
| 1274 | desc->fullbatt_vchkdrop_ms = 0; |
| 1275 | desc->fullbatt_vchkdrop_uV = 0; |
| 1276 | } |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1277 | if (desc->fullbatt_soc == 0) { |
| 1278 | dev_info(&pdev->dev, "Ignoring full-battery soc(state of" |
| 1279 | " charge) threshold as it is not" |
| 1280 | " supplied."); |
| 1281 | } |
| 1282 | if (desc->fullbatt_full_capacity == 0) { |
| 1283 | dev_info(&pdev->dev, "Ignoring full-battery full capacity" |
| 1284 | " threshold as it is not supplied."); |
| 1285 | } |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1286 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1287 | if (!desc->charger_regulators || desc->num_charger_regulators < 1) { |
| 1288 | ret = -EINVAL; |
| 1289 | dev_err(&pdev->dev, "charger_regulators undefined.\n"); |
| 1290 | goto err_no_charger; |
| 1291 | } |
| 1292 | |
| 1293 | if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) { |
| 1294 | dev_err(&pdev->dev, "No power supply defined.\n"); |
| 1295 | ret = -EINVAL; |
| 1296 | goto err_no_charger_stat; |
| 1297 | } |
| 1298 | |
| 1299 | /* Counting index only */ |
| 1300 | while (desc->psy_charger_stat[i]) |
| 1301 | i++; |
| 1302 | |
| 1303 | cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1), |
| 1304 | GFP_KERNEL); |
| 1305 | if (!cm->charger_stat) { |
| 1306 | ret = -ENOMEM; |
| 1307 | goto err_no_charger_stat; |
| 1308 | } |
| 1309 | |
| 1310 | for (i = 0; desc->psy_charger_stat[i]; i++) { |
| 1311 | cm->charger_stat[i] = power_supply_get_by_name( |
| 1312 | desc->psy_charger_stat[i]); |
| 1313 | if (!cm->charger_stat[i]) { |
| 1314 | dev_err(&pdev->dev, "Cannot find power supply " |
| 1315 | "\"%s\"\n", |
| 1316 | desc->psy_charger_stat[i]); |
| 1317 | ret = -ENODEV; |
| 1318 | goto err_chg_stat; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge); |
| 1323 | if (!cm->fuel_gauge) { |
| 1324 | dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n", |
| 1325 | desc->psy_fuel_gauge); |
| 1326 | ret = -ENODEV; |
| 1327 | goto err_chg_stat; |
| 1328 | } |
| 1329 | |
| 1330 | if (desc->polling_interval_ms == 0 || |
| 1331 | msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) { |
| 1332 | dev_err(&pdev->dev, "polling_interval_ms is too small\n"); |
| 1333 | ret = -EINVAL; |
| 1334 | goto err_chg_stat; |
| 1335 | } |
| 1336 | |
| 1337 | if (!desc->temperature_out_of_range) { |
| 1338 | dev_err(&pdev->dev, "there is no temperature_out_of_range\n"); |
| 1339 | ret = -EINVAL; |
| 1340 | goto err_chg_stat; |
| 1341 | } |
| 1342 | |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame^] | 1343 | if (!desc->charging_max_duration_ms || |
| 1344 | !desc->discharging_max_duration_ms) { |
| 1345 | dev_info(&pdev->dev, "Cannot limit charging duration " |
| 1346 | "checking mechanism to prevent overcharge/overheat " |
| 1347 | "and control discharging duration"); |
| 1348 | desc->charging_max_duration_ms = 0; |
| 1349 | desc->discharging_max_duration_ms = 0; |
| 1350 | } |
| 1351 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1352 | platform_set_drvdata(pdev, cm); |
| 1353 | |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1354 | memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default)); |
| 1355 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1356 | if (!desc->psy_name) { |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1357 | strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1358 | } else { |
| 1359 | strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX); |
| 1360 | } |
| 1361 | cm->charger_psy.name = cm->psy_name_buf; |
| 1362 | |
| 1363 | /* Allocate for psy properties because they may vary */ |
| 1364 | cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property) |
| 1365 | * (ARRAY_SIZE(default_charger_props) + |
| 1366 | NUM_CHARGER_PSY_OPTIONAL), |
| 1367 | GFP_KERNEL); |
| 1368 | if (!cm->charger_psy.properties) { |
| 1369 | dev_err(&pdev->dev, "Cannot allocate for psy properties.\n"); |
| 1370 | ret = -ENOMEM; |
| 1371 | goto err_chg_stat; |
| 1372 | } |
| 1373 | memcpy(cm->charger_psy.properties, default_charger_props, |
| 1374 | sizeof(enum power_supply_property) * |
| 1375 | ARRAY_SIZE(default_charger_props)); |
| 1376 | cm->charger_psy.num_properties = psy_default.num_properties; |
| 1377 | |
| 1378 | /* Find which optional psy-properties are available */ |
| 1379 | if (!cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 1380 | POWER_SUPPLY_PROP_CHARGE_NOW, &val)) { |
| 1381 | cm->charger_psy.properties[cm->charger_psy.num_properties] = |
| 1382 | POWER_SUPPLY_PROP_CHARGE_NOW; |
| 1383 | cm->charger_psy.num_properties++; |
| 1384 | } |
| 1385 | if (!cm->fuel_gauge->get_property(cm->fuel_gauge, |
| 1386 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 1387 | &val)) { |
| 1388 | cm->charger_psy.properties[cm->charger_psy.num_properties] = |
| 1389 | POWER_SUPPLY_PROP_CURRENT_NOW; |
| 1390 | cm->charger_psy.num_properties++; |
| 1391 | } |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1392 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1393 | if (desc->measure_battery_temp) { |
| 1394 | cm->charger_psy.properties[cm->charger_psy.num_properties] = |
| 1395 | POWER_SUPPLY_PROP_TEMP; |
| 1396 | cm->charger_psy.num_properties++; |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1397 | } else { |
| 1398 | cm->charger_psy.properties[cm->charger_psy.num_properties] = |
| 1399 | POWER_SUPPLY_PROP_TEMP_AMBIENT; |
| 1400 | cm->charger_psy.num_properties++; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1401 | } |
| 1402 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1403 | INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk); |
| 1404 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1405 | ret = power_supply_register(NULL, &cm->charger_psy); |
| 1406 | if (ret) { |
| 1407 | dev_err(&pdev->dev, "Cannot register charger-manager with" |
| 1408 | " name \"%s\".\n", cm->charger_psy.name); |
| 1409 | goto err_register; |
| 1410 | } |
| 1411 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1412 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
| 1413 | struct charger_regulator *charger |
| 1414 | = &desc->charger_regulators[i]; |
| 1415 | |
| 1416 | charger->consumer = regulator_get(&pdev->dev, |
| 1417 | charger->regulator_name); |
| 1418 | if (charger->consumer == NULL) { |
| 1419 | dev_err(&pdev->dev, "Cannot find charger(%s)n", |
| 1420 | charger->regulator_name); |
| 1421 | ret = -EINVAL; |
| 1422 | goto err_chg_get; |
| 1423 | } |
| 1424 | |
| 1425 | for (j = 0 ; j < charger->num_cables ; j++) { |
| 1426 | struct charger_cable *cable = &charger->cables[j]; |
| 1427 | |
| 1428 | ret = charger_extcon_init(cm, cable); |
| 1429 | if (ret < 0) { |
| 1430 | dev_err(&pdev->dev, "Cannot find charger(%s)n", |
| 1431 | charger->regulator_name); |
| 1432 | goto err_extcon; |
| 1433 | } |
| 1434 | cable->charger = charger; |
| 1435 | cable->cm = cm; |
| 1436 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | ret = try_charger_enable(cm, true); |
| 1440 | if (ret) { |
| 1441 | dev_err(&pdev->dev, "Cannot enable charger regulators\n"); |
| 1442 | goto err_chg_enable; |
| 1443 | } |
| 1444 | |
| 1445 | /* Add to the list */ |
| 1446 | mutex_lock(&cm_list_mtx); |
| 1447 | list_add(&cm->entry, &cm_list); |
| 1448 | mutex_unlock(&cm_list_mtx); |
| 1449 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1450 | /* |
| 1451 | * Charger-manager is capable of waking up the systme from sleep |
| 1452 | * when event is happend through cm_notify_event() |
| 1453 | */ |
| 1454 | device_init_wakeup(&pdev->dev, true); |
| 1455 | device_set_wakeup_capable(&pdev->dev, false); |
| 1456 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1457 | schedule_work(&setup_polling); |
| 1458 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1459 | return 0; |
| 1460 | |
| 1461 | err_chg_enable: |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1462 | err_extcon: |
| 1463 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
| 1464 | struct charger_regulator *charger |
| 1465 | = &desc->charger_regulators[i]; |
| 1466 | for (j = 0 ; j < charger->num_cables ; j++) { |
| 1467 | struct charger_cable *cable = &charger->cables[j]; |
| 1468 | extcon_unregister_interest(&cable->extcon_dev); |
| 1469 | } |
| 1470 | } |
| 1471 | err_chg_get: |
| 1472 | for (i = 0 ; i < desc->num_charger_regulators ; i++) |
| 1473 | regulator_put(desc->charger_regulators[i].consumer); |
| 1474 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1475 | power_supply_unregister(&cm->charger_psy); |
| 1476 | err_register: |
| 1477 | kfree(cm->charger_psy.properties); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1478 | err_chg_stat: |
| 1479 | kfree(cm->charger_stat); |
| 1480 | err_no_charger_stat: |
| 1481 | err_no_charger: |
| 1482 | kfree(cm->desc); |
| 1483 | err_alloc_desc: |
| 1484 | kfree(cm); |
| 1485 | err_alloc: |
| 1486 | return ret; |
| 1487 | } |
| 1488 | |
| 1489 | static int __devexit charger_manager_remove(struct platform_device *pdev) |
| 1490 | { |
| 1491 | struct charger_manager *cm = platform_get_drvdata(pdev); |
| 1492 | struct charger_desc *desc = cm->desc; |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1493 | int i = 0; |
| 1494 | int j = 0; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1495 | |
| 1496 | /* Remove from the list */ |
| 1497 | mutex_lock(&cm_list_mtx); |
| 1498 | list_del(&cm->entry); |
| 1499 | mutex_unlock(&cm_list_mtx); |
| 1500 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1501 | if (work_pending(&setup_polling)) |
| 1502 | cancel_work_sync(&setup_polling); |
| 1503 | if (delayed_work_pending(&cm_monitor_work)) |
| 1504 | cancel_delayed_work_sync(&cm_monitor_work); |
| 1505 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1506 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
| 1507 | struct charger_regulator *charger |
| 1508 | = &desc->charger_regulators[i]; |
| 1509 | for (j = 0 ; j < charger->num_cables ; j++) { |
| 1510 | struct charger_cable *cable = &charger->cables[j]; |
| 1511 | extcon_unregister_interest(&cable->extcon_dev); |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | for (i = 0 ; i < desc->num_charger_regulators ; i++) |
| 1516 | regulator_put(desc->charger_regulators[i].consumer); |
| 1517 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1518 | power_supply_unregister(&cm->charger_psy); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1519 | |
| 1520 | try_charger_enable(cm, false); |
| 1521 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1522 | kfree(cm->charger_psy.properties); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1523 | kfree(cm->charger_stat); |
| 1524 | kfree(cm->desc); |
| 1525 | kfree(cm); |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
Axel Lin | 1bbe24d | 2012-01-11 17:19:45 +0800 | [diff] [blame] | 1530 | static const struct platform_device_id charger_manager_id[] = { |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1531 | { "charger-manager", 0 }, |
| 1532 | { }, |
| 1533 | }; |
Axel Lin | 1bbe24d | 2012-01-11 17:19:45 +0800 | [diff] [blame] | 1534 | MODULE_DEVICE_TABLE(platform, charger_manager_id); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1535 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1536 | static int cm_suspend_noirq(struct device *dev) |
| 1537 | { |
| 1538 | int ret = 0; |
| 1539 | |
| 1540 | if (device_may_wakeup(dev)) { |
| 1541 | device_set_wakeup_capable(dev, false); |
| 1542 | ret = -EAGAIN; |
| 1543 | } |
| 1544 | |
| 1545 | return ret; |
| 1546 | } |
| 1547 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1548 | static int cm_suspend_prepare(struct device *dev) |
| 1549 | { |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1550 | struct charger_manager *cm = dev_get_drvdata(dev); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1551 | |
| 1552 | if (!cm_suspended) { |
| 1553 | if (rtc_dev) { |
| 1554 | struct rtc_time tmp; |
| 1555 | unsigned long now; |
| 1556 | |
| 1557 | rtc_read_alarm(rtc_dev, &rtc_wkalarm_save); |
| 1558 | rtc_read_time(rtc_dev, &tmp); |
| 1559 | |
| 1560 | if (rtc_wkalarm_save.enabled) { |
| 1561 | rtc_tm_to_time(&rtc_wkalarm_save.time, |
| 1562 | &rtc_wkalarm_save_time); |
| 1563 | rtc_tm_to_time(&tmp, &now); |
| 1564 | if (now > rtc_wkalarm_save_time) |
| 1565 | rtc_wkalarm_save_time = 0; |
| 1566 | } else { |
| 1567 | rtc_wkalarm_save_time = 0; |
| 1568 | } |
| 1569 | } |
| 1570 | cm_suspended = true; |
| 1571 | } |
| 1572 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1573 | if (delayed_work_pending(&cm->fullbatt_vchk_work)) |
| 1574 | cancel_delayed_work(&cm->fullbatt_vchk_work); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1575 | cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm); |
| 1576 | cm->status_save_batt = is_batt_present(cm); |
| 1577 | |
| 1578 | if (!cm_rtc_set) { |
| 1579 | cm_suspend_duration_ms = 0; |
| 1580 | cm_rtc_set = cm_setup_timer(); |
| 1581 | } |
| 1582 | |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | static void cm_suspend_complete(struct device *dev) |
| 1587 | { |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1588 | struct charger_manager *cm = dev_get_drvdata(dev); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1589 | |
| 1590 | if (cm_suspended) { |
| 1591 | if (rtc_dev) { |
| 1592 | struct rtc_wkalrm tmp; |
| 1593 | |
| 1594 | rtc_read_alarm(rtc_dev, &tmp); |
| 1595 | rtc_wkalarm_save.pending = tmp.pending; |
| 1596 | rtc_set_alarm(rtc_dev, &rtc_wkalarm_save); |
| 1597 | } |
| 1598 | cm_suspended = false; |
| 1599 | cm_rtc_set = false; |
| 1600 | } |
| 1601 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1602 | /* Re-enqueue delayed work (fullbatt_vchk_work) */ |
| 1603 | if (cm->fullbatt_vchk_jiffies_at) { |
| 1604 | unsigned long delay = 0; |
| 1605 | unsigned long now = jiffies + CM_JIFFIES_SMALL; |
| 1606 | |
| 1607 | if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) { |
| 1608 | delay = (unsigned long)((long)now |
| 1609 | - (long)(cm->fullbatt_vchk_jiffies_at)); |
| 1610 | delay = jiffies_to_msecs(delay); |
| 1611 | } else { |
| 1612 | delay = 0; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Account for cm_suspend_duration_ms if |
| 1617 | * assume_timer_stops_in_suspend is active |
| 1618 | */ |
| 1619 | if (g_desc && g_desc->assume_timer_stops_in_suspend) { |
| 1620 | if (delay > cm_suspend_duration_ms) |
| 1621 | delay -= cm_suspend_duration_ms; |
| 1622 | else |
| 1623 | delay = 0; |
| 1624 | } |
| 1625 | |
| 1626 | queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work, |
| 1627 | msecs_to_jiffies(delay)); |
| 1628 | } |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1629 | device_set_wakeup_capable(cm->dev, false); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1630 | uevent_notify(cm, NULL); |
| 1631 | } |
| 1632 | |
| 1633 | static const struct dev_pm_ops charger_manager_pm = { |
| 1634 | .prepare = cm_suspend_prepare, |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1635 | .suspend_noirq = cm_suspend_noirq, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1636 | .complete = cm_suspend_complete, |
| 1637 | }; |
| 1638 | |
| 1639 | static struct platform_driver charger_manager_driver = { |
| 1640 | .driver = { |
| 1641 | .name = "charger-manager", |
| 1642 | .owner = THIS_MODULE, |
| 1643 | .pm = &charger_manager_pm, |
| 1644 | }, |
| 1645 | .probe = charger_manager_probe, |
| 1646 | .remove = __devexit_p(charger_manager_remove), |
| 1647 | .id_table = charger_manager_id, |
| 1648 | }; |
| 1649 | |
| 1650 | static int __init charger_manager_init(void) |
| 1651 | { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1652 | cm_wq = create_freezable_workqueue("charger_manager"); |
| 1653 | INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller); |
| 1654 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1655 | return platform_driver_register(&charger_manager_driver); |
| 1656 | } |
| 1657 | late_initcall(charger_manager_init); |
| 1658 | |
| 1659 | static void __exit charger_manager_cleanup(void) |
| 1660 | { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1661 | destroy_workqueue(cm_wq); |
| 1662 | cm_wq = NULL; |
| 1663 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1664 | platform_driver_unregister(&charger_manager_driver); |
| 1665 | } |
| 1666 | module_exit(charger_manager_cleanup); |
| 1667 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1668 | /** |
| 1669 | * find_power_supply - find the associated power_supply of charger |
| 1670 | * @cm: the Charger Manager representing the battery |
| 1671 | * @psy: pointer to instance of charger's power_supply |
| 1672 | */ |
| 1673 | static bool find_power_supply(struct charger_manager *cm, |
| 1674 | struct power_supply *psy) |
| 1675 | { |
| 1676 | int i; |
| 1677 | bool found = false; |
| 1678 | |
| 1679 | for (i = 0; cm->charger_stat[i]; i++) { |
| 1680 | if (psy == cm->charger_stat[i]) { |
| 1681 | found = true; |
| 1682 | break; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | return found; |
| 1687 | } |
| 1688 | |
| 1689 | /** |
| 1690 | * cm_notify_event - charger driver notify Charger Manager of charger event |
| 1691 | * @psy: pointer to instance of charger's power_supply |
| 1692 | * @type: type of charger event |
| 1693 | * @msg: optional message passed to uevent_notify fuction |
| 1694 | */ |
| 1695 | void cm_notify_event(struct power_supply *psy, enum cm_event_types type, |
| 1696 | char *msg) |
| 1697 | { |
| 1698 | struct charger_manager *cm; |
| 1699 | bool found_power_supply = false; |
| 1700 | |
| 1701 | if (psy == NULL) |
| 1702 | return; |
| 1703 | |
| 1704 | mutex_lock(&cm_list_mtx); |
| 1705 | list_for_each_entry(cm, &cm_list, entry) { |
| 1706 | found_power_supply = find_power_supply(cm, psy); |
| 1707 | if (found_power_supply) |
| 1708 | break; |
| 1709 | } |
| 1710 | mutex_unlock(&cm_list_mtx); |
| 1711 | |
| 1712 | if (!found_power_supply) |
| 1713 | return; |
| 1714 | |
| 1715 | switch (type) { |
| 1716 | case CM_EVENT_BATT_FULL: |
| 1717 | fullbatt_handler(cm); |
| 1718 | break; |
| 1719 | case CM_EVENT_BATT_OUT: |
| 1720 | battout_handler(cm); |
| 1721 | break; |
| 1722 | case CM_EVENT_BATT_IN: |
| 1723 | case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP: |
| 1724 | misc_event_handler(cm, type); |
| 1725 | break; |
| 1726 | case CM_EVENT_UNKNOWN: |
| 1727 | case CM_EVENT_OTHERS: |
| 1728 | uevent_notify(cm, msg ? msg : default_event_names[type]); |
| 1729 | break; |
| 1730 | default: |
| 1731 | dev_err(cm->dev, "%s type not specified.\n", __func__); |
| 1732 | break; |
| 1733 | } |
| 1734 | } |
| 1735 | EXPORT_SYMBOL_GPL(cm_notify_event); |
| 1736 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1737 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 1738 | MODULE_DESCRIPTION("Charger Manager"); |
| 1739 | MODULE_LICENSE("GPL"); |