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

power: supply: core: make power_supply_class constant

Since commit 43a7206b0963 ("driver core: class: make class_register() take
a const *"), the driver core allows for struct class to be in read-only
memory, so move the power_supply_class structure to be declared at build
time placing it into read-only memory, instead of having to be dynamically
allocated at boot time.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Ricardo B. Marliere <ricardo@marliere.net>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20240301-class_cleanup-power-v1-1-97e0b7bf9c94@marliere.net
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>

authored by

Ricardo B. Marliere and committed by
Sebastian Reichel
71c2cc5c 9a451f1b

+27 -24
+1 -1
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, 620 + class_for_each_device(&power_supply_class, NULL, psy, 621 621 ab8500_btemp_get_ext_psy_data); 622 622 } 623 623
+1 -1
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, 1234 + class_for_each_device(&power_supply_class, NULL, 1235 1235 di->chargalg_psy, ab8500_chargalg_get_ext_psy_data); 1236 1236 1237 1237 ab8500_chargalg_end_of_charge(di);
+1 -1
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, 1952 + class_for_each_device(&power_supply_class, NULL, 1953 1953 &di->usb_chg, ab8500_charger_get_ext_psy_data); 1954 1954 1955 1955 /* First run old_vbat is 0. */
+1 -1
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, 2410 + class_for_each_device(&power_supply_class, NULL, psy, 2411 2411 ab8500_fg_get_ext_psy_data); 2412 2412 } 2413 2413
+1 -1
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, 82 + error = class_for_each_device(&power_supply_class, NULL, &bp, 83 83 __find_main_battery); 84 84 if (error) { 85 85 main_battery = bp.main;
+21 -18
drivers/power/supply/power_supply_core.c
··· 26 26 #include "samsung-sdi-battery.h" 27 27 28 28 /* exported for the APM Power driver, APM emulation */ 29 - struct class *power_supply_class; 29 + const struct class power_supply_class = { 30 + .name = "power_supply", 31 + .dev_uevent = power_supply_uevent, 32 + }; 30 33 EXPORT_SYMBOL_GPL(power_supply_class); 31 34 32 35 static BLOCKING_NOTIFIER_HEAD(power_supply_notifier); ··· 100 97 if (likely(psy->changed)) { 101 98 psy->changed = false; 102 99 spin_unlock_irqrestore(&psy->changed_lock, flags); 103 - class_for_each_device(power_supply_class, NULL, psy, 100 + class_for_each_device(&power_supply_class, NULL, psy, 104 101 __power_supply_changed_work); 105 102 power_supply_update_leds(psy); 106 103 blocking_notifier_call_chain(&power_supply_notifier, ··· 194 191 { 195 192 int error; 196 193 197 - error = class_for_each_device(power_supply_class, NULL, psy, 194 + error = class_for_each_device(&power_supply_class, NULL, psy, 198 195 __power_supply_populate_supplied_from); 199 196 200 197 dev_dbg(&psy->dev, "%s %d\n", __func__, error); ··· 229 226 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if 230 227 * it returned 0, or error as returned by it. 231 228 */ 232 - error = class_for_each_device(power_supply_class, NULL, supply_node, 233 - __power_supply_find_supply_from_node); 229 + error = class_for_each_device(&power_supply_class, NULL, supply_node, 230 + __power_supply_find_supply_from_node); 234 231 235 232 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; 236 233 } ··· 336 333 struct psy_am_i_supplied_data data = { psy, 0 }; 337 334 int error; 338 335 339 - error = class_for_each_device(power_supply_class, NULL, &data, 336 + error = class_for_each_device(&power_supply_class, NULL, &data, 340 337 __power_supply_am_i_supplied); 341 338 342 339 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error); ··· 372 369 int error; 373 370 unsigned int count = 0; 374 371 375 - error = class_for_each_device(power_supply_class, NULL, &count, 372 + error = class_for_each_device(&power_supply_class, NULL, &count, 376 373 __power_supply_is_system_supplied); 377 374 378 375 /* ··· 419 416 * This function is not intended for use with a supply with multiple 420 417 * suppliers, we simply pick the first supply to report the psp. 421 418 */ 422 - ret = class_for_each_device(power_supply_class, NULL, &data, 419 + ret = class_for_each_device(&power_supply_class, NULL, &data, 423 420 __power_supply_get_supplier_property); 424 421 if (ret < 0) 425 422 return ret; ··· 465 462 struct power_supply *power_supply_get_by_name(const char *name) 466 463 { 467 464 struct power_supply *psy = NULL; 468 - struct device *dev = class_find_device(power_supply_class, NULL, name, 469 - power_supply_match_device_by_name); 465 + struct device *dev = class_find_device(&power_supply_class, NULL, name, 466 + power_supply_match_device_by_name); 470 467 471 468 if (dev) { 472 469 psy = dev_get_drvdata(dev); ··· 522 519 if (!power_supply_np) 523 520 return ERR_PTR(-ENODEV); 524 521 525 - dev = class_find_device(power_supply_class, NULL, power_supply_np, 526 - power_supply_match_device_node); 522 + dev = class_find_device(&power_supply_class, NULL, power_supply_np, 523 + power_supply_match_device_node); 527 524 528 525 of_node_put(power_supply_np); 529 526 ··· 1376 1373 1377 1374 device_initialize(dev); 1378 1375 1379 - dev->class = power_supply_class; 1376 + dev->class = &power_supply_class; 1380 1377 dev->type = &power_supply_dev_type; 1381 1378 dev->parent = parent; 1382 1379 dev->release = power_supply_dev_release; ··· 1624 1621 1625 1622 static int __init power_supply_class_init(void) 1626 1623 { 1627 - power_supply_class = class_create("power_supply"); 1624 + int err; 1628 1625 1629 - if (IS_ERR(power_supply_class)) 1630 - return PTR_ERR(power_supply_class); 1626 + err = class_register(&power_supply_class); 1627 + if (err) 1628 + return err; 1631 1629 1632 - power_supply_class->dev_uevent = power_supply_uevent; 1633 1630 power_supply_init_attrs(); 1634 1631 1635 1632 return 0; ··· 1637 1634 1638 1635 static void __exit power_supply_class_exit(void) 1639 1636 { 1640 - class_destroy(power_supply_class); 1637 + class_unregister(&power_supply_class); 1641 1638 } 1642 1639 1643 1640 subsys_initcall(power_supply_class_init);
+1 -1
include/linux/power_supply.h
··· 895 895 896 896 extern void *power_supply_get_drvdata(struct power_supply *psy); 897 897 /* For APM emulation, think legacy userspace. */ 898 - extern struct class *power_supply_class; 898 + extern const struct class power_supply_class; 899 899 900 900 static inline bool power_supply_is_amp_property(enum power_supply_property psp) 901 901 {