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