16 #include <linux/module.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
27 static const char *
const default_event_names[] = {
42 #define CM_JIFFIES_SMALL (2)
45 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
51 #define CM_RTC_SMALL (2)
53 #define UEVENT_BUF_SIZE 32
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;
72 static unsigned long polling_jiffy =
ULONG_MAX;
73 static unsigned long next_polling;
90 switch (cm->
desc->battery_present) {
99 if (ret == 0 &&
val.intval)
107 if (ret == 0 &&
val.intval) {
137 if (ret == 0 &&
val.intval) {
178 bool charging =
false;
182 if (!is_batt_present(cm))
198 dev_warn(cm->
dev,
"Cannot read ONLINE value from %s.\n",
199 cm->
desc->psy_charger_stat[i]);
213 dev_warn(cm->
dev,
"Cannot read STATUS value from %s.\n",
214 cm->
desc->psy_charger_stat[i]);
242 if (!is_batt_present(cm)) {
259 ret = get_batt_uV(cm, &uV);
279 return val.intval ?
true :
false;
288 switch (cm->
desc->polling_mode) {
294 return is_ext_pwr_online(cm);
296 return is_charging(cm);
299 cm->
desc->polling_mode);
342 "Cannot enable %s regulator\n",
361 "Cannot disable %s regulator\n",
376 "Disable regulator(%s) forcibly.\n",
401 err = try_charger_enable(cm,
false);
405 return try_charger_enable(cm,
true);
427 if (env_str_save[0] == 0) {
442 if (!env_str_save[0])
478 int batt_uV,
err, diff;
486 err = get_batt_uV(cm, &batt_uV);
488 dev_err(cm->
dev,
"%s: get_batt_uV error(%d).\n", __func__, err);
495 dev_info(cm->
dev,
"VBATT dropped %duV after full-batt.\n", diff);
498 try_charger_restart(cm);
499 uevent_notify(cm,
"Recharging");
528 dev_info(cm->
dev,
"Charging duration exceed %lldms",
530 uevent_notify(cm,
"Discharging");
531 try_charger_enable(cm,
false);
538 is_ext_pwr_online(cm)) {
539 dev_info(cm->
dev,
"DisCharging duration exceed %lldms",
541 uevent_notify(cm,
"Recharing");
542 try_charger_enable(cm,
true);
562 dev_dbg(cm->
dev,
"monitoring (%2.2d.%3.3dC)\n",
575 if (!try_charger_enable(cm,
false)) {
577 uevent_notify(cm,
"OVERHEAT");
579 uevent_notify(cm,
"COLD");
588 "Charging/Discharging duration is out of range");
604 dev_info(cm->
dev,
"EVENT_HANDLE: Battery Fully Charged.\n");
607 try_charger_enable(cm,
false);
612 if (is_ext_pwr_online(cm)) {
613 if (!try_charger_enable(cm,
true))
614 uevent_notify(cm,
"CHARGING");
627 static bool cm_monitor(
void)
648 static void _setup_polling(
struct work_struct *work)
652 bool keep_polling =
false;
653 unsigned long _next_polling;
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;
675 WARN(cm_wq ==
NULL,
"charger-manager: workqueue not initialized"
676 ". try it later. %s\n", __func__);
678 _next_polling =
jiffies + polling_jiffy;
683 next_polling =
jiffies + polling_jiffy;
699 static void cm_monitor_poller(
struct work_struct *work)
728 dev_info(cm->
dev,
"EVENT_HANDLE: Battery Fully Charged.\n");
741 if (!is_batt_present(cm)) {
742 dev_emerg(cm->
dev,
"Battery Pulled Out!\n");
745 uevent_notify(cm,
"Battery Reinserted?");
761 is_polling_required(cm) && cm->
desc->polling_interval_ms)
763 uevent_notify(cm, default_event_names[type]);
766 static int charger_get_property(
struct power_supply *psy,
780 else if (is_ext_pwr_online(cm))
794 if (is_batt_present(cm))
800 ret = get_batt_uV(cm, &val->
intval);
828 if (!is_batt_present(cm)) {
854 ret = get_batt_uV(cm, &uV);
869 if (is_ext_pwr_online(cm))
875 if (is_full_charged(cm))
882 if (is_charging(cm)) {
904 #define NUM_CHARGER_PSY_OPTIONAL (4)
926 .properties = default_charger_props,
927 .num_properties =
ARRAY_SIZE(default_charger_props),
928 .get_property = charger_get_property,
941 static bool cm_setup_timer(
void)
950 unsigned int fbchk_ms = 0;
968 if (cm->
desc->polling_interval_ms == 0)
975 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
976 pr_info(
"Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
979 unsigned long time, now;
997 if (rtc_wkalarm_save.enabled &&
998 rtc_wkalarm_save_time &&
999 rtc_wkalarm_save_time < time) {
1003 time = rtc_wkalarm_save_time;
1009 pr_info(
"Waking up after %lu secs.\n",
1014 cm_suspend_duration_ms += wakeup_ms;
1026 unsigned long jiffy_now =
jiffies;
1062 _cm_fbchk_in_suspend(cm);
1072 cm_rtc_set = cm_setup_timer();
1075 if (rtc_wkalarm_save.enabled) {
1082 if (rtc_wkalarm_save_time &&
1105 pr_err(
"The callback rtc_only_wakeup is not given.\n");
1111 if (IS_ERR_OR_NULL(rtc_dev)) {
1116 pr_warn(
"No wakeup timer is given for charger manager."
1117 "In-suspend monitoring won't work.\n");
1131 static void charger_extcon_work(
struct work_struct *work)
1141 pr_err(
"Cannot set current limit of %s (%s)\n",
1146 pr_info(
"Set current limit of %s : %duA ~ %duA\n",
1147 cable->
charger->regulator_name,
1151 try_charger_enable(cable->
cm, cable->
attached);
1163 unsigned long event,
void *
ptr)
1178 if (cable->
attached && is_polling_required(cable->
cm)) {
1211 cable->
nb.notifier_call = charger_extcon_notifier;
1215 pr_info(
"Cannot register extcon_dev for %s(cable: %s).\n",
1244 return sprintf(buf,
"%s\n", state ?
"enabled" :
"disabled");
1247 static ssize_t charger_externally_control_show(
struct device *dev,
1256 static ssize_t charger_externally_control_store(
struct device *dev,
1267 int externally_control;
1268 int chargers_externally_control = 1;
1270 ret =
sscanf(buf,
"%d", &externally_control);
1276 if (!externally_control) {
1288 chargers_externally_control = 0;
1293 if (!chargers_externally_control) {
1295 try_charger_enable(charger->
cm,
false);
1297 try_charger_enable(charger->
cm,
true);
1303 "'%s' regulator should be controlled "
1304 "in charger-manager because charger-manager "
1305 "must need at least one charger for charging\n",
1318 int chargers_externally_control = 1;
1321 if (g_desc && !rtc_dev && g_desc->
rtc_name) {
1323 if (IS_ERR_OR_NULL(rtc_dev)) {
1333 dev_err(&pdev->
dev,
"No platform data (desc) found.\n");
1340 dev_err(&pdev->
dev,
"Cannot allocate memory.\n");
1349 dev_err(&pdev->
dev,
"Cannot allocate memory.\n");
1351 goto err_alloc_desc;
1361 dev_info(&pdev->
dev,
"Ignoring full-battery voltage threshold"
1362 " as it is not supplied.");
1365 dev_info(&pdev->
dev,
"Disabling full-battery voltage drop "
1366 "checking mechanism as it is not supplied.");
1371 dev_info(&pdev->
dev,
"Ignoring full-battery soc(state of"
1372 " charge) threshold as it is not"
1376 dev_info(&pdev->
dev,
"Ignoring full-battery full capacity"
1377 " threshold as it is not supplied.");
1382 dev_err(&pdev->
dev,
"charger_regulators undefined.\n");
1383 goto err_no_charger;
1387 dev_err(&pdev->
dev,
"No power supply defined.\n");
1389 goto err_no_charger_stat;
1400 goto err_no_charger_stat;
1407 dev_err(&pdev->
dev,
"Cannot find power supply "
1417 dev_err(&pdev->
dev,
"Cannot find power supply \"%s\"\n",
1425 dev_err(&pdev->
dev,
"polling_interval_ms is too small\n");
1431 dev_err(&pdev->
dev,
"there is no temperature_out_of_range\n");
1438 dev_info(&pdev->
dev,
"Cannot limit charging duration "
1439 "checking mechanism to prevent overcharge/overheat "
1440 "and control discharging duration");
1445 platform_set_drvdata(pdev, cm);
1462 dev_err(&pdev->
dev,
"Cannot allocate for psy properties.\n");
1500 dev_err(&pdev->
dev,
"Cannot register charger-manager with"
1514 dev_err(&pdev->
dev,
"Cannot find charger(%s)n",
1521 for (j = 0 ; j < charger->
num_cables ; j++) {
1524 ret = charger_extcon_init(cm, cable);
1526 dev_err(&pdev->
dev,
"Cannot find charger(%s)n",
1535 snprintf(buf, 10,
"charger.%d", i);
1538 for (i--; i >= 0; i--) {
1558 charger->
attr_name.show = charger_name_show;
1563 charger->
attr_state.show = charger_state_show;
1567 =
"externally_control";
1570 = charger_externally_control_show;
1572 = charger_externally_control_store;
1575 !chargers_externally_control) {
1576 chargers_externally_control = 0;
1578 dev_info(&pdev->
dev,
"'%s' regulator's externally_control"
1586 "of %s regulator\n",
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;
1608 list_add(&cm->
entry, &cm_list);
1635 for (j = 0 ; j < charger->
num_cables ; j++) {
1649 err_no_charger_stat:
1678 for (j = 0 ; j < charger->
num_cables ; j++) {
1689 try_charger_enable(cm,
false);
1700 {
"charger-manager", 0 },
1705 static int cm_suspend_noirq(
struct device *dev)
1709 if (device_may_wakeup(dev)) {
1717 static int cm_suspend_prepare(
struct device *dev)
1721 if (!cm_suspended) {
1729 if (rtc_wkalarm_save.enabled) {
1731 &rtc_wkalarm_save_time);
1733 if (now > rtc_wkalarm_save_time)
1734 rtc_wkalarm_save_time = 0;
1736 rtc_wkalarm_save_time = 0;
1739 cm_suspended =
true;
1748 cm_suspend_duration_ms = 0;
1749 cm_rtc_set = cm_setup_timer();
1755 static void cm_suspend_complete(
struct device *dev)
1764 rtc_wkalarm_save.pending =
tmp.pending;
1767 cm_suspended =
false;
1773 unsigned long delay = 0;
1777 delay = (
unsigned long)((
long)now
1789 if (delay > cm_suspend_duration_ms)
1790 delay -= cm_suspend_duration_ms;
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,
1810 .name =
"charger-manager",
1812 .pm = &charger_manager_pm,
1814 .probe = charger_manager_probe,
1816 .id_table = charger_manager_id,
1819 static int __init charger_manager_init(
void)
1828 static void __exit charger_manager_cleanup(
void)
1868 bool found_power_supply =
false;
1875 found_power_supply = find_power_supply(cm, psy);
1876 if (found_power_supply)
1881 if (!found_power_supply)
1886 fullbatt_handler(cm);
1889 battout_handler(cm);
1893 misc_event_handler(cm, type);
1897 uevent_notify(cm, msg ? msg : default_event_names[type]);
1900 dev_err(cm->
dev,
"%s type not specified.\n", __func__);