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
;
138 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
139 POWER_SUPPLY_PROP_VOLTAGE_NOW
, &val
);
151 * is_charging - Returns true if the battery is being charged.
152 * @cm: the Charger Manager representing the battery.
154 static bool is_charging(struct charger_manager
*cm
)
157 bool charging
= false;
158 union power_supply_propval val
;
160 /* If there is no battery, it cannot be charged */
161 if (!is_batt_present(cm
))
164 /* If at least one of the charger is charging, return yes */
165 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
166 /* 1. The charger sholuld not be DISABLED */
167 if (cm
->emergency_stop
)
169 if (!cm
->charger_enabled
)
172 /* 2. The charger should be online (ext-power) */
173 ret
= cm
->charger_stat
[i
]->get_property(
175 POWER_SUPPLY_PROP_ONLINE
, &val
);
177 dev_warn(cm
->dev
, "Cannot read ONLINE value from %s.\n",
178 cm
->desc
->psy_charger_stat
[i
]);
185 * 3. The charger should not be FULL, DISCHARGING,
188 ret
= cm
->charger_stat
[i
]->get_property(
190 POWER_SUPPLY_PROP_STATUS
, &val
);
192 dev_warn(cm
->dev
, "Cannot read STATUS value from %s.\n",
193 cm
->desc
->psy_charger_stat
[i
]);
196 if (val
.intval
== POWER_SUPPLY_STATUS_FULL
||
197 val
.intval
== POWER_SUPPLY_STATUS_DISCHARGING
||
198 val
.intval
== POWER_SUPPLY_STATUS_NOT_CHARGING
)
201 /* Then, this is charging. */
210 * is_polling_required - Return true if need to continue polling for this CM.
211 * @cm: the Charger Manager representing the battery.
213 static bool is_polling_required(struct charger_manager
*cm
)
215 switch (cm
->desc
->polling_mode
) {
216 case CM_POLL_DISABLE
:
220 case CM_POLL_EXTERNAL_POWER_ONLY
:
221 return is_ext_pwr_online(cm
);
222 case CM_POLL_CHARGING_ONLY
:
223 return is_charging(cm
);
225 dev_warn(cm
->dev
, "Incorrect polling_mode (%d)\n",
226 cm
->desc
->polling_mode
);
233 * try_charger_enable - Enable/Disable chargers altogether
234 * @cm: the Charger Manager representing the battery.
235 * @enable: true: enable / false: disable
237 * Note that Charger Manager keeps the charger enabled regardless whether
238 * the charger is charging or not (because battery is full or no external
239 * power source exists) except when CM needs to disable chargers forcibly
240 * bacause of emergency causes; when the battery is overheated or too cold.
242 static int try_charger_enable(struct charger_manager
*cm
, bool enable
)
245 struct charger_desc
*desc
= cm
->desc
;
247 /* Ignore if it's redundent command */
248 if (enable
&& cm
->charger_enabled
)
250 if (!enable
&& !cm
->charger_enabled
)
254 if (cm
->emergency_stop
)
256 err
= regulator_bulk_enable(desc
->num_charger_regulators
,
257 desc
->charger_regulators
);
260 * Abnormal battery state - Stop charging forcibly,
261 * even if charger was enabled at the other places
263 err
= regulator_bulk_disable(desc
->num_charger_regulators
,
264 desc
->charger_regulators
);
266 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
267 if (regulator_is_enabled(
268 desc
->charger_regulators
[i
].consumer
)) {
269 regulator_force_disable(
270 desc
->charger_regulators
[i
].consumer
);
272 "Disable regulator(%s) forcibly.\n",
273 desc
->charger_regulators
[i
].supply
);
279 cm
->charger_enabled
= enable
;
285 * uevent_notify - Let users know something has changed.
286 * @cm: the Charger Manager representing the battery.
287 * @event: the event string.
289 * If @event is null, it implies that uevent_notify is called
290 * by resume function. When called in the resume function, cm_suspended
291 * should be already reset to false in order to let uevent_notify
292 * notify the recent event during the suspend to users. While
293 * suspended, uevent_notify does not notify users, but tracks
294 * events so that uevent_notify can notify users later after resumed.
296 static void uevent_notify(struct charger_manager
*cm
, const char *event
)
298 static char env_str
[UEVENT_BUF_SIZE
+ 1] = "";
299 static char env_str_save
[UEVENT_BUF_SIZE
+ 1] = "";
302 /* Nothing in suspended-event buffer */
303 if (env_str_save
[0] == 0) {
304 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
305 return; /* status not changed */
306 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
310 if (!strncmp(env_str_save
, event
, UEVENT_BUF_SIZE
))
311 return; /* Duplicated. */
313 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
319 /* No messages pending */
320 if (!env_str_save
[0])
323 strncpy(env_str
, env_str_save
, UEVENT_BUF_SIZE
);
324 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
330 /* status not changed */
331 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
334 /* save the status and notify the update */
335 strncpy(env_str
, event
, UEVENT_BUF_SIZE
);
336 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
338 dev_info(cm
->dev
, event
);
342 * _cm_monitor - Monitor the temperature and return true for exceptions.
343 * @cm: the Charger Manager representing the battery.
345 * Returns true if there is an event to notify for the battery.
346 * (True if the status of "emergency_stop" changes)
348 static bool _cm_monitor(struct charger_manager
*cm
)
350 struct charger_desc
*desc
= cm
->desc
;
351 int temp
= desc
->temperature_out_of_range(&cm
->last_temp_mC
);
353 dev_dbg(cm
->dev
, "monitoring (%2.2d.%3.3dC)\n",
354 cm
->last_temp_mC
/ 1000, cm
->last_temp_mC
% 1000);
356 /* It has been stopped or charging already */
357 if (!!temp
== !!cm
->emergency_stop
)
361 cm
->emergency_stop
= temp
;
362 if (!try_charger_enable(cm
, false)) {
364 uevent_notify(cm
, "OVERHEAT");
366 uevent_notify(cm
, "COLD");
369 cm
->emergency_stop
= 0;
370 if (!try_charger_enable(cm
, true))
371 uevent_notify(cm
, "CHARGING");
378 * cm_monitor - Monitor every battery.
380 * Returns true if there is an event to notify from any of the batteries.
381 * (True if the status of "emergency_stop" changes)
383 static bool cm_monitor(void)
386 struct charger_manager
*cm
;
388 mutex_lock(&cm_list_mtx
);
390 list_for_each_entry(cm
, &cm_list
, entry
)
391 stop
= stop
|| _cm_monitor(cm
);
393 mutex_unlock(&cm_list_mtx
);
398 static int charger_get_property(struct power_supply
*psy
,
399 enum power_supply_property psp
,
400 union power_supply_propval
*val
)
402 struct charger_manager
*cm
= container_of(psy
,
403 struct charger_manager
, charger_psy
);
404 struct charger_desc
*desc
= cm
->desc
;
408 case POWER_SUPPLY_PROP_STATUS
:
410 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
411 else if (is_ext_pwr_online(cm
))
412 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
414 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
416 case POWER_SUPPLY_PROP_HEALTH
:
417 if (cm
->emergency_stop
> 0)
418 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
419 else if (cm
->emergency_stop
< 0)
420 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
422 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
424 case POWER_SUPPLY_PROP_PRESENT
:
425 if (is_batt_present(cm
))
430 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
431 ret
= get_batt_uV(cm
, &i
);
434 case POWER_SUPPLY_PROP_CURRENT_NOW
:
435 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
436 POWER_SUPPLY_PROP_CURRENT_NOW
, val
);
438 case POWER_SUPPLY_PROP_TEMP
:
439 /* in thenth of centigrade */
440 if (cm
->last_temp_mC
== INT_MIN
)
441 desc
->temperature_out_of_range(&cm
->last_temp_mC
);
442 val
->intval
= cm
->last_temp_mC
/ 100;
443 if (!desc
->measure_battery_temp
)
446 case POWER_SUPPLY_PROP_TEMP_AMBIENT
:
447 /* in thenth of centigrade */
448 if (cm
->last_temp_mC
== INT_MIN
)
449 desc
->temperature_out_of_range(&cm
->last_temp_mC
);
450 val
->intval
= cm
->last_temp_mC
/ 100;
451 if (desc
->measure_battery_temp
)
454 case POWER_SUPPLY_PROP_CAPACITY
:
455 if (!cm
->fuel_gauge
) {
460 if (!is_batt_present(cm
)) {
461 /* There is no battery. Assume 100% */
466 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
467 POWER_SUPPLY_PROP_CAPACITY
, val
);
471 if (val
->intval
> 100) {
478 /* Do not adjust SOC when charging: voltage is overrated */
483 * If the capacity value is inconsistent, calibrate it base on
484 * the battery voltage values and the thresholds given as desc
486 ret
= get_batt_uV(cm
, &uV
);
488 /* Voltage information not available. No calibration */
493 if (desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
500 case POWER_SUPPLY_PROP_ONLINE
:
501 if (is_ext_pwr_online(cm
))
506 case POWER_SUPPLY_PROP_CHARGE_FULL
:
507 if (cm
->fuel_gauge
) {
508 if (cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
509 POWER_SUPPLY_PROP_CHARGE_FULL
, val
) == 0)
513 if (is_ext_pwr_online(cm
)) {
514 /* Not full if it's charging. */
515 if (is_charging(cm
)) {
520 * Full if it's powered but not charging andi
521 * not forced stop by emergency
523 if (!cm
->emergency_stop
) {
529 /* Full if it's over the fullbatt voltage */
530 ret
= get_batt_uV(cm
, &uV
);
531 if (!ret
&& desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
537 /* Full if the cap is 100 */
538 if (cm
->fuel_gauge
) {
539 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
540 POWER_SUPPLY_PROP_CAPACITY
, val
);
541 if (!ret
&& val
->intval
>= 100 && !is_charging(cm
)) {
550 case POWER_SUPPLY_PROP_CHARGE_NOW
:
551 if (is_charging(cm
)) {
552 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
553 POWER_SUPPLY_PROP_CHARGE_NOW
,
559 /* If CHARGE_NOW is supplied, use it */
560 val
->intval
= (val
->intval
> 0) ?
573 #define NUM_CHARGER_PSY_OPTIONAL (4)
574 static enum power_supply_property default_charger_props
[] = {
575 /* Guaranteed to provide */
576 POWER_SUPPLY_PROP_STATUS
,
577 POWER_SUPPLY_PROP_HEALTH
,
578 POWER_SUPPLY_PROP_PRESENT
,
579 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
580 POWER_SUPPLY_PROP_CAPACITY
,
581 POWER_SUPPLY_PROP_ONLINE
,
582 POWER_SUPPLY_PROP_CHARGE_FULL
,
584 * Optional properties are:
585 * POWER_SUPPLY_PROP_CHARGE_NOW,
586 * POWER_SUPPLY_PROP_CURRENT_NOW,
587 * POWER_SUPPLY_PROP_TEMP, and
588 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
592 static struct power_supply psy_default
= {
594 .type
= POWER_SUPPLY_TYPE_BATTERY
,
595 .properties
= default_charger_props
,
596 .num_properties
= ARRAY_SIZE(default_charger_props
),
597 .get_property
= charger_get_property
,
601 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
604 * Returns true if the alarm is set for Charger Manager to use.
606 * cm_setup_timer fails to set an alarm,
607 * cm_setup_timer does not need to set an alarm for Charger Manager,
608 * or an alarm previously configured is to be used.
610 static bool cm_setup_timer(void)
612 struct charger_manager
*cm
;
613 unsigned int wakeup_ms
= UINT_MAX
;
616 mutex_lock(&cm_list_mtx
);
618 list_for_each_entry(cm
, &cm_list
, entry
) {
619 /* Skip if polling is not required for this CM */
620 if (!is_polling_required(cm
) && !cm
->emergency_stop
)
622 if (cm
->desc
->polling_interval_ms
== 0)
624 CM_MIN_VALID(wakeup_ms
, cm
->desc
->polling_interval_ms
);
627 mutex_unlock(&cm_list_mtx
);
629 if (wakeup_ms
< UINT_MAX
&& wakeup_ms
> 0) {
630 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms
);
632 struct rtc_wkalrm tmp
;
633 unsigned long time
, now
;
634 unsigned long add
= DIV_ROUND_UP(wakeup_ms
, 1000);
637 * Set alarm with the polling interval (wakeup_ms)
638 * except when rtc_wkalarm_save comes first.
639 * However, the alarm time should be NOW +
640 * CM_RTC_SMALL or later.
643 rtc_read_time(rtc_dev
, &tmp
.time
);
644 rtc_tm_to_time(&tmp
.time
, &now
);
645 if (add
< CM_RTC_SMALL
)
651 if (rtc_wkalarm_save
.enabled
&&
652 rtc_wkalarm_save_time
&&
653 rtc_wkalarm_save_time
< time
) {
654 if (rtc_wkalarm_save_time
< now
+ CM_RTC_SMALL
)
655 time
= now
+ CM_RTC_SMALL
;
657 time
= rtc_wkalarm_save_time
;
659 /* The timer is not appointed by CM */
663 pr_info("Waking up after %lu secs.\n",
666 rtc_time_to_tm(time
, &tmp
.time
);
667 rtc_set_alarm(rtc_dev
, &tmp
);
668 cm_suspend_duration_ms
+= wakeup_ms
;
674 rtc_set_alarm(rtc_dev
, &rtc_wkalarm_save
);
679 * cm_suspend_again - Determine whether suspend again or not
681 * Returns true if the system should be suspended again
682 * Returns false if the system should be woken up
684 bool cm_suspend_again(void)
686 struct charger_manager
*cm
;
689 if (!g_desc
|| !g_desc
->rtc_only_wakeup
|| !g_desc
->rtc_only_wakeup() ||
697 mutex_lock(&cm_list_mtx
);
698 list_for_each_entry(cm
, &cm_list
, entry
) {
699 if (cm
->status_save_ext_pwr_inserted
!= is_ext_pwr_online(cm
) ||
700 cm
->status_save_batt
!= is_batt_present(cm
))
703 mutex_unlock(&cm_list_mtx
);
705 cm_rtc_set
= cm_setup_timer();
707 /* It's about the time when the non-CM appointed timer goes off */
708 if (rtc_wkalarm_save
.enabled
) {
712 rtc_read_time(rtc_dev
, &tmp
);
713 rtc_tm_to_time(&tmp
, &now
);
715 if (rtc_wkalarm_save_time
&&
716 now
+ CM_RTC_SMALL
>= rtc_wkalarm_save_time
)
721 EXPORT_SYMBOL_GPL(cm_suspend_again
);
724 * setup_charger_manager - initialize charger_global_desc data
725 * @gd: pointer to instance of charger_global_desc
727 int setup_charger_manager(struct charger_global_desc
*gd
)
733 rtc_class_close(rtc_dev
);
737 if (!gd
->rtc_only_wakeup
) {
738 pr_err("The callback rtc_only_wakeup is not given.\n");
743 rtc_dev
= rtc_class_open(gd
->rtc_name
);
744 if (IS_ERR_OR_NULL(rtc_dev
)) {
746 /* Retry at probe. RTC may be not registered yet */
749 pr_warn("No wakeup timer is given for charger manager."
750 "In-suspend monitoring won't work.\n");
756 EXPORT_SYMBOL_GPL(setup_charger_manager
);
758 static int charger_manager_probe(struct platform_device
*pdev
)
760 struct charger_desc
*desc
= dev_get_platdata(&pdev
->dev
);
761 struct charger_manager
*cm
;
763 union power_supply_propval val
;
765 if (g_desc
&& !rtc_dev
&& g_desc
->rtc_name
) {
766 rtc_dev
= rtc_class_open(g_desc
->rtc_name
);
767 if (IS_ERR_OR_NULL(rtc_dev
)) {
769 dev_err(&pdev
->dev
, "Cannot get RTC %s.\n",
777 dev_err(&pdev
->dev
, "No platform data (desc) found.\n");
782 cm
= kzalloc(sizeof(struct charger_manager
), GFP_KERNEL
);
784 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
789 /* Basic Values. Unspecified are Null or 0 */
790 cm
->dev
= &pdev
->dev
;
791 cm
->desc
= kzalloc(sizeof(struct charger_desc
), GFP_KERNEL
);
793 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
797 memcpy(cm
->desc
, desc
, sizeof(struct charger_desc
));
798 cm
->last_temp_mC
= INT_MIN
; /* denotes "unmeasured, yet" */
800 if (!desc
->charger_regulators
|| desc
->num_charger_regulators
< 1) {
802 dev_err(&pdev
->dev
, "charger_regulators undefined.\n");
806 if (!desc
->psy_charger_stat
|| !desc
->psy_charger_stat
[0]) {
807 dev_err(&pdev
->dev
, "No power supply defined.\n");
809 goto err_no_charger_stat
;
812 /* Counting index only */
813 while (desc
->psy_charger_stat
[i
])
816 cm
->charger_stat
= kzalloc(sizeof(struct power_supply
*) * (i
+ 1),
818 if (!cm
->charger_stat
) {
820 goto err_no_charger_stat
;
823 for (i
= 0; desc
->psy_charger_stat
[i
]; i
++) {
824 cm
->charger_stat
[i
] = power_supply_get_by_name(
825 desc
->psy_charger_stat
[i
]);
826 if (!cm
->charger_stat
[i
]) {
827 dev_err(&pdev
->dev
, "Cannot find power supply "
829 desc
->psy_charger_stat
[i
]);
835 cm
->fuel_gauge
= power_supply_get_by_name(desc
->psy_fuel_gauge
);
836 if (!cm
->fuel_gauge
) {
837 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
838 desc
->psy_fuel_gauge
);
843 if (desc
->polling_interval_ms
== 0 ||
844 msecs_to_jiffies(desc
->polling_interval_ms
) <= CM_JIFFIES_SMALL
) {
845 dev_err(&pdev
->dev
, "polling_interval_ms is too small\n");
850 if (!desc
->temperature_out_of_range
) {
851 dev_err(&pdev
->dev
, "there is no temperature_out_of_range\n");
856 platform_set_drvdata(pdev
, cm
);
858 memcpy(&cm
->charger_psy
, &psy_default
,
859 sizeof(psy_default
));
860 if (!desc
->psy_name
) {
861 strncpy(cm
->psy_name_buf
, psy_default
.name
,
864 strncpy(cm
->psy_name_buf
, desc
->psy_name
, PSY_NAME_MAX
);
866 cm
->charger_psy
.name
= cm
->psy_name_buf
;
868 /* Allocate for psy properties because they may vary */
869 cm
->charger_psy
.properties
= kzalloc(sizeof(enum power_supply_property
)
870 * (ARRAY_SIZE(default_charger_props
) +
871 NUM_CHARGER_PSY_OPTIONAL
),
873 if (!cm
->charger_psy
.properties
) {
874 dev_err(&pdev
->dev
, "Cannot allocate for psy properties.\n");
878 memcpy(cm
->charger_psy
.properties
, default_charger_props
,
879 sizeof(enum power_supply_property
) *
880 ARRAY_SIZE(default_charger_props
));
881 cm
->charger_psy
.num_properties
= psy_default
.num_properties
;
883 /* Find which optional psy-properties are available */
884 if (!cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
885 POWER_SUPPLY_PROP_CHARGE_NOW
, &val
)) {
886 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
887 POWER_SUPPLY_PROP_CHARGE_NOW
;
888 cm
->charger_psy
.num_properties
++;
890 if (!cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
891 POWER_SUPPLY_PROP_CURRENT_NOW
,
893 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
894 POWER_SUPPLY_PROP_CURRENT_NOW
;
895 cm
->charger_psy
.num_properties
++;
897 if (!desc
->measure_battery_temp
) {
898 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
899 POWER_SUPPLY_PROP_TEMP_AMBIENT
;
900 cm
->charger_psy
.num_properties
++;
902 if (desc
->measure_battery_temp
) {
903 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
904 POWER_SUPPLY_PROP_TEMP
;
905 cm
->charger_psy
.num_properties
++;
908 ret
= power_supply_register(NULL
, &cm
->charger_psy
);
910 dev_err(&pdev
->dev
, "Cannot register charger-manager with"
911 " name \"%s\".\n", cm
->charger_psy
.name
);
915 ret
= regulator_bulk_get(&pdev
->dev
, desc
->num_charger_regulators
,
916 desc
->charger_regulators
);
918 dev_err(&pdev
->dev
, "Cannot get charger regulators.\n");
922 ret
= try_charger_enable(cm
, true);
924 dev_err(&pdev
->dev
, "Cannot enable charger regulators\n");
928 /* Add to the list */
929 mutex_lock(&cm_list_mtx
);
930 list_add(&cm
->entry
, &cm_list
);
931 mutex_unlock(&cm_list_mtx
);
936 if (desc
->charger_regulators
)
937 regulator_bulk_free(desc
->num_charger_regulators
,
938 desc
->charger_regulators
);
940 power_supply_unregister(&cm
->charger_psy
);
942 kfree(cm
->charger_psy
.properties
);
944 kfree(cm
->charger_stat
);
954 static int __devexit
charger_manager_remove(struct platform_device
*pdev
)
956 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
957 struct charger_desc
*desc
= cm
->desc
;
959 /* Remove from the list */
960 mutex_lock(&cm_list_mtx
);
961 list_del(&cm
->entry
);
962 mutex_unlock(&cm_list_mtx
);
964 if (desc
->charger_regulators
)
965 regulator_bulk_free(desc
->num_charger_regulators
,
966 desc
->charger_regulators
);
968 power_supply_unregister(&cm
->charger_psy
);
969 kfree(cm
->charger_psy
.properties
);
970 kfree(cm
->charger_stat
);
977 static const struct platform_device_id charger_manager_id
[] = {
978 { "charger-manager", 0 },
981 MODULE_DEVICE_TABLE(platform
, charger_manager_id
);
983 static int cm_suspend_prepare(struct device
*dev
)
985 struct platform_device
*pdev
= container_of(dev
, struct platform_device
,
987 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
994 rtc_read_alarm(rtc_dev
, &rtc_wkalarm_save
);
995 rtc_read_time(rtc_dev
, &tmp
);
997 if (rtc_wkalarm_save
.enabled
) {
998 rtc_tm_to_time(&rtc_wkalarm_save
.time
,
999 &rtc_wkalarm_save_time
);
1000 rtc_tm_to_time(&tmp
, &now
);
1001 if (now
> rtc_wkalarm_save_time
)
1002 rtc_wkalarm_save_time
= 0;
1004 rtc_wkalarm_save_time
= 0;
1007 cm_suspended
= true;
1010 cm
->status_save_ext_pwr_inserted
= is_ext_pwr_online(cm
);
1011 cm
->status_save_batt
= is_batt_present(cm
);
1014 cm_suspend_duration_ms
= 0;
1015 cm_rtc_set
= cm_setup_timer();
1021 static void cm_suspend_complete(struct device
*dev
)
1023 struct platform_device
*pdev
= container_of(dev
, struct platform_device
,
1025 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
1029 struct rtc_wkalrm tmp
;
1031 rtc_read_alarm(rtc_dev
, &tmp
);
1032 rtc_wkalarm_save
.pending
= tmp
.pending
;
1033 rtc_set_alarm(rtc_dev
, &rtc_wkalarm_save
);
1035 cm_suspended
= false;
1039 uevent_notify(cm
, NULL
);
1042 static const struct dev_pm_ops charger_manager_pm
= {
1043 .prepare
= cm_suspend_prepare
,
1044 .complete
= cm_suspend_complete
,
1047 static struct platform_driver charger_manager_driver
= {
1049 .name
= "charger-manager",
1050 .owner
= THIS_MODULE
,
1051 .pm
= &charger_manager_pm
,
1053 .probe
= charger_manager_probe
,
1054 .remove
= __devexit_p(charger_manager_remove
),
1055 .id_table
= charger_manager_id
,
1058 static int __init
charger_manager_init(void)
1060 return platform_driver_register(&charger_manager_driver
);
1062 late_initcall(charger_manager_init
);
1064 static void __exit
charger_manager_cleanup(void)
1066 platform_driver_unregister(&charger_manager_driver
);
1068 module_exit(charger_manager_cleanup
);
1070 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1071 MODULE_DESCRIPTION("Charger Manager");
1072 MODULE_LICENSE("GPL");