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