^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2011 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * MyungJoo Ham <myungjoo.ham@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This driver enables to monitor battery health and control charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * during suspend-to-mem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Charger manager depends on other devices. Register this later than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * the depending devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/power/charger-manager.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u64 extcon_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) } extcon_mapping[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Current textual representations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) { "USB", EXTCON_USB },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) { "USB-HOST", EXTCON_USB_HOST },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) { "SDP", EXTCON_CHG_USB_SDP },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) { "DCP", EXTCON_CHG_USB_DCP },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) { "CDP", EXTCON_CHG_USB_CDP },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) { "ACA", EXTCON_CHG_USB_ACA },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) { "FAST-CHARGER", EXTCON_CHG_USB_FAST },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) { "SLOW-CHARGER", EXTCON_CHG_USB_SLOW },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) { "WPT", EXTCON_CHG_WPT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) { "PD", EXTCON_CHG_USB_PD },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) { "DOCK", EXTCON_DOCK },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) { "JIG", EXTCON_JIG },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) { "MECHANICAL", EXTCON_MECHANICAL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Deprecated textual representations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) { "TA", EXTCON_CHG_USB_SDP },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { "CHARGE-DOWNSTREAM", EXTCON_CHG_USB_CDP },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Default temperature threshold for charging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Every temperature units are in tenth of centigrade.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define CM_DEFAULT_RECHARGE_TEMP_DIFF 50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define CM_DEFAULT_CHARGE_TEMP_MAX 500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * without any delays.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define CM_JIFFIES_SMALL (2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* If y is valid (> 0) and smaller than x, do x = y */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * rtc alarm. It should be 2 or larger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define CM_RTC_SMALL (2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static LIST_HEAD(cm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static DEFINE_MUTEX(cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* About in-suspend (suspend-again) monitoring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static struct alarm *cm_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static bool cm_suspended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static bool cm_timer_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static unsigned long cm_suspend_duration_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* About normal (not suspended) monitoring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static unsigned long next_polling; /* Next appointed polling time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static struct workqueue_struct *cm_wq; /* init at driver add */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static struct delayed_work cm_monitor_work; /* init at driver add */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * is_batt_present - See if the battery presents in place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static bool is_batt_present(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct power_supply *psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) bool present = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) switch (cm->desc->battery_present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) case CM_BATTERY_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) present = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) case CM_NO_BATTERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) case CM_FUEL_GAUGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) psy = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (ret == 0 && val.intval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) present = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) power_supply_put(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) case CM_CHARGER_STAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) psy = power_supply_get_by_name(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) cm->desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) cm->desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = power_supply_get_property(psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) POWER_SUPPLY_PROP_PRESENT, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) power_supply_put(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (ret == 0 && val.intval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) present = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * is_ext_pwr_online - See if an external power source is attached to charge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * Returns true if at least one of the chargers of the battery has an external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * power source attached to charge the battery regardless of whether it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * actually charging or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static bool is_ext_pwr_online(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct power_supply *psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) bool online = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* If at least one of them has one, it's yes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) cm->desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) power_supply_put(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (ret == 0 && val.intval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) online = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * get_batt_uV - Get the voltage level of the battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * @uV: the voltage level returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * Returns 0 if there is no error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * Returns a negative value on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int get_batt_uV(struct charger_manager *cm, int *uV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct power_supply *fuel_gauge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!fuel_gauge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret = power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) power_supply_put(fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) *uV = val.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * is_charging - Returns true if the battery is being charged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static bool is_charging(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) bool charging = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct power_supply *psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /* If there is no battery, it cannot be charged */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (!is_batt_present(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /* If at least one of the charger is charging, return yes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* 1. The charger sholuld not be DISABLED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (cm->emergency_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!cm->charger_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) cm->desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* 2. The charger should be online (ext-power) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) cm->desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) power_supply_put(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (val.intval == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) power_supply_put(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * 3. The charger should not be FULL, DISCHARGING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * or NOT_CHARGING.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) power_supply_put(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) cm->desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (val.intval == POWER_SUPPLY_STATUS_FULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* Then, this is charging. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) charging = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return charging;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * is_full_charged - Returns true if the battery is fully charged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static bool is_full_charged(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct power_supply *fuel_gauge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) bool is_full = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) int uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /* If there is no battery, it cannot be charged */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!is_batt_present(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (!fuel_gauge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* Full, if it's over the fullbatt voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (desc->fullbatt_uV > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) ret = get_batt_uV(cm, &uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* Battery is already full, checks voltage drop. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (cm->battery_status == POWER_SUPPLY_STATUS_FULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) && desc->fullbatt_vchkdrop_uV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) uV += desc->fullbatt_vchkdrop_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (uV >= desc->fullbatt_uV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (desc->fullbatt_full_capacity > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) val.intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* Not full if capacity of fuel gauge isn't full */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) POWER_SUPPLY_PROP_CHARGE_FULL, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!ret && val.intval > desc->fullbatt_full_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) is_full = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /* Full, if the capacity is more than fullbatt_soc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (desc->fullbatt_soc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) val.intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) ret = power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) POWER_SUPPLY_PROP_CAPACITY, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (!ret && val.intval >= desc->fullbatt_soc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) is_full = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) power_supply_put(fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return is_full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * is_polling_required - Return true if need to continue polling for this CM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static bool is_polling_required(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) switch (cm->desc->polling_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) case CM_POLL_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) case CM_POLL_ALWAYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) case CM_POLL_EXTERNAL_POWER_ONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return is_ext_pwr_online(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) case CM_POLL_CHARGING_ONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return is_charging(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) cm->desc->polling_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * try_charger_enable - Enable/Disable chargers altogether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * @enable: true: enable / false: disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * Note that Charger Manager keeps the charger enabled regardless whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * the charger is charging or not (because battery is full or no external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * power source exists) except when CM needs to disable chargers forcibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * because of emergency causes; when the battery is overheated or too cold.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int try_charger_enable(struct charger_manager *cm, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int err = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* Ignore if it's redundant command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (enable == cm->charger_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (cm->emergency_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * Save start time of charging to limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * maximum possible charging time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) cm->charging_start_time = ktime_to_ms(ktime_get());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) cm->charging_end_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) for (i = 0 ; i < desc->num_charger_regulators ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (desc->charger_regulators[i].externally_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) err = regulator_enable(desc->charger_regulators[i].consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) dev_warn(cm->dev, "Cannot enable %s regulator\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) desc->charger_regulators[i].regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * Save end time of charging to maintain fully charged state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * of battery after full-batt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) cm->charging_start_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) cm->charging_end_time = ktime_to_ms(ktime_get());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) for (i = 0 ; i < desc->num_charger_regulators ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (desc->charger_regulators[i].externally_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) err = regulator_disable(desc->charger_regulators[i].consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) dev_warn(cm->dev, "Cannot disable %s regulator\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) desc->charger_regulators[i].regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * Abnormal battery state - Stop charging forcibly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * even if charger was enabled at the other places
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) for (i = 0; i < desc->num_charger_regulators; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (regulator_is_enabled(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) desc->charger_regulators[i].consumer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) regulator_force_disable(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) desc->charger_regulators[i].consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) desc->charger_regulators[i].regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) cm->charger_enabled = enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * check_charging_duration - Monitor charging/discharging duration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * If whole charging duration exceed 'charging_max_duration_ms',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * cm stop charging to prevent overcharge/overheat. If discharging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * duration exceed 'discharging _max_duration_ms', charger cable is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * attached, after full-batt, cm start charging to maintain fully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * charged state for battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static int check_charging_duration(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) u64 curr = ktime_to_ms(ktime_get());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) u64 duration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) int ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (!desc->charging_max_duration_ms &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) !desc->discharging_max_duration_ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (cm->charger_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) duration = curr - cm->charging_start_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (duration > desc->charging_max_duration_ms) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) dev_info(cm->dev, "Charging duration exceed %ums\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) desc->charging_max_duration_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) } else if (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) duration = curr - cm->charging_end_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (duration > desc->discharging_max_duration_ms) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) dev_info(cm->dev, "Discharging duration exceed %ums\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) desc->discharging_max_duration_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static int cm_get_battery_temperature_by_psy(struct charger_manager *cm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct power_supply *fuel_gauge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (!fuel_gauge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) ret = power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) (union power_supply_propval *)temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) power_supply_put(fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) static int cm_get_battery_temperature(struct charger_manager *cm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (!cm->desc->measure_battery_temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) #ifdef CONFIG_THERMAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (cm->tzd_batt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ret = thermal_zone_get_temp(cm->tzd_batt, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /* Calibrate temperature unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) *temp /= 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /* if-else continued from CONFIG_THERMAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) ret = cm_get_battery_temperature_by_psy(cm, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static int cm_check_thermal_status(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) int temp, upper_limit, lower_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) ret = cm_get_battery_temperature(cm, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /* FIXME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * No information of battery temperature might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * occur hazardous result. We have to handle it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * depending on battery type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) dev_err(cm->dev, "Failed to get battery temperature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) upper_limit = desc->temp_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) lower_limit = desc->temp_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (cm->emergency_stop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) upper_limit -= desc->temp_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) lower_limit += desc->temp_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (temp > upper_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) ret = CM_BATT_OVERHEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) else if (temp < lower_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) ret = CM_BATT_COLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) ret = CM_BATT_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) cm->emergency_stop = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * cm_get_target_status - Check current status and get next target status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) static int cm_get_target_status(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (!is_ext_pwr_online(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (cm_check_thermal_status(cm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /* Check if discharging duration exeeds limit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (check_charging_duration(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) goto charging_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) switch (cm->battery_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) case POWER_SUPPLY_STATUS_CHARGING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) /* Check if charging duration exeeds limit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (check_charging_duration(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) case POWER_SUPPLY_STATUS_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (is_full_charged(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) charging_ok:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) /* Charging is allowed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * _cm_monitor - Monitor the temperature and return true for exceptions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * Returns true if there is an event to notify for the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * (True if the status of "emergency_stop" changes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) static bool _cm_monitor(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) int target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) target = cm_get_target_status(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) try_charger_enable(cm, (target == POWER_SUPPLY_STATUS_CHARGING));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (cm->battery_status != target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) cm->battery_status = target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) power_supply_changed(cm->charger_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * cm_monitor - Monitor every battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * Returns true if there is an event to notify from any of the batteries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * (True if the status of "emergency_stop" changes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static bool cm_monitor(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) bool stop = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) struct charger_manager *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) mutex_lock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) list_for_each_entry(cm, &cm_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (_cm_monitor(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) stop = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) mutex_unlock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * _setup_polling - Setup the next instance of polling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * @work: work_struct of the function _setup_polling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static void _setup_polling(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) unsigned long min = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) struct charger_manager *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) bool keep_polling = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) unsigned long _next_polling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) mutex_lock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) list_for_each_entry(cm, &cm_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) keep_polling = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (min > cm->desc->polling_interval_ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) min = cm->desc->polling_interval_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) polling_jiffy = msecs_to_jiffies(min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (polling_jiffy <= CM_JIFFIES_SMALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) polling_jiffy = CM_JIFFIES_SMALL + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (!keep_polling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) polling_jiffy = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (polling_jiffy == ULONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) ". try it later. %s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * Use mod_delayed_work() iff the next polling interval should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * occur before the currently scheduled one. If @cm_monitor_work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * isn't active, the end result is the same, so no need to worry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * about stale @next_polling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) _next_polling = jiffies + polling_jiffy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (time_before(_next_polling, next_polling)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) next_polling = _next_polling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) next_polling = _next_polling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) mutex_unlock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) static DECLARE_WORK(setup_polling, _setup_polling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * cm_monitor_poller - The Monitor / Poller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * @work: work_struct of the function cm_monitor_poller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * During non-suspended state, cm_monitor_poller is used to poll and monitor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * the batteries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static void cm_monitor_poller(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) cm_monitor();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) schedule_work(&setup_polling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) static int charger_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct charger_manager *cm = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct power_supply *fuel_gauge = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) int uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) val->intval = cm->battery_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (cm->emergency_stop > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) else if (cm->emergency_stop < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) val->intval = POWER_SUPPLY_HEALTH_COLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) val->intval = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (is_batt_present(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) val->intval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) ret = get_batt_uV(cm, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!fuel_gauge) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) ret = power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) POWER_SUPPLY_PROP_CURRENT_NOW, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return cm_get_battery_temperature(cm, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) if (!is_batt_present(cm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) /* There is no battery. Assume 100% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) val->intval = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (!fuel_gauge) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) ret = power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) POWER_SUPPLY_PROP_CAPACITY, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (val->intval > 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) val->intval = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (val->intval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) /* Do not adjust SOC when charging: voltage is overrated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) if (is_charging(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * If the capacity value is inconsistent, calibrate it base on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * the battery voltage values and the thresholds given as desc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) ret = get_batt_uV(cm, &uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) /* Voltage information not available. No calibration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) !is_charging(cm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) val->intval = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (is_ext_pwr_online(cm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) val->intval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) case POWER_SUPPLY_PROP_CHARGE_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) case POWER_SUPPLY_PROP_CHARGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (!fuel_gauge) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) ret = power_supply_get_property(fuel_gauge, psp, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (fuel_gauge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) power_supply_put(fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) #define NUM_CHARGER_PSY_OPTIONAL (4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) static enum power_supply_property default_charger_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) /* Guaranteed to provide */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * Optional properties are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * POWER_SUPPLY_PROP_CHARGE_FULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * POWER_SUPPLY_PROP_CHARGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) static const struct power_supply_desc psy_default = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) .name = "battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) .type = POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) .properties = default_charger_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) .num_properties = ARRAY_SIZE(default_charger_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) .get_property = charger_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) .no_thermal = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) * for suspend_again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * Returns true if the alarm is set for Charger Manager to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * Returns false if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * cm_setup_timer fails to set an alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * cm_setup_timer does not need to set an alarm for Charger Manager,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * or an alarm previously configured is to be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) static bool cm_setup_timer(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) struct charger_manager *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) unsigned int wakeup_ms = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) int timer_req = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (time_after(next_polling, jiffies))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) CM_MIN_VALID(wakeup_ms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) jiffies_to_msecs(next_polling - jiffies));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) mutex_lock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) list_for_each_entry(cm, &cm_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) /* Skip if polling is not required for this CM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (!is_polling_required(cm) && !cm->emergency_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) timer_req++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (cm->desc->polling_interval_ms == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) mutex_unlock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (timer_req && cm_timer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) ktime_t now, add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * Set alarm with the polling interval (wakeup_ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * The alarm time should be NOW + CM_RTC_SMALL or later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (wakeup_ms == UINT_MAX ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) now = ktime_get_boottime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) add = ktime_set(wakeup_ms / MSEC_PER_SEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) (wakeup_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) alarm_start(cm_timer, ktime_add(now, add));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) cm_suspend_duration_ms = wakeup_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) * charger_extcon_work - enable/diable charger according to the state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) * of charger cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) * @work: work_struct of the function charger_extcon_work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) static void charger_extcon_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) struct charger_cable *cable =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) container_of(work, struct charger_cable, wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) ret = regulator_set_current_limit(cable->charger->consumer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) cable->min_uA, cable->max_uA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) pr_err("Cannot set current limit of %s (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) cable->charger->regulator_name, cable->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) pr_info("Set current limit of %s : %duA ~ %duA\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) cable->charger->regulator_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) cable->min_uA, cable->max_uA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) cancel_delayed_work(&cm_monitor_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) queue_delayed_work(cm_wq, &cm_monitor_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * charger_extcon_notifier - receive the state of charger cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * when registered cable is attached or detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) * @self: the notifier block of the charger_extcon_notifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * @event: the cable state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * @ptr: the data pointer of notifier block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) static int charger_extcon_notifier(struct notifier_block *self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) struct charger_cable *cable =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) container_of(self, struct charger_cable, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * The newly state of charger cable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * If cable is attached, cable->attached is true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) cable->attached = event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) * Setup work for controlling charger(regulator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) * according to charger cable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) schedule_work(&cable->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * charger_extcon_init - register external connector to use it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * as the charger cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) * @cable: the Charger cable representing the external connector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) static int charger_extcon_init(struct charger_manager *cm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) struct charger_cable *cable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) u64 extcon_type = EXTCON_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) * Charger manager use Extcon framework to identify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * the charger cable among various external connector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * cable (e.g., TA, USB, MHL, Dock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) INIT_WORK(&cable->wq, charger_extcon_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) cable->nb.notifier_call = charger_extcon_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) cable->extcon_dev = extcon_get_extcon_dev(cable->extcon_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (IS_ERR_OR_NULL(cable->extcon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) pr_err("Cannot find extcon_dev for %s (cable: %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) cable->extcon_name, cable->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (cable->extcon_dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) return PTR_ERR(cable->extcon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) for (i = 0; i < ARRAY_SIZE(extcon_mapping); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if (!strcmp(cable->name, extcon_mapping[i].name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) extcon_type = extcon_mapping[i].extcon_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) if (extcon_type == EXTCON_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) pr_err("Cannot find cable for type %s", cable->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) cable->extcon_type = extcon_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) ret = devm_extcon_register_notifier(cm->dev, cable->extcon_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) cable->extcon_type, &cable->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) pr_err("Cannot register extcon_dev for %s (cable: %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) cable->extcon_name, cable->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * charger_manager_register_extcon - Register extcon device to receive state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * of charger cable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * This function support EXTCON(External Connector) subsystem to detect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * state of charger cables for enabling or disabling charger(regulator) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * select the charger cable for charging among a number of external cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * according to policy of H/W board.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) static int charger_manager_register_extcon(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) struct charger_regulator *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) unsigned long event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) for (i = 0; i < desc->num_charger_regulators; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) charger = &desc->charger_regulators[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) charger->consumer = regulator_get(cm->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) charger->regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (IS_ERR(charger->consumer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) dev_err(cm->dev, "Cannot find charger(%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) charger->regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) return PTR_ERR(charger->consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) charger->cm = cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) for (j = 0; j < charger->num_cables; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) struct charger_cable *cable = &charger->cables[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) ret = charger_extcon_init(cm, cable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) dev_err(cm->dev, "Cannot initialize charger(%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) charger->regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) cable->charger = charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) cable->cm = cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) event = extcon_get_state(cable->extcon_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) cable->extcon_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) charger_extcon_notifier(&cable->nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) event, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) /* help function of sysfs node to control charger(regulator) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) static ssize_t charger_name_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) struct charger_regulator *charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) = container_of(attr, struct charger_regulator, attr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) return sprintf(buf, "%s\n", charger->regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) static ssize_t charger_state_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) struct charger_regulator *charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) = container_of(attr, struct charger_regulator, attr_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) int state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) if (!charger->externally_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) state = regulator_is_enabled(charger->consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) static ssize_t charger_externally_control_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) struct charger_regulator *charger = container_of(attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) struct charger_regulator, attr_externally_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) return sprintf(buf, "%d\n", charger->externally_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) static ssize_t charger_externally_control_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) struct charger_regulator *charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) = container_of(attr, struct charger_regulator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) attr_externally_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) struct charger_manager *cm = charger->cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) int externally_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) int chargers_externally_control = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) ret = sscanf(buf, "%d", &externally_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) if (!externally_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) charger->externally_control = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) for (i = 0; i < desc->num_charger_regulators; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) if (&desc->charger_regulators[i] != charger &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) !desc->charger_regulators[i].externally_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) * At least, one charger is controlled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) * charger-manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) chargers_externally_control = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (!chargers_externally_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) if (cm->charger_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) try_charger_enable(charger->cm, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) charger->externally_control = externally_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) try_charger_enable(charger->cm, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) charger->externally_control = externally_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) dev_warn(cm->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) charger->regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * charger_manager_prepare_sysfs - Prepare sysfs entry for each charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * @cm: the Charger Manager representing the battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * This function add sysfs entry for charger(regulator) to control charger from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * user-space. If some development board use one more chargers for charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * but only need one charger on specific case which is dependent on user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * class/power_supply/battery/charger.[index]/externally_control'. For example,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) * externally_control, this charger isn't controlled from charger-manager and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * always stay off state of regulator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) static int charger_manager_prepare_sysfs(struct charger_manager *cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) struct charger_regulator *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) int chargers_externally_control = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) /* Create sysfs entry to control charger(regulator) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) for (i = 0; i < desc->num_charger_regulators; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) charger = &desc->charger_regulators[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) name = devm_kasprintf(cm->dev, GFP_KERNEL, "charger.%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) charger->attrs[0] = &charger->attr_name.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) charger->attrs[1] = &charger->attr_state.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) charger->attrs[2] = &charger->attr_externally_control.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) charger->attrs[3] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) charger->attr_grp.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) charger->attr_grp.attrs = charger->attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) desc->sysfs_groups[i] = &charger->attr_grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) sysfs_attr_init(&charger->attr_name.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) charger->attr_name.attr.name = "name";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) charger->attr_name.attr.mode = 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) charger->attr_name.show = charger_name_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) sysfs_attr_init(&charger->attr_state.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) charger->attr_state.attr.name = "state";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) charger->attr_state.attr.mode = 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) charger->attr_state.show = charger_state_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) sysfs_attr_init(&charger->attr_externally_control.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) charger->attr_externally_control.attr.name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) = "externally_control";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) charger->attr_externally_control.attr.mode = 0644;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) charger->attr_externally_control.show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) = charger_externally_control_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) charger->attr_externally_control.store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) = charger_externally_control_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) if (!desc->charger_regulators[i].externally_control ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) !chargers_externally_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) chargers_externally_control = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) charger->regulator_name, charger->externally_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) if (chargers_externally_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) static int cm_init_thermal_data(struct charger_manager *cm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) struct power_supply *fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) enum power_supply_property *properties,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) size_t *num_properties)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) /* Verify whether fuel gauge provides battery temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) ret = power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) POWER_SUPPLY_PROP_TEMP, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) properties[*num_properties] = POWER_SUPPLY_PROP_TEMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) (*num_properties)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) cm->desc->measure_battery_temp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) #ifdef CONFIG_THERMAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) if (ret && desc->thermal_zone) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) cm->tzd_batt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) thermal_zone_get_zone_by_name(desc->thermal_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) if (IS_ERR(cm->tzd_batt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return PTR_ERR(cm->tzd_batt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) /* Use external thermometer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) properties[*num_properties] = POWER_SUPPLY_PROP_TEMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) (*num_properties)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) cm->desc->measure_battery_temp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) if (cm->desc->measure_battery_temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) /* NOTICE : Default allowable minimum charge temperature is 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if (!desc->temp_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) if (!desc->temp_diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) static const struct of_device_id charger_manager_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) .compatible = "charger-manager",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) MODULE_DEVICE_TABLE(of, charger_manager_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) static struct charger_desc *of_cm_parse_desc(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) struct charger_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) u32 poll_mode = CM_POLL_DISABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) u32 battery_stat = CM_NO_BATTERY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) int num_chgs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) of_property_read_string(np, "cm-name", &desc->psy_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) of_property_read_u32(np, "cm-poll-mode", &poll_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) desc->polling_mode = poll_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) of_property_read_u32(np, "cm-poll-interval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) &desc->polling_interval_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) &desc->fullbatt_vchkdrop_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) of_property_read_u32(np, "cm-fullbatt-capacity",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) &desc->fullbatt_full_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) of_property_read_u32(np, "cm-battery-stat", &battery_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) desc->battery_present = battery_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) /* chargers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) num_chgs = of_property_count_strings(np, "cm-chargers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) if (num_chgs > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) /* Allocate empty bin at the tail of array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) desc->psy_charger_stat = devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) num_chgs + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) sizeof(char *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) if (!desc->psy_charger_stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) for (i = 0; i < num_chgs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) of_property_read_string_index(np, "cm-chargers",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) i, &desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) of_property_read_u32(np, "cm-battery-cold", &desc->temp_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) if (of_get_property(np, "cm-battery-cold-in-minus", NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) desc->temp_min *= -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) of_property_read_u32(np, "cm-battery-hot", &desc->temp_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) of_property_read_u32(np, "cm-charging-max",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) &desc->charging_max_duration_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) of_property_read_u32(np, "cm-discharging-max",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) &desc->discharging_max_duration_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) /* battery charger regulators */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) desc->num_charger_regulators = of_get_child_count(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) if (desc->num_charger_regulators) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) struct charger_regulator *chg_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) chg_regs = devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) desc->num_charger_regulators,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) sizeof(*chg_regs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) if (!chg_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) desc->charger_regulators = chg_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) desc->sysfs_groups = devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) desc->num_charger_regulators + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) sizeof(*desc->sysfs_groups),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) if (!desc->sysfs_groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) for_each_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) struct charger_cable *cables;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) struct device_node *_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) of_property_read_string(child, "cm-regulator-name",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) &chg_regs->regulator_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) /* charger cables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) chg_regs->num_cables = of_get_child_count(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if (chg_regs->num_cables) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) cables = devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) chg_regs->num_cables,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) sizeof(*cables),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) if (!cables) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) chg_regs->cables = cables;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) for_each_child_of_node(child, _child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) of_property_read_string(_child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) "cm-cable-name", &cables->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) of_property_read_string(_child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) "cm-cable-extcon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) &cables->extcon_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) of_property_read_u32(_child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) "cm-cable-min",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) &cables->min_uA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) of_property_read_u32(_child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) "cm-cable-max",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) &cables->max_uA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) cables++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) chg_regs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) if (pdev->dev.of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) return of_cm_parse_desc(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) return dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) cm_timer_set = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) return ALARMTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) static int charger_manager_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) struct charger_desc *desc = cm_get_drv_data(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) struct charger_manager *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) int ret, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) union power_supply_propval val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) struct power_supply *fuel_gauge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) enum power_supply_property *properties;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) size_t num_properties;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) if (IS_ERR(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) dev_err(&pdev->dev, "No platform data (desc) found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) return PTR_ERR(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) cm = devm_kzalloc(&pdev->dev, sizeof(*cm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) if (!cm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) /* Basic Values. Unspecified are Null or 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) cm->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) cm->desc = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) psy_cfg.drv_data = cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) /* Initialize alarm timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) if (alarmtimer_get_rtcdev()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) if (!cm_timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) * Some of the following do not need to be errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) * Users may intentionally ignore those features.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) if (desc->fullbatt_uV == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) if (!desc->fullbatt_vchkdrop_uV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) desc->fullbatt_vchkdrop_uV = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) if (desc->fullbatt_soc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) if (desc->fullbatt_full_capacity == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) dev_err(&pdev->dev, "charger_regulators undefined\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) dev_err(&pdev->dev, "No power supply defined\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) if (!desc->psy_fuel_gauge) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) /* Check if charger's supplies are present at probe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) for (i = 0; desc->psy_charger_stat[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) struct power_supply *psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) psy = power_supply_get_by_name(desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) if (!psy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) desc->psy_charger_stat[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) power_supply_put(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) if (cm->desc->polling_mode != CM_POLL_DISABLE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) (desc->polling_interval_ms == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) dev_err(&pdev->dev, "polling_interval_ms is too small\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) if (!desc->charging_max_duration_ms ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) !desc->discharging_max_duration_ms) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) desc->charging_max_duration_ms = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) desc->discharging_max_duration_ms = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) platform_set_drvdata(pdev, cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) memcpy(&cm->charger_psy_desc, &psy_default, sizeof(psy_default));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) if (!desc->psy_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) cm->charger_psy_desc.name = cm->psy_name_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) /* Allocate for psy properties because they may vary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) properties = devm_kcalloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) ARRAY_SIZE(default_charger_props) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) NUM_CHARGER_PSY_OPTIONAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) sizeof(*properties), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) if (!properties)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) memcpy(properties, default_charger_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) sizeof(enum power_supply_property) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) ARRAY_SIZE(default_charger_props));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) num_properties = ARRAY_SIZE(default_charger_props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) /* Find which optional psy-properties are available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) if (!fuel_gauge) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) desc->psy_fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) if (!power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) POWER_SUPPLY_PROP_CHARGE_FULL, &val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) properties[num_properties] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) POWER_SUPPLY_PROP_CHARGE_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) num_properties++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) if (!power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) properties[num_properties] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) POWER_SUPPLY_PROP_CHARGE_NOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) num_properties++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) if (!power_supply_get_property(fuel_gauge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) &val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) properties[num_properties] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) POWER_SUPPLY_PROP_CURRENT_NOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) num_properties++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) ret = cm_init_thermal_data(cm, fuel_gauge, properties, &num_properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) dev_err(&pdev->dev, "Failed to initialize thermal data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) cm->desc->measure_battery_temp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) power_supply_put(fuel_gauge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) cm->charger_psy_desc.properties = properties;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) cm->charger_psy_desc.num_properties = num_properties;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) /* Register sysfs entry for charger(regulator) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) ret = charger_manager_prepare_sysfs(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) "Cannot prepare sysfs entry of regulators\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) psy_cfg.attr_grp = desc->sysfs_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) cm->charger_psy = power_supply_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) &cm->charger_psy_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) if (IS_ERR(cm->charger_psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) cm->charger_psy_desc.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) return PTR_ERR(cm->charger_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) /* Register extcon device for charger cable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) ret = charger_manager_register_extcon(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) dev_err(&pdev->dev, "Cannot initialize extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) goto err_reg_extcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) /* Add to the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) mutex_lock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) list_add(&cm->entry, &cm_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) mutex_unlock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) * Charger-manager is capable of waking up the systme from sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) * when event is happened through cm_notify_event()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) device_init_wakeup(&pdev->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) device_set_wakeup_capable(&pdev->dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) * Charger-manager have to check the charging state right after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) * initialization of charger-manager and then update current charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) * state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) cm_monitor();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) schedule_work(&setup_polling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) err_reg_extcon:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) for (i = 0; i < desc->num_charger_regulators; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) regulator_put(desc->charger_regulators[i].consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) power_supply_unregister(cm->charger_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) static int charger_manager_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) struct charger_manager *cm = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) struct charger_desc *desc = cm->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) /* Remove from the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) mutex_lock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) list_del(&cm->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) mutex_unlock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) cancel_work_sync(&setup_polling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) cancel_delayed_work_sync(&cm_monitor_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) for (i = 0 ; i < desc->num_charger_regulators ; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) regulator_put(desc->charger_regulators[i].consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) power_supply_unregister(cm->charger_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) try_charger_enable(cm, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) static const struct platform_device_id charger_manager_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) { "charger-manager", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) MODULE_DEVICE_TABLE(platform, charger_manager_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) static int cm_suspend_noirq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) if (device_may_wakeup(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) device_set_wakeup_capable(dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) static bool cm_need_to_awake(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) struct charger_manager *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) if (cm_timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) mutex_lock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) list_for_each_entry(cm, &cm_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) if (is_charging(cm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) mutex_unlock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) mutex_unlock(&cm_list_mtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) static int cm_suspend_prepare(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) if (cm_need_to_awake())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) if (!cm_suspended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) cm_suspended = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) cm_timer_set = cm_setup_timer();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) if (cm_timer_set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) cancel_work_sync(&setup_polling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) cancel_delayed_work_sync(&cm_monitor_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) static void cm_suspend_complete(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) struct charger_manager *cm = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) if (cm_suspended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) cm_suspended = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) if (cm_timer_set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) ktime_t remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) alarm_cancel(cm_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) cm_timer_set = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) remain = alarm_expires_remaining(cm_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) cm_suspend_duration_ms -= ktime_to_ms(remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) schedule_work(&setup_polling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) _cm_monitor(cm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) device_set_wakeup_capable(cm->dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) static const struct dev_pm_ops charger_manager_pm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) .prepare = cm_suspend_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) .suspend_noirq = cm_suspend_noirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) .complete = cm_suspend_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) static struct platform_driver charger_manager_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) .name = "charger-manager",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) .pm = &charger_manager_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) .of_match_table = charger_manager_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) .probe = charger_manager_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) .remove = charger_manager_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) .id_table = charger_manager_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) static int __init charger_manager_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) cm_wq = create_freezable_workqueue("charger_manager");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) if (unlikely(!cm_wq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) return platform_driver_register(&charger_manager_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) late_initcall(charger_manager_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) static void __exit charger_manager_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) destroy_workqueue(cm_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) cm_wq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) platform_driver_unregister(&charger_manager_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) module_exit(charger_manager_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) MODULE_DESCRIPTION("Charger Manager");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) MODULE_LICENSE("GPL");