power: supply: core: make power_supply_class constant
authorRicardo B. Marliere <ricardo@marliere.net>
Fri, 1 Mar 2024 17:46:15 +0000 (14:46 -0300)
committerSebastian Reichel <sebastian.reichel@collabora.com>
Fri, 1 Mar 2024 20:16:01 +0000 (21:16 +0100)
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>
drivers/power/supply/ab8500_btemp.c
drivers/power/supply/ab8500_chargalg.c
drivers/power/supply/ab8500_charger.c
drivers/power/supply/ab8500_fg.c
drivers/power/supply/apm_power.c
drivers/power/supply/power_supply_core.c
include/linux/power_supply.h

index 7905eba93dead1b6be92875edf0685838bb64b0e..41dba40fffdfce8d47fdeb12957f424cb62e7ca8 100644 (file)
@@ -617,7 +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)
 {
-       class_for_each_device(power_supply_class, NULL, psy,
+       class_for_each_device(&power_supply_class, NULL, psy,
                              ab8500_btemp_get_ext_psy_data);
 }
 
index de912658facb921a5f90fd78c17b1a7f2190b530..329ae784a72d4d5ab0f641baed3bed4b93b42b1e 100644 (file)
@@ -1231,7 +1231,7 @@ static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di)
        int ret;
 
        /* Collect data from all power_supply class devices */
-       class_for_each_device(power_supply_class, NULL,
+       class_for_each_device(&power_supply_class, NULL,
                di->chargalg_psy, ab8500_chargalg_get_ext_psy_data);
 
        ab8500_chargalg_end_of_charge(di);
index d72f32c663bc0ea240f5fff5dfe49049f2d98e2b..1c2b69bbed17785271bc651d0c890aca32545574 100644 (file)
@@ -1949,7 +1949,7 @@ static void ab8500_charger_check_vbat_work(struct work_struct *work)
        struct ab8500_charger *di = container_of(work,
                struct ab8500_charger, check_vbat_work.work);
 
-       class_for_each_device(power_supply_class, NULL,
+       class_for_each_device(&power_supply_class, NULL,
                              &di->usb_chg, ab8500_charger_get_ext_psy_data);
 
        /* First run old_vbat is 0. */
index 8c593fbdd45a7317f7e8ce85cfe7af8fcd509318..e49e704023e1b66233925f3e7cb20b2959478ffa 100644 (file)
@@ -2407,7 +2407,7 @@ out:
  */
 static void ab8500_fg_external_power_changed(struct power_supply *psy)
 {
-       class_for_each_device(power_supply_class, NULL, psy,
+       class_for_each_device(&power_supply_class, NULL, psy,
                              ab8500_fg_get_ext_psy_data);
 }
 
index 9d1a7fbcaed4226d1893ea610299dcff31687f07..034f28699977ac173ba7a284a34ec1a7add965f7 100644 (file)
@@ -79,7 +79,7 @@ static void find_main_battery(void)
        main_battery = NULL;
        bp.main = main_battery;
 
-       error = class_for_each_device(power_supply_class, NULL, &bp,
+       error = class_for_each_device(&power_supply_class, NULL, &bp,
                                      __find_main_battery);
        if (error) {
                main_battery = bp.main;
index 37dac766909008d61e76815bb45903248169e1a0..4f27f17f8741fdca773a72fbf265f84de1090ba9 100644 (file)
 #include "samsung-sdi-battery.h"
 
 /* exported for the APM Power driver, APM emulation */
-struct class *power_supply_class;
+const struct class power_supply_class = {
+       .name = "power_supply",
+       .dev_uevent = power_supply_uevent,
+};
 EXPORT_SYMBOL_GPL(power_supply_class);
 
 static BLOCKING_NOTIFIER_HEAD(power_supply_notifier);
@@ -97,7 +100,7 @@ static void power_supply_changed_work(struct work_struct *work)
        if (likely(psy->changed)) {
                psy->changed = false;
                spin_unlock_irqrestore(&psy->changed_lock, flags);
-               class_for_each_device(power_supply_class, NULL, psy,
+               class_for_each_device(&power_supply_class, NULL, psy,
                                      __power_supply_changed_work);
                power_supply_update_leds(psy);
                blocking_notifier_call_chain(&power_supply_notifier,
@@ -191,7 +194,7 @@ static int power_supply_populate_supplied_from(struct power_supply *psy)
 {
        int error;
 
-       error = class_for_each_device(power_supply_class, NULL, psy,
+       error = class_for_each_device(&power_supply_class, NULL, psy,
                                      __power_supply_populate_supplied_from);
 
        dev_dbg(&psy->dev, "%s %d\n", __func__, error);
@@ -226,8 +229,8 @@ static int power_supply_find_supply_from_node(struct device_node *supply_node)
         * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
         * it returned 0, or error as returned by it.
         */
-       error = class_for_each_device(power_supply_class, NULL, supply_node,
-                                      __power_supply_find_supply_from_node);
+       error = class_for_each_device(&power_supply_class, NULL, supply_node,
+                                     __power_supply_find_supply_from_node);
 
        return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
 }
@@ -333,7 +336,7 @@ int power_supply_am_i_supplied(struct power_supply *psy)
        struct psy_am_i_supplied_data data = { psy, 0 };
        int error;
 
-       error = class_for_each_device(power_supply_class, NULL, &data,
+       error = class_for_each_device(&power_supply_class, NULL, &data,
                                      __power_supply_am_i_supplied);
 
        dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
@@ -369,7 +372,7 @@ int power_supply_is_system_supplied(void)
        int error;
        unsigned int count = 0;
 
-       error = class_for_each_device(power_supply_class, NULL, &count,
+       error = class_for_each_device(&power_supply_class, NULL, &count,
                                      __power_supply_is_system_supplied);
 
        /*
@@ -416,7 +419,7 @@ int power_supply_get_property_from_supplier(struct power_supply *psy,
         * This function is not intended for use with a supply with multiple
         * suppliers, we simply pick the first supply to report the psp.
         */
-       ret = class_for_each_device(power_supply_class, NULL, &data,
+       ret = class_for_each_device(&power_supply_class, NULL, &data,
                                    __power_supply_get_supplier_property);
        if (ret < 0)
                return ret;
@@ -462,8 +465,8 @@ static int power_supply_match_device_by_name(struct device *dev, const void *dat
 struct power_supply *power_supply_get_by_name(const char *name)
 {
        struct power_supply *psy = NULL;
-       struct device *dev = class_find_device(power_supply_class, NULL, name,
-                                       power_supply_match_device_by_name);
+       struct device *dev = class_find_device(&power_supply_class, NULL, name,
+                                              power_supply_match_device_by_name);
 
        if (dev) {
                psy = dev_get_drvdata(dev);
@@ -519,8 +522,8 @@ struct power_supply *power_supply_get_by_phandle(struct device_node *np,
        if (!power_supply_np)
                return ERR_PTR(-ENODEV);
 
-       dev = class_find_device(power_supply_class, NULL, power_supply_np,
-                                               power_supply_match_device_node);
+       dev = class_find_device(&power_supply_class, NULL, power_supply_np,
+                               power_supply_match_device_node);
 
        of_node_put(power_supply_np);
 
@@ -1373,7 +1376,7 @@ __power_supply_register(struct device *parent,
 
        device_initialize(dev);
 
-       dev->class = power_supply_class;
+       dev->class = &power_supply_class;
        dev->type = &power_supply_dev_type;
        dev->parent = parent;
        dev->release = power_supply_dev_release;
@@ -1621,12 +1624,12 @@ EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
 
 static int __init power_supply_class_init(void)
 {
-       power_supply_class = class_create("power_supply");
+       int err;
 
-       if (IS_ERR(power_supply_class))
-               return PTR_ERR(power_supply_class);
+       err = class_register(&power_supply_class);
+       if (err)
+               return err;
 
-       power_supply_class->dev_uevent = power_supply_uevent;
        power_supply_init_attrs();
 
        return 0;
@@ -1634,7 +1637,7 @@ static int __init power_supply_class_init(void)
 
 static void __exit power_supply_class_exit(void)
 {
-       class_destroy(power_supply_class);
+       class_unregister(&power_supply_class);
 }
 
 subsys_initcall(power_supply_class_init);
index c0992a77feeaad8e44c7cdfcf5da9196dab39826..514f652de64d0a2f3bce7013a91a7e61ada79044 100644 (file)
@@ -895,7 +895,7 @@ extern int power_supply_powers(struct power_supply *psy, struct device *dev);
 
 extern void *power_supply_get_drvdata(struct power_supply *psy);
 /* For APM emulation, think legacy userspace. */
-extern struct class *power_supply_class;
+extern const struct class power_supply_class;
 
 static inline bool power_supply_is_amp_property(enum power_supply_property psp)
 {