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>
27 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
28 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
31 #define CM_JIFFIES_SMALL (2)
33 /* If y is valid (> 0) and smaller than x, do x = y */
34 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
37 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
38 * rtc alarm. It should be 2 or larger
40 #define CM_RTC_SMALL (2)
42 #define UEVENT_BUF_SIZE 32
44 static LIST_HEAD(cm_list);
45 static DEFINE_MUTEX(cm_list_mtx);
47 /* About in-suspend (suspend-again) monitoring */
48 static struct rtc_device *rtc_dev;
51 * Save the wakeup alarm before entering suspend-to-RAM
53 static struct rtc_wkalrm rtc_wkalarm_save;
54 /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
55 static unsigned long rtc_wkalarm_save_time;
56 static bool cm_suspended;
57 static bool cm_rtc_set;
58 static unsigned long cm_suspend_duration_ms;
60 /* Global charger-manager description */
61 static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
64 * is_batt_present - See if the battery presents in place.
65 * @cm: the Charger Manager representing the battery.
67 static bool is_batt_present(struct charger_manager *cm)
69 union power_supply_propval val;
73 switch (cm->desc->battery_present) {
75 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
76 POWER_SUPPLY_PROP_PRESENT, &val);
77 if (ret == 0 && val.intval)
81 for (i = 0; cm->charger_stat[i]; i++) {
82 ret = cm->charger_stat[i]->get_property(
84 POWER_SUPPLY_PROP_PRESENT, &val);
85 if (ret == 0 && val.intval) {
97 * is_ext_pwr_online - See if an external power source is attached to charge
98 * @cm: the Charger Manager representing the battery.
100 * Returns true if at least one of the chargers of the battery has an external
101 * power source attached to charge the battery regardless of whether it is
102 * actually charging or not.
104 static bool is_ext_pwr_online(struct charger_manager *cm)
106 union power_supply_propval val;
110 /* If at least one of them has one, it's yes. */
111 for (i = 0; cm->charger_stat[i]; i++) {
112 ret = cm->charger_stat[i]->get_property(
114 POWER_SUPPLY_PROP_ONLINE, &val);
115 if (ret == 0 && val.intval) {
125 * get_batt_uV - Get the voltage level of the battery
126 * @cm: the Charger Manager representing the battery.
127 * @uV: the voltage level returned.
129 * Returns 0 if there is no error.
130 * Returns a negative value on error.
132 static int get_batt_uV(struct charger_manager *cm, int *uV)
134 union power_supply_propval val;
140 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
141 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
150 * is_charging - Returns true if the battery is being charged.
151 * @cm: the Charger Manager representing the battery.
153 static bool is_charging(struct charger_manager *cm)
156 bool charging = false;
157 union power_supply_propval val;
159 /* If there is no battery, it cannot be charged */
160 if (!is_batt_present(cm))
163 /* If at least one of the charger is charging, return yes */
164 for (i = 0; cm->charger_stat[i]; i++) {
165 /* 1. The charger sholuld not be DISABLED */
166 if (cm->emergency_stop)
168 if (!cm->charger_enabled)
171 /* 2. The charger should be online (ext-power) */
172 ret = cm->charger_stat[i]->get_property(
174 POWER_SUPPLY_PROP_ONLINE, &val);
176 dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
177 cm->desc->psy_charger_stat[i]);
184 * 3. The charger should not be FULL, DISCHARGING,
187 ret = cm->charger_stat[i]->get_property(
189 POWER_SUPPLY_PROP_STATUS, &val);
191 dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
192 cm->desc->psy_charger_stat[i]);
195 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
196 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
197 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
200 /* Then, this is charging. */
209 * is_polling_required - Return true if need to continue polling for this CM.
210 * @cm: the Charger Manager representing the battery.
212 static bool is_polling_required(struct charger_manager *cm)
214 switch (cm->desc->polling_mode) {
215 case CM_POLL_DISABLE:
219 case CM_POLL_EXTERNAL_POWER_ONLY:
220 return is_ext_pwr_online(cm);
221 case CM_POLL_CHARGING_ONLY:
222 return is_charging(cm);
224 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
225 cm->desc->polling_mode);
232 * try_charger_enable - Enable/Disable chargers altogether
233 * @cm: the Charger Manager representing the battery.
234 * @enable: true: enable / false: disable
236 * Note that Charger Manager keeps the charger enabled regardless whether
237 * the charger is charging or not (because battery is full or no external
238 * power source exists) except when CM needs to disable chargers forcibly
239 * bacause of emergency causes; when the battery is overheated or too cold.
241 static int try_charger_enable(struct charger_manager *cm, bool enable)
244 struct charger_desc *desc = cm->desc;
246 /* Ignore if it's redundent command */
247 if (enable == cm->charger_enabled)
251 if (cm->emergency_stop)
253 err = regulator_bulk_enable(desc->num_charger_regulators,
254 desc->charger_regulators);
257 * Abnormal battery state - Stop charging forcibly,
258 * even if charger was enabled at the other places
260 err = regulator_bulk_disable(desc->num_charger_regulators,
261 desc->charger_regulators);
263 for (i = 0; i < desc->num_charger_regulators; i++) {
264 if (regulator_is_enabled(
265 desc->charger_regulators[i].consumer)) {
266 regulator_force_disable(
267 desc->charger_regulators[i].consumer);
269 "Disable regulator(%s) forcibly.\n",
270 desc->charger_regulators[i].supply);
276 cm->charger_enabled = enable;
282 * uevent_notify - Let users know something has changed.
283 * @cm: the Charger Manager representing the battery.
284 * @event: the event string.
286 * If @event is null, it implies that uevent_notify is called
287 * by resume function. When called in the resume function, cm_suspended
288 * should be already reset to false in order to let uevent_notify
289 * notify the recent event during the suspend to users. While
290 * suspended, uevent_notify does not notify users, but tracks
291 * events so that uevent_notify can notify users later after resumed.
293 static void uevent_notify(struct charger_manager *cm, const char *event)
295 static char env_str[UEVENT_BUF_SIZE + 1] = "";
296 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
299 /* Nothing in suspended-event buffer */
300 if (env_str_save[0] == 0) {
301 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
302 return; /* status not changed */
303 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
307 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
308 return; /* Duplicated. */
309 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
314 /* No messages pending */
315 if (!env_str_save[0])
318 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
319 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
325 /* status not changed */
326 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
329 /* save the status and notify the update */
330 strncpy(env_str, event, UEVENT_BUF_SIZE);
331 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
333 dev_info(cm->dev, event);
337 * _cm_monitor - Monitor the temperature and return true for exceptions.
338 * @cm: the Charger Manager representing the battery.
340 * Returns true if there is an event to notify for the battery.
341 * (True if the status of "emergency_stop" changes)
343 static bool _cm_monitor(struct charger_manager *cm)
345 struct charger_desc *desc = cm->desc;
346 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
348 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
349 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
351 /* It has been stopped or charging already */
352 if (!!temp == !!cm->emergency_stop)
356 cm->emergency_stop = temp;
357 if (!try_charger_enable(cm, false)) {
359 uevent_notify(cm, "OVERHEAT");
361 uevent_notify(cm, "COLD");
364 cm->emergency_stop = 0;
365 if (!try_charger_enable(cm, true))
366 uevent_notify(cm, "CHARGING");
373 * cm_monitor - Monitor every battery.
375 * Returns true if there is an event to notify from any of the batteries.
376 * (True if the status of "emergency_stop" changes)
378 static bool cm_monitor(void)
381 struct charger_manager *cm;
383 mutex_lock(&cm_list_mtx);
385 list_for_each_entry(cm, &cm_list, entry) {
390 mutex_unlock(&cm_list_mtx);
395 static int charger_get_property(struct power_supply *psy,
396 enum power_supply_property psp,
397 union power_supply_propval *val)
399 struct charger_manager *cm = container_of(psy,
400 struct charger_manager, charger_psy);
401 struct charger_desc *desc = cm->desc;
406 case POWER_SUPPLY_PROP_STATUS:
408 val->intval = POWER_SUPPLY_STATUS_CHARGING;
409 else if (is_ext_pwr_online(cm))
410 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
412 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
414 case POWER_SUPPLY_PROP_HEALTH:
415 if (cm->emergency_stop > 0)
416 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
417 else if (cm->emergency_stop < 0)
418 val->intval = POWER_SUPPLY_HEALTH_COLD;
420 val->intval = POWER_SUPPLY_HEALTH_GOOD;
422 case POWER_SUPPLY_PROP_PRESENT:
423 if (is_batt_present(cm))
428 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
429 ret = get_batt_uV(cm, &val->intval);
431 case POWER_SUPPLY_PROP_CURRENT_NOW:
432 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
433 POWER_SUPPLY_PROP_CURRENT_NOW, val);
435 case POWER_SUPPLY_PROP_TEMP:
436 /* in thenth of centigrade */
437 if (cm->last_temp_mC == INT_MIN)
438 desc->temperature_out_of_range(&cm->last_temp_mC);
439 val->intval = cm->last_temp_mC / 100;
440 if (!desc->measure_battery_temp)
443 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
444 /* in thenth of centigrade */
445 if (cm->last_temp_mC == INT_MIN)
446 desc->temperature_out_of_range(&cm->last_temp_mC);
447 val->intval = cm->last_temp_mC / 100;
448 if (desc->measure_battery_temp)
451 case POWER_SUPPLY_PROP_CAPACITY:
452 if (!cm->fuel_gauge) {
457 if (!is_batt_present(cm)) {
458 /* There is no battery. Assume 100% */
463 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
464 POWER_SUPPLY_PROP_CAPACITY, val);
468 if (val->intval > 100) {
475 /* Do not adjust SOC when charging: voltage is overrated */
480 * If the capacity value is inconsistent, calibrate it base on
481 * the battery voltage values and the thresholds given as desc
483 ret = get_batt_uV(cm, &uV);
485 /* Voltage information not available. No calibration */
490 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
497 case POWER_SUPPLY_PROP_ONLINE:
498 if (is_ext_pwr_online(cm))
503 case POWER_SUPPLY_PROP_CHARGE_FULL:
504 if (cm->fuel_gauge) {
505 if (cm->fuel_gauge->get_property(cm->fuel_gauge,
506 POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0)
510 if (is_ext_pwr_online(cm)) {
511 /* Not full if it's charging. */
512 if (is_charging(cm)) {
517 * Full if it's powered but not charging andi
518 * not forced stop by emergency
520 if (!cm->emergency_stop) {
526 /* Full if it's over the fullbatt voltage */
527 ret = get_batt_uV(cm, &uV);
528 if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
534 /* Full if the cap is 100 */
535 if (cm->fuel_gauge) {
536 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
537 POWER_SUPPLY_PROP_CAPACITY, val);
538 if (!ret && val->intval >= 100 && !is_charging(cm)) {
547 case POWER_SUPPLY_PROP_CHARGE_NOW:
548 if (is_charging(cm)) {
549 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
550 POWER_SUPPLY_PROP_CHARGE_NOW,
556 /* If CHARGE_NOW is supplied, use it */
557 val->intval = (val->intval > 0) ?
570 #define NUM_CHARGER_PSY_OPTIONAL (4)
571 static enum power_supply_property default_charger_props[] = {
572 /* Guaranteed to provide */
573 POWER_SUPPLY_PROP_STATUS,
574 POWER_SUPPLY_PROP_HEALTH,
575 POWER_SUPPLY_PROP_PRESENT,
576 POWER_SUPPLY_PROP_VOLTAGE_NOW,
577 POWER_SUPPLY_PROP_CAPACITY,
578 POWER_SUPPLY_PROP_ONLINE,
579 POWER_SUPPLY_PROP_CHARGE_FULL,
581 * Optional properties are:
582 * POWER_SUPPLY_PROP_CHARGE_NOW,
583 * POWER_SUPPLY_PROP_CURRENT_NOW,
584 * POWER_SUPPLY_PROP_TEMP, and
585 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
589 static struct power_supply psy_default = {
591 .type = POWER_SUPPLY_TYPE_BATTERY,
592 .properties = default_charger_props,
593 .num_properties = ARRAY_SIZE(default_charger_props),
594 .get_property = charger_get_property,
598 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
601 * Returns true if the alarm is set for Charger Manager to use.
603 * cm_setup_timer fails to set an alarm,
604 * cm_setup_timer does not need to set an alarm for Charger Manager,
605 * or an alarm previously configured is to be used.
607 static bool cm_setup_timer(void)
609 struct charger_manager *cm;
610 unsigned int wakeup_ms = UINT_MAX;
613 mutex_lock(&cm_list_mtx);
615 list_for_each_entry(cm, &cm_list, entry) {
616 /* Skip if polling is not required for this CM */
617 if (!is_polling_required(cm) && !cm->emergency_stop)
619 if (cm->desc->polling_interval_ms == 0)
621 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
624 mutex_unlock(&cm_list_mtx);
626 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
627 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
629 struct rtc_wkalrm tmp;
630 unsigned long time, now;
631 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
634 * Set alarm with the polling interval (wakeup_ms)
635 * except when rtc_wkalarm_save comes first.
636 * However, the alarm time should be NOW +
637 * CM_RTC_SMALL or later.
640 rtc_read_time(rtc_dev, &tmp.time);
641 rtc_tm_to_time(&tmp.time, &now);
642 if (add < CM_RTC_SMALL)
648 if (rtc_wkalarm_save.enabled &&
649 rtc_wkalarm_save_time &&
650 rtc_wkalarm_save_time < time) {
651 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
652 time = now + CM_RTC_SMALL;
654 time = rtc_wkalarm_save_time;
656 /* The timer is not appointed by CM */
660 pr_info("Waking up after %lu secs.\n",
663 rtc_time_to_tm(time, &tmp.time);
664 rtc_set_alarm(rtc_dev, &tmp);
665 cm_suspend_duration_ms += wakeup_ms;
671 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
676 * cm_suspend_again - Determine whether suspend again or not
678 * Returns true if the system should be suspended again
679 * Returns false if the system should be woken up
681 bool cm_suspend_again(void)
683 struct charger_manager *cm;
686 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
694 mutex_lock(&cm_list_mtx);
695 list_for_each_entry(cm, &cm_list, entry) {
696 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
697 cm->status_save_batt != is_batt_present(cm)) {
702 mutex_unlock(&cm_list_mtx);
704 cm_rtc_set = cm_setup_timer();
706 /* It's about the time when the non-CM appointed timer goes off */
707 if (rtc_wkalarm_save.enabled) {
711 rtc_read_time(rtc_dev, &tmp);
712 rtc_tm_to_time(&tmp, &now);
714 if (rtc_wkalarm_save_time &&
715 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
720 EXPORT_SYMBOL_GPL(cm_suspend_again);
723 * setup_charger_manager - initialize charger_global_desc data
724 * @gd: pointer to instance of charger_global_desc
726 int setup_charger_manager(struct charger_global_desc *gd)
732 rtc_class_close(rtc_dev);
736 if (!gd->rtc_only_wakeup) {
737 pr_err("The callback rtc_only_wakeup is not given.\n");
742 rtc_dev = rtc_class_open(gd->rtc_name);
743 if (IS_ERR_OR_NULL(rtc_dev)) {
745 /* Retry at probe. RTC may be not registered yet */
748 pr_warn("No wakeup timer is given for charger manager."
749 "In-suspend monitoring won't work.\n");
755 EXPORT_SYMBOL_GPL(setup_charger_manager);
757 static int charger_manager_probe(struct platform_device *pdev)
759 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
760 struct charger_manager *cm;
762 union power_supply_propval val;
764 if (g_desc && !rtc_dev && g_desc->rtc_name) {
765 rtc_dev = rtc_class_open(g_desc->rtc_name);
766 if (IS_ERR_OR_NULL(rtc_dev)) {
768 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
776 dev_err(&pdev->dev, "No platform data (desc) found.\n");
781 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
783 dev_err(&pdev->dev, "Cannot allocate memory.\n");
788 /* Basic Values. Unspecified are Null or 0 */
789 cm->dev = &pdev->dev;
790 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
792 dev_err(&pdev->dev, "Cannot allocate memory.\n");
796 memcpy(cm->desc, desc, sizeof(struct charger_desc));
797 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
799 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
801 dev_err(&pdev->dev, "charger_regulators undefined.\n");
805 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
806 dev_err(&pdev->dev, "No power supply defined.\n");
808 goto err_no_charger_stat;
811 /* Counting index only */
812 while (desc->psy_charger_stat[i])
815 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
817 if (!cm->charger_stat) {
819 goto err_no_charger_stat;
822 for (i = 0; desc->psy_charger_stat[i]; i++) {
823 cm->charger_stat[i] = power_supply_get_by_name(
824 desc->psy_charger_stat[i]);
825 if (!cm->charger_stat[i]) {
826 dev_err(&pdev->dev, "Cannot find power supply "
828 desc->psy_charger_stat[i]);
834 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
835 if (!cm->fuel_gauge) {
836 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
837 desc->psy_fuel_gauge);
842 if (desc->polling_interval_ms == 0 ||
843 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
844 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
849 if (!desc->temperature_out_of_range) {
850 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
855 platform_set_drvdata(pdev, cm);
857 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
859 if (!desc->psy_name) {
860 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
862 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
864 cm->charger_psy.name = cm->psy_name_buf;
866 /* Allocate for psy properties because they may vary */
867 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
868 * (ARRAY_SIZE(default_charger_props) +
869 NUM_CHARGER_PSY_OPTIONAL),
871 if (!cm->charger_psy.properties) {
872 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
876 memcpy(cm->charger_psy.properties, default_charger_props,
877 sizeof(enum power_supply_property) *
878 ARRAY_SIZE(default_charger_props));
879 cm->charger_psy.num_properties = psy_default.num_properties;
881 /* Find which optional psy-properties are available */
882 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
883 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
884 cm->charger_psy.properties[cm->charger_psy.num_properties] =
885 POWER_SUPPLY_PROP_CHARGE_NOW;
886 cm->charger_psy.num_properties++;
888 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
889 POWER_SUPPLY_PROP_CURRENT_NOW,
891 cm->charger_psy.properties[cm->charger_psy.num_properties] =
892 POWER_SUPPLY_PROP_CURRENT_NOW;
893 cm->charger_psy.num_properties++;
896 if (desc->measure_battery_temp) {
897 cm->charger_psy.properties[cm->charger_psy.num_properties] =
898 POWER_SUPPLY_PROP_TEMP;
899 cm->charger_psy.num_properties++;
901 cm->charger_psy.properties[cm->charger_psy.num_properties] =
902 POWER_SUPPLY_PROP_TEMP_AMBIENT;
903 cm->charger_psy.num_properties++;
906 ret = power_supply_register(NULL, &cm->charger_psy);
908 dev_err(&pdev->dev, "Cannot register charger-manager with"
909 " name \"%s\".\n", cm->charger_psy.name);
913 ret = regulator_bulk_get(&pdev->dev, desc->num_charger_regulators,
914 desc->charger_regulators);
916 dev_err(&pdev->dev, "Cannot get charger regulators.\n");
920 ret = try_charger_enable(cm, true);
922 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
926 /* Add to the list */
927 mutex_lock(&cm_list_mtx);
928 list_add(&cm->entry, &cm_list);
929 mutex_unlock(&cm_list_mtx);
934 regulator_bulk_free(desc->num_charger_regulators,
935 desc->charger_regulators);
937 power_supply_unregister(&cm->charger_psy);
939 kfree(cm->charger_psy.properties);
941 kfree(cm->charger_stat);
951 static int __devexit charger_manager_remove(struct platform_device *pdev)
953 struct charger_manager *cm = platform_get_drvdata(pdev);
954 struct charger_desc *desc = cm->desc;
956 /* Remove from the list */
957 mutex_lock(&cm_list_mtx);
958 list_del(&cm->entry);
959 mutex_unlock(&cm_list_mtx);
961 regulator_bulk_free(desc->num_charger_regulators,
962 desc->charger_regulators);
963 power_supply_unregister(&cm->charger_psy);
964 kfree(cm->charger_psy.properties);
965 kfree(cm->charger_stat);
972 static const struct platform_device_id charger_manager_id[] = {
973 { "charger-manager", 0 },
976 MODULE_DEVICE_TABLE(platform, charger_manager_id);
978 static int cm_suspend_prepare(struct device *dev)
980 struct charger_manager *cm = dev_get_drvdata(dev);
987 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
988 rtc_read_time(rtc_dev, &tmp);
990 if (rtc_wkalarm_save.enabled) {
991 rtc_tm_to_time(&rtc_wkalarm_save.time,
992 &rtc_wkalarm_save_time);
993 rtc_tm_to_time(&tmp, &now);
994 if (now > rtc_wkalarm_save_time)
995 rtc_wkalarm_save_time = 0;
997 rtc_wkalarm_save_time = 0;
1000 cm_suspended = true;
1003 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1004 cm->status_save_batt = is_batt_present(cm);
1007 cm_suspend_duration_ms = 0;
1008 cm_rtc_set = cm_setup_timer();
1014 static void cm_suspend_complete(struct device *dev)
1016 struct charger_manager *cm = dev_get_drvdata(dev);
1020 struct rtc_wkalrm tmp;
1022 rtc_read_alarm(rtc_dev, &tmp);
1023 rtc_wkalarm_save.pending = tmp.pending;
1024 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1026 cm_suspended = false;
1030 uevent_notify(cm, NULL);
1033 static const struct dev_pm_ops charger_manager_pm = {
1034 .prepare = cm_suspend_prepare,
1035 .complete = cm_suspend_complete,
1038 static struct platform_driver charger_manager_driver = {
1040 .name = "charger-manager",
1041 .owner = THIS_MODULE,
1042 .pm = &charger_manager_pm,
1044 .probe = charger_manager_probe,
1045 .remove = __devexit_p(charger_manager_remove),
1046 .id_table = charger_manager_id,
1049 static int __init charger_manager_init(void)
1051 return platform_driver_register(&charger_manager_driver);
1053 late_initcall(charger_manager_init);
1055 static void __exit charger_manager_cleanup(void)
1057 platform_driver_unregister(&charger_manager_driver);
1059 module_exit(charger_manager_cleanup);
1061 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1062 MODULE_DESCRIPTION("Charger Manager");
1063 MODULE_LICENSE("GPL");