Skip to content

Commit dfeccb1

Browse files
chanwoochoiAnton Vorontsov
authored andcommitted
charger-manager: Provide cm_notify_event function for in-kernel use
By using cm_notify_event function, charger driver can report several charger events (e.g. battery full and external power in/out, etc) to Charger-Manager. Charger-Manager can properly and immediately control chargers by the reported event. Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com> Signed-off-by: Donggeun Kim <dg77.kim@samsung.com> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com> Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
1 parent d829dc7 commit dfeccb1

3 files changed

Lines changed: 195 additions & 9 deletions

File tree

‎Documentation/power/charger-manager.txt‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Charger Manager supports the following:
5050
restarts charging. This check is also performed while suspended by
5151
setting wakeup time accordingly and using suspend_again.
5252

53+
* Support for uevent-notify
54+
With the charger-related events, the device sends
55+
notification to users with UEVENT.
56+
5357
2. Global Charger-Manager Data related with suspend_again
5458
========================================================
5559
In order to setup Charger Manager with suspend-again feature
@@ -174,7 +178,17 @@ bool measure_battery_temp;
174178
the value of measure_battery_temp.
175179
};
176180

177-
5. Other Considerations
181+
5. Notify Charger-Manager of charger events: cm_notify_event()
182+
=========================================================
183+
If there is an charger event is required to notify
184+
Charger Manager, a charger device driver that triggers the event can call
185+
cm_notify_event(psy, type, msg) to notify the corresponding Charger Manager.
186+
In the function, psy is the charger driver's power_supply pointer, which is
187+
associated with Charger-Manager. The parameter "type"
188+
is the same as irq's type (enum cm_event_types). The event message "msg" is
189+
optional and is effective only if the event type is "UNDESCRIBED" or "OTHERS".
190+
191+
6. Other Considerations
178192
=======================
179193

180194
At the charger/battery-related events such as battery-pulled-out,

‎drivers/power/charger-manager.c‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
#include <linux/power/charger-manager.h>
2424
#include <linux/regulator/consumer.h>
2525

26+
static const char * const default_event_names[] = {
27+
[CM_EVENT_UNKNOWN] = "Unknown",
28+
[CM_EVENT_BATT_FULL] = "Battery Full",
29+
[CM_EVENT_BATT_IN] = "Battery Inserted",
30+
[CM_EVENT_BATT_OUT] = "Battery Pulled Out",
31+
[CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
32+
[CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
33+
[CM_EVENT_OTHERS] = "Other battery events"
34+
};
35+
2636
/*
2737
* Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
2838
* delayed works so that we can run delayed works with CM_JIFFIES_SMALL
@@ -525,6 +535,69 @@ static void cm_monitor_poller(struct work_struct *work)
525535
schedule_work(&setup_polling);
526536
}
527537

538+
/**
539+
* fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
540+
* @cm: the Charger Manager representing the battery.
541+
*/
542+
static void fullbatt_handler(struct charger_manager *cm)
543+
{
544+
struct charger_desc *desc = cm->desc;
545+
546+
if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
547+
goto out;
548+
549+
if (cm_suspended)
550+
device_set_wakeup_capable(cm->dev, true);
551+
552+
if (delayed_work_pending(&cm->fullbatt_vchk_work))
553+
cancel_delayed_work(&cm->fullbatt_vchk_work);
554+
queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
555+
msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
556+
cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
557+
desc->fullbatt_vchkdrop_ms);
558+
559+
if (cm->fullbatt_vchk_jiffies_at == 0)
560+
cm->fullbatt_vchk_jiffies_at = 1;
561+
562+
out:
563+
dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
564+
uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
565+
}
566+
567+
/**
568+
* battout_handler - Event handler for CM_EVENT_BATT_OUT
569+
* @cm: the Charger Manager representing the battery.
570+
*/
571+
static void battout_handler(struct charger_manager *cm)
572+
{
573+
if (cm_suspended)
574+
device_set_wakeup_capable(cm->dev, true);
575+
576+
if (!is_batt_present(cm)) {
577+
dev_emerg(cm->dev, "Battery Pulled Out!\n");
578+
uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
579+
} else {
580+
uevent_notify(cm, "Battery Reinserted?");
581+
}
582+
}
583+
584+
/**
585+
* misc_event_handler - Handler for other evnets
586+
* @cm: the Charger Manager representing the battery.
587+
* @type: the Charger Manager representing the battery.
588+
*/
589+
static void misc_event_handler(struct charger_manager *cm,
590+
enum cm_event_types type)
591+
{
592+
if (cm_suspended)
593+
device_set_wakeup_capable(cm->dev, true);
594+
595+
if (!delayed_work_pending(&cm_monitor_work) &&
596+
is_polling_required(cm) && cm->desc->polling_interval_ms)
597+
schedule_work(&setup_polling);
598+
uevent_notify(cm, default_event_names[type]);
599+
}
600+
528601
static int charger_get_property(struct power_supply *psy,
529602
enum power_supply_property psp,
530603
union power_supply_propval *val)
@@ -1112,6 +1185,13 @@ static int charger_manager_probe(struct platform_device *pdev)
11121185
list_add(&cm->entry, &cm_list);
11131186
mutex_unlock(&cm_list_mtx);
11141187

1188+
/*
1189+
* Charger-manager is capable of waking up the systme from sleep
1190+
* when event is happend through cm_notify_event()
1191+
*/
1192+
device_init_wakeup(&pdev->dev, true);
1193+
device_set_wakeup_capable(&pdev->dev, false);
1194+
11151195
schedule_work(&setup_polling);
11161196

11171197
return 0;
@@ -1169,6 +1249,18 @@ static const struct platform_device_id charger_manager_id[] = {
11691249
};
11701250
MODULE_DEVICE_TABLE(platform, charger_manager_id);
11711251

1252+
static int cm_suspend_noirq(struct device *dev)
1253+
{
1254+
int ret = 0;
1255+
1256+
if (device_may_wakeup(dev)) {
1257+
device_set_wakeup_capable(dev, false);
1258+
ret = -EAGAIN;
1259+
}
1260+
1261+
return ret;
1262+
}
1263+
11721264
static int cm_suspend_prepare(struct device *dev)
11731265
{
11741266
struct charger_manager *cm = dev_get_drvdata(dev);
@@ -1250,11 +1342,13 @@ static void cm_suspend_complete(struct device *dev)
12501342
queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
12511343
msecs_to_jiffies(delay));
12521344
}
1345+
device_set_wakeup_capable(cm->dev, false);
12531346
uevent_notify(cm, NULL);
12541347
}
12551348

12561349
static const struct dev_pm_ops charger_manager_pm = {
12571350
.prepare = cm_suspend_prepare,
1351+
.suspend_noirq = cm_suspend_noirq,
12581352
.complete = cm_suspend_complete,
12591353
};
12601354

@@ -1287,6 +1381,75 @@ static void __exit charger_manager_cleanup(void)
12871381
}
12881382
module_exit(charger_manager_cleanup);
12891383

1384+
/**
1385+
* find_power_supply - find the associated power_supply of charger
1386+
* @cm: the Charger Manager representing the battery
1387+
* @psy: pointer to instance of charger's power_supply
1388+
*/
1389+
static bool find_power_supply(struct charger_manager *cm,
1390+
struct power_supply *psy)
1391+
{
1392+
int i;
1393+
bool found = false;
1394+
1395+
for (i = 0; cm->charger_stat[i]; i++) {
1396+
if (psy == cm->charger_stat[i]) {
1397+
found = true;
1398+
break;
1399+
}
1400+
}
1401+
1402+
return found;
1403+
}
1404+
1405+
/**
1406+
* cm_notify_event - charger driver notify Charger Manager of charger event
1407+
* @psy: pointer to instance of charger's power_supply
1408+
* @type: type of charger event
1409+
* @msg: optional message passed to uevent_notify fuction
1410+
*/
1411+
void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1412+
char *msg)
1413+
{
1414+
struct charger_manager *cm;
1415+
bool found_power_supply = false;
1416+
1417+
if (psy == NULL)
1418+
return;
1419+
1420+
mutex_lock(&cm_list_mtx);
1421+
list_for_each_entry(cm, &cm_list, entry) {
1422+
found_power_supply = find_power_supply(cm, psy);
1423+
if (found_power_supply)
1424+
break;
1425+
}
1426+
mutex_unlock(&cm_list_mtx);
1427+
1428+
if (!found_power_supply)
1429+
return;
1430+
1431+
switch (type) {
1432+
case CM_EVENT_BATT_FULL:
1433+
fullbatt_handler(cm);
1434+
break;
1435+
case CM_EVENT_BATT_OUT:
1436+
battout_handler(cm);
1437+
break;
1438+
case CM_EVENT_BATT_IN:
1439+
case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1440+
misc_event_handler(cm, type);
1441+
break;
1442+
case CM_EVENT_UNKNOWN:
1443+
case CM_EVENT_OTHERS:
1444+
uevent_notify(cm, msg ? msg : default_event_names[type]);
1445+
break;
1446+
default:
1447+
dev_err(cm->dev, "%s type not specified.\n", __func__);
1448+
break;
1449+
}
1450+
}
1451+
EXPORT_SYMBOL_GPL(cm_notify_event);
1452+
12901453
MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
12911454
MODULE_DESCRIPTION("Charger Manager");
12921455
MODULE_LICENSE("GPL");

‎include/linux/power/charger-manager.h‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ enum polling_modes {
3131
CM_POLL_CHARGING_ONLY,
3232
};
3333

34+
enum cm_event_types {
35+
CM_EVENT_UNKNOWN = 0,
36+
CM_EVENT_BATT_FULL,
37+
CM_EVENT_BATT_IN,
38+
CM_EVENT_BATT_OUT,
39+
CM_EVENT_EXT_PWR_IN_OUT,
40+
CM_EVENT_CHG_START_STOP,
41+
CM_EVENT_OTHERS,
42+
};
43+
3444
/**
3545
* struct charger_global_desc
3646
* @rtc_name: the name of RTC used to wake up the system from suspend.
@@ -159,14 +169,13 @@ struct charger_manager {
159169
#ifdef CONFIG_CHARGER_MANAGER
160170
extern int setup_charger_manager(struct charger_global_desc *gd);
161171
extern bool cm_suspend_again(void);
172+
extern void cm_notify_event(struct power_supply *psy,
173+
enum cm_event_types type, char *msg);
162174
#else
163-
static void __maybe_unused setup_charger_manager(struct charger_global_desc *gd)
164-
{ }
165-
166-
static bool __maybe_unused cm_suspend_again(void)
167-
{
168-
return false;
169-
}
175+
static inline int setup_charger_manager(struct charger_global_desc *gd)
176+
{ return 0; }
177+
static inline bool cm_suspend_again(void) { return false; }
178+
static inline void cm_notify_event(struct power_supply *psy,
179+
enum cm_event_types type, char *msg) { }
170180
#endif
171-
172181
#endif /* _CHARGER_MANAGER_H */

0 commit comments

Comments
 (0)