Skip to contentSkip to conversation

device property: Add new array helper

PATCH In review Charles Keepax 3 participants 9 messages Jun 26, 2019 – Jun 27, 2019

Subsystems (6)
Sound ACPI ASoC Driver core +2 more subsystems
lore
Share & tools
lore

Charles Keepax

author maintainer
#0 lore raw

PATCH 1/2 device property: Add new array helper

It is fairly common to want to read an integer array property that is composed of an unknown number of fixed size integer groups. For example, say each group consists of three values which correspond to the settings for one input on the device and the driver supports several chips with different numbers of inputs.

Add a new helper function to provide this functionality, it differs for the existing helpers in that it allows reading a smaller number of values than the full array size and checks that the number of values read is a multiple of the group size.

---
 drivers/base/property.c  | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/property.h |  2 ++
 2 files changed, 50 insertions(+)
drivers/base/property.c +48 -0
index 348b37e64944c..656d21e01a648 100644
@@ -133,6 +133,54 @@ int device_property_read_u32_array(struct device *dev, const char *propname,
 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
 
 /**
+ * device_property_read_u32_2darray - return a 2d u32 array property of a device
+ * @dev: Device to get the property of
+ * @propname: Name of the property
+ * @val: The values are stored here or %NULL to return the number of values
+ * @nval: Size of the @val array
+ * @multiple: Number of entries in each block of data
+ *
+ * Function reads an array of u32 properties split up into fixed size
+ * sub-groups, with @propname from the device firmware description and
+ * stores them to @val if found.
+ *
+ * Return: Number of values read
+ *	   %0 if the property was not found,
+ *	   %-EINVAL if given arguments are not valid,
+ *	   %-ENODATA if the property does not have a value,
+ *	   %-EPROTO if the property is not an array of numbers,
+ *	   %-EOVERFLOW if the size of the property is not as expected.
+ *	   %-ENXIO if no suitable firmware interface is present.
+ */
+int device_property_read_u32_2darray(struct device *dev, const char *propname,
+				     u32 *val, size_t nval, int multiple)
+{
+	int n, ret;
+
+	n = device_property_read_u32_array(dev, propname, NULL, 0);
+	if (n == -EINVAL) {
+		return 0;	/* missing, ignore */
+	} else if (n < 0) {
+		dev_warn(dev, "%s malformed (%d)\n", propname, n);
+		return n;
+	} else if ((n % multiple) != 0) {
+		dev_warn(dev, "%s not a multiple of %d entries\n",
+			 propname, multiple);
+		return -EOVERFLOW;
+	}
+
+	if (n > nval)
+		n = nval;
+
+	ret = device_property_read_u32_array(dev, propname, val, n);
+	if (ret < 0)
+		return ret;
+	else
+		return n;
+}
+EXPORT_SYMBOL_GPL(device_property_read_u32_2darray);
+
+/**
  * device_property_read_u64_array - return a u64 array property of a device
  * @dev: Device to get the property of
  * @propname: Name of the property
include/linux/property.h +2 -0
index e9caa290cda52..5ab0b4a7d34a2 100644
@@ -40,6 +40,8 @@ int device_property_read_u16_array(struct device *dev, const char *propname,
 				   u16 *val, size_t nval);
 int device_property_read_u32_array(struct device *dev, const char *propname,
 				   u32 *val, size_t nval);
+int device_property_read_u32_2darray(struct device *dev, const char *propname,
+				     u32 *val, size_t nval, int multiple);
 int device_property_read_u64_array(struct device *dev, const char *propname,
 				   u64 *val, size_t nval);
 int device_property_read_string_array(struct device *dev, const char *propname,
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH 2/2 ASoC: madera: Read device tree configuration

Read the configuration of the Madera sound driver from device tree using the new device tree helper function.

---

Note this patch depends on patches that are currently going through Mark's tree so probably best if this one also goes through there.

Thanks,
Charles

 sound/soc/codecs/madera.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
sound/soc/codecs/madera.c +65 -0
index 1b1be19a2f991..8fe16e4e19c7b 100644
@@ -300,6 +300,68 @@ int madera_free_overheat(struct madera_priv *priv)
 }
 EXPORT_SYMBOL_GPL(madera_free_overheat);
 
+static void madera_prop_get_inmode(struct madera_priv *priv)
+{
+	struct madera *madera = priv->madera;
+	struct madera_codec_pdata *pdata = &madera->pdata.codec;
+	u32 tmp[MADERA_MAX_INPUT * MADERA_MAX_MUXED_CHANNELS];
+	int n, i, in_idx, ch_idx;
+
+	BUILD_BUG_ON(ARRAY_SIZE(pdata->inmode) != MADERA_MAX_INPUT);
+	BUILD_BUG_ON(ARRAY_SIZE(pdata->inmode[0]) != MADERA_MAX_MUXED_CHANNELS);
+
+	n = device_property_read_u32_2darray(madera->dev, "cirrus,inmode",
+					     tmp, ARRAY_SIZE(tmp),
+					     MADERA_MAX_MUXED_CHANNELS);
+	if (n < 0)
+		return;
+
+	in_idx = 0;
+	ch_idx = 0;
+	for (i = 0; i < n; ++i) {
+		pdata->inmode[in_idx][ch_idx] = tmp[i];
+
+		if (++ch_idx == MADERA_MAX_MUXED_CHANNELS) {
+			ch_idx = 0;
+			++in_idx;
+		}
+	}
+}
+
+static void madera_prop_get_pdata(struct madera_priv *priv)
+{
+	struct madera *madera = priv->madera;
+	struct madera_codec_pdata *pdata = &madera->pdata.codec;
+	u32 out_mono[ARRAY_SIZE(pdata->out_mono)];
+	int i, n;
+
+	madera_prop_get_inmode(priv);
+
+	n = device_property_read_u32_2darray(madera->dev, "cirrus,out-mono",
+					     out_mono, ARRAY_SIZE(out_mono), 1);
+	if (n > 0)
+		for (i = 0; i < n; ++i)
+			pdata->out_mono[i] = !!out_mono[i];
+
+	device_property_read_u32_2darray(madera->dev,
+					 "cirrus,max-channels-clocked",
+					 pdata->max_channels_clocked,
+					 ARRAY_SIZE(pdata->max_channels_clocked),
+					 1);
+
+	device_property_read_u32_2darray(madera->dev, "cirrus,pdm-fmt",
+					 pdata->pdm_fmt,
+					 ARRAY_SIZE(pdata->pdm_fmt), 1);
+
+	device_property_read_u32_2darray(madera->dev, "cirrus,pdm-mute",
+					 pdata->pdm_mute,
+					 ARRAY_SIZE(pdata->pdm_mute), 1);
+
+	device_property_read_u32_2darray(madera->dev, "cirrus,dmic-ref",
+					 pdata->dmic_ref,
+					 ARRAY_SIZE(pdata->dmic_ref), 1);
+}
+
 int madera_core_init(struct madera_priv *priv)
 {
 	int i;
@@ -308,6 +370,9 @@ int madera_core_init(struct madera_priv *priv)
 	BUILD_BUG_ON(!madera_mixer_texts[MADERA_NUM_MIXER_INPUTS - 1]);
 	BUILD_BUG_ON(!madera_mixer_values[MADERA_NUM_MIXER_INPUTS - 1]);
 
+	if (!dev_get_platdata(priv->madera->dev))
+		madera_prop_get_pdata(priv);
+
 	mutex_init(&priv->rate_lock);
 
 	for (i = 0; i < MADERA_MAX_HP_OUTPUT; i++)
Signature · 1 line
2.11.0
maintainer
#2 lore raw

Re: [PATCH 2/2] ASoC: madera: Read device tree configuration

Quoted 8 lines · Charles Keepax

Read the configuration of the Madera sound driver from device tree using the new device tree helper function.

---

Note this patch depends on patches that are currently going through Mark's tree so probably best if this one also goes through there.

No objection from me for him to take the previous one with this one in his tree.

thanks,

greg k-h

maintainer Acked-by on v1 1/2
#3 lore raw
In reply to Charles Keepax
Quoted 12 lines · Charles Keepax

It is fairly common to want to read an integer array property that is composed of an unknown number of fixed size integer groups. For example, say each group consists of three values which correspond to the settings for one input on the device and the driver supports several chips with different numbers of inputs.

Add a new helper function to provide this functionality, it differs for the existing helpers in that it allows reading a smaller number of values than the full array size and checks that the number of values read is a multiple of the group size.

Quoted 5 lines
---
 drivers/base/property.c  | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/property.h |  2 ++
 2 files changed, 50 insertions(+)
#4 lore raw
Quoted 14 lines · Greg KH
Quoted 12 lines · Charles Keepax

It is fairly common to want to read an integer array property that is composed of an unknown number of fixed size integer groups. For example, say each group consists of three values which correspond to the settings for one input on the device and the driver supports several chips with different numbers of inputs.

Add a new helper function to provide this functionality, it differs for the existing helpers in that it allows reading a smaller number of values than the full array size and checks that the number of values read is a multiple of the group size.

Quoted 7 lines
Quoted 5 lines
---
 drivers/base/property.c  | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/property.h |  2 ++
 2 files changed, 50 insertions(+)

Well, I'm kind of not convinced about this one.

More comments in direct reply to the patch message.

#5 lore raw
In reply to Charles Keepax
Quoted 8 lines · Charles Keepax

It is fairly common to want to read an integer array property that is composed of an unknown number of fixed size integer groups. For example, say each group consists of three values which correspond to the settings for one input on the device and the driver supports several chips with different numbers of inputs.

Quoted 4 lines

Add a new helper function to provide this functionality, it differs for the existing helpers in that it allows reading a smaller number of values than the full array size and checks that the number of values read is a multiple of the group size.

I'm not convinced.

Quoted 31 lines
---
 drivers/base/property.c  | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/property.h |  2 ++
 2 files changed, 50 insertions(+)
drivers/base/property.c +17 -0
index 348b37e64944c..656d21e01a648 100644
@@ -133,6 +133,54 @@ int device_property_read_u32_array(struct device *dev, const char *propname,
 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
 
 /**
+ * device_property_read_u32_2darray - return a 2d u32 array property of a device
+ * @dev: Device to get the property of
+ * @propname: Name of the property
+ * @val: The values are stored here or %NULL to return the number of values
+ * @nval: Size of the @val array
+ * @multiple: Number of entries in each block of data
+ *
+ * Function reads an array of u32 properties split up into fixed size
+ * sub-groups, with @propname from the device firmware description and
+ * stores them to @val if found.
+ *
+ * Return: Number of values read
+ *        %0 if the property was not found,
+ *        %-EINVAL if given arguments are not valid,
+ *        %-ENODATA if the property does not have a value,
+ *        %-EPROTO if the property is not an array of numbers,
+ *        %-EOVERFLOW if the size of the property is not as expected.

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 162

Quoted 10 lines
drivers/base/property.c +10 -0
+ *        %-ENXIO if no suitable firmware interface is present.
+ */
+int device_property_read_u32_2darray(struct device *dev, const char *propname,
+                                    u32 *val, size_t nval, int multiple)
+{
+       int n, ret;
+
+       n = device_property_read_u32_array(dev, propname, NULL, 0);
+       if (n == -EINVAL) {
+               return 0;       /* missing, ignore */

Why can't the caller use the (scheduled for merging in the 5.3 cycle) new device_property_count_u32() to get the size of the array?

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 164

Quoted 2 lines
drivers/base/property.c +2 -0
+       } else if (n < 0) {
+               dev_warn(dev, "%s malformed (%d)\n", propname, n);

Why dev_warn()? Is there any reason real for anything higher-level that dev_dbg() here?

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 166

Quoted 2 lines
drivers/base/property.c +2 -0
+               return n;
+       } else if ((n % multiple) != 0) {

I guess the reason why this matters is that the caller expects a certain number of full "rows" and n values are read. Why not to discard the extra values instead of returning an error here?

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 169

Quoted 3 lines
drivers/base/property.c +3 -0
+               dev_warn(dev, "%s not a multiple of %d entries\n",
+                        propname, multiple);
+               return -EOVERFLOW;

Why this error code?

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 175

Quoted 6 lines
drivers/base/property.c +6 -0
+       }
+
+       if (n > nval)
+               n = nval;
+
+       ret = device_property_read_u32_array(dev, propname, val, n);

So this reads "copy at most nval values from the array property".

If that's really what you need, it can be done in two lines of code in prospective callers of this wrapper.

Quoted 16 lines
drivers/base/property.c +8 -0
+       if (ret < 0)
+               return ret;
+       else
+               return n;
+}
+EXPORT_SYMBOL_GPL(device_property_read_u32_2darray);
+
+/**
  * device_property_read_u64_array - return a u64 array property of a device
  * @dev: Device to get the property of
  * @propname: Name of the property
include/linux/property.h +0 -0
index e9caa290cda52..5ab0b4a7d34a2 100644

include/linux/property.h int device_property_read_u16_array(struct device *dev, const char *propname,

Quoted 9 lines
include/linux/property.h +2 -1
@@ -40,6 +40,8 @@ int device_property_read_u16_array(struct device *dev, const char *propname,
                                   u16 *val, size_t nval);
 int device_property_read_u32_array(struct device *dev, const char *propname,
                                   u32 *val, size_t nval);
+int device_property_read_u32_2darray(struct device *dev, const char *propname,
+                                    u32 *val, size_t nval, int multiple);
 int device_property_read_u64_array(struct device *dev, const char *propname,
                                   u64 *val, size_t nval);
 int device_property_read_string_array(struct device *dev, const char *propname,
--

I don't see much value in this change, sorry.

author maintainer
#6 lore raw
Quoted 9 lines · Rafael J. Wysocki

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 162

Quoted 3 lines · Charles Keepax
drivers/base/property.c +3 -0
+       n = device_property_read_u32_array(dev, propname, NULL, 0);
+       if (n == -EINVAL) {
+               return 0;       /* missing, ignore */

Why can't the caller use the (scheduled for merging in the 5.3 cycle) new device_property_count_u32() to get the size of the array?

I wasn't aware of it, am now.

Quoted 6 lines

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 164

Quoted 2 lines
drivers/base/property.c +2 -0
+       } else if (n < 0) {
+               dev_warn(dev, "%s malformed (%d)\n", propname, n);

Why dev_warn()? Is there any reason real for anything higher-level that dev_dbg() here?

Nice to know that your DT wasn't valid, but could be left to the caller I guess.

Quoted 7 lines

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 166

Quoted 2 lines
drivers/base/property.c +2 -0
+               return n;
+       } else if ((n % multiple) != 0) {

I guess the reason why this matters is that the caller expects a certain number of full "rows" and n values are read. Why not to discard the extra values instead of returning an error here?

No reason really why it couldn't. Although my expectation would generally be this helper is for reading a variable number of fixed size groups. As in each group represents a "whole" item but you don't know how many of those you have.

Quoted 6 lines

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 169

Quoted 3 lines
drivers/base/property.c +3 -0
+               dev_warn(dev, "%s not a multiple of %d entries\n",
+                        propname, multiple);
+               return -EOVERFLOW;

Why this error code?

As that is the error code all the device_property functions return when the size is not as expected.

Quoted 10 lines

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 175

Quoted 4 lines
drivers/base/property.c +4 -0
+       if (n > nval)
+               n = nval;
+
+       ret = device_property_read_u32_array(dev, propname, val, n);

So this reads "copy at most nval values from the array property".

If that's really what you need, it can be done in two lines of code in prospective callers of this wrapper.

Indeed the helper here is basically exactly what would be done in the caller if no helper existed.

Quoted 8 lines

include/linux/property.h int device_property_read_u16_array(struct device *dev, const char *propname,

Quoted 6 lines
include/linux/property.h +2 -1
+int device_property_read_u32_2darray(struct device *dev, const char *propname,
+                                    u32 *val, size_t nval, int multiple);
 int device_property_read_u64_array(struct device *dev, const char *propname,
                                   u64 *val, size_t nval);
 int device_property_read_string_array(struct device *dev, const char *propname,
--

I don't see much value in this change, sorry.

That is fine, I don't have any problem with the helper living within our driver instead. Basically the issue from my side is I need to read 6 different device tree properties all of which require this behaviour, ie. read a variable number of fixed groups and check I have whole groups. Open coding this for each call is indeed only going to be 5-10 lines of code for each one but since there are 6 of them it makes sense to put those 5-10 lines into a helper and have 5-10 lines not 30-60 lines. Seemed the helper might be generally more useful, but if it is not then it can go back into the driver.

Thanks,
Charles

#7 lore raw
Quoted 17 lines · Charles Keepax
Quoted 9 lines · Rafael J. Wysocki

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 162

Quoted 3 lines · Charles Keepax
drivers/base/property.c +3 -0
+       n = device_property_read_u32_array(dev, propname, NULL, 0);
+       if (n == -EINVAL) {
+               return 0;       /* missing, ignore */

Why can't the caller use the (scheduled for merging in the 5.3 cycle) new device_property_count_u32() to get the size of the array?

I wasn't aware of it, am now.

Quoted 3 lines
Quoted 2 lines
drivers/base/property.c +2 -0
+       } else if (n < 0) {
+               dev_warn(dev, "%s malformed (%d)\n", propname, n);
Quoted 6 lines
Quoted 3 lines

Why dev_warn()? Is there any reason real for anything higher-level that dev_dbg() here?

Nice to know that your DT wasn't valid, but could be left to the caller I guess.

Right. And only print the message when the caller really is going to fail.

Quoted 8 lines
Quoted 7 lines

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 166

Quoted 2 lines
drivers/base/property.c +2 -0
+               return n;
+       } else if ((n % multiple) != 0) {

I guess the reason why this matters is that the caller expects a certain number of full "rows" and n values are read. Why not to discard the extra values instead of returning an error here?

Quoted 4 lines

No reason really why it couldn't. Although my expectation would generally be this helper is for reading a variable number of fixed size groups. As in each group represents a "whole" item but you don't know how many of those you have.

That really depends on how the property is defined and which is what the caller knows.

Quoted 9 lines
Quoted 6 lines

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 169

Quoted 3 lines
drivers/base/property.c +3 -0
+               dev_warn(dev, "%s not a multiple of %d entries\n",
+                        propname, multiple);
+               return -EOVERFLOW;

Why this error code?

As that is the error code all the device_property functions return when the size is not as expected.

To be precise, when there is a mismatch between the storage space supplied by the caller and the property size. That does not seem to be the case here. It is rather about the property format being not as expected and that would be -EPROTO.

Quoted 21 lines
Quoted 10 lines

drivers/base/property.c int device_property_read_u32_array(struct device *dev, const char *propname, · line 175

Quoted 4 lines
drivers/base/property.c +4 -0
+       if (n > nval)
+               n = nval;
+
+       ret = device_property_read_u32_array(dev, propname, val, n);

So this reads "copy at most nval values from the array property".

If that's really what you need, it can be done in two lines of code in prospective callers of this wrapper.

Indeed the helper here is basically exactly what would be done in the caller if no helper existed.

Quoted 7 lines
Quoted 6 lines
include/linux/property.h +2 -1
+int device_property_read_u32_2darray(struct device *dev, const char *propname,
+                                    u32 *val, size_t nval, int multiple);
 int device_property_read_u64_array(struct device *dev, const char *propname,
                                   u64 *val, size_t nval);
 int device_property_read_string_array(struct device *dev, const char *propname,
--
Quoted 8 lines
Quoted 1 line

I don't see much value in this change, sorry.

That is fine, I don't have any problem with the helper living within our driver instead. Basically the issue from my side is I need to read 6 different device tree properties all of which require this behaviour, ie. read a variable number of fixed groups and check I have whole groups. Open coding this for each call is indeed only going to be 5-10 lines of code

Exactly two:

n = device_property_count_u32(dev, name);
ret = device_property_read_u32_array(dev, propname, val, n > nval ? nval : n);

And I would be fine with adding wrappers like this (and for the other data types too for that matter).

It would take more lines if you wanted to complain about the format, but as pointed out above, that would need to be done in the caller anyway.

Quoted 4 lines

for each one but since there are 6 of them it makes sense to put those 5-10 lines into a helper and have 5-10 lines not 30-60 lines. Seemed the helper might be generally more useful, but if it is not then it can go back into the driver.

author maintainer
#8 lore raw
Quoted 17 lines · Rafael J. Wysocki
Quoted 9 lines · Charles Keepax
Quoted 2 lines · Rafael J. Wysocki

On Wed, Jun 26, 2019 at 5:36 PM Charles Keepax
<[email protected]> wrote:

That is fine, I don't have any problem with the helper living within our driver instead. Basically the issue from my side is I need to read 6 different device tree properties all of which require this behaviour, ie. read a variable number of fixed groups and check I have whole groups. Open coding this for each call is indeed only going to be 5-10 lines of code

Exactly two:

n = device_property_count_u32(dev, name);
ret = device_property_read_u32_array(dev, propname, val, n > nval ? nval : n);

Quoted 7 lines

And I would be fine with adding wrappers like this (and for the other data types too for that matter).

It would take more lines if you wanted to complain about the format, but as pointed out above, that would need to be done in the caller anyway.

Ok I think that helps me to follow the situation. If device_property_count cuts down the code required and leaves the majority of the code as printing the messages which then wants to live in the end driver anyways it probably isn't worth adding a core helper for this.

Thank you for the review and the explanation. I will update the driver patches to use the new function and resend those.

Thanks,
Charles

Quoted 4 lines
Quoted 4 lines

for each one but since there are 6 of them it makes sense to put those 5-10 lines into a helper and have 5-10 lines not 30-60 lines. Seemed the helper might be generally more useful, but if it is not then it can go back into the driver.