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 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++)
275 regulator_enable(desc
->charger_regulators
[i
].consumer
);
278 * Abnormal battery state - Stop charging forcibly,
279 * even if charger was enabled at the other places
281 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
282 if (regulator_is_enabled(
283 desc
->charger_regulators
[i
].consumer
)) {
284 regulator_force_disable(
285 desc
->charger_regulators
[i
].consumer
);
287 "Disable regulator(%s) forcibly.\n",
288 desc
->charger_regulators
[i
].regulator_name
);
294 cm
->charger_enabled
= enable
;
300 * try_charger_restart - Restart charging.
301 * @cm: the Charger Manager representing the battery.
303 * Restart charging by turning off and on the charger.
305 static int try_charger_restart(struct charger_manager
*cm
)
309 if (cm
->emergency_stop
)
312 err
= try_charger_enable(cm
, false);
316 return try_charger_enable(cm
, true);
320 * uevent_notify - Let users know something has changed.
321 * @cm: the Charger Manager representing the battery.
322 * @event: the event string.
324 * If @event is null, it implies that uevent_notify is called
325 * by resume function. When called in the resume function, cm_suspended
326 * should be already reset to false in order to let uevent_notify
327 * notify the recent event during the suspend to users. While
328 * suspended, uevent_notify does not notify users, but tracks
329 * events so that uevent_notify can notify users later after resumed.
331 static void uevent_notify(struct charger_manager
*cm
, const char *event
)
333 static char env_str
[UEVENT_BUF_SIZE
+ 1] = "";
334 static char env_str_save
[UEVENT_BUF_SIZE
+ 1] = "";
337 /* Nothing in suspended-event buffer */
338 if (env_str_save
[0] == 0) {
339 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
340 return; /* status not changed */
341 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
345 if (!strncmp(env_str_save
, event
, UEVENT_BUF_SIZE
))
346 return; /* Duplicated. */
347 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
352 /* No messages pending */
353 if (!env_str_save
[0])
356 strncpy(env_str
, env_str_save
, UEVENT_BUF_SIZE
);
357 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
363 /* status not changed */
364 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
367 /* save the status and notify the update */
368 strncpy(env_str
, event
, UEVENT_BUF_SIZE
);
369 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
371 dev_info(cm
->dev
, event
);
375 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
376 * @work: the work_struct appointing the function
378 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
379 * charger_desc, Charger Manager checks voltage drop after the battery
380 * "FULL" event. It checks whether the voltage has dropped more than
381 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
383 static void fullbatt_vchk(struct work_struct
*work
)
385 struct delayed_work
*dwork
= to_delayed_work(work
);
386 struct charger_manager
*cm
= container_of(dwork
,
387 struct charger_manager
, fullbatt_vchk_work
);
388 struct charger_desc
*desc
= cm
->desc
;
389 int batt_uV
, err
, diff
;
391 /* remove the appointment for fullbatt_vchk */
392 cm
->fullbatt_vchk_jiffies_at
= 0;
394 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
397 err
= get_batt_uV(cm
, &batt_uV
);
399 dev_err(cm
->dev
, "%s: get_batt_uV error(%d).\n", __func__
, err
);
403 diff
= cm
->fullbatt_vchk_uV
;
406 dev_dbg(cm
->dev
, "VBATT dropped %duV after full-batt.\n", diff
);
408 if (diff
> desc
->fullbatt_vchkdrop_uV
) {
409 try_charger_restart(cm
);
410 uevent_notify(cm
, "Recharge");
415 * _cm_monitor - Monitor the temperature and return true for exceptions.
416 * @cm: the Charger Manager representing the battery.
418 * Returns true if there is an event to notify for the battery.
419 * (True if the status of "emergency_stop" changes)
421 static bool _cm_monitor(struct charger_manager
*cm
)
423 struct charger_desc
*desc
= cm
->desc
;
424 int temp
= desc
->temperature_out_of_range(&cm
->last_temp_mC
);
426 dev_dbg(cm
->dev
, "monitoring (%2.2d.%3.3dC)\n",
427 cm
->last_temp_mC
/ 1000, cm
->last_temp_mC
% 1000);
429 /* It has been stopped or charging already */
430 if (!!temp
== !!cm
->emergency_stop
)
434 cm
->emergency_stop
= temp
;
435 if (!try_charger_enable(cm
, false)) {
437 uevent_notify(cm
, "OVERHEAT");
439 uevent_notify(cm
, "COLD");
442 cm
->emergency_stop
= 0;
443 if (!try_charger_enable(cm
, true))
444 uevent_notify(cm
, "CHARGING");
451 * cm_monitor - Monitor every battery.
453 * Returns true if there is an event to notify from any of the batteries.
454 * (True if the status of "emergency_stop" changes)
456 static bool cm_monitor(void)
459 struct charger_manager
*cm
;
461 mutex_lock(&cm_list_mtx
);
463 list_for_each_entry(cm
, &cm_list
, entry
) {
468 mutex_unlock(&cm_list_mtx
);
474 * _setup_polling - Setup the next instance of polling.
475 * @work: work_struct of the function _setup_polling.
477 static void _setup_polling(struct work_struct
*work
)
479 unsigned long min
= ULONG_MAX
;
480 struct charger_manager
*cm
;
481 bool keep_polling
= false;
482 unsigned long _next_polling
;
484 mutex_lock(&cm_list_mtx
);
486 list_for_each_entry(cm
, &cm_list
, entry
) {
487 if (is_polling_required(cm
) && cm
->desc
->polling_interval_ms
) {
490 if (min
> cm
->desc
->polling_interval_ms
)
491 min
= cm
->desc
->polling_interval_ms
;
495 polling_jiffy
= msecs_to_jiffies(min
);
496 if (polling_jiffy
<= CM_JIFFIES_SMALL
)
497 polling_jiffy
= CM_JIFFIES_SMALL
+ 1;
500 polling_jiffy
= ULONG_MAX
;
501 if (polling_jiffy
== ULONG_MAX
)
504 WARN(cm_wq
== NULL
, "charger-manager: workqueue not initialized"
505 ". try it later. %s\n", __func__
);
507 _next_polling
= jiffies
+ polling_jiffy
;
509 if (!delayed_work_pending(&cm_monitor_work
) ||
510 (delayed_work_pending(&cm_monitor_work
) &&
511 time_after(next_polling
, _next_polling
))) {
512 next_polling
= jiffies
+ polling_jiffy
;
513 mod_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
);
517 mutex_unlock(&cm_list_mtx
);
519 static DECLARE_WORK(setup_polling
, _setup_polling
);
522 * cm_monitor_poller - The Monitor / Poller.
523 * @work: work_struct of the function cm_monitor_poller
525 * During non-suspended state, cm_monitor_poller is used to poll and monitor
528 static void cm_monitor_poller(struct work_struct
*work
)
531 schedule_work(&setup_polling
);
535 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
536 * @cm: the Charger Manager representing the battery.
538 static void fullbatt_handler(struct charger_manager
*cm
)
540 struct charger_desc
*desc
= cm
->desc
;
542 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
546 device_set_wakeup_capable(cm
->dev
, true);
548 mod_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
549 msecs_to_jiffies(desc
->fullbatt_vchkdrop_ms
));
550 cm
->fullbatt_vchk_jiffies_at
= jiffies
+ msecs_to_jiffies(
551 desc
->fullbatt_vchkdrop_ms
);
553 if (cm
->fullbatt_vchk_jiffies_at
== 0)
554 cm
->fullbatt_vchk_jiffies_at
= 1;
557 dev_info(cm
->dev
, "EVENT_HANDLE: Battery Fully Charged.\n");
558 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_FULL
]);
562 * battout_handler - Event handler for CM_EVENT_BATT_OUT
563 * @cm: the Charger Manager representing the battery.
565 static void battout_handler(struct charger_manager
*cm
)
568 device_set_wakeup_capable(cm
->dev
, true);
570 if (!is_batt_present(cm
)) {
571 dev_emerg(cm
->dev
, "Battery Pulled Out!\n");
572 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_OUT
]);
574 uevent_notify(cm
, "Battery Reinserted?");
579 * misc_event_handler - Handler for other evnets
580 * @cm: the Charger Manager representing the battery.
581 * @type: the Charger Manager representing the battery.
583 static void misc_event_handler(struct charger_manager
*cm
,
584 enum cm_event_types type
)
587 device_set_wakeup_capable(cm
->dev
, true);
589 if (!delayed_work_pending(&cm_monitor_work
) &&
590 is_polling_required(cm
) && cm
->desc
->polling_interval_ms
)
591 schedule_work(&setup_polling
);
592 uevent_notify(cm
, default_event_names
[type
]);
595 static int charger_get_property(struct power_supply
*psy
,
596 enum power_supply_property psp
,
597 union power_supply_propval
*val
)
599 struct charger_manager
*cm
= container_of(psy
,
600 struct charger_manager
, charger_psy
);
601 struct charger_desc
*desc
= cm
->desc
;
606 case POWER_SUPPLY_PROP_STATUS
:
608 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
609 else if (is_ext_pwr_online(cm
))
610 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
612 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
614 case POWER_SUPPLY_PROP_HEALTH
:
615 if (cm
->emergency_stop
> 0)
616 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
617 else if (cm
->emergency_stop
< 0)
618 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
620 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
622 case POWER_SUPPLY_PROP_PRESENT
:
623 if (is_batt_present(cm
))
628 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
629 ret
= get_batt_uV(cm
, &val
->intval
);
631 case POWER_SUPPLY_PROP_CURRENT_NOW
:
632 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
633 POWER_SUPPLY_PROP_CURRENT_NOW
, val
);
635 case POWER_SUPPLY_PROP_TEMP
:
636 /* in thenth of centigrade */
637 if (cm
->last_temp_mC
== INT_MIN
)
638 desc
->temperature_out_of_range(&cm
->last_temp_mC
);
639 val
->intval
= cm
->last_temp_mC
/ 100;
640 if (!desc
->measure_battery_temp
)
643 case POWER_SUPPLY_PROP_TEMP_AMBIENT
:
644 /* in thenth of centigrade */
645 if (cm
->last_temp_mC
== INT_MIN
)
646 desc
->temperature_out_of_range(&cm
->last_temp_mC
);
647 val
->intval
= cm
->last_temp_mC
/ 100;
648 if (desc
->measure_battery_temp
)
651 case POWER_SUPPLY_PROP_CAPACITY
:
652 if (!cm
->fuel_gauge
) {
657 if (!is_batt_present(cm
)) {
658 /* There is no battery. Assume 100% */
663 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
664 POWER_SUPPLY_PROP_CAPACITY
, val
);
668 if (val
->intval
> 100) {
675 /* Do not adjust SOC when charging: voltage is overrated */
680 * If the capacity value is inconsistent, calibrate it base on
681 * the battery voltage values and the thresholds given as desc
683 ret
= get_batt_uV(cm
, &uV
);
685 /* Voltage information not available. No calibration */
690 if (desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
697 case POWER_SUPPLY_PROP_ONLINE
:
698 if (is_ext_pwr_online(cm
))
703 case POWER_SUPPLY_PROP_CHARGE_FULL
:
704 if (cm
->fuel_gauge
) {
705 if (cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
706 POWER_SUPPLY_PROP_CHARGE_FULL
, val
) == 0)
710 if (is_ext_pwr_online(cm
)) {
711 /* Not full if it's charging. */
712 if (is_charging(cm
)) {
717 * Full if it's powered but not charging andi
718 * not forced stop by emergency
720 if (!cm
->emergency_stop
) {
726 /* Full if it's over the fullbatt voltage */
727 ret
= get_batt_uV(cm
, &uV
);
728 if (!ret
&& desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
734 /* Full if the cap is 100 */
735 if (cm
->fuel_gauge
) {
736 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
737 POWER_SUPPLY_PROP_CAPACITY
, val
);
738 if (!ret
&& val
->intval
>= 100 && !is_charging(cm
)) {
747 case POWER_SUPPLY_PROP_CHARGE_NOW
:
748 if (is_charging(cm
)) {
749 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
750 POWER_SUPPLY_PROP_CHARGE_NOW
,
756 /* If CHARGE_NOW is supplied, use it */
757 val
->intval
= (val
->intval
> 0) ?
770 #define NUM_CHARGER_PSY_OPTIONAL (4)
771 static enum power_supply_property default_charger_props
[] = {
772 /* Guaranteed to provide */
773 POWER_SUPPLY_PROP_STATUS
,
774 POWER_SUPPLY_PROP_HEALTH
,
775 POWER_SUPPLY_PROP_PRESENT
,
776 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
777 POWER_SUPPLY_PROP_CAPACITY
,
778 POWER_SUPPLY_PROP_ONLINE
,
779 POWER_SUPPLY_PROP_CHARGE_FULL
,
781 * Optional properties are:
782 * POWER_SUPPLY_PROP_CHARGE_NOW,
783 * POWER_SUPPLY_PROP_CURRENT_NOW,
784 * POWER_SUPPLY_PROP_TEMP, and
785 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
789 static struct power_supply psy_default
= {
791 .type
= POWER_SUPPLY_TYPE_BATTERY
,
792 .properties
= default_charger_props
,
793 .num_properties
= ARRAY_SIZE(default_charger_props
),
794 .get_property
= charger_get_property
,
798 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
801 * Returns true if the alarm is set for Charger Manager to use.
803 * cm_setup_timer fails to set an alarm,
804 * cm_setup_timer does not need to set an alarm for Charger Manager,
805 * or an alarm previously configured is to be used.
807 static bool cm_setup_timer(void)
809 struct charger_manager
*cm
;
810 unsigned int wakeup_ms
= UINT_MAX
;
813 mutex_lock(&cm_list_mtx
);
815 list_for_each_entry(cm
, &cm_list
, entry
) {
816 unsigned int fbchk_ms
= 0;
818 /* fullbatt_vchk is required. setup timer for that */
819 if (cm
->fullbatt_vchk_jiffies_at
) {
820 fbchk_ms
= jiffies_to_msecs(cm
->fullbatt_vchk_jiffies_at
822 if (time_is_before_eq_jiffies(
823 cm
->fullbatt_vchk_jiffies_at
) ||
824 msecs_to_jiffies(fbchk_ms
) < CM_JIFFIES_SMALL
) {
825 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
829 CM_MIN_VALID(wakeup_ms
, fbchk_ms
);
831 /* Skip if polling is not required for this CM */
832 if (!is_polling_required(cm
) && !cm
->emergency_stop
)
834 if (cm
->desc
->polling_interval_ms
== 0)
836 CM_MIN_VALID(wakeup_ms
, cm
->desc
->polling_interval_ms
);
839 mutex_unlock(&cm_list_mtx
);
841 if (wakeup_ms
< UINT_MAX
&& wakeup_ms
> 0) {
842 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms
);
844 struct rtc_wkalrm tmp
;
845 unsigned long time
, now
;
846 unsigned long add
= DIV_ROUND_UP(wakeup_ms
, 1000);
849 * Set alarm with the polling interval (wakeup_ms)
850 * except when rtc_wkalarm_save comes first.
851 * However, the alarm time should be NOW +
852 * CM_RTC_SMALL or later.
855 rtc_read_time(rtc_dev
, &tmp
.time
);
856 rtc_tm_to_time(&tmp
.time
, &now
);
857 if (add
< CM_RTC_SMALL
)
863 if (rtc_wkalarm_save
.enabled
&&
864 rtc_wkalarm_save_time
&&
865 rtc_wkalarm_save_time
< time
) {
866 if (rtc_wkalarm_save_time
< now
+ CM_RTC_SMALL
)
867 time
= now
+ CM_RTC_SMALL
;
869 time
= rtc_wkalarm_save_time
;
871 /* The timer is not appointed by CM */
875 pr_info("Waking up after %lu secs.\n",
878 rtc_time_to_tm(time
, &tmp
.time
);
879 rtc_set_alarm(rtc_dev
, &tmp
);
880 cm_suspend_duration_ms
+= wakeup_ms
;
886 rtc_set_alarm(rtc_dev
, &rtc_wkalarm_save
);
890 static void _cm_fbchk_in_suspend(struct charger_manager
*cm
)
892 unsigned long jiffy_now
= jiffies
;
894 if (!cm
->fullbatt_vchk_jiffies_at
)
897 if (g_desc
&& g_desc
->assume_timer_stops_in_suspend
)
898 jiffy_now
+= msecs_to_jiffies(cm_suspend_duration_ms
);
900 /* Execute now if it's going to be executed not too long after */
901 jiffy_now
+= CM_JIFFIES_SMALL
;
903 if (time_after_eq(jiffy_now
, cm
->fullbatt_vchk_jiffies_at
))
904 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
908 * cm_suspend_again - Determine whether suspend again or not
910 * Returns true if the system should be suspended again
911 * Returns false if the system should be woken up
913 bool cm_suspend_again(void)
915 struct charger_manager
*cm
;
918 if (!g_desc
|| !g_desc
->rtc_only_wakeup
|| !g_desc
->rtc_only_wakeup() ||
926 mutex_lock(&cm_list_mtx
);
927 list_for_each_entry(cm
, &cm_list
, entry
) {
928 _cm_fbchk_in_suspend(cm
);
930 if (cm
->status_save_ext_pwr_inserted
!= is_ext_pwr_online(cm
) ||
931 cm
->status_save_batt
!= is_batt_present(cm
)) {
936 mutex_unlock(&cm_list_mtx
);
938 cm_rtc_set
= cm_setup_timer();
940 /* It's about the time when the non-CM appointed timer goes off */
941 if (rtc_wkalarm_save
.enabled
) {
945 rtc_read_time(rtc_dev
, &tmp
);
946 rtc_tm_to_time(&tmp
, &now
);
948 if (rtc_wkalarm_save_time
&&
949 now
+ CM_RTC_SMALL
>= rtc_wkalarm_save_time
)
954 EXPORT_SYMBOL_GPL(cm_suspend_again
);
957 * setup_charger_manager - initialize charger_global_desc data
958 * @gd: pointer to instance of charger_global_desc
960 int setup_charger_manager(struct charger_global_desc
*gd
)
966 rtc_class_close(rtc_dev
);
970 if (!gd
->rtc_only_wakeup
) {
971 pr_err("The callback rtc_only_wakeup is not given.\n");
976 rtc_dev
= rtc_class_open(gd
->rtc_name
);
977 if (IS_ERR_OR_NULL(rtc_dev
)) {
979 /* Retry at probe. RTC may be not registered yet */
982 pr_warn("No wakeup timer is given for charger manager."
983 "In-suspend monitoring won't work.\n");
989 EXPORT_SYMBOL_GPL(setup_charger_manager
);
992 * charger_extcon_work - enable/diable charger according to the state
995 * @work: work_struct of the function charger_extcon_work.
997 static void charger_extcon_work(struct work_struct
*work
)
999 struct charger_cable
*cable
=
1000 container_of(work
, struct charger_cable
, wq
);
1003 if (cable
->attached
&& cable
->min_uA
!= 0 && cable
->max_uA
!= 0) {
1004 ret
= regulator_set_current_limit(cable
->charger
->consumer
,
1005 cable
->min_uA
, cable
->max_uA
);
1007 pr_err("Cannot set current limit of %s (%s)\n",
1008 cable
->charger
->regulator_name
, cable
->name
);
1012 pr_info("Set current limit of %s : %duA ~ %duA\n",
1013 cable
->charger
->regulator_name
,
1014 cable
->min_uA
, cable
->max_uA
);
1017 try_charger_enable(cable
->cm
, cable
->attached
);
1021 * charger_extcon_notifier - receive the state of charger cable
1022 * when registered cable is attached or detached.
1024 * @self: the notifier block of the charger_extcon_notifier.
1025 * @event: the cable state.
1026 * @ptr: the data pointer of notifier block.
1028 static int charger_extcon_notifier(struct notifier_block
*self
,
1029 unsigned long event
, void *ptr
)
1031 struct charger_cable
*cable
=
1032 container_of(self
, struct charger_cable
, nb
);
1034 cable
->attached
= event
;
1035 schedule_work(&cable
->wq
);
1041 * charger_extcon_init - register external connector to use it
1042 * as the charger cable
1044 * @cm: the Charger Manager representing the battery.
1045 * @cable: the Charger cable representing the external connector.
1047 static int charger_extcon_init(struct charger_manager
*cm
,
1048 struct charger_cable
*cable
)
1053 * Charger manager use Extcon framework to identify
1054 * the charger cable among various external connector
1055 * cable (e.g., TA, USB, MHL, Dock).
1057 INIT_WORK(&cable
->wq
, charger_extcon_work
);
1058 cable
->nb
.notifier_call
= charger_extcon_notifier
;
1059 ret
= extcon_register_interest(&cable
->extcon_dev
,
1060 cable
->extcon_name
, cable
->name
, &cable
->nb
);
1062 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1071 static int charger_manager_probe(struct platform_device
*pdev
)
1073 struct charger_desc
*desc
= dev_get_platdata(&pdev
->dev
);
1074 struct charger_manager
*cm
;
1077 union power_supply_propval val
;
1079 if (g_desc
&& !rtc_dev
&& g_desc
->rtc_name
) {
1080 rtc_dev
= rtc_class_open(g_desc
->rtc_name
);
1081 if (IS_ERR_OR_NULL(rtc_dev
)) {
1083 dev_err(&pdev
->dev
, "Cannot get RTC %s.\n",
1091 dev_err(&pdev
->dev
, "No platform data (desc) found.\n");
1096 cm
= kzalloc(sizeof(struct charger_manager
), GFP_KERNEL
);
1098 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
1103 /* Basic Values. Unspecified are Null or 0 */
1104 cm
->dev
= &pdev
->dev
;
1105 cm
->desc
= kzalloc(sizeof(struct charger_desc
), GFP_KERNEL
);
1107 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
1109 goto err_alloc_desc
;
1111 memcpy(cm
->desc
, desc
, sizeof(struct charger_desc
));
1112 cm
->last_temp_mC
= INT_MIN
; /* denotes "unmeasured, yet" */
1115 * The following two do not need to be errors.
1116 * Users may intentionally ignore those two features.
1118 if (desc
->fullbatt_uV
== 0) {
1119 dev_info(&pdev
->dev
, "Ignoring full-battery voltage threshold"
1120 " as it is not supplied.");
1122 if (!desc
->fullbatt_vchkdrop_ms
|| !desc
->fullbatt_vchkdrop_uV
) {
1123 dev_info(&pdev
->dev
, "Disabling full-battery voltage drop "
1124 "checking mechanism as it is not supplied.");
1125 desc
->fullbatt_vchkdrop_ms
= 0;
1126 desc
->fullbatt_vchkdrop_uV
= 0;
1129 if (!desc
->charger_regulators
|| desc
->num_charger_regulators
< 1) {
1131 dev_err(&pdev
->dev
, "charger_regulators undefined.\n");
1132 goto err_no_charger
;
1135 if (!desc
->psy_charger_stat
|| !desc
->psy_charger_stat
[0]) {
1136 dev_err(&pdev
->dev
, "No power supply defined.\n");
1138 goto err_no_charger_stat
;
1141 /* Counting index only */
1142 while (desc
->psy_charger_stat
[i
])
1145 cm
->charger_stat
= kzalloc(sizeof(struct power_supply
*) * (i
+ 1),
1147 if (!cm
->charger_stat
) {
1149 goto err_no_charger_stat
;
1152 for (i
= 0; desc
->psy_charger_stat
[i
]; i
++) {
1153 cm
->charger_stat
[i
] = power_supply_get_by_name(
1154 desc
->psy_charger_stat
[i
]);
1155 if (!cm
->charger_stat
[i
]) {
1156 dev_err(&pdev
->dev
, "Cannot find power supply "
1158 desc
->psy_charger_stat
[i
]);
1164 cm
->fuel_gauge
= power_supply_get_by_name(desc
->psy_fuel_gauge
);
1165 if (!cm
->fuel_gauge
) {
1166 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1167 desc
->psy_fuel_gauge
);
1172 if (desc
->polling_interval_ms
== 0 ||
1173 msecs_to_jiffies(desc
->polling_interval_ms
) <= CM_JIFFIES_SMALL
) {
1174 dev_err(&pdev
->dev
, "polling_interval_ms is too small\n");
1179 if (!desc
->temperature_out_of_range
) {
1180 dev_err(&pdev
->dev
, "there is no temperature_out_of_range\n");
1185 platform_set_drvdata(pdev
, cm
);
1187 memcpy(&cm
->charger_psy
, &psy_default
, sizeof(psy_default
));
1189 if (!desc
->psy_name
) {
1190 strncpy(cm
->psy_name_buf
, psy_default
.name
, PSY_NAME_MAX
);
1192 strncpy(cm
->psy_name_buf
, desc
->psy_name
, PSY_NAME_MAX
);
1194 cm
->charger_psy
.name
= cm
->psy_name_buf
;
1196 /* Allocate for psy properties because they may vary */
1197 cm
->charger_psy
.properties
= kzalloc(sizeof(enum power_supply_property
)
1198 * (ARRAY_SIZE(default_charger_props
) +
1199 NUM_CHARGER_PSY_OPTIONAL
),
1201 if (!cm
->charger_psy
.properties
) {
1202 dev_err(&pdev
->dev
, "Cannot allocate for psy properties.\n");
1206 memcpy(cm
->charger_psy
.properties
, default_charger_props
,
1207 sizeof(enum power_supply_property
) *
1208 ARRAY_SIZE(default_charger_props
));
1209 cm
->charger_psy
.num_properties
= psy_default
.num_properties
;
1211 /* Find which optional psy-properties are available */
1212 if (!cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
1213 POWER_SUPPLY_PROP_CHARGE_NOW
, &val
)) {
1214 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1215 POWER_SUPPLY_PROP_CHARGE_NOW
;
1216 cm
->charger_psy
.num_properties
++;
1218 if (!cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
1219 POWER_SUPPLY_PROP_CURRENT_NOW
,
1221 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1222 POWER_SUPPLY_PROP_CURRENT_NOW
;
1223 cm
->charger_psy
.num_properties
++;
1226 if (desc
->measure_battery_temp
) {
1227 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1228 POWER_SUPPLY_PROP_TEMP
;
1229 cm
->charger_psy
.num_properties
++;
1231 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1232 POWER_SUPPLY_PROP_TEMP_AMBIENT
;
1233 cm
->charger_psy
.num_properties
++;
1236 INIT_DELAYED_WORK(&cm
->fullbatt_vchk_work
, fullbatt_vchk
);
1238 ret
= power_supply_register(NULL
, &cm
->charger_psy
);
1240 dev_err(&pdev
->dev
, "Cannot register charger-manager with"
1241 " name \"%s\".\n", cm
->charger_psy
.name
);
1245 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
1246 struct charger_regulator
*charger
1247 = &desc
->charger_regulators
[i
];
1249 charger
->consumer
= regulator_get(&pdev
->dev
,
1250 charger
->regulator_name
);
1251 if (charger
->consumer
== NULL
) {
1252 dev_err(&pdev
->dev
, "Cannot find charger(%s)n",
1253 charger
->regulator_name
);
1258 for (j
= 0 ; j
< charger
->num_cables
; j
++) {
1259 struct charger_cable
*cable
= &charger
->cables
[j
];
1261 ret
= charger_extcon_init(cm
, cable
);
1263 dev_err(&pdev
->dev
, "Cannot find charger(%s)n",
1264 charger
->regulator_name
);
1267 cable
->charger
= charger
;
1272 ret
= try_charger_enable(cm
, true);
1274 dev_err(&pdev
->dev
, "Cannot enable charger regulators\n");
1275 goto err_chg_enable
;
1278 /* Add to the list */
1279 mutex_lock(&cm_list_mtx
);
1280 list_add(&cm
->entry
, &cm_list
);
1281 mutex_unlock(&cm_list_mtx
);
1284 * Charger-manager is capable of waking up the systme from sleep
1285 * when event is happend through cm_notify_event()
1287 device_init_wakeup(&pdev
->dev
, true);
1288 device_set_wakeup_capable(&pdev
->dev
, false);
1290 schedule_work(&setup_polling
);
1296 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
1297 struct charger_regulator
*charger
1298 = &desc
->charger_regulators
[i
];
1299 for (j
= 0 ; j
< charger
->num_cables
; j
++) {
1300 struct charger_cable
*cable
= &charger
->cables
[j
];
1301 extcon_unregister_interest(&cable
->extcon_dev
);
1305 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++)
1306 regulator_put(desc
->charger_regulators
[i
].consumer
);
1308 power_supply_unregister(&cm
->charger_psy
);
1310 kfree(cm
->charger_psy
.properties
);
1312 kfree(cm
->charger_stat
);
1313 err_no_charger_stat
:
1322 static int __devexit
charger_manager_remove(struct platform_device
*pdev
)
1324 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
1325 struct charger_desc
*desc
= cm
->desc
;
1329 /* Remove from the list */
1330 mutex_lock(&cm_list_mtx
);
1331 list_del(&cm
->entry
);
1332 mutex_unlock(&cm_list_mtx
);
1334 if (work_pending(&setup_polling
))
1335 cancel_work_sync(&setup_polling
);
1336 if (delayed_work_pending(&cm_monitor_work
))
1337 cancel_delayed_work_sync(&cm_monitor_work
);
1339 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
1340 struct charger_regulator
*charger
1341 = &desc
->charger_regulators
[i
];
1342 for (j
= 0 ; j
< charger
->num_cables
; j
++) {
1343 struct charger_cable
*cable
= &charger
->cables
[j
];
1344 extcon_unregister_interest(&cable
->extcon_dev
);
1348 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++)
1349 regulator_put(desc
->charger_regulators
[i
].consumer
);
1351 power_supply_unregister(&cm
->charger_psy
);
1353 try_charger_enable(cm
, false);
1355 kfree(cm
->charger_psy
.properties
);
1356 kfree(cm
->charger_stat
);
1363 static const struct platform_device_id charger_manager_id
[] = {
1364 { "charger-manager", 0 },
1367 MODULE_DEVICE_TABLE(platform
, charger_manager_id
);
1369 static int cm_suspend_noirq(struct device
*dev
)
1373 if (device_may_wakeup(dev
)) {
1374 device_set_wakeup_capable(dev
, false);
1381 static int cm_suspend_prepare(struct device
*dev
)
1383 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1385 if (!cm_suspended
) {
1387 struct rtc_time tmp
;
1390 rtc_read_alarm(rtc_dev
, &rtc_wkalarm_save
);
1391 rtc_read_time(rtc_dev
, &tmp
);
1393 if (rtc_wkalarm_save
.enabled
) {
1394 rtc_tm_to_time(&rtc_wkalarm_save
.time
,
1395 &rtc_wkalarm_save_time
);
1396 rtc_tm_to_time(&tmp
, &now
);
1397 if (now
> rtc_wkalarm_save_time
)
1398 rtc_wkalarm_save_time
= 0;
1400 rtc_wkalarm_save_time
= 0;
1403 cm_suspended
= true;
1406 if (delayed_work_pending(&cm
->fullbatt_vchk_work
))
1407 cancel_delayed_work(&cm
->fullbatt_vchk_work
);
1408 cm
->status_save_ext_pwr_inserted
= is_ext_pwr_online(cm
);
1409 cm
->status_save_batt
= is_batt_present(cm
);
1412 cm_suspend_duration_ms
= 0;
1413 cm_rtc_set
= cm_setup_timer();
1419 static void cm_suspend_complete(struct device
*dev
)
1421 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1425 struct rtc_wkalrm tmp
;
1427 rtc_read_alarm(rtc_dev
, &tmp
);
1428 rtc_wkalarm_save
.pending
= tmp
.pending
;
1429 rtc_set_alarm(rtc_dev
, &rtc_wkalarm_save
);
1431 cm_suspended
= false;
1435 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1436 if (cm
->fullbatt_vchk_jiffies_at
) {
1437 unsigned long delay
= 0;
1438 unsigned long now
= jiffies
+ CM_JIFFIES_SMALL
;
1440 if (time_after_eq(now
, cm
->fullbatt_vchk_jiffies_at
)) {
1441 delay
= (unsigned long)((long)now
1442 - (long)(cm
->fullbatt_vchk_jiffies_at
));
1443 delay
= jiffies_to_msecs(delay
);
1449 * Account for cm_suspend_duration_ms if
1450 * assume_timer_stops_in_suspend is active
1452 if (g_desc
&& g_desc
->assume_timer_stops_in_suspend
) {
1453 if (delay
> cm_suspend_duration_ms
)
1454 delay
-= cm_suspend_duration_ms
;
1459 queue_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
1460 msecs_to_jiffies(delay
));
1462 device_set_wakeup_capable(cm
->dev
, false);
1463 uevent_notify(cm
, NULL
);
1466 static const struct dev_pm_ops charger_manager_pm
= {
1467 .prepare
= cm_suspend_prepare
,
1468 .suspend_noirq
= cm_suspend_noirq
,
1469 .complete
= cm_suspend_complete
,
1472 static struct platform_driver charger_manager_driver
= {
1474 .name
= "charger-manager",
1475 .owner
= THIS_MODULE
,
1476 .pm
= &charger_manager_pm
,
1478 .probe
= charger_manager_probe
,
1479 .remove
= __devexit_p(charger_manager_remove
),
1480 .id_table
= charger_manager_id
,
1483 static int __init
charger_manager_init(void)
1485 cm_wq
= create_freezable_workqueue("charger_manager");
1486 INIT_DELAYED_WORK(&cm_monitor_work
, cm_monitor_poller
);
1488 return platform_driver_register(&charger_manager_driver
);
1490 late_initcall(charger_manager_init
);
1492 static void __exit
charger_manager_cleanup(void)
1494 destroy_workqueue(cm_wq
);
1497 platform_driver_unregister(&charger_manager_driver
);
1499 module_exit(charger_manager_cleanup
);
1502 * find_power_supply - find the associated power_supply of charger
1503 * @cm: the Charger Manager representing the battery
1504 * @psy: pointer to instance of charger's power_supply
1506 static bool find_power_supply(struct charger_manager
*cm
,
1507 struct power_supply
*psy
)
1512 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
1513 if (psy
== cm
->charger_stat
[i
]) {
1523 * cm_notify_event - charger driver notify Charger Manager of charger event
1524 * @psy: pointer to instance of charger's power_supply
1525 * @type: type of charger event
1526 * @msg: optional message passed to uevent_notify fuction
1528 void cm_notify_event(struct power_supply
*psy
, enum cm_event_types type
,
1531 struct charger_manager
*cm
;
1532 bool found_power_supply
= false;
1537 mutex_lock(&cm_list_mtx
);
1538 list_for_each_entry(cm
, &cm_list
, entry
) {
1539 found_power_supply
= find_power_supply(cm
, psy
);
1540 if (found_power_supply
)
1543 mutex_unlock(&cm_list_mtx
);
1545 if (!found_power_supply
)
1549 case CM_EVENT_BATT_FULL
:
1550 fullbatt_handler(cm
);
1552 case CM_EVENT_BATT_OUT
:
1553 battout_handler(cm
);
1555 case CM_EVENT_BATT_IN
:
1556 case CM_EVENT_EXT_PWR_IN_OUT
... CM_EVENT_CHG_START_STOP
:
1557 misc_event_handler(cm
, type
);
1559 case CM_EVENT_UNKNOWN
:
1560 case CM_EVENT_OTHERS
:
1561 uevent_notify(cm
, msg
? msg
: default_event_names
[type
]);
1564 dev_err(cm
->dev
, "%s type not specified.\n", __func__
);
1568 EXPORT_SYMBOL_GPL(cm_notify_event
);
1570 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1571 MODULE_DESCRIPTION("Charger Manager");
1572 MODULE_LICENSE("GPL");