blob: 173102dafb07131da6fc9b5af19e3c38e0bc1140 [file] [log] [blame]
Stephen Rothwell76c1ce72007-05-01 16:19:07 +10001#ifndef _LINUX_OF_H
2#define _LINUX_OF_H
3/*
4 * Definitions for talking to the Open Firmware PROM on
5 * Power Macintosh and other computers.
6 *
7 * Copyright (C) 1996-2005 Paul Mackerras.
8 *
9 * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
10 * Updates for SPARC64 by David S. Miller
11 * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 */
18#include <linux/types.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070019#include <linux/bitops.h>
Kalle Valoe51130c2011-10-06 15:40:44 +030020#include <linux/errno.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000021#include <linux/kobject.h>
Grant Likely283029d2008-01-09 06:20:40 +110022#include <linux/mod_devicetable.h>
Grant Likely0d351c32010-02-14 14:13:57 -070023#include <linux/spinlock.h>
Paul Mundt5ca4db62012-06-03 22:04:34 -070024#include <linux/topology.h>
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +000025#include <linux/notifier.h>
Rafael J. Wysockib31384f2014-11-04 01:28:56 +010026#include <linux/property.h>
Pantelis Antoniou7518b5892014-10-28 22:35:58 +020027#include <linux/list.h>
Stephen Rothwell76c1ce72007-05-01 16:19:07 +100028
Jeremy Kerr2e89e682010-01-30 01:41:49 -070029#include <asm/byteorder.h>
Paul Gortmakerd0a99402011-10-29 10:17:06 -040030#include <asm/errno.h>
Jeremy Kerr2e89e682010-01-30 01:41:49 -070031
Grant Likely731581e2009-10-15 10:57:46 -060032typedef u32 phandle;
33typedef u32 ihandle;
34
35struct property {
36 char *name;
37 int length;
38 void *value;
39 struct property *next;
Rob Herring16bba302017-10-04 14:30:02 -050040#if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
Grant Likely731581e2009-10-15 10:57:46 -060041 unsigned long _flags;
Rob Herring16bba302017-10-04 14:30:02 -050042#endif
Rob Herring36689ec2017-09-29 20:08:28 -050043#if defined(CONFIG_OF_PROMTREE)
Grant Likely731581e2009-10-15 10:57:46 -060044 unsigned int unique_id;
Rob Herring36689ec2017-09-29 20:08:28 -050045#endif
Rob Herringb56b5522017-10-04 14:09:40 -050046#if defined(CONFIG_OF_KOBJ)
Grant Likely75b57ec2014-02-20 18:02:11 +000047 struct bin_attribute attr;
Rob Herringb56b5522017-10-04 14:09:40 -050048#endif
Grant Likely731581e2009-10-15 10:57:46 -060049};
50
Grant Likely6f192492009-10-15 10:57:49 -060051#if defined(CONFIG_SPARC)
52struct of_irq_controller;
53#endif
54
55struct device_node {
56 const char *name;
57 const char *type;
Grant Likely6016a362010-01-28 14:06:53 -070058 phandle phandle;
Grant Likelyc22618a2012-11-14 22:37:12 +000059 const char *full_name;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +010060 struct fwnode_handle fwnode;
Grant Likely6f192492009-10-15 10:57:49 -060061
62 struct property *properties;
63 struct property *deadprops; /* removed properties */
64 struct device_node *parent;
65 struct device_node *child;
66 struct device_node *sibling;
Rob Herringb56b5522017-10-04 14:09:40 -050067#if defined(CONFIG_OF_KOBJ)
Grant Likely75b57ec2014-02-20 18:02:11 +000068 struct kobject kobj;
Rob Herringb56b5522017-10-04 14:09:40 -050069#endif
Grant Likely6f192492009-10-15 10:57:49 -060070 unsigned long _flags;
71 void *data;
72#if defined(CONFIG_SPARC)
Grant Likelyc22618a2012-11-14 22:37:12 +000073 const char *path_component_name;
Grant Likely6f192492009-10-15 10:57:49 -060074 unsigned int unique_id;
75 struct of_irq_controller *irq_trans;
76#endif
77};
78
Andreas Herrmannb66548e22014-01-17 12:08:30 +010079#define MAX_PHANDLE_ARGS 16
Grant Likely15c9a0a2011-12-12 09:25:57 -070080struct of_phandle_args {
81 struct device_node *np;
82 int args_count;
83 uint32_t args[MAX_PHANDLE_ARGS];
84};
85
Joerg Roedel74e1fbb2016-04-04 17:49:17 +020086struct of_phandle_iterator {
87 /* Common iterator information */
88 const char *cells_name;
89 int cell_count;
90 const struct device_node *parent;
91
92 /* List size information */
93 const __be32 *list_end;
94 const __be32 *phandle_end;
95
96 /* Current position state */
97 const __be32 *cur;
98 uint32_t cur_count;
99 phandle phandle;
100 struct device_node *node;
101};
102
Grant Likelyf5242e52014-11-24 17:58:01 +0000103struct of_reconfig_data {
104 struct device_node *dn;
105 struct property *prop;
106 struct property *old_prop;
107};
108
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200109/* initialize a node */
110extern struct kobj_type of_node_ktype;
Sakari Ailus37081842017-06-06 12:37:37 +0300111extern const struct fwnode_operations of_fwnode_ops;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200112static inline void of_node_init(struct device_node *node)
113{
Rob Herringb56b5522017-10-04 14:09:40 -0500114#if defined(CONFIG_OF_KOBJ)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200115 kobject_init(&node->kobj, &of_node_ktype);
Rob Herringb56b5522017-10-04 14:09:40 -0500116#endif
Sakari Ailus37081842017-06-06 12:37:37 +0300117 node->fwnode.ops = &of_fwnode_ops;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200118}
119
Rob Herringb56b5522017-10-04 14:09:40 -0500120#if defined(CONFIG_OF_KOBJ)
Rob Herring0c3c2342017-10-04 14:04:01 -0500121#define of_node_kobj(n) (&(n)->kobj)
Rob Herringb56b5522017-10-04 14:09:40 -0500122#else
123#define of_node_kobj(n) NULL
124#endif
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200125
Grant Likely0f22dd32012-02-15 20:38:40 -0700126#ifdef CONFIG_OF_DYNAMIC
127extern struct device_node *of_node_get(struct device_node *node);
128extern void of_node_put(struct device_node *node);
129#else /* CONFIG_OF_DYNAMIC */
Rob Herring3ecdd052011-12-13 09:13:54 -0600130/* Dummy ref counting routines - to be implemented later */
131static inline struct device_node *of_node_get(struct device_node *node)
132{
133 return node;
134}
Grant Likely0f22dd32012-02-15 20:38:40 -0700135static inline void of_node_put(struct device_node *node) { }
136#endif /* !CONFIG_OF_DYNAMIC */
Rob Herring3ecdd052011-12-13 09:13:54 -0600137
Grant Likely41f88002009-11-23 20:07:01 -0700138/* Pointer for first entry in chain of all nodes. */
Grant Likely5063e252014-10-03 16:28:27 +0100139extern struct device_node *of_root;
Grant Likelyfc0bdae2010-02-14 07:13:55 -0700140extern struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +0800141extern struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -0700142extern struct device_node *of_stdout;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500143extern raw_spinlock_t devtree_lock;
Grant Likely41f88002009-11-23 20:07:01 -0700144
Pantelis Antoniou391b4592015-04-24 12:41:56 +0300145/* flag descriptions (need to be visible even when !CONFIG_OF) */
146#define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
147#define OF_DETACHED 2 /* node has been detached from the device tree */
148#define OF_POPULATED 3 /* device already created for the node */
149#define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
150
Guenter Roeckb1d06b62015-11-06 19:28:22 -0800151#define OF_BAD_ADDR ((u64)-1)
152
Hans de Goede6d09dc62014-11-14 13:26:52 +0100153#ifdef CONFIG_OF
Sudeep Holla194ec932015-05-14 15:28:24 +0100154void of_core_init(void);
155
Sakari Ailusd20dc142017-05-24 17:53:55 +0300156static inline bool is_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100157{
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300158 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100159}
160
Sakari Ailusd20dc142017-05-24 17:53:55 +0300161#define to_of_node(__fwnode) \
162 ({ \
163 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
164 \
165 is_of_node(__to_of_node_fwnode) ? \
166 container_of(__to_of_node_fwnode, \
167 struct device_node, fwnode) : \
168 NULL; \
169 })
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100170
Sakari Ailusdebd3a32017-05-24 17:53:54 +0300171#define of_fwnode_handle(node) \
172 ({ \
173 typeof(node) __of_fwnode_handle_node = (node); \
174 \
175 __of_fwnode_handle_node ? \
176 &__of_fwnode_handle_node->fwnode : NULL; \
177 })
Sakari Ailus67831832017-03-28 10:52:23 +0300178
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100179static inline bool of_have_populated_dt(void)
180{
Grant Likely5063e252014-10-03 16:28:27 +0100181 return of_root != NULL;
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100182}
183
Andres Salomon035ebef2010-07-13 09:42:26 +0000184static inline bool of_node_is_root(const struct device_node *node)
185{
186 return node && (node->parent == NULL);
187}
188
Grant Likely50436312009-10-15 10:57:58 -0600189static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
190{
191 return test_bit(flag, &n->_flags);
192}
193
Pawel Mollc6e126d2014-05-15 16:55:24 +0100194static inline int of_node_test_and_set_flag(struct device_node *n,
195 unsigned long flag)
196{
197 return test_and_set_bit(flag, &n->_flags);
198}
199
Grant Likely50436312009-10-15 10:57:58 -0600200static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
201{
202 set_bit(flag, &n->_flags);
203}
204
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200205static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
206{
207 clear_bit(flag, &n->_flags);
208}
209
Rob Herring16bba302017-10-04 14:30:02 -0500210#if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200211static inline int of_property_check_flag(struct property *p, unsigned long flag)
212{
213 return test_bit(flag, &p->_flags);
214}
215
216static inline void of_property_set_flag(struct property *p, unsigned long flag)
217{
218 set_bit(flag, &p->_flags);
219}
220
221static inline void of_property_clear_flag(struct property *p, unsigned long flag)
222{
223 clear_bit(flag, &p->_flags);
224}
Rob Herring16bba302017-10-04 14:30:02 -0500225#endif
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200226
Grant Likely5063e252014-10-03 16:28:27 +0100227extern struct device_node *__of_find_all_nodes(struct device_node *prev);
Grant Likelye91edcf2009-10-15 10:58:09 -0600228extern struct device_node *of_find_all_nodes(struct device_node *prev);
229
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600230/*
Lennert Buytenhek3d6b8822011-02-22 18:18:51 +0100231 * OF address retrieval & translation
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600232 */
233
234/* Helper to read a big number; size is in cells (not bytes) */
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700235static inline u64 of_read_number(const __be32 *cell, int size)
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600236{
237 u64 r = 0;
238 while (size--)
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700239 r = (r << 32) | be32_to_cpu(*(cell++));
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600240 return r;
241}
242
243/* Like of_read_number, but we want an unsigned long result */
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700244static inline unsigned long of_read_ulong(const __be32 *cell, int size)
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600245{
Grant Likely2be09cb2009-11-23 20:16:46 -0700246 /* toss away upper bits if unsigned long is smaller than u64 */
247 return of_read_number(cell, size);
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600248}
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600249
Rob Herringb5b4bb32013-09-07 14:08:20 -0500250#if defined(CONFIG_SPARC)
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000251#include <asm/prom.h>
Rob Herringb5b4bb32013-09-07 14:08:20 -0500252#endif
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000253
Grant Likely7c7b60c2010-02-14 07:13:50 -0700254/* Default #address and #size cells. Allow arch asm/prom.h to override */
255#if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
256#define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
257#define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
258#endif
259
Grant Likely61e955d2009-10-15 10:57:51 -0600260#define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
261#define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
262
Steffen Trumtrarc0a05bf2012-12-18 11:32:03 +0100263static inline const char *of_node_full_name(const struct device_node *np)
Grant Likely74a7f082012-06-15 11:50:25 -0600264{
265 return np ? np->full_name : "<no-node>";
266}
267
Grant Likely5063e252014-10-03 16:28:27 +0100268#define for_each_of_allnodes_from(from, dn) \
269 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
270#define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000271extern struct device_node *of_find_node_by_name(struct device_node *from,
272 const char *name);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000273extern struct device_node *of_find_node_by_type(struct device_node *from,
274 const char *type);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000275extern struct device_node *of_find_compatible_node(struct device_node *from,
276 const char *type, const char *compat);
Stephen Warren50c8af42012-11-20 16:12:20 -0700277extern struct device_node *of_find_matching_node_and_match(
278 struct device_node *from,
279 const struct of_device_id *matches,
280 const struct of_device_id **match);
Rob Herring662372e2014-02-03 08:53:44 -0600281
Leif Lindholm75c28c02014-11-28 11:34:28 +0000282extern struct device_node *of_find_node_opts_by_path(const char *path,
283 const char **opts);
284static inline struct device_node *of_find_node_by_path(const char *path)
285{
286 return of_find_node_opts_by_path(path, NULL);
287}
288
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000289extern struct device_node *of_find_node_by_phandle(phandle handle);
290extern struct device_node *of_get_parent(const struct device_node *node);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000291extern struct device_node *of_get_next_parent(struct device_node *node);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000292extern struct device_node *of_get_next_child(const struct device_node *node,
293 struct device_node *prev);
Timur Tabi32961932012-08-14 13:20:23 +0000294extern struct device_node *of_get_next_available_child(
295 const struct device_node *node, struct device_node *prev);
296
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100297extern struct device_node *of_get_child_by_name(const struct device_node *node,
298 const char *name);
Bryan Wu954e04b2013-09-24 10:38:26 -0700299
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +0100300/* cache lookup */
301extern struct device_node *of_find_next_cache_node(const struct device_node *);
Sudeep Holla5fa23532017-01-16 10:40:43 +0000302extern int of_find_last_cache_level(unsigned int cpu);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000303extern struct device_node *of_find_node_with_property(
304 struct device_node *from, const char *prop_name);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000305
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000306extern struct property *of_find_property(const struct device_node *np,
307 const char *name,
308 int *lenp);
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100309extern int of_property_count_elems_of_size(const struct device_node *np,
310 const char *propname, int elem_size);
Tony Prisk3daf3722013-03-23 17:02:15 +1300311extern int of_property_read_u32_index(const struct device_node *np,
312 const char *propname,
313 u32 index, u32 *out_value);
Alistair Popple2475a2b2017-04-03 19:51:42 +1000314extern int of_property_read_u64_index(const struct device_node *np,
315 const char *propname,
316 u32 index, u64 *out_value);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100317extern int of_property_read_variable_u8_array(const struct device_node *np,
318 const char *propname, u8 *out_values,
319 size_t sz_min, size_t sz_max);
320extern int of_property_read_variable_u16_array(const struct device_node *np,
321 const char *propname, u16 *out_values,
322 size_t sz_min, size_t sz_max);
323extern int of_property_read_variable_u32_array(const struct device_node *np,
324 const char *propname,
325 u32 *out_values,
326 size_t sz_min,
327 size_t sz_max);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100328extern int of_property_read_u64(const struct device_node *np,
329 const char *propname, u64 *out_value);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100330extern int of_property_read_variable_u64_array(const struct device_node *np,
331 const char *propname,
332 u64 *out_values,
333 size_t sz_min,
334 size_t sz_max);
Rob Herring0e373632011-07-06 15:42:58 -0500335
David Rivshinfe99c702016-03-02 16:35:51 -0500336extern int of_property_read_string(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100337 const char *propname,
338 const char **out_string);
David Rivshinfe99c702016-03-02 16:35:51 -0500339extern int of_property_match_string(const struct device_node *np,
Grant Likely7aff0fe2011-12-12 09:25:58 -0700340 const char *propname,
341 const char *string);
David Rivshinfe99c702016-03-02 16:35:51 -0500342extern int of_property_read_string_helper(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000343 const char *propname,
344 const char **out_strs, size_t sz, int index);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000345extern int of_device_is_compatible(const struct device_node *device,
346 const char *);
Benjamin Herrenschmidtb9c13fe2016-07-08 08:35:59 +1000347extern int of_device_compatible_match(struct device_node *device,
348 const char *const *compat);
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800349extern bool of_device_is_available(const struct device_node *device);
Kevin Cernekee37786c72015-04-09 13:05:14 -0700350extern bool of_device_is_big_endian(const struct device_node *device);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000351extern const void *of_get_property(const struct device_node *node,
352 const char *name,
353 int *lenp);
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100354extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
Dong Aisheng8af0da92011-12-22 20:19:24 +0800355#define for_each_property_of_node(dn, pp) \
356 for (pp = dn->properties; pp != NULL; pp = pp->next)
Shawn Guo611cad72011-08-15 15:28:14 +0800357
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000358extern int of_n_addr_cells(struct device_node *np);
359extern int of_n_size_cells(struct device_node *np);
Grant Likely283029d2008-01-09 06:20:40 +1100360extern const struct of_device_id *of_match_node(
361 const struct of_device_id *matches, const struct device_node *node);
Grant Likely3f07af42008-07-25 22:25:13 -0400362extern int of_modalias_node(struct device_node *node, char *modalias, int len);
Grant Likely624cfca2013-10-11 22:05:10 +0100363extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100364extern struct device_node *of_parse_phandle(const struct device_node *np,
Grant Likely739649c2009-04-25 12:52:40 +0000365 const char *phandle_name,
366 int index);
Guennadi Liakhovetski93c667c2012-12-10 20:41:30 +0100367extern int of_parse_phandle_with_args(const struct device_node *np,
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000368 const char *list_name, const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -0700369 struct of_phandle_args *out_args);
Stephen Warren035fd942013-08-14 15:27:10 -0600370extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
371 const char *list_name, int cells_count, int index,
372 struct of_phandle_args *out_args);
Grant Likelybd69f732013-02-10 22:57:21 +0000373extern int of_count_phandle_with_args(const struct device_node *np,
374 const char *list_name, const char *cells_name);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000375
Joerg Roedel74e1fbb2016-04-04 17:49:17 +0200376/* phandle iterator functions */
377extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
378 const struct device_node *np,
379 const char *list_name,
380 const char *cells_name,
381 int cell_count);
382
Joerg Roedelcd209b42016-04-04 17:49:18 +0200383extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
Joerg Roedelabdaa772016-04-04 17:49:21 +0200384extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
385 uint32_t *args,
386 int size);
Joerg Roedelcd209b42016-04-04 17:49:18 +0200387
Shawn Guo611cad72011-08-15 15:28:14 +0800388extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
389extern int of_alias_get_id(struct device_node *np, const char *stem);
Wolfram Sang351d2242015-03-12 17:17:58 +0100390extern int of_alias_get_highest_id(const char *stem);
Shawn Guo611cad72011-08-15 15:28:14 +0800391
Grant Likely21b082e2010-02-14 07:13:38 -0700392extern int of_machine_is_compatible(const char *compat);
393
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000394extern int of_add_property(struct device_node *np, struct property *prop);
395extern int of_remove_property(struct device_node *np, struct property *prop);
396extern int of_update_property(struct device_node *np, struct property *newprop);
Grant Likely21b082e2010-02-14 07:13:38 -0700397
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700398/* For updating the device tree at runtime */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000399#define OF_RECONFIG_ATTACH_NODE 0x0001
400#define OF_RECONFIG_DETACH_NODE 0x0002
401#define OF_RECONFIG_ADD_PROPERTY 0x0003
402#define OF_RECONFIG_REMOVE_PROPERTY 0x0004
403#define OF_RECONFIG_UPDATE_PROPERTY 0x0005
404
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000405extern int of_attach_node(struct device_node *);
406extern int of_detach_node(struct device_node *);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700407
Ben Dooks3a1e3622011-08-03 10:11:42 +0100408#define of_match_ptr(_ptr) (_ptr)
Stephen Warrenc541adc2012-04-04 09:27:46 -0600409
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100410/**
411 * of_property_read_u8_array - Find and read an array of u8 from a property.
412 *
413 * @np: device node from which the property value is to be read.
414 * @propname: name of the property to be searched.
415 * @out_values: pointer to return value, modified only if return value is 0.
416 * @sz: number of array elements to read
417 *
418 * Search for a property in a device node and read 8-bit value(s) from
419 * it. Returns 0 on success, -EINVAL if the property does not exist,
420 * -ENODATA if property does not have a value, and -EOVERFLOW if the
421 * property data isn't large enough.
422 *
423 * dts entry of array should be like:
424 * property = /bits/ 8 <0x50 0x60 0x70>;
425 *
426 * The out_values is modified only if a valid u8 value can be decoded.
427 */
428static inline int of_property_read_u8_array(const struct device_node *np,
429 const char *propname,
430 u8 *out_values, size_t sz)
431{
432 int ret = of_property_read_variable_u8_array(np, propname, out_values,
433 sz, 0);
434 if (ret >= 0)
435 return 0;
436 else
437 return ret;
438}
439
440/**
441 * of_property_read_u16_array - Find and read an array of u16 from a property.
442 *
443 * @np: device node from which the property value is to be read.
444 * @propname: name of the property to be searched.
445 * @out_values: pointer to return value, modified only if return value is 0.
446 * @sz: number of array elements to read
447 *
448 * Search for a property in a device node and read 16-bit value(s) from
449 * it. Returns 0 on success, -EINVAL if the property does not exist,
450 * -ENODATA if property does not have a value, and -EOVERFLOW if the
451 * property data isn't large enough.
452 *
453 * dts entry of array should be like:
454 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
455 *
456 * The out_values is modified only if a valid u16 value can be decoded.
457 */
458static inline int of_property_read_u16_array(const struct device_node *np,
459 const char *propname,
460 u16 *out_values, size_t sz)
461{
462 int ret = of_property_read_variable_u16_array(np, propname, out_values,
463 sz, 0);
464 if (ret >= 0)
465 return 0;
466 else
467 return ret;
468}
469
470/**
471 * of_property_read_u32_array - Find and read an array of 32 bit integers
472 * from a property.
473 *
474 * @np: device node from which the property value is to be read.
475 * @propname: name of the property to be searched.
476 * @out_values: pointer to return value, modified only if return value is 0.
477 * @sz: number of array elements to read
478 *
479 * Search for a property in a device node and read 32-bit value(s) from
480 * it. Returns 0 on success, -EINVAL if the property does not exist,
481 * -ENODATA if property does not have a value, and -EOVERFLOW if the
482 * property data isn't large enough.
483 *
484 * The out_values is modified only if a valid u32 value can be decoded.
485 */
486static inline int of_property_read_u32_array(const struct device_node *np,
487 const char *propname,
488 u32 *out_values, size_t sz)
489{
490 int ret = of_property_read_variable_u32_array(np, propname, out_values,
491 sz, 0);
492 if (ret >= 0)
493 return 0;
494 else
495 return ret;
496}
497
498/**
499 * of_property_read_u64_array - Find and read an array of 64 bit integers
500 * from a property.
501 *
502 * @np: device node from which the property value is to be read.
503 * @propname: name of the property to be searched.
504 * @out_values: pointer to return value, modified only if return value is 0.
505 * @sz: number of array elements to read
506 *
507 * Search for a property in a device node and read 64-bit value(s) from
508 * it. Returns 0 on success, -EINVAL if the property does not exist,
509 * -ENODATA if property does not have a value, and -EOVERFLOW if the
510 * property data isn't large enough.
511 *
512 * The out_values is modified only if a valid u64 value can be decoded.
513 */
514static inline int of_property_read_u64_array(const struct device_node *np,
515 const char *propname,
516 u64 *out_values, size_t sz)
517{
518 int ret = of_property_read_variable_u64_array(np, propname, out_values,
519 sz, 0);
520 if (ret >= 0)
521 return 0;
522 else
523 return ret;
524}
525
Stephen Warrenc541adc2012-04-04 09:27:46 -0600526/*
527 * struct property *prop;
528 * const __be32 *p;
529 * u32 u;
530 *
531 * of_property_for_each_u32(np, "propname", prop, p, u)
532 * printk("U32 value: %x\n", u);
533 */
534const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
535 u32 *pu);
Stephen Warrenc541adc2012-04-04 09:27:46 -0600536/*
537 * struct property *prop;
538 * const char *s;
539 *
540 * of_property_for_each_string(np, "propname", prop, s)
541 * printk("String value: %s\n", s);
542 */
543const char *of_prop_next_string(struct property *prop, const char *cur);
Stephen Warrenc541adc2012-04-04 09:27:46 -0600544
Grant Likely3482f2c2014-03-27 17:18:55 -0700545bool of_console_check(struct device_node *dn, char *name, int index);
Sascha Hauer5c19e952013-08-05 14:40:44 +0200546
Suzuki K Poulosea0e71cd2018-01-02 11:25:27 +0000547extern int of_cpu_node_to_id(struct device_node *np);
548
Shawn Guob98c0232011-07-08 16:27:33 +0800549#else /* CONFIG_OF */
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100550
Sudeep Holla194ec932015-05-14 15:28:24 +0100551static inline void of_core_init(void)
552{
553}
554
Sakari Ailusd20dc142017-05-24 17:53:55 +0300555static inline bool is_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100556{
557 return false;
558}
559
Sakari Ailusd20dc142017-05-24 17:53:55 +0300560static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100561{
562 return NULL;
563}
564
Stephen Rothwell4c358e12014-05-15 14:44:30 +1000565static inline const char* of_node_full_name(const struct device_node *np)
Grant Likely74a7f082012-06-15 11:50:25 -0600566{
567 return "<no-node>";
568}
569
Peter Ujfalusi1cc44f42012-09-10 13:46:24 +0300570static inline struct device_node *of_find_node_by_name(struct device_node *from,
571 const char *name)
572{
573 return NULL;
574}
575
Rob Herring662372e2014-02-03 08:53:44 -0600576static inline struct device_node *of_find_node_by_type(struct device_node *from,
577 const char *type)
578{
579 return NULL;
580}
581
582static inline struct device_node *of_find_matching_node_and_match(
583 struct device_node *from,
584 const struct of_device_id *matches,
585 const struct of_device_id **match)
586{
587 return NULL;
588}
589
Alexander Shiyan20cd4772014-04-16 10:49:20 +0400590static inline struct device_node *of_find_node_by_path(const char *path)
591{
592 return NULL;
593}
594
Leif Lindholm75c28c02014-11-28 11:34:28 +0000595static inline struct device_node *of_find_node_opts_by_path(const char *path,
596 const char **opts)
597{
598 return NULL;
599}
600
Suman Annace16b9d2015-06-17 11:53:53 -0500601static inline struct device_node *of_find_node_by_phandle(phandle handle)
602{
603 return NULL;
604}
605
Alexander Shiyan066ec1d2013-04-09 19:47:40 +0400606static inline struct device_node *of_get_parent(const struct device_node *node)
607{
608 return NULL;
609}
610
Rob Herring662372e2014-02-03 08:53:44 -0600611static inline struct device_node *of_get_next_child(
612 const struct device_node *node, struct device_node *prev)
613{
614 return NULL;
615}
616
617static inline struct device_node *of_get_next_available_child(
618 const struct device_node *node, struct device_node *prev)
619{
620 return NULL;
621}
622
623static inline struct device_node *of_find_node_with_property(
624 struct device_node *from, const char *prop_name)
625{
626 return NULL;
627}
628
Sakari Ailus67831832017-03-28 10:52:23 +0300629#define of_fwnode_handle(node) NULL
630
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100631static inline bool of_have_populated_dt(void)
632{
633 return false;
634}
635
Olof Johansson25c040c2012-10-07 10:40:54 -0700636static inline struct device_node *of_get_child_by_name(
637 const struct device_node *node,
638 const char *name)
639{
640 return NULL;
641}
642
Rajendra Nayak36a09042011-10-10 21:49:35 +0530643static inline int of_device_is_compatible(const struct device_node *device,
644 const char *name)
645{
646 return 0;
647}
648
Geert Uytterhoeven3bc16302017-04-25 19:38:48 +0200649static inline int of_device_compatible_match(struct device_node *device,
650 const char *const *compat)
651{
652 return 0;
653}
654
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800655static inline bool of_device_is_available(const struct device_node *device)
Rob Herringd7195692013-03-20 16:56:18 -0500656{
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800657 return false;
Rob Herringd7195692013-03-20 16:56:18 -0500658}
659
Kevin Cernekee37786c72015-04-09 13:05:14 -0700660static inline bool of_device_is_big_endian(const struct device_node *device)
661{
662 return false;
663}
664
Stephen Warrenaba3dff2011-09-21 13:23:10 -0600665static inline struct property *of_find_property(const struct device_node *np,
666 const char *name,
667 int *lenp)
668{
669 return NULL;
670}
671
Shawn Guo2261cc62012-02-15 10:47:42 -0800672static inline struct device_node *of_find_compatible_node(
673 struct device_node *from,
674 const char *type,
675 const char *compat)
676{
677 return NULL;
678}
679
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100680static inline int of_property_count_elems_of_size(const struct device_node *np,
681 const char *propname, int elem_size)
682{
683 return -ENOSYS;
684}
685
Viresh Kumarbe193242012-11-20 10:15:19 +0530686static inline int of_property_read_u8_array(const struct device_node *np,
687 const char *propname, u8 *out_values, size_t sz)
688{
689 return -ENOSYS;
690}
691
692static inline int of_property_read_u16_array(const struct device_node *np,
693 const char *propname, u16 *out_values, size_t sz)
694{
695 return -ENOSYS;
696}
697
Shawn Guob98c0232011-07-08 16:27:33 +0800698static inline int of_property_read_u32_array(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100699 const char *propname,
700 u32 *out_values, size_t sz)
Shawn Guob98c0232011-07-08 16:27:33 +0800701{
702 return -ENOSYS;
703}
704
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100705static inline int of_property_read_u64_array(const struct device_node *np,
706 const char *propname,
707 u64 *out_values, size_t sz)
708{
709 return -ENOSYS;
710}
711
Arnd Bergmann96c623e2017-11-06 14:26:10 +0100712static inline int of_property_read_u32_index(const struct device_node *np,
713 const char *propname, u32 index, u32 *out_value)
Shawn Guob98c0232011-07-08 16:27:33 +0800714{
715 return -ENOSYS;
716}
717
Arnd Bergmann96c623e2017-11-06 14:26:10 +0100718static inline int of_property_read_u64_index(const struct device_node *np,
719 const char *propname, u32 index, u64 *out_value)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200720{
721 return -ENOSYS;
722}
723
Stephen Warren89272b82011-08-05 16:50:30 -0600724static inline const void *of_get_property(const struct device_node *node,
725 const char *name,
726 int *lenp)
727{
728 return NULL;
729}
730
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100731static inline struct device_node *of_get_cpu_node(int cpu,
732 unsigned int *thread)
733{
734 return NULL;
735}
736
Arnd Bergmann8a1ac5d2017-10-13 15:57:40 -0700737static inline int of_n_addr_cells(struct device_node *np)
738{
739 return 0;
740
741}
742static inline int of_n_size_cells(struct device_node *np)
743{
744 return 0;
745}
746
Arnd Bergmann96c623e2017-11-06 14:26:10 +0100747static inline int of_property_read_variable_u8_array(const struct device_node *np,
748 const char *propname, u8 *out_values,
749 size_t sz_min, size_t sz_max)
750{
751 return -ENOSYS;
752}
753
754static inline int of_property_read_variable_u16_array(const struct device_node *np,
755 const char *propname, u16 *out_values,
756 size_t sz_min, size_t sz_max)
757{
758 return -ENOSYS;
759}
760
761static inline int of_property_read_variable_u32_array(const struct device_node *np,
762 const char *propname,
763 u32 *out_values,
764 size_t sz_min,
765 size_t sz_max)
766{
767 return -ENOSYS;
768}
769
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100770static inline int of_property_read_u64(const struct device_node *np,
771 const char *propname, u64 *out_value)
772{
773 return -ENOSYS;
774}
775
Arnd Bergmann96c623e2017-11-06 14:26:10 +0100776static inline int of_property_read_variable_u64_array(const struct device_node *np,
777 const char *propname,
778 u64 *out_values,
779 size_t sz_min,
780 size_t sz_max)
781{
782 return -ENOSYS;
783}
784
785static inline int of_property_read_string(const struct device_node *np,
786 const char *propname,
787 const char **out_string)
788{
789 return -ENOSYS;
790}
791
David Rivshinfe99c702016-03-02 16:35:51 -0500792static inline int of_property_match_string(const struct device_node *np,
Thierry Redingbd3d5502012-04-13 16:18:34 +0200793 const char *propname,
794 const char *string)
795{
796 return -ENOSYS;
797}
798
Arnd Bergmann96c623e2017-11-06 14:26:10 +0100799static inline int of_property_read_string_helper(const struct device_node *np,
800 const char *propname,
801 const char **out_strs, size_t sz, int index)
802{
803 return -ENOSYS;
804}
805
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100806static inline struct device_node *of_parse_phandle(const struct device_node *np,
Rajendra Nayak36a09042011-10-10 21:49:35 +0530807 const char *phandle_name,
808 int index)
809{
810 return NULL;
811}
812
Kuninori Morimotoe93aeea2016-05-25 01:15:04 +0000813static inline int of_parse_phandle_with_args(const struct device_node *np,
Thierry Redinge05e5072012-04-13 16:19:21 +0200814 const char *list_name,
815 const char *cells_name,
816 int index,
817 struct of_phandle_args *out_args)
818{
819 return -ENOSYS;
820}
821
Stephen Warren035fd942013-08-14 15:27:10 -0600822static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
823 const char *list_name, int cells_count, int index,
824 struct of_phandle_args *out_args)
825{
826 return -ENOSYS;
827}
828
Grant Likelybd69f732013-02-10 22:57:21 +0000829static inline int of_count_phandle_with_args(struct device_node *np,
830 const char *list_name,
831 const char *cells_name)
832{
833 return -ENOSYS;
834}
835
Joerg Roedel74e1fbb2016-04-04 17:49:17 +0200836static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
837 const struct device_node *np,
838 const char *list_name,
839 const char *cells_name,
840 int cell_count)
841{
842 return -ENOSYS;
843}
844
Joerg Roedelcd209b42016-04-04 17:49:18 +0200845static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
846{
847 return -ENOSYS;
848}
849
Joerg Roedelabdaa772016-04-04 17:49:21 +0200850static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
851 uint32_t *args,
852 int size)
853{
854 return 0;
855}
856
Nicolas Ferreed5f8862011-10-27 11:07:28 +0200857static inline int of_alias_get_id(struct device_node *np, const char *stem)
858{
859 return -ENOSYS;
860}
861
Wolfram Sang351d2242015-03-12 17:17:58 +0100862static inline int of_alias_get_highest_id(const char *stem)
863{
864 return -ENOSYS;
865}
866
Stephen Warren50e07f82011-10-25 14:01:26 +0200867static inline int of_machine_is_compatible(const char *compat)
868{
869 return 0;
870}
871
Grant Likely3482f2c2014-03-27 17:18:55 -0700872static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +0200873{
Grant Likely3482f2c2014-03-27 17:18:55 -0700874 return false;
Sascha Hauer5c19e952013-08-05 14:40:44 +0200875}
876
Sebastian Andrzej Siewior2adfffa2013-06-17 16:48:13 +0200877static inline const __be32 *of_prop_next_u32(struct property *prop,
878 const __be32 *cur, u32 *pu)
879{
880 return NULL;
881}
882
883static inline const char *of_prop_next_string(struct property *prop,
884 const char *cur)
885{
886 return NULL;
887}
888
Pantelis Antoniou0384e8c2015-01-21 19:05:57 +0200889static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
890{
891 return 0;
892}
893
894static inline int of_node_test_and_set_flag(struct device_node *n,
895 unsigned long flag)
896{
897 return 0;
898}
899
900static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
901{
902}
903
904static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
905{
906}
907
908static inline int of_property_check_flag(struct property *p, unsigned long flag)
909{
910 return 0;
911}
912
913static inline void of_property_set_flag(struct property *p, unsigned long flag)
914{
915}
916
917static inline void of_property_clear_flag(struct property *p, unsigned long flag)
918{
919}
920
Suzuki K Poulosea0e71cd2018-01-02 11:25:27 +0000921static inline int of_cpu_node_to_id(struct device_node *np)
922{
923 return -ENODEV;
924}
925
Ben Dooks3a1e3622011-08-03 10:11:42 +0100926#define of_match_ptr(_ptr) NULL
Nicolas Ferre5762c202011-10-24 11:53:32 +0200927#define of_match_node(_matches, _node) NULL
Jeremy Kerr9dfbf202010-02-14 07:13:43 -0700928#endif /* CONFIG_OF */
Shawn Guob98c0232011-07-08 16:27:33 +0800929
Adam Thomson613e9722016-06-21 18:50:20 +0100930/* Default string compare functions, Allow arch asm/prom.h to override */
931#if !defined(of_compat_cmp)
932#define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
933#define of_prop_cmp(s1, s2) strcmp((s1), (s2))
934#define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
935#endif
936
Rob Herring0c3f0612013-09-17 10:42:50 -0500937#if defined(CONFIG_OF) && defined(CONFIG_NUMA)
938extern int of_node_to_nid(struct device_node *np);
939#else
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +0300940static inline int of_node_to_nid(struct device_node *device)
941{
942 return NUMA_NO_NODE;
943}
Paul Mundt5ca4db62012-06-03 22:04:34 -0700944#endif
945
David Daney298535c2016-04-08 15:50:25 -0700946#ifdef CONFIG_OF_NUMA
947extern int of_numa_init(void);
948#else
949static inline int of_numa_init(void)
950{
951 return -ENOSYS;
952}
953#endif
954
Rob Herring662372e2014-02-03 08:53:44 -0600955static inline struct device_node *of_find_matching_node(
956 struct device_node *from,
957 const struct of_device_id *matches)
958{
959 return of_find_matching_node_and_match(from, matches, NULL);
960}
961
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +0800962/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100963 * of_property_count_u8_elems - Count the number of u8 elements in a property
964 *
965 * @np: device node from which the property value is to be read.
966 * @propname: name of the property to be searched.
967 *
968 * Search for a property in a device node and count the number of u8 elements
969 * in it. Returns number of elements on sucess, -EINVAL if the property does
970 * not exist or its length does not match a multiple of u8 and -ENODATA if the
971 * property does not have a value.
972 */
973static inline int of_property_count_u8_elems(const struct device_node *np,
974 const char *propname)
975{
976 return of_property_count_elems_of_size(np, propname, sizeof(u8));
977}
978
979/**
980 * of_property_count_u16_elems - Count the number of u16 elements in a property
981 *
982 * @np: device node from which the property value is to be read.
983 * @propname: name of the property to be searched.
984 *
985 * Search for a property in a device node and count the number of u16 elements
986 * in it. Returns number of elements on sucess, -EINVAL if the property does
987 * not exist or its length does not match a multiple of u16 and -ENODATA if the
988 * property does not have a value.
989 */
990static inline int of_property_count_u16_elems(const struct device_node *np,
991 const char *propname)
992{
993 return of_property_count_elems_of_size(np, propname, sizeof(u16));
994}
995
996/**
997 * of_property_count_u32_elems - Count the number of u32 elements in a property
998 *
999 * @np: device node from which the property value is to be read.
1000 * @propname: name of the property to be searched.
1001 *
1002 * Search for a property in a device node and count the number of u32 elements
1003 * in it. Returns number of elements on sucess, -EINVAL if the property does
1004 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1005 * property does not have a value.
1006 */
1007static inline int of_property_count_u32_elems(const struct device_node *np,
1008 const char *propname)
1009{
1010 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1011}
1012
1013/**
1014 * of_property_count_u64_elems - Count the number of u64 elements in a property
1015 *
1016 * @np: device node from which the property value is to be read.
1017 * @propname: name of the property to be searched.
1018 *
1019 * Search for a property in a device node and count the number of u64 elements
1020 * in it. Returns number of elements on sucess, -EINVAL if the property does
1021 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1022 * property does not have a value.
1023 */
1024static inline int of_property_count_u64_elems(const struct device_node *np,
1025 const char *propname)
1026{
1027 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1028}
1029
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001030/**
Grant Likelya87fa1d2014-11-03 15:15:35 +00001031 * of_property_read_string_array() - Read an array of strings from a multiple
1032 * strings property.
1033 * @np: device node from which the property value is to be read.
1034 * @propname: name of the property to be searched.
1035 * @out_strs: output array of string pointers.
1036 * @sz: number of array elements to read.
1037 *
1038 * Search for a property in a device tree node and retrieve a list of
1039 * terminated string values (pointer to data, not a copy) in that property.
1040 *
1041 * If @out_strs is NULL, the number of strings in the property is returned.
1042 */
David Rivshinfe99c702016-03-02 16:35:51 -05001043static inline int of_property_read_string_array(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +00001044 const char *propname, const char **out_strs,
1045 size_t sz)
1046{
1047 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1048}
1049
1050/**
1051 * of_property_count_strings() - Find and return the number of strings from a
1052 * multiple strings property.
1053 * @np: device node from which the property value is to be read.
1054 * @propname: name of the property to be searched.
1055 *
1056 * Search for a property in a device tree node and retrieve the number of null
1057 * terminated string contain in it. Returns the number of strings on
1058 * success, -EINVAL if the property does not exist, -ENODATA if property
1059 * does not have a value, and -EILSEQ if the string is not null-terminated
1060 * within the length of the property data.
1061 */
David Rivshinfe99c702016-03-02 16:35:51 -05001062static inline int of_property_count_strings(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +00001063 const char *propname)
1064{
1065 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1066}
1067
1068/**
1069 * of_property_read_string_index() - Find and read a string from a multiple
1070 * strings property.
1071 * @np: device node from which the property value is to be read.
1072 * @propname: name of the property to be searched.
1073 * @index: index of the string in the list of strings
1074 * @out_string: pointer to null terminated return string, modified only if
1075 * return value is 0.
1076 *
1077 * Search for a property in a device tree node and retrieve a null
1078 * terminated string value (pointer to data, not a copy) in the list of strings
1079 * contained in that property.
1080 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1081 * property does not have a value, and -EILSEQ if the string is not
1082 * null-terminated within the length of the property data.
1083 *
1084 * The out_string pointer is modified only if a valid string can be decoded.
1085 */
David Rivshinfe99c702016-03-02 16:35:51 -05001086static inline int of_property_read_string_index(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +00001087 const char *propname,
1088 int index, const char **output)
1089{
1090 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1091 return rc < 0 ? rc : 0;
1092}
1093
1094/**
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001095 * of_property_read_bool - Findfrom a property
1096 * @np: device node from which the property value is to be read.
1097 * @propname: name of the property to be searched.
1098 *
1099 * Search for a property in a device node.
Geert Uytterhoeven31712c92015-05-04 19:42:19 +02001100 * Returns true if the property exists false otherwise.
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001101 */
1102static inline bool of_property_read_bool(const struct device_node *np,
1103 const char *propname)
1104{
1105 struct property *prop = of_find_property(np, propname, NULL);
1106
1107 return prop ? true : false;
1108}
1109
Viresh Kumarbe193242012-11-20 10:15:19 +05301110static inline int of_property_read_u8(const struct device_node *np,
1111 const char *propname,
1112 u8 *out_value)
1113{
1114 return of_property_read_u8_array(np, propname, out_value, 1);
1115}
1116
1117static inline int of_property_read_u16(const struct device_node *np,
1118 const char *propname,
1119 u16 *out_value)
1120{
1121 return of_property_read_u16_array(np, propname, out_value, 1);
1122}
1123
Shawn Guob98c0232011-07-08 16:27:33 +08001124static inline int of_property_read_u32(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +01001125 const char *propname,
Shawn Guob98c0232011-07-08 16:27:33 +08001126 u32 *out_value)
1127{
1128 return of_property_read_u32_array(np, propname, out_value, 1);
1129}
1130
Sebastian Reichele7a00e42014-04-06 12:52:11 +02001131static inline int of_property_read_s32(const struct device_node *np,
1132 const char *propname,
1133 s32 *out_value)
1134{
1135 return of_property_read_u32(np, propname, (u32*) out_value);
1136}
1137
Joerg Roedelf623ce92016-04-04 17:49:20 +02001138#define of_for_each_phandle(it, err, np, ln, cn, cc) \
1139 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1140 err = of_phandle_iterator_next(it); \
1141 err == 0; \
1142 err = of_phandle_iterator_next(it))
1143
Sebastian Andrzej Siewior2adfffa2013-06-17 16:48:13 +02001144#define of_property_for_each_u32(np, propname, prop, p, u) \
1145 for (prop = of_find_property(np, propname, NULL), \
1146 p = of_prop_next_u32(prop, NULL, &u); \
1147 p; \
1148 p = of_prop_next_u32(prop, p, &u))
1149
1150#define of_property_for_each_string(np, propname, prop, s) \
1151 for (prop = of_find_property(np, propname, NULL), \
1152 s = of_prop_next_string(prop, NULL); \
1153 s; \
1154 s = of_prop_next_string(prop, s))
1155
Rob Herring662372e2014-02-03 08:53:44 -06001156#define for_each_node_by_name(dn, name) \
1157 for (dn = of_find_node_by_name(NULL, name); dn; \
1158 dn = of_find_node_by_name(dn, name))
1159#define for_each_node_by_type(dn, type) \
1160 for (dn = of_find_node_by_type(NULL, type); dn; \
1161 dn = of_find_node_by_type(dn, type))
1162#define for_each_compatible_node(dn, type, compatible) \
1163 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1164 dn = of_find_compatible_node(dn, type, compatible))
1165#define for_each_matching_node(dn, matches) \
1166 for (dn = of_find_matching_node(NULL, matches); dn; \
1167 dn = of_find_matching_node(dn, matches))
1168#define for_each_matching_node_and_match(dn, matches, match) \
1169 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1170 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1171
1172#define for_each_child_of_node(parent, child) \
1173 for (child = of_get_next_child(parent, NULL); child != NULL; \
1174 child = of_get_next_child(parent, child))
1175#define for_each_available_child_of_node(parent, child) \
1176 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1177 child = of_get_next_available_child(parent, child))
1178
1179#define for_each_node_with_property(dn, prop_name) \
1180 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1181 dn = of_find_node_with_property(dn, prop_name))
1182
1183static inline int of_get_child_count(const struct device_node *np)
1184{
1185 struct device_node *child;
1186 int num = 0;
1187
1188 for_each_child_of_node(np, child)
1189 num++;
1190
1191 return num;
1192}
1193
1194static inline int of_get_available_child_count(const struct device_node *np)
1195{
1196 struct device_node *child;
1197 int num = 0;
1198
1199 for_each_available_child_of_node(np, child)
1200 num++;
1201
1202 return num;
1203}
1204
Masahiro Yamada71f50c62016-01-22 01:33:51 +09001205#if defined(CONFIG_OF) && !defined(MODULE)
Rob Herring54196cc2014-05-08 16:09:24 -05001206#define _OF_DECLARE(table, name, compat, fn, fn_type) \
1207 static const struct of_device_id __of_table_##name \
1208 __used __section(__##table##_of_table) \
1209 = { .compatible = compat, \
1210 .data = (fn == (fn_type)NULL) ? fn : fn }
1211#else
Thierry Reding5f563582014-10-02 16:01:10 +02001212#define _OF_DECLARE(table, name, compat, fn, fn_type) \
Rob Herring54196cc2014-05-08 16:09:24 -05001213 static const struct of_device_id __of_table_##name \
1214 __attribute__((unused)) \
1215 = { .compatible = compat, \
1216 .data = (fn == (fn_type)NULL) ? fn : fn }
1217#endif
1218
1219typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
Daniel Lezcanoc35d9292016-04-18 23:06:48 +02001220typedef int (*of_init_fn_1_ret)(struct device_node *);
Rob Herring54196cc2014-05-08 16:09:24 -05001221typedef void (*of_init_fn_1)(struct device_node *);
1222
1223#define OF_DECLARE_1(table, name, compat, fn) \
1224 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
Daniel Lezcanoc35d9292016-04-18 23:06:48 +02001225#define OF_DECLARE_1_RET(table, name, compat, fn) \
1226 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
Rob Herring54196cc2014-05-08 16:09:24 -05001227#define OF_DECLARE_2(table, name, compat, fn) \
1228 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1229
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001230/**
1231 * struct of_changeset_entry - Holds a changeset entry
1232 *
1233 * @node: list_head for the log list
1234 * @action: notifier action
1235 * @np: pointer to the device node affected
1236 * @prop: pointer to the property affected
1237 * @old_prop: hold a pointer to the original property
1238 *
1239 * Every modification of the device tree during a changeset
1240 * is held in a list of of_changeset_entry structures.
1241 * That way we can recover from a partial application, or we can
1242 * revert the changeset
1243 */
1244struct of_changeset_entry {
1245 struct list_head node;
1246 unsigned long action;
1247 struct device_node *np;
1248 struct property *prop;
1249 struct property *old_prop;
1250};
1251
1252/**
1253 * struct of_changeset - changeset tracker structure
1254 *
1255 * @entries: list_head for the changeset entries
1256 *
1257 * changesets are a convenient way to apply bulk changes to the
1258 * live tree. In case of an error, changes are rolled-back.
1259 * changesets live on after initial application, and if not
1260 * destroyed after use, they can be reverted in one single call.
1261 */
1262struct of_changeset {
1263 struct list_head entries;
1264};
1265
Pantelis Antonioub53a2342014-10-28 22:33:53 +02001266enum of_reconfig_change {
1267 OF_RECONFIG_NO_CHANGE = 0,
1268 OF_RECONFIG_CHANGE_ADD,
1269 OF_RECONFIG_CHANGE_REMOVE,
1270};
1271
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001272#ifdef CONFIG_OF_DYNAMIC
Grant Likelyf6892d12014-11-21 15:14:58 +00001273extern int of_reconfig_notifier_register(struct notifier_block *);
1274extern int of_reconfig_notifier_unregister(struct notifier_block *);
Grant Likelyf5242e52014-11-24 17:58:01 +00001275extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1276extern int of_reconfig_get_state_change(unsigned long action,
1277 struct of_reconfig_data *arg);
Grant Likelyf6892d12014-11-21 15:14:58 +00001278
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001279extern void of_changeset_init(struct of_changeset *ocs);
1280extern void of_changeset_destroy(struct of_changeset *ocs);
1281extern int of_changeset_apply(struct of_changeset *ocs);
1282extern int of_changeset_revert(struct of_changeset *ocs);
1283extern int of_changeset_action(struct of_changeset *ocs,
1284 unsigned long action, struct device_node *np,
1285 struct property *prop);
1286
1287static inline int of_changeset_attach_node(struct of_changeset *ocs,
1288 struct device_node *np)
1289{
1290 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1291}
1292
1293static inline int of_changeset_detach_node(struct of_changeset *ocs,
1294 struct device_node *np)
1295{
1296 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1297}
1298
1299static inline int of_changeset_add_property(struct of_changeset *ocs,
1300 struct device_node *np, struct property *prop)
1301{
1302 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1303}
1304
1305static inline int of_changeset_remove_property(struct of_changeset *ocs,
1306 struct device_node *np, struct property *prop)
1307{
1308 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1309}
1310
1311static inline int of_changeset_update_property(struct of_changeset *ocs,
1312 struct device_node *np, struct property *prop)
1313{
1314 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1315}
Grant Likelyf6892d12014-11-21 15:14:58 +00001316#else /* CONFIG_OF_DYNAMIC */
1317static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1318{
1319 return -EINVAL;
1320}
1321static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1322{
1323 return -EINVAL;
1324}
Grant Likelyf5242e52014-11-24 17:58:01 +00001325static inline int of_reconfig_notify(unsigned long action,
1326 struct of_reconfig_data *arg)
Grant Likelyf6892d12014-11-21 15:14:58 +00001327{
1328 return -EINVAL;
1329}
Grant Likelyf5242e52014-11-24 17:58:01 +00001330static inline int of_reconfig_get_state_change(unsigned long action,
1331 struct of_reconfig_data *arg)
Grant Likelyf6892d12014-11-21 15:14:58 +00001332{
1333 return -EINVAL;
1334}
1335#endif /* CONFIG_OF_DYNAMIC */
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001336
Romain Periera4b4e042014-10-14 06:31:09 +00001337/**
Romain Perier8f731102014-11-25 12:28:25 +00001338 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
Romain Periera4b4e042014-10-14 06:31:09 +00001339 * @np: Pointer to the given device_node
1340 *
1341 * return true if present false otherwise
1342 */
Romain Perier8f731102014-11-25 12:28:25 +00001343static inline bool of_device_is_system_power_controller(const struct device_node *np)
Romain Periera4b4e042014-10-14 06:31:09 +00001344{
Romain Perier8f731102014-11-25 12:28:25 +00001345 return of_property_read_bool(np, "system-power-controller");
Romain Periera4b4e042014-10-14 06:31:09 +00001346}
1347
Linus Torvalds7ef58b32014-12-11 13:06:58 -08001348/**
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001349 * Overlay support
1350 */
1351
Alan Tull39a842e2016-11-01 14:14:22 -05001352enum of_overlay_notify_action {
Frank Rowand24789c52017-10-17 16:36:26 -07001353 OF_OVERLAY_PRE_APPLY = 0,
Alan Tull39a842e2016-11-01 14:14:22 -05001354 OF_OVERLAY_POST_APPLY,
1355 OF_OVERLAY_PRE_REMOVE,
1356 OF_OVERLAY_POST_REMOVE,
1357};
1358
1359struct of_overlay_notify_data {
1360 struct device_node *overlay;
1361 struct device_node *target;
1362};
1363
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001364#ifdef CONFIG_OF_OVERLAY
1365
1366/* ID based overlays; the API for external users */
Frank Rowand24789c52017-10-17 16:36:26 -07001367int of_overlay_apply(struct device_node *tree, int *ovcs_id);
1368int of_overlay_remove(int *ovcs_id);
Frank Rowand0290c4c2017-10-17 16:36:23 -07001369int of_overlay_remove_all(void);
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001370
Alan Tull39a842e2016-11-01 14:14:22 -05001371int of_overlay_notifier_register(struct notifier_block *nb);
1372int of_overlay_notifier_unregister(struct notifier_block *nb);
1373
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001374#else
1375
Frank Rowand24789c52017-10-17 16:36:26 -07001376static inline int of_overlay_apply(struct device_node *tree, int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001377{
1378 return -ENOTSUPP;
1379}
1380
Frank Rowand24789c52017-10-17 16:36:26 -07001381static inline int of_overlay_remove(int *ovcs_id)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001382{
1383 return -ENOTSUPP;
1384}
1385
Frank Rowand0290c4c2017-10-17 16:36:23 -07001386static inline int of_overlay_remove_all(void)
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001387{
1388 return -ENOTSUPP;
1389}
1390
Alan Tull39a842e2016-11-01 14:14:22 -05001391static inline int of_overlay_notifier_register(struct notifier_block *nb)
1392{
1393 return 0;
1394}
1395
1396static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1397{
1398 return 0;
1399}
1400
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001401#endif
1402
Stephen Rothwell76c1ce72007-05-01 16:19:07 +10001403#endif /* _LINUX_OF_H */