2 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
3 * MyungJoo Ham <myungjoo.ham@samsung.com>
5 * This driver enables to monitor battery health and control charger
6 * during suspend-to-mem.
7 * Charger manager depends on other devices. register this later than
8 * the depending devices.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/rtc.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <linux/platform_device.h>
25 #include <linux/power/charger-manager.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/sysfs.h>
29 #include <linux/thermal.h>
32 * Default termperature threshold for charging.
33 * Every temperature units are in tenth of centigrade.
35 #define CM_DEFAULT_RECHARGE_TEMP_DIFF 50
36 #define CM_DEFAULT_CHARGE_TEMP_MAX 500
38 static const char * const default_event_names
[] = {
39 [CM_EVENT_UNKNOWN
] = "Unknown",
40 [CM_EVENT_BATT_FULL
] = "Battery Full",
41 [CM_EVENT_BATT_IN
] = "Battery Inserted",
42 [CM_EVENT_BATT_OUT
] = "Battery Pulled Out",
43 [CM_EVENT_BATT_OVERHEAT
] = "Battery Overheat",
44 [CM_EVENT_BATT_COLD
] = "Battery Cold",
45 [CM_EVENT_EXT_PWR_IN_OUT
] = "External Power Attach/Detach",
46 [CM_EVENT_CHG_START_STOP
] = "Charging Start/Stop",
47 [CM_EVENT_OTHERS
] = "Other battery events"
51 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
52 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
55 #define CM_JIFFIES_SMALL (2)
57 /* If y is valid (> 0) and smaller than x, do x = y */
58 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
61 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
62 * rtc alarm. It should be 2 or larger
64 #define CM_RTC_SMALL (2)
66 #define UEVENT_BUF_SIZE 32
68 static LIST_HEAD(cm_list
);
69 static DEFINE_MUTEX(cm_list_mtx
);
71 /* About in-suspend (suspend-again) monitoring */
72 static struct alarm
*cm_timer
;
74 static bool cm_suspended
;
75 static bool cm_timer_set
;
76 static unsigned long cm_suspend_duration_ms
;
78 /* About normal (not suspended) monitoring */
79 static unsigned long polling_jiffy
= ULONG_MAX
; /* ULONG_MAX: no polling */
80 static unsigned long next_polling
; /* Next appointed polling time */
81 static struct workqueue_struct
*cm_wq
; /* init at driver add */
82 static struct delayed_work cm_monitor_work
; /* init at driver add */
85 * is_batt_present - See if the battery presents in place.
86 * @cm: the Charger Manager representing the battery.
88 static bool is_batt_present(struct charger_manager
*cm
)
90 union power_supply_propval val
;
91 struct power_supply
*psy
;
95 switch (cm
->desc
->battery_present
) {
96 case CM_BATTERY_PRESENT
:
102 psy
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
106 ret
= psy
->get_property(psy
,
107 POWER_SUPPLY_PROP_PRESENT
, &val
);
108 if (ret
== 0 && val
.intval
)
111 case CM_CHARGER_STAT
:
112 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
113 psy
= power_supply_get_by_name(
114 cm
->desc
->psy_charger_stat
[i
]);
116 dev_err(cm
->dev
, "Cannot find power supply \"%s\"\n",
117 cm
->desc
->psy_charger_stat
[i
]);
121 ret
= psy
->get_property(psy
, POWER_SUPPLY_PROP_PRESENT
,
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
= psy
->get_property(psy
, POWER_SUPPLY_PROP_ONLINE
, &val
);
159 if (ret
== 0 && val
.intval
) {
169 * get_batt_uV - Get the voltage level of the battery
170 * @cm: the Charger Manager representing the battery.
171 * @uV: the voltage level returned.
173 * Returns 0 if there is no error.
174 * Returns a negative value on error.
176 static int get_batt_uV(struct charger_manager
*cm
, int *uV
)
178 union power_supply_propval val
;
179 struct power_supply
*fuel_gauge
;
182 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
186 ret
= fuel_gauge
->get_property(fuel_gauge
,
187 POWER_SUPPLY_PROP_VOLTAGE_NOW
, &val
);
196 * is_charging - Returns true if the battery is being charged.
197 * @cm: the Charger Manager representing the battery.
199 static bool is_charging(struct charger_manager
*cm
)
202 bool charging
= false;
203 struct power_supply
*psy
;
204 union power_supply_propval val
;
206 /* If there is no battery, it cannot be charged */
207 if (!is_batt_present(cm
))
210 /* If at least one of the charger is charging, return yes */
211 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
212 /* 1. The charger sholuld not be DISABLED */
213 if (cm
->emergency_stop
)
215 if (!cm
->charger_enabled
)
218 psy
= power_supply_get_by_name(cm
->desc
->psy_charger_stat
[i
]);
220 dev_err(cm
->dev
, "Cannot find power supply \"%s\"\n",
221 cm
->desc
->psy_charger_stat
[i
]);
225 /* 2. The charger should be online (ext-power) */
226 ret
= psy
->get_property(psy
, POWER_SUPPLY_PROP_ONLINE
, &val
);
228 dev_warn(cm
->dev
, "Cannot read ONLINE value from %s\n",
229 cm
->desc
->psy_charger_stat
[i
]);
236 * 3. The charger should not be FULL, DISCHARGING,
239 ret
= psy
->get_property(psy
, POWER_SUPPLY_PROP_STATUS
, &val
);
241 dev_warn(cm
->dev
, "Cannot read STATUS value from %s\n",
242 cm
->desc
->psy_charger_stat
[i
]);
245 if (val
.intval
== POWER_SUPPLY_STATUS_FULL
||
246 val
.intval
== POWER_SUPPLY_STATUS_DISCHARGING
||
247 val
.intval
== POWER_SUPPLY_STATUS_NOT_CHARGING
)
250 /* Then, this is charging. */
259 * is_full_charged - Returns true if the battery is fully charged.
260 * @cm: the Charger Manager representing the battery.
262 static bool is_full_charged(struct charger_manager
*cm
)
264 struct charger_desc
*desc
= cm
->desc
;
265 union power_supply_propval val
;
266 struct power_supply
*fuel_gauge
;
270 /* If there is no battery, it cannot be charged */
271 if (!is_batt_present(cm
))
274 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
278 if (desc
->fullbatt_full_capacity
> 0) {
281 /* Not full if capacity of fuel gauge isn't full */
282 ret
= fuel_gauge
->get_property(fuel_gauge
,
283 POWER_SUPPLY_PROP_CHARGE_FULL
, &val
);
284 if (!ret
&& val
.intval
> desc
->fullbatt_full_capacity
)
288 /* Full, if it's over the fullbatt voltage */
289 if (desc
->fullbatt_uV
> 0) {
290 ret
= get_batt_uV(cm
, &uV
);
291 if (!ret
&& uV
>= desc
->fullbatt_uV
)
295 /* Full, if the capacity is more than fullbatt_soc */
296 if (desc
->fullbatt_soc
> 0) {
299 ret
= fuel_gauge
->get_property(fuel_gauge
,
300 POWER_SUPPLY_PROP_CAPACITY
, &val
);
301 if (!ret
&& val
.intval
>= desc
->fullbatt_soc
)
309 * is_polling_required - Return true if need to continue polling for this CM.
310 * @cm: the Charger Manager representing the battery.
312 static bool is_polling_required(struct charger_manager
*cm
)
314 switch (cm
->desc
->polling_mode
) {
315 case CM_POLL_DISABLE
:
319 case CM_POLL_EXTERNAL_POWER_ONLY
:
320 return is_ext_pwr_online(cm
);
321 case CM_POLL_CHARGING_ONLY
:
322 return is_charging(cm
);
324 dev_warn(cm
->dev
, "Incorrect polling_mode (%d)\n",
325 cm
->desc
->polling_mode
);
332 * try_charger_enable - Enable/Disable chargers altogether
333 * @cm: the Charger Manager representing the battery.
334 * @enable: true: enable / false: disable
336 * Note that Charger Manager keeps the charger enabled regardless whether
337 * the charger is charging or not (because battery is full or no external
338 * power source exists) except when CM needs to disable chargers forcibly
339 * bacause of emergency causes; when the battery is overheated or too cold.
341 static int try_charger_enable(struct charger_manager
*cm
, bool enable
)
344 struct charger_desc
*desc
= cm
->desc
;
346 /* Ignore if it's redundent command */
347 if (enable
== cm
->charger_enabled
)
351 if (cm
->emergency_stop
)
355 * Save start time of charging to limit
356 * maximum possible charging time.
358 cm
->charging_start_time
= ktime_to_ms(ktime_get());
359 cm
->charging_end_time
= 0;
361 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
362 if (desc
->charger_regulators
[i
].externally_control
)
365 err
= regulator_enable(desc
->charger_regulators
[i
].consumer
);
367 dev_warn(cm
->dev
, "Cannot enable %s regulator\n",
368 desc
->charger_regulators
[i
].regulator_name
);
373 * Save end time of charging to maintain fully charged state
374 * of battery after full-batt.
376 cm
->charging_start_time
= 0;
377 cm
->charging_end_time
= ktime_to_ms(ktime_get());
379 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
380 if (desc
->charger_regulators
[i
].externally_control
)
383 err
= regulator_disable(desc
->charger_regulators
[i
].consumer
);
385 dev_warn(cm
->dev
, "Cannot disable %s regulator\n",
386 desc
->charger_regulators
[i
].regulator_name
);
391 * Abnormal battery state - Stop charging forcibly,
392 * even if charger was enabled at the other places
394 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
395 if (regulator_is_enabled(
396 desc
->charger_regulators
[i
].consumer
)) {
397 regulator_force_disable(
398 desc
->charger_regulators
[i
].consumer
);
399 dev_warn(cm
->dev
, "Disable regulator(%s) forcibly\n",
400 desc
->charger_regulators
[i
].regulator_name
);
406 cm
->charger_enabled
= enable
;
412 * try_charger_restart - Restart charging.
413 * @cm: the Charger Manager representing the battery.
415 * Restart charging by turning off and on the charger.
417 static int try_charger_restart(struct charger_manager
*cm
)
421 if (cm
->emergency_stop
)
424 err
= try_charger_enable(cm
, false);
428 return try_charger_enable(cm
, true);
432 * uevent_notify - Let users know something has changed.
433 * @cm: the Charger Manager representing the battery.
434 * @event: the event string.
436 * If @event is null, it implies that uevent_notify is called
437 * by resume function. When called in the resume function, cm_suspended
438 * should be already reset to false in order to let uevent_notify
439 * notify the recent event during the suspend to users. While
440 * suspended, uevent_notify does not notify users, but tracks
441 * events so that uevent_notify can notify users later after resumed.
443 static void uevent_notify(struct charger_manager
*cm
, const char *event
)
445 static char env_str
[UEVENT_BUF_SIZE
+ 1] = "";
446 static char env_str_save
[UEVENT_BUF_SIZE
+ 1] = "";
449 /* Nothing in suspended-event buffer */
450 if (env_str_save
[0] == 0) {
451 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
452 return; /* status not changed */
453 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
457 if (!strncmp(env_str_save
, event
, UEVENT_BUF_SIZE
))
458 return; /* Duplicated. */
459 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
464 /* No messages pending */
465 if (!env_str_save
[0])
468 strncpy(env_str
, env_str_save
, UEVENT_BUF_SIZE
);
469 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
475 /* status not changed */
476 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
479 /* save the status and notify the update */
480 strncpy(env_str
, event
, UEVENT_BUF_SIZE
);
481 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
483 dev_info(cm
->dev
, "%s\n", event
);
487 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
488 * @work: the work_struct appointing the function
490 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
491 * charger_desc, Charger Manager checks voltage drop after the battery
492 * "FULL" event. It checks whether the voltage has dropped more than
493 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
495 static void fullbatt_vchk(struct work_struct
*work
)
497 struct delayed_work
*dwork
= to_delayed_work(work
);
498 struct charger_manager
*cm
= container_of(dwork
,
499 struct charger_manager
, fullbatt_vchk_work
);
500 struct charger_desc
*desc
= cm
->desc
;
501 int batt_uV
, err
, diff
;
503 /* remove the appointment for fullbatt_vchk */
504 cm
->fullbatt_vchk_jiffies_at
= 0;
506 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
509 err
= get_batt_uV(cm
, &batt_uV
);
511 dev_err(cm
->dev
, "%s: get_batt_uV error(%d)\n", __func__
, err
);
515 diff
= desc
->fullbatt_uV
- batt_uV
;
519 dev_info(cm
->dev
, "VBATT dropped %duV after full-batt\n", diff
);
521 if (diff
> desc
->fullbatt_vchkdrop_uV
) {
522 try_charger_restart(cm
);
523 uevent_notify(cm
, "Recharging");
528 * check_charging_duration - Monitor charging/discharging duration
529 * @cm: the Charger Manager representing the battery.
531 * If whole charging duration exceed 'charging_max_duration_ms',
532 * cm stop charging to prevent overcharge/overheat. If discharging
533 * duration exceed 'discharging _max_duration_ms', charger cable is
534 * attached, after full-batt, cm start charging to maintain fully
535 * charged state for battery.
537 static int check_charging_duration(struct charger_manager
*cm
)
539 struct charger_desc
*desc
= cm
->desc
;
540 u64 curr
= ktime_to_ms(ktime_get());
544 if (!desc
->charging_max_duration_ms
&&
545 !desc
->discharging_max_duration_ms
)
548 if (cm
->charger_enabled
) {
549 duration
= curr
- cm
->charging_start_time
;
551 if (duration
> desc
->charging_max_duration_ms
) {
552 dev_info(cm
->dev
, "Charging duration exceed %ums\n",
553 desc
->charging_max_duration_ms
);
554 uevent_notify(cm
, "Discharging");
555 try_charger_enable(cm
, false);
558 } else if (is_ext_pwr_online(cm
) && !cm
->charger_enabled
) {
559 duration
= curr
- cm
->charging_end_time
;
561 if (duration
> desc
->charging_max_duration_ms
&&
562 is_ext_pwr_online(cm
)) {
563 dev_info(cm
->dev
, "Discharging duration exceed %ums\n",
564 desc
->discharging_max_duration_ms
);
565 uevent_notify(cm
, "Recharging");
566 try_charger_enable(cm
, true);
574 static int cm_get_battery_temperature_by_psy(struct charger_manager
*cm
,
577 struct power_supply
*fuel_gauge
;
579 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
583 return fuel_gauge
->get_property(fuel_gauge
,
584 POWER_SUPPLY_PROP_TEMP
,
585 (union power_supply_propval
*)temp
);
588 static int cm_get_battery_temperature(struct charger_manager
*cm
,
593 if (!cm
->desc
->measure_battery_temp
)
596 #ifdef CONFIG_THERMAL
598 ret
= thermal_zone_get_temp(cm
->tzd_batt
, (unsigned long *)temp
);
600 /* Calibrate temperature unit */
605 /* if-else continued from CONFIG_THERMAL */
606 ret
= cm_get_battery_temperature_by_psy(cm
, temp
);
612 static int cm_check_thermal_status(struct charger_manager
*cm
)
614 struct charger_desc
*desc
= cm
->desc
;
615 int temp
, upper_limit
, lower_limit
;
618 ret
= cm_get_battery_temperature(cm
, &temp
);
621 * No information of battery temperature might
622 * occur hazadous result. We have to handle it
623 * depending on battery type.
625 dev_err(cm
->dev
, "Failed to get battery temperature\n");
629 upper_limit
= desc
->temp_max
;
630 lower_limit
= desc
->temp_min
;
632 if (cm
->emergency_stop
) {
633 upper_limit
-= desc
->temp_diff
;
634 lower_limit
+= desc
->temp_diff
;
637 if (temp
> upper_limit
)
638 ret
= CM_EVENT_BATT_OVERHEAT
;
639 else if (temp
< lower_limit
)
640 ret
= CM_EVENT_BATT_COLD
;
646 * _cm_monitor - Monitor the temperature and return true for exceptions.
647 * @cm: the Charger Manager representing the battery.
649 * Returns true if there is an event to notify for the battery.
650 * (True if the status of "emergency_stop" changes)
652 static bool _cm_monitor(struct charger_manager
*cm
)
656 temp_alrt
= cm_check_thermal_status(cm
);
658 /* It has been stopped already */
659 if (temp_alrt
&& cm
->emergency_stop
)
663 * Check temperature whether overheat or cold.
664 * If temperature is out of range normal state, stop charging.
667 cm
->emergency_stop
= temp_alrt
;
668 if (!try_charger_enable(cm
, false))
669 uevent_notify(cm
, default_event_names
[temp_alrt
]);
672 * Check whole charging duration and discharing duration
675 } else if (!cm
->emergency_stop
&& check_charging_duration(cm
)) {
677 "Charging/Discharging duration is out of range\n");
679 * Check dropped voltage of battery. If battery voltage is more
680 * dropped than fullbatt_vchkdrop_uV after fully charged state,
681 * charger-manager have to recharge battery.
683 } else if (!cm
->emergency_stop
&& is_ext_pwr_online(cm
) &&
684 !cm
->charger_enabled
) {
685 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
688 * Check whether fully charged state to protect overcharge
689 * if charger-manager is charging for battery.
691 } else if (!cm
->emergency_stop
&& is_full_charged(cm
) &&
692 cm
->charger_enabled
) {
693 dev_info(cm
->dev
, "EVENT_HANDLE: Battery Fully Charged\n");
694 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_FULL
]);
696 try_charger_enable(cm
, false);
698 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
700 cm
->emergency_stop
= 0;
701 if (is_ext_pwr_online(cm
)) {
702 if (!try_charger_enable(cm
, true))
703 uevent_notify(cm
, "CHARGING");
711 * cm_monitor - Monitor every battery.
713 * Returns true if there is an event to notify from any of the batteries.
714 * (True if the status of "emergency_stop" changes)
716 static bool cm_monitor(void)
719 struct charger_manager
*cm
;
721 mutex_lock(&cm_list_mtx
);
723 list_for_each_entry(cm
, &cm_list
, entry
) {
728 mutex_unlock(&cm_list_mtx
);
734 * _setup_polling - Setup the next instance of polling.
735 * @work: work_struct of the function _setup_polling.
737 static void _setup_polling(struct work_struct
*work
)
739 unsigned long min
= ULONG_MAX
;
740 struct charger_manager
*cm
;
741 bool keep_polling
= false;
742 unsigned long _next_polling
;
744 mutex_lock(&cm_list_mtx
);
746 list_for_each_entry(cm
, &cm_list
, entry
) {
747 if (is_polling_required(cm
) && cm
->desc
->polling_interval_ms
) {
750 if (min
> cm
->desc
->polling_interval_ms
)
751 min
= cm
->desc
->polling_interval_ms
;
755 polling_jiffy
= msecs_to_jiffies(min
);
756 if (polling_jiffy
<= CM_JIFFIES_SMALL
)
757 polling_jiffy
= CM_JIFFIES_SMALL
+ 1;
760 polling_jiffy
= ULONG_MAX
;
761 if (polling_jiffy
== ULONG_MAX
)
764 WARN(cm_wq
== NULL
, "charger-manager: workqueue not initialized"
765 ". try it later. %s\n", __func__
);
768 * Use mod_delayed_work() iff the next polling interval should
769 * occur before the currently scheduled one. If @cm_monitor_work
770 * isn't active, the end result is the same, so no need to worry
771 * about stale @next_polling.
773 _next_polling
= jiffies
+ polling_jiffy
;
775 if (time_before(_next_polling
, next_polling
)) {
776 mod_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
);
777 next_polling
= _next_polling
;
779 if (queue_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
))
780 next_polling
= _next_polling
;
783 mutex_unlock(&cm_list_mtx
);
785 static DECLARE_WORK(setup_polling
, _setup_polling
);
788 * cm_monitor_poller - The Monitor / Poller.
789 * @work: work_struct of the function cm_monitor_poller
791 * During non-suspended state, cm_monitor_poller is used to poll and monitor
794 static void cm_monitor_poller(struct work_struct
*work
)
797 schedule_work(&setup_polling
);
801 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
802 * @cm: the Charger Manager representing the battery.
804 static void fullbatt_handler(struct charger_manager
*cm
)
806 struct charger_desc
*desc
= cm
->desc
;
808 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
812 device_set_wakeup_capable(cm
->dev
, true);
814 mod_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
815 msecs_to_jiffies(desc
->fullbatt_vchkdrop_ms
));
816 cm
->fullbatt_vchk_jiffies_at
= jiffies
+ msecs_to_jiffies(
817 desc
->fullbatt_vchkdrop_ms
);
819 if (cm
->fullbatt_vchk_jiffies_at
== 0)
820 cm
->fullbatt_vchk_jiffies_at
= 1;
823 dev_info(cm
->dev
, "EVENT_HANDLE: Battery Fully Charged\n");
824 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_FULL
]);
828 * battout_handler - Event handler for CM_EVENT_BATT_OUT
829 * @cm: the Charger Manager representing the battery.
831 static void battout_handler(struct charger_manager
*cm
)
834 device_set_wakeup_capable(cm
->dev
, true);
836 if (!is_batt_present(cm
)) {
837 dev_emerg(cm
->dev
, "Battery Pulled Out!\n");
838 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_OUT
]);
840 uevent_notify(cm
, "Battery Reinserted?");
845 * misc_event_handler - Handler for other evnets
846 * @cm: the Charger Manager representing the battery.
847 * @type: the Charger Manager representing the battery.
849 static void misc_event_handler(struct charger_manager
*cm
,
850 enum cm_event_types type
)
853 device_set_wakeup_capable(cm
->dev
, true);
855 if (is_polling_required(cm
) && cm
->desc
->polling_interval_ms
)
856 schedule_work(&setup_polling
);
857 uevent_notify(cm
, default_event_names
[type
]);
860 static int charger_get_property(struct power_supply
*psy
,
861 enum power_supply_property psp
,
862 union power_supply_propval
*val
)
864 struct charger_manager
*cm
= container_of(psy
,
865 struct charger_manager
, charger_psy
);
866 struct charger_desc
*desc
= cm
->desc
;
867 struct power_supply
*fuel_gauge
;
872 case POWER_SUPPLY_PROP_STATUS
:
874 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
875 else if (is_ext_pwr_online(cm
))
876 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
878 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
880 case POWER_SUPPLY_PROP_HEALTH
:
881 if (cm
->emergency_stop
> 0)
882 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
883 else if (cm
->emergency_stop
< 0)
884 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
886 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
888 case POWER_SUPPLY_PROP_PRESENT
:
889 if (is_batt_present(cm
))
894 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
895 ret
= get_batt_uV(cm
, &val
->intval
);
897 case POWER_SUPPLY_PROP_CURRENT_NOW
:
898 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
903 ret
= fuel_gauge
->get_property(fuel_gauge
,
904 POWER_SUPPLY_PROP_CURRENT_NOW
, val
);
906 case POWER_SUPPLY_PROP_TEMP
:
907 case POWER_SUPPLY_PROP_TEMP_AMBIENT
:
908 return cm_get_battery_temperature(cm
, &val
->intval
);
909 case POWER_SUPPLY_PROP_CAPACITY
:
910 fuel_gauge
= power_supply_get_by_name(cm
->desc
->psy_fuel_gauge
);
916 if (!is_batt_present(cm
)) {
917 /* There is no battery. Assume 100% */
922 ret
= fuel_gauge
->get_property(fuel_gauge
,
923 POWER_SUPPLY_PROP_CAPACITY
, val
);
927 if (val
->intval
> 100) {
934 /* Do not adjust SOC when charging: voltage is overrated */
939 * If the capacity value is inconsistent, calibrate it base on
940 * the battery voltage values and the thresholds given as desc
942 ret
= get_batt_uV(cm
, &uV
);
944 /* Voltage information not available. No calibration */
949 if (desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
956 case POWER_SUPPLY_PROP_ONLINE
:
957 if (is_ext_pwr_online(cm
))
962 case POWER_SUPPLY_PROP_CHARGE_FULL
:
963 if (is_full_charged(cm
))
969 case POWER_SUPPLY_PROP_CHARGE_NOW
:
970 if (is_charging(cm
)) {
971 fuel_gauge
= power_supply_get_by_name(
972 cm
->desc
->psy_fuel_gauge
);
978 ret
= fuel_gauge
->get_property(fuel_gauge
,
979 POWER_SUPPLY_PROP_CHARGE_NOW
,
985 /* If CHARGE_NOW is supplied, use it */
986 val
->intval
= (val
->intval
> 0) ?
999 #define NUM_CHARGER_PSY_OPTIONAL (4)
1000 static enum power_supply_property default_charger_props
[] = {
1001 /* Guaranteed to provide */
1002 POWER_SUPPLY_PROP_STATUS
,
1003 POWER_SUPPLY_PROP_HEALTH
,
1004 POWER_SUPPLY_PROP_PRESENT
,
1005 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
1006 POWER_SUPPLY_PROP_CAPACITY
,
1007 POWER_SUPPLY_PROP_ONLINE
,
1008 POWER_SUPPLY_PROP_CHARGE_FULL
,
1010 * Optional properties are:
1011 * POWER_SUPPLY_PROP_CHARGE_NOW,
1012 * POWER_SUPPLY_PROP_CURRENT_NOW,
1013 * POWER_SUPPLY_PROP_TEMP, and
1014 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
1018 static struct power_supply psy_default
= {
1020 .type
= POWER_SUPPLY_TYPE_BATTERY
,
1021 .properties
= default_charger_props
,
1022 .num_properties
= ARRAY_SIZE(default_charger_props
),
1023 .get_property
= charger_get_property
,
1028 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
1029 * for suspend_again.
1031 * Returns true if the alarm is set for Charger Manager to use.
1033 * cm_setup_timer fails to set an alarm,
1034 * cm_setup_timer does not need to set an alarm for Charger Manager,
1035 * or an alarm previously configured is to be used.
1037 static bool cm_setup_timer(void)
1039 struct charger_manager
*cm
;
1040 unsigned int wakeup_ms
= UINT_MAX
;
1043 if (time_after(next_polling
, jiffies
))
1044 CM_MIN_VALID(wakeup_ms
,
1045 jiffies_to_msecs(next_polling
- jiffies
));
1047 mutex_lock(&cm_list_mtx
);
1048 list_for_each_entry(cm
, &cm_list
, entry
) {
1049 unsigned int fbchk_ms
= 0;
1051 /* fullbatt_vchk is required. setup timer for that */
1052 if (cm
->fullbatt_vchk_jiffies_at
) {
1053 fbchk_ms
= jiffies_to_msecs(cm
->fullbatt_vchk_jiffies_at
1055 if (time_is_before_eq_jiffies(
1056 cm
->fullbatt_vchk_jiffies_at
) ||
1057 msecs_to_jiffies(fbchk_ms
) < CM_JIFFIES_SMALL
) {
1058 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
1062 CM_MIN_VALID(wakeup_ms
, fbchk_ms
);
1064 /* Skip if polling is not required for this CM */
1065 if (!is_polling_required(cm
) && !cm
->emergency_stop
)
1068 if (cm
->desc
->polling_interval_ms
== 0)
1070 CM_MIN_VALID(wakeup_ms
, cm
->desc
->polling_interval_ms
);
1072 mutex_unlock(&cm_list_mtx
);
1074 if (timer_req
&& cm_timer
) {
1078 * Set alarm with the polling interval (wakeup_ms)
1079 * The alarm time should be NOW + CM_RTC_SMALL or later.
1081 if (wakeup_ms
== UINT_MAX
||
1082 wakeup_ms
< CM_RTC_SMALL
* MSEC_PER_SEC
)
1083 wakeup_ms
= 2 * CM_RTC_SMALL
* MSEC_PER_SEC
;
1085 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms
);
1087 now
= ktime_get_boottime();
1088 add
= ktime_set(wakeup_ms
/ MSEC_PER_SEC
,
1089 (wakeup_ms
% MSEC_PER_SEC
) * NSEC_PER_MSEC
);
1090 alarm_start(cm_timer
, ktime_add(now
, add
));
1092 cm_suspend_duration_ms
= wakeup_ms
;
1100 * charger_extcon_work - enable/diable charger according to the state
1103 * @work: work_struct of the function charger_extcon_work.
1105 static void charger_extcon_work(struct work_struct
*work
)
1107 struct charger_cable
*cable
=
1108 container_of(work
, struct charger_cable
, wq
);
1111 if (cable
->attached
&& cable
->min_uA
!= 0 && cable
->max_uA
!= 0) {
1112 ret
= regulator_set_current_limit(cable
->charger
->consumer
,
1113 cable
->min_uA
, cable
->max_uA
);
1115 pr_err("Cannot set current limit of %s (%s)\n",
1116 cable
->charger
->regulator_name
, cable
->name
);
1120 pr_info("Set current limit of %s : %duA ~ %duA\n",
1121 cable
->charger
->regulator_name
,
1122 cable
->min_uA
, cable
->max_uA
);
1125 try_charger_enable(cable
->cm
, cable
->attached
);
1129 * charger_extcon_notifier - receive the state of charger cable
1130 * when registered cable is attached or detached.
1132 * @self: the notifier block of the charger_extcon_notifier.
1133 * @event: the cable state.
1134 * @ptr: the data pointer of notifier block.
1136 static int charger_extcon_notifier(struct notifier_block
*self
,
1137 unsigned long event
, void *ptr
)
1139 struct charger_cable
*cable
=
1140 container_of(self
, struct charger_cable
, nb
);
1143 * The newly state of charger cable.
1144 * If cable is attached, cable->attached is true.
1146 cable
->attached
= event
;
1149 * Setup monitoring to check battery state
1150 * when charger cable is attached.
1152 if (cable
->attached
&& is_polling_required(cable
->cm
)) {
1153 cancel_work_sync(&setup_polling
);
1154 schedule_work(&setup_polling
);
1158 * Setup work for controlling charger(regulator)
1159 * according to charger cable.
1161 schedule_work(&cable
->wq
);
1167 * charger_extcon_init - register external connector to use it
1168 * as the charger cable
1170 * @cm: the Charger Manager representing the battery.
1171 * @cable: the Charger cable representing the external connector.
1173 static int charger_extcon_init(struct charger_manager
*cm
,
1174 struct charger_cable
*cable
)
1179 * Charger manager use Extcon framework to identify
1180 * the charger cable among various external connector
1181 * cable (e.g., TA, USB, MHL, Dock).
1183 INIT_WORK(&cable
->wq
, charger_extcon_work
);
1184 cable
->nb
.notifier_call
= charger_extcon_notifier
;
1185 ret
= extcon_register_interest(&cable
->extcon_dev
,
1186 cable
->extcon_name
, cable
->name
, &cable
->nb
);
1188 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1189 cable
->extcon_name
, cable
->name
);
1197 * charger_manager_register_extcon - Register extcon device to recevie state
1199 * @cm: the Charger Manager representing the battery.
1201 * This function support EXTCON(External Connector) subsystem to detect the
1202 * state of charger cables for enabling or disabling charger(regulator) and
1203 * select the charger cable for charging among a number of external cable
1204 * according to policy of H/W board.
1206 static int charger_manager_register_extcon(struct charger_manager
*cm
)
1208 struct charger_desc
*desc
= cm
->desc
;
1209 struct charger_regulator
*charger
;
1214 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1215 charger
= &desc
->charger_regulators
[i
];
1217 charger
->consumer
= regulator_get(cm
->dev
,
1218 charger
->regulator_name
);
1219 if (IS_ERR(charger
->consumer
)) {
1220 dev_err(cm
->dev
, "Cannot find charger(%s)\n",
1221 charger
->regulator_name
);
1222 return PTR_ERR(charger
->consumer
);
1226 for (j
= 0; j
< charger
->num_cables
; j
++) {
1227 struct charger_cable
*cable
= &charger
->cables
[j
];
1229 ret
= charger_extcon_init(cm
, cable
);
1231 dev_err(cm
->dev
, "Cannot initialize charger(%s)\n",
1232 charger
->regulator_name
);
1235 cable
->charger
= charger
;
1244 /* help function of sysfs node to control charger(regulator) */
1245 static ssize_t
charger_name_show(struct device
*dev
,
1246 struct device_attribute
*attr
, char *buf
)
1248 struct charger_regulator
*charger
1249 = container_of(attr
, struct charger_regulator
, attr_name
);
1251 return sprintf(buf
, "%s\n", charger
->regulator_name
);
1254 static ssize_t
charger_state_show(struct device
*dev
,
1255 struct device_attribute
*attr
, char *buf
)
1257 struct charger_regulator
*charger
1258 = container_of(attr
, struct charger_regulator
, attr_state
);
1261 if (!charger
->externally_control
)
1262 state
= regulator_is_enabled(charger
->consumer
);
1264 return sprintf(buf
, "%s\n", state
? "enabled" : "disabled");
1267 static ssize_t
charger_externally_control_show(struct device
*dev
,
1268 struct device_attribute
*attr
, char *buf
)
1270 struct charger_regulator
*charger
= container_of(attr
,
1271 struct charger_regulator
, attr_externally_control
);
1273 return sprintf(buf
, "%d\n", charger
->externally_control
);
1276 static ssize_t
charger_externally_control_store(struct device
*dev
,
1277 struct device_attribute
*attr
, const char *buf
,
1280 struct charger_regulator
*charger
1281 = container_of(attr
, struct charger_regulator
,
1282 attr_externally_control
);
1283 struct charger_manager
*cm
= charger
->cm
;
1284 struct charger_desc
*desc
= cm
->desc
;
1287 int externally_control
;
1288 int chargers_externally_control
= 1;
1290 ret
= sscanf(buf
, "%d", &externally_control
);
1296 if (!externally_control
) {
1297 charger
->externally_control
= 0;
1301 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1302 if (&desc
->charger_regulators
[i
] != charger
&&
1303 !desc
->charger_regulators
[i
].externally_control
) {
1305 * At least, one charger is controlled by
1308 chargers_externally_control
= 0;
1313 if (!chargers_externally_control
) {
1314 if (cm
->charger_enabled
) {
1315 try_charger_enable(charger
->cm
, false);
1316 charger
->externally_control
= externally_control
;
1317 try_charger_enable(charger
->cm
, true);
1319 charger
->externally_control
= externally_control
;
1323 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1324 charger
->regulator_name
);
1331 * charger_manager_register_sysfs - Register sysfs entry for each charger
1332 * @cm: the Charger Manager representing the battery.
1334 * This function add sysfs entry for charger(regulator) to control charger from
1335 * user-space. If some development board use one more chargers for charging
1336 * but only need one charger on specific case which is dependent on user
1337 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1338 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1339 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1340 * externally_control, this charger isn't controlled from charger-manager and
1341 * always stay off state of regulator.
1343 static int charger_manager_register_sysfs(struct charger_manager
*cm
)
1345 struct charger_desc
*desc
= cm
->desc
;
1346 struct charger_regulator
*charger
;
1347 int chargers_externally_control
= 1;
1353 /* Create sysfs entry to control charger(regulator) */
1354 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1355 charger
= &desc
->charger_regulators
[i
];
1357 snprintf(buf
, 10, "charger.%d", i
);
1358 str
= devm_kzalloc(cm
->dev
,
1359 sizeof(char) * (strlen(buf
) + 1), GFP_KERNEL
);
1366 charger
->attrs
[0] = &charger
->attr_name
.attr
;
1367 charger
->attrs
[1] = &charger
->attr_state
.attr
;
1368 charger
->attrs
[2] = &charger
->attr_externally_control
.attr
;
1369 charger
->attrs
[3] = NULL
;
1370 charger
->attr_g
.name
= str
;
1371 charger
->attr_g
.attrs
= charger
->attrs
;
1373 sysfs_attr_init(&charger
->attr_name
.attr
);
1374 charger
->attr_name
.attr
.name
= "name";
1375 charger
->attr_name
.attr
.mode
= 0444;
1376 charger
->attr_name
.show
= charger_name_show
;
1378 sysfs_attr_init(&charger
->attr_state
.attr
);
1379 charger
->attr_state
.attr
.name
= "state";
1380 charger
->attr_state
.attr
.mode
= 0444;
1381 charger
->attr_state
.show
= charger_state_show
;
1383 sysfs_attr_init(&charger
->attr_externally_control
.attr
);
1384 charger
->attr_externally_control
.attr
.name
1385 = "externally_control";
1386 charger
->attr_externally_control
.attr
.mode
= 0644;
1387 charger
->attr_externally_control
.show
1388 = charger_externally_control_show
;
1389 charger
->attr_externally_control
.store
1390 = charger_externally_control_store
;
1392 if (!desc
->charger_regulators
[i
].externally_control
||
1393 !chargers_externally_control
)
1394 chargers_externally_control
= 0;
1396 dev_info(cm
->dev
, "'%s' regulator's externally_control is %d\n",
1397 charger
->regulator_name
, charger
->externally_control
);
1399 ret
= sysfs_create_group(&cm
->charger_psy
.dev
->kobj
,
1402 dev_err(cm
->dev
, "Cannot create sysfs entry of %s regulator\n",
1403 charger
->regulator_name
);
1409 if (chargers_externally_control
) {
1410 dev_err(cm
->dev
, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n");
1419 static int cm_init_thermal_data(struct charger_manager
*cm
,
1420 struct power_supply
*fuel_gauge
)
1422 struct charger_desc
*desc
= cm
->desc
;
1423 union power_supply_propval val
;
1426 /* Verify whether fuel gauge provides battery temperature */
1427 ret
= fuel_gauge
->get_property(fuel_gauge
,
1428 POWER_SUPPLY_PROP_TEMP
, &val
);
1431 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1432 POWER_SUPPLY_PROP_TEMP
;
1433 cm
->charger_psy
.num_properties
++;
1434 cm
->desc
->measure_battery_temp
= true;
1436 #ifdef CONFIG_THERMAL
1437 if (ret
&& desc
->thermal_zone
) {
1439 thermal_zone_get_zone_by_name(desc
->thermal_zone
);
1440 if (IS_ERR(cm
->tzd_batt
))
1441 return PTR_ERR(cm
->tzd_batt
);
1443 /* Use external thermometer */
1444 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1445 POWER_SUPPLY_PROP_TEMP_AMBIENT
;
1446 cm
->charger_psy
.num_properties
++;
1447 cm
->desc
->measure_battery_temp
= true;
1451 if (cm
->desc
->measure_battery_temp
) {
1452 /* NOTICE : Default allowable minimum charge temperature is 0 */
1453 if (!desc
->temp_max
)
1454 desc
->temp_max
= CM_DEFAULT_CHARGE_TEMP_MAX
;
1455 if (!desc
->temp_diff
)
1456 desc
->temp_diff
= CM_DEFAULT_RECHARGE_TEMP_DIFF
;
1462 static struct of_device_id charger_manager_match
[] = {
1464 .compatible
= "charger-manager",
1469 static struct charger_desc
*of_cm_parse_desc(struct device
*dev
)
1471 struct charger_desc
*desc
;
1472 struct device_node
*np
= dev
->of_node
;
1473 u32 poll_mode
= CM_POLL_DISABLE
;
1474 u32 battery_stat
= CM_NO_BATTERY
;
1477 desc
= devm_kzalloc(dev
, sizeof(*desc
), GFP_KERNEL
);
1479 return ERR_PTR(-ENOMEM
);
1481 of_property_read_string(np
, "cm-name", &desc
->psy_name
);
1483 of_property_read_u32(np
, "cm-poll-mode", &poll_mode
);
1484 desc
->polling_mode
= poll_mode
;
1486 of_property_read_u32(np
, "cm-poll-interval",
1487 &desc
->polling_interval_ms
);
1489 of_property_read_u32(np
, "cm-fullbatt-vchkdrop-ms",
1490 &desc
->fullbatt_vchkdrop_ms
);
1491 of_property_read_u32(np
, "cm-fullbatt-vchkdrop-volt",
1492 &desc
->fullbatt_vchkdrop_uV
);
1493 of_property_read_u32(np
, "cm-fullbatt-voltage", &desc
->fullbatt_uV
);
1494 of_property_read_u32(np
, "cm-fullbatt-soc", &desc
->fullbatt_soc
);
1495 of_property_read_u32(np
, "cm-fullbatt-capacity",
1496 &desc
->fullbatt_full_capacity
);
1498 of_property_read_u32(np
, "cm-battery-stat", &battery_stat
);
1499 desc
->battery_present
= battery_stat
;
1502 of_property_read_u32(np
, "cm-num-chargers", &num_chgs
);
1504 /* Allocate empty bin at the tail of array */
1505 desc
->psy_charger_stat
= devm_kzalloc(dev
, sizeof(char *)
1506 * (num_chgs
+ 1), GFP_KERNEL
);
1507 if (desc
->psy_charger_stat
) {
1509 for (i
= 0; i
< num_chgs
; i
++)
1510 of_property_read_string_index(np
, "cm-chargers",
1511 i
, &desc
->psy_charger_stat
[i
]);
1513 return ERR_PTR(-ENOMEM
);
1517 of_property_read_string(np
, "cm-fuel-gauge", &desc
->psy_fuel_gauge
);
1519 of_property_read_string(np
, "cm-thermal-zone", &desc
->thermal_zone
);
1521 of_property_read_u32(np
, "cm-battery-cold", &desc
->temp_min
);
1522 if (of_get_property(np
, "cm-battery-cold-in-minus", NULL
))
1523 desc
->temp_min
*= -1;
1524 of_property_read_u32(np
, "cm-battery-hot", &desc
->temp_max
);
1525 of_property_read_u32(np
, "cm-battery-temp-diff", &desc
->temp_diff
);
1527 of_property_read_u32(np
, "cm-charging-max",
1528 &desc
->charging_max_duration_ms
);
1529 of_property_read_u32(np
, "cm-discharging-max",
1530 &desc
->discharging_max_duration_ms
);
1532 /* battery charger regualtors */
1533 desc
->num_charger_regulators
= of_get_child_count(np
);
1534 if (desc
->num_charger_regulators
) {
1535 struct charger_regulator
*chg_regs
;
1536 struct device_node
*child
;
1538 chg_regs
= devm_kzalloc(dev
, sizeof(*chg_regs
)
1539 * desc
->num_charger_regulators
,
1542 return ERR_PTR(-ENOMEM
);
1544 desc
->charger_regulators
= chg_regs
;
1546 for_each_child_of_node(np
, child
) {
1547 struct charger_cable
*cables
;
1548 struct device_node
*_child
;
1550 of_property_read_string(child
, "cm-regulator-name",
1551 &chg_regs
->regulator_name
);
1553 /* charger cables */
1554 chg_regs
->num_cables
= of_get_child_count(child
);
1555 if (chg_regs
->num_cables
) {
1556 cables
= devm_kzalloc(dev
, sizeof(*cables
)
1557 * chg_regs
->num_cables
,
1560 return ERR_PTR(-ENOMEM
);
1562 chg_regs
->cables
= cables
;
1564 for_each_child_of_node(child
, _child
) {
1565 of_property_read_string(_child
,
1566 "cm-cable-name", &cables
->name
);
1567 of_property_read_string(_child
,
1569 &cables
->extcon_name
);
1570 of_property_read_u32(_child
,
1573 of_property_read_u32(_child
,
1585 static inline struct charger_desc
*cm_get_drv_data(struct platform_device
*pdev
)
1587 if (pdev
->dev
.of_node
)
1588 return of_cm_parse_desc(&pdev
->dev
);
1589 return dev_get_platdata(&pdev
->dev
);
1592 static enum alarmtimer_restart
cm_timer_func(struct alarm
*alarm
, ktime_t now
)
1594 cm_timer_set
= false;
1595 return ALARMTIMER_NORESTART
;
1598 static int charger_manager_probe(struct platform_device
*pdev
)
1600 struct charger_desc
*desc
= cm_get_drv_data(pdev
);
1601 struct charger_manager
*cm
;
1604 union power_supply_propval val
;
1605 struct power_supply
*fuel_gauge
;
1608 dev_err(&pdev
->dev
, "No platform data (desc) found\n");
1612 cm
= devm_kzalloc(&pdev
->dev
,
1613 sizeof(struct charger_manager
), GFP_KERNEL
);
1617 /* Basic Values. Unspecified are Null or 0 */
1618 cm
->dev
= &pdev
->dev
;
1621 /* Initialize alarm timer */
1622 if (alarmtimer_get_rtcdev()) {
1623 cm_timer
= devm_kzalloc(cm
->dev
, sizeof(*cm_timer
), GFP_KERNEL
);
1624 alarm_init(cm_timer
, ALARM_BOOTTIME
, cm_timer_func
);
1628 * The following two do not need to be errors.
1629 * Users may intentionally ignore those two features.
1631 if (desc
->fullbatt_uV
== 0) {
1632 dev_info(&pdev
->dev
, "Ignoring full-battery voltage threshold as it is not supplied\n");
1634 if (!desc
->fullbatt_vchkdrop_ms
|| !desc
->fullbatt_vchkdrop_uV
) {
1635 dev_info(&pdev
->dev
, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
1636 desc
->fullbatt_vchkdrop_ms
= 0;
1637 desc
->fullbatt_vchkdrop_uV
= 0;
1639 if (desc
->fullbatt_soc
== 0) {
1640 dev_info(&pdev
->dev
, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
1642 if (desc
->fullbatt_full_capacity
== 0) {
1643 dev_info(&pdev
->dev
, "Ignoring full-battery full capacity threshold as it is not supplied\n");
1646 if (!desc
->charger_regulators
|| desc
->num_charger_regulators
< 1) {
1647 dev_err(&pdev
->dev
, "charger_regulators undefined\n");
1651 if (!desc
->psy_charger_stat
|| !desc
->psy_charger_stat
[0]) {
1652 dev_err(&pdev
->dev
, "No power supply defined\n");
1656 if (!desc
->psy_fuel_gauge
) {
1657 dev_err(&pdev
->dev
, "No fuel gauge power supply defined\n");
1661 /* Counting index only */
1662 while (desc
->psy_charger_stat
[i
])
1665 /* Check if charger's supplies are present at probe */
1666 for (i
= 0; desc
->psy_charger_stat
[i
]; i
++) {
1667 struct power_supply
*psy
;
1669 psy
= power_supply_get_by_name(desc
->psy_charger_stat
[i
]);
1671 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1672 desc
->psy_charger_stat
[i
]);
1677 fuel_gauge
= power_supply_get_by_name(desc
->psy_fuel_gauge
);
1679 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1680 desc
->psy_fuel_gauge
);
1684 if (desc
->polling_interval_ms
== 0 ||
1685 msecs_to_jiffies(desc
->polling_interval_ms
) <= CM_JIFFIES_SMALL
) {
1686 dev_err(&pdev
->dev
, "polling_interval_ms is too small\n");
1690 if (!desc
->charging_max_duration_ms
||
1691 !desc
->discharging_max_duration_ms
) {
1692 dev_info(&pdev
->dev
, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
1693 desc
->charging_max_duration_ms
= 0;
1694 desc
->discharging_max_duration_ms
= 0;
1697 platform_set_drvdata(pdev
, cm
);
1699 memcpy(&cm
->charger_psy
, &psy_default
, sizeof(psy_default
));
1701 if (!desc
->psy_name
)
1702 strncpy(cm
->psy_name_buf
, psy_default
.name
, PSY_NAME_MAX
);
1704 strncpy(cm
->psy_name_buf
, desc
->psy_name
, PSY_NAME_MAX
);
1705 cm
->charger_psy
.name
= cm
->psy_name_buf
;
1707 /* Allocate for psy properties because they may vary */
1708 cm
->charger_psy
.properties
= devm_kzalloc(&pdev
->dev
,
1709 sizeof(enum power_supply_property
)
1710 * (ARRAY_SIZE(default_charger_props
) +
1711 NUM_CHARGER_PSY_OPTIONAL
), GFP_KERNEL
);
1712 if (!cm
->charger_psy
.properties
)
1715 memcpy(cm
->charger_psy
.properties
, default_charger_props
,
1716 sizeof(enum power_supply_property
) *
1717 ARRAY_SIZE(default_charger_props
));
1718 cm
->charger_psy
.num_properties
= psy_default
.num_properties
;
1720 /* Find which optional psy-properties are available */
1721 if (!fuel_gauge
->get_property(fuel_gauge
,
1722 POWER_SUPPLY_PROP_CHARGE_NOW
, &val
)) {
1723 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1724 POWER_SUPPLY_PROP_CHARGE_NOW
;
1725 cm
->charger_psy
.num_properties
++;
1727 if (!fuel_gauge
->get_property(fuel_gauge
,
1728 POWER_SUPPLY_PROP_CURRENT_NOW
,
1730 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1731 POWER_SUPPLY_PROP_CURRENT_NOW
;
1732 cm
->charger_psy
.num_properties
++;
1735 ret
= cm_init_thermal_data(cm
, fuel_gauge
);
1737 dev_err(&pdev
->dev
, "Failed to initialize thermal data\n");
1738 cm
->desc
->measure_battery_temp
= false;
1741 INIT_DELAYED_WORK(&cm
->fullbatt_vchk_work
, fullbatt_vchk
);
1743 ret
= power_supply_register(NULL
, &cm
->charger_psy
);
1745 dev_err(&pdev
->dev
, "Cannot register charger-manager with name \"%s\"\n",
1746 cm
->charger_psy
.name
);
1750 /* Register extcon device for charger cable */
1751 ret
= charger_manager_register_extcon(cm
);
1753 dev_err(&pdev
->dev
, "Cannot initialize extcon device\n");
1754 goto err_reg_extcon
;
1757 /* Register sysfs entry for charger(regulator) */
1758 ret
= charger_manager_register_sysfs(cm
);
1761 "Cannot initialize sysfs entry of regulator\n");
1765 /* Add to the list */
1766 mutex_lock(&cm_list_mtx
);
1767 list_add(&cm
->entry
, &cm_list
);
1768 mutex_unlock(&cm_list_mtx
);
1771 * Charger-manager is capable of waking up the systme from sleep
1772 * when event is happend through cm_notify_event()
1774 device_init_wakeup(&pdev
->dev
, true);
1775 device_set_wakeup_capable(&pdev
->dev
, false);
1778 * Charger-manager have to check the charging state right after
1779 * tialization of charger-manager and then update current charging
1784 schedule_work(&setup_polling
);
1789 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1790 struct charger_regulator
*charger
;
1792 charger
= &desc
->charger_regulators
[i
];
1793 sysfs_remove_group(&cm
->charger_psy
.dev
->kobj
,
1797 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1798 struct charger_regulator
*charger
;
1800 charger
= &desc
->charger_regulators
[i
];
1801 for (j
= 0; j
< charger
->num_cables
; j
++) {
1802 struct charger_cable
*cable
= &charger
->cables
[j
];
1803 /* Remove notifier block if only edev exists */
1804 if (cable
->extcon_dev
.edev
)
1805 extcon_unregister_interest(&cable
->extcon_dev
);
1808 regulator_put(desc
->charger_regulators
[i
].consumer
);
1811 power_supply_unregister(&cm
->charger_psy
);
1816 static int charger_manager_remove(struct platform_device
*pdev
)
1818 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
1819 struct charger_desc
*desc
= cm
->desc
;
1823 /* Remove from the list */
1824 mutex_lock(&cm_list_mtx
);
1825 list_del(&cm
->entry
);
1826 mutex_unlock(&cm_list_mtx
);
1828 cancel_work_sync(&setup_polling
);
1829 cancel_delayed_work_sync(&cm_monitor_work
);
1831 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
1832 struct charger_regulator
*charger
1833 = &desc
->charger_regulators
[i
];
1834 for (j
= 0 ; j
< charger
->num_cables
; j
++) {
1835 struct charger_cable
*cable
= &charger
->cables
[j
];
1836 extcon_unregister_interest(&cable
->extcon_dev
);
1840 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++)
1841 regulator_put(desc
->charger_regulators
[i
].consumer
);
1843 power_supply_unregister(&cm
->charger_psy
);
1845 try_charger_enable(cm
, false);
1850 static const struct platform_device_id charger_manager_id
[] = {
1851 { "charger-manager", 0 },
1854 MODULE_DEVICE_TABLE(platform
, charger_manager_id
);
1856 static int cm_suspend_noirq(struct device
*dev
)
1860 if (device_may_wakeup(dev
)) {
1861 device_set_wakeup_capable(dev
, false);
1868 static bool cm_need_to_awake(void)
1870 struct charger_manager
*cm
;
1875 mutex_lock(&cm_list_mtx
);
1876 list_for_each_entry(cm
, &cm_list
, entry
) {
1877 if (is_charging(cm
)) {
1878 mutex_unlock(&cm_list_mtx
);
1882 mutex_unlock(&cm_list_mtx
);
1887 static int cm_suspend_prepare(struct device
*dev
)
1889 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1891 if (cm_need_to_awake())
1895 cm_suspended
= true;
1897 cm_timer_set
= cm_setup_timer();
1900 cancel_work_sync(&setup_polling
);
1901 cancel_delayed_work_sync(&cm_monitor_work
);
1902 cancel_delayed_work(&cm
->fullbatt_vchk_work
);
1908 static void cm_suspend_complete(struct device
*dev
)
1910 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1913 cm_suspended
= false;
1918 alarm_cancel(cm_timer
);
1919 cm_timer_set
= false;
1920 remain
= alarm_expires_remaining(cm_timer
);
1921 cm_suspend_duration_ms
-= ktime_to_ms(remain
);
1922 schedule_work(&setup_polling
);
1927 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1928 if (cm
->fullbatt_vchk_jiffies_at
) {
1929 unsigned long delay
= 0;
1930 unsigned long now
= jiffies
+ CM_JIFFIES_SMALL
;
1932 if (time_after_eq(now
, cm
->fullbatt_vchk_jiffies_at
)) {
1933 delay
= (unsigned long)((long)now
1934 - (long)(cm
->fullbatt_vchk_jiffies_at
));
1935 delay
= jiffies_to_msecs(delay
);
1941 * Account for cm_suspend_duration_ms with assuming that
1942 * timer stops in suspend.
1944 if (delay
> cm_suspend_duration_ms
)
1945 delay
-= cm_suspend_duration_ms
;
1949 queue_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
1950 msecs_to_jiffies(delay
));
1952 device_set_wakeup_capable(cm
->dev
, false);
1955 static const struct dev_pm_ops charger_manager_pm
= {
1956 .prepare
= cm_suspend_prepare
,
1957 .suspend_noirq
= cm_suspend_noirq
,
1958 .complete
= cm_suspend_complete
,
1961 static struct platform_driver charger_manager_driver
= {
1963 .name
= "charger-manager",
1964 .pm
= &charger_manager_pm
,
1965 .of_match_table
= charger_manager_match
,
1967 .probe
= charger_manager_probe
,
1968 .remove
= charger_manager_remove
,
1969 .id_table
= charger_manager_id
,
1972 static int __init
charger_manager_init(void)
1974 cm_wq
= create_freezable_workqueue("charger_manager");
1975 INIT_DELAYED_WORK(&cm_monitor_work
, cm_monitor_poller
);
1977 return platform_driver_register(&charger_manager_driver
);
1979 late_initcall(charger_manager_init
);
1981 static void __exit
charger_manager_cleanup(void)
1983 destroy_workqueue(cm_wq
);
1986 platform_driver_unregister(&charger_manager_driver
);
1988 module_exit(charger_manager_cleanup
);
1991 * find_power_supply - find the associated power_supply of charger
1992 * @cm: the Charger Manager representing the battery
1993 * @psy: pointer to instance of charger's power_supply
1995 static bool find_power_supply(struct charger_manager
*cm
,
1996 struct power_supply
*psy
)
2001 for (i
= 0; cm
->desc
->psy_charger_stat
[i
]; i
++) {
2002 if (!strcmp(psy
->name
, cm
->desc
->psy_charger_stat
[i
])) {
2012 * cm_notify_event - charger driver notify Charger Manager of charger event
2013 * @psy: pointer to instance of charger's power_supply
2014 * @type: type of charger event
2015 * @msg: optional message passed to uevent_notify fuction
2017 void cm_notify_event(struct power_supply
*psy
, enum cm_event_types type
,
2020 struct charger_manager
*cm
;
2021 bool found_power_supply
= false;
2026 mutex_lock(&cm_list_mtx
);
2027 list_for_each_entry(cm
, &cm_list
, entry
) {
2028 found_power_supply
= find_power_supply(cm
, psy
);
2029 if (found_power_supply
)
2032 mutex_unlock(&cm_list_mtx
);
2034 if (!found_power_supply
)
2038 case CM_EVENT_BATT_FULL
:
2039 fullbatt_handler(cm
);
2041 case CM_EVENT_BATT_OUT
:
2042 battout_handler(cm
);
2044 case CM_EVENT_BATT_IN
:
2045 case CM_EVENT_EXT_PWR_IN_OUT
... CM_EVENT_CHG_START_STOP
:
2046 misc_event_handler(cm
, type
);
2048 case CM_EVENT_UNKNOWN
:
2049 case CM_EVENT_OTHERS
:
2050 uevent_notify(cm
, msg
? msg
: default_event_names
[type
]);
2053 dev_err(cm
->dev
, "%s: type not specified\n", __func__
);
2057 EXPORT_SYMBOL_GPL(cm_notify_event
);
2059 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
2060 MODULE_DESCRIPTION("Charger Manager");
2061 MODULE_LICENSE("GPL");