]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - include/linux/of.h
media: v4l2-core: Migration from upstream
[sojka/nv-tegra/linux-3.10.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/kref.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/spinlock.h>
24 #include <linux/topology.h>
25 #include <linux/notifier.h>
26
27 #include <asm/byteorder.h>
28 #include <asm/errno.h>
29
30 typedef u32 phandle;
31 typedef u32 ihandle;
32
33 struct property {
34         char    *name;
35         int     length;
36         void    *value;
37         struct property *next;
38         unsigned long _flags;
39         unsigned int unique_id;
40 };
41
42 #if defined(CONFIG_SPARC)
43 struct of_irq_controller;
44 #endif
45
46 struct device_node {
47         const char *name;
48         const char *type;
49         phandle phandle;
50         const char *full_name;
51
52         struct  property *properties;
53         struct  property *deadprops;    /* removed properties */
54         struct  device_node *parent;
55         struct  device_node *child;
56         struct  device_node *sibling;
57         struct  device_node *next;      /* next device of same type */
58         struct  device_node *allnext;   /* next in list of all nodes */
59         struct  proc_dir_entry *pde;    /* this node's proc directory */
60         struct  kref kref;
61         unsigned long _flags;
62         void    *data;
63 #if defined(CONFIG_SPARC)
64         const char *path_component_name;
65         unsigned int unique_id;
66         struct of_irq_controller *irq_trans;
67 #endif
68 };
69
70 #define MAX_PHANDLE_ARGS 16
71 struct of_phandle_args {
72         struct device_node *np;
73         int args_count;
74         uint32_t args[MAX_PHANDLE_ARGS];
75 };
76
77 /*
78  * keep the state at iterating a list of phandles with variable number
79  * of args
80  */
81 struct of_phandle_iter {
82         const __be32 *cur; /* current phandle */
83         const __be32 *end; /* end of the last phandle */
84         const char *cells_name;
85         int cell_count;
86         struct of_phandle_args out_args;
87 };
88
89 #ifdef CONFIG_OF_DYNAMIC
90 extern struct device_node *of_node_get(struct device_node *node);
91 extern void of_node_put(struct device_node *node);
92 #else /* CONFIG_OF_DYNAMIC */
93 /* Dummy ref counting routines - to be implemented later */
94 static inline struct device_node *of_node_get(struct device_node *node)
95 {
96         return node;
97 }
98 static inline void of_node_put(struct device_node *node) { }
99 #endif /* !CONFIG_OF_DYNAMIC */
100
101 #ifdef CONFIG_OF
102
103 /* Pointer for first entry in chain of all nodes. */
104 extern struct device_node *of_allnodes;
105 extern struct device_node *of_chosen;
106 extern struct device_node *of_aliases;
107 extern raw_spinlock_t devtree_lock;
108
109 static inline bool of_have_populated_dt(void)
110 {
111         return of_allnodes != NULL;
112 }
113
114 static inline bool of_node_is_root(const struct device_node *node)
115 {
116         return node && (node->parent == NULL);
117 }
118
119 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
120 {
121         return test_bit(flag, &n->_flags);
122 }
123
124 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
125 {
126         set_bit(flag, &n->_flags);
127 }
128
129 extern struct device_node *of_find_all_nodes(struct device_node *prev);
130
131 /*
132  * OF address retrieval & translation
133  */
134
135 /* Helper to read a big number; size is in cells (not bytes) */
136 static inline u64 of_read_number(const __be32 *cell, int size)
137 {
138         u64 r = 0;
139         while (size--)
140                 r = (r << 32) | be32_to_cpu(*(cell++));
141         return r;
142 }
143
144 /* Like of_read_number, but we want an unsigned long result */
145 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
146 {
147         /* toss away upper bits if unsigned long is smaller than u64 */
148         return of_read_number(cell, size);
149 }
150
151 #include <asm/prom.h>
152
153 /* Default #address and #size cells.  Allow arch asm/prom.h to override */
154 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
155 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
156 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
157 #endif
158
159 /* Default string compare functions, Allow arch asm/prom.h to override */
160 #if !defined(of_compat_cmp)
161 #define of_compat_cmp(s1, s2, l)        strcasecmp((s1), (s2))
162 #define of_prop_cmp(s1, s2)             strcmp((s1), (s2))
163 #define of_node_cmp(s1, s2)             strcasecmp((s1), (s2))
164 #endif
165
166 /* flag descriptions */
167 #define OF_DYNAMIC      1 /* node and properties were allocated via kmalloc */
168 #define OF_DETACHED     2 /* node has been detached from the device tree */
169
170 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
171 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
172
173 #define OF_BAD_ADDR     ((u64)-1)
174
175 static inline const char *of_node_full_name(const struct device_node *np)
176 {
177         return np ? np->full_name : "<no-node>";
178 }
179
180 extern struct device_node *of_find_node_by_name(struct device_node *from,
181         const char *name);
182 #define for_each_node_by_name(dn, name) \
183         for (dn = of_find_node_by_name(NULL, name); dn; \
184              dn = of_find_node_by_name(dn, name))
185 extern struct device_node *of_find_node_by_type(struct device_node *from,
186         const char *type);
187 #define for_each_node_by_type(dn, type) \
188         for (dn = of_find_node_by_type(NULL, type); dn; \
189              dn = of_find_node_by_type(dn, type))
190 extern struct device_node *of_find_compatible_node(struct device_node *from,
191         const char *type, const char *compat);
192 #define for_each_compatible_node(dn, type, compatible) \
193         for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
194              dn = of_find_compatible_node(dn, type, compatible))
195 extern struct device_node *of_find_matching_node_and_match(
196         struct device_node *from,
197         const struct of_device_id *matches,
198         const struct of_device_id **match);
199 static inline struct device_node *of_find_matching_node(
200         struct device_node *from,
201         const struct of_device_id *matches)
202 {
203         return of_find_matching_node_and_match(from, matches, NULL);
204 }
205 #define for_each_matching_node(dn, matches) \
206         for (dn = of_find_matching_node(NULL, matches); dn; \
207              dn = of_find_matching_node(dn, matches))
208 #define for_each_matching_node_and_match(dn, matches, match) \
209         for (dn = of_find_matching_node_and_match(NULL, matches, match); \
210              dn; dn = of_find_matching_node_and_match(dn, matches, match))
211 extern struct device_node *of_find_node_by_path(const char *path);
212 extern struct device_node *of_find_node_by_phandle(phandle handle);
213 extern struct device_node *of_get_parent(const struct device_node *node);
214 extern struct device_node *of_get_next_parent(struct device_node *node);
215 extern struct device_node *of_get_next_child(const struct device_node *node,
216                                              struct device_node *prev);
217 extern struct device_node *of_get_next_available_child(
218         const struct device_node *node, struct device_node *prev);
219
220 extern struct device_node *of_get_child_by_name(const struct device_node *node,
221                                         const char *name);
222 #define for_each_child_of_node(parent, child) \
223         for (child = of_get_next_child(parent, NULL); child != NULL; \
224              child = of_get_next_child(parent, child))
225
226 #define for_each_available_child_of_node(parent, child) \
227         for (child = of_get_next_available_child(parent, NULL); child != NULL; \
228              child = of_get_next_available_child(parent, child))
229
230 static inline int of_get_child_count(const struct device_node *np)
231 {
232         struct device_node *child;
233         int num = 0;
234
235         for_each_child_of_node(np, child)
236                 num++;
237
238         return num;
239 }
240
241 extern struct device_node *of_find_node_with_property(
242         struct device_node *from, const char *prop_name);
243 #define for_each_node_with_property(dn, prop_name) \
244         for (dn = of_find_node_with_property(NULL, prop_name); dn; \
245              dn = of_find_node_with_property(dn, prop_name))
246
247 extern struct property *of_find_property(const struct device_node *np,
248                                          const char *name,
249                                          int *lenp);
250 extern int of_property_read_u32_index(const struct device_node *np,
251                                        const char *propname,
252                                        u32 index, u32 *out_value);
253 extern int of_property_read_u8_array(const struct device_node *np,
254                         const char *propname, u8 *out_values, size_t sz);
255 extern int of_property_read_u16_array(const struct device_node *np,
256                         const char *propname, u16 *out_values, size_t sz);
257 extern int of_property_read_u32_array(const struct device_node *np,
258                                       const char *propname,
259                                       u32 *out_values,
260                                       size_t sz);
261 extern int of_property_read_u64(const struct device_node *np,
262                                 const char *propname, u64 *out_value);
263 extern int of_property_read_u64_array(const struct device_node *np,
264                                       const char *propname,
265                                       u64 *out_values,
266                                       size_t sz);
267
268 extern int of_property_read_string(struct device_node *np,
269                                    const char *propname,
270                                    const char **out_string);
271 extern int of_property_match_string(struct device_node *np,
272                                     const char *propname,
273                                     const char *string);
274 extern int of_property_read_string_helper(struct device_node *np,
275                                               const char *propname,
276                                               const char **out_strs, size_t sz, int index);
277 extern int of_device_is_compatible(const struct device_node *device,
278                                    const char *);
279 extern int of_device_is_available(const struct device_node *device);
280 extern const void *of_get_property(const struct device_node *node,
281                                 const char *name,
282                                 int *lenp);
283 #define for_each_property_of_node(dn, pp) \
284         for (pp = dn->properties; pp != NULL; pp = pp->next)
285
286 extern int of_n_addr_cells(struct device_node *np);
287 extern int of_n_size_cells(struct device_node *np);
288 extern const struct of_device_id *of_match_node(
289         const struct of_device_id *matches, const struct device_node *node);
290 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
291 extern struct device_node *of_parse_phandle(const struct device_node *np,
292                                             const char *phandle_name,
293                                             int index);
294 extern int of_parse_phandle_with_args(const struct device_node *np,
295         const char *list_name, const char *cells_name, int index,
296         struct of_phandle_args *out_args);
297 extern int of_count_phandle_with_args(const struct device_node *np,
298         const char *list_name, const char *cells_name);
299
300 extern void of_phandle_iter_start(struct of_phandle_iter *iter,
301                                   const struct device_node *np,
302                                   const char *list_name,
303                                   const char *cells_name, int cell_count);
304 extern void of_phandle_iter_next(struct of_phandle_iter *iter);
305
306 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
307 extern int of_alias_get_id(struct device_node *np, const char *stem);
308 extern int of_alias_get_max_id(const char *stem);
309
310 extern int of_machine_is_compatible(const char *compat);
311
312 extern int of_add_property(struct device_node *np, struct property *prop);
313 extern int of_remove_property(struct device_node *np, struct property *prop);
314 extern int of_update_property(struct device_node *np, struct property *newprop);
315
316 /* For updating the device tree at runtime */
317 #define OF_RECONFIG_ATTACH_NODE         0x0001
318 #define OF_RECONFIG_DETACH_NODE         0x0002
319 #define OF_RECONFIG_ADD_PROPERTY        0x0003
320 #define OF_RECONFIG_REMOVE_PROPERTY     0x0004
321 #define OF_RECONFIG_UPDATE_PROPERTY     0x0005
322
323 struct of_prop_reconfig {
324         struct device_node      *dn;
325         struct property         *prop;
326 };
327
328 extern int of_reconfig_notifier_register(struct notifier_block *);
329 extern int of_reconfig_notifier_unregister(struct notifier_block *);
330 extern int of_reconfig_notify(unsigned long, void *);
331
332 extern int of_attach_node(struct device_node *);
333 extern int of_detach_node(struct device_node *);
334
335 #define of_match_ptr(_ptr)      (_ptr)
336
337 /*
338  * struct property *prop;
339  * const __be32 *p;
340  * u32 u;
341  *
342  * of_property_for_each_u32(np, "propname", prop, p, u)
343  *         printk("U32 value: %x\n", u);
344  */
345 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
346                                u32 *pu);
347 #define of_property_for_each_u32(np, propname, prop, p, u)      \
348         for (prop = of_find_property(np, propname, NULL),       \
349                 p = of_prop_next_u32(prop, NULL, &u);           \
350                 p;                                              \
351                 p = of_prop_next_u32(prop, p, &u))
352
353 /*
354  * struct property *prop;
355  * const char *s;
356  *
357  * of_property_for_each_string(np, "propname", prop, s)
358  *         printk("String value: %s\n", s);
359  */
360 const char *of_prop_next_string(struct property *prop, const char *cur);
361 #define of_property_for_each_string(np, propname, prop, s)      \
362         for (prop = of_find_property(np, propname, NULL),       \
363                 s = of_prop_next_string(prop, NULL);            \
364                 s;                                              \
365                 s = of_prop_next_string(prop, s))
366
367 #define of_property_for_each_phandle_with_args(iter, np, list_name,     \
368                                                cells_name, cell_count)  \
369         for (of_phandle_iter_start(&iter, np, list_name,                \
370                                    cells_name, cell_count);             \
371              iter.cur; of_phandle_iter_next(&iter))
372
373 #else /* CONFIG_OF */
374
375 static inline const char* of_node_full_name(struct device_node *np)
376 {
377         return "<no-node>";
378 }
379
380 static inline struct device_node *of_find_node_by_name(struct device_node *from,
381         const char *name)
382 {
383         return NULL;
384 }
385
386 static inline struct device_node *of_get_parent(const struct device_node *node)
387 {
388         return NULL;
389 }
390
391 static inline bool of_have_populated_dt(void)
392 {
393         return false;
394 }
395
396 #define for_each_child_of_node(parent, child) \
397         while (0)
398
399 static inline struct device_node *of_get_child_by_name(
400                                         const struct device_node *node,
401                                         const char *name)
402 {
403         return NULL;
404 }
405
406 static inline int of_get_child_count(const struct device_node *np)
407 {
408         return 0;
409 }
410
411 static inline int of_device_is_compatible(const struct device_node *device,
412                                           const char *name)
413 {
414         return 0;
415 }
416
417 static inline int of_device_is_available(const struct device_node *device)
418 {
419         return 0;
420 }
421
422 static inline struct property *of_find_property(const struct device_node *np,
423                                                 const char *name,
424                                                 int *lenp)
425 {
426         return NULL;
427 }
428
429 static inline struct device_node *of_find_compatible_node(
430                                                 struct device_node *from,
431                                                 const char *type,
432                                                 const char *compat)
433 {
434         return NULL;
435 }
436
437 static inline int of_property_read_u32_index(const struct device_node *np,
438                         const char *propname, u32 index, u32 *out_value)
439 {
440         return -ENOSYS;
441 }
442
443 static inline int of_property_read_u8_array(const struct device_node *np,
444                         const char *propname, u8 *out_values, size_t sz)
445 {
446         return -ENOSYS;
447 }
448
449 static inline int of_property_read_u16_array(const struct device_node *np,
450                         const char *propname, u16 *out_values, size_t sz)
451 {
452         return -ENOSYS;
453 }
454
455 static inline int of_property_read_u32_array(const struct device_node *np,
456                                              const char *propname,
457                                              u32 *out_values, size_t sz)
458 {
459         return -ENOSYS;
460 }
461
462 static inline int of_property_read_u64_array(const struct device_node *np,
463                                              const char *propname,
464                                              u64 *out_values, size_t sz)
465 {
466         return -ENOSYS;
467 }
468
469 static inline int of_property_read_string(struct device_node *np,
470                                           const char *propname,
471                                           const char **out_string)
472 {
473         return -ENOSYS;
474 }
475
476 static inline int of_property_read_string_helper(struct device_node *np,
477                                                  const char *propname,
478                                                  const char **out_strs, size_t sz, int index)
479 {
480         return -ENOSYS;
481 }
482
483 static inline const void *of_get_property(const struct device_node *node,
484                                 const char *name,
485                                 int *lenp)
486 {
487         return NULL;
488 }
489
490 static inline int of_property_read_u64(const struct device_node *np,
491                                        const char *propname, u64 *out_value)
492 {
493         return -ENOSYS;
494 }
495
496 static inline int of_property_match_string(struct device_node *np,
497                                            const char *propname,
498                                            const char *string)
499 {
500         return -ENOSYS;
501 }
502
503 static inline struct device_node *of_parse_phandle(const struct device_node *np,
504                                                    const char *phandle_name,
505                                                    int index)
506 {
507         return NULL;
508 }
509
510 static inline int of_parse_phandle_with_args(struct device_node *np,
511                                              const char *list_name,
512                                              const char *cells_name,
513                                              int index,
514                                              struct of_phandle_args *out_args)
515 {
516         return -ENOSYS;
517 }
518
519 static inline int of_count_phandle_with_args(struct device_node *np,
520                                              const char *list_name,
521                                              const char *cells_name)
522 {
523         return -ENOSYS;
524 }
525
526 static inline void of_phandle_iter_start(struct of_phandle_iter *iter,
527                                          const struct device_node *np,
528                                          const char *list_name,
529                                          const char *cells_name,
530                                          int cell_count);
531 {
532 }
533
534 static inline void of_phandle_iter_next(struct of_phandle_iter *iter)
535 {
536 }
537
538 static inline int of_alias_get_id(struct device_node *np, const char *stem)
539 {
540         return -ENOSYS;
541 }
542
543 static inline int of_alias_get_max_id(const char *stem)
544 {
545         return -ENODEV;
546 }
547
548 static inline int of_machine_is_compatible(const char *compat)
549 {
550         return 0;
551 }
552
553 #define of_match_ptr(_ptr)      NULL
554 #define of_match_node(_matches, _node)  NULL
555 #define of_property_for_each_u32(np, propname, prop, p, u) \
556         while (0)
557 #define of_property_for_each_string(np, propname, prop, s) \
558         while (0)
559 #endif /* CONFIG_OF */
560
561 #ifndef of_node_to_nid
562 static inline int of_node_to_nid(struct device_node *np)
563 {
564         return numa_node_id();
565 }
566
567 #define of_node_to_nid of_node_to_nid
568 #endif
569
570 /**
571  * of_property_read_string_array() - Read an array of strings from a multiple
572  * strings property.
573  * @np:         device node from which the property value is to be read.
574  * @propname:   name of the property to be searched.
575  * @out_strs:   output array of string pointers.
576  * @sz:         number of array elements to read.
577  *
578  * Search for a property in a device tree node and retrieve a list of
579  * terminated string values (pointer to data, not a copy) in that property.
580  *
581  * If @out_strs is NULL, the number of strings in the property is returned.
582  */
583 static inline int of_property_read_string_array(struct device_node *np,
584                                                 const char *propname, const char **out_strs,
585                                                 size_t sz)
586 {
587         return of_property_read_string_helper(np, propname, out_strs, sz, 0);
588 }
589
590 /**
591  * of_property_count_strings() - Find and return the number of strings from a
592  * multiple strings property.
593  * @np:         device node from which the property value is to be read.
594  * @propname:   name of the property to be searched.
595  *
596  * Search for a property in a device tree node and retrieve the number of null
597  * terminated string contain in it. Returns the number of strings on
598  * success, -EINVAL if the property does not exist, -ENODATA if property
599  * does not have a value, and -EILSEQ if the string is not null-terminated
600  * within the length of the property data.
601  */
602 static inline int of_property_count_strings(struct device_node *np,
603                                             const char *propname)
604 {
605         return of_property_read_string_helper(np, propname, NULL, 0, 0);
606 }
607
608 /**
609  * of_property_read_string_index() - Find and read a string from a multiple
610  * strings property.
611  * @np:         device node from which the property value is to be read.
612  * @propname:   name of the property to be searched.
613  * @index:      index of the string in the list of strings
614  * @out_string: pointer to null terminated return string, modified only if
615  *              return value is 0.
616  *
617  * Search for a property in a device tree node and retrieve a null
618  * terminated string value (pointer to data, not a copy) in the list of strings
619  * contained in that property.
620  * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
621  * property does not have a value, and -EILSEQ if the string is not
622  * null-terminated within the length of the property data.
623  *
624  * The out_string pointer is modified only if a valid string can be decoded.
625  */
626 static inline int of_property_read_string_index(struct device_node *np,
627                                                 const char *propname,
628                                                 int index, const char **output)
629 {
630         int rc = of_property_read_string_helper(np, propname, output, 1, index);
631         return rc < 0 ? rc : 0;
632 }
633
634 /**
635  * of_property_read_bool - Findfrom a property
636  * @np:         device node from which the property value is to be read.
637  * @propname:   name of the property to be searched.
638  *
639  * Search for a property in a device node.
640  * Returns true if the property exist false otherwise.
641  */
642 static inline bool of_property_read_bool(const struct device_node *np,
643                                          const char *propname)
644 {
645         struct property *prop = of_find_property(np, propname, NULL);
646
647         return prop ? true : false;
648 }
649
650 static inline int of_property_read_u8(const struct device_node *np,
651                                        const char *propname,
652                                        u8 *out_value)
653 {
654         return of_property_read_u8_array(np, propname, out_value, 1);
655 }
656
657 static inline int of_property_read_u16(const struct device_node *np,
658                                        const char *propname,
659                                        u16 *out_value)
660 {
661         return of_property_read_u16_array(np, propname, out_value, 1);
662 }
663
664 static inline int of_property_read_u32(const struct device_node *np,
665                                        const char *propname,
666                                        u32 *out_value)
667 {
668         return of_property_read_u32_array(np, propname, out_value, 1);
669 }
670
671 static inline int of_property_count_u32(const struct device_node *np,
672                                        const char *propname)
673 {
674         const void *prop_ptr;
675         int prop_len;
676
677         prop_ptr = of_get_property(np, propname, &prop_len);
678         if (!prop_ptr)
679                 return -EINVAL;
680
681         return prop_len / sizeof(u32);
682 }
683
684 static inline int of_property_read_s32(const struct device_node *np,
685                                         const char *propname,
686                                         s32 *out_value)
687 {
688         u32 val;
689         int ret;
690
691         ret = of_property_read_u32(np, propname, &val);
692         if (ret < 0)
693                 return ret;
694         *out_value = (val & 0x80000000U) ? -((val ^ 0xFFFFFFFFU) + 1) : val;
695         return ret;
696 }
697
698 #if defined(CONFIG_PROC_FS) && defined(CONFIG_PROC_DEVICETREE)
699 extern void proc_device_tree_add_node(struct device_node *, struct proc_dir_entry *);
700 extern void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop);
701 extern void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
702                                          struct property *prop);
703 extern void proc_device_tree_update_prop(struct proc_dir_entry *pde,
704                                          struct property *newprop,
705                                          struct property *oldprop);
706 #endif
707
708 #endif /* _LINUX_OF_H */