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>
25 #include <linux/sysfs.h>
27 static const char * const default_event_names[] = {
28 [CM_EVENT_UNKNOWN] = "Unknown",
29 [CM_EVENT_BATT_FULL] = "Battery Full",
30 [CM_EVENT_BATT_IN] = "Battery Inserted",
31 [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
32 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
33 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
34 [CM_EVENT_OTHERS] = "Other battery events"
38 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
39 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
42 #define CM_JIFFIES_SMALL (2)
44 /* If y is valid (> 0) and smaller than x, do x = y */
45 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
48 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
49 * rtc alarm. It should be 2 or larger
51 #define CM_RTC_SMALL (2)
53 #define UEVENT_BUF_SIZE 32
55 static LIST_HEAD(cm_list);
56 static DEFINE_MUTEX(cm_list_mtx);
58 /* About in-suspend (suspend-again) monitoring */
59 static struct rtc_device *rtc_dev;
62 * Save the wakeup alarm before entering suspend-to-RAM
64 static struct rtc_wkalrm rtc_wkalarm_save;
65 /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
66 static unsigned long rtc_wkalarm_save_time;
67 static bool cm_suspended;
68 static bool cm_rtc_set;
69 static unsigned long cm_suspend_duration_ms;
71 /* About normal (not suspended) monitoring */
72 static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
73 static unsigned long next_polling; /* Next appointed polling time */
74 static struct workqueue_struct *cm_wq; /* init at driver add */
75 static struct delayed_work cm_monitor_work; /* init at driver add */
77 /* Global charger-manager description */
78 static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
81 * is_batt_present - See if the battery presents in place.
82 * @cm: the Charger Manager representing the battery.
84 static bool is_batt_present(struct charger_manager *cm)
86 union power_supply_propval val;
90 switch (cm->desc->battery_present) {
91 case CM_BATTERY_PRESENT:
97 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
98 POWER_SUPPLY_PROP_PRESENT, &val);
99 if (ret == 0 && val.intval)
102 case CM_CHARGER_STAT:
103 for (i = 0; cm->charger_stat[i]; i++) {
104 ret = cm->charger_stat[i]->get_property(
106 POWER_SUPPLY_PROP_PRESENT, &val);
107 if (ret == 0 && val.intval) {
119 * is_ext_pwr_online - See if an external power source is attached to charge
120 * @cm: the Charger Manager representing the battery.
122 * Returns true if at least one of the chargers of the battery has an external
123 * power source attached to charge the battery regardless of whether it is
124 * actually charging or not.
126 static bool is_ext_pwr_online(struct charger_manager *cm)
128 union power_supply_propval val;
132 /* If at least one of them has one, it's yes. */
133 for (i = 0; cm->charger_stat[i]; i++) {
134 ret = cm->charger_stat[i]->get_property(
136 POWER_SUPPLY_PROP_ONLINE, &val);
137 if (ret == 0 && val.intval) {
147 * get_batt_uV - Get the voltage level of the battery
148 * @cm: the Charger Manager representing the battery.
149 * @uV: the voltage level returned.
151 * Returns 0 if there is no error.
152 * Returns a negative value on error.
154 static int get_batt_uV(struct charger_manager *cm, int *uV)
156 union power_supply_propval val;
162 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
163 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
172 * is_charging - Returns true if the battery is being charged.
173 * @cm: the Charger Manager representing the battery.
175 static bool is_charging(struct charger_manager *cm)
178 bool charging = false;
179 union power_supply_propval val;
181 /* If there is no battery, it cannot be charged */
182 if (!is_batt_present(cm))
185 /* If at least one of the charger is charging, return yes */
186 for (i = 0; cm->charger_stat[i]; i++) {
187 /* 1. The charger sholuld not be DISABLED */
188 if (cm->emergency_stop)
190 if (!cm->charger_enabled)
193 /* 2. The charger should be online (ext-power) */
194 ret = cm->charger_stat[i]->get_property(
196 POWER_SUPPLY_PROP_ONLINE, &val);
198 dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
199 cm->desc->psy_charger_stat[i]);
206 * 3. The charger should not be FULL, DISCHARGING,
209 ret = cm->charger_stat[i]->get_property(
211 POWER_SUPPLY_PROP_STATUS, &val);
213 dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
214 cm->desc->psy_charger_stat[i]);
217 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
218 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
219 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
222 /* Then, this is charging. */
231 * is_full_charged - Returns true if the battery is fully charged.
232 * @cm: the Charger Manager representing the battery.
234 static bool is_full_charged(struct charger_manager *cm)
236 struct charger_desc *desc = cm->desc;
237 union power_supply_propval val;
241 /* If there is no battery, it cannot be charged */
242 if (!is_batt_present(cm))
245 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
248 /* Not full if capacity of fuel gauge isn't full */
249 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
250 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
251 if (!ret && val.intval > desc->fullbatt_full_capacity)
255 /* Full, if it's over the fullbatt voltage */
256 if (desc->fullbatt_uV > 0) {
257 ret = get_batt_uV(cm, &uV);
258 if (!ret && uV >= desc->fullbatt_uV)
262 /* Full, if the capacity is more than fullbatt_soc */
263 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
266 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
267 POWER_SUPPLY_PROP_CAPACITY, &val);
268 if (!ret && val.intval >= desc->fullbatt_soc)
276 * is_polling_required - Return true if need to continue polling for this CM.
277 * @cm: the Charger Manager representing the battery.
279 static bool is_polling_required(struct charger_manager *cm)
281 switch (cm->desc->polling_mode) {
282 case CM_POLL_DISABLE:
286 case CM_POLL_EXTERNAL_POWER_ONLY:
287 return is_ext_pwr_online(cm);
288 case CM_POLL_CHARGING_ONLY:
289 return is_charging(cm);
291 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
292 cm->desc->polling_mode);
299 * try_charger_enable - Enable/Disable chargers altogether
300 * @cm: the Charger Manager representing the battery.
301 * @enable: true: enable / false: disable
303 * Note that Charger Manager keeps the charger enabled regardless whether
304 * the charger is charging or not (because battery is full or no external
305 * power source exists) except when CM needs to disable chargers forcibly
306 * bacause of emergency causes; when the battery is overheated or too cold.
308 static int try_charger_enable(struct charger_manager *cm, bool enable)
311 struct charger_desc *desc = cm->desc;
313 /* Ignore if it's redundent command */
314 if (enable == cm->charger_enabled)
318 if (cm->emergency_stop)
322 * Save start time of charging to limit
323 * maximum possible charging time.
325 cm->charging_start_time = ktime_to_ms(ktime_get());
326 cm->charging_end_time = 0;
328 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
329 if (desc->charger_regulators[i].externally_control)
332 err = regulator_enable(desc->charger_regulators[i].consumer);
335 "Cannot enable %s regulator\n",
336 desc->charger_regulators[i].regulator_name);
341 * Save end time of charging to maintain fully charged state
342 * of battery after full-batt.
344 cm->charging_start_time = 0;
345 cm->charging_end_time = ktime_to_ms(ktime_get());
347 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
348 if (desc->charger_regulators[i].externally_control)
351 err = regulator_disable(desc->charger_regulators[i].consumer);
354 "Cannot disable %s regulator\n",
355 desc->charger_regulators[i].regulator_name);
360 * Abnormal battery state - Stop charging forcibly,
361 * even if charger was enabled at the other places
363 for (i = 0; i < desc->num_charger_regulators; i++) {
364 if (regulator_is_enabled(
365 desc->charger_regulators[i].consumer)) {
366 regulator_force_disable(
367 desc->charger_regulators[i].consumer);
369 "Disable regulator(%s) forcibly.\n",
370 desc->charger_regulators[i].regulator_name);
376 cm->charger_enabled = enable;
382 * try_charger_restart - Restart charging.
383 * @cm: the Charger Manager representing the battery.
385 * Restart charging by turning off and on the charger.
387 static int try_charger_restart(struct charger_manager *cm)
391 if (cm->emergency_stop)
394 err = try_charger_enable(cm, false);
398 return try_charger_enable(cm, true);
402 * uevent_notify - Let users know something has changed.
403 * @cm: the Charger Manager representing the battery.
404 * @event: the event string.
406 * If @event is null, it implies that uevent_notify is called
407 * by resume function. When called in the resume function, cm_suspended
408 * should be already reset to false in order to let uevent_notify
409 * notify the recent event during the suspend to users. While
410 * suspended, uevent_notify does not notify users, but tracks
411 * events so that uevent_notify can notify users later after resumed.
413 static void uevent_notify(struct charger_manager *cm, const char *event)
415 static char env_str[UEVENT_BUF_SIZE + 1] = "";
416 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
419 /* Nothing in suspended-event buffer */
420 if (env_str_save[0] == 0) {
421 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
422 return; /* status not changed */
423 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
427 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
428 return; /* Duplicated. */
429 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
434 /* No messages pending */
435 if (!env_str_save[0])
438 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
439 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
445 /* status not changed */
446 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
449 /* save the status and notify the update */
450 strncpy(env_str, event, UEVENT_BUF_SIZE);
451 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
453 dev_info(cm->dev, event);
457 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
458 * @work: the work_struct appointing the function
460 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
461 * charger_desc, Charger Manager checks voltage drop after the battery
462 * "FULL" event. It checks whether the voltage has dropped more than
463 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
465 static void fullbatt_vchk(struct work_struct *work)
467 struct delayed_work *dwork = to_delayed_work(work);
468 struct charger_manager *cm = container_of(dwork,
469 struct charger_manager, fullbatt_vchk_work);
470 struct charger_desc *desc = cm->desc;
471 int batt_uV, err, diff;
473 /* remove the appointment for fullbatt_vchk */
474 cm->fullbatt_vchk_jiffies_at = 0;
476 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
479 err = get_batt_uV(cm, &batt_uV);
481 dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
485 diff = desc->fullbatt_uV - batt_uV;
489 dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
491 if (diff > desc->fullbatt_vchkdrop_uV) {
492 try_charger_restart(cm);
493 uevent_notify(cm, "Recharging");
498 * check_charging_duration - Monitor charging/discharging duration
499 * @cm: the Charger Manager representing the battery.
501 * If whole charging duration exceed 'charging_max_duration_ms',
502 * cm stop charging to prevent overcharge/overheat. If discharging
503 * duration exceed 'discharging _max_duration_ms', charger cable is
504 * attached, after full-batt, cm start charging to maintain fully
505 * charged state for battery.
507 static int check_charging_duration(struct charger_manager *cm)
509 struct charger_desc *desc = cm->desc;
510 u64 curr = ktime_to_ms(ktime_get());
514 if (!desc->charging_max_duration_ms &&
515 !desc->discharging_max_duration_ms)
518 if (cm->charger_enabled) {
519 duration = curr - cm->charging_start_time;
521 if (duration > desc->charging_max_duration_ms) {
522 dev_info(cm->dev, "Charging duration exceed %lldms",
523 desc->charging_max_duration_ms);
524 uevent_notify(cm, "Discharging");
525 try_charger_enable(cm, false);
528 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
529 duration = curr - cm->charging_end_time;
531 if (duration > desc->charging_max_duration_ms &&
532 is_ext_pwr_online(cm)) {
533 dev_info(cm->dev, "DisCharging duration exceed %lldms",
534 desc->discharging_max_duration_ms);
535 uevent_notify(cm, "Recharing");
536 try_charger_enable(cm, true);
545 * _cm_monitor - Monitor the temperature and return true for exceptions.
546 * @cm: the Charger Manager representing the battery.
548 * Returns true if there is an event to notify for the battery.
549 * (True if the status of "emergency_stop" changes)
551 static bool _cm_monitor(struct charger_manager *cm)
553 struct charger_desc *desc = cm->desc;
554 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
556 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
557 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
559 /* It has been stopped already */
560 if (temp && cm->emergency_stop)
564 * Check temperature whether overheat or cold.
565 * If temperature is out of range normal state, stop charging.
568 cm->emergency_stop = temp;
569 if (!try_charger_enable(cm, false)) {
571 uevent_notify(cm, "OVERHEAT");
573 uevent_notify(cm, "COLD");
577 * Check whole charging duration and discharing duration
580 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
582 "Charging/Discharging duration is out of range");
584 * Check dropped voltage of battery. If battery voltage is more
585 * dropped than fullbatt_vchkdrop_uV after fully charged state,
586 * charger-manager have to recharge battery.
588 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
589 !cm->charger_enabled) {
590 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
593 * Check whether fully charged state to protect overcharge
594 * if charger-manager is charging for battery.
596 } else if (!cm->emergency_stop && is_full_charged(cm) &&
597 cm->charger_enabled) {
598 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
599 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
601 try_charger_enable(cm, false);
603 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
605 cm->emergency_stop = 0;
606 if (is_ext_pwr_online(cm)) {
607 if (!try_charger_enable(cm, true))
608 uevent_notify(cm, "CHARGING");
616 * cm_monitor - Monitor every battery.
618 * Returns true if there is an event to notify from any of the batteries.
619 * (True if the status of "emergency_stop" changes)
621 static bool cm_monitor(void)
624 struct charger_manager *cm;
626 mutex_lock(&cm_list_mtx);
628 list_for_each_entry(cm, &cm_list, entry) {
633 mutex_unlock(&cm_list_mtx);
639 * _setup_polling - Setup the next instance of polling.
640 * @work: work_struct of the function _setup_polling.
642 static void _setup_polling(struct work_struct *work)
644 unsigned long min = ULONG_MAX;
645 struct charger_manager *cm;
646 bool keep_polling = false;
647 unsigned long _next_polling;
649 mutex_lock(&cm_list_mtx);
651 list_for_each_entry(cm, &cm_list, entry) {
652 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
655 if (min > cm->desc->polling_interval_ms)
656 min = cm->desc->polling_interval_ms;
660 polling_jiffy = msecs_to_jiffies(min);
661 if (polling_jiffy <= CM_JIFFIES_SMALL)
662 polling_jiffy = CM_JIFFIES_SMALL + 1;
665 polling_jiffy = ULONG_MAX;
666 if (polling_jiffy == ULONG_MAX)
669 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
670 ". try it later. %s\n", __func__);
672 _next_polling = jiffies + polling_jiffy;
674 if (!delayed_work_pending(&cm_monitor_work) ||
675 (delayed_work_pending(&cm_monitor_work) &&
676 time_after(next_polling, _next_polling))) {
677 next_polling = jiffies + polling_jiffy;
678 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
682 mutex_unlock(&cm_list_mtx);
684 static DECLARE_WORK(setup_polling, _setup_polling);
687 * cm_monitor_poller - The Monitor / Poller.
688 * @work: work_struct of the function cm_monitor_poller
690 * During non-suspended state, cm_monitor_poller is used to poll and monitor
693 static void cm_monitor_poller(struct work_struct *work)
696 schedule_work(&setup_polling);
700 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
701 * @cm: the Charger Manager representing the battery.
703 static void fullbatt_handler(struct charger_manager *cm)
705 struct charger_desc *desc = cm->desc;
707 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
711 device_set_wakeup_capable(cm->dev, true);
713 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
714 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
715 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
716 desc->fullbatt_vchkdrop_ms);
718 if (cm->fullbatt_vchk_jiffies_at == 0)
719 cm->fullbatt_vchk_jiffies_at = 1;
722 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
723 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
727 * battout_handler - Event handler for CM_EVENT_BATT_OUT
728 * @cm: the Charger Manager representing the battery.
730 static void battout_handler(struct charger_manager *cm)
733 device_set_wakeup_capable(cm->dev, true);
735 if (!is_batt_present(cm)) {
736 dev_emerg(cm->dev, "Battery Pulled Out!\n");
737 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
739 uevent_notify(cm, "Battery Reinserted?");
744 * misc_event_handler - Handler for other evnets
745 * @cm: the Charger Manager representing the battery.
746 * @type: the Charger Manager representing the battery.
748 static void misc_event_handler(struct charger_manager *cm,
749 enum cm_event_types type)
752 device_set_wakeup_capable(cm->dev, true);
754 if (!delayed_work_pending(&cm_monitor_work) &&
755 is_polling_required(cm) && cm->desc->polling_interval_ms)
756 schedule_work(&setup_polling);
757 uevent_notify(cm, default_event_names[type]);
760 static int charger_get_property(struct power_supply *psy,
761 enum power_supply_property psp,
762 union power_supply_propval *val)
764 struct charger_manager *cm = container_of(psy,
765 struct charger_manager, charger_psy);
766 struct charger_desc *desc = cm->desc;
771 case POWER_SUPPLY_PROP_STATUS:
773 val->intval = POWER_SUPPLY_STATUS_CHARGING;
774 else if (is_ext_pwr_online(cm))
775 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
777 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
779 case POWER_SUPPLY_PROP_HEALTH:
780 if (cm->emergency_stop > 0)
781 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
782 else if (cm->emergency_stop < 0)
783 val->intval = POWER_SUPPLY_HEALTH_COLD;
785 val->intval = POWER_SUPPLY_HEALTH_GOOD;
787 case POWER_SUPPLY_PROP_PRESENT:
788 if (is_batt_present(cm))
793 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
794 ret = get_batt_uV(cm, &val->intval);
796 case POWER_SUPPLY_PROP_CURRENT_NOW:
797 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
798 POWER_SUPPLY_PROP_CURRENT_NOW, val);
800 case POWER_SUPPLY_PROP_TEMP:
801 /* in thenth of centigrade */
802 if (cm->last_temp_mC == INT_MIN)
803 desc->temperature_out_of_range(&cm->last_temp_mC);
804 val->intval = cm->last_temp_mC / 100;
805 if (!desc->measure_battery_temp)
808 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
809 /* in thenth of centigrade */
810 if (cm->last_temp_mC == INT_MIN)
811 desc->temperature_out_of_range(&cm->last_temp_mC);
812 val->intval = cm->last_temp_mC / 100;
813 if (desc->measure_battery_temp)
816 case POWER_SUPPLY_PROP_CAPACITY:
817 if (!cm->fuel_gauge) {
822 if (!is_batt_present(cm)) {
823 /* There is no battery. Assume 100% */
828 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
829 POWER_SUPPLY_PROP_CAPACITY, val);
833 if (val->intval > 100) {
840 /* Do not adjust SOC when charging: voltage is overrated */
845 * If the capacity value is inconsistent, calibrate it base on
846 * the battery voltage values and the thresholds given as desc
848 ret = get_batt_uV(cm, &uV);
850 /* Voltage information not available. No calibration */
855 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
862 case POWER_SUPPLY_PROP_ONLINE:
863 if (is_ext_pwr_online(cm))
868 case POWER_SUPPLY_PROP_CHARGE_FULL:
869 if (is_full_charged(cm))
875 case POWER_SUPPLY_PROP_CHARGE_NOW:
876 if (is_charging(cm)) {
877 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
878 POWER_SUPPLY_PROP_CHARGE_NOW,
884 /* If CHARGE_NOW is supplied, use it */
885 val->intval = (val->intval > 0) ?
898 #define NUM_CHARGER_PSY_OPTIONAL (4)
899 static enum power_supply_property default_charger_props[] = {
900 /* Guaranteed to provide */
901 POWER_SUPPLY_PROP_STATUS,
902 POWER_SUPPLY_PROP_HEALTH,
903 POWER_SUPPLY_PROP_PRESENT,
904 POWER_SUPPLY_PROP_VOLTAGE_NOW,
905 POWER_SUPPLY_PROP_CAPACITY,
906 POWER_SUPPLY_PROP_ONLINE,
907 POWER_SUPPLY_PROP_CHARGE_FULL,
909 * Optional properties are:
910 * POWER_SUPPLY_PROP_CHARGE_NOW,
911 * POWER_SUPPLY_PROP_CURRENT_NOW,
912 * POWER_SUPPLY_PROP_TEMP, and
913 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
917 static struct power_supply psy_default = {
919 .type = POWER_SUPPLY_TYPE_BATTERY,
920 .properties = default_charger_props,
921 .num_properties = ARRAY_SIZE(default_charger_props),
922 .get_property = charger_get_property,
926 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
929 * Returns true if the alarm is set for Charger Manager to use.
931 * cm_setup_timer fails to set an alarm,
932 * cm_setup_timer does not need to set an alarm for Charger Manager,
933 * or an alarm previously configured is to be used.
935 static bool cm_setup_timer(void)
937 struct charger_manager *cm;
938 unsigned int wakeup_ms = UINT_MAX;
941 mutex_lock(&cm_list_mtx);
943 list_for_each_entry(cm, &cm_list, entry) {
944 unsigned int fbchk_ms = 0;
946 /* fullbatt_vchk is required. setup timer for that */
947 if (cm->fullbatt_vchk_jiffies_at) {
948 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
950 if (time_is_before_eq_jiffies(
951 cm->fullbatt_vchk_jiffies_at) ||
952 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
953 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
957 CM_MIN_VALID(wakeup_ms, fbchk_ms);
959 /* Skip if polling is not required for this CM */
960 if (!is_polling_required(cm) && !cm->emergency_stop)
962 if (cm->desc->polling_interval_ms == 0)
964 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
967 mutex_unlock(&cm_list_mtx);
969 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
970 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
972 struct rtc_wkalrm tmp;
973 unsigned long time, now;
974 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
977 * Set alarm with the polling interval (wakeup_ms)
978 * except when rtc_wkalarm_save comes first.
979 * However, the alarm time should be NOW +
980 * CM_RTC_SMALL or later.
983 rtc_read_time(rtc_dev, &tmp.time);
984 rtc_tm_to_time(&tmp.time, &now);
985 if (add < CM_RTC_SMALL)
991 if (rtc_wkalarm_save.enabled &&
992 rtc_wkalarm_save_time &&
993 rtc_wkalarm_save_time < time) {
994 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
995 time = now + CM_RTC_SMALL;
997 time = rtc_wkalarm_save_time;
999 /* The timer is not appointed by CM */
1003 pr_info("Waking up after %lu secs.\n",
1006 rtc_time_to_tm(time, &tmp.time);
1007 rtc_set_alarm(rtc_dev, &tmp);
1008 cm_suspend_duration_ms += wakeup_ms;
1014 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1018 static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1020 unsigned long jiffy_now = jiffies;
1022 if (!cm->fullbatt_vchk_jiffies_at)
1025 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1026 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1028 /* Execute now if it's going to be executed not too long after */
1029 jiffy_now += CM_JIFFIES_SMALL;
1031 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1032 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1036 * cm_suspend_again - Determine whether suspend again or not
1038 * Returns true if the system should be suspended again
1039 * Returns false if the system should be woken up
1041 bool cm_suspend_again(void)
1043 struct charger_manager *cm;
1046 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1054 mutex_lock(&cm_list_mtx);
1055 list_for_each_entry(cm, &cm_list, entry) {
1056 _cm_fbchk_in_suspend(cm);
1058 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
1059 cm->status_save_batt != is_batt_present(cm)) {
1064 mutex_unlock(&cm_list_mtx);
1066 cm_rtc_set = cm_setup_timer();
1068 /* It's about the time when the non-CM appointed timer goes off */
1069 if (rtc_wkalarm_save.enabled) {
1071 struct rtc_time tmp;
1073 rtc_read_time(rtc_dev, &tmp);
1074 rtc_tm_to_time(&tmp, &now);
1076 if (rtc_wkalarm_save_time &&
1077 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1082 EXPORT_SYMBOL_GPL(cm_suspend_again);
1085 * setup_charger_manager - initialize charger_global_desc data
1086 * @gd: pointer to instance of charger_global_desc
1088 int setup_charger_manager(struct charger_global_desc *gd)
1094 rtc_class_close(rtc_dev);
1098 if (!gd->rtc_only_wakeup) {
1099 pr_err("The callback rtc_only_wakeup is not given.\n");
1104 rtc_dev = rtc_class_open(gd->rtc_name);
1105 if (IS_ERR_OR_NULL(rtc_dev)) {
1107 /* Retry at probe. RTC may be not registered yet */
1110 pr_warn("No wakeup timer is given for charger manager."
1111 "In-suspend monitoring won't work.\n");
1117 EXPORT_SYMBOL_GPL(setup_charger_manager);
1120 * charger_extcon_work - enable/diable charger according to the state
1123 * @work: work_struct of the function charger_extcon_work.
1125 static void charger_extcon_work(struct work_struct *work)
1127 struct charger_cable *cable =
1128 container_of(work, struct charger_cable, wq);
1131 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1132 ret = regulator_set_current_limit(cable->charger->consumer,
1133 cable->min_uA, cable->max_uA);
1135 pr_err("Cannot set current limit of %s (%s)\n",
1136 cable->charger->regulator_name, cable->name);
1140 pr_info("Set current limit of %s : %duA ~ %duA\n",
1141 cable->charger->regulator_name,
1142 cable->min_uA, cable->max_uA);
1145 try_charger_enable(cable->cm, cable->attached);
1149 * charger_extcon_notifier - receive the state of charger cable
1150 * when registered cable is attached or detached.
1152 * @self: the notifier block of the charger_extcon_notifier.
1153 * @event: the cable state.
1154 * @ptr: the data pointer of notifier block.
1156 static int charger_extcon_notifier(struct notifier_block *self,
1157 unsigned long event, void *ptr)
1159 struct charger_cable *cable =
1160 container_of(self, struct charger_cable, nb);
1163 * The newly state of charger cable.
1164 * If cable is attached, cable->attached is true.
1166 cable->attached = event;
1169 * Setup monitoring to check battery state
1170 * when charger cable is attached.
1172 if (cable->attached && is_polling_required(cable->cm)) {
1173 if (work_pending(&setup_polling))
1174 cancel_work_sync(&setup_polling);
1175 schedule_work(&setup_polling);
1179 * Setup work for controlling charger(regulator)
1180 * according to charger cable.
1182 schedule_work(&cable->wq);
1188 * charger_extcon_init - register external connector to use it
1189 * as the charger cable
1191 * @cm: the Charger Manager representing the battery.
1192 * @cable: the Charger cable representing the external connector.
1194 static int charger_extcon_init(struct charger_manager *cm,
1195 struct charger_cable *cable)
1200 * Charger manager use Extcon framework to identify
1201 * the charger cable among various external connector
1202 * cable (e.g., TA, USB, MHL, Dock).
1204 INIT_WORK(&cable->wq, charger_extcon_work);
1205 cable->nb.notifier_call = charger_extcon_notifier;
1206 ret = extcon_register_interest(&cable->extcon_dev,
1207 cable->extcon_name, cable->name, &cable->nb);
1209 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1218 /* help function of sysfs node to control charger(regulator) */
1219 static ssize_t charger_name_show(struct device *dev,
1220 struct device_attribute *attr, char *buf)
1222 struct charger_regulator *charger
1223 = container_of(attr, struct charger_regulator, attr_name);
1225 return sprintf(buf, "%s\n", charger->regulator_name);
1228 static ssize_t charger_state_show(struct device *dev,
1229 struct device_attribute *attr, char *buf)
1231 struct charger_regulator *charger
1232 = container_of(attr, struct charger_regulator, attr_state);
1235 if (!charger->externally_control)
1236 state = regulator_is_enabled(charger->consumer);
1238 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1241 static ssize_t charger_externally_control_show(struct device *dev,
1242 struct device_attribute *attr, char *buf)
1244 struct charger_regulator *charger = container_of(attr,
1245 struct charger_regulator, attr_externally_control);
1247 return sprintf(buf, "%d\n", charger->externally_control);
1250 static ssize_t charger_externally_control_store(struct device *dev,
1251 struct device_attribute *attr, const char *buf,
1254 struct charger_regulator *charger
1255 = container_of(attr, struct charger_regulator,
1256 attr_externally_control);
1257 struct charger_manager *cm = charger->cm;
1258 struct charger_desc *desc = cm->desc;
1261 int externally_control;
1262 int chargers_externally_control = 1;
1264 ret = sscanf(buf, "%d", &externally_control);
1270 if (!externally_control) {
1271 charger->externally_control = 0;
1275 for (i = 0; i < desc->num_charger_regulators; i++) {
1276 if (&desc->charger_regulators[i] != charger &&
1277 !desc->charger_regulators[i].externally_control) {
1279 * At least, one charger is controlled by
1282 chargers_externally_control = 0;
1287 if (!chargers_externally_control) {
1288 if (cm->charger_enabled) {
1289 try_charger_enable(charger->cm, false);
1290 charger->externally_control = externally_control;
1291 try_charger_enable(charger->cm, true);
1293 charger->externally_control = externally_control;
1297 "'%s' regulator should be controlled "
1298 "in charger-manager because charger-manager "
1299 "must need at least one charger for charging\n",
1300 charger->regulator_name);
1306 static int charger_manager_probe(struct platform_device *pdev)
1308 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1309 struct charger_manager *cm;
1312 int chargers_externally_control = 1;
1313 union power_supply_propval val;
1315 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1316 rtc_dev = rtc_class_open(g_desc->rtc_name);
1317 if (IS_ERR_OR_NULL(rtc_dev)) {
1319 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1327 dev_err(&pdev->dev, "No platform data (desc) found.\n");
1332 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1334 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1339 /* Basic Values. Unspecified are Null or 0 */
1340 cm->dev = &pdev->dev;
1341 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1343 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1345 goto err_alloc_desc;
1347 memcpy(cm->desc, desc, sizeof(struct charger_desc));
1348 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1351 * The following two do not need to be errors.
1352 * Users may intentionally ignore those two features.
1354 if (desc->fullbatt_uV == 0) {
1355 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1356 " as it is not supplied.");
1358 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1359 dev_info(&pdev->dev, "Disabling full-battery voltage drop "
1360 "checking mechanism as it is not supplied.");
1361 desc->fullbatt_vchkdrop_ms = 0;
1362 desc->fullbatt_vchkdrop_uV = 0;
1364 if (desc->fullbatt_soc == 0) {
1365 dev_info(&pdev->dev, "Ignoring full-battery soc(state of"
1366 " charge) threshold as it is not"
1369 if (desc->fullbatt_full_capacity == 0) {
1370 dev_info(&pdev->dev, "Ignoring full-battery full capacity"
1371 " threshold as it is not supplied.");
1374 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1376 dev_err(&pdev->dev, "charger_regulators undefined.\n");
1377 goto err_no_charger;
1380 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1381 dev_err(&pdev->dev, "No power supply defined.\n");
1383 goto err_no_charger_stat;
1386 /* Counting index only */
1387 while (desc->psy_charger_stat[i])
1390 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1392 if (!cm->charger_stat) {
1394 goto err_no_charger_stat;
1397 for (i = 0; desc->psy_charger_stat[i]; i++) {
1398 cm->charger_stat[i] = power_supply_get_by_name(
1399 desc->psy_charger_stat[i]);
1400 if (!cm->charger_stat[i]) {
1401 dev_err(&pdev->dev, "Cannot find power supply "
1403 desc->psy_charger_stat[i]);
1409 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1410 if (!cm->fuel_gauge) {
1411 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1412 desc->psy_fuel_gauge);
1417 if (desc->polling_interval_ms == 0 ||
1418 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1419 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1424 if (!desc->temperature_out_of_range) {
1425 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1430 if (!desc->charging_max_duration_ms ||
1431 !desc->discharging_max_duration_ms) {
1432 dev_info(&pdev->dev, "Cannot limit charging duration "
1433 "checking mechanism to prevent overcharge/overheat "
1434 "and control discharging duration");
1435 desc->charging_max_duration_ms = 0;
1436 desc->discharging_max_duration_ms = 0;
1439 platform_set_drvdata(pdev, cm);
1441 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1443 if (!desc->psy_name) {
1444 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
1446 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1448 cm->charger_psy.name = cm->psy_name_buf;
1450 /* Allocate for psy properties because they may vary */
1451 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1452 * (ARRAY_SIZE(default_charger_props) +
1453 NUM_CHARGER_PSY_OPTIONAL),
1455 if (!cm->charger_psy.properties) {
1456 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
1460 memcpy(cm->charger_psy.properties, default_charger_props,
1461 sizeof(enum power_supply_property) *
1462 ARRAY_SIZE(default_charger_props));
1463 cm->charger_psy.num_properties = psy_default.num_properties;
1465 /* Find which optional psy-properties are available */
1466 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1467 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1468 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1469 POWER_SUPPLY_PROP_CHARGE_NOW;
1470 cm->charger_psy.num_properties++;
1472 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1473 POWER_SUPPLY_PROP_CURRENT_NOW,
1475 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1476 POWER_SUPPLY_PROP_CURRENT_NOW;
1477 cm->charger_psy.num_properties++;
1480 if (desc->measure_battery_temp) {
1481 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1482 POWER_SUPPLY_PROP_TEMP;
1483 cm->charger_psy.num_properties++;
1485 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1486 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1487 cm->charger_psy.num_properties++;
1490 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1492 ret = power_supply_register(NULL, &cm->charger_psy);
1494 dev_err(&pdev->dev, "Cannot register charger-manager with"
1495 " name \"%s\".\n", cm->charger_psy.name);
1499 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1500 struct charger_regulator *charger
1501 = &desc->charger_regulators[i];
1505 charger->consumer = regulator_get(&pdev->dev,
1506 charger->regulator_name);
1507 if (charger->consumer == NULL) {
1508 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1509 charger->regulator_name);
1515 for (j = 0 ; j < charger->num_cables ; j++) {
1516 struct charger_cable *cable = &charger->cables[j];
1518 ret = charger_extcon_init(cm, cable);
1520 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1521 charger->regulator_name);
1524 cable->charger = charger;
1528 /* Create sysfs entry to control charger(regulator) */
1529 snprintf(buf, 10, "charger.%d", i);
1530 str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
1532 for (i--; i >= 0; i--) {
1533 charger = &desc->charger_regulators[i];
1534 kfree(charger->attr_g.name);
1542 charger->attrs[0] = &charger->attr_name.attr;
1543 charger->attrs[1] = &charger->attr_state.attr;
1544 charger->attrs[2] = &charger->attr_externally_control.attr;
1545 charger->attrs[3] = NULL;
1546 charger->attr_g.name = str;
1547 charger->attr_g.attrs = charger->attrs;
1549 sysfs_attr_init(&charger->attr_name.attr);
1550 charger->attr_name.attr.name = "name";
1551 charger->attr_name.attr.mode = 0444;
1552 charger->attr_name.show = charger_name_show;
1554 sysfs_attr_init(&charger->attr_state.attr);
1555 charger->attr_state.attr.name = "state";
1556 charger->attr_state.attr.mode = 0444;
1557 charger->attr_state.show = charger_state_show;
1559 sysfs_attr_init(&charger->attr_externally_control.attr);
1560 charger->attr_externally_control.attr.name
1561 = "externally_control";
1562 charger->attr_externally_control.attr.mode = 0644;
1563 charger->attr_externally_control.show
1564 = charger_externally_control_show;
1565 charger->attr_externally_control.store
1566 = charger_externally_control_store;
1568 if (!desc->charger_regulators[i].externally_control ||
1569 !chargers_externally_control) {
1570 chargers_externally_control = 0;
1572 dev_info(&pdev->dev, "'%s' regulator's externally_control"
1573 "is %d\n", charger->regulator_name,
1574 charger->externally_control);
1576 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1579 dev_info(&pdev->dev, "Cannot create sysfs entry"
1580 "of %s regulator\n",
1581 charger->regulator_name);
1585 if (chargers_externally_control) {
1586 dev_err(&pdev->dev, "Cannot register regulator because "
1587 "charger-manager must need at least "
1588 "one charger for charging battery\n");
1591 goto err_chg_enable;
1594 ret = try_charger_enable(cm, true);
1596 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
1597 goto err_chg_enable;
1600 /* Add to the list */
1601 mutex_lock(&cm_list_mtx);
1602 list_add(&cm->entry, &cm_list);
1603 mutex_unlock(&cm_list_mtx);
1606 * Charger-manager is capable of waking up the systme from sleep
1607 * when event is happend through cm_notify_event()
1609 device_init_wakeup(&pdev->dev, true);
1610 device_set_wakeup_capable(&pdev->dev, false);
1612 schedule_work(&setup_polling);
1617 for (i = 0; i < desc->num_charger_regulators; i++) {
1618 struct charger_regulator *charger;
1620 charger = &desc->charger_regulators[i];
1621 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1623 kfree(charger->attr_g.name);
1626 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1627 struct charger_regulator *charger
1628 = &desc->charger_regulators[i];
1629 for (j = 0 ; j < charger->num_cables ; j++) {
1630 struct charger_cable *cable = &charger->cables[j];
1631 extcon_unregister_interest(&cable->extcon_dev);
1635 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1636 regulator_put(desc->charger_regulators[i].consumer);
1638 power_supply_unregister(&cm->charger_psy);
1640 kfree(cm->charger_psy.properties);
1642 kfree(cm->charger_stat);
1643 err_no_charger_stat:
1652 static int charger_manager_remove(struct platform_device *pdev)
1654 struct charger_manager *cm = platform_get_drvdata(pdev);
1655 struct charger_desc *desc = cm->desc;
1659 /* Remove from the list */
1660 mutex_lock(&cm_list_mtx);
1661 list_del(&cm->entry);
1662 mutex_unlock(&cm_list_mtx);
1664 if (work_pending(&setup_polling))
1665 cancel_work_sync(&setup_polling);
1666 if (delayed_work_pending(&cm_monitor_work))
1667 cancel_delayed_work_sync(&cm_monitor_work);
1669 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1670 struct charger_regulator *charger
1671 = &desc->charger_regulators[i];
1672 for (j = 0 ; j < charger->num_cables ; j++) {
1673 struct charger_cable *cable = &charger->cables[j];
1674 extcon_unregister_interest(&cable->extcon_dev);
1678 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1679 regulator_put(desc->charger_regulators[i].consumer);
1681 power_supply_unregister(&cm->charger_psy);
1683 try_charger_enable(cm, false);
1685 kfree(cm->charger_psy.properties);
1686 kfree(cm->charger_stat);
1693 static const struct platform_device_id charger_manager_id[] = {
1694 { "charger-manager", 0 },
1697 MODULE_DEVICE_TABLE(platform, charger_manager_id);
1699 static int cm_suspend_noirq(struct device *dev)
1703 if (device_may_wakeup(dev)) {
1704 device_set_wakeup_capable(dev, false);
1711 static int cm_suspend_prepare(struct device *dev)
1713 struct charger_manager *cm = dev_get_drvdata(dev);
1715 if (!cm_suspended) {
1717 struct rtc_time tmp;
1720 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1721 rtc_read_time(rtc_dev, &tmp);
1723 if (rtc_wkalarm_save.enabled) {
1724 rtc_tm_to_time(&rtc_wkalarm_save.time,
1725 &rtc_wkalarm_save_time);
1726 rtc_tm_to_time(&tmp, &now);
1727 if (now > rtc_wkalarm_save_time)
1728 rtc_wkalarm_save_time = 0;
1730 rtc_wkalarm_save_time = 0;
1733 cm_suspended = true;
1736 if (delayed_work_pending(&cm->fullbatt_vchk_work))
1737 cancel_delayed_work(&cm->fullbatt_vchk_work);
1738 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1739 cm->status_save_batt = is_batt_present(cm);
1742 cm_suspend_duration_ms = 0;
1743 cm_rtc_set = cm_setup_timer();
1749 static void cm_suspend_complete(struct device *dev)
1751 struct charger_manager *cm = dev_get_drvdata(dev);
1755 struct rtc_wkalrm tmp;
1757 rtc_read_alarm(rtc_dev, &tmp);
1758 rtc_wkalarm_save.pending = tmp.pending;
1759 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1761 cm_suspended = false;
1765 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1766 if (cm->fullbatt_vchk_jiffies_at) {
1767 unsigned long delay = 0;
1768 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1770 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1771 delay = (unsigned long)((long)now
1772 - (long)(cm->fullbatt_vchk_jiffies_at));
1773 delay = jiffies_to_msecs(delay);
1779 * Account for cm_suspend_duration_ms if
1780 * assume_timer_stops_in_suspend is active
1782 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1783 if (delay > cm_suspend_duration_ms)
1784 delay -= cm_suspend_duration_ms;
1789 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1790 msecs_to_jiffies(delay));
1792 device_set_wakeup_capable(cm->dev, false);
1793 uevent_notify(cm, NULL);
1796 static const struct dev_pm_ops charger_manager_pm = {
1797 .prepare = cm_suspend_prepare,
1798 .suspend_noirq = cm_suspend_noirq,
1799 .complete = cm_suspend_complete,
1802 static struct platform_driver charger_manager_driver = {
1804 .name = "charger-manager",
1805 .owner = THIS_MODULE,
1806 .pm = &charger_manager_pm,
1808 .probe = charger_manager_probe,
1809 .remove = charger_manager_remove,
1810 .id_table = charger_manager_id,
1813 static int __init charger_manager_init(void)
1815 cm_wq = create_freezable_workqueue("charger_manager");
1816 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1818 return platform_driver_register(&charger_manager_driver);
1820 late_initcall(charger_manager_init);
1822 static void __exit charger_manager_cleanup(void)
1824 destroy_workqueue(cm_wq);
1827 platform_driver_unregister(&charger_manager_driver);
1829 module_exit(charger_manager_cleanup);
1832 * find_power_supply - find the associated power_supply of charger
1833 * @cm: the Charger Manager representing the battery
1834 * @psy: pointer to instance of charger's power_supply
1836 static bool find_power_supply(struct charger_manager *cm,
1837 struct power_supply *psy)
1842 for (i = 0; cm->charger_stat[i]; i++) {
1843 if (psy == cm->charger_stat[i]) {
1853 * cm_notify_event - charger driver notify Charger Manager of charger event
1854 * @psy: pointer to instance of charger's power_supply
1855 * @type: type of charger event
1856 * @msg: optional message passed to uevent_notify fuction
1858 void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1861 struct charger_manager *cm;
1862 bool found_power_supply = false;
1867 mutex_lock(&cm_list_mtx);
1868 list_for_each_entry(cm, &cm_list, entry) {
1869 found_power_supply = find_power_supply(cm, psy);
1870 if (found_power_supply)
1873 mutex_unlock(&cm_list_mtx);
1875 if (!found_power_supply)
1879 case CM_EVENT_BATT_FULL:
1880 fullbatt_handler(cm);
1882 case CM_EVENT_BATT_OUT:
1883 battout_handler(cm);
1885 case CM_EVENT_BATT_IN:
1886 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1887 misc_event_handler(cm, type);
1889 case CM_EVENT_UNKNOWN:
1890 case CM_EVENT_OTHERS:
1891 uevent_notify(cm, msg ? msg : default_event_names[type]);
1894 dev_err(cm->dev, "%s type not specified.\n", __func__);
1898 EXPORT_SYMBOL_GPL(cm_notify_event);
1900 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1901 MODULE_DESCRIPTION("Charger Manager");
1902 MODULE_LICENSE("GPL");