Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
25typedef u32 phandle;
26typedef u32 ihandle;
27
28struct 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)
45struct of_irq_controller;
46#endif
47
48struct 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
71struct of_phandle_args {
72 struct device_node *np;
73 int args_count;
74 uint32_t args[MAX_PHANDLE_ARGS];
75};
76
77struct 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
94struct of_reconfig_data {
95 struct device_node *dn;
96 struct property *prop;
97 struct property *old_prop;
98};
99
100extern const struct kobj_type of_node_ktype;
101extern 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 */
113static 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
128extern struct device_node *of_node_get(struct device_node *node);
129extern void of_node_put(struct device_node *node);
130#else /* CONFIG_OF_DYNAMIC */
131/* Dummy ref counting routines - to be implemented later */
132static inline struct device_node *of_node_get(struct device_node *node)
133{
134 return node;
135}
136static inline void of_node_put(struct device_node *node) { }
137#endif /* !CONFIG_OF_DYNAMIC */
138DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))
139
140/* Pointer for first entry in chain of all nodes. */
141extern struct device_node *of_root;
142extern struct device_node *of_chosen;
143extern struct device_node *of_aliases;
144extern 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
160void of_core_init(void);
161
162static 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
185static inline bool of_node_is_root(const struct device_node *node)
186{
187 return node && (node->parent == NULL);
188}
189
190static inline int of_node_check_flag(const struct device_node *n, unsigned long flag)
191{
192 return test_bit(flag, &n->_flags);
193}
194
195static 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
201static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
202{
203 set_bit(flag, &n->_flags);
204}
205
206static 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)
212static inline int of_property_check_flag(const struct property *p, unsigned long flag)
213{
214 return test_bit(flag, &p->_flags);
215}
216
217static inline void of_property_set_flag(struct property *p, unsigned long flag)
218{
219 set_bit(flag, &p->_flags);
220}
221
222static inline void of_property_clear_flag(struct property *p, unsigned long flag)
223{
224 clear_bit(flag, &p->_flags);
225}
226#endif
227
228extern struct device_node *__of_find_all_nodes(struct device_node *prev);
229extern 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) */
236static 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 */
245static 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
258extern bool of_node_name_eq(const struct device_node *np, const char *name);
259extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
260
261static 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)
269extern struct device_node *of_find_node_by_name(struct device_node *from,
270 const char *name);
271extern struct device_node *of_find_node_by_type(struct device_node *from,
272 const char *type);
273extern struct device_node *of_find_compatible_node(struct device_node *from,
274 const char *type, const char *compat);
275extern 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
280extern struct device_node *of_find_node_opts_by_path(const char *path,
281 const char **opts);
282static 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
287extern struct device_node *of_find_node_by_phandle(phandle handle);
288extern struct device_node *of_get_parent(const struct device_node *node);
289extern struct device_node *of_get_next_parent(struct device_node *node);
290extern struct device_node *of_get_next_child(const struct device_node *node,
291 struct device_node *prev);
292extern struct device_node *of_get_next_child_with_prefix(const struct device_node *node,
293 struct device_node *prev,
294 const char *prefix);
295extern struct device_node *of_get_next_available_child(
296 const struct device_node *node, struct device_node *prev);
297extern struct device_node *of_get_next_reserved_child(
298 const struct device_node *node, struct device_node *prev);
299
300extern struct device_node *of_get_compatible_child(const struct device_node *parent,
301 const char *compatible);
302extern struct device_node *of_get_child_by_name(const struct device_node *node,
303 const char *name);
304extern struct device_node *of_get_available_child_by_name(const struct device_node *node,
305 const char *name);
306
307/* cache lookup */
308extern struct device_node *of_find_next_cache_node(const struct device_node *);
309extern int of_find_last_cache_level(unsigned int cpu);
310extern struct device_node *of_find_node_with_property(
311 struct device_node *from, const char *prop_name);
312
313extern struct property *of_find_property(const struct device_node *np,
314 const char *name,
315 int *lenp);
316extern bool of_property_read_bool(const struct device_node *np, const char *propname);
317extern int of_property_count_elems_of_size(const struct device_node *np,
318 const char *propname, int elem_size);
319extern int of_property_read_u8_index(const struct device_node *np,
320 const char *propname,
321 u32 index, u8 *out_value);
322extern int of_property_read_u16_index(const struct device_node *np,
323 const char *propname,
324 u32 index, u16 *out_value);
325extern int of_property_read_u32_index(const struct device_node *np,
326 const char *propname,
327 u32 index, u32 *out_value);
328extern int of_property_read_u64_index(const struct device_node *np,
329 const char *propname,
330 u32 index, u64 *out_value);
331extern 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);
334extern 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);
337extern 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);
342extern int of_property_read_u64(const struct device_node *np,
343 const char *propname, u64 *out_value);
344extern 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
350extern int of_property_read_string(const struct device_node *np,
351 const char *propname,
352 const char **out_string);
353extern int of_property_match_string(const struct device_node *np,
354 const char *propname,
355 const char *string);
356extern 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);
359extern int of_device_is_compatible(const struct device_node *device,
360 const char *);
361extern int of_device_compatible_match(const struct device_node *device,
362 const char *const *compat);
363extern bool of_device_is_available(const struct device_node *device);
364extern bool of_device_is_big_endian(const struct device_node *device);
365extern const void *of_get_property(const struct device_node *node,
366 const char *name,
367 int *lenp);
368extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
369extern struct device_node *of_cpu_device_node_get(int cpu);
370extern int of_cpu_node_to_id(struct device_node *np);
371extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
372extern struct device_node *of_get_cpu_state_node(const struct device_node *cpu_node,
373 int index);
374extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread);
375
376extern int of_n_addr_cells(struct device_node *np);
377extern int of_n_size_cells(struct device_node *np);
378extern const struct of_device_id *of_match_node(
379 const struct of_device_id *matches, const struct device_node *node);
380extern const void *of_device_get_match_data(const struct device *dev);
381extern int of_alias_from_compatible(const struct device_node *node, char *alias,
382 int len);
383extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
384extern 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);
387extern 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);
390extern int of_count_phandle_with_args(const struct device_node *np,
391 const char *list_name, const char *cells_name);
392
393/* module functions */
394extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len);
395extern int of_request_module(const struct device_node *np);
396
397/* phandle iterator functions */
398extern 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
404extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
405extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
406 uint32_t *args,
407 int size);
408
409extern int of_alias_get_id(const struct device_node *np, const char *stem);
410extern int of_alias_get_highest_id(const char *stem);
411
412bool of_machine_compatible_match(const char *const *compats);
413const struct of_device_id *of_machine_get_match(const struct of_device_id *matches);
414const 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 */
422static 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
429int of_machine_read_compatible(const char **compatible, unsigned int index);
430int of_machine_read_model(const char **model);
431
432extern int of_add_property(struct device_node *np, struct property *prop);
433extern int of_remove_property(struct device_node *np, struct property *prop);
434extern 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
443extern int of_attach_node(struct device_node *);
444extern 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 */
454const __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 */
463const char *of_prop_next_string(const struct property *prop, const char *cur);
464
465bool of_console_check(const struct device_node *dn, char *name, int index);
466
467int 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
471phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
472
473struct kimage;
474void *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
480static inline void of_core_init(void)
481{
482}
483
484static inline bool is_of_node(const struct fwnode_handle *fwnode)
485{
486 return false;
487}
488
489static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
490{
491 return NULL;
492}
493
494static inline bool of_node_name_eq(const struct device_node *np, const char *name)
495{
496 return false;
497}
498
499static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
500{
501 return false;
502}
503
504static inline const char* of_node_full_name(const struct device_node *np)
505{
506 return "<no-node>";
507}
508
509static inline struct device_node *of_find_node_by_name(struct device_node *from,
510 const char *name)
511{
512 return NULL;
513}
514
515static inline struct device_node *of_find_node_by_type(struct device_node *from,
516 const char *type)
517{
518 return NULL;
519}
520
521static 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
529static inline struct device_node *of_find_node_by_path(const char *path)
530{
531 return NULL;
532}
533
534static inline struct device_node *of_find_node_opts_by_path(const char *path,
535 const char **opts)
536{
537 return NULL;
538}
539
540static inline struct device_node *of_find_node_by_phandle(phandle handle)
541{
542 return NULL;
543}
544
545static inline struct device_node *of_get_parent(const struct device_node *node)
546{
547 return NULL;
548}
549
550static inline struct device_node *of_get_next_parent(struct device_node *node)
551{
552 return NULL;
553}
554
555static inline struct device_node *of_get_next_child(
556 const struct device_node *node, struct device_node *prev)
557{
558 return NULL;
559}
560
561static 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
568static 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
574static 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
580static 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
588static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
589 const char *compatible)
590{
591 return NULL;
592}
593
594static 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
601static 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
608static inline int of_device_is_compatible(const struct device_node *device,
609 const char *name)
610{
611 return 0;
612}
613
614static inline int of_device_compatible_match(const struct device_node *device,
615 const char *const *compat)
616{
617 return 0;
618}
619
620static inline bool of_device_is_available(const struct device_node *device)
621{
622 return false;
623}
624
625static inline bool of_device_is_big_endian(const struct device_node *device)
626{
627 return false;
628}
629
630static inline struct property *of_find_property(const struct device_node *np,
631 const char *name,
632 int *lenp)
633{
634 return NULL;
635}
636
637static 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
645static inline bool of_property_read_bool(const struct device_node *np,
646 const char *propname)
647{
648 return false;
649}
650
651static 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
657static 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
663static 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
669static 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
675static 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
681static inline const void *of_get_property(const struct device_node *node,
682 const char *name,
683 int *lenp)
684{
685 return NULL;
686}
687
688static inline struct device_node *of_get_cpu_node(int cpu,
689 unsigned int *thread)
690{
691 return NULL;
692}
693
694static inline struct device_node *of_cpu_device_node_get(int cpu)
695{
696 return NULL;
697}
698
699static inline int of_cpu_node_to_id(struct device_node *np)
700{
701 return -ENODEV;
702}
703
704static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
705{
706 return NULL;
707}
708
709static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
710 int index)
711{
712 return NULL;
713}
714
715static inline int of_n_addr_cells(struct device_node *np)
716{
717 return 0;
718
719}
720static inline int of_n_size_cells(struct device_node *np)
721{
722 return 0;
723}
724
725static 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
732static 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
739static 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
748static inline int of_property_read_u64(const struct device_node *np,
749 const char *propname, u64 *out_value)
750{
751 return -ENOSYS;
752}
753
754static 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
763static 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
770static 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
777static 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
784static 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
794static 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
803static 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
810static inline ssize_t of_modalias(const struct device_node *np, char *str,
811 ssize_t len)
812{
813 return -ENODEV;
814}
815
816static inline int of_request_module(const struct device_node *np)
817{
818 return -ENODEV;
819}
820
821static 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
830static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
831{
832 return -ENOSYS;
833}
834
835static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
836 uint32_t *args,
837 int size)
838{
839 return 0;
840}
841
842static inline int of_alias_get_id(struct device_node *np, const char *stem)
843{
844 return -ENOSYS;
845}
846
847static inline int of_alias_get_highest_id(const char *stem)
848{
849 return -ENOSYS;
850}
851
852static inline int of_machine_is_compatible(const char *compat)
853{
854 return 0;
855}
856
857static inline int of_machine_read_compatible(const char **compatible,
858 unsigned int index)
859{
860 return -ENOSYS;
861}
862
863static inline int of_machine_read_model(const char **model)
864{
865 return -ENOSYS;
866}
867
868static inline int of_add_property(struct device_node *np, struct property *prop)
869{
870 return 0;
871}
872
873static inline int of_remove_property(struct device_node *np, struct property *prop)
874{
875 return 0;
876}
877
878static inline bool of_machine_compatible_match(const char *const *compats)
879{
880 return false;
881}
882
883static inline const struct of_device_id *of_machine_get_match(const struct of_device_id *matches)
884{
885 return NULL;
886}
887
888static inline const void *
889of_machine_get_match_data(const struct of_device_id *matches)
890{
891 return NULL;
892}
893
894static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
895{
896 return false;
897}
898
899static inline const __be32 *of_prop_next_u32(const struct property *prop,
900 const __be32 *cur, u32 *pu)
901{
902 return NULL;
903}
904
905static inline const char *of_prop_next_string(const struct property *prop,
906 const char *cur)
907{
908 return NULL;
909}
910
911static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
912{
913 return 0;
914}
915
916static inline int of_node_test_and_set_flag(struct device_node *n,
917 unsigned long flag)
918{
919 return 0;
920}
921
922static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
923{
924}
925
926static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
927{
928}
929
930static inline int of_property_check_flag(const struct property *p,
931 unsigned long flag)
932{
933 return 0;
934}
935
936static inline void of_property_set_flag(struct property *p, unsigned long flag)
937{
938}
939
940static inline void of_property_clear_flag(struct property *p, unsigned long flag)
941{
942}
943
944static 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
951static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
952{
953 return PHYS_ADDR_MAX;
954}
955
956static 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)
976extern int of_node_to_nid(struct device_node *np);
977#else
978static 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
985extern int of_numa_init(void);
986#else
987static inline int of_numa_init(void)
988{
989 return -ENOSYS;
990}
991#endif
992
993static inline bool of_machine_device_match(const struct of_device_id *matches)
994{
995 return of_machine_get_match(matches) != NULL;
996}
997
998static inline struct device_node *of_find_matching_node(
999 struct device_node *from,
1000 const struct of_device_id *matches)
1001{
1002 return of_find_matching_node_and_match(from, matches, NULL);
1003}
1004
1005static inline const char *of_node_get_device_type(const struct device_node *np)
1006{
1007 return of_get_property(np, "device_type", NULL);
1008}
1009
1010static inline bool of_node_is_type(const struct device_node *np, const char *type)
1011{
1012 const char *match = of_node_get_device_type(np);
1013
1014 return np && match && type && !strcmp(match, type);
1015}
1016
1017/**
1018 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1019 * @np: Pointer to device node holding phandle property
1020 * @phandle_name: Name of property holding a phandle value
1021 * @index: For properties holding a table of phandles, this is the index into
1022 * the table
1023 *
1024 * Return: The device_node pointer with refcount incremented. Use
1025 * of_node_put() on it when done.
1026 */
1027static inline struct device_node *of_parse_phandle(const struct device_node *np,
1028 const char *phandle_name,
1029 int index)
1030{
1031 struct of_phandle_args args;
1032
1033 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1034 index, &args))
1035 return NULL;
1036
1037 return args.np;
1038}
1039
1040/**
1041 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1042 * @np: pointer to a device tree node containing a list
1043 * @list_name: property name that contains a list
1044 * @cells_name: property name that specifies phandles' arguments count
1045 * @index: index of a phandle to parse out
1046 * @out_args: optional pointer to output arguments structure (will be filled)
1047 *
1048 * This function is useful to parse lists of phandles and their arguments.
1049 * Returns 0 on success and fills out_args, on error returns appropriate
1050 * errno value.
1051 *
1052 * Caller is responsible to call of_node_put() on the returned out_args->np
1053 * pointer.
1054 *
1055 * Example::
1056 *
1057 * phandle1: node1 {
1058 * #list-cells = <2>;
1059 * };
1060 *
1061 * phandle2: node2 {
1062 * #list-cells = <1>;
1063 * };
1064 *
1065 * node3 {
1066 * list = <&phandle1 1 2 &phandle2 3>;
1067 * };
1068 *
1069 * To get a device_node of the ``node2`` node you may call this:
1070 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1071 */
1072static inline int of_parse_phandle_with_args(const struct device_node *np,
1073 const char *list_name,
1074 const char *cells_name,
1075 int index,
1076 struct of_phandle_args *out_args)
1077{
1078 int cell_count = -1;
1079
1080 /* If cells_name is NULL we assume a cell count of 0 */
1081 if (!cells_name)
1082 cell_count = 0;
1083
1084 return __of_parse_phandle_with_args(np, list_name, cells_name,
1085 cell_count, index, out_args);
1086}
1087
1088/**
1089 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1090 * @np: pointer to a device tree node containing a list
1091 * @list_name: property name that contains a list
1092 * @cell_count: number of argument cells following the phandle
1093 * @index: index of a phandle to parse out
1094 * @out_args: optional pointer to output arguments structure (will be filled)
1095 *
1096 * This function is useful to parse lists of phandles and their arguments.
1097 * Returns 0 on success and fills out_args, on error returns appropriate
1098 * errno value.
1099 *
1100 * Caller is responsible to call of_node_put() on the returned out_args->np
1101 * pointer.
1102 *
1103 * Example::
1104 *
1105 * phandle1: node1 {
1106 * };
1107 *
1108 * phandle2: node2 {
1109 * };
1110 *
1111 * node3 {
1112 * list = <&phandle1 0 2 &phandle2 2 3>;
1113 * };
1114 *
1115 * To get a device_node of the ``node2`` node you may call this:
1116 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1117 */
1118static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
1119 const char *list_name,
1120 int cell_count,
1121 int index,
1122 struct of_phandle_args *out_args)
1123{
1124 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1125 index, out_args);
1126}
1127
1128/**
1129 * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
1130 * @np: pointer to a device tree node containing a list
1131 * @list_name: property name that contains a list
1132 * @cells_name: property name that specifies phandles' arguments count
1133 * @index: index of a phandle to parse out
1134 * @out_args: optional pointer to output arguments structure (will be filled)
1135 *
1136 * Same as of_parse_phandle_with_args() except that if the cells_name property
1137 * is not found, cell_count of 0 is assumed.
1138 *
1139 * This is used to useful, if you have a phandle which didn't have arguments
1140 * before and thus doesn't have a '#*-cells' property but is now migrated to
1141 * having arguments while retaining backwards compatibility.
1142 */
1143static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
1144 const char *list_name,
1145 const char *cells_name,
1146 int index,
1147 struct of_phandle_args *out_args)
1148{
1149 return __of_parse_phandle_with_args(np, list_name, cells_name,
1150 0, index, out_args);
1151}
1152
1153/**
1154 * of_phandle_args_equal() - Compare two of_phandle_args
1155 * @a1: First of_phandle_args to compare
1156 * @a2: Second of_phandle_args to compare
1157 *
1158 * Return: True if a1 and a2 are the same (same node pointer, same phandle
1159 * args), false otherwise.
1160 */
1161static inline bool of_phandle_args_equal(const struct of_phandle_args *a1,
1162 const struct of_phandle_args *a2)
1163{
1164 return a1->np == a2->np &&
1165 a1->args_count == a2->args_count &&
1166 !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count);
1167}
1168
1169/**
1170 * of_property_count_u8_elems - Count the number of u8 elements in a property
1171 *
1172 * @np: device node from which the property value is to be read.
1173 * @propname: name of the property to be searched.
1174 *
1175 * Search for a property in a device node and count the number of u8 elements
1176 * in it.
1177 *
1178 * Return: The number of elements on success, -EINVAL if the property does
1179 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1180 * property does not have a value.
1181 */
1182static inline int of_property_count_u8_elems(const struct device_node *np,
1183 const char *propname)
1184{
1185 return of_property_count_elems_of_size(np, propname, sizeof(u8));
1186}
1187
1188/**
1189 * of_property_count_u16_elems - Count the number of u16 elements in a property
1190 *
1191 * @np: device node from which the property value is to be read.
1192 * @propname: name of the property to be searched.
1193 *
1194 * Search for a property in a device node and count the number of u16 elements
1195 * in it.
1196 *
1197 * Return: The number of elements on success, -EINVAL if the property does
1198 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1199 * property does not have a value.
1200 */
1201static inline int of_property_count_u16_elems(const struct device_node *np,
1202 const char *propname)
1203{
1204 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1205}
1206
1207/**
1208 * of_property_count_u32_elems - Count the number of u32 elements in a property
1209 *
1210 * @np: device node from which the property value is to be read.
1211 * @propname: name of the property to be searched.
1212 *
1213 * Search for a property in a device node and count the number of u32 elements
1214 * in it.
1215 *
1216 * Return: The number of elements on success, -EINVAL if the property does
1217 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1218 * property does not have a value.
1219 */
1220static inline int of_property_count_u32_elems(const struct device_node *np,
1221 const char *propname)
1222{
1223 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1224}
1225
1226/**
1227 * of_property_count_u64_elems - Count the number of u64 elements in a property
1228 *
1229 * @np: device node from which the property value is to be read.
1230 * @propname: name of the property to be searched.
1231 *
1232 * Search for a property in a device node and count the number of u64 elements
1233 * in it.
1234 *
1235 * Return: The number of elements on success, -EINVAL if the property does
1236 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1237 * property does not have a value.
1238 */
1239static inline int of_property_count_u64_elems(const struct device_node *np,
1240 const char *propname)
1241{
1242 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1243}
1244
1245/**
1246 * of_property_read_string_array() - Read an array of strings from a multiple
1247 * strings property.
1248 * @np: device node from which the property value is to be read.
1249 * @propname: name of the property to be searched.
1250 * @out_strs: output array of string pointers.
1251 * @sz: number of array elements to read.
1252 *
1253 * Search for a property in a device tree node and retrieve a list of
1254 * terminated string values (pointer to data, not a copy) in that property.
1255 *
1256 * Return: If @out_strs is NULL, the number of strings in the property is returned.
1257 */
1258static inline int of_property_read_string_array(const struct device_node *np,
1259 const char *propname, const char **out_strs,
1260 size_t sz)
1261{
1262 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1263}
1264
1265/**
1266 * of_property_count_strings() - Find and return the number of strings from a
1267 * multiple strings property.
1268 * @np: device node from which the property value is to be read.
1269 * @propname: name of the property to be searched.
1270 *
1271 * Search for a property in a device tree node and retrieve the number of null
1272 * terminated string contain in it.
1273 *
1274 * Return: The number of strings on success, -EINVAL if the property does not
1275 * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
1276 * is not null-terminated within the length of the property data.
1277 */
1278static inline int of_property_count_strings(const struct device_node *np,
1279 const char *propname)
1280{
1281 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1282}
1283
1284/**
1285 * of_property_read_string_index() - Find and read a string from a multiple
1286 * strings property.
1287 * @np: device node from which the property value is to be read.
1288 * @propname: name of the property to be searched.
1289 * @index: index of the string in the list of strings
1290 * @output: pointer to null terminated return string, modified only if
1291 * return value is 0.
1292 *
1293 * Search for a property in a device tree node and retrieve a null
1294 * terminated string value (pointer to data, not a copy) in the list of strings
1295 * contained in that property.
1296 *
1297 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
1298 * property does not have a value, and -EILSEQ if the string is not
1299 * null-terminated within the length of the property data.
1300 *
1301 * The out_string pointer is modified only if a valid string can be decoded.
1302 */
1303static inline int of_property_read_string_index(const struct device_node *np,
1304 const char *propname,
1305 int index, const char **output)
1306{
1307 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1308 return rc < 0 ? rc : 0;
1309}
1310
1311/**
1312 * of_property_present - Test if a property is present in a node
1313 * @np: device node to search for the property.
1314 * @propname: name of the property to be searched.
1315 *
1316 * Test for a property present in a device node.
1317 *
1318 * Return: true if the property exists false otherwise.
1319 */
1320static inline bool of_property_present(const struct device_node *np, const char *propname)
1321{
1322 struct property *prop = of_find_property(np, propname, NULL);
1323
1324 return prop ? true : false;
1325}
1326
1327/**
1328 * of_property_read_u8_array - Find and read an array of u8 from a property.
1329 *
1330 * @np: device node from which the property value is to be read.
1331 * @propname: name of the property to be searched.
1332 * @out_values: pointer to return value, modified only if return value is 0.
1333 * @sz: number of array elements to read
1334 *
1335 * Search for a property in a device node and read 8-bit value(s) from
1336 * it.
1337 *
1338 * dts entry of array should be like:
1339 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
1340 *
1341 * Return: 0 on success, -EINVAL if the property does not exist,
1342 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1343 * property data isn't large enough.
1344 *
1345 * The out_values is modified only if a valid u8 value can be decoded.
1346 */
1347static inline int of_property_read_u8_array(const struct device_node *np,
1348 const char *propname,
1349 u8 *out_values, size_t sz)
1350{
1351 int ret = of_property_read_variable_u8_array(np, propname, out_values,
1352 sz, 0);
1353 if (ret >= 0)
1354 return 0;
1355 else
1356 return ret;
1357}
1358
1359/**
1360 * of_property_read_u16_array - Find and read an array of u16 from a property.
1361 *
1362 * @np: device node from which the property value is to be read.
1363 * @propname: name of the property to be searched.
1364 * @out_values: pointer to return value, modified only if return value is 0.
1365 * @sz: number of array elements to read
1366 *
1367 * Search for a property in a device node and read 16-bit value(s) from
1368 * it.
1369 *
1370 * dts entry of array should be like:
1371 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1372 *
1373 * Return: 0 on success, -EINVAL if the property does not exist,
1374 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1375 * property data isn't large enough.
1376 *
1377 * The out_values is modified only if a valid u16 value can be decoded.
1378 */
1379static inline int of_property_read_u16_array(const struct device_node *np,
1380 const char *propname,
1381 u16 *out_values, size_t sz)
1382{
1383 int ret = of_property_read_variable_u16_array(np, propname, out_values,
1384 sz, 0);
1385 if (ret >= 0)
1386 return 0;
1387 else
1388 return ret;
1389}
1390
1391/**
1392 * of_property_read_u32_array - Find and read an array of 32 bit integers
1393 * from a property.
1394 *
1395 * @np: device node from which the property value is to be read.
1396 * @propname: name of the property to be searched.
1397 * @out_values: pointer to return value, modified only if return value is 0.
1398 * @sz: number of array elements to read
1399 *
1400 * Search for a property in a device node and read 32-bit value(s) from
1401 * it.
1402 *
1403 * Return: 0 on success, -EINVAL if the property does not exist,
1404 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1405 * property data isn't large enough.
1406 *
1407 * The out_values is modified only if a valid u32 value can be decoded.
1408 */
1409static inline int of_property_read_u32_array(const struct device_node *np,
1410 const char *propname,
1411 u32 *out_values, size_t sz)
1412{
1413 int ret = of_property_read_variable_u32_array(np, propname, out_values,
1414 sz, 0);
1415 if (ret >= 0)
1416 return 0;
1417 else
1418 return ret;
1419}
1420
1421/**
1422 * of_property_read_u64_array - Find and read an array of 64 bit integers
1423 * from a property.
1424 *
1425 * @np: device node from which the property value is to be read.
1426 * @propname: name of the property to be searched.
1427 * @out_values: pointer to return value, modified only if return value is 0.
1428 * @sz: number of array elements to read
1429 *
1430 * Search for a property in a device node and read 64-bit value(s) from
1431 * it.
1432 *
1433 * Return: 0 on success, -EINVAL if the property does not exist,
1434 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1435 * property data isn't large enough.
1436 *
1437 * The out_values is modified only if a valid u64 value can be decoded.
1438 */
1439static inline int of_property_read_u64_array(const struct device_node *np,
1440 const char *propname,
1441 u64 *out_values, size_t sz)
1442{
1443 int ret = of_property_read_variable_u64_array(np, propname, out_values,
1444 sz, 0);
1445 if (ret >= 0)
1446 return 0;
1447 else
1448 return ret;
1449}
1450
1451static inline int of_property_read_u8(const struct device_node *np,
1452 const char *propname,
1453 u8 *out_value)
1454{
1455 return of_property_read_u8_array(np, propname, out_value, 1);
1456}
1457
1458static inline int of_property_read_u16(const struct device_node *np,
1459 const char *propname,
1460 u16 *out_value)
1461{
1462 return of_property_read_u16_array(np, propname, out_value, 1);
1463}
1464
1465static inline int of_property_read_u32(const struct device_node *np,
1466 const char *propname,
1467 u32 *out_value)
1468{
1469 return of_property_read_u32_array(np, propname, out_value, 1);
1470}
1471
1472static inline int of_property_read_s32(const struct device_node *np,
1473 const char *propname,
1474 s32 *out_value)
1475{
1476 return of_property_read_u32(np, propname, (u32*) out_value);
1477}
1478
1479#define of_for_each_phandle(it, err, np, ln, cn, cc) \
1480 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1481 err = of_phandle_iterator_next(it); \
1482 err == 0; \
1483 err = of_phandle_iterator_next(it))
1484
1485#define of_property_for_each_u32(np, propname, u) \
1486 for (struct {const struct property *prop; const __be32 *item; } _it = \
1487 {of_find_property(np, propname, NULL), \
1488 of_prop_next_u32(_it.prop, NULL, &u)}; \
1489 _it.item; \
1490 _it.item = of_prop_next_u32(_it.prop, _it.item, &u))
1491
1492#define of_property_for_each_string(np, propname, prop, s) \
1493 for (prop = of_find_property(np, propname, NULL), \
1494 s = of_prop_next_string(prop, NULL); \
1495 s; \
1496 s = of_prop_next_string(prop, s))
1497
1498#define for_each_node_by_name(dn, name) \
1499 for (dn = of_find_node_by_name(NULL, name); dn; \
1500 dn = of_find_node_by_name(dn, name))
1501#define for_each_node_by_type(dn, type) \
1502 for (dn = of_find_node_by_type(NULL, type); dn; \
1503 dn = of_find_node_by_type(dn, type))
1504#define for_each_compatible_node(dn, type, compatible) \
1505 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1506 dn = of_find_compatible_node(dn, type, compatible))
1507
1508#define for_each_compatible_node_scoped(dn, type, compatible) \
1509 for (struct device_node *dn __free(device_node) = \
1510 of_find_compatible_node(NULL, type, compatible); \
1511 dn; \
1512 dn = of_find_compatible_node(dn, type, compatible))
1513
1514#define for_each_matching_node(dn, matches) \
1515 for (dn = of_find_matching_node(NULL, matches); dn; \
1516 dn = of_find_matching_node(dn, matches))
1517#define for_each_matching_node_and_match(dn, matches, match) \
1518 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1519 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1520
1521#define for_each_child_of_node(parent, child) \
1522 for (child = of_get_next_child(parent, NULL); child != NULL; \
1523 child = of_get_next_child(parent, child))
1524
1525#define for_each_child_of_node_scoped(parent, child) \
1526 for (struct device_node *child __free(device_node) = \
1527 of_get_next_child(parent, NULL); \
1528 child != NULL; \
1529 child = of_get_next_child(parent, child))
1530
1531#define for_each_child_of_node_with_prefix(parent, child, prefix) \
1532 for (struct device_node *child __free(device_node) = \
1533 of_get_next_child_with_prefix(parent, NULL, prefix); \
1534 child != NULL; \
1535 child = of_get_next_child_with_prefix(parent, child, prefix))
1536
1537#define for_each_available_child_of_node(parent, child) \
1538 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1539 child = of_get_next_available_child(parent, child))
1540#define for_each_reserved_child_of_node(parent, child) \
1541 for (child = of_get_next_reserved_child(parent, NULL); child != NULL; \
1542 child = of_get_next_reserved_child(parent, child))
1543
1544#define for_each_available_child_of_node_scoped(parent, child) \
1545 for (struct device_node *child __free(device_node) = \
1546 of_get_next_available_child(parent, NULL); \
1547 child != NULL; \
1548 child = of_get_next_available_child(parent, child))
1549
1550#define for_each_of_cpu_node(cpu) \
1551 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1552 cpu = of_get_next_cpu_node(cpu))
1553
1554#define for_each_node_with_property(dn, prop_name) \
1555 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1556 dn = of_find_node_with_property(dn, prop_name))
1557
1558static inline int of_get_child_count(const struct device_node *np)
1559{
1560 struct device_node *child;
1561 int num = 0;
1562
1563 for_each_child_of_node(np, child)
1564 num++;
1565
1566 return num;
1567}
1568
1569static inline int of_get_available_child_count(const struct device_node *np)
1570{
1571 struct device_node *child;
1572 int num = 0;
1573
1574 for_each_available_child_of_node(np, child)
1575 num++;
1576
1577 return num;
1578}
1579
1580#define _OF_DECLARE_STUB(table, name, compat, fn, fn_type) \
1581 static const struct of_device_id __of_table_##name \
1582 __attribute__((unused)) \
1583 = { .compatible = compat, \
1584 .data = (fn == (fn_type)NULL) ? fn : fn }
1585
1586#if defined(CONFIG_OF) && !defined(MODULE)
1587#define _OF_DECLARE(table, name, compat, fn, fn_type) \
1588 static const struct of_device_id __of_table_##name \
1589 __used __section("__" #table "_of_table") \
1590 __aligned(__alignof__(struct of_device_id)) \
1591 = { .compatible = compat, \
1592 .data = (fn == (fn_type)NULL) ? fn : fn }
1593#else
1594#define _OF_DECLARE(table, name, compat, fn, fn_type) \
1595 _OF_DECLARE_STUB(table, name, compat, fn, fn_type)
1596#endif
1597
1598typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1599typedef int (*of_init_fn_1_ret)(struct device_node *);
1600typedef void (*of_init_fn_1)(struct device_node *);
1601
1602#define OF_DECLARE_1(table, name, compat, fn) \
1603 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1604#define OF_DECLARE_1_RET(table, name, compat, fn) \
1605 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1606#define OF_DECLARE_2(table, name, compat, fn) \
1607 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1608
1609/**
1610 * struct of_changeset_entry - Holds a changeset entry
1611 *
1612 * @node: list_head for the log list
1613 * @action: notifier action
1614 * @np: pointer to the device node affected
1615 * @prop: pointer to the property affected
1616 * @old_prop: hold a pointer to the original property
1617 *
1618 * Every modification of the device tree during a changeset
1619 * is held in a list of of_changeset_entry structures.
1620 * That way we can recover from a partial application, or we can
1621 * revert the changeset
1622 */
1623struct of_changeset_entry {
1624 struct list_head node;
1625 unsigned long action;
1626 struct device_node *np;
1627 struct property *prop;
1628 struct property *old_prop;
1629};
1630
1631/**
1632 * struct of_changeset - changeset tracker structure
1633 *
1634 * @entries: list_head for the changeset entries
1635 *
1636 * changesets are a convenient way to apply bulk changes to the
1637 * live tree. In case of an error, changes are rolled-back.
1638 * changesets live on after initial application, and if not
1639 * destroyed after use, they can be reverted in one single call.
1640 */
1641struct of_changeset {
1642 struct list_head entries;
1643};
1644
1645enum of_reconfig_change {
1646 OF_RECONFIG_NO_CHANGE = 0,
1647 OF_RECONFIG_CHANGE_ADD,
1648 OF_RECONFIG_CHANGE_REMOVE,
1649};
1650
1651struct notifier_block;
1652
1653#ifdef CONFIG_OF_DYNAMIC
1654extern int of_reconfig_notifier_register(struct notifier_block *);
1655extern int of_reconfig_notifier_unregister(struct notifier_block *);
1656extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1657extern int of_reconfig_get_state_change(unsigned long action,
1658 struct of_reconfig_data *arg);
1659
1660extern void of_changeset_init(struct of_changeset *ocs);
1661extern void of_changeset_destroy(struct of_changeset *ocs);
1662extern int of_changeset_apply(struct of_changeset *ocs);
1663extern int of_changeset_revert(struct of_changeset *ocs);
1664extern int of_changeset_action(struct of_changeset *ocs,
1665 unsigned long action, struct device_node *np,
1666 struct property *prop);
1667
1668static inline int of_changeset_attach_node(struct of_changeset *ocs,
1669 struct device_node *np)
1670{
1671 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1672}
1673
1674static inline int of_changeset_detach_node(struct of_changeset *ocs,
1675 struct device_node *np)
1676{
1677 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1678}
1679
1680static inline int of_changeset_add_property(struct of_changeset *ocs,
1681 struct device_node *np, struct property *prop)
1682{
1683 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1684}
1685
1686static inline int of_changeset_remove_property(struct of_changeset *ocs,
1687 struct device_node *np, struct property *prop)
1688{
1689 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1690}
1691
1692static inline int of_changeset_update_property(struct of_changeset *ocs,
1693 struct device_node *np, struct property *prop)
1694{
1695 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1696}
1697
1698struct device_node *of_changeset_create_node(struct of_changeset *ocs,
1699 struct device_node *parent,
1700 const char *full_name);
1701int of_changeset_add_prop_string(struct of_changeset *ocs,
1702 struct device_node *np,
1703 const char *prop_name, const char *str);
1704int of_changeset_add_prop_string_array(struct of_changeset *ocs,
1705 struct device_node *np,
1706 const char *prop_name,
1707 const char * const *str_array, size_t sz);
1708int of_changeset_add_prop_u32_array(struct of_changeset *ocs,
1709 struct device_node *np,
1710 const char *prop_name,
1711 const u32 *array, size_t sz);
1712static inline int of_changeset_add_prop_u32(struct of_changeset *ocs,
1713 struct device_node *np,
1714 const char *prop_name,
1715 const u32 val)
1716{
1717 return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1);
1718}
1719
1720int of_changeset_update_prop_string(struct of_changeset *ocs,
1721 struct device_node *np,
1722 const char *prop_name, const char *str);
1723
1724int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np,
1725 const char *prop_name);
1726
1727#else /* CONFIG_OF_DYNAMIC */
1728static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1729{
1730 return -EINVAL;
1731}
1732static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1733{
1734 return -EINVAL;
1735}
1736static inline int of_reconfig_notify(unsigned long action,
1737 struct of_reconfig_data *arg)
1738{
1739 return -EINVAL;
1740}
1741static inline int of_reconfig_get_state_change(unsigned long action,
1742 struct of_reconfig_data *arg)
1743{
1744 return -EINVAL;
1745}
1746#endif /* CONFIG_OF_DYNAMIC */
1747
1748/**
1749 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1750 * @np: Pointer to the given device_node
1751 *
1752 * Return: true if present false otherwise
1753 */
1754static inline bool of_device_is_system_power_controller(const struct device_node *np)
1755{
1756 return of_property_read_bool(np, "system-power-controller");
1757}
1758
1759/**
1760 * of_have_populated_dt() - Has DT been populated by bootloader
1761 *
1762 * Return: True if a DTB has been populated by the bootloader and it isn't the
1763 * empty builtin one. False otherwise.
1764 */
1765static inline bool of_have_populated_dt(void)
1766{
1767#ifdef CONFIG_OF
1768 return of_property_present(of_root, "compatible");
1769#else
1770 return false;
1771#endif
1772}
1773
1774/*
1775 * Overlay support
1776 */
1777
1778enum of_overlay_notify_action {
1779 OF_OVERLAY_INIT = 0, /* kzalloc() of ovcs sets this value */
1780 OF_OVERLAY_PRE_APPLY,
1781 OF_OVERLAY_POST_APPLY,
1782 OF_OVERLAY_PRE_REMOVE,
1783 OF_OVERLAY_POST_REMOVE,
1784};
1785
1786static inline const char *of_overlay_action_name(enum of_overlay_notify_action action)
1787{
1788 static const char *const of_overlay_action_name[] = {
1789 "init",
1790 "pre-apply",
1791 "post-apply",
1792 "pre-remove",
1793 "post-remove",
1794 };
1795
1796 return of_overlay_action_name[action];
1797}
1798
1799struct of_overlay_notify_data {
1800 struct device_node *overlay;
1801 struct device_node *target;
1802};
1803
1804#ifdef CONFIG_OF_OVERLAY
1805
1806int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1807 int *ovcs_id, const struct device_node *target_base);
1808int of_overlay_remove(int *ovcs_id);
1809int of_overlay_remove_all(void);
1810
1811int of_overlay_notifier_register(struct notifier_block *nb);
1812int of_overlay_notifier_unregister(struct notifier_block *nb);
1813
1814#else
1815
1816static inline int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1817 int *ovcs_id, const struct device_node *target_base)
1818{
1819 return -ENOTSUPP;
1820}
1821
1822static inline int of_overlay_remove(int *ovcs_id)
1823{
1824 return -ENOTSUPP;
1825}
1826
1827static inline int of_overlay_remove_all(void)
1828{
1829 return -ENOTSUPP;
1830}
1831
1832static inline int of_overlay_notifier_register(struct notifier_block *nb)
1833{
1834 return 0;
1835}
1836
1837static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1838{
1839 return 0;
1840}
1841
1842#endif
1843
1844#endif /* _LINUX_OF_H */