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