dt: add helper function to read u8 & u16 variables & arrays

Message ID 0ee5b74534a09d75c14b22c2d9330c4971ab30fb.1353386646.git.viresh.kumar@linaro.org
State Superseded
Headers show

Commit Message

Viresh Kumar Nov. 20, 2012, 4:45 a.m.
This adds following helper routines:
- of_property_read_u8_array()
- of_property_read_u16_array()
- of_property_read_u8()
- of_property_read_u16()

This expects arrays from DT to be passed as:
- u8 array:
	property = /bits/ 8 <0x50 0x60 0x70>;
- u16 array:
	property = /bits/ 16 <0x5000 0x6000 0x7000>;

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
V2->V3:
- Expect u8 & u16 arrays to be passed using: /bits/ 8 or 16
- remove common macro, as not much common now :(
- Tested on ARM platform.

 drivers/of/base.c  | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/of.h | 30 +++++++++++++++++++++
 2 files changed, 107 insertions(+)

Comments

Andy Shevchenko Nov. 20, 2012, 8:21 a.m. | #1
On Tue, 2012-11-20 at 10:15 +0530, Viresh Kumar wrote: 
> This adds following helper routines:

> - of_property_read_u8_array()

> - of_property_read_u16_array()

> - of_property_read_u8()

> - of_property_read_u16()

> 

> This expects arrays from DT to be passed as:

> - u8 array:

> 	property = /bits/ 8 <0x50 0x60 0x70>;

> - u16 array:

> 	property = /bits/ 16 <0x5000 0x6000 0x7000>;

> 

> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

> ---

> V2->V3:

> - Expect u8 & u16 arrays to be passed using: /bits/ 8 or 16

> - remove common macro, as not much common now :(


You could at least create macro to do a precheck if you want to.

Like 
#define CHECK_PROP(prop, sz, out)

{
if (!prop) 
		return -EINVAL;
	if (!prop->value)
		return -ENODATA;
	if ((sz * sizeof(*out)) > prop->length)
		return -EOVERFLOW;
}

Of course you can do even robust macro to cover types, but I don't like
ugly macros (as I can't see how it could be shaped nicely).

> - Tested on ARM platform.

> 


> --- a/drivers/of/base.c

> +++ b/drivers/of/base.c

> @@ -671,12 +671,89 @@ struct device_node *of_find_node_by_phandle(phandle handle)

>  EXPORT_SYMBOL(of_find_node_by_phandle);

>  

>  /**

> + * of_property_read_u8_array - Find and read an array of u8 from a property.

> + *

> + * @np:		device node from which the property value is to be read.

> + * @propname:	name of the property to be searched.

> + * @out_value:	pointer to return value, modified only if return value is 0.

> + * @sz:		number of array elements to read

> + *

> + * Search for a property in a device node and read 8-bit value(s) from

> + * it. Returns 0 on success, -EINVAL if the property does not exist,

> + * -ENODATA if property does not have a value, and -EOVERFLOW if the

> + * property data isn't large enough.

> + *

> + * dts entry of array should be like:

> + *	property = /bits/ 8 <0x50 0x60 0x70>;

> + *

> + * The out_value is modified only if a valid u8 value can be decoded.

> + */

> +int of_property_read_u8_array(const struct device_node *np,

> +			const char *propname, u8 *out_values, size_t sz)

> +{

> +	struct property *prop = of_find_property(np, propname, NULL);

> +	const u8 *val;

> +

> +	if (!prop)

> +		return -EINVAL;

> +	if (!prop->value)

> +		return -ENODATA;

> +	if ((sz * sizeof(*out_values)) > prop->length)

> +		return -EOVERFLOW;

> +

> +	val = prop->value;

> +	while (sz--)

> +		*out_values++ = *val++;

> +	return 0;

> +}

> +EXPORT_SYMBOL_GPL(of_property_read_u8_array);

> +

> +/**

> + * of_property_read_u16_array - Find and read an array of u16 from a property.

> + *

> + * @np:		device node from which the property value is to be read.

> + * @propname:	name of the property to be searched.

> + * @out_value:	pointer to return value, modified only if return value is 0.

> + * @sz:		number of array elements to read

> + *

> + * Search for a property in a device node and read 16-bit value(s) from

> + * it. Returns 0 on success, -EINVAL if the property does not exist,

> + * -ENODATA if property does not have a value, and -EOVERFLOW if the

> + * property data isn't large enough.

> + *

> + * dts entry of array should be like:

> + *	property = /bits/ 16 <0x5000 0x6000 0x7000>;

> + *

> + * The out_value is modified only if a valid u16 value can be decoded.

> + */

> +int of_property_read_u16_array(const struct device_node *np,

> +			const char *propname, u16 *out_values, size_t sz)

> +{

> +	struct property *prop = of_find_property(np, propname, NULL);

> +	const __be16 *val;

> +

> +	if (!prop)

> +		return -EINVAL;

> +	if (!prop->value)

> +		return -ENODATA;

> +	if ((sz * sizeof(*out_values)) > prop->length)

> +		return -EOVERFLOW;

> +

> +	val = prop->value;

> +	while (sz--)

> +		*out_values++ = be16_to_cpup(val++);

> +	return 0;

> +}

> +EXPORT_SYMBOL_GPL(of_property_read_u16_array);

> +

> +/**

>   * of_property_read_u32_array - Find and read an array of 32 bit integers

>   * from a property.

>   *

>   * @np:		device node from which the property value is to be read.

>   * @propname:	name of the property to be searched.

>   * @out_value:	pointer to return value, modified only if return value is 0.

> + * @sz:		number of array elements to read

>   *

>   * Search for a property in a device node and read 32-bit value(s) from

>   * it. Returns 0 on success, -EINVAL if the property does not exist,


-- 
Andy Shevchenko <andriy.shevchenko@intel.com>
Intel Finland Oy
---------------------------------------------------------------------
Intel Finland Oy
Registered Address: PL 281, 00181 Helsinki 
Business Identity Code: 0357606 - 4 
Domiciled in Helsinki 

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
Viresh Kumar Nov. 20, 2012, 8:25 a.m. | #2
On 20 November 2012 13:51, Shevchenko, Andriy
<andriy.shevchenko@intel.com> wrote:
> You could at least create macro to do a precheck if you want to.
>
> Like
> #define CHECK_PROP(prop, sz, out)
>
> {
> if (!prop)
>                 return -EINVAL;
>         if (!prop->value)
>                 return -ENODATA;
>         if ((sz * sizeof(*out)) > prop->length)
>                 return -EOVERFLOW;
> }

Hi Andriy,

Initially i started with a macro for the complete routine, but later as types
started to play important role in it, i have to split it to routines.

I thought of this idea to do prop check in a macro, but then i thought
it might cover a bigger range of files and so thought of doing that separately.
For, simplicity left it this time.

--
viresh
Stephen Warren Nov. 20, 2012, 5:21 p.m. | #3
On 11/19/2012 09:45 PM, Viresh Kumar wrote:
> This adds following helper routines:
> - of_property_read_u8_array()
> - of_property_read_u16_array()
> - of_property_read_u8()
> - of_property_read_u16()
> 
> This expects arrays from DT to be passed as:
> - u8 array:
> 	property = /bits/ 8 <0x50 0x60 0x70>;
> - u16 array:
> 	property = /bits/ 16 <0x5000 0x6000 0x7000>;

Reviewed-by: Stephen Warren <swarren@nvidia.com>
Rob Herring Nov. 21, 2012, 4:24 a.m. | #4
On 11/19/2012 10:45 PM, Viresh Kumar wrote:
> This adds following helper routines:
> - of_property_read_u8_array()
> - of_property_read_u16_array()
> - of_property_read_u8()
> - of_property_read_u16()
> 
> This expects arrays from DT to be passed as:
> - u8 array:
> 	property = /bits/ 8 <0x50 0x60 0x70>;
> - u16 array:
> 	property = /bits/ 16 <0x5000 0x6000 0x7000>;
> 
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

Applied.

Rob

> ---
> V2->V3:
> - Expect u8 & u16 arrays to be passed using: /bits/ 8 or 16
> - remove common macro, as not much common now :(
> - Tested on ARM platform.
> 
>  drivers/of/base.c  | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  include/linux/of.h | 30 +++++++++++++++++++++
>  2 files changed, 107 insertions(+)
> 
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index af3b22a..f564e31 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -671,12 +671,89 @@ struct device_node *of_find_node_by_phandle(phandle handle)
>  EXPORT_SYMBOL(of_find_node_by_phandle);
>  
>  /**
> + * of_property_read_u8_array - Find and read an array of u8 from a property.
> + *
> + * @np:		device node from which the property value is to be read.
> + * @propname:	name of the property to be searched.
> + * @out_value:	pointer to return value, modified only if return value is 0.
> + * @sz:		number of array elements to read
> + *
> + * Search for a property in a device node and read 8-bit value(s) from
> + * it. Returns 0 on success, -EINVAL if the property does not exist,
> + * -ENODATA if property does not have a value, and -EOVERFLOW if the
> + * property data isn't large enough.
> + *
> + * dts entry of array should be like:
> + *	property = /bits/ 8 <0x50 0x60 0x70>;
> + *
> + * The out_value is modified only if a valid u8 value can be decoded.
> + */
> +int of_property_read_u8_array(const struct device_node *np,
> +			const char *propname, u8 *out_values, size_t sz)
> +{
> +	struct property *prop = of_find_property(np, propname, NULL);
> +	const u8 *val;
> +
> +	if (!prop)
> +		return -EINVAL;
> +	if (!prop->value)
> +		return -ENODATA;
> +	if ((sz * sizeof(*out_values)) > prop->length)
> +		return -EOVERFLOW;
> +
> +	val = prop->value;
> +	while (sz--)
> +		*out_values++ = *val++;
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(of_property_read_u8_array);
> +
> +/**
> + * of_property_read_u16_array - Find and read an array of u16 from a property.
> + *
> + * @np:		device node from which the property value is to be read.
> + * @propname:	name of the property to be searched.
> + * @out_value:	pointer to return value, modified only if return value is 0.
> + * @sz:		number of array elements to read
> + *
> + * Search for a property in a device node and read 16-bit value(s) from
> + * it. Returns 0 on success, -EINVAL if the property does not exist,
> + * -ENODATA if property does not have a value, and -EOVERFLOW if the
> + * property data isn't large enough.
> + *
> + * dts entry of array should be like:
> + *	property = /bits/ 16 <0x5000 0x6000 0x7000>;
> + *
> + * The out_value is modified only if a valid u16 value can be decoded.
> + */
> +int of_property_read_u16_array(const struct device_node *np,
> +			const char *propname, u16 *out_values, size_t sz)
> +{
> +	struct property *prop = of_find_property(np, propname, NULL);
> +	const __be16 *val;
> +
> +	if (!prop)
> +		return -EINVAL;
> +	if (!prop->value)
> +		return -ENODATA;
> +	if ((sz * sizeof(*out_values)) > prop->length)
> +		return -EOVERFLOW;
> +
> +	val = prop->value;
> +	while (sz--)
> +		*out_values++ = be16_to_cpup(val++);
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(of_property_read_u16_array);
> +
> +/**
>   * of_property_read_u32_array - Find and read an array of 32 bit integers
>   * from a property.
>   *
>   * @np:		device node from which the property value is to be read.
>   * @propname:	name of the property to be searched.
>   * @out_value:	pointer to return value, modified only if return value is 0.
> + * @sz:		number of array elements to read
>   *
>   * Search for a property in a device node and read 32-bit value(s) from
>   * it. Returns 0 on success, -EINVAL if the property does not exist,
> diff --git a/include/linux/of.h b/include/linux/of.h
> index b4e50d5..bfdc130 100644
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -223,6 +223,10 @@ extern struct device_node *of_find_node_with_property(
>  extern struct property *of_find_property(const struct device_node *np,
>  					 const char *name,
>  					 int *lenp);
> +extern int of_property_read_u8_array(const struct device_node *np,
> +			const char *propname, u8 *out_values, size_t sz);
> +extern int of_property_read_u16_array(const struct device_node *np,
> +			const char *propname, u16 *out_values, size_t sz);
>  extern int of_property_read_u32_array(const struct device_node *np,
>  				      const char *propname,
>  				      u32 *out_values,
> @@ -364,6 +368,18 @@ static inline struct device_node *of_find_compatible_node(
>  	return NULL;
>  }
>  
> +static inline int of_property_read_u8_array(const struct device_node *np,
> +			const char *propname, u8 *out_values, size_t sz)
> +{
> +	return -ENOSYS;
> +}
> +
> +static inline int of_property_read_u16_array(const struct device_node *np,
> +			const char *propname, u16 *out_values, size_t sz)
> +{
> +	return -ENOSYS;
> +}
> +
>  static inline int of_property_read_u32_array(const struct device_node *np,
>  					     const char *propname,
>  					     u32 *out_values, size_t sz)
> @@ -470,6 +486,20 @@ static inline bool of_property_read_bool(const struct device_node *np,
>  	return prop ? true : false;
>  }
>  
> +static inline int of_property_read_u8(const struct device_node *np,
> +				       const char *propname,
> +				       u8 *out_value)
> +{
> +	return of_property_read_u8_array(np, propname, out_value, 1);
> +}
> +
> +static inline int of_property_read_u16(const struct device_node *np,
> +				       const char *propname,
> +				       u16 *out_value)
> +{
> +	return of_property_read_u16_array(np, propname, out_value, 1);
> +}
> +
>  static inline int of_property_read_u32(const struct device_node *np,
>  				       const char *propname,
>  				       u32 *out_value)
>

Patch

diff --git a/drivers/of/base.c b/drivers/of/base.c
index af3b22a..f564e31 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -671,12 +671,89 @@  struct device_node *of_find_node_by_phandle(phandle handle)
 EXPORT_SYMBOL(of_find_node_by_phandle);
 
 /**
+ * of_property_read_u8_array - Find and read an array of u8 from a property.
+ *
+ * @np:		device node from which the property value is to be read.
+ * @propname:	name of the property to be searched.
+ * @out_value:	pointer to return value, modified only if return value is 0.
+ * @sz:		number of array elements to read
+ *
+ * Search for a property in a device node and read 8-bit value(s) from
+ * it. Returns 0 on success, -EINVAL if the property does not exist,
+ * -ENODATA if property does not have a value, and -EOVERFLOW if the
+ * property data isn't large enough.
+ *
+ * dts entry of array should be like:
+ *	property = /bits/ 8 <0x50 0x60 0x70>;
+ *
+ * The out_value is modified only if a valid u8 value can be decoded.
+ */
+int of_property_read_u8_array(const struct device_node *np,
+			const char *propname, u8 *out_values, size_t sz)
+{
+	struct property *prop = of_find_property(np, propname, NULL);
+	const u8 *val;
+
+	if (!prop)
+		return -EINVAL;
+	if (!prop->value)
+		return -ENODATA;
+	if ((sz * sizeof(*out_values)) > prop->length)
+		return -EOVERFLOW;
+
+	val = prop->value;
+	while (sz--)
+		*out_values++ = *val++;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(of_property_read_u8_array);
+
+/**
+ * of_property_read_u16_array - Find and read an array of u16 from a property.
+ *
+ * @np:		device node from which the property value is to be read.
+ * @propname:	name of the property to be searched.
+ * @out_value:	pointer to return value, modified only if return value is 0.
+ * @sz:		number of array elements to read
+ *
+ * Search for a property in a device node and read 16-bit value(s) from
+ * it. Returns 0 on success, -EINVAL if the property does not exist,
+ * -ENODATA if property does not have a value, and -EOVERFLOW if the
+ * property data isn't large enough.
+ *
+ * dts entry of array should be like:
+ *	property = /bits/ 16 <0x5000 0x6000 0x7000>;
+ *
+ * The out_value is modified only if a valid u16 value can be decoded.
+ */
+int of_property_read_u16_array(const struct device_node *np,
+			const char *propname, u16 *out_values, size_t sz)
+{
+	struct property *prop = of_find_property(np, propname, NULL);
+	const __be16 *val;
+
+	if (!prop)
+		return -EINVAL;
+	if (!prop->value)
+		return -ENODATA;
+	if ((sz * sizeof(*out_values)) > prop->length)
+		return -EOVERFLOW;
+
+	val = prop->value;
+	while (sz--)
+		*out_values++ = be16_to_cpup(val++);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(of_property_read_u16_array);
+
+/**
  * of_property_read_u32_array - Find and read an array of 32 bit integers
  * from a property.
  *
  * @np:		device node from which the property value is to be read.
  * @propname:	name of the property to be searched.
  * @out_value:	pointer to return value, modified only if return value is 0.
+ * @sz:		number of array elements to read
  *
  * Search for a property in a device node and read 32-bit value(s) from
  * it. Returns 0 on success, -EINVAL if the property does not exist,
diff --git a/include/linux/of.h b/include/linux/of.h
index b4e50d5..bfdc130 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -223,6 +223,10 @@  extern struct device_node *of_find_node_with_property(
 extern struct property *of_find_property(const struct device_node *np,
 					 const char *name,
 					 int *lenp);
+extern int of_property_read_u8_array(const struct device_node *np,
+			const char *propname, u8 *out_values, size_t sz);
+extern int of_property_read_u16_array(const struct device_node *np,
+			const char *propname, u16 *out_values, size_t sz);
 extern int of_property_read_u32_array(const struct device_node *np,
 				      const char *propname,
 				      u32 *out_values,
@@ -364,6 +368,18 @@  static inline struct device_node *of_find_compatible_node(
 	return NULL;
 }
 
+static inline int of_property_read_u8_array(const struct device_node *np,
+			const char *propname, u8 *out_values, size_t sz)
+{
+	return -ENOSYS;
+}
+
+static inline int of_property_read_u16_array(const struct device_node *np,
+			const char *propname, u16 *out_values, size_t sz)
+{
+	return -ENOSYS;
+}
+
 static inline int of_property_read_u32_array(const struct device_node *np,
 					     const char *propname,
 					     u32 *out_values, size_t sz)
@@ -470,6 +486,20 @@  static inline bool of_property_read_bool(const struct device_node *np,
 	return prop ? true : false;
 }
 
+static inline int of_property_read_u8(const struct device_node *np,
+				       const char *propname,
+				       u8 *out_value)
+{
+	return of_property_read_u8_array(np, propname, out_value, 1);
+}
+
+static inline int of_property_read_u16(const struct device_node *np,
+				       const char *propname,
+				       u16 *out_value)
+{
+	return of_property_read_u16_array(np, propname, out_value, 1);
+}
+
 static inline int of_property_read_u32(const struct device_node *np,
 				       const char *propname,
 				       u32 *out_value)