Skip to contentSkip to conversation

iio: adc: bcm_iproc_adc: convert to devm-managed APIs

PATCH Superseded [email protected] 2 participants 10 messages Sep 5

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover iio: adc: bcm_iproc_adc: convert to devm-managed APIs

From: Mohammad Shahid <[email protected]>

Convert the bcm_iproc_adc driver to use devm-managed APIs to simplify resource management and cleanup.

Changes in v3:

-Add the missing direct include dependencies.
-Split the clock and ADC cleanup conversions into separate patches. -Use devm_clk_get_enabled() for clock management.
-Use devm_add_action_or_reset() for ADC cleanup.
-Remove platform_set_drvdata() and the remove callback when converting
to devm_iio_device_register().
-Simplify the devm_iio_device_register() return path.

---
Mohammad Shahid (6):
iio: adc: bcm_iproc_adc: sort headers alphabetically
iio: adc: bcm_iproc_adc: fix include dependencies
iio: adc: bcm_iproc_adc: use devm-managed clock
iio: adc: bcm_iproc_adc: use devm-managed ADC cleanup
iio: adc: bcm_iproc_adc: use devm-managed mutex initialization
iio: adc: bcm_iproc_adc: use devm_iio_device_register()

 drivers/iio/adc/bcm_iproc_adc.c | 67 +++++++++++++--------------------
 1 file changed, 27 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/6 iio: adc: bcm_iproc_adc: sort headers alphabetically

From: Mohammad Shahid <[email protected]>

Sort the Linux kernel headers alphabetically to improve consistency and make the include list easier to maintain.

---
 drivers/iio/adc/bcm_iproc_adc.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/adc/bcm_iproc_adc.c +4 -4
index ab66b97d2f04..e46e3761993a 100644
@@ -3,14 +3,14 @@
  * Copyright 2016 Broadcom
  */
 
-#include <linux/module.h>
-#include <linux/io.h>
 #include <linux/clk.h>
-#include <linux/mfd/syscon.h>
-#include <linux/regmap.h>
 #include <linux/delay.h>
 #include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/mfd/syscon.h>
+#include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/regmap.h>
 
 #include <linux/iio/iio.h>
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/6 iio: adc: bcm_iproc_adc: fix include dependencies

In reply to [email protected]

From: Mohammad Shahid <[email protected]>

Add the headers required directly by the driver for the APIs and types it uses.

Remove the unused linux/io.h header now that register access is handled through regmap APIs.

---
 drivers/iio/adc/bcm_iproc_adc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/iio/adc/bcm_iproc_adc.c +5 -2
index e46e3761993a..e54f613d266a 100644
@@ -2,15 +2,18 @@
 /*
  * Copyright 2016 Broadcom
  */
-
+#include <linux/array_size.h>
+#include <linux/bits.h>
 #include <linux/clk.h>
+#include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/interrupt.h>
-#include <linux/io.h>
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
+#include <linux/types.h>
 
 #include <linux/iio/iio.h>
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v3 3/6 iio: adc: bcm_iproc_adc: use devm-managed clock

In reply to [email protected]

From: Mohammad Shahid <[email protected]>

Convert the ADC clock to use devm-managed APIs to simplify resource cleanup.

---
 drivers/iio/adc/bcm_iproc_adc.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/iio/adc/bcm_iproc_adc.c +4 -13
index e54f613d266a..3bbf814d4c8f 100644
@@ -109,7 +109,6 @@ do { \
 
 struct iproc_adc_priv {
 	struct regmap *regmap;
-	struct clk *adc_clk;
 	struct mutex mutex;
 	int  irqno;
 	int chan_val;
@@ -510,6 +509,7 @@ static int iproc_adc_probe(struct platform_device *pdev)
 	struct iproc_adc_priv *adc_priv;
 	struct iio_dev *indio_dev = NULL;
 	struct device *dev = &pdev->dev;
+	struct clk *adc_clk;
 	int ret;
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*adc_priv));
@@ -529,10 +529,9 @@ static int iproc_adc_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(adc_priv->regmap),
 				     "failed to get handle for tsc syscon\n");
 
-	adc_priv->adc_clk = devm_clk_get(dev, "tsc_clk");
-	if (IS_ERR(adc_priv->adc_clk))
-		return dev_err_probe(dev, PTR_ERR(adc_priv->adc_clk),
-				     "failed getting clock tsc_clk\n");
+	adc_clk = devm_clk_get_enabled(dev, "tsc_clk");
+	if (IS_ERR(adc_clk))
+		return dev_err_probe(dev, PTR_ERR(adc_clk), "failed to enable clock\n");
 
 	adc_priv->irqno = platform_get_irq(pdev, 0);
 	if (adc_priv->irqno < 0)
@@ -550,10 +549,6 @@ static int iproc_adc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	ret = clk_prepare_enable(adc_priv->adc_clk);
-	if (ret)
-		return dev_err_probe(dev, ret, "failed to enable clock\n");
-
 	ret = iproc_adc_enable(indio_dev);
 	if (ret)
 		goto err_adc_enable;
@@ -575,19 +570,15 @@ static int iproc_adc_probe(struct platform_device *pdev)
 err_clk:
 	iproc_adc_disable(indio_dev);
 err_adc_enable:
-	clk_disable_unprepare(adc_priv->adc_clk);
-
 	return ret;
 }
 
 static void iproc_adc_remove(struct platform_device *pdev)
 {
 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
-	struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
 
 	iio_device_unregister(indio_dev);
 	iproc_adc_disable(indio_dev);
-	clk_disable_unprepare(adc_priv->adc_clk);
 }
 
 static const struct of_device_id iproc_adc_of_match[] = {
Signature · 1 line
2.43.0
#6 lore raw

PATCH v3 4/6 iio: adc: bcm_iproc_adc: use devm-managed ADC cleanup

In reply to [email protected]

From: Mohammad Shahid <[email protected]>

Use devm_add_action_or_reset() to disable the ADC automatically when the device is detached or probe fails.

This removes the manual ADC cleanup from the remove and error paths and ties the ADC cleanup to the device lifetime.

---
 drivers/iio/adc/bcm_iproc_adc.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/iio/adc/bcm_iproc_adc.c +14 -10
index 3bbf814d4c8f..24fb41751e93 100644
@@ -450,6 +450,13 @@ static void iproc_adc_disable(struct iio_dev *indio_dev)
 	}
 }
 
+static void iproc_adc_disable_action(void *data)
+{
+	struct iio_dev *indio_dev = data;
+
+	iproc_adc_disable(indio_dev);
+}
+
 static int iproc_adc_read_raw(struct iio_dev *indio_dev,
 			  struct iio_chan_spec const *chan,
 			  int *val,
@@ -551,7 +558,11 @@ static int iproc_adc_probe(struct platform_device *pdev)
 
 	ret = iproc_adc_enable(indio_dev);
 	if (ret)
-		goto err_adc_enable;
+		return ret;
+
+	ret = devm_add_action_or_reset(dev, iproc_adc_disable_action, indio_dev);
+	if (ret)
+		return ret;
 
 	indio_dev->name = "iproc-static-adc";
 	indio_dev->info = &iproc_adc_iio_info;
@@ -560,17 +571,11 @@ static int iproc_adc_probe(struct platform_device *pdev)
 	indio_dev->num_channels = ARRAY_SIZE(iproc_adc_iio_channels);
 
 	ret = iio_device_register(indio_dev);
-	if (ret) {
-		dev_err(&pdev->dev, "iio_device_register failed:err %d\n", ret);
-		goto err_clk;
-	}
+	if (ret)
+		return dev_err_probe(dev, ret, "iio_device_register failed\n");
 
 	return 0;
 
-err_clk:
-	iproc_adc_disable(indio_dev);
-err_adc_enable:
-	return ret;
 }
 
 static void iproc_adc_remove(struct platform_device *pdev)
@@ -578,7 +583,6 @@ static void iproc_adc_remove(struct platform_device *pdev)
 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
 
 	iio_device_unregister(indio_dev);
-	iproc_adc_disable(indio_dev);
 }
 
 static const struct of_device_id iproc_adc_of_match[] = {
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 5/6 iio: adc: bcm_iproc_adc: use devm-managed mutex initialization

In reply to [email protected]

From: Mohammad Shahid <[email protected]>

Use devm_mutex_init() to tie the mutex lifetime to the device and improve debugging when CONFIG_DEBUG_MUTEXES is enabled.

---
 drivers/iio/adc/bcm_iproc_adc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/adc/bcm_iproc_adc.c +3 -1
index 24fb41751e93..f4676607682b 100644
@@ -526,7 +526,9 @@ static int iproc_adc_probe(struct platform_device *pdev)
 	adc_priv = iio_priv(indio_dev);
 	platform_set_drvdata(pdev, indio_dev);
 
-	mutex_init(&adc_priv->mutex);
+	ret = devm_mutex_init(dev, &adc_priv->mutex);
+	if (ret)
+		return ret;
 
 	init_completion(&adc_priv->completion);
 
Signature · 1 line
2.43.0
#9 lore raw

PATCH v3 6/6 iio: adc: bcm_iproc_adc: use devm_iio_device_register()

In reply to [email protected]

From: Mohammad Shahid <[email protected]>

Use devm_iio_device_register() to automatically unregister the IIO device when the device is detached.

This removes the manual iio_device_unregister() call and makes the iproc_adc_remove() callback unnecessary.

---
 drivers/iio/adc/bcm_iproc_adc.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/iio/adc/bcm_iproc_adc.c +1 -14
index f4676607682b..6eae7596ac8d 100644
@@ -524,7 +524,6 @@ static int iproc_adc_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	adc_priv = iio_priv(indio_dev);
-	platform_set_drvdata(pdev, indio_dev);
 
 	ret = devm_mutex_init(dev, &adc_priv->mutex);
 	if (ret)
@@ -572,19 +571,8 @@ static int iproc_adc_probe(struct platform_device *pdev)
 	indio_dev->channels = iproc_adc_iio_channels;
 	indio_dev->num_channels = ARRAY_SIZE(iproc_adc_iio_channels);
 
-	ret = iio_device_register(indio_dev);
-	if (ret)
-		return dev_err_probe(dev, ret, "iio_device_register failed\n");
-
-	return 0;
-
-}
-
-static void iproc_adc_remove(struct platform_device *pdev)
-{
-	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+	return devm_iio_device_register(dev, indio_dev);
 
-	iio_device_unregister(indio_dev);
 }
 
 static const struct of_device_id iproc_adc_of_match[] = {
@@ -595,7 +583,6 @@ MODULE_DEVICE_TABLE(of, iproc_adc_of_match);
 
 static struct platform_driver iproc_adc_driver = {
 	.probe = iproc_adc_probe,
-	.remove = iproc_adc_remove,
 	.driver = {
 		.name = "iproc-static-adc",
 		.of_match_table = iproc_adc_of_match,
Signature · 1 line
2.43.0
maintainer
#3 lore raw

Re: [PATCH v3 2/6] iio: adc: bcm_iproc_adc: fix include dependencies

In reply to [email protected]
Quoted 13 lines

Add the headers required directly by the driver for the APIs and types it uses.

Remove the unused linux/io.h header now that register access is handled through regmap APIs.

drivers/iio/adc/bcm_iproc_adc.c +0 -0
index e46e3761993a..e54f613d266a 100644

drivers/iio/adc/bcm_iproc_adc.c line 5

Quoted 4 lines
drivers/iio/adc/bcm_iproc_adc.c +0 -1
@@ -2,15 +2,18 @@
 /*
  * Copyright 2016 Broadcom
  */
-

Really trivial but this change doesn't belong in here.
I don't really mind whether or not there is a blank line here but it is definitely not related to the header changes that this patch is focussed on. I might fix up whilst applying or decide I can't be bothered. If you should spin a new version for other reasons please put the line back again!

Quoted 5 lines
drivers/iio/adc/bcm_iproc_adc.c +3 -0
+#include <linux/array_size.h>
+#include <linux/bits.h>
 #include <linux/clk.h>
+#include <linux/completion.h>
 #include <linux/delay.h>

drivers/iio/adc/bcm_iproc_adc.c line 18

Quoted 10 lines
drivers/iio/adc/bcm_iproc_adc.c +2 -1
 #include <linux/interrupt.h>
-#include <linux/io.h>
 #include <linux/mfd/syscon.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
+#include <linux/types.h>
 
 #include <linux/iio/iio.h>

Otherwise, this looks fine to me. The sashiko comment is a preexisting issue so out of scope for this patch at least (I also didn't check if it is correct or not)

Thanks,

J

Signature · 1 line
Jonathan Cameron <[email protected]>
maintainer
#5 lore raw

Re: [PATCH v3 3/6] iio: adc: bcm_iproc_adc: use devm-managed clock

In reply to [email protected]
Quoted 4 lines

Convert the ADC clock to use devm-managed APIs to simplify resource cleanup.

Hi,

One thing that should have been in this patch has ended up in the next one. Whilst minor this just passed the level of what I'd normally tidy up whilst applying. So I will be looking for a v4 after this has had time for others to comment on v3.

Breaking changes up into a series of minor changes is a bit of an art form and often takes a couple of passes to get perfect. It is worth doing though as keeps each change nice an easy to review on their own.

Thanks,

Jonathan

Quoted 47 lines
drivers/iio/adc/bcm_iproc_adc.c +4 -9
index e54f613d266a..3bbf814d4c8f 100644
@@ -109,7 +109,6 @@ do { \
 
 struct iproc_adc_priv {
 	struct regmap *regmap;
-	struct clk *adc_clk;
 	struct mutex mutex;
 	int  irqno;
 	int chan_val;
@@ -510,6 +509,7 @@ static int iproc_adc_probe(struct platform_device *pdev)
 	struct iproc_adc_priv *adc_priv;
 	struct iio_dev *indio_dev = NULL;
 	struct device *dev = &pdev->dev;
+	struct clk *adc_clk;
 	int ret;
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*adc_priv));
@@ -529,10 +529,9 @@ static int iproc_adc_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(adc_priv->regmap),
 				     "failed to get handle for tsc syscon\n");
 
-	adc_priv->adc_clk = devm_clk_get(dev, "tsc_clk");
-	if (IS_ERR(adc_priv->adc_clk))
-		return dev_err_probe(dev, PTR_ERR(adc_priv->adc_clk),
-				     "failed getting clock tsc_clk\n");
+	adc_clk = devm_clk_get_enabled(dev, "tsc_clk");
+	if (IS_ERR(adc_clk))
+		return dev_err_probe(dev, PTR_ERR(adc_clk), "failed to enable clock\n");
 
 	adc_priv->irqno = platform_get_irq(pdev, 0);
 	if (adc_priv->irqno < 0)
@@ -550,10 +549,6 @@ static int iproc_adc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	ret = clk_prepare_enable(adc_priv->adc_clk);
-	if (ret)
-		return dev_err_probe(dev, ret, "failed to enable clock\n");
-
 	ret = iproc_adc_enable(indio_dev);
 	if (ret)
 		goto err_adc_enable;

drivers/iio/adc/bcm_iproc_adc.c static int iproc_adc_probe(struct platform_device *pdev) · line 578

Quoted 4 lines
drivers/iio/adc/bcm_iproc_adc.c +0 -1
@@ -575,19 +570,15 @@ static int iproc_adc_probe(struct platform_device *pdev)
 err_clk:
 	iproc_adc_disable(indio_dev);
 err_adc_enable:
-	clk_disable_unprepare(adc_priv->adc_clk);

This gets tidied up in the next patch, but ideal would have been to replace the goto err_adc_enable; with a return ret in this patch (the one that enables that change) and remove the label here.

Signature · 1 line
Jonathan Cameron <[email protected]>
maintainer
#7 lore raw

Re: [PATCH v3 4/6] iio: adc: bcm_iproc_adc: use devm-managed ADC cleanup

In reply to [email protected]
Quoted 5 lines

Use devm_add_action_or_reset() to disable the ADC automatically when the device is detached or probe fails.

This removes the manual ADC cleanup from the remove and error paths and ties the ADC cleanup to the device lifetime.

Sashiko raised a bunch of pre existing issues whilst looking at this one. As far as I'm concerned whether correct or not they are out of scope for this series. Feel free to take a look if you like though (for a potential follow up) Note I haven't analysed any of them - just noted there were a few comments - so they may or may not be correct.

https://sashiko.dev/#/patchset/20260905093631.48667-1-mdshahid03%40gmail.com

Quoted 8 lines
drivers/iio/adc/bcm_iproc_adc.c +0 -0
index 3bbf814d4c8f..24fb41751e93 100644

drivers/iio/adc/bcm_iproc_adc.c static void iproc_adc_disable(struct iio_dev *indio_dev) · line 457

Quoted 8 lines
drivers/iio/adc/bcm_iproc_adc.c +5 -0
@@ -450,6 +450,13 @@ static void iproc_adc_disable(struct iio_dev *indio_dev)
 	}
 }
 
+static void iproc_adc_disable_action(void *data)
+{
+	struct iio_dev *indio_dev = data;
+
+	iproc_adc_disable(indio_dev);

For this it is fine and more compact to do

static void iproc_adc_disable_action(void *indio_dev)

{
	iproc_adc_disable(indio_dev);
}

Given we are going via a void * anyway, there is no additional type safety or particular readability advantages to having the intermediate given we have well known naming fo the struct iio_dev *

drivers/iio/adc/bcm_iproc_adc.c static int iproc_adc_probe(struct platform_device *pdev) · line 561

Quoted 11 lines
drivers/iio/adc/bcm_iproc_adc.c +3 -1
+}
+
 static int iproc_adc_read_raw(struct iio_dev *indio_dev,
 			  struct iio_chan_spec const *chan,
 			  int *val,
@@ -551,7 +558,11 @@ static int iproc_adc_probe(struct platform_device *pdev)
 
 	ret = iproc_adc_enable(indio_dev);
 	if (ret)
-		goto err_adc_enable;
+		return ret;

This change belongs in the earlier patch as none of the other changes in this patch have anything to do with enabling it.

Quoted 13 lines
drivers/iio/adc/bcm_iproc_adc.c +4 -2
+
+	ret = devm_add_action_or_reset(dev, iproc_adc_disable_action, indio_dev);
+	if (ret)
+		return ret;
 
 	indio_dev->name = "iproc-static-adc";
 	indio_dev->info = &iproc_adc_iio_info;
@@ -560,17 +571,11 @@ static int iproc_adc_probe(struct platform_device *pdev)
 	indio_dev->num_channels = ARRAY_SIZE(iproc_adc_iio_channels);
 
 	ret = iio_device_register(indio_dev);
-	if (ret) {
-		dev_err(&pdev->dev, "iio_device_register failed:err %d\n", ret);

drivers/iio/adc/bcm_iproc_adc.c static int iproc_adc_probe(struct platform_device *pdev) · line 572

Quoted 10 lines
drivers/iio/adc/bcm_iproc_adc.c +2 -5
-		goto err_clk;
-	}
+	if (ret)
+		return dev_err_probe(dev, ret, "iio_device_register failed\n");
 
 	return 0;
 
-err_clk:
-	iproc_adc_disable(indio_dev);
-err_adc_enable:

This label should have gone in the earlier patch.

Signature · 1 line
Jonathan Cameron <[email protected]>