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