of: Add device_type access helper functions
[linux-2.6/btrfs-unstable.git] / include / linux / of.h
blob99b0ebf496329f347cf723ad5da07ad5c23bcf9d
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 #ifndef _LINUX_OF_H
3 #define _LINUX_OF_H
4 /*
5 * Definitions for talking to the Open Firmware PROM on
6 * Power Macintosh and other computers.
8 * Copyright (C) 1996-2005 Paul Mackerras.
10 * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
11 * Updates for SPARC64 by David S. Miller
12 * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
14 #include <linux/types.h>
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/kobject.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/spinlock.h>
20 #include <linux/topology.h>
21 #include <linux/notifier.h>
22 #include <linux/property.h>
23 #include <linux/list.h>
25 #include <asm/byteorder.h>
26 #include <asm/errno.h>
28 typedef u32 phandle;
29 typedef u32 ihandle;
31 struct property {
32 char *name;
33 int length;
34 void *value;
35 struct property *next;
36 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
37 unsigned long _flags;
38 #endif
39 #if defined(CONFIG_OF_PROMTREE)
40 unsigned int unique_id;
41 #endif
42 #if defined(CONFIG_OF_KOBJ)
43 struct bin_attribute attr;
44 #endif
47 #if defined(CONFIG_SPARC)
48 struct of_irq_controller;
49 #endif
51 struct device_node {
52 const char *name;
53 const char *type;
54 phandle phandle;
55 const char *full_name;
56 struct fwnode_handle fwnode;
58 struct property *properties;
59 struct property *deadprops; /* removed properties */
60 struct device_node *parent;
61 struct device_node *child;
62 struct device_node *sibling;
63 #if defined(CONFIG_OF_KOBJ)
64 struct kobject kobj;
65 #endif
66 unsigned long _flags;
67 void *data;
68 #if defined(CONFIG_SPARC)
69 const char *path_component_name;
70 unsigned int unique_id;
71 struct of_irq_controller *irq_trans;
72 #endif
75 #define MAX_PHANDLE_ARGS 16
76 struct of_phandle_args {
77 struct device_node *np;
78 int args_count;
79 uint32_t args[MAX_PHANDLE_ARGS];
82 struct of_phandle_iterator {
83 /* Common iterator information */
84 const char *cells_name;
85 int cell_count;
86 const struct device_node *parent;
88 /* List size information */
89 const __be32 *list_end;
90 const __be32 *phandle_end;
92 /* Current position state */
93 const __be32 *cur;
94 uint32_t cur_count;
95 phandle phandle;
96 struct device_node *node;
99 struct of_reconfig_data {
100 struct device_node *dn;
101 struct property *prop;
102 struct property *old_prop;
105 /* initialize a node */
106 extern struct kobj_type of_node_ktype;
107 extern const struct fwnode_operations of_fwnode_ops;
108 static inline void of_node_init(struct device_node *node)
110 #if defined(CONFIG_OF_KOBJ)
111 kobject_init(&node->kobj, &of_node_ktype);
112 #endif
113 node->fwnode.ops = &of_fwnode_ops;
116 #if defined(CONFIG_OF_KOBJ)
117 #define of_node_kobj(n) (&(n)->kobj)
118 #else
119 #define of_node_kobj(n) NULL
120 #endif
122 #ifdef CONFIG_OF_DYNAMIC
123 extern struct device_node *of_node_get(struct device_node *node);
124 extern void of_node_put(struct device_node *node);
125 #else /* CONFIG_OF_DYNAMIC */
126 /* Dummy ref counting routines - to be implemented later */
127 static inline struct device_node *of_node_get(struct device_node *node)
129 return node;
131 static inline void of_node_put(struct device_node *node) { }
132 #endif /* !CONFIG_OF_DYNAMIC */
134 /* Pointer for first entry in chain of all nodes. */
135 extern struct device_node *of_root;
136 extern struct device_node *of_chosen;
137 extern struct device_node *of_aliases;
138 extern struct device_node *of_stdout;
139 extern raw_spinlock_t devtree_lock;
141 /* flag descriptions (need to be visible even when !CONFIG_OF) */
142 #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
143 #define OF_DETACHED 2 /* node has been detached from the device tree */
144 #define OF_POPULATED 3 /* device already created for the node */
145 #define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
147 #define OF_BAD_ADDR ((u64)-1)
149 #ifdef CONFIG_OF
150 void of_core_init(void);
152 static inline bool is_of_node(const struct fwnode_handle *fwnode)
154 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
157 #define to_of_node(__fwnode) \
158 ({ \
159 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
161 is_of_node(__to_of_node_fwnode) ? \
162 container_of(__to_of_node_fwnode, \
163 struct device_node, fwnode) : \
164 NULL; \
167 #define of_fwnode_handle(node) \
168 ({ \
169 typeof(node) __of_fwnode_handle_node = (node); \
171 __of_fwnode_handle_node ? \
172 &__of_fwnode_handle_node->fwnode : NULL; \
175 static inline bool of_have_populated_dt(void)
177 return of_root != NULL;
180 static inline bool of_node_is_root(const struct device_node *node)
182 return node && (node->parent == NULL);
185 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
187 return test_bit(flag, &n->_flags);
190 static inline int of_node_test_and_set_flag(struct device_node *n,
191 unsigned long flag)
193 return test_and_set_bit(flag, &n->_flags);
196 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
198 set_bit(flag, &n->_flags);
201 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
203 clear_bit(flag, &n->_flags);
206 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
207 static inline int of_property_check_flag(struct property *p, unsigned long flag)
209 return test_bit(flag, &p->_flags);
212 static inline void of_property_set_flag(struct property *p, unsigned long flag)
214 set_bit(flag, &p->_flags);
217 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
219 clear_bit(flag, &p->_flags);
221 #endif
223 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
224 extern struct device_node *of_find_all_nodes(struct device_node *prev);
227 * OF address retrieval & translation
230 /* Helper to read a big number; size is in cells (not bytes) */
231 static inline u64 of_read_number(const __be32 *cell, int size)
233 u64 r = 0;
234 while (size--)
235 r = (r << 32) | be32_to_cpu(*(cell++));
236 return r;
239 /* Like of_read_number, but we want an unsigned long result */
240 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
242 /* toss away upper bits if unsigned long is smaller than u64 */
243 return of_read_number(cell, size);
246 #if defined(CONFIG_SPARC)
247 #include <asm/prom.h>
248 #endif
250 /* Default #address and #size cells. Allow arch asm/prom.h to override */
251 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
252 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
253 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
254 #endif
256 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
257 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
259 extern bool of_node_name_eq(const struct device_node *np, const char *name);
260 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
262 static inline const char *of_node_full_name(const struct device_node *np)
264 return np ? np->full_name : "<no-node>";
267 #define for_each_of_allnodes_from(from, dn) \
268 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
269 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
270 extern struct device_node *of_find_node_by_name(struct device_node *from,
271 const char *name);
272 extern struct device_node *of_find_node_by_type(struct device_node *from,
273 const char *type);
274 extern struct device_node *of_find_compatible_node(struct device_node *from,
275 const char *type, const char *compat);
276 extern struct device_node *of_find_matching_node_and_match(
277 struct device_node *from,
278 const struct of_device_id *matches,
279 const struct of_device_id **match);
281 extern struct device_node *of_find_node_opts_by_path(const char *path,
282 const char **opts);
283 static inline struct device_node *of_find_node_by_path(const char *path)
285 return of_find_node_opts_by_path(path, NULL);
288 extern struct device_node *of_find_node_by_phandle(phandle handle);
289 extern struct device_node *of_get_parent(const struct device_node *node);
290 extern struct device_node *of_get_next_parent(struct device_node *node);
291 extern struct device_node *of_get_next_child(const struct device_node *node,
292 struct device_node *prev);
293 extern struct device_node *of_get_next_available_child(
294 const struct device_node *node, struct device_node *prev);
296 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
297 const char *compatible);
298 extern struct device_node *of_get_child_by_name(const struct device_node *node,
299 const char *name);
301 /* cache lookup */
302 extern struct device_node *of_find_next_cache_node(const struct device_node *);
303 extern int of_find_last_cache_level(unsigned int cpu);
304 extern struct device_node *of_find_node_with_property(
305 struct device_node *from, const char *prop_name);
307 extern struct property *of_find_property(const struct device_node *np,
308 const char *name,
309 int *lenp);
310 extern int of_property_count_elems_of_size(const struct device_node *np,
311 const char *propname, int elem_size);
312 extern int of_property_read_u32_index(const struct device_node *np,
313 const char *propname,
314 u32 index, u32 *out_value);
315 extern int of_property_read_u64_index(const struct device_node *np,
316 const char *propname,
317 u32 index, u64 *out_value);
318 extern int of_property_read_variable_u8_array(const struct device_node *np,
319 const char *propname, u8 *out_values,
320 size_t sz_min, size_t sz_max);
321 extern int of_property_read_variable_u16_array(const struct device_node *np,
322 const char *propname, u16 *out_values,
323 size_t sz_min, size_t sz_max);
324 extern int of_property_read_variable_u32_array(const struct device_node *np,
325 const char *propname,
326 u32 *out_values,
327 size_t sz_min,
328 size_t sz_max);
329 extern int of_property_read_u64(const struct device_node *np,
330 const char *propname, u64 *out_value);
331 extern int of_property_read_variable_u64_array(const struct device_node *np,
332 const char *propname,
333 u64 *out_values,
334 size_t sz_min,
335 size_t sz_max);
337 extern int of_property_read_string(const struct device_node *np,
338 const char *propname,
339 const char **out_string);
340 extern int of_property_match_string(const struct device_node *np,
341 const char *propname,
342 const char *string);
343 extern int of_property_read_string_helper(const struct device_node *np,
344 const char *propname,
345 const char **out_strs, size_t sz, int index);
346 extern int of_device_is_compatible(const struct device_node *device,
347 const char *);
348 extern int of_device_compatible_match(struct device_node *device,
349 const char *const *compat);
350 extern bool of_device_is_available(const struct device_node *device);
351 extern bool of_device_is_big_endian(const struct device_node *device);
352 extern const void *of_get_property(const struct device_node *node,
353 const char *name,
354 int *lenp);
355 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
356 #define for_each_property_of_node(dn, pp) \
357 for (pp = dn->properties; pp != NULL; pp = pp->next)
359 extern int of_n_addr_cells(struct device_node *np);
360 extern int of_n_size_cells(struct device_node *np);
361 extern const struct of_device_id *of_match_node(
362 const struct of_device_id *matches, const struct device_node *node);
363 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
364 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
365 extern struct device_node *of_parse_phandle(const struct device_node *np,
366 const char *phandle_name,
367 int index);
368 extern int of_parse_phandle_with_args(const struct device_node *np,
369 const char *list_name, const char *cells_name, int index,
370 struct of_phandle_args *out_args);
371 extern int of_parse_phandle_with_args_map(const struct device_node *np,
372 const char *list_name, const char *stem_name, int index,
373 struct of_phandle_args *out_args);
374 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
375 const char *list_name, int cells_count, int index,
376 struct of_phandle_args *out_args);
377 extern int of_count_phandle_with_args(const struct device_node *np,
378 const char *list_name, const char *cells_name);
380 /* phandle iterator functions */
381 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
382 const struct device_node *np,
383 const char *list_name,
384 const char *cells_name,
385 int cell_count);
387 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
388 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
389 uint32_t *args,
390 int size);
392 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
393 extern int of_alias_get_id(struct device_node *np, const char *stem);
394 extern int of_alias_get_highest_id(const char *stem);
396 extern int of_machine_is_compatible(const char *compat);
398 extern int of_add_property(struct device_node *np, struct property *prop);
399 extern int of_remove_property(struct device_node *np, struct property *prop);
400 extern int of_update_property(struct device_node *np, struct property *newprop);
402 /* For updating the device tree at runtime */
403 #define OF_RECONFIG_ATTACH_NODE 0x0001
404 #define OF_RECONFIG_DETACH_NODE 0x0002
405 #define OF_RECONFIG_ADD_PROPERTY 0x0003
406 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
407 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
409 extern int of_attach_node(struct device_node *);
410 extern int of_detach_node(struct device_node *);
412 #define of_match_ptr(_ptr) (_ptr)
415 * of_property_read_u8_array - Find and read an array of u8 from a property.
417 * @np: device node from which the property value is to be read.
418 * @propname: name of the property to be searched.
419 * @out_values: pointer to return value, modified only if return value is 0.
420 * @sz: number of array elements to read
422 * Search for a property in a device node and read 8-bit value(s) from
423 * it. Returns 0 on success, -EINVAL if the property does not exist,
424 * -ENODATA if property does not have a value, and -EOVERFLOW if the
425 * property data isn't large enough.
427 * dts entry of array should be like:
428 * property = /bits/ 8 <0x50 0x60 0x70>;
430 * The out_values is modified only if a valid u8 value can be decoded.
432 static inline int of_property_read_u8_array(const struct device_node *np,
433 const char *propname,
434 u8 *out_values, size_t sz)
436 int ret = of_property_read_variable_u8_array(np, propname, out_values,
437 sz, 0);
438 if (ret >= 0)
439 return 0;
440 else
441 return ret;
445 * of_property_read_u16_array - Find and read an array of u16 from a property.
447 * @np: device node from which the property value is to be read.
448 * @propname: name of the property to be searched.
449 * @out_values: pointer to return value, modified only if return value is 0.
450 * @sz: number of array elements to read
452 * Search for a property in a device node and read 16-bit value(s) from
453 * it. Returns 0 on success, -EINVAL if the property does not exist,
454 * -ENODATA if property does not have a value, and -EOVERFLOW if the
455 * property data isn't large enough.
457 * dts entry of array should be like:
458 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
460 * The out_values is modified only if a valid u16 value can be decoded.
462 static inline int of_property_read_u16_array(const struct device_node *np,
463 const char *propname,
464 u16 *out_values, size_t sz)
466 int ret = of_property_read_variable_u16_array(np, propname, out_values,
467 sz, 0);
468 if (ret >= 0)
469 return 0;
470 else
471 return ret;
475 * of_property_read_u32_array - Find and read an array of 32 bit integers
476 * from a property.
478 * @np: device node from which the property value is to be read.
479 * @propname: name of the property to be searched.
480 * @out_values: pointer to return value, modified only if return value is 0.
481 * @sz: number of array elements to read
483 * Search for a property in a device node and read 32-bit value(s) from
484 * it. Returns 0 on success, -EINVAL if the property does not exist,
485 * -ENODATA if property does not have a value, and -EOVERFLOW if the
486 * property data isn't large enough.
488 * The out_values is modified only if a valid u32 value can be decoded.
490 static inline int of_property_read_u32_array(const struct device_node *np,
491 const char *propname,
492 u32 *out_values, size_t sz)
494 int ret = of_property_read_variable_u32_array(np, propname, out_values,
495 sz, 0);
496 if (ret >= 0)
497 return 0;
498 else
499 return ret;
503 * of_property_read_u64_array - Find and read an array of 64 bit integers
504 * from a property.
506 * @np: device node from which the property value is to be read.
507 * @propname: name of the property to be searched.
508 * @out_values: pointer to return value, modified only if return value is 0.
509 * @sz: number of array elements to read
511 * Search for a property in a device node and read 64-bit value(s) from
512 * it. Returns 0 on success, -EINVAL if the property does not exist,
513 * -ENODATA if property does not have a value, and -EOVERFLOW if the
514 * property data isn't large enough.
516 * The out_values is modified only if a valid u64 value can be decoded.
518 static inline int of_property_read_u64_array(const struct device_node *np,
519 const char *propname,
520 u64 *out_values, size_t sz)
522 int ret = of_property_read_variable_u64_array(np, propname, out_values,
523 sz, 0);
524 if (ret >= 0)
525 return 0;
526 else
527 return ret;
531 * struct property *prop;
532 * const __be32 *p;
533 * u32 u;
535 * of_property_for_each_u32(np, "propname", prop, p, u)
536 * printk("U32 value: %x\n", u);
538 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
539 u32 *pu);
541 * struct property *prop;
542 * const char *s;
544 * of_property_for_each_string(np, "propname", prop, s)
545 * printk("String value: %s\n", s);
547 const char *of_prop_next_string(struct property *prop, const char *cur);
549 bool of_console_check(struct device_node *dn, char *name, int index);
551 extern int of_cpu_node_to_id(struct device_node *np);
553 #else /* CONFIG_OF */
555 static inline void of_core_init(void)
559 static inline bool is_of_node(const struct fwnode_handle *fwnode)
561 return false;
564 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
566 return NULL;
569 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
571 return false;
574 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
576 return false;
579 static inline const char* of_node_full_name(const struct device_node *np)
581 return "<no-node>";
584 static inline struct device_node *of_find_node_by_name(struct device_node *from,
585 const char *name)
587 return NULL;
590 static inline struct device_node *of_find_node_by_type(struct device_node *from,
591 const char *type)
593 return NULL;
596 static inline struct device_node *of_find_matching_node_and_match(
597 struct device_node *from,
598 const struct of_device_id *matches,
599 const struct of_device_id **match)
601 return NULL;
604 static inline struct device_node *of_find_node_by_path(const char *path)
606 return NULL;
609 static inline struct device_node *of_find_node_opts_by_path(const char *path,
610 const char **opts)
612 return NULL;
615 static inline struct device_node *of_find_node_by_phandle(phandle handle)
617 return NULL;
620 static inline struct device_node *of_get_parent(const struct device_node *node)
622 return NULL;
625 static inline struct device_node *of_get_next_child(
626 const struct device_node *node, struct device_node *prev)
628 return NULL;
631 static inline struct device_node *of_get_next_available_child(
632 const struct device_node *node, struct device_node *prev)
634 return NULL;
637 static inline struct device_node *of_find_node_with_property(
638 struct device_node *from, const char *prop_name)
640 return NULL;
643 #define of_fwnode_handle(node) NULL
645 static inline bool of_have_populated_dt(void)
647 return false;
650 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
651 const char *compatible)
653 return NULL;
656 static inline struct device_node *of_get_child_by_name(
657 const struct device_node *node,
658 const char *name)
660 return NULL;
663 static inline int of_device_is_compatible(const struct device_node *device,
664 const char *name)
666 return 0;
669 static inline int of_device_compatible_match(struct device_node *device,
670 const char *const *compat)
672 return 0;
675 static inline bool of_device_is_available(const struct device_node *device)
677 return false;
680 static inline bool of_device_is_big_endian(const struct device_node *device)
682 return false;
685 static inline struct property *of_find_property(const struct device_node *np,
686 const char *name,
687 int *lenp)
689 return NULL;
692 static inline struct device_node *of_find_compatible_node(
693 struct device_node *from,
694 const char *type,
695 const char *compat)
697 return NULL;
700 static inline int of_property_count_elems_of_size(const struct device_node *np,
701 const char *propname, int elem_size)
703 return -ENOSYS;
706 static inline int of_property_read_u8_array(const struct device_node *np,
707 const char *propname, u8 *out_values, size_t sz)
709 return -ENOSYS;
712 static inline int of_property_read_u16_array(const struct device_node *np,
713 const char *propname, u16 *out_values, size_t sz)
715 return -ENOSYS;
718 static inline int of_property_read_u32_array(const struct device_node *np,
719 const char *propname,
720 u32 *out_values, size_t sz)
722 return -ENOSYS;
725 static inline int of_property_read_u64_array(const struct device_node *np,
726 const char *propname,
727 u64 *out_values, size_t sz)
729 return -ENOSYS;
732 static inline int of_property_read_u32_index(const struct device_node *np,
733 const char *propname, u32 index, u32 *out_value)
735 return -ENOSYS;
738 static inline int of_property_read_u64_index(const struct device_node *np,
739 const char *propname, u32 index, u64 *out_value)
741 return -ENOSYS;
744 static inline const void *of_get_property(const struct device_node *node,
745 const char *name,
746 int *lenp)
748 return NULL;
751 static inline struct device_node *of_get_cpu_node(int cpu,
752 unsigned int *thread)
754 return NULL;
757 static inline int of_n_addr_cells(struct device_node *np)
759 return 0;
762 static inline int of_n_size_cells(struct device_node *np)
764 return 0;
767 static inline int of_property_read_variable_u8_array(const struct device_node *np,
768 const char *propname, u8 *out_values,
769 size_t sz_min, size_t sz_max)
771 return -ENOSYS;
774 static inline int of_property_read_variable_u16_array(const struct device_node *np,
775 const char *propname, u16 *out_values,
776 size_t sz_min, size_t sz_max)
778 return -ENOSYS;
781 static inline int of_property_read_variable_u32_array(const struct device_node *np,
782 const char *propname,
783 u32 *out_values,
784 size_t sz_min,
785 size_t sz_max)
787 return -ENOSYS;
790 static inline int of_property_read_u64(const struct device_node *np,
791 const char *propname, u64 *out_value)
793 return -ENOSYS;
796 static inline int of_property_read_variable_u64_array(const struct device_node *np,
797 const char *propname,
798 u64 *out_values,
799 size_t sz_min,
800 size_t sz_max)
802 return -ENOSYS;
805 static inline int of_property_read_string(const struct device_node *np,
806 const char *propname,
807 const char **out_string)
809 return -ENOSYS;
812 static inline int of_property_match_string(const struct device_node *np,
813 const char *propname,
814 const char *string)
816 return -ENOSYS;
819 static inline int of_property_read_string_helper(const struct device_node *np,
820 const char *propname,
821 const char **out_strs, size_t sz, int index)
823 return -ENOSYS;
826 static inline struct device_node *of_parse_phandle(const struct device_node *np,
827 const char *phandle_name,
828 int index)
830 return NULL;
833 static inline int of_parse_phandle_with_args(const struct device_node *np,
834 const char *list_name,
835 const char *cells_name,
836 int index,
837 struct of_phandle_args *out_args)
839 return -ENOSYS;
842 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
843 const char *list_name,
844 const char *stem_name,
845 int index,
846 struct of_phandle_args *out_args)
848 return -ENOSYS;
851 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
852 const char *list_name, int cells_count, int index,
853 struct of_phandle_args *out_args)
855 return -ENOSYS;
858 static inline int of_count_phandle_with_args(struct device_node *np,
859 const char *list_name,
860 const char *cells_name)
862 return -ENOSYS;
865 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
866 const struct device_node *np,
867 const char *list_name,
868 const char *cells_name,
869 int cell_count)
871 return -ENOSYS;
874 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
876 return -ENOSYS;
879 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
880 uint32_t *args,
881 int size)
883 return 0;
886 static inline int of_alias_get_id(struct device_node *np, const char *stem)
888 return -ENOSYS;
891 static inline int of_alias_get_highest_id(const char *stem)
893 return -ENOSYS;
896 static inline int of_machine_is_compatible(const char *compat)
898 return 0;
901 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
903 return false;
906 static inline const __be32 *of_prop_next_u32(struct property *prop,
907 const __be32 *cur, u32 *pu)
909 return NULL;
912 static inline const char *of_prop_next_string(struct property *prop,
913 const char *cur)
915 return NULL;
918 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
920 return 0;
923 static inline int of_node_test_and_set_flag(struct device_node *n,
924 unsigned long flag)
926 return 0;
929 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
933 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
937 static inline int of_property_check_flag(struct property *p, unsigned long flag)
939 return 0;
942 static inline void of_property_set_flag(struct property *p, unsigned long flag)
946 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
950 static inline int of_cpu_node_to_id(struct device_node *np)
952 return -ENODEV;
955 #define of_match_ptr(_ptr) NULL
956 #define of_match_node(_matches, _node) NULL
957 #endif /* CONFIG_OF */
959 /* Default string compare functions, Allow arch asm/prom.h to override */
960 #if !defined(of_compat_cmp)
961 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
962 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
963 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
964 #endif
966 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
967 extern int of_node_to_nid(struct device_node *np);
968 #else
969 static inline int of_node_to_nid(struct device_node *device)
971 return NUMA_NO_NODE;
973 #endif
975 #ifdef CONFIG_OF_NUMA
976 extern int of_numa_init(void);
977 #else
978 static inline int of_numa_init(void)
980 return -ENOSYS;
982 #endif
984 static inline struct device_node *of_find_matching_node(
985 struct device_node *from,
986 const struct of_device_id *matches)
988 return of_find_matching_node_and_match(from, matches, NULL);
991 static inline const char *of_node_get_device_type(const struct device_node *np)
993 return of_get_property(np, "type", NULL);
996 static inline bool of_node_is_type(const struct device_node *np, const char *type)
998 const char *match = of_node_get_device_type(np);
1000 return np && match && type && !strcmp(match, type);
1004 * of_property_count_u8_elems - Count the number of u8 elements in a property
1006 * @np: device node from which the property value is to be read.
1007 * @propname: name of the property to be searched.
1009 * Search for a property in a device node and count the number of u8 elements
1010 * in it. Returns number of elements on sucess, -EINVAL if the property does
1011 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1012 * property does not have a value.
1014 static inline int of_property_count_u8_elems(const struct device_node *np,
1015 const char *propname)
1017 return of_property_count_elems_of_size(np, propname, sizeof(u8));
1021 * of_property_count_u16_elems - Count the number of u16 elements in a property
1023 * @np: device node from which the property value is to be read.
1024 * @propname: name of the property to be searched.
1026 * Search for a property in a device node and count the number of u16 elements
1027 * in it. Returns number of elements on sucess, -EINVAL if the property does
1028 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1029 * property does not have a value.
1031 static inline int of_property_count_u16_elems(const struct device_node *np,
1032 const char *propname)
1034 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1038 * of_property_count_u32_elems - Count the number of u32 elements in a property
1040 * @np: device node from which the property value is to be read.
1041 * @propname: name of the property to be searched.
1043 * Search for a property in a device node and count the number of u32 elements
1044 * in it. Returns number of elements on sucess, -EINVAL if the property does
1045 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1046 * property does not have a value.
1048 static inline int of_property_count_u32_elems(const struct device_node *np,
1049 const char *propname)
1051 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1055 * of_property_count_u64_elems - Count the number of u64 elements in a property
1057 * @np: device node from which the property value is to be read.
1058 * @propname: name of the property to be searched.
1060 * Search for a property in a device node and count the number of u64 elements
1061 * in it. Returns number of elements on sucess, -EINVAL if the property does
1062 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1063 * property does not have a value.
1065 static inline int of_property_count_u64_elems(const struct device_node *np,
1066 const char *propname)
1068 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1072 * of_property_read_string_array() - Read an array of strings from a multiple
1073 * strings property.
1074 * @np: device node from which the property value is to be read.
1075 * @propname: name of the property to be searched.
1076 * @out_strs: output array of string pointers.
1077 * @sz: number of array elements to read.
1079 * Search for a property in a device tree node and retrieve a list of
1080 * terminated string values (pointer to data, not a copy) in that property.
1082 * If @out_strs is NULL, the number of strings in the property is returned.
1084 static inline int of_property_read_string_array(const struct device_node *np,
1085 const char *propname, const char **out_strs,
1086 size_t sz)
1088 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1092 * of_property_count_strings() - Find and return the number of strings from a
1093 * multiple strings property.
1094 * @np: device node from which the property value is to be read.
1095 * @propname: name of the property to be searched.
1097 * Search for a property in a device tree node and retrieve the number of null
1098 * terminated string contain in it. Returns the number of strings on
1099 * success, -EINVAL if the property does not exist, -ENODATA if property
1100 * does not have a value, and -EILSEQ if the string is not null-terminated
1101 * within the length of the property data.
1103 static inline int of_property_count_strings(const struct device_node *np,
1104 const char *propname)
1106 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1110 * of_property_read_string_index() - Find and read a string from a multiple
1111 * strings property.
1112 * @np: device node from which the property value is to be read.
1113 * @propname: name of the property to be searched.
1114 * @index: index of the string in the list of strings
1115 * @out_string: pointer to null terminated return string, modified only if
1116 * return value is 0.
1118 * Search for a property in a device tree node and retrieve a null
1119 * terminated string value (pointer to data, not a copy) in the list of strings
1120 * contained in that property.
1121 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1122 * property does not have a value, and -EILSEQ if the string is not
1123 * null-terminated within the length of the property data.
1125 * The out_string pointer is modified only if a valid string can be decoded.
1127 static inline int of_property_read_string_index(const struct device_node *np,
1128 const char *propname,
1129 int index, const char **output)
1131 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1132 return rc < 0 ? rc : 0;
1136 * of_property_read_bool - Findfrom a property
1137 * @np: device node from which the property value is to be read.
1138 * @propname: name of the property to be searched.
1140 * Search for a property in a device node.
1141 * Returns true if the property exists false otherwise.
1143 static inline bool of_property_read_bool(const struct device_node *np,
1144 const char *propname)
1146 struct property *prop = of_find_property(np, propname, NULL);
1148 return prop ? true : false;
1151 static inline int of_property_read_u8(const struct device_node *np,
1152 const char *propname,
1153 u8 *out_value)
1155 return of_property_read_u8_array(np, propname, out_value, 1);
1158 static inline int of_property_read_u16(const struct device_node *np,
1159 const char *propname,
1160 u16 *out_value)
1162 return of_property_read_u16_array(np, propname, out_value, 1);
1165 static inline int of_property_read_u32(const struct device_node *np,
1166 const char *propname,
1167 u32 *out_value)
1169 return of_property_read_u32_array(np, propname, out_value, 1);
1172 static inline int of_property_read_s32(const struct device_node *np,
1173 const char *propname,
1174 s32 *out_value)
1176 return of_property_read_u32(np, propname, (u32*) out_value);
1179 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1180 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1181 err = of_phandle_iterator_next(it); \
1182 err == 0; \
1183 err = of_phandle_iterator_next(it))
1185 #define of_property_for_each_u32(np, propname, prop, p, u) \
1186 for (prop = of_find_property(np, propname, NULL), \
1187 p = of_prop_next_u32(prop, NULL, &u); \
1188 p; \
1189 p = of_prop_next_u32(prop, p, &u))
1191 #define of_property_for_each_string(np, propname, prop, s) \
1192 for (prop = of_find_property(np, propname, NULL), \
1193 s = of_prop_next_string(prop, NULL); \
1194 s; \
1195 s = of_prop_next_string(prop, s))
1197 #define for_each_node_by_name(dn, name) \
1198 for (dn = of_find_node_by_name(NULL, name); dn; \
1199 dn = of_find_node_by_name(dn, name))
1200 #define for_each_node_by_type(dn, type) \
1201 for (dn = of_find_node_by_type(NULL, type); dn; \
1202 dn = of_find_node_by_type(dn, type))
1203 #define for_each_compatible_node(dn, type, compatible) \
1204 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1205 dn = of_find_compatible_node(dn, type, compatible))
1206 #define for_each_matching_node(dn, matches) \
1207 for (dn = of_find_matching_node(NULL, matches); dn; \
1208 dn = of_find_matching_node(dn, matches))
1209 #define for_each_matching_node_and_match(dn, matches, match) \
1210 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1211 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1213 #define for_each_child_of_node(parent, child) \
1214 for (child = of_get_next_child(parent, NULL); child != NULL; \
1215 child = of_get_next_child(parent, child))
1216 #define for_each_available_child_of_node(parent, child) \
1217 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1218 child = of_get_next_available_child(parent, child))
1220 #define for_each_node_with_property(dn, prop_name) \
1221 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1222 dn = of_find_node_with_property(dn, prop_name))
1224 static inline int of_get_child_count(const struct device_node *np)
1226 struct device_node *child;
1227 int num = 0;
1229 for_each_child_of_node(np, child)
1230 num++;
1232 return num;
1235 static inline int of_get_available_child_count(const struct device_node *np)
1237 struct device_node *child;
1238 int num = 0;
1240 for_each_available_child_of_node(np, child)
1241 num++;
1243 return num;
1246 #if defined(CONFIG_OF) && !defined(MODULE)
1247 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1248 static const struct of_device_id __of_table_##name \
1249 __used __section(__##table##_of_table) \
1250 = { .compatible = compat, \
1251 .data = (fn == (fn_type)NULL) ? fn : fn }
1252 #else
1253 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1254 static const struct of_device_id __of_table_##name \
1255 __attribute__((unused)) \
1256 = { .compatible = compat, \
1257 .data = (fn == (fn_type)NULL) ? fn : fn }
1258 #endif
1260 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1261 typedef int (*of_init_fn_1_ret)(struct device_node *);
1262 typedef void (*of_init_fn_1)(struct device_node *);
1264 #define OF_DECLARE_1(table, name, compat, fn) \
1265 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1266 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1267 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1268 #define OF_DECLARE_2(table, name, compat, fn) \
1269 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1272 * struct of_changeset_entry - Holds a changeset entry
1274 * @node: list_head for the log list
1275 * @action: notifier action
1276 * @np: pointer to the device node affected
1277 * @prop: pointer to the property affected
1278 * @old_prop: hold a pointer to the original property
1280 * Every modification of the device tree during a changeset
1281 * is held in a list of of_changeset_entry structures.
1282 * That way we can recover from a partial application, or we can
1283 * revert the changeset
1285 struct of_changeset_entry {
1286 struct list_head node;
1287 unsigned long action;
1288 struct device_node *np;
1289 struct property *prop;
1290 struct property *old_prop;
1294 * struct of_changeset - changeset tracker structure
1296 * @entries: list_head for the changeset entries
1298 * changesets are a convenient way to apply bulk changes to the
1299 * live tree. In case of an error, changes are rolled-back.
1300 * changesets live on after initial application, and if not
1301 * destroyed after use, they can be reverted in one single call.
1303 struct of_changeset {
1304 struct list_head entries;
1307 enum of_reconfig_change {
1308 OF_RECONFIG_NO_CHANGE = 0,
1309 OF_RECONFIG_CHANGE_ADD,
1310 OF_RECONFIG_CHANGE_REMOVE,
1313 #ifdef CONFIG_OF_DYNAMIC
1314 extern int of_reconfig_notifier_register(struct notifier_block *);
1315 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1316 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1317 extern int of_reconfig_get_state_change(unsigned long action,
1318 struct of_reconfig_data *arg);
1320 extern void of_changeset_init(struct of_changeset *ocs);
1321 extern void of_changeset_destroy(struct of_changeset *ocs);
1322 extern int of_changeset_apply(struct of_changeset *ocs);
1323 extern int of_changeset_revert(struct of_changeset *ocs);
1324 extern int of_changeset_action(struct of_changeset *ocs,
1325 unsigned long action, struct device_node *np,
1326 struct property *prop);
1328 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1329 struct device_node *np)
1331 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1334 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1335 struct device_node *np)
1337 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1340 static inline int of_changeset_add_property(struct of_changeset *ocs,
1341 struct device_node *np, struct property *prop)
1343 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1346 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1347 struct device_node *np, struct property *prop)
1349 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1352 static inline int of_changeset_update_property(struct of_changeset *ocs,
1353 struct device_node *np, struct property *prop)
1355 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1357 #else /* CONFIG_OF_DYNAMIC */
1358 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1360 return -EINVAL;
1362 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1364 return -EINVAL;
1366 static inline int of_reconfig_notify(unsigned long action,
1367 struct of_reconfig_data *arg)
1369 return -EINVAL;
1371 static inline int of_reconfig_get_state_change(unsigned long action,
1372 struct of_reconfig_data *arg)
1374 return -EINVAL;
1376 #endif /* CONFIG_OF_DYNAMIC */
1379 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1380 * @np: Pointer to the given device_node
1382 * return true if present false otherwise
1384 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1386 return of_property_read_bool(np, "system-power-controller");
1390 * Overlay support
1393 enum of_overlay_notify_action {
1394 OF_OVERLAY_PRE_APPLY = 0,
1395 OF_OVERLAY_POST_APPLY,
1396 OF_OVERLAY_PRE_REMOVE,
1397 OF_OVERLAY_POST_REMOVE,
1400 struct of_overlay_notify_data {
1401 struct device_node *overlay;
1402 struct device_node *target;
1405 #ifdef CONFIG_OF_OVERLAY
1407 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1408 int *ovcs_id);
1409 int of_overlay_remove(int *ovcs_id);
1410 int of_overlay_remove_all(void);
1412 int of_overlay_notifier_register(struct notifier_block *nb);
1413 int of_overlay_notifier_unregister(struct notifier_block *nb);
1415 #else
1417 static inline int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)
1419 return -ENOTSUPP;
1422 static inline int of_overlay_remove(int *ovcs_id)
1424 return -ENOTSUPP;
1427 static inline int of_overlay_remove_all(void)
1429 return -ENOTSUPP;
1432 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1434 return 0;
1437 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1439 return 0;
1442 #endif
1444 #endif /* _LINUX_OF_H */