4 * Definitions for talking to the Open Firmware PROM on
5 * Power Macintosh and other computers.
7 * Copyright (C) 1996-2005 Paul Mackerras.
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.
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.
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>
29 #include <asm/byteorder.h>
30 #include <asm/errno.h>
39 struct property
*next
;
40 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
43 #if defined(CONFIG_OF_PROMTREE)
44 unsigned int unique_id
;
46 #if defined(CONFIG_OF_KOBJ)
47 struct bin_attribute attr
;
51 #if defined(CONFIG_SPARC)
52 struct of_irq_controller
;
59 const char *full_name
;
60 struct fwnode_handle fwnode
;
62 struct property
*properties
;
63 struct property
*deadprops
; /* removed properties */
64 struct device_node
*parent
;
65 struct device_node
*child
;
66 struct device_node
*sibling
;
67 #if defined(CONFIG_OF_KOBJ)
72 #if defined(CONFIG_SPARC)
73 const char *path_component_name
;
74 unsigned int unique_id
;
75 struct of_irq_controller
*irq_trans
;
79 #define MAX_PHANDLE_ARGS 16
80 struct of_phandle_args
{
81 struct device_node
*np
;
83 uint32_t args
[MAX_PHANDLE_ARGS
];
86 struct of_phandle_iterator
{
87 /* Common iterator information */
88 const char *cells_name
;
90 const struct device_node
*parent
;
92 /* List size information */
93 const __be32
*list_end
;
94 const __be32
*phandle_end
;
96 /* Current position state */
100 struct device_node
*node
;
103 struct of_reconfig_data
{
104 struct device_node
*dn
;
105 struct property
*prop
;
106 struct property
*old_prop
;
109 /* initialize a node */
110 extern struct kobj_type of_node_ktype
;
111 extern const struct fwnode_operations of_fwnode_ops
;
112 static inline void of_node_init(struct device_node
*node
)
114 #if defined(CONFIG_OF_KOBJ)
115 kobject_init(&node
->kobj
, &of_node_ktype
);
117 node
->fwnode
.ops
= &of_fwnode_ops
;
120 #if defined(CONFIG_OF_KOBJ)
121 #define of_node_kobj(n) (&(n)->kobj)
123 #define of_node_kobj(n) NULL
126 #ifdef CONFIG_OF_DYNAMIC
127 extern struct device_node
*of_node_get(struct device_node
*node
);
128 extern void of_node_put(struct device_node
*node
);
129 #else /* CONFIG_OF_DYNAMIC */
130 /* Dummy ref counting routines - to be implemented later */
131 static inline struct device_node
*of_node_get(struct device_node
*node
)
135 static inline void of_node_put(struct device_node
*node
) { }
136 #endif /* !CONFIG_OF_DYNAMIC */
138 /* Pointer for first entry in chain of all nodes. */
139 extern struct device_node
*of_root
;
140 extern struct device_node
*of_chosen
;
141 extern struct device_node
*of_aliases
;
142 extern struct device_node
*of_stdout
;
143 extern raw_spinlock_t devtree_lock
;
145 /* flag descriptions (need to be visible even when !CONFIG_OF) */
146 #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
147 #define OF_DETACHED 2 /* node has been detached from the device tree */
148 #define OF_POPULATED 3 /* device already created for the node */
149 #define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
151 #define OF_BAD_ADDR ((u64)-1)
154 void of_core_init(void);
156 static inline bool is_of_node(const struct fwnode_handle
*fwnode
)
158 return !IS_ERR_OR_NULL(fwnode
) && fwnode
->ops
== &of_fwnode_ops
;
161 #define to_of_node(__fwnode) \
163 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
165 is_of_node(__to_of_node_fwnode) ? \
166 container_of(__to_of_node_fwnode, \
167 struct device_node, fwnode) : \
171 #define of_fwnode_handle(node) \
173 typeof(node) __of_fwnode_handle_node = (node); \
175 __of_fwnode_handle_node ? \
176 &__of_fwnode_handle_node->fwnode : NULL; \
179 static inline bool of_have_populated_dt(void)
181 return of_root
!= NULL
;
184 static inline bool of_node_is_root(const struct device_node
*node
)
186 return node
&& (node
->parent
== NULL
);
189 static inline int of_node_check_flag(struct device_node
*n
, unsigned long flag
)
191 return test_bit(flag
, &n
->_flags
);
194 static inline int of_node_test_and_set_flag(struct device_node
*n
,
197 return test_and_set_bit(flag
, &n
->_flags
);
200 static inline void of_node_set_flag(struct device_node
*n
, unsigned long flag
)
202 set_bit(flag
, &n
->_flags
);
205 static inline void of_node_clear_flag(struct device_node
*n
, unsigned long flag
)
207 clear_bit(flag
, &n
->_flags
);
210 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
211 static inline int of_property_check_flag(struct property
*p
, unsigned long flag
)
213 return test_bit(flag
, &p
->_flags
);
216 static inline void of_property_set_flag(struct property
*p
, unsigned long flag
)
218 set_bit(flag
, &p
->_flags
);
221 static inline void of_property_clear_flag(struct property
*p
, unsigned long flag
)
223 clear_bit(flag
, &p
->_flags
);
227 extern struct device_node
*__of_find_all_nodes(struct device_node
*prev
);
228 extern struct device_node
*of_find_all_nodes(struct device_node
*prev
);
231 * OF address retrieval & translation
234 /* Helper to read a big number; size is in cells (not bytes) */
235 static inline u64
of_read_number(const __be32
*cell
, int size
)
239 r
= (r
<< 32) | be32_to_cpu(*(cell
++));
243 /* Like of_read_number, but we want an unsigned long result */
244 static inline unsigned long of_read_ulong(const __be32
*cell
, int size
)
246 /* toss away upper bits if unsigned long is smaller than u64 */
247 return of_read_number(cell
, size
);
250 #if defined(CONFIG_SPARC)
251 #include <asm/prom.h>
254 /* Default #address and #size cells. Allow arch asm/prom.h to override */
255 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
256 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
257 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
260 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
261 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
263 static inline const char *of_node_full_name(const struct device_node
*np
)
265 return np
? np
->full_name
: "<no-node>";
268 #define for_each_of_allnodes_from(from, dn) \
269 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
270 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
271 extern struct device_node
*of_find_node_by_name(struct device_node
*from
,
273 extern struct device_node
*of_find_node_by_type(struct device_node
*from
,
275 extern struct device_node
*of_find_compatible_node(struct device_node
*from
,
276 const char *type
, const char *compat
);
277 extern struct device_node
*of_find_matching_node_and_match(
278 struct device_node
*from
,
279 const struct of_device_id
*matches
,
280 const struct of_device_id
**match
);
282 extern struct device_node
*of_find_node_opts_by_path(const char *path
,
284 static inline struct device_node
*of_find_node_by_path(const char *path
)
286 return of_find_node_opts_by_path(path
, NULL
);
289 extern struct device_node
*of_find_node_by_phandle(phandle handle
);
290 extern struct device_node
*of_get_parent(const struct device_node
*node
);
291 extern struct device_node
*of_get_next_parent(struct device_node
*node
);
292 extern struct device_node
*of_get_next_child(const struct device_node
*node
,
293 struct device_node
*prev
);
294 extern struct device_node
*of_get_next_available_child(
295 const struct device_node
*node
, struct device_node
*prev
);
297 extern struct device_node
*of_get_child_by_name(const struct device_node
*node
,
301 extern struct device_node
*of_find_next_cache_node(const struct device_node
*);
302 extern int of_find_last_cache_level(unsigned int cpu
);
303 extern struct device_node
*of_find_node_with_property(
304 struct device_node
*from
, const char *prop_name
);
306 extern struct property
*of_find_property(const struct device_node
*np
,
309 extern int of_property_count_elems_of_size(const struct device_node
*np
,
310 const char *propname
, int elem_size
);
311 extern int of_property_read_u32_index(const struct device_node
*np
,
312 const char *propname
,
313 u32 index
, u32
*out_value
);
314 extern int of_property_read_u64_index(const struct device_node
*np
,
315 const char *propname
,
316 u32 index
, u64
*out_value
);
317 extern int of_property_read_variable_u8_array(const struct device_node
*np
,
318 const char *propname
, u8
*out_values
,
319 size_t sz_min
, size_t sz_max
);
320 extern int of_property_read_variable_u16_array(const struct device_node
*np
,
321 const char *propname
, u16
*out_values
,
322 size_t sz_min
, size_t sz_max
);
323 extern int of_property_read_variable_u32_array(const struct device_node
*np
,
324 const char *propname
,
328 extern int of_property_read_u64(const struct device_node
*np
,
329 const char *propname
, u64
*out_value
);
330 extern int of_property_read_variable_u64_array(const struct device_node
*np
,
331 const char *propname
,
336 extern int of_property_read_string(const struct device_node
*np
,
337 const char *propname
,
338 const char **out_string
);
339 extern int of_property_match_string(const struct device_node
*np
,
340 const char *propname
,
342 extern int of_property_read_string_helper(const struct device_node
*np
,
343 const char *propname
,
344 const char **out_strs
, size_t sz
, int index
);
345 extern int of_device_is_compatible(const struct device_node
*device
,
347 extern int of_device_compatible_match(struct device_node
*device
,
348 const char *const *compat
);
349 extern bool of_device_is_available(const struct device_node
*device
);
350 extern bool of_device_is_big_endian(const struct device_node
*device
);
351 extern const void *of_get_property(const struct device_node
*node
,
354 extern struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
);
355 #define for_each_property_of_node(dn, pp) \
356 for (pp = dn->properties; pp != NULL; pp = pp->next)
358 extern int of_n_addr_cells(struct device_node
*np
);
359 extern int of_n_size_cells(struct device_node
*np
);
360 extern const struct of_device_id
*of_match_node(
361 const struct of_device_id
*matches
, const struct device_node
*node
);
362 extern int of_modalias_node(struct device_node
*node
, char *modalias
, int len
);
363 extern void of_print_phandle_args(const char *msg
, const struct of_phandle_args
*args
);
364 extern struct device_node
*of_parse_phandle(const struct device_node
*np
,
365 const char *phandle_name
,
367 extern int of_parse_phandle_with_args(const struct device_node
*np
,
368 const char *list_name
, const char *cells_name
, int index
,
369 struct of_phandle_args
*out_args
);
370 extern int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
371 const char *list_name
, int cells_count
, int index
,
372 struct of_phandle_args
*out_args
);
373 extern int of_count_phandle_with_args(const struct device_node
*np
,
374 const char *list_name
, const char *cells_name
);
376 /* phandle iterator functions */
377 extern int of_phandle_iterator_init(struct of_phandle_iterator
*it
,
378 const struct device_node
*np
,
379 const char *list_name
,
380 const char *cells_name
,
383 extern int of_phandle_iterator_next(struct of_phandle_iterator
*it
);
384 extern int of_phandle_iterator_args(struct of_phandle_iterator
*it
,
388 extern void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
));
389 extern int of_alias_get_id(struct device_node
*np
, const char *stem
);
390 extern int of_alias_get_highest_id(const char *stem
);
392 extern int of_machine_is_compatible(const char *compat
);
394 extern int of_add_property(struct device_node
*np
, struct property
*prop
);
395 extern int of_remove_property(struct device_node
*np
, struct property
*prop
);
396 extern int of_update_property(struct device_node
*np
, struct property
*newprop
);
398 /* For updating the device tree at runtime */
399 #define OF_RECONFIG_ATTACH_NODE 0x0001
400 #define OF_RECONFIG_DETACH_NODE 0x0002
401 #define OF_RECONFIG_ADD_PROPERTY 0x0003
402 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
403 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
405 extern int of_attach_node(struct device_node
*);
406 extern int of_detach_node(struct device_node
*);
408 #define of_match_ptr(_ptr) (_ptr)
411 * of_property_read_u8_array - Find and read an array of u8 from a property.
413 * @np: device node from which the property value is to be read.
414 * @propname: name of the property to be searched.
415 * @out_values: pointer to return value, modified only if return value is 0.
416 * @sz: number of array elements to read
418 * Search for a property in a device node and read 8-bit value(s) from
419 * it. Returns 0 on success, -EINVAL if the property does not exist,
420 * -ENODATA if property does not have a value, and -EOVERFLOW if the
421 * property data isn't large enough.
423 * dts entry of array should be like:
424 * property = /bits/ 8 <0x50 0x60 0x70>;
426 * The out_values is modified only if a valid u8 value can be decoded.
428 static inline int of_property_read_u8_array(const struct device_node
*np
,
429 const char *propname
,
430 u8
*out_values
, size_t sz
)
432 int ret
= of_property_read_variable_u8_array(np
, propname
, out_values
,
441 * of_property_read_u16_array - Find and read an array of u16 from a property.
443 * @np: device node from which the property value is to be read.
444 * @propname: name of the property to be searched.
445 * @out_values: pointer to return value, modified only if return value is 0.
446 * @sz: number of array elements to read
448 * Search for a property in a device node and read 16-bit value(s) from
449 * it. Returns 0 on success, -EINVAL if the property does not exist,
450 * -ENODATA if property does not have a value, and -EOVERFLOW if the
451 * property data isn't large enough.
453 * dts entry of array should be like:
454 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
456 * The out_values is modified only if a valid u16 value can be decoded.
458 static inline int of_property_read_u16_array(const struct device_node
*np
,
459 const char *propname
,
460 u16
*out_values
, size_t sz
)
462 int ret
= of_property_read_variable_u16_array(np
, propname
, out_values
,
471 * of_property_read_u32_array - Find and read an array of 32 bit integers
474 * @np: device node from which the property value is to be read.
475 * @propname: name of the property to be searched.
476 * @out_values: pointer to return value, modified only if return value is 0.
477 * @sz: number of array elements to read
479 * Search for a property in a device node and read 32-bit value(s) from
480 * it. Returns 0 on success, -EINVAL if the property does not exist,
481 * -ENODATA if property does not have a value, and -EOVERFLOW if the
482 * property data isn't large enough.
484 * The out_values is modified only if a valid u32 value can be decoded.
486 static inline int of_property_read_u32_array(const struct device_node
*np
,
487 const char *propname
,
488 u32
*out_values
, size_t sz
)
490 int ret
= of_property_read_variable_u32_array(np
, propname
, out_values
,
499 * of_property_read_u64_array - Find and read an array of 64 bit integers
502 * @np: device node from which the property value is to be read.
503 * @propname: name of the property to be searched.
504 * @out_values: pointer to return value, modified only if return value is 0.
505 * @sz: number of array elements to read
507 * Search for a property in a device node and read 64-bit value(s) from
508 * it. Returns 0 on success, -EINVAL if the property does not exist,
509 * -ENODATA if property does not have a value, and -EOVERFLOW if the
510 * property data isn't large enough.
512 * The out_values is modified only if a valid u64 value can be decoded.
514 static inline int of_property_read_u64_array(const struct device_node
*np
,
515 const char *propname
,
516 u64
*out_values
, size_t sz
)
518 int ret
= of_property_read_variable_u64_array(np
, propname
, out_values
,
527 * struct property *prop;
531 * of_property_for_each_u32(np, "propname", prop, p, u)
532 * printk("U32 value: %x\n", u);
534 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
537 * struct property *prop;
540 * of_property_for_each_string(np, "propname", prop, s)
541 * printk("String value: %s\n", s);
543 const char *of_prop_next_string(struct property
*prop
, const char *cur
);
545 bool of_console_check(struct device_node
*dn
, char *name
, int index
);
547 #else /* CONFIG_OF */
549 static inline void of_core_init(void)
553 static inline bool is_of_node(const struct fwnode_handle
*fwnode
)
558 static inline struct device_node
*to_of_node(const struct fwnode_handle
*fwnode
)
563 static inline const char* of_node_full_name(const struct device_node
*np
)
568 static inline struct device_node
*of_find_node_by_name(struct device_node
*from
,
574 static inline struct device_node
*of_find_node_by_type(struct device_node
*from
,
580 static inline struct device_node
*of_find_matching_node_and_match(
581 struct device_node
*from
,
582 const struct of_device_id
*matches
,
583 const struct of_device_id
**match
)
588 static inline struct device_node
*of_find_node_by_path(const char *path
)
593 static inline struct device_node
*of_find_node_opts_by_path(const char *path
,
599 static inline struct device_node
*of_find_node_by_phandle(phandle handle
)
604 static inline struct device_node
*of_get_parent(const struct device_node
*node
)
609 static inline struct device_node
*of_get_next_child(
610 const struct device_node
*node
, struct device_node
*prev
)
615 static inline struct device_node
*of_get_next_available_child(
616 const struct device_node
*node
, struct device_node
*prev
)
621 static inline struct device_node
*of_find_node_with_property(
622 struct device_node
*from
, const char *prop_name
)
627 #define of_fwnode_handle(node) NULL
629 static inline bool of_have_populated_dt(void)
634 static inline struct device_node
*of_get_child_by_name(
635 const struct device_node
*node
,
641 static inline int of_device_is_compatible(const struct device_node
*device
,
647 static inline int of_device_compatible_match(struct device_node
*device
,
648 const char *const *compat
)
653 static inline bool of_device_is_available(const struct device_node
*device
)
658 static inline bool of_device_is_big_endian(const struct device_node
*device
)
663 static inline struct property
*of_find_property(const struct device_node
*np
,
670 static inline struct device_node
*of_find_compatible_node(
671 struct device_node
*from
,
678 static inline int of_property_count_elems_of_size(const struct device_node
*np
,
679 const char *propname
, int elem_size
)
684 static inline int of_property_read_u8_array(const struct device_node
*np
,
685 const char *propname
, u8
*out_values
, size_t sz
)
690 static inline int of_property_read_u16_array(const struct device_node
*np
,
691 const char *propname
, u16
*out_values
, size_t sz
)
696 static inline int of_property_read_u32_array(const struct device_node
*np
,
697 const char *propname
,
698 u32
*out_values
, size_t sz
)
703 static inline int of_property_read_u64_array(const struct device_node
*np
,
704 const char *propname
,
705 u64
*out_values
, size_t sz
)
710 static inline int of_property_read_u32_index(const struct device_node
*np
,
711 const char *propname
, u32 index
, u32
*out_value
)
716 static inline int of_property_read_u64_index(const struct device_node
*np
,
717 const char *propname
, u32 index
, u64
*out_value
)
722 static inline const void *of_get_property(const struct device_node
*node
,
729 static inline struct device_node
*of_get_cpu_node(int cpu
,
730 unsigned int *thread
)
735 static inline int of_n_addr_cells(struct device_node
*np
)
740 static inline int of_n_size_cells(struct device_node
*np
)
745 static inline int of_property_read_variable_u8_array(const struct device_node
*np
,
746 const char *propname
, u8
*out_values
,
747 size_t sz_min
, size_t sz_max
)
752 static inline int of_property_read_variable_u16_array(const struct device_node
*np
,
753 const char *propname
, u16
*out_values
,
754 size_t sz_min
, size_t sz_max
)
759 static inline int of_property_read_variable_u32_array(const struct device_node
*np
,
760 const char *propname
,
768 static inline int of_property_read_u64(const struct device_node
*np
,
769 const char *propname
, u64
*out_value
)
774 static inline int of_property_read_variable_u64_array(const struct device_node
*np
,
775 const char *propname
,
783 static inline int of_property_read_string(const struct device_node
*np
,
784 const char *propname
,
785 const char **out_string
)
790 static inline int of_property_match_string(const struct device_node
*np
,
791 const char *propname
,
797 static inline int of_property_read_string_helper(const struct device_node
*np
,
798 const char *propname
,
799 const char **out_strs
, size_t sz
, int index
)
804 static inline struct device_node
*of_parse_phandle(const struct device_node
*np
,
805 const char *phandle_name
,
811 static inline int of_parse_phandle_with_args(const struct device_node
*np
,
812 const char *list_name
,
813 const char *cells_name
,
815 struct of_phandle_args
*out_args
)
820 static inline int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
821 const char *list_name
, int cells_count
, int index
,
822 struct of_phandle_args
*out_args
)
827 static inline int of_count_phandle_with_args(struct device_node
*np
,
828 const char *list_name
,
829 const char *cells_name
)
834 static inline int of_phandle_iterator_init(struct of_phandle_iterator
*it
,
835 const struct device_node
*np
,
836 const char *list_name
,
837 const char *cells_name
,
843 static inline int of_phandle_iterator_next(struct of_phandle_iterator
*it
)
848 static inline int of_phandle_iterator_args(struct of_phandle_iterator
*it
,
855 static inline int of_alias_get_id(struct device_node
*np
, const char *stem
)
860 static inline int of_alias_get_highest_id(const char *stem
)
865 static inline int of_machine_is_compatible(const char *compat
)
870 static inline bool of_console_check(const struct device_node
*dn
, const char *name
, int index
)
875 static inline const __be32
*of_prop_next_u32(struct property
*prop
,
876 const __be32
*cur
, u32
*pu
)
881 static inline const char *of_prop_next_string(struct property
*prop
,
887 static inline int of_node_check_flag(struct device_node
*n
, unsigned long flag
)
892 static inline int of_node_test_and_set_flag(struct device_node
*n
,
898 static inline void of_node_set_flag(struct device_node
*n
, unsigned long flag
)
902 static inline void of_node_clear_flag(struct device_node
*n
, unsigned long flag
)
906 static inline int of_property_check_flag(struct property
*p
, unsigned long flag
)
911 static inline void of_property_set_flag(struct property
*p
, unsigned long flag
)
915 static inline void of_property_clear_flag(struct property
*p
, unsigned long flag
)
919 #define of_match_ptr(_ptr) NULL
920 #define of_match_node(_matches, _node) NULL
921 #endif /* CONFIG_OF */
923 /* Default string compare functions, Allow arch asm/prom.h to override */
924 #if !defined(of_compat_cmp)
925 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
926 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
927 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
930 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
931 extern int of_node_to_nid(struct device_node
*np
);
933 static inline int of_node_to_nid(struct device_node
*device
)
939 #ifdef CONFIG_OF_NUMA
940 extern int of_numa_init(void);
942 static inline int of_numa_init(void)
948 static inline struct device_node
*of_find_matching_node(
949 struct device_node
*from
,
950 const struct of_device_id
*matches
)
952 return of_find_matching_node_and_match(from
, matches
, NULL
);
956 * of_property_count_u8_elems - Count the number of u8 elements in a property
958 * @np: device node from which the property value is to be read.
959 * @propname: name of the property to be searched.
961 * Search for a property in a device node and count the number of u8 elements
962 * in it. Returns number of elements on sucess, -EINVAL if the property does
963 * not exist or its length does not match a multiple of u8 and -ENODATA if the
964 * property does not have a value.
966 static inline int of_property_count_u8_elems(const struct device_node
*np
,
967 const char *propname
)
969 return of_property_count_elems_of_size(np
, propname
, sizeof(u8
));
973 * of_property_count_u16_elems - Count the number of u16 elements in a property
975 * @np: device node from which the property value is to be read.
976 * @propname: name of the property to be searched.
978 * Search for a property in a device node and count the number of u16 elements
979 * in it. Returns number of elements on sucess, -EINVAL if the property does
980 * not exist or its length does not match a multiple of u16 and -ENODATA if the
981 * property does not have a value.
983 static inline int of_property_count_u16_elems(const struct device_node
*np
,
984 const char *propname
)
986 return of_property_count_elems_of_size(np
, propname
, sizeof(u16
));
990 * of_property_count_u32_elems - Count the number of u32 elements in a property
992 * @np: device node from which the property value is to be read.
993 * @propname: name of the property to be searched.
995 * Search for a property in a device node and count the number of u32 elements
996 * in it. Returns number of elements on sucess, -EINVAL if the property does
997 * not exist or its length does not match a multiple of u32 and -ENODATA if the
998 * property does not have a value.
1000 static inline int of_property_count_u32_elems(const struct device_node
*np
,
1001 const char *propname
)
1003 return of_property_count_elems_of_size(np
, propname
, sizeof(u32
));
1007 * of_property_count_u64_elems - Count the number of u64 elements in a property
1009 * @np: device node from which the property value is to be read.
1010 * @propname: name of the property to be searched.
1012 * Search for a property in a device node and count the number of u64 elements
1013 * in it. Returns number of elements on sucess, -EINVAL if the property does
1014 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1015 * property does not have a value.
1017 static inline int of_property_count_u64_elems(const struct device_node
*np
,
1018 const char *propname
)
1020 return of_property_count_elems_of_size(np
, propname
, sizeof(u64
));
1024 * of_property_read_string_array() - Read an array of strings from a multiple
1026 * @np: device node from which the property value is to be read.
1027 * @propname: name of the property to be searched.
1028 * @out_strs: output array of string pointers.
1029 * @sz: number of array elements to read.
1031 * Search for a property in a device tree node and retrieve a list of
1032 * terminated string values (pointer to data, not a copy) in that property.
1034 * If @out_strs is NULL, the number of strings in the property is returned.
1036 static inline int of_property_read_string_array(const struct device_node
*np
,
1037 const char *propname
, const char **out_strs
,
1040 return of_property_read_string_helper(np
, propname
, out_strs
, sz
, 0);
1044 * of_property_count_strings() - Find and return the number of strings from a
1045 * multiple strings property.
1046 * @np: device node from which the property value is to be read.
1047 * @propname: name of the property to be searched.
1049 * Search for a property in a device tree node and retrieve the number of null
1050 * terminated string contain in it. Returns the number of strings on
1051 * success, -EINVAL if the property does not exist, -ENODATA if property
1052 * does not have a value, and -EILSEQ if the string is not null-terminated
1053 * within the length of the property data.
1055 static inline int of_property_count_strings(const struct device_node
*np
,
1056 const char *propname
)
1058 return of_property_read_string_helper(np
, propname
, NULL
, 0, 0);
1062 * of_property_read_string_index() - Find and read a string from a multiple
1064 * @np: device node from which the property value is to be read.
1065 * @propname: name of the property to be searched.
1066 * @index: index of the string in the list of strings
1067 * @out_string: pointer to null terminated return string, modified only if
1068 * return value is 0.
1070 * Search for a property in a device tree node and retrieve a null
1071 * terminated string value (pointer to data, not a copy) in the list of strings
1072 * contained in that property.
1073 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1074 * property does not have a value, and -EILSEQ if the string is not
1075 * null-terminated within the length of the property data.
1077 * The out_string pointer is modified only if a valid string can be decoded.
1079 static inline int of_property_read_string_index(const struct device_node
*np
,
1080 const char *propname
,
1081 int index
, const char **output
)
1083 int rc
= of_property_read_string_helper(np
, propname
, output
, 1, index
);
1084 return rc
< 0 ? rc
: 0;
1088 * of_property_read_bool - Findfrom a property
1089 * @np: device node from which the property value is to be read.
1090 * @propname: name of the property to be searched.
1092 * Search for a property in a device node.
1093 * Returns true if the property exists false otherwise.
1095 static inline bool of_property_read_bool(const struct device_node
*np
,
1096 const char *propname
)
1098 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1100 return prop
? true : false;
1103 static inline int of_property_read_u8(const struct device_node
*np
,
1104 const char *propname
,
1107 return of_property_read_u8_array(np
, propname
, out_value
, 1);
1110 static inline int of_property_read_u16(const struct device_node
*np
,
1111 const char *propname
,
1114 return of_property_read_u16_array(np
, propname
, out_value
, 1);
1117 static inline int of_property_read_u32(const struct device_node
*np
,
1118 const char *propname
,
1121 return of_property_read_u32_array(np
, propname
, out_value
, 1);
1124 static inline int of_property_read_s32(const struct device_node
*np
,
1125 const char *propname
,
1128 return of_property_read_u32(np
, propname
, (u32
*) out_value
);
1131 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1132 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1133 err = of_phandle_iterator_next(it); \
1135 err = of_phandle_iterator_next(it))
1137 #define of_property_for_each_u32(np, propname, prop, p, u) \
1138 for (prop = of_find_property(np, propname, NULL), \
1139 p = of_prop_next_u32(prop, NULL, &u); \
1141 p = of_prop_next_u32(prop, p, &u))
1143 #define of_property_for_each_string(np, propname, prop, s) \
1144 for (prop = of_find_property(np, propname, NULL), \
1145 s = of_prop_next_string(prop, NULL); \
1147 s = of_prop_next_string(prop, s))
1149 #define for_each_node_by_name(dn, name) \
1150 for (dn = of_find_node_by_name(NULL, name); dn; \
1151 dn = of_find_node_by_name(dn, name))
1152 #define for_each_node_by_type(dn, type) \
1153 for (dn = of_find_node_by_type(NULL, type); dn; \
1154 dn = of_find_node_by_type(dn, type))
1155 #define for_each_compatible_node(dn, type, compatible) \
1156 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1157 dn = of_find_compatible_node(dn, type, compatible))
1158 #define for_each_matching_node(dn, matches) \
1159 for (dn = of_find_matching_node(NULL, matches); dn; \
1160 dn = of_find_matching_node(dn, matches))
1161 #define for_each_matching_node_and_match(dn, matches, match) \
1162 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1163 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1165 #define for_each_child_of_node(parent, child) \
1166 for (child = of_get_next_child(parent, NULL); child != NULL; \
1167 child = of_get_next_child(parent, child))
1168 #define for_each_available_child_of_node(parent, child) \
1169 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1170 child = of_get_next_available_child(parent, child))
1172 #define for_each_node_with_property(dn, prop_name) \
1173 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1174 dn = of_find_node_with_property(dn, prop_name))
1176 static inline int of_get_child_count(const struct device_node
*np
)
1178 struct device_node
*child
;
1181 for_each_child_of_node(np
, child
)
1187 static inline int of_get_available_child_count(const struct device_node
*np
)
1189 struct device_node
*child
;
1192 for_each_available_child_of_node(np
, child
)
1198 #if defined(CONFIG_OF) && !defined(MODULE)
1199 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1200 static const struct of_device_id __of_table_##name \
1201 __used __section(__##table##_of_table) \
1202 = { .compatible = compat, \
1203 .data = (fn == (fn_type)NULL) ? fn : fn }
1205 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1206 static const struct of_device_id __of_table_##name \
1207 __attribute__((unused)) \
1208 = { .compatible = compat, \
1209 .data = (fn == (fn_type)NULL) ? fn : fn }
1212 typedef int (*of_init_fn_2
)(struct device_node
*, struct device_node
*);
1213 typedef int (*of_init_fn_1_ret
)(struct device_node
*);
1214 typedef void (*of_init_fn_1
)(struct device_node
*);
1216 #define OF_DECLARE_1(table, name, compat, fn) \
1217 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1218 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1219 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1220 #define OF_DECLARE_2(table, name, compat, fn) \
1221 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1224 * struct of_changeset_entry - Holds a changeset entry
1226 * @node: list_head for the log list
1227 * @action: notifier action
1228 * @np: pointer to the device node affected
1229 * @prop: pointer to the property affected
1230 * @old_prop: hold a pointer to the original property
1232 * Every modification of the device tree during a changeset
1233 * is held in a list of of_changeset_entry structures.
1234 * That way we can recover from a partial application, or we can
1235 * revert the changeset
1237 struct of_changeset_entry
{
1238 struct list_head node
;
1239 unsigned long action
;
1240 struct device_node
*np
;
1241 struct property
*prop
;
1242 struct property
*old_prop
;
1246 * struct of_changeset - changeset tracker structure
1248 * @entries: list_head for the changeset entries
1250 * changesets are a convenient way to apply bulk changes to the
1251 * live tree. In case of an error, changes are rolled-back.
1252 * changesets live on after initial application, and if not
1253 * destroyed after use, they can be reverted in one single call.
1255 struct of_changeset
{
1256 struct list_head entries
;
1259 enum of_reconfig_change
{
1260 OF_RECONFIG_NO_CHANGE
= 0,
1261 OF_RECONFIG_CHANGE_ADD
,
1262 OF_RECONFIG_CHANGE_REMOVE
,
1265 #ifdef CONFIG_OF_DYNAMIC
1266 extern int of_reconfig_notifier_register(struct notifier_block
*);
1267 extern int of_reconfig_notifier_unregister(struct notifier_block
*);
1268 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data
*rd
);
1269 extern int of_reconfig_get_state_change(unsigned long action
,
1270 struct of_reconfig_data
*arg
);
1272 extern void of_changeset_init(struct of_changeset
*ocs
);
1273 extern void of_changeset_destroy(struct of_changeset
*ocs
);
1274 extern int of_changeset_apply(struct of_changeset
*ocs
);
1275 extern int of_changeset_revert(struct of_changeset
*ocs
);
1276 extern int of_changeset_action(struct of_changeset
*ocs
,
1277 unsigned long action
, struct device_node
*np
,
1278 struct property
*prop
);
1280 static inline int of_changeset_attach_node(struct of_changeset
*ocs
,
1281 struct device_node
*np
)
1283 return of_changeset_action(ocs
, OF_RECONFIG_ATTACH_NODE
, np
, NULL
);
1286 static inline int of_changeset_detach_node(struct of_changeset
*ocs
,
1287 struct device_node
*np
)
1289 return of_changeset_action(ocs
, OF_RECONFIG_DETACH_NODE
, np
, NULL
);
1292 static inline int of_changeset_add_property(struct of_changeset
*ocs
,
1293 struct device_node
*np
, struct property
*prop
)
1295 return of_changeset_action(ocs
, OF_RECONFIG_ADD_PROPERTY
, np
, prop
);
1298 static inline int of_changeset_remove_property(struct of_changeset
*ocs
,
1299 struct device_node
*np
, struct property
*prop
)
1301 return of_changeset_action(ocs
, OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
);
1304 static inline int of_changeset_update_property(struct of_changeset
*ocs
,
1305 struct device_node
*np
, struct property
*prop
)
1307 return of_changeset_action(ocs
, OF_RECONFIG_UPDATE_PROPERTY
, np
, prop
);
1309 #else /* CONFIG_OF_DYNAMIC */
1310 static inline int of_reconfig_notifier_register(struct notifier_block
*nb
)
1314 static inline int of_reconfig_notifier_unregister(struct notifier_block
*nb
)
1318 static inline int of_reconfig_notify(unsigned long action
,
1319 struct of_reconfig_data
*arg
)
1323 static inline int of_reconfig_get_state_change(unsigned long action
,
1324 struct of_reconfig_data
*arg
)
1328 #endif /* CONFIG_OF_DYNAMIC */
1331 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1332 * @np: Pointer to the given device_node
1334 * return true if present false otherwise
1336 static inline bool of_device_is_system_power_controller(const struct device_node
*np
)
1338 return of_property_read_bool(np
, "system-power-controller");
1345 enum of_overlay_notify_action
{
1346 OF_OVERLAY_PRE_APPLY
= 0,
1347 OF_OVERLAY_POST_APPLY
,
1348 OF_OVERLAY_PRE_REMOVE
,
1349 OF_OVERLAY_POST_REMOVE
,
1352 struct of_overlay_notify_data
{
1353 struct device_node
*overlay
;
1354 struct device_node
*target
;
1357 #ifdef CONFIG_OF_OVERLAY
1359 /* ID based overlays; the API for external users */
1360 int of_overlay_apply(struct device_node
*tree
, int *ovcs_id
);
1361 int of_overlay_remove(int *ovcs_id
);
1362 int of_overlay_remove_all(void);
1364 int of_overlay_notifier_register(struct notifier_block
*nb
);
1365 int of_overlay_notifier_unregister(struct notifier_block
*nb
);
1369 static inline int of_overlay_apply(struct device_node
*tree
, int *ovcs_id
)
1374 static inline int of_overlay_remove(int *ovcs_id
)
1379 static inline int of_overlay_remove_all(void)
1384 static inline int of_overlay_notifier_register(struct notifier_block
*nb
)
1389 static inline int of_overlay_notifier_unregister(struct notifier_block
*nb
)
1396 #endif /* _LINUX_OF_H */