GNU Linux-libre 4.4.288-gnu1
[releases.git] / include / linux / of.h
1 #ifndef _LINUX_OF_H
2 #define _LINUX_OF_H
3 /*
4  * Definitions for talking to the Open Firmware PROM on
5  * Power Macintosh and other computers.
6  *
7  * Copyright (C) 1996-2005 Paul Mackerras.
8  *
9  * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
10  * Updates for SPARC64 by David S. Miller
11  * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version
16  * 2 of the License, or (at your option) any later version.
17  */
18 #include <linux/types.h>
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/kobject.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/spinlock.h>
24 #include <linux/topology.h>
25 #include <linux/notifier.h>
26 #include <linux/property.h>
27 #include <linux/list.h>
28
29 #include <asm/byteorder.h>
30 #include <asm/errno.h>
31
32 typedef u32 phandle;
33 typedef u32 ihandle;
34
35 struct property {
36         char    *name;
37         int     length;
38         void    *value;
39         struct property *next;
40         unsigned long _flags;
41         unsigned int unique_id;
42         struct bin_attribute attr;
43 };
44
45 #if defined(CONFIG_SPARC)
46 struct of_irq_controller;
47 #endif
48
49 struct device_node {
50         const char *name;
51         const char *type;
52         phandle phandle;
53         const char *full_name;
54         struct fwnode_handle fwnode;
55
56         struct  property *properties;
57         struct  property *deadprops;    /* removed properties */
58         struct  device_node *parent;
59         struct  device_node *child;
60         struct  device_node *sibling;
61         struct  kobject kobj;
62         unsigned long _flags;
63         void    *data;
64 #if defined(CONFIG_SPARC)
65         const char *path_component_name;
66         unsigned int unique_id;
67         struct of_irq_controller *irq_trans;
68 #endif
69 };
70
71 #define MAX_PHANDLE_ARGS 16
72 struct of_phandle_args {
73         struct device_node *np;
74         int args_count;
75         uint32_t args[MAX_PHANDLE_ARGS];
76 };
77
78 struct of_reconfig_data {
79         struct device_node      *dn;
80         struct property         *prop;
81         struct property         *old_prop;
82 };
83
84 /* initialize a node */
85 extern struct kobj_type of_node_ktype;
86 static inline void of_node_init(struct device_node *node)
87 {
88         kobject_init(&node->kobj, &of_node_ktype);
89         node->fwnode.type = FWNODE_OF;
90 }
91
92 /* true when node is initialized */
93 static inline int of_node_is_initialized(struct device_node *node)
94 {
95         return node && node->kobj.state_initialized;
96 }
97
98 /* true when node is attached (i.e. present on sysfs) */
99 static inline int of_node_is_attached(struct device_node *node)
100 {
101         return node && node->kobj.state_in_sysfs;
102 }
103
104 #ifdef CONFIG_OF_DYNAMIC
105 extern struct device_node *of_node_get(struct device_node *node);
106 extern void of_node_put(struct device_node *node);
107 #else /* CONFIG_OF_DYNAMIC */
108 /* Dummy ref counting routines - to be implemented later */
109 static inline struct device_node *of_node_get(struct device_node *node)
110 {
111         return node;
112 }
113 static inline void of_node_put(struct device_node *node) { }
114 #endif /* !CONFIG_OF_DYNAMIC */
115
116 /* Pointer for first entry in chain of all nodes. */
117 extern struct device_node *of_root;
118 extern struct device_node *of_chosen;
119 extern struct device_node *of_aliases;
120 extern struct device_node *of_stdout;
121 extern raw_spinlock_t devtree_lock;
122
123 /* flag descriptions (need to be visible even when !CONFIG_OF) */
124 #define OF_DYNAMIC      1 /* node and properties were allocated via kmalloc */
125 #define OF_DETACHED     2 /* node has been detached from the device tree */
126 #define OF_POPULATED    3 /* device already created for the node */
127 #define OF_POPULATED_BUS        4 /* of_platform_populate recursed to children of this node */
128
129 #define OF_BAD_ADDR     ((u64)-1)
130
131 #ifdef CONFIG_OF
132 void of_core_init(void);
133
134 static inline bool is_of_node(struct fwnode_handle *fwnode)
135 {
136         return fwnode && fwnode->type == FWNODE_OF;
137 }
138
139 static inline struct device_node *to_of_node(struct fwnode_handle *fwnode)
140 {
141         return is_of_node(fwnode) ?
142                 container_of(fwnode, struct device_node, fwnode) : NULL;
143 }
144
145 static inline bool of_have_populated_dt(void)
146 {
147         return of_root != NULL;
148 }
149
150 static inline bool of_node_is_root(const struct device_node *node)
151 {
152         return node && (node->parent == NULL);
153 }
154
155 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
156 {
157         return test_bit(flag, &n->_flags);
158 }
159
160 static inline int of_node_test_and_set_flag(struct device_node *n,
161                                             unsigned long flag)
162 {
163         return test_and_set_bit(flag, &n->_flags);
164 }
165
166 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
167 {
168         set_bit(flag, &n->_flags);
169 }
170
171 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
172 {
173         clear_bit(flag, &n->_flags);
174 }
175
176 static inline int of_property_check_flag(struct property *p, unsigned long flag)
177 {
178         return test_bit(flag, &p->_flags);
179 }
180
181 static inline void of_property_set_flag(struct property *p, unsigned long flag)
182 {
183         set_bit(flag, &p->_flags);
184 }
185
186 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
187 {
188         clear_bit(flag, &p->_flags);
189 }
190
191 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
192 extern struct device_node *of_find_all_nodes(struct device_node *prev);
193
194 /*
195  * OF address retrieval & translation
196  */
197
198 /* Helper to read a big number; size is in cells (not bytes) */
199 static inline u64 of_read_number(const __be32 *cell, int size)
200 {
201         u64 r = 0;
202         for (; size--; cell++)
203                 r = (r << 32) | be32_to_cpu(*cell);
204         return r;
205 }
206
207 /* Like of_read_number, but we want an unsigned long result */
208 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
209 {
210         /* toss away upper bits if unsigned long is smaller than u64 */
211         return of_read_number(cell, size);
212 }
213
214 #if defined(CONFIG_SPARC)
215 #include <asm/prom.h>
216 #endif
217
218 /* Default #address and #size cells.  Allow arch asm/prom.h to override */
219 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
220 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
221 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
222 #endif
223
224 /* Default string compare functions, Allow arch asm/prom.h to override */
225 #if !defined(of_compat_cmp)
226 #define of_compat_cmp(s1, s2, l)        strcasecmp((s1), (s2))
227 #define of_prop_cmp(s1, s2)             strcmp((s1), (s2))
228 #define of_node_cmp(s1, s2)             strcasecmp((s1), (s2))
229 #endif
230
231 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
232 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
233
234 static inline const char *of_node_full_name(const struct device_node *np)
235 {
236         return np ? np->full_name : "<no-node>";
237 }
238
239 #define for_each_of_allnodes_from(from, dn) \
240         for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
241 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
242 extern struct device_node *of_find_node_by_name(struct device_node *from,
243         const char *name);
244 extern struct device_node *of_find_node_by_type(struct device_node *from,
245         const char *type);
246 extern struct device_node *of_find_compatible_node(struct device_node *from,
247         const char *type, const char *compat);
248 extern struct device_node *of_find_matching_node_and_match(
249         struct device_node *from,
250         const struct of_device_id *matches,
251         const struct of_device_id **match);
252
253 extern struct device_node *of_find_node_opts_by_path(const char *path,
254         const char **opts);
255 static inline struct device_node *of_find_node_by_path(const char *path)
256 {
257         return of_find_node_opts_by_path(path, NULL);
258 }
259
260 extern struct device_node *of_find_node_by_phandle(phandle handle);
261 extern struct device_node *of_get_parent(const struct device_node *node);
262 extern struct device_node *of_get_next_parent(struct device_node *node);
263 extern struct device_node *of_get_next_child(const struct device_node *node,
264                                              struct device_node *prev);
265 extern struct device_node *of_get_next_available_child(
266         const struct device_node *node, struct device_node *prev);
267
268 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
269                                         const char *compatible);
270 extern struct device_node *of_get_child_by_name(const struct device_node *node,
271                                         const char *name);
272
273 /* cache lookup */
274 extern struct device_node *of_find_next_cache_node(const struct device_node *);
275 extern struct device_node *of_find_node_with_property(
276         struct device_node *from, const char *prop_name);
277
278 extern struct property *of_find_property(const struct device_node *np,
279                                          const char *name,
280                                          int *lenp);
281 extern int of_property_count_elems_of_size(const struct device_node *np,
282                                 const char *propname, int elem_size);
283 extern int of_property_read_u32_index(const struct device_node *np,
284                                        const char *propname,
285                                        u32 index, u32 *out_value);
286 extern int of_property_read_u8_array(const struct device_node *np,
287                         const char *propname, u8 *out_values, size_t sz);
288 extern int of_property_read_u16_array(const struct device_node *np,
289                         const char *propname, u16 *out_values, size_t sz);
290 extern int of_property_read_u32_array(const struct device_node *np,
291                                       const char *propname,
292                                       u32 *out_values,
293                                       size_t sz);
294 extern int of_property_read_u64(const struct device_node *np,
295                                 const char *propname, u64 *out_value);
296 extern int of_property_read_u64_array(const struct device_node *np,
297                                       const char *propname,
298                                       u64 *out_values,
299                                       size_t sz);
300
301 extern int of_property_read_string(struct device_node *np,
302                                    const char *propname,
303                                    const char **out_string);
304 extern int of_property_match_string(struct device_node *np,
305                                     const char *propname,
306                                     const char *string);
307 extern int of_property_read_string_helper(struct device_node *np,
308                                               const char *propname,
309                                               const char **out_strs, size_t sz, int index);
310 extern int of_device_is_compatible(const struct device_node *device,
311                                    const char *);
312 extern bool of_device_is_available(const struct device_node *device);
313 extern bool of_device_is_big_endian(const struct device_node *device);
314 extern const void *of_get_property(const struct device_node *node,
315                                 const char *name,
316                                 int *lenp);
317 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
318 #define for_each_property_of_node(dn, pp) \
319         for (pp = dn->properties; pp != NULL; pp = pp->next)
320
321 extern int of_n_addr_cells(struct device_node *np);
322 extern int of_n_size_cells(struct device_node *np);
323 extern const struct of_device_id *of_match_node(
324         const struct of_device_id *matches, const struct device_node *node);
325 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
326 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
327 extern struct device_node *of_parse_phandle(const struct device_node *np,
328                                             const char *phandle_name,
329                                             int index);
330 extern int of_parse_phandle_with_args(const struct device_node *np,
331         const char *list_name, const char *cells_name, int index,
332         struct of_phandle_args *out_args);
333 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
334         const char *list_name, int cells_count, int index,
335         struct of_phandle_args *out_args);
336 extern int of_count_phandle_with_args(const struct device_node *np,
337         const char *list_name, const char *cells_name);
338
339 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
340 extern int of_alias_get_id(struct device_node *np, const char *stem);
341 extern int of_alias_get_highest_id(const char *stem);
342
343 extern int of_machine_is_compatible(const char *compat);
344
345 extern int of_add_property(struct device_node *np, struct property *prop);
346 extern int of_remove_property(struct device_node *np, struct property *prop);
347 extern int of_update_property(struct device_node *np, struct property *newprop);
348
349 /* For updating the device tree at runtime */
350 #define OF_RECONFIG_ATTACH_NODE         0x0001
351 #define OF_RECONFIG_DETACH_NODE         0x0002
352 #define OF_RECONFIG_ADD_PROPERTY        0x0003
353 #define OF_RECONFIG_REMOVE_PROPERTY     0x0004
354 #define OF_RECONFIG_UPDATE_PROPERTY     0x0005
355
356 extern int of_attach_node(struct device_node *);
357 extern int of_detach_node(struct device_node *);
358
359 #define of_match_ptr(_ptr)      (_ptr)
360
361 /*
362  * struct property *prop;
363  * const __be32 *p;
364  * u32 u;
365  *
366  * of_property_for_each_u32(np, "propname", prop, p, u)
367  *         printk("U32 value: %x\n", u);
368  */
369 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
370                                u32 *pu);
371 /*
372  * struct property *prop;
373  * const char *s;
374  *
375  * of_property_for_each_string(np, "propname", prop, s)
376  *         printk("String value: %s\n", s);
377  */
378 const char *of_prop_next_string(struct property *prop, const char *cur);
379
380 bool of_console_check(struct device_node *dn, char *name, int index);
381
382 #else /* CONFIG_OF */
383
384 static inline void of_core_init(void)
385 {
386 }
387
388 static inline bool is_of_node(struct fwnode_handle *fwnode)
389 {
390         return false;
391 }
392
393 static inline struct device_node *to_of_node(struct fwnode_handle *fwnode)
394 {
395         return NULL;
396 }
397
398 static inline const char* of_node_full_name(const struct device_node *np)
399 {
400         return "<no-node>";
401 }
402
403 static inline struct device_node *of_find_node_by_name(struct device_node *from,
404         const char *name)
405 {
406         return NULL;
407 }
408
409 static inline struct device_node *of_find_node_by_type(struct device_node *from,
410         const char *type)
411 {
412         return NULL;
413 }
414
415 static inline struct device_node *of_find_matching_node_and_match(
416         struct device_node *from,
417         const struct of_device_id *matches,
418         const struct of_device_id **match)
419 {
420         return NULL;
421 }
422
423 static inline struct device_node *of_find_node_by_path(const char *path)
424 {
425         return NULL;
426 }
427
428 static inline struct device_node *of_find_node_opts_by_path(const char *path,
429         const char **opts)
430 {
431         return NULL;
432 }
433
434 static inline struct device_node *of_find_node_by_phandle(phandle handle)
435 {
436         return NULL;
437 }
438
439 static inline struct device_node *of_get_parent(const struct device_node *node)
440 {
441         return NULL;
442 }
443
444 static inline struct device_node *of_get_next_child(
445         const struct device_node *node, struct device_node *prev)
446 {
447         return NULL;
448 }
449
450 static inline struct device_node *of_get_next_available_child(
451         const struct device_node *node, struct device_node *prev)
452 {
453         return NULL;
454 }
455
456 static inline struct device_node *of_find_node_with_property(
457         struct device_node *from, const char *prop_name)
458 {
459         return NULL;
460 }
461
462 static inline bool of_have_populated_dt(void)
463 {
464         return false;
465 }
466
467 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
468                                         const char *compatible)
469 {
470         return NULL;
471 }
472
473 static inline struct device_node *of_get_child_by_name(
474                                         const struct device_node *node,
475                                         const char *name)
476 {
477         return NULL;
478 }
479
480 static inline int of_device_is_compatible(const struct device_node *device,
481                                           const char *name)
482 {
483         return 0;
484 }
485
486 static inline bool of_device_is_available(const struct device_node *device)
487 {
488         return false;
489 }
490
491 static inline bool of_device_is_big_endian(const struct device_node *device)
492 {
493         return false;
494 }
495
496 static inline struct property *of_find_property(const struct device_node *np,
497                                                 const char *name,
498                                                 int *lenp)
499 {
500         return NULL;
501 }
502
503 static inline struct device_node *of_find_compatible_node(
504                                                 struct device_node *from,
505                                                 const char *type,
506                                                 const char *compat)
507 {
508         return NULL;
509 }
510
511 static inline int of_property_count_elems_of_size(const struct device_node *np,
512                         const char *propname, int elem_size)
513 {
514         return -ENOSYS;
515 }
516
517 static inline int of_property_read_u32_index(const struct device_node *np,
518                         const char *propname, u32 index, u32 *out_value)
519 {
520         return -ENOSYS;
521 }
522
523 static inline int of_property_read_u8_array(const struct device_node *np,
524                         const char *propname, u8 *out_values, size_t sz)
525 {
526         return -ENOSYS;
527 }
528
529 static inline int of_property_read_u16_array(const struct device_node *np,
530                         const char *propname, u16 *out_values, size_t sz)
531 {
532         return -ENOSYS;
533 }
534
535 static inline int of_property_read_u32_array(const struct device_node *np,
536                                              const char *propname,
537                                              u32 *out_values, size_t sz)
538 {
539         return -ENOSYS;
540 }
541
542 static inline int of_property_read_u64_array(const struct device_node *np,
543                                              const char *propname,
544                                              u64 *out_values, size_t sz)
545 {
546         return -ENOSYS;
547 }
548
549 static inline int of_property_read_string(struct device_node *np,
550                                           const char *propname,
551                                           const char **out_string)
552 {
553         return -ENOSYS;
554 }
555
556 static inline int of_property_read_string_helper(struct device_node *np,
557                                                  const char *propname,
558                                                  const char **out_strs, size_t sz, int index)
559 {
560         return -ENOSYS;
561 }
562
563 static inline const void *of_get_property(const struct device_node *node,
564                                 const char *name,
565                                 int *lenp)
566 {
567         return NULL;
568 }
569
570 static inline struct device_node *of_get_cpu_node(int cpu,
571                                         unsigned int *thread)
572 {
573         return NULL;
574 }
575
576 static inline int of_property_read_u64(const struct device_node *np,
577                                        const char *propname, u64 *out_value)
578 {
579         return -ENOSYS;
580 }
581
582 static inline int of_property_match_string(struct device_node *np,
583                                            const char *propname,
584                                            const char *string)
585 {
586         return -ENOSYS;
587 }
588
589 static inline struct device_node *of_parse_phandle(const struct device_node *np,
590                                                    const char *phandle_name,
591                                                    int index)
592 {
593         return NULL;
594 }
595
596 static inline int of_parse_phandle_with_args(struct device_node *np,
597                                              const char *list_name,
598                                              const char *cells_name,
599                                              int index,
600                                              struct of_phandle_args *out_args)
601 {
602         return -ENOSYS;
603 }
604
605 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
606         const char *list_name, int cells_count, int index,
607         struct of_phandle_args *out_args)
608 {
609         return -ENOSYS;
610 }
611
612 static inline int of_count_phandle_with_args(struct device_node *np,
613                                              const char *list_name,
614                                              const char *cells_name)
615 {
616         return -ENOSYS;
617 }
618
619 static inline int of_alias_get_id(struct device_node *np, const char *stem)
620 {
621         return -ENOSYS;
622 }
623
624 static inline int of_alias_get_highest_id(const char *stem)
625 {
626         return -ENOSYS;
627 }
628
629 static inline int of_machine_is_compatible(const char *compat)
630 {
631         return 0;
632 }
633
634 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
635 {
636         return false;
637 }
638
639 static inline const __be32 *of_prop_next_u32(struct property *prop,
640                 const __be32 *cur, u32 *pu)
641 {
642         return NULL;
643 }
644
645 static inline const char *of_prop_next_string(struct property *prop,
646                 const char *cur)
647 {
648         return NULL;
649 }
650
651 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
652 {
653         return 0;
654 }
655
656 static inline int of_node_test_and_set_flag(struct device_node *n,
657                                             unsigned long flag)
658 {
659         return 0;
660 }
661
662 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
663 {
664 }
665
666 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
667 {
668 }
669
670 static inline int of_property_check_flag(struct property *p, unsigned long flag)
671 {
672         return 0;
673 }
674
675 static inline void of_property_set_flag(struct property *p, unsigned long flag)
676 {
677 }
678
679 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
680 {
681 }
682
683 #define of_match_ptr(_ptr)      NULL
684 #define of_match_node(_matches, _node)  NULL
685 #endif /* CONFIG_OF */
686
687 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
688 extern int of_node_to_nid(struct device_node *np);
689 #else
690 static inline int of_node_to_nid(struct device_node *device)
691 {
692         return NUMA_NO_NODE;
693 }
694 #endif
695
696 static inline struct device_node *of_find_matching_node(
697         struct device_node *from,
698         const struct of_device_id *matches)
699 {
700         return of_find_matching_node_and_match(from, matches, NULL);
701 }
702
703 /**
704  * of_property_count_u8_elems - Count the number of u8 elements in a property
705  *
706  * @np:         device node from which the property value is to be read.
707  * @propname:   name of the property to be searched.
708  *
709  * Search for a property in a device node and count the number of u8 elements
710  * in it. Returns number of elements on sucess, -EINVAL if the property does
711  * not exist or its length does not match a multiple of u8 and -ENODATA if the
712  * property does not have a value.
713  */
714 static inline int of_property_count_u8_elems(const struct device_node *np,
715                                 const char *propname)
716 {
717         return of_property_count_elems_of_size(np, propname, sizeof(u8));
718 }
719
720 /**
721  * of_property_count_u16_elems - Count the number of u16 elements in a property
722  *
723  * @np:         device node from which the property value is to be read.
724  * @propname:   name of the property to be searched.
725  *
726  * Search for a property in a device node and count the number of u16 elements
727  * in it. Returns number of elements on sucess, -EINVAL if the property does
728  * not exist or its length does not match a multiple of u16 and -ENODATA if the
729  * property does not have a value.
730  */
731 static inline int of_property_count_u16_elems(const struct device_node *np,
732                                 const char *propname)
733 {
734         return of_property_count_elems_of_size(np, propname, sizeof(u16));
735 }
736
737 /**
738  * of_property_count_u32_elems - Count the number of u32 elements in a property
739  *
740  * @np:         device node from which the property value is to be read.
741  * @propname:   name of the property to be searched.
742  *
743  * Search for a property in a device node and count the number of u32 elements
744  * in it. Returns number of elements on sucess, -EINVAL if the property does
745  * not exist or its length does not match a multiple of u32 and -ENODATA if the
746  * property does not have a value.
747  */
748 static inline int of_property_count_u32_elems(const struct device_node *np,
749                                 const char *propname)
750 {
751         return of_property_count_elems_of_size(np, propname, sizeof(u32));
752 }
753
754 /**
755  * of_property_count_u64_elems - Count the number of u64 elements in a property
756  *
757  * @np:         device node from which the property value is to be read.
758  * @propname:   name of the property to be searched.
759  *
760  * Search for a property in a device node and count the number of u64 elements
761  * in it. Returns number of elements on sucess, -EINVAL if the property does
762  * not exist or its length does not match a multiple of u64 and -ENODATA if the
763  * property does not have a value.
764  */
765 static inline int of_property_count_u64_elems(const struct device_node *np,
766                                 const char *propname)
767 {
768         return of_property_count_elems_of_size(np, propname, sizeof(u64));
769 }
770
771 /**
772  * of_property_read_string_array() - Read an array of strings from a multiple
773  * strings property.
774  * @np:         device node from which the property value is to be read.
775  * @propname:   name of the property to be searched.
776  * @out_strs:   output array of string pointers.
777  * @sz:         number of array elements to read.
778  *
779  * Search for a property in a device tree node and retrieve a list of
780  * terminated string values (pointer to data, not a copy) in that property.
781  *
782  * If @out_strs is NULL, the number of strings in the property is returned.
783  */
784 static inline int of_property_read_string_array(struct device_node *np,
785                                                 const char *propname, const char **out_strs,
786                                                 size_t sz)
787 {
788         return of_property_read_string_helper(np, propname, out_strs, sz, 0);
789 }
790
791 /**
792  * of_property_count_strings() - Find and return the number of strings from a
793  * multiple strings property.
794  * @np:         device node from which the property value is to be read.
795  * @propname:   name of the property to be searched.
796  *
797  * Search for a property in a device tree node and retrieve the number of null
798  * terminated string contain in it. Returns the number of strings on
799  * success, -EINVAL if the property does not exist, -ENODATA if property
800  * does not have a value, and -EILSEQ if the string is not null-terminated
801  * within the length of the property data.
802  */
803 static inline int of_property_count_strings(struct device_node *np,
804                                             const char *propname)
805 {
806         return of_property_read_string_helper(np, propname, NULL, 0, 0);
807 }
808
809 /**
810  * of_property_read_string_index() - Find and read a string from a multiple
811  * strings property.
812  * @np:         device node from which the property value is to be read.
813  * @propname:   name of the property to be searched.
814  * @index:      index of the string in the list of strings
815  * @out_string: pointer to null terminated return string, modified only if
816  *              return value is 0.
817  *
818  * Search for a property in a device tree node and retrieve a null
819  * terminated string value (pointer to data, not a copy) in the list of strings
820  * contained in that property.
821  * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
822  * property does not have a value, and -EILSEQ if the string is not
823  * null-terminated within the length of the property data.
824  *
825  * The out_string pointer is modified only if a valid string can be decoded.
826  */
827 static inline int of_property_read_string_index(struct device_node *np,
828                                                 const char *propname,
829                                                 int index, const char **output)
830 {
831         int rc = of_property_read_string_helper(np, propname, output, 1, index);
832         return rc < 0 ? rc : 0;
833 }
834
835 /**
836  * of_property_read_bool - Findfrom a property
837  * @np:         device node from which the property value is to be read.
838  * @propname:   name of the property to be searched.
839  *
840  * Search for a property in a device node.
841  * Returns true if the property exists false otherwise.
842  */
843 static inline bool of_property_read_bool(const struct device_node *np,
844                                          const char *propname)
845 {
846         struct property *prop = of_find_property(np, propname, NULL);
847
848         return prop ? true : false;
849 }
850
851 static inline int of_property_read_u8(const struct device_node *np,
852                                        const char *propname,
853                                        u8 *out_value)
854 {
855         return of_property_read_u8_array(np, propname, out_value, 1);
856 }
857
858 static inline int of_property_read_u16(const struct device_node *np,
859                                        const char *propname,
860                                        u16 *out_value)
861 {
862         return of_property_read_u16_array(np, propname, out_value, 1);
863 }
864
865 static inline int of_property_read_u32(const struct device_node *np,
866                                        const char *propname,
867                                        u32 *out_value)
868 {
869         return of_property_read_u32_array(np, propname, out_value, 1);
870 }
871
872 static inline int of_property_read_s32(const struct device_node *np,
873                                        const char *propname,
874                                        s32 *out_value)
875 {
876         return of_property_read_u32(np, propname, (u32*) out_value);
877 }
878
879 #define of_property_for_each_u32(np, propname, prop, p, u)      \
880         for (prop = of_find_property(np, propname, NULL),       \
881                 p = of_prop_next_u32(prop, NULL, &u);           \
882                 p;                                              \
883                 p = of_prop_next_u32(prop, p, &u))
884
885 #define of_property_for_each_string(np, propname, prop, s)      \
886         for (prop = of_find_property(np, propname, NULL),       \
887                 s = of_prop_next_string(prop, NULL);            \
888                 s;                                              \
889                 s = of_prop_next_string(prop, s))
890
891 #define for_each_node_by_name(dn, name) \
892         for (dn = of_find_node_by_name(NULL, name); dn; \
893              dn = of_find_node_by_name(dn, name))
894 #define for_each_node_by_type(dn, type) \
895         for (dn = of_find_node_by_type(NULL, type); dn; \
896              dn = of_find_node_by_type(dn, type))
897 #define for_each_compatible_node(dn, type, compatible) \
898         for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
899              dn = of_find_compatible_node(dn, type, compatible))
900 #define for_each_matching_node(dn, matches) \
901         for (dn = of_find_matching_node(NULL, matches); dn; \
902              dn = of_find_matching_node(dn, matches))
903 #define for_each_matching_node_and_match(dn, matches, match) \
904         for (dn = of_find_matching_node_and_match(NULL, matches, match); \
905              dn; dn = of_find_matching_node_and_match(dn, matches, match))
906
907 #define for_each_child_of_node(parent, child) \
908         for (child = of_get_next_child(parent, NULL); child != NULL; \
909              child = of_get_next_child(parent, child))
910 #define for_each_available_child_of_node(parent, child) \
911         for (child = of_get_next_available_child(parent, NULL); child != NULL; \
912              child = of_get_next_available_child(parent, child))
913
914 #define for_each_node_with_property(dn, prop_name) \
915         for (dn = of_find_node_with_property(NULL, prop_name); dn; \
916              dn = of_find_node_with_property(dn, prop_name))
917
918 static inline int of_get_child_count(const struct device_node *np)
919 {
920         struct device_node *child;
921         int num = 0;
922
923         for_each_child_of_node(np, child)
924                 num++;
925
926         return num;
927 }
928
929 static inline int of_get_available_child_count(const struct device_node *np)
930 {
931         struct device_node *child;
932         int num = 0;
933
934         for_each_available_child_of_node(np, child)
935                 num++;
936
937         return num;
938 }
939
940 #ifdef CONFIG_OF
941 #define _OF_DECLARE(table, name, compat, fn, fn_type)                   \
942         static const struct of_device_id __of_table_##name              \
943                 __used __section(__##table##_of_table)                  \
944                 __aligned(__alignof__(struct of_device_id))             \
945                  = { .compatible = compat,                              \
946                      .data = (fn == (fn_type)NULL) ? fn : fn  }
947 #else
948 #define _OF_DECLARE(table, name, compat, fn, fn_type)                   \
949         static const struct of_device_id __of_table_##name              \
950                 __attribute__((unused))                                 \
951                  = { .compatible = compat,                              \
952                      .data = (fn == (fn_type)NULL) ? fn : fn }
953 #endif
954
955 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
956 typedef void (*of_init_fn_1)(struct device_node *);
957
958 #define OF_DECLARE_1(table, name, compat, fn) \
959                 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
960 #define OF_DECLARE_2(table, name, compat, fn) \
961                 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
962
963 /**
964  * struct of_changeset_entry    - Holds a changeset entry
965  *
966  * @node:       list_head for the log list
967  * @action:     notifier action
968  * @np:         pointer to the device node affected
969  * @prop:       pointer to the property affected
970  * @old_prop:   hold a pointer to the original property
971  *
972  * Every modification of the device tree during a changeset
973  * is held in a list of of_changeset_entry structures.
974  * That way we can recover from a partial application, or we can
975  * revert the changeset
976  */
977 struct of_changeset_entry {
978         struct list_head node;
979         unsigned long action;
980         struct device_node *np;
981         struct property *prop;
982         struct property *old_prop;
983 };
984
985 /**
986  * struct of_changeset - changeset tracker structure
987  *
988  * @entries:    list_head for the changeset entries
989  *
990  * changesets are a convenient way to apply bulk changes to the
991  * live tree. In case of an error, changes are rolled-back.
992  * changesets live on after initial application, and if not
993  * destroyed after use, they can be reverted in one single call.
994  */
995 struct of_changeset {
996         struct list_head entries;
997 };
998
999 enum of_reconfig_change {
1000         OF_RECONFIG_NO_CHANGE = 0,
1001         OF_RECONFIG_CHANGE_ADD,
1002         OF_RECONFIG_CHANGE_REMOVE,
1003 };
1004
1005 #ifdef CONFIG_OF_DYNAMIC
1006 extern int of_reconfig_notifier_register(struct notifier_block *);
1007 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1008 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1009 extern int of_reconfig_get_state_change(unsigned long action,
1010                                         struct of_reconfig_data *arg);
1011
1012 extern void of_changeset_init(struct of_changeset *ocs);
1013 extern void of_changeset_destroy(struct of_changeset *ocs);
1014 extern int of_changeset_apply(struct of_changeset *ocs);
1015 extern int of_changeset_revert(struct of_changeset *ocs);
1016 extern int of_changeset_action(struct of_changeset *ocs,
1017                 unsigned long action, struct device_node *np,
1018                 struct property *prop);
1019
1020 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1021                 struct device_node *np)
1022 {
1023         return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1024 }
1025
1026 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1027                 struct device_node *np)
1028 {
1029         return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1030 }
1031
1032 static inline int of_changeset_add_property(struct of_changeset *ocs,
1033                 struct device_node *np, struct property *prop)
1034 {
1035         return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1036 }
1037
1038 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1039                 struct device_node *np, struct property *prop)
1040 {
1041         return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1042 }
1043
1044 static inline int of_changeset_update_property(struct of_changeset *ocs,
1045                 struct device_node *np, struct property *prop)
1046 {
1047         return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1048 }
1049 #else /* CONFIG_OF_DYNAMIC */
1050 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1051 {
1052         return -EINVAL;
1053 }
1054 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1055 {
1056         return -EINVAL;
1057 }
1058 static inline int of_reconfig_notify(unsigned long action,
1059                                      struct of_reconfig_data *arg)
1060 {
1061         return -EINVAL;
1062 }
1063 static inline int of_reconfig_get_state_change(unsigned long action,
1064                                                 struct of_reconfig_data *arg)
1065 {
1066         return -EINVAL;
1067 }
1068 #endif /* CONFIG_OF_DYNAMIC */
1069
1070 /* CONFIG_OF_RESOLVE api */
1071 extern int of_resolve_phandles(struct device_node *tree);
1072
1073 /**
1074  * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1075  * @np: Pointer to the given device_node
1076  *
1077  * return true if present false otherwise
1078  */
1079 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1080 {
1081         return of_property_read_bool(np, "system-power-controller");
1082 }
1083
1084 /**
1085  * Overlay support
1086  */
1087
1088 #ifdef CONFIG_OF_OVERLAY
1089
1090 /* ID based overlays; the API for external users */
1091 int of_overlay_create(struct device_node *tree);
1092 int of_overlay_destroy(int id);
1093 int of_overlay_destroy_all(void);
1094
1095 #else
1096
1097 static inline int of_overlay_create(struct device_node *tree)
1098 {
1099         return -ENOTSUPP;
1100 }
1101
1102 static inline int of_overlay_destroy(int id)
1103 {
1104         return -ENOTSUPP;
1105 }
1106
1107 static inline int of_overlay_destroy_all(void)
1108 {
1109         return -ENOTSUPP;
1110 }
1111
1112 #endif
1113
1114 #endif /* _LINUX_OF_H */