Skip to contentSkip to conversation

iio: multiplexer: Make use of device properties

PATCH Superseded Andy Shevchenko 3 participants 6 messages Feb 2, 2022 – Feb 27, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: multiplexer: Make use of device properties

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

---
v2: dropped depends on OF

 drivers/iio/multiplexer/Kconfig   |  1 -
 drivers/iio/multiplexer/iio-mux.c | 48 ++++++++++++++-----------------
 2 files changed, 22 insertions(+), 27 deletions(-)
drivers/iio/multiplexer/Kconfig +0 -1
index a1e1332d1206..928f424a1ed3 100644
@@ -9,7 +9,6 @@ menu "Multiplexers"
 config IIO_MUX
 	tristate "IIO multiplexer driver"
 	select MULTIPLEXER
-	depends on OF || COMPILE_TEST
 	help
 	  Say yes here to build support for the IIO multiplexer.
 
drivers/iio/multiplexer/iio-mux.c +22 -26

97 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 4 lines · On Wed, 2 Feb 2022 22:44:27 +0200 Andy Shevchenko…

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

Looks good to me, but as it's complex I'd like Peter + anyone else interested to have a bit more time to take a look before I apply this one.

Thanks,

Jonathan

Quoted 124 lines

---
v2: dropped depends on OF

 drivers/iio/multiplexer/Kconfig   |  1 -
 drivers/iio/multiplexer/iio-mux.c | 48 ++++++++++++++-----------------
 2 files changed, 22 insertions(+), 27 deletions(-)
drivers/iio/multiplexer/Kconfig +0 -1
index a1e1332d1206..928f424a1ed3 100644
@@ -9,7 +9,6 @@ menu "Multiplexers"
 config IIO_MUX
 	tristate "IIO multiplexer driver"
 	select MULTIPLEXER
-	depends on OF || COMPILE_TEST
 	help
 	  Say yes here to build support for the IIO multiplexer.
 
drivers/iio/multiplexer/iio-mux.c +22 -26
index f422d44377df..e6f0fef96494 100644
@@ -10,11 +10,12 @@
 #include <linux/err.h>
 #include <linux/iio/consumer.h>
 #include <linux/iio/iio.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/mux/consumer.h>
-#include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 
 struct mux_ext_info_cache {
 	char *data;
@@ -324,37 +325,20 @@ static int mux_configure_channel(struct device *dev, struct mux *mux,
 	return 0;
 }
 
-/*
- * Same as of_property_for_each_string(), but also keeps track of the
- * index of each string.
- */
-#define of_property_for_each_string_index(np, propname, prop, s, i)	\
-	for (prop = of_find_property(np, propname, NULL),		\
-	     s = of_prop_next_string(prop, NULL),			\
-	     i = 0;							\
-	     s;								\
-	     s = of_prop_next_string(prop, s),				\
-	     i++)
-
 static int mux_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
-	struct device_node *np = pdev->dev.of_node;
 	struct iio_dev *indio_dev;
 	struct iio_channel *parent;
 	struct mux *mux;
-	struct property *prop;
-	const char *label;
+	const char **labels;
 	u32 state;
+	int children, all_children;
 	int sizeof_ext_info;
-	int children;
 	int sizeof_priv;
 	int i;
 	int ret;
 
-	if (!np)
-		return -ENODEV;
-
 	parent = devm_iio_channel_get(dev, "parent");
 	if (IS_ERR(parent))
 		return dev_err_probe(dev, PTR_ERR(parent),
@@ -366,9 +350,21 @@ static int mux_probe(struct platform_device *pdev)
 		sizeof_ext_info *= sizeof(*mux->ext_info);
 	}
 
+	all_children = device_property_count_string_array(dev, "channels");
+	if (all_children < 0)
+		return all_children;
+
+	labels = devm_kmalloc_array(dev, all_children, sizeof(char *), GFP_KERNEL);
+	if (!labels)
+		return -ENOMEM;
+
+	ret = device_property_read_string_array(dev, "channels", labels, all_children);
+	if (ret < 0)
+		return ret;
+
 	children = 0;
-	of_property_for_each_string(np, "channels", prop, label) {
-		if (*label)
+	for (state = 0; state < all_children; state++) {
+		if (*labels[state])
 			children++;
 	}
 	if (children <= 0) {
@@ -395,7 +391,7 @@ static int mux_probe(struct platform_device *pdev)
 	mux->cached_state = -1;
 
 	mux->delay_us = 0;
-	of_property_read_u32(np, "settle-time-us", &mux->delay_us);
+	device_property_read_u32(dev, "settle-time-us", &mux->delay_us);
 
 	indio_dev->name = dev_name(dev);
 	indio_dev->info = &mux_info;
@@ -426,11 +422,11 @@ static int mux_probe(struct platform_device *pdev)
 	}
 
 	i = 0;
-	of_property_for_each_string_index(np, "channels", prop, label, state) {
-		if (!*label)
+	for (state = 0; state < all_children; state++) {
+		if (!*labels[state])
 			continue;
 
-		ret = mux_configure_channel(dev, mux, state, label, i++);
+		ret = mux_configure_channel(dev, mux, state, labels[state], i++);
 		if (ret < 0)
 			return ret;
 	}
#2 lore raw
Quoted 9 lines · Jonathan Cameron
Quoted 4 lines · On Wed, 2 Feb 2022 22:44:27 +0200 Andy Shevchenko…

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

Looks good to me, but as it's complex I'd like Peter + anyone else interested to have a bit more time to take a look before I apply this one.

Sure, I would love to see more eyes and hear comments!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw
Quoted 4 lines · On Sat, 5 Feb 2022 20:30:18 +0200 Andy Shevchenko…
Quoted 3 lines · Jonathan Cameron

On Wed, 2 Feb 2022 22:44:27 +0200
Andy Shevchenko <[email protected]> wrote:

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

Looks good to me, but as it's complex I'd like Peter + anyone else interested to have a bit more time to take a look before I apply this one.

Sure, I would love to see more eyes and hear comments!

Peter, do you have time to take a look at this one?
No problem if you are too busy, I'll just take another look myself and apply it if I think it looks fine.

Thanks,

Jonathan

Acked-by on v2 1/1
#4 lore raw
In reply to Andy Shevchenko

Hi!

Sorry for the late reply.

Quoted 58 lines · Andy Shevchenko

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

---
v2: dropped depends on OF

 drivers/iio/multiplexer/Kconfig   |  1 -
 drivers/iio/multiplexer/iio-mux.c | 48 ++++++++++++++-----------------
 2 files changed, 22 insertions(+), 27 deletions(-)
drivers/iio/multiplexer/Kconfig +0 -1
index a1e1332d1206..928f424a1ed3 100644
@@ -9,7 +9,6 @@ menu "Multiplexers"
 config IIO_MUX
 	tristate "IIO multiplexer driver"
 	select MULTIPLEXER
-	depends on OF || COMPILE_TEST
 	help
 	  Say yes here to build support for the IIO multiplexer.
 
drivers/iio/multiplexer/iio-mux.c +2 -13
index f422d44377df..e6f0fef96494 100644
@@ -10,11 +10,12 @@
 #include <linux/err.h>
 #include <linux/iio/consumer.h>
 #include <linux/iio/iio.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/mux/consumer.h>
-#include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 
 struct mux_ext_info_cache {
 	char *data;
@@ -324,37 +325,20 @@ static int mux_configure_channel(struct device *dev, struct mux *mux,
 	return 0;
 }
 
-/*
- * Same as of_property_for_each_string(), but also keeps track of the
- * index of each string.
- */
-#define of_property_for_each_string_index(np, propname, prop, s, i)	\
-	for (prop = of_find_property(np, propname, NULL),		\
-	     s = of_prop_next_string(prop, NULL),			\
-	     i = 0;							\
-	     s;								\
-	     s = of_prop_next_string(prop, s),				\
-	     i++)
-
 static int mux_probe(struct platform_device *pdev)
 {

drivers/iio/multiplexer/iio-mux.c static int mux_configure_channel(struct device *dev, struct mux *mux, · line 336

Quoted 10 lines
drivers/iio/multiplexer/iio-mux.c +2 -3
 	struct device *dev = &pdev->dev;
-	struct device_node *np = pdev->dev.of_node;
 	struct iio_dev *indio_dev;
 	struct iio_channel *parent;
 	struct mux *mux;
-	struct property *prop;
-	const char *label;
+	const char **labels;
 	u32 state;
+	int children, all_children;

I dislike that the pattern with one declaration per line is broken here. There are perhaps a few too many variables, but introducing one instance of two declarations on one line is disturbing for me.

So, with that fixed,

Quoted 13 lines
drivers/iio/multiplexer/iio-mux.c +0 -4
 	int sizeof_ext_info;
-	int children;
 	int sizeof_priv;
 	int i;
 	int ret;
 
-	if (!np)
-		return -ENODEV;
-
 	parent = devm_iio_channel_get(dev, "parent");
 	if (IS_ERR(parent))
 		return dev_err_probe(dev, PTR_ERR(parent),

drivers/iio/multiplexer/iio-mux.c static int mux_probe(struct platform_device *pdev) · line 357

Quoted 8 lines
drivers/iio/multiplexer/iio-mux.c +5 -0
@@ -366,9 +350,21 @@ static int mux_probe(struct platform_device *pdev)
 		sizeof_ext_info *= sizeof(*mux->ext_info);
 	}
 
+	all_children = device_property_count_string_array(dev, "channels");
+	if (all_children < 0)
+		return all_children;
+
+	labels = devm_kmalloc_array(dev, all_children, sizeof(char *), GFP_KERNEL);

I'm a bit sad to see the copy of the labels array. No big deal, but it seems inelegant. But the of_property_for_each_string_index macro was also misplaced and therefore inelegant. My guess is that the device properties should be fairly static and that the copy is a waste?

So, anyway, I'm just whining here, and the labels copy is ok.

Cheers,
Peter

Quoted 39 lines
drivers/iio/multiplexer/iio-mux.c +13 -6
+	if (!labels)
+		return -ENOMEM;
+
+	ret = device_property_read_string_array(dev, "channels", labels, all_children);
+	if (ret < 0)
+		return ret;
+
 	children = 0;
-	of_property_for_each_string(np, "channels", prop, label) {
-		if (*label)
+	for (state = 0; state < all_children; state++) {
+		if (*labels[state])
 			children++;
 	}
 	if (children <= 0) {
@@ -395,7 +391,7 @@ static int mux_probe(struct platform_device *pdev)
 	mux->cached_state = -1;
 
 	mux->delay_us = 0;
-	of_property_read_u32(np, "settle-time-us", &mux->delay_us);
+	device_property_read_u32(dev, "settle-time-us", &mux->delay_us);
 
 	indio_dev->name = dev_name(dev);
 	indio_dev->info = &mux_info;
@@ -426,11 +422,11 @@ static int mux_probe(struct platform_device *pdev)
 	}
 
 	i = 0;
-	of_property_for_each_string_index(np, "channels", prop, label, state) {
-		if (!*label)
+	for (state = 0; state < all_children; state++) {
+		if (!*labels[state])
 			continue;
 
-		ret = mux_configure_channel(dev, mux, state, label, i++);
+		ret = mux_configure_channel(dev, mux, state, labels[state], i++);
 		if (ret < 0)
 			return ret;
 	}
maintainer
#5 lore raw
Quoted 98 lines · On Sun, 27 Feb 2022 03:28:15 +0100 Peter Rosin <[email protected]> wrote:

Hi!

Sorry for the late reply.

Quoted 58 lines · Andy Shevchenko

Convert the module to be property provider agnostic and allow it to be used on non-OF platforms.

---
v2: dropped depends on OF

 drivers/iio/multiplexer/Kconfig   |  1 -
 drivers/iio/multiplexer/iio-mux.c | 48 ++++++++++++++-----------------
 2 files changed, 22 insertions(+), 27 deletions(-)
drivers/iio/multiplexer/Kconfig +0 -1
index a1e1332d1206..928f424a1ed3 100644
@@ -9,7 +9,6 @@ menu "Multiplexers"
 config IIO_MUX
 	tristate "IIO multiplexer driver"
 	select MULTIPLEXER
-	depends on OF || COMPILE_TEST
 	help
 	  Say yes here to build support for the IIO multiplexer.
 
drivers/iio/multiplexer/iio-mux.c +2 -13
index f422d44377df..e6f0fef96494 100644
@@ -10,11 +10,12 @@
 #include <linux/err.h>
 #include <linux/iio/consumer.h>
 #include <linux/iio/iio.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/mux/consumer.h>
-#include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 
 struct mux_ext_info_cache {
 	char *data;
@@ -324,37 +325,20 @@ static int mux_configure_channel(struct device *dev, struct mux *mux,
 	return 0;
 }
 
-/*
- * Same as of_property_for_each_string(), but also keeps track of the
- * index of each string.
- */
-#define of_property_for_each_string_index(np, propname, prop, s, i)	\
-	for (prop = of_find_property(np, propname, NULL),		\
-	     s = of_prop_next_string(prop, NULL),			\
-	     i = 0;							\
-	     s;								\
-	     s = of_prop_next_string(prop, s),				\
-	     i++)
-
 static int mux_probe(struct platform_device *pdev)
 {

drivers/iio/multiplexer/iio-mux.c static int mux_configure_channel(struct device *dev, struct mux *mux, · line 336

Quoted 10 lines
drivers/iio/multiplexer/iio-mux.c +2 -3
 	struct device *dev = &pdev->dev;
-	struct device_node *np = pdev->dev.of_node;
 	struct iio_dev *indio_dev;
 	struct iio_channel *parent;
 	struct mux *mux;
-	struct property *prop;
-	const char *label;
+	const char **labels;
 	u32 state;
+	int children, all_children;  

I dislike that the pattern with one declaration per line is broken here. There are perhaps a few too many variables, but introducing one instance of two declarations on one line is disturbing for me.

So, with that fixed,

Quoted 16 lines
drivers/iio/multiplexer/iio-mux.c +0 -4
 	int sizeof_ext_info;
-	int children;
 	int sizeof_priv;
 	int i;
 	int ret;
 
-	if (!np)
-		return -ENODEV;
-
 	parent = devm_iio_channel_get(dev, "parent");
 	if (IS_ERR(parent))
 		return dev_err_probe(dev, PTR_ERR(parent),
@@ -366,9 +350,21 @@ static int mux_probe(struct platform_device *pdev)
 		sizeof_ext_info *= sizeof(*mux->ext_info);
 	}
 
Quoted 3 lines

drivers/iio/multiplexer/iio-mux.c static int mux_probe(struct platform_device *pdev) · line 355

Quoted 3 lines
drivers/iio/multiplexer/iio-mux.c +3 -0
+	all_children = device_property_count_string_array(dev, "channels");
+	if (all_children < 0)
+		return all_children;

Function seems to be called device_property_string_array_count() so this will need a v3 anyway as it doesn't currently build.

Thanks,

Jonathan

Quoted 53 lines

drivers/iio/multiplexer/iio-mux.c static int mux_probe(struct platform_device *pdev) · line 357

Quoted 2 lines
drivers/iio/multiplexer/iio-mux.c +2 -0
+
+	labels = devm_kmalloc_array(dev, all_children, sizeof(char *), GFP_KERNEL);  

I'm a bit sad to see the copy of the labels array. No big deal, but it seems inelegant. But the of_property_for_each_string_index macro was also misplaced and therefore inelegant. My guess is that the device properties should be fairly static and that the copy is a waste?

So, anyway, I'm just whining here, and the labels copy is ok.

Cheers,
Peter

Quoted 39 lines
drivers/iio/multiplexer/iio-mux.c +13 -6
+	if (!labels)
+		return -ENOMEM;
+
+	ret = device_property_read_string_array(dev, "channels", labels, all_children);
+	if (ret < 0)
+		return ret;
+
 	children = 0;
-	of_property_for_each_string(np, "channels", prop, label) {
-		if (*label)
+	for (state = 0; state < all_children; state++) {
+		if (*labels[state])
 			children++;
 	}
 	if (children <= 0) {
@@ -395,7 +391,7 @@ static int mux_probe(struct platform_device *pdev)
 	mux->cached_state = -1;
 
 	mux->delay_us = 0;
-	of_property_read_u32(np, "settle-time-us", &mux->delay_us);
+	device_property_read_u32(dev, "settle-time-us", &mux->delay_us);
 
 	indio_dev->name = dev_name(dev);
 	indio_dev->info = &mux_info;
@@ -426,11 +422,11 @@ static int mux_probe(struct platform_device *pdev)
 	}
 
 	i = 0;
-	of_property_for_each_string_index(np, "channels", prop, label, state) {
-		if (!*label)
+	for (state = 0; state < all_children; state++) {
+		if (!*labels[state])
 			continue;
 
-		ret = mux_configure_channel(dev, mux, state, label, i++);
+		ret = mux_configure_channel(dev, mux, state, labels[state], i++);
 		if (ret < 0)
 			return ret;
 	}