device property: Add fwnode_property_match_property_string()
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Tue, 8 Aug 2023 16:27:56 +0000 (19:27 +0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Thu, 16 Nov 2023 19:10:26 +0000 (19:10 +0000)
Sometimes the users want to match the single value string property
against an array of predefined strings. Create a helper for them.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Rafael J. Wysocki <rafael@kernel.org>
Link: https://lore.kernel.org/r/20230808162800.61651-3-andriy.shevchenko@linux.intel.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/base/property.c
include/linux/property.h

index 3bb9505f1631a4cdb89bd1d23a86876adcbdc63e..8f8e2a6816bc00a67d3600d5e46bed9ac3fff33b 100644 (file)
@@ -498,6 +498,41 @@ out_free:
 }
 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
 
+/**
+ * fwnode_property_match_property_string - find a property string value in an array and return index
+ * @fwnode: Firmware node to get the property of
+ * @propname: Name of the property holding the string value
+ * @array: String array to search in
+ * @n: Size of the @array
+ *
+ * Find a property string value in a given @array and if it is found return
+ * the index back.
+ *
+ * Return: index, starting from %0, if the string value was found in the @array (success),
+ *        %-ENOENT when the string value was not found in the @array,
+ *        %-EINVAL if given arguments are not valid,
+ *        %-ENODATA if the property does not have a value,
+ *        %-EPROTO or %-EILSEQ if the property is not a string,
+ *        %-ENXIO if no suitable firmware interface is present.
+ */
+int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
+       const char *propname, const char * const *array, size_t n)
+{
+       const char *string;
+       int ret;
+
+       ret = fwnode_property_read_string(fwnode, propname, &string);
+       if (ret)
+               return ret;
+
+       ret = match_string(array, n, string);
+       if (ret < 0)
+               ret = -ENOENT;
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);
+
 /**
  * fwnode_property_get_reference_args() - Find a reference with arguments
  * @fwnode:    Firmware node where to look for the reference
index 9f2585d705a867ce2f7d35da322a1e9ee6292360..2b8f07fc68a97c5f77694dc08f10269c918b114f 100644 (file)
@@ -98,6 +98,18 @@ static inline bool device_is_compatible(const struct device *dev, const char *co
        return fwnode_device_is_compatible(dev_fwnode(dev), compat);
 }
 
+int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
+                                         const char *propname,
+                                         const char * const *array, size_t n);
+
+static inline
+int device_property_match_property_string(const struct device *dev,
+                                         const char *propname,
+                                         const char * const *array, size_t n)
+{
+       return fwnode_property_match_property_string(dev_fwnode(dev), propname, array, n);
+}
+
 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
                                       const char *prop, const char *nargs_prop,
                                       unsigned int nargs, unsigned int index,