linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] iio: adc: ad7298: convert probe to device-managed functions
@ 2020-11-27  9:40 Alexandru Ardelean
  2020-11-27  9:40 ` [PATCH 2/2] iio: adc: ad7298: check regulator for null in ad7298_get_ref_voltage() Alexandru Ardelean
  2020-11-28 13:04 ` [PATCH 1/2] iio: adc: ad7298: convert probe to device-managed functions Jonathan Cameron
  0 siblings, 2 replies; 3+ messages in thread
From: Alexandru Ardelean @ 2020-11-27  9:40 UTC (permalink / raw)
  To: linux-iio, linux-kernel; +Cc: jic23, colin.king, Alexandru Ardelean

This change converts the probe of this driver to use device-managed
register functions, and a devm_add_action_or_reset() for the regulator
disable.

With this, the exit & error paths can be removed.
Another side-effect is that this should avoid some static-analyzer's check
with respect to a potential null dereference of the regulator. The null
dereference isn't likely to happen (under normal operation), so there isn't
a requirement to have this fixed/backported in other releases.

As a note: this is removing spi_set_drvdata() since there is no other
spi_get_drvdata() (or dev_get_drvdata()) call that need it.

Cc: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 drivers/iio/adc/ad7298.c | 46 +++++++++++++---------------------------
 1 file changed, 15 insertions(+), 31 deletions(-)

diff --git a/drivers/iio/adc/ad7298.c b/drivers/iio/adc/ad7298.c
index fa1047f74a1f..ecdb01bd5b2f 100644
--- a/drivers/iio/adc/ad7298.c
+++ b/drivers/iio/adc/ad7298.c
@@ -279,6 +279,13 @@ static const struct iio_info ad7298_info = {
 	.update_scan_mode = ad7298_update_scan_mode,
 };
 
+static void ad7298_reg_disable(void *data)
+{
+	struct regulator *reg = data;
+
+	regulator_disable(reg);
+}
+
 static int ad7298_probe(struct spi_device *spi)
 {
 	struct ad7298_state *st;
@@ -306,9 +313,12 @@ static int ad7298_probe(struct spi_device *spi)
 		ret = regulator_enable(st->reg);
 		if (ret)
 			return ret;
-	}
 
-	spi_set_drvdata(spi, indio_dev);
+		ret = devm_add_action_or_reset(&spi->dev, ad7298_reg_disable,
+					       st->reg);
+		if (ret)
+			return ret;
+	}
 
 	st->spi = spi;
 
@@ -334,37 +344,12 @@ static int ad7298_probe(struct spi_device *spi)
 	spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
 	spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
 
-	ret = iio_triggered_buffer_setup(indio_dev, NULL,
+	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
 			&ad7298_trigger_handler, NULL);
 	if (ret)
-		goto error_disable_reg;
-
-	ret = iio_device_register(indio_dev);
-	if (ret)
-		goto error_cleanup_ring;
-
-	return 0;
-
-error_cleanup_ring:
-	iio_triggered_buffer_cleanup(indio_dev);
-error_disable_reg:
-	if (st->ext_ref)
-		regulator_disable(st->reg);
-
-	return ret;
-}
-
-static int ad7298_remove(struct spi_device *spi)
-{
-	struct iio_dev *indio_dev = spi_get_drvdata(spi);
-	struct ad7298_state *st = iio_priv(indio_dev);
-
-	iio_device_unregister(indio_dev);
-	iio_triggered_buffer_cleanup(indio_dev);
-	if (st->ext_ref)
-		regulator_disable(st->reg);
+		return ret;
 
-	return 0;
+	return devm_iio_device_register(&spi->dev, indio_dev);
 }
 
 static const struct spi_device_id ad7298_id[] = {
@@ -378,7 +363,6 @@ static struct spi_driver ad7298_driver = {
 		.name	= "ad7298",
 	},
 	.probe		= ad7298_probe,
-	.remove		= ad7298_remove,
 	.id_table	= ad7298_id,
 };
 module_spi_driver(ad7298_driver);
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] iio: adc: ad7298: check regulator for null in ad7298_get_ref_voltage()
  2020-11-27  9:40 [PATCH 1/2] iio: adc: ad7298: convert probe to device-managed functions Alexandru Ardelean
@ 2020-11-27  9:40 ` Alexandru Ardelean
  2020-11-28 13:04 ` [PATCH 1/2] iio: adc: ad7298: convert probe to device-managed functions Jonathan Cameron
  1 sibling, 0 replies; 3+ messages in thread
From: Alexandru Ardelean @ 2020-11-27  9:40 UTC (permalink / raw)
  To: linux-iio, linux-kernel; +Cc: jic23, colin.king, Alexandru Ardelean

'st->ext_ref' & 'st->reg' are both non-zero/non-null at the same time, so
logically the code isn't broken.
But it is more correct to check that 'st->reg' is non-null, since we make
sure that the regulator is NULL (in probe) in case one isn't defined.

Cc: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 drivers/iio/adc/ad7298.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/adc/ad7298.c b/drivers/iio/adc/ad7298.c
index ecdb01bd5b2f..689ecd5dd563 100644
--- a/drivers/iio/adc/ad7298.c
+++ b/drivers/iio/adc/ad7298.c
@@ -214,7 +214,7 @@ static int ad7298_get_ref_voltage(struct ad7298_state *st)
 {
 	int vref;
 
-	if (st->ext_ref) {
+	if (st->reg) {
 		vref = regulator_get_voltage(st->reg);
 		if (vref < 0)
 			return vref;
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] iio: adc: ad7298: convert probe to device-managed functions
  2020-11-27  9:40 [PATCH 1/2] iio: adc: ad7298: convert probe to device-managed functions Alexandru Ardelean
  2020-11-27  9:40 ` [PATCH 2/2] iio: adc: ad7298: check regulator for null in ad7298_get_ref_voltage() Alexandru Ardelean
@ 2020-11-28 13:04 ` Jonathan Cameron
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2020-11-28 13:04 UTC (permalink / raw)
  To: Alexandru Ardelean; +Cc: linux-iio, linux-kernel, colin.king

On Fri, 27 Nov 2020 11:40:37 +0200
Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:

> This change converts the probe of this driver to use device-managed
> register functions, and a devm_add_action_or_reset() for the regulator
> disable.
> 
> With this, the exit & error paths can be removed.
> Another side-effect is that this should avoid some static-analyzer's check
> with respect to a potential null dereference of the regulator. The null
> dereference isn't likely to happen (under normal operation), so there isn't
> a requirement to have this fixed/backported in other releases.
> 
> As a note: this is removing spi_set_drvdata() since there is no other
> spi_get_drvdata() (or dev_get_drvdata()) call that need it.
> 
> Cc: Colin Ian King <colin.king@canonical.com>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Series applied to the togreg branch of iio.git and pushed out as testing
for the autobuilders to work their magic.

Thanks,

Jonathan

> ---
>  drivers/iio/adc/ad7298.c | 46 +++++++++++++---------------------------
>  1 file changed, 15 insertions(+), 31 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad7298.c b/drivers/iio/adc/ad7298.c
> index fa1047f74a1f..ecdb01bd5b2f 100644
> --- a/drivers/iio/adc/ad7298.c
> +++ b/drivers/iio/adc/ad7298.c
> @@ -279,6 +279,13 @@ static const struct iio_info ad7298_info = {
>  	.update_scan_mode = ad7298_update_scan_mode,
>  };
>  
> +static void ad7298_reg_disable(void *data)
> +{
> +	struct regulator *reg = data;
> +
> +	regulator_disable(reg);
> +}
> +
>  static int ad7298_probe(struct spi_device *spi)
>  {
>  	struct ad7298_state *st;
> @@ -306,9 +313,12 @@ static int ad7298_probe(struct spi_device *spi)
>  		ret = regulator_enable(st->reg);
>  		if (ret)
>  			return ret;
> -	}
>  
> -	spi_set_drvdata(spi, indio_dev);
> +		ret = devm_add_action_or_reset(&spi->dev, ad7298_reg_disable,
> +					       st->reg);
> +		if (ret)
> +			return ret;
> +	}
>  
>  	st->spi = spi;
>  
> @@ -334,37 +344,12 @@ static int ad7298_probe(struct spi_device *spi)
>  	spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
>  	spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
>  
> -	ret = iio_triggered_buffer_setup(indio_dev, NULL,
> +	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
>  			&ad7298_trigger_handler, NULL);
>  	if (ret)
> -		goto error_disable_reg;
> -
> -	ret = iio_device_register(indio_dev);
> -	if (ret)
> -		goto error_cleanup_ring;
> -
> -	return 0;
> -
> -error_cleanup_ring:
> -	iio_triggered_buffer_cleanup(indio_dev);
> -error_disable_reg:
> -	if (st->ext_ref)
> -		regulator_disable(st->reg);
> -
> -	return ret;
> -}
> -
> -static int ad7298_remove(struct spi_device *spi)
> -{
> -	struct iio_dev *indio_dev = spi_get_drvdata(spi);
> -	struct ad7298_state *st = iio_priv(indio_dev);
> -
> -	iio_device_unregister(indio_dev);
> -	iio_triggered_buffer_cleanup(indio_dev);
> -	if (st->ext_ref)
> -		regulator_disable(st->reg);
> +		return ret;
>  
> -	return 0;
> +	return devm_iio_device_register(&spi->dev, indio_dev);
>  }
>  
>  static const struct spi_device_id ad7298_id[] = {
> @@ -378,7 +363,6 @@ static struct spi_driver ad7298_driver = {
>  		.name	= "ad7298",
>  	},
>  	.probe		= ad7298_probe,
> -	.remove		= ad7298_remove,
>  	.id_table	= ad7298_id,
>  };
>  module_spi_driver(ad7298_driver);


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-11-28 21:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-27  9:40 [PATCH 1/2] iio: adc: ad7298: convert probe to device-managed functions Alexandru Ardelean
2020-11-27  9:40 ` [PATCH 2/2] iio: adc: ad7298: check regulator for null in ad7298_get_ref_voltage() Alexandru Ardelean
2020-11-28 13:04 ` [PATCH 1/2] iio: adc: ad7298: convert probe to device-managed functions Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).