Skip to content

Commit de135d7

Browse files
andy-shevjic23
authored andcommitted
device property: Add fwnode_property_match_property_string()
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>
1 parent 9732d64 commit de135d7

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

drivers/base/property.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,41 @@ int fwnode_property_match_string(const struct fwnode_handle *fwnode,
498498
}
499499
EXPORT_SYMBOL_GPL(fwnode_property_match_string);
500500

501+
/**
502+
* fwnode_property_match_property_string - find a property string value in an array and return index
503+
* @fwnode: Firmware node to get the property of
504+
* @propname: Name of the property holding the string value
505+
* @array: String array to search in
506+
* @n: Size of the @array
507+
*
508+
* Find a property string value in a given @array and if it is found return
509+
* the index back.
510+
*
511+
* Return: index, starting from %0, if the string value was found in the @array (success),
512+
* %-ENOENT when the string value was not found in the @array,
513+
* %-EINVAL if given arguments are not valid,
514+
* %-ENODATA if the property does not have a value,
515+
* %-EPROTO or %-EILSEQ if the property is not a string,
516+
* %-ENXIO if no suitable firmware interface is present.
517+
*/
518+
int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
519+
const char *propname, const char * const *array, size_t n)
520+
{
521+
const char *string;
522+
int ret;
523+
524+
ret = fwnode_property_read_string(fwnode, propname, &string);
525+
if (ret)
526+
return ret;
527+
528+
ret = match_string(array, n, string);
529+
if (ret < 0)
530+
ret = -ENOENT;
531+
532+
return ret;
533+
}
534+
EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);
535+
501536
/**
502537
* fwnode_property_get_reference_args() - Find a reference with arguments
503538
* @fwnode: Firmware node where to look for the reference

include/linux/property.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ static inline bool device_is_compatible(const struct device *dev, const char *co
9797
return fwnode_device_is_compatible(dev_fwnode(dev), compat);
9898
}
9999

100+
int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,
101+
const char *propname,
102+
const char * const *array, size_t n);
103+
104+
static inline
105+
int device_property_match_property_string(const struct device *dev,
106+
const char *propname,
107+
const char * const *array, size_t n)
108+
{
109+
return fwnode_property_match_property_string(dev_fwnode(dev), propname, array, n);
110+
}
111+
100112
int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
101113
const char *prop, const char *nargs_prop,
102114
unsigned int nargs, unsigned int index,

0 commit comments

Comments
 (0)