2 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
3 * MyungJoo Ham <myungjoo.ham@samsung.com>
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.
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.
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>
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"
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
41 #define CM_JIFFIES_SMALL (2)
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))
47 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
48 * rtc alarm. It should be 2 or larger
50 #define CM_RTC_SMALL (2)
52 #define UEVENT_BUF_SIZE 32
54 static LIST_HEAD(cm_list);
55 static DEFINE_MUTEX(cm_list_mtx);
57 /* About in-suspend (suspend-again) monitoring */
58 static struct rtc_device *rtc_dev;
61 * Save the wakeup alarm before entering suspend-to-RAM
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;
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 */
76 /* Global charger-manager description */
77 static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
80 * is_batt_present - See if the battery presents in place.
81 * @cm: the Charger Manager representing the battery.
83 static bool is_batt_present(struct charger_manager *cm)
85 union power_supply_propval val;
89 switch (cm->desc->battery_present) {
90 case CM_BATTERY_PRESENT:
96 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
97 POWER_SUPPLY_PROP_PRESENT, &val);
98 if (ret == 0 && val.intval)
101 case CM_CHARGER_STAT:
102 for (i = 0; cm->charger_stat[i]; i++) {
103 ret = cm->charger_stat[i]->get_property(
105 POWER_SUPPLY_PROP_PRESENT, &val);
106 if (ret == 0 && val.intval) {
118 * is_ext_pwr_online - See if an external power source is attached to charge
119 * @cm: the Charger Manager representing the battery.
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.
125 static bool is_ext_pwr_online(struct charger_manager *cm)
127 union power_supply_propval val;
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(
135 POWER_SUPPLY_PROP_ONLINE, &val);
136 if (ret == 0 && val.intval) {
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.
150 * Returns 0 if there is no error.
151 * Returns a negative value on error.
153 static int get_batt_uV(struct charger_manager *cm, int *uV)
155 union power_supply_propval val;
161 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
162 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
171 * is_charging - Returns true if the battery is being charged.
172 * @cm: the Charger Manager representing the battery.
174 static bool is_charging(struct charger_manager *cm)
177 bool charging = false;
178 union power_supply_propval val;
180 /* If there is no battery, it cannot be charged */
181 if (!is_batt_present(cm))
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)
189 if (!cm->charger_enabled)
192 /* 2. The charger should be online (ext-power) */
193 ret = cm->charger_stat[i]->get_property(
195 POWER_SUPPLY_PROP_ONLINE, &val);
197 dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
198 cm->desc->psy_charger_stat[i]);
205 * 3. The charger should not be FULL, DISCHARGING,
208 ret = cm->charger_stat[i]->get_property(
210 POWER_SUPPLY_PROP_STATUS, &val);
212 dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
213 cm->desc->psy_charger_stat[i]);
216 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
217 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
218 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
221 /* Then, this is charging. */
230 * is_polling_required - Return true if need to continue polling for this CM.
231 * @cm: the Charger Manager representing the battery.
233 static bool is_polling_required(struct charger_manager *cm)
235 switch (cm->desc->polling_mode) {
236 case CM_POLL_DISABLE:
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);
245 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
246 cm->desc->polling_mode);
253 * try_charger_enable - Enable/Disable chargers altogether
254 * @cm: the Charger Manager representing the battery.
255 * @enable: true: enable / false: disable
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.
262 static int try_charger_enable(struct charger_manager *cm, bool enable)
265 struct charger_desc *desc = cm->desc;
267 /* Ignore if it's redundent command */
268 if (enable == cm->charger_enabled)
272 if (cm->emergency_stop)
274 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
275 err = regulator_enable(desc->charger_regulators[i].consumer);
278 "Cannot enable %s regulator\n",
279 desc->charger_regulators[i].regulator_name);
283 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
284 err = regulator_disable(desc->charger_regulators[i].consumer);
287 "Cannot disable %s regulator\n",
288 desc->charger_regulators[i].regulator_name);
293 * Abnormal battery state - Stop charging forcibly,
294 * even if charger was enabled at the other places
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);
302 "Disable regulator(%s) forcibly.\n",
303 desc->charger_regulators[i].regulator_name);
309 cm->charger_enabled = enable;
315 * try_charger_restart - Restart charging.
316 * @cm: the Charger Manager representing the battery.
318 * Restart charging by turning off and on the charger.
320 static int try_charger_restart(struct charger_manager *cm)
324 if (cm->emergency_stop)
327 err = try_charger_enable(cm, false);
331 return try_charger_enable(cm, true);
335 * uevent_notify - Let users know something has changed.
336 * @cm: the Charger Manager representing the battery.
337 * @event: the event string.
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.
346 static void uevent_notify(struct charger_manager *cm, const char *event)
348 static char env_str[UEVENT_BUF_SIZE + 1] = "";
349 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
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);
360 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
361 return; /* Duplicated. */
362 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
367 /* No messages pending */
368 if (!env_str_save[0])
371 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
372 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
378 /* status not changed */
379 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
382 /* save the status and notify the update */
383 strncpy(env_str, event, UEVENT_BUF_SIZE);
384 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
386 dev_info(cm->dev, event);
390 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
391 * @work: the work_struct appointing the function
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.
398 static void fullbatt_vchk(struct work_struct *work)
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;
406 /* remove the appointment for fullbatt_vchk */
407 cm->fullbatt_vchk_jiffies_at = 0;
409 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
412 err = get_batt_uV(cm, &batt_uV);
414 dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
418 diff = cm->fullbatt_vchk_uV;
421 dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
423 if (diff > desc->fullbatt_vchkdrop_uV) {
424 try_charger_restart(cm);
425 uevent_notify(cm, "Recharge");
430 * _cm_monitor - Monitor the temperature and return true for exceptions.
431 * @cm: the Charger Manager representing the battery.
433 * Returns true if there is an event to notify for the battery.
434 * (True if the status of "emergency_stop" changes)
436 static bool _cm_monitor(struct charger_manager *cm)
438 struct charger_desc *desc = cm->desc;
439 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
441 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
442 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
444 /* It has been stopped or charging already */
445 if (!!temp == !!cm->emergency_stop)
449 cm->emergency_stop = temp;
450 if (!try_charger_enable(cm, false)) {
452 uevent_notify(cm, "OVERHEAT");
454 uevent_notify(cm, "COLD");
457 cm->emergency_stop = 0;
458 if (!try_charger_enable(cm, true))
459 uevent_notify(cm, "CHARGING");
466 * cm_monitor - Monitor every battery.
468 * Returns true if there is an event to notify from any of the batteries.
469 * (True if the status of "emergency_stop" changes)
471 static bool cm_monitor(void)
474 struct charger_manager *cm;
476 mutex_lock(&cm_list_mtx);
478 list_for_each_entry(cm, &cm_list, entry) {
483 mutex_unlock(&cm_list_mtx);
489 * _setup_polling - Setup the next instance of polling.
490 * @work: work_struct of the function _setup_polling.
492 static void _setup_polling(struct work_struct *work)
494 unsigned long min = ULONG_MAX;
495 struct charger_manager *cm;
496 bool keep_polling = false;
497 unsigned long _next_polling;
499 mutex_lock(&cm_list_mtx);
501 list_for_each_entry(cm, &cm_list, entry) {
502 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
505 if (min > cm->desc->polling_interval_ms)
506 min = cm->desc->polling_interval_ms;
510 polling_jiffy = msecs_to_jiffies(min);
511 if (polling_jiffy <= CM_JIFFIES_SMALL)
512 polling_jiffy = CM_JIFFIES_SMALL + 1;
515 polling_jiffy = ULONG_MAX;
516 if (polling_jiffy == ULONG_MAX)
519 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
520 ". try it later. %s\n", __func__);
522 _next_polling = jiffies + polling_jiffy;
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);
533 mutex_unlock(&cm_list_mtx);
535 static DECLARE_WORK(setup_polling, _setup_polling);
538 * cm_monitor_poller - The Monitor / Poller.
539 * @work: work_struct of the function cm_monitor_poller
541 * During non-suspended state, cm_monitor_poller is used to poll and monitor
544 static void cm_monitor_poller(struct work_struct *work)
547 schedule_work(&setup_polling);
551 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
552 * @cm: the Charger Manager representing the battery.
554 static void fullbatt_handler(struct charger_manager *cm)
556 struct charger_desc *desc = cm->desc;
558 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
562 device_set_wakeup_capable(cm->dev, true);
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);
571 if (cm->fullbatt_vchk_jiffies_at == 0)
572 cm->fullbatt_vchk_jiffies_at = 1;
575 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
576 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
580 * battout_handler - Event handler for CM_EVENT_BATT_OUT
581 * @cm: the Charger Manager representing the battery.
583 static void battout_handler(struct charger_manager *cm)
586 device_set_wakeup_capable(cm->dev, true);
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]);
592 uevent_notify(cm, "Battery Reinserted?");
597 * misc_event_handler - Handler for other evnets
598 * @cm: the Charger Manager representing the battery.
599 * @type: the Charger Manager representing the battery.
601 static void misc_event_handler(struct charger_manager *cm,
602 enum cm_event_types type)
605 device_set_wakeup_capable(cm->dev, true);
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]);
613 static int charger_get_property(struct power_supply *psy,
614 enum power_supply_property psp,
615 union power_supply_propval *val)
617 struct charger_manager *cm = container_of(psy,
618 struct charger_manager, charger_psy);
619 struct charger_desc *desc = cm->desc;
624 case POWER_SUPPLY_PROP_STATUS:
626 val->intval = POWER_SUPPLY_STATUS_CHARGING;
627 else if (is_ext_pwr_online(cm))
628 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
630 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
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;
638 val->intval = POWER_SUPPLY_HEALTH_GOOD;
640 case POWER_SUPPLY_PROP_PRESENT:
641 if (is_batt_present(cm))
646 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
647 ret = get_batt_uV(cm, &val->intval);
649 case POWER_SUPPLY_PROP_CURRENT_NOW:
650 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
651 POWER_SUPPLY_PROP_CURRENT_NOW, val);
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)
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)
669 case POWER_SUPPLY_PROP_CAPACITY:
670 if (!cm->fuel_gauge) {
675 if (!is_batt_present(cm)) {
676 /* There is no battery. Assume 100% */
681 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
682 POWER_SUPPLY_PROP_CAPACITY, val);
686 if (val->intval > 100) {
693 /* Do not adjust SOC when charging: voltage is overrated */
698 * If the capacity value is inconsistent, calibrate it base on
699 * the battery voltage values and the thresholds given as desc
701 ret = get_batt_uV(cm, &uV);
703 /* Voltage information not available. No calibration */
708 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
715 case POWER_SUPPLY_PROP_ONLINE:
716 if (is_ext_pwr_online(cm))
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)
728 if (is_ext_pwr_online(cm)) {
729 /* Not full if it's charging. */
730 if (is_charging(cm)) {
735 * Full if it's powered but not charging andi
736 * not forced stop by emergency
738 if (!cm->emergency_stop) {
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 &&
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)) {
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,
774 /* If CHARGE_NOW is supplied, use it */
775 val->intval = (val->intval > 0) ?
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,
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,
807 static struct power_supply psy_default = {
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,
816 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
819 * Returns true if the alarm is set for Charger Manager to use.
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.
825 static bool cm_setup_timer(void)
827 struct charger_manager *cm;
828 unsigned int wakeup_ms = UINT_MAX;
831 mutex_lock(&cm_list_mtx);
833 list_for_each_entry(cm, &cm_list, entry) {
834 unsigned int fbchk_ms = 0;
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
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);
847 CM_MIN_VALID(wakeup_ms, fbchk_ms);
849 /* Skip if polling is not required for this CM */
850 if (!is_polling_required(cm) && !cm->emergency_stop)
852 if (cm->desc->polling_interval_ms == 0)
854 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
857 mutex_unlock(&cm_list_mtx);
859 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
860 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
862 struct rtc_wkalrm tmp;
863 unsigned long time, now;
864 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
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.
873 rtc_read_time(rtc_dev, &tmp.time);
874 rtc_tm_to_time(&tmp.time, &now);
875 if (add < CM_RTC_SMALL)
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;
887 time = rtc_wkalarm_save_time;
889 /* The timer is not appointed by CM */
893 pr_info("Waking up after %lu secs.\n",
896 rtc_time_to_tm(time, &tmp.time);
897 rtc_set_alarm(rtc_dev, &tmp);
898 cm_suspend_duration_ms += wakeup_ms;
904 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
908 static void _cm_fbchk_in_suspend(struct charger_manager *cm)
910 unsigned long jiffy_now = jiffies;
912 if (!cm->fullbatt_vchk_jiffies_at)
915 if (g_desc && g_desc->assume_timer_stops_in_suspend)
916 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
918 /* Execute now if it's going to be executed not too long after */
919 jiffy_now += CM_JIFFIES_SMALL;
921 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
922 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
926 * cm_suspend_again - Determine whether suspend again or not
928 * Returns true if the system should be suspended again
929 * Returns false if the system should be woken up
931 bool cm_suspend_again(void)
933 struct charger_manager *cm;
936 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
944 mutex_lock(&cm_list_mtx);
945 list_for_each_entry(cm, &cm_list, entry) {
946 _cm_fbchk_in_suspend(cm);
948 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
949 cm->status_save_batt != is_batt_present(cm)) {
954 mutex_unlock(&cm_list_mtx);
956 cm_rtc_set = cm_setup_timer();
958 /* It's about the time when the non-CM appointed timer goes off */
959 if (rtc_wkalarm_save.enabled) {
963 rtc_read_time(rtc_dev, &tmp);
964 rtc_tm_to_time(&tmp, &now);
966 if (rtc_wkalarm_save_time &&
967 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
972 EXPORT_SYMBOL_GPL(cm_suspend_again);
975 * setup_charger_manager - initialize charger_global_desc data
976 * @gd: pointer to instance of charger_global_desc
978 int setup_charger_manager(struct charger_global_desc *gd)
984 rtc_class_close(rtc_dev);
988 if (!gd->rtc_only_wakeup) {
989 pr_err("The callback rtc_only_wakeup is not given.\n");
994 rtc_dev = rtc_class_open(gd->rtc_name);
995 if (IS_ERR_OR_NULL(rtc_dev)) {
997 /* Retry at probe. RTC may be not registered yet */
1000 pr_warn("No wakeup timer is given for charger manager."
1001 "In-suspend monitoring won't work.\n");
1007 EXPORT_SYMBOL_GPL(setup_charger_manager);
1010 * charger_extcon_work - enable/diable charger according to the state
1013 * @work: work_struct of the function charger_extcon_work.
1015 static void charger_extcon_work(struct work_struct *work)
1017 struct charger_cable *cable =
1018 container_of(work, struct charger_cable, wq);
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);
1025 pr_err("Cannot set current limit of %s (%s)\n",
1026 cable->charger->regulator_name, cable->name);
1030 pr_info("Set current limit of %s : %duA ~ %duA\n",
1031 cable->charger->regulator_name,
1032 cable->min_uA, cable->max_uA);
1035 try_charger_enable(cable->cm, cable->attached);
1039 * charger_extcon_notifier - receive the state of charger cable
1040 * when registered cable is attached or detached.
1042 * @self: the notifier block of the charger_extcon_notifier.
1043 * @event: the cable state.
1044 * @ptr: the data pointer of notifier block.
1046 static int charger_extcon_notifier(struct notifier_block *self,
1047 unsigned long event, void *ptr)
1049 struct charger_cable *cable =
1050 container_of(self, struct charger_cable, nb);
1052 cable->attached = event;
1053 schedule_work(&cable->wq);
1059 * charger_extcon_init - register external connector to use it
1060 * as the charger cable
1062 * @cm: the Charger Manager representing the battery.
1063 * @cable: the Charger cable representing the external connector.
1065 static int charger_extcon_init(struct charger_manager *cm,
1066 struct charger_cable *cable)
1071 * Charger manager use Extcon framework to identify
1072 * the charger cable among various external connector
1073 * cable (e.g., TA, USB, MHL, Dock).
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);
1080 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1089 static int charger_manager_probe(struct platform_device *pdev)
1091 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1092 struct charger_manager *cm;
1095 union power_supply_propval val;
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)) {
1101 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1109 dev_err(&pdev->dev, "No platform data (desc) found.\n");
1114 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1116 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1121 /* Basic Values. Unspecified are Null or 0 */
1122 cm->dev = &pdev->dev;
1123 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1125 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1127 goto err_alloc_desc;
1129 memcpy(cm->desc, desc, sizeof(struct charger_desc));
1130 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1133 * The following two do not need to be errors.
1134 * Users may intentionally ignore those two features.
1136 if (desc->fullbatt_uV == 0) {
1137 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1138 " as it is not supplied.");
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;
1147 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1149 dev_err(&pdev->dev, "charger_regulators undefined.\n");
1150 goto err_no_charger;
1153 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1154 dev_err(&pdev->dev, "No power supply defined.\n");
1156 goto err_no_charger_stat;
1159 /* Counting index only */
1160 while (desc->psy_charger_stat[i])
1163 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1165 if (!cm->charger_stat) {
1167 goto err_no_charger_stat;
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 "
1176 desc->psy_charger_stat[i]);
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);
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");
1197 if (!desc->temperature_out_of_range) {
1198 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1203 platform_set_drvdata(pdev, cm);
1205 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1207 if (!desc->psy_name) {
1208 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
1210 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1212 cm->charger_psy.name = cm->psy_name_buf;
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),
1219 if (!cm->charger_psy.properties) {
1220 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
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;
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++;
1236 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1237 POWER_SUPPLY_PROP_CURRENT_NOW,
1239 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1240 POWER_SUPPLY_PROP_CURRENT_NOW;
1241 cm->charger_psy.num_properties++;
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++;
1249 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1250 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1251 cm->charger_psy.num_properties++;
1254 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1256 ret = power_supply_register(NULL, &cm->charger_psy);
1258 dev_err(&pdev->dev, "Cannot register charger-manager with"
1259 " name \"%s\".\n", cm->charger_psy.name);
1263 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1264 struct charger_regulator *charger
1265 = &desc->charger_regulators[i];
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);
1276 for (j = 0 ; j < charger->num_cables ; j++) {
1277 struct charger_cable *cable = &charger->cables[j];
1279 ret = charger_extcon_init(cm, cable);
1281 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1282 charger->regulator_name);
1285 cable->charger = charger;
1290 ret = try_charger_enable(cm, true);
1292 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
1293 goto err_chg_enable;
1296 /* Add to the list */
1297 mutex_lock(&cm_list_mtx);
1298 list_add(&cm->entry, &cm_list);
1299 mutex_unlock(&cm_list_mtx);
1302 * Charger-manager is capable of waking up the systme from sleep
1303 * when event is happend through cm_notify_event()
1305 device_init_wakeup(&pdev->dev, true);
1306 device_set_wakeup_capable(&pdev->dev, false);
1308 schedule_work(&setup_polling);
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);
1323 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1324 regulator_put(desc->charger_regulators[i].consumer);
1326 power_supply_unregister(&cm->charger_psy);
1328 kfree(cm->charger_psy.properties);
1330 kfree(cm->charger_stat);
1331 err_no_charger_stat:
1340 static int __devexit charger_manager_remove(struct platform_device *pdev)
1342 struct charger_manager *cm = platform_get_drvdata(pdev);
1343 struct charger_desc *desc = cm->desc;
1347 /* Remove from the list */
1348 mutex_lock(&cm_list_mtx);
1349 list_del(&cm->entry);
1350 mutex_unlock(&cm_list_mtx);
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);
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);
1366 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1367 regulator_put(desc->charger_regulators[i].consumer);
1369 power_supply_unregister(&cm->charger_psy);
1371 try_charger_enable(cm, false);
1373 kfree(cm->charger_psy.properties);
1374 kfree(cm->charger_stat);
1381 static const struct platform_device_id charger_manager_id[] = {
1382 { "charger-manager", 0 },
1385 MODULE_DEVICE_TABLE(platform, charger_manager_id);
1387 static int cm_suspend_noirq(struct device *dev)
1391 if (device_may_wakeup(dev)) {
1392 device_set_wakeup_capable(dev, false);
1399 static int cm_suspend_prepare(struct device *dev)
1401 struct charger_manager *cm = dev_get_drvdata(dev);
1403 if (!cm_suspended) {
1405 struct rtc_time tmp;
1408 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1409 rtc_read_time(rtc_dev, &tmp);
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;
1418 rtc_wkalarm_save_time = 0;
1421 cm_suspended = true;
1424 if (delayed_work_pending(&cm->fullbatt_vchk_work))
1425 cancel_delayed_work(&cm->fullbatt_vchk_work);
1426 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1427 cm->status_save_batt = is_batt_present(cm);
1430 cm_suspend_duration_ms = 0;
1431 cm_rtc_set = cm_setup_timer();
1437 static void cm_suspend_complete(struct device *dev)
1439 struct charger_manager *cm = dev_get_drvdata(dev);
1443 struct rtc_wkalrm tmp;
1445 rtc_read_alarm(rtc_dev, &tmp);
1446 rtc_wkalarm_save.pending = tmp.pending;
1447 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1449 cm_suspended = false;
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;
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);
1467 * Account for cm_suspend_duration_ms if
1468 * assume_timer_stops_in_suspend is active
1470 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1471 if (delay > cm_suspend_duration_ms)
1472 delay -= cm_suspend_duration_ms;
1477 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1478 msecs_to_jiffies(delay));
1480 device_set_wakeup_capable(cm->dev, false);
1481 uevent_notify(cm, NULL);
1484 static const struct dev_pm_ops charger_manager_pm = {
1485 .prepare = cm_suspend_prepare,
1486 .suspend_noirq = cm_suspend_noirq,
1487 .complete = cm_suspend_complete,
1490 static struct platform_driver charger_manager_driver = {
1492 .name = "charger-manager",
1493 .owner = THIS_MODULE,
1494 .pm = &charger_manager_pm,
1496 .probe = charger_manager_probe,
1497 .remove = __devexit_p(charger_manager_remove),
1498 .id_table = charger_manager_id,
1501 static int __init charger_manager_init(void)
1503 cm_wq = create_freezable_workqueue("charger_manager");
1504 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1506 return platform_driver_register(&charger_manager_driver);
1508 late_initcall(charger_manager_init);
1510 static void __exit charger_manager_cleanup(void)
1512 destroy_workqueue(cm_wq);
1515 platform_driver_unregister(&charger_manager_driver);
1517 module_exit(charger_manager_cleanup);
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
1524 static bool find_power_supply(struct charger_manager *cm,
1525 struct power_supply *psy)
1530 for (i = 0; cm->charger_stat[i]; i++) {
1531 if (psy == cm->charger_stat[i]) {
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
1546 void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1549 struct charger_manager *cm;
1550 bool found_power_supply = false;
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)
1561 mutex_unlock(&cm_list_mtx);
1563 if (!found_power_supply)
1567 case CM_EVENT_BATT_FULL:
1568 fullbatt_handler(cm);
1570 case CM_EVENT_BATT_OUT:
1571 battout_handler(cm);
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);
1577 case CM_EVENT_UNKNOWN:
1578 case CM_EVENT_OTHERS:
1579 uevent_notify(cm, msg ? msg : default_event_names[type]);
1582 dev_err(cm->dev, "%s type not specified.\n", __func__);
1586 EXPORT_SYMBOL_GPL(cm_notify_event);
1588 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1589 MODULE_DESCRIPTION("Charger Manager");
1590 MODULE_LICENSE("GPL");