diff options
author | Viresh Kumar <viresh.kumar@linaro.org> | 2012-11-19 23:45:19 -0500 |
---|---|---|
committer | Rob Herring <rob.herring@calxeda.com> | 2012-11-20 23:58:55 -0500 |
commit | be193249b4178158c0f697cb452b2bbf0cb16361 (patch) | |
tree | 4278eba967e625f0cdb6de865a83038243c51e80 | |
parent | 9846210b1ec9bbaa30022d6d8af7e55ef67ccb45 (diff) |
dt: add helper function to read u8 & u16 variables & arrays
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>
Reviewed-by: Stephen Warren <swarren@nvidia.com>
Signed-off-by: Rob Herring <rob.herring@calxeda.com>
-rw-r--r-- | drivers/of/base.c | 77 | ||||
-rw-r--r-- | include/linux/of.h | 30 |
2 files changed, 107 insertions, 0 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c index af3b22ac7627..f564e3107b3e 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) | |||
671 | EXPORT_SYMBOL(of_find_node_by_phandle); | 671 | EXPORT_SYMBOL(of_find_node_by_phandle); |
672 | 672 | ||
673 | /** | 673 | /** |
674 | * of_property_read_u8_array - Find and read an array of u8 from a property. | ||
675 | * | ||
676 | * @np: device node from which the property value is to be read. | ||
677 | * @propname: name of the property to be searched. | ||
678 | * @out_value: pointer to return value, modified only if return value is 0. | ||
679 | * @sz: number of array elements to read | ||
680 | * | ||
681 | * Search for a property in a device node and read 8-bit value(s) from | ||
682 | * it. Returns 0 on success, -EINVAL if the property does not exist, | ||
683 | * -ENODATA if property does not have a value, and -EOVERFLOW if the | ||
684 | * property data isn't large enough. | ||
685 | * | ||
686 | * dts entry of array should be like: | ||
687 | * property = /bits/ 8 <0x50 0x60 0x70>; | ||
688 | * | ||
689 | * The out_value is modified only if a valid u8 value can be decoded. | ||
690 | */ | ||
691 | int of_property_read_u8_array(const struct device_node *np, | ||
692 | const char *propname, u8 *out_values, size_t sz) | ||
693 | { | ||
694 | struct property *prop = of_find_property(np, propname, NULL); | ||
695 | const u8 *val; | ||
696 | |||
697 | if (!prop) | ||
698 | return -EINVAL; | ||
699 | if (!prop->value) | ||
700 | return -ENODATA; | ||
701 | if ((sz * sizeof(*out_values)) > prop->length) | ||
702 | return -EOVERFLOW; | ||
703 | |||
704 | val = prop->value; | ||
705 | while (sz--) | ||
706 | *out_values++ = *val++; | ||
707 | return 0; | ||
708 | } | ||
709 | EXPORT_SYMBOL_GPL(of_property_read_u8_array); | ||
710 | |||
711 | /** | ||
712 | * of_property_read_u16_array - Find and read an array of u16 from a property. | ||
713 | * | ||
714 | * @np: device node from which the property value is to be read. | ||
715 | * @propname: name of the property to be searched. | ||
716 | * @out_value: pointer to return value, modified only if return value is 0. | ||
717 | * @sz: number of array elements to read | ||
718 | * | ||
719 | * Search for a property in a device node and read 16-bit value(s) from | ||
720 | * it. Returns 0 on success, -EINVAL if the property does not exist, | ||
721 | * -ENODATA if property does not have a value, and -EOVERFLOW if the | ||
722 | * property data isn't large enough. | ||
723 | * | ||
724 | * dts entry of array should be like: | ||
725 | * property = /bits/ 16 <0x5000 0x6000 0x7000>; | ||
726 | * | ||
727 | * The out_value is modified only if a valid u16 value can be decoded. | ||
728 | */ | ||
729 | int of_property_read_u16_array(const struct device_node *np, | ||
730 | const char *propname, u16 *out_values, size_t sz) | ||
731 | { | ||
732 | struct property *prop = of_find_property(np, propname, NULL); | ||
733 | const __be16 *val; | ||
734 | |||
735 | if (!prop) | ||
736 | return -EINVAL; | ||
737 | if (!prop->value) | ||
738 | return -ENODATA; | ||
739 | if ((sz * sizeof(*out_values)) > prop->length) | ||
740 | return -EOVERFLOW; | ||
741 | |||
742 | val = prop->value; | ||
743 | while (sz--) | ||
744 | *out_values++ = be16_to_cpup(val++); | ||
745 | return 0; | ||
746 | } | ||
747 | EXPORT_SYMBOL_GPL(of_property_read_u16_array); | ||
748 | |||
749 | /** | ||
674 | * of_property_read_u32_array - Find and read an array of 32 bit integers | 750 | * of_property_read_u32_array - Find and read an array of 32 bit integers |
675 | * from a property. | 751 | * from a property. |
676 | * | 752 | * |
677 | * @np: device node from which the property value is to be read. | 753 | * @np: device node from which the property value is to be read. |
678 | * @propname: name of the property to be searched. | 754 | * @propname: name of the property to be searched. |
679 | * @out_value: pointer to return value, modified only if return value is 0. | 755 | * @out_value: pointer to return value, modified only if return value is 0. |
756 | * @sz: number of array elements to read | ||
680 | * | 757 | * |
681 | * Search for a property in a device node and read 32-bit value(s) from | 758 | * Search for a property in a device node and read 32-bit value(s) from |
682 | * it. Returns 0 on success, -EINVAL if the property does not exist, | 759 | * 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 857dde984a6e..ab1af0e14659 100644 --- a/include/linux/of.h +++ b/include/linux/of.h | |||
@@ -223,6 +223,10 @@ extern struct device_node *of_find_node_with_property( | |||
223 | extern struct property *of_find_property(const struct device_node *np, | 223 | extern struct property *of_find_property(const struct device_node *np, |
224 | const char *name, | 224 | const char *name, |
225 | int *lenp); | 225 | int *lenp); |
226 | extern int of_property_read_u8_array(const struct device_node *np, | ||
227 | const char *propname, u8 *out_values, size_t sz); | ||
228 | extern int of_property_read_u16_array(const struct device_node *np, | ||
229 | const char *propname, u16 *out_values, size_t sz); | ||
226 | extern int of_property_read_u32_array(const struct device_node *np, | 230 | extern int of_property_read_u32_array(const struct device_node *np, |
227 | const char *propname, | 231 | const char *propname, |
228 | u32 *out_values, | 232 | u32 *out_values, |
@@ -364,6 +368,18 @@ static inline struct device_node *of_find_compatible_node( | |||
364 | return NULL; | 368 | return NULL; |
365 | } | 369 | } |
366 | 370 | ||
371 | static inline int of_property_read_u8_array(const struct device_node *np, | ||
372 | const char *propname, u8 *out_values, size_t sz) | ||
373 | { | ||
374 | return -ENOSYS; | ||
375 | } | ||
376 | |||
377 | static inline int of_property_read_u16_array(const struct device_node *np, | ||
378 | const char *propname, u16 *out_values, size_t sz) | ||
379 | { | ||
380 | return -ENOSYS; | ||
381 | } | ||
382 | |||
367 | static inline int of_property_read_u32_array(const struct device_node *np, | 383 | static inline int of_property_read_u32_array(const struct device_node *np, |
368 | const char *propname, | 384 | const char *propname, |
369 | u32 *out_values, size_t sz) | 385 | u32 *out_values, size_t sz) |
@@ -470,6 +486,20 @@ static inline bool of_property_read_bool(const struct device_node *np, | |||
470 | return prop ? true : false; | 486 | return prop ? true : false; |
471 | } | 487 | } |
472 | 488 | ||
489 | static inline int of_property_read_u8(const struct device_node *np, | ||
490 | const char *propname, | ||
491 | u8 *out_value) | ||
492 | { | ||
493 | return of_property_read_u8_array(np, propname, out_value, 1); | ||
494 | } | ||
495 | |||
496 | static inline int of_property_read_u16(const struct device_node *np, | ||
497 | const char *propname, | ||
498 | u16 *out_value) | ||
499 | { | ||
500 | return of_property_read_u16_array(np, propname, out_value, 1); | ||
501 | } | ||
502 | |||
473 | static inline int of_property_read_u32(const struct device_node *np, | 503 | static inline int of_property_read_u32(const struct device_node *np, |
474 | const char *propname, | 504 | const char *propname, |
475 | u32 *out_value) | 505 | u32 *out_value) |