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