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>
30 * Default temperature threshold for charging.
31 * Every temperature units are in tenth of centigrade.
33 #define CM_DEFAULT_RECHARGE_TEMP_DIFF 50
34 #define CM_DEFAULT_CHARGE_TEMP_MAX 500
36 static const char * const default_event_names
[] = {
37 [CM_EVENT_UNKNOWN
] = "Unknown",
38 [CM_EVENT_BATT_FULL
] = "Battery Full",
39 [CM_EVENT_BATT_IN
] = "Battery Inserted",
40 [CM_EVENT_BATT_OUT
] = "Battery Pulled Out",
41 [CM_EVENT_BATT_OVERHEAT
] = "Battery Overheat",
42 [CM_EVENT_BATT_COLD
] = "Battery Cold",
43 [CM_EVENT_EXT_PWR_IN_OUT
] = "External Power Attach/Detach",
44 [CM_EVENT_CHG_START_STOP
] = "Charging Start/Stop",
45 [CM_EVENT_OTHERS
] = "Other battery events"
49 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
50 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
53 #define CM_JIFFIES_SMALL (2)
55 /* If y is valid (> 0) and smaller than x, do x = y */
56 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
59 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
60 * rtc alarm. It should be 2 or larger
62 #define CM_RTC_SMALL (2)
64 #define UEVENT_BUF_SIZE 32
66 static LIST_HEAD(cm_list
);
67 static DEFINE_MUTEX(cm_list_mtx
);
69 /* About in-suspend (suspend-again) monitoring */
70 static struct alarm
*cm_timer
;
72 static bool cm_suspended
;
73 static bool cm_timer_set
;
74 static unsigned long cm_suspend_duration_ms
;
76 /* About normal (not suspended) monitoring */
77 static unsigned long polling_jiffy
= ULONG_MAX
; /* ULONG_MAX: no polling */
78 static unsigned long next_polling
; /* Next appointed polling time */
79 static struct workqueue_struct
*cm_wq
; /* init at driver add */
80 static struct delayed_work cm_monitor_work
; /* init at driver add */
83 * is_batt_present - See if the battery presents in place.
84 * @cm: the Charger Manager representing the battery.
86 static bool is_batt_present(struct charger_manager
*cm
)
88 union power_supply_propval val
;
89 struct power_supply
*psy
;
93 switch (cm
->desc
->battery_present
) {
94 case CM_BATTERY_PRESENT
:
100 psy
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
104 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_PRESENT
,
106 if (ret
== 0 && val
.intval
)
108 power_supply_put(psy
);
110 case CM_CHARGER_STAT
:
111 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
112 psy
= power_supply_get_by_name(
113 cm
->desc
->psy_charger_stat
[i
]);
115 dev_err(cm
->dev
, "Cannot find power supply \"%s\"\n",
116 cm
->desc
->psy_charger_stat
[i
]);
120 ret
= power_supply_get_property(psy
,
121 POWER_SUPPLY_PROP_PRESENT
, &val
);
122 power_supply_put(psy
);
123 if (ret
== 0 && val
.intval
) {
135 * is_ext_pwr_online - See if an external power source is attached to charge
136 * @cm: the Charger Manager representing the battery.
138 * Returns true if at least one of the chargers of the battery has an external
139 * power source attached to charge the battery regardless of whether it is
140 * actually charging or not.
142 static bool is_ext_pwr_online(struct charger_manager
*cm
)
144 union power_supply_propval val
;
145 struct power_supply
*psy
;
149 /* If at least one of them has one, it's yes. */
150 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
151 psy
= power_supply_get_by_name(cm
->desc
->psy_charger_stat
[i
]);
153 dev_err(cm
->dev
, "Cannot find power supply \"%s\"\n",
154 cm
->desc
->psy_charger_stat
[i
]);
158 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_ONLINE
,
160 power_supply_put(psy
);
161 if (ret
== 0 && val
.intval
) {
171 * get_batt_uV - Get the voltage level of the battery
172 * @cm: the Charger Manager representing the battery.
173 * @uV: the voltage level returned.
175 * Returns 0 if there is no error.
176 * Returns a negative value on error.
178 static int get_batt_uV(struct charger_manager
*cm
, int *uV
)
180 union power_supply_propval val
;
181 struct power_supply
*fuel_gauge
;
184 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
188 ret
= power_supply_get_property(fuel_gauge
,
189 POWER_SUPPLY_PROP_VOLTAGE_NOW
, &val
);
190 power_supply_put(fuel_gauge
);
199 * is_charging - Returns true if the battery is being charged.
200 * @cm: the Charger Manager representing the battery.
202 static bool is_charging(struct charger_manager
*cm
)
205 bool charging
= false;
206 struct power_supply
*psy
;
207 union power_supply_propval val
;
209 /* If there is no battery, it cannot be charged */
210 if (!is_batt_present(cm
))
213 /* If at least one of the charger is charging, return yes */
214 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
215 /* 1. The charger sholuld not be DISABLED */
216 if (cm
->emergency_stop
)
218 if (!cm
->charger_enabled
)
221 psy
= power_supply_get_by_name(cm
->desc
->psy_charger_stat
[i
]);
223 dev_err(cm
->dev
, "Cannot find power supply \"%s\"\n",
224 cm
->desc
->psy_charger_stat
[i
]);
228 /* 2. The charger should be online (ext-power) */
229 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_ONLINE
,
232 dev_warn(cm
->dev
, "Cannot read ONLINE value from %s\n",
233 cm
->desc
->psy_charger_stat
[i
]);
234 power_supply_put(psy
);
237 if (val
.intval
== 0) {
238 power_supply_put(psy
);
243 * 3. The charger should not be FULL, DISCHARGING,
246 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_STATUS
,
248 power_supply_put(psy
);
250 dev_warn(cm
->dev
, "Cannot read STATUS value from %s\n",
251 cm
->desc
->psy_charger_stat
[i
]);
254 if (val
.intval
== POWER_SUPPLY_STATUS_FULL
||
255 val
.intval
== POWER_SUPPLY_STATUS_DISCHARGING
||
256 val
.intval
== POWER_SUPPLY_STATUS_NOT_CHARGING
)
259 /* Then, this is charging. */
268 * is_full_charged - Returns true if the battery is fully charged.
269 * @cm: the Charger Manager representing the battery.
271 static bool is_full_charged(struct charger_manager
*cm
)
273 struct charger_desc
*desc
= cm
->desc
;
274 union power_supply_propval val
;
275 struct power_supply
*fuel_gauge
;
276 bool is_full
= false;
280 /* If there is no battery, it cannot be charged */
281 if (!is_batt_present(cm
))
284 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
288 if (desc
->fullbatt_full_capacity
> 0) {
291 /* Not full if capacity of fuel gauge isn't full */
292 ret
= power_supply_get_property(fuel_gauge
,
293 POWER_SUPPLY_PROP_CHARGE_FULL
, &val
);
294 if (!ret
&& val
.intval
> desc
->fullbatt_full_capacity
) {
300 /* Full, if it's over the fullbatt voltage */
301 if (desc
->fullbatt_uV
> 0) {
302 ret
= get_batt_uV(cm
, &uV
);
303 if (!ret
&& uV
>= desc
->fullbatt_uV
) {
309 /* Full, if the capacity is more than fullbatt_soc */
310 if (desc
->fullbatt_soc
> 0) {
313 ret
= power_supply_get_property(fuel_gauge
,
314 POWER_SUPPLY_PROP_CAPACITY
, &val
);
315 if (!ret
&& val
.intval
>= desc
->fullbatt_soc
) {
322 power_supply_put(fuel_gauge
);
327 * is_polling_required - Return true if need to continue polling for this CM.
328 * @cm: the Charger Manager representing the battery.
330 static bool is_polling_required(struct charger_manager
*cm
)
332 switch (cm
->desc
->polling_mode
) {
333 case CM_POLL_DISABLE
:
337 case CM_POLL_EXTERNAL_POWER_ONLY
:
338 return is_ext_pwr_online(cm
);
339 case CM_POLL_CHARGING_ONLY
:
340 return is_charging(cm
);
342 dev_warn(cm
->dev
, "Incorrect polling_mode (%d)\n",
343 cm
->desc
->polling_mode
);
350 * try_charger_enable - Enable/Disable chargers altogether
351 * @cm: the Charger Manager representing the battery.
352 * @enable: true: enable / false: disable
354 * Note that Charger Manager keeps the charger enabled regardless whether
355 * the charger is charging or not (because battery is full or no external
356 * power source exists) except when CM needs to disable chargers forcibly
357 * because of emergency causes; when the battery is overheated or too cold.
359 static int try_charger_enable(struct charger_manager
*cm
, bool enable
)
362 struct charger_desc
*desc
= cm
->desc
;
364 /* Ignore if it's redundant command */
365 if (enable
== cm
->charger_enabled
)
369 if (cm
->emergency_stop
)
373 * Save start time of charging to limit
374 * maximum possible charging time.
376 cm
->charging_start_time
= ktime_to_ms(ktime_get());
377 cm
->charging_end_time
= 0;
379 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
380 if (desc
->charger_regulators
[i
].externally_control
)
383 err
= regulator_enable(desc
->charger_regulators
[i
].consumer
);
385 dev_warn(cm
->dev
, "Cannot enable %s regulator\n",
386 desc
->charger_regulators
[i
].regulator_name
);
391 * Save end time of charging to maintain fully charged state
392 * of battery after full-batt.
394 cm
->charging_start_time
= 0;
395 cm
->charging_end_time
= ktime_to_ms(ktime_get());
397 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
398 if (desc
->charger_regulators
[i
].externally_control
)
401 err
= regulator_disable(desc
->charger_regulators
[i
].consumer
);
403 dev_warn(cm
->dev
, "Cannot disable %s regulator\n",
404 desc
->charger_regulators
[i
].regulator_name
);
409 * Abnormal battery state - Stop charging forcibly,
410 * even if charger was enabled at the other places
412 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
413 if (regulator_is_enabled(
414 desc
->charger_regulators
[i
].consumer
)) {
415 regulator_force_disable(
416 desc
->charger_regulators
[i
].consumer
);
417 dev_warn(cm
->dev
, "Disable regulator(%s) forcibly\n",
418 desc
->charger_regulators
[i
].regulator_name
);
424 cm
->charger_enabled
= enable
;
430 * try_charger_restart - Restart charging.
431 * @cm: the Charger Manager representing the battery.
433 * Restart charging by turning off and on the charger.
435 static int try_charger_restart(struct charger_manager
*cm
)
439 if (cm
->emergency_stop
)
442 err
= try_charger_enable(cm
, false);
446 return try_charger_enable(cm
, true);
450 * uevent_notify - Let users know something has changed.
451 * @cm: the Charger Manager representing the battery.
452 * @event: the event string.
454 * If @event is null, it implies that uevent_notify is called
455 * by resume function. When called in the resume function, cm_suspended
456 * should be already reset to false in order to let uevent_notify
457 * notify the recent event during the suspend to users. While
458 * suspended, uevent_notify does not notify users, but tracks
459 * events so that uevent_notify can notify users later after resumed.
461 static void uevent_notify(struct charger_manager
*cm
, const char *event
)
463 static char env_str
[UEVENT_BUF_SIZE
+ 1] = "";
464 static char env_str_save
[UEVENT_BUF_SIZE
+ 1] = "";
467 /* Nothing in suspended-event buffer */
468 if (env_str_save
[0] == 0) {
469 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
470 return; /* status not changed */
471 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
475 if (!strncmp(env_str_save
, event
, UEVENT_BUF_SIZE
))
476 return; /* Duplicated. */
477 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
482 /* No messages pending */
483 if (!env_str_save
[0])
486 strncpy(env_str
, env_str_save
, UEVENT_BUF_SIZE
);
487 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
493 /* status not changed */
494 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
497 /* save the status and notify the update */
498 strncpy(env_str
, event
, UEVENT_BUF_SIZE
);
499 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
501 dev_info(cm
->dev
, "%s\n", event
);
505 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
506 * @work: the work_struct appointing the function
508 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
509 * charger_desc, Charger Manager checks voltage drop after the battery
510 * "FULL" event. It checks whether the voltage has dropped more than
511 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
513 static void fullbatt_vchk(struct work_struct
*work
)
515 struct delayed_work
*dwork
= to_delayed_work(work
);
516 struct charger_manager
*cm
= container_of(dwork
,
517 struct charger_manager
, fullbatt_vchk_work
);
518 struct charger_desc
*desc
= cm
->desc
;
519 int batt_uV
, err
, diff
;
521 /* remove the appointment for fullbatt_vchk */
522 cm
->fullbatt_vchk_jiffies_at
= 0;
524 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
527 err
= get_batt_uV(cm
, &batt_uV
);
529 dev_err(cm
->dev
, "%s: get_batt_uV error(%d)\n", __func__
, err
);
533 diff
= desc
->fullbatt_uV
- batt_uV
;
537 dev_info(cm
->dev
, "VBATT dropped %duV after full-batt\n", diff
);
539 if (diff
> desc
->fullbatt_vchkdrop_uV
) {
540 try_charger_restart(cm
);
541 uevent_notify(cm
, "Recharging");
546 * check_charging_duration - Monitor charging/discharging duration
547 * @cm: the Charger Manager representing the battery.
549 * If whole charging duration exceed 'charging_max_duration_ms',
550 * cm stop charging to prevent overcharge/overheat. If discharging
551 * duration exceed 'discharging _max_duration_ms', charger cable is
552 * attached, after full-batt, cm start charging to maintain fully
553 * charged state for battery.
555 static int check_charging_duration(struct charger_manager
*cm
)
557 struct charger_desc
*desc
= cm
->desc
;
558 u64 curr
= ktime_to_ms(ktime_get());
562 if (!desc
->charging_max_duration_ms
&&
563 !desc
->discharging_max_duration_ms
)
566 if (cm
->charger_enabled
) {
567 duration
= curr
- cm
->charging_start_time
;
569 if (duration
> desc
->charging_max_duration_ms
) {
570 dev_info(cm
->dev
, "Charging duration exceed %ums\n",
571 desc
->charging_max_duration_ms
);
572 uevent_notify(cm
, "Discharging");
573 try_charger_enable(cm
, false);
576 } else if (is_ext_pwr_online(cm
) && !cm
->charger_enabled
) {
577 duration
= curr
- cm
->charging_end_time
;
579 if (duration
> desc
->discharging_max_duration_ms
&&
580 is_ext_pwr_online(cm
)) {
581 dev_info(cm
->dev
, "Discharging duration exceed %ums\n",
582 desc
->discharging_max_duration_ms
);
583 uevent_notify(cm
, "Recharging");
584 try_charger_enable(cm
, true);
592 static int cm_get_battery_temperature_by_psy(struct charger_manager
*cm
,
595 struct power_supply
*fuel_gauge
;
598 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
602 ret
= power_supply_get_property(fuel_gauge
,
603 POWER_SUPPLY_PROP_TEMP
,
604 (union power_supply_propval
*)temp
);
605 power_supply_put(fuel_gauge
);
610 static int cm_get_battery_temperature(struct charger_manager
*cm
,
615 if (!cm
->desc
->measure_battery_temp
)
618 #ifdef CONFIG_THERMAL
620 ret
= thermal_zone_get_temp(cm
->tzd_batt
, temp
);
622 /* Calibrate temperature unit */
627 /* if-else continued from CONFIG_THERMAL */
628 ret
= cm_get_battery_temperature_by_psy(cm
, temp
);
634 static int cm_check_thermal_status(struct charger_manager
*cm
)
636 struct charger_desc
*desc
= cm
->desc
;
637 int temp
, upper_limit
, lower_limit
;
640 ret
= cm_get_battery_temperature(cm
, &temp
);
643 * No information of battery temperature might
644 * occur hazardous result. We have to handle it
645 * depending on battery type.
647 dev_err(cm
->dev
, "Failed to get battery temperature\n");
651 upper_limit
= desc
->temp_max
;
652 lower_limit
= desc
->temp_min
;
654 if (cm
->emergency_stop
) {
655 upper_limit
-= desc
->temp_diff
;
656 lower_limit
+= desc
->temp_diff
;
659 if (temp
> upper_limit
)
660 ret
= CM_EVENT_BATT_OVERHEAT
;
661 else if (temp
< lower_limit
)
662 ret
= CM_EVENT_BATT_COLD
;
668 * _cm_monitor - Monitor the temperature and return true for exceptions.
669 * @cm: the Charger Manager representing the battery.
671 * Returns true if there is an event to notify for the battery.
672 * (True if the status of "emergency_stop" changes)
674 static bool _cm_monitor(struct charger_manager
*cm
)
678 temp_alrt
= cm_check_thermal_status(cm
);
680 /* It has been stopped already */
681 if (temp_alrt
&& cm
->emergency_stop
)
685 * Check temperature whether overheat or cold.
686 * If temperature is out of range normal state, stop charging.
689 cm
->emergency_stop
= temp_alrt
;
690 if (!try_charger_enable(cm
, false))
691 uevent_notify(cm
, default_event_names
[temp_alrt
]);
694 * Check whole charging duration and discharging duration
697 } else if (!cm
->emergency_stop
&& check_charging_duration(cm
)) {
699 "Charging/Discharging duration is out of range\n");
701 * Check dropped voltage of battery. If battery voltage is more
702 * dropped than fullbatt_vchkdrop_uV after fully charged state,
703 * charger-manager have to recharge battery.
705 } else if (!cm
->emergency_stop
&& is_ext_pwr_online(cm
) &&
706 !cm
->charger_enabled
) {
707 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
710 * Check whether fully charged state to protect overcharge
711 * if charger-manager is charging for battery.
713 } else if (!cm
->emergency_stop
&& is_full_charged(cm
) &&
714 cm
->charger_enabled
) {
715 dev_info(cm
->dev
, "EVENT_HANDLE: Battery Fully Charged\n");
716 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_FULL
]);
718 try_charger_enable(cm
, false);
720 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
722 cm
->emergency_stop
= 0;
723 if (is_ext_pwr_online(cm
)) {
724 if (!try_charger_enable(cm
, true))
725 uevent_notify(cm
, "CHARGING");
733 * cm_monitor - Monitor every battery.
735 * Returns true if there is an event to notify from any of the batteries.
736 * (True if the status of "emergency_stop" changes)
738 static bool cm_monitor(void)
741 struct charger_manager
*cm
;
743 mutex_lock(&cm_list_mtx
);
745 list_for_each_entry(cm
, &cm_list
, entry
) {
750 mutex_unlock(&cm_list_mtx
);
756 * _setup_polling - Setup the next instance of polling.
757 * @work: work_struct of the function _setup_polling.
759 static void _setup_polling(struct work_struct
*work
)
761 unsigned long min
= ULONG_MAX
;
762 struct charger_manager
*cm
;
763 bool keep_polling
= false;
764 unsigned long _next_polling
;
766 mutex_lock(&cm_list_mtx
);
768 list_for_each_entry(cm
, &cm_list
, entry
) {
769 if (is_polling_required(cm
) && cm
->desc
->polling_interval_ms
) {
772 if (min
> cm
->desc
->polling_interval_ms
)
773 min
= cm
->desc
->polling_interval_ms
;
777 polling_jiffy
= msecs_to_jiffies(min
);
778 if (polling_jiffy
<= CM_JIFFIES_SMALL
)
779 polling_jiffy
= CM_JIFFIES_SMALL
+ 1;
782 polling_jiffy
= ULONG_MAX
;
783 if (polling_jiffy
== ULONG_MAX
)
786 WARN(cm_wq
== NULL
, "charger-manager: workqueue not initialized"
787 ". try it later. %s\n", __func__
);
790 * Use mod_delayed_work() iff the next polling interval should
791 * occur before the currently scheduled one. If @cm_monitor_work
792 * isn't active, the end result is the same, so no need to worry
793 * about stale @next_polling.
795 _next_polling
= jiffies
+ polling_jiffy
;
797 if (time_before(_next_polling
, next_polling
)) {
798 mod_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
);
799 next_polling
= _next_polling
;
801 if (queue_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
))
802 next_polling
= _next_polling
;
805 mutex_unlock(&cm_list_mtx
);
807 static DECLARE_WORK(setup_polling
, _setup_polling
);
810 * cm_monitor_poller - The Monitor / Poller.
811 * @work: work_struct of the function cm_monitor_poller
813 * During non-suspended state, cm_monitor_poller is used to poll and monitor
816 static void cm_monitor_poller(struct work_struct
*work
)
819 schedule_work(&setup_polling
);
823 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
824 * @cm: the Charger Manager representing the battery.
826 static void fullbatt_handler(struct charger_manager
*cm
)
828 struct charger_desc
*desc
= cm
->desc
;
830 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
834 device_set_wakeup_capable(cm
->dev
, true);
836 mod_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
837 msecs_to_jiffies(desc
->fullbatt_vchkdrop_ms
));
838 cm
->fullbatt_vchk_jiffies_at
= jiffies
+ msecs_to_jiffies(
839 desc
->fullbatt_vchkdrop_ms
);
841 if (cm
->fullbatt_vchk_jiffies_at
== 0)
842 cm
->fullbatt_vchk_jiffies_at
= 1;
845 dev_info(cm
->dev
, "EVENT_HANDLE: Battery Fully Charged\n");
846 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_FULL
]);
850 * battout_handler - Event handler for CM_EVENT_BATT_OUT
851 * @cm: the Charger Manager representing the battery.
853 static void battout_handler(struct charger_manager
*cm
)
856 device_set_wakeup_capable(cm
->dev
, true);
858 if (!is_batt_present(cm
)) {
859 dev_emerg(cm
->dev
, "Battery Pulled Out!\n");
860 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_OUT
]);
862 uevent_notify(cm
, "Battery Reinserted?");
867 * misc_event_handler - Handler for other events
868 * @cm: the Charger Manager representing the battery.
869 * @type: the Charger Manager representing the battery.
871 static void misc_event_handler(struct charger_manager
*cm
,
872 enum cm_event_types type
)
875 device_set_wakeup_capable(cm
->dev
, true);
877 if (is_polling_required(cm
) && cm
->desc
->polling_interval_ms
)
878 schedule_work(&setup_polling
);
879 uevent_notify(cm
, default_event_names
[type
]);
882 static int charger_get_property(struct power_supply
*psy
,
883 enum power_supply_property psp
,
884 union power_supply_propval
*val
)
886 struct charger_manager
*cm
= power_supply_get_drvdata(psy
);
887 struct charger_desc
*desc
= cm
->desc
;
888 struct power_supply
*fuel_gauge
= NULL
;
893 case POWER_SUPPLY_PROP_STATUS
:
895 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
896 else if (is_ext_pwr_online(cm
))
897 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
899 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
901 case POWER_SUPPLY_PROP_HEALTH
:
902 if (cm
->emergency_stop
> 0)
903 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
904 else if (cm
->emergency_stop
< 0)
905 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
907 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
909 case POWER_SUPPLY_PROP_PRESENT
:
910 if (is_batt_present(cm
))
915 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
916 ret
= get_batt_uV(cm
, &val
->intval
);
918 case POWER_SUPPLY_PROP_CURRENT_NOW
:
919 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
924 ret
= power_supply_get_property(fuel_gauge
,
925 POWER_SUPPLY_PROP_CURRENT_NOW
, val
);
927 case POWER_SUPPLY_PROP_TEMP
:
928 case POWER_SUPPLY_PROP_TEMP_AMBIENT
:
929 return cm_get_battery_temperature(cm
, &val
->intval
);
930 case POWER_SUPPLY_PROP_CAPACITY
:
931 if (!is_batt_present(cm
)) {
932 /* There is no battery. Assume 100% */
937 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
943 ret
= power_supply_get_property(fuel_gauge
,
944 POWER_SUPPLY_PROP_CAPACITY
, val
);
948 if (val
->intval
> 100) {
955 /* Do not adjust SOC when charging: voltage is overrated */
960 * If the capacity value is inconsistent, calibrate it base on
961 * the battery voltage values and the thresholds given as desc
963 ret
= get_batt_uV(cm
, &uV
);
965 /* Voltage information not available. No calibration */
970 if (desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
977 case POWER_SUPPLY_PROP_ONLINE
:
978 if (is_ext_pwr_online(cm
))
983 case POWER_SUPPLY_PROP_CHARGE_FULL
:
984 if (is_full_charged(cm
))
990 case POWER_SUPPLY_PROP_CHARGE_NOW
:
991 if (is_charging(cm
)) {
992 fuel_gauge
= power_supply_get_by_name(
993 cm
->desc
->psy_fuel_gauge
);
999 ret
= power_supply_get_property(fuel_gauge
,
1000 POWER_SUPPLY_PROP_CHARGE_NOW
,
1006 /* If CHARGE_NOW is supplied, use it */
1007 val
->intval
= (val
->intval
> 0) ?
1018 power_supply_put(fuel_gauge
);
1022 #define NUM_CHARGER_PSY_OPTIONAL (4)
1023 static enum power_supply_property default_charger_props
[] = {
1024 /* Guaranteed to provide */
1025 POWER_SUPPLY_PROP_STATUS
,
1026 POWER_SUPPLY_PROP_HEALTH
,
1027 POWER_SUPPLY_PROP_PRESENT
,
1028 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
1029 POWER_SUPPLY_PROP_CAPACITY
,
1030 POWER_SUPPLY_PROP_ONLINE
,
1031 POWER_SUPPLY_PROP_CHARGE_FULL
,
1033 * Optional properties are:
1034 * POWER_SUPPLY_PROP_CHARGE_NOW,
1035 * POWER_SUPPLY_PROP_CURRENT_NOW,
1036 * POWER_SUPPLY_PROP_TEMP, and
1037 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
1041 static const struct power_supply_desc psy_default
= {
1043 .type
= POWER_SUPPLY_TYPE_BATTERY
,
1044 .properties
= default_charger_props
,
1045 .num_properties
= ARRAY_SIZE(default_charger_props
),
1046 .get_property
= charger_get_property
,
1051 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
1052 * for suspend_again.
1054 * Returns true if the alarm is set for Charger Manager to use.
1056 * cm_setup_timer fails to set an alarm,
1057 * cm_setup_timer does not need to set an alarm for Charger Manager,
1058 * or an alarm previously configured is to be used.
1060 static bool cm_setup_timer(void)
1062 struct charger_manager
*cm
;
1063 unsigned int wakeup_ms
= UINT_MAX
;
1066 if (time_after(next_polling
, jiffies
))
1067 CM_MIN_VALID(wakeup_ms
,
1068 jiffies_to_msecs(next_polling
- jiffies
));
1070 mutex_lock(&cm_list_mtx
);
1071 list_for_each_entry(cm
, &cm_list
, entry
) {
1072 unsigned int fbchk_ms
= 0;
1074 /* fullbatt_vchk is required. setup timer for that */
1075 if (cm
->fullbatt_vchk_jiffies_at
) {
1076 fbchk_ms
= jiffies_to_msecs(cm
->fullbatt_vchk_jiffies_at
1078 if (time_is_before_eq_jiffies(
1079 cm
->fullbatt_vchk_jiffies_at
) ||
1080 msecs_to_jiffies(fbchk_ms
) < CM_JIFFIES_SMALL
) {
1081 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
1085 CM_MIN_VALID(wakeup_ms
, fbchk_ms
);
1087 /* Skip if polling is not required for this CM */
1088 if (!is_polling_required(cm
) && !cm
->emergency_stop
)
1091 if (cm
->desc
->polling_interval_ms
== 0)
1093 CM_MIN_VALID(wakeup_ms
, cm
->desc
->polling_interval_ms
);
1095 mutex_unlock(&cm_list_mtx
);
1097 if (timer_req
&& cm_timer
) {
1101 * Set alarm with the polling interval (wakeup_ms)
1102 * The alarm time should be NOW + CM_RTC_SMALL or later.
1104 if (wakeup_ms
== UINT_MAX
||
1105 wakeup_ms
< CM_RTC_SMALL
* MSEC_PER_SEC
)
1106 wakeup_ms
= 2 * CM_RTC_SMALL
* MSEC_PER_SEC
;
1108 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms
);
1110 now
= ktime_get_boottime();
1111 add
= ktime_set(wakeup_ms
/ MSEC_PER_SEC
,
1112 (wakeup_ms
% MSEC_PER_SEC
) * NSEC_PER_MSEC
);
1113 alarm_start(cm_timer
, ktime_add(now
, add
));
1115 cm_suspend_duration_ms
= wakeup_ms
;
1123 * charger_extcon_work - enable/diable charger according to the state
1126 * @work: work_struct of the function charger_extcon_work.
1128 static void charger_extcon_work(struct work_struct
*work
)
1130 struct charger_cable
*cable
=
1131 container_of(work
, struct charger_cable
, wq
);
1134 if (cable
->attached
&& cable
->min_uA
!= 0 && cable
->max_uA
!= 0) {
1135 ret
= regulator_set_current_limit(cable
->charger
->consumer
,
1136 cable
->min_uA
, cable
->max_uA
);
1138 pr_err("Cannot set current limit of %s (%s)\n",
1139 cable
->charger
->regulator_name
, cable
->name
);
1143 pr_info("Set current limit of %s : %duA ~ %duA\n",
1144 cable
->charger
->regulator_name
,
1145 cable
->min_uA
, cable
->max_uA
);
1148 try_charger_enable(cable
->cm
, cable
->attached
);
1152 * charger_extcon_notifier - receive the state of charger cable
1153 * when registered cable is attached or detached.
1155 * @self: the notifier block of the charger_extcon_notifier.
1156 * @event: the cable state.
1157 * @ptr: the data pointer of notifier block.
1159 static int charger_extcon_notifier(struct notifier_block
*self
,
1160 unsigned long event
, void *ptr
)
1162 struct charger_cable
*cable
=
1163 container_of(self
, struct charger_cable
, nb
);
1166 * The newly state of charger cable.
1167 * If cable is attached, cable->attached is true.
1169 cable
->attached
= event
;
1172 * Setup monitoring to check battery state
1173 * when charger cable is attached.
1175 if (cable
->attached
&& is_polling_required(cable
->cm
)) {
1176 cancel_work_sync(&setup_polling
);
1177 schedule_work(&setup_polling
);
1181 * Setup work for controlling charger(regulator)
1182 * according to charger cable.
1184 schedule_work(&cable
->wq
);
1190 * charger_extcon_init - register external connector to use it
1191 * as the charger cable
1193 * @cm: the Charger Manager representing the battery.
1194 * @cable: the Charger cable representing the external connector.
1196 static int charger_extcon_init(struct charger_manager
*cm
,
1197 struct charger_cable
*cable
)
1202 * Charger manager use Extcon framework to identify
1203 * the charger cable among various external connector
1204 * cable (e.g., TA, USB, MHL, Dock).
1206 INIT_WORK(&cable
->wq
, charger_extcon_work
);
1207 cable
->nb
.notifier_call
= charger_extcon_notifier
;
1208 ret
= extcon_register_interest(&cable
->extcon_dev
,
1209 cable
->extcon_name
, cable
->name
, &cable
->nb
);
1211 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1212 cable
->extcon_name
, cable
->name
);
1219 * charger_manager_register_extcon - Register extcon device to receive state
1221 * @cm: the Charger Manager representing the battery.
1223 * This function support EXTCON(External Connector) subsystem to detect the
1224 * state of charger cables for enabling or disabling charger(regulator) and
1225 * select the charger cable for charging among a number of external cable
1226 * according to policy of H/W board.
1228 static int charger_manager_register_extcon(struct charger_manager
*cm
)
1230 struct charger_desc
*desc
= cm
->desc
;
1231 struct charger_regulator
*charger
;
1236 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1237 charger
= &desc
->charger_regulators
[i
];
1239 charger
->consumer
= regulator_get(cm
->dev
,
1240 charger
->regulator_name
);
1241 if (IS_ERR(charger
->consumer
)) {
1242 dev_err(cm
->dev
, "Cannot find charger(%s)\n",
1243 charger
->regulator_name
);
1244 return PTR_ERR(charger
->consumer
);
1248 for (j
= 0; j
< charger
->num_cables
; j
++) {
1249 struct charger_cable
*cable
= &charger
->cables
[j
];
1251 ret
= charger_extcon_init(cm
, cable
);
1253 dev_err(cm
->dev
, "Cannot initialize charger(%s)\n",
1254 charger
->regulator_name
);
1257 cable
->charger
= charger
;
1265 /* help function of sysfs node to control charger(regulator) */
1266 static ssize_t
charger_name_show(struct device
*dev
,
1267 struct device_attribute
*attr
, char *buf
)
1269 struct charger_regulator
*charger
1270 = container_of(attr
, struct charger_regulator
, attr_name
);
1272 return sprintf(buf
, "%s\n", charger
->regulator_name
);
1275 static ssize_t
charger_state_show(struct device
*dev
,
1276 struct device_attribute
*attr
, char *buf
)
1278 struct charger_regulator
*charger
1279 = container_of(attr
, struct charger_regulator
, attr_state
);
1282 if (!charger
->externally_control
)
1283 state
= regulator_is_enabled(charger
->consumer
);
1285 return sprintf(buf
, "%s\n", state
? "enabled" : "disabled");
1288 static ssize_t
charger_externally_control_show(struct device
*dev
,
1289 struct device_attribute
*attr
, char *buf
)
1291 struct charger_regulator
*charger
= container_of(attr
,
1292 struct charger_regulator
, attr_externally_control
);
1294 return sprintf(buf
, "%d\n", charger
->externally_control
);
1297 static ssize_t
charger_externally_control_store(struct device
*dev
,
1298 struct device_attribute
*attr
, const char *buf
,
1301 struct charger_regulator
*charger
1302 = container_of(attr
, struct charger_regulator
,
1303 attr_externally_control
);
1304 struct charger_manager
*cm
= charger
->cm
;
1305 struct charger_desc
*desc
= cm
->desc
;
1308 int externally_control
;
1309 int chargers_externally_control
= 1;
1311 ret
= sscanf(buf
, "%d", &externally_control
);
1317 if (!externally_control
) {
1318 charger
->externally_control
= 0;
1322 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1323 if (&desc
->charger_regulators
[i
] != charger
&&
1324 !desc
->charger_regulators
[i
].externally_control
) {
1326 * At least, one charger is controlled by
1329 chargers_externally_control
= 0;
1334 if (!chargers_externally_control
) {
1335 if (cm
->charger_enabled
) {
1336 try_charger_enable(charger
->cm
, false);
1337 charger
->externally_control
= externally_control
;
1338 try_charger_enable(charger
->cm
, true);
1340 charger
->externally_control
= externally_control
;
1344 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1345 charger
->regulator_name
);
1352 * charger_manager_prepare_sysfs - Prepare sysfs entry for each charger
1353 * @cm: the Charger Manager representing the battery.
1355 * This function add sysfs entry for charger(regulator) to control charger from
1356 * user-space. If some development board use one more chargers for charging
1357 * but only need one charger on specific case which is dependent on user
1358 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1359 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1360 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1361 * externally_control, this charger isn't controlled from charger-manager and
1362 * always stay off state of regulator.
1364 static int charger_manager_prepare_sysfs(struct charger_manager
*cm
)
1366 struct charger_desc
*desc
= cm
->desc
;
1367 struct charger_regulator
*charger
;
1368 int chargers_externally_control
= 1;
1372 /* Create sysfs entry to control charger(regulator) */
1373 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1374 charger
= &desc
->charger_regulators
[i
];
1376 name
= devm_kasprintf(cm
->dev
, GFP_KERNEL
, "charger.%d", i
);
1380 charger
->attrs
[0] = &charger
->attr_name
.attr
;
1381 charger
->attrs
[1] = &charger
->attr_state
.attr
;
1382 charger
->attrs
[2] = &charger
->attr_externally_control
.attr
;
1383 charger
->attrs
[3] = NULL
;
1385 charger
->attr_grp
.name
= name
;
1386 charger
->attr_grp
.attrs
= charger
->attrs
;
1387 desc
->sysfs_groups
[i
] = &charger
->attr_grp
;
1389 sysfs_attr_init(&charger
->attr_name
.attr
);
1390 charger
->attr_name
.attr
.name
= "name";
1391 charger
->attr_name
.attr
.mode
= 0444;
1392 charger
->attr_name
.show
= charger_name_show
;
1394 sysfs_attr_init(&charger
->attr_state
.attr
);
1395 charger
->attr_state
.attr
.name
= "state";
1396 charger
->attr_state
.attr
.mode
= 0444;
1397 charger
->attr_state
.show
= charger_state_show
;
1399 sysfs_attr_init(&charger
->attr_externally_control
.attr
);
1400 charger
->attr_externally_control
.attr
.name
1401 = "externally_control";
1402 charger
->attr_externally_control
.attr
.mode
= 0644;
1403 charger
->attr_externally_control
.show
1404 = charger_externally_control_show
;
1405 charger
->attr_externally_control
.store
1406 = charger_externally_control_store
;
1408 if (!desc
->charger_regulators
[i
].externally_control
||
1409 !chargers_externally_control
)
1410 chargers_externally_control
= 0;
1412 dev_info(cm
->dev
, "'%s' regulator's externally_control is %d\n",
1413 charger
->regulator_name
, charger
->externally_control
);
1416 if (chargers_externally_control
) {
1417 dev_err(cm
->dev
, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n");
1424 static int cm_init_thermal_data(struct charger_manager
*cm
,
1425 struct power_supply
*fuel_gauge
)
1427 struct charger_desc
*desc
= cm
->desc
;
1428 union power_supply_propval val
;
1431 /* Verify whether fuel gauge provides battery temperature */
1432 ret
= power_supply_get_property(fuel_gauge
,
1433 POWER_SUPPLY_PROP_TEMP
, &val
);
1436 cm
->charger_psy_desc
.properties
[cm
->charger_psy_desc
.num_properties
] =
1437 POWER_SUPPLY_PROP_TEMP
;
1438 cm
->charger_psy_desc
.num_properties
++;
1439 cm
->desc
->measure_battery_temp
= true;
1441 #ifdef CONFIG_THERMAL
1442 if (ret
&& desc
->thermal_zone
) {
1444 thermal_zone_get_zone_by_name(desc
->thermal_zone
);
1445 if (IS_ERR(cm
->tzd_batt
))
1446 return PTR_ERR(cm
->tzd_batt
);
1448 /* Use external thermometer */
1449 cm
->charger_psy_desc
.properties
[cm
->charger_psy_desc
.num_properties
] =
1450 POWER_SUPPLY_PROP_TEMP_AMBIENT
;
1451 cm
->charger_psy_desc
.num_properties
++;
1452 cm
->desc
->measure_battery_temp
= true;
1456 if (cm
->desc
->measure_battery_temp
) {
1457 /* NOTICE : Default allowable minimum charge temperature is 0 */
1458 if (!desc
->temp_max
)
1459 desc
->temp_max
= CM_DEFAULT_CHARGE_TEMP_MAX
;
1460 if (!desc
->temp_diff
)
1461 desc
->temp_diff
= CM_DEFAULT_RECHARGE_TEMP_DIFF
;
1467 static const struct of_device_id charger_manager_match
[] = {
1469 .compatible
= "charger-manager",
1474 static struct charger_desc
*of_cm_parse_desc(struct device
*dev
)
1476 struct charger_desc
*desc
;
1477 struct device_node
*np
= dev
->of_node
;
1478 u32 poll_mode
= CM_POLL_DISABLE
;
1479 u32 battery_stat
= CM_NO_BATTERY
;
1482 desc
= devm_kzalloc(dev
, sizeof(*desc
), GFP_KERNEL
);
1484 return ERR_PTR(-ENOMEM
);
1486 of_property_read_string(np
, "cm-name", &desc
->psy_name
);
1488 of_property_read_u32(np
, "cm-poll-mode", &poll_mode
);
1489 desc
->polling_mode
= poll_mode
;
1491 of_property_read_u32(np
, "cm-poll-interval",
1492 &desc
->polling_interval_ms
);
1494 of_property_read_u32(np
, "cm-fullbatt-vchkdrop-ms",
1495 &desc
->fullbatt_vchkdrop_ms
);
1496 of_property_read_u32(np
, "cm-fullbatt-vchkdrop-volt",
1497 &desc
->fullbatt_vchkdrop_uV
);
1498 of_property_read_u32(np
, "cm-fullbatt-voltage", &desc
->fullbatt_uV
);
1499 of_property_read_u32(np
, "cm-fullbatt-soc", &desc
->fullbatt_soc
);
1500 of_property_read_u32(np
, "cm-fullbatt-capacity",
1501 &desc
->fullbatt_full_capacity
);
1503 of_property_read_u32(np
, "cm-battery-stat", &battery_stat
);
1504 desc
->battery_present
= battery_stat
;
1507 of_property_read_u32(np
, "cm-num-chargers", &num_chgs
);
1511 /* Allocate empty bin at the tail of array */
1512 desc
->psy_charger_stat
= devm_kcalloc(dev
,
1516 if (!desc
->psy_charger_stat
)
1517 return ERR_PTR(-ENOMEM
);
1519 for (i
= 0; i
< num_chgs
; i
++)
1520 of_property_read_string_index(np
, "cm-chargers",
1521 i
, &desc
->psy_charger_stat
[i
]);
1524 of_property_read_string(np
, "cm-fuel-gauge", &desc
->psy_fuel_gauge
);
1526 of_property_read_string(np
, "cm-thermal-zone", &desc
->thermal_zone
);
1528 of_property_read_u32(np
, "cm-battery-cold", &desc
->temp_min
);
1529 if (of_get_property(np
, "cm-battery-cold-in-minus", NULL
))
1530 desc
->temp_min
*= -1;
1531 of_property_read_u32(np
, "cm-battery-hot", &desc
->temp_max
);
1532 of_property_read_u32(np
, "cm-battery-temp-diff", &desc
->temp_diff
);
1534 of_property_read_u32(np
, "cm-charging-max",
1535 &desc
->charging_max_duration_ms
);
1536 of_property_read_u32(np
, "cm-discharging-max",
1537 &desc
->discharging_max_duration_ms
);
1539 /* battery charger regulators */
1540 desc
->num_charger_regulators
= of_get_child_count(np
);
1541 if (desc
->num_charger_regulators
) {
1542 struct charger_regulator
*chg_regs
;
1543 struct device_node
*child
;
1545 chg_regs
= devm_kcalloc(dev
,
1546 desc
->num_charger_regulators
,
1550 return ERR_PTR(-ENOMEM
);
1552 desc
->charger_regulators
= chg_regs
;
1554 desc
->sysfs_groups
= devm_kcalloc(dev
,
1555 desc
->num_charger_regulators
+ 1,
1556 sizeof(*desc
->sysfs_groups
),
1558 if (!desc
->sysfs_groups
)
1559 return ERR_PTR(-ENOMEM
);
1561 for_each_child_of_node(np
, child
) {
1562 struct charger_cable
*cables
;
1563 struct device_node
*_child
;
1565 of_property_read_string(child
, "cm-regulator-name",
1566 &chg_regs
->regulator_name
);
1568 /* charger cables */
1569 chg_regs
->num_cables
= of_get_child_count(child
);
1570 if (chg_regs
->num_cables
) {
1571 cables
= devm_kcalloc(dev
,
1572 chg_regs
->num_cables
,
1577 return ERR_PTR(-ENOMEM
);
1580 chg_regs
->cables
= cables
;
1582 for_each_child_of_node(child
, _child
) {
1583 of_property_read_string(_child
,
1584 "cm-cable-name", &cables
->name
);
1585 of_property_read_string(_child
,
1587 &cables
->extcon_name
);
1588 of_property_read_u32(_child
,
1591 of_property_read_u32(_child
,
1603 static inline struct charger_desc
*cm_get_drv_data(struct platform_device
*pdev
)
1605 if (pdev
->dev
.of_node
)
1606 return of_cm_parse_desc(&pdev
->dev
);
1607 return dev_get_platdata(&pdev
->dev
);
1610 static enum alarmtimer_restart
cm_timer_func(struct alarm
*alarm
, ktime_t now
)
1612 cm_timer_set
= false;
1613 return ALARMTIMER_NORESTART
;
1616 static int charger_manager_probe(struct platform_device
*pdev
)
1618 struct charger_desc
*desc
= cm_get_drv_data(pdev
);
1619 struct charger_manager
*cm
;
1622 union power_supply_propval val
;
1623 struct power_supply
*fuel_gauge
;
1624 struct power_supply_config psy_cfg
= {};
1627 dev_err(&pdev
->dev
, "No platform data (desc) found\n");
1628 return PTR_ERR(desc
);
1631 cm
= devm_kzalloc(&pdev
->dev
, sizeof(*cm
), GFP_KERNEL
);
1635 /* Basic Values. Unspecified are Null or 0 */
1636 cm
->dev
= &pdev
->dev
;
1638 psy_cfg
.drv_data
= cm
;
1640 /* Initialize alarm timer */
1641 if (alarmtimer_get_rtcdev()) {
1642 cm_timer
= devm_kzalloc(cm
->dev
, sizeof(*cm_timer
), GFP_KERNEL
);
1645 alarm_init(cm_timer
, ALARM_BOOTTIME
, cm_timer_func
);
1649 * Some of the following do not need to be errors.
1650 * Users may intentionally ignore those features.
1652 if (desc
->fullbatt_uV
== 0) {
1653 dev_info(&pdev
->dev
, "Ignoring full-battery voltage threshold as it is not supplied\n");
1655 if (!desc
->fullbatt_vchkdrop_ms
|| !desc
->fullbatt_vchkdrop_uV
) {
1656 dev_info(&pdev
->dev
, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
1657 desc
->fullbatt_vchkdrop_ms
= 0;
1658 desc
->fullbatt_vchkdrop_uV
= 0;
1660 if (desc
->fullbatt_soc
== 0) {
1661 dev_info(&pdev
->dev
, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
1663 if (desc
->fullbatt_full_capacity
== 0) {
1664 dev_info(&pdev
->dev
, "Ignoring full-battery full capacity threshold as it is not supplied\n");
1667 if (!desc
->charger_regulators
|| desc
->num_charger_regulators
< 1) {
1668 dev_err(&pdev
->dev
, "charger_regulators undefined\n");
1672 if (!desc
->psy_charger_stat
|| !desc
->psy_charger_stat
[0]) {
1673 dev_err(&pdev
->dev
, "No power supply defined\n");
1677 if (!desc
->psy_fuel_gauge
) {
1678 dev_err(&pdev
->dev
, "No fuel gauge power supply defined\n");
1682 /* Check if charger's supplies are present at probe */
1683 for (i
= 0; desc
->psy_charger_stat
[i
]; i
++) {
1684 struct power_supply
*psy
;
1686 psy
= power_supply_get_by_name(desc
->psy_charger_stat
[i
]);
1688 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1689 desc
->psy_charger_stat
[i
]);
1692 power_supply_put(psy
);
1695 if (cm
->desc
->polling_mode
!= CM_POLL_DISABLE
&&
1696 (desc
->polling_interval_ms
== 0 ||
1697 msecs_to_jiffies(desc
->polling_interval_ms
) <= CM_JIFFIES_SMALL
)) {
1698 dev_err(&pdev
->dev
, "polling_interval_ms is too small\n");
1702 if (!desc
->charging_max_duration_ms
||
1703 !desc
->discharging_max_duration_ms
) {
1704 dev_info(&pdev
->dev
, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
1705 desc
->charging_max_duration_ms
= 0;
1706 desc
->discharging_max_duration_ms
= 0;
1709 platform_set_drvdata(pdev
, cm
);
1711 memcpy(&cm
->charger_psy_desc
, &psy_default
, sizeof(psy_default
));
1713 if (!desc
->psy_name
)
1714 strncpy(cm
->psy_name_buf
, psy_default
.name
, PSY_NAME_MAX
);
1716 strncpy(cm
->psy_name_buf
, desc
->psy_name
, PSY_NAME_MAX
);
1717 cm
->charger_psy_desc
.name
= cm
->psy_name_buf
;
1719 /* Allocate for psy properties because they may vary */
1720 cm
->charger_psy_desc
.properties
=
1721 devm_kcalloc(&pdev
->dev
,
1722 ARRAY_SIZE(default_charger_props
) +
1723 NUM_CHARGER_PSY_OPTIONAL
,
1724 sizeof(enum power_supply_property
), GFP_KERNEL
);
1725 if (!cm
->charger_psy_desc
.properties
)
1728 memcpy(cm
->charger_psy_desc
.properties
, default_charger_props
,
1729 sizeof(enum power_supply_property
) *
1730 ARRAY_SIZE(default_charger_props
));
1731 cm
->charger_psy_desc
.num_properties
= psy_default
.num_properties
;
1733 /* Find which optional psy-properties are available */
1734 fuel_gauge
= power_supply_get_by_name(desc
->psy_fuel_gauge
);
1736 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1737 desc
->psy_fuel_gauge
);
1740 if (!power_supply_get_property(fuel_gauge
,
1741 POWER_SUPPLY_PROP_CHARGE_NOW
, &val
)) {
1742 cm
->charger_psy_desc
.properties
[cm
->charger_psy_desc
.num_properties
] =
1743 POWER_SUPPLY_PROP_CHARGE_NOW
;
1744 cm
->charger_psy_desc
.num_properties
++;
1746 if (!power_supply_get_property(fuel_gauge
,
1747 POWER_SUPPLY_PROP_CURRENT_NOW
,
1749 cm
->charger_psy_desc
.properties
[cm
->charger_psy_desc
.num_properties
] =
1750 POWER_SUPPLY_PROP_CURRENT_NOW
;
1751 cm
->charger_psy_desc
.num_properties
++;
1754 ret
= cm_init_thermal_data(cm
, fuel_gauge
);
1756 dev_err(&pdev
->dev
, "Failed to initialize thermal data\n");
1757 cm
->desc
->measure_battery_temp
= false;
1759 power_supply_put(fuel_gauge
);
1761 INIT_DELAYED_WORK(&cm
->fullbatt_vchk_work
, fullbatt_vchk
);
1763 /* Register sysfs entry for charger(regulator) */
1764 ret
= charger_manager_prepare_sysfs(cm
);
1767 "Cannot prepare sysfs entry of regulators\n");
1770 psy_cfg
.attr_grp
= desc
->sysfs_groups
;
1772 cm
->charger_psy
= power_supply_register(&pdev
->dev
,
1773 &cm
->charger_psy_desc
,
1775 if (IS_ERR(cm
->charger_psy
)) {
1776 dev_err(&pdev
->dev
, "Cannot register charger-manager with name \"%s\"\n",
1777 cm
->charger_psy_desc
.name
);
1778 return PTR_ERR(cm
->charger_psy
);
1781 /* Register extcon device for charger cable */
1782 ret
= charger_manager_register_extcon(cm
);
1784 dev_err(&pdev
->dev
, "Cannot initialize extcon device\n");
1785 goto err_reg_extcon
;
1788 /* Add to the list */
1789 mutex_lock(&cm_list_mtx
);
1790 list_add(&cm
->entry
, &cm_list
);
1791 mutex_unlock(&cm_list_mtx
);
1794 * Charger-manager is capable of waking up the systme from sleep
1795 * when event is happened through cm_notify_event()
1797 device_init_wakeup(&pdev
->dev
, true);
1798 device_set_wakeup_capable(&pdev
->dev
, false);
1801 * Charger-manager have to check the charging state right after
1802 * initialization of charger-manager and then update current charging
1807 schedule_work(&setup_polling
);
1812 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1813 struct charger_regulator
*charger
;
1815 charger
= &desc
->charger_regulators
[i
];
1816 for (j
= 0; j
< charger
->num_cables
; j
++) {
1817 struct charger_cable
*cable
= &charger
->cables
[j
];
1818 /* Remove notifier block if only edev exists */
1819 if (cable
->extcon_dev
.edev
)
1820 extcon_unregister_interest(&cable
->extcon_dev
);
1823 regulator_put(desc
->charger_regulators
[i
].consumer
);
1826 power_supply_unregister(cm
->charger_psy
);
1831 static int charger_manager_remove(struct platform_device
*pdev
)
1833 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
1834 struct charger_desc
*desc
= cm
->desc
;
1838 /* Remove from the list */
1839 mutex_lock(&cm_list_mtx
);
1840 list_del(&cm
->entry
);
1841 mutex_unlock(&cm_list_mtx
);
1843 cancel_work_sync(&setup_polling
);
1844 cancel_delayed_work_sync(&cm_monitor_work
);
1846 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
1847 struct charger_regulator
*charger
1848 = &desc
->charger_regulators
[i
];
1849 for (j
= 0 ; j
< charger
->num_cables
; j
++) {
1850 struct charger_cable
*cable
= &charger
->cables
[j
];
1851 extcon_unregister_interest(&cable
->extcon_dev
);
1855 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++)
1856 regulator_put(desc
->charger_regulators
[i
].consumer
);
1858 power_supply_unregister(cm
->charger_psy
);
1860 try_charger_enable(cm
, false);
1865 static const struct platform_device_id charger_manager_id
[] = {
1866 { "charger-manager", 0 },
1869 MODULE_DEVICE_TABLE(platform
, charger_manager_id
);
1871 static int cm_suspend_noirq(struct device
*dev
)
1873 if (device_may_wakeup(dev
)) {
1874 device_set_wakeup_capable(dev
, false);
1881 static bool cm_need_to_awake(void)
1883 struct charger_manager
*cm
;
1888 mutex_lock(&cm_list_mtx
);
1889 list_for_each_entry(cm
, &cm_list
, entry
) {
1890 if (is_charging(cm
)) {
1891 mutex_unlock(&cm_list_mtx
);
1895 mutex_unlock(&cm_list_mtx
);
1900 static int cm_suspend_prepare(struct device
*dev
)
1902 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1904 if (cm_need_to_awake())
1908 cm_suspended
= true;
1910 cm_timer_set
= cm_setup_timer();
1913 cancel_work_sync(&setup_polling
);
1914 cancel_delayed_work_sync(&cm_monitor_work
);
1915 cancel_delayed_work(&cm
->fullbatt_vchk_work
);
1921 static void cm_suspend_complete(struct device
*dev
)
1923 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1926 cm_suspended
= false;
1931 alarm_cancel(cm_timer
);
1932 cm_timer_set
= false;
1933 remain
= alarm_expires_remaining(cm_timer
);
1934 cm_suspend_duration_ms
-= ktime_to_ms(remain
);
1935 schedule_work(&setup_polling
);
1940 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1941 if (cm
->fullbatt_vchk_jiffies_at
) {
1942 unsigned long delay
= 0;
1943 unsigned long now
= jiffies
+ CM_JIFFIES_SMALL
;
1945 if (time_after_eq(now
, cm
->fullbatt_vchk_jiffies_at
)) {
1946 delay
= (unsigned long)((long)now
1947 - (long)(cm
->fullbatt_vchk_jiffies_at
));
1948 delay
= jiffies_to_msecs(delay
);
1954 * Account for cm_suspend_duration_ms with assuming that
1955 * timer stops in suspend.
1957 if (delay
> cm_suspend_duration_ms
)
1958 delay
-= cm_suspend_duration_ms
;
1962 queue_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
1963 msecs_to_jiffies(delay
));
1965 device_set_wakeup_capable(cm
->dev
, false);
1968 static const struct dev_pm_ops charger_manager_pm
= {
1969 .prepare
= cm_suspend_prepare
,
1970 .suspend_noirq
= cm_suspend_noirq
,
1971 .complete
= cm_suspend_complete
,
1974 static struct platform_driver charger_manager_driver
= {
1976 .name
= "charger-manager",
1977 .pm
= &charger_manager_pm
,
1978 .of_match_table
= charger_manager_match
,
1980 .probe
= charger_manager_probe
,
1981 .remove
= charger_manager_remove
,
1982 .id_table
= charger_manager_id
,
1985 static int __init
charger_manager_init(void)
1987 cm_wq
= create_freezable_workqueue("charger_manager");
1988 if (unlikely(!cm_wq
))
1991 INIT_DELAYED_WORK(&cm_monitor_work
, cm_monitor_poller
);
1993 return platform_driver_register(&charger_manager_driver
);
1995 late_initcall(charger_manager_init
);
1997 static void __exit
charger_manager_cleanup(void)
1999 destroy_workqueue(cm_wq
);
2002 platform_driver_unregister(&charger_manager_driver
);
2004 module_exit(charger_manager_cleanup
);
2007 * cm_notify_event - charger driver notify Charger Manager of charger event
2008 * @psy: pointer to instance of charger's power_supply
2009 * @type: type of charger event
2010 * @msg: optional message passed to uevent_notify function
2012 void cm_notify_event(struct power_supply
*psy
, enum cm_event_types type
,
2015 struct charger_manager
*cm
;
2016 bool found_power_supply
= false;
2021 mutex_lock(&cm_list_mtx
);
2022 list_for_each_entry(cm
, &cm_list
, entry
) {
2023 if (match_string(cm
->desc
->psy_charger_stat
, -1,
2024 psy
->desc
->name
) >= 0) {
2025 found_power_supply
= true;
2029 mutex_unlock(&cm_list_mtx
);
2031 if (!found_power_supply
)
2035 case CM_EVENT_BATT_FULL
:
2036 fullbatt_handler(cm
);
2038 case CM_EVENT_BATT_OUT
:
2039 battout_handler(cm
);
2041 case CM_EVENT_BATT_IN
:
2042 case CM_EVENT_EXT_PWR_IN_OUT
... CM_EVENT_CHG_START_STOP
:
2043 misc_event_handler(cm
, type
);
2045 case CM_EVENT_UNKNOWN
:
2046 case CM_EVENT_OTHERS
:
2047 uevent_notify(cm
, msg
? msg
: default_event_names
[type
]);
2050 dev_err(cm
->dev
, "%s: type not specified\n", __func__
);
2054 EXPORT_SYMBOL_GPL(cm_notify_event
);
2056 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
2057 MODULE_DESCRIPTION("Charger Manager");
2058 MODULE_LICENSE("GPL");