Commit 68ade097 authored by Sebastian Reichel's avatar Sebastian Reichel

power: supply: core: add power_supply_for_each_device()

Introduce power_supply_for_each_device(), which is a wrapper
for class_for_each_device() using the power_supply_class and
going through all devices.

This allows making the power_supply_class itself a local
variable, so that drivers cannot mess with it and simplifies
the code slightly.
Reviewed-by: default avatarRicardo B. Marliere <ricardo@marliere.net>
Link: https://lore.kernel.org/r/20240301-psy-class-cleanup-v1-1-aebe8c4b6b08@collabora.comSigned-off-by: default avatarSebastian Reichel <sebastian.reichel@collabora.com>
parent 71c2cc5c
...@@ -617,8 +617,7 @@ static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data) ...@@ -617,8 +617,7 @@ static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
*/ */
static void ab8500_btemp_external_power_changed(struct power_supply *psy) static void ab8500_btemp_external_power_changed(struct power_supply *psy)
{ {
class_for_each_device(&power_supply_class, NULL, psy, power_supply_for_each_device(psy, ab8500_btemp_get_ext_psy_data);
ab8500_btemp_get_ext_psy_data);
} }
/* ab8500 btemp driver interrupts and their respective isr */ /* ab8500 btemp driver interrupts and their respective isr */
......
...@@ -1231,8 +1231,7 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di) ...@@ -1231,8 +1231,7 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di)
int ret; int ret;
/* Collect data from all power_supply class devices */ /* Collect data from all power_supply class devices */
class_for_each_device(&power_supply_class, NULL, power_supply_for_each_device(di->chargalg_psy, ab8500_chargalg_get_ext_psy_data);
di->chargalg_psy, ab8500_chargalg_get_ext_psy_data);
ab8500_chargalg_end_of_charge(di); ab8500_chargalg_end_of_charge(di);
ab8500_chargalg_check_temp(di); ab8500_chargalg_check_temp(di);
......
...@@ -1949,8 +1949,7 @@ static void ab8500_charger_check_vbat_work(struct work_struct *work) ...@@ -1949,8 +1949,7 @@ static void ab8500_charger_check_vbat_work(struct work_struct *work)
struct ab8500_charger *di = container_of(work, struct ab8500_charger *di = container_of(work,
struct ab8500_charger, check_vbat_work.work); struct ab8500_charger, check_vbat_work.work);
class_for_each_device(&power_supply_class, NULL, power_supply_for_each_device(&di->usb_chg, ab8500_charger_get_ext_psy_data);
&di->usb_chg, ab8500_charger_get_ext_psy_data);
/* First run old_vbat is 0. */ /* First run old_vbat is 0. */
if (di->old_vbat == 0) if (di->old_vbat == 0)
......
...@@ -2407,8 +2407,7 @@ static int ab8500_fg_init_hw_registers(struct ab8500_fg *di) ...@@ -2407,8 +2407,7 @@ static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
*/ */
static void ab8500_fg_external_power_changed(struct power_supply *psy) static void ab8500_fg_external_power_changed(struct power_supply *psy)
{ {
class_for_each_device(&power_supply_class, NULL, psy, power_supply_for_each_device(psy, ab8500_fg_get_ext_psy_data);
ab8500_fg_get_ext_psy_data);
} }
/** /**
......
...@@ -79,8 +79,7 @@ static void find_main_battery(void) ...@@ -79,8 +79,7 @@ static void find_main_battery(void)
main_battery = NULL; main_battery = NULL;
bp.main = main_battery; bp.main = main_battery;
error = class_for_each_device(&power_supply_class, NULL, &bp, error = power_supply_for_each_device(&bp, __find_main_battery);
__find_main_battery);
if (error) { if (error) {
main_battery = bp.main; main_battery = bp.main;
return; return;
......
...@@ -25,12 +25,10 @@ ...@@ -25,12 +25,10 @@
#include "power_supply.h" #include "power_supply.h"
#include "samsung-sdi-battery.h" #include "samsung-sdi-battery.h"
/* exported for the APM Power driver, APM emulation */ static const struct class power_supply_class = {
const struct class power_supply_class = {
.name = "power_supply", .name = "power_supply",
.dev_uevent = power_supply_uevent, .dev_uevent = power_supply_uevent,
}; };
EXPORT_SYMBOL_GPL(power_supply_class);
static BLOCKING_NOTIFIER_HEAD(power_supply_notifier); static BLOCKING_NOTIFIER_HEAD(power_supply_notifier);
...@@ -100,8 +98,7 @@ static void power_supply_changed_work(struct work_struct *work) ...@@ -100,8 +98,7 @@ static void power_supply_changed_work(struct work_struct *work)
if (likely(psy->changed)) { if (likely(psy->changed)) {
psy->changed = false; psy->changed = false;
spin_unlock_irqrestore(&psy->changed_lock, flags); spin_unlock_irqrestore(&psy->changed_lock, flags);
class_for_each_device(&power_supply_class, NULL, psy, power_supply_for_each_device(psy, __power_supply_changed_work);
__power_supply_changed_work);
power_supply_update_leds(psy); power_supply_update_leds(psy);
blocking_notifier_call_chain(&power_supply_notifier, blocking_notifier_call_chain(&power_supply_notifier,
PSY_EVENT_PROP_CHANGED, psy); PSY_EVENT_PROP_CHANGED, psy);
...@@ -119,6 +116,12 @@ static void power_supply_changed_work(struct work_struct *work) ...@@ -119,6 +116,12 @@ static void power_supply_changed_work(struct work_struct *work)
spin_unlock_irqrestore(&psy->changed_lock, flags); spin_unlock_irqrestore(&psy->changed_lock, flags);
} }
int power_supply_for_each_device(void *data, int (*fn)(struct device *dev, void *data))
{
return class_for_each_device(&power_supply_class, NULL, data, fn);
}
EXPORT_SYMBOL_GPL(power_supply_for_each_device);
void power_supply_changed(struct power_supply *psy) void power_supply_changed(struct power_supply *psy)
{ {
unsigned long flags; unsigned long flags;
...@@ -194,8 +197,7 @@ static int power_supply_populate_supplied_from(struct power_supply *psy) ...@@ -194,8 +197,7 @@ static int power_supply_populate_supplied_from(struct power_supply *psy)
{ {
int error; int error;
error = class_for_each_device(&power_supply_class, NULL, psy, error = power_supply_for_each_device(psy, __power_supply_populate_supplied_from);
__power_supply_populate_supplied_from);
dev_dbg(&psy->dev, "%s %d\n", __func__, error); dev_dbg(&psy->dev, "%s %d\n", __func__, error);
...@@ -208,7 +210,7 @@ static int __power_supply_find_supply_from_node(struct device *dev, ...@@ -208,7 +210,7 @@ static int __power_supply_find_supply_from_node(struct device *dev,
struct device_node *np = data; struct device_node *np = data;
struct power_supply *epsy = dev_get_drvdata(dev); struct power_supply *epsy = dev_get_drvdata(dev);
/* returning non-zero breaks out of class_for_each_device loop */ /* returning non-zero breaks out of power_supply_for_each_device loop */
if (epsy->of_node == np) if (epsy->of_node == np)
return 1; return 1;
...@@ -220,17 +222,16 @@ static int power_supply_find_supply_from_node(struct device_node *supply_node) ...@@ -220,17 +222,16 @@ static int power_supply_find_supply_from_node(struct device_node *supply_node)
int error; int error;
/* /*
* class_for_each_device() either returns its own errors or values * power_supply_for_each_device() either returns its own errors or values
* returned by __power_supply_find_supply_from_node(). * returned by __power_supply_find_supply_from_node().
* *
* __power_supply_find_supply_from_node() will return 0 (no match) * __power_supply_find_supply_from_node() will return 0 (no match)
* or 1 (match). * or 1 (match).
* *
* We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if * We return 0 if power_supply_for_each_device() returned 1, -EPROBE_DEFER if
* it returned 0, or error as returned by it. * it returned 0, or error as returned by it.
*/ */
error = class_for_each_device(&power_supply_class, NULL, supply_node, error = power_supply_for_each_device(supply_node, __power_supply_find_supply_from_node);
__power_supply_find_supply_from_node);
return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
} }
...@@ -336,8 +337,7 @@ int power_supply_am_i_supplied(struct power_supply *psy) ...@@ -336,8 +337,7 @@ int power_supply_am_i_supplied(struct power_supply *psy)
struct psy_am_i_supplied_data data = { psy, 0 }; struct psy_am_i_supplied_data data = { psy, 0 };
int error; int error;
error = class_for_each_device(&power_supply_class, NULL, &data, error = power_supply_for_each_device(&data, __power_supply_am_i_supplied);
__power_supply_am_i_supplied);
dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error); dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
...@@ -372,8 +372,7 @@ int power_supply_is_system_supplied(void) ...@@ -372,8 +372,7 @@ int power_supply_is_system_supplied(void)
int error; int error;
unsigned int count = 0; unsigned int count = 0;
error = class_for_each_device(&power_supply_class, NULL, &count, error = power_supply_for_each_device(&count, __power_supply_is_system_supplied);
__power_supply_is_system_supplied);
/* /*
* If no system scope power class device was found at all, most probably we * If no system scope power class device was found at all, most probably we
...@@ -419,8 +418,7 @@ int power_supply_get_property_from_supplier(struct power_supply *psy, ...@@ -419,8 +418,7 @@ int power_supply_get_property_from_supplier(struct power_supply *psy,
* This function is not intended for use with a supply with multiple * This function is not intended for use with a supply with multiple
* suppliers, we simply pick the first supply to report the psp. * suppliers, we simply pick the first supply to report the psp.
*/ */
ret = class_for_each_device(&power_supply_class, NULL, &data, ret = power_supply_for_each_device(&data, __power_supply_get_supplier_property);
__power_supply_get_supplier_property);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (ret == 0) if (ret == 0)
......
...@@ -894,8 +894,7 @@ extern int power_supply_powers(struct power_supply *psy, struct device *dev); ...@@ -894,8 +894,7 @@ extern int power_supply_powers(struct power_supply *psy, struct device *dev);
#define to_power_supply(device) container_of(device, struct power_supply, dev) #define to_power_supply(device) container_of(device, struct power_supply, dev)
extern void *power_supply_get_drvdata(struct power_supply *psy); extern void *power_supply_get_drvdata(struct power_supply *psy);
/* For APM emulation, think legacy userspace. */ extern int power_supply_for_each_device(void *data, int (*fn)(struct device *dev, void *data));
extern const struct class power_supply_class;
static inline bool power_supply_is_amp_property(enum power_supply_property psp) static inline bool power_supply_is_amp_property(enum power_supply_property psp)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment