staticbool __power_supply_is_supplied_by(struct power_supply *supplier, struct power_supply *supply)
{ int i;
if (!supply->supplied_from && !supplier->supplied_to) returnfalse;
/* Support both supplied_to and supplied_from modes */ if (supply->supplied_from) { if (!supplier->desc->name) returnfalse; for (i = 0; i < supply->num_supplies; i++) if (!strcmp(supplier->desc->name, supply->supplied_from[i])) returntrue;
} else { if (!supply->desc->name) returnfalse; for (i = 0; i < supplier->num_supplicants; i++) if (!strcmp(supplier->supplied_to[i], supply->desc->name)) returntrue;
}
if (unlikely(psy->update_groups)) {
psy->update_groups = false;
spin_unlock_irqrestore(&psy->changed_lock, flags);
ret = sysfs_update_groups(&psy->dev.kobj, power_supply_dev_type.groups); if (ret)
dev_warn(&psy->dev, "failed to update sysfs groups: %pe\n", ERR_PTR(ret));
spin_lock_irqsave(&psy->changed_lock, flags);
}
/* * Check 'changed' here to avoid issues due to race between * power_supply_changed() and this routine. In worst case * power_supply_changed() can be called again just before we take above * lock. During the first call of this routine we will mark 'changed' as * false and it will stay false for the next call as well.
*/ if (likely(psy->changed)) {
psy->changed = false;
spin_unlock_irqrestore(&psy->changed_lock, flags);
power_supply_for_each_psy(psy, __power_supply_changed_work);
power_supply_update_leds(psy);
blocking_notifier_call_chain(&power_supply_notifier,
PSY_EVENT_PROP_CHANGED, psy);
kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
spin_lock_irqsave(&psy->changed_lock, flags);
}
/* * Hold the wakeup_source until all events are processed. * power_supply_changed() might have called again and have set 'changed' * to true.
*/ if (likely(!psy->changed))
pm_relax(&psy->dev);
spin_unlock_irqrestore(&psy->changed_lock, flags);
}
/* * Notify that power supply was registered after parent finished the probing. * * Often power supply is registered from driver's probe function. However * calling power_supply_changed() directly from power_supply_register() * would lead to execution of get_property() function provided by the driver * too early - before the probe ends. * * Avoid that by waiting on parent's mutex.
*/ staticvoid power_supply_deferred_register_work(struct work_struct *work)
{ struct power_supply *psy = container_of(work, struct power_supply,
deferred_register_work.work);
if (psy->dev.parent) { while (!device_trylock(psy->dev.parent)) { if (psy->removing) return;
msleep(10);
}
}
power_supply_changed(psy);
if (psy->dev.parent)
device_unlock(psy->dev.parent);
}
/* returning non-zero breaks out of power_supply_for_each_psy loop */ if (epsy->dev.fwnode == fwnode) return 1;
return 0;
}
staticint power_supply_find_supply_from_fwnode(struct fwnode_handle *supply_node)
{ int error;
/* * power_supply_for_each_psy() either returns its own errors or values * returned by __power_supply_find_supply_from_node(). * * __power_supply_find_supply_from_fwnode() will return 0 (no match) * or 1 (match). * * We return 0 if power_supply_for_each_psy() returned 1, -EPROBE_DEFER if * it returned 0, or error as returned by it.
*/
error = power_supply_for_each_psy(supply_node, __power_supply_find_supply_from_node);
/* * If no system scope power class device was found at all, most probably we * are running on a desktop system, so assume we are on mains power.
*/ if (count == 0) return 1;
if (__power_supply_is_supplied_by(epsy, data->psy)) if (!power_supply_get_property(epsy, data->psp, data->val)) return 1; /* Success */
return 0; /* Continue iterating */
}
int power_supply_get_property_from_supplier(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val)
{ struct psy_get_supplier_prop_data data = {
.psy = psy,
.psp = psp,
.val = val,
}; int ret;
/* * This function is not intended for use with a supply with multiple * suppliers, we simply pick the first supply to report the psp.
*/
ret = power_supply_for_each_psy(&data, __power_supply_get_supplier_property); if (ret < 0) return ret; if (ret == 0) return -ENODEV;
/** * power_supply_get_by_name() - Search for a power supply and returns its ref * @name: Power supply name to fetch * * If power supply was found, it increases reference count for the * internal power supply's device. The user should power_supply_put() * after usage. * * Return: On success returns a reference to a power supply with * matching name equals to @name, a NULL otherwise.
*/ struct power_supply *power_supply_get_by_name(constchar *name)
{ struct power_supply *psy = NULL; struct device *dev = class_find_device(&power_supply_class, NULL, name,
power_supply_match_device_by_name);
if (dev) {
psy = dev_to_psy(dev);
atomic_inc(&psy->use_cnt);
}
/** * power_supply_put() - Drop reference obtained with power_supply_get_by_name * @psy: Reference to put * * The reference to power supply should be put before unregistering * the power supply.
*/ void power_supply_put(struct power_supply *psy)
{
atomic_dec(&psy->use_cnt);
put_device(&psy->dev);
}
EXPORT_SYMBOL_GPL(power_supply_put);
/** * power_supply_get_by_reference() - Search for a power supply and returns its ref * @fwnode: Pointer to fwnode holding phandle property * @property: Name of property holding a power supply name * * If power supply was found, it increases reference count for the * internal power supply's device. The user should power_supply_put() * after usage. * * Return: On success returns a reference to a power supply with * matching name equals to value under @property, NULL or ERR_PTR otherwise.
*/ struct power_supply *power_supply_get_by_reference(struct fwnode_handle *fwnode, constchar *property)
{ struct fwnode_handle *power_supply_fwnode; struct power_supply *psy = NULL; struct device *dev;
power_supply_fwnode = fwnode_find_reference(fwnode, property, 0); if (IS_ERR(power_supply_fwnode)) return ERR_CAST(power_supply_fwnode);
dev = class_find_device(&power_supply_class, NULL, power_supply_fwnode,
power_supply_match_device_fwnode);
fwnode_handle_put(power_supply_fwnode);
if (dev) {
psy = dev_to_psy(dev);
atomic_inc(&psy->use_cnt);
}
/** * devm_power_supply_get_by_reference() - Resource managed version of * power_supply_get_by_reference() * @dev: Pointer to device holding phandle property * @property: Name of property holding a power supply phandle * * Return: On success returns a reference to a power supply with * matching name equals to value under @property, NULL or ERR_PTR otherwise.
*/ struct power_supply *devm_power_supply_get_by_reference(struct device *dev, constchar *property)
{ struct power_supply **ptr, *psy;
if (!dev_fwnode(dev)) return ERR_PTR(-ENODEV);
ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL); if (!ptr) return ERR_PTR(-ENOMEM);
srcnode = dev_fwnode(&psy->dev); if (!srcnode && psy->dev.parent)
srcnode = dev_fwnode(psy->dev.parent);
fwnode = fwnode_find_reference(srcnode, "monitored-battery", 0); if (IS_ERR(fwnode)) return PTR_ERR(fwnode);
err = fwnode_property_read_string(fwnode, "compatible", &value); if (err) goto out_put_node;
/* Try static batteries first */
err = samsung_sdi_battery_get_info(&psy->dev, value, &info); if (!err) goto out_ret_pointer; elseif (err == -ENODEV) /* * Device does not have a static battery. * Proceed to look for a simple battery.
*/
err = 0;
if (strcmp("simple-battery", value)) {
err = -ENODEV; goto out_put_node;
}
info = devm_kzalloc(&psy->dev, sizeof(*info), GFP_KERNEL); if (!info) {
err = -ENOMEM; goto out_put_node;
}
for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
info->ocv_table[index] = NULL;
info->ocv_temp[index] = -EINVAL;
info->ocv_table_size[index] = -EINVAL;
}
/* The property and field names below must correspond to elements * in enum power_supply_property. For reasoning, see * Documentation/power/power_supply_class.rst.
*/
switch (psp) { case POWER_SUPPLY_PROP_TECHNOLOGY: return info->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN; case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: return info->energy_full_design_uwh >= 0; case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: return info->charge_full_design_uah >= 0; case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: return info->voltage_min_design_uv >= 0; case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: return info->voltage_max_design_uv >= 0; case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: return info->precharge_current_ua >= 0; case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: return info->charge_term_current_ua >= 0; case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: return info->constant_charge_current_max_ua >= 0; case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: return info->constant_charge_voltage_max_uv >= 0; case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: return info->temp_ambient_alert_min > INT_MIN; case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: return info->temp_ambient_alert_max < INT_MAX; case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: return info->temp_alert_min > INT_MIN; case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: return info->temp_alert_max < INT_MAX; case POWER_SUPPLY_PROP_TEMP_MIN: return info->temp_min > INT_MIN; case POWER_SUPPLY_PROP_TEMP_MAX: return info->temp_max < INT_MAX; default: returnfalse;
}
}
EXPORT_SYMBOL_GPL(power_supply_battery_info_has_prop);
int power_supply_battery_info_get_prop(struct power_supply_battery_info *info, enum power_supply_property psp, union power_supply_propval *val)
{ if (!info) return -EINVAL;
if (!power_supply_battery_info_has_prop(info, psp)) return -EINVAL;
switch (psp) { case POWER_SUPPLY_PROP_TECHNOLOGY:
val->intval = info->technology; return 0; case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
val->intval = info->energy_full_design_uwh; return 0; case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
val->intval = info->charge_full_design_uah; return 0; case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
val->intval = info->voltage_min_design_uv; return 0; case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
val->intval = info->voltage_max_design_uv; return 0; case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
val->intval = info->precharge_current_ua; return 0; case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
val->intval = info->charge_term_current_ua; return 0; case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
val->intval = info->constant_charge_current_max_ua; return 0; case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
val->intval = info->constant_charge_voltage_max_uv; return 0; case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN:
val->intval = info->temp_ambient_alert_min; return 0; case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX:
val->intval = info->temp_ambient_alert_max; return 0; case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
val->intval = info->temp_alert_min; return 0; case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
val->intval = info->temp_alert_max; return 0; case POWER_SUPPLY_PROP_TEMP_MIN:
val->intval = info->temp_min; return 0; case POWER_SUPPLY_PROP_TEMP_MAX:
val->intval = info->temp_max; return 0; default: return -EINVAL;
}
}
EXPORT_SYMBOL_GPL(power_supply_battery_info_get_prop);
/** * power_supply_temp2resist_simple() - find the battery internal resistance * percent from temperature * @table: Pointer to battery resistance temperature table * @table_len: The table length * @temp: Current temperature * * This helper function is used to look up battery internal resistance percent * according to current temperature value from the resistance temperature table, * and the table must be ordered descending. Then the actual battery internal * resistance = the ideal battery internal resistance * percent / 100. * * Return: the battery internal resistance percent
*/ int power_supply_temp2resist_simple(conststruct power_supply_resistance_temp_table *table, int table_len, int temp)
{ int i, high, low;
for (i = 0; i < table_len; i++) if (temp > table[i].temp) break;
/* The library function will deal with high == low */ if (i == 0)
high = low = i; elseif (i == table_len)
high = low = i - 1; else
high = (low = i) - 1;
/** * power_supply_vbat2ri() - find the battery internal resistance * from the battery voltage * @info: The battery information container * @vbat_uv: The battery voltage in microvolt * @charging: If we are charging (true) or not (false) * * This helper function is used to look up battery internal resistance * according to current battery voltage. Depending on whether the battery * is currently charging or not, different resistance will be returned. * * Returns the internal resistance in microohm or negative error code.
*/ int power_supply_vbat2ri(struct power_supply_battery_info *info, int vbat_uv, bool charging)
{ conststruct power_supply_vbat_ri_table *vbat2ri; int table_len; int i, high, low;
/* * If we are charging, and the battery supplies a separate table * for this state, we use that in order to compensate for the * charging voltage. Otherwise we use the main table.
*/ if (charging && info->vbat2ri_charging) {
vbat2ri = info->vbat2ri_charging;
table_len = info->vbat2ri_charging_size;
} else {
vbat2ri = info->vbat2ri_discharging;
table_len = info->vbat2ri_discharging_size;
}
/* * If no tables are specified, or if we are above the highest voltage in * the voltage table, just return the factory specified internal resistance.
*/ if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) { if (charging && (info->factory_internal_resistance_charging_uohm > 0)) return info->factory_internal_resistance_charging_uohm; else return info->factory_internal_resistance_uohm;
}
/* Break loop at table_len - 1 because that is the highest index */ for (i = 0; i < table_len - 1; i++) if (vbat_uv > vbat2ri[i].vbat_uv) break;
/* The library function will deal with high == low */ if ((i == 0) || (i == (table_len - 1)))
high = i; else
high = i - 1;
low = i;
conststruct power_supply_maintenance_charge_table *
power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, int index)
{ if (index >= info->maintenance_charge_size) return NULL; return &info->maintenance_charge[index];
}
EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting);
/** * power_supply_ocv2cap_simple() - find the battery capacity * @table: Pointer to battery OCV lookup table * @table_len: OCV table length * @ocv: Current OCV value * * This helper function is used to look up battery capacity according to * current OCV value from one OCV table, and the OCV table must be ordered * descending. * * Return: the battery capacity.
*/ int power_supply_ocv2cap_simple(conststruct power_supply_battery_ocv_table *table, int table_len, int ocv)
{ int i, high, low;
for (i = 0; i < table_len; i++) if (ocv > table[i].ocv) break;
/* The library function will deal with high == low */ if (i == 0)
high = low = i; elseif (i == table_len)
high = low = i - 1; else
high = (low = i) - 1;
int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, int ocv, int temp)
{ conststruct power_supply_battery_ocv_table *table; int table_len;
table = power_supply_find_ocv2cap_table(info, temp, &table_len); if (!table) return -EINVAL;
int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val)
{ return __power_supply_get_property(psy, psp, val, true);
}
EXPORT_SYMBOL_GPL(power_supply_get_property);
/** * power_supply_get_property_direct - Read a power supply property without checking for extensions * @psy: The power supply * @psp: The power supply property to read * @val: The resulting value of the power supply property * * Read a power supply property without taking into account any power supply extensions registered * on the given power supply. This is mostly useful for power supply extensions that want to access * their own power supply as using power_supply_get_property() directly will result in a potential * deadlock. * * Return: 0 on success or negative error code on failure.
*/ int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val)
{ return __power_supply_get_property(psy, psp, val, false);
}
EXPORT_SYMBOL_GPL(power_supply_get_property_direct);
/** * power_supply_set_property_direct - Write a power supply property without checking for extensions * @psy: The power supply * @psp: The power supply property to write * @val: The value to write to the power supply property * * Write a power supply property without taking into account any power supply extensions registered * on the given power supply. This is mostly useful for power supply extensions that want to access * their own power supply as using power_supply_set_property() directly will result in a potential * deadlock. * * Return: 0 on success or negative error code on failure.
*/ int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp, constunion power_supply_propval *val)
{ return __power_supply_set_property(psy, psp, val, false);
}
EXPORT_SYMBOL_GPL(power_supply_set_property_direct);
rc = power_supply_check_supplies(psy); if (rc) {
dev_dbg(dev, "Not all required supplies found, defer probe\n"); goto check_supplies_failed;
}
/* * Expose constant battery info, if it is available. While there are * some chargers accessing constant battery data, we only want to * expose battery data to userspace for battery devices.
*/ if (desc->type == POWER_SUPPLY_TYPE_BATTERY) {
rc = power_supply_get_battery_info(psy, &psy->battery_info); if (rc && rc != -ENODEV && rc != -ENOENT) goto check_supplies_failed;
}
/* * Update use_cnt after any uevents (most notably from device_add()). * We are here still during driver's probe but * the power_supply_uevent() calls back driver's get_property * method so: * 1. Driver did not assigned the returned struct power_supply, * 2. Driver could not finish initialization (anything in its probe * after calling power_supply_register()).
*/
atomic_inc(&psy->use_cnt);
psy->initialized = true;
/** * power_supply_register() - Register new power supply * @parent: Device to be a parent of power supply's device, usually * the device which probe function calls this * @desc: Description of power supply, must be valid through whole * lifetime of this power supply * @cfg: Run-time specific configuration accessed during registering, * may be NULL * * Return: A pointer to newly allocated power_supply on success * or ERR_PTR otherwise. * Use power_supply_unregister() on returned power_supply pointer to release * resources.
*/ struct power_supply *__must_check power_supply_register(struct device *parent, conststruct power_supply_desc *desc, conststruct power_supply_config *cfg)
{ return __power_supply_register(parent, desc, cfg);
}
EXPORT_SYMBOL_GPL(power_supply_register);
/** * devm_power_supply_register() - Register managed power supply * @parent: Device to be a parent of power supply's device, usually * the device which probe function calls this * @desc: Description of power supply, must be valid through whole * lifetime of this power supply * @cfg: Run-time specific configuration accessed during registering, * may be NULL * * Return: A pointer to newly allocated power_supply on success * or ERR_PTR otherwise. * The returned power_supply pointer will be automatically unregistered * on driver detach.
*/ struct power_supply *__must_check
devm_power_supply_register(struct device *parent, conststruct power_supply_desc *desc, conststruct power_supply_config *cfg)
{ struct power_supply **ptr, *psy;
/** * power_supply_unregister() - Remove this power supply from system * @psy: Pointer to power supply to unregister * * Remove this power supply from the system. The resources of power supply * will be freed here or on last power_supply_put() call.
*/ void power_supply_unregister(struct power_supply *psy)
{
WARN_ON(atomic_dec_return(&psy->use_cnt));
psy->removing = true;
cancel_work_sync(&psy->changed_work);
cancel_delayed_work_sync(&psy->deferred_register_work);
sysfs_remove_link(&psy->dev.kobj, "powers");
power_supply_remove_hwmon_sysfs(psy);
power_supply_remove_triggers(psy);
psy_unregister_thermal(psy);
device_init_wakeup(&psy->dev, false);
device_unregister(&psy->dev);
}
EXPORT_SYMBOL_GPL(power_supply_unregister);
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.