1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
4 * MyungJoo Ham <myungjoo.ham@samsung.com>
6 * This driver enables to monitor battery health and control charger
7 * during suspend-to-mem.
8 * Charger manager depends on other devices. Register this later than
9 * the depending devices.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 #include <linux/sysfs.h>
27 #include <linux/thermal.h>
32 } extcon_mapping
[] = {
33 /* Current textual representations */
34 { "USB", EXTCON_USB
},
35 { "USB-HOST", EXTCON_USB_HOST
},
36 { "SDP", EXTCON_CHG_USB_SDP
},
37 { "DCP", EXTCON_CHG_USB_DCP
},
38 { "CDP", EXTCON_CHG_USB_CDP
},
39 { "ACA", EXTCON_CHG_USB_ACA
},
40 { "FAST-CHARGER", EXTCON_CHG_USB_FAST
},
41 { "SLOW-CHARGER", EXTCON_CHG_USB_SLOW
},
42 { "WPT", EXTCON_CHG_WPT
},
43 { "PD", EXTCON_CHG_USB_PD
},
44 { "DOCK", EXTCON_DOCK
},
45 { "JIG", EXTCON_JIG
},
46 { "MECHANICAL", EXTCON_MECHANICAL
},
47 /* Deprecated textual representations */
48 { "TA", EXTCON_CHG_USB_SDP
},
49 { "CHARGE-DOWNSTREAM", EXTCON_CHG_USB_CDP
},
53 * Default temperature threshold for charging.
54 * Every temperature units are in tenth of centigrade.
56 #define CM_DEFAULT_RECHARGE_TEMP_DIFF 50
57 #define CM_DEFAULT_CHARGE_TEMP_MAX 500
60 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
61 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
64 #define CM_JIFFIES_SMALL (2)
66 /* If y is valid (> 0) and smaller than x, do x = y */
67 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
70 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
71 * rtc alarm. It should be 2 or larger
73 #define CM_RTC_SMALL (2)
75 static LIST_HEAD(cm_list
);
76 static DEFINE_MUTEX(cm_list_mtx
);
78 /* About in-suspend (suspend-again) monitoring */
79 static struct alarm
*cm_timer
;
81 static bool cm_suspended
;
82 static bool cm_timer_set
;
83 static unsigned long cm_suspend_duration_ms
;
85 /* About normal (not suspended) monitoring */
86 static unsigned long polling_jiffy
= ULONG_MAX
; /* ULONG_MAX: no polling */
87 static unsigned long next_polling
; /* Next appointed polling time */
88 static struct workqueue_struct
*cm_wq
; /* init at driver add */
89 static struct delayed_work cm_monitor_work
; /* init at driver add */
92 * is_batt_present - See if the battery presents in place.
93 * @cm: the Charger Manager representing the battery.
95 static bool is_batt_present(struct charger_manager
*cm
)
97 union power_supply_propval val
;
98 struct power_supply
*psy
;
102 switch (cm
->desc
->battery_present
) {
103 case CM_BATTERY_PRESENT
:
109 psy
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
113 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_PRESENT
,
115 if (ret
== 0 && val
.intval
)
117 power_supply_put(psy
);
119 case CM_CHARGER_STAT
:
120 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
121 psy
= power_supply_get_by_name(
122 cm
->desc
->psy_charger_stat
[i
]);
124 dev_err(cm
->dev
, "Cannot find power supply \"%s\"\n",
125 cm
->desc
->psy_charger_stat
[i
]);
129 ret
= power_supply_get_property(psy
,
130 POWER_SUPPLY_PROP_PRESENT
, &val
);
131 power_supply_put(psy
);
132 if (ret
== 0 && val
.intval
) {
144 * is_ext_pwr_online - See if an external power source is attached to charge
145 * @cm: the Charger Manager representing the battery.
147 * Returns true if at least one of the chargers of the battery has an external
148 * power source attached to charge the battery regardless of whether it is
149 * actually charging or not.
151 static bool is_ext_pwr_online(struct charger_manager
*cm
)
153 union power_supply_propval val
;
154 struct power_supply
*psy
;
158 /* If at least one of them has one, it's yes. */
159 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
160 psy
= power_supply_get_by_name(cm
->desc
->psy_charger_stat
[i
]);
162 dev_err(cm
->dev
, "Cannot find power supply \"%s\"\n",
163 cm
->desc
->psy_charger_stat
[i
]);
167 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_ONLINE
,
169 power_supply_put(psy
);
170 if (ret
== 0 && val
.intval
) {
180 * get_batt_uV - Get the voltage level of the battery
181 * @cm: the Charger Manager representing the battery.
182 * @uV: the voltage level returned.
184 * Returns 0 if there is no error.
185 * Returns a negative value on error.
187 static int get_batt_uV(struct charger_manager
*cm
, int *uV
)
189 union power_supply_propval val
;
190 struct power_supply
*fuel_gauge
;
193 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
197 ret
= power_supply_get_property(fuel_gauge
,
198 POWER_SUPPLY_PROP_VOLTAGE_NOW
, &val
);
199 power_supply_put(fuel_gauge
);
208 * is_charging - Returns true if the battery is being charged.
209 * @cm: the Charger Manager representing the battery.
211 static bool is_charging(struct charger_manager
*cm
)
214 bool charging
= false;
215 struct power_supply
*psy
;
216 union power_supply_propval val
;
218 /* If there is no battery, it cannot be charged */
219 if (!is_batt_present(cm
))
222 /* If at least one of the charger is charging, return yes */
223 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
224 /* 1. The charger should not be DISABLED */
225 if (cm
->emergency_stop
)
227 if (!cm
->charger_enabled
)
230 psy
= power_supply_get_by_name(cm
->desc
->psy_charger_stat
[i
]);
232 dev_err(cm
->dev
, "Cannot find power supply \"%s\"\n",
233 cm
->desc
->psy_charger_stat
[i
]);
237 /* 2. The charger should be online (ext-power) */
238 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_ONLINE
,
241 dev_warn(cm
->dev
, "Cannot read ONLINE value from %s\n",
242 cm
->desc
->psy_charger_stat
[i
]);
243 power_supply_put(psy
);
246 if (val
.intval
== 0) {
247 power_supply_put(psy
);
252 * 3. The charger should not be FULL, DISCHARGING,
255 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_STATUS
,
257 power_supply_put(psy
);
259 dev_warn(cm
->dev
, "Cannot read STATUS value from %s\n",
260 cm
->desc
->psy_charger_stat
[i
]);
263 if (val
.intval
== POWER_SUPPLY_STATUS_FULL
||
264 val
.intval
== POWER_SUPPLY_STATUS_DISCHARGING
||
265 val
.intval
== POWER_SUPPLY_STATUS_NOT_CHARGING
)
268 /* Then, this is charging. */
277 * is_full_charged - Returns true if the battery is fully charged.
278 * @cm: the Charger Manager representing the battery.
280 static bool is_full_charged(struct charger_manager
*cm
)
282 struct charger_desc
*desc
= cm
->desc
;
283 union power_supply_propval val
;
284 struct power_supply
*fuel_gauge
;
285 bool is_full
= false;
289 /* If there is no battery, it cannot be charged */
290 if (!is_batt_present(cm
))
293 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
297 /* Full, if it's over the fullbatt voltage */
298 if (desc
->fullbatt_uV
> 0) {
299 ret
= get_batt_uV(cm
, &uV
);
301 /* Battery is already full, checks voltage drop. */
302 if (cm
->battery_status
== POWER_SUPPLY_STATUS_FULL
303 && desc
->fullbatt_vchkdrop_uV
)
304 uV
+= desc
->fullbatt_vchkdrop_uV
;
305 if (uV
>= desc
->fullbatt_uV
)
310 if (desc
->fullbatt_full_capacity
> 0) {
313 /* Not full if capacity of fuel gauge isn't full */
314 ret
= power_supply_get_property(fuel_gauge
,
315 POWER_SUPPLY_PROP_CHARGE_FULL
, &val
);
316 if (!ret
&& val
.intval
> desc
->fullbatt_full_capacity
) {
322 /* Full, if the capacity is more than fullbatt_soc */
323 if (desc
->fullbatt_soc
> 0) {
326 ret
= power_supply_get_property(fuel_gauge
,
327 POWER_SUPPLY_PROP_CAPACITY
, &val
);
328 if (!ret
&& val
.intval
>= desc
->fullbatt_soc
) {
335 power_supply_put(fuel_gauge
);
340 * is_polling_required - Return true if need to continue polling for this CM.
341 * @cm: the Charger Manager representing the battery.
343 static bool is_polling_required(struct charger_manager
*cm
)
345 switch (cm
->desc
->polling_mode
) {
346 case CM_POLL_DISABLE
:
350 case CM_POLL_EXTERNAL_POWER_ONLY
:
351 return is_ext_pwr_online(cm
);
352 case CM_POLL_CHARGING_ONLY
:
353 return is_charging(cm
);
355 dev_warn(cm
->dev
, "Incorrect polling_mode (%d)\n",
356 cm
->desc
->polling_mode
);
363 * try_charger_enable - Enable/Disable chargers altogether
364 * @cm: the Charger Manager representing the battery.
365 * @enable: true: enable / false: disable
367 * Note that Charger Manager keeps the charger enabled regardless whether
368 * the charger is charging or not (because battery is full or no external
369 * power source exists) except when CM needs to disable chargers forcibly
370 * because of emergency causes; when the battery is overheated or too cold.
372 static int try_charger_enable(struct charger_manager
*cm
, bool enable
)
375 struct charger_desc
*desc
= cm
->desc
;
377 /* Ignore if it's redundant command */
378 if (enable
== cm
->charger_enabled
)
382 if (cm
->emergency_stop
)
386 * Save start time of charging to limit
387 * maximum possible charging time.
389 cm
->charging_start_time
= ktime_to_ms(ktime_get());
390 cm
->charging_end_time
= 0;
392 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
393 if (desc
->charger_regulators
[i
].externally_control
)
396 err
= regulator_enable(desc
->charger_regulators
[i
].consumer
);
398 dev_warn(cm
->dev
, "Cannot enable %s regulator\n",
399 desc
->charger_regulators
[i
].regulator_name
);
404 * Save end time of charging to maintain fully charged state
405 * of battery after full-batt.
407 cm
->charging_start_time
= 0;
408 cm
->charging_end_time
= ktime_to_ms(ktime_get());
410 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
411 if (desc
->charger_regulators
[i
].externally_control
)
414 err
= regulator_disable(desc
->charger_regulators
[i
].consumer
);
416 dev_warn(cm
->dev
, "Cannot disable %s regulator\n",
417 desc
->charger_regulators
[i
].regulator_name
);
422 * Abnormal battery state - Stop charging forcibly,
423 * even if charger was enabled at the other places
425 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
426 if (regulator_is_enabled(
427 desc
->charger_regulators
[i
].consumer
)) {
428 regulator_force_disable(
429 desc
->charger_regulators
[i
].consumer
);
430 dev_warn(cm
->dev
, "Disable regulator(%s) forcibly\n",
431 desc
->charger_regulators
[i
].regulator_name
);
437 cm
->charger_enabled
= enable
;
443 * check_charging_duration - Monitor charging/discharging duration
444 * @cm: the Charger Manager representing the battery.
446 * If whole charging duration exceed 'charging_max_duration_ms',
447 * cm stop charging to prevent overcharge/overheat. If discharging
448 * duration exceed 'discharging _max_duration_ms', charger cable is
449 * attached, after full-batt, cm start charging to maintain fully
450 * charged state for battery.
452 static int check_charging_duration(struct charger_manager
*cm
)
454 struct charger_desc
*desc
= cm
->desc
;
455 u64 curr
= ktime_to_ms(ktime_get());
459 if (!desc
->charging_max_duration_ms
&&
460 !desc
->discharging_max_duration_ms
)
463 if (cm
->charger_enabled
) {
464 duration
= curr
- cm
->charging_start_time
;
466 if (duration
> desc
->charging_max_duration_ms
) {
467 dev_info(cm
->dev
, "Charging duration exceed %ums\n",
468 desc
->charging_max_duration_ms
);
471 } else if (cm
->battery_status
== POWER_SUPPLY_STATUS_NOT_CHARGING
) {
472 duration
= curr
- cm
->charging_end_time
;
474 if (duration
> desc
->discharging_max_duration_ms
) {
475 dev_info(cm
->dev
, "Discharging duration exceed %ums\n",
476 desc
->discharging_max_duration_ms
);
484 static int cm_get_battery_temperature_by_psy(struct charger_manager
*cm
,
487 struct power_supply
*fuel_gauge
;
490 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
494 ret
= power_supply_get_property(fuel_gauge
,
495 POWER_SUPPLY_PROP_TEMP
,
496 (union power_supply_propval
*)temp
);
497 power_supply_put(fuel_gauge
);
502 static int cm_get_battery_temperature(struct charger_manager
*cm
,
507 if (!cm
->desc
->measure_battery_temp
)
510 #ifdef CONFIG_THERMAL
512 ret
= thermal_zone_get_temp(cm
->tzd_batt
, temp
);
514 /* Calibrate temperature unit */
519 /* if-else continued from CONFIG_THERMAL */
520 ret
= cm_get_battery_temperature_by_psy(cm
, temp
);
526 static int cm_check_thermal_status(struct charger_manager
*cm
)
528 struct charger_desc
*desc
= cm
->desc
;
529 int temp
, upper_limit
, lower_limit
;
532 ret
= cm_get_battery_temperature(cm
, &temp
);
535 * No information of battery temperature might
536 * occur hazardous result. We have to handle it
537 * depending on battery type.
539 dev_err(cm
->dev
, "Failed to get battery temperature\n");
543 upper_limit
= desc
->temp_max
;
544 lower_limit
= desc
->temp_min
;
546 if (cm
->emergency_stop
) {
547 upper_limit
-= desc
->temp_diff
;
548 lower_limit
+= desc
->temp_diff
;
551 if (temp
> upper_limit
)
552 ret
= CM_BATT_OVERHEAT
;
553 else if (temp
< lower_limit
)
558 cm
->emergency_stop
= ret
;
564 * cm_get_target_status - Check current status and get next target status.
565 * @cm: the Charger Manager representing the battery.
567 static int cm_get_target_status(struct charger_manager
*cm
)
569 if (!is_ext_pwr_online(cm
))
570 return POWER_SUPPLY_STATUS_DISCHARGING
;
572 if (cm_check_thermal_status(cm
)) {
573 /* Check if discharging duration exceeds limit. */
574 if (check_charging_duration(cm
))
576 return POWER_SUPPLY_STATUS_NOT_CHARGING
;
579 switch (cm
->battery_status
) {
580 case POWER_SUPPLY_STATUS_CHARGING
:
581 /* Check if charging duration exceeds limit. */
582 if (check_charging_duration(cm
))
583 return POWER_SUPPLY_STATUS_FULL
;
585 case POWER_SUPPLY_STATUS_FULL
:
586 if (is_full_charged(cm
))
587 return POWER_SUPPLY_STATUS_FULL
;
594 /* Charging is allowed. */
595 return POWER_SUPPLY_STATUS_CHARGING
;
599 * _cm_monitor - Monitor the temperature and return true for exceptions.
600 * @cm: the Charger Manager representing the battery.
602 * Returns true if there is an event to notify for the battery.
603 * (True if the status of "emergency_stop" changes)
605 static bool _cm_monitor(struct charger_manager
*cm
)
609 target
= cm_get_target_status(cm
);
611 try_charger_enable(cm
, (target
== POWER_SUPPLY_STATUS_CHARGING
));
613 if (cm
->battery_status
!= target
) {
614 cm
->battery_status
= target
;
615 power_supply_changed(cm
->charger_psy
);
618 return (cm
->battery_status
== POWER_SUPPLY_STATUS_NOT_CHARGING
);
622 * cm_monitor - Monitor every battery.
624 * Returns true if there is an event to notify from any of the batteries.
625 * (True if the status of "emergency_stop" changes)
627 static bool cm_monitor(void)
630 struct charger_manager
*cm
;
632 mutex_lock(&cm_list_mtx
);
634 list_for_each_entry(cm
, &cm_list
, entry
) {
639 mutex_unlock(&cm_list_mtx
);
645 * _setup_polling - Setup the next instance of polling.
646 * @work: work_struct of the function _setup_polling.
648 static void _setup_polling(struct work_struct
*work
)
650 unsigned long min
= ULONG_MAX
;
651 struct charger_manager
*cm
;
652 bool keep_polling
= false;
653 unsigned long _next_polling
;
655 mutex_lock(&cm_list_mtx
);
657 list_for_each_entry(cm
, &cm_list
, entry
) {
658 if (is_polling_required(cm
) && cm
->desc
->polling_interval_ms
) {
661 if (min
> cm
->desc
->polling_interval_ms
)
662 min
= cm
->desc
->polling_interval_ms
;
666 polling_jiffy
= msecs_to_jiffies(min
);
667 if (polling_jiffy
<= CM_JIFFIES_SMALL
)
668 polling_jiffy
= CM_JIFFIES_SMALL
+ 1;
671 polling_jiffy
= ULONG_MAX
;
672 if (polling_jiffy
== ULONG_MAX
)
675 WARN(cm_wq
== NULL
, "charger-manager: workqueue not initialized"
676 ". try it later. %s\n", __func__
);
679 * Use mod_delayed_work() iff the next polling interval should
680 * occur before the currently scheduled one. If @cm_monitor_work
681 * isn't active, the end result is the same, so no need to worry
682 * about stale @next_polling.
684 _next_polling
= jiffies
+ polling_jiffy
;
686 if (time_before(_next_polling
, next_polling
)) {
687 mod_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
);
688 next_polling
= _next_polling
;
690 if (queue_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
))
691 next_polling
= _next_polling
;
694 mutex_unlock(&cm_list_mtx
);
696 static DECLARE_WORK(setup_polling
, _setup_polling
);
699 * cm_monitor_poller - The Monitor / Poller.
700 * @work: work_struct of the function cm_monitor_poller
702 * During non-suspended state, cm_monitor_poller is used to poll and monitor
705 static void cm_monitor_poller(struct work_struct
*work
)
708 schedule_work(&setup_polling
);
711 static int charger_get_property(struct power_supply
*psy
,
712 enum power_supply_property psp
,
713 union power_supply_propval
*val
)
715 struct charger_manager
*cm
= power_supply_get_drvdata(psy
);
716 struct charger_desc
*desc
= cm
->desc
;
717 struct power_supply
*fuel_gauge
= NULL
;
722 case POWER_SUPPLY_PROP_STATUS
:
723 val
->intval
= cm
->battery_status
;
725 case POWER_SUPPLY_PROP_HEALTH
:
726 if (cm
->emergency_stop
== CM_BATT_OVERHEAT
)
727 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
728 else if (cm
->emergency_stop
== CM_BATT_COLD
)
729 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
731 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
733 case POWER_SUPPLY_PROP_PRESENT
:
734 if (is_batt_present(cm
))
739 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
740 ret
= get_batt_uV(cm
, &val
->intval
);
742 case POWER_SUPPLY_PROP_CURRENT_NOW
:
743 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
748 ret
= power_supply_get_property(fuel_gauge
,
749 POWER_SUPPLY_PROP_CURRENT_NOW
, val
);
751 case POWER_SUPPLY_PROP_TEMP
:
752 return cm_get_battery_temperature(cm
, &val
->intval
);
753 case POWER_SUPPLY_PROP_CAPACITY
:
754 if (!is_batt_present(cm
)) {
755 /* There is no battery. Assume 100% */
760 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
766 ret
= power_supply_get_property(fuel_gauge
,
767 POWER_SUPPLY_PROP_CAPACITY
, val
);
771 if (val
->intval
> 100) {
778 /* Do not adjust SOC when charging: voltage is overrated */
783 * If the capacity value is inconsistent, calibrate it base on
784 * the battery voltage values and the thresholds given as desc
786 ret
= get_batt_uV(cm
, &uV
);
788 /* Voltage information not available. No calibration */
793 if (desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
800 case POWER_SUPPLY_PROP_ONLINE
:
801 if (is_ext_pwr_online(cm
))
806 case POWER_SUPPLY_PROP_CHARGE_FULL
:
807 case POWER_SUPPLY_PROP_CHARGE_NOW
:
808 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
813 ret
= power_supply_get_property(fuel_gauge
, psp
, val
);
819 power_supply_put(fuel_gauge
);
823 #define NUM_CHARGER_PSY_OPTIONAL (4)
824 static enum power_supply_property default_charger_props
[] = {
825 /* Guaranteed to provide */
826 POWER_SUPPLY_PROP_STATUS
,
827 POWER_SUPPLY_PROP_HEALTH
,
828 POWER_SUPPLY_PROP_PRESENT
,
829 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
830 POWER_SUPPLY_PROP_CAPACITY
,
831 POWER_SUPPLY_PROP_ONLINE
,
833 * Optional properties are:
834 * POWER_SUPPLY_PROP_CHARGE_FULL,
835 * POWER_SUPPLY_PROP_CHARGE_NOW,
836 * POWER_SUPPLY_PROP_CURRENT_NOW,
837 * POWER_SUPPLY_PROP_TEMP,
841 static const struct power_supply_desc psy_default
= {
843 .type
= POWER_SUPPLY_TYPE_BATTERY
,
844 .properties
= default_charger_props
,
845 .num_properties
= ARRAY_SIZE(default_charger_props
),
846 .get_property
= charger_get_property
,
851 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
854 * Returns true if the alarm is set for Charger Manager to use.
856 * cm_setup_timer fails to set an alarm,
857 * cm_setup_timer does not need to set an alarm for Charger Manager,
858 * or an alarm previously configured is to be used.
860 static bool cm_setup_timer(void)
862 struct charger_manager
*cm
;
863 unsigned int wakeup_ms
= UINT_MAX
;
866 if (time_after(next_polling
, jiffies
))
867 CM_MIN_VALID(wakeup_ms
,
868 jiffies_to_msecs(next_polling
- jiffies
));
870 mutex_lock(&cm_list_mtx
);
871 list_for_each_entry(cm
, &cm_list
, entry
) {
872 /* Skip if polling is not required for this CM */
873 if (!is_polling_required(cm
) && !cm
->emergency_stop
)
876 if (cm
->desc
->polling_interval_ms
== 0)
878 CM_MIN_VALID(wakeup_ms
, cm
->desc
->polling_interval_ms
);
880 mutex_unlock(&cm_list_mtx
);
882 if (timer_req
&& cm_timer
) {
886 * Set alarm with the polling interval (wakeup_ms)
887 * The alarm time should be NOW + CM_RTC_SMALL or later.
889 if (wakeup_ms
== UINT_MAX
||
890 wakeup_ms
< CM_RTC_SMALL
* MSEC_PER_SEC
)
891 wakeup_ms
= 2 * CM_RTC_SMALL
* MSEC_PER_SEC
;
893 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms
);
895 now
= ktime_get_boottime();
896 add
= ktime_set(wakeup_ms
/ MSEC_PER_SEC
,
897 (wakeup_ms
% MSEC_PER_SEC
) * NSEC_PER_MSEC
);
898 alarm_start(cm_timer
, ktime_add(now
, add
));
900 cm_suspend_duration_ms
= wakeup_ms
;
908 * charger_extcon_work - enable/diable charger according to the state
911 * @work: work_struct of the function charger_extcon_work.
913 static void charger_extcon_work(struct work_struct
*work
)
915 struct charger_cable
*cable
=
916 container_of(work
, struct charger_cable
, wq
);
919 if (cable
->attached
&& cable
->min_uA
!= 0 && cable
->max_uA
!= 0) {
920 ret
= regulator_set_current_limit(cable
->charger
->consumer
,
921 cable
->min_uA
, cable
->max_uA
);
923 pr_err("Cannot set current limit of %s (%s)\n",
924 cable
->charger
->regulator_name
, cable
->name
);
928 pr_info("Set current limit of %s : %duA ~ %duA\n",
929 cable
->charger
->regulator_name
,
930 cable
->min_uA
, cable
->max_uA
);
933 cancel_delayed_work(&cm_monitor_work
);
934 queue_delayed_work(cm_wq
, &cm_monitor_work
, 0);
938 * charger_extcon_notifier - receive the state of charger cable
939 * when registered cable is attached or detached.
941 * @self: the notifier block of the charger_extcon_notifier.
942 * @event: the cable state.
943 * @ptr: the data pointer of notifier block.
945 static int charger_extcon_notifier(struct notifier_block
*self
,
946 unsigned long event
, void *ptr
)
948 struct charger_cable
*cable
=
949 container_of(self
, struct charger_cable
, nb
);
952 * The newly state of charger cable.
953 * If cable is attached, cable->attached is true.
955 cable
->attached
= event
;
958 * Setup work for controlling charger(regulator)
959 * according to charger cable.
961 schedule_work(&cable
->wq
);
967 * charger_extcon_init - register external connector to use it
968 * as the charger cable
970 * @cm: the Charger Manager representing the battery.
971 * @cable: the Charger cable representing the external connector.
973 static int charger_extcon_init(struct charger_manager
*cm
,
974 struct charger_cable
*cable
)
977 u64 extcon_type
= EXTCON_NONE
;
980 * Charger manager use Extcon framework to identify
981 * the charger cable among various external connector
982 * cable (e.g., TA, USB, MHL, Dock).
984 INIT_WORK(&cable
->wq
, charger_extcon_work
);
985 cable
->nb
.notifier_call
= charger_extcon_notifier
;
987 cable
->extcon_dev
= extcon_get_extcon_dev(cable
->extcon_name
);
988 if (IS_ERR(cable
->extcon_dev
)) {
989 pr_err("Cannot find extcon_dev for %s (cable: %s)\n",
990 cable
->extcon_name
, cable
->name
);
991 return PTR_ERR(cable
->extcon_dev
);
994 for (i
= 0; i
< ARRAY_SIZE(extcon_mapping
); i
++) {
995 if (!strcmp(cable
->name
, extcon_mapping
[i
].name
)) {
996 extcon_type
= extcon_mapping
[i
].extcon_type
;
1000 if (extcon_type
== EXTCON_NONE
) {
1001 pr_err("Cannot find cable for type %s", cable
->name
);
1005 cable
->extcon_type
= extcon_type
;
1007 ret
= devm_extcon_register_notifier(cm
->dev
, cable
->extcon_dev
,
1008 cable
->extcon_type
, &cable
->nb
);
1010 pr_err("Cannot register extcon_dev for %s (cable: %s)\n",
1011 cable
->extcon_name
, cable
->name
);
1019 * charger_manager_register_extcon - Register extcon device to receive state
1021 * @cm: the Charger Manager representing the battery.
1023 * This function support EXTCON(External Connector) subsystem to detect the
1024 * state of charger cables for enabling or disabling charger(regulator) and
1025 * select the charger cable for charging among a number of external cable
1026 * according to policy of H/W board.
1028 static int charger_manager_register_extcon(struct charger_manager
*cm
)
1030 struct charger_desc
*desc
= cm
->desc
;
1031 struct charger_regulator
*charger
;
1032 unsigned long event
;
1037 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1038 charger
= &desc
->charger_regulators
[i
];
1040 charger
->consumer
= regulator_get(cm
->dev
,
1041 charger
->regulator_name
);
1042 if (IS_ERR(charger
->consumer
)) {
1043 dev_err(cm
->dev
, "Cannot find charger(%s)\n",
1044 charger
->regulator_name
);
1045 return PTR_ERR(charger
->consumer
);
1049 for (j
= 0; j
< charger
->num_cables
; j
++) {
1050 struct charger_cable
*cable
= &charger
->cables
[j
];
1052 ret
= charger_extcon_init(cm
, cable
);
1054 dev_err(cm
->dev
, "Cannot initialize charger(%s)\n",
1055 charger
->regulator_name
);
1058 cable
->charger
= charger
;
1061 event
= extcon_get_state(cable
->extcon_dev
,
1062 cable
->extcon_type
);
1063 charger_extcon_notifier(&cable
->nb
,
1071 /* help function of sysfs node to control charger(regulator) */
1072 static ssize_t
charger_name_show(struct device
*dev
,
1073 struct device_attribute
*attr
, char *buf
)
1075 struct charger_regulator
*charger
1076 = container_of(attr
, struct charger_regulator
, attr_name
);
1078 return sysfs_emit(buf
, "%s\n", charger
->regulator_name
);
1081 static ssize_t
charger_state_show(struct device
*dev
,
1082 struct device_attribute
*attr
, char *buf
)
1084 struct charger_regulator
*charger
1085 = container_of(attr
, struct charger_regulator
, attr_state
);
1088 if (!charger
->externally_control
)
1089 state
= regulator_is_enabled(charger
->consumer
);
1091 return sysfs_emit(buf
, "%s\n", state
? "enabled" : "disabled");
1094 static ssize_t
charger_externally_control_show(struct device
*dev
,
1095 struct device_attribute
*attr
, char *buf
)
1097 struct charger_regulator
*charger
= container_of(attr
,
1098 struct charger_regulator
, attr_externally_control
);
1100 return sysfs_emit(buf
, "%d\n", charger
->externally_control
);
1103 static ssize_t
charger_externally_control_store(struct device
*dev
,
1104 struct device_attribute
*attr
, const char *buf
,
1107 struct charger_regulator
*charger
1108 = container_of(attr
, struct charger_regulator
,
1109 attr_externally_control
);
1110 struct charger_manager
*cm
= charger
->cm
;
1111 struct charger_desc
*desc
= cm
->desc
;
1114 int externally_control
;
1115 int chargers_externally_control
= 1;
1117 ret
= sscanf(buf
, "%d", &externally_control
);
1123 if (!externally_control
) {
1124 charger
->externally_control
= 0;
1128 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1129 if (&desc
->charger_regulators
[i
] != charger
&&
1130 !desc
->charger_regulators
[i
].externally_control
) {
1132 * At least, one charger is controlled by
1135 chargers_externally_control
= 0;
1140 if (!chargers_externally_control
) {
1141 if (cm
->charger_enabled
) {
1142 try_charger_enable(charger
->cm
, false);
1143 charger
->externally_control
= externally_control
;
1144 try_charger_enable(charger
->cm
, true);
1146 charger
->externally_control
= externally_control
;
1150 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1151 charger
->regulator_name
);
1158 * charger_manager_prepare_sysfs - Prepare sysfs entry for each charger
1159 * @cm: the Charger Manager representing the battery.
1161 * This function add sysfs entry for charger(regulator) to control charger from
1162 * user-space. If some development board use one more chargers for charging
1163 * but only need one charger on specific case which is dependent on user
1164 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1165 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1166 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1167 * externally_control, this charger isn't controlled from charger-manager and
1168 * always stay off state of regulator.
1170 static int charger_manager_prepare_sysfs(struct charger_manager
*cm
)
1172 struct charger_desc
*desc
= cm
->desc
;
1173 struct charger_regulator
*charger
;
1174 int chargers_externally_control
= 1;
1178 /* Create sysfs entry to control charger(regulator) */
1179 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1180 charger
= &desc
->charger_regulators
[i
];
1182 name
= devm_kasprintf(cm
->dev
, GFP_KERNEL
, "charger.%d", i
);
1186 charger
->attrs
[0] = &charger
->attr_name
.attr
;
1187 charger
->attrs
[1] = &charger
->attr_state
.attr
;
1188 charger
->attrs
[2] = &charger
->attr_externally_control
.attr
;
1189 charger
->attrs
[3] = NULL
;
1191 charger
->attr_grp
.name
= name
;
1192 charger
->attr_grp
.attrs
= charger
->attrs
;
1193 desc
->sysfs_groups
[i
] = &charger
->attr_grp
;
1195 sysfs_attr_init(&charger
->attr_name
.attr
);
1196 charger
->attr_name
.attr
.name
= "name";
1197 charger
->attr_name
.attr
.mode
= 0444;
1198 charger
->attr_name
.show
= charger_name_show
;
1200 sysfs_attr_init(&charger
->attr_state
.attr
);
1201 charger
->attr_state
.attr
.name
= "state";
1202 charger
->attr_state
.attr
.mode
= 0444;
1203 charger
->attr_state
.show
= charger_state_show
;
1205 sysfs_attr_init(&charger
->attr_externally_control
.attr
);
1206 charger
->attr_externally_control
.attr
.name
1207 = "externally_control";
1208 charger
->attr_externally_control
.attr
.mode
= 0644;
1209 charger
->attr_externally_control
.show
1210 = charger_externally_control_show
;
1211 charger
->attr_externally_control
.store
1212 = charger_externally_control_store
;
1214 if (!desc
->charger_regulators
[i
].externally_control
||
1215 !chargers_externally_control
)
1216 chargers_externally_control
= 0;
1218 dev_info(cm
->dev
, "'%s' regulator's externally_control is %d\n",
1219 charger
->regulator_name
, charger
->externally_control
);
1222 if (chargers_externally_control
) {
1223 dev_err(cm
->dev
, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n");
1230 static int cm_init_thermal_data(struct charger_manager
*cm
,
1231 struct power_supply
*fuel_gauge
,
1232 enum power_supply_property
*properties
,
1233 size_t *num_properties
)
1235 struct charger_desc
*desc
= cm
->desc
;
1236 union power_supply_propval val
;
1239 /* Verify whether fuel gauge provides battery temperature */
1240 ret
= power_supply_get_property(fuel_gauge
,
1241 POWER_SUPPLY_PROP_TEMP
, &val
);
1244 properties
[*num_properties
] = POWER_SUPPLY_PROP_TEMP
;
1245 (*num_properties
)++;
1246 cm
->desc
->measure_battery_temp
= true;
1248 #ifdef CONFIG_THERMAL
1249 if (ret
&& desc
->thermal_zone
) {
1251 thermal_zone_get_zone_by_name(desc
->thermal_zone
);
1252 if (IS_ERR(cm
->tzd_batt
))
1253 return PTR_ERR(cm
->tzd_batt
);
1255 /* Use external thermometer */
1256 properties
[*num_properties
] = POWER_SUPPLY_PROP_TEMP
;
1257 (*num_properties
)++;
1258 cm
->desc
->measure_battery_temp
= true;
1262 if (cm
->desc
->measure_battery_temp
) {
1263 /* NOTICE : Default allowable minimum charge temperature is 0 */
1264 if (!desc
->temp_max
)
1265 desc
->temp_max
= CM_DEFAULT_CHARGE_TEMP_MAX
;
1266 if (!desc
->temp_diff
)
1267 desc
->temp_diff
= CM_DEFAULT_RECHARGE_TEMP_DIFF
;
1273 static const struct of_device_id charger_manager_match
[] = {
1275 .compatible
= "charger-manager",
1279 MODULE_DEVICE_TABLE(of
, charger_manager_match
);
1281 static struct charger_desc
*of_cm_parse_desc(struct device
*dev
)
1283 struct charger_desc
*desc
;
1284 struct device_node
*np
= dev
->of_node
;
1285 u32 poll_mode
= CM_POLL_DISABLE
;
1286 u32 battery_stat
= CM_NO_BATTERY
;
1289 desc
= devm_kzalloc(dev
, sizeof(*desc
), GFP_KERNEL
);
1291 return ERR_PTR(-ENOMEM
);
1293 of_property_read_string(np
, "cm-name", &desc
->psy_name
);
1295 of_property_read_u32(np
, "cm-poll-mode", &poll_mode
);
1296 desc
->polling_mode
= poll_mode
;
1298 of_property_read_u32(np
, "cm-poll-interval",
1299 &desc
->polling_interval_ms
);
1301 of_property_read_u32(np
, "cm-fullbatt-vchkdrop-volt",
1302 &desc
->fullbatt_vchkdrop_uV
);
1303 of_property_read_u32(np
, "cm-fullbatt-voltage", &desc
->fullbatt_uV
);
1304 of_property_read_u32(np
, "cm-fullbatt-soc", &desc
->fullbatt_soc
);
1305 of_property_read_u32(np
, "cm-fullbatt-capacity",
1306 &desc
->fullbatt_full_capacity
);
1308 of_property_read_u32(np
, "cm-battery-stat", &battery_stat
);
1309 desc
->battery_present
= battery_stat
;
1312 num_chgs
= of_property_count_strings(np
, "cm-chargers");
1316 /* Allocate empty bin at the tail of array */
1317 desc
->psy_charger_stat
= devm_kcalloc(dev
,
1321 if (!desc
->psy_charger_stat
)
1322 return ERR_PTR(-ENOMEM
);
1324 for (i
= 0; i
< num_chgs
; i
++)
1325 of_property_read_string_index(np
, "cm-chargers",
1326 i
, &desc
->psy_charger_stat
[i
]);
1329 of_property_read_string(np
, "cm-fuel-gauge", &desc
->psy_fuel_gauge
);
1331 of_property_read_string(np
, "cm-thermal-zone", &desc
->thermal_zone
);
1333 of_property_read_u32(np
, "cm-battery-cold", &desc
->temp_min
);
1334 if (of_property_read_bool(np
, "cm-battery-cold-in-minus"))
1335 desc
->temp_min
*= -1;
1336 of_property_read_u32(np
, "cm-battery-hot", &desc
->temp_max
);
1337 of_property_read_u32(np
, "cm-battery-temp-diff", &desc
->temp_diff
);
1339 of_property_read_u32(np
, "cm-charging-max",
1340 &desc
->charging_max_duration_ms
);
1341 of_property_read_u32(np
, "cm-discharging-max",
1342 &desc
->discharging_max_duration_ms
);
1344 /* battery charger regulators */
1345 desc
->num_charger_regulators
= of_get_child_count(np
);
1346 if (desc
->num_charger_regulators
) {
1347 struct charger_regulator
*chg_regs
;
1348 struct device_node
*child
;
1350 chg_regs
= devm_kcalloc(dev
,
1351 desc
->num_charger_regulators
,
1355 return ERR_PTR(-ENOMEM
);
1357 desc
->charger_regulators
= chg_regs
;
1359 desc
->sysfs_groups
= devm_kcalloc(dev
,
1360 desc
->num_charger_regulators
+ 1,
1361 sizeof(*desc
->sysfs_groups
),
1363 if (!desc
->sysfs_groups
)
1364 return ERR_PTR(-ENOMEM
);
1366 for_each_child_of_node(np
, child
) {
1367 struct charger_cable
*cables
;
1368 struct device_node
*_child
;
1370 of_property_read_string(child
, "cm-regulator-name",
1371 &chg_regs
->regulator_name
);
1373 /* charger cables */
1374 chg_regs
->num_cables
= of_get_child_count(child
);
1375 if (chg_regs
->num_cables
) {
1376 cables
= devm_kcalloc(dev
,
1377 chg_regs
->num_cables
,
1382 return ERR_PTR(-ENOMEM
);
1385 chg_regs
->cables
= cables
;
1387 for_each_child_of_node(child
, _child
) {
1388 of_property_read_string(_child
,
1389 "cm-cable-name", &cables
->name
);
1390 of_property_read_string(_child
,
1392 &cables
->extcon_name
);
1393 of_property_read_u32(_child
,
1396 of_property_read_u32(_child
,
1408 static inline struct charger_desc
*cm_get_drv_data(struct platform_device
*pdev
)
1410 if (pdev
->dev
.of_node
)
1411 return of_cm_parse_desc(&pdev
->dev
);
1412 return dev_get_platdata(&pdev
->dev
);
1415 static void cm_timer_func(struct alarm
*alarm
, ktime_t now
)
1417 cm_timer_set
= false;
1420 static int charger_manager_probe(struct platform_device
*pdev
)
1422 struct charger_desc
*desc
= cm_get_drv_data(pdev
);
1423 struct charger_manager
*cm
;
1425 union power_supply_propval val
;
1426 struct power_supply
*fuel_gauge
;
1427 enum power_supply_property
*properties
;
1428 size_t num_properties
;
1429 struct power_supply_config psy_cfg
= {};
1432 dev_err(&pdev
->dev
, "No platform data (desc) found\n");
1433 return PTR_ERR(desc
);
1436 cm
= devm_kzalloc(&pdev
->dev
, sizeof(*cm
), GFP_KERNEL
);
1440 /* Basic Values. Unspecified are Null or 0 */
1441 cm
->dev
= &pdev
->dev
;
1443 psy_cfg
.drv_data
= cm
;
1445 /* Initialize alarm timer */
1446 if (alarmtimer_get_rtcdev()) {
1447 cm_timer
= devm_kzalloc(cm
->dev
, sizeof(*cm_timer
), GFP_KERNEL
);
1450 alarm_init(cm_timer
, ALARM_BOOTTIME
, cm_timer_func
);
1454 * Some of the following do not need to be errors.
1455 * Users may intentionally ignore those features.
1457 if (desc
->fullbatt_uV
== 0) {
1458 dev_info(&pdev
->dev
, "Ignoring full-battery voltage threshold as it is not supplied\n");
1460 if (!desc
->fullbatt_vchkdrop_uV
) {
1461 dev_info(&pdev
->dev
, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
1462 desc
->fullbatt_vchkdrop_uV
= 0;
1464 if (desc
->fullbatt_soc
== 0) {
1465 dev_info(&pdev
->dev
, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
1467 if (desc
->fullbatt_full_capacity
== 0) {
1468 dev_info(&pdev
->dev
, "Ignoring full-battery full capacity threshold as it is not supplied\n");
1471 if (!desc
->charger_regulators
|| desc
->num_charger_regulators
< 1) {
1472 dev_err(&pdev
->dev
, "charger_regulators undefined\n");
1476 if (!desc
->psy_charger_stat
|| !desc
->psy_charger_stat
[0]) {
1477 dev_err(&pdev
->dev
, "No power supply defined\n");
1481 if (!desc
->psy_fuel_gauge
) {
1482 dev_err(&pdev
->dev
, "No fuel gauge power supply defined\n");
1486 /* Check if charger's supplies are present at probe */
1487 for (i
= 0; desc
->psy_charger_stat
[i
]; i
++) {
1488 struct power_supply
*psy
;
1490 psy
= power_supply_get_by_name(desc
->psy_charger_stat
[i
]);
1492 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1493 desc
->psy_charger_stat
[i
]);
1496 power_supply_put(psy
);
1499 if (cm
->desc
->polling_mode
!= CM_POLL_DISABLE
&&
1500 (desc
->polling_interval_ms
== 0 ||
1501 msecs_to_jiffies(desc
->polling_interval_ms
) <= CM_JIFFIES_SMALL
)) {
1502 dev_err(&pdev
->dev
, "polling_interval_ms is too small\n");
1506 if (!desc
->charging_max_duration_ms
||
1507 !desc
->discharging_max_duration_ms
) {
1508 dev_info(&pdev
->dev
, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
1509 desc
->charging_max_duration_ms
= 0;
1510 desc
->discharging_max_duration_ms
= 0;
1513 platform_set_drvdata(pdev
, cm
);
1515 memcpy(&cm
->charger_psy_desc
, &psy_default
, sizeof(psy_default
));
1517 if (!desc
->psy_name
)
1518 strscpy(cm
->psy_name_buf
, psy_default
.name
,
1519 sizeof(cm
->psy_name_buf
));
1521 strscpy(cm
->psy_name_buf
, desc
->psy_name
,
1522 sizeof(cm
->psy_name_buf
));
1523 cm
->charger_psy_desc
.name
= cm
->psy_name_buf
;
1525 /* Allocate for psy properties because they may vary */
1526 properties
= devm_kcalloc(&pdev
->dev
,
1527 ARRAY_SIZE(default_charger_props
) +
1528 NUM_CHARGER_PSY_OPTIONAL
,
1529 sizeof(*properties
), GFP_KERNEL
);
1533 memcpy(properties
, default_charger_props
,
1534 sizeof(enum power_supply_property
) *
1535 ARRAY_SIZE(default_charger_props
));
1536 num_properties
= ARRAY_SIZE(default_charger_props
);
1538 /* Find which optional psy-properties are available */
1539 fuel_gauge
= power_supply_get_by_name(desc
->psy_fuel_gauge
);
1541 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1542 desc
->psy_fuel_gauge
);
1545 if (!power_supply_get_property(fuel_gauge
,
1546 POWER_SUPPLY_PROP_CHARGE_FULL
, &val
)) {
1547 properties
[num_properties
] =
1548 POWER_SUPPLY_PROP_CHARGE_FULL
;
1551 if (!power_supply_get_property(fuel_gauge
,
1552 POWER_SUPPLY_PROP_CHARGE_NOW
, &val
)) {
1553 properties
[num_properties
] =
1554 POWER_SUPPLY_PROP_CHARGE_NOW
;
1557 if (!power_supply_get_property(fuel_gauge
,
1558 POWER_SUPPLY_PROP_CURRENT_NOW
,
1560 properties
[num_properties
] =
1561 POWER_SUPPLY_PROP_CURRENT_NOW
;
1565 ret
= cm_init_thermal_data(cm
, fuel_gauge
, properties
, &num_properties
);
1567 dev_err(&pdev
->dev
, "Failed to initialize thermal data\n");
1568 cm
->desc
->measure_battery_temp
= false;
1570 power_supply_put(fuel_gauge
);
1572 cm
->charger_psy_desc
.properties
= properties
;
1573 cm
->charger_psy_desc
.num_properties
= num_properties
;
1575 /* Register sysfs entry for charger(regulator) */
1576 ret
= charger_manager_prepare_sysfs(cm
);
1579 "Cannot prepare sysfs entry of regulators\n");
1582 psy_cfg
.attr_grp
= desc
->sysfs_groups
;
1584 cm
->charger_psy
= power_supply_register(&pdev
->dev
,
1585 &cm
->charger_psy_desc
,
1587 if (IS_ERR(cm
->charger_psy
)) {
1588 dev_err(&pdev
->dev
, "Cannot register charger-manager with name \"%s\"\n",
1589 cm
->charger_psy_desc
.name
);
1590 return PTR_ERR(cm
->charger_psy
);
1593 /* Register extcon device for charger cable */
1594 ret
= charger_manager_register_extcon(cm
);
1596 dev_err(&pdev
->dev
, "Cannot initialize extcon device\n");
1597 goto err_reg_extcon
;
1600 /* Add to the list */
1601 mutex_lock(&cm_list_mtx
);
1602 list_add(&cm
->entry
, &cm_list
);
1603 mutex_unlock(&cm_list_mtx
);
1606 * Charger-manager is capable of waking up the system from sleep
1607 * when event is happened through cm_notify_event()
1609 device_init_wakeup(&pdev
->dev
, true);
1610 device_set_wakeup_capable(&pdev
->dev
, false);
1613 * Charger-manager have to check the charging state right after
1614 * initialization of charger-manager and then update current charging
1619 schedule_work(&setup_polling
);
1624 for (i
= 0; i
< desc
->num_charger_regulators
; i
++)
1625 regulator_put(desc
->charger_regulators
[i
].consumer
);
1627 power_supply_unregister(cm
->charger_psy
);
1632 static void charger_manager_remove(struct platform_device
*pdev
)
1634 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
1635 struct charger_desc
*desc
= cm
->desc
;
1638 /* Remove from the list */
1639 mutex_lock(&cm_list_mtx
);
1640 list_del(&cm
->entry
);
1641 mutex_unlock(&cm_list_mtx
);
1643 cancel_work_sync(&setup_polling
);
1644 cancel_delayed_work_sync(&cm_monitor_work
);
1646 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++)
1647 regulator_put(desc
->charger_regulators
[i
].consumer
);
1649 power_supply_unregister(cm
->charger_psy
);
1651 try_charger_enable(cm
, false);
1654 static const struct platform_device_id charger_manager_id
[] = {
1655 { "charger-manager", 0 },
1658 MODULE_DEVICE_TABLE(platform
, charger_manager_id
);
1660 static int cm_suspend_noirq(struct device
*dev
)
1662 if (device_may_wakeup(dev
)) {
1663 device_set_wakeup_capable(dev
, false);
1670 static bool cm_need_to_awake(void)
1672 struct charger_manager
*cm
;
1677 mutex_lock(&cm_list_mtx
);
1678 list_for_each_entry(cm
, &cm_list
, entry
) {
1679 if (is_charging(cm
)) {
1680 mutex_unlock(&cm_list_mtx
);
1684 mutex_unlock(&cm_list_mtx
);
1689 static int cm_suspend_prepare(struct device
*dev
)
1691 if (cm_need_to_awake())
1695 cm_suspended
= true;
1697 cm_timer_set
= cm_setup_timer();
1700 cancel_work_sync(&setup_polling
);
1701 cancel_delayed_work_sync(&cm_monitor_work
);
1707 static void cm_suspend_complete(struct device
*dev
)
1709 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1712 cm_suspended
= false;
1717 alarm_cancel(cm_timer
);
1718 cm_timer_set
= false;
1719 remain
= alarm_expires_remaining(cm_timer
);
1720 cm_suspend_duration_ms
-= ktime_to_ms(remain
);
1721 schedule_work(&setup_polling
);
1726 device_set_wakeup_capable(cm
->dev
, false);
1729 static const struct dev_pm_ops charger_manager_pm
= {
1730 .prepare
= cm_suspend_prepare
,
1731 .suspend_noirq
= cm_suspend_noirq
,
1732 .complete
= cm_suspend_complete
,
1735 static struct platform_driver charger_manager_driver
= {
1737 .name
= "charger-manager",
1738 .pm
= &charger_manager_pm
,
1739 .of_match_table
= charger_manager_match
,
1741 .probe
= charger_manager_probe
,
1742 .remove
= charger_manager_remove
,
1743 .id_table
= charger_manager_id
,
1746 static int __init
charger_manager_init(void)
1748 cm_wq
= create_freezable_workqueue("charger_manager");
1749 if (unlikely(!cm_wq
))
1752 INIT_DELAYED_WORK(&cm_monitor_work
, cm_monitor_poller
);
1754 return platform_driver_register(&charger_manager_driver
);
1756 late_initcall(charger_manager_init
);
1758 static void __exit
charger_manager_cleanup(void)
1760 destroy_workqueue(cm_wq
);
1763 platform_driver_unregister(&charger_manager_driver
);
1765 module_exit(charger_manager_cleanup
);
1767 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1768 MODULE_DESCRIPTION("Charger Manager");
1769 MODULE_LICENSE("GPL");