aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of/base.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/of/base.c')
-rw-r--r--drivers/of/base.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 3ff22e32b602..b970562e0111 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -17,14 +17,39 @@
17 * as published by the Free Software Foundation; either version 17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version. 18 * 2 of the License, or (at your option) any later version.
19 */ 19 */
20#include <linux/ctype.h>
20#include <linux/module.h> 21#include <linux/module.h>
21#include <linux/of.h> 22#include <linux/of.h>
22#include <linux/spinlock.h> 23#include <linux/spinlock.h>
23#include <linux/slab.h> 24#include <linux/slab.h>
24#include <linux/proc_fs.h> 25#include <linux/proc_fs.h>
25 26
27/**
28 * struct alias_prop - Alias property in 'aliases' node
29 * @link: List node to link the structure in aliases_lookup list
30 * @alias: Alias property name
31 * @np: Pointer to device_node that the alias stands for
32 * @id: Index value from end of alias name
33 * @stem: Alias string without the index
34 *
35 * The structure represents one alias property of 'aliases' node as
36 * an entry in aliases_lookup list.
37 */
38struct alias_prop {
39 struct list_head link;
40 const char *alias;
41 struct device_node *np;
42 int id;
43 char stem[0];
44};
45
46static LIST_HEAD(aliases_lookup);
47
26struct device_node *allnodes; 48struct device_node *allnodes;
27struct device_node *of_chosen; 49struct device_node *of_chosen;
50struct device_node *of_aliases;
51
52static DEFINE_MUTEX(of_aliases_mutex);
28 53
29/* use when traversing tree through the allnext, child, sibling, 54/* use when traversing tree through the allnext, child, sibling,
30 * or parent members of struct device_node. 55 * or parent members of struct device_node.
@@ -632,6 +657,35 @@ int of_property_read_u32_array(const struct device_node *np,
632EXPORT_SYMBOL_GPL(of_property_read_u32_array); 657EXPORT_SYMBOL_GPL(of_property_read_u32_array);
633 658
634/** 659/**
660 * of_property_read_u64 - Find and read a 64 bit integer from a property
661 * @np: device node from which the property value is to be read.
662 * @propname: name of the property to be searched.
663 * @out_value: pointer to return value, modified only if return value is 0.
664 *
665 * Search for a property in a device node and read a 64-bit value from
666 * it. Returns 0 on success, -EINVAL if the property does not exist,
667 * -ENODATA if property does not have a value, and -EOVERFLOW if the
668 * property data isn't large enough.
669 *
670 * The out_value is modified only if a valid u64 value can be decoded.
671 */
672int of_property_read_u64(const struct device_node *np, const char *propname,
673 u64 *out_value)
674{
675 struct property *prop = of_find_property(np, propname, NULL);
676
677 if (!prop)
678 return -EINVAL;
679 if (!prop->value)
680 return -ENODATA;
681 if (sizeof(*out_value) > prop->length)
682 return -EOVERFLOW;
683 *out_value = of_read_number(prop->value, 2);
684 return 0;
685}
686EXPORT_SYMBOL_GPL(of_property_read_u64);
687
688/**
635 * of_property_read_string - Find and read a string from a property 689 * of_property_read_string - Find and read a string from a property
636 * @np: device node from which the property value is to be read. 690 * @np: device node from which the property value is to be read.
637 * @propname: name of the property to be searched. 691 * @propname: name of the property to be searched.
@@ -988,3 +1042,99 @@ out_unlock:
988} 1042}
989#endif /* defined(CONFIG_OF_DYNAMIC) */ 1043#endif /* defined(CONFIG_OF_DYNAMIC) */
990 1044
1045static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1046 int id, const char *stem, int stem_len)
1047{
1048 ap->np = np;
1049 ap->id = id;
1050 strncpy(ap->stem, stem, stem_len);
1051 ap->stem[stem_len] = 0;
1052 list_add_tail(&ap->link, &aliases_lookup);
1053 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1054 ap->alias, ap->stem, ap->id, np ? np->full_name : NULL);
1055}
1056
1057/**
1058 * of_alias_scan - Scan all properties of 'aliases' node
1059 *
1060 * The function scans all the properties of 'aliases' node and populate
1061 * the the global lookup table with the properties. It returns the
1062 * number of alias_prop found, or error code in error case.
1063 *
1064 * @dt_alloc: An allocator that provides a virtual address to memory
1065 * for the resulting tree
1066 */
1067void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1068{
1069 struct property *pp;
1070
1071 of_chosen = of_find_node_by_path("/chosen");
1072 if (of_chosen == NULL)
1073 of_chosen = of_find_node_by_path("/chosen@0");
1074 of_aliases = of_find_node_by_path("/aliases");
1075 if (!of_aliases)
1076 return;
1077
1078 for_each_property(pp, of_aliases->properties) {
1079 const char *start = pp->name;
1080 const char *end = start + strlen(start);
1081 struct device_node *np;
1082 struct alias_prop *ap;
1083 int id, len;
1084
1085 /* Skip those we do not want to proceed */
1086 if (!strcmp(pp->name, "name") ||
1087 !strcmp(pp->name, "phandle") ||
1088 !strcmp(pp->name, "linux,phandle"))
1089 continue;
1090
1091 np = of_find_node_by_path(pp->value);
1092 if (!np)
1093 continue;
1094
1095 /* walk the alias backwards to extract the id and work out
1096 * the 'stem' string */
1097 while (isdigit(*(end-1)) && end > start)
1098 end--;
1099 len = end - start;
1100
1101 if (kstrtoint(end, 10, &id) < 0)
1102 continue;
1103
1104 /* Allocate an alias_prop with enough space for the stem */
1105 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1106 if (!ap)
1107 continue;
1108 ap->alias = start;
1109 of_alias_add(ap, np, id, start, len);
1110 }
1111}
1112
1113/**
1114 * of_alias_get_id - Get alias id for the given device_node
1115 * @np: Pointer to the given device_node
1116 * @stem: Alias stem of the given device_node
1117 *
1118 * The function travels the lookup table to get alias id for the given
1119 * device_node and alias stem. It returns the alias id if find it.
1120 */
1121int of_alias_get_id(struct device_node *np, const char *stem)
1122{
1123 struct alias_prop *app;
1124 int id = -ENODEV;
1125
1126 mutex_lock(&of_aliases_mutex);
1127 list_for_each_entry(app, &aliases_lookup, link) {
1128 if (strcmp(app->stem, stem) != 0)
1129 continue;
1130
1131 if (np == app->np) {
1132 id = app->id;
1133 break;
1134 }
1135 }
1136 mutex_unlock(&of_aliases_mutex);
1137
1138 return id;
1139}
1140EXPORT_SYMBOL_GPL(of_alias_get_id);