xref: /linux/include/linux/of.h (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
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_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_available_child(const struct device_node * node,struct device_node * prev)553  static inline struct device_node *of_get_next_available_child(
554  	const struct device_node *node, struct device_node *prev)
555  {
556  	return NULL;
557  }
558  
of_get_next_reserved_child(const struct device_node * node,struct device_node * prev)559  static inline struct device_node *of_get_next_reserved_child(
560  	const struct device_node *node, struct device_node *prev)
561  {
562  	return NULL;
563  }
564  
of_find_node_with_property(struct device_node * from,const char * prop_name)565  static inline struct device_node *of_find_node_with_property(
566  	struct device_node *from, const char *prop_name)
567  {
568  	return NULL;
569  }
570  
571  #define of_fwnode_handle(node) NULL
572  
of_get_compatible_child(const struct device_node * parent,const char * compatible)573  static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
574  					const char *compatible)
575  {
576  	return NULL;
577  }
578  
of_get_child_by_name(const struct device_node * node,const char * name)579  static inline struct device_node *of_get_child_by_name(
580  					const struct device_node *node,
581  					const char *name)
582  {
583  	return NULL;
584  }
585  
of_get_available_child_by_name(const struct device_node * node,const char * name)586  static inline struct device_node *of_get_available_child_by_name(
587  					const struct device_node *node,
588  					const char *name)
589  {
590  	return NULL;
591  }
592  
of_device_is_compatible(const struct device_node * device,const char * name)593  static inline int of_device_is_compatible(const struct device_node *device,
594  					  const char *name)
595  {
596  	return 0;
597  }
598  
of_device_compatible_match(const struct device_node * device,const char * const * compat)599  static inline  int of_device_compatible_match(const struct device_node *device,
600  					      const char *const *compat)
601  {
602  	return 0;
603  }
604  
of_device_is_available(const struct device_node * device)605  static inline bool of_device_is_available(const struct device_node *device)
606  {
607  	return false;
608  }
609  
of_device_is_big_endian(const struct device_node * device)610  static inline bool of_device_is_big_endian(const struct device_node *device)
611  {
612  	return false;
613  }
614  
of_find_property(const struct device_node * np,const char * name,int * lenp)615  static inline struct property *of_find_property(const struct device_node *np,
616  						const char *name,
617  						int *lenp)
618  {
619  	return NULL;
620  }
621  
of_find_compatible_node(struct device_node * from,const char * type,const char * compat)622  static inline struct device_node *of_find_compatible_node(
623  						struct device_node *from,
624  						const char *type,
625  						const char *compat)
626  {
627  	return NULL;
628  }
629  
of_property_read_bool(const struct device_node * np,const char * propname)630  static inline bool of_property_read_bool(const struct device_node *np,
631  					const char *propname)
632  {
633  	return false;
634  }
635  
of_property_count_elems_of_size(const struct device_node * np,const char * propname,int elem_size)636  static inline int of_property_count_elems_of_size(const struct device_node *np,
637  			const char *propname, int elem_size)
638  {
639  	return -ENOSYS;
640  }
641  
of_property_read_u16_index(const struct device_node * np,const char * propname,u32 index,u16 * out_value)642  static inline int of_property_read_u16_index(const struct device_node *np,
643  			const char *propname, u32 index, u16 *out_value)
644  {
645  	return -ENOSYS;
646  }
647  
of_property_read_u32_index(const struct device_node * np,const char * propname,u32 index,u32 * out_value)648  static inline int of_property_read_u32_index(const struct device_node *np,
649  			const char *propname, u32 index, u32 *out_value)
650  {
651  	return -ENOSYS;
652  }
653  
of_property_read_u64_index(const struct device_node * np,const char * propname,u32 index,u64 * out_value)654  static inline int of_property_read_u64_index(const struct device_node *np,
655  			const char *propname, u32 index, u64 *out_value)
656  {
657  	return -ENOSYS;
658  }
659  
of_get_property(const struct device_node * node,const char * name,int * lenp)660  static inline const void *of_get_property(const struct device_node *node,
661  				const char *name,
662  				int *lenp)
663  {
664  	return NULL;
665  }
666  
of_get_cpu_node(int cpu,unsigned int * thread)667  static inline struct device_node *of_get_cpu_node(int cpu,
668  					unsigned int *thread)
669  {
670  	return NULL;
671  }
672  
of_cpu_device_node_get(int cpu)673  static inline struct device_node *of_cpu_device_node_get(int cpu)
674  {
675  	return NULL;
676  }
677  
of_cpu_node_to_id(struct device_node * np)678  static inline int of_cpu_node_to_id(struct device_node *np)
679  {
680  	return -ENODEV;
681  }
682  
of_get_next_cpu_node(struct device_node * prev)683  static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
684  {
685  	return NULL;
686  }
687  
of_get_cpu_state_node(struct device_node * cpu_node,int index)688  static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
689  					int index)
690  {
691  	return NULL;
692  }
693  
of_n_addr_cells(struct device_node * np)694  static inline int of_n_addr_cells(struct device_node *np)
695  {
696  	return 0;
697  
698  }
of_n_size_cells(struct device_node * np)699  static inline int of_n_size_cells(struct device_node *np)
700  {
701  	return 0;
702  }
703  
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)704  static inline int of_property_read_variable_u8_array(const struct device_node *np,
705  					const char *propname, u8 *out_values,
706  					size_t sz_min, size_t sz_max)
707  {
708  	return -ENOSYS;
709  }
710  
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)711  static inline int of_property_read_variable_u16_array(const struct device_node *np,
712  					const char *propname, u16 *out_values,
713  					size_t sz_min, size_t sz_max)
714  {
715  	return -ENOSYS;
716  }
717  
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)718  static inline int of_property_read_variable_u32_array(const struct device_node *np,
719  					const char *propname,
720  					u32 *out_values,
721  					size_t sz_min,
722  					size_t sz_max)
723  {
724  	return -ENOSYS;
725  }
726  
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)727  static inline int of_property_read_u64(const struct device_node *np,
728  				       const char *propname, u64 *out_value)
729  {
730  	return -ENOSYS;
731  }
732  
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)733  static inline int of_property_read_variable_u64_array(const struct device_node *np,
734  					const char *propname,
735  					u64 *out_values,
736  					size_t sz_min,
737  					size_t sz_max)
738  {
739  	return -ENOSYS;
740  }
741  
of_property_read_string(const struct device_node * np,const char * propname,const char ** out_string)742  static inline int of_property_read_string(const struct device_node *np,
743  					  const char *propname,
744  					  const char **out_string)
745  {
746  	return -ENOSYS;
747  }
748  
of_property_match_string(const struct device_node * np,const char * propname,const char * string)749  static inline int of_property_match_string(const struct device_node *np,
750  					   const char *propname,
751  					   const char *string)
752  {
753  	return -ENOSYS;
754  }
755  
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int index)756  static inline int of_property_read_string_helper(const struct device_node *np,
757  						 const char *propname,
758  						 const char **out_strs, size_t sz, int index)
759  {
760  	return -ENOSYS;
761  }
762  
__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)763  static inline int __of_parse_phandle_with_args(const struct device_node *np,
764  					       const char *list_name,
765  					       const char *cells_name,
766  					       int cell_count,
767  					       int index,
768  					       struct of_phandle_args *out_args)
769  {
770  	return -ENOSYS;
771  }
772  
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)773  static inline int of_parse_phandle_with_args_map(const struct device_node *np,
774  						 const char *list_name,
775  						 const char *stem_name,
776  						 int index,
777  						 struct of_phandle_args *out_args)
778  {
779  	return -ENOSYS;
780  }
781  
of_count_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name)782  static inline int of_count_phandle_with_args(const struct device_node *np,
783  					     const char *list_name,
784  					     const char *cells_name)
785  {
786  	return -ENOSYS;
787  }
788  
of_modalias(const struct device_node * np,char * str,ssize_t len)789  static inline ssize_t of_modalias(const struct device_node *np, char *str,
790  				  ssize_t len)
791  {
792  	return -ENODEV;
793  }
794  
of_request_module(const struct device_node * np)795  static inline int of_request_module(const struct device_node *np)
796  {
797  	return -ENODEV;
798  }
799  
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)800  static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
801  					   const struct device_node *np,
802  					   const char *list_name,
803  					   const char *cells_name,
804  					   int cell_count)
805  {
806  	return -ENOSYS;
807  }
808  
of_phandle_iterator_next(struct of_phandle_iterator * it)809  static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
810  {
811  	return -ENOSYS;
812  }
813  
of_phandle_iterator_args(struct of_phandle_iterator * it,uint32_t * args,int size)814  static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
815  					   uint32_t *args,
816  					   int size)
817  {
818  	return 0;
819  }
820  
of_alias_get_id(struct device_node * np,const char * stem)821  static inline int of_alias_get_id(struct device_node *np, const char *stem)
822  {
823  	return -ENOSYS;
824  }
825  
of_alias_get_highest_id(const char * stem)826  static inline int of_alias_get_highest_id(const char *stem)
827  {
828  	return -ENOSYS;
829  }
830  
of_machine_is_compatible(const char * compat)831  static inline int of_machine_is_compatible(const char *compat)
832  {
833  	return 0;
834  }
835  
of_add_property(struct device_node * np,struct property * prop)836  static inline int of_add_property(struct device_node *np, struct property *prop)
837  {
838  	return 0;
839  }
840  
of_remove_property(struct device_node * np,struct property * prop)841  static inline int of_remove_property(struct device_node *np, struct property *prop)
842  {
843  	return 0;
844  }
845  
of_machine_compatible_match(const char * const * compats)846  static inline bool of_machine_compatible_match(const char *const *compats)
847  {
848  	return false;
849  }
850  
of_console_check(const struct device_node * dn,const char * name,int index)851  static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
852  {
853  	return false;
854  }
855  
of_prop_next_u32(const struct property * prop,const __be32 * cur,u32 * pu)856  static inline const __be32 *of_prop_next_u32(const struct property *prop,
857  		const __be32 *cur, u32 *pu)
858  {
859  	return NULL;
860  }
861  
of_prop_next_string(const struct property * prop,const char * cur)862  static inline const char *of_prop_next_string(const struct property *prop,
863  		const char *cur)
864  {
865  	return NULL;
866  }
867  
of_node_check_flag(struct device_node * n,unsigned long flag)868  static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
869  {
870  	return 0;
871  }
872  
of_node_test_and_set_flag(struct device_node * n,unsigned long flag)873  static inline int of_node_test_and_set_flag(struct device_node *n,
874  					    unsigned long flag)
875  {
876  	return 0;
877  }
878  
of_node_set_flag(struct device_node * n,unsigned long flag)879  static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
880  {
881  }
882  
of_node_clear_flag(struct device_node * n,unsigned long flag)883  static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
884  {
885  }
886  
of_property_check_flag(const struct property * p,unsigned long flag)887  static inline int of_property_check_flag(const struct property *p,
888  					 unsigned long flag)
889  {
890  	return 0;
891  }
892  
of_property_set_flag(struct property * p,unsigned long flag)893  static inline void of_property_set_flag(struct property *p, unsigned long flag)
894  {
895  }
896  
of_property_clear_flag(struct property * p,unsigned long flag)897  static inline void of_property_clear_flag(struct property *p, unsigned long flag)
898  {
899  }
900  
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)901  static inline int of_map_id(const struct device_node *np, u32 id,
902  			     const char *map_name, const char *map_mask_name,
903  			     struct device_node **target, u32 *id_out)
904  {
905  	return -EINVAL;
906  }
907  
of_dma_get_max_cpu_address(struct device_node * np)908  static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
909  {
910  	return PHYS_ADDR_MAX;
911  }
912  
of_device_get_match_data(const struct device * dev)913  static inline const void *of_device_get_match_data(const struct device *dev)
914  {
915  	return NULL;
916  }
917  
918  #define of_match_ptr(_ptr)	NULL
919  #define of_match_node(_matches, _node)	NULL
920  #endif /* CONFIG_OF */
921  
922  /* Default string compare functions, Allow arch asm/prom.h to override */
923  #if !defined(of_compat_cmp)
924  #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
925  #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
926  #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
927  #endif
928  
of_prop_val_eq(const struct property * p1,const struct property * p2)929  static inline int of_prop_val_eq(const struct property *p1, const struct property *p2)
930  {
931  	return p1->length == p2->length &&
932  	       !memcmp(p1->value, p2->value, (size_t)p1->length);
933  }
934  
935  #define for_each_property_of_node(dn, pp) \
936  	for (pp = dn->properties; pp != NULL; pp = pp->next)
937  
938  #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
939  extern int of_node_to_nid(struct device_node *np);
940  #else
of_node_to_nid(struct device_node * device)941  static inline int of_node_to_nid(struct device_node *device)
942  {
943  	return NUMA_NO_NODE;
944  }
945  #endif
946  
947  #ifdef CONFIG_OF_NUMA
948  extern int of_numa_init(void);
949  #else
of_numa_init(void)950  static inline int of_numa_init(void)
951  {
952  	return -ENOSYS;
953  }
954  #endif
955  
of_find_matching_node(struct device_node * from,const struct of_device_id * matches)956  static inline struct device_node *of_find_matching_node(
957  	struct device_node *from,
958  	const struct of_device_id *matches)
959  {
960  	return of_find_matching_node_and_match(from, matches, NULL);
961  }
962  
of_node_get_device_type(const struct device_node * np)963  static inline const char *of_node_get_device_type(const struct device_node *np)
964  {
965  	return of_get_property(np, "device_type", NULL);
966  }
967  
of_node_is_type(const struct device_node * np,const char * type)968  static inline bool of_node_is_type(const struct device_node *np, const char *type)
969  {
970  	const char *match = of_node_get_device_type(np);
971  
972  	return np && match && type && !strcmp(match, type);
973  }
974  
975  /**
976   * of_parse_phandle - Resolve a phandle property to a device_node pointer
977   * @np: Pointer to device node holding phandle property
978   * @phandle_name: Name of property holding a phandle value
979   * @index: For properties holding a table of phandles, this is the index into
980   *         the table
981   *
982   * Return: The device_node pointer with refcount incremented.  Use
983   * of_node_put() on it when done.
984   */
of_parse_phandle(const struct device_node * np,const char * phandle_name,int index)985  static inline struct device_node *of_parse_phandle(const struct device_node *np,
986  						   const char *phandle_name,
987  						   int index)
988  {
989  	struct of_phandle_args args;
990  
991  	if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
992  					 index, &args))
993  		return NULL;
994  
995  	return args.np;
996  }
997  
998  /**
999   * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1000   * @np:		pointer to a device tree node containing a list
1001   * @list_name:	property name that contains a list
1002   * @cells_name:	property name that specifies phandles' arguments count
1003   * @index:	index of a phandle to parse out
1004   * @out_args:	optional pointer to output arguments structure (will be filled)
1005   *
1006   * This function is useful to parse lists of phandles and their arguments.
1007   * Returns 0 on success and fills out_args, on error returns appropriate
1008   * errno value.
1009   *
1010   * Caller is responsible to call of_node_put() on the returned out_args->np
1011   * pointer.
1012   *
1013   * Example::
1014   *
1015   *  phandle1: node1 {
1016   *	#list-cells = <2>;
1017   *  };
1018   *
1019   *  phandle2: node2 {
1020   *	#list-cells = <1>;
1021   *  };
1022   *
1023   *  node3 {
1024   *	list = <&phandle1 1 2 &phandle2 3>;
1025   *  };
1026   *
1027   * To get a device_node of the ``node2`` node you may call this:
1028   * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1029   */
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)1030  static inline int of_parse_phandle_with_args(const struct device_node *np,
1031  					     const char *list_name,
1032  					     const char *cells_name,
1033  					     int index,
1034  					     struct of_phandle_args *out_args)
1035  {
1036  	int cell_count = -1;
1037  
1038  	/* If cells_name is NULL we assume a cell count of 0 */
1039  	if (!cells_name)
1040  		cell_count = 0;
1041  
1042  	return __of_parse_phandle_with_args(np, list_name, cells_name,
1043  					    cell_count, index, out_args);
1044  }
1045  
1046  /**
1047   * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1048   * @np:		pointer to a device tree node containing a list
1049   * @list_name:	property name that contains a list
1050   * @cell_count: number of argument cells following the phandle
1051   * @index:	index of a phandle to parse out
1052   * @out_args:	optional pointer to output arguments structure (will be filled)
1053   *
1054   * This function is useful to parse lists of phandles and their arguments.
1055   * Returns 0 on success and fills out_args, on error returns appropriate
1056   * errno value.
1057   *
1058   * Caller is responsible to call of_node_put() on the returned out_args->np
1059   * pointer.
1060   *
1061   * Example::
1062   *
1063   *  phandle1: node1 {
1064   *  };
1065   *
1066   *  phandle2: node2 {
1067   *  };
1068   *
1069   *  node3 {
1070   *	list = <&phandle1 0 2 &phandle2 2 3>;
1071   *  };
1072   *
1073   * To get a device_node of the ``node2`` node you may call this:
1074   * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1075   */
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)1076  static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
1077  						   const char *list_name,
1078  						   int cell_count,
1079  						   int index,
1080  						   struct of_phandle_args *out_args)
1081  {
1082  	return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1083  					    index, out_args);
1084  }
1085  
1086  /**
1087   * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
1088   * @np:		pointer to a device tree node containing a list
1089   * @list_name:	property name that contains a list
1090   * @cells_name:	property name that specifies phandles' arguments count
1091   * @index:	index of a phandle to parse out
1092   * @out_args:	optional pointer to output arguments structure (will be filled)
1093   *
1094   * Same as of_parse_phandle_with_args() except that if the cells_name property
1095   * is not found, cell_count of 0 is assumed.
1096   *
1097   * This is used to useful, if you have a phandle which didn't have arguments
1098   * before and thus doesn't have a '#*-cells' property but is now migrated to
1099   * having arguments while retaining backwards compatibility.
1100   */
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)1101  static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
1102  						      const char *list_name,
1103  						      const char *cells_name,
1104  						      int index,
1105  						      struct of_phandle_args *out_args)
1106  {
1107  	return __of_parse_phandle_with_args(np, list_name, cells_name,
1108  					    0, index, out_args);
1109  }
1110  
1111  /**
1112   * of_phandle_args_equal() - Compare two of_phandle_args
1113   * @a1:		First of_phandle_args to compare
1114   * @a2:		Second of_phandle_args to compare
1115   *
1116   * Return: True if a1 and a2 are the same (same node pointer, same phandle
1117   * args), false otherwise.
1118   */
of_phandle_args_equal(const struct of_phandle_args * a1,const struct of_phandle_args * a2)1119  static inline bool of_phandle_args_equal(const struct of_phandle_args *a1,
1120  					 const struct of_phandle_args *a2)
1121  {
1122  	return a1->np == a2->np &&
1123  	       a1->args_count == a2->args_count &&
1124  	       !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count);
1125  }
1126  
1127  /**
1128   * of_property_count_u8_elems - Count the number of u8 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 u8 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 u8 and -ENODATA if the
1138   * property does not have a value.
1139   */
of_property_count_u8_elems(const struct device_node * np,const char * propname)1140  static inline int of_property_count_u8_elems(const struct device_node *np,
1141  				const char *propname)
1142  {
1143  	return of_property_count_elems_of_size(np, propname, sizeof(u8));
1144  }
1145  
1146  /**
1147   * of_property_count_u16_elems - Count the number of u16 elements in a property
1148   *
1149   * @np:		device node from which the property value is to be read.
1150   * @propname:	name of the property to be searched.
1151   *
1152   * Search for a property in a device node and count the number of u16 elements
1153   * in it.
1154   *
1155   * Return: The number of elements on sucess, -EINVAL if the property does
1156   * not exist or its length does not match a multiple of u16 and -ENODATA if the
1157   * property does not have a value.
1158   */
of_property_count_u16_elems(const struct device_node * np,const char * propname)1159  static inline int of_property_count_u16_elems(const struct device_node *np,
1160  				const char *propname)
1161  {
1162  	return of_property_count_elems_of_size(np, propname, sizeof(u16));
1163  }
1164  
1165  /**
1166   * of_property_count_u32_elems - Count the number of u32 elements in a property
1167   *
1168   * @np:		device node from which the property value is to be read.
1169   * @propname:	name of the property to be searched.
1170   *
1171   * Search for a property in a device node and count the number of u32 elements
1172   * in it.
1173   *
1174   * Return: The number of elements on sucess, -EINVAL if the property does
1175   * not exist or its length does not match a multiple of u32 and -ENODATA if the
1176   * property does not have a value.
1177   */
of_property_count_u32_elems(const struct device_node * np,const char * propname)1178  static inline int of_property_count_u32_elems(const struct device_node *np,
1179  				const char *propname)
1180  {
1181  	return of_property_count_elems_of_size(np, propname, sizeof(u32));
1182  }
1183  
1184  /**
1185   * of_property_count_u64_elems - Count the number of u64 elements in a property
1186   *
1187   * @np:		device node from which the property value is to be read.
1188   * @propname:	name of the property to be searched.
1189   *
1190   * Search for a property in a device node and count the number of u64 elements
1191   * in it.
1192   *
1193   * Return: The number of elements on sucess, -EINVAL if the property does
1194   * not exist or its length does not match a multiple of u64 and -ENODATA if the
1195   * property does not have a value.
1196   */
of_property_count_u64_elems(const struct device_node * np,const char * propname)1197  static inline int of_property_count_u64_elems(const struct device_node *np,
1198  				const char *propname)
1199  {
1200  	return of_property_count_elems_of_size(np, propname, sizeof(u64));
1201  }
1202  
1203  /**
1204   * of_property_read_string_array() - Read an array of strings from a multiple
1205   * strings property.
1206   * @np:		device node from which the property value is to be read.
1207   * @propname:	name of the property to be searched.
1208   * @out_strs:	output array of string pointers.
1209   * @sz:		number of array elements to read.
1210   *
1211   * Search for a property in a device tree node and retrieve a list of
1212   * terminated string values (pointer to data, not a copy) in that property.
1213   *
1214   * Return: If @out_strs is NULL, the number of strings in the property is returned.
1215   */
of_property_read_string_array(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz)1216  static inline int of_property_read_string_array(const struct device_node *np,
1217  						const char *propname, const char **out_strs,
1218  						size_t sz)
1219  {
1220  	return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1221  }
1222  
1223  /**
1224   * of_property_count_strings() - Find and return the number of strings from a
1225   * multiple strings property.
1226   * @np:		device node from which the property value is to be read.
1227   * @propname:	name of the property to be searched.
1228   *
1229   * Search for a property in a device tree node and retrieve the number of null
1230   * terminated string contain in it.
1231   *
1232   * Return: The number of strings on success, -EINVAL if the property does not
1233   * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
1234   * is not null-terminated within the length of the property data.
1235   */
of_property_count_strings(const struct device_node * np,const char * propname)1236  static inline int of_property_count_strings(const struct device_node *np,
1237  					    const char *propname)
1238  {
1239  	return of_property_read_string_helper(np, propname, NULL, 0, 0);
1240  }
1241  
1242  /**
1243   * of_property_read_string_index() - Find and read a string from a multiple
1244   * strings property.
1245   * @np:		device node from which the property value is to be read.
1246   * @propname:	name of the property to be searched.
1247   * @index:	index of the string in the list of strings
1248   * @output:	pointer to null terminated return string, modified only if
1249   *		return value is 0.
1250   *
1251   * Search for a property in a device tree node and retrieve a null
1252   * terminated string value (pointer to data, not a copy) in the list of strings
1253   * contained in that property.
1254   *
1255   * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
1256   * property does not have a value, and -EILSEQ if the string is not
1257   * null-terminated within the length of the property data.
1258   *
1259   * The out_string pointer is modified only if a valid string can be decoded.
1260   */
of_property_read_string_index(const struct device_node * np,const char * propname,int index,const char ** output)1261  static inline int of_property_read_string_index(const struct device_node *np,
1262  						const char *propname,
1263  						int index, const char **output)
1264  {
1265  	int rc = of_property_read_string_helper(np, propname, output, 1, index);
1266  	return rc < 0 ? rc : 0;
1267  }
1268  
1269  /**
1270   * of_property_present - Test if a property is present in a node
1271   * @np:		device node to search for the property.
1272   * @propname:	name of the property to be searched.
1273   *
1274   * Test for a property present in a device node.
1275   *
1276   * Return: true if the property exists false otherwise.
1277   */
of_property_present(const struct device_node * np,const char * propname)1278  static inline bool of_property_present(const struct device_node *np, const char *propname)
1279  {
1280  	struct property *prop = of_find_property(np, propname, NULL);
1281  
1282  	return prop ? true : false;
1283  }
1284  
1285  /**
1286   * of_property_read_u8_array - Find and read an array of u8 from a property.
1287   *
1288   * @np:		device node from which the property value is to be read.
1289   * @propname:	name of the property to be searched.
1290   * @out_values:	pointer to return value, modified only if return value is 0.
1291   * @sz:		number of array elements to read
1292   *
1293   * Search for a property in a device node and read 8-bit value(s) from
1294   * it.
1295   *
1296   * dts entry of array should be like:
1297   *  ``property = /bits/ 8 <0x50 0x60 0x70>;``
1298   *
1299   * Return: 0 on success, -EINVAL if the property does not exist,
1300   * -ENODATA if property does not have a value, and -EOVERFLOW if the
1301   * property data isn't large enough.
1302   *
1303   * The out_values is modified only if a valid u8 value can be decoded.
1304   */
of_property_read_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz)1305  static inline int of_property_read_u8_array(const struct device_node *np,
1306  					    const char *propname,
1307  					    u8 *out_values, size_t sz)
1308  {
1309  	int ret = of_property_read_variable_u8_array(np, propname, out_values,
1310  						     sz, 0);
1311  	if (ret >= 0)
1312  		return 0;
1313  	else
1314  		return ret;
1315  }
1316  
1317  /**
1318   * of_property_read_u16_array - Find and read an array of u16 from a property.
1319   *
1320   * @np:		device node from which the property value is to be read.
1321   * @propname:	name of the property to be searched.
1322   * @out_values:	pointer to return value, modified only if return value is 0.
1323   * @sz:		number of array elements to read
1324   *
1325   * Search for a property in a device node and read 16-bit value(s) from
1326   * it.
1327   *
1328   * dts entry of array should be like:
1329   *  ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1330   *
1331   * Return: 0 on success, -EINVAL if the property does not exist,
1332   * -ENODATA if property does not have a value, and -EOVERFLOW if the
1333   * property data isn't large enough.
1334   *
1335   * The out_values is modified only if a valid u16 value can be decoded.
1336   */
of_property_read_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz)1337  static inline int of_property_read_u16_array(const struct device_node *np,
1338  					     const char *propname,
1339  					     u16 *out_values, size_t sz)
1340  {
1341  	int ret = of_property_read_variable_u16_array(np, propname, out_values,
1342  						      sz, 0);
1343  	if (ret >= 0)
1344  		return 0;
1345  	else
1346  		return ret;
1347  }
1348  
1349  /**
1350   * of_property_read_u32_array - Find and read an array of 32 bit integers
1351   * from a property.
1352   *
1353   * @np:		device node from which the property value is to be read.
1354   * @propname:	name of the property to be searched.
1355   * @out_values:	pointer to return value, modified only if return value is 0.
1356   * @sz:		number of array elements to read
1357   *
1358   * Search for a property in a device node and read 32-bit value(s) from
1359   * it.
1360   *
1361   * Return: 0 on success, -EINVAL if the property does not exist,
1362   * -ENODATA if property does not have a value, and -EOVERFLOW if the
1363   * property data isn't large enough.
1364   *
1365   * The out_values is modified only if a valid u32 value can be decoded.
1366   */
of_property_read_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz)1367  static inline int of_property_read_u32_array(const struct device_node *np,
1368  					     const char *propname,
1369  					     u32 *out_values, size_t sz)
1370  {
1371  	int ret = of_property_read_variable_u32_array(np, propname, out_values,
1372  						      sz, 0);
1373  	if (ret >= 0)
1374  		return 0;
1375  	else
1376  		return ret;
1377  }
1378  
1379  /**
1380   * of_property_read_u64_array - Find and read an array of 64 bit integers
1381   * from a property.
1382   *
1383   * @np:		device node from which the property value is to be read.
1384   * @propname:	name of the property to be searched.
1385   * @out_values:	pointer to return value, modified only if return value is 0.
1386   * @sz:		number of array elements to read
1387   *
1388   * Search for a property in a device node and read 64-bit value(s) from
1389   * it.
1390   *
1391   * Return: 0 on success, -EINVAL if the property does not exist,
1392   * -ENODATA if property does not have a value, and -EOVERFLOW if the
1393   * property data isn't large enough.
1394   *
1395   * The out_values is modified only if a valid u64 value can be decoded.
1396   */
of_property_read_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz)1397  static inline int of_property_read_u64_array(const struct device_node *np,
1398  					     const char *propname,
1399  					     u64 *out_values, size_t sz)
1400  {
1401  	int ret = of_property_read_variable_u64_array(np, propname, out_values,
1402  						      sz, 0);
1403  	if (ret >= 0)
1404  		return 0;
1405  	else
1406  		return ret;
1407  }
1408  
of_property_read_u8(const struct device_node * np,const char * propname,u8 * out_value)1409  static inline int of_property_read_u8(const struct device_node *np,
1410  				       const char *propname,
1411  				       u8 *out_value)
1412  {
1413  	return of_property_read_u8_array(np, propname, out_value, 1);
1414  }
1415  
of_property_read_u16(const struct device_node * np,const char * propname,u16 * out_value)1416  static inline int of_property_read_u16(const struct device_node *np,
1417  				       const char *propname,
1418  				       u16 *out_value)
1419  {
1420  	return of_property_read_u16_array(np, propname, out_value, 1);
1421  }
1422  
of_property_read_u32(const struct device_node * np,const char * propname,u32 * out_value)1423  static inline int of_property_read_u32(const struct device_node *np,
1424  				       const char *propname,
1425  				       u32 *out_value)
1426  {
1427  	return of_property_read_u32_array(np, propname, out_value, 1);
1428  }
1429  
of_property_read_s32(const struct device_node * np,const char * propname,s32 * out_value)1430  static inline int of_property_read_s32(const struct device_node *np,
1431  				       const char *propname,
1432  				       s32 *out_value)
1433  {
1434  	return of_property_read_u32(np, propname, (u32*) out_value);
1435  }
1436  
1437  #define of_for_each_phandle(it, err, np, ln, cn, cc)			\
1438  	for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)),	\
1439  	     err = of_phandle_iterator_next(it);			\
1440  	     err == 0;							\
1441  	     err = of_phandle_iterator_next(it))
1442  
1443  #define of_property_for_each_u32(np, propname, u)			\
1444  	for (struct {const struct property *prop; const __be32 *item; } _it =	\
1445  		{of_find_property(np, propname, NULL),			\
1446  		 of_prop_next_u32(_it.prop, NULL, &u)};			\
1447  	     _it.item;							\
1448  	     _it.item = of_prop_next_u32(_it.prop, _it.item, &u))
1449  
1450  #define of_property_for_each_string(np, propname, prop, s)	\
1451  	for (prop = of_find_property(np, propname, NULL),	\
1452  		s = of_prop_next_string(prop, NULL);		\
1453  		s;						\
1454  		s = of_prop_next_string(prop, s))
1455  
1456  #define for_each_node_by_name(dn, name) \
1457  	for (dn = of_find_node_by_name(NULL, name); dn; \
1458  	     dn = of_find_node_by_name(dn, name))
1459  #define for_each_node_by_type(dn, type) \
1460  	for (dn = of_find_node_by_type(NULL, type); dn; \
1461  	     dn = of_find_node_by_type(dn, type))
1462  #define for_each_compatible_node(dn, type, compatible) \
1463  	for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1464  	     dn = of_find_compatible_node(dn, type, compatible))
1465  #define for_each_matching_node(dn, matches) \
1466  	for (dn = of_find_matching_node(NULL, matches); dn; \
1467  	     dn = of_find_matching_node(dn, matches))
1468  #define for_each_matching_node_and_match(dn, matches, match) \
1469  	for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1470  	     dn; dn = of_find_matching_node_and_match(dn, matches, match))
1471  
1472  #define for_each_child_of_node(parent, child) \
1473  	for (child = of_get_next_child(parent, NULL); child != NULL; \
1474  	     child = of_get_next_child(parent, child))
1475  
1476  #define for_each_child_of_node_scoped(parent, child) \
1477  	for (struct device_node *child __free(device_node) =		\
1478  	     of_get_next_child(parent, NULL);				\
1479  	     child != NULL;						\
1480  	     child = of_get_next_child(parent, child))
1481  
1482  #define for_each_child_of_node_with_prefix(parent, child, prefix)	\
1483  	for (struct device_node *child __free(device_node) =		\
1484  	     of_get_next_child_with_prefix(parent, NULL, prefix);	\
1485  	     child != NULL;						\
1486  	     child = of_get_next_child_with_prefix(parent, child, prefix))
1487  
1488  #define for_each_available_child_of_node(parent, child) \
1489  	for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1490  	     child = of_get_next_available_child(parent, child))
1491  #define for_each_reserved_child_of_node(parent, child)			\
1492  	for (child = of_get_next_reserved_child(parent, NULL); child != NULL; \
1493  	     child = of_get_next_reserved_child(parent, child))
1494  
1495  #define for_each_available_child_of_node_scoped(parent, child) \
1496  	for (struct device_node *child __free(device_node) =		\
1497  	     of_get_next_available_child(parent, NULL);			\
1498  	     child != NULL;						\
1499  	     child = of_get_next_available_child(parent, child))
1500  
1501  #define for_each_of_cpu_node(cpu) \
1502  	for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1503  	     cpu = of_get_next_cpu_node(cpu))
1504  
1505  #define for_each_node_with_property(dn, prop_name) \
1506  	for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1507  	     dn = of_find_node_with_property(dn, prop_name))
1508  
of_get_child_count(const struct device_node * np)1509  static inline int of_get_child_count(const struct device_node *np)
1510  {
1511  	struct device_node *child;
1512  	int num = 0;
1513  
1514  	for_each_child_of_node(np, child)
1515  		num++;
1516  
1517  	return num;
1518  }
1519  
of_get_available_child_count(const struct device_node * np)1520  static inline int of_get_available_child_count(const struct device_node *np)
1521  {
1522  	struct device_node *child;
1523  	int num = 0;
1524  
1525  	for_each_available_child_of_node(np, child)
1526  		num++;
1527  
1528  	return num;
1529  }
1530  
1531  #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type)		\
1532  	static const struct of_device_id __of_table_##name		\
1533  		__attribute__((unused))					\
1534  		 = { .compatible = compat,				\
1535  		     .data = (fn == (fn_type)NULL) ? fn : fn }
1536  
1537  #if defined(CONFIG_OF) && !defined(MODULE)
1538  #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1539  	static const struct of_device_id __of_table_##name		\
1540  		__used __section("__" #table "_of_table")		\
1541  		__aligned(__alignof__(struct of_device_id))		\
1542  		 = { .compatible = compat,				\
1543  		     .data = (fn == (fn_type)NULL) ? fn : fn  }
1544  #else
1545  #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1546  	_OF_DECLARE_STUB(table, name, compat, fn, fn_type)
1547  #endif
1548  
1549  typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1550  typedef int (*of_init_fn_1_ret)(struct device_node *);
1551  typedef void (*of_init_fn_1)(struct device_node *);
1552  
1553  #define OF_DECLARE_1(table, name, compat, fn) \
1554  		_OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1555  #define OF_DECLARE_1_RET(table, name, compat, fn) \
1556  		_OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1557  #define OF_DECLARE_2(table, name, compat, fn) \
1558  		_OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1559  
1560  /**
1561   * struct of_changeset_entry	- Holds a changeset entry
1562   *
1563   * @node:	list_head for the log list
1564   * @action:	notifier action
1565   * @np:		pointer to the device node affected
1566   * @prop:	pointer to the property affected
1567   * @old_prop:	hold a pointer to the original property
1568   *
1569   * Every modification of the device tree during a changeset
1570   * is held in a list of of_changeset_entry structures.
1571   * That way we can recover from a partial application, or we can
1572   * revert the changeset
1573   */
1574  struct of_changeset_entry {
1575  	struct list_head node;
1576  	unsigned long action;
1577  	struct device_node *np;
1578  	struct property *prop;
1579  	struct property *old_prop;
1580  };
1581  
1582  /**
1583   * struct of_changeset - changeset tracker structure
1584   *
1585   * @entries:	list_head for the changeset entries
1586   *
1587   * changesets are a convenient way to apply bulk changes to the
1588   * live tree. In case of an error, changes are rolled-back.
1589   * changesets live on after initial application, and if not
1590   * destroyed after use, they can be reverted in one single call.
1591   */
1592  struct of_changeset {
1593  	struct list_head entries;
1594  };
1595  
1596  enum of_reconfig_change {
1597  	OF_RECONFIG_NO_CHANGE = 0,
1598  	OF_RECONFIG_CHANGE_ADD,
1599  	OF_RECONFIG_CHANGE_REMOVE,
1600  };
1601  
1602  struct notifier_block;
1603  
1604  #ifdef CONFIG_OF_DYNAMIC
1605  extern int of_reconfig_notifier_register(struct notifier_block *);
1606  extern int of_reconfig_notifier_unregister(struct notifier_block *);
1607  extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1608  extern int of_reconfig_get_state_change(unsigned long action,
1609  					struct of_reconfig_data *arg);
1610  
1611  extern void of_changeset_init(struct of_changeset *ocs);
1612  extern void of_changeset_destroy(struct of_changeset *ocs);
1613  extern int of_changeset_apply(struct of_changeset *ocs);
1614  extern int of_changeset_revert(struct of_changeset *ocs);
1615  extern int of_changeset_action(struct of_changeset *ocs,
1616  		unsigned long action, struct device_node *np,
1617  		struct property *prop);
1618  
of_changeset_attach_node(struct of_changeset * ocs,struct device_node * np)1619  static inline int of_changeset_attach_node(struct of_changeset *ocs,
1620  		struct device_node *np)
1621  {
1622  	return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1623  }
1624  
of_changeset_detach_node(struct of_changeset * ocs,struct device_node * np)1625  static inline int of_changeset_detach_node(struct of_changeset *ocs,
1626  		struct device_node *np)
1627  {
1628  	return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1629  }
1630  
of_changeset_add_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1631  static inline int of_changeset_add_property(struct of_changeset *ocs,
1632  		struct device_node *np, struct property *prop)
1633  {
1634  	return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1635  }
1636  
of_changeset_remove_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1637  static inline int of_changeset_remove_property(struct of_changeset *ocs,
1638  		struct device_node *np, struct property *prop)
1639  {
1640  	return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1641  }
1642  
of_changeset_update_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1643  static inline int of_changeset_update_property(struct of_changeset *ocs,
1644  		struct device_node *np, struct property *prop)
1645  {
1646  	return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1647  }
1648  
1649  struct device_node *of_changeset_create_node(struct of_changeset *ocs,
1650  					     struct device_node *parent,
1651  					     const char *full_name);
1652  int of_changeset_add_prop_string(struct of_changeset *ocs,
1653  				 struct device_node *np,
1654  				 const char *prop_name, const char *str);
1655  int of_changeset_add_prop_string_array(struct of_changeset *ocs,
1656  				       struct device_node *np,
1657  				       const char *prop_name,
1658  				       const char * const *str_array, size_t sz);
1659  int of_changeset_add_prop_u32_array(struct of_changeset *ocs,
1660  				    struct device_node *np,
1661  				    const char *prop_name,
1662  				    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)1663  static inline int of_changeset_add_prop_u32(struct of_changeset *ocs,
1664  					    struct device_node *np,
1665  					    const char *prop_name,
1666  					    const u32 val)
1667  {
1668  	return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1);
1669  }
1670  
1671  int of_changeset_update_prop_string(struct of_changeset *ocs,
1672  				    struct device_node *np,
1673  				    const char *prop_name, const char *str);
1674  
1675  int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np,
1676  			       const char *prop_name);
1677  
1678  #else /* CONFIG_OF_DYNAMIC */
of_reconfig_notifier_register(struct notifier_block * nb)1679  static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1680  {
1681  	return -EINVAL;
1682  }
of_reconfig_notifier_unregister(struct notifier_block * nb)1683  static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1684  {
1685  	return -EINVAL;
1686  }
of_reconfig_notify(unsigned long action,struct of_reconfig_data * arg)1687  static inline int of_reconfig_notify(unsigned long action,
1688  				     struct of_reconfig_data *arg)
1689  {
1690  	return -EINVAL;
1691  }
of_reconfig_get_state_change(unsigned long action,struct of_reconfig_data * arg)1692  static inline int of_reconfig_get_state_change(unsigned long action,
1693  						struct of_reconfig_data *arg)
1694  {
1695  	return -EINVAL;
1696  }
1697  #endif /* CONFIG_OF_DYNAMIC */
1698  
1699  /**
1700   * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1701   * @np: Pointer to the given device_node
1702   *
1703   * Return: true if present false otherwise
1704   */
of_device_is_system_power_controller(const struct device_node * np)1705  static inline bool of_device_is_system_power_controller(const struct device_node *np)
1706  {
1707  	return of_property_read_bool(np, "system-power-controller");
1708  }
1709  
1710  /**
1711   * of_have_populated_dt() - Has DT been populated by bootloader
1712   *
1713   * Return: True if a DTB has been populated by the bootloader and it isn't the
1714   * empty builtin one. False otherwise.
1715   */
of_have_populated_dt(void)1716  static inline bool of_have_populated_dt(void)
1717  {
1718  #ifdef CONFIG_OF
1719  	return of_property_present(of_root, "compatible");
1720  #else
1721  	return false;
1722  #endif
1723  }
1724  
1725  /*
1726   * Overlay support
1727   */
1728  
1729  enum of_overlay_notify_action {
1730  	OF_OVERLAY_INIT = 0,	/* kzalloc() of ovcs sets this value */
1731  	OF_OVERLAY_PRE_APPLY,
1732  	OF_OVERLAY_POST_APPLY,
1733  	OF_OVERLAY_PRE_REMOVE,
1734  	OF_OVERLAY_POST_REMOVE,
1735  };
1736  
of_overlay_action_name(enum of_overlay_notify_action action)1737  static inline const char *of_overlay_action_name(enum of_overlay_notify_action action)
1738  {
1739  	static const char *const of_overlay_action_name[] = {
1740  		"init",
1741  		"pre-apply",
1742  		"post-apply",
1743  		"pre-remove",
1744  		"post-remove",
1745  	};
1746  
1747  	return of_overlay_action_name[action];
1748  }
1749  
1750  struct of_overlay_notify_data {
1751  	struct device_node *overlay;
1752  	struct device_node *target;
1753  };
1754  
1755  #ifdef CONFIG_OF_OVERLAY
1756  
1757  int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1758  			 int *ovcs_id, const struct device_node *target_base);
1759  int of_overlay_remove(int *ovcs_id);
1760  int of_overlay_remove_all(void);
1761  
1762  int of_overlay_notifier_register(struct notifier_block *nb);
1763  int of_overlay_notifier_unregister(struct notifier_block *nb);
1764  
1765  #else
1766  
of_overlay_fdt_apply(const void * overlay_fdt,u32 overlay_fdt_size,int * ovcs_id,const struct device_node * target_base)1767  static inline int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1768  				       int *ovcs_id, const struct device_node *target_base)
1769  {
1770  	return -ENOTSUPP;
1771  }
1772  
of_overlay_remove(int * ovcs_id)1773  static inline int of_overlay_remove(int *ovcs_id)
1774  {
1775  	return -ENOTSUPP;
1776  }
1777  
of_overlay_remove_all(void)1778  static inline int of_overlay_remove_all(void)
1779  {
1780  	return -ENOTSUPP;
1781  }
1782  
of_overlay_notifier_register(struct notifier_block * nb)1783  static inline int of_overlay_notifier_register(struct notifier_block *nb)
1784  {
1785  	return 0;
1786  }
1787  
of_overlay_notifier_unregister(struct notifier_block * nb)1788  static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1789  {
1790  	return 0;
1791  }
1792  
1793  #endif
1794  
1795  #endif /* _LINUX_OF_H */
1796