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