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