2 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
3 * MyungJoo Ham <myungjoo.ham@samsung.com>
5 * This driver enables to monitor battery health and control charger
6 * during suspend-to-mem.
7 * Charger manager depends on other devices. register this later than
8 * the depending devices.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/irq.h>
18 #include <linux/interrupt.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
22 #include <linux/platform_device.h>
23 #include <linux/power/charger-manager.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/sysfs.h>
27 static const char * const default_event_names
[] = {
28 [CM_EVENT_UNKNOWN
] = "Unknown",
29 [CM_EVENT_BATT_FULL
] = "Battery Full",
30 [CM_EVENT_BATT_IN
] = "Battery Inserted",
31 [CM_EVENT_BATT_OUT
] = "Battery Pulled Out",
32 [CM_EVENT_EXT_PWR_IN_OUT
] = "External Power Attach/Detach",
33 [CM_EVENT_CHG_START_STOP
] = "Charging Start/Stop",
34 [CM_EVENT_OTHERS
] = "Other battery events"
38 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
39 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
42 #define CM_JIFFIES_SMALL (2)
44 /* If y is valid (> 0) and smaller than x, do x = y */
45 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
48 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
49 * rtc alarm. It should be 2 or larger
51 #define CM_RTC_SMALL (2)
53 #define UEVENT_BUF_SIZE 32
55 static LIST_HEAD(cm_list
);
56 static DEFINE_MUTEX(cm_list_mtx
);
58 /* About in-suspend (suspend-again) monitoring */
59 static struct rtc_device
*rtc_dev
;
62 * Save the wakeup alarm before entering suspend-to-RAM
64 static struct rtc_wkalrm rtc_wkalarm_save
;
65 /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
66 static unsigned long rtc_wkalarm_save_time
;
67 static bool cm_suspended
;
68 static bool cm_rtc_set
;
69 static unsigned long cm_suspend_duration_ms
;
71 /* About normal (not suspended) monitoring */
72 static unsigned long polling_jiffy
= ULONG_MAX
; /* ULONG_MAX: no polling */
73 static unsigned long next_polling
; /* Next appointed polling time */
74 static struct workqueue_struct
*cm_wq
; /* init at driver add */
75 static struct delayed_work cm_monitor_work
; /* init at driver add */
77 /* Global charger-manager description */
78 static struct charger_global_desc
*g_desc
; /* init with setup_charger_manager */
81 * is_batt_present - See if the battery presents in place.
82 * @cm: the Charger Manager representing the battery.
84 static bool is_batt_present(struct charger_manager
*cm
)
86 union power_supply_propval val
;
90 switch (cm
->desc
->battery_present
) {
91 case CM_BATTERY_PRESENT
:
97 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
98 POWER_SUPPLY_PROP_PRESENT
, &val
);
99 if (ret
== 0 && val
.intval
)
102 case CM_CHARGER_STAT
:
103 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
104 ret
= cm
->charger_stat
[i
]->get_property(
106 POWER_SUPPLY_PROP_PRESENT
, &val
);
107 if (ret
== 0 && val
.intval
) {
119 * is_ext_pwr_online - See if an external power source is attached to charge
120 * @cm: the Charger Manager representing the battery.
122 * Returns true if at least one of the chargers of the battery has an external
123 * power source attached to charge the battery regardless of whether it is
124 * actually charging or not.
126 static bool is_ext_pwr_online(struct charger_manager
*cm
)
128 union power_supply_propval val
;
132 /* If at least one of them has one, it's yes. */
133 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
134 ret
= cm
->charger_stat
[i
]->get_property(
136 POWER_SUPPLY_PROP_ONLINE
, &val
);
137 if (ret
== 0 && val
.intval
) {
147 * get_batt_uV - Get the voltage level of the battery
148 * @cm: the Charger Manager representing the battery.
149 * @uV: the voltage level returned.
151 * Returns 0 if there is no error.
152 * Returns a negative value on error.
154 static int get_batt_uV(struct charger_manager
*cm
, int *uV
)
156 union power_supply_propval val
;
162 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
163 POWER_SUPPLY_PROP_VOLTAGE_NOW
, &val
);
172 * is_charging - Returns true if the battery is being charged.
173 * @cm: the Charger Manager representing the battery.
175 static bool is_charging(struct charger_manager
*cm
)
178 bool charging
= false;
179 union power_supply_propval val
;
181 /* If there is no battery, it cannot be charged */
182 if (!is_batt_present(cm
))
185 /* If at least one of the charger is charging, return yes */
186 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
187 /* 1. The charger sholuld not be DISABLED */
188 if (cm
->emergency_stop
)
190 if (!cm
->charger_enabled
)
193 /* 2. The charger should be online (ext-power) */
194 ret
= cm
->charger_stat
[i
]->get_property(
196 POWER_SUPPLY_PROP_ONLINE
, &val
);
198 dev_warn(cm
->dev
, "Cannot read ONLINE value from %s.\n",
199 cm
->desc
->psy_charger_stat
[i
]);
206 * 3. The charger should not be FULL, DISCHARGING,
209 ret
= cm
->charger_stat
[i
]->get_property(
211 POWER_SUPPLY_PROP_STATUS
, &val
);
213 dev_warn(cm
->dev
, "Cannot read STATUS value from %s.\n",
214 cm
->desc
->psy_charger_stat
[i
]);
217 if (val
.intval
== POWER_SUPPLY_STATUS_FULL
||
218 val
.intval
== POWER_SUPPLY_STATUS_DISCHARGING
||
219 val
.intval
== POWER_SUPPLY_STATUS_NOT_CHARGING
)
222 /* Then, this is charging. */
231 * is_full_charged - Returns true if the battery is fully charged.
232 * @cm: the Charger Manager representing the battery.
234 static bool is_full_charged(struct charger_manager
*cm
)
236 struct charger_desc
*desc
= cm
->desc
;
237 union power_supply_propval val
;
241 /* If there is no battery, it cannot be charged */
242 if (!is_batt_present(cm
)) {
247 if (cm
->fuel_gauge
&& desc
->fullbatt_full_capacity
> 0) {
248 /* Not full if capacity of fuel gauge isn't full */
249 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
250 POWER_SUPPLY_PROP_CHARGE_FULL
, &val
);
251 if (!ret
&& val
.intval
> desc
->fullbatt_full_capacity
) {
257 /* Full, if it's over the fullbatt voltage */
258 if (desc
->fullbatt_uV
> 0) {
259 ret
= get_batt_uV(cm
, &uV
);
260 if (!ret
&& uV
>= desc
->fullbatt_uV
) {
266 /* Full, if the capacity is more than fullbatt_soc */
267 if (cm
->fuel_gauge
&& desc
->fullbatt_soc
> 0) {
268 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
269 POWER_SUPPLY_PROP_CAPACITY
, &val
);
270 if (!ret
&& val
.intval
>= desc
->fullbatt_soc
) {
279 return val
.intval
? true : false;
283 * is_polling_required - Return true if need to continue polling for this CM.
284 * @cm: the Charger Manager representing the battery.
286 static bool is_polling_required(struct charger_manager
*cm
)
288 switch (cm
->desc
->polling_mode
) {
289 case CM_POLL_DISABLE
:
293 case CM_POLL_EXTERNAL_POWER_ONLY
:
294 return is_ext_pwr_online(cm
);
295 case CM_POLL_CHARGING_ONLY
:
296 return is_charging(cm
);
298 dev_warn(cm
->dev
, "Incorrect polling_mode (%d)\n",
299 cm
->desc
->polling_mode
);
306 * try_charger_enable - Enable/Disable chargers altogether
307 * @cm: the Charger Manager representing the battery.
308 * @enable: true: enable / false: disable
310 * Note that Charger Manager keeps the charger enabled regardless whether
311 * the charger is charging or not (because battery is full or no external
312 * power source exists) except when CM needs to disable chargers forcibly
313 * bacause of emergency causes; when the battery is overheated or too cold.
315 static int try_charger_enable(struct charger_manager
*cm
, bool enable
)
318 struct charger_desc
*desc
= cm
->desc
;
320 /* Ignore if it's redundent command */
321 if (enable
== cm
->charger_enabled
)
325 if (cm
->emergency_stop
)
329 * Save start time of charging to limit
330 * maximum possible charging time.
332 cm
->charging_start_time
= ktime_to_ms(ktime_get());
333 cm
->charging_end_time
= 0;
335 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
336 if (desc
->charger_regulators
[i
].externally_control
)
339 err
= regulator_enable(desc
->charger_regulators
[i
].consumer
);
342 "Cannot enable %s regulator\n",
343 desc
->charger_regulators
[i
].regulator_name
);
348 * Save end time of charging to maintain fully charged state
349 * of battery after full-batt.
351 cm
->charging_start_time
= 0;
352 cm
->charging_end_time
= ktime_to_ms(ktime_get());
354 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
355 if (desc
->charger_regulators
[i
].externally_control
)
358 err
= regulator_disable(desc
->charger_regulators
[i
].consumer
);
361 "Cannot disable %s regulator\n",
362 desc
->charger_regulators
[i
].regulator_name
);
367 * Abnormal battery state - Stop charging forcibly,
368 * even if charger was enabled at the other places
370 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
371 if (regulator_is_enabled(
372 desc
->charger_regulators
[i
].consumer
)) {
373 regulator_force_disable(
374 desc
->charger_regulators
[i
].consumer
);
376 "Disable regulator(%s) forcibly.\n",
377 desc
->charger_regulators
[i
].regulator_name
);
383 cm
->charger_enabled
= enable
;
389 * try_charger_restart - Restart charging.
390 * @cm: the Charger Manager representing the battery.
392 * Restart charging by turning off and on the charger.
394 static int try_charger_restart(struct charger_manager
*cm
)
398 if (cm
->emergency_stop
)
401 err
= try_charger_enable(cm
, false);
405 return try_charger_enable(cm
, true);
409 * uevent_notify - Let users know something has changed.
410 * @cm: the Charger Manager representing the battery.
411 * @event: the event string.
413 * If @event is null, it implies that uevent_notify is called
414 * by resume function. When called in the resume function, cm_suspended
415 * should be already reset to false in order to let uevent_notify
416 * notify the recent event during the suspend to users. While
417 * suspended, uevent_notify does not notify users, but tracks
418 * events so that uevent_notify can notify users later after resumed.
420 static void uevent_notify(struct charger_manager
*cm
, const char *event
)
422 static char env_str
[UEVENT_BUF_SIZE
+ 1] = "";
423 static char env_str_save
[UEVENT_BUF_SIZE
+ 1] = "";
426 /* Nothing in suspended-event buffer */
427 if (env_str_save
[0] == 0) {
428 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
429 return; /* status not changed */
430 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
434 if (!strncmp(env_str_save
, event
, UEVENT_BUF_SIZE
))
435 return; /* Duplicated. */
436 strncpy(env_str_save
, event
, UEVENT_BUF_SIZE
);
441 /* No messages pending */
442 if (!env_str_save
[0])
445 strncpy(env_str
, env_str_save
, UEVENT_BUF_SIZE
);
446 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
452 /* status not changed */
453 if (!strncmp(env_str
, event
, UEVENT_BUF_SIZE
))
456 /* save the status and notify the update */
457 strncpy(env_str
, event
, UEVENT_BUF_SIZE
);
458 kobject_uevent(&cm
->dev
->kobj
, KOBJ_CHANGE
);
460 dev_info(cm
->dev
, event
);
464 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
465 * @work: the work_struct appointing the function
467 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
468 * charger_desc, Charger Manager checks voltage drop after the battery
469 * "FULL" event. It checks whether the voltage has dropped more than
470 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
472 static void fullbatt_vchk(struct work_struct
*work
)
474 struct delayed_work
*dwork
= to_delayed_work(work
);
475 struct charger_manager
*cm
= container_of(dwork
,
476 struct charger_manager
, fullbatt_vchk_work
);
477 struct charger_desc
*desc
= cm
->desc
;
478 int batt_uV
, err
, diff
;
480 /* remove the appointment for fullbatt_vchk */
481 cm
->fullbatt_vchk_jiffies_at
= 0;
483 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
486 err
= get_batt_uV(cm
, &batt_uV
);
488 dev_err(cm
->dev
, "%s: get_batt_uV error(%d).\n", __func__
, err
);
492 diff
= desc
->fullbatt_uV
;
495 dev_info(cm
->dev
, "VBATT dropped %duV after full-batt.\n", diff
);
497 if (diff
> desc
->fullbatt_vchkdrop_uV
) {
498 try_charger_restart(cm
);
499 uevent_notify(cm
, "Recharging");
504 * check_charging_duration - Monitor charging/discharging duration
505 * @cm: the Charger Manager representing the battery.
507 * If whole charging duration exceed 'charging_max_duration_ms',
508 * cm stop charging to prevent overcharge/overheat. If discharging
509 * duration exceed 'discharging _max_duration_ms', charger cable is
510 * attached, after full-batt, cm start charging to maintain fully
511 * charged state for battery.
513 static int check_charging_duration(struct charger_manager
*cm
)
515 struct charger_desc
*desc
= cm
->desc
;
516 u64 curr
= ktime_to_ms(ktime_get());
520 if (!desc
->charging_max_duration_ms
&&
521 !desc
->discharging_max_duration_ms
)
524 if (cm
->charger_enabled
) {
525 duration
= curr
- cm
->charging_start_time
;
527 if (duration
> desc
->charging_max_duration_ms
) {
528 dev_info(cm
->dev
, "Charging duration exceed %lldms",
529 desc
->charging_max_duration_ms
);
530 uevent_notify(cm
, "Discharging");
531 try_charger_enable(cm
, false);
534 } else if (is_ext_pwr_online(cm
) && !cm
->charger_enabled
) {
535 duration
= curr
- cm
->charging_end_time
;
537 if (duration
> desc
->charging_max_duration_ms
&&
538 is_ext_pwr_online(cm
)) {
539 dev_info(cm
->dev
, "DisCharging duration exceed %lldms",
540 desc
->discharging_max_duration_ms
);
541 uevent_notify(cm
, "Recharing");
542 try_charger_enable(cm
, true);
551 * _cm_monitor - Monitor the temperature and return true for exceptions.
552 * @cm: the Charger Manager representing the battery.
554 * Returns true if there is an event to notify for the battery.
555 * (True if the status of "emergency_stop" changes)
557 static bool _cm_monitor(struct charger_manager
*cm
)
559 struct charger_desc
*desc
= cm
->desc
;
560 int temp
= desc
->temperature_out_of_range(&cm
->last_temp_mC
);
562 dev_dbg(cm
->dev
, "monitoring (%2.2d.%3.3dC)\n",
563 cm
->last_temp_mC
/ 1000, cm
->last_temp_mC
% 1000);
565 /* It has been stopped already */
566 if (temp
&& cm
->emergency_stop
)
570 * Check temperature whether overheat or cold.
571 * If temperature is out of range normal state, stop charging.
574 cm
->emergency_stop
= temp
;
575 if (!try_charger_enable(cm
, false)) {
577 uevent_notify(cm
, "OVERHEAT");
579 uevent_notify(cm
, "COLD");
583 * Check whole charging duration and discharing duration
586 } else if (!cm
->emergency_stop
&& check_charging_duration(cm
)) {
588 "Charging/Discharging duration is out of range");
590 * Check dropped voltage of battery. If battery voltage is more
591 * dropped than fullbatt_vchkdrop_uV after fully charged state,
592 * charger-manager have to recharge battery.
594 } else if (!cm
->emergency_stop
&& is_ext_pwr_online(cm
) &&
595 !cm
->charger_enabled
) {
596 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
599 * Check whether fully charged state to protect overcharge
600 * if charger-manager is charging for battery.
602 } else if (!cm
->emergency_stop
&& is_full_charged(cm
) &&
603 cm
->charger_enabled
) {
604 dev_info(cm
->dev
, "EVENT_HANDLE: Battery Fully Charged.\n");
605 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_FULL
]);
607 try_charger_enable(cm
, false);
609 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
611 cm
->emergency_stop
= 0;
612 if (is_ext_pwr_online(cm
)) {
613 if (!try_charger_enable(cm
, true))
614 uevent_notify(cm
, "CHARGING");
622 * cm_monitor - Monitor every battery.
624 * Returns true if there is an event to notify from any of the batteries.
625 * (True if the status of "emergency_stop" changes)
627 static bool cm_monitor(void)
630 struct charger_manager
*cm
;
632 mutex_lock(&cm_list_mtx
);
634 list_for_each_entry(cm
, &cm_list
, entry
) {
639 mutex_unlock(&cm_list_mtx
);
645 * _setup_polling - Setup the next instance of polling.
646 * @work: work_struct of the function _setup_polling.
648 static void _setup_polling(struct work_struct
*work
)
650 unsigned long min
= ULONG_MAX
;
651 struct charger_manager
*cm
;
652 bool keep_polling
= false;
653 unsigned long _next_polling
;
655 mutex_lock(&cm_list_mtx
);
657 list_for_each_entry(cm
, &cm_list
, entry
) {
658 if (is_polling_required(cm
) && cm
->desc
->polling_interval_ms
) {
661 if (min
> cm
->desc
->polling_interval_ms
)
662 min
= cm
->desc
->polling_interval_ms
;
666 polling_jiffy
= msecs_to_jiffies(min
);
667 if (polling_jiffy
<= CM_JIFFIES_SMALL
)
668 polling_jiffy
= CM_JIFFIES_SMALL
+ 1;
671 polling_jiffy
= ULONG_MAX
;
672 if (polling_jiffy
== ULONG_MAX
)
675 WARN(cm_wq
== NULL
, "charger-manager: workqueue not initialized"
676 ". try it later. %s\n", __func__
);
678 _next_polling
= jiffies
+ polling_jiffy
;
680 if (!delayed_work_pending(&cm_monitor_work
) ||
681 (delayed_work_pending(&cm_monitor_work
) &&
682 time_after(next_polling
, _next_polling
))) {
683 next_polling
= jiffies
+ polling_jiffy
;
684 mod_delayed_work(cm_wq
, &cm_monitor_work
, polling_jiffy
);
688 mutex_unlock(&cm_list_mtx
);
690 static DECLARE_WORK(setup_polling
, _setup_polling
);
693 * cm_monitor_poller - The Monitor / Poller.
694 * @work: work_struct of the function cm_monitor_poller
696 * During non-suspended state, cm_monitor_poller is used to poll and monitor
699 static void cm_monitor_poller(struct work_struct
*work
)
702 schedule_work(&setup_polling
);
706 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
707 * @cm: the Charger Manager representing the battery.
709 static void fullbatt_handler(struct charger_manager
*cm
)
711 struct charger_desc
*desc
= cm
->desc
;
713 if (!desc
->fullbatt_vchkdrop_uV
|| !desc
->fullbatt_vchkdrop_ms
)
717 device_set_wakeup_capable(cm
->dev
, true);
719 mod_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
720 msecs_to_jiffies(desc
->fullbatt_vchkdrop_ms
));
721 cm
->fullbatt_vchk_jiffies_at
= jiffies
+ msecs_to_jiffies(
722 desc
->fullbatt_vchkdrop_ms
);
724 if (cm
->fullbatt_vchk_jiffies_at
== 0)
725 cm
->fullbatt_vchk_jiffies_at
= 1;
728 dev_info(cm
->dev
, "EVENT_HANDLE: Battery Fully Charged.\n");
729 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_FULL
]);
733 * battout_handler - Event handler for CM_EVENT_BATT_OUT
734 * @cm: the Charger Manager representing the battery.
736 static void battout_handler(struct charger_manager
*cm
)
739 device_set_wakeup_capable(cm
->dev
, true);
741 if (!is_batt_present(cm
)) {
742 dev_emerg(cm
->dev
, "Battery Pulled Out!\n");
743 uevent_notify(cm
, default_event_names
[CM_EVENT_BATT_OUT
]);
745 uevent_notify(cm
, "Battery Reinserted?");
750 * misc_event_handler - Handler for other evnets
751 * @cm: the Charger Manager representing the battery.
752 * @type: the Charger Manager representing the battery.
754 static void misc_event_handler(struct charger_manager
*cm
,
755 enum cm_event_types type
)
758 device_set_wakeup_capable(cm
->dev
, true);
760 if (!delayed_work_pending(&cm_monitor_work
) &&
761 is_polling_required(cm
) && cm
->desc
->polling_interval_ms
)
762 schedule_work(&setup_polling
);
763 uevent_notify(cm
, default_event_names
[type
]);
766 static int charger_get_property(struct power_supply
*psy
,
767 enum power_supply_property psp
,
768 union power_supply_propval
*val
)
770 struct charger_manager
*cm
= container_of(psy
,
771 struct charger_manager
, charger_psy
);
772 struct charger_desc
*desc
= cm
->desc
;
777 case POWER_SUPPLY_PROP_STATUS
:
779 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
780 else if (is_ext_pwr_online(cm
))
781 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
783 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
785 case POWER_SUPPLY_PROP_HEALTH
:
786 if (cm
->emergency_stop
> 0)
787 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
788 else if (cm
->emergency_stop
< 0)
789 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
791 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
793 case POWER_SUPPLY_PROP_PRESENT
:
794 if (is_batt_present(cm
))
799 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
800 ret
= get_batt_uV(cm
, &val
->intval
);
802 case POWER_SUPPLY_PROP_CURRENT_NOW
:
803 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
804 POWER_SUPPLY_PROP_CURRENT_NOW
, val
);
806 case POWER_SUPPLY_PROP_TEMP
:
807 /* in thenth of centigrade */
808 if (cm
->last_temp_mC
== INT_MIN
)
809 desc
->temperature_out_of_range(&cm
->last_temp_mC
);
810 val
->intval
= cm
->last_temp_mC
/ 100;
811 if (!desc
->measure_battery_temp
)
814 case POWER_SUPPLY_PROP_TEMP_AMBIENT
:
815 /* in thenth of centigrade */
816 if (cm
->last_temp_mC
== INT_MIN
)
817 desc
->temperature_out_of_range(&cm
->last_temp_mC
);
818 val
->intval
= cm
->last_temp_mC
/ 100;
819 if (desc
->measure_battery_temp
)
822 case POWER_SUPPLY_PROP_CAPACITY
:
823 if (!cm
->fuel_gauge
) {
828 if (!is_batt_present(cm
)) {
829 /* There is no battery. Assume 100% */
834 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
835 POWER_SUPPLY_PROP_CAPACITY
, val
);
839 if (val
->intval
> 100) {
846 /* Do not adjust SOC when charging: voltage is overrated */
851 * If the capacity value is inconsistent, calibrate it base on
852 * the battery voltage values and the thresholds given as desc
854 ret
= get_batt_uV(cm
, &uV
);
856 /* Voltage information not available. No calibration */
861 if (desc
->fullbatt_uV
> 0 && uV
>= desc
->fullbatt_uV
&&
868 case POWER_SUPPLY_PROP_ONLINE
:
869 if (is_ext_pwr_online(cm
))
874 case POWER_SUPPLY_PROP_CHARGE_FULL
:
875 if (is_full_charged(cm
))
881 case POWER_SUPPLY_PROP_CHARGE_NOW
:
882 if (is_charging(cm
)) {
883 ret
= cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
884 POWER_SUPPLY_PROP_CHARGE_NOW
,
890 /* If CHARGE_NOW is supplied, use it */
891 val
->intval
= (val
->intval
> 0) ?
904 #define NUM_CHARGER_PSY_OPTIONAL (4)
905 static enum power_supply_property default_charger_props
[] = {
906 /* Guaranteed to provide */
907 POWER_SUPPLY_PROP_STATUS
,
908 POWER_SUPPLY_PROP_HEALTH
,
909 POWER_SUPPLY_PROP_PRESENT
,
910 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
911 POWER_SUPPLY_PROP_CAPACITY
,
912 POWER_SUPPLY_PROP_ONLINE
,
913 POWER_SUPPLY_PROP_CHARGE_FULL
,
915 * Optional properties are:
916 * POWER_SUPPLY_PROP_CHARGE_NOW,
917 * POWER_SUPPLY_PROP_CURRENT_NOW,
918 * POWER_SUPPLY_PROP_TEMP, and
919 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
923 static struct power_supply psy_default
= {
925 .type
= POWER_SUPPLY_TYPE_BATTERY
,
926 .properties
= default_charger_props
,
927 .num_properties
= ARRAY_SIZE(default_charger_props
),
928 .get_property
= charger_get_property
,
932 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
935 * Returns true if the alarm is set for Charger Manager to use.
937 * cm_setup_timer fails to set an alarm,
938 * cm_setup_timer does not need to set an alarm for Charger Manager,
939 * or an alarm previously configured is to be used.
941 static bool cm_setup_timer(void)
943 struct charger_manager
*cm
;
944 unsigned int wakeup_ms
= UINT_MAX
;
947 mutex_lock(&cm_list_mtx
);
949 list_for_each_entry(cm
, &cm_list
, entry
) {
950 unsigned int fbchk_ms
= 0;
952 /* fullbatt_vchk is required. setup timer for that */
953 if (cm
->fullbatt_vchk_jiffies_at
) {
954 fbchk_ms
= jiffies_to_msecs(cm
->fullbatt_vchk_jiffies_at
956 if (time_is_before_eq_jiffies(
957 cm
->fullbatt_vchk_jiffies_at
) ||
958 msecs_to_jiffies(fbchk_ms
) < CM_JIFFIES_SMALL
) {
959 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
963 CM_MIN_VALID(wakeup_ms
, fbchk_ms
);
965 /* Skip if polling is not required for this CM */
966 if (!is_polling_required(cm
) && !cm
->emergency_stop
)
968 if (cm
->desc
->polling_interval_ms
== 0)
970 CM_MIN_VALID(wakeup_ms
, cm
->desc
->polling_interval_ms
);
973 mutex_unlock(&cm_list_mtx
);
975 if (wakeup_ms
< UINT_MAX
&& wakeup_ms
> 0) {
976 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms
);
978 struct rtc_wkalrm tmp
;
979 unsigned long time
, now
;
980 unsigned long add
= DIV_ROUND_UP(wakeup_ms
, 1000);
983 * Set alarm with the polling interval (wakeup_ms)
984 * except when rtc_wkalarm_save comes first.
985 * However, the alarm time should be NOW +
986 * CM_RTC_SMALL or later.
989 rtc_read_time(rtc_dev
, &tmp
.time
);
990 rtc_tm_to_time(&tmp
.time
, &now
);
991 if (add
< CM_RTC_SMALL
)
997 if (rtc_wkalarm_save
.enabled
&&
998 rtc_wkalarm_save_time
&&
999 rtc_wkalarm_save_time
< time
) {
1000 if (rtc_wkalarm_save_time
< now
+ CM_RTC_SMALL
)
1001 time
= now
+ CM_RTC_SMALL
;
1003 time
= rtc_wkalarm_save_time
;
1005 /* The timer is not appointed by CM */
1009 pr_info("Waking up after %lu secs.\n",
1012 rtc_time_to_tm(time
, &tmp
.time
);
1013 rtc_set_alarm(rtc_dev
, &tmp
);
1014 cm_suspend_duration_ms
+= wakeup_ms
;
1020 rtc_set_alarm(rtc_dev
, &rtc_wkalarm_save
);
1024 static void _cm_fbchk_in_suspend(struct charger_manager
*cm
)
1026 unsigned long jiffy_now
= jiffies
;
1028 if (!cm
->fullbatt_vchk_jiffies_at
)
1031 if (g_desc
&& g_desc
->assume_timer_stops_in_suspend
)
1032 jiffy_now
+= msecs_to_jiffies(cm_suspend_duration_ms
);
1034 /* Execute now if it's going to be executed not too long after */
1035 jiffy_now
+= CM_JIFFIES_SMALL
;
1037 if (time_after_eq(jiffy_now
, cm
->fullbatt_vchk_jiffies_at
))
1038 fullbatt_vchk(&cm
->fullbatt_vchk_work
.work
);
1042 * cm_suspend_again - Determine whether suspend again or not
1044 * Returns true if the system should be suspended again
1045 * Returns false if the system should be woken up
1047 bool cm_suspend_again(void)
1049 struct charger_manager
*cm
;
1052 if (!g_desc
|| !g_desc
->rtc_only_wakeup
|| !g_desc
->rtc_only_wakeup() ||
1060 mutex_lock(&cm_list_mtx
);
1061 list_for_each_entry(cm
, &cm_list
, entry
) {
1062 _cm_fbchk_in_suspend(cm
);
1064 if (cm
->status_save_ext_pwr_inserted
!= is_ext_pwr_online(cm
) ||
1065 cm
->status_save_batt
!= is_batt_present(cm
)) {
1070 mutex_unlock(&cm_list_mtx
);
1072 cm_rtc_set
= cm_setup_timer();
1074 /* It's about the time when the non-CM appointed timer goes off */
1075 if (rtc_wkalarm_save
.enabled
) {
1077 struct rtc_time tmp
;
1079 rtc_read_time(rtc_dev
, &tmp
);
1080 rtc_tm_to_time(&tmp
, &now
);
1082 if (rtc_wkalarm_save_time
&&
1083 now
+ CM_RTC_SMALL
>= rtc_wkalarm_save_time
)
1088 EXPORT_SYMBOL_GPL(cm_suspend_again
);
1091 * setup_charger_manager - initialize charger_global_desc data
1092 * @gd: pointer to instance of charger_global_desc
1094 int setup_charger_manager(struct charger_global_desc
*gd
)
1100 rtc_class_close(rtc_dev
);
1104 if (!gd
->rtc_only_wakeup
) {
1105 pr_err("The callback rtc_only_wakeup is not given.\n");
1110 rtc_dev
= rtc_class_open(gd
->rtc_name
);
1111 if (IS_ERR_OR_NULL(rtc_dev
)) {
1113 /* Retry at probe. RTC may be not registered yet */
1116 pr_warn("No wakeup timer is given for charger manager."
1117 "In-suspend monitoring won't work.\n");
1123 EXPORT_SYMBOL_GPL(setup_charger_manager
);
1126 * charger_extcon_work - enable/diable charger according to the state
1129 * @work: work_struct of the function charger_extcon_work.
1131 static void charger_extcon_work(struct work_struct
*work
)
1133 struct charger_cable
*cable
=
1134 container_of(work
, struct charger_cable
, wq
);
1137 if (cable
->attached
&& cable
->min_uA
!= 0 && cable
->max_uA
!= 0) {
1138 ret
= regulator_set_current_limit(cable
->charger
->consumer
,
1139 cable
->min_uA
, cable
->max_uA
);
1141 pr_err("Cannot set current limit of %s (%s)\n",
1142 cable
->charger
->regulator_name
, cable
->name
);
1146 pr_info("Set current limit of %s : %duA ~ %duA\n",
1147 cable
->charger
->regulator_name
,
1148 cable
->min_uA
, cable
->max_uA
);
1151 try_charger_enable(cable
->cm
, cable
->attached
);
1155 * charger_extcon_notifier - receive the state of charger cable
1156 * when registered cable is attached or detached.
1158 * @self: the notifier block of the charger_extcon_notifier.
1159 * @event: the cable state.
1160 * @ptr: the data pointer of notifier block.
1162 static int charger_extcon_notifier(struct notifier_block
*self
,
1163 unsigned long event
, void *ptr
)
1165 struct charger_cable
*cable
=
1166 container_of(self
, struct charger_cable
, nb
);
1169 * The newly state of charger cable.
1170 * If cable is attached, cable->attached is true.
1172 cable
->attached
= event
;
1175 * Setup monitoring to check battery state
1176 * when charger cable is attached.
1178 if (cable
->attached
&& is_polling_required(cable
->cm
)) {
1179 if (work_pending(&setup_polling
))
1180 cancel_work_sync(&setup_polling
);
1181 schedule_work(&setup_polling
);
1185 * Setup work for controlling charger(regulator)
1186 * according to charger cable.
1188 schedule_work(&cable
->wq
);
1194 * charger_extcon_init - register external connector to use it
1195 * as the charger cable
1197 * @cm: the Charger Manager representing the battery.
1198 * @cable: the Charger cable representing the external connector.
1200 static int charger_extcon_init(struct charger_manager
*cm
,
1201 struct charger_cable
*cable
)
1206 * Charger manager use Extcon framework to identify
1207 * the charger cable among various external connector
1208 * cable (e.g., TA, USB, MHL, Dock).
1210 INIT_WORK(&cable
->wq
, charger_extcon_work
);
1211 cable
->nb
.notifier_call
= charger_extcon_notifier
;
1212 ret
= extcon_register_interest(&cable
->extcon_dev
,
1213 cable
->extcon_name
, cable
->name
, &cable
->nb
);
1215 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1224 /* help function of sysfs node to control charger(regulator) */
1225 static ssize_t
charger_name_show(struct device
*dev
,
1226 struct device_attribute
*attr
, char *buf
)
1228 struct charger_regulator
*charger
1229 = container_of(attr
, struct charger_regulator
, attr_name
);
1231 return sprintf(buf
, "%s\n", charger
->regulator_name
);
1234 static ssize_t
charger_state_show(struct device
*dev
,
1235 struct device_attribute
*attr
, char *buf
)
1237 struct charger_regulator
*charger
1238 = container_of(attr
, struct charger_regulator
, attr_state
);
1241 if (!charger
->externally_control
)
1242 state
= regulator_is_enabled(charger
->consumer
);
1244 return sprintf(buf
, "%s\n", state
? "enabled" : "disabled");
1247 static ssize_t
charger_externally_control_show(struct device
*dev
,
1248 struct device_attribute
*attr
, char *buf
)
1250 struct charger_regulator
*charger
= container_of(attr
,
1251 struct charger_regulator
, attr_externally_control
);
1253 return sprintf(buf
, "%d\n", charger
->externally_control
);
1256 static ssize_t
charger_externally_control_store(struct device
*dev
,
1257 struct device_attribute
*attr
, const char *buf
,
1260 struct charger_regulator
*charger
1261 = container_of(attr
, struct charger_regulator
,
1262 attr_externally_control
);
1263 struct charger_manager
*cm
= charger
->cm
;
1264 struct charger_desc
*desc
= cm
->desc
;
1267 int externally_control
;
1268 int chargers_externally_control
= 1;
1270 ret
= sscanf(buf
, "%d", &externally_control
);
1276 if (!externally_control
) {
1277 charger
->externally_control
= 0;
1281 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1282 if (&desc
->charger_regulators
[i
] != charger
&&
1283 !desc
->charger_regulators
[i
].externally_control
) {
1285 * At least, one charger is controlled by
1288 chargers_externally_control
= 0;
1293 if (!chargers_externally_control
) {
1294 if (cm
->charger_enabled
) {
1295 try_charger_enable(charger
->cm
, false);
1296 charger
->externally_control
= externally_control
;
1297 try_charger_enable(charger
->cm
, true);
1299 charger
->externally_control
= externally_control
;
1303 "'%s' regulator should be controlled "
1304 "in charger-manager because charger-manager "
1305 "must need at least one charger for charging\n",
1306 charger
->regulator_name
);
1312 static int charger_manager_probe(struct platform_device
*pdev
)
1314 struct charger_desc
*desc
= dev_get_platdata(&pdev
->dev
);
1315 struct charger_manager
*cm
;
1318 int chargers_externally_control
= 1;
1319 union power_supply_propval val
;
1321 if (g_desc
&& !rtc_dev
&& g_desc
->rtc_name
) {
1322 rtc_dev
= rtc_class_open(g_desc
->rtc_name
);
1323 if (IS_ERR_OR_NULL(rtc_dev
)) {
1325 dev_err(&pdev
->dev
, "Cannot get RTC %s.\n",
1333 dev_err(&pdev
->dev
, "No platform data (desc) found.\n");
1338 cm
= kzalloc(sizeof(struct charger_manager
), GFP_KERNEL
);
1340 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
1345 /* Basic Values. Unspecified are Null or 0 */
1346 cm
->dev
= &pdev
->dev
;
1347 cm
->desc
= kzalloc(sizeof(struct charger_desc
), GFP_KERNEL
);
1349 dev_err(&pdev
->dev
, "Cannot allocate memory.\n");
1351 goto err_alloc_desc
;
1353 memcpy(cm
->desc
, desc
, sizeof(struct charger_desc
));
1354 cm
->last_temp_mC
= INT_MIN
; /* denotes "unmeasured, yet" */
1357 * The following two do not need to be errors.
1358 * Users may intentionally ignore those two features.
1360 if (desc
->fullbatt_uV
== 0) {
1361 dev_info(&pdev
->dev
, "Ignoring full-battery voltage threshold"
1362 " as it is not supplied.");
1364 if (!desc
->fullbatt_vchkdrop_ms
|| !desc
->fullbatt_vchkdrop_uV
) {
1365 dev_info(&pdev
->dev
, "Disabling full-battery voltage drop "
1366 "checking mechanism as it is not supplied.");
1367 desc
->fullbatt_vchkdrop_ms
= 0;
1368 desc
->fullbatt_vchkdrop_uV
= 0;
1370 if (desc
->fullbatt_soc
== 0) {
1371 dev_info(&pdev
->dev
, "Ignoring full-battery soc(state of"
1372 " charge) threshold as it is not"
1375 if (desc
->fullbatt_full_capacity
== 0) {
1376 dev_info(&pdev
->dev
, "Ignoring full-battery full capacity"
1377 " threshold as it is not supplied.");
1380 if (!desc
->charger_regulators
|| desc
->num_charger_regulators
< 1) {
1382 dev_err(&pdev
->dev
, "charger_regulators undefined.\n");
1383 goto err_no_charger
;
1386 if (!desc
->psy_charger_stat
|| !desc
->psy_charger_stat
[0]) {
1387 dev_err(&pdev
->dev
, "No power supply defined.\n");
1389 goto err_no_charger_stat
;
1392 /* Counting index only */
1393 while (desc
->psy_charger_stat
[i
])
1396 cm
->charger_stat
= kzalloc(sizeof(struct power_supply
*) * (i
+ 1),
1398 if (!cm
->charger_stat
) {
1400 goto err_no_charger_stat
;
1403 for (i
= 0; desc
->psy_charger_stat
[i
]; i
++) {
1404 cm
->charger_stat
[i
] = power_supply_get_by_name(
1405 desc
->psy_charger_stat
[i
]);
1406 if (!cm
->charger_stat
[i
]) {
1407 dev_err(&pdev
->dev
, "Cannot find power supply "
1409 desc
->psy_charger_stat
[i
]);
1415 cm
->fuel_gauge
= power_supply_get_by_name(desc
->psy_fuel_gauge
);
1416 if (!cm
->fuel_gauge
) {
1417 dev_err(&pdev
->dev
, "Cannot find power supply \"%s\"\n",
1418 desc
->psy_fuel_gauge
);
1423 if (desc
->polling_interval_ms
== 0 ||
1424 msecs_to_jiffies(desc
->polling_interval_ms
) <= CM_JIFFIES_SMALL
) {
1425 dev_err(&pdev
->dev
, "polling_interval_ms is too small\n");
1430 if (!desc
->temperature_out_of_range
) {
1431 dev_err(&pdev
->dev
, "there is no temperature_out_of_range\n");
1436 if (!desc
->charging_max_duration_ms
||
1437 !desc
->discharging_max_duration_ms
) {
1438 dev_info(&pdev
->dev
, "Cannot limit charging duration "
1439 "checking mechanism to prevent overcharge/overheat "
1440 "and control discharging duration");
1441 desc
->charging_max_duration_ms
= 0;
1442 desc
->discharging_max_duration_ms
= 0;
1445 platform_set_drvdata(pdev
, cm
);
1447 memcpy(&cm
->charger_psy
, &psy_default
, sizeof(psy_default
));
1449 if (!desc
->psy_name
) {
1450 strncpy(cm
->psy_name_buf
, psy_default
.name
, PSY_NAME_MAX
);
1452 strncpy(cm
->psy_name_buf
, desc
->psy_name
, PSY_NAME_MAX
);
1454 cm
->charger_psy
.name
= cm
->psy_name_buf
;
1456 /* Allocate for psy properties because they may vary */
1457 cm
->charger_psy
.properties
= kzalloc(sizeof(enum power_supply_property
)
1458 * (ARRAY_SIZE(default_charger_props
) +
1459 NUM_CHARGER_PSY_OPTIONAL
),
1461 if (!cm
->charger_psy
.properties
) {
1462 dev_err(&pdev
->dev
, "Cannot allocate for psy properties.\n");
1466 memcpy(cm
->charger_psy
.properties
, default_charger_props
,
1467 sizeof(enum power_supply_property
) *
1468 ARRAY_SIZE(default_charger_props
));
1469 cm
->charger_psy
.num_properties
= psy_default
.num_properties
;
1471 /* Find which optional psy-properties are available */
1472 if (!cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
1473 POWER_SUPPLY_PROP_CHARGE_NOW
, &val
)) {
1474 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1475 POWER_SUPPLY_PROP_CHARGE_NOW
;
1476 cm
->charger_psy
.num_properties
++;
1478 if (!cm
->fuel_gauge
->get_property(cm
->fuel_gauge
,
1479 POWER_SUPPLY_PROP_CURRENT_NOW
,
1481 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1482 POWER_SUPPLY_PROP_CURRENT_NOW
;
1483 cm
->charger_psy
.num_properties
++;
1486 if (desc
->measure_battery_temp
) {
1487 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1488 POWER_SUPPLY_PROP_TEMP
;
1489 cm
->charger_psy
.num_properties
++;
1491 cm
->charger_psy
.properties
[cm
->charger_psy
.num_properties
] =
1492 POWER_SUPPLY_PROP_TEMP_AMBIENT
;
1493 cm
->charger_psy
.num_properties
++;
1496 INIT_DELAYED_WORK(&cm
->fullbatt_vchk_work
, fullbatt_vchk
);
1498 ret
= power_supply_register(NULL
, &cm
->charger_psy
);
1500 dev_err(&pdev
->dev
, "Cannot register charger-manager with"
1501 " name \"%s\".\n", cm
->charger_psy
.name
);
1505 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
1506 struct charger_regulator
*charger
1507 = &desc
->charger_regulators
[i
];
1511 charger
->consumer
= regulator_get(&pdev
->dev
,
1512 charger
->regulator_name
);
1513 if (charger
->consumer
== NULL
) {
1514 dev_err(&pdev
->dev
, "Cannot find charger(%s)n",
1515 charger
->regulator_name
);
1521 for (j
= 0 ; j
< charger
->num_cables
; j
++) {
1522 struct charger_cable
*cable
= &charger
->cables
[j
];
1524 ret
= charger_extcon_init(cm
, cable
);
1526 dev_err(&pdev
->dev
, "Cannot find charger(%s)n",
1527 charger
->regulator_name
);
1530 cable
->charger
= charger
;
1534 /* Create sysfs entry to control charger(regulator) */
1535 snprintf(buf
, 10, "charger.%d", i
);
1536 str
= kzalloc(sizeof(char) * (strlen(buf
) + 1), GFP_KERNEL
);
1538 for (i
--; i
>= 0; i
--) {
1539 charger
= &desc
->charger_regulators
[i
];
1540 kfree(charger
->attr_g
.name
);
1548 charger
->attrs
[0] = &charger
->attr_name
.attr
;
1549 charger
->attrs
[1] = &charger
->attr_state
.attr
;
1550 charger
->attrs
[2] = &charger
->attr_externally_control
.attr
;
1551 charger
->attrs
[3] = NULL
;
1552 charger
->attr_g
.name
= str
;
1553 charger
->attr_g
.attrs
= charger
->attrs
;
1555 sysfs_attr_init(&charger
->attr_name
.attr
);
1556 charger
->attr_name
.attr
.name
= "name";
1557 charger
->attr_name
.attr
.mode
= 0444;
1558 charger
->attr_name
.show
= charger_name_show
;
1560 sysfs_attr_init(&charger
->attr_state
.attr
);
1561 charger
->attr_state
.attr
.name
= "state";
1562 charger
->attr_state
.attr
.mode
= 0444;
1563 charger
->attr_state
.show
= charger_state_show
;
1565 sysfs_attr_init(&charger
->attr_externally_control
.attr
);
1566 charger
->attr_externally_control
.attr
.name
1567 = "externally_control";
1568 charger
->attr_externally_control
.attr
.mode
= 0644;
1569 charger
->attr_externally_control
.show
1570 = charger_externally_control_show
;
1571 charger
->attr_externally_control
.store
1572 = charger_externally_control_store
;
1574 if (!desc
->charger_regulators
[i
].externally_control
||
1575 !chargers_externally_control
) {
1576 chargers_externally_control
= 0;
1578 dev_info(&pdev
->dev
, "'%s' regulator's externally_control"
1579 "is %d\n", charger
->regulator_name
,
1580 charger
->externally_control
);
1582 ret
= sysfs_create_group(&cm
->charger_psy
.dev
->kobj
,
1585 dev_info(&pdev
->dev
, "Cannot create sysfs entry"
1586 "of %s regulator\n",
1587 charger
->regulator_name
);
1591 if (chargers_externally_control
) {
1592 dev_err(&pdev
->dev
, "Cannot register regulator because "
1593 "charger-manager must need at least "
1594 "one charger for charging battery\n");
1597 goto err_chg_enable
;
1600 ret
= try_charger_enable(cm
, true);
1602 dev_err(&pdev
->dev
, "Cannot enable charger regulators\n");
1603 goto err_chg_enable
;
1606 /* Add to the list */
1607 mutex_lock(&cm_list_mtx
);
1608 list_add(&cm
->entry
, &cm_list
);
1609 mutex_unlock(&cm_list_mtx
);
1612 * Charger-manager is capable of waking up the systme from sleep
1613 * when event is happend through cm_notify_event()
1615 device_init_wakeup(&pdev
->dev
, true);
1616 device_set_wakeup_capable(&pdev
->dev
, false);
1618 schedule_work(&setup_polling
);
1623 for (i
= 0; i
< desc
->num_charger_regulators
; i
++) {
1624 struct charger_regulator
*charger
;
1626 charger
= &desc
->charger_regulators
[i
];
1627 sysfs_remove_group(&cm
->charger_psy
.dev
->kobj
,
1629 kfree(charger
->attr_g
.name
);
1632 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
1633 struct charger_regulator
*charger
1634 = &desc
->charger_regulators
[i
];
1635 for (j
= 0 ; j
< charger
->num_cables
; j
++) {
1636 struct charger_cable
*cable
= &charger
->cables
[j
];
1637 extcon_unregister_interest(&cable
->extcon_dev
);
1641 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++)
1642 regulator_put(desc
->charger_regulators
[i
].consumer
);
1644 power_supply_unregister(&cm
->charger_psy
);
1646 kfree(cm
->charger_psy
.properties
);
1648 kfree(cm
->charger_stat
);
1649 err_no_charger_stat
:
1658 static int charger_manager_remove(struct platform_device
*pdev
)
1660 struct charger_manager
*cm
= platform_get_drvdata(pdev
);
1661 struct charger_desc
*desc
= cm
->desc
;
1665 /* Remove from the list */
1666 mutex_lock(&cm_list_mtx
);
1667 list_del(&cm
->entry
);
1668 mutex_unlock(&cm_list_mtx
);
1670 if (work_pending(&setup_polling
))
1671 cancel_work_sync(&setup_polling
);
1672 if (delayed_work_pending(&cm_monitor_work
))
1673 cancel_delayed_work_sync(&cm_monitor_work
);
1675 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++) {
1676 struct charger_regulator
*charger
1677 = &desc
->charger_regulators
[i
];
1678 for (j
= 0 ; j
< charger
->num_cables
; j
++) {
1679 struct charger_cable
*cable
= &charger
->cables
[j
];
1680 extcon_unregister_interest(&cable
->extcon_dev
);
1684 for (i
= 0 ; i
< desc
->num_charger_regulators
; i
++)
1685 regulator_put(desc
->charger_regulators
[i
].consumer
);
1687 power_supply_unregister(&cm
->charger_psy
);
1689 try_charger_enable(cm
, false);
1691 kfree(cm
->charger_psy
.properties
);
1692 kfree(cm
->charger_stat
);
1699 static const struct platform_device_id charger_manager_id
[] = {
1700 { "charger-manager", 0 },
1703 MODULE_DEVICE_TABLE(platform
, charger_manager_id
);
1705 static int cm_suspend_noirq(struct device
*dev
)
1709 if (device_may_wakeup(dev
)) {
1710 device_set_wakeup_capable(dev
, false);
1717 static int cm_suspend_prepare(struct device
*dev
)
1719 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1721 if (!cm_suspended
) {
1723 struct rtc_time tmp
;
1726 rtc_read_alarm(rtc_dev
, &rtc_wkalarm_save
);
1727 rtc_read_time(rtc_dev
, &tmp
);
1729 if (rtc_wkalarm_save
.enabled
) {
1730 rtc_tm_to_time(&rtc_wkalarm_save
.time
,
1731 &rtc_wkalarm_save_time
);
1732 rtc_tm_to_time(&tmp
, &now
);
1733 if (now
> rtc_wkalarm_save_time
)
1734 rtc_wkalarm_save_time
= 0;
1736 rtc_wkalarm_save_time
= 0;
1739 cm_suspended
= true;
1742 if (delayed_work_pending(&cm
->fullbatt_vchk_work
))
1743 cancel_delayed_work(&cm
->fullbatt_vchk_work
);
1744 cm
->status_save_ext_pwr_inserted
= is_ext_pwr_online(cm
);
1745 cm
->status_save_batt
= is_batt_present(cm
);
1748 cm_suspend_duration_ms
= 0;
1749 cm_rtc_set
= cm_setup_timer();
1755 static void cm_suspend_complete(struct device
*dev
)
1757 struct charger_manager
*cm
= dev_get_drvdata(dev
);
1761 struct rtc_wkalrm tmp
;
1763 rtc_read_alarm(rtc_dev
, &tmp
);
1764 rtc_wkalarm_save
.pending
= tmp
.pending
;
1765 rtc_set_alarm(rtc_dev
, &rtc_wkalarm_save
);
1767 cm_suspended
= false;
1771 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1772 if (cm
->fullbatt_vchk_jiffies_at
) {
1773 unsigned long delay
= 0;
1774 unsigned long now
= jiffies
+ CM_JIFFIES_SMALL
;
1776 if (time_after_eq(now
, cm
->fullbatt_vchk_jiffies_at
)) {
1777 delay
= (unsigned long)((long)now
1778 - (long)(cm
->fullbatt_vchk_jiffies_at
));
1779 delay
= jiffies_to_msecs(delay
);
1785 * Account for cm_suspend_duration_ms if
1786 * assume_timer_stops_in_suspend is active
1788 if (g_desc
&& g_desc
->assume_timer_stops_in_suspend
) {
1789 if (delay
> cm_suspend_duration_ms
)
1790 delay
-= cm_suspend_duration_ms
;
1795 queue_delayed_work(cm_wq
, &cm
->fullbatt_vchk_work
,
1796 msecs_to_jiffies(delay
));
1798 device_set_wakeup_capable(cm
->dev
, false);
1799 uevent_notify(cm
, NULL
);
1802 static const struct dev_pm_ops charger_manager_pm
= {
1803 .prepare
= cm_suspend_prepare
,
1804 .suspend_noirq
= cm_suspend_noirq
,
1805 .complete
= cm_suspend_complete
,
1808 static struct platform_driver charger_manager_driver
= {
1810 .name
= "charger-manager",
1811 .owner
= THIS_MODULE
,
1812 .pm
= &charger_manager_pm
,
1814 .probe
= charger_manager_probe
,
1815 .remove
= charger_manager_remove
,
1816 .id_table
= charger_manager_id
,
1819 static int __init
charger_manager_init(void)
1821 cm_wq
= create_freezable_workqueue("charger_manager");
1822 INIT_DELAYED_WORK(&cm_monitor_work
, cm_monitor_poller
);
1824 return platform_driver_register(&charger_manager_driver
);
1826 late_initcall(charger_manager_init
);
1828 static void __exit
charger_manager_cleanup(void)
1830 destroy_workqueue(cm_wq
);
1833 platform_driver_unregister(&charger_manager_driver
);
1835 module_exit(charger_manager_cleanup
);
1838 * find_power_supply - find the associated power_supply of charger
1839 * @cm: the Charger Manager representing the battery
1840 * @psy: pointer to instance of charger's power_supply
1842 static bool find_power_supply(struct charger_manager
*cm
,
1843 struct power_supply
*psy
)
1848 for (i
= 0; cm
->charger_stat
[i
]; i
++) {
1849 if (psy
== cm
->charger_stat
[i
]) {
1859 * cm_notify_event - charger driver notify Charger Manager of charger event
1860 * @psy: pointer to instance of charger's power_supply
1861 * @type: type of charger event
1862 * @msg: optional message passed to uevent_notify fuction
1864 void cm_notify_event(struct power_supply
*psy
, enum cm_event_types type
,
1867 struct charger_manager
*cm
;
1868 bool found_power_supply
= false;
1873 mutex_lock(&cm_list_mtx
);
1874 list_for_each_entry(cm
, &cm_list
, entry
) {
1875 found_power_supply
= find_power_supply(cm
, psy
);
1876 if (found_power_supply
)
1879 mutex_unlock(&cm_list_mtx
);
1881 if (!found_power_supply
)
1885 case CM_EVENT_BATT_FULL
:
1886 fullbatt_handler(cm
);
1888 case CM_EVENT_BATT_OUT
:
1889 battout_handler(cm
);
1891 case CM_EVENT_BATT_IN
:
1892 case CM_EVENT_EXT_PWR_IN_OUT
... CM_EVENT_CHG_START_STOP
:
1893 misc_event_handler(cm
, type
);
1895 case CM_EVENT_UNKNOWN
:
1896 case CM_EVENT_OTHERS
:
1897 uevent_notify(cm
, msg
? msg
: default_event_names
[type
]);
1900 dev_err(cm
->dev
, "%s type not specified.\n", __func__
);
1904 EXPORT_SYMBOL_GPL(cm_notify_event
);
1906 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1907 MODULE_DESCRIPTION("Charger Manager");
1908 MODULE_LICENSE("GPL");