xref: /linux-6.15/include/linux/of.h (revision 266570f4)
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.
7  *
8  * Copyright (C) 1996-2005 Paul Mackerras.
9  *
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.
13  */
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>
24 
25 #include <asm/byteorder.h>
26 #include <asm/errno.h>
27 
28 typedef u32 phandle;
29 typedef u32 ihandle;
30 
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
45 };
46 
47 #if defined(CONFIG_SPARC)
48 struct of_irq_controller;
49 #endif
50 
51 struct device_node {
52 	const char *name;
53 	phandle phandle;
54 	const char *full_name;
55 	struct fwnode_handle fwnode;
56 
57 	struct	property *properties;
58 	struct	property *deadprops;	/* removed properties */
59 	struct	device_node *parent;
60 	struct	device_node *child;
61 	struct	device_node *sibling;
62 #if defined(CONFIG_OF_KOBJ)
63 	struct	kobject kobj;
64 #endif
65 	unsigned long _flags;
66 	void	*data;
67 #if defined(CONFIG_SPARC)
68 	unsigned int unique_id;
69 	struct of_irq_controller *irq_trans;
70 #endif
71 };
72 
73 #define MAX_PHANDLE_ARGS 16
74 struct of_phandle_args {
75 	struct device_node *np;
76 	int args_count;
77 	uint32_t args[MAX_PHANDLE_ARGS];
78 };
79 
80 struct of_phandle_iterator {
81 	/* Common iterator information */
82 	const char *cells_name;
83 	int cell_count;
84 	const struct device_node *parent;
85 
86 	/* List size information */
87 	const __be32 *list_end;
88 	const __be32 *phandle_end;
89 
90 	/* Current position state */
91 	const __be32 *cur;
92 	uint32_t cur_count;
93 	phandle phandle;
94 	struct device_node *node;
95 };
96 
97 struct of_reconfig_data {
98 	struct device_node	*dn;
99 	struct property		*prop;
100 	struct property		*old_prop;
101 };
102 
103 /**
104  * of_node_init - initialize a devicetree node
105  * @node: Pointer to device node that has been created by kzalloc()
106  * @phandle_name: Name of property holding a phandle value
107  *
108  * On return the device_node refcount is set to one.  Use of_node_put()
109  * on @node when done to free the memory allocated for it.  If the node
110  * is NOT a dynamic node the memory will not be freed. The decision of
111  * whether to free the memory will be done by node->release(), which is
112  * of_node_release().
113  */
114 /* initialize a node */
115 extern const struct kobj_type of_node_ktype;
116 extern const struct fwnode_operations of_fwnode_ops;
117 static inline void of_node_init(struct device_node *node)
118 {
119 #if defined(CONFIG_OF_KOBJ)
120 	kobject_init(&node->kobj, &of_node_ktype);
121 #endif
122 	fwnode_init(&node->fwnode, &of_fwnode_ops);
123 }
124 
125 #if defined(CONFIG_OF_KOBJ)
126 #define of_node_kobj(n) (&(n)->kobj)
127 #else
128 #define of_node_kobj(n) NULL
129 #endif
130 
131 #ifdef CONFIG_OF_DYNAMIC
132 extern struct device_node *of_node_get(struct device_node *node);
133 extern void of_node_put(struct device_node *node);
134 #else /* CONFIG_OF_DYNAMIC */
135 /* Dummy ref counting routines - to be implemented later */
136 static inline struct device_node *of_node_get(struct device_node *node)
137 {
138 	return node;
139 }
140 static inline void of_node_put(struct device_node *node) { }
141 #endif /* !CONFIG_OF_DYNAMIC */
142 
143 /* Pointer for first entry in chain of all nodes. */
144 extern struct device_node *of_root;
145 extern struct device_node *of_chosen;
146 extern struct device_node *of_aliases;
147 extern struct device_node *of_stdout;
148 extern raw_spinlock_t devtree_lock;
149 
150 /*
151  * struct device_node flag descriptions
152  * (need to be visible even when !CONFIG_OF)
153  */
154 #define OF_DYNAMIC		1 /* (and properties) allocated via kmalloc */
155 #define OF_DETACHED		2 /* detached from the device tree */
156 #define OF_POPULATED		3 /* device already created */
157 #define OF_POPULATED_BUS	4 /* platform bus created for children */
158 #define OF_OVERLAY		5 /* allocated for an overlay */
159 #define OF_OVERLAY_FREE_CSET	6 /* in overlay cset being freed */
160 
161 #define OF_BAD_ADDR	((u64)-1)
162 
163 #ifdef CONFIG_OF
164 void of_core_init(void);
165 
166 static inline bool is_of_node(const struct fwnode_handle *fwnode)
167 {
168 	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
169 }
170 
171 #define to_of_node(__fwnode)						\
172 	({								\
173 		typeof(__fwnode) __to_of_node_fwnode = (__fwnode);	\
174 									\
175 		is_of_node(__to_of_node_fwnode) ?			\
176 			container_of(__to_of_node_fwnode,		\
177 				     struct device_node, fwnode) :	\
178 			NULL;						\
179 	})
180 
181 #define of_fwnode_handle(node)						\
182 	({								\
183 		typeof(node) __of_fwnode_handle_node = (node);		\
184 									\
185 		__of_fwnode_handle_node ?				\
186 			&__of_fwnode_handle_node->fwnode : NULL;	\
187 	})
188 
189 static inline bool of_have_populated_dt(void)
190 {
191 	return of_root != NULL;
192 }
193 
194 static inline bool of_node_is_root(const struct device_node *node)
195 {
196 	return node && (node->parent == NULL);
197 }
198 
199 static inline int of_node_check_flag(const struct device_node *n, unsigned long flag)
200 {
201 	return test_bit(flag, &n->_flags);
202 }
203 
204 static inline int of_node_test_and_set_flag(struct device_node *n,
205 					    unsigned long flag)
206 {
207 	return test_and_set_bit(flag, &n->_flags);
208 }
209 
210 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
211 {
212 	set_bit(flag, &n->_flags);
213 }
214 
215 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
216 {
217 	clear_bit(flag, &n->_flags);
218 }
219 
220 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
221 static inline int of_property_check_flag(const struct property *p, unsigned long flag)
222 {
223 	return test_bit(flag, &p->_flags);
224 }
225 
226 static inline void of_property_set_flag(struct property *p, unsigned long flag)
227 {
228 	set_bit(flag, &p->_flags);
229 }
230 
231 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
232 {
233 	clear_bit(flag, &p->_flags);
234 }
235 #endif
236 
237 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
238 extern struct device_node *of_find_all_nodes(struct device_node *prev);
239 
240 /*
241  * OF address retrieval & translation
242  */
243 
244 /* Helper to read a big number; size is in cells (not bytes) */
245 static inline u64 of_read_number(const __be32 *cell, int size)
246 {
247 	u64 r = 0;
248 	for (; size--; cell++)
249 		r = (r << 32) | be32_to_cpu(*cell);
250 	return r;
251 }
252 
253 /* Like of_read_number, but we want an unsigned long result */
254 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
255 {
256 	/* toss away upper bits if unsigned long is smaller than u64 */
257 	return of_read_number(cell, size);
258 }
259 
260 #if defined(CONFIG_SPARC)
261 #include <asm/prom.h>
262 #endif
263 
264 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
265 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
266 
267 extern bool of_node_name_eq(const struct device_node *np, const char *name);
268 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
269 
270 static inline const char *of_node_full_name(const struct device_node *np)
271 {
272 	return np ? np->full_name : "<no-node>";
273 }
274 
275 #define for_each_of_allnodes_from(from, dn) \
276 	for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
277 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
278 extern struct device_node *of_find_node_by_name(struct device_node *from,
279 	const char *name);
280 extern struct device_node *of_find_node_by_type(struct device_node *from,
281 	const char *type);
282 extern struct device_node *of_find_compatible_node(struct device_node *from,
283 	const char *type, const char *compat);
284 extern struct device_node *of_find_matching_node_and_match(
285 	struct device_node *from,
286 	const struct of_device_id *matches,
287 	const struct of_device_id **match);
288 
289 extern struct device_node *of_find_node_opts_by_path(const char *path,
290 	const char **opts);
291 static inline struct device_node *of_find_node_by_path(const char *path)
292 {
293 	return of_find_node_opts_by_path(path, NULL);
294 }
295 
296 extern struct device_node *of_find_node_by_phandle(phandle handle);
297 extern struct device_node *of_get_parent(const struct device_node *node);
298 extern struct device_node *of_get_next_parent(struct device_node *node);
299 extern struct device_node *of_get_next_child(const struct device_node *node,
300 					     struct device_node *prev);
301 extern struct device_node *of_get_next_available_child(
302 	const struct device_node *node, struct device_node *prev);
303 
304 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
305 					const char *compatible);
306 extern struct device_node *of_get_child_by_name(const struct device_node *node,
307 					const char *name);
308 
309 /* cache lookup */
310 extern struct device_node *of_find_next_cache_node(const struct device_node *);
311 extern int of_find_last_cache_level(unsigned int cpu);
312 extern struct device_node *of_find_node_with_property(
313 	struct device_node *from, const char *prop_name);
314 
315 extern struct property *of_find_property(const struct device_node *np,
316 					 const char *name,
317 					 int *lenp);
318 extern int of_property_count_elems_of_size(const struct device_node *np,
319 				const char *propname, int elem_size);
320 extern int of_property_read_u32_index(const struct device_node *np,
321 				       const char *propname,
322 				       u32 index, u32 *out_value);
323 extern int of_property_read_u64_index(const struct device_node *np,
324 				       const char *propname,
325 				       u32 index, u64 *out_value);
326 extern int of_property_read_variable_u8_array(const struct device_node *np,
327 					const char *propname, u8 *out_values,
328 					size_t sz_min, size_t sz_max);
329 extern int of_property_read_variable_u16_array(const struct device_node *np,
330 					const char *propname, u16 *out_values,
331 					size_t sz_min, size_t sz_max);
332 extern int of_property_read_variable_u32_array(const struct device_node *np,
333 					const char *propname,
334 					u32 *out_values,
335 					size_t sz_min,
336 					size_t sz_max);
337 extern int of_property_read_u64(const struct device_node *np,
338 				const char *propname, u64 *out_value);
339 extern int of_property_read_variable_u64_array(const struct device_node *np,
340 					const char *propname,
341 					u64 *out_values,
342 					size_t sz_min,
343 					size_t sz_max);
344 
345 extern int of_property_read_string(const struct device_node *np,
346 				   const char *propname,
347 				   const char **out_string);
348 extern int of_property_match_string(const struct device_node *np,
349 				    const char *propname,
350 				    const char *string);
351 extern int of_property_read_string_helper(const struct device_node *np,
352 					      const char *propname,
353 					      const char **out_strs, size_t sz, int index);
354 extern int of_device_is_compatible(const struct device_node *device,
355 				   const char *);
356 extern int of_device_compatible_match(const struct device_node *device,
357 				      const char *const *compat);
358 extern bool of_device_is_available(const struct device_node *device);
359 extern bool of_device_is_big_endian(const struct device_node *device);
360 extern const void *of_get_property(const struct device_node *node,
361 				const char *name,
362 				int *lenp);
363 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
364 extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
365 extern struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
366 						 int index);
367 extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread);
368 
369 #define for_each_property_of_node(dn, pp) \
370 	for (pp = dn->properties; pp != NULL; pp = pp->next)
371 
372 extern int of_n_addr_cells(struct device_node *np);
373 extern int of_n_size_cells(struct device_node *np);
374 extern const struct of_device_id *of_match_node(
375 	const struct of_device_id *matches, const struct device_node *node);
376 extern int of_alias_from_compatible(const struct device_node *node, char *alias,
377 				    int len);
378 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
379 extern int __of_parse_phandle_with_args(const struct device_node *np,
380 	const char *list_name, const char *cells_name, int cell_count,
381 	int index, struct of_phandle_args *out_args);
382 extern int of_parse_phandle_with_args_map(const struct device_node *np,
383 	const char *list_name, const char *stem_name, int index,
384 	struct of_phandle_args *out_args);
385 extern int of_count_phandle_with_args(const struct device_node *np,
386 	const char *list_name, const char *cells_name);
387 
388 /* module functions */
389 extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len);
390 extern int of_request_module(const struct device_node *np);
391 
392 /* phandle iterator functions */
393 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
394 				    const struct device_node *np,
395 				    const char *list_name,
396 				    const char *cells_name,
397 				    int cell_count);
398 
399 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
400 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
401 				    uint32_t *args,
402 				    int size);
403 
404 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
405 extern int of_alias_get_id(struct device_node *np, const char *stem);
406 extern int of_alias_get_highest_id(const char *stem);
407 
408 extern int of_machine_is_compatible(const char *compat);
409 
410 extern int of_add_property(struct device_node *np, struct property *prop);
411 extern int of_remove_property(struct device_node *np, struct property *prop);
412 extern int of_update_property(struct device_node *np, struct property *newprop);
413 
414 /* For updating the device tree at runtime */
415 #define OF_RECONFIG_ATTACH_NODE		0x0001
416 #define OF_RECONFIG_DETACH_NODE		0x0002
417 #define OF_RECONFIG_ADD_PROPERTY	0x0003
418 #define OF_RECONFIG_REMOVE_PROPERTY	0x0004
419 #define OF_RECONFIG_UPDATE_PROPERTY	0x0005
420 
421 extern int of_attach_node(struct device_node *);
422 extern int of_detach_node(struct device_node *);
423 
424 #define of_match_ptr(_ptr)	(_ptr)
425 
426 /*
427  * struct property *prop;
428  * const __be32 *p;
429  * u32 u;
430  *
431  * of_property_for_each_u32(np, "propname", prop, p, u)
432  *         printk("U32 value: %x\n", u);
433  */
434 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
435 			       u32 *pu);
436 /*
437  * struct property *prop;
438  * const char *s;
439  *
440  * of_property_for_each_string(np, "propname", prop, s)
441  *         printk("String value: %s\n", s);
442  */
443 const char *of_prop_next_string(struct property *prop, const char *cur);
444 
445 bool of_console_check(struct device_node *dn, char *name, int index);
446 
447 extern int of_cpu_node_to_id(struct device_node *np);
448 
449 int of_map_id(struct device_node *np, u32 id,
450 	       const char *map_name, const char *map_mask_name,
451 	       struct device_node **target, u32 *id_out);
452 
453 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
454 
455 struct kimage;
456 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
457 				   unsigned long initrd_load_addr,
458 				   unsigned long initrd_len,
459 				   const char *cmdline, size_t extra_fdt_size);
460 #else /* CONFIG_OF */
461 
462 static inline void of_core_init(void)
463 {
464 }
465 
466 static inline bool is_of_node(const struct fwnode_handle *fwnode)
467 {
468 	return false;
469 }
470 
471 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
472 {
473 	return NULL;
474 }
475 
476 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
477 {
478 	return false;
479 }
480 
481 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
482 {
483 	return false;
484 }
485 
486 static inline const char* of_node_full_name(const struct device_node *np)
487 {
488 	return "<no-node>";
489 }
490 
491 static inline struct device_node *of_find_node_by_name(struct device_node *from,
492 	const char *name)
493 {
494 	return NULL;
495 }
496 
497 static inline struct device_node *of_find_node_by_type(struct device_node *from,
498 	const char *type)
499 {
500 	return NULL;
501 }
502 
503 static inline struct device_node *of_find_matching_node_and_match(
504 	struct device_node *from,
505 	const struct of_device_id *matches,
506 	const struct of_device_id **match)
507 {
508 	return NULL;
509 }
510 
511 static inline struct device_node *of_find_node_by_path(const char *path)
512 {
513 	return NULL;
514 }
515 
516 static inline struct device_node *of_find_node_opts_by_path(const char *path,
517 	const char **opts)
518 {
519 	return NULL;
520 }
521 
522 static inline struct device_node *of_find_node_by_phandle(phandle handle)
523 {
524 	return NULL;
525 }
526 
527 static inline struct device_node *of_get_parent(const struct device_node *node)
528 {
529 	return NULL;
530 }
531 
532 static inline struct device_node *of_get_next_parent(struct device_node *node)
533 {
534 	return NULL;
535 }
536 
537 static inline struct device_node *of_get_next_child(
538 	const struct device_node *node, struct device_node *prev)
539 {
540 	return NULL;
541 }
542 
543 static inline struct device_node *of_get_next_available_child(
544 	const struct device_node *node, struct device_node *prev)
545 {
546 	return NULL;
547 }
548 
549 static inline struct device_node *of_find_node_with_property(
550 	struct device_node *from, const char *prop_name)
551 {
552 	return NULL;
553 }
554 
555 #define of_fwnode_handle(node) NULL
556 
557 static inline bool of_have_populated_dt(void)
558 {
559 	return false;
560 }
561 
562 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
563 					const char *compatible)
564 {
565 	return NULL;
566 }
567 
568 static inline struct device_node *of_get_child_by_name(
569 					const struct device_node *node,
570 					const char *name)
571 {
572 	return NULL;
573 }
574 
575 static inline int of_device_is_compatible(const struct device_node *device,
576 					  const char *name)
577 {
578 	return 0;
579 }
580 
581 static inline  int of_device_compatible_match(const struct device_node *device,
582 					      const char *const *compat)
583 {
584 	return 0;
585 }
586 
587 static inline bool of_device_is_available(const struct device_node *device)
588 {
589 	return false;
590 }
591 
592 static inline bool of_device_is_big_endian(const struct device_node *device)
593 {
594 	return false;
595 }
596 
597 static inline struct property *of_find_property(const struct device_node *np,
598 						const char *name,
599 						int *lenp)
600 {
601 	return NULL;
602 }
603 
604 static inline struct device_node *of_find_compatible_node(
605 						struct device_node *from,
606 						const char *type,
607 						const char *compat)
608 {
609 	return NULL;
610 }
611 
612 static inline int of_property_count_elems_of_size(const struct device_node *np,
613 			const char *propname, int elem_size)
614 {
615 	return -ENOSYS;
616 }
617 
618 static inline int of_property_read_u32_index(const struct device_node *np,
619 			const char *propname, u32 index, u32 *out_value)
620 {
621 	return -ENOSYS;
622 }
623 
624 static inline int of_property_read_u64_index(const struct device_node *np,
625 			const char *propname, u32 index, u64 *out_value)
626 {
627 	return -ENOSYS;
628 }
629 
630 static inline const void *of_get_property(const struct device_node *node,
631 				const char *name,
632 				int *lenp)
633 {
634 	return NULL;
635 }
636 
637 static inline struct device_node *of_get_cpu_node(int cpu,
638 					unsigned int *thread)
639 {
640 	return NULL;
641 }
642 
643 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
644 {
645 	return NULL;
646 }
647 
648 static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
649 					int index)
650 {
651 	return NULL;
652 }
653 
654 static inline int of_n_addr_cells(struct device_node *np)
655 {
656 	return 0;
657 
658 }
659 static inline int of_n_size_cells(struct device_node *np)
660 {
661 	return 0;
662 }
663 
664 static inline int of_property_read_variable_u8_array(const struct device_node *np,
665 					const char *propname, u8 *out_values,
666 					size_t sz_min, size_t sz_max)
667 {
668 	return -ENOSYS;
669 }
670 
671 static inline int of_property_read_variable_u16_array(const struct device_node *np,
672 					const char *propname, u16 *out_values,
673 					size_t sz_min, size_t sz_max)
674 {
675 	return -ENOSYS;
676 }
677 
678 static inline int of_property_read_variable_u32_array(const struct device_node *np,
679 					const char *propname,
680 					u32 *out_values,
681 					size_t sz_min,
682 					size_t sz_max)
683 {
684 	return -ENOSYS;
685 }
686 
687 static inline int of_property_read_u64(const struct device_node *np,
688 				       const char *propname, u64 *out_value)
689 {
690 	return -ENOSYS;
691 }
692 
693 static inline int of_property_read_variable_u64_array(const struct device_node *np,
694 					const char *propname,
695 					u64 *out_values,
696 					size_t sz_min,
697 					size_t sz_max)
698 {
699 	return -ENOSYS;
700 }
701 
702 static inline int of_property_read_string(const struct device_node *np,
703 					  const char *propname,
704 					  const char **out_string)
705 {
706 	return -ENOSYS;
707 }
708 
709 static inline int of_property_match_string(const struct device_node *np,
710 					   const char *propname,
711 					   const char *string)
712 {
713 	return -ENOSYS;
714 }
715 
716 static inline int of_property_read_string_helper(const struct device_node *np,
717 						 const char *propname,
718 						 const char **out_strs, size_t sz, int index)
719 {
720 	return -ENOSYS;
721 }
722 
723 static inline int __of_parse_phandle_with_args(const struct device_node *np,
724 					       const char *list_name,
725 					       const char *cells_name,
726 					       int cell_count,
727 					       int index,
728 					       struct of_phandle_args *out_args)
729 {
730 	return -ENOSYS;
731 }
732 
733 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
734 						 const char *list_name,
735 						 const char *stem_name,
736 						 int index,
737 						 struct of_phandle_args *out_args)
738 {
739 	return -ENOSYS;
740 }
741 
742 static inline int of_count_phandle_with_args(const struct device_node *np,
743 					     const char *list_name,
744 					     const char *cells_name)
745 {
746 	return -ENOSYS;
747 }
748 
749 static inline ssize_t of_modalias(const struct device_node *np, char *str,
750 				  ssize_t len)
751 {
752 	return -ENODEV;
753 }
754 
755 static inline int of_request_module(const struct device_node *np)
756 {
757 	return -ENODEV;
758 }
759 
760 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
761 					   const struct device_node *np,
762 					   const char *list_name,
763 					   const char *cells_name,
764 					   int cell_count)
765 {
766 	return -ENOSYS;
767 }
768 
769 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
770 {
771 	return -ENOSYS;
772 }
773 
774 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
775 					   uint32_t *args,
776 					   int size)
777 {
778 	return 0;
779 }
780 
781 static inline int of_alias_get_id(struct device_node *np, const char *stem)
782 {
783 	return -ENOSYS;
784 }
785 
786 static inline int of_alias_get_highest_id(const char *stem)
787 {
788 	return -ENOSYS;
789 }
790 
791 static inline int of_machine_is_compatible(const char *compat)
792 {
793 	return 0;
794 }
795 
796 static inline int of_add_property(struct device_node *np, struct property *prop)
797 {
798 	return 0;
799 }
800 
801 static inline int of_remove_property(struct device_node *np, struct property *prop)
802 {
803 	return 0;
804 }
805 
806 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
807 {
808 	return false;
809 }
810 
811 static inline const __be32 *of_prop_next_u32(struct property *prop,
812 		const __be32 *cur, u32 *pu)
813 {
814 	return NULL;
815 }
816 
817 static inline const char *of_prop_next_string(struct property *prop,
818 		const char *cur)
819 {
820 	return NULL;
821 }
822 
823 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
824 {
825 	return 0;
826 }
827 
828 static inline int of_node_test_and_set_flag(struct device_node *n,
829 					    unsigned long flag)
830 {
831 	return 0;
832 }
833 
834 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
835 {
836 }
837 
838 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
839 {
840 }
841 
842 static inline int of_property_check_flag(const struct property *p,
843 					 unsigned long flag)
844 {
845 	return 0;
846 }
847 
848 static inline void of_property_set_flag(struct property *p, unsigned long flag)
849 {
850 }
851 
852 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
853 {
854 }
855 
856 static inline int of_cpu_node_to_id(struct device_node *np)
857 {
858 	return -ENODEV;
859 }
860 
861 static inline int of_map_id(struct device_node *np, u32 id,
862 			     const char *map_name, const char *map_mask_name,
863 			     struct device_node **target, u32 *id_out)
864 {
865 	return -EINVAL;
866 }
867 
868 static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
869 {
870 	return PHYS_ADDR_MAX;
871 }
872 
873 #define of_match_ptr(_ptr)	NULL
874 #define of_match_node(_matches, _node)	NULL
875 #endif /* CONFIG_OF */
876 
877 /* Default string compare functions, Allow arch asm/prom.h to override */
878 #if !defined(of_compat_cmp)
879 #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
880 #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
881 #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
882 #endif
883 
884 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
885 {
886 	return p1->length == p2->length &&
887 	       !memcmp(p1->value, p2->value, (size_t)p1->length);
888 }
889 
890 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
891 extern int of_node_to_nid(struct device_node *np);
892 #else
893 static inline int of_node_to_nid(struct device_node *device)
894 {
895 	return NUMA_NO_NODE;
896 }
897 #endif
898 
899 #ifdef CONFIG_OF_NUMA
900 extern int of_numa_init(void);
901 #else
902 static inline int of_numa_init(void)
903 {
904 	return -ENOSYS;
905 }
906 #endif
907 
908 static inline struct device_node *of_find_matching_node(
909 	struct device_node *from,
910 	const struct of_device_id *matches)
911 {
912 	return of_find_matching_node_and_match(from, matches, NULL);
913 }
914 
915 static inline const char *of_node_get_device_type(const struct device_node *np)
916 {
917 	return of_get_property(np, "device_type", NULL);
918 }
919 
920 static inline bool of_node_is_type(const struct device_node *np, const char *type)
921 {
922 	const char *match = of_node_get_device_type(np);
923 
924 	return np && match && type && !strcmp(match, type);
925 }
926 
927 /**
928  * of_parse_phandle - Resolve a phandle property to a device_node pointer
929  * @np: Pointer to device node holding phandle property
930  * @phandle_name: Name of property holding a phandle value
931  * @index: For properties holding a table of phandles, this is the index into
932  *         the table
933  *
934  * Return: The device_node pointer with refcount incremented.  Use
935  * of_node_put() on it when done.
936  */
937 static inline struct device_node *of_parse_phandle(const struct device_node *np,
938 						   const char *phandle_name,
939 						   int index)
940 {
941 	struct of_phandle_args args;
942 
943 	if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
944 					 index, &args))
945 		return NULL;
946 
947 	return args.np;
948 }
949 
950 /**
951  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
952  * @np:		pointer to a device tree node containing a list
953  * @list_name:	property name that contains a list
954  * @cells_name:	property name that specifies phandles' arguments count
955  * @index:	index of a phandle to parse out
956  * @out_args:	optional pointer to output arguments structure (will be filled)
957  *
958  * This function is useful to parse lists of phandles and their arguments.
959  * Returns 0 on success and fills out_args, on error returns appropriate
960  * errno value.
961  *
962  * Caller is responsible to call of_node_put() on the returned out_args->np
963  * pointer.
964  *
965  * Example::
966  *
967  *  phandle1: node1 {
968  *	#list-cells = <2>;
969  *  };
970  *
971  *  phandle2: node2 {
972  *	#list-cells = <1>;
973  *  };
974  *
975  *  node3 {
976  *	list = <&phandle1 1 2 &phandle2 3>;
977  *  };
978  *
979  * To get a device_node of the ``node2`` node you may call this:
980  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
981  */
982 static inline int of_parse_phandle_with_args(const struct device_node *np,
983 					     const char *list_name,
984 					     const char *cells_name,
985 					     int index,
986 					     struct of_phandle_args *out_args)
987 {
988 	int cell_count = -1;
989 
990 	/* If cells_name is NULL we assume a cell count of 0 */
991 	if (!cells_name)
992 		cell_count = 0;
993 
994 	return __of_parse_phandle_with_args(np, list_name, cells_name,
995 					    cell_count, index, out_args);
996 }
997 
998 /**
999  * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1000  * @np:		pointer to a device tree node containing a list
1001  * @list_name:	property name that contains a list
1002  * @cell_count: number of argument cells following the phandle
1003  * @index:	index of a phandle to parse out
1004  * @out_args:	optional pointer to output arguments structure (will be filled)
1005  *
1006  * This function is useful to parse lists of phandles and their arguments.
1007  * Returns 0 on success and fills out_args, on error returns appropriate
1008  * errno value.
1009  *
1010  * Caller is responsible to call of_node_put() on the returned out_args->np
1011  * pointer.
1012  *
1013  * Example::
1014  *
1015  *  phandle1: node1 {
1016  *  };
1017  *
1018  *  phandle2: node2 {
1019  *  };
1020  *
1021  *  node3 {
1022  *	list = <&phandle1 0 2 &phandle2 2 3>;
1023  *  };
1024  *
1025  * To get a device_node of the ``node2`` node you may call this:
1026  * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1027  */
1028 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
1029 						   const char *list_name,
1030 						   int cell_count,
1031 						   int index,
1032 						   struct of_phandle_args *out_args)
1033 {
1034 	return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1035 					    index, out_args);
1036 }
1037 
1038 /**
1039  * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
1040  * @np:		pointer to a device tree node containing a list
1041  * @list_name:	property name that contains a list
1042  * @cells_name:	property name that specifies phandles' arguments count
1043  * @index:	index of a phandle to parse out
1044  * @out_args:	optional pointer to output arguments structure (will be filled)
1045  *
1046  * Same as of_parse_phandle_with_args() except that if the cells_name property
1047  * is not found, cell_count of 0 is assumed.
1048  *
1049  * This is used to useful, if you have a phandle which didn't have arguments
1050  * before and thus doesn't have a '#*-cells' property but is now migrated to
1051  * having arguments while retaining backwards compatibility.
1052  */
1053 static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
1054 						      const char *list_name,
1055 						      const char *cells_name,
1056 						      int index,
1057 						      struct of_phandle_args *out_args)
1058 {
1059 	return __of_parse_phandle_with_args(np, list_name, cells_name,
1060 					    0, index, out_args);
1061 }
1062 
1063 /**
1064  * of_property_count_u8_elems - Count the number of u8 elements in a property
1065  *
1066  * @np:		device node from which the property value is to be read.
1067  * @propname:	name of the property to be searched.
1068  *
1069  * Search for a property in a device node and count the number of u8 elements
1070  * in it.
1071  *
1072  * Return: The number of elements on sucess, -EINVAL if the property does
1073  * not exist or its length does not match a multiple of u8 and -ENODATA if the
1074  * property does not have a value.
1075  */
1076 static inline int of_property_count_u8_elems(const struct device_node *np,
1077 				const char *propname)
1078 {
1079 	return of_property_count_elems_of_size(np, propname, sizeof(u8));
1080 }
1081 
1082 /**
1083  * of_property_count_u16_elems - Count the number of u16 elements in a property
1084  *
1085  * @np:		device node from which the property value is to be read.
1086  * @propname:	name of the property to be searched.
1087  *
1088  * Search for a property in a device node and count the number of u16 elements
1089  * in it.
1090  *
1091  * Return: The number of elements on sucess, -EINVAL if the property does
1092  * not exist or its length does not match a multiple of u16 and -ENODATA if the
1093  * property does not have a value.
1094  */
1095 static inline int of_property_count_u16_elems(const struct device_node *np,
1096 				const char *propname)
1097 {
1098 	return of_property_count_elems_of_size(np, propname, sizeof(u16));
1099 }
1100 
1101 /**
1102  * of_property_count_u32_elems - Count the number of u32 elements in a property
1103  *
1104  * @np:		device node from which the property value is to be read.
1105  * @propname:	name of the property to be searched.
1106  *
1107  * Search for a property in a device node and count the number of u32 elements
1108  * in it.
1109  *
1110  * Return: The number of elements on sucess, -EINVAL if the property does
1111  * not exist or its length does not match a multiple of u32 and -ENODATA if the
1112  * property does not have a value.
1113  */
1114 static inline int of_property_count_u32_elems(const struct device_node *np,
1115 				const char *propname)
1116 {
1117 	return of_property_count_elems_of_size(np, propname, sizeof(u32));
1118 }
1119 
1120 /**
1121  * of_property_count_u64_elems - Count the number of u64 elements in a property
1122  *
1123  * @np:		device node from which the property value is to be read.
1124  * @propname:	name of the property to be searched.
1125  *
1126  * Search for a property in a device node and count the number of u64 elements
1127  * in it.
1128  *
1129  * Return: The number of elements on sucess, -EINVAL if the property does
1130  * not exist or its length does not match a multiple of u64 and -ENODATA if the
1131  * property does not have a value.
1132  */
1133 static inline int of_property_count_u64_elems(const struct device_node *np,
1134 				const char *propname)
1135 {
1136 	return of_property_count_elems_of_size(np, propname, sizeof(u64));
1137 }
1138 
1139 /**
1140  * of_property_read_string_array() - Read an array of strings from a multiple
1141  * strings property.
1142  * @np:		device node from which the property value is to be read.
1143  * @propname:	name of the property to be searched.
1144  * @out_strs:	output array of string pointers.
1145  * @sz:		number of array elements to read.
1146  *
1147  * Search for a property in a device tree node and retrieve a list of
1148  * terminated string values (pointer to data, not a copy) in that property.
1149  *
1150  * Return: If @out_strs is NULL, the number of strings in the property is returned.
1151  */
1152 static inline int of_property_read_string_array(const struct device_node *np,
1153 						const char *propname, const char **out_strs,
1154 						size_t sz)
1155 {
1156 	return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1157 }
1158 
1159 /**
1160  * of_property_count_strings() - Find and return the number of strings from a
1161  * multiple strings property.
1162  * @np:		device node from which the property value is to be read.
1163  * @propname:	name of the property to be searched.
1164  *
1165  * Search for a property in a device tree node and retrieve the number of null
1166  * terminated string contain in it.
1167  *
1168  * Return: The number of strings on success, -EINVAL if the property does not
1169  * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
1170  * is not null-terminated within the length of the property data.
1171  */
1172 static inline int of_property_count_strings(const struct device_node *np,
1173 					    const char *propname)
1174 {
1175 	return of_property_read_string_helper(np, propname, NULL, 0, 0);
1176 }
1177 
1178 /**
1179  * of_property_read_string_index() - Find and read a string from a multiple
1180  * strings property.
1181  * @np:		device node from which the property value is to be read.
1182  * @propname:	name of the property to be searched.
1183  * @index:	index of the string in the list of strings
1184  * @output:	pointer to null terminated return string, modified only if
1185  *		return value is 0.
1186  *
1187  * Search for a property in a device tree node and retrieve a null
1188  * terminated string value (pointer to data, not a copy) in the list of strings
1189  * contained in that property.
1190  *
1191  * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
1192  * property does not have a value, and -EILSEQ if the string is not
1193  * null-terminated within the length of the property data.
1194  *
1195  * The out_string pointer is modified only if a valid string can be decoded.
1196  */
1197 static inline int of_property_read_string_index(const struct device_node *np,
1198 						const char *propname,
1199 						int index, const char **output)
1200 {
1201 	int rc = of_property_read_string_helper(np, propname, output, 1, index);
1202 	return rc < 0 ? rc : 0;
1203 }
1204 
1205 /**
1206  * of_property_read_bool - Find a property
1207  * @np:		device node from which the property value is to be read.
1208  * @propname:	name of the property to be searched.
1209  *
1210  * Search for a boolean property in a device node. Usage on non-boolean
1211  * property types is deprecated.
1212  *
1213  * Return: true if the property exists false otherwise.
1214  */
1215 static inline bool of_property_read_bool(const struct device_node *np,
1216 					 const char *propname)
1217 {
1218 	struct property *prop = of_find_property(np, propname, NULL);
1219 
1220 	return prop ? true : false;
1221 }
1222 
1223 /**
1224  * of_property_present - Test if a property is present in a node
1225  * @np:		device node to search for the property.
1226  * @propname:	name of the property to be searched.
1227  *
1228  * Test for a property present in a device node.
1229  *
1230  * Return: true if the property exists false otherwise.
1231  */
1232 static inline bool of_property_present(const struct device_node *np, const char *propname)
1233 {
1234 	return of_property_read_bool(np, propname);
1235 }
1236 
1237 /**
1238  * of_property_read_u8_array - Find and read an array of u8 from a property.
1239  *
1240  * @np:		device node from which the property value is to be read.
1241  * @propname:	name of the property to be searched.
1242  * @out_values:	pointer to return value, modified only if return value is 0.
1243  * @sz:		number of array elements to read
1244  *
1245  * Search for a property in a device node and read 8-bit value(s) from
1246  * it.
1247  *
1248  * dts entry of array should be like:
1249  *  ``property = /bits/ 8 <0x50 0x60 0x70>;``
1250  *
1251  * Return: 0 on success, -EINVAL if the property does not exist,
1252  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1253  * property data isn't large enough.
1254  *
1255  * The out_values is modified only if a valid u8 value can be decoded.
1256  */
1257 static inline int of_property_read_u8_array(const struct device_node *np,
1258 					    const char *propname,
1259 					    u8 *out_values, size_t sz)
1260 {
1261 	int ret = of_property_read_variable_u8_array(np, propname, out_values,
1262 						     sz, 0);
1263 	if (ret >= 0)
1264 		return 0;
1265 	else
1266 		return ret;
1267 }
1268 
1269 /**
1270  * of_property_read_u16_array - Find and read an array of u16 from a property.
1271  *
1272  * @np:		device node from which the property value is to be read.
1273  * @propname:	name of the property to be searched.
1274  * @out_values:	pointer to return value, modified only if return value is 0.
1275  * @sz:		number of array elements to read
1276  *
1277  * Search for a property in a device node and read 16-bit value(s) from
1278  * it.
1279  *
1280  * dts entry of array should be like:
1281  *  ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1282  *
1283  * Return: 0 on success, -EINVAL if the property does not exist,
1284  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1285  * property data isn't large enough.
1286  *
1287  * The out_values is modified only if a valid u16 value can be decoded.
1288  */
1289 static inline int of_property_read_u16_array(const struct device_node *np,
1290 					     const char *propname,
1291 					     u16 *out_values, size_t sz)
1292 {
1293 	int ret = of_property_read_variable_u16_array(np, propname, out_values,
1294 						      sz, 0);
1295 	if (ret >= 0)
1296 		return 0;
1297 	else
1298 		return ret;
1299 }
1300 
1301 /**
1302  * of_property_read_u32_array - Find and read an array of 32 bit integers
1303  * from a property.
1304  *
1305  * @np:		device node from which the property value is to be read.
1306  * @propname:	name of the property to be searched.
1307  * @out_values:	pointer to return value, modified only if return value is 0.
1308  * @sz:		number of array elements to read
1309  *
1310  * Search for a property in a device node and read 32-bit value(s) from
1311  * it.
1312  *
1313  * Return: 0 on success, -EINVAL if the property does not exist,
1314  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1315  * property data isn't large enough.
1316  *
1317  * The out_values is modified only if a valid u32 value can be decoded.
1318  */
1319 static inline int of_property_read_u32_array(const struct device_node *np,
1320 					     const char *propname,
1321 					     u32 *out_values, size_t sz)
1322 {
1323 	int ret = of_property_read_variable_u32_array(np, propname, out_values,
1324 						      sz, 0);
1325 	if (ret >= 0)
1326 		return 0;
1327 	else
1328 		return ret;
1329 }
1330 
1331 /**
1332  * of_property_read_u64_array - Find and read an array of 64 bit integers
1333  * from a property.
1334  *
1335  * @np:		device node from which the property value is to be read.
1336  * @propname:	name of the property to be searched.
1337  * @out_values:	pointer to return value, modified only if return value is 0.
1338  * @sz:		number of array elements to read
1339  *
1340  * Search for a property in a device node and read 64-bit value(s) from
1341  * it.
1342  *
1343  * Return: 0 on success, -EINVAL if the property does not exist,
1344  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1345  * property data isn't large enough.
1346  *
1347  * The out_values is modified only if a valid u64 value can be decoded.
1348  */
1349 static inline int of_property_read_u64_array(const struct device_node *np,
1350 					     const char *propname,
1351 					     u64 *out_values, size_t sz)
1352 {
1353 	int ret = of_property_read_variable_u64_array(np, propname, out_values,
1354 						      sz, 0);
1355 	if (ret >= 0)
1356 		return 0;
1357 	else
1358 		return ret;
1359 }
1360 
1361 static inline int of_property_read_u8(const struct device_node *np,
1362 				       const char *propname,
1363 				       u8 *out_value)
1364 {
1365 	return of_property_read_u8_array(np, propname, out_value, 1);
1366 }
1367 
1368 static inline int of_property_read_u16(const struct device_node *np,
1369 				       const char *propname,
1370 				       u16 *out_value)
1371 {
1372 	return of_property_read_u16_array(np, propname, out_value, 1);
1373 }
1374 
1375 static inline int of_property_read_u32(const struct device_node *np,
1376 				       const char *propname,
1377 				       u32 *out_value)
1378 {
1379 	return of_property_read_u32_array(np, propname, out_value, 1);
1380 }
1381 
1382 static inline int of_property_read_s32(const struct device_node *np,
1383 				       const char *propname,
1384 				       s32 *out_value)
1385 {
1386 	return of_property_read_u32(np, propname, (u32*) out_value);
1387 }
1388 
1389 #define of_for_each_phandle(it, err, np, ln, cn, cc)			\
1390 	for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)),	\
1391 	     err = of_phandle_iterator_next(it);			\
1392 	     err == 0;							\
1393 	     err = of_phandle_iterator_next(it))
1394 
1395 #define of_property_for_each_u32(np, propname, prop, p, u)	\
1396 	for (prop = of_find_property(np, propname, NULL),	\
1397 		p = of_prop_next_u32(prop, NULL, &u);		\
1398 		p;						\
1399 		p = of_prop_next_u32(prop, p, &u))
1400 
1401 #define of_property_for_each_string(np, propname, prop, s)	\
1402 	for (prop = of_find_property(np, propname, NULL),	\
1403 		s = of_prop_next_string(prop, NULL);		\
1404 		s;						\
1405 		s = of_prop_next_string(prop, s))
1406 
1407 #define for_each_node_by_name(dn, name) \
1408 	for (dn = of_find_node_by_name(NULL, name); dn; \
1409 	     dn = of_find_node_by_name(dn, name))
1410 #define for_each_node_by_type(dn, type) \
1411 	for (dn = of_find_node_by_type(NULL, type); dn; \
1412 	     dn = of_find_node_by_type(dn, type))
1413 #define for_each_compatible_node(dn, type, compatible) \
1414 	for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1415 	     dn = of_find_compatible_node(dn, type, compatible))
1416 #define for_each_matching_node(dn, matches) \
1417 	for (dn = of_find_matching_node(NULL, matches); dn; \
1418 	     dn = of_find_matching_node(dn, matches))
1419 #define for_each_matching_node_and_match(dn, matches, match) \
1420 	for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1421 	     dn; dn = of_find_matching_node_and_match(dn, matches, match))
1422 
1423 #define for_each_child_of_node(parent, child) \
1424 	for (child = of_get_next_child(parent, NULL); child != NULL; \
1425 	     child = of_get_next_child(parent, child))
1426 #define for_each_available_child_of_node(parent, child) \
1427 	for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1428 	     child = of_get_next_available_child(parent, child))
1429 
1430 #define for_each_of_cpu_node(cpu) \
1431 	for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1432 	     cpu = of_get_next_cpu_node(cpu))
1433 
1434 #define for_each_node_with_property(dn, prop_name) \
1435 	for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1436 	     dn = of_find_node_with_property(dn, prop_name))
1437 
1438 static inline int of_get_child_count(const struct device_node *np)
1439 {
1440 	struct device_node *child;
1441 	int num = 0;
1442 
1443 	for_each_child_of_node(np, child)
1444 		num++;
1445 
1446 	return num;
1447 }
1448 
1449 static inline int of_get_available_child_count(const struct device_node *np)
1450 {
1451 	struct device_node *child;
1452 	int num = 0;
1453 
1454 	for_each_available_child_of_node(np, child)
1455 		num++;
1456 
1457 	return num;
1458 }
1459 
1460 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type)		\
1461 	static const struct of_device_id __of_table_##name		\
1462 		__attribute__((unused))					\
1463 		 = { .compatible = compat,				\
1464 		     .data = (fn == (fn_type)NULL) ? fn : fn }
1465 
1466 #if defined(CONFIG_OF) && !defined(MODULE)
1467 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1468 	static const struct of_device_id __of_table_##name		\
1469 		__used __section("__" #table "_of_table")		\
1470 		__aligned(__alignof__(struct of_device_id))		\
1471 		 = { .compatible = compat,				\
1472 		     .data = (fn == (fn_type)NULL) ? fn : fn  }
1473 #else
1474 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1475 	_OF_DECLARE_STUB(table, name, compat, fn, fn_type)
1476 #endif
1477 
1478 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1479 typedef int (*of_init_fn_1_ret)(struct device_node *);
1480 typedef void (*of_init_fn_1)(struct device_node *);
1481 
1482 #define OF_DECLARE_1(table, name, compat, fn) \
1483 		_OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1484 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1485 		_OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1486 #define OF_DECLARE_2(table, name, compat, fn) \
1487 		_OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1488 
1489 /**
1490  * struct of_changeset_entry	- Holds a changeset entry
1491  *
1492  * @node:	list_head for the log list
1493  * @action:	notifier action
1494  * @np:		pointer to the device node affected
1495  * @prop:	pointer to the property affected
1496  * @old_prop:	hold a pointer to the original property
1497  *
1498  * Every modification of the device tree during a changeset
1499  * is held in a list of of_changeset_entry structures.
1500  * That way we can recover from a partial application, or we can
1501  * revert the changeset
1502  */
1503 struct of_changeset_entry {
1504 	struct list_head node;
1505 	unsigned long action;
1506 	struct device_node *np;
1507 	struct property *prop;
1508 	struct property *old_prop;
1509 };
1510 
1511 /**
1512  * struct of_changeset - changeset tracker structure
1513  *
1514  * @entries:	list_head for the changeset entries
1515  *
1516  * changesets are a convenient way to apply bulk changes to the
1517  * live tree. In case of an error, changes are rolled-back.
1518  * changesets live on after initial application, and if not
1519  * destroyed after use, they can be reverted in one single call.
1520  */
1521 struct of_changeset {
1522 	struct list_head entries;
1523 };
1524 
1525 enum of_reconfig_change {
1526 	OF_RECONFIG_NO_CHANGE = 0,
1527 	OF_RECONFIG_CHANGE_ADD,
1528 	OF_RECONFIG_CHANGE_REMOVE,
1529 };
1530 
1531 #ifdef CONFIG_OF_DYNAMIC
1532 extern int of_reconfig_notifier_register(struct notifier_block *);
1533 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1534 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1535 extern int of_reconfig_get_state_change(unsigned long action,
1536 					struct of_reconfig_data *arg);
1537 
1538 extern void of_changeset_init(struct of_changeset *ocs);
1539 extern void of_changeset_destroy(struct of_changeset *ocs);
1540 extern int of_changeset_apply(struct of_changeset *ocs);
1541 extern int of_changeset_revert(struct of_changeset *ocs);
1542 extern int of_changeset_action(struct of_changeset *ocs,
1543 		unsigned long action, struct device_node *np,
1544 		struct property *prop);
1545 
1546 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1547 		struct device_node *np)
1548 {
1549 	return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1550 }
1551 
1552 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1553 		struct device_node *np)
1554 {
1555 	return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1556 }
1557 
1558 static inline int of_changeset_add_property(struct of_changeset *ocs,
1559 		struct device_node *np, struct property *prop)
1560 {
1561 	return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1562 }
1563 
1564 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1565 		struct device_node *np, struct property *prop)
1566 {
1567 	return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1568 }
1569 
1570 static inline int of_changeset_update_property(struct of_changeset *ocs,
1571 		struct device_node *np, struct property *prop)
1572 {
1573 	return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1574 }
1575 #else /* CONFIG_OF_DYNAMIC */
1576 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1577 {
1578 	return -EINVAL;
1579 }
1580 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1581 {
1582 	return -EINVAL;
1583 }
1584 static inline int of_reconfig_notify(unsigned long action,
1585 				     struct of_reconfig_data *arg)
1586 {
1587 	return -EINVAL;
1588 }
1589 static inline int of_reconfig_get_state_change(unsigned long action,
1590 						struct of_reconfig_data *arg)
1591 {
1592 	return -EINVAL;
1593 }
1594 #endif /* CONFIG_OF_DYNAMIC */
1595 
1596 /**
1597  * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1598  * @np: Pointer to the given device_node
1599  *
1600  * Return: true if present false otherwise
1601  */
1602 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1603 {
1604 	return of_property_read_bool(np, "system-power-controller");
1605 }
1606 
1607 /*
1608  * Overlay support
1609  */
1610 
1611 enum of_overlay_notify_action {
1612 	OF_OVERLAY_INIT = 0,	/* kzalloc() of ovcs sets this value */
1613 	OF_OVERLAY_PRE_APPLY,
1614 	OF_OVERLAY_POST_APPLY,
1615 	OF_OVERLAY_PRE_REMOVE,
1616 	OF_OVERLAY_POST_REMOVE,
1617 };
1618 
1619 static inline const char *of_overlay_action_name(enum of_overlay_notify_action action)
1620 {
1621 	static const char *const of_overlay_action_name[] = {
1622 		"init",
1623 		"pre-apply",
1624 		"post-apply",
1625 		"pre-remove",
1626 		"post-remove",
1627 	};
1628 
1629 	return of_overlay_action_name[action];
1630 }
1631 
1632 struct of_overlay_notify_data {
1633 	struct device_node *overlay;
1634 	struct device_node *target;
1635 };
1636 
1637 #ifdef CONFIG_OF_OVERLAY
1638 
1639 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1640 			 int *ovcs_id);
1641 int of_overlay_remove(int *ovcs_id);
1642 int of_overlay_remove_all(void);
1643 
1644 int of_overlay_notifier_register(struct notifier_block *nb);
1645 int of_overlay_notifier_unregister(struct notifier_block *nb);
1646 
1647 #else
1648 
1649 static inline int of_overlay_fdt_apply(void *overlay_fdt, u32 overlay_fdt_size,
1650 				       int *ovcs_id)
1651 {
1652 	return -ENOTSUPP;
1653 }
1654 
1655 static inline int of_overlay_remove(int *ovcs_id)
1656 {
1657 	return -ENOTSUPP;
1658 }
1659 
1660 static inline int of_overlay_remove_all(void)
1661 {
1662 	return -ENOTSUPP;
1663 }
1664 
1665 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1666 {
1667 	return 0;
1668 }
1669 
1670 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1671 {
1672 	return 0;
1673 }
1674 
1675 #endif
1676 
1677 #endif /* _LINUX_OF_H */
1678