2 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
3 * MyungJoo Ham <myungjoo.ham@samsung.com>
5 * This driver enables to monitor battery health and control charger
6 * during suspend-to-mem.
7 * Charger manager depends on other devices. register this later than
8 * the depending devices.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/irq.h>
18 #include <linux/interrupt.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
22 #include <linux/platform_device.h>
23 #include <linux/power/charger-manager.h>
24 #include <linux/regulator/consumer.h>
26 static const char * const default_event_names
[] = {
27 [CM_EVENT_UNKNOWN
] = "Unknown",
28 [CM_EVENT_BATT_FULL
] = "Battery Full",
29 [CM_EVENT_BATT_IN
] = "Battery Inserted",
30 [CM_EVENT_BATT_OUT
] = "Battery Pulled Out",
31 [CM_EVENT_EXT_PWR_IN_OUT
] = "External Power Attach/Detach",
32 [CM_EVENT_CHG_START_STOP
] = "Charging Start/Stop",
33 [CM_EVENT_OTHERS
] = "Other battery events"
37 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
38 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
41 #define CM_JIFFIES_SMALL (2)
43 /* If y is valid (> 0) and smaller than x, do x = y */
44 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
47 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
48 * rtc alarm. It should be 2 or larger
50 #define CM_RTC_SMALL (2)
52 #define UEVENT_BUF_SIZE 32
54 static LIST_HEAD(cm_list
);
55 static DEFINE_MUTEX(cm_list_mtx
);
57 /* About in-suspend (suspend-again) monitoring */
58 static struct rtc_device
*rtc_dev
;
61 * Save the wakeup alarm before entering suspend-to-RAM
63 static struct rtc_wkalrm rtc_wkalarm_save
;
64 /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
65 static unsigned long rtc_wkalarm_save_time
;
66 static bool cm_suspended
;
67 static bool cm_rtc_set
;
68 static unsigned long cm_suspend_duration_ms
;
70 /* About normal (not suspended) monitoring */
71 static unsigned long polling_jiffy
= ULONG_MAX
; /* ULONG_MAX: no polling */
72 static unsigned long next_polling
; /* Next appointed polling time */
73 static struct workqueue_struct
*cm_wq
; /* init at driver add */
74 static struct delayed_work cm_monitor_work
; /* init at driver add */
76 /* Global charger-manager description */
77 static struct charger_global_desc
*g_desc
; /* init with setup_charger_manager */
80 * is_batt_present - See if the battery presents in place.
81 * @cm: the Charger Manager representing the battery.
83 static bool is_batt_present(struct charger_manager
*cm
)
85 union power_supply_propval val
;
89 switch (cm
->desc
->battery_present
) {
90 case CM_BATTERY_PRESENT
:
96 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
97 POWER_SUPPLY_PROP_PRESENT
, &val
);
98 if (ret
== 0 && val
.intval
)
101 case CM_CHARGER_STAT
:
102 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
103 ret
= cm
->charger_stat
[i
]->get_property(
105 POWER_SUPPLY_PROP_PRESENT
, &val
);
106 if (ret
== 0 && val
.intval
) {
118 * is_ext_pwr_online - See if an external power source is attached to charge
119 * @cm: the Charger Manager representing the battery.
121 * Returns true if at least one of the chargers of the battery has an external
122 * power source attached to charge the battery regardless of whether it is
123 * actually charging or not.
125 static bool is_ext_pwr_online(struct charger_manager
*cm
)
127 union power_supply_propval val
;
131 /* If at least one of them has one, it's yes. */
132 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
133 ret
= cm
->charger_stat
[i
]->get_property(
135 POWER_SUPPLY_PROP_ONLINE
, &val
);
136 if (ret
== 0 && val
.intval
) {
146 * get_batt_uV - Get the voltage level of the battery
147 * @cm: the Charger Manager representing the battery.
148 * @uV: the voltage level returned.
150 * Returns 0 if there is no error.
151 * Returns a negative value on error.
153 static int get_batt_uV(struct charger_manager
*cm
, int *uV
)
155 union power_supply_propval val
;
161 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
162 POWER_SUPPLY_PROP_VOLTAGE_NOW
, &val
);
171 * is_charging - Returns true if the battery is being charged.
172 * @cm: the Charger Manager representing the battery.
174 static bool is_charging(struct charger_manager
*cm
)
177 bool charging
= false;
178 union power_supply_propval val
;
180 /* If there is no battery, it cannot be charged */
181 if (!is_batt_present(cm
))
184 /* If at least one of the charger is charging, return yes */
185 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
186 /* 1. The charger sholuld not be DISABLED */
187 if (cm
->emergency_stop
)
189 if (!cm
->charger_enabled
)
192 /* 2. The charger should be online (ext-power) */
193 ret
= cm
->charger_stat
[i
]->get_property(
195 POWER_SUPPLY_PROP_ONLINE
, &val
);
197 dev_warn(cm
->dev
, "Cannot read ONLINE value from %s.\n",
198 cm
->desc
->psy_charger_stat
[i
]);
205 * 3. The charger should not be FULL, DISCHARGING,
208 ret
= cm
->charger_stat
[i
]->get_property(
210 POWER_SUPPLY_PROP_STATUS
, &val
);
212 dev_warn(cm
->dev
, "Cannot read STATUS value from %s.\n",
213 cm
->desc
->psy_charger_stat
[i
]);
216 if (val
.intval
== POWER_SUPPLY_STATUS_FULL
||
217 val
.intval
== POWER_SUPPLY_STATUS_DISCHARGING
||
218 val
.intval
== POWER_SUPPLY_STATUS_NOT_CHARGING
)
221 /* Then, this is charging. */
230 * is_polling_required - Return true if need to continue polling for this CM.
231 * @cm: the Charger Manager representing the battery.
233 static bool is_polling_required(struct charger_manager
*cm
)
235 switch (cm
->desc
->polling_mode
) {
236 case CM_POLL_DISABLE
:
240 case CM_POLL_EXTERNAL_POWER_ONLY
:
241 return is_ext_pwr_online(cm
);
242 case CM_POLL_CHARGING_ONLY
:
243 return is_charging(cm
);
245 dev_warn(cm
->dev
, "Incorrect polling_mode (%d)\n",
246 cm
->desc
->polling_mode
);
253 * try_charger_enable - Enable/Disable chargers altogether
254 * @cm: the Charger Manager representing the battery.
255 * @enable: true: enable / false: disable
257 * Note that Charger Manager keeps the charger enabled regardless whether
258 * the charger is charging or not (because battery is full or no external
259 * power source exists) except when CM needs to disable chargers forcibly
260 * bacause of emergency causes; when the battery is overheated or too cold.
262 static int try_charger_enable(struct charger_manager
*cm
, bool enable
)
265 struct charger_desc
*desc
= cm
->desc
;
267 /* Ignore if it's redundent command */
268 if (enable
== cm
->charger_enabled
)
272 if (cm
->emergency_stop
)
274 err
= regulator_bulk_enable(desc
->num_charger_regulators
,
275 desc
->charger_regulators
);
278 * Abnormal battery state - Stop charging forcibly,
279 * even if charger was enabled at the other places
281 err
= regulator_bulk_disable(desc
->num_charger_regulators
,
282 desc
->charger_regulators
);
284 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
285 if (regulator_is_enabled(
286 desc
->charger_regulators
[i
].consumer
)) {
287 regulator_force_disable(
288 desc
->charger_regulators
[i
].consumer
);
290 "Disable regulator(%s) forcibly.\n",
291 desc
->charger_regulators
[i
].supply
);
297 cm
->charger_enabled
= enable
;
303 * try_charger_restart - Restart charging.
304 * @cm: the Charger Manager representing the battery.
306 * Restart charging by turning off and on the charger.
308 static int try_charger_restart(struct charger_manager
*cm
)
312 if (cm
->emergency_stop
)
315 err
= try_charger_enable(cm
, false);
319 return try_charger_enable(cm
, true);
323 * uevent_notify - Let users know something has changed.
324 * @cm: the Charger Manager representing the battery.
325 * @event: the event string.
327 * If @event is null, it implies that uevent_notify is called
328 * by resume function. When called in the resume function, cm_suspended
329 * should be already reset to false in order to let uevent_notify
330 * notify the recent event during the suspend to users. While
331 * suspended, uevent_notify does not notify users, but tracks
332 * events so that uevent_notify can notify users later after resumed.
334 static void uevent_notify(struct charger_manager
*cm
, const char *event
)
336 static char env_str
[UEVENT_BUF_SIZE
+ 1] = "";
337 static char env_str_save
[UEVENT_BUF_SIZE
+ 1] = "";
340 /* Nothing in suspended-event buffer */
341 if (env_str_save
[0] == 0) {
342 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
343 return; /* status not changed */
344 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
348 if (!strncmp(env_str_save
, event
, UEVENT_BUF_SIZE
))
349 return; /* Duplicated. */
350 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
355 /* No messages pending */
356 if (!env_str_save
[0])
359 strncpy(env_str
, env_str_save
, UEVENT_BUF_SIZE
);
360 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
366 /* status not changed */
367 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
370 /* save the status and notify the update */
371 strncpy(env_str
, event
, UEVENT_BUF_SIZE
);
372 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
374 dev_info(cm
->dev
, event
);
378 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
379 * @work: the work_struct appointing the function
381 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
382 * charger_desc, Charger Manager checks voltage drop after the battery
383 * "FULL" event. It checks whether the voltage has dropped more than
384 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
386 static void fullbatt_vchk(struct work_struct
*work
)
388 struct delayed_work
*dwork
= to_delayed_work(work
);
389 struct charger_manager
*cm
= container_of(dwork
,
390 struct charger_manager
, fullbatt_vchk_work
);
391 struct charger_desc
*desc
= cm
->desc
;
392 int batt_uV
, err
, diff
;
394 /* remove the appointment for fullbatt_vchk */
395 cm
->fullbatt_vchk_jiffies_at
= 0;
397 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
400 err
= get_batt_uV(cm
, &batt_uV
);
402 dev_err(cm
->dev
, "%s: get_batt_uV error(%d).\n", __func__
, err
);
406 diff
= cm
->fullbatt_vchk_uV
;
409 dev_dbg(cm
->dev
, "VBATT dropped %duV after full-batt.\n", diff
);
411 if (diff
> desc
->fullbatt_vchkdrop_uV
) {
412 try_charger_restart(cm
);
413 uevent_notify(cm
, "Recharge");
418 * _cm_monitor - Monitor the temperature and return true for exceptions.
419 * @cm: the Charger Manager representing the battery.
421 * Returns true if there is an event to notify for the battery.
422 * (True if the status of "emergency_stop" changes)
424 static bool _cm_monitor(struct charger_manager
*cm
)
426 struct charger_desc
*desc
= cm
->desc
;
427 int temp
= desc
->temperature_out_of_range(&cm
->last_temp_mC
);
429 dev_dbg(cm
->dev
, "monitoring (%2.2d.%3.3dC)\n",
430 cm
->last_temp_mC
/ 1000, cm
->last_temp_mC
% 1000);
432 /* It has been stopped or charging already */
433 if (!!temp
== !!cm
->emergency_stop
)
437 cm
->emergency_stop
= temp
;
438 if (!try_charger_enable(cm
, false)) {
440 uevent_notify(cm
, "OVERHEAT");
442 uevent_notify(cm
, "COLD");
445 cm
->emergency_stop
= 0;
446 if (!try_charger_enable(cm
, true))
447 uevent_notify(cm
, "CHARGING");
454 * cm_monitor - Monitor every battery.
456 * Returns true if there is an event to notify from any of the batteries.
457 * (True if the status of "emergency_stop" changes)
459 static bool cm_monitor(void)
462 struct charger_manager
*cm
;
464 mutex_lock(&cm_list_mtx
);
466 list_for_each_entry(cm
, &cm_list
, entry
) {
471 mutex_unlock(&cm_list_mtx
);
477 * _setup_polling - Setup the next instance of polling.
478 * @work: work_struct of the function _setup_polling.
480 static void _setup_polling(struct work_struct
*work
)
482 unsigned long min
= ULONG_MAX
;
483 struct charger_manager
*cm
;
484 bool keep_polling
= false;
485 unsigned long _next_polling
;
487 mutex_lock(&cm_list_mtx
);
489 list_for_each_entry(cm
, &cm_list
, entry
) {
490 if (is_polling_required(cm
) && cm
->desc
->polling_interval_ms
) {
493 if (min
> cm
->desc
->polling_interval_ms
)
494 min
= cm
->desc
->polling_interval_ms
;
498 polling_jiffy
= msecs_to_jiffies(min
);
499 if (polling_jiffy
<= CM_JIFFIES_SMALL
)
500 polling_jiffy
= CM_JIFFIES_SMALL
+ 1;
503 polling_jiffy
= ULONG_MAX
;
504 if (polling_jiffy
== ULONG_MAX
)
507 WARN(cm_wq
== NULL
, "charger-manager: workqueue not initialized"
508 ". try it later. %s\n", __func__
);
510 _next_polling
= jiffies
+ polling_jiffy
;
512 if (!delayed_work_pending(&cm_monitor_work
) ||
513 (delayed_work_pending(&cm_monitor_work
) &&
514 time_after(next_polling
, _next_polling
))) {
515 cancel_delayed_work_sync(&cm_monitor_work
);
516 next_polling
= jiffies
+ polling_jiffy
;
517 queue_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
);
521 mutex_unlock(&cm_list_mtx
);
523 static DECLARE_WORK(setup_polling
, _setup_polling
);
526 * cm_monitor_poller - The Monitor / Poller.
527 * @work: work_struct of the function cm_monitor_poller
529 * During non-suspended state, cm_monitor_poller is used to poll and monitor
532 static void cm_monitor_poller(struct work_struct
*work
)
535 schedule_work(&setup_polling
);
539 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
540 * @cm: the Charger Manager representing the battery.
542 static void fullbatt_handler(struct charger_manager
*cm
)
544 struct charger_desc
*desc
= cm
->desc
;
546 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
550 device_set_wakeup_capable(cm
->dev
, true);
552 if (delayed_work_pending(&cm
->fullbatt_vchk_work
))
553 cancel_delayed_work(&cm
->fullbatt_vchk_work
);
554 queue_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
555 msecs_to_jiffies(desc
->fullbatt_vchkdrop_ms
));
556 cm
->fullbatt_vchk_jiffies_at
= jiffies
+ msecs_to_jiffies(
557 desc
->fullbatt_vchkdrop_ms
);
559 if (cm
->fullbatt_vchk_jiffies_at
== 0)
560 cm
->fullbatt_vchk_jiffies_at
= 1;
563 dev_info(cm
->dev
, "EVENT_HANDLE: Battery Fully Charged.\n");
564 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_FULL
]);
568 * battout_handler - Event handler for CM_EVENT_BATT_OUT
569 * @cm: the Charger Manager representing the battery.
571 static void battout_handler(struct charger_manager
*cm
)
574 device_set_wakeup_capable(cm
->dev
, true);
576 if (!is_batt_present(cm
)) {
577 dev_emerg(cm
->dev
, "Battery Pulled Out!\n");
578 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_OUT
]);
580 uevent_notify(cm
, "Battery Reinserted?");
585 * misc_event_handler - Handler for other evnets
586 * @cm: the Charger Manager representing the battery.
587 * @type: the Charger Manager representing the battery.
589 static void misc_event_handler(struct charger_manager
*cm
,
590 enum cm_event_types type
)
593 device_set_wakeup_capable(cm
->dev
, true);
595 if (!delayed_work_pending(&cm_monitor_work
) &&
596 is_polling_required(cm
) && cm
->desc
->polling_interval_ms
)
597 schedule_work(&setup_polling
);
598 uevent_notify(cm
, default_event_names
[type
]);
601 static int charger_get_property(struct power_supply
*psy
,
602 enum power_supply_property psp
,
603 union power_supply_propval
*val
)
605 struct charger_manager
*cm
= container_of(psy
,
606 struct charger_manager
, charger_psy
);
607 struct charger_desc
*desc
= cm
->desc
;
612 case POWER_SUPPLY_PROP_STATUS
:
614 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
615 else if (is_ext_pwr_online(cm
))
616 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
618 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
620 case POWER_SUPPLY_PROP_HEALTH
:
621 if (cm
->emergency_stop
> 0)
622 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
623 else if (cm
->emergency_stop
< 0)
624 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
626 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
628 case POWER_SUPPLY_PROP_PRESENT
:
629 if (is_batt_present(cm
))
634 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
635 ret
= get_batt_uV(cm
, &val
->intval
);
637 case POWER_SUPPLY_PROP_CURRENT_NOW
:
638 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
639 POWER_SUPPLY_PROP_CURRENT_NOW
, val
);
641 case POWER_SUPPLY_PROP_TEMP
:
642 /* in thenth of centigrade */
643 if (cm
->last_temp_mC
== INT_MIN
)
644 desc
->temperature_out_of_range(&cm
->last_temp_mC
);
645 val
->intval
= cm
->last_temp_mC
/ 100;
646 if (!desc
->measure_battery_temp
)
649 case POWER_SUPPLY_PROP_TEMP_AMBIENT
:
650 /* in thenth of centigrade */
651 if (cm
->last_temp_mC
== INT_MIN
)
652 desc
->temperature_out_of_range(&cm
->last_temp_mC
);
653 val
->intval
= cm
->last_temp_mC
/ 100;
654 if (desc
->measure_battery_temp
)
657 case POWER_SUPPLY_PROP_CAPACITY
:
658 if (!cm
->fuel_gauge
) {
663 if (!is_batt_present(cm
)) {
664 /* There is no battery. Assume 100% */
669 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
670 POWER_SUPPLY_PROP_CAPACITY
, val
);
674 if (val
->intval
> 100) {
681 /* Do not adjust SOC when charging: voltage is overrated */
686 * If the capacity value is inconsistent, calibrate it base on
687 * the battery voltage values and the thresholds given as desc
689 ret
= get_batt_uV(cm
, &uV
);
691 /* Voltage information not available. No calibration */
696 if (desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
703 case POWER_SUPPLY_PROP_ONLINE
:
704 if (is_ext_pwr_online(cm
))
709 case POWER_SUPPLY_PROP_CHARGE_FULL
:
710 if (cm
->fuel_gauge
) {
711 if (cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
712 POWER_SUPPLY_PROP_CHARGE_FULL
, val
) == 0)
716 if (is_ext_pwr_online(cm
)) {
717 /* Not full if it's charging. */
718 if (is_charging(cm
)) {
723 * Full if it's powered but not charging andi
724 * not forced stop by emergency
726 if (!cm
->emergency_stop
) {
732 /* Full if it's over the fullbatt voltage */
733 ret
= get_batt_uV(cm
, &uV
);
734 if (!ret
&& desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
740 /* Full if the cap is 100 */
741 if (cm
->fuel_gauge
) {
742 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
743 POWER_SUPPLY_PROP_CAPACITY
, val
);
744 if (!ret
&& val
->intval
>= 100 && !is_charging(cm
)) {
753 case POWER_SUPPLY_PROP_CHARGE_NOW
:
754 if (is_charging(cm
)) {
755 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
756 POWER_SUPPLY_PROP_CHARGE_NOW
,
762 /* If CHARGE_NOW is supplied, use it */
763 val
->intval
= (val
->intval
> 0) ?
776 #define NUM_CHARGER_PSY_OPTIONAL (4)
777 static enum power_supply_property default_charger_props
[] = {
778 /* Guaranteed to provide */
779 POWER_SUPPLY_PROP_STATUS
,
780 POWER_SUPPLY_PROP_HEALTH
,
781 POWER_SUPPLY_PROP_PRESENT
,
782 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
783 POWER_SUPPLY_PROP_CAPACITY
,
784 POWER_SUPPLY_PROP_ONLINE
,
785 POWER_SUPPLY_PROP_CHARGE_FULL
,
787 * Optional properties are:
788 * POWER_SUPPLY_PROP_CHARGE_NOW,
789 * POWER_SUPPLY_PROP_CURRENT_NOW,
790 * POWER_SUPPLY_PROP_TEMP, and
791 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
795 static struct power_supply psy_default
= {
797 .type
= POWER_SUPPLY_TYPE_BATTERY
,
798 .properties
= default_charger_props
,
799 .num_properties
= ARRAY_SIZE(default_charger_props
),
800 .get_property
= charger_get_property
,
804 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
807 * Returns true if the alarm is set for Charger Manager to use.
809 * cm_setup_timer fails to set an alarm,
810 * cm_setup_timer does not need to set an alarm for Charger Manager,
811 * or an alarm previously configured is to be used.
813 static bool cm_setup_timer(void)
815 struct charger_manager
*cm
;
816 unsigned int wakeup_ms
= UINT_MAX
;
819 mutex_lock(&cm_list_mtx
);
821 list_for_each_entry(cm
, &cm_list
, entry
) {
822 unsigned int fbchk_ms
= 0;
824 /* fullbatt_vchk is required. setup timer for that */
825 if (cm
->fullbatt_vchk_jiffies_at
) {
826 fbchk_ms
= jiffies_to_msecs(cm
->fullbatt_vchk_jiffies_at
828 if (time_is_before_eq_jiffies(
829 cm
->fullbatt_vchk_jiffies_at
) ||
830 msecs_to_jiffies(fbchk_ms
) < CM_JIFFIES_SMALL
) {
831 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
835 CM_MIN_VALID(wakeup_ms
, fbchk_ms
);
837 /* Skip if polling is not required for this CM */
838 if (!is_polling_required(cm
) && !cm
->emergency_stop
)
840 if (cm
->desc
->polling_interval_ms
== 0)
842 CM_MIN_VALID(wakeup_ms
, cm
->desc
->polling_interval_ms
);
845 mutex_unlock(&cm_list_mtx
);
847 if (wakeup_ms
< UINT_MAX
&& wakeup_ms
> 0) {
848 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms
);
850 struct rtc_wkalrm tmp
;
851 unsigned long time
, now
;
852 unsigned long add
= DIV_ROUND_UP(wakeup_ms
, 1000);
855 * Set alarm with the polling interval (wakeup_ms)
856 * except when rtc_wkalarm_save comes first.
857 * However, the alarm time should be NOW +
858 * CM_RTC_SMALL or later.
861 rtc_read_time(rtc_dev
, &tmp
.time
);
862 rtc_tm_to_time(&tmp
.time
, &now
);
863 if (add
< CM_RTC_SMALL
)
869 if (rtc_wkalarm_save
.enabled
&&
870 rtc_wkalarm_save_time
&&
871 rtc_wkalarm_save_time
< time
) {
872 if (rtc_wkalarm_save_time
< now
+ CM_RTC_SMALL
)
873 time
= now
+ CM_RTC_SMALL
;
875 time
= rtc_wkalarm_save_time
;
877 /* The timer is not appointed by CM */
881 pr_info("Waking up after %lu secs.\n",
884 rtc_time_to_tm(time
, &tmp
.time
);
885 rtc_set_alarm(rtc_dev
, &tmp
);
886 cm_suspend_duration_ms
+= wakeup_ms
;
892 rtc_set_alarm(rtc_dev
, &rtc_wkalarm_save
);
896 static void _cm_fbchk_in_suspend(struct charger_manager
*cm
)
898 unsigned long jiffy_now
= jiffies
;
900 if (!cm
->fullbatt_vchk_jiffies_at
)
903 if (g_desc
&& g_desc
->assume_timer_stops_in_suspend
)
904 jiffy_now
+= msecs_to_jiffies(cm_suspend_duration_ms
);
906 /* Execute now if it's going to be executed not too long after */
907 jiffy_now
+= CM_JIFFIES_SMALL
;
909 if (time_after_eq(jiffy_now
, cm
->fullbatt_vchk_jiffies_at
))
910 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
914 * cm_suspend_again - Determine whether suspend again or not
916 * Returns true if the system should be suspended again
917 * Returns false if the system should be woken up
919 bool cm_suspend_again(void)
921 struct charger_manager
*cm
;
924 if (!g_desc
|| !g_desc
->rtc_only_wakeup
|| !g_desc
->rtc_only_wakeup() ||
932 mutex_lock(&cm_list_mtx
);
933 list_for_each_entry(cm
, &cm_list
, entry
) {
934 _cm_fbchk_in_suspend(cm
);
936 if (cm
->status_save_ext_pwr_inserted
!= is_ext_pwr_online(cm
) ||
937 cm
->status_save_batt
!= is_batt_present(cm
)) {
942 mutex_unlock(&cm_list_mtx
);
944 cm_rtc_set
= cm_setup_timer();
946 /* It's about the time when the non-CM appointed timer goes off */
947 if (rtc_wkalarm_save
.enabled
) {
951 rtc_read_time(rtc_dev
, &tmp
);
952 rtc_tm_to_time(&tmp
, &now
);
954 if (rtc_wkalarm_save_time
&&
955 now
+ CM_RTC_SMALL
>= rtc_wkalarm_save_time
)
960 EXPORT_SYMBOL_GPL(cm_suspend_again
);
963 * setup_charger_manager - initialize charger_global_desc data
964 * @gd: pointer to instance of charger_global_desc
966 int setup_charger_manager(struct charger_global_desc
*gd
)
972 rtc_class_close(rtc_dev
);
976 if (!gd
->rtc_only_wakeup
) {
977 pr_err("The callback rtc_only_wakeup is not given.\n");
982 rtc_dev
= rtc_class_open(gd
->rtc_name
);
983 if (IS_ERR_OR_NULL(rtc_dev
)) {
985 /* Retry at probe. RTC may be not registered yet */
988 pr_warn("No wakeup timer is given for charger manager."
989 "In-suspend monitoring won't work.\n");
995 EXPORT_SYMBOL_GPL(setup_charger_manager
);
997 static int charger_manager_probe(struct platform_device
*pdev
)
999 struct charger_desc
*desc
= dev_get_platdata(&pdev
->dev
);
1000 struct charger_manager
*cm
;
1002 union power_supply_propval val
;
1004 if (g_desc
&& !rtc_dev
&& g_desc
->rtc_name
) {
1005 rtc_dev
= rtc_class_open(g_desc
->rtc_name
);
1006 if (IS_ERR_OR_NULL(rtc_dev
)) {
1008 dev_err(&pdev
->dev
, "Cannot get RTC %s.\n",
1016 dev_err(&pdev
->dev
, "No platform data (desc) found.\n");
1021 cm
= kzalloc(sizeof(struct charger_manager
), GFP_KERNEL
);
1023 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
1028 /* Basic Values. Unspecified are Null or 0 */
1029 cm
->dev
= &pdev
->dev
;
1030 cm
->desc
= kzalloc(sizeof(struct charger_desc
), GFP_KERNEL
);
1032 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
1034 goto err_alloc_desc
;
1036 memcpy(cm
->desc
, desc
, sizeof(struct charger_desc
));
1037 cm
->last_temp_mC
= INT_MIN
; /* denotes "unmeasured, yet" */
1040 * The following two do not need to be errors.
1041 * Users may intentionally ignore those two features.
1043 if (desc
->fullbatt_uV
== 0) {
1044 dev_info(&pdev
->dev
, "Ignoring full-battery voltage threshold"
1045 " as it is not supplied.");
1047 if (!desc
->fullbatt_vchkdrop_ms
|| !desc
->fullbatt_vchkdrop_uV
) {
1048 dev_info(&pdev
->dev
, "Disabling full-battery voltage drop "
1049 "checking mechanism as it is not supplied.");
1050 desc
->fullbatt_vchkdrop_ms
= 0;
1051 desc
->fullbatt_vchkdrop_uV
= 0;
1054 if (!desc
->charger_regulators
|| desc
->num_charger_regulators
< 1) {
1056 dev_err(&pdev
->dev
, "charger_regulators undefined.\n");
1057 goto err_no_charger
;
1060 if (!desc
->psy_charger_stat
|| !desc
->psy_charger_stat
[0]) {
1061 dev_err(&pdev
->dev
, "No power supply defined.\n");
1063 goto err_no_charger_stat
;
1066 /* Counting index only */
1067 while (desc
->psy_charger_stat
[i
])
1070 cm
->charger_stat
= kzalloc(sizeof(struct power_supply
*) * (i
+ 1),
1072 if (!cm
->charger_stat
) {
1074 goto err_no_charger_stat
;
1077 for (i
= 0; desc
->psy_charger_stat
[i
]; i
++) {
1078 cm
->charger_stat
[i
] = power_supply_get_by_name(
1079 desc
->psy_charger_stat
[i
]);
1080 if (!cm
->charger_stat
[i
]) {
1081 dev_err(&pdev
->dev
, "Cannot find power supply "
1083 desc
->psy_charger_stat
[i
]);
1089 cm
->fuel_gauge
= power_supply_get_by_name(desc
->psy_fuel_gauge
);
1090 if (!cm
->fuel_gauge
) {
1091 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1092 desc
->psy_fuel_gauge
);
1097 if (desc
->polling_interval_ms
== 0 ||
1098 msecs_to_jiffies(desc
->polling_interval_ms
) <= CM_JIFFIES_SMALL
) {
1099 dev_err(&pdev
->dev
, "polling_interval_ms is too small\n");
1104 if (!desc
->temperature_out_of_range
) {
1105 dev_err(&pdev
->dev
, "there is no temperature_out_of_range\n");
1110 platform_set_drvdata(pdev
, cm
);
1112 memcpy(&cm
->charger_psy
, &psy_default
, sizeof(psy_default
));
1114 if (!desc
->psy_name
) {
1115 strncpy(cm
->psy_name_buf
, psy_default
.name
, PSY_NAME_MAX
);
1117 strncpy(cm
->psy_name_buf
, desc
->psy_name
, PSY_NAME_MAX
);
1119 cm
->charger_psy
.name
= cm
->psy_name_buf
;
1121 /* Allocate for psy properties because they may vary */
1122 cm
->charger_psy
.properties
= kzalloc(sizeof(enum power_supply_property
)
1123 * (ARRAY_SIZE(default_charger_props
) +
1124 NUM_CHARGER_PSY_OPTIONAL
),
1126 if (!cm
->charger_psy
.properties
) {
1127 dev_err(&pdev
->dev
, "Cannot allocate for psy properties.\n");
1131 memcpy(cm
->charger_psy
.properties
, default_charger_props
,
1132 sizeof(enum power_supply_property
) *
1133 ARRAY_SIZE(default_charger_props
));
1134 cm
->charger_psy
.num_properties
= psy_default
.num_properties
;
1136 /* Find which optional psy-properties are available */
1137 if (!cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
1138 POWER_SUPPLY_PROP_CHARGE_NOW
, &val
)) {
1139 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1140 POWER_SUPPLY_PROP_CHARGE_NOW
;
1141 cm
->charger_psy
.num_properties
++;
1143 if (!cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
1144 POWER_SUPPLY_PROP_CURRENT_NOW
,
1146 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1147 POWER_SUPPLY_PROP_CURRENT_NOW
;
1148 cm
->charger_psy
.num_properties
++;
1151 if (desc
->measure_battery_temp
) {
1152 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1153 POWER_SUPPLY_PROP_TEMP
;
1154 cm
->charger_psy
.num_properties
++;
1156 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1157 POWER_SUPPLY_PROP_TEMP_AMBIENT
;
1158 cm
->charger_psy
.num_properties
++;
1161 INIT_DELAYED_WORK(&cm
->fullbatt_vchk_work
, fullbatt_vchk
);
1163 ret
= power_supply_register(NULL
, &cm
->charger_psy
);
1165 dev_err(&pdev
->dev
, "Cannot register charger-manager with"
1166 " name \"%s\".\n", cm
->charger_psy
.name
);
1170 ret
= regulator_bulk_get(&pdev
->dev
, desc
->num_charger_regulators
,
1171 desc
->charger_regulators
);
1173 dev_err(&pdev
->dev
, "Cannot get charger regulators.\n");
1177 ret
= try_charger_enable(cm
, true);
1179 dev_err(&pdev
->dev
, "Cannot enable charger regulators\n");
1180 goto err_chg_enable
;
1183 /* Add to the list */
1184 mutex_lock(&cm_list_mtx
);
1185 list_add(&cm
->entry
, &cm_list
);
1186 mutex_unlock(&cm_list_mtx
);
1189 * Charger-manager is capable of waking up the systme from sleep
1190 * when event is happend through cm_notify_event()
1192 device_init_wakeup(&pdev
->dev
, true);
1193 device_set_wakeup_capable(&pdev
->dev
, false);
1195 schedule_work(&setup_polling
);
1200 regulator_bulk_free(desc
->num_charger_regulators
,
1201 desc
->charger_regulators
);
1203 power_supply_unregister(&cm
->charger_psy
);
1205 kfree(cm
->charger_psy
.properties
);
1207 kfree(cm
->charger_stat
);
1208 err_no_charger_stat
:
1217 static int __devexit
charger_manager_remove(struct platform_device
*pdev
)
1219 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
1220 struct charger_desc
*desc
= cm
->desc
;
1222 /* Remove from the list */
1223 mutex_lock(&cm_list_mtx
);
1224 list_del(&cm
->entry
);
1225 mutex_unlock(&cm_list_mtx
);
1227 if (work_pending(&setup_polling
))
1228 cancel_work_sync(&setup_polling
);
1229 if (delayed_work_pending(&cm_monitor_work
))
1230 cancel_delayed_work_sync(&cm_monitor_work
);
1232 regulator_bulk_free(desc
->num_charger_regulators
,
1233 desc
->charger_regulators
);
1234 power_supply_unregister(&cm
->charger_psy
);
1236 try_charger_enable(cm
, false);
1238 kfree(cm
->charger_psy
.properties
);
1239 kfree(cm
->charger_stat
);
1246 static const struct platform_device_id charger_manager_id
[] = {
1247 { "charger-manager", 0 },
1250 MODULE_DEVICE_TABLE(platform
, charger_manager_id
);
1252 static int cm_suspend_noirq(struct device
*dev
)
1256 if (device_may_wakeup(dev
)) {
1257 device_set_wakeup_capable(dev
, false);
1264 static int cm_suspend_prepare(struct device
*dev
)
1266 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1268 if (!cm_suspended
) {
1270 struct rtc_time tmp
;
1273 rtc_read_alarm(rtc_dev
, &rtc_wkalarm_save
);
1274 rtc_read_time(rtc_dev
, &tmp
);
1276 if (rtc_wkalarm_save
.enabled
) {
1277 rtc_tm_to_time(&rtc_wkalarm_save
.time
,
1278 &rtc_wkalarm_save_time
);
1279 rtc_tm_to_time(&tmp
, &now
);
1280 if (now
> rtc_wkalarm_save_time
)
1281 rtc_wkalarm_save_time
= 0;
1283 rtc_wkalarm_save_time
= 0;
1286 cm_suspended
= true;
1289 if (delayed_work_pending(&cm
->fullbatt_vchk_work
))
1290 cancel_delayed_work(&cm
->fullbatt_vchk_work
);
1291 cm
->status_save_ext_pwr_inserted
= is_ext_pwr_online(cm
);
1292 cm
->status_save_batt
= is_batt_present(cm
);
1295 cm_suspend_duration_ms
= 0;
1296 cm_rtc_set
= cm_setup_timer();
1302 static void cm_suspend_complete(struct device
*dev
)
1304 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1308 struct rtc_wkalrm tmp
;
1310 rtc_read_alarm(rtc_dev
, &tmp
);
1311 rtc_wkalarm_save
.pending
= tmp
.pending
;
1312 rtc_set_alarm(rtc_dev
, &rtc_wkalarm_save
);
1314 cm_suspended
= false;
1318 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1319 if (cm
->fullbatt_vchk_jiffies_at
) {
1320 unsigned long delay
= 0;
1321 unsigned long now
= jiffies
+ CM_JIFFIES_SMALL
;
1323 if (time_after_eq(now
, cm
->fullbatt_vchk_jiffies_at
)) {
1324 delay
= (unsigned long)((long)now
1325 - (long)(cm
->fullbatt_vchk_jiffies_at
));
1326 delay
= jiffies_to_msecs(delay
);
1332 * Account for cm_suspend_duration_ms if
1333 * assume_timer_stops_in_suspend is active
1335 if (g_desc
&& g_desc
->assume_timer_stops_in_suspend
) {
1336 if (delay
> cm_suspend_duration_ms
)
1337 delay
-= cm_suspend_duration_ms
;
1342 queue_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
1343 msecs_to_jiffies(delay
));
1345 device_set_wakeup_capable(cm
->dev
, false);
1346 uevent_notify(cm
, NULL
);
1349 static const struct dev_pm_ops charger_manager_pm
= {
1350 .prepare
= cm_suspend_prepare
,
1351 .suspend_noirq
= cm_suspend_noirq
,
1352 .complete
= cm_suspend_complete
,
1355 static struct platform_driver charger_manager_driver
= {
1357 .name
= "charger-manager",
1358 .owner
= THIS_MODULE
,
1359 .pm
= &charger_manager_pm
,
1361 .probe
= charger_manager_probe
,
1362 .remove
= __devexit_p(charger_manager_remove
),
1363 .id_table
= charger_manager_id
,
1366 static int __init
charger_manager_init(void)
1368 cm_wq
= create_freezable_workqueue("charger_manager");
1369 INIT_DELAYED_WORK(&cm_monitor_work
, cm_monitor_poller
);
1371 return platform_driver_register(&charger_manager_driver
);
1373 late_initcall(charger_manager_init
);
1375 static void __exit
charger_manager_cleanup(void)
1377 destroy_workqueue(cm_wq
);
1380 platform_driver_unregister(&charger_manager_driver
);
1382 module_exit(charger_manager_cleanup
);
1385 * find_power_supply - find the associated power_supply of charger
1386 * @cm: the Charger Manager representing the battery
1387 * @psy: pointer to instance of charger's power_supply
1389 static bool find_power_supply(struct charger_manager
*cm
,
1390 struct power_supply
*psy
)
1395 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
1396 if (psy
== cm
->charger_stat
[i
]) {
1406 * cm_notify_event - charger driver notify Charger Manager of charger event
1407 * @psy: pointer to instance of charger's power_supply
1408 * @type: type of charger event
1409 * @msg: optional message passed to uevent_notify fuction
1411 void cm_notify_event(struct power_supply
*psy
, enum cm_event_types type
,
1414 struct charger_manager
*cm
;
1415 bool found_power_supply
= false;
1420 mutex_lock(&cm_list_mtx
);
1421 list_for_each_entry(cm
, &cm_list
, entry
) {
1422 found_power_supply
= find_power_supply(cm
, psy
);
1423 if (found_power_supply
)
1426 mutex_unlock(&cm_list_mtx
);
1428 if (!found_power_supply
)
1432 case CM_EVENT_BATT_FULL
:
1433 fullbatt_handler(cm
);
1435 case CM_EVENT_BATT_OUT
:
1436 battout_handler(cm
);
1438 case CM_EVENT_BATT_IN
:
1439 case CM_EVENT_EXT_PWR_IN_OUT
... CM_EVENT_CHG_START_STOP
:
1440 misc_event_handler(cm
, type
);
1442 case CM_EVENT_UNKNOWN
:
1443 case CM_EVENT_OTHERS
:
1444 uevent_notify(cm
, msg
? msg
: default_event_names
[type
]);
1447 dev_err(cm
->dev
, "%s type not specified.\n", __func__
);
1451 EXPORT_SYMBOL_GPL(cm_notify_event
);
1453 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1454 MODULE_DESCRIPTION("Charger Manager");
1455 MODULE_LICENSE("GPL");