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