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