// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2011 Samsung Electronics Co., Ltd. * MyungJoo Ham <myungjoo.ham@samsung.com> * * This driver enables to monitor battery health and control charger * during suspend-to-mem. * Charger manager depends on other devices. Register this later than * the depending devices. *
**/
/* * Default temperature threshold for charging. * Every temperature units are in tenth of centigrade.
*/ #define CM_DEFAULT_RECHARGE_TEMP_DIFF 50 #define CM_DEFAULT_CHARGE_TEMP_MAX 500
/* * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for * delayed works so that we can run delayed works with CM_JIFFIES_SMALL * without any delays.
*/ #define CM_JIFFIES_SMALL (2)
/* If y is valid (> 0) and smaller than x, do x = y */ #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
/* * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking * rtc alarm. It should be 2 or larger
*/ #define CM_RTC_SMALL (2)
/* About normal (not suspended) monitoring */ staticunsignedlong polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */ staticunsignedlong next_polling; /* Next appointed polling time */ staticstruct workqueue_struct *cm_wq; /* init at driver add */ staticstruct delayed_work cm_monitor_work; /* init at driver add */
/** * is_batt_present - See if the battery presents in place. * @cm: the Charger Manager representing the battery.
*/ staticbool is_batt_present(struct charger_manager *cm)
{ union power_supply_propval val; struct power_supply *psy; bool present = false; int i, ret;
switch (cm->desc->battery_present) { case CM_BATTERY_PRESENT:
present = true; break; case CM_NO_BATTERY: break; case CM_FUEL_GAUGE:
psy = power_supply_get_by_name(cm->desc->psy_fuel_gauge); if (!psy) break;
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT,
&val); if (ret == 0 && val.intval)
present = true;
power_supply_put(psy); break; case CM_CHARGER_STAT: for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
psy = power_supply_get_by_name(
cm->desc->psy_charger_stat[i]); if (!psy) {
dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
cm->desc->psy_charger_stat[i]); continue;
}
/** * is_ext_pwr_online - See if an external power source is attached to charge * @cm: the Charger Manager representing the battery. * * Returns true if at least one of the chargers of the battery has an external * power source attached to charge the battery regardless of whether it is * actually charging or not.
*/ staticbool is_ext_pwr_online(struct charger_manager *cm)
{ union power_supply_propval val; struct power_supply *psy; bool online = false; int i, ret;
/* If at least one of them has one, it's yes. */ for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]); if (!psy) {
dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
cm->desc->psy_charger_stat[i]); continue;
}
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
&val);
power_supply_put(psy); if (ret == 0 && val.intval) {
online = true; break;
}
}
return online;
}
/** * get_batt_uV - Get the voltage level of the battery * @cm: the Charger Manager representing the battery. * @uV: the voltage level returned. * * Returns 0 if there is no error. * Returns a negative value on error.
*/ staticint get_batt_uV(struct charger_manager *cm, int *uV)
{ union power_supply_propval val; struct power_supply *fuel_gauge; int ret;
fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); if (!fuel_gauge) return -ENODEV;
ret = power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
power_supply_put(fuel_gauge); if (ret) return ret;
*uV = val.intval; return 0;
}
/** * is_charging - Returns true if the battery is being charged. * @cm: the Charger Manager representing the battery.
*/ staticbool is_charging(struct charger_manager *cm)
{ int i, ret; bool charging = false; struct power_supply *psy; union power_supply_propval val;
/* If there is no battery, it cannot be charged */ if (!is_batt_present(cm)) returnfalse;
/* If at least one of the charger is charging, return yes */ for (i = 0; cm->desc->psy_charger_stat[i]; i++) { /* 1. The charger should not be DISABLED */ if (cm->emergency_stop) continue; if (!cm->charger_enabled) continue;
psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]); if (!psy) {
dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
cm->desc->psy_charger_stat[i]); continue;
}
/* 2. The charger should be online (ext-power) */
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
&val); if (ret) {
dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
cm->desc->psy_charger_stat[i]);
power_supply_put(psy); continue;
} if (val.intval == 0) {
power_supply_put(psy); continue;
}
/* * 3. The charger should not be FULL, DISCHARGING, * or NOT_CHARGING.
*/
ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
&val);
power_supply_put(psy); if (ret) {
dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
cm->desc->psy_charger_stat[i]); continue;
} if (val.intval == POWER_SUPPLY_STATUS_FULL ||
val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) continue;
/* Then, this is charging. */
charging = true; break;
}
return charging;
}
/** * is_full_charged - Returns true if the battery is fully charged. * @cm: the Charger Manager representing the battery.
*/ staticbool is_full_charged(struct charger_manager *cm)
{ struct charger_desc *desc = cm->desc; union power_supply_propval val; struct power_supply *fuel_gauge; bool is_full = false; int ret = 0; int uV;
/* If there is no battery, it cannot be charged */ if (!is_batt_present(cm)) returnfalse;
fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); if (!fuel_gauge) returnfalse;
/* Full, if it's over the fullbatt voltage */ if (desc->fullbatt_uV > 0) {
ret = get_batt_uV(cm, &uV); if (!ret) { /* Battery is already full, checks voltage drop. */ if (cm->battery_status == POWER_SUPPLY_STATUS_FULL
&& desc->fullbatt_vchkdrop_uV)
uV += desc->fullbatt_vchkdrop_uV; if (uV >= desc->fullbatt_uV) returntrue;
}
}
if (desc->fullbatt_full_capacity > 0) {
val.intval = 0;
/* Not full if capacity of fuel gauge isn't full */
ret = power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CHARGE_FULL, &val); if (!ret && val.intval > desc->fullbatt_full_capacity) {
is_full = true; goto out;
}
}
/* Full, if the capacity is more than fullbatt_soc */ if (desc->fullbatt_soc > 0) {
val.intval = 0;
ret = power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CAPACITY, &val); if (!ret && val.intval >= desc->fullbatt_soc) {
is_full = true; goto out;
}
}
/** * is_polling_required - Return true if need to continue polling for this CM. * @cm: the Charger Manager representing the battery.
*/ staticbool is_polling_required(struct charger_manager *cm)
{ switch (cm->desc->polling_mode) { case CM_POLL_DISABLE: returnfalse; case CM_POLL_ALWAYS: returntrue; case CM_POLL_EXTERNAL_POWER_ONLY: return is_ext_pwr_online(cm); case CM_POLL_CHARGING_ONLY: return is_charging(cm); default:
dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
cm->desc->polling_mode);
}
returnfalse;
}
/** * try_charger_enable - Enable/Disable chargers altogether * @cm: the Charger Manager representing the battery. * @enable: true: enable / false: disable * * Note that Charger Manager keeps the charger enabled regardless whether * the charger is charging or not (because battery is full or no external * power source exists) except when CM needs to disable chargers forcibly * because of emergency causes; when the battery is overheated or too cold.
*/ staticint try_charger_enable(struct charger_manager *cm, bool enable)
{ int err = 0, i; struct charger_desc *desc = cm->desc;
/* Ignore if it's redundant command */ if (enable == cm->charger_enabled) return 0;
if (enable) { if (cm->emergency_stop) return -EAGAIN;
/* * Save start time of charging to limit * maximum possible charging time.
*/
cm->charging_start_time = ktime_to_ms(ktime_get());
cm->charging_end_time = 0;
for (i = 0 ; i < desc->num_charger_regulators ; i++) { if (desc->charger_regulators[i].externally_control) continue;
err = regulator_enable(desc->charger_regulators[i].consumer); if (err < 0) {
dev_warn(cm->dev, "Cannot enable %s regulator\n",
desc->charger_regulators[i].regulator_name);
}
}
} else { /* * Save end time of charging to maintain fully charged state * of battery after full-batt.
*/
cm->charging_start_time = 0;
cm->charging_end_time = ktime_to_ms(ktime_get());
for (i = 0 ; i < desc->num_charger_regulators ; i++) { if (desc->charger_regulators[i].externally_control) continue;
/* * Abnormal battery state - Stop charging forcibly, * even if charger was enabled at the other places
*/ for (i = 0; i < desc->num_charger_regulators; i++) { if (regulator_is_enabled(
desc->charger_regulators[i].consumer)) {
regulator_force_disable(
desc->charger_regulators[i].consumer);
dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
desc->charger_regulators[i].regulator_name);
}
}
}
if (!err)
cm->charger_enabled = enable;
return err;
}
/** * check_charging_duration - Monitor charging/discharging duration * @cm: the Charger Manager representing the battery. * * If whole charging duration exceed 'charging_max_duration_ms', * cm stop charging to prevent overcharge/overheat. If discharging * duration exceed 'discharging _max_duration_ms', charger cable is * attached, after full-batt, cm start charging to maintain fully * charged state for battery.
*/ staticint check_charging_duration(struct charger_manager *cm)
{ struct charger_desc *desc = cm->desc;
u64 curr = ktime_to_ms(ktime_get());
u64 duration; int ret = false;
if (!desc->charging_max_duration_ms &&
!desc->discharging_max_duration_ms) return ret;
if (cm->charger_enabled) {
duration = curr - cm->charging_start_time;
if (duration > desc->discharging_max_duration_ms) {
dev_info(cm->dev, "Discharging duration exceed %ums\n",
desc->discharging_max_duration_ms);
ret = true;
}
}
return ret;
}
staticint cm_get_battery_temperature_by_psy(struct charger_manager *cm, int *temp)
{ struct power_supply *fuel_gauge; int ret;
fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); if (!fuel_gauge) return -ENODEV;
ret = power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_TEMP,
(union power_supply_propval *)temp);
power_supply_put(fuel_gauge);
return ret;
}
staticint cm_get_battery_temperature(struct charger_manager *cm, int *temp)
{ int ret;
if (!cm->desc->measure_battery_temp) return -ENODEV;
#ifdef CONFIG_THERMAL if (cm->tzd_batt) {
ret = thermal_zone_get_temp(cm->tzd_batt, temp); if (!ret) /* Calibrate temperature unit */
*temp /= 100;
} else #endif
{ /* if-else continued from CONFIG_THERMAL */
ret = cm_get_battery_temperature_by_psy(cm, temp);
}
return ret;
}
staticint cm_check_thermal_status(struct charger_manager *cm)
{ struct charger_desc *desc = cm->desc; int temp, upper_limit, lower_limit; int ret = 0;
ret = cm_get_battery_temperature(cm, &temp); if (ret) { /* FIXME: * No information of battery temperature might * occur hazardous result. We have to handle it * depending on battery type.
*/
dev_err(cm->dev, "Failed to get battery temperature\n"); return 0;
}
if (cm->emergency_stop) {
upper_limit -= desc->temp_diff;
lower_limit += desc->temp_diff;
}
if (temp > upper_limit)
ret = CM_BATT_OVERHEAT; elseif (temp < lower_limit)
ret = CM_BATT_COLD; else
ret = CM_BATT_OK;
cm->emergency_stop = ret;
return ret;
}
/** * cm_get_target_status - Check current status and get next target status. * @cm: the Charger Manager representing the battery.
*/ staticint cm_get_target_status(struct charger_manager *cm)
{ if (!is_ext_pwr_online(cm)) return POWER_SUPPLY_STATUS_DISCHARGING;
if (cm_check_thermal_status(cm)) { /* Check if discharging duration exceeds limit. */ if (check_charging_duration(cm)) goto charging_ok; return POWER_SUPPLY_STATUS_NOT_CHARGING;
}
switch (cm->battery_status) { case POWER_SUPPLY_STATUS_CHARGING: /* Check if charging duration exceeds limit. */ if (check_charging_duration(cm)) return POWER_SUPPLY_STATUS_FULL;
fallthrough; case POWER_SUPPLY_STATUS_FULL: if (is_full_charged(cm)) return POWER_SUPPLY_STATUS_FULL;
fallthrough; default: break;
}
charging_ok: /* Charging is allowed. */ return POWER_SUPPLY_STATUS_CHARGING;
}
/** * _cm_monitor - Monitor the temperature and return true for exceptions. * @cm: the Charger Manager representing the battery. * * Returns true if there is an event to notify for the battery. * (True if the status of "emergency_stop" changes)
*/ staticbool _cm_monitor(struct charger_manager *cm)
{ int target;
/** * cm_monitor - Monitor every battery. * * Returns true if there is an event to notify from any of the batteries. * (True if the status of "emergency_stop" changes)
*/ staticbool cm_monitor(void)
{ bool stop = false; struct charger_manager *cm;
mutex_lock(&cm_list_mtx);
list_for_each_entry(cm, &cm_list, entry) { if (_cm_monitor(cm))
stop = true;
}
mutex_unlock(&cm_list_mtx);
return stop;
}
/** * _setup_polling - Setup the next instance of polling. * @work: work_struct of the function _setup_polling.
*/ staticvoid _setup_polling(struct work_struct *work)
{ unsignedlong min = ULONG_MAX; struct charger_manager *cm; bool keep_polling = false; unsignedlong _next_polling;
if (!keep_polling)
polling_jiffy = ULONG_MAX; if (polling_jiffy == ULONG_MAX) goto out;
WARN(cm_wq == NULL, "charger-manager: workqueue not initialized" ". try it later. %s\n", __func__);
/* * Use mod_delayed_work() iff the next polling interval should * occur before the currently scheduled one. If @cm_monitor_work * isn't active, the end result is the same, so no need to worry * about stale @next_polling.
*/
_next_polling = jiffies + polling_jiffy;
/** * cm_monitor_poller - The Monitor / Poller. * @work: work_struct of the function cm_monitor_poller * * During non-suspended state, cm_monitor_poller is used to poll and monitor * the batteries.
*/ staticvoid cm_monitor_poller(struct work_struct *work)
{
cm_monitor();
schedule_work(&setup_polling);
}
staticint charger_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val)
{ struct charger_manager *cm = power_supply_get_drvdata(psy); struct charger_desc *desc = cm->desc; struct power_supply *fuel_gauge = NULL; int ret = 0; int uV;
switch (psp) { case POWER_SUPPLY_PROP_STATUS:
val->intval = cm->battery_status; break; case POWER_SUPPLY_PROP_HEALTH: if (cm->emergency_stop == CM_BATT_OVERHEAT)
val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; elseif (cm->emergency_stop == CM_BATT_COLD)
val->intval = POWER_SUPPLY_HEALTH_COLD; else
val->intval = POWER_SUPPLY_HEALTH_GOOD; break; case POWER_SUPPLY_PROP_PRESENT: if (is_batt_present(cm))
val->intval = 1; else
val->intval = 0; break; case POWER_SUPPLY_PROP_VOLTAGE_NOW:
ret = get_batt_uV(cm, &val->intval); break; case POWER_SUPPLY_PROP_CURRENT_NOW:
fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); if (!fuel_gauge) {
ret = -ENODEV; break;
}
ret = power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CURRENT_NOW, val); break; case POWER_SUPPLY_PROP_TEMP: return cm_get_battery_temperature(cm, &val->intval); case POWER_SUPPLY_PROP_CAPACITY: if (!is_batt_present(cm)) { /* There is no battery. Assume 100% */
val->intval = 100; break;
}
fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); if (!fuel_gauge) {
ret = -ENODEV; break;
}
ret = power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CAPACITY, val); if (ret) break;
if (val->intval > 100) {
val->intval = 100; break;
} if (val->intval < 0)
val->intval = 0;
/* Do not adjust SOC when charging: voltage is overrated */ if (is_charging(cm)) break;
/* * If the capacity value is inconsistent, calibrate it base on * the battery voltage values and the thresholds given as desc
*/
ret = get_batt_uV(cm, &uV); if (ret) { /* Voltage information not available. No calibration */
ret = 0; break;
}
/** * cm_setup_timer - For in-suspend monitoring setup wakeup alarm * for suspend_again. * * Returns true if the alarm is set for Charger Manager to use. * Returns false if * cm_setup_timer fails to set an alarm, * cm_setup_timer does not need to set an alarm for Charger Manager, * or an alarm previously configured is to be used.
*/ staticbool cm_setup_timer(void)
{ struct charger_manager *cm; unsignedint wakeup_ms = UINT_MAX; int timer_req = 0;
if (time_after(next_polling, jiffies))
CM_MIN_VALID(wakeup_ms,
jiffies_to_msecs(next_polling - jiffies));
mutex_lock(&cm_list_mtx);
list_for_each_entry(cm, &cm_list, entry) { /* Skip if polling is not required for this CM */ if (!is_polling_required(cm) && !cm->emergency_stop) continue;
timer_req++; if (cm->desc->polling_interval_ms == 0) continue;
CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
}
mutex_unlock(&cm_list_mtx);
if (timer_req && cm_timer) {
ktime_t now, add;
/* * Set alarm with the polling interval (wakeup_ms) * The alarm time should be NOW + CM_RTC_SMALL or later.
*/ if (wakeup_ms == UINT_MAX ||
wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC)
wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC;
/** * charger_extcon_work - enable/diable charger according to the state * of charger cable * * @work: work_struct of the function charger_extcon_work.
*/ staticvoid charger_extcon_work(struct work_struct *work)
{ struct charger_cable *cable =
container_of(work, struct charger_cable, wq); int ret;
if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
ret = regulator_set_current_limit(cable->charger->consumer,
cable->min_uA, cable->max_uA); if (ret < 0) {
pr_err("Cannot set current limit of %s (%s)\n",
cable->charger->regulator_name, cable->name); return;
}
pr_info("Set current limit of %s : %duA ~ %duA\n",
cable->charger->regulator_name,
cable->min_uA, cable->max_uA);
}
/** * charger_extcon_notifier - receive the state of charger cable * when registered cable is attached or detached. * * @self: the notifier block of the charger_extcon_notifier. * @event: the cable state. * @ptr: the data pointer of notifier block.
*/ staticint charger_extcon_notifier(struct notifier_block *self, unsignedlong event, void *ptr)
{ struct charger_cable *cable =
container_of(self, struct charger_cable, nb);
/* * The newly state of charger cable. * If cable is attached, cable->attached is true.
*/
cable->attached = event;
/* * Setup work for controlling charger(regulator) * according to charger cable.
*/
schedule_work(&cable->wq);
return NOTIFY_DONE;
}
/** * charger_extcon_init - register external connector to use it * as the charger cable * * @cm: the Charger Manager representing the battery. * @cable: the Charger cable representing the external connector.
*/ staticint charger_extcon_init(struct charger_manager *cm, struct charger_cable *cable)
{ int ret, i;
u64 extcon_type = EXTCON_NONE;
/* * Charger manager use Extcon framework to identify * the charger cable among various external connector * cable (e.g., TA, USB, MHL, Dock).
*/
INIT_WORK(&cable->wq, charger_extcon_work);
cable->nb.notifier_call = charger_extcon_notifier;
cable->extcon_dev = extcon_get_extcon_dev(cable->extcon_name); if (IS_ERR(cable->extcon_dev)) {
pr_err("Cannot find extcon_dev for %s (cable: %s)\n",
cable->extcon_name, cable->name); return PTR_ERR(cable->extcon_dev);
}
for (i = 0; i < ARRAY_SIZE(extcon_mapping); i++) { if (!strcmp(cable->name, extcon_mapping[i].name)) {
extcon_type = extcon_mapping[i].extcon_type; break;
}
} if (extcon_type == EXTCON_NONE) {
pr_err("Cannot find cable for type %s", cable->name); return -EINVAL;
}
cable->extcon_type = extcon_type;
ret = devm_extcon_register_notifier(cm->dev, cable->extcon_dev,
cable->extcon_type, &cable->nb); if (ret < 0) {
pr_err("Cannot register extcon_dev for %s (cable: %s)\n",
cable->extcon_name, cable->name); return ret;
}
return 0;
}
/** * charger_manager_register_extcon - Register extcon device to receive state * of charger cable. * @cm: the Charger Manager representing the battery. * * This function support EXTCON(External Connector) subsystem to detect the * state of charger cables for enabling or disabling charger(regulator) and * select the charger cable for charging among a number of external cable * according to policy of H/W board.
*/ staticint charger_manager_register_extcon(struct charger_manager *cm)
{ struct charger_desc *desc = cm->desc; struct charger_regulator *charger; unsignedlong event; int ret; int i; int j;
for (i = 0; i < desc->num_charger_regulators; i++) {
charger = &desc->charger_regulators[i];
ret = sscanf(buf, "%d", &externally_control); if (ret == 0) {
ret = -EINVAL; return ret;
}
if (!externally_control) {
charger->externally_control = 0; return count;
}
for (i = 0; i < desc->num_charger_regulators; i++) { if (&desc->charger_regulators[i] != charger &&
!desc->charger_regulators[i].externally_control) { /* * At least, one charger is controlled by * charger-manager
*/
chargers_externally_control = 0; break;
}
}
if (!chargers_externally_control) { if (cm->charger_enabled) {
try_charger_enable(charger->cm, false);
charger->externally_control = externally_control;
try_charger_enable(charger->cm, true);
} else {
charger->externally_control = externally_control;
}
} else {
dev_warn(cm->dev, "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
charger->regulator_name);
}
return count;
}
/** * charger_manager_prepare_sysfs - Prepare sysfs entry for each charger * @cm: the Charger Manager representing the battery. * * This function add sysfs entry for charger(regulator) to control charger from * user-space. If some development board use one more chargers for charging * but only need one charger on specific case which is dependent on user * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/ * class/power_supply/battery/charger.[index]/externally_control'. For example, * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/ * externally_control, this charger isn't controlled from charger-manager and * always stay off state of regulator.
*/ staticint charger_manager_prepare_sysfs(struct charger_manager *cm)
{ struct charger_desc *desc = cm->desc; struct charger_regulator *charger; int chargers_externally_control = 1; char *name; int i;
/* Create sysfs entry to control charger(regulator) */ for (i = 0; i < desc->num_charger_regulators; i++) {
charger = &desc->charger_regulators[i];
name = devm_kasprintf(cm->dev, GFP_KERNEL, "charger.%d", i); if (!name) return -ENOMEM;
if (!desc->charger_regulators[i].externally_control ||
!chargers_externally_control)
chargers_externally_control = 0;
dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
charger->regulator_name, charger->externally_control);
}
if (chargers_externally_control) {
dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n"); return -EINVAL;
}
/* chargers */
num_chgs = of_property_count_strings(np, "cm-chargers"); if (num_chgs > 0) { int i;
/* Allocate empty bin at the tail of array */
desc->psy_charger_stat = devm_kcalloc(dev,
num_chgs + 1, sizeof(char *),
GFP_KERNEL); if (!desc->psy_charger_stat) return ERR_PTR(-ENOMEM);
for (i = 0; i < num_chgs; i++)
of_property_read_string_index(np, "cm-chargers",
i, &desc->psy_charger_stat[i]);
}
/* * Some of the following do not need to be errors. * Users may intentionally ignore those features.
*/ if (desc->fullbatt_uV == 0) {
dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
} if (!desc->fullbatt_vchkdrop_uV) {
dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
desc->fullbatt_vchkdrop_uV = 0;
} if (desc->fullbatt_soc == 0) {
dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
} if (desc->fullbatt_full_capacity == 0) {
dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
}
/* Allocate for psy properties because they may vary */
properties = devm_kcalloc(&pdev->dev,
ARRAY_SIZE(default_charger_props) +
NUM_CHARGER_PSY_OPTIONAL, sizeof(*properties), GFP_KERNEL); if (!properties) return -ENOMEM;
/* Register sysfs entry for charger(regulator) */
ret = charger_manager_prepare_sysfs(cm); if (ret < 0) {
dev_err(&pdev->dev, "Cannot prepare sysfs entry of regulators\n"); return ret;
}
psy_cfg.attr_grp = desc->sysfs_groups;
cm->charger_psy = power_supply_register(&pdev->dev,
&cm->charger_psy_desc,
&psy_cfg); if (IS_ERR(cm->charger_psy)) {
dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
cm->charger_psy_desc.name); return PTR_ERR(cm->charger_psy);
}
/* Register extcon device for charger cable */
ret = charger_manager_register_extcon(cm); if (ret < 0) {
dev_err(&pdev->dev, "Cannot initialize extcon device\n"); goto err_reg_extcon;
}
/* Add to the list */
mutex_lock(&cm_list_mtx);
list_add(&cm->entry, &cm_list);
mutex_unlock(&cm_list_mtx);
/* * Charger-manager is capable of waking up the system from sleep * when event is happened through cm_notify_event()
*/
device_init_wakeup(&pdev->dev, true);
device_set_wakeup_capable(&pdev->dev, false);
/* * Charger-manager have to check the charging state right after * initialization of charger-manager and then update current charging * state.
*/
cm_monitor();
schedule_work(&setup_polling);
return 0;
err_reg_extcon: for (i = 0; i < desc->num_charger_regulators; i++)
regulator_put(desc->charger_regulators[i].consumer);
power_supply_unregister(cm->charger_psy);
return ret;
}
staticvoid charger_manager_remove(struct platform_device *pdev)
{ struct charger_manager *cm = platform_get_drvdata(pdev); struct charger_desc *desc = cm->desc; int i = 0;
/* Remove from the list */
mutex_lock(&cm_list_mtx);
list_del(&cm->entry);
mutex_unlock(&cm_list_mtx);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.