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