1 /* SPDX-License-Identifier: GPL-2.0+ */
5 * Definitions for talking to the Open Firmware PROM on
6 * Power Macintosh and other computers.
8 * Copyright (C) 1996-2005 Paul Mackerras.
10 * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
11 * Updates for SPARC64 by David S. Miller
12 * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
14 #include <linux/types.h>
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/kobject.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/spinlock.h>
20 #include <linux/topology.h>
21 #include <linux/notifier.h>
22 #include <linux/property.h>
23 #include <linux/list.h>
25 #include <asm/byteorder.h>
26 #include <asm/errno.h>
35 struct property *next;
36 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
39 #if defined(CONFIG_OF_PROMTREE)
40 unsigned int unique_id;
42 #if defined(CONFIG_OF_KOBJ)
43 struct bin_attribute attr;
47 #if defined(CONFIG_SPARC)
48 struct of_irq_controller;
55 const char *full_name;
56 struct fwnode_handle fwnode;
58 struct property *properties;
59 struct property *deadprops; /* removed properties */
60 struct device_node *parent;
61 struct device_node *child;
62 struct device_node *sibling;
63 #if defined(CONFIG_OF_KOBJ)
68 #if defined(CONFIG_SPARC)
69 const char *path_component_name;
70 unsigned int unique_id;
71 struct of_irq_controller *irq_trans;
75 #define MAX_PHANDLE_ARGS 16
76 struct of_phandle_args {
77 struct device_node *np;
79 uint32_t args[MAX_PHANDLE_ARGS];
82 struct of_phandle_iterator {
83 /* Common iterator information */
84 const char *cells_name;
86 const struct device_node *parent;
88 /* List size information */
89 const __be32 *list_end;
90 const __be32 *phandle_end;
92 /* Current position state */
96 struct device_node *node;
99 struct of_reconfig_data {
100 struct device_node *dn;
101 struct property *prop;
102 struct property *old_prop;
105 /* initialize a node */
106 extern struct kobj_type of_node_ktype;
107 extern const struct fwnode_operations of_fwnode_ops;
108 static inline void of_node_init(struct device_node *node)
110 #if defined(CONFIG_OF_KOBJ)
111 kobject_init(&node->kobj, &of_node_ktype);
113 node->fwnode.ops = &of_fwnode_ops;
116 #if defined(CONFIG_OF_KOBJ)
117 #define of_node_kobj(n) (&(n)->kobj)
119 #define of_node_kobj(n) NULL
122 #ifdef CONFIG_OF_DYNAMIC
123 extern struct device_node *of_node_get(struct device_node *node);
124 extern void of_node_put(struct device_node *node);
125 #else /* CONFIG_OF_DYNAMIC */
126 /* Dummy ref counting routines - to be implemented later */
127 static inline struct device_node *of_node_get(struct device_node *node)
131 static inline void of_node_put(struct device_node *node) { }
132 #endif /* !CONFIG_OF_DYNAMIC */
134 /* Pointer for first entry in chain of all nodes. */
135 extern struct device_node *of_root;
136 extern struct device_node *of_chosen;
137 extern struct device_node *of_aliases;
138 extern struct device_node *of_stdout;
139 extern raw_spinlock_t devtree_lock;
141 /* flag descriptions (need to be visible even when !CONFIG_OF) */
142 #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
143 #define OF_DETACHED 2 /* node has been detached from the device tree */
144 #define OF_POPULATED 3 /* device already created for the node */
145 #define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
147 #define OF_BAD_ADDR ((u64)-1)
150 void of_core_init(void);
152 static inline bool is_of_node(const struct fwnode_handle *fwnode)
154 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
157 #define to_of_node(__fwnode) \
159 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
161 is_of_node(__to_of_node_fwnode) ? \
162 container_of(__to_of_node_fwnode, \
163 struct device_node, fwnode) : \
167 #define of_fwnode_handle(node) \
169 typeof(node) __of_fwnode_handle_node = (node); \
171 __of_fwnode_handle_node ? \
172 &__of_fwnode_handle_node->fwnode : NULL; \
175 static inline bool of_have_populated_dt(void)
177 return of_root != NULL;
180 static inline bool of_node_is_root(const struct device_node *node)
182 return node && (node->parent == NULL);
185 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
187 return test_bit(flag, &n->_flags);
190 static inline int of_node_test_and_set_flag(struct device_node *n,
193 return test_and_set_bit(flag, &n->_flags);
196 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
198 set_bit(flag, &n->_flags);
201 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
203 clear_bit(flag, &n->_flags);
206 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
207 static inline int of_property_check_flag(struct property *p, unsigned long flag)
209 return test_bit(flag, &p->_flags);
212 static inline void of_property_set_flag(struct property *p, unsigned long flag)
214 set_bit(flag, &p->_flags);
217 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
219 clear_bit(flag, &p->_flags);
223 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
224 extern struct device_node *of_find_all_nodes(struct device_node *prev);
227 * OF address retrieval & translation
230 /* Helper to read a big number; size is in cells (not bytes) */
231 static inline u64 of_read_number(const __be32 *cell, int size)
235 r = (r << 32) | be32_to_cpu(*(cell++));
239 /* Like of_read_number, but we want an unsigned long result */
240 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
242 /* toss away upper bits if unsigned long is smaller than u64 */
243 return of_read_number(cell, size);
246 #if defined(CONFIG_SPARC)
247 #include <asm/prom.h>
250 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
251 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
253 extern bool of_node_name_eq(const struct device_node *np, const char *name);
254 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
256 static inline const char *of_node_full_name(const struct device_node *np)
258 return np ? np->full_name : "<no-node>";
261 #define for_each_of_allnodes_from(from, dn) \
262 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
263 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
264 extern struct device_node *of_find_node_by_name(struct device_node *from,
266 extern struct device_node *of_find_node_by_type(struct device_node *from,
268 extern struct device_node *of_find_compatible_node(struct device_node *from,
269 const char *type, const char *compat);
270 extern struct device_node *of_find_matching_node_and_match(
271 struct device_node *from,
272 const struct of_device_id *matches,
273 const struct of_device_id **match);
275 extern struct device_node *of_find_node_opts_by_path(const char *path,
277 static inline struct device_node *of_find_node_by_path(const char *path)
279 return of_find_node_opts_by_path(path, NULL);
282 extern struct device_node *of_find_node_by_phandle(phandle handle);
283 extern struct device_node *of_get_parent(const struct device_node *node);
284 extern struct device_node *of_get_next_parent(struct device_node *node);
285 extern struct device_node *of_get_next_child(const struct device_node *node,
286 struct device_node *prev);
287 extern struct device_node *of_get_next_available_child(
288 const struct device_node *node, struct device_node *prev);
290 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
291 const char *compatible);
292 extern struct device_node *of_get_child_by_name(const struct device_node *node,
296 extern struct device_node *of_find_next_cache_node(const struct device_node *);
297 extern int of_find_last_cache_level(unsigned int cpu);
298 extern struct device_node *of_find_node_with_property(
299 struct device_node *from, const char *prop_name);
301 extern struct property *of_find_property(const struct device_node *np,
304 extern int of_property_count_elems_of_size(const struct device_node *np,
305 const char *propname, int elem_size);
306 extern int of_property_read_u32_index(const struct device_node *np,
307 const char *propname,
308 u32 index, u32 *out_value);
309 extern int of_property_read_u64_index(const struct device_node *np,
310 const char *propname,
311 u32 index, u64 *out_value);
312 extern int of_property_read_variable_u8_array(const struct device_node *np,
313 const char *propname, u8 *out_values,
314 size_t sz_min, size_t sz_max);
315 extern int of_property_read_variable_u16_array(const struct device_node *np,
316 const char *propname, u16 *out_values,
317 size_t sz_min, size_t sz_max);
318 extern int of_property_read_variable_u32_array(const struct device_node *np,
319 const char *propname,
323 extern int of_property_read_u64(const struct device_node *np,
324 const char *propname, u64 *out_value);
325 extern int of_property_read_variable_u64_array(const struct device_node *np,
326 const char *propname,
331 extern int of_property_read_string(const struct device_node *np,
332 const char *propname,
333 const char **out_string);
334 extern int of_property_match_string(const struct device_node *np,
335 const char *propname,
337 extern int of_property_read_string_helper(const struct device_node *np,
338 const char *propname,
339 const char **out_strs, size_t sz, int index);
340 extern int of_device_is_compatible(const struct device_node *device,
342 extern int of_device_compatible_match(struct device_node *device,
343 const char *const *compat);
344 extern bool of_device_is_available(const struct device_node *device);
345 extern bool of_device_is_big_endian(const struct device_node *device);
346 extern const void *of_get_property(const struct device_node *node,
349 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
350 extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
352 #define for_each_property_of_node(dn, pp) \
353 for (pp = dn->properties; pp != NULL; pp = pp->next)
355 extern int of_n_addr_cells(struct device_node *np);
356 extern int of_n_size_cells(struct device_node *np);
357 extern const struct of_device_id *of_match_node(
358 const struct of_device_id *matches, const struct device_node *node);
359 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
360 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
361 extern struct device_node *of_parse_phandle(const struct device_node *np,
362 const char *phandle_name,
364 extern int of_parse_phandle_with_args(const struct device_node *np,
365 const char *list_name, const char *cells_name, int index,
366 struct of_phandle_args *out_args);
367 extern int of_parse_phandle_with_args_map(const struct device_node *np,
368 const char *list_name, const char *stem_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);
391 extern int of_alias_get_alias_list(const struct of_device_id *matches,
392 const char *stem, unsigned long *bitmap,
395 extern int of_machine_is_compatible(const char *compat);
397 extern int of_add_property(struct device_node *np, struct property *prop);
398 extern int of_remove_property(struct device_node *np, struct property *prop);
399 extern int of_update_property(struct device_node *np, struct property *newprop);
401 /* For updating the device tree at runtime */
402 #define OF_RECONFIG_ATTACH_NODE 0x0001
403 #define OF_RECONFIG_DETACH_NODE 0x0002
404 #define OF_RECONFIG_ADD_PROPERTY 0x0003
405 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
406 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
408 extern int of_attach_node(struct device_node *);
409 extern int of_detach_node(struct device_node *);
411 #define of_match_ptr(_ptr) (_ptr)
414 * of_property_read_u8_array - Find and read an array of u8 from a property.
416 * @np: device node from which the property value is to be read.
417 * @propname: name of the property to be searched.
418 * @out_values: pointer to return value, modified only if return value is 0.
419 * @sz: number of array elements to read
421 * Search for a property in a device node and read 8-bit value(s) from
422 * it. Returns 0 on success, -EINVAL if the property does not exist,
423 * -ENODATA if property does not have a value, and -EOVERFLOW if the
424 * property data isn't large enough.
426 * dts entry of array should be like:
427 * property = /bits/ 8 <0x50 0x60 0x70>;
429 * The out_values is modified only if a valid u8 value can be decoded.
431 static inline int of_property_read_u8_array(const struct device_node *np,
432 const char *propname,
433 u8 *out_values, size_t sz)
435 int ret = of_property_read_variable_u8_array(np, propname, out_values,
444 * of_property_read_u16_array - Find and read an array of u16 from a property.
446 * @np: device node from which the property value is to be read.
447 * @propname: name of the property to be searched.
448 * @out_values: pointer to return value, modified only if return value is 0.
449 * @sz: number of array elements to read
451 * Search for a property in a device node and read 16-bit value(s) from
452 * it. Returns 0 on success, -EINVAL if the property does not exist,
453 * -ENODATA if property does not have a value, and -EOVERFLOW if the
454 * property data isn't large enough.
456 * dts entry of array should be like:
457 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
459 * The out_values is modified only if a valid u16 value can be decoded.
461 static inline int of_property_read_u16_array(const struct device_node *np,
462 const char *propname,
463 u16 *out_values, size_t sz)
465 int ret = of_property_read_variable_u16_array(np, propname, out_values,
474 * of_property_read_u32_array - Find and read an array of 32 bit integers
477 * @np: device node from which the property value is to be read.
478 * @propname: name of the property to be searched.
479 * @out_values: pointer to return value, modified only if return value is 0.
480 * @sz: number of array elements to read
482 * Search for a property in a device node and read 32-bit value(s) from
483 * it. Returns 0 on success, -EINVAL if the property does not exist,
484 * -ENODATA if property does not have a value, and -EOVERFLOW if the
485 * property data isn't large enough.
487 * The out_values is modified only if a valid u32 value can be decoded.
489 static inline int of_property_read_u32_array(const struct device_node *np,
490 const char *propname,
491 u32 *out_values, size_t sz)
493 int ret = of_property_read_variable_u32_array(np, propname, out_values,
502 * of_property_read_u64_array - Find and read an array of 64 bit integers
505 * @np: device node from which the property value is to be read.
506 * @propname: name of the property to be searched.
507 * @out_values: pointer to return value, modified only if return value is 0.
508 * @sz: number of array elements to read
510 * Search for a property in a device node and read 64-bit value(s) from
511 * it. Returns 0 on success, -EINVAL if the property does not exist,
512 * -ENODATA if property does not have a value, and -EOVERFLOW if the
513 * property data isn't large enough.
515 * The out_values is modified only if a valid u64 value can be decoded.
517 static inline int of_property_read_u64_array(const struct device_node *np,
518 const char *propname,
519 u64 *out_values, size_t sz)
521 int ret = of_property_read_variable_u64_array(np, propname, out_values,
530 * struct property *prop;
534 * of_property_for_each_u32(np, "propname", prop, p, u)
535 * printk("U32 value: %x\n", u);
537 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
540 * struct property *prop;
543 * of_property_for_each_string(np, "propname", prop, s)
544 * printk("String value: %s\n", s);
546 const char *of_prop_next_string(struct property *prop, const char *cur);
548 bool of_console_check(struct device_node *dn, char *name, int index);
550 extern int of_cpu_node_to_id(struct device_node *np);
552 int of_map_rid(struct device_node *np, u32 rid,
553 const char *map_name, const char *map_mask_name,
554 struct device_node **target, u32 *id_out);
556 #else /* CONFIG_OF */
558 static inline void of_core_init(void)
562 static inline bool is_of_node(const struct fwnode_handle *fwnode)
567 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
572 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
577 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
582 static inline const char* of_node_full_name(const struct device_node *np)
587 static inline struct device_node *of_find_node_by_name(struct device_node *from,
593 static inline struct device_node *of_find_node_by_type(struct device_node *from,
599 static inline struct device_node *of_find_matching_node_and_match(
600 struct device_node *from,
601 const struct of_device_id *matches,
602 const struct of_device_id **match)
607 static inline struct device_node *of_find_node_by_path(const char *path)
612 static inline struct device_node *of_find_node_opts_by_path(const char *path,
618 static inline struct device_node *of_find_node_by_phandle(phandle handle)
623 static inline struct device_node *of_get_parent(const struct device_node *node)
628 static inline struct device_node *of_get_next_child(
629 const struct device_node *node, struct device_node *prev)
634 static inline struct device_node *of_get_next_available_child(
635 const struct device_node *node, struct device_node *prev)
640 static inline struct device_node *of_find_node_with_property(
641 struct device_node *from, const char *prop_name)
646 #define of_fwnode_handle(node) NULL
648 static inline bool of_have_populated_dt(void)
653 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
654 const char *compatible)
659 static inline struct device_node *of_get_child_by_name(
660 const struct device_node *node,
666 static inline int of_device_is_compatible(const struct device_node *device,
672 static inline int of_device_compatible_match(struct device_node *device,
673 const char *const *compat)
678 static inline bool of_device_is_available(const struct device_node *device)
683 static inline bool of_device_is_big_endian(const struct device_node *device)
688 static inline struct property *of_find_property(const struct device_node *np,
695 static inline struct device_node *of_find_compatible_node(
696 struct device_node *from,
703 static inline int of_property_count_elems_of_size(const struct device_node *np,
704 const char *propname, int elem_size)
709 static inline int of_property_read_u8_array(const struct device_node *np,
710 const char *propname, u8 *out_values, size_t sz)
715 static inline int of_property_read_u16_array(const struct device_node *np,
716 const char *propname, u16 *out_values, size_t sz)
721 static inline int of_property_read_u32_array(const struct device_node *np,
722 const char *propname,
723 u32 *out_values, size_t sz)
728 static inline int of_property_read_u64_array(const struct device_node *np,
729 const char *propname,
730 u64 *out_values, size_t sz)
735 static inline int of_property_read_u32_index(const struct device_node *np,
736 const char *propname, u32 index, u32 *out_value)
741 static inline int of_property_read_u64_index(const struct device_node *np,
742 const char *propname, u32 index, u64 *out_value)
747 static inline const void *of_get_property(const struct device_node *node,
754 static inline struct device_node *of_get_cpu_node(int cpu,
755 unsigned int *thread)
760 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
765 static inline int of_n_addr_cells(struct device_node *np)
770 static inline int of_n_size_cells(struct device_node *np)
775 static inline int of_property_read_variable_u8_array(const struct device_node *np,
776 const char *propname, u8 *out_values,
777 size_t sz_min, size_t sz_max)
782 static inline int of_property_read_variable_u16_array(const struct device_node *np,
783 const char *propname, u16 *out_values,
784 size_t sz_min, size_t sz_max)
789 static inline int of_property_read_variable_u32_array(const struct device_node *np,
790 const char *propname,
798 static inline int of_property_read_u64(const struct device_node *np,
799 const char *propname, u64 *out_value)
804 static inline int of_property_read_variable_u64_array(const struct device_node *np,
805 const char *propname,
813 static inline int of_property_read_string(const struct device_node *np,
814 const char *propname,
815 const char **out_string)
820 static inline int of_property_match_string(const struct device_node *np,
821 const char *propname,
827 static inline int of_property_read_string_helper(const struct device_node *np,
828 const char *propname,
829 const char **out_strs, size_t sz, int index)
834 static inline struct device_node *of_parse_phandle(const struct device_node *np,
835 const char *phandle_name,
841 static inline int of_parse_phandle_with_args(const struct device_node *np,
842 const char *list_name,
843 const char *cells_name,
845 struct of_phandle_args *out_args)
850 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
851 const char *list_name,
852 const char *stem_name,
854 struct of_phandle_args *out_args)
859 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
860 const char *list_name, int cells_count, int index,
861 struct of_phandle_args *out_args)
866 static inline int of_count_phandle_with_args(struct device_node *np,
867 const char *list_name,
868 const char *cells_name)
873 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
874 const struct device_node *np,
875 const char *list_name,
876 const char *cells_name,
882 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
887 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
894 static inline int of_alias_get_id(struct device_node *np, const char *stem)
899 static inline int of_alias_get_highest_id(const char *stem)
904 static inline int of_alias_get_alias_list(const struct of_device_id *matches,
905 const char *stem, unsigned long *bitmap,
911 static inline int of_machine_is_compatible(const char *compat)
916 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
921 static inline const __be32 *of_prop_next_u32(struct property *prop,
922 const __be32 *cur, u32 *pu)
927 static inline const char *of_prop_next_string(struct property *prop,
933 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
938 static inline int of_node_test_and_set_flag(struct device_node *n,
944 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
948 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
952 static inline int of_property_check_flag(struct property *p, unsigned long flag)
957 static inline void of_property_set_flag(struct property *p, unsigned long flag)
961 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
965 static inline int of_cpu_node_to_id(struct device_node *np)
970 static inline int of_map_rid(struct device_node *np, u32 rid,
971 const char *map_name, const char *map_mask_name,
972 struct device_node **target, u32 *id_out)
977 #define of_match_ptr(_ptr) NULL
978 #define of_match_node(_matches, _node) NULL
979 #endif /* CONFIG_OF */
981 /* Default string compare functions, Allow arch asm/prom.h to override */
982 #if !defined(of_compat_cmp)
983 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
984 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
985 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
988 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
989 extern int of_node_to_nid(struct device_node *np);
991 static inline int of_node_to_nid(struct device_node *device)
997 #ifdef CONFIG_OF_NUMA
998 extern int of_numa_init(void);
1000 static inline int of_numa_init(void)
1006 static inline struct device_node *of_find_matching_node(
1007 struct device_node *from,
1008 const struct of_device_id *matches)
1010 return of_find_matching_node_and_match(from, matches, NULL);
1013 static inline const char *of_node_get_device_type(const struct device_node *np)
1015 return of_get_property(np, "device_type", NULL);
1018 static inline bool of_node_is_type(const struct device_node *np, const char *type)
1020 const char *match = of_node_get_device_type(np);
1022 return np && match && type && !strcmp(match, type);
1026 * of_property_count_u8_elems - Count the number of u8 elements in a property
1028 * @np: device node from which the property value is to be read.
1029 * @propname: name of the property to be searched.
1031 * Search for a property in a device node and count the number of u8 elements
1032 * in it. Returns number of elements on sucess, -EINVAL if the property does
1033 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1034 * property does not have a value.
1036 static inline int of_property_count_u8_elems(const struct device_node *np,
1037 const char *propname)
1039 return of_property_count_elems_of_size(np, propname, sizeof(u8));
1043 * of_property_count_u16_elems - Count the number of u16 elements in a property
1045 * @np: device node from which the property value is to be read.
1046 * @propname: name of the property to be searched.
1048 * Search for a property in a device node and count the number of u16 elements
1049 * in it. Returns number of elements on sucess, -EINVAL if the property does
1050 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1051 * property does not have a value.
1053 static inline int of_property_count_u16_elems(const struct device_node *np,
1054 const char *propname)
1056 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1060 * of_property_count_u32_elems - Count the number of u32 elements in a property
1062 * @np: device node from which the property value is to be read.
1063 * @propname: name of the property to be searched.
1065 * Search for a property in a device node and count the number of u32 elements
1066 * in it. Returns number of elements on sucess, -EINVAL if the property does
1067 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1068 * property does not have a value.
1070 static inline int of_property_count_u32_elems(const struct device_node *np,
1071 const char *propname)
1073 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1077 * of_property_count_u64_elems - Count the number of u64 elements in a property
1079 * @np: device node from which the property value is to be read.
1080 * @propname: name of the property to be searched.
1082 * Search for a property in a device node and count the number of u64 elements
1083 * in it. Returns number of elements on sucess, -EINVAL if the property does
1084 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1085 * property does not have a value.
1087 static inline int of_property_count_u64_elems(const struct device_node *np,
1088 const char *propname)
1090 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1094 * of_property_read_string_array() - Read an array of strings from a multiple
1096 * @np: device node from which the property value is to be read.
1097 * @propname: name of the property to be searched.
1098 * @out_strs: output array of string pointers.
1099 * @sz: number of array elements to read.
1101 * Search for a property in a device tree node and retrieve a list of
1102 * terminated string values (pointer to data, not a copy) in that property.
1104 * If @out_strs is NULL, the number of strings in the property is returned.
1106 static inline int of_property_read_string_array(const struct device_node *np,
1107 const char *propname, const char **out_strs,
1110 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1114 * of_property_count_strings() - Find and return the number of strings from a
1115 * multiple strings property.
1116 * @np: device node from which the property value is to be read.
1117 * @propname: name of the property to be searched.
1119 * Search for a property in a device tree node and retrieve the number of null
1120 * terminated string contain in it. Returns the number of strings on
1121 * success, -EINVAL if the property does not exist, -ENODATA if property
1122 * does not have a value, and -EILSEQ if the string is not null-terminated
1123 * within the length of the property data.
1125 static inline int of_property_count_strings(const struct device_node *np,
1126 const char *propname)
1128 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1132 * of_property_read_string_index() - Find and read a string from a multiple
1134 * @np: device node from which the property value is to be read.
1135 * @propname: name of the property to be searched.
1136 * @index: index of the string in the list of strings
1137 * @out_string: pointer to null terminated return string, modified only if
1138 * return value is 0.
1140 * Search for a property in a device tree node and retrieve a null
1141 * terminated string value (pointer to data, not a copy) in the list of strings
1142 * contained in that property.
1143 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1144 * property does not have a value, and -EILSEQ if the string is not
1145 * null-terminated within the length of the property data.
1147 * The out_string pointer is modified only if a valid string can be decoded.
1149 static inline int of_property_read_string_index(const struct device_node *np,
1150 const char *propname,
1151 int index, const char **output)
1153 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1154 return rc < 0 ? rc : 0;
1158 * of_property_read_bool - Findfrom a property
1159 * @np: device node from which the property value is to be read.
1160 * @propname: name of the property to be searched.
1162 * Search for a property in a device node.
1163 * Returns true if the property exists false otherwise.
1165 static inline bool of_property_read_bool(const struct device_node *np,
1166 const char *propname)
1168 struct property *prop = of_find_property(np, propname, NULL);
1170 return prop ? true : false;
1173 static inline int of_property_read_u8(const struct device_node *np,
1174 const char *propname,
1177 return of_property_read_u8_array(np, propname, out_value, 1);
1180 static inline int of_property_read_u16(const struct device_node *np,
1181 const char *propname,
1184 return of_property_read_u16_array(np, propname, out_value, 1);
1187 static inline int of_property_read_u32(const struct device_node *np,
1188 const char *propname,
1191 return of_property_read_u32_array(np, propname, out_value, 1);
1194 static inline int of_property_read_s32(const struct device_node *np,
1195 const char *propname,
1198 return of_property_read_u32(np, propname, (u32*) out_value);
1201 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1202 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1203 err = of_phandle_iterator_next(it); \
1205 err = of_phandle_iterator_next(it))
1207 #define of_property_for_each_u32(np, propname, prop, p, u) \
1208 for (prop = of_find_property(np, propname, NULL), \
1209 p = of_prop_next_u32(prop, NULL, &u); \
1211 p = of_prop_next_u32(prop, p, &u))
1213 #define of_property_for_each_string(np, propname, prop, s) \
1214 for (prop = of_find_property(np, propname, NULL), \
1215 s = of_prop_next_string(prop, NULL); \
1217 s = of_prop_next_string(prop, s))
1219 #define for_each_node_by_name(dn, name) \
1220 for (dn = of_find_node_by_name(NULL, name); dn; \
1221 dn = of_find_node_by_name(dn, name))
1222 #define for_each_node_by_type(dn, type) \
1223 for (dn = of_find_node_by_type(NULL, type); dn; \
1224 dn = of_find_node_by_type(dn, type))
1225 #define for_each_compatible_node(dn, type, compatible) \
1226 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1227 dn = of_find_compatible_node(dn, type, compatible))
1228 #define for_each_matching_node(dn, matches) \
1229 for (dn = of_find_matching_node(NULL, matches); dn; \
1230 dn = of_find_matching_node(dn, matches))
1231 #define for_each_matching_node_and_match(dn, matches, match) \
1232 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1233 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1235 #define for_each_child_of_node(parent, child) \
1236 for (child = of_get_next_child(parent, NULL); child != NULL; \
1237 child = of_get_next_child(parent, child))
1238 #define for_each_available_child_of_node(parent, child) \
1239 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1240 child = of_get_next_available_child(parent, child))
1242 #define for_each_of_cpu_node(cpu) \
1243 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1244 cpu = of_get_next_cpu_node(cpu))
1246 #define for_each_node_with_property(dn, prop_name) \
1247 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1248 dn = of_find_node_with_property(dn, prop_name))
1250 static inline int of_get_child_count(const struct device_node *np)
1252 struct device_node *child;
1255 for_each_child_of_node(np, child)
1261 static inline int of_get_available_child_count(const struct device_node *np)
1263 struct device_node *child;
1266 for_each_available_child_of_node(np, child)
1272 #if defined(CONFIG_OF) && !defined(MODULE)
1273 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1274 static const struct of_device_id __of_table_##name \
1275 __used __section(__##table##_of_table) \
1276 = { .compatible = compat, \
1277 .data = (fn == (fn_type)NULL) ? fn : fn }
1279 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1280 static const struct of_device_id __of_table_##name \
1281 __attribute__((unused)) \
1282 = { .compatible = compat, \
1283 .data = (fn == (fn_type)NULL) ? fn : fn }
1286 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1287 typedef int (*of_init_fn_1_ret)(struct device_node *);
1288 typedef void (*of_init_fn_1)(struct device_node *);
1290 #define OF_DECLARE_1(table, name, compat, fn) \
1291 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1292 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1293 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1294 #define OF_DECLARE_2(table, name, compat, fn) \
1295 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1298 * struct of_changeset_entry - Holds a changeset entry
1300 * @node: list_head for the log list
1301 * @action: notifier action
1302 * @np: pointer to the device node affected
1303 * @prop: pointer to the property affected
1304 * @old_prop: hold a pointer to the original property
1306 * Every modification of the device tree during a changeset
1307 * is held in a list of of_changeset_entry structures.
1308 * That way we can recover from a partial application, or we can
1309 * revert the changeset
1311 struct of_changeset_entry {
1312 struct list_head node;
1313 unsigned long action;
1314 struct device_node *np;
1315 struct property *prop;
1316 struct property *old_prop;
1320 * struct of_changeset - changeset tracker structure
1322 * @entries: list_head for the changeset entries
1324 * changesets are a convenient way to apply bulk changes to the
1325 * live tree. In case of an error, changes are rolled-back.
1326 * changesets live on after initial application, and if not
1327 * destroyed after use, they can be reverted in one single call.
1329 struct of_changeset {
1330 struct list_head entries;
1333 enum of_reconfig_change {
1334 OF_RECONFIG_NO_CHANGE = 0,
1335 OF_RECONFIG_CHANGE_ADD,
1336 OF_RECONFIG_CHANGE_REMOVE,
1339 #ifdef CONFIG_OF_DYNAMIC
1340 extern int of_reconfig_notifier_register(struct notifier_block *);
1341 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1342 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1343 extern int of_reconfig_get_state_change(unsigned long action,
1344 struct of_reconfig_data *arg);
1346 extern void of_changeset_init(struct of_changeset *ocs);
1347 extern void of_changeset_destroy(struct of_changeset *ocs);
1348 extern int of_changeset_apply(struct of_changeset *ocs);
1349 extern int of_changeset_revert(struct of_changeset *ocs);
1350 extern int of_changeset_action(struct of_changeset *ocs,
1351 unsigned long action, struct device_node *np,
1352 struct property *prop);
1354 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1355 struct device_node *np)
1357 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1360 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1361 struct device_node *np)
1363 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1366 static inline int of_changeset_add_property(struct of_changeset *ocs,
1367 struct device_node *np, struct property *prop)
1369 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1372 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1373 struct device_node *np, struct property *prop)
1375 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1378 static inline int of_changeset_update_property(struct of_changeset *ocs,
1379 struct device_node *np, struct property *prop)
1381 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1383 #else /* CONFIG_OF_DYNAMIC */
1384 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1388 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1392 static inline int of_reconfig_notify(unsigned long action,
1393 struct of_reconfig_data *arg)
1397 static inline int of_reconfig_get_state_change(unsigned long action,
1398 struct of_reconfig_data *arg)
1402 #endif /* CONFIG_OF_DYNAMIC */
1405 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1406 * @np: Pointer to the given device_node
1408 * return true if present false otherwise
1410 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1412 return of_property_read_bool(np, "system-power-controller");
1419 enum of_overlay_notify_action {
1420 OF_OVERLAY_PRE_APPLY = 0,
1421 OF_OVERLAY_POST_APPLY,
1422 OF_OVERLAY_PRE_REMOVE,
1423 OF_OVERLAY_POST_REMOVE,
1426 struct of_overlay_notify_data {
1427 struct device_node *overlay;
1428 struct device_node *target;
1431 #ifdef CONFIG_OF_OVERLAY
1433 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1435 int of_overlay_remove(int *ovcs_id);
1436 int of_overlay_remove_all(void);
1438 int of_overlay_notifier_register(struct notifier_block *nb);
1439 int of_overlay_notifier_unregister(struct notifier_block *nb);
1443 static inline int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)
1448 static inline int of_overlay_remove(int *ovcs_id)
1453 static inline int of_overlay_remove_all(void)
1458 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1463 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1470 #endif /* _LINUX_OF_H */