iio: adc: mcp320x: Simplify device removal logic
PATCH 1/1 iio: adc: mcp320x: Simplify device removal logic
Use devm_* APIs to enable regulator and to register in IIO infrastructure.
--- drivers/iio/adc/mcp320x.c | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/iio/adc/mcp320x.c +2 -17
@@ -388,7 +388,6 @@ static int mcp320x_probe(struct spi_device *spi) indio_dev->name =spi_get_device_id (spi)->name; indio_dev->modes =INDIO_DIRECT_MODE ; indio_dev->info = &mcp320x_info; -spi_set_drvdata (spi, indio_dev); device_index =spi_get_device_id (spi)->driver_data; chip_info = &mcp320x_chip_infos[device_index];
@@ -441,31 +440,17 @@ static int mcp320x_probe(struct spi_device *spi)if (IS_ERR (adc->reg))return PTR_ERR (adc->reg); - ret =regulator_enable (adc->reg); + ret =devm_regulator_get_enable (&spi->dev,"vref" );if (ret <0 )return ret;mutex_init (&adc->lock); - ret =iio_device_register (indio_dev); -if (ret <0 ) - goto reg_disable; - -return 0 ; - -reg_disable: -regulator_disable (adc->reg); - -return ret; +return devm_iio_device_register (&spi->dev, indio_dev); }static void mcp320x_remove (struct spi_device *spi) { -struct iio_dev *indio_dev =spi_get_drvdata (spi); -struct mcp320x *adc =iio_priv (indio_dev); - -iio_device_unregister (indio_dev); -regulator_disable (adc->reg); }static const struct of_device_id mcp320x_dt_ids[] = {
base-commit: 047371968ffc470769f541d6933e262dc7085456
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerQuoted 3 lines · On Thu, 8 Feb 2024 11:43:39 +0200 Arturas Moskvinas…
Use devm_* APIs to enable regulator and to register in IIO infrastructure.
Signed-off-by Arturas Moskvinas <[email protected]>
Don't get the same regulator twice so as automate turning it off. The devm_regulator_get_enable() call is carefully hiding the regulator for cases where the driver never accesses it.
Here we need it to read the voltage.
To convert such a case to fully devm managed, use a devm_add_action_or_reset() and a custom callback.
Quoted 17 lines
--- drivers/iio/adc/mcp320x.c | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-)drivers/iio/adc/mcp320x.c +0
-1@@ -388,7 +388,6 @@ static int mcp320x_probe(struct spi_device *spi) indio_dev->name =spi_get_device_id (spi)->name; indio_dev->modes =INDIO_DIRECT_MODE ; indio_dev->info = &mcp320x_info; -spi_set_drvdata (spi, indio_dev); device_index =spi_get_device_id (spi)->driver_data; chip_info = &mcp320x_chip_infos[device_index];
drivers/iio/adc/mcp320x.c static int mcp320x_probe(struct spi_device *spi) · line 441
Quoted 2 lines
drivers/iio/adc/mcp320x.c +0
-0@@ -441,31 +440,17 @@ static int mcp320x_probe(struct spi_device *spi)if (IS_ERR (adc->reg))return PTR_ERR (adc->reg);
Just above here is the first regulator get. Whilst it may work we should not get it twice as the logic gets confused at the very least.
Quoted 19 lines
drivers/iio/adc/mcp320x.c +2
-11- ret =regulator_enable (adc->reg); + ret =devm_regulator_get_enable (&spi->dev,"vref" );if (ret <0 )return ret;mutex_init (&adc->lock); - ret =iio_device_register (indio_dev); -if (ret <0 ) - goto reg_disable; - -return 0 ; - -reg_disable: -regulator_disable (adc->reg); - -return ret; +return devm_iio_device_register (&spi->dev, indio_dev);
drivers/iio/adc/mcp320x.c static int mcp320x_probe(struct spi_device *spi) · line 454
Quoted 10 lines
drivers/iio/adc/mcp320x.c +0
-5}static void mcp320x_remove (struct spi_device *spi) { -struct iio_dev *indio_dev =spi_get_drvdata (spi); -struct mcp320x *adc =iio_priv (indio_dev); - -iio_device_unregister (indio_dev); -regulator_disable (adc->reg); }
Had the change otherwise been ok...
You should be able to remove this function completely now it is empty.
https://elixir.bootlin.com/linux/latest/source/drivers/spi/spi.c#L446
checks it's existence before calling it.
Thanks
Jonathan
Quoted 4 lines
drivers/iio/adc/mcp320x.c +0
-0static const struct of_device_id mcp320x_dt_ids[] = {base-commit:
047371968ffc470769f541d6933e262dc7085456
Quoted 4 lines · Jonathan Cameron
Quoted 3 lines
Use devm_* APIs to enable regulator and to register in IIO infrastructure.
Signed-off-by Arturas Moskvinas <[email protected]>
Quoted 8 lines
Don't get the same regulator twice so as automate turning it off. The
devm_regulator_get_enable()call is carefully hiding the regulator for cases where the driver never accesses it.Here we need it to read the voltage.
To convert such a case to fully devm managed, use a
devm_add_action_or_reset()and a custom callback.
This is exactly the reason I wanted to use devm_regulator_get_enable() instead of devm_add_action_or_reset + custom callback which I saw in other ADCs code. It seems quite repetitive. Wondering if this could be addressed by regulator subsystem to provide device managed method which gets _enabled_ regulator resource and an automated disabling + resource freeing? We will loose fine grained information where failure happened exactly - getting resource or enabling regulator though...
Quoted 10 lines
drivers/iio/adc/mcp320x.c static int mcp320x_probe(struct spi_device *spi) · line 454
Quoted 8 lines
static void mcp320x_remove(struct spi_device *spi) {drivers/iio/adc/mcp320x.c +0
-5-struct iio_dev *indio_dev =spi_get_drvdata (spi); -struct mcp320x *adc =iio_priv (indio_dev); - -iio_device_unregister (indio_dev); -regulator_disable (adc->reg); }Had the change otherwise been ok...
Quoted 3 lines
You should be able to remove this function completely now it is empty.
https://elixir.bootlin.com/linux/latest/source/drivers/spi/spi.c#L446
checks it's existence before calling it.
Ack, will address with V2 patch.
Arturas Moskvinas