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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/rtc.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <linux/platform_device.h>
25 #include <linux/power/charger-manager.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/sysfs.h>
29 static const char * const default_event_names[] = {
30 [CM_EVENT_UNKNOWN] = "Unknown",
31 [CM_EVENT_BATT_FULL] = "Battery Full",
32 [CM_EVENT_BATT_IN] = "Battery Inserted",
33 [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
34 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
35 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
36 [CM_EVENT_OTHERS] = "Other battery events"
40 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
41 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
44 #define CM_JIFFIES_SMALL (2)
46 /* If y is valid (> 0) and smaller than x, do x = y */
47 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
50 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
51 * rtc alarm. It should be 2 or larger
53 #define CM_RTC_SMALL (2)
55 #define UEVENT_BUF_SIZE 32
57 static LIST_HEAD(cm_list);
58 static DEFINE_MUTEX(cm_list_mtx);
60 /* About in-suspend (suspend-again) monitoring */
61 static struct rtc_device *rtc_dev;
64 * Save the wakeup alarm before entering suspend-to-RAM
66 static struct rtc_wkalrm rtc_wkalarm_save;
67 /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
68 static unsigned long rtc_wkalarm_save_time;
69 static bool cm_suspended;
70 static bool cm_rtc_set;
71 static unsigned long cm_suspend_duration_ms;
73 /* About normal (not suspended) monitoring */
74 static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
75 static unsigned long next_polling; /* Next appointed polling time */
76 static struct workqueue_struct *cm_wq; /* init at driver add */
77 static struct delayed_work cm_monitor_work; /* init at driver add */
79 /* Global charger-manager description */
80 static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
83 * is_batt_present - See if the battery presents in place.
84 * @cm: the Charger Manager representing the battery.
86 static bool is_batt_present(struct charger_manager *cm)
88 union power_supply_propval val;
92 switch (cm->desc->battery_present) {
93 case CM_BATTERY_PRESENT:
99 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
100 POWER_SUPPLY_PROP_PRESENT, &val);
101 if (ret == 0 && val.intval)
104 case CM_CHARGER_STAT:
105 for (i = 0; cm->charger_stat[i]; i++) {
106 ret = cm->charger_stat[i]->get_property(
108 POWER_SUPPLY_PROP_PRESENT, &val);
109 if (ret == 0 && val.intval) {
121 * is_ext_pwr_online - See if an external power source is attached to charge
122 * @cm: the Charger Manager representing the battery.
124 * Returns true if at least one of the chargers of the battery has an external
125 * power source attached to charge the battery regardless of whether it is
126 * actually charging or not.
128 static bool is_ext_pwr_online(struct charger_manager *cm)
130 union power_supply_propval val;
134 /* If at least one of them has one, it's yes. */
135 for (i = 0; cm->charger_stat[i]; i++) {
136 ret = cm->charger_stat[i]->get_property(
138 POWER_SUPPLY_PROP_ONLINE, &val);
139 if (ret == 0 && val.intval) {
149 * get_batt_uV - Get the voltage level of the battery
150 * @cm: the Charger Manager representing the battery.
151 * @uV: the voltage level returned.
153 * Returns 0 if there is no error.
154 * Returns a negative value on error.
156 static int get_batt_uV(struct charger_manager *cm, int *uV)
158 union power_supply_propval val;
164 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
165 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
174 * is_charging - Returns true if the battery is being charged.
175 * @cm: the Charger Manager representing the battery.
177 static bool is_charging(struct charger_manager *cm)
180 bool charging = false;
181 union power_supply_propval val;
183 /* If there is no battery, it cannot be charged */
184 if (!is_batt_present(cm))
187 /* If at least one of the charger is charging, return yes */
188 for (i = 0; cm->charger_stat[i]; i++) {
189 /* 1. The charger sholuld not be DISABLED */
190 if (cm->emergency_stop)
192 if (!cm->charger_enabled)
195 /* 2. The charger should be online (ext-power) */
196 ret = cm->charger_stat[i]->get_property(
198 POWER_SUPPLY_PROP_ONLINE, &val);
200 dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
201 cm->desc->psy_charger_stat[i]);
208 * 3. The charger should not be FULL, DISCHARGING,
211 ret = cm->charger_stat[i]->get_property(
213 POWER_SUPPLY_PROP_STATUS, &val);
215 dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
216 cm->desc->psy_charger_stat[i]);
219 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
220 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
221 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
224 /* Then, this is charging. */
233 * is_full_charged - Returns true if the battery is fully charged.
234 * @cm: the Charger Manager representing the battery.
236 static bool is_full_charged(struct charger_manager *cm)
238 struct charger_desc *desc = cm->desc;
239 union power_supply_propval val;
243 /* If there is no battery, it cannot be charged */
244 if (!is_batt_present(cm))
247 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
250 /* Not full if capacity of fuel gauge isn't full */
251 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
252 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
253 if (!ret && val.intval > desc->fullbatt_full_capacity)
257 /* Full, if it's over the fullbatt voltage */
258 if (desc->fullbatt_uV > 0) {
259 ret = get_batt_uV(cm, &uV);
260 if (!ret && uV >= desc->fullbatt_uV)
264 /* Full, if the capacity is more than fullbatt_soc */
265 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
268 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
269 POWER_SUPPLY_PROP_CAPACITY, &val);
270 if (!ret && val.intval >= desc->fullbatt_soc)
278 * is_polling_required - Return true if need to continue polling for this CM.
279 * @cm: the Charger Manager representing the battery.
281 static bool is_polling_required(struct charger_manager *cm)
283 switch (cm->desc->polling_mode) {
284 case CM_POLL_DISABLE:
288 case CM_POLL_EXTERNAL_POWER_ONLY:
289 return is_ext_pwr_online(cm);
290 case CM_POLL_CHARGING_ONLY:
291 return is_charging(cm);
293 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
294 cm->desc->polling_mode);
301 * try_charger_enable - Enable/Disable chargers altogether
302 * @cm: the Charger Manager representing the battery.
303 * @enable: true: enable / false: disable
305 * Note that Charger Manager keeps the charger enabled regardless whether
306 * the charger is charging or not (because battery is full or no external
307 * power source exists) except when CM needs to disable chargers forcibly
308 * bacause of emergency causes; when the battery is overheated or too cold.
310 static int try_charger_enable(struct charger_manager *cm, bool enable)
313 struct charger_desc *desc = cm->desc;
315 /* Ignore if it's redundent command */
316 if (enable == cm->charger_enabled)
320 if (cm->emergency_stop)
324 * Save start time of charging to limit
325 * maximum possible charging time.
327 cm->charging_start_time = ktime_to_ms(ktime_get());
328 cm->charging_end_time = 0;
330 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
331 if (desc->charger_regulators[i].externally_control)
334 err = regulator_enable(desc->charger_regulators[i].consumer);
336 dev_warn(cm->dev, "Cannot enable %s regulator\n",
337 desc->charger_regulators[i].regulator_name);
342 * Save end time of charging to maintain fully charged state
343 * of battery after full-batt.
345 cm->charging_start_time = 0;
346 cm->charging_end_time = ktime_to_ms(ktime_get());
348 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
349 if (desc->charger_regulators[i].externally_control)
352 err = regulator_disable(desc->charger_regulators[i].consumer);
354 dev_warn(cm->dev, "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);
368 dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
369 desc->charger_regulators[i].regulator_name);
375 cm->charger_enabled = enable;
381 * try_charger_restart - Restart charging.
382 * @cm: the Charger Manager representing the battery.
384 * Restart charging by turning off and on the charger.
386 static int try_charger_restart(struct charger_manager *cm)
390 if (cm->emergency_stop)
393 err = try_charger_enable(cm, false);
397 return try_charger_enable(cm, true);
401 * uevent_notify - Let users know something has changed.
402 * @cm: the Charger Manager representing the battery.
403 * @event: the event string.
405 * If @event is null, it implies that uevent_notify is called
406 * by resume function. When called in the resume function, cm_suspended
407 * should be already reset to false in order to let uevent_notify
408 * notify the recent event during the suspend to users. While
409 * suspended, uevent_notify does not notify users, but tracks
410 * events so that uevent_notify can notify users later after resumed.
412 static void uevent_notify(struct charger_manager *cm, const char *event)
414 static char env_str[UEVENT_BUF_SIZE + 1] = "";
415 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
418 /* Nothing in suspended-event buffer */
419 if (env_str_save[0] == 0) {
420 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
421 return; /* status not changed */
422 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
426 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
427 return; /* Duplicated. */
428 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
433 /* No messages pending */
434 if (!env_str_save[0])
437 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
438 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
444 /* status not changed */
445 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
448 /* save the status and notify the update */
449 strncpy(env_str, event, UEVENT_BUF_SIZE);
450 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
452 dev_info(cm->dev, "%s\n", event);
456 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
457 * @work: the work_struct appointing the function
459 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
460 * charger_desc, Charger Manager checks voltage drop after the battery
461 * "FULL" event. It checks whether the voltage has dropped more than
462 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
464 static void fullbatt_vchk(struct work_struct *work)
466 struct delayed_work *dwork = to_delayed_work(work);
467 struct charger_manager *cm = container_of(dwork,
468 struct charger_manager, fullbatt_vchk_work);
469 struct charger_desc *desc = cm->desc;
470 int batt_uV, err, diff;
472 /* remove the appointment for fullbatt_vchk */
473 cm->fullbatt_vchk_jiffies_at = 0;
475 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
478 err = get_batt_uV(cm, &batt_uV);
480 dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err);
484 diff = desc->fullbatt_uV - batt_uV;
488 dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff);
490 if (diff > desc->fullbatt_vchkdrop_uV) {
491 try_charger_restart(cm);
492 uevent_notify(cm, "Recharging");
497 * check_charging_duration - Monitor charging/discharging duration
498 * @cm: the Charger Manager representing the battery.
500 * If whole charging duration exceed 'charging_max_duration_ms',
501 * cm stop charging to prevent overcharge/overheat. If discharging
502 * duration exceed 'discharging _max_duration_ms', charger cable is
503 * attached, after full-batt, cm start charging to maintain fully
504 * charged state for battery.
506 static int check_charging_duration(struct charger_manager *cm)
508 struct charger_desc *desc = cm->desc;
509 u64 curr = ktime_to_ms(ktime_get());
513 if (!desc->charging_max_duration_ms &&
514 !desc->discharging_max_duration_ms)
517 if (cm->charger_enabled) {
518 duration = curr - cm->charging_start_time;
520 if (duration > desc->charging_max_duration_ms) {
521 dev_info(cm->dev, "Charging duration exceed %lldms\n",
522 desc->charging_max_duration_ms);
523 uevent_notify(cm, "Discharging");
524 try_charger_enable(cm, false);
527 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
528 duration = curr - cm->charging_end_time;
530 if (duration > desc->charging_max_duration_ms &&
531 is_ext_pwr_online(cm)) {
532 dev_info(cm->dev, "Discharging duration exceed %lldms\n",
533 desc->discharging_max_duration_ms);
534 uevent_notify(cm, "Recharging");
535 try_charger_enable(cm, true);
544 * _cm_monitor - Monitor the temperature and return true for exceptions.
545 * @cm: the Charger Manager representing the battery.
547 * Returns true if there is an event to notify for the battery.
548 * (True if the status of "emergency_stop" changes)
550 static bool _cm_monitor(struct charger_manager *cm)
552 struct charger_desc *desc = cm->desc;
553 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
555 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
556 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
558 /* It has been stopped already */
559 if (temp && cm->emergency_stop)
563 * Check temperature whether overheat or cold.
564 * If temperature is out of range normal state, stop charging.
567 cm->emergency_stop = temp;
568 if (!try_charger_enable(cm, false)) {
570 uevent_notify(cm, "OVERHEAT");
572 uevent_notify(cm, "COLD");
576 * Check whole charging duration and discharing duration
579 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
581 "Charging/Discharging duration is out of range\n");
583 * Check dropped voltage of battery. If battery voltage is more
584 * dropped than fullbatt_vchkdrop_uV after fully charged state,
585 * charger-manager have to recharge battery.
587 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
588 !cm->charger_enabled) {
589 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
592 * Check whether fully charged state to protect overcharge
593 * if charger-manager is charging for battery.
595 } else if (!cm->emergency_stop && is_full_charged(cm) &&
596 cm->charger_enabled) {
597 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
598 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
600 try_charger_enable(cm, false);
602 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
604 cm->emergency_stop = 0;
605 if (is_ext_pwr_online(cm)) {
606 if (!try_charger_enable(cm, true))
607 uevent_notify(cm, "CHARGING");
615 * cm_monitor - Monitor every battery.
617 * Returns true if there is an event to notify from any of the batteries.
618 * (True if the status of "emergency_stop" changes)
620 static bool cm_monitor(void)
623 struct charger_manager *cm;
625 mutex_lock(&cm_list_mtx);
627 list_for_each_entry(cm, &cm_list, entry) {
632 mutex_unlock(&cm_list_mtx);
638 * _setup_polling - Setup the next instance of polling.
639 * @work: work_struct of the function _setup_polling.
641 static void _setup_polling(struct work_struct *work)
643 unsigned long min = ULONG_MAX;
644 struct charger_manager *cm;
645 bool keep_polling = false;
646 unsigned long _next_polling;
648 mutex_lock(&cm_list_mtx);
650 list_for_each_entry(cm, &cm_list, entry) {
651 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
654 if (min > cm->desc->polling_interval_ms)
655 min = cm->desc->polling_interval_ms;
659 polling_jiffy = msecs_to_jiffies(min);
660 if (polling_jiffy <= CM_JIFFIES_SMALL)
661 polling_jiffy = CM_JIFFIES_SMALL + 1;
664 polling_jiffy = ULONG_MAX;
665 if (polling_jiffy == ULONG_MAX)
668 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
669 ". try it later. %s\n", __func__);
672 * Use mod_delayed_work() iff the next polling interval should
673 * occur before the currently scheduled one. If @cm_monitor_work
674 * isn't active, the end result is the same, so no need to worry
675 * about stale @next_polling.
677 _next_polling = jiffies + polling_jiffy;
679 if (time_before(_next_polling, next_polling)) {
680 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
681 next_polling = _next_polling;
683 if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
684 next_polling = _next_polling;
687 mutex_unlock(&cm_list_mtx);
689 static DECLARE_WORK(setup_polling, _setup_polling);
692 * cm_monitor_poller - The Monitor / Poller.
693 * @work: work_struct of the function cm_monitor_poller
695 * During non-suspended state, cm_monitor_poller is used to poll and monitor
698 static void cm_monitor_poller(struct work_struct *work)
701 schedule_work(&setup_polling);
705 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
706 * @cm: the Charger Manager representing the battery.
708 static void fullbatt_handler(struct charger_manager *cm)
710 struct charger_desc *desc = cm->desc;
712 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
716 device_set_wakeup_capable(cm->dev, true);
718 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
719 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
720 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
721 desc->fullbatt_vchkdrop_ms);
723 if (cm->fullbatt_vchk_jiffies_at == 0)
724 cm->fullbatt_vchk_jiffies_at = 1;
727 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
728 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
732 * battout_handler - Event handler for CM_EVENT_BATT_OUT
733 * @cm: the Charger Manager representing the battery.
735 static void battout_handler(struct charger_manager *cm)
738 device_set_wakeup_capable(cm->dev, true);
740 if (!is_batt_present(cm)) {
741 dev_emerg(cm->dev, "Battery Pulled Out!\n");
742 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
744 uevent_notify(cm, "Battery Reinserted?");
749 * misc_event_handler - Handler for other evnets
750 * @cm: the Charger Manager representing the battery.
751 * @type: the Charger Manager representing the battery.
753 static void misc_event_handler(struct charger_manager *cm,
754 enum cm_event_types type)
757 device_set_wakeup_capable(cm->dev, true);
759 if (is_polling_required(cm) && cm->desc->polling_interval_ms)
760 schedule_work(&setup_polling);
761 uevent_notify(cm, default_event_names[type]);
764 static int charger_get_property(struct power_supply *psy,
765 enum power_supply_property psp,
766 union power_supply_propval *val)
768 struct charger_manager *cm = container_of(psy,
769 struct charger_manager, charger_psy);
770 struct charger_desc *desc = cm->desc;
775 case POWER_SUPPLY_PROP_STATUS:
777 val->intval = POWER_SUPPLY_STATUS_CHARGING;
778 else if (is_ext_pwr_online(cm))
779 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
781 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
783 case POWER_SUPPLY_PROP_HEALTH:
784 if (cm->emergency_stop > 0)
785 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
786 else if (cm->emergency_stop < 0)
787 val->intval = POWER_SUPPLY_HEALTH_COLD;
789 val->intval = POWER_SUPPLY_HEALTH_GOOD;
791 case POWER_SUPPLY_PROP_PRESENT:
792 if (is_batt_present(cm))
797 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
798 ret = get_batt_uV(cm, &val->intval);
800 case POWER_SUPPLY_PROP_CURRENT_NOW:
801 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
802 POWER_SUPPLY_PROP_CURRENT_NOW, val);
804 case POWER_SUPPLY_PROP_TEMP:
805 /* in thenth of centigrade */
806 if (cm->last_temp_mC == INT_MIN)
807 desc->temperature_out_of_range(&cm->last_temp_mC);
808 val->intval = cm->last_temp_mC / 100;
809 if (!desc->measure_battery_temp)
812 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
813 /* in thenth of centigrade */
814 if (cm->last_temp_mC == INT_MIN)
815 desc->temperature_out_of_range(&cm->last_temp_mC);
816 val->intval = cm->last_temp_mC / 100;
817 if (desc->measure_battery_temp)
820 case POWER_SUPPLY_PROP_CAPACITY:
821 if (!cm->fuel_gauge) {
826 if (!is_batt_present(cm)) {
827 /* There is no battery. Assume 100% */
832 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
833 POWER_SUPPLY_PROP_CAPACITY, val);
837 if (val->intval > 100) {
844 /* Do not adjust SOC when charging: voltage is overrated */
849 * If the capacity value is inconsistent, calibrate it base on
850 * the battery voltage values and the thresholds given as desc
852 ret = get_batt_uV(cm, &uV);
854 /* Voltage information not available. No calibration */
859 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
866 case POWER_SUPPLY_PROP_ONLINE:
867 if (is_ext_pwr_online(cm))
872 case POWER_SUPPLY_PROP_CHARGE_FULL:
873 if (is_full_charged(cm))
879 case POWER_SUPPLY_PROP_CHARGE_NOW:
880 if (is_charging(cm)) {
881 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
882 POWER_SUPPLY_PROP_CHARGE_NOW,
888 /* If CHARGE_NOW is supplied, use it */
889 val->intval = (val->intval > 0) ?
902 #define NUM_CHARGER_PSY_OPTIONAL (4)
903 static enum power_supply_property default_charger_props[] = {
904 /* Guaranteed to provide */
905 POWER_SUPPLY_PROP_STATUS,
906 POWER_SUPPLY_PROP_HEALTH,
907 POWER_SUPPLY_PROP_PRESENT,
908 POWER_SUPPLY_PROP_VOLTAGE_NOW,
909 POWER_SUPPLY_PROP_CAPACITY,
910 POWER_SUPPLY_PROP_ONLINE,
911 POWER_SUPPLY_PROP_CHARGE_FULL,
913 * Optional properties are:
914 * POWER_SUPPLY_PROP_CHARGE_NOW,
915 * POWER_SUPPLY_PROP_CURRENT_NOW,
916 * POWER_SUPPLY_PROP_TEMP, and
917 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
921 static struct power_supply psy_default = {
923 .type = POWER_SUPPLY_TYPE_BATTERY,
924 .properties = default_charger_props,
925 .num_properties = ARRAY_SIZE(default_charger_props),
926 .get_property = charger_get_property,
930 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
933 * Returns true if the alarm is set for Charger Manager to use.
935 * cm_setup_timer fails to set an alarm,
936 * cm_setup_timer does not need to set an alarm for Charger Manager,
937 * or an alarm previously configured is to be used.
939 static bool cm_setup_timer(void)
941 struct charger_manager *cm;
942 unsigned int wakeup_ms = UINT_MAX;
945 mutex_lock(&cm_list_mtx);
947 list_for_each_entry(cm, &cm_list, entry) {
948 unsigned int fbchk_ms = 0;
950 /* fullbatt_vchk is required. setup timer for that */
951 if (cm->fullbatt_vchk_jiffies_at) {
952 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
954 if (time_is_before_eq_jiffies(
955 cm->fullbatt_vchk_jiffies_at) ||
956 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
957 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
961 CM_MIN_VALID(wakeup_ms, fbchk_ms);
963 /* Skip if polling is not required for this CM */
964 if (!is_polling_required(cm) && !cm->emergency_stop)
966 if (cm->desc->polling_interval_ms == 0)
968 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
971 mutex_unlock(&cm_list_mtx);
973 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
974 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
976 struct rtc_wkalrm tmp;
977 unsigned long time, now;
978 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
981 * Set alarm with the polling interval (wakeup_ms)
982 * except when rtc_wkalarm_save comes first.
983 * However, the alarm time should be NOW +
984 * CM_RTC_SMALL or later.
987 rtc_read_time(rtc_dev, &tmp.time);
988 rtc_tm_to_time(&tmp.time, &now);
989 if (add < CM_RTC_SMALL)
995 if (rtc_wkalarm_save.enabled &&
996 rtc_wkalarm_save_time &&
997 rtc_wkalarm_save_time < time) {
998 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
999 time = now + CM_RTC_SMALL;
1001 time = rtc_wkalarm_save_time;
1003 /* The timer is not appointed by CM */
1007 pr_info("Waking up after %lu secs\n", time - now);
1009 rtc_time_to_tm(time, &tmp.time);
1010 rtc_set_alarm(rtc_dev, &tmp);
1011 cm_suspend_duration_ms += wakeup_ms;
1017 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1021 static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1023 unsigned long jiffy_now = jiffies;
1025 if (!cm->fullbatt_vchk_jiffies_at)
1028 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1029 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1031 /* Execute now if it's going to be executed not too long after */
1032 jiffy_now += CM_JIFFIES_SMALL;
1034 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1035 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1039 * cm_suspend_again - Determine whether suspend again or not
1041 * Returns true if the system should be suspended again
1042 * Returns false if the system should be woken up
1044 bool cm_suspend_again(void)
1046 struct charger_manager *cm;
1049 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1057 mutex_lock(&cm_list_mtx);
1058 list_for_each_entry(cm, &cm_list, entry) {
1059 _cm_fbchk_in_suspend(cm);
1061 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
1062 cm->status_save_batt != is_batt_present(cm)) {
1067 mutex_unlock(&cm_list_mtx);
1069 cm_rtc_set = cm_setup_timer();
1071 /* It's about the time when the non-CM appointed timer goes off */
1072 if (rtc_wkalarm_save.enabled) {
1074 struct rtc_time tmp;
1076 rtc_read_time(rtc_dev, &tmp);
1077 rtc_tm_to_time(&tmp, &now);
1079 if (rtc_wkalarm_save_time &&
1080 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1085 EXPORT_SYMBOL_GPL(cm_suspend_again);
1088 * setup_charger_manager - initialize charger_global_desc data
1089 * @gd: pointer to instance of charger_global_desc
1091 int setup_charger_manager(struct charger_global_desc *gd)
1097 rtc_class_close(rtc_dev);
1101 if (!gd->rtc_only_wakeup) {
1102 pr_err("The callback rtc_only_wakeup is not given\n");
1107 rtc_dev = rtc_class_open(gd->rtc_name);
1108 if (IS_ERR_OR_NULL(rtc_dev)) {
1110 /* Retry at probe. RTC may be not registered yet */
1113 pr_warn("No wakeup timer is given for charger manager. "
1114 "In-suspend monitoring won't work.\n");
1120 EXPORT_SYMBOL_GPL(setup_charger_manager);
1123 * charger_extcon_work - enable/diable charger according to the state
1126 * @work: work_struct of the function charger_extcon_work.
1128 static void charger_extcon_work(struct work_struct *work)
1130 struct charger_cable *cable =
1131 container_of(work, struct charger_cable, wq);
1134 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1135 ret = regulator_set_current_limit(cable->charger->consumer,
1136 cable->min_uA, cable->max_uA);
1138 pr_err("Cannot set current limit of %s (%s)\n",
1139 cable->charger->regulator_name, cable->name);
1143 pr_info("Set current limit of %s : %duA ~ %duA\n",
1144 cable->charger->regulator_name,
1145 cable->min_uA, cable->max_uA);
1148 try_charger_enable(cable->cm, cable->attached);
1152 * charger_extcon_notifier - receive the state of charger cable
1153 * when registered cable is attached or detached.
1155 * @self: the notifier block of the charger_extcon_notifier.
1156 * @event: the cable state.
1157 * @ptr: the data pointer of notifier block.
1159 static int charger_extcon_notifier(struct notifier_block *self,
1160 unsigned long event, void *ptr)
1162 struct charger_cable *cable =
1163 container_of(self, struct charger_cable, nb);
1166 * The newly state of charger cable.
1167 * If cable is attached, cable->attached is true.
1169 cable->attached = event;
1172 * Setup monitoring to check battery state
1173 * when charger cable is attached.
1175 if (cable->attached && is_polling_required(cable->cm)) {
1176 cancel_work_sync(&setup_polling);
1177 schedule_work(&setup_polling);
1181 * Setup work for controlling charger(regulator)
1182 * according to charger cable.
1184 schedule_work(&cable->wq);
1190 * charger_extcon_init - register external connector to use it
1191 * as the charger cable
1193 * @cm: the Charger Manager representing the battery.
1194 * @cable: the Charger cable representing the external connector.
1196 static int charger_extcon_init(struct charger_manager *cm,
1197 struct charger_cable *cable)
1202 * Charger manager use Extcon framework to identify
1203 * the charger cable among various external connector
1204 * cable (e.g., TA, USB, MHL, Dock).
1206 INIT_WORK(&cable->wq, charger_extcon_work);
1207 cable->nb.notifier_call = charger_extcon_notifier;
1208 ret = extcon_register_interest(&cable->extcon_dev,
1209 cable->extcon_name, cable->name, &cable->nb);
1211 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1212 cable->extcon_name, cable->name);
1220 * charger_manager_register_extcon - Register extcon device to recevie state
1222 * @cm: the Charger Manager representing the battery.
1224 * This function support EXTCON(External Connector) subsystem to detect the
1225 * state of charger cables for enabling or disabling charger(regulator) and
1226 * select the charger cable for charging among a number of external cable
1227 * according to policy of H/W board.
1229 static int charger_manager_register_extcon(struct charger_manager *cm)
1231 struct charger_desc *desc = cm->desc;
1232 struct charger_regulator *charger;
1237 for (i = 0; i < desc->num_charger_regulators; i++) {
1238 charger = &desc->charger_regulators[i];
1240 charger->consumer = regulator_get(cm->dev,
1241 charger->regulator_name);
1242 if (IS_ERR(charger->consumer)) {
1243 dev_err(cm->dev, "Cannot find charger(%s)\n",
1244 charger->regulator_name);
1245 return PTR_ERR(charger->consumer);
1249 for (j = 0; j < charger->num_cables; j++) {
1250 struct charger_cable *cable = &charger->cables[j];
1252 ret = charger_extcon_init(cm, cable);
1254 dev_err(cm->dev, "Cannot initialize charger(%s)\n",
1255 charger->regulator_name);
1258 cable->charger = charger;
1267 /* help function of sysfs node to control charger(regulator) */
1268 static ssize_t charger_name_show(struct device *dev,
1269 struct device_attribute *attr, char *buf)
1271 struct charger_regulator *charger
1272 = container_of(attr, struct charger_regulator, attr_name);
1274 return sprintf(buf, "%s\n", charger->regulator_name);
1277 static ssize_t charger_state_show(struct device *dev,
1278 struct device_attribute *attr, char *buf)
1280 struct charger_regulator *charger
1281 = container_of(attr, struct charger_regulator, attr_state);
1284 if (!charger->externally_control)
1285 state = regulator_is_enabled(charger->consumer);
1287 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1290 static ssize_t charger_externally_control_show(struct device *dev,
1291 struct device_attribute *attr, char *buf)
1293 struct charger_regulator *charger = container_of(attr,
1294 struct charger_regulator, attr_externally_control);
1296 return sprintf(buf, "%d\n", charger->externally_control);
1299 static ssize_t charger_externally_control_store(struct device *dev,
1300 struct device_attribute *attr, const char *buf,
1303 struct charger_regulator *charger
1304 = container_of(attr, struct charger_regulator,
1305 attr_externally_control);
1306 struct charger_manager *cm = charger->cm;
1307 struct charger_desc *desc = cm->desc;
1310 int externally_control;
1311 int chargers_externally_control = 1;
1313 ret = sscanf(buf, "%d", &externally_control);
1319 if (!externally_control) {
1320 charger->externally_control = 0;
1324 for (i = 0; i < desc->num_charger_regulators; i++) {
1325 if (&desc->charger_regulators[i] != charger &&
1326 !desc->charger_regulators[i].externally_control) {
1328 * At least, one charger is controlled by
1331 chargers_externally_control = 0;
1336 if (!chargers_externally_control) {
1337 if (cm->charger_enabled) {
1338 try_charger_enable(charger->cm, false);
1339 charger->externally_control = externally_control;
1340 try_charger_enable(charger->cm, true);
1342 charger->externally_control = externally_control;
1346 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1347 charger->regulator_name);
1354 * charger_manager_register_sysfs - Register sysfs entry for each charger
1355 * @cm: the Charger Manager representing the battery.
1357 * This function add sysfs entry for charger(regulator) to control charger from
1358 * user-space. If some development board use one more chargers for charging
1359 * but only need one charger on specific case which is dependent on user
1360 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1361 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1362 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1363 * externally_control, this charger isn't controlled from charger-manager and
1364 * always stay off state of regulator.
1366 static int charger_manager_register_sysfs(struct charger_manager *cm)
1368 struct charger_desc *desc = cm->desc;
1369 struct charger_regulator *charger;
1370 int chargers_externally_control = 1;
1376 /* Create sysfs entry to control charger(regulator) */
1377 for (i = 0; i < desc->num_charger_regulators; i++) {
1378 charger = &desc->charger_regulators[i];
1380 snprintf(buf, 10, "charger.%d", i);
1381 str = devm_kzalloc(cm->dev,
1382 sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
1389 charger->attrs[0] = &charger->attr_name.attr;
1390 charger->attrs[1] = &charger->attr_state.attr;
1391 charger->attrs[2] = &charger->attr_externally_control.attr;
1392 charger->attrs[3] = NULL;
1393 charger->attr_g.name = str;
1394 charger->attr_g.attrs = charger->attrs;
1396 sysfs_attr_init(&charger->attr_name.attr);
1397 charger->attr_name.attr.name = "name";
1398 charger->attr_name.attr.mode = 0444;
1399 charger->attr_name.show = charger_name_show;
1401 sysfs_attr_init(&charger->attr_state.attr);
1402 charger->attr_state.attr.name = "state";
1403 charger->attr_state.attr.mode = 0444;
1404 charger->attr_state.show = charger_state_show;
1406 sysfs_attr_init(&charger->attr_externally_control.attr);
1407 charger->attr_externally_control.attr.name
1408 = "externally_control";
1409 charger->attr_externally_control.attr.mode = 0644;
1410 charger->attr_externally_control.show
1411 = charger_externally_control_show;
1412 charger->attr_externally_control.store
1413 = charger_externally_control_store;
1415 if (!desc->charger_regulators[i].externally_control ||
1416 !chargers_externally_control)
1417 chargers_externally_control = 0;
1419 dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1420 charger->regulator_name, charger->externally_control);
1422 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1425 dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n",
1426 charger->regulator_name);
1432 if (chargers_externally_control) {
1433 dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n");
1442 static int charger_manager_probe(struct platform_device *pdev)
1444 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1445 struct charger_manager *cm;
1448 union power_supply_propval val;
1450 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1451 rtc_dev = rtc_class_open(g_desc->rtc_name);
1452 if (IS_ERR_OR_NULL(rtc_dev)) {
1454 dev_err(&pdev->dev, "Cannot get RTC %s\n",
1461 dev_err(&pdev->dev, "No platform data (desc) found\n");
1465 cm = devm_kzalloc(&pdev->dev,
1466 sizeof(struct charger_manager), GFP_KERNEL);
1470 /* Basic Values. Unspecified are Null or 0 */
1471 cm->dev = &pdev->dev;
1473 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1476 * The following two do not need to be errors.
1477 * Users may intentionally ignore those two features.
1479 if (desc->fullbatt_uV == 0) {
1480 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
1482 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1483 dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
1484 desc->fullbatt_vchkdrop_ms = 0;
1485 desc->fullbatt_vchkdrop_uV = 0;
1487 if (desc->fullbatt_soc == 0) {
1488 dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
1490 if (desc->fullbatt_full_capacity == 0) {
1491 dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
1494 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1495 dev_err(&pdev->dev, "charger_regulators undefined\n");
1499 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1500 dev_err(&pdev->dev, "No power supply defined\n");
1504 /* Counting index only */
1505 while (desc->psy_charger_stat[i])
1508 cm->charger_stat = devm_kzalloc(&pdev->dev,
1509 sizeof(struct power_supply *) * i, GFP_KERNEL);
1510 if (!cm->charger_stat)
1513 for (i = 0; desc->psy_charger_stat[i]; i++) {
1514 cm->charger_stat[i] = power_supply_get_by_name(
1515 desc->psy_charger_stat[i]);
1516 if (!cm->charger_stat[i]) {
1517 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1518 desc->psy_charger_stat[i]);
1523 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1524 if (!cm->fuel_gauge) {
1525 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1526 desc->psy_fuel_gauge);
1530 if (desc->polling_interval_ms == 0 ||
1531 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1532 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1536 if (!desc->temperature_out_of_range) {
1537 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1541 if (!desc->charging_max_duration_ms ||
1542 !desc->discharging_max_duration_ms) {
1543 dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
1544 desc->charging_max_duration_ms = 0;
1545 desc->discharging_max_duration_ms = 0;
1548 platform_set_drvdata(pdev, cm);
1550 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1552 if (!desc->psy_name)
1553 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
1555 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1556 cm->charger_psy.name = cm->psy_name_buf;
1558 /* Allocate for psy properties because they may vary */
1559 cm->charger_psy.properties = devm_kzalloc(&pdev->dev,
1560 sizeof(enum power_supply_property)
1561 * (ARRAY_SIZE(default_charger_props) +
1562 NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL);
1563 if (!cm->charger_psy.properties)
1566 memcpy(cm->charger_psy.properties, default_charger_props,
1567 sizeof(enum power_supply_property) *
1568 ARRAY_SIZE(default_charger_props));
1569 cm->charger_psy.num_properties = psy_default.num_properties;
1571 /* Find which optional psy-properties are available */
1572 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1573 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1574 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1575 POWER_SUPPLY_PROP_CHARGE_NOW;
1576 cm->charger_psy.num_properties++;
1578 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1579 POWER_SUPPLY_PROP_CURRENT_NOW,
1581 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1582 POWER_SUPPLY_PROP_CURRENT_NOW;
1583 cm->charger_psy.num_properties++;
1586 if (desc->measure_battery_temp) {
1587 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1588 POWER_SUPPLY_PROP_TEMP;
1589 cm->charger_psy.num_properties++;
1591 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1592 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1593 cm->charger_psy.num_properties++;
1596 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1598 ret = power_supply_register(NULL, &cm->charger_psy);
1600 dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
1601 cm->charger_psy.name);
1605 /* Register extcon device for charger cable */
1606 ret = charger_manager_register_extcon(cm);
1608 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1609 goto err_reg_extcon;
1612 /* Register sysfs entry for charger(regulator) */
1613 ret = charger_manager_register_sysfs(cm);
1616 "Cannot initialize sysfs entry of regulator\n");
1620 /* Add to the list */
1621 mutex_lock(&cm_list_mtx);
1622 list_add(&cm->entry, &cm_list);
1623 mutex_unlock(&cm_list_mtx);
1626 * Charger-manager is capable of waking up the systme from sleep
1627 * when event is happend through cm_notify_event()
1629 device_init_wakeup(&pdev->dev, true);
1630 device_set_wakeup_capable(&pdev->dev, false);
1632 schedule_work(&setup_polling);
1637 for (i = 0; i < desc->num_charger_regulators; i++) {
1638 struct charger_regulator *charger;
1640 charger = &desc->charger_regulators[i];
1641 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1645 for (i = 0; i < desc->num_charger_regulators; i++) {
1646 struct charger_regulator *charger;
1648 charger = &desc->charger_regulators[i];
1649 for (j = 0; j < charger->num_cables; j++) {
1650 struct charger_cable *cable = &charger->cables[j];
1651 /* Remove notifier block if only edev exists */
1652 if (cable->extcon_dev.edev)
1653 extcon_unregister_interest(&cable->extcon_dev);
1656 regulator_put(desc->charger_regulators[i].consumer);
1659 power_supply_unregister(&cm->charger_psy);
1664 static int charger_manager_remove(struct platform_device *pdev)
1666 struct charger_manager *cm = platform_get_drvdata(pdev);
1667 struct charger_desc *desc = cm->desc;
1671 /* Remove from the list */
1672 mutex_lock(&cm_list_mtx);
1673 list_del(&cm->entry);
1674 mutex_unlock(&cm_list_mtx);
1676 cancel_work_sync(&setup_polling);
1677 cancel_delayed_work_sync(&cm_monitor_work);
1679 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1680 struct charger_regulator *charger
1681 = &desc->charger_regulators[i];
1682 for (j = 0 ; j < charger->num_cables ; j++) {
1683 struct charger_cable *cable = &charger->cables[j];
1684 extcon_unregister_interest(&cable->extcon_dev);
1688 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1689 regulator_put(desc->charger_regulators[i].consumer);
1691 power_supply_unregister(&cm->charger_psy);
1693 try_charger_enable(cm, false);
1698 static const struct platform_device_id charger_manager_id[] = {
1699 { "charger-manager", 0 },
1702 MODULE_DEVICE_TABLE(platform, charger_manager_id);
1704 static int cm_suspend_noirq(struct device *dev)
1708 if (device_may_wakeup(dev)) {
1709 device_set_wakeup_capable(dev, false);
1716 static int cm_suspend_prepare(struct device *dev)
1718 struct charger_manager *cm = dev_get_drvdata(dev);
1720 if (!cm_suspended) {
1722 struct rtc_time tmp;
1725 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1726 rtc_read_time(rtc_dev, &tmp);
1728 if (rtc_wkalarm_save.enabled) {
1729 rtc_tm_to_time(&rtc_wkalarm_save.time,
1730 &rtc_wkalarm_save_time);
1731 rtc_tm_to_time(&tmp, &now);
1732 if (now > rtc_wkalarm_save_time)
1733 rtc_wkalarm_save_time = 0;
1735 rtc_wkalarm_save_time = 0;
1738 cm_suspended = true;
1741 cancel_delayed_work(&cm->fullbatt_vchk_work);
1742 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1743 cm->status_save_batt = is_batt_present(cm);
1746 cm_suspend_duration_ms = 0;
1747 cm_rtc_set = cm_setup_timer();
1753 static void cm_suspend_complete(struct device *dev)
1755 struct charger_manager *cm = dev_get_drvdata(dev);
1759 struct rtc_wkalrm tmp;
1761 rtc_read_alarm(rtc_dev, &tmp);
1762 rtc_wkalarm_save.pending = tmp.pending;
1763 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1765 cm_suspended = false;
1769 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1770 if (cm->fullbatt_vchk_jiffies_at) {
1771 unsigned long delay = 0;
1772 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1774 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1775 delay = (unsigned long)((long)now
1776 - (long)(cm->fullbatt_vchk_jiffies_at));
1777 delay = jiffies_to_msecs(delay);
1783 * Account for cm_suspend_duration_ms if
1784 * assume_timer_stops_in_suspend is active
1786 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1787 if (delay > cm_suspend_duration_ms)
1788 delay -= cm_suspend_duration_ms;
1793 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1794 msecs_to_jiffies(delay));
1796 device_set_wakeup_capable(cm->dev, false);
1797 uevent_notify(cm, NULL);
1800 static const struct dev_pm_ops charger_manager_pm = {
1801 .prepare = cm_suspend_prepare,
1802 .suspend_noirq = cm_suspend_noirq,
1803 .complete = cm_suspend_complete,
1806 static struct platform_driver charger_manager_driver = {
1808 .name = "charger-manager",
1809 .owner = THIS_MODULE,
1810 .pm = &charger_manager_pm,
1812 .probe = charger_manager_probe,
1813 .remove = charger_manager_remove,
1814 .id_table = charger_manager_id,
1817 static int __init charger_manager_init(void)
1819 cm_wq = create_freezable_workqueue("charger_manager");
1820 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1822 return platform_driver_register(&charger_manager_driver);
1824 late_initcall(charger_manager_init);
1826 static void __exit charger_manager_cleanup(void)
1828 destroy_workqueue(cm_wq);
1831 platform_driver_unregister(&charger_manager_driver);
1833 module_exit(charger_manager_cleanup);
1836 * find_power_supply - find the associated power_supply of charger
1837 * @cm: the Charger Manager representing the battery
1838 * @psy: pointer to instance of charger's power_supply
1840 static bool find_power_supply(struct charger_manager *cm,
1841 struct power_supply *psy)
1846 for (i = 0; cm->charger_stat[i]; i++) {
1847 if (psy == cm->charger_stat[i]) {
1857 * cm_notify_event - charger driver notify Charger Manager of charger event
1858 * @psy: pointer to instance of charger's power_supply
1859 * @type: type of charger event
1860 * @msg: optional message passed to uevent_notify fuction
1862 void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1865 struct charger_manager *cm;
1866 bool found_power_supply = false;
1871 mutex_lock(&cm_list_mtx);
1872 list_for_each_entry(cm, &cm_list, entry) {
1873 found_power_supply = find_power_supply(cm, psy);
1874 if (found_power_supply)
1877 mutex_unlock(&cm_list_mtx);
1879 if (!found_power_supply)
1883 case CM_EVENT_BATT_FULL:
1884 fullbatt_handler(cm);
1886 case CM_EVENT_BATT_OUT:
1887 battout_handler(cm);
1889 case CM_EVENT_BATT_IN:
1890 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1891 misc_event_handler(cm, type);
1893 case CM_EVENT_UNKNOWN:
1894 case CM_EVENT_OTHERS:
1895 uevent_notify(cm, msg ? msg : default_event_names[type]);
1898 dev_err(cm->dev, "%s: type not specified\n", __func__);
1902 EXPORT_SYMBOL_GPL(cm_notify_event);
1904 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1905 MODULE_DESCRIPTION("Charger Manager");
1906 MODULE_LICENSE("GPL");