iio: tsl2772: Use devm_add_action_or_reset for tsl2772_chip_off
authorChuhong Yuan <hslester96@gmail.com>
Thu, 1 Aug 2019 07:36:05 +0000 (15:36 +0800)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 5 Aug 2019 15:28:29 +0000 (16:28 +0100)
Use devm_add_action_or_reset to call tsl2772_chip_off
when the device is removed.
This also fixes the issue that the chip is turned off
before the device is unregistered.

Not marked for stable as fairly hard to hit the bug and
this is in the middle of a set making other cleanups
to the driver.  Hence will probably need explicit backporting.

Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
Fixes: c06c4d793584 ("staging: iio: tsl2x7x/tsl2772: move out of staging")
Reviewed-by: Brian Masney <masneyb@onstation.org>
Tested-by: Brian Masney <masneyb@onstation.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/light/tsl2772.c

index 29cfd8ae2700809233d0358bbca96a502e3a7366..e866ae40f1571a241cc9ceb2e3495b7e4e293881 100644 (file)
@@ -860,6 +860,13 @@ static int tsl2772_chip_off(struct iio_dev *indio_dev)
        return tsl2772_write_control_reg(chip, 0x00);
 }
 
+static void tsl2772_chip_off_action(void *data)
+{
+       struct iio_dev *indio_dev = data;
+
+       tsl2772_chip_off(indio_dev);
+}
+
 /**
  * tsl2772_invoke_change - power cycle the device to implement the user
  *                         parameters
@@ -1877,9 +1884,14 @@ static int tsl2772_probe(struct i2c_client *clientp,
        if (ret < 0)
                return ret;
 
+       ret = devm_add_action_or_reset(&clientp->dev,
+                                       tsl2772_chip_off_action,
+                                       indio_dev);
+       if (ret < 0)
+               return ret;
+
        ret = iio_device_register(indio_dev);
        if (ret) {
-               tsl2772_chip_off(indio_dev);
                dev_err(&clientp->dev,
                        "%s: iio registration failed\n", __func__);
                return ret;
@@ -1926,8 +1938,6 @@ static int tsl2772_remove(struct i2c_client *client)
 {
        struct iio_dev *indio_dev = i2c_get_clientdata(client);
 
-       tsl2772_chip_off(indio_dev);
-
        iio_device_unregister(indio_dev);
 
        return 0;