18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2011 Samsung Electronics Co., Ltd. 48c2ecf20Sopenharmony_ci * MyungJoo Ham <myungjoo.ham@samsung.com> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * This driver enables to monitor battery health and control charger 78c2ecf20Sopenharmony_ci * during suspend-to-mem. 88c2ecf20Sopenharmony_ci * Charger manager depends on other devices. Register this later than 98c2ecf20Sopenharmony_ci * the depending devices. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci**/ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/io.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/irq.h> 188c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 198c2ecf20Sopenharmony_ci#include <linux/rtc.h> 208c2ecf20Sopenharmony_ci#include <linux/slab.h> 218c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 228c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 238c2ecf20Sopenharmony_ci#include <linux/power/charger-manager.h> 248c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h> 258c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 268c2ecf20Sopenharmony_ci#include <linux/of.h> 278c2ecf20Sopenharmony_ci#include <linux/thermal.h> 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic struct { 308c2ecf20Sopenharmony_ci const char *name; 318c2ecf20Sopenharmony_ci u64 extcon_type; 328c2ecf20Sopenharmony_ci} extcon_mapping[] = { 338c2ecf20Sopenharmony_ci /* Current textual representations */ 348c2ecf20Sopenharmony_ci { "USB", EXTCON_USB }, 358c2ecf20Sopenharmony_ci { "USB-HOST", EXTCON_USB_HOST }, 368c2ecf20Sopenharmony_ci { "SDP", EXTCON_CHG_USB_SDP }, 378c2ecf20Sopenharmony_ci { "DCP", EXTCON_CHG_USB_DCP }, 388c2ecf20Sopenharmony_ci { "CDP", EXTCON_CHG_USB_CDP }, 398c2ecf20Sopenharmony_ci { "ACA", EXTCON_CHG_USB_ACA }, 408c2ecf20Sopenharmony_ci { "FAST-CHARGER", EXTCON_CHG_USB_FAST }, 418c2ecf20Sopenharmony_ci { "SLOW-CHARGER", EXTCON_CHG_USB_SLOW }, 428c2ecf20Sopenharmony_ci { "WPT", EXTCON_CHG_WPT }, 438c2ecf20Sopenharmony_ci { "PD", EXTCON_CHG_USB_PD }, 448c2ecf20Sopenharmony_ci { "DOCK", EXTCON_DOCK }, 458c2ecf20Sopenharmony_ci { "JIG", EXTCON_JIG }, 468c2ecf20Sopenharmony_ci { "MECHANICAL", EXTCON_MECHANICAL }, 478c2ecf20Sopenharmony_ci /* Deprecated textual representations */ 488c2ecf20Sopenharmony_ci { "TA", EXTCON_CHG_USB_SDP }, 498c2ecf20Sopenharmony_ci { "CHARGE-DOWNSTREAM", EXTCON_CHG_USB_CDP }, 508c2ecf20Sopenharmony_ci}; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/* 538c2ecf20Sopenharmony_ci * Default temperature threshold for charging. 548c2ecf20Sopenharmony_ci * Every temperature units are in tenth of centigrade. 558c2ecf20Sopenharmony_ci */ 568c2ecf20Sopenharmony_ci#define CM_DEFAULT_RECHARGE_TEMP_DIFF 50 578c2ecf20Sopenharmony_ci#define CM_DEFAULT_CHARGE_TEMP_MAX 500 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci/* 608c2ecf20Sopenharmony_ci * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for 618c2ecf20Sopenharmony_ci * delayed works so that we can run delayed works with CM_JIFFIES_SMALL 628c2ecf20Sopenharmony_ci * without any delays. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ci#define CM_JIFFIES_SMALL (2) 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* If y is valid (> 0) and smaller than x, do x = y */ 678c2ecf20Sopenharmony_ci#define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x)) 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* 708c2ecf20Sopenharmony_ci * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking 718c2ecf20Sopenharmony_ci * rtc alarm. It should be 2 or larger 728c2ecf20Sopenharmony_ci */ 738c2ecf20Sopenharmony_ci#define CM_RTC_SMALL (2) 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic LIST_HEAD(cm_list); 768c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(cm_list_mtx); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/* About in-suspend (suspend-again) monitoring */ 798c2ecf20Sopenharmony_cistatic struct alarm *cm_timer; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic bool cm_suspended; 828c2ecf20Sopenharmony_cistatic bool cm_timer_set; 838c2ecf20Sopenharmony_cistatic unsigned long cm_suspend_duration_ms; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/* About normal (not suspended) monitoring */ 868c2ecf20Sopenharmony_cistatic unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */ 878c2ecf20Sopenharmony_cistatic unsigned long next_polling; /* Next appointed polling time */ 888c2ecf20Sopenharmony_cistatic struct workqueue_struct *cm_wq; /* init at driver add */ 898c2ecf20Sopenharmony_cistatic struct delayed_work cm_monitor_work; /* init at driver add */ 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/** 928c2ecf20Sopenharmony_ci * is_batt_present - See if the battery presents in place. 938c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 948c2ecf20Sopenharmony_ci */ 958c2ecf20Sopenharmony_cistatic bool is_batt_present(struct charger_manager *cm) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci union power_supply_propval val; 988c2ecf20Sopenharmony_ci struct power_supply *psy; 998c2ecf20Sopenharmony_ci bool present = false; 1008c2ecf20Sopenharmony_ci int i, ret; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci switch (cm->desc->battery_present) { 1038c2ecf20Sopenharmony_ci case CM_BATTERY_PRESENT: 1048c2ecf20Sopenharmony_ci present = true; 1058c2ecf20Sopenharmony_ci break; 1068c2ecf20Sopenharmony_ci case CM_NO_BATTERY: 1078c2ecf20Sopenharmony_ci break; 1088c2ecf20Sopenharmony_ci case CM_FUEL_GAUGE: 1098c2ecf20Sopenharmony_ci psy = power_supply_get_by_name(cm->desc->psy_fuel_gauge); 1108c2ecf20Sopenharmony_ci if (!psy) 1118c2ecf20Sopenharmony_ci break; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT, 1148c2ecf20Sopenharmony_ci &val); 1158c2ecf20Sopenharmony_ci if (ret == 0 && val.intval) 1168c2ecf20Sopenharmony_ci present = true; 1178c2ecf20Sopenharmony_ci power_supply_put(psy); 1188c2ecf20Sopenharmony_ci break; 1198c2ecf20Sopenharmony_ci case CM_CHARGER_STAT: 1208c2ecf20Sopenharmony_ci for (i = 0; cm->desc->psy_charger_stat[i]; i++) { 1218c2ecf20Sopenharmony_ci psy = power_supply_get_by_name( 1228c2ecf20Sopenharmony_ci cm->desc->psy_charger_stat[i]); 1238c2ecf20Sopenharmony_ci if (!psy) { 1248c2ecf20Sopenharmony_ci dev_err(cm->dev, "Cannot find power supply \"%s\"\n", 1258c2ecf20Sopenharmony_ci cm->desc->psy_charger_stat[i]); 1268c2ecf20Sopenharmony_ci continue; 1278c2ecf20Sopenharmony_ci } 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci ret = power_supply_get_property(psy, 1308c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_PRESENT, &val); 1318c2ecf20Sopenharmony_ci power_supply_put(psy); 1328c2ecf20Sopenharmony_ci if (ret == 0 && val.intval) { 1338c2ecf20Sopenharmony_ci present = true; 1348c2ecf20Sopenharmony_ci break; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci break; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci return present; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci/** 1448c2ecf20Sopenharmony_ci * is_ext_pwr_online - See if an external power source is attached to charge 1458c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 1468c2ecf20Sopenharmony_ci * 1478c2ecf20Sopenharmony_ci * Returns true if at least one of the chargers of the battery has an external 1488c2ecf20Sopenharmony_ci * power source attached to charge the battery regardless of whether it is 1498c2ecf20Sopenharmony_ci * actually charging or not. 1508c2ecf20Sopenharmony_ci */ 1518c2ecf20Sopenharmony_cistatic bool is_ext_pwr_online(struct charger_manager *cm) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci union power_supply_propval val; 1548c2ecf20Sopenharmony_ci struct power_supply *psy; 1558c2ecf20Sopenharmony_ci bool online = false; 1568c2ecf20Sopenharmony_ci int i, ret; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci /* If at least one of them has one, it's yes. */ 1598c2ecf20Sopenharmony_ci for (i = 0; cm->desc->psy_charger_stat[i]; i++) { 1608c2ecf20Sopenharmony_ci psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]); 1618c2ecf20Sopenharmony_ci if (!psy) { 1628c2ecf20Sopenharmony_ci dev_err(cm->dev, "Cannot find power supply \"%s\"\n", 1638c2ecf20Sopenharmony_ci cm->desc->psy_charger_stat[i]); 1648c2ecf20Sopenharmony_ci continue; 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, 1688c2ecf20Sopenharmony_ci &val); 1698c2ecf20Sopenharmony_ci power_supply_put(psy); 1708c2ecf20Sopenharmony_ci if (ret == 0 && val.intval) { 1718c2ecf20Sopenharmony_ci online = true; 1728c2ecf20Sopenharmony_ci break; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return online; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci/** 1808c2ecf20Sopenharmony_ci * get_batt_uV - Get the voltage level of the battery 1818c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 1828c2ecf20Sopenharmony_ci * @uV: the voltage level returned. 1838c2ecf20Sopenharmony_ci * 1848c2ecf20Sopenharmony_ci * Returns 0 if there is no error. 1858c2ecf20Sopenharmony_ci * Returns a negative value on error. 1868c2ecf20Sopenharmony_ci */ 1878c2ecf20Sopenharmony_cistatic int get_batt_uV(struct charger_manager *cm, int *uV) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci union power_supply_propval val; 1908c2ecf20Sopenharmony_ci struct power_supply *fuel_gauge; 1918c2ecf20Sopenharmony_ci int ret; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); 1948c2ecf20Sopenharmony_ci if (!fuel_gauge) 1958c2ecf20Sopenharmony_ci return -ENODEV; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci ret = power_supply_get_property(fuel_gauge, 1988c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); 1998c2ecf20Sopenharmony_ci power_supply_put(fuel_gauge); 2008c2ecf20Sopenharmony_ci if (ret) 2018c2ecf20Sopenharmony_ci return ret; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci *uV = val.intval; 2048c2ecf20Sopenharmony_ci return 0; 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci/** 2088c2ecf20Sopenharmony_ci * is_charging - Returns true if the battery is being charged. 2098c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 2108c2ecf20Sopenharmony_ci */ 2118c2ecf20Sopenharmony_cistatic bool is_charging(struct charger_manager *cm) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci int i, ret; 2148c2ecf20Sopenharmony_ci bool charging = false; 2158c2ecf20Sopenharmony_ci struct power_supply *psy; 2168c2ecf20Sopenharmony_ci union power_supply_propval val; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci /* If there is no battery, it cannot be charged */ 2198c2ecf20Sopenharmony_ci if (!is_batt_present(cm)) 2208c2ecf20Sopenharmony_ci return false; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* If at least one of the charger is charging, return yes */ 2238c2ecf20Sopenharmony_ci for (i = 0; cm->desc->psy_charger_stat[i]; i++) { 2248c2ecf20Sopenharmony_ci /* 1. The charger sholuld not be DISABLED */ 2258c2ecf20Sopenharmony_ci if (cm->emergency_stop) 2268c2ecf20Sopenharmony_ci continue; 2278c2ecf20Sopenharmony_ci if (!cm->charger_enabled) 2288c2ecf20Sopenharmony_ci continue; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]); 2318c2ecf20Sopenharmony_ci if (!psy) { 2328c2ecf20Sopenharmony_ci dev_err(cm->dev, "Cannot find power supply \"%s\"\n", 2338c2ecf20Sopenharmony_ci cm->desc->psy_charger_stat[i]); 2348c2ecf20Sopenharmony_ci continue; 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* 2. The charger should be online (ext-power) */ 2388c2ecf20Sopenharmony_ci ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, 2398c2ecf20Sopenharmony_ci &val); 2408c2ecf20Sopenharmony_ci if (ret) { 2418c2ecf20Sopenharmony_ci dev_warn(cm->dev, "Cannot read ONLINE value from %s\n", 2428c2ecf20Sopenharmony_ci cm->desc->psy_charger_stat[i]); 2438c2ecf20Sopenharmony_ci power_supply_put(psy); 2448c2ecf20Sopenharmony_ci continue; 2458c2ecf20Sopenharmony_ci } 2468c2ecf20Sopenharmony_ci if (val.intval == 0) { 2478c2ecf20Sopenharmony_ci power_supply_put(psy); 2488c2ecf20Sopenharmony_ci continue; 2498c2ecf20Sopenharmony_ci } 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci /* 2528c2ecf20Sopenharmony_ci * 3. The charger should not be FULL, DISCHARGING, 2538c2ecf20Sopenharmony_ci * or NOT_CHARGING. 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_ci ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS, 2568c2ecf20Sopenharmony_ci &val); 2578c2ecf20Sopenharmony_ci power_supply_put(psy); 2588c2ecf20Sopenharmony_ci if (ret) { 2598c2ecf20Sopenharmony_ci dev_warn(cm->dev, "Cannot read STATUS value from %s\n", 2608c2ecf20Sopenharmony_ci cm->desc->psy_charger_stat[i]); 2618c2ecf20Sopenharmony_ci continue; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci if (val.intval == POWER_SUPPLY_STATUS_FULL || 2648c2ecf20Sopenharmony_ci val.intval == POWER_SUPPLY_STATUS_DISCHARGING || 2658c2ecf20Sopenharmony_ci val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) 2668c2ecf20Sopenharmony_ci continue; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci /* Then, this is charging. */ 2698c2ecf20Sopenharmony_ci charging = true; 2708c2ecf20Sopenharmony_ci break; 2718c2ecf20Sopenharmony_ci } 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci return charging; 2748c2ecf20Sopenharmony_ci} 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci/** 2778c2ecf20Sopenharmony_ci * is_full_charged - Returns true if the battery is fully charged. 2788c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 2798c2ecf20Sopenharmony_ci */ 2808c2ecf20Sopenharmony_cistatic bool is_full_charged(struct charger_manager *cm) 2818c2ecf20Sopenharmony_ci{ 2828c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 2838c2ecf20Sopenharmony_ci union power_supply_propval val; 2848c2ecf20Sopenharmony_ci struct power_supply *fuel_gauge; 2858c2ecf20Sopenharmony_ci bool is_full = false; 2868c2ecf20Sopenharmony_ci int ret = 0; 2878c2ecf20Sopenharmony_ci int uV; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci /* If there is no battery, it cannot be charged */ 2908c2ecf20Sopenharmony_ci if (!is_batt_present(cm)) 2918c2ecf20Sopenharmony_ci return false; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); 2948c2ecf20Sopenharmony_ci if (!fuel_gauge) 2958c2ecf20Sopenharmony_ci return false; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci /* Full, if it's over the fullbatt voltage */ 2988c2ecf20Sopenharmony_ci if (desc->fullbatt_uV > 0) { 2998c2ecf20Sopenharmony_ci ret = get_batt_uV(cm, &uV); 3008c2ecf20Sopenharmony_ci if (!ret) { 3018c2ecf20Sopenharmony_ci /* Battery is already full, checks voltage drop. */ 3028c2ecf20Sopenharmony_ci if (cm->battery_status == POWER_SUPPLY_STATUS_FULL 3038c2ecf20Sopenharmony_ci && desc->fullbatt_vchkdrop_uV) 3048c2ecf20Sopenharmony_ci uV += desc->fullbatt_vchkdrop_uV; 3058c2ecf20Sopenharmony_ci if (uV >= desc->fullbatt_uV) 3068c2ecf20Sopenharmony_ci return true; 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci if (desc->fullbatt_full_capacity > 0) { 3118c2ecf20Sopenharmony_ci val.intval = 0; 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci /* Not full if capacity of fuel gauge isn't full */ 3148c2ecf20Sopenharmony_ci ret = power_supply_get_property(fuel_gauge, 3158c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_FULL, &val); 3168c2ecf20Sopenharmony_ci if (!ret && val.intval > desc->fullbatt_full_capacity) { 3178c2ecf20Sopenharmony_ci is_full = true; 3188c2ecf20Sopenharmony_ci goto out; 3198c2ecf20Sopenharmony_ci } 3208c2ecf20Sopenharmony_ci } 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci /* Full, if the capacity is more than fullbatt_soc */ 3238c2ecf20Sopenharmony_ci if (desc->fullbatt_soc > 0) { 3248c2ecf20Sopenharmony_ci val.intval = 0; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci ret = power_supply_get_property(fuel_gauge, 3278c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CAPACITY, &val); 3288c2ecf20Sopenharmony_ci if (!ret && val.intval >= desc->fullbatt_soc) { 3298c2ecf20Sopenharmony_ci is_full = true; 3308c2ecf20Sopenharmony_ci goto out; 3318c2ecf20Sopenharmony_ci } 3328c2ecf20Sopenharmony_ci } 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ciout: 3358c2ecf20Sopenharmony_ci power_supply_put(fuel_gauge); 3368c2ecf20Sopenharmony_ci return is_full; 3378c2ecf20Sopenharmony_ci} 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci/** 3408c2ecf20Sopenharmony_ci * is_polling_required - Return true if need to continue polling for this CM. 3418c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 3428c2ecf20Sopenharmony_ci */ 3438c2ecf20Sopenharmony_cistatic bool is_polling_required(struct charger_manager *cm) 3448c2ecf20Sopenharmony_ci{ 3458c2ecf20Sopenharmony_ci switch (cm->desc->polling_mode) { 3468c2ecf20Sopenharmony_ci case CM_POLL_DISABLE: 3478c2ecf20Sopenharmony_ci return false; 3488c2ecf20Sopenharmony_ci case CM_POLL_ALWAYS: 3498c2ecf20Sopenharmony_ci return true; 3508c2ecf20Sopenharmony_ci case CM_POLL_EXTERNAL_POWER_ONLY: 3518c2ecf20Sopenharmony_ci return is_ext_pwr_online(cm); 3528c2ecf20Sopenharmony_ci case CM_POLL_CHARGING_ONLY: 3538c2ecf20Sopenharmony_ci return is_charging(cm); 3548c2ecf20Sopenharmony_ci default: 3558c2ecf20Sopenharmony_ci dev_warn(cm->dev, "Incorrect polling_mode (%d)\n", 3568c2ecf20Sopenharmony_ci cm->desc->polling_mode); 3578c2ecf20Sopenharmony_ci } 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci return false; 3608c2ecf20Sopenharmony_ci} 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci/** 3638c2ecf20Sopenharmony_ci * try_charger_enable - Enable/Disable chargers altogether 3648c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 3658c2ecf20Sopenharmony_ci * @enable: true: enable / false: disable 3668c2ecf20Sopenharmony_ci * 3678c2ecf20Sopenharmony_ci * Note that Charger Manager keeps the charger enabled regardless whether 3688c2ecf20Sopenharmony_ci * the charger is charging or not (because battery is full or no external 3698c2ecf20Sopenharmony_ci * power source exists) except when CM needs to disable chargers forcibly 3708c2ecf20Sopenharmony_ci * because of emergency causes; when the battery is overheated or too cold. 3718c2ecf20Sopenharmony_ci */ 3728c2ecf20Sopenharmony_cistatic int try_charger_enable(struct charger_manager *cm, bool enable) 3738c2ecf20Sopenharmony_ci{ 3748c2ecf20Sopenharmony_ci int err = 0, i; 3758c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci /* Ignore if it's redundant command */ 3788c2ecf20Sopenharmony_ci if (enable == cm->charger_enabled) 3798c2ecf20Sopenharmony_ci return 0; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci if (enable) { 3828c2ecf20Sopenharmony_ci if (cm->emergency_stop) 3838c2ecf20Sopenharmony_ci return -EAGAIN; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci /* 3868c2ecf20Sopenharmony_ci * Save start time of charging to limit 3878c2ecf20Sopenharmony_ci * maximum possible charging time. 3888c2ecf20Sopenharmony_ci */ 3898c2ecf20Sopenharmony_ci cm->charging_start_time = ktime_to_ms(ktime_get()); 3908c2ecf20Sopenharmony_ci cm->charging_end_time = 0; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci for (i = 0 ; i < desc->num_charger_regulators ; i++) { 3938c2ecf20Sopenharmony_ci if (desc->charger_regulators[i].externally_control) 3948c2ecf20Sopenharmony_ci continue; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci err = regulator_enable(desc->charger_regulators[i].consumer); 3978c2ecf20Sopenharmony_ci if (err < 0) { 3988c2ecf20Sopenharmony_ci dev_warn(cm->dev, "Cannot enable %s regulator\n", 3998c2ecf20Sopenharmony_ci desc->charger_regulators[i].regulator_name); 4008c2ecf20Sopenharmony_ci } 4018c2ecf20Sopenharmony_ci } 4028c2ecf20Sopenharmony_ci } else { 4038c2ecf20Sopenharmony_ci /* 4048c2ecf20Sopenharmony_ci * Save end time of charging to maintain fully charged state 4058c2ecf20Sopenharmony_ci * of battery after full-batt. 4068c2ecf20Sopenharmony_ci */ 4078c2ecf20Sopenharmony_ci cm->charging_start_time = 0; 4088c2ecf20Sopenharmony_ci cm->charging_end_time = ktime_to_ms(ktime_get()); 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci for (i = 0 ; i < desc->num_charger_regulators ; i++) { 4118c2ecf20Sopenharmony_ci if (desc->charger_regulators[i].externally_control) 4128c2ecf20Sopenharmony_ci continue; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci err = regulator_disable(desc->charger_regulators[i].consumer); 4158c2ecf20Sopenharmony_ci if (err < 0) { 4168c2ecf20Sopenharmony_ci dev_warn(cm->dev, "Cannot disable %s regulator\n", 4178c2ecf20Sopenharmony_ci desc->charger_regulators[i].regulator_name); 4188c2ecf20Sopenharmony_ci } 4198c2ecf20Sopenharmony_ci } 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci /* 4228c2ecf20Sopenharmony_ci * Abnormal battery state - Stop charging forcibly, 4238c2ecf20Sopenharmony_ci * even if charger was enabled at the other places 4248c2ecf20Sopenharmony_ci */ 4258c2ecf20Sopenharmony_ci for (i = 0; i < desc->num_charger_regulators; i++) { 4268c2ecf20Sopenharmony_ci if (regulator_is_enabled( 4278c2ecf20Sopenharmony_ci desc->charger_regulators[i].consumer)) { 4288c2ecf20Sopenharmony_ci regulator_force_disable( 4298c2ecf20Sopenharmony_ci desc->charger_regulators[i].consumer); 4308c2ecf20Sopenharmony_ci dev_warn(cm->dev, "Disable regulator(%s) forcibly\n", 4318c2ecf20Sopenharmony_ci desc->charger_regulators[i].regulator_name); 4328c2ecf20Sopenharmony_ci } 4338c2ecf20Sopenharmony_ci } 4348c2ecf20Sopenharmony_ci } 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci if (!err) 4378c2ecf20Sopenharmony_ci cm->charger_enabled = enable; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci return err; 4408c2ecf20Sopenharmony_ci} 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci/** 4438c2ecf20Sopenharmony_ci * check_charging_duration - Monitor charging/discharging duration 4448c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 4458c2ecf20Sopenharmony_ci * 4468c2ecf20Sopenharmony_ci * If whole charging duration exceed 'charging_max_duration_ms', 4478c2ecf20Sopenharmony_ci * cm stop charging to prevent overcharge/overheat. If discharging 4488c2ecf20Sopenharmony_ci * duration exceed 'discharging _max_duration_ms', charger cable is 4498c2ecf20Sopenharmony_ci * attached, after full-batt, cm start charging to maintain fully 4508c2ecf20Sopenharmony_ci * charged state for battery. 4518c2ecf20Sopenharmony_ci */ 4528c2ecf20Sopenharmony_cistatic int check_charging_duration(struct charger_manager *cm) 4538c2ecf20Sopenharmony_ci{ 4548c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 4558c2ecf20Sopenharmony_ci u64 curr = ktime_to_ms(ktime_get()); 4568c2ecf20Sopenharmony_ci u64 duration; 4578c2ecf20Sopenharmony_ci int ret = false; 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci if (!desc->charging_max_duration_ms && 4608c2ecf20Sopenharmony_ci !desc->discharging_max_duration_ms) 4618c2ecf20Sopenharmony_ci return ret; 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci if (cm->charger_enabled) { 4648c2ecf20Sopenharmony_ci duration = curr - cm->charging_start_time; 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci if (duration > desc->charging_max_duration_ms) { 4678c2ecf20Sopenharmony_ci dev_info(cm->dev, "Charging duration exceed %ums\n", 4688c2ecf20Sopenharmony_ci desc->charging_max_duration_ms); 4698c2ecf20Sopenharmony_ci ret = true; 4708c2ecf20Sopenharmony_ci } 4718c2ecf20Sopenharmony_ci } else if (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING) { 4728c2ecf20Sopenharmony_ci duration = curr - cm->charging_end_time; 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci if (duration > desc->discharging_max_duration_ms) { 4758c2ecf20Sopenharmony_ci dev_info(cm->dev, "Discharging duration exceed %ums\n", 4768c2ecf20Sopenharmony_ci desc->discharging_max_duration_ms); 4778c2ecf20Sopenharmony_ci ret = true; 4788c2ecf20Sopenharmony_ci } 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci return ret; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistatic int cm_get_battery_temperature_by_psy(struct charger_manager *cm, 4858c2ecf20Sopenharmony_ci int *temp) 4868c2ecf20Sopenharmony_ci{ 4878c2ecf20Sopenharmony_ci struct power_supply *fuel_gauge; 4888c2ecf20Sopenharmony_ci int ret; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); 4918c2ecf20Sopenharmony_ci if (!fuel_gauge) 4928c2ecf20Sopenharmony_ci return -ENODEV; 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci ret = power_supply_get_property(fuel_gauge, 4958c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TEMP, 4968c2ecf20Sopenharmony_ci (union power_supply_propval *)temp); 4978c2ecf20Sopenharmony_ci power_supply_put(fuel_gauge); 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci return ret; 5008c2ecf20Sopenharmony_ci} 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_cistatic int cm_get_battery_temperature(struct charger_manager *cm, 5038c2ecf20Sopenharmony_ci int *temp) 5048c2ecf20Sopenharmony_ci{ 5058c2ecf20Sopenharmony_ci int ret; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci if (!cm->desc->measure_battery_temp) 5088c2ecf20Sopenharmony_ci return -ENODEV; 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL 5118c2ecf20Sopenharmony_ci if (cm->tzd_batt) { 5128c2ecf20Sopenharmony_ci ret = thermal_zone_get_temp(cm->tzd_batt, temp); 5138c2ecf20Sopenharmony_ci if (!ret) 5148c2ecf20Sopenharmony_ci /* Calibrate temperature unit */ 5158c2ecf20Sopenharmony_ci *temp /= 100; 5168c2ecf20Sopenharmony_ci } else 5178c2ecf20Sopenharmony_ci#endif 5188c2ecf20Sopenharmony_ci { 5198c2ecf20Sopenharmony_ci /* if-else continued from CONFIG_THERMAL */ 5208c2ecf20Sopenharmony_ci ret = cm_get_battery_temperature_by_psy(cm, temp); 5218c2ecf20Sopenharmony_ci } 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci return ret; 5248c2ecf20Sopenharmony_ci} 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_cistatic int cm_check_thermal_status(struct charger_manager *cm) 5278c2ecf20Sopenharmony_ci{ 5288c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 5298c2ecf20Sopenharmony_ci int temp, upper_limit, lower_limit; 5308c2ecf20Sopenharmony_ci int ret = 0; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci ret = cm_get_battery_temperature(cm, &temp); 5338c2ecf20Sopenharmony_ci if (ret) { 5348c2ecf20Sopenharmony_ci /* FIXME: 5358c2ecf20Sopenharmony_ci * No information of battery temperature might 5368c2ecf20Sopenharmony_ci * occur hazardous result. We have to handle it 5378c2ecf20Sopenharmony_ci * depending on battery type. 5388c2ecf20Sopenharmony_ci */ 5398c2ecf20Sopenharmony_ci dev_err(cm->dev, "Failed to get battery temperature\n"); 5408c2ecf20Sopenharmony_ci return 0; 5418c2ecf20Sopenharmony_ci } 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci upper_limit = desc->temp_max; 5448c2ecf20Sopenharmony_ci lower_limit = desc->temp_min; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci if (cm->emergency_stop) { 5478c2ecf20Sopenharmony_ci upper_limit -= desc->temp_diff; 5488c2ecf20Sopenharmony_ci lower_limit += desc->temp_diff; 5498c2ecf20Sopenharmony_ci } 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci if (temp > upper_limit) 5528c2ecf20Sopenharmony_ci ret = CM_BATT_OVERHEAT; 5538c2ecf20Sopenharmony_ci else if (temp < lower_limit) 5548c2ecf20Sopenharmony_ci ret = CM_BATT_COLD; 5558c2ecf20Sopenharmony_ci else 5568c2ecf20Sopenharmony_ci ret = CM_BATT_OK; 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci cm->emergency_stop = ret; 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci return ret; 5618c2ecf20Sopenharmony_ci} 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci/** 5648c2ecf20Sopenharmony_ci * cm_get_target_status - Check current status and get next target status. 5658c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 5668c2ecf20Sopenharmony_ci */ 5678c2ecf20Sopenharmony_cistatic int cm_get_target_status(struct charger_manager *cm) 5688c2ecf20Sopenharmony_ci{ 5698c2ecf20Sopenharmony_ci if (!is_ext_pwr_online(cm)) 5708c2ecf20Sopenharmony_ci return POWER_SUPPLY_STATUS_DISCHARGING; 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci if (cm_check_thermal_status(cm)) { 5738c2ecf20Sopenharmony_ci /* Check if discharging duration exeeds limit. */ 5748c2ecf20Sopenharmony_ci if (check_charging_duration(cm)) 5758c2ecf20Sopenharmony_ci goto charging_ok; 5768c2ecf20Sopenharmony_ci return POWER_SUPPLY_STATUS_NOT_CHARGING; 5778c2ecf20Sopenharmony_ci } 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci switch (cm->battery_status) { 5808c2ecf20Sopenharmony_ci case POWER_SUPPLY_STATUS_CHARGING: 5818c2ecf20Sopenharmony_ci /* Check if charging duration exeeds limit. */ 5828c2ecf20Sopenharmony_ci if (check_charging_duration(cm)) 5838c2ecf20Sopenharmony_ci return POWER_SUPPLY_STATUS_FULL; 5848c2ecf20Sopenharmony_ci fallthrough; 5858c2ecf20Sopenharmony_ci case POWER_SUPPLY_STATUS_FULL: 5868c2ecf20Sopenharmony_ci if (is_full_charged(cm)) 5878c2ecf20Sopenharmony_ci return POWER_SUPPLY_STATUS_FULL; 5888c2ecf20Sopenharmony_ci fallthrough; 5898c2ecf20Sopenharmony_ci default: 5908c2ecf20Sopenharmony_ci break; 5918c2ecf20Sopenharmony_ci } 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_cicharging_ok: 5948c2ecf20Sopenharmony_ci /* Charging is allowed. */ 5958c2ecf20Sopenharmony_ci return POWER_SUPPLY_STATUS_CHARGING; 5968c2ecf20Sopenharmony_ci} 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci/** 5998c2ecf20Sopenharmony_ci * _cm_monitor - Monitor the temperature and return true for exceptions. 6008c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 6018c2ecf20Sopenharmony_ci * 6028c2ecf20Sopenharmony_ci * Returns true if there is an event to notify for the battery. 6038c2ecf20Sopenharmony_ci * (True if the status of "emergency_stop" changes) 6048c2ecf20Sopenharmony_ci */ 6058c2ecf20Sopenharmony_cistatic bool _cm_monitor(struct charger_manager *cm) 6068c2ecf20Sopenharmony_ci{ 6078c2ecf20Sopenharmony_ci int target; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci target = cm_get_target_status(cm); 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci try_charger_enable(cm, (target == POWER_SUPPLY_STATUS_CHARGING)); 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci if (cm->battery_status != target) { 6148c2ecf20Sopenharmony_ci cm->battery_status = target; 6158c2ecf20Sopenharmony_ci power_supply_changed(cm->charger_psy); 6168c2ecf20Sopenharmony_ci } 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci return (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING); 6198c2ecf20Sopenharmony_ci} 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci/** 6228c2ecf20Sopenharmony_ci * cm_monitor - Monitor every battery. 6238c2ecf20Sopenharmony_ci * 6248c2ecf20Sopenharmony_ci * Returns true if there is an event to notify from any of the batteries. 6258c2ecf20Sopenharmony_ci * (True if the status of "emergency_stop" changes) 6268c2ecf20Sopenharmony_ci */ 6278c2ecf20Sopenharmony_cistatic bool cm_monitor(void) 6288c2ecf20Sopenharmony_ci{ 6298c2ecf20Sopenharmony_ci bool stop = false; 6308c2ecf20Sopenharmony_ci struct charger_manager *cm; 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci mutex_lock(&cm_list_mtx); 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci list_for_each_entry(cm, &cm_list, entry) { 6358c2ecf20Sopenharmony_ci if (_cm_monitor(cm)) 6368c2ecf20Sopenharmony_ci stop = true; 6378c2ecf20Sopenharmony_ci } 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci mutex_unlock(&cm_list_mtx); 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_ci return stop; 6428c2ecf20Sopenharmony_ci} 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci/** 6458c2ecf20Sopenharmony_ci * _setup_polling - Setup the next instance of polling. 6468c2ecf20Sopenharmony_ci * @work: work_struct of the function _setup_polling. 6478c2ecf20Sopenharmony_ci */ 6488c2ecf20Sopenharmony_cistatic void _setup_polling(struct work_struct *work) 6498c2ecf20Sopenharmony_ci{ 6508c2ecf20Sopenharmony_ci unsigned long min = ULONG_MAX; 6518c2ecf20Sopenharmony_ci struct charger_manager *cm; 6528c2ecf20Sopenharmony_ci bool keep_polling = false; 6538c2ecf20Sopenharmony_ci unsigned long _next_polling; 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_ci mutex_lock(&cm_list_mtx); 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci list_for_each_entry(cm, &cm_list, entry) { 6588c2ecf20Sopenharmony_ci if (is_polling_required(cm) && cm->desc->polling_interval_ms) { 6598c2ecf20Sopenharmony_ci keep_polling = true; 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci if (min > cm->desc->polling_interval_ms) 6628c2ecf20Sopenharmony_ci min = cm->desc->polling_interval_ms; 6638c2ecf20Sopenharmony_ci } 6648c2ecf20Sopenharmony_ci } 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci polling_jiffy = msecs_to_jiffies(min); 6678c2ecf20Sopenharmony_ci if (polling_jiffy <= CM_JIFFIES_SMALL) 6688c2ecf20Sopenharmony_ci polling_jiffy = CM_JIFFIES_SMALL + 1; 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci if (!keep_polling) 6718c2ecf20Sopenharmony_ci polling_jiffy = ULONG_MAX; 6728c2ecf20Sopenharmony_ci if (polling_jiffy == ULONG_MAX) 6738c2ecf20Sopenharmony_ci goto out; 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci WARN(cm_wq == NULL, "charger-manager: workqueue not initialized" 6768c2ecf20Sopenharmony_ci ". try it later. %s\n", __func__); 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_ci /* 6798c2ecf20Sopenharmony_ci * Use mod_delayed_work() iff the next polling interval should 6808c2ecf20Sopenharmony_ci * occur before the currently scheduled one. If @cm_monitor_work 6818c2ecf20Sopenharmony_ci * isn't active, the end result is the same, so no need to worry 6828c2ecf20Sopenharmony_ci * about stale @next_polling. 6838c2ecf20Sopenharmony_ci */ 6848c2ecf20Sopenharmony_ci _next_polling = jiffies + polling_jiffy; 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_ci if (time_before(_next_polling, next_polling)) { 6878c2ecf20Sopenharmony_ci mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy); 6888c2ecf20Sopenharmony_ci next_polling = _next_polling; 6898c2ecf20Sopenharmony_ci } else { 6908c2ecf20Sopenharmony_ci if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy)) 6918c2ecf20Sopenharmony_ci next_polling = _next_polling; 6928c2ecf20Sopenharmony_ci } 6938c2ecf20Sopenharmony_ciout: 6948c2ecf20Sopenharmony_ci mutex_unlock(&cm_list_mtx); 6958c2ecf20Sopenharmony_ci} 6968c2ecf20Sopenharmony_cistatic DECLARE_WORK(setup_polling, _setup_polling); 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci/** 6998c2ecf20Sopenharmony_ci * cm_monitor_poller - The Monitor / Poller. 7008c2ecf20Sopenharmony_ci * @work: work_struct of the function cm_monitor_poller 7018c2ecf20Sopenharmony_ci * 7028c2ecf20Sopenharmony_ci * During non-suspended state, cm_monitor_poller is used to poll and monitor 7038c2ecf20Sopenharmony_ci * the batteries. 7048c2ecf20Sopenharmony_ci */ 7058c2ecf20Sopenharmony_cistatic void cm_monitor_poller(struct work_struct *work) 7068c2ecf20Sopenharmony_ci{ 7078c2ecf20Sopenharmony_ci cm_monitor(); 7088c2ecf20Sopenharmony_ci schedule_work(&setup_polling); 7098c2ecf20Sopenharmony_ci} 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_cistatic int charger_get_property(struct power_supply *psy, 7128c2ecf20Sopenharmony_ci enum power_supply_property psp, 7138c2ecf20Sopenharmony_ci union power_supply_propval *val) 7148c2ecf20Sopenharmony_ci{ 7158c2ecf20Sopenharmony_ci struct charger_manager *cm = power_supply_get_drvdata(psy); 7168c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 7178c2ecf20Sopenharmony_ci struct power_supply *fuel_gauge = NULL; 7188c2ecf20Sopenharmony_ci int ret = 0; 7198c2ecf20Sopenharmony_ci int uV; 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ci switch (psp) { 7228c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_STATUS: 7238c2ecf20Sopenharmony_ci val->intval = cm->battery_status; 7248c2ecf20Sopenharmony_ci break; 7258c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_HEALTH: 7268c2ecf20Sopenharmony_ci if (cm->emergency_stop > 0) 7278c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 7288c2ecf20Sopenharmony_ci else if (cm->emergency_stop < 0) 7298c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_COLD; 7308c2ecf20Sopenharmony_ci else 7318c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_HEALTH_GOOD; 7328c2ecf20Sopenharmony_ci break; 7338c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRESENT: 7348c2ecf20Sopenharmony_ci if (is_batt_present(cm)) 7358c2ecf20Sopenharmony_ci val->intval = 1; 7368c2ecf20Sopenharmony_ci else 7378c2ecf20Sopenharmony_ci val->intval = 0; 7388c2ecf20Sopenharmony_ci break; 7398c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_NOW: 7408c2ecf20Sopenharmony_ci ret = get_batt_uV(cm, &val->intval); 7418c2ecf20Sopenharmony_ci break; 7428c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_NOW: 7438c2ecf20Sopenharmony_ci fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); 7448c2ecf20Sopenharmony_ci if (!fuel_gauge) { 7458c2ecf20Sopenharmony_ci ret = -ENODEV; 7468c2ecf20Sopenharmony_ci break; 7478c2ecf20Sopenharmony_ci } 7488c2ecf20Sopenharmony_ci ret = power_supply_get_property(fuel_gauge, 7498c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_NOW, val); 7508c2ecf20Sopenharmony_ci break; 7518c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TEMP: 7528c2ecf20Sopenharmony_ci return cm_get_battery_temperature(cm, &val->intval); 7538c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CAPACITY: 7548c2ecf20Sopenharmony_ci if (!is_batt_present(cm)) { 7558c2ecf20Sopenharmony_ci /* There is no battery. Assume 100% */ 7568c2ecf20Sopenharmony_ci val->intval = 100; 7578c2ecf20Sopenharmony_ci break; 7588c2ecf20Sopenharmony_ci } 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); 7618c2ecf20Sopenharmony_ci if (!fuel_gauge) { 7628c2ecf20Sopenharmony_ci ret = -ENODEV; 7638c2ecf20Sopenharmony_ci break; 7648c2ecf20Sopenharmony_ci } 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci ret = power_supply_get_property(fuel_gauge, 7678c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CAPACITY, val); 7688c2ecf20Sopenharmony_ci if (ret) 7698c2ecf20Sopenharmony_ci break; 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci if (val->intval > 100) { 7728c2ecf20Sopenharmony_ci val->intval = 100; 7738c2ecf20Sopenharmony_ci break; 7748c2ecf20Sopenharmony_ci } 7758c2ecf20Sopenharmony_ci if (val->intval < 0) 7768c2ecf20Sopenharmony_ci val->intval = 0; 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci /* Do not adjust SOC when charging: voltage is overrated */ 7798c2ecf20Sopenharmony_ci if (is_charging(cm)) 7808c2ecf20Sopenharmony_ci break; 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci /* 7838c2ecf20Sopenharmony_ci * If the capacity value is inconsistent, calibrate it base on 7848c2ecf20Sopenharmony_ci * the battery voltage values and the thresholds given as desc 7858c2ecf20Sopenharmony_ci */ 7868c2ecf20Sopenharmony_ci ret = get_batt_uV(cm, &uV); 7878c2ecf20Sopenharmony_ci if (ret) { 7888c2ecf20Sopenharmony_ci /* Voltage information not available. No calibration */ 7898c2ecf20Sopenharmony_ci ret = 0; 7908c2ecf20Sopenharmony_ci break; 7918c2ecf20Sopenharmony_ci } 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_ci if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV && 7948c2ecf20Sopenharmony_ci !is_charging(cm)) { 7958c2ecf20Sopenharmony_ci val->intval = 100; 7968c2ecf20Sopenharmony_ci break; 7978c2ecf20Sopenharmony_ci } 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci break; 8008c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ONLINE: 8018c2ecf20Sopenharmony_ci if (is_ext_pwr_online(cm)) 8028c2ecf20Sopenharmony_ci val->intval = 1; 8038c2ecf20Sopenharmony_ci else 8048c2ecf20Sopenharmony_ci val->intval = 0; 8058c2ecf20Sopenharmony_ci break; 8068c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_FULL: 8078c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_NOW: 8088c2ecf20Sopenharmony_ci fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); 8098c2ecf20Sopenharmony_ci if (!fuel_gauge) { 8108c2ecf20Sopenharmony_ci ret = -ENODEV; 8118c2ecf20Sopenharmony_ci break; 8128c2ecf20Sopenharmony_ci } 8138c2ecf20Sopenharmony_ci ret = power_supply_get_property(fuel_gauge, psp, val); 8148c2ecf20Sopenharmony_ci break; 8158c2ecf20Sopenharmony_ci default: 8168c2ecf20Sopenharmony_ci return -EINVAL; 8178c2ecf20Sopenharmony_ci } 8188c2ecf20Sopenharmony_ci if (fuel_gauge) 8198c2ecf20Sopenharmony_ci power_supply_put(fuel_gauge); 8208c2ecf20Sopenharmony_ci return ret; 8218c2ecf20Sopenharmony_ci} 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci#define NUM_CHARGER_PSY_OPTIONAL (4) 8248c2ecf20Sopenharmony_cistatic enum power_supply_property default_charger_props[] = { 8258c2ecf20Sopenharmony_ci /* Guaranteed to provide */ 8268c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_STATUS, 8278c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_HEALTH, 8288c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_PRESENT, 8298c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_NOW, 8308c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CAPACITY, 8318c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_ONLINE, 8328c2ecf20Sopenharmony_ci /* 8338c2ecf20Sopenharmony_ci * Optional properties are: 8348c2ecf20Sopenharmony_ci * POWER_SUPPLY_PROP_CHARGE_FULL, 8358c2ecf20Sopenharmony_ci * POWER_SUPPLY_PROP_CHARGE_NOW, 8368c2ecf20Sopenharmony_ci * POWER_SUPPLY_PROP_CURRENT_NOW, 8378c2ecf20Sopenharmony_ci * POWER_SUPPLY_PROP_TEMP, 8388c2ecf20Sopenharmony_ci */ 8398c2ecf20Sopenharmony_ci}; 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_cistatic const struct power_supply_desc psy_default = { 8428c2ecf20Sopenharmony_ci .name = "battery", 8438c2ecf20Sopenharmony_ci .type = POWER_SUPPLY_TYPE_BATTERY, 8448c2ecf20Sopenharmony_ci .properties = default_charger_props, 8458c2ecf20Sopenharmony_ci .num_properties = ARRAY_SIZE(default_charger_props), 8468c2ecf20Sopenharmony_ci .get_property = charger_get_property, 8478c2ecf20Sopenharmony_ci .no_thermal = true, 8488c2ecf20Sopenharmony_ci}; 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci/** 8518c2ecf20Sopenharmony_ci * cm_setup_timer - For in-suspend monitoring setup wakeup alarm 8528c2ecf20Sopenharmony_ci * for suspend_again. 8538c2ecf20Sopenharmony_ci * 8548c2ecf20Sopenharmony_ci * Returns true if the alarm is set for Charger Manager to use. 8558c2ecf20Sopenharmony_ci * Returns false if 8568c2ecf20Sopenharmony_ci * cm_setup_timer fails to set an alarm, 8578c2ecf20Sopenharmony_ci * cm_setup_timer does not need to set an alarm for Charger Manager, 8588c2ecf20Sopenharmony_ci * or an alarm previously configured is to be used. 8598c2ecf20Sopenharmony_ci */ 8608c2ecf20Sopenharmony_cistatic bool cm_setup_timer(void) 8618c2ecf20Sopenharmony_ci{ 8628c2ecf20Sopenharmony_ci struct charger_manager *cm; 8638c2ecf20Sopenharmony_ci unsigned int wakeup_ms = UINT_MAX; 8648c2ecf20Sopenharmony_ci int timer_req = 0; 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci if (time_after(next_polling, jiffies)) 8678c2ecf20Sopenharmony_ci CM_MIN_VALID(wakeup_ms, 8688c2ecf20Sopenharmony_ci jiffies_to_msecs(next_polling - jiffies)); 8698c2ecf20Sopenharmony_ci 8708c2ecf20Sopenharmony_ci mutex_lock(&cm_list_mtx); 8718c2ecf20Sopenharmony_ci list_for_each_entry(cm, &cm_list, entry) { 8728c2ecf20Sopenharmony_ci /* Skip if polling is not required for this CM */ 8738c2ecf20Sopenharmony_ci if (!is_polling_required(cm) && !cm->emergency_stop) 8748c2ecf20Sopenharmony_ci continue; 8758c2ecf20Sopenharmony_ci timer_req++; 8768c2ecf20Sopenharmony_ci if (cm->desc->polling_interval_ms == 0) 8778c2ecf20Sopenharmony_ci continue; 8788c2ecf20Sopenharmony_ci CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms); 8798c2ecf20Sopenharmony_ci } 8808c2ecf20Sopenharmony_ci mutex_unlock(&cm_list_mtx); 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ci if (timer_req && cm_timer) { 8838c2ecf20Sopenharmony_ci ktime_t now, add; 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci /* 8868c2ecf20Sopenharmony_ci * Set alarm with the polling interval (wakeup_ms) 8878c2ecf20Sopenharmony_ci * The alarm time should be NOW + CM_RTC_SMALL or later. 8888c2ecf20Sopenharmony_ci */ 8898c2ecf20Sopenharmony_ci if (wakeup_ms == UINT_MAX || 8908c2ecf20Sopenharmony_ci wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC) 8918c2ecf20Sopenharmony_ci wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC; 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms); 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci now = ktime_get_boottime(); 8968c2ecf20Sopenharmony_ci add = ktime_set(wakeup_ms / MSEC_PER_SEC, 8978c2ecf20Sopenharmony_ci (wakeup_ms % MSEC_PER_SEC) * NSEC_PER_MSEC); 8988c2ecf20Sopenharmony_ci alarm_start(cm_timer, ktime_add(now, add)); 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_ci cm_suspend_duration_ms = wakeup_ms; 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci return true; 9038c2ecf20Sopenharmony_ci } 9048c2ecf20Sopenharmony_ci return false; 9058c2ecf20Sopenharmony_ci} 9068c2ecf20Sopenharmony_ci 9078c2ecf20Sopenharmony_ci/** 9088c2ecf20Sopenharmony_ci * charger_extcon_work - enable/diable charger according to the state 9098c2ecf20Sopenharmony_ci * of charger cable 9108c2ecf20Sopenharmony_ci * 9118c2ecf20Sopenharmony_ci * @work: work_struct of the function charger_extcon_work. 9128c2ecf20Sopenharmony_ci */ 9138c2ecf20Sopenharmony_cistatic void charger_extcon_work(struct work_struct *work) 9148c2ecf20Sopenharmony_ci{ 9158c2ecf20Sopenharmony_ci struct charger_cable *cable = 9168c2ecf20Sopenharmony_ci container_of(work, struct charger_cable, wq); 9178c2ecf20Sopenharmony_ci int ret; 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) { 9208c2ecf20Sopenharmony_ci ret = regulator_set_current_limit(cable->charger->consumer, 9218c2ecf20Sopenharmony_ci cable->min_uA, cable->max_uA); 9228c2ecf20Sopenharmony_ci if (ret < 0) { 9238c2ecf20Sopenharmony_ci pr_err("Cannot set current limit of %s (%s)\n", 9248c2ecf20Sopenharmony_ci cable->charger->regulator_name, cable->name); 9258c2ecf20Sopenharmony_ci return; 9268c2ecf20Sopenharmony_ci } 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci pr_info("Set current limit of %s : %duA ~ %duA\n", 9298c2ecf20Sopenharmony_ci cable->charger->regulator_name, 9308c2ecf20Sopenharmony_ci cable->min_uA, cable->max_uA); 9318c2ecf20Sopenharmony_ci } 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci cancel_delayed_work(&cm_monitor_work); 9348c2ecf20Sopenharmony_ci queue_delayed_work(cm_wq, &cm_monitor_work, 0); 9358c2ecf20Sopenharmony_ci} 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci/** 9388c2ecf20Sopenharmony_ci * charger_extcon_notifier - receive the state of charger cable 9398c2ecf20Sopenharmony_ci * when registered cable is attached or detached. 9408c2ecf20Sopenharmony_ci * 9418c2ecf20Sopenharmony_ci * @self: the notifier block of the charger_extcon_notifier. 9428c2ecf20Sopenharmony_ci * @event: the cable state. 9438c2ecf20Sopenharmony_ci * @ptr: the data pointer of notifier block. 9448c2ecf20Sopenharmony_ci */ 9458c2ecf20Sopenharmony_cistatic int charger_extcon_notifier(struct notifier_block *self, 9468c2ecf20Sopenharmony_ci unsigned long event, void *ptr) 9478c2ecf20Sopenharmony_ci{ 9488c2ecf20Sopenharmony_ci struct charger_cable *cable = 9498c2ecf20Sopenharmony_ci container_of(self, struct charger_cable, nb); 9508c2ecf20Sopenharmony_ci 9518c2ecf20Sopenharmony_ci /* 9528c2ecf20Sopenharmony_ci * The newly state of charger cable. 9538c2ecf20Sopenharmony_ci * If cable is attached, cable->attached is true. 9548c2ecf20Sopenharmony_ci */ 9558c2ecf20Sopenharmony_ci cable->attached = event; 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_ci /* 9588c2ecf20Sopenharmony_ci * Setup work for controlling charger(regulator) 9598c2ecf20Sopenharmony_ci * according to charger cable. 9608c2ecf20Sopenharmony_ci */ 9618c2ecf20Sopenharmony_ci schedule_work(&cable->wq); 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci return NOTIFY_DONE; 9648c2ecf20Sopenharmony_ci} 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci/** 9678c2ecf20Sopenharmony_ci * charger_extcon_init - register external connector to use it 9688c2ecf20Sopenharmony_ci * as the charger cable 9698c2ecf20Sopenharmony_ci * 9708c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 9718c2ecf20Sopenharmony_ci * @cable: the Charger cable representing the external connector. 9728c2ecf20Sopenharmony_ci */ 9738c2ecf20Sopenharmony_cistatic int charger_extcon_init(struct charger_manager *cm, 9748c2ecf20Sopenharmony_ci struct charger_cable *cable) 9758c2ecf20Sopenharmony_ci{ 9768c2ecf20Sopenharmony_ci int ret, i; 9778c2ecf20Sopenharmony_ci u64 extcon_type = EXTCON_NONE; 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_ci /* 9808c2ecf20Sopenharmony_ci * Charger manager use Extcon framework to identify 9818c2ecf20Sopenharmony_ci * the charger cable among various external connector 9828c2ecf20Sopenharmony_ci * cable (e.g., TA, USB, MHL, Dock). 9838c2ecf20Sopenharmony_ci */ 9848c2ecf20Sopenharmony_ci INIT_WORK(&cable->wq, charger_extcon_work); 9858c2ecf20Sopenharmony_ci cable->nb.notifier_call = charger_extcon_notifier; 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci cable->extcon_dev = extcon_get_extcon_dev(cable->extcon_name); 9888c2ecf20Sopenharmony_ci if (IS_ERR_OR_NULL(cable->extcon_dev)) { 9898c2ecf20Sopenharmony_ci pr_err("Cannot find extcon_dev for %s (cable: %s)\n", 9908c2ecf20Sopenharmony_ci cable->extcon_name, cable->name); 9918c2ecf20Sopenharmony_ci if (cable->extcon_dev == NULL) 9928c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 9938c2ecf20Sopenharmony_ci else 9948c2ecf20Sopenharmony_ci return PTR_ERR(cable->extcon_dev); 9958c2ecf20Sopenharmony_ci } 9968c2ecf20Sopenharmony_ci 9978c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(extcon_mapping); i++) { 9988c2ecf20Sopenharmony_ci if (!strcmp(cable->name, extcon_mapping[i].name)) { 9998c2ecf20Sopenharmony_ci extcon_type = extcon_mapping[i].extcon_type; 10008c2ecf20Sopenharmony_ci break; 10018c2ecf20Sopenharmony_ci } 10028c2ecf20Sopenharmony_ci } 10038c2ecf20Sopenharmony_ci if (extcon_type == EXTCON_NONE) { 10048c2ecf20Sopenharmony_ci pr_err("Cannot find cable for type %s", cable->name); 10058c2ecf20Sopenharmony_ci return -EINVAL; 10068c2ecf20Sopenharmony_ci } 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_ci cable->extcon_type = extcon_type; 10098c2ecf20Sopenharmony_ci 10108c2ecf20Sopenharmony_ci ret = devm_extcon_register_notifier(cm->dev, cable->extcon_dev, 10118c2ecf20Sopenharmony_ci cable->extcon_type, &cable->nb); 10128c2ecf20Sopenharmony_ci if (ret < 0) { 10138c2ecf20Sopenharmony_ci pr_err("Cannot register extcon_dev for %s (cable: %s)\n", 10148c2ecf20Sopenharmony_ci cable->extcon_name, cable->name); 10158c2ecf20Sopenharmony_ci return ret; 10168c2ecf20Sopenharmony_ci } 10178c2ecf20Sopenharmony_ci 10188c2ecf20Sopenharmony_ci return 0; 10198c2ecf20Sopenharmony_ci} 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_ci/** 10228c2ecf20Sopenharmony_ci * charger_manager_register_extcon - Register extcon device to receive state 10238c2ecf20Sopenharmony_ci * of charger cable. 10248c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 10258c2ecf20Sopenharmony_ci * 10268c2ecf20Sopenharmony_ci * This function support EXTCON(External Connector) subsystem to detect the 10278c2ecf20Sopenharmony_ci * state of charger cables for enabling or disabling charger(regulator) and 10288c2ecf20Sopenharmony_ci * select the charger cable for charging among a number of external cable 10298c2ecf20Sopenharmony_ci * according to policy of H/W board. 10308c2ecf20Sopenharmony_ci */ 10318c2ecf20Sopenharmony_cistatic int charger_manager_register_extcon(struct charger_manager *cm) 10328c2ecf20Sopenharmony_ci{ 10338c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 10348c2ecf20Sopenharmony_ci struct charger_regulator *charger; 10358c2ecf20Sopenharmony_ci unsigned long event; 10368c2ecf20Sopenharmony_ci int ret; 10378c2ecf20Sopenharmony_ci int i; 10388c2ecf20Sopenharmony_ci int j; 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_ci for (i = 0; i < desc->num_charger_regulators; i++) { 10418c2ecf20Sopenharmony_ci charger = &desc->charger_regulators[i]; 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci charger->consumer = regulator_get(cm->dev, 10448c2ecf20Sopenharmony_ci charger->regulator_name); 10458c2ecf20Sopenharmony_ci if (IS_ERR(charger->consumer)) { 10468c2ecf20Sopenharmony_ci dev_err(cm->dev, "Cannot find charger(%s)\n", 10478c2ecf20Sopenharmony_ci charger->regulator_name); 10488c2ecf20Sopenharmony_ci return PTR_ERR(charger->consumer); 10498c2ecf20Sopenharmony_ci } 10508c2ecf20Sopenharmony_ci charger->cm = cm; 10518c2ecf20Sopenharmony_ci 10528c2ecf20Sopenharmony_ci for (j = 0; j < charger->num_cables; j++) { 10538c2ecf20Sopenharmony_ci struct charger_cable *cable = &charger->cables[j]; 10548c2ecf20Sopenharmony_ci 10558c2ecf20Sopenharmony_ci ret = charger_extcon_init(cm, cable); 10568c2ecf20Sopenharmony_ci if (ret < 0) { 10578c2ecf20Sopenharmony_ci dev_err(cm->dev, "Cannot initialize charger(%s)\n", 10588c2ecf20Sopenharmony_ci charger->regulator_name); 10598c2ecf20Sopenharmony_ci return ret; 10608c2ecf20Sopenharmony_ci } 10618c2ecf20Sopenharmony_ci cable->charger = charger; 10628c2ecf20Sopenharmony_ci cable->cm = cm; 10638c2ecf20Sopenharmony_ci 10648c2ecf20Sopenharmony_ci event = extcon_get_state(cable->extcon_dev, 10658c2ecf20Sopenharmony_ci cable->extcon_type); 10668c2ecf20Sopenharmony_ci charger_extcon_notifier(&cable->nb, 10678c2ecf20Sopenharmony_ci event, NULL); 10688c2ecf20Sopenharmony_ci } 10698c2ecf20Sopenharmony_ci } 10708c2ecf20Sopenharmony_ci 10718c2ecf20Sopenharmony_ci return 0; 10728c2ecf20Sopenharmony_ci} 10738c2ecf20Sopenharmony_ci 10748c2ecf20Sopenharmony_ci/* help function of sysfs node to control charger(regulator) */ 10758c2ecf20Sopenharmony_cistatic ssize_t charger_name_show(struct device *dev, 10768c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 10778c2ecf20Sopenharmony_ci{ 10788c2ecf20Sopenharmony_ci struct charger_regulator *charger 10798c2ecf20Sopenharmony_ci = container_of(attr, struct charger_regulator, attr_name); 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_ci return sprintf(buf, "%s\n", charger->regulator_name); 10828c2ecf20Sopenharmony_ci} 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_cistatic ssize_t charger_state_show(struct device *dev, 10858c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 10868c2ecf20Sopenharmony_ci{ 10878c2ecf20Sopenharmony_ci struct charger_regulator *charger 10888c2ecf20Sopenharmony_ci = container_of(attr, struct charger_regulator, attr_state); 10898c2ecf20Sopenharmony_ci int state = 0; 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci if (!charger->externally_control) 10928c2ecf20Sopenharmony_ci state = regulator_is_enabled(charger->consumer); 10938c2ecf20Sopenharmony_ci 10948c2ecf20Sopenharmony_ci return sprintf(buf, "%s\n", state ? "enabled" : "disabled"); 10958c2ecf20Sopenharmony_ci} 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_cistatic ssize_t charger_externally_control_show(struct device *dev, 10988c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 10998c2ecf20Sopenharmony_ci{ 11008c2ecf20Sopenharmony_ci struct charger_regulator *charger = container_of(attr, 11018c2ecf20Sopenharmony_ci struct charger_regulator, attr_externally_control); 11028c2ecf20Sopenharmony_ci 11038c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", charger->externally_control); 11048c2ecf20Sopenharmony_ci} 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_cistatic ssize_t charger_externally_control_store(struct device *dev, 11078c2ecf20Sopenharmony_ci struct device_attribute *attr, const char *buf, 11088c2ecf20Sopenharmony_ci size_t count) 11098c2ecf20Sopenharmony_ci{ 11108c2ecf20Sopenharmony_ci struct charger_regulator *charger 11118c2ecf20Sopenharmony_ci = container_of(attr, struct charger_regulator, 11128c2ecf20Sopenharmony_ci attr_externally_control); 11138c2ecf20Sopenharmony_ci struct charger_manager *cm = charger->cm; 11148c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 11158c2ecf20Sopenharmony_ci int i; 11168c2ecf20Sopenharmony_ci int ret; 11178c2ecf20Sopenharmony_ci int externally_control; 11188c2ecf20Sopenharmony_ci int chargers_externally_control = 1; 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_ci ret = sscanf(buf, "%d", &externally_control); 11218c2ecf20Sopenharmony_ci if (ret == 0) { 11228c2ecf20Sopenharmony_ci ret = -EINVAL; 11238c2ecf20Sopenharmony_ci return ret; 11248c2ecf20Sopenharmony_ci } 11258c2ecf20Sopenharmony_ci 11268c2ecf20Sopenharmony_ci if (!externally_control) { 11278c2ecf20Sopenharmony_ci charger->externally_control = 0; 11288c2ecf20Sopenharmony_ci return count; 11298c2ecf20Sopenharmony_ci } 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci for (i = 0; i < desc->num_charger_regulators; i++) { 11328c2ecf20Sopenharmony_ci if (&desc->charger_regulators[i] != charger && 11338c2ecf20Sopenharmony_ci !desc->charger_regulators[i].externally_control) { 11348c2ecf20Sopenharmony_ci /* 11358c2ecf20Sopenharmony_ci * At least, one charger is controlled by 11368c2ecf20Sopenharmony_ci * charger-manager 11378c2ecf20Sopenharmony_ci */ 11388c2ecf20Sopenharmony_ci chargers_externally_control = 0; 11398c2ecf20Sopenharmony_ci break; 11408c2ecf20Sopenharmony_ci } 11418c2ecf20Sopenharmony_ci } 11428c2ecf20Sopenharmony_ci 11438c2ecf20Sopenharmony_ci if (!chargers_externally_control) { 11448c2ecf20Sopenharmony_ci if (cm->charger_enabled) { 11458c2ecf20Sopenharmony_ci try_charger_enable(charger->cm, false); 11468c2ecf20Sopenharmony_ci charger->externally_control = externally_control; 11478c2ecf20Sopenharmony_ci try_charger_enable(charger->cm, true); 11488c2ecf20Sopenharmony_ci } else { 11498c2ecf20Sopenharmony_ci charger->externally_control = externally_control; 11508c2ecf20Sopenharmony_ci } 11518c2ecf20Sopenharmony_ci } else { 11528c2ecf20Sopenharmony_ci dev_warn(cm->dev, 11538c2ecf20Sopenharmony_ci "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n", 11548c2ecf20Sopenharmony_ci charger->regulator_name); 11558c2ecf20Sopenharmony_ci } 11568c2ecf20Sopenharmony_ci 11578c2ecf20Sopenharmony_ci return count; 11588c2ecf20Sopenharmony_ci} 11598c2ecf20Sopenharmony_ci 11608c2ecf20Sopenharmony_ci/** 11618c2ecf20Sopenharmony_ci * charger_manager_prepare_sysfs - Prepare sysfs entry for each charger 11628c2ecf20Sopenharmony_ci * @cm: the Charger Manager representing the battery. 11638c2ecf20Sopenharmony_ci * 11648c2ecf20Sopenharmony_ci * This function add sysfs entry for charger(regulator) to control charger from 11658c2ecf20Sopenharmony_ci * user-space. If some development board use one more chargers for charging 11668c2ecf20Sopenharmony_ci * but only need one charger on specific case which is dependent on user 11678c2ecf20Sopenharmony_ci * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/ 11688c2ecf20Sopenharmony_ci * class/power_supply/battery/charger.[index]/externally_control'. For example, 11698c2ecf20Sopenharmony_ci * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/ 11708c2ecf20Sopenharmony_ci * externally_control, this charger isn't controlled from charger-manager and 11718c2ecf20Sopenharmony_ci * always stay off state of regulator. 11728c2ecf20Sopenharmony_ci */ 11738c2ecf20Sopenharmony_cistatic int charger_manager_prepare_sysfs(struct charger_manager *cm) 11748c2ecf20Sopenharmony_ci{ 11758c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 11768c2ecf20Sopenharmony_ci struct charger_regulator *charger; 11778c2ecf20Sopenharmony_ci int chargers_externally_control = 1; 11788c2ecf20Sopenharmony_ci char *name; 11798c2ecf20Sopenharmony_ci int i; 11808c2ecf20Sopenharmony_ci 11818c2ecf20Sopenharmony_ci /* Create sysfs entry to control charger(regulator) */ 11828c2ecf20Sopenharmony_ci for (i = 0; i < desc->num_charger_regulators; i++) { 11838c2ecf20Sopenharmony_ci charger = &desc->charger_regulators[i]; 11848c2ecf20Sopenharmony_ci 11858c2ecf20Sopenharmony_ci name = devm_kasprintf(cm->dev, GFP_KERNEL, "charger.%d", i); 11868c2ecf20Sopenharmony_ci if (!name) 11878c2ecf20Sopenharmony_ci return -ENOMEM; 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_ci charger->attrs[0] = &charger->attr_name.attr; 11908c2ecf20Sopenharmony_ci charger->attrs[1] = &charger->attr_state.attr; 11918c2ecf20Sopenharmony_ci charger->attrs[2] = &charger->attr_externally_control.attr; 11928c2ecf20Sopenharmony_ci charger->attrs[3] = NULL; 11938c2ecf20Sopenharmony_ci 11948c2ecf20Sopenharmony_ci charger->attr_grp.name = name; 11958c2ecf20Sopenharmony_ci charger->attr_grp.attrs = charger->attrs; 11968c2ecf20Sopenharmony_ci desc->sysfs_groups[i] = &charger->attr_grp; 11978c2ecf20Sopenharmony_ci 11988c2ecf20Sopenharmony_ci sysfs_attr_init(&charger->attr_name.attr); 11998c2ecf20Sopenharmony_ci charger->attr_name.attr.name = "name"; 12008c2ecf20Sopenharmony_ci charger->attr_name.attr.mode = 0444; 12018c2ecf20Sopenharmony_ci charger->attr_name.show = charger_name_show; 12028c2ecf20Sopenharmony_ci 12038c2ecf20Sopenharmony_ci sysfs_attr_init(&charger->attr_state.attr); 12048c2ecf20Sopenharmony_ci charger->attr_state.attr.name = "state"; 12058c2ecf20Sopenharmony_ci charger->attr_state.attr.mode = 0444; 12068c2ecf20Sopenharmony_ci charger->attr_state.show = charger_state_show; 12078c2ecf20Sopenharmony_ci 12088c2ecf20Sopenharmony_ci sysfs_attr_init(&charger->attr_externally_control.attr); 12098c2ecf20Sopenharmony_ci charger->attr_externally_control.attr.name 12108c2ecf20Sopenharmony_ci = "externally_control"; 12118c2ecf20Sopenharmony_ci charger->attr_externally_control.attr.mode = 0644; 12128c2ecf20Sopenharmony_ci charger->attr_externally_control.show 12138c2ecf20Sopenharmony_ci = charger_externally_control_show; 12148c2ecf20Sopenharmony_ci charger->attr_externally_control.store 12158c2ecf20Sopenharmony_ci = charger_externally_control_store; 12168c2ecf20Sopenharmony_ci 12178c2ecf20Sopenharmony_ci if (!desc->charger_regulators[i].externally_control || 12188c2ecf20Sopenharmony_ci !chargers_externally_control) 12198c2ecf20Sopenharmony_ci chargers_externally_control = 0; 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_ci dev_info(cm->dev, "'%s' regulator's externally_control is %d\n", 12228c2ecf20Sopenharmony_ci charger->regulator_name, charger->externally_control); 12238c2ecf20Sopenharmony_ci } 12248c2ecf20Sopenharmony_ci 12258c2ecf20Sopenharmony_ci if (chargers_externally_control) { 12268c2ecf20Sopenharmony_ci dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n"); 12278c2ecf20Sopenharmony_ci return -EINVAL; 12288c2ecf20Sopenharmony_ci } 12298c2ecf20Sopenharmony_ci 12308c2ecf20Sopenharmony_ci return 0; 12318c2ecf20Sopenharmony_ci} 12328c2ecf20Sopenharmony_ci 12338c2ecf20Sopenharmony_cistatic int cm_init_thermal_data(struct charger_manager *cm, 12348c2ecf20Sopenharmony_ci struct power_supply *fuel_gauge, 12358c2ecf20Sopenharmony_ci enum power_supply_property *properties, 12368c2ecf20Sopenharmony_ci size_t *num_properties) 12378c2ecf20Sopenharmony_ci{ 12388c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 12398c2ecf20Sopenharmony_ci union power_supply_propval val; 12408c2ecf20Sopenharmony_ci int ret; 12418c2ecf20Sopenharmony_ci 12428c2ecf20Sopenharmony_ci /* Verify whether fuel gauge provides battery temperature */ 12438c2ecf20Sopenharmony_ci ret = power_supply_get_property(fuel_gauge, 12448c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TEMP, &val); 12458c2ecf20Sopenharmony_ci 12468c2ecf20Sopenharmony_ci if (!ret) { 12478c2ecf20Sopenharmony_ci properties[*num_properties] = POWER_SUPPLY_PROP_TEMP; 12488c2ecf20Sopenharmony_ci (*num_properties)++; 12498c2ecf20Sopenharmony_ci cm->desc->measure_battery_temp = true; 12508c2ecf20Sopenharmony_ci } 12518c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL 12528c2ecf20Sopenharmony_ci if (ret && desc->thermal_zone) { 12538c2ecf20Sopenharmony_ci cm->tzd_batt = 12548c2ecf20Sopenharmony_ci thermal_zone_get_zone_by_name(desc->thermal_zone); 12558c2ecf20Sopenharmony_ci if (IS_ERR(cm->tzd_batt)) 12568c2ecf20Sopenharmony_ci return PTR_ERR(cm->tzd_batt); 12578c2ecf20Sopenharmony_ci 12588c2ecf20Sopenharmony_ci /* Use external thermometer */ 12598c2ecf20Sopenharmony_ci properties[*num_properties] = POWER_SUPPLY_PROP_TEMP; 12608c2ecf20Sopenharmony_ci (*num_properties)++; 12618c2ecf20Sopenharmony_ci cm->desc->measure_battery_temp = true; 12628c2ecf20Sopenharmony_ci ret = 0; 12638c2ecf20Sopenharmony_ci } 12648c2ecf20Sopenharmony_ci#endif 12658c2ecf20Sopenharmony_ci if (cm->desc->measure_battery_temp) { 12668c2ecf20Sopenharmony_ci /* NOTICE : Default allowable minimum charge temperature is 0 */ 12678c2ecf20Sopenharmony_ci if (!desc->temp_max) 12688c2ecf20Sopenharmony_ci desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX; 12698c2ecf20Sopenharmony_ci if (!desc->temp_diff) 12708c2ecf20Sopenharmony_ci desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF; 12718c2ecf20Sopenharmony_ci } 12728c2ecf20Sopenharmony_ci 12738c2ecf20Sopenharmony_ci return ret; 12748c2ecf20Sopenharmony_ci} 12758c2ecf20Sopenharmony_ci 12768c2ecf20Sopenharmony_cistatic const struct of_device_id charger_manager_match[] = { 12778c2ecf20Sopenharmony_ci { 12788c2ecf20Sopenharmony_ci .compatible = "charger-manager", 12798c2ecf20Sopenharmony_ci }, 12808c2ecf20Sopenharmony_ci {}, 12818c2ecf20Sopenharmony_ci}; 12828c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, charger_manager_match); 12838c2ecf20Sopenharmony_ci 12848c2ecf20Sopenharmony_cistatic struct charger_desc *of_cm_parse_desc(struct device *dev) 12858c2ecf20Sopenharmony_ci{ 12868c2ecf20Sopenharmony_ci struct charger_desc *desc; 12878c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 12888c2ecf20Sopenharmony_ci u32 poll_mode = CM_POLL_DISABLE; 12898c2ecf20Sopenharmony_ci u32 battery_stat = CM_NO_BATTERY; 12908c2ecf20Sopenharmony_ci int num_chgs = 0; 12918c2ecf20Sopenharmony_ci 12928c2ecf20Sopenharmony_ci desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); 12938c2ecf20Sopenharmony_ci if (!desc) 12948c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 12958c2ecf20Sopenharmony_ci 12968c2ecf20Sopenharmony_ci of_property_read_string(np, "cm-name", &desc->psy_name); 12978c2ecf20Sopenharmony_ci 12988c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-poll-mode", &poll_mode); 12998c2ecf20Sopenharmony_ci desc->polling_mode = poll_mode; 13008c2ecf20Sopenharmony_ci 13018c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-poll-interval", 13028c2ecf20Sopenharmony_ci &desc->polling_interval_ms); 13038c2ecf20Sopenharmony_ci 13048c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt", 13058c2ecf20Sopenharmony_ci &desc->fullbatt_vchkdrop_uV); 13068c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV); 13078c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc); 13088c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-fullbatt-capacity", 13098c2ecf20Sopenharmony_ci &desc->fullbatt_full_capacity); 13108c2ecf20Sopenharmony_ci 13118c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-battery-stat", &battery_stat); 13128c2ecf20Sopenharmony_ci desc->battery_present = battery_stat; 13138c2ecf20Sopenharmony_ci 13148c2ecf20Sopenharmony_ci /* chargers */ 13158c2ecf20Sopenharmony_ci num_chgs = of_property_count_strings(np, "cm-chargers"); 13168c2ecf20Sopenharmony_ci if (num_chgs > 0) { 13178c2ecf20Sopenharmony_ci int i; 13188c2ecf20Sopenharmony_ci 13198c2ecf20Sopenharmony_ci /* Allocate empty bin at the tail of array */ 13208c2ecf20Sopenharmony_ci desc->psy_charger_stat = devm_kcalloc(dev, 13218c2ecf20Sopenharmony_ci num_chgs + 1, 13228c2ecf20Sopenharmony_ci sizeof(char *), 13238c2ecf20Sopenharmony_ci GFP_KERNEL); 13248c2ecf20Sopenharmony_ci if (!desc->psy_charger_stat) 13258c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 13268c2ecf20Sopenharmony_ci 13278c2ecf20Sopenharmony_ci for (i = 0; i < num_chgs; i++) 13288c2ecf20Sopenharmony_ci of_property_read_string_index(np, "cm-chargers", 13298c2ecf20Sopenharmony_ci i, &desc->psy_charger_stat[i]); 13308c2ecf20Sopenharmony_ci } 13318c2ecf20Sopenharmony_ci 13328c2ecf20Sopenharmony_ci of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge); 13338c2ecf20Sopenharmony_ci 13348c2ecf20Sopenharmony_ci of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone); 13358c2ecf20Sopenharmony_ci 13368c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-battery-cold", &desc->temp_min); 13378c2ecf20Sopenharmony_ci if (of_get_property(np, "cm-battery-cold-in-minus", NULL)) 13388c2ecf20Sopenharmony_ci desc->temp_min *= -1; 13398c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-battery-hot", &desc->temp_max); 13408c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff); 13418c2ecf20Sopenharmony_ci 13428c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-charging-max", 13438c2ecf20Sopenharmony_ci &desc->charging_max_duration_ms); 13448c2ecf20Sopenharmony_ci of_property_read_u32(np, "cm-discharging-max", 13458c2ecf20Sopenharmony_ci &desc->discharging_max_duration_ms); 13468c2ecf20Sopenharmony_ci 13478c2ecf20Sopenharmony_ci /* battery charger regulators */ 13488c2ecf20Sopenharmony_ci desc->num_charger_regulators = of_get_child_count(np); 13498c2ecf20Sopenharmony_ci if (desc->num_charger_regulators) { 13508c2ecf20Sopenharmony_ci struct charger_regulator *chg_regs; 13518c2ecf20Sopenharmony_ci struct device_node *child; 13528c2ecf20Sopenharmony_ci 13538c2ecf20Sopenharmony_ci chg_regs = devm_kcalloc(dev, 13548c2ecf20Sopenharmony_ci desc->num_charger_regulators, 13558c2ecf20Sopenharmony_ci sizeof(*chg_regs), 13568c2ecf20Sopenharmony_ci GFP_KERNEL); 13578c2ecf20Sopenharmony_ci if (!chg_regs) 13588c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 13598c2ecf20Sopenharmony_ci 13608c2ecf20Sopenharmony_ci desc->charger_regulators = chg_regs; 13618c2ecf20Sopenharmony_ci 13628c2ecf20Sopenharmony_ci desc->sysfs_groups = devm_kcalloc(dev, 13638c2ecf20Sopenharmony_ci desc->num_charger_regulators + 1, 13648c2ecf20Sopenharmony_ci sizeof(*desc->sysfs_groups), 13658c2ecf20Sopenharmony_ci GFP_KERNEL); 13668c2ecf20Sopenharmony_ci if (!desc->sysfs_groups) 13678c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 13688c2ecf20Sopenharmony_ci 13698c2ecf20Sopenharmony_ci for_each_child_of_node(np, child) { 13708c2ecf20Sopenharmony_ci struct charger_cable *cables; 13718c2ecf20Sopenharmony_ci struct device_node *_child; 13728c2ecf20Sopenharmony_ci 13738c2ecf20Sopenharmony_ci of_property_read_string(child, "cm-regulator-name", 13748c2ecf20Sopenharmony_ci &chg_regs->regulator_name); 13758c2ecf20Sopenharmony_ci 13768c2ecf20Sopenharmony_ci /* charger cables */ 13778c2ecf20Sopenharmony_ci chg_regs->num_cables = of_get_child_count(child); 13788c2ecf20Sopenharmony_ci if (chg_regs->num_cables) { 13798c2ecf20Sopenharmony_ci cables = devm_kcalloc(dev, 13808c2ecf20Sopenharmony_ci chg_regs->num_cables, 13818c2ecf20Sopenharmony_ci sizeof(*cables), 13828c2ecf20Sopenharmony_ci GFP_KERNEL); 13838c2ecf20Sopenharmony_ci if (!cables) { 13848c2ecf20Sopenharmony_ci of_node_put(child); 13858c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 13868c2ecf20Sopenharmony_ci } 13878c2ecf20Sopenharmony_ci 13888c2ecf20Sopenharmony_ci chg_regs->cables = cables; 13898c2ecf20Sopenharmony_ci 13908c2ecf20Sopenharmony_ci for_each_child_of_node(child, _child) { 13918c2ecf20Sopenharmony_ci of_property_read_string(_child, 13928c2ecf20Sopenharmony_ci "cm-cable-name", &cables->name); 13938c2ecf20Sopenharmony_ci of_property_read_string(_child, 13948c2ecf20Sopenharmony_ci "cm-cable-extcon", 13958c2ecf20Sopenharmony_ci &cables->extcon_name); 13968c2ecf20Sopenharmony_ci of_property_read_u32(_child, 13978c2ecf20Sopenharmony_ci "cm-cable-min", 13988c2ecf20Sopenharmony_ci &cables->min_uA); 13998c2ecf20Sopenharmony_ci of_property_read_u32(_child, 14008c2ecf20Sopenharmony_ci "cm-cable-max", 14018c2ecf20Sopenharmony_ci &cables->max_uA); 14028c2ecf20Sopenharmony_ci cables++; 14038c2ecf20Sopenharmony_ci } 14048c2ecf20Sopenharmony_ci } 14058c2ecf20Sopenharmony_ci chg_regs++; 14068c2ecf20Sopenharmony_ci } 14078c2ecf20Sopenharmony_ci } 14088c2ecf20Sopenharmony_ci return desc; 14098c2ecf20Sopenharmony_ci} 14108c2ecf20Sopenharmony_ci 14118c2ecf20Sopenharmony_cistatic inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev) 14128c2ecf20Sopenharmony_ci{ 14138c2ecf20Sopenharmony_ci if (pdev->dev.of_node) 14148c2ecf20Sopenharmony_ci return of_cm_parse_desc(&pdev->dev); 14158c2ecf20Sopenharmony_ci return dev_get_platdata(&pdev->dev); 14168c2ecf20Sopenharmony_ci} 14178c2ecf20Sopenharmony_ci 14188c2ecf20Sopenharmony_cistatic enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t now) 14198c2ecf20Sopenharmony_ci{ 14208c2ecf20Sopenharmony_ci cm_timer_set = false; 14218c2ecf20Sopenharmony_ci return ALARMTIMER_NORESTART; 14228c2ecf20Sopenharmony_ci} 14238c2ecf20Sopenharmony_ci 14248c2ecf20Sopenharmony_cistatic int charger_manager_probe(struct platform_device *pdev) 14258c2ecf20Sopenharmony_ci{ 14268c2ecf20Sopenharmony_ci struct charger_desc *desc = cm_get_drv_data(pdev); 14278c2ecf20Sopenharmony_ci struct charger_manager *cm; 14288c2ecf20Sopenharmony_ci int ret, i = 0; 14298c2ecf20Sopenharmony_ci union power_supply_propval val; 14308c2ecf20Sopenharmony_ci struct power_supply *fuel_gauge; 14318c2ecf20Sopenharmony_ci enum power_supply_property *properties; 14328c2ecf20Sopenharmony_ci size_t num_properties; 14338c2ecf20Sopenharmony_ci struct power_supply_config psy_cfg = {}; 14348c2ecf20Sopenharmony_ci 14358c2ecf20Sopenharmony_ci if (IS_ERR(desc)) { 14368c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "No platform data (desc) found\n"); 14378c2ecf20Sopenharmony_ci return PTR_ERR(desc); 14388c2ecf20Sopenharmony_ci } 14398c2ecf20Sopenharmony_ci 14408c2ecf20Sopenharmony_ci cm = devm_kzalloc(&pdev->dev, sizeof(*cm), GFP_KERNEL); 14418c2ecf20Sopenharmony_ci if (!cm) 14428c2ecf20Sopenharmony_ci return -ENOMEM; 14438c2ecf20Sopenharmony_ci 14448c2ecf20Sopenharmony_ci /* Basic Values. Unspecified are Null or 0 */ 14458c2ecf20Sopenharmony_ci cm->dev = &pdev->dev; 14468c2ecf20Sopenharmony_ci cm->desc = desc; 14478c2ecf20Sopenharmony_ci psy_cfg.drv_data = cm; 14488c2ecf20Sopenharmony_ci 14498c2ecf20Sopenharmony_ci /* Initialize alarm timer */ 14508c2ecf20Sopenharmony_ci if (alarmtimer_get_rtcdev()) { 14518c2ecf20Sopenharmony_ci cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL); 14528c2ecf20Sopenharmony_ci if (!cm_timer) 14538c2ecf20Sopenharmony_ci return -ENOMEM; 14548c2ecf20Sopenharmony_ci alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func); 14558c2ecf20Sopenharmony_ci } 14568c2ecf20Sopenharmony_ci 14578c2ecf20Sopenharmony_ci /* 14588c2ecf20Sopenharmony_ci * Some of the following do not need to be errors. 14598c2ecf20Sopenharmony_ci * Users may intentionally ignore those features. 14608c2ecf20Sopenharmony_ci */ 14618c2ecf20Sopenharmony_ci if (desc->fullbatt_uV == 0) { 14628c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n"); 14638c2ecf20Sopenharmony_ci } 14648c2ecf20Sopenharmony_ci if (!desc->fullbatt_vchkdrop_uV) { 14658c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n"); 14668c2ecf20Sopenharmony_ci desc->fullbatt_vchkdrop_uV = 0; 14678c2ecf20Sopenharmony_ci } 14688c2ecf20Sopenharmony_ci if (desc->fullbatt_soc == 0) { 14698c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n"); 14708c2ecf20Sopenharmony_ci } 14718c2ecf20Sopenharmony_ci if (desc->fullbatt_full_capacity == 0) { 14728c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n"); 14738c2ecf20Sopenharmony_ci } 14748c2ecf20Sopenharmony_ci 14758c2ecf20Sopenharmony_ci if (!desc->charger_regulators || desc->num_charger_regulators < 1) { 14768c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "charger_regulators undefined\n"); 14778c2ecf20Sopenharmony_ci return -EINVAL; 14788c2ecf20Sopenharmony_ci } 14798c2ecf20Sopenharmony_ci 14808c2ecf20Sopenharmony_ci if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) { 14818c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "No power supply defined\n"); 14828c2ecf20Sopenharmony_ci return -EINVAL; 14838c2ecf20Sopenharmony_ci } 14848c2ecf20Sopenharmony_ci 14858c2ecf20Sopenharmony_ci if (!desc->psy_fuel_gauge) { 14868c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "No fuel gauge power supply defined\n"); 14878c2ecf20Sopenharmony_ci return -EINVAL; 14888c2ecf20Sopenharmony_ci } 14898c2ecf20Sopenharmony_ci 14908c2ecf20Sopenharmony_ci /* Check if charger's supplies are present at probe */ 14918c2ecf20Sopenharmony_ci for (i = 0; desc->psy_charger_stat[i]; i++) { 14928c2ecf20Sopenharmony_ci struct power_supply *psy; 14938c2ecf20Sopenharmony_ci 14948c2ecf20Sopenharmony_ci psy = power_supply_get_by_name(desc->psy_charger_stat[i]); 14958c2ecf20Sopenharmony_ci if (!psy) { 14968c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n", 14978c2ecf20Sopenharmony_ci desc->psy_charger_stat[i]); 14988c2ecf20Sopenharmony_ci return -ENODEV; 14998c2ecf20Sopenharmony_ci } 15008c2ecf20Sopenharmony_ci power_supply_put(psy); 15018c2ecf20Sopenharmony_ci } 15028c2ecf20Sopenharmony_ci 15038c2ecf20Sopenharmony_ci if (cm->desc->polling_mode != CM_POLL_DISABLE && 15048c2ecf20Sopenharmony_ci (desc->polling_interval_ms == 0 || 15058c2ecf20Sopenharmony_ci msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL)) { 15068c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "polling_interval_ms is too small\n"); 15078c2ecf20Sopenharmony_ci return -EINVAL; 15088c2ecf20Sopenharmony_ci } 15098c2ecf20Sopenharmony_ci 15108c2ecf20Sopenharmony_ci if (!desc->charging_max_duration_ms || 15118c2ecf20Sopenharmony_ci !desc->discharging_max_duration_ms) { 15128c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n"); 15138c2ecf20Sopenharmony_ci desc->charging_max_duration_ms = 0; 15148c2ecf20Sopenharmony_ci desc->discharging_max_duration_ms = 0; 15158c2ecf20Sopenharmony_ci } 15168c2ecf20Sopenharmony_ci 15178c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, cm); 15188c2ecf20Sopenharmony_ci 15198c2ecf20Sopenharmony_ci memcpy(&cm->charger_psy_desc, &psy_default, sizeof(psy_default)); 15208c2ecf20Sopenharmony_ci 15218c2ecf20Sopenharmony_ci if (!desc->psy_name) 15228c2ecf20Sopenharmony_ci strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX); 15238c2ecf20Sopenharmony_ci else 15248c2ecf20Sopenharmony_ci strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX); 15258c2ecf20Sopenharmony_ci cm->charger_psy_desc.name = cm->psy_name_buf; 15268c2ecf20Sopenharmony_ci 15278c2ecf20Sopenharmony_ci /* Allocate for psy properties because they may vary */ 15288c2ecf20Sopenharmony_ci properties = devm_kcalloc(&pdev->dev, 15298c2ecf20Sopenharmony_ci ARRAY_SIZE(default_charger_props) + 15308c2ecf20Sopenharmony_ci NUM_CHARGER_PSY_OPTIONAL, 15318c2ecf20Sopenharmony_ci sizeof(*properties), GFP_KERNEL); 15328c2ecf20Sopenharmony_ci if (!properties) 15338c2ecf20Sopenharmony_ci return -ENOMEM; 15348c2ecf20Sopenharmony_ci 15358c2ecf20Sopenharmony_ci memcpy(properties, default_charger_props, 15368c2ecf20Sopenharmony_ci sizeof(enum power_supply_property) * 15378c2ecf20Sopenharmony_ci ARRAY_SIZE(default_charger_props)); 15388c2ecf20Sopenharmony_ci num_properties = ARRAY_SIZE(default_charger_props); 15398c2ecf20Sopenharmony_ci 15408c2ecf20Sopenharmony_ci /* Find which optional psy-properties are available */ 15418c2ecf20Sopenharmony_ci fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge); 15428c2ecf20Sopenharmony_ci if (!fuel_gauge) { 15438c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n", 15448c2ecf20Sopenharmony_ci desc->psy_fuel_gauge); 15458c2ecf20Sopenharmony_ci return -ENODEV; 15468c2ecf20Sopenharmony_ci } 15478c2ecf20Sopenharmony_ci if (!power_supply_get_property(fuel_gauge, 15488c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_FULL, &val)) { 15498c2ecf20Sopenharmony_ci properties[num_properties] = 15508c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_FULL; 15518c2ecf20Sopenharmony_ci num_properties++; 15528c2ecf20Sopenharmony_ci } 15538c2ecf20Sopenharmony_ci if (!power_supply_get_property(fuel_gauge, 15548c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_NOW, &val)) { 15558c2ecf20Sopenharmony_ci properties[num_properties] = 15568c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_NOW; 15578c2ecf20Sopenharmony_ci num_properties++; 15588c2ecf20Sopenharmony_ci } 15598c2ecf20Sopenharmony_ci if (!power_supply_get_property(fuel_gauge, 15608c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_NOW, 15618c2ecf20Sopenharmony_ci &val)) { 15628c2ecf20Sopenharmony_ci properties[num_properties] = 15638c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_NOW; 15648c2ecf20Sopenharmony_ci num_properties++; 15658c2ecf20Sopenharmony_ci } 15668c2ecf20Sopenharmony_ci 15678c2ecf20Sopenharmony_ci ret = cm_init_thermal_data(cm, fuel_gauge, properties, &num_properties); 15688c2ecf20Sopenharmony_ci if (ret) { 15698c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to initialize thermal data\n"); 15708c2ecf20Sopenharmony_ci cm->desc->measure_battery_temp = false; 15718c2ecf20Sopenharmony_ci } 15728c2ecf20Sopenharmony_ci power_supply_put(fuel_gauge); 15738c2ecf20Sopenharmony_ci 15748c2ecf20Sopenharmony_ci cm->charger_psy_desc.properties = properties; 15758c2ecf20Sopenharmony_ci cm->charger_psy_desc.num_properties = num_properties; 15768c2ecf20Sopenharmony_ci 15778c2ecf20Sopenharmony_ci /* Register sysfs entry for charger(regulator) */ 15788c2ecf20Sopenharmony_ci ret = charger_manager_prepare_sysfs(cm); 15798c2ecf20Sopenharmony_ci if (ret < 0) { 15808c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 15818c2ecf20Sopenharmony_ci "Cannot prepare sysfs entry of regulators\n"); 15828c2ecf20Sopenharmony_ci return ret; 15838c2ecf20Sopenharmony_ci } 15848c2ecf20Sopenharmony_ci psy_cfg.attr_grp = desc->sysfs_groups; 15858c2ecf20Sopenharmony_ci 15868c2ecf20Sopenharmony_ci cm->charger_psy = power_supply_register(&pdev->dev, 15878c2ecf20Sopenharmony_ci &cm->charger_psy_desc, 15888c2ecf20Sopenharmony_ci &psy_cfg); 15898c2ecf20Sopenharmony_ci if (IS_ERR(cm->charger_psy)) { 15908c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n", 15918c2ecf20Sopenharmony_ci cm->charger_psy_desc.name); 15928c2ecf20Sopenharmony_ci return PTR_ERR(cm->charger_psy); 15938c2ecf20Sopenharmony_ci } 15948c2ecf20Sopenharmony_ci 15958c2ecf20Sopenharmony_ci /* Register extcon device for charger cable */ 15968c2ecf20Sopenharmony_ci ret = charger_manager_register_extcon(cm); 15978c2ecf20Sopenharmony_ci if (ret < 0) { 15988c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Cannot initialize extcon device\n"); 15998c2ecf20Sopenharmony_ci goto err_reg_extcon; 16008c2ecf20Sopenharmony_ci } 16018c2ecf20Sopenharmony_ci 16028c2ecf20Sopenharmony_ci /* Add to the list */ 16038c2ecf20Sopenharmony_ci mutex_lock(&cm_list_mtx); 16048c2ecf20Sopenharmony_ci list_add(&cm->entry, &cm_list); 16058c2ecf20Sopenharmony_ci mutex_unlock(&cm_list_mtx); 16068c2ecf20Sopenharmony_ci 16078c2ecf20Sopenharmony_ci /* 16088c2ecf20Sopenharmony_ci * Charger-manager is capable of waking up the systme from sleep 16098c2ecf20Sopenharmony_ci * when event is happened through cm_notify_event() 16108c2ecf20Sopenharmony_ci */ 16118c2ecf20Sopenharmony_ci device_init_wakeup(&pdev->dev, true); 16128c2ecf20Sopenharmony_ci device_set_wakeup_capable(&pdev->dev, false); 16138c2ecf20Sopenharmony_ci 16148c2ecf20Sopenharmony_ci /* 16158c2ecf20Sopenharmony_ci * Charger-manager have to check the charging state right after 16168c2ecf20Sopenharmony_ci * initialization of charger-manager and then update current charging 16178c2ecf20Sopenharmony_ci * state. 16188c2ecf20Sopenharmony_ci */ 16198c2ecf20Sopenharmony_ci cm_monitor(); 16208c2ecf20Sopenharmony_ci 16218c2ecf20Sopenharmony_ci schedule_work(&setup_polling); 16228c2ecf20Sopenharmony_ci 16238c2ecf20Sopenharmony_ci return 0; 16248c2ecf20Sopenharmony_ci 16258c2ecf20Sopenharmony_cierr_reg_extcon: 16268c2ecf20Sopenharmony_ci for (i = 0; i < desc->num_charger_regulators; i++) 16278c2ecf20Sopenharmony_ci regulator_put(desc->charger_regulators[i].consumer); 16288c2ecf20Sopenharmony_ci 16298c2ecf20Sopenharmony_ci power_supply_unregister(cm->charger_psy); 16308c2ecf20Sopenharmony_ci 16318c2ecf20Sopenharmony_ci return ret; 16328c2ecf20Sopenharmony_ci} 16338c2ecf20Sopenharmony_ci 16348c2ecf20Sopenharmony_cistatic int charger_manager_remove(struct platform_device *pdev) 16358c2ecf20Sopenharmony_ci{ 16368c2ecf20Sopenharmony_ci struct charger_manager *cm = platform_get_drvdata(pdev); 16378c2ecf20Sopenharmony_ci struct charger_desc *desc = cm->desc; 16388c2ecf20Sopenharmony_ci int i = 0; 16398c2ecf20Sopenharmony_ci 16408c2ecf20Sopenharmony_ci /* Remove from the list */ 16418c2ecf20Sopenharmony_ci mutex_lock(&cm_list_mtx); 16428c2ecf20Sopenharmony_ci list_del(&cm->entry); 16438c2ecf20Sopenharmony_ci mutex_unlock(&cm_list_mtx); 16448c2ecf20Sopenharmony_ci 16458c2ecf20Sopenharmony_ci cancel_work_sync(&setup_polling); 16468c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&cm_monitor_work); 16478c2ecf20Sopenharmony_ci 16488c2ecf20Sopenharmony_ci for (i = 0 ; i < desc->num_charger_regulators ; i++) 16498c2ecf20Sopenharmony_ci regulator_put(desc->charger_regulators[i].consumer); 16508c2ecf20Sopenharmony_ci 16518c2ecf20Sopenharmony_ci power_supply_unregister(cm->charger_psy); 16528c2ecf20Sopenharmony_ci 16538c2ecf20Sopenharmony_ci try_charger_enable(cm, false); 16548c2ecf20Sopenharmony_ci 16558c2ecf20Sopenharmony_ci return 0; 16568c2ecf20Sopenharmony_ci} 16578c2ecf20Sopenharmony_ci 16588c2ecf20Sopenharmony_cistatic const struct platform_device_id charger_manager_id[] = { 16598c2ecf20Sopenharmony_ci { "charger-manager", 0 }, 16608c2ecf20Sopenharmony_ci { }, 16618c2ecf20Sopenharmony_ci}; 16628c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, charger_manager_id); 16638c2ecf20Sopenharmony_ci 16648c2ecf20Sopenharmony_cistatic int cm_suspend_noirq(struct device *dev) 16658c2ecf20Sopenharmony_ci{ 16668c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) { 16678c2ecf20Sopenharmony_ci device_set_wakeup_capable(dev, false); 16688c2ecf20Sopenharmony_ci return -EAGAIN; 16698c2ecf20Sopenharmony_ci } 16708c2ecf20Sopenharmony_ci 16718c2ecf20Sopenharmony_ci return 0; 16728c2ecf20Sopenharmony_ci} 16738c2ecf20Sopenharmony_ci 16748c2ecf20Sopenharmony_cistatic bool cm_need_to_awake(void) 16758c2ecf20Sopenharmony_ci{ 16768c2ecf20Sopenharmony_ci struct charger_manager *cm; 16778c2ecf20Sopenharmony_ci 16788c2ecf20Sopenharmony_ci if (cm_timer) 16798c2ecf20Sopenharmony_ci return false; 16808c2ecf20Sopenharmony_ci 16818c2ecf20Sopenharmony_ci mutex_lock(&cm_list_mtx); 16828c2ecf20Sopenharmony_ci list_for_each_entry(cm, &cm_list, entry) { 16838c2ecf20Sopenharmony_ci if (is_charging(cm)) { 16848c2ecf20Sopenharmony_ci mutex_unlock(&cm_list_mtx); 16858c2ecf20Sopenharmony_ci return true; 16868c2ecf20Sopenharmony_ci } 16878c2ecf20Sopenharmony_ci } 16888c2ecf20Sopenharmony_ci mutex_unlock(&cm_list_mtx); 16898c2ecf20Sopenharmony_ci 16908c2ecf20Sopenharmony_ci return false; 16918c2ecf20Sopenharmony_ci} 16928c2ecf20Sopenharmony_ci 16938c2ecf20Sopenharmony_cistatic int cm_suspend_prepare(struct device *dev) 16948c2ecf20Sopenharmony_ci{ 16958c2ecf20Sopenharmony_ci if (cm_need_to_awake()) 16968c2ecf20Sopenharmony_ci return -EBUSY; 16978c2ecf20Sopenharmony_ci 16988c2ecf20Sopenharmony_ci if (!cm_suspended) 16998c2ecf20Sopenharmony_ci cm_suspended = true; 17008c2ecf20Sopenharmony_ci 17018c2ecf20Sopenharmony_ci cm_timer_set = cm_setup_timer(); 17028c2ecf20Sopenharmony_ci 17038c2ecf20Sopenharmony_ci if (cm_timer_set) { 17048c2ecf20Sopenharmony_ci cancel_work_sync(&setup_polling); 17058c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&cm_monitor_work); 17068c2ecf20Sopenharmony_ci } 17078c2ecf20Sopenharmony_ci 17088c2ecf20Sopenharmony_ci return 0; 17098c2ecf20Sopenharmony_ci} 17108c2ecf20Sopenharmony_ci 17118c2ecf20Sopenharmony_cistatic void cm_suspend_complete(struct device *dev) 17128c2ecf20Sopenharmony_ci{ 17138c2ecf20Sopenharmony_ci struct charger_manager *cm = dev_get_drvdata(dev); 17148c2ecf20Sopenharmony_ci 17158c2ecf20Sopenharmony_ci if (cm_suspended) 17168c2ecf20Sopenharmony_ci cm_suspended = false; 17178c2ecf20Sopenharmony_ci 17188c2ecf20Sopenharmony_ci if (cm_timer_set) { 17198c2ecf20Sopenharmony_ci ktime_t remain; 17208c2ecf20Sopenharmony_ci 17218c2ecf20Sopenharmony_ci alarm_cancel(cm_timer); 17228c2ecf20Sopenharmony_ci cm_timer_set = false; 17238c2ecf20Sopenharmony_ci remain = alarm_expires_remaining(cm_timer); 17248c2ecf20Sopenharmony_ci cm_suspend_duration_ms -= ktime_to_ms(remain); 17258c2ecf20Sopenharmony_ci schedule_work(&setup_polling); 17268c2ecf20Sopenharmony_ci } 17278c2ecf20Sopenharmony_ci 17288c2ecf20Sopenharmony_ci _cm_monitor(cm); 17298c2ecf20Sopenharmony_ci 17308c2ecf20Sopenharmony_ci device_set_wakeup_capable(cm->dev, false); 17318c2ecf20Sopenharmony_ci} 17328c2ecf20Sopenharmony_ci 17338c2ecf20Sopenharmony_cistatic const struct dev_pm_ops charger_manager_pm = { 17348c2ecf20Sopenharmony_ci .prepare = cm_suspend_prepare, 17358c2ecf20Sopenharmony_ci .suspend_noirq = cm_suspend_noirq, 17368c2ecf20Sopenharmony_ci .complete = cm_suspend_complete, 17378c2ecf20Sopenharmony_ci}; 17388c2ecf20Sopenharmony_ci 17398c2ecf20Sopenharmony_cistatic struct platform_driver charger_manager_driver = { 17408c2ecf20Sopenharmony_ci .driver = { 17418c2ecf20Sopenharmony_ci .name = "charger-manager", 17428c2ecf20Sopenharmony_ci .pm = &charger_manager_pm, 17438c2ecf20Sopenharmony_ci .of_match_table = charger_manager_match, 17448c2ecf20Sopenharmony_ci }, 17458c2ecf20Sopenharmony_ci .probe = charger_manager_probe, 17468c2ecf20Sopenharmony_ci .remove = charger_manager_remove, 17478c2ecf20Sopenharmony_ci .id_table = charger_manager_id, 17488c2ecf20Sopenharmony_ci}; 17498c2ecf20Sopenharmony_ci 17508c2ecf20Sopenharmony_cistatic int __init charger_manager_init(void) 17518c2ecf20Sopenharmony_ci{ 17528c2ecf20Sopenharmony_ci cm_wq = create_freezable_workqueue("charger_manager"); 17538c2ecf20Sopenharmony_ci if (unlikely(!cm_wq)) 17548c2ecf20Sopenharmony_ci return -ENOMEM; 17558c2ecf20Sopenharmony_ci 17568c2ecf20Sopenharmony_ci INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller); 17578c2ecf20Sopenharmony_ci 17588c2ecf20Sopenharmony_ci return platform_driver_register(&charger_manager_driver); 17598c2ecf20Sopenharmony_ci} 17608c2ecf20Sopenharmony_cilate_initcall(charger_manager_init); 17618c2ecf20Sopenharmony_ci 17628c2ecf20Sopenharmony_cistatic void __exit charger_manager_cleanup(void) 17638c2ecf20Sopenharmony_ci{ 17648c2ecf20Sopenharmony_ci destroy_workqueue(cm_wq); 17658c2ecf20Sopenharmony_ci cm_wq = NULL; 17668c2ecf20Sopenharmony_ci 17678c2ecf20Sopenharmony_ci platform_driver_unregister(&charger_manager_driver); 17688c2ecf20Sopenharmony_ci} 17698c2ecf20Sopenharmony_cimodule_exit(charger_manager_cleanup); 17708c2ecf20Sopenharmony_ci 17718c2ecf20Sopenharmony_ciMODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 17728c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Charger Manager"); 17738c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1774