aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Warren <swarren@nvidia.com>2012-04-04 11:27:46 -0400
committerLinus Walleij <linus.walleij@linaro.org>2012-04-18 07:53:11 -0400
commitc541adc637066407d4cda9db14dcb0e618966a4c (patch)
tree0fda4e8f97c9e9a2a61524f3bb390a0a6b04ccbe
parentc05127c4e2c6e7d9949347a76fd05c337bcd5e84 (diff)
dt: add property iteration helpers
This patch adds macros of_property_for_each_u32() and of_property_for_each_string(), which iterate over an array of values within a device-tree property. Usage is for example: struct property *prop; const __be32 *p; u32 u; of_property_for_each_u32(np, "propname", prop, p, u) printk("U32 value: %x\n", u); struct property *prop; const char *s; of_property_for_each_string(np, "propname", prop, s) printk("String value: %s\n", s); Based on work by Rob Herring <robherring2@gmail.com> Cc: Grant Likely <grant.likely@secretlab.ca> Signed-off-by: Stephen Warren <swarren@nvidia.com> Acked-by: Rob Herring <rob.herring@calxeda.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r--drivers/of/base.c41
-rw-r--r--include/linux/of.h35
2 files changed, 76 insertions, 0 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 580644986945..d9bfd49b1935 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -1260,3 +1260,44 @@ int of_alias_get_id(struct device_node *np, const char *stem)
1260 return id; 1260 return id;
1261} 1261}
1262EXPORT_SYMBOL_GPL(of_alias_get_id); 1262EXPORT_SYMBOL_GPL(of_alias_get_id);
1263
1264const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1265 u32 *pu)
1266{
1267 const void *curv = cur;
1268
1269 if (!prop)
1270 return NULL;
1271
1272 if (!cur) {
1273 curv = prop->value;
1274 goto out_val;
1275 }
1276
1277 curv += sizeof(*cur);
1278 if (curv >= prop->value + prop->length)
1279 return NULL;
1280
1281out_val:
1282 *pu = be32_to_cpup(curv);
1283 return curv;
1284}
1285EXPORT_SYMBOL_GPL(of_prop_next_u32);
1286
1287const char *of_prop_next_string(struct property *prop, const char *cur)
1288{
1289 const void *curv = cur;
1290
1291 if (!prop)
1292 return NULL;
1293
1294 if (!cur)
1295 return prop->value;
1296
1297 curv += strlen(cur) + 1;
1298 if (curv >= prop->value + prop->length)
1299 return NULL;
1300
1301 return curv;
1302}
1303EXPORT_SYMBOL_GPL(of_prop_next_string);
diff --git a/include/linux/of.h b/include/linux/of.h
index fa7fb1d97458..e3f942d9da89 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -259,6 +259,37 @@ extern void of_detach_node(struct device_node *);
259#endif 259#endif
260 260
261#define of_match_ptr(_ptr) (_ptr) 261#define of_match_ptr(_ptr) (_ptr)
262
263/*
264 * struct property *prop;
265 * const __be32 *p;
266 * u32 u;
267 *
268 * of_property_for_each_u32(np, "propname", prop, p, u)
269 * printk("U32 value: %x\n", u);
270 */
271const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
272 u32 *pu);
273#define of_property_for_each_u32(np, propname, prop, p, u) \
274 for (prop = of_find_property(np, propname, NULL), \
275 p = of_prop_next_u32(prop, NULL, &u); \
276 p; \
277 p = of_prop_next_u32(prop, p, &u))
278
279/*
280 * struct property *prop;
281 * const char *s;
282 *
283 * of_property_for_each_string(np, "propname", prop, s)
284 * printk("String value: %s\n", s);
285 */
286const char *of_prop_next_string(struct property *prop, const char *cur);
287#define of_property_for_each_string(np, propname, prop, s) \
288 for (prop = of_find_property(np, propname, NULL), \
289 s = of_prop_next_string(prop, NULL); \
290 s; \
291 s = of_prop_next_string(prop, s))
292
262#else /* CONFIG_OF */ 293#else /* CONFIG_OF */
263 294
264static inline bool of_have_populated_dt(void) 295static inline bool of_have_populated_dt(void)
@@ -349,6 +380,10 @@ static inline int of_machine_is_compatible(const char *compat)
349 380
350#define of_match_ptr(_ptr) NULL 381#define of_match_ptr(_ptr) NULL
351#define of_match_node(_matches, _node) NULL 382#define of_match_node(_matches, _node) NULL
383#define of_property_for_each_u32(np, propname, prop, p, u) \
384 while (0)
385#define of_property_for_each_string(np, propname, prop, s) \
386 while (0)
352#endif /* CONFIG_OF */ 387#endif /* CONFIG_OF */
353 388
354/** 389/**