diff mbox series

[v3,4/9] device property: add fwnode_property_read_string_index()

Message ID 20220325113148.588163-5-clement.leger@bootlin.com (mailing list archive)
State Handled Elsewhere, archived
Headers show
Series introduce fwnode in the I2C subsystem | expand

Commit Message

Clément Léger March 25, 2022, 11:31 a.m. UTC
Add fwnode_property_read_string_index() function which allows to
retrieve a single string from an array by its index. This function is
the equivalent of of_property_read_string_index() but for fwnode
support.

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
---
 drivers/base/property.c  | 33 +++++++++++++++++++++++++++++++++
 include/linux/property.h |  3 +++
 2 files changed, 36 insertions(+)

Comments

Andy Shevchenko March 25, 2022, 2:33 p.m. UTC | #1
On Fri, Mar 25, 2022 at 12:31:43PM +0100, Clément Léger wrote:
> Add fwnode_property_read_string_index() function which allows to
> retrieve a single string from an array by its index. This function is
> the equivalent of of_property_read_string_index() but for fwnode
> support.

...

> +	ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
> +				 string, 1, index);
> +	if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
> +	    !IS_ERR_OR_NULL(fwnode->secondary))
> +		ret = fwnode_call_int_op(fwnode->secondary,
> +					 property_read_string_array, propname,
> +					 string, 1, index);

This is not fully correct. See [1] for the details.
I hope to send the new version just after the merge window ends.

[1]: https://lore.kernel.org/lkml/20220308123712.18613-1-andriy.shevchenko@linux.intel.com/
Clément Léger March 25, 2022, 3:10 p.m. UTC | #2
Le Fri, 25 Mar 2022 16:33:54 +0200,
Andy Shevchenko <andriy.shevchenko@linux.intel.com> a écrit :

> On Fri, Mar 25, 2022 at 12:31:43PM +0100, Clément Léger wrote:
> > Add fwnode_property_read_string_index() function which allows to
> > retrieve a single string from an array by its index. This function is
> > the equivalent of of_property_read_string_index() but for fwnode
> > support.  
> 
> ...
> 
> > +	ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
> > +				 string, 1, index);
> > +	if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
> > +	    !IS_ERR_OR_NULL(fwnode->secondary))
> > +		ret = fwnode_call_int_op(fwnode->secondary,
> > +					 property_read_string_array, propname,
> > +					 string, 1, index);  
> 
> This is not fully correct. See [1] for the details.
> I hope to send the new version just after the merge window ends.
> 
> [1]: https://lore.kernel.org/lkml/20220308123712.18613-1-andriy.shevchenko@linux.intel.com/
> 

Ok, I think we can wait for your patch to be applied in that case.
diff mbox series

Patch

diff --git a/drivers/base/property.c b/drivers/base/property.c
index d95c949e0a79..a2cfdf57b847 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -406,6 +406,39 @@  int fwnode_property_read_string(const struct fwnode_handle *fwnode,
 }
 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
 
+/**
+ * fwnode_property_read_string_index - read a string in an array using an index
+ * @fwnode: Firmware node to get the property of
+ * @propname: Name of the property holding the array
+ * @index: Index of the string to look for
+ * @string: Pointer to the string if found
+ *
+ * Find a string by a given index in a string array and if it is found return
+ * the string value in @string.
+ *
+ * Return: %0 if the string was found (success),
+ *	   %-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 strings,
+ *	   %-ENXIO if no suitable firmware interface is present.
+ */
+int fwnode_property_read_string_index(const struct fwnode_handle *fwnode,
+				      const char *propname, int index,
+				      const char **string)
+{
+	int ret;
+
+	ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
+				 string, 1, index);
+	if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
+	    !IS_ERR_OR_NULL(fwnode->secondary))
+		ret = fwnode_call_int_op(fwnode->secondary,
+					 property_read_string_array, propname,
+					 string, 1, index);
+	return ret == 1 ? 0 : ret;
+}
+EXPORT_SYMBOL_GPL(fwnode_property_read_string_index);
+
 /**
  * fwnode_property_match_string - find a string in an array and return index
  * @fwnode: Firmware node to get the property of
diff --git a/include/linux/property.h b/include/linux/property.h
index 7399a0b45f98..a033920eb10a 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -70,6 +70,9 @@  int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
 				      size_t nval);
 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
 				const char *propname, const char **val);
+int fwnode_property_read_string_index(const struct fwnode_handle *fwnode,
+				      const char *propname, int index,
+				      const char **string);
 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
 				 const char *propname, const char *string);
 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,