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