hwmon: (adt7x10) Add device managed action for restoring config
authorCosmin Tanislav <cosmin.tanislav@analog.com>
Tue, 21 Dec 2021 21:58:36 +0000 (23:58 +0200)
committerGuenter Roeck <linux@roeck-us.net>
Mon, 28 Feb 2022 01:03:18 +0000 (17:03 -0800)
To simplify the core driver remove function.

Signed-off-by: Cosmin Tanislav <cosmin.tanislav@analog.com>
Link: https://lore.kernel.org/r/20211221215841.2641417-3-demonsingur@gmail.com
[groeck: Adjust to use regmap; only register restore function if needed]
Tested-by: Cosmin Tanislav <cosmin.tanislav@analog.com>
Reviewed-by: Cosmin Tanislav <cosmin.tanislav@analog.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/adt7x10.c

index 05dd48b..47ce1a8 100644 (file)
@@ -296,6 +296,13 @@ static const struct attribute_group adt7x10_group = {
        .attrs = adt7x10_attributes,
 };
 
+static void adt7x10_restore_config(void *private)
+{
+       struct adt7x10_data *data = private;
+
+       regmap_write(data->regmap, ADT7X10_CONFIG, data->oldconfig);
+}
+
 int adt7x10_probe(struct device *dev, const char *name, int irq,
                  struct regmap *regmap)
 {
@@ -333,13 +340,16 @@ int adt7x10_probe(struct device *dev, const char *name, int irq,
                ret = regmap_write(regmap, ADT7X10_CONFIG, data->config);
                if (ret)
                        return ret;
+               ret = devm_add_action_or_reset(dev, adt7x10_restore_config, data);
+               if (ret)
+                       return ret;
        }
        dev_dbg(dev, "Config %02x\n", data->config);
 
        /* Register sysfs hooks */
        ret = sysfs_create_group(&dev->kobj, &adt7x10_group);
        if (ret)
-               goto exit_restore;
+               return ret;
 
        /*
         * The I2C device will already have it's own 'name' attribute, but for
@@ -375,8 +385,6 @@ exit_remove_name:
                device_remove_file(dev, &dev_attr_name);
 exit_remove:
        sysfs_remove_group(&dev->kobj, &adt7x10_group);
-exit_restore:
-       regmap_write(regmap, ADT7X10_CONFIG, data->oldconfig);
        return ret;
 }
 EXPORT_SYMBOL_GPL(adt7x10_probe);
@@ -392,8 +400,6 @@ void adt7x10_remove(struct device *dev, int irq)
        if (data->name)
                device_remove_file(dev, &dev_attr_name);
        sysfs_remove_group(&dev->kobj, &adt7x10_group);
-       if (data->oldconfig != data->config)
-               regmap_write(data->regmap, ADT7X10_CONFIG, data->oldconfig);
 }
 EXPORT_SYMBOL_GPL(adt7x10_remove);