1 /* SPDX-License-Identifier: GPL-2.0+ */
2 #ifndef _LINUX_OF_H
3 #define _LINUX_OF_H
4 /*
5  * Definitions for talking to the Open Firmware PROM on
6  * Power Macintosh and other computers.
7  *
8  * Copyright (C) 1996-2005 Paul Mackerras.
9  *
10  * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
11  * Updates for SPARC64 by David S. Miller
12  * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
13  */
14 #include <linux/types.h>
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/kobject.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/spinlock.h>
20 #include <linux/topology.h>
21 #include <linux/notifier.h>
22 #include <linux/property.h>
23 #include <linux/list.h>
24 
25 #include <asm/byteorder.h>
26 #include <asm/errno.h>
27 
28 typedef u32 phandle;
29 typedef u32 ihandle;
30 
31 struct property {
32 	char	*name;
33 	int	length;
34 	void	*value;
35 	struct property *next;
36 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
37 	unsigned long _flags;
38 #endif
39 #if defined(CONFIG_OF_PROMTREE)
40 	unsigned int unique_id;
41 #endif
42 #if defined(CONFIG_OF_KOBJ)
43 	struct bin_attribute attr;
44 #endif
45 };
46 
47 #if defined(CONFIG_SPARC)
48 struct of_irq_controller;
49 #endif
50 
51 struct device_node {
52 	const char *name;
53 	const char *type;
54 	phandle phandle;
55 	const char *full_name;
56 	struct fwnode_handle fwnode;
57 
58 	struct	property *properties;
59 	struct	property *deadprops;	/* removed properties */
60 	struct	device_node *parent;
61 	struct	device_node *child;
62 	struct	device_node *sibling;
63 #if defined(CONFIG_OF_KOBJ)
64 	struct	kobject kobj;
65 #endif
66 	unsigned long _flags;
67 	void	*data;
68 #if defined(CONFIG_SPARC)
69 	const char *path_component_name;
70 	unsigned int unique_id;
71 	struct of_irq_controller *irq_trans;
72 #endif
73 };
74 
75 #define MAX_PHANDLE_ARGS 16
76 struct of_phandle_args {
77 	struct device_node *np;
78 	int args_count;
79 	uint32_t args[MAX_PHANDLE_ARGS];
80 };
81 
82 struct of_phandle_iterator {
83 	/* Common iterator information */
84 	const char *cells_name;
85 	int cell_count;
86 	const struct device_node *parent;
87 
88 	/* List size information */
89 	const __be32 *list_end;
90 	const __be32 *phandle_end;
91 
92 	/* Current position state */
93 	const __be32 *cur;
94 	uint32_t cur_count;
95 	phandle phandle;
96 	struct device_node *node;
97 };
98 
99 struct of_reconfig_data {
100 	struct device_node	*dn;
101 	struct property		*prop;
102 	struct property		*old_prop;
103 };
104 
105 /* initialize a node */
106 extern struct kobj_type of_node_ktype;
107 extern const struct fwnode_operations of_fwnode_ops;
of_node_init(struct device_node * node)108 static inline void of_node_init(struct device_node *node)
109 {
110 #if defined(CONFIG_OF_KOBJ)
111 	kobject_init(&node->kobj, &of_node_ktype);
112 #endif
113 	node->fwnode.ops = &of_fwnode_ops;
114 }
115 
116 #if defined(CONFIG_OF_KOBJ)
117 #define of_node_kobj(n) (&(n)->kobj)
118 #else
119 #define of_node_kobj(n) NULL
120 #endif
121 
122 #ifdef CONFIG_OF_DYNAMIC
123 extern struct device_node *of_node_get(struct device_node *node);
124 extern void of_node_put(struct device_node *node);
125 #else /* CONFIG_OF_DYNAMIC */
126 /* Dummy ref counting routines - to be implemented later */
of_node_get(struct device_node * node)127 static inline struct device_node *of_node_get(struct device_node *node)
128 {
129 	return node;
130 }
of_node_put(struct device_node * node)131 static inline void of_node_put(struct device_node *node) { }
132 #endif /* !CONFIG_OF_DYNAMIC */
133 
134 /* Pointer for first entry in chain of all nodes. */
135 extern struct device_node *of_root;
136 extern struct device_node *of_chosen;
137 extern struct device_node *of_aliases;
138 extern struct device_node *of_stdout;
139 extern raw_spinlock_t devtree_lock;
140 
141 /*
142  * struct device_node flag descriptions
143  * (need to be visible even when !CONFIG_OF)
144  */
145 #define OF_DYNAMIC		1 /* (and properties) allocated via kmalloc */
146 #define OF_DETACHED		2 /* detached from the device tree */
147 #define OF_POPULATED		3 /* device already created */
148 #define OF_POPULATED_BUS	4 /* platform bus created for children */
149 #define OF_OVERLAY		5 /* allocated for an overlay */
150 #define OF_OVERLAY_FREE_CSET	6 /* in overlay cset being freed */
151 
152 #define OF_BAD_ADDR	((u64)-1)
153 
154 #ifdef CONFIG_OF
155 void of_core_init(void);
156 
is_of_node(const struct fwnode_handle * fwnode)157 static inline bool is_of_node(const struct fwnode_handle *fwnode)
158 {
159 	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
160 }
161 
162 #define to_of_node(__fwnode)						\
163 	({								\
164 		typeof(__fwnode) __to_of_node_fwnode = (__fwnode);	\
165 									\
166 		is_of_node(__to_of_node_fwnode) ?			\
167 			container_of(__to_of_node_fwnode,		\
168 				     struct device_node, fwnode) :	\
169 			NULL;						\
170 	})
171 
172 #define of_fwnode_handle(node)						\
173 	({								\
174 		typeof(node) __of_fwnode_handle_node = (node);		\
175 									\
176 		__of_fwnode_handle_node ?				\
177 			&__of_fwnode_handle_node->fwnode : NULL;	\
178 	})
179 
of_have_populated_dt(void)180 static inline bool of_have_populated_dt(void)
181 {
182 	return of_root != NULL;
183 }
184 
of_node_is_root(const struct device_node * node)185 static inline bool of_node_is_root(const struct device_node *node)
186 {
187 	return node && (node->parent == NULL);
188 }
189 
of_node_check_flag(struct device_node * n,unsigned long flag)190 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
191 {
192 	return test_bit(flag, &n->_flags);
193 }
194 
of_node_test_and_set_flag(struct device_node * n,unsigned long flag)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 
of_node_set_flag(struct device_node * n,unsigned long flag)201 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
202 {
203 	set_bit(flag, &n->_flags);
204 }
205 
of_node_clear_flag(struct device_node * n,unsigned long flag)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)
of_property_check_flag(struct property * p,unsigned long flag)212 static inline int of_property_check_flag(struct property *p, unsigned long flag)
213 {
214 	return test_bit(flag, &p->_flags);
215 }
216 
of_property_set_flag(struct property * p,unsigned long flag)217 static inline void of_property_set_flag(struct property *p, unsigned long flag)
218 {
219 	set_bit(flag, &p->_flags);
220 }
221 
of_property_clear_flag(struct property * p,unsigned long flag)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) */
of_read_number(const __be32 * cell,int size)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 */
of_read_ulong(const __be32 * cell,int size)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 /* Default #address and #size cells.  Allow arch asm/prom.h to override */
256 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
257 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
258 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
259 #endif
260 
261 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
262 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
263 
264 extern bool of_node_name_eq(const struct device_node *np, const char *name);
265 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
266 
of_node_full_name(const struct device_node * np)267 static inline const char *of_node_full_name(const struct device_node *np)
268 {
269 	return np ? np->full_name : "<no-node>";
270 }
271 
272 #define for_each_of_allnodes_from(from, dn) \
273 	for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
274 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
275 extern struct device_node *of_find_node_by_name(struct device_node *from,
276 	const char *name);
277 extern struct device_node *of_find_node_by_type(struct device_node *from,
278 	const char *type);
279 extern struct device_node *of_find_compatible_node(struct device_node *from,
280 	const char *type, const char *compat);
281 extern struct device_node *of_find_matching_node_and_match(
282 	struct device_node *from,
283 	const struct of_device_id *matches,
284 	const struct of_device_id **match);
285 
286 extern struct device_node *of_find_node_opts_by_path(const char *path,
287 	const char **opts);
of_find_node_by_path(const char * path)288 static inline struct device_node *of_find_node_by_path(const char *path)
289 {
290 	return of_find_node_opts_by_path(path, NULL);
291 }
292 
293 extern struct device_node *of_find_node_by_phandle(phandle handle);
294 extern struct device_node *of_get_parent(const struct device_node *node);
295 extern struct device_node *of_get_next_parent(struct device_node *node);
296 extern struct device_node *of_get_next_child(const struct device_node *node,
297 					     struct device_node *prev);
298 extern struct device_node *of_get_next_available_child(
299 	const struct device_node *node, struct device_node *prev);
300 
301 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
302 					const char *compatible);
303 extern struct device_node *of_get_child_by_name(const struct device_node *node,
304 					const char *name);
305 
306 /* cache lookup */
307 extern struct device_node *of_find_next_cache_node(const struct device_node *);
308 extern int of_find_last_cache_level(unsigned int cpu);
309 extern struct device_node *of_find_node_with_property(
310 	struct device_node *from, const char *prop_name);
311 
312 extern struct property *of_find_property(const struct device_node *np,
313 					 const char *name,
314 					 int *lenp);
315 extern int of_property_count_elems_of_size(const struct device_node *np,
316 				const char *propname, int elem_size);
317 extern int of_property_read_u32_index(const struct device_node *np,
318 				       const char *propname,
319 				       u32 index, u32 *out_value);
320 extern int of_property_read_u64_index(const struct device_node *np,
321 				       const char *propname,
322 				       u32 index, u64 *out_value);
323 extern int of_property_read_variable_u8_array(const struct device_node *np,
324 					const char *propname, u8 *out_values,
325 					size_t sz_min, size_t sz_max);
326 extern int of_property_read_variable_u16_array(const struct device_node *np,
327 					const char *propname, u16 *out_values,
328 					size_t sz_min, size_t sz_max);
329 extern int of_property_read_variable_u32_array(const struct device_node *np,
330 					const char *propname,
331 					u32 *out_values,
332 					size_t sz_min,
333 					size_t sz_max);
334 extern int of_property_read_u64(const struct device_node *np,
335 				const char *propname, u64 *out_value);
336 extern int of_property_read_variable_u64_array(const struct device_node *np,
337 					const char *propname,
338 					u64 *out_values,
339 					size_t sz_min,
340 					size_t sz_max);
341 
342 extern int of_property_read_string(const struct device_node *np,
343 				   const char *propname,
344 				   const char **out_string);
345 extern int of_property_match_string(const struct device_node *np,
346 				    const char *propname,
347 				    const char *string);
348 extern int of_property_read_string_helper(const struct device_node *np,
349 					      const char *propname,
350 					      const char **out_strs, size_t sz, int index);
351 extern int of_device_is_compatible(const struct device_node *device,
352 				   const char *);
353 extern int of_device_compatible_match(struct device_node *device,
354 				      const char *const *compat);
355 extern bool of_device_is_available(const struct device_node *device);
356 extern bool of_device_is_big_endian(const struct device_node *device);
357 extern const void *of_get_property(const struct device_node *node,
358 				const char *name,
359 				int *lenp);
360 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
361 #define for_each_property_of_node(dn, pp) \
362 	for (pp = dn->properties; pp != NULL; pp = pp->next)
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 int of_modalias_node(struct device_node *node, char *modalias, int len);
369 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
370 extern struct device_node *of_parse_phandle(const struct device_node *np,
371 					    const char *phandle_name,
372 					    int index);
373 extern int of_parse_phandle_with_args(const struct device_node *np,
374 	const char *list_name, const char *cells_name, int index,
375 	struct of_phandle_args *out_args);
376 extern int of_parse_phandle_with_args_map(const struct device_node *np,
377 	const char *list_name, const char *stem_name, int index,
378 	struct of_phandle_args *out_args);
379 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
380 	const char *list_name, int cells_count, int index,
381 	struct of_phandle_args *out_args);
382 extern int of_count_phandle_with_args(const struct device_node *np,
383 	const char *list_name, const char *cells_name);
384 
385 /* 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 extern int of_machine_is_compatible(const char *compat);
402 
403 extern int of_add_property(struct device_node *np, struct property *prop);
404 extern int of_remove_property(struct device_node *np, struct property *prop);
405 extern int of_update_property(struct device_node *np, struct property *newprop);
406 
407 /* For updating the device tree at runtime */
408 #define OF_RECONFIG_ATTACH_NODE		0x0001
409 #define OF_RECONFIG_DETACH_NODE		0x0002
410 #define OF_RECONFIG_ADD_PROPERTY	0x0003
411 #define OF_RECONFIG_REMOVE_PROPERTY	0x0004
412 #define OF_RECONFIG_UPDATE_PROPERTY	0x0005
413 
414 extern int of_attach_node(struct device_node *);
415 extern int of_detach_node(struct device_node *);
416 
417 #define of_match_ptr(_ptr)	(_ptr)
418 
419 /**
420  * of_property_read_u8_array - Find and read an array of u8 from a property.
421  *
422  * @np:		device node from which the property value is to be read.
423  * @propname:	name of the property to be searched.
424  * @out_values:	pointer to return value, modified only if return value is 0.
425  * @sz:		number of array elements to read
426  *
427  * Search for a property in a device node and read 8-bit value(s) from
428  * it. Returns 0 on success, -EINVAL if the property does not exist,
429  * -ENODATA if property does not have a value, and -EOVERFLOW if the
430  * property data isn't large enough.
431  *
432  * dts entry of array should be like:
433  *	property = /bits/ 8 <0x50 0x60 0x70>;
434  *
435  * The out_values is modified only if a valid u8 value can be decoded.
436  */
of_property_read_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz)437 static inline int of_property_read_u8_array(const struct device_node *np,
438 					    const char *propname,
439 					    u8 *out_values, size_t sz)
440 {
441 	int ret = of_property_read_variable_u8_array(np, propname, out_values,
442 						     sz, 0);
443 	if (ret >= 0)
444 		return 0;
445 	else
446 		return ret;
447 }
448 
449 /**
450  * of_property_read_u16_array - Find and read an array of u16 from a property.
451  *
452  * @np:		device node from which the property value is to be read.
453  * @propname:	name of the property to be searched.
454  * @out_values:	pointer to return value, modified only if return value is 0.
455  * @sz:		number of array elements to read
456  *
457  * Search for a property in a device node and read 16-bit value(s) from
458  * it. Returns 0 on success, -EINVAL if the property does not exist,
459  * -ENODATA if property does not have a value, and -EOVERFLOW if the
460  * property data isn't large enough.
461  *
462  * dts entry of array should be like:
463  *	property = /bits/ 16 <0x5000 0x6000 0x7000>;
464  *
465  * The out_values is modified only if a valid u16 value can be decoded.
466  */
of_property_read_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz)467 static inline int of_property_read_u16_array(const struct device_node *np,
468 					     const char *propname,
469 					     u16 *out_values, size_t sz)
470 {
471 	int ret = of_property_read_variable_u16_array(np, propname, out_values,
472 						      sz, 0);
473 	if (ret >= 0)
474 		return 0;
475 	else
476 		return ret;
477 }
478 
479 /**
480  * of_property_read_u32_array - Find and read an array of 32 bit integers
481  * from a property.
482  *
483  * @np:		device node from which the property value is to be read.
484  * @propname:	name of the property to be searched.
485  * @out_values:	pointer to return value, modified only if return value is 0.
486  * @sz:		number of array elements to read
487  *
488  * Search for a property in a device node and read 32-bit value(s) from
489  * it. Returns 0 on success, -EINVAL if the property does not exist,
490  * -ENODATA if property does not have a value, and -EOVERFLOW if the
491  * property data isn't large enough.
492  *
493  * The out_values is modified only if a valid u32 value can be decoded.
494  */
of_property_read_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz)495 static inline int of_property_read_u32_array(const struct device_node *np,
496 					     const char *propname,
497 					     u32 *out_values, size_t sz)
498 {
499 	int ret = of_property_read_variable_u32_array(np, propname, out_values,
500 						      sz, 0);
501 	if (ret >= 0)
502 		return 0;
503 	else
504 		return ret;
505 }
506 
507 /**
508  * of_property_read_u64_array - Find and read an array of 64 bit integers
509  * from a property.
510  *
511  * @np:		device node from which the property value is to be read.
512  * @propname:	name of the property to be searched.
513  * @out_values:	pointer to return value, modified only if return value is 0.
514  * @sz:		number of array elements to read
515  *
516  * Search for a property in a device node and read 64-bit value(s) from
517  * it. Returns 0 on success, -EINVAL if the property does not exist,
518  * -ENODATA if property does not have a value, and -EOVERFLOW if the
519  * property data isn't large enough.
520  *
521  * The out_values is modified only if a valid u64 value can be decoded.
522  */
of_property_read_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz)523 static inline int of_property_read_u64_array(const struct device_node *np,
524 					     const char *propname,
525 					     u64 *out_values, size_t sz)
526 {
527 	int ret = of_property_read_variable_u64_array(np, propname, out_values,
528 						      sz, 0);
529 	if (ret >= 0)
530 		return 0;
531 	else
532 		return ret;
533 }
534 
535 /*
536  * struct property *prop;
537  * const __be32 *p;
538  * u32 u;
539  *
540  * of_property_for_each_u32(np, "propname", prop, p, u)
541  *         printk("U32 value: %x\n", u);
542  */
543 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
544 			       u32 *pu);
545 /*
546  * struct property *prop;
547  * const char *s;
548  *
549  * of_property_for_each_string(np, "propname", prop, s)
550  *         printk("String value: %s\n", s);
551  */
552 const char *of_prop_next_string(struct property *prop, const char *cur);
553 
554 bool of_console_check(struct device_node *dn, char *name, int index);
555 
556 extern int of_cpu_node_to_id(struct device_node *np);
557 
558 #else /* CONFIG_OF */
559 
of_core_init(void)560 static inline void of_core_init(void)
561 {
562 }
563 
is_of_node(const struct fwnode_handle * fwnode)564 static inline bool is_of_node(const struct fwnode_handle *fwnode)
565 {
566 	return false;
567 }
568 
to_of_node(const struct fwnode_handle * fwnode)569 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
570 {
571 	return NULL;
572 }
573 
of_node_name_eq(const struct device_node * np,const char * name)574 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
575 {
576 	return false;
577 }
578 
of_node_name_prefix(const struct device_node * np,const char * prefix)579 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
580 {
581 	return false;
582 }
583 
of_node_full_name(const struct device_node * np)584 static inline const char* of_node_full_name(const struct device_node *np)
585 {
586 	return "<no-node>";
587 }
588 
of_find_node_by_name(struct device_node * from,const char * name)589 static inline struct device_node *of_find_node_by_name(struct device_node *from,
590 	const char *name)
591 {
592 	return NULL;
593 }
594 
of_find_node_by_type(struct device_node * from,const char * type)595 static inline struct device_node *of_find_node_by_type(struct device_node *from,
596 	const char *type)
597 {
598 	return NULL;
599 }
600 
of_find_matching_node_and_match(struct device_node * from,const struct of_device_id * matches,const struct of_device_id ** match)601 static inline struct device_node *of_find_matching_node_and_match(
602 	struct device_node *from,
603 	const struct of_device_id *matches,
604 	const struct of_device_id **match)
605 {
606 	return NULL;
607 }
608 
of_find_node_by_path(const char * path)609 static inline struct device_node *of_find_node_by_path(const char *path)
610 {
611 	return NULL;
612 }
613 
of_find_node_opts_by_path(const char * path,const char ** opts)614 static inline struct device_node *of_find_node_opts_by_path(const char *path,
615 	const char **opts)
616 {
617 	return NULL;
618 }
619 
of_find_node_by_phandle(phandle handle)620 static inline struct device_node *of_find_node_by_phandle(phandle handle)
621 {
622 	return NULL;
623 }
624 
of_get_parent(const struct device_node * node)625 static inline struct device_node *of_get_parent(const struct device_node *node)
626 {
627 	return NULL;
628 }
629 
of_get_next_child(const struct device_node * node,struct device_node * prev)630 static inline struct device_node *of_get_next_child(
631 	const struct device_node *node, struct device_node *prev)
632 {
633 	return NULL;
634 }
635 
of_get_next_available_child(const struct device_node * node,struct device_node * prev)636 static inline struct device_node *of_get_next_available_child(
637 	const struct device_node *node, struct device_node *prev)
638 {
639 	return NULL;
640 }
641 
of_find_node_with_property(struct device_node * from,const char * prop_name)642 static inline struct device_node *of_find_node_with_property(
643 	struct device_node *from, const char *prop_name)
644 {
645 	return NULL;
646 }
647 
648 #define of_fwnode_handle(node) NULL
649 
of_have_populated_dt(void)650 static inline bool of_have_populated_dt(void)
651 {
652 	return false;
653 }
654 
of_get_compatible_child(const struct device_node * parent,const char * compatible)655 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
656 					const char *compatible)
657 {
658 	return NULL;
659 }
660 
of_get_child_by_name(const struct device_node * node,const char * name)661 static inline struct device_node *of_get_child_by_name(
662 					const struct device_node *node,
663 					const char *name)
664 {
665 	return NULL;
666 }
667 
of_device_is_compatible(const struct device_node * device,const char * name)668 static inline int of_device_is_compatible(const struct device_node *device,
669 					  const char *name)
670 {
671 	return 0;
672 }
673 
of_device_compatible_match(struct device_node * device,const char * const * compat)674 static inline  int of_device_compatible_match(struct device_node *device,
675 					      const char *const *compat)
676 {
677 	return 0;
678 }
679 
of_device_is_available(const struct device_node * device)680 static inline bool of_device_is_available(const struct device_node *device)
681 {
682 	return false;
683 }
684 
of_device_is_big_endian(const struct device_node * device)685 static inline bool of_device_is_big_endian(const struct device_node *device)
686 {
687 	return false;
688 }
689 
of_find_property(const struct device_node * np,const char * name,int * lenp)690 static inline struct property *of_find_property(const struct device_node *np,
691 						const char *name,
692 						int *lenp)
693 {
694 	return NULL;
695 }
696 
of_find_compatible_node(struct device_node * from,const char * type,const char * compat)697 static inline struct device_node *of_find_compatible_node(
698 						struct device_node *from,
699 						const char *type,
700 						const char *compat)
701 {
702 	return NULL;
703 }
704 
of_property_count_elems_of_size(const struct device_node * np,const char * propname,int elem_size)705 static inline int of_property_count_elems_of_size(const struct device_node *np,
706 			const char *propname, int elem_size)
707 {
708 	return -ENOSYS;
709 }
710 
of_property_read_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz)711 static inline int of_property_read_u8_array(const struct device_node *np,
712 			const char *propname, u8 *out_values, size_t sz)
713 {
714 	return -ENOSYS;
715 }
716 
of_property_read_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz)717 static inline int of_property_read_u16_array(const struct device_node *np,
718 			const char *propname, u16 *out_values, size_t sz)
719 {
720 	return -ENOSYS;
721 }
722 
of_property_read_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz)723 static inline int of_property_read_u32_array(const struct device_node *np,
724 					     const char *propname,
725 					     u32 *out_values, size_t sz)
726 {
727 	return -ENOSYS;
728 }
729 
of_property_read_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz)730 static inline int of_property_read_u64_array(const struct device_node *np,
731 					     const char *propname,
732 					     u64 *out_values, size_t sz)
733 {
734 	return -ENOSYS;
735 }
736 
of_property_read_u32_index(const struct device_node * np,const char * propname,u32 index,u32 * out_value)737 static inline int of_property_read_u32_index(const struct device_node *np,
738 			const char *propname, u32 index, u32 *out_value)
739 {
740 	return -ENOSYS;
741 }
742 
of_property_read_u64_index(const struct device_node * np,const char * propname,u32 index,u64 * out_value)743 static inline int of_property_read_u64_index(const struct device_node *np,
744 			const char *propname, u32 index, u64 *out_value)
745 {
746 	return -ENOSYS;
747 }
748 
of_get_property(const struct device_node * node,const char * name,int * lenp)749 static inline const void *of_get_property(const struct device_node *node,
750 				const char *name,
751 				int *lenp)
752 {
753 	return NULL;
754 }
755 
of_get_cpu_node(int cpu,unsigned int * thread)756 static inline struct device_node *of_get_cpu_node(int cpu,
757 					unsigned int *thread)
758 {
759 	return NULL;
760 }
761 
of_n_addr_cells(struct device_node * np)762 static inline int of_n_addr_cells(struct device_node *np)
763 {
764 	return 0;
765 
766 }
of_n_size_cells(struct device_node * np)767 static inline int of_n_size_cells(struct device_node *np)
768 {
769 	return 0;
770 }
771 
of_property_read_variable_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz_min,size_t sz_max)772 static inline int of_property_read_variable_u8_array(const struct device_node *np,
773 					const char *propname, u8 *out_values,
774 					size_t sz_min, size_t sz_max)
775 {
776 	return -ENOSYS;
777 }
778 
of_property_read_variable_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz_min,size_t sz_max)779 static inline int of_property_read_variable_u16_array(const struct device_node *np,
780 					const char *propname, u16 *out_values,
781 					size_t sz_min, size_t sz_max)
782 {
783 	return -ENOSYS;
784 }
785 
of_property_read_variable_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz_min,size_t sz_max)786 static inline int of_property_read_variable_u32_array(const struct device_node *np,
787 					const char *propname,
788 					u32 *out_values,
789 					size_t sz_min,
790 					size_t sz_max)
791 {
792 	return -ENOSYS;
793 }
794 
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)795 static inline int of_property_read_u64(const struct device_node *np,
796 				       const char *propname, u64 *out_value)
797 {
798 	return -ENOSYS;
799 }
800 
of_property_read_variable_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz_min,size_t sz_max)801 static inline int of_property_read_variable_u64_array(const struct device_node *np,
802 					const char *propname,
803 					u64 *out_values,
804 					size_t sz_min,
805 					size_t sz_max)
806 {
807 	return -ENOSYS;
808 }
809 
of_property_read_string(const struct device_node * np,const char * propname,const char ** out_string)810 static inline int of_property_read_string(const struct device_node *np,
811 					  const char *propname,
812 					  const char **out_string)
813 {
814 	return -ENOSYS;
815 }
816 
of_property_match_string(const struct device_node * np,const char * propname,const char * string)817 static inline int of_property_match_string(const struct device_node *np,
818 					   const char *propname,
819 					   const char *string)
820 {
821 	return -ENOSYS;
822 }
823 
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int index)824 static inline int of_property_read_string_helper(const struct device_node *np,
825 						 const char *propname,
826 						 const char **out_strs, size_t sz, int index)
827 {
828 	return -ENOSYS;
829 }
830 
of_parse_phandle(const struct device_node * np,const char * phandle_name,int index)831 static inline struct device_node *of_parse_phandle(const struct device_node *np,
832 						   const char *phandle_name,
833 						   int index)
834 {
835 	return NULL;
836 }
837 
of_parse_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name,int index,struct of_phandle_args * out_args)838 static inline int of_parse_phandle_with_args(const struct device_node *np,
839 					     const char *list_name,
840 					     const char *cells_name,
841 					     int index,
842 					     struct of_phandle_args *out_args)
843 {
844 	return -ENOSYS;
845 }
846 
of_parse_phandle_with_args_map(const struct device_node * np,const char * list_name,const char * stem_name,int index,struct of_phandle_args * out_args)847 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
848 						 const char *list_name,
849 						 const char *stem_name,
850 						 int index,
851 						 struct of_phandle_args *out_args)
852 {
853 	return -ENOSYS;
854 }
855 
of_parse_phandle_with_fixed_args(const struct device_node * np,const char * list_name,int cells_count,int index,struct of_phandle_args * out_args)856 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
857 	const char *list_name, int cells_count, int index,
858 	struct of_phandle_args *out_args)
859 {
860 	return -ENOSYS;
861 }
862 
of_count_phandle_with_args(struct device_node * np,const char * list_name,const char * cells_name)863 static inline int of_count_phandle_with_args(struct device_node *np,
864 					     const char *list_name,
865 					     const char *cells_name)
866 {
867 	return -ENOSYS;
868 }
869 
of_phandle_iterator_init(struct of_phandle_iterator * it,const struct device_node * np,const char * list_name,const char * cells_name,int cell_count)870 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
871 					   const struct device_node *np,
872 					   const char *list_name,
873 					   const char *cells_name,
874 					   int cell_count)
875 {
876 	return -ENOSYS;
877 }
878 
of_phandle_iterator_next(struct of_phandle_iterator * it)879 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
880 {
881 	return -ENOSYS;
882 }
883 
of_phandle_iterator_args(struct of_phandle_iterator * it,uint32_t * args,int size)884 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
885 					   uint32_t *args,
886 					   int size)
887 {
888 	return 0;
889 }
890 
of_alias_get_id(struct device_node * np,const char * stem)891 static inline int of_alias_get_id(struct device_node *np, const char *stem)
892 {
893 	return -ENOSYS;
894 }
895 
of_alias_get_highest_id(const char * stem)896 static inline int of_alias_get_highest_id(const char *stem)
897 {
898 	return -ENOSYS;
899 }
900 
of_machine_is_compatible(const char * compat)901 static inline int of_machine_is_compatible(const char *compat)
902 {
903 	return 0;
904 }
905 
of_console_check(const struct device_node * dn,const char * name,int index)906 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
907 {
908 	return false;
909 }
910 
of_prop_next_u32(struct property * prop,const __be32 * cur,u32 * pu)911 static inline const __be32 *of_prop_next_u32(struct property *prop,
912 		const __be32 *cur, u32 *pu)
913 {
914 	return NULL;
915 }
916 
of_prop_next_string(struct property * prop,const char * cur)917 static inline const char *of_prop_next_string(struct property *prop,
918 		const char *cur)
919 {
920 	return NULL;
921 }
922 
of_node_check_flag(struct device_node * n,unsigned long flag)923 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
924 {
925 	return 0;
926 }
927 
of_node_test_and_set_flag(struct device_node * n,unsigned long flag)928 static inline int of_node_test_and_set_flag(struct device_node *n,
929 					    unsigned long flag)
930 {
931 	return 0;
932 }
933 
of_node_set_flag(struct device_node * n,unsigned long flag)934 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
935 {
936 }
937 
of_node_clear_flag(struct device_node * n,unsigned long flag)938 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
939 {
940 }
941 
of_property_check_flag(struct property * p,unsigned long flag)942 static inline int of_property_check_flag(struct property *p, unsigned long flag)
943 {
944 	return 0;
945 }
946 
of_property_set_flag(struct property * p,unsigned long flag)947 static inline void of_property_set_flag(struct property *p, unsigned long flag)
948 {
949 }
950 
of_property_clear_flag(struct property * p,unsigned long flag)951 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
952 {
953 }
954 
of_cpu_node_to_id(struct device_node * np)955 static inline int of_cpu_node_to_id(struct device_node *np)
956 {
957 	return -ENODEV;
958 }
959 
960 #define of_match_ptr(_ptr)	NULL
961 #define of_match_node(_matches, _node)	NULL
962 #endif /* CONFIG_OF */
963 
964 /* Default string compare functions, Allow arch asm/prom.h to override */
965 #if !defined(of_compat_cmp)
966 #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
967 #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
968 #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
969 #endif
970 
of_prop_val_eq(struct property * p1,struct property * p2)971 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
972 {
973 	return p1->length == p2->length &&
974 	       !memcmp(p1->value, p2->value, (size_t)p1->length);
975 }
976 
977 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
978 extern int of_node_to_nid(struct device_node *np);
979 #else
of_node_to_nid(struct device_node * device)980 static inline int of_node_to_nid(struct device_node *device)
981 {
982 	return NUMA_NO_NODE;
983 }
984 #endif
985 
986 #ifdef CONFIG_OF_NUMA
987 extern int of_numa_init(void);
988 #else
of_numa_init(void)989 static inline int of_numa_init(void)
990 {
991 	return -ENOSYS;
992 }
993 #endif
994 
of_find_matching_node(struct device_node * from,const struct of_device_id * matches)995 static inline struct device_node *of_find_matching_node(
996 	struct device_node *from,
997 	const struct of_device_id *matches)
998 {
999 	return of_find_matching_node_and_match(from, matches, NULL);
1000 }
1001 
of_node_get_device_type(const struct device_node * np)1002 static inline const char *of_node_get_device_type(const struct device_node *np)
1003 {
1004 	return of_get_property(np, "device_type", NULL);
1005 }
1006 
of_node_is_type(const struct device_node * np,const char * type)1007 static inline bool of_node_is_type(const struct device_node *np, const char *type)
1008 {
1009 	const char *match = of_node_get_device_type(np);
1010 
1011 	return np && match && type && !strcmp(match, type);
1012 }
1013 
1014 /**
1015  * of_property_count_u8_elems - Count the number of u8 elements in a property
1016  *
1017  * @np:		device node from which the property value is to be read.
1018  * @propname:	name of the property to be searched.
1019  *
1020  * Search for a property in a device node and count the number of u8 elements
1021  * in it. Returns number of elements on sucess, -EINVAL if the property does
1022  * not exist or its length does not match a multiple of u8 and -ENODATA if the
1023  * property does not have a value.
1024  */
of_property_count_u8_elems(const struct device_node * np,const char * propname)1025 static inline int of_property_count_u8_elems(const struct device_node *np,
1026 				const char *propname)
1027 {
1028 	return of_property_count_elems_of_size(np, propname, sizeof(u8));
1029 }
1030 
1031 /**
1032  * of_property_count_u16_elems - Count the number of u16 elements in a property
1033  *
1034  * @np:		device node from which the property value is to be read.
1035  * @propname:	name of the property to be searched.
1036  *
1037  * Search for a property in a device node and count the number of u16 elements
1038  * in it. Returns number of elements on sucess, -EINVAL if the property does
1039  * not exist or its length does not match a multiple of u16 and -ENODATA if the
1040  * property does not have a value.
1041  */
of_property_count_u16_elems(const struct device_node * np,const char * propname)1042 static inline int of_property_count_u16_elems(const struct device_node *np,
1043 				const char *propname)
1044 {
1045 	return of_property_count_elems_of_size(np, propname, sizeof(u16));
1046 }
1047 
1048 /**
1049  * of_property_count_u32_elems - Count the number of u32 elements in a property
1050  *
1051  * @np:		device node from which the property value is to be read.
1052  * @propname:	name of the property to be searched.
1053  *
1054  * Search for a property in a device node and count the number of u32 elements
1055  * in it. Returns number of elements on sucess, -EINVAL if the property does
1056  * not exist or its length does not match a multiple of u32 and -ENODATA if the
1057  * property does not have a value.
1058  */
of_property_count_u32_elems(const struct device_node * np,const char * propname)1059 static inline int of_property_count_u32_elems(const struct device_node *np,
1060 				const char *propname)
1061 {
1062 	return of_property_count_elems_of_size(np, propname, sizeof(u32));
1063 }
1064 
1065 /**
1066  * of_property_count_u64_elems - Count the number of u64 elements in a property
1067  *
1068  * @np:		device node from which the property value is to be read.
1069  * @propname:	name of the property to be searched.
1070  *
1071  * Search for a property in a device node and count the number of u64 elements
1072  * in it. Returns number of elements on sucess, -EINVAL if the property does
1073  * not exist or its length does not match a multiple of u64 and -ENODATA if the
1074  * property does not have a value.
1075  */
of_property_count_u64_elems(const struct device_node * np,const char * propname)1076 static inline int of_property_count_u64_elems(const struct device_node *np,
1077 				const char *propname)
1078 {
1079 	return of_property_count_elems_of_size(np, propname, sizeof(u64));
1080 }
1081 
1082 /**
1083  * of_property_read_string_array() - Read an array of strings from a multiple
1084  * strings property.
1085  * @np:		device node from which the property value is to be read.
1086  * @propname:	name of the property to be searched.
1087  * @out_strs:	output array of string pointers.
1088  * @sz:		number of array elements to read.
1089  *
1090  * Search for a property in a device tree node and retrieve a list of
1091  * terminated string values (pointer to data, not a copy) in that property.
1092  *
1093  * If @out_strs is NULL, the number of strings in the property is returned.
1094  */
of_property_read_string_array(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz)1095 static inline int of_property_read_string_array(const struct device_node *np,
1096 						const char *propname, const char **out_strs,
1097 						size_t sz)
1098 {
1099 	return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1100 }
1101 
1102 /**
1103  * of_property_count_strings() - Find and return the number of strings from a
1104  * multiple strings property.
1105  * @np:		device node from which the property value is to be read.
1106  * @propname:	name of the property to be searched.
1107  *
1108  * Search for a property in a device tree node and retrieve the number of null
1109  * terminated string contain in it. Returns the number of strings on
1110  * success, -EINVAL if the property does not exist, -ENODATA if property
1111  * does not have a value, and -EILSEQ if the string is not null-terminated
1112  * within the length of the property data.
1113  */
of_property_count_strings(const struct device_node * np,const char * propname)1114 static inline int of_property_count_strings(const struct device_node *np,
1115 					    const char *propname)
1116 {
1117 	return of_property_read_string_helper(np, propname, NULL, 0, 0);
1118 }
1119 
1120 /**
1121  * of_property_read_string_index() - Find and read a string from a multiple
1122  * strings property.
1123  * @np:		device node from which the property value is to be read.
1124  * @propname:	name of the property to be searched.
1125  * @index:	index of the string in the list of strings
1126  * @out_string:	pointer to null terminated return string, modified only if
1127  *		return value is 0.
1128  *
1129  * Search for a property in a device tree node and retrieve a null
1130  * terminated string value (pointer to data, not a copy) in the list of strings
1131  * contained in that property.
1132  * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1133  * property does not have a value, and -EILSEQ if the string is not
1134  * null-terminated within the length of the property data.
1135  *
1136  * The out_string pointer is modified only if a valid string can be decoded.
1137  */
of_property_read_string_index(const struct device_node * np,const char * propname,int index,const char ** output)1138 static inline int of_property_read_string_index(const struct device_node *np,
1139 						const char *propname,
1140 						int index, const char **output)
1141 {
1142 	int rc = of_property_read_string_helper(np, propname, output, 1, index);
1143 	return rc < 0 ? rc : 0;
1144 }
1145 
1146 /**
1147  * of_property_read_bool - Findfrom a property
1148  * @np:		device node from which the property value is to be read.
1149  * @propname:	name of the property to be searched.
1150  *
1151  * Search for a property in a device node.
1152  * Returns true if the property exists false otherwise.
1153  */
of_property_read_bool(const struct device_node * np,const char * propname)1154 static inline bool of_property_read_bool(const struct device_node *np,
1155 					 const char *propname)
1156 {
1157 	struct property *prop = of_find_property(np, propname, NULL);
1158 
1159 	return prop ? true : false;
1160 }
1161 
of_property_read_u8(const struct device_node * np,const char * propname,u8 * out_value)1162 static inline int of_property_read_u8(const struct device_node *np,
1163 				       const char *propname,
1164 				       u8 *out_value)
1165 {
1166 	return of_property_read_u8_array(np, propname, out_value, 1);
1167 }
1168 
of_property_read_u16(const struct device_node * np,const char * propname,u16 * out_value)1169 static inline int of_property_read_u16(const struct device_node *np,
1170 				       const char *propname,
1171 				       u16 *out_value)
1172 {
1173 	return of_property_read_u16_array(np, propname, out_value, 1);
1174 }
1175 
of_property_read_u32(const struct device_node * np,const char * propname,u32 * out_value)1176 static inline int of_property_read_u32(const struct device_node *np,
1177 				       const char *propname,
1178 				       u32 *out_value)
1179 {
1180 	return of_property_read_u32_array(np, propname, out_value, 1);
1181 }
1182 
of_property_read_s32(const struct device_node * np,const char * propname,s32 * out_value)1183 static inline int of_property_read_s32(const struct device_node *np,
1184 				       const char *propname,
1185 				       s32 *out_value)
1186 {
1187 	return of_property_read_u32(np, propname, (u32*) out_value);
1188 }
1189 
1190 #define of_for_each_phandle(it, err, np, ln, cn, cc)			\
1191 	for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)),	\
1192 	     err = of_phandle_iterator_next(it);			\
1193 	     err == 0;							\
1194 	     err = of_phandle_iterator_next(it))
1195 
1196 #define of_property_for_each_u32(np, propname, prop, p, u)	\
1197 	for (prop = of_find_property(np, propname, NULL),	\
1198 		p = of_prop_next_u32(prop, NULL, &u);		\
1199 		p;						\
1200 		p = of_prop_next_u32(prop, p, &u))
1201 
1202 #define of_property_for_each_string(np, propname, prop, s)	\
1203 	for (prop = of_find_property(np, propname, NULL),	\
1204 		s = of_prop_next_string(prop, NULL);		\
1205 		s;						\
1206 		s = of_prop_next_string(prop, s))
1207 
1208 #define for_each_node_by_name(dn, name) \
1209 	for (dn = of_find_node_by_name(NULL, name); dn; \
1210 	     dn = of_find_node_by_name(dn, name))
1211 #define for_each_node_by_type(dn, type) \
1212 	for (dn = of_find_node_by_type(NULL, type); dn; \
1213 	     dn = of_find_node_by_type(dn, type))
1214 #define for_each_compatible_node(dn, type, compatible) \
1215 	for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1216 	     dn = of_find_compatible_node(dn, type, compatible))
1217 #define for_each_matching_node(dn, matches) \
1218 	for (dn = of_find_matching_node(NULL, matches); dn; \
1219 	     dn = of_find_matching_node(dn, matches))
1220 #define for_each_matching_node_and_match(dn, matches, match) \
1221 	for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1222 	     dn; dn = of_find_matching_node_and_match(dn, matches, match))
1223 
1224 #define for_each_child_of_node(parent, child) \
1225 	for (child = of_get_next_child(parent, NULL); child != NULL; \
1226 	     child = of_get_next_child(parent, child))
1227 #define for_each_available_child_of_node(parent, child) \
1228 	for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1229 	     child = of_get_next_available_child(parent, child))
1230 
1231 #define for_each_node_with_property(dn, prop_name) \
1232 	for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1233 	     dn = of_find_node_with_property(dn, prop_name))
1234 
of_get_child_count(const struct device_node * np)1235 static inline int of_get_child_count(const struct device_node *np)
1236 {
1237 	struct device_node *child;
1238 	int num = 0;
1239 
1240 	for_each_child_of_node(np, child)
1241 		num++;
1242 
1243 	return num;
1244 }
1245 
of_get_available_child_count(const struct device_node * np)1246 static inline int of_get_available_child_count(const struct device_node *np)
1247 {
1248 	struct device_node *child;
1249 	int num = 0;
1250 
1251 	for_each_available_child_of_node(np, child)
1252 		num++;
1253 
1254 	return num;
1255 }
1256 
1257 #if defined(CONFIG_OF) && !defined(MODULE)
1258 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1259 	static const struct of_device_id __of_table_##name		\
1260 		__used __section(__##table##_of_table)			\
1261 		__aligned(__alignof__(struct of_device_id))		\
1262 		 = { .compatible = compat,				\
1263 		     .data = (fn == (fn_type)NULL) ? fn : fn  }
1264 #else
1265 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1266 	static const struct of_device_id __of_table_##name		\
1267 		__attribute__((unused))					\
1268 		 = { .compatible = compat,				\
1269 		     .data = (fn == (fn_type)NULL) ? fn : fn }
1270 #endif
1271 
1272 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1273 typedef int (*of_init_fn_1_ret)(struct device_node *);
1274 typedef void (*of_init_fn_1)(struct device_node *);
1275 
1276 #define OF_DECLARE_1(table, name, compat, fn) \
1277 		_OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1278 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1279 		_OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1280 #define OF_DECLARE_2(table, name, compat, fn) \
1281 		_OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1282 
1283 /**
1284  * struct of_changeset_entry	- Holds a changeset entry
1285  *
1286  * @node:	list_head for the log list
1287  * @action:	notifier action
1288  * @np:		pointer to the device node affected
1289  * @prop:	pointer to the property affected
1290  * @old_prop:	hold a pointer to the original property
1291  *
1292  * Every modification of the device tree during a changeset
1293  * is held in a list of of_changeset_entry structures.
1294  * That way we can recover from a partial application, or we can
1295  * revert the changeset
1296  */
1297 struct of_changeset_entry {
1298 	struct list_head node;
1299 	unsigned long action;
1300 	struct device_node *np;
1301 	struct property *prop;
1302 	struct property *old_prop;
1303 };
1304 
1305 /**
1306  * struct of_changeset - changeset tracker structure
1307  *
1308  * @entries:	list_head for the changeset entries
1309  *
1310  * changesets are a convenient way to apply bulk changes to the
1311  * live tree. In case of an error, changes are rolled-back.
1312  * changesets live on after initial application, and if not
1313  * destroyed after use, they can be reverted in one single call.
1314  */
1315 struct of_changeset {
1316 	struct list_head entries;
1317 };
1318 
1319 enum of_reconfig_change {
1320 	OF_RECONFIG_NO_CHANGE = 0,
1321 	OF_RECONFIG_CHANGE_ADD,
1322 	OF_RECONFIG_CHANGE_REMOVE,
1323 };
1324 
1325 #ifdef CONFIG_OF_DYNAMIC
1326 extern int of_reconfig_notifier_register(struct notifier_block *);
1327 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1328 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1329 extern int of_reconfig_get_state_change(unsigned long action,
1330 					struct of_reconfig_data *arg);
1331 
1332 extern void of_changeset_init(struct of_changeset *ocs);
1333 extern void of_changeset_destroy(struct of_changeset *ocs);
1334 extern int of_changeset_apply(struct of_changeset *ocs);
1335 extern int of_changeset_revert(struct of_changeset *ocs);
1336 extern int of_changeset_action(struct of_changeset *ocs,
1337 		unsigned long action, struct device_node *np,
1338 		struct property *prop);
1339 
of_changeset_attach_node(struct of_changeset * ocs,struct device_node * np)1340 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1341 		struct device_node *np)
1342 {
1343 	return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1344 }
1345 
of_changeset_detach_node(struct of_changeset * ocs,struct device_node * np)1346 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1347 		struct device_node *np)
1348 {
1349 	return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1350 }
1351 
of_changeset_add_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1352 static inline int of_changeset_add_property(struct of_changeset *ocs,
1353 		struct device_node *np, struct property *prop)
1354 {
1355 	return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1356 }
1357 
of_changeset_remove_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1358 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1359 		struct device_node *np, struct property *prop)
1360 {
1361 	return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1362 }
1363 
of_changeset_update_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1364 static inline int of_changeset_update_property(struct of_changeset *ocs,
1365 		struct device_node *np, struct property *prop)
1366 {
1367 	return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1368 }
1369 #else /* CONFIG_OF_DYNAMIC */
of_reconfig_notifier_register(struct notifier_block * nb)1370 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1371 {
1372 	return -EINVAL;
1373 }
of_reconfig_notifier_unregister(struct notifier_block * nb)1374 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1375 {
1376 	return -EINVAL;
1377 }
of_reconfig_notify(unsigned long action,struct of_reconfig_data * arg)1378 static inline int of_reconfig_notify(unsigned long action,
1379 				     struct of_reconfig_data *arg)
1380 {
1381 	return -EINVAL;
1382 }
of_reconfig_get_state_change(unsigned long action,struct of_reconfig_data * arg)1383 static inline int of_reconfig_get_state_change(unsigned long action,
1384 						struct of_reconfig_data *arg)
1385 {
1386 	return -EINVAL;
1387 }
1388 #endif /* CONFIG_OF_DYNAMIC */
1389 
1390 /**
1391  * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1392  * @np: Pointer to the given device_node
1393  *
1394  * return true if present false otherwise
1395  */
of_device_is_system_power_controller(const struct device_node * np)1396 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1397 {
1398 	return of_property_read_bool(np, "system-power-controller");
1399 }
1400 
1401 /**
1402  * Overlay support
1403  */
1404 
1405 enum of_overlay_notify_action {
1406 	OF_OVERLAY_PRE_APPLY = 0,
1407 	OF_OVERLAY_POST_APPLY,
1408 	OF_OVERLAY_PRE_REMOVE,
1409 	OF_OVERLAY_POST_REMOVE,
1410 };
1411 
1412 struct of_overlay_notify_data {
1413 	struct device_node *overlay;
1414 	struct device_node *target;
1415 };
1416 
1417 #ifdef CONFIG_OF_OVERLAY
1418 
1419 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1420 			 int *ovcs_id);
1421 int of_overlay_remove(int *ovcs_id);
1422 int of_overlay_remove_all(void);
1423 
1424 int of_overlay_notifier_register(struct notifier_block *nb);
1425 int of_overlay_notifier_unregister(struct notifier_block *nb);
1426 
1427 #else
1428 
of_overlay_fdt_apply(void * overlay_fdt,u32 overlay_fdt_size,int * ovcs_id)1429 static inline int of_overlay_fdt_apply(void *overlay_fdt, u32 overlay_fdt_size,
1430 				       int *ovcs_id)
1431 {
1432 	return -ENOTSUPP;
1433 }
1434 
of_overlay_remove(int * ovcs_id)1435 static inline int of_overlay_remove(int *ovcs_id)
1436 {
1437 	return -ENOTSUPP;
1438 }
1439 
of_overlay_remove_all(void)1440 static inline int of_overlay_remove_all(void)
1441 {
1442 	return -ENOTSUPP;
1443 }
1444 
of_overlay_notifier_register(struct notifier_block * nb)1445 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1446 {
1447 	return 0;
1448 }
1449 
of_overlay_notifier_unregister(struct notifier_block * nb)1450 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1451 {
1452 	return 0;
1453 }
1454 
1455 #endif
1456 
1457 #endif /* _LINUX_OF_H */
1458