Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: Ricardo B. Marliere <ricardo@marliere.net>
Link: https://lore.kernel.org/r/20240301-psy-class-cleanup-v1-1-aebe8c4b6b08@collabora.com
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>

+22 -30
+1 -2
drivers/power/supply/ab8500_btemp.c
··· 617 617 */ 618 618 static void ab8500_btemp_external_power_changed(struct power_supply *psy) 619 619 { 620 - class_for_each_device(&power_supply_class, NULL, psy, 621 - ab8500_btemp_get_ext_psy_data); 620 + power_supply_for_each_device(psy, ab8500_btemp_get_ext_psy_data); 622 621 } 623 622 624 623 /* ab8500 btemp driver interrupts and their respective isr */
+1 -2
drivers/power/supply/ab8500_chargalg.c
··· 1231 1231 int ret; 1232 1232 1233 1233 /* Collect data from all power_supply class devices */ 1234 - class_for_each_device(&power_supply_class, NULL, 1235 - di->chargalg_psy, ab8500_chargalg_get_ext_psy_data); 1234 + power_supply_for_each_device(di->chargalg_psy, ab8500_chargalg_get_ext_psy_data); 1236 1235 1237 1236 ab8500_chargalg_end_of_charge(di); 1238 1237 ab8500_chargalg_check_temp(di);
+1 -2
drivers/power/supply/ab8500_charger.c
··· 1949 1949 struct ab8500_charger *di = container_of(work, 1950 1950 struct ab8500_charger, check_vbat_work.work); 1951 1951 1952 - class_for_each_device(&power_supply_class, NULL, 1953 - &di->usb_chg, ab8500_charger_get_ext_psy_data); 1952 + power_supply_for_each_device(&di->usb_chg, ab8500_charger_get_ext_psy_data); 1954 1953 1955 1954 /* First run old_vbat is 0. */ 1956 1955 if (di->old_vbat == 0)
+1 -2
drivers/power/supply/ab8500_fg.c
··· 2407 2407 */ 2408 2408 static void ab8500_fg_external_power_changed(struct power_supply *psy) 2409 2409 { 2410 - class_for_each_device(&power_supply_class, NULL, psy, 2411 - ab8500_fg_get_ext_psy_data); 2410 + power_supply_for_each_device(psy, ab8500_fg_get_ext_psy_data); 2412 2411 } 2413 2412 2414 2413 /**
+1 -2
drivers/power/supply/apm_power.c
··· 79 79 main_battery = NULL; 80 80 bp.main = main_battery; 81 81 82 - error = class_for_each_device(&power_supply_class, NULL, &bp, 83 - __find_main_battery); 82 + error = power_supply_for_each_device(&bp, __find_main_battery); 84 83 if (error) { 85 84 main_battery = bp.main; 86 85 return;
+16 -18
drivers/power/supply/power_supply_core.c
··· 25 25 #include "power_supply.h" 26 26 #include "samsung-sdi-battery.h" 27 27 28 - /* exported for the APM Power driver, APM emulation */ 29 - const struct class power_supply_class = { 28 + static const struct class power_supply_class = { 30 29 .name = "power_supply", 31 30 .dev_uevent = power_supply_uevent, 32 31 }; 33 - EXPORT_SYMBOL_GPL(power_supply_class); 34 32 35 33 static BLOCKING_NOTIFIER_HEAD(power_supply_notifier); 36 34 ··· 98 100 if (likely(psy->changed)) { 99 101 psy->changed = false; 100 102 spin_unlock_irqrestore(&psy->changed_lock, flags); 101 - class_for_each_device(&power_supply_class, NULL, psy, 102 - __power_supply_changed_work); 103 + power_supply_for_each_device(psy, __power_supply_changed_work); 103 104 power_supply_update_leds(psy); 104 105 blocking_notifier_call_chain(&power_supply_notifier, 105 106 PSY_EVENT_PROP_CHANGED, psy); ··· 115 118 pm_relax(&psy->dev); 116 119 spin_unlock_irqrestore(&psy->changed_lock, flags); 117 120 } 121 + 122 + int power_supply_for_each_device(void *data, int (*fn)(struct device *dev, void *data)) 123 + { 124 + return class_for_each_device(&power_supply_class, NULL, data, fn); 125 + } 126 + EXPORT_SYMBOL_GPL(power_supply_for_each_device); 118 127 119 128 void power_supply_changed(struct power_supply *psy) 120 129 { ··· 197 194 { 198 195 int error; 199 196 200 - error = class_for_each_device(&power_supply_class, NULL, psy, 201 - __power_supply_populate_supplied_from); 197 + error = power_supply_for_each_device(psy, __power_supply_populate_supplied_from); 202 198 203 199 dev_dbg(&psy->dev, "%s %d\n", __func__, error); 204 200 ··· 210 208 struct device_node *np = data; 211 209 struct power_supply *epsy = dev_get_drvdata(dev); 212 210 213 - /* returning non-zero breaks out of class_for_each_device loop */ 211 + /* returning non-zero breaks out of power_supply_for_each_device loop */ 214 212 if (epsy->of_node == np) 215 213 return 1; 216 214 ··· 222 220 int error; 223 221 224 222 /* 225 - * class_for_each_device() either returns its own errors or values 223 + * power_supply_for_each_device() either returns its own errors or values 226 224 * returned by __power_supply_find_supply_from_node(). 227 225 * 228 226 * __power_supply_find_supply_from_node() will return 0 (no match) 229 227 * or 1 (match). 230 228 * 231 - * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if 229 + * We return 0 if power_supply_for_each_device() returned 1, -EPROBE_DEFER if 232 230 * it returned 0, or error as returned by it. 233 231 */ 234 - error = class_for_each_device(&power_supply_class, NULL, supply_node, 235 - __power_supply_find_supply_from_node); 232 + error = power_supply_for_each_device(supply_node, __power_supply_find_supply_from_node); 236 233 237 234 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; 238 235 } ··· 337 336 struct psy_am_i_supplied_data data = { psy, 0 }; 338 337 int error; 339 338 340 - error = class_for_each_device(&power_supply_class, NULL, &data, 341 - __power_supply_am_i_supplied); 339 + error = power_supply_for_each_device(&data, __power_supply_am_i_supplied); 342 340 343 341 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error); 344 342 ··· 372 372 int error; 373 373 unsigned int count = 0; 374 374 375 - error = class_for_each_device(&power_supply_class, NULL, &count, 376 - __power_supply_is_system_supplied); 375 + error = power_supply_for_each_device(&count, __power_supply_is_system_supplied); 377 376 378 377 /* 379 378 * If no system scope power class device was found at all, most probably we ··· 418 419 * This function is not intended for use with a supply with multiple 419 420 * suppliers, we simply pick the first supply to report the psp. 420 421 */ 421 - ret = class_for_each_device(&power_supply_class, NULL, &data, 422 - __power_supply_get_supplier_property); 422 + ret = power_supply_for_each_device(&data, __power_supply_get_supplier_property); 423 423 if (ret < 0) 424 424 return ret; 425 425 if (ret == 0)
+1 -2
include/linux/power_supply.h
··· 894 894 #define to_power_supply(device) container_of(device, struct power_supply, dev) 895 895 896 896 extern void *power_supply_get_drvdata(struct power_supply *psy); 897 - /* For APM emulation, think legacy userspace. */ 898 - extern const struct class power_supply_class; 897 + extern int power_supply_for_each_device(void *data, int (*fn)(struct device *dev, void *data)); 899 898 900 899 static inline bool power_supply_is_amp_property(enum power_supply_property psp) 901 900 {