blob: 29b7b738b5090d66bfb217dc8c6a8ab43b06edb1 [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;
40 unsigned long _flags;
41 unsigned int unique_id;
Grant Likely75b57ec2014-02-20 18:02:11 +000042 struct bin_attribute attr;
Grant Likely731581e2009-10-15 10:57:46 -060043};
44
Grant Likely6f192492009-10-15 10:57:49 -060045#if defined(CONFIG_SPARC)
46struct of_irq_controller;
47#endif
48
49struct device_node {
50 const char *name;
51 const char *type;
Grant Likely6016a362010-01-28 14:06:53 -070052 phandle phandle;
Grant Likelyc22618a2012-11-14 22:37:12 +000053 const char *full_name;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +010054 struct fwnode_handle fwnode;
Grant Likely6f192492009-10-15 10:57:49 -060055
56 struct property *properties;
57 struct property *deadprops; /* removed properties */
58 struct device_node *parent;
59 struct device_node *child;
60 struct device_node *sibling;
Grant Likely75b57ec2014-02-20 18:02:11 +000061 struct kobject kobj;
Grant Likely6f192492009-10-15 10:57:49 -060062 unsigned long _flags;
63 void *data;
64#if defined(CONFIG_SPARC)
Grant Likelyc22618a2012-11-14 22:37:12 +000065 const char *path_component_name;
Grant Likely6f192492009-10-15 10:57:49 -060066 unsigned int unique_id;
67 struct of_irq_controller *irq_trans;
68#endif
69};
70
Andreas Herrmannb66548e22014-01-17 12:08:30 +010071#define MAX_PHANDLE_ARGS 16
Grant Likely15c9a0a2011-12-12 09:25:57 -070072struct of_phandle_args {
73 struct device_node *np;
74 int args_count;
75 uint32_t args[MAX_PHANDLE_ARGS];
76};
77
Joerg Roedel74e1fbb2016-04-04 17:49:17 +020078struct of_phandle_iterator {
79 /* Common iterator information */
80 const char *cells_name;
81 int cell_count;
82 const struct device_node *parent;
83
84 /* List size information */
85 const __be32 *list_end;
86 const __be32 *phandle_end;
87
88 /* Current position state */
89 const __be32 *cur;
90 uint32_t cur_count;
91 phandle phandle;
92 struct device_node *node;
93};
94
Grant Likelyf5242e52014-11-24 17:58:01 +000095struct of_reconfig_data {
96 struct device_node *dn;
97 struct property *prop;
98 struct property *old_prop;
99};
100
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200101/* initialize a node */
102extern struct kobj_type of_node_ktype;
103static inline void of_node_init(struct device_node *node)
104{
105 kobject_init(&node->kobj, &of_node_ktype);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100106 node->fwnode.type = FWNODE_OF;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200107}
108
109/* true when node is initialized */
110static inline int of_node_is_initialized(struct device_node *node)
111{
112 return node && node->kobj.state_initialized;
113}
114
115/* true when node is attached (i.e. present on sysfs) */
116static inline int of_node_is_attached(struct device_node *node)
117{
118 return node && node->kobj.state_in_sysfs;
119}
120
Grant Likely0f22dd32012-02-15 20:38:40 -0700121#ifdef CONFIG_OF_DYNAMIC
122extern struct device_node *of_node_get(struct device_node *node);
123extern void of_node_put(struct device_node *node);
124#else /* CONFIG_OF_DYNAMIC */
Rob Herring3ecdd052011-12-13 09:13:54 -0600125/* Dummy ref counting routines - to be implemented later */
126static inline struct device_node *of_node_get(struct device_node *node)
127{
128 return node;
129}
Grant Likely0f22dd32012-02-15 20:38:40 -0700130static inline void of_node_put(struct device_node *node) { }
131#endif /* !CONFIG_OF_DYNAMIC */
Rob Herring3ecdd052011-12-13 09:13:54 -0600132
Grant Likely41f88002009-11-23 20:07:01 -0700133/* Pointer for first entry in chain of all nodes. */
Grant Likely5063e252014-10-03 16:28:27 +0100134extern struct device_node *of_root;
Grant Likelyfc0bdae2010-02-14 07:13:55 -0700135extern struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +0800136extern struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -0700137extern struct device_node *of_stdout;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500138extern raw_spinlock_t devtree_lock;
Grant Likely41f88002009-11-23 20:07:01 -0700139
Pantelis Antoniou391b4592015-04-24 12:41:56 +0300140/* flag descriptions (need to be visible even when !CONFIG_OF) */
141#define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
142#define OF_DETACHED 2 /* node has been detached from the device tree */
143#define OF_POPULATED 3 /* device already created for the node */
144#define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
145
Guenter Roeckb1d06b62015-11-06 19:28:22 -0800146#define OF_BAD_ADDR ((u64)-1)
147
Hans de Goede6d09dc62014-11-14 13:26:52 +0100148#ifdef CONFIG_OF
Sudeep Holla194ec932015-05-14 15:28:24 +0100149void of_core_init(void);
150
Sakari Ailusd20dc142017-05-24 17:53:55 +0300151static inline bool is_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100152{
Heikki Krogerus0224a4a2016-04-27 14:04:20 +0300153 return !IS_ERR_OR_NULL(fwnode) && fwnode->type == FWNODE_OF;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100154}
155
Sakari Ailusd20dc142017-05-24 17:53:55 +0300156#define to_of_node(__fwnode) \
157 ({ \
158 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
159 \
160 is_of_node(__to_of_node_fwnode) ? \
161 container_of(__to_of_node_fwnode, \
162 struct device_node, fwnode) : \
163 NULL; \
164 })
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100165
Sakari Ailusdebd3a32017-05-24 17:53:54 +0300166#define of_fwnode_handle(node) \
167 ({ \
168 typeof(node) __of_fwnode_handle_node = (node); \
169 \
170 __of_fwnode_handle_node ? \
171 &__of_fwnode_handle_node->fwnode : NULL; \
172 })
Sakari Ailus67831832017-03-28 10:52:23 +0300173
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100174static inline bool of_have_populated_dt(void)
175{
Grant Likely5063e252014-10-03 16:28:27 +0100176 return of_root != NULL;
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100177}
178
Andres Salomon035ebef2010-07-13 09:42:26 +0000179static inline bool of_node_is_root(const struct device_node *node)
180{
181 return node && (node->parent == NULL);
182}
183
Grant Likely50436312009-10-15 10:57:58 -0600184static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
185{
186 return test_bit(flag, &n->_flags);
187}
188
Pawel Mollc6e126d2014-05-15 16:55:24 +0100189static inline int of_node_test_and_set_flag(struct device_node *n,
190 unsigned long flag)
191{
192 return test_and_set_bit(flag, &n->_flags);
193}
194
Grant Likely50436312009-10-15 10:57:58 -0600195static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
196{
197 set_bit(flag, &n->_flags);
198}
199
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200200static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
201{
202 clear_bit(flag, &n->_flags);
203}
204
205static inline int of_property_check_flag(struct property *p, unsigned long flag)
206{
207 return test_bit(flag, &p->_flags);
208}
209
210static inline void of_property_set_flag(struct property *p, unsigned long flag)
211{
212 set_bit(flag, &p->_flags);
213}
214
215static inline void of_property_clear_flag(struct property *p, unsigned long flag)
216{
217 clear_bit(flag, &p->_flags);
218}
219
Grant Likely5063e252014-10-03 16:28:27 +0100220extern struct device_node *__of_find_all_nodes(struct device_node *prev);
Grant Likelye91edcf2009-10-15 10:58:09 -0600221extern struct device_node *of_find_all_nodes(struct device_node *prev);
222
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600223/*
Lennert Buytenhek3d6b8822011-02-22 18:18:51 +0100224 * OF address retrieval & translation
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600225 */
226
227/* Helper to read a big number; size is in cells (not bytes) */
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700228static inline u64 of_read_number(const __be32 *cell, int size)
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600229{
230 u64 r = 0;
231 while (size--)
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700232 r = (r << 32) | be32_to_cpu(*(cell++));
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600233 return r;
234}
235
236/* Like of_read_number, but we want an unsigned long result */
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700237static inline unsigned long of_read_ulong(const __be32 *cell, int size)
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600238{
Grant Likely2be09cb2009-11-23 20:16:46 -0700239 /* toss away upper bits if unsigned long is smaller than u64 */
240 return of_read_number(cell, size);
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600241}
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600242
Rob Herringb5b4bb32013-09-07 14:08:20 -0500243#if defined(CONFIG_SPARC)
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000244#include <asm/prom.h>
Rob Herringb5b4bb32013-09-07 14:08:20 -0500245#endif
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000246
Grant Likely7c7b60c2010-02-14 07:13:50 -0700247/* Default #address and #size cells. Allow arch asm/prom.h to override */
248#if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
249#define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
250#define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
251#endif
252
Grant Likely61e955d2009-10-15 10:57:51 -0600253#define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
254#define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
255
Steffen Trumtrarc0a05bf2012-12-18 11:32:03 +0100256static inline const char *of_node_full_name(const struct device_node *np)
Grant Likely74a7f082012-06-15 11:50:25 -0600257{
258 return np ? np->full_name : "<no-node>";
259}
260
Grant Likely5063e252014-10-03 16:28:27 +0100261#define for_each_of_allnodes_from(from, dn) \
262 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
263#define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000264extern struct device_node *of_find_node_by_name(struct device_node *from,
265 const char *name);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000266extern struct device_node *of_find_node_by_type(struct device_node *from,
267 const char *type);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000268extern struct device_node *of_find_compatible_node(struct device_node *from,
269 const char *type, const char *compat);
Stephen Warren50c8af42012-11-20 16:12:20 -0700270extern struct device_node *of_find_matching_node_and_match(
271 struct device_node *from,
272 const struct of_device_id *matches,
273 const struct of_device_id **match);
Rob Herring662372e2014-02-03 08:53:44 -0600274
Leif Lindholm75c28c02014-11-28 11:34:28 +0000275extern struct device_node *of_find_node_opts_by_path(const char *path,
276 const char **opts);
277static inline struct device_node *of_find_node_by_path(const char *path)
278{
279 return of_find_node_opts_by_path(path, NULL);
280}
281
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000282extern struct device_node *of_find_node_by_phandle(phandle handle);
283extern struct device_node *of_get_parent(const struct device_node *node);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000284extern struct device_node *of_get_next_parent(struct device_node *node);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000285extern struct device_node *of_get_next_child(const struct device_node *node,
286 struct device_node *prev);
Timur Tabi32961932012-08-14 13:20:23 +0000287extern struct device_node *of_get_next_available_child(
288 const struct device_node *node, struct device_node *prev);
289
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100290extern struct device_node *of_get_child_by_name(const struct device_node *node,
291 const char *name);
Bryan Wu954e04b2013-09-24 10:38:26 -0700292
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +0100293/* cache lookup */
294extern struct device_node *of_find_next_cache_node(const struct device_node *);
Sudeep Holla5fa23532017-01-16 10:40:43 +0000295extern int of_find_last_cache_level(unsigned int cpu);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000296extern struct device_node *of_find_node_with_property(
297 struct device_node *from, const char *prop_name);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000298
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000299extern struct property *of_find_property(const struct device_node *np,
300 const char *name,
301 int *lenp);
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100302extern int of_property_count_elems_of_size(const struct device_node *np,
303 const char *propname, int elem_size);
Tony Prisk3daf3722013-03-23 17:02:15 +1300304extern int of_property_read_u32_index(const struct device_node *np,
305 const char *propname,
306 u32 index, u32 *out_value);
Alistair Popple2475a2b2017-04-03 19:51:42 +1000307extern int of_property_read_u64_index(const struct device_node *np,
308 const char *propname,
309 u32 index, u64 *out_value);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100310extern int of_property_read_variable_u8_array(const struct device_node *np,
311 const char *propname, u8 *out_values,
312 size_t sz_min, size_t sz_max);
313extern int of_property_read_variable_u16_array(const struct device_node *np,
314 const char *propname, u16 *out_values,
315 size_t sz_min, size_t sz_max);
316extern int of_property_read_variable_u32_array(const struct device_node *np,
317 const char *propname,
318 u32 *out_values,
319 size_t sz_min,
320 size_t sz_max);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100321extern int of_property_read_u64(const struct device_node *np,
322 const char *propname, u64 *out_value);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100323extern int of_property_read_variable_u64_array(const struct device_node *np,
324 const char *propname,
325 u64 *out_values,
326 size_t sz_min,
327 size_t sz_max);
Rob Herring0e373632011-07-06 15:42:58 -0500328
David Rivshinfe99c702016-03-02 16:35:51 -0500329extern int of_property_read_string(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100330 const char *propname,
331 const char **out_string);
David Rivshinfe99c702016-03-02 16:35:51 -0500332extern int of_property_match_string(const struct device_node *np,
Grant Likely7aff0fe2011-12-12 09:25:58 -0700333 const char *propname,
334 const char *string);
David Rivshinfe99c702016-03-02 16:35:51 -0500335extern int of_property_read_string_helper(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000336 const char *propname,
337 const char **out_strs, size_t sz, int index);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000338extern int of_device_is_compatible(const struct device_node *device,
339 const char *);
Benjamin Herrenschmidtb9c13fe2016-07-08 08:35:59 +1000340extern int of_device_compatible_match(struct device_node *device,
341 const char *const *compat);
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800342extern bool of_device_is_available(const struct device_node *device);
Kevin Cernekee37786c72015-04-09 13:05:14 -0700343extern bool of_device_is_big_endian(const struct device_node *device);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000344extern const void *of_get_property(const struct device_node *node,
345 const char *name,
346 int *lenp);
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100347extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
Dong Aisheng8af0da92011-12-22 20:19:24 +0800348#define for_each_property_of_node(dn, pp) \
349 for (pp = dn->properties; pp != NULL; pp = pp->next)
Shawn Guo611cad72011-08-15 15:28:14 +0800350
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000351extern int of_n_addr_cells(struct device_node *np);
352extern int of_n_size_cells(struct device_node *np);
Grant Likely283029d2008-01-09 06:20:40 +1100353extern const struct of_device_id *of_match_node(
354 const struct of_device_id *matches, const struct device_node *node);
Grant Likely3f07af42008-07-25 22:25:13 -0400355extern int of_modalias_node(struct device_node *node, char *modalias, int len);
Grant Likely624cfca2013-10-11 22:05:10 +0100356extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100357extern struct device_node *of_parse_phandle(const struct device_node *np,
Grant Likely739649c2009-04-25 12:52:40 +0000358 const char *phandle_name,
359 int index);
Guennadi Liakhovetski93c667c2012-12-10 20:41:30 +0100360extern int of_parse_phandle_with_args(const struct device_node *np,
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000361 const char *list_name, const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -0700362 struct of_phandle_args *out_args);
Stephen Warren035fd942013-08-14 15:27:10 -0600363extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
364 const char *list_name, int cells_count, int index,
365 struct of_phandle_args *out_args);
Grant Likelybd69f732013-02-10 22:57:21 +0000366extern int of_count_phandle_with_args(const struct device_node *np,
367 const char *list_name, const char *cells_name);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000368
Joerg Roedel74e1fbb2016-04-04 17:49:17 +0200369/* phandle iterator functions */
370extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
371 const struct device_node *np,
372 const char *list_name,
373 const char *cells_name,
374 int cell_count);
375
Joerg Roedelcd209b42016-04-04 17:49:18 +0200376extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
Joerg Roedelabdaa772016-04-04 17:49:21 +0200377extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
378 uint32_t *args,
379 int size);
Joerg Roedelcd209b42016-04-04 17:49:18 +0200380
Shawn Guo611cad72011-08-15 15:28:14 +0800381extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
382extern int of_alias_get_id(struct device_node *np, const char *stem);
Wolfram Sang351d2242015-03-12 17:17:58 +0100383extern int of_alias_get_highest_id(const char *stem);
Shawn Guo611cad72011-08-15 15:28:14 +0800384
Grant Likely21b082e2010-02-14 07:13:38 -0700385extern int of_machine_is_compatible(const char *compat);
386
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000387extern int of_add_property(struct device_node *np, struct property *prop);
388extern int of_remove_property(struct device_node *np, struct property *prop);
389extern int of_update_property(struct device_node *np, struct property *newprop);
Grant Likely21b082e2010-02-14 07:13:38 -0700390
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700391/* For updating the device tree at runtime */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000392#define OF_RECONFIG_ATTACH_NODE 0x0001
393#define OF_RECONFIG_DETACH_NODE 0x0002
394#define OF_RECONFIG_ADD_PROPERTY 0x0003
395#define OF_RECONFIG_REMOVE_PROPERTY 0x0004
396#define OF_RECONFIG_UPDATE_PROPERTY 0x0005
397
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000398extern int of_attach_node(struct device_node *);
399extern int of_detach_node(struct device_node *);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700400
Ben Dooks3a1e3622011-08-03 10:11:42 +0100401#define of_match_ptr(_ptr) (_ptr)
Stephen Warrenc541adc2012-04-04 09:27:46 -0600402
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100403/**
404 * of_property_read_u8_array - Find and read an array of u8 from a property.
405 *
406 * @np: device node from which the property value is to be read.
407 * @propname: name of the property to be searched.
408 * @out_values: pointer to return value, modified only if return value is 0.
409 * @sz: number of array elements to read
410 *
411 * Search for a property in a device node and read 8-bit value(s) from
412 * it. Returns 0 on success, -EINVAL if the property does not exist,
413 * -ENODATA if property does not have a value, and -EOVERFLOW if the
414 * property data isn't large enough.
415 *
416 * dts entry of array should be like:
417 * property = /bits/ 8 <0x50 0x60 0x70>;
418 *
419 * The out_values is modified only if a valid u8 value can be decoded.
420 */
421static inline int of_property_read_u8_array(const struct device_node *np,
422 const char *propname,
423 u8 *out_values, size_t sz)
424{
425 int ret = of_property_read_variable_u8_array(np, propname, out_values,
426 sz, 0);
427 if (ret >= 0)
428 return 0;
429 else
430 return ret;
431}
432
433/**
434 * of_property_read_u16_array - Find and read an array of u16 from a property.
435 *
436 * @np: device node from which the property value is to be read.
437 * @propname: name of the property to be searched.
438 * @out_values: pointer to return value, modified only if return value is 0.
439 * @sz: number of array elements to read
440 *
441 * Search for a property in a device node and read 16-bit value(s) from
442 * it. Returns 0 on success, -EINVAL if the property does not exist,
443 * -ENODATA if property does not have a value, and -EOVERFLOW if the
444 * property data isn't large enough.
445 *
446 * dts entry of array should be like:
447 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
448 *
449 * The out_values is modified only if a valid u16 value can be decoded.
450 */
451static inline int of_property_read_u16_array(const struct device_node *np,
452 const char *propname,
453 u16 *out_values, size_t sz)
454{
455 int ret = of_property_read_variable_u16_array(np, propname, out_values,
456 sz, 0);
457 if (ret >= 0)
458 return 0;
459 else
460 return ret;
461}
462
463/**
464 * of_property_read_u32_array - Find and read an array of 32 bit integers
465 * from a property.
466 *
467 * @np: device node from which the property value is to be read.
468 * @propname: name of the property to be searched.
469 * @out_values: pointer to return value, modified only if return value is 0.
470 * @sz: number of array elements to read
471 *
472 * Search for a property in a device node and read 32-bit value(s) from
473 * it. Returns 0 on success, -EINVAL if the property does not exist,
474 * -ENODATA if property does not have a value, and -EOVERFLOW if the
475 * property data isn't large enough.
476 *
477 * The out_values is modified only if a valid u32 value can be decoded.
478 */
479static inline int of_property_read_u32_array(const struct device_node *np,
480 const char *propname,
481 u32 *out_values, size_t sz)
482{
483 int ret = of_property_read_variable_u32_array(np, propname, out_values,
484 sz, 0);
485 if (ret >= 0)
486 return 0;
487 else
488 return ret;
489}
490
491/**
492 * of_property_read_u64_array - Find and read an array of 64 bit integers
493 * from a property.
494 *
495 * @np: device node from which the property value is to be read.
496 * @propname: name of the property to be searched.
497 * @out_values: pointer to return value, modified only if return value is 0.
498 * @sz: number of array elements to read
499 *
500 * Search for a property in a device node and read 64-bit value(s) from
501 * it. Returns 0 on success, -EINVAL if the property does not exist,
502 * -ENODATA if property does not have a value, and -EOVERFLOW if the
503 * property data isn't large enough.
504 *
505 * The out_values is modified only if a valid u64 value can be decoded.
506 */
507static inline int of_property_read_u64_array(const struct device_node *np,
508 const char *propname,
509 u64 *out_values, size_t sz)
510{
511 int ret = of_property_read_variable_u64_array(np, propname, out_values,
512 sz, 0);
513 if (ret >= 0)
514 return 0;
515 else
516 return ret;
517}
518
Stephen Warrenc541adc2012-04-04 09:27:46 -0600519/*
520 * struct property *prop;
521 * const __be32 *p;
522 * u32 u;
523 *
524 * of_property_for_each_u32(np, "propname", prop, p, u)
525 * printk("U32 value: %x\n", u);
526 */
527const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
528 u32 *pu);
Stephen Warrenc541adc2012-04-04 09:27:46 -0600529/*
530 * struct property *prop;
531 * const char *s;
532 *
533 * of_property_for_each_string(np, "propname", prop, s)
534 * printk("String value: %s\n", s);
535 */
536const char *of_prop_next_string(struct property *prop, const char *cur);
Stephen Warrenc541adc2012-04-04 09:27:46 -0600537
Grant Likely3482f2c2014-03-27 17:18:55 -0700538bool of_console_check(struct device_node *dn, char *name, int index);
Sascha Hauer5c19e952013-08-05 14:40:44 +0200539
Shawn Guob98c0232011-07-08 16:27:33 +0800540#else /* CONFIG_OF */
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100541
Sudeep Holla194ec932015-05-14 15:28:24 +0100542static inline void of_core_init(void)
543{
544}
545
Sakari Ailusd20dc142017-05-24 17:53:55 +0300546static inline bool is_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100547{
548 return false;
549}
550
Sakari Ailusd20dc142017-05-24 17:53:55 +0300551static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100552{
553 return NULL;
554}
555
Stephen Rothwell4c358e12014-05-15 14:44:30 +1000556static inline const char* of_node_full_name(const struct device_node *np)
Grant Likely74a7f082012-06-15 11:50:25 -0600557{
558 return "<no-node>";
559}
560
Peter Ujfalusi1cc44f42012-09-10 13:46:24 +0300561static inline struct device_node *of_find_node_by_name(struct device_node *from,
562 const char *name)
563{
564 return NULL;
565}
566
Rob Herring662372e2014-02-03 08:53:44 -0600567static inline struct device_node *of_find_node_by_type(struct device_node *from,
568 const char *type)
569{
570 return NULL;
571}
572
573static inline struct device_node *of_find_matching_node_and_match(
574 struct device_node *from,
575 const struct of_device_id *matches,
576 const struct of_device_id **match)
577{
578 return NULL;
579}
580
Alexander Shiyan20cd4772014-04-16 10:49:20 +0400581static inline struct device_node *of_find_node_by_path(const char *path)
582{
583 return NULL;
584}
585
Leif Lindholm75c28c02014-11-28 11:34:28 +0000586static inline struct device_node *of_find_node_opts_by_path(const char *path,
587 const char **opts)
588{
589 return NULL;
590}
591
Suman Annace16b9d2015-06-17 11:53:53 -0500592static inline struct device_node *of_find_node_by_phandle(phandle handle)
593{
594 return NULL;
595}
596
Alexander Shiyan066ec1d2013-04-09 19:47:40 +0400597static inline struct device_node *of_get_parent(const struct device_node *node)
598{
599 return NULL;
600}
601
Rob Herring662372e2014-02-03 08:53:44 -0600602static inline struct device_node *of_get_next_child(
603 const struct device_node *node, struct device_node *prev)
604{
605 return NULL;
606}
607
608static inline struct device_node *of_get_next_available_child(
609 const struct device_node *node, struct device_node *prev)
610{
611 return NULL;
612}
613
614static inline struct device_node *of_find_node_with_property(
615 struct device_node *from, const char *prop_name)
616{
617 return NULL;
618}
619
Sakari Ailus67831832017-03-28 10:52:23 +0300620#define of_fwnode_handle(node) NULL
621
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100622static inline bool of_have_populated_dt(void)
623{
624 return false;
625}
626
Olof Johansson25c040c2012-10-07 10:40:54 -0700627static inline struct device_node *of_get_child_by_name(
628 const struct device_node *node,
629 const char *name)
630{
631 return NULL;
632}
633
Rajendra Nayak36a09042011-10-10 21:49:35 +0530634static inline int of_device_is_compatible(const struct device_node *device,
635 const char *name)
636{
637 return 0;
638}
639
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800640static inline bool of_device_is_available(const struct device_node *device)
Rob Herringd7195692013-03-20 16:56:18 -0500641{
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800642 return false;
Rob Herringd7195692013-03-20 16:56:18 -0500643}
644
Kevin Cernekee37786c72015-04-09 13:05:14 -0700645static inline bool of_device_is_big_endian(const struct device_node *device)
646{
647 return false;
648}
649
Stephen Warrenaba3dff2011-09-21 13:23:10 -0600650static inline struct property *of_find_property(const struct device_node *np,
651 const char *name,
652 int *lenp)
653{
654 return NULL;
655}
656
Shawn Guo2261cc62012-02-15 10:47:42 -0800657static inline struct device_node *of_find_compatible_node(
658 struct device_node *from,
659 const char *type,
660 const char *compat)
661{
662 return NULL;
663}
664
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100665static inline int of_property_count_elems_of_size(const struct device_node *np,
666 const char *propname, int elem_size)
667{
668 return -ENOSYS;
669}
670
Tony Prisk3daf3722013-03-23 17:02:15 +1300671static inline int of_property_read_u32_index(const struct device_node *np,
672 const char *propname, u32 index, u32 *out_value)
673{
674 return -ENOSYS;
675}
676
Viresh Kumarbe193242012-11-20 10:15:19 +0530677static inline int of_property_read_u8_array(const struct device_node *np,
678 const char *propname, u8 *out_values, size_t sz)
679{
680 return -ENOSYS;
681}
682
683static inline int of_property_read_u16_array(const struct device_node *np,
684 const char *propname, u16 *out_values, size_t sz)
685{
686 return -ENOSYS;
687}
688
Shawn Guob98c0232011-07-08 16:27:33 +0800689static inline int of_property_read_u32_array(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100690 const char *propname,
691 u32 *out_values, size_t sz)
Shawn Guob98c0232011-07-08 16:27:33 +0800692{
693 return -ENOSYS;
694}
695
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100696static inline int of_property_read_u64_array(const struct device_node *np,
697 const char *propname,
698 u64 *out_values, size_t sz)
699{
700 return -ENOSYS;
701}
702
David Rivshinfe99c702016-03-02 16:35:51 -0500703static inline int of_property_read_string(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100704 const char *propname,
705 const char **out_string)
Shawn Guob98c0232011-07-08 16:27:33 +0800706{
707 return -ENOSYS;
708}
709
David Rivshinfe99c702016-03-02 16:35:51 -0500710static inline int of_property_read_string_helper(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000711 const char *propname,
712 const char **out_strs, size_t sz, int index)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200713{
714 return -ENOSYS;
715}
716
Stephen Warren89272b82011-08-05 16:50:30 -0600717static inline const void *of_get_property(const struct device_node *node,
718 const char *name,
719 int *lenp)
720{
721 return NULL;
722}
723
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100724static inline struct device_node *of_get_cpu_node(int cpu,
725 unsigned int *thread)
726{
727 return NULL;
728}
729
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100730static inline int of_property_read_u64(const struct device_node *np,
731 const char *propname, u64 *out_value)
732{
733 return -ENOSYS;
734}
735
David Rivshinfe99c702016-03-02 16:35:51 -0500736static inline int of_property_match_string(const struct device_node *np,
Thierry Redingbd3d5502012-04-13 16:18:34 +0200737 const char *propname,
738 const char *string)
739{
740 return -ENOSYS;
741}
742
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100743static inline struct device_node *of_parse_phandle(const struct device_node *np,
Rajendra Nayak36a09042011-10-10 21:49:35 +0530744 const char *phandle_name,
745 int index)
746{
747 return NULL;
748}
749
Kuninori Morimotoe93aeea2016-05-25 01:15:04 +0000750static inline int of_parse_phandle_with_args(const struct device_node *np,
Thierry Redinge05e5072012-04-13 16:19:21 +0200751 const char *list_name,
752 const char *cells_name,
753 int index,
754 struct of_phandle_args *out_args)
755{
756 return -ENOSYS;
757}
758
Stephen Warren035fd942013-08-14 15:27:10 -0600759static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
760 const char *list_name, int cells_count, int index,
761 struct of_phandle_args *out_args)
762{
763 return -ENOSYS;
764}
765
Grant Likelybd69f732013-02-10 22:57:21 +0000766static inline int of_count_phandle_with_args(struct device_node *np,
767 const char *list_name,
768 const char *cells_name)
769{
770 return -ENOSYS;
771}
772
Joerg Roedel74e1fbb2016-04-04 17:49:17 +0200773static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
774 const struct device_node *np,
775 const char *list_name,
776 const char *cells_name,
777 int cell_count)
778{
779 return -ENOSYS;
780}
781
Joerg Roedelcd209b42016-04-04 17:49:18 +0200782static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
783{
784 return -ENOSYS;
785}
786
Joerg Roedelabdaa772016-04-04 17:49:21 +0200787static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
788 uint32_t *args,
789 int size)
790{
791 return 0;
792}
793
Nicolas Ferreed5f8862011-10-27 11:07:28 +0200794static inline int of_alias_get_id(struct device_node *np, const char *stem)
795{
796 return -ENOSYS;
797}
798
Wolfram Sang351d2242015-03-12 17:17:58 +0100799static inline int of_alias_get_highest_id(const char *stem)
800{
801 return -ENOSYS;
802}
803
Stephen Warren50e07f82011-10-25 14:01:26 +0200804static inline int of_machine_is_compatible(const char *compat)
805{
806 return 0;
807}
808
Grant Likely3482f2c2014-03-27 17:18:55 -0700809static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +0200810{
Grant Likely3482f2c2014-03-27 17:18:55 -0700811 return false;
Sascha Hauer5c19e952013-08-05 14:40:44 +0200812}
813
Sebastian Andrzej Siewior2adfffa2013-06-17 16:48:13 +0200814static inline const __be32 *of_prop_next_u32(struct property *prop,
815 const __be32 *cur, u32 *pu)
816{
817 return NULL;
818}
819
820static inline const char *of_prop_next_string(struct property *prop,
821 const char *cur)
822{
823 return NULL;
824}
825
Pantelis Antoniou0384e8c2015-01-21 19:05:57 +0200826static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
827{
828 return 0;
829}
830
831static inline int of_node_test_and_set_flag(struct device_node *n,
832 unsigned long flag)
833{
834 return 0;
835}
836
837static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
838{
839}
840
841static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
842{
843}
844
845static inline int of_property_check_flag(struct property *p, unsigned long flag)
846{
847 return 0;
848}
849
850static inline void of_property_set_flag(struct property *p, unsigned long flag)
851{
852}
853
854static inline void of_property_clear_flag(struct property *p, unsigned long flag)
855{
856}
857
Ben Dooks3a1e3622011-08-03 10:11:42 +0100858#define of_match_ptr(_ptr) NULL
Nicolas Ferre5762c202011-10-24 11:53:32 +0200859#define of_match_node(_matches, _node) NULL
Jeremy Kerr9dfbf202010-02-14 07:13:43 -0700860#endif /* CONFIG_OF */
Shawn Guob98c0232011-07-08 16:27:33 +0800861
Adam Thomson613e9722016-06-21 18:50:20 +0100862/* Default string compare functions, Allow arch asm/prom.h to override */
863#if !defined(of_compat_cmp)
864#define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
865#define of_prop_cmp(s1, s2) strcmp((s1), (s2))
866#define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
867#endif
868
Rob Herring0c3f0612013-09-17 10:42:50 -0500869#if defined(CONFIG_OF) && defined(CONFIG_NUMA)
870extern int of_node_to_nid(struct device_node *np);
871#else
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +0300872static inline int of_node_to_nid(struct device_node *device)
873{
874 return NUMA_NO_NODE;
875}
Paul Mundt5ca4db62012-06-03 22:04:34 -0700876#endif
877
David Daney298535c2016-04-08 15:50:25 -0700878#ifdef CONFIG_OF_NUMA
879extern int of_numa_init(void);
880#else
881static inline int of_numa_init(void)
882{
883 return -ENOSYS;
884}
885#endif
886
Rob Herring662372e2014-02-03 08:53:44 -0600887static inline struct device_node *of_find_matching_node(
888 struct device_node *from,
889 const struct of_device_id *matches)
890{
891 return of_find_matching_node_and_match(from, matches, NULL);
892}
893
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +0800894/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100895 * of_property_count_u8_elems - Count the number of u8 elements in a property
896 *
897 * @np: device node from which the property value is to be read.
898 * @propname: name of the property to be searched.
899 *
900 * Search for a property in a device node and count the number of u8 elements
901 * in it. Returns number of elements on sucess, -EINVAL if the property does
902 * not exist or its length does not match a multiple of u8 and -ENODATA if the
903 * property does not have a value.
904 */
905static inline int of_property_count_u8_elems(const struct device_node *np,
906 const char *propname)
907{
908 return of_property_count_elems_of_size(np, propname, sizeof(u8));
909}
910
911/**
912 * of_property_count_u16_elems - Count the number of u16 elements in a property
913 *
914 * @np: device node from which the property value is to be read.
915 * @propname: name of the property to be searched.
916 *
917 * Search for a property in a device node and count the number of u16 elements
918 * in it. Returns number of elements on sucess, -EINVAL if the property does
919 * not exist or its length does not match a multiple of u16 and -ENODATA if the
920 * property does not have a value.
921 */
922static inline int of_property_count_u16_elems(const struct device_node *np,
923 const char *propname)
924{
925 return of_property_count_elems_of_size(np, propname, sizeof(u16));
926}
927
928/**
929 * of_property_count_u32_elems - Count the number of u32 elements in a property
930 *
931 * @np: device node from which the property value is to be read.
932 * @propname: name of the property to be searched.
933 *
934 * Search for a property in a device node and count the number of u32 elements
935 * in it. Returns number of elements on sucess, -EINVAL if the property does
936 * not exist or its length does not match a multiple of u32 and -ENODATA if the
937 * property does not have a value.
938 */
939static inline int of_property_count_u32_elems(const struct device_node *np,
940 const char *propname)
941{
942 return of_property_count_elems_of_size(np, propname, sizeof(u32));
943}
944
945/**
946 * of_property_count_u64_elems - Count the number of u64 elements in a property
947 *
948 * @np: device node from which the property value is to be read.
949 * @propname: name of the property to be searched.
950 *
951 * Search for a property in a device node and count the number of u64 elements
952 * in it. Returns number of elements on sucess, -EINVAL if the property does
953 * not exist or its length does not match a multiple of u64 and -ENODATA if the
954 * property does not have a value.
955 */
956static inline int of_property_count_u64_elems(const struct device_node *np,
957 const char *propname)
958{
959 return of_property_count_elems_of_size(np, propname, sizeof(u64));
960}
961
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +0800962/**
Grant Likelya87fa1d2014-11-03 15:15:35 +0000963 * of_property_read_string_array() - Read an array of strings from a multiple
964 * strings property.
965 * @np: device node from which the property value is to be read.
966 * @propname: name of the property to be searched.
967 * @out_strs: output array of string pointers.
968 * @sz: number of array elements to read.
969 *
970 * Search for a property in a device tree node and retrieve a list of
971 * terminated string values (pointer to data, not a copy) in that property.
972 *
973 * If @out_strs is NULL, the number of strings in the property is returned.
974 */
David Rivshinfe99c702016-03-02 16:35:51 -0500975static inline int of_property_read_string_array(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000976 const char *propname, const char **out_strs,
977 size_t sz)
978{
979 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
980}
981
982/**
983 * of_property_count_strings() - Find and return the number of strings from a
984 * multiple strings property.
985 * @np: device node from which the property value is to be read.
986 * @propname: name of the property to be searched.
987 *
988 * Search for a property in a device tree node and retrieve the number of null
989 * terminated string contain in it. Returns the number of strings on
990 * success, -EINVAL if the property does not exist, -ENODATA if property
991 * does not have a value, and -EILSEQ if the string is not null-terminated
992 * within the length of the property data.
993 */
David Rivshinfe99c702016-03-02 16:35:51 -0500994static inline int of_property_count_strings(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000995 const char *propname)
996{
997 return of_property_read_string_helper(np, propname, NULL, 0, 0);
998}
999
1000/**
1001 * of_property_read_string_index() - Find and read a string from a multiple
1002 * strings property.
1003 * @np: device node from which the property value is to be read.
1004 * @propname: name of the property to be searched.
1005 * @index: index of the string in the list of strings
1006 * @out_string: pointer to null terminated return string, modified only if
1007 * return value is 0.
1008 *
1009 * Search for a property in a device tree node and retrieve a null
1010 * terminated string value (pointer to data, not a copy) in the list of strings
1011 * contained in that property.
1012 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1013 * property does not have a value, and -EILSEQ if the string is not
1014 * null-terminated within the length of the property data.
1015 *
1016 * The out_string pointer is modified only if a valid string can be decoded.
1017 */
David Rivshinfe99c702016-03-02 16:35:51 -05001018static inline int of_property_read_string_index(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +00001019 const char *propname,
1020 int index, const char **output)
1021{
1022 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1023 return rc < 0 ? rc : 0;
1024}
1025
1026/**
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001027 * of_property_read_bool - Findfrom a property
1028 * @np: device node from which the property value is to be read.
1029 * @propname: name of the property to be searched.
1030 *
1031 * Search for a property in a device node.
Geert Uytterhoeven31712c92015-05-04 19:42:19 +02001032 * Returns true if the property exists false otherwise.
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001033 */
1034static inline bool of_property_read_bool(const struct device_node *np,
1035 const char *propname)
1036{
1037 struct property *prop = of_find_property(np, propname, NULL);
1038
1039 return prop ? true : false;
1040}
1041
Viresh Kumarbe193242012-11-20 10:15:19 +05301042static inline int of_property_read_u8(const struct device_node *np,
1043 const char *propname,
1044 u8 *out_value)
1045{
1046 return of_property_read_u8_array(np, propname, out_value, 1);
1047}
1048
1049static inline int of_property_read_u16(const struct device_node *np,
1050 const char *propname,
1051 u16 *out_value)
1052{
1053 return of_property_read_u16_array(np, propname, out_value, 1);
1054}
1055
Shawn Guob98c0232011-07-08 16:27:33 +08001056static inline int of_property_read_u32(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +01001057 const char *propname,
Shawn Guob98c0232011-07-08 16:27:33 +08001058 u32 *out_value)
1059{
1060 return of_property_read_u32_array(np, propname, out_value, 1);
1061}
1062
Sebastian Reichele7a00e42014-04-06 12:52:11 +02001063static inline int of_property_read_s32(const struct device_node *np,
1064 const char *propname,
1065 s32 *out_value)
1066{
1067 return of_property_read_u32(np, propname, (u32*) out_value);
1068}
1069
Joerg Roedelf623ce92016-04-04 17:49:20 +02001070#define of_for_each_phandle(it, err, np, ln, cn, cc) \
1071 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1072 err = of_phandle_iterator_next(it); \
1073 err == 0; \
1074 err = of_phandle_iterator_next(it))
1075
Sebastian Andrzej Siewior2adfffa2013-06-17 16:48:13 +02001076#define of_property_for_each_u32(np, propname, prop, p, u) \
1077 for (prop = of_find_property(np, propname, NULL), \
1078 p = of_prop_next_u32(prop, NULL, &u); \
1079 p; \
1080 p = of_prop_next_u32(prop, p, &u))
1081
1082#define of_property_for_each_string(np, propname, prop, s) \
1083 for (prop = of_find_property(np, propname, NULL), \
1084 s = of_prop_next_string(prop, NULL); \
1085 s; \
1086 s = of_prop_next_string(prop, s))
1087
Rob Herring662372e2014-02-03 08:53:44 -06001088#define for_each_node_by_name(dn, name) \
1089 for (dn = of_find_node_by_name(NULL, name); dn; \
1090 dn = of_find_node_by_name(dn, name))
1091#define for_each_node_by_type(dn, type) \
1092 for (dn = of_find_node_by_type(NULL, type); dn; \
1093 dn = of_find_node_by_type(dn, type))
1094#define for_each_compatible_node(dn, type, compatible) \
1095 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1096 dn = of_find_compatible_node(dn, type, compatible))
1097#define for_each_matching_node(dn, matches) \
1098 for (dn = of_find_matching_node(NULL, matches); dn; \
1099 dn = of_find_matching_node(dn, matches))
1100#define for_each_matching_node_and_match(dn, matches, match) \
1101 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1102 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1103
1104#define for_each_child_of_node(parent, child) \
1105 for (child = of_get_next_child(parent, NULL); child != NULL; \
1106 child = of_get_next_child(parent, child))
1107#define for_each_available_child_of_node(parent, child) \
1108 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1109 child = of_get_next_available_child(parent, child))
1110
1111#define for_each_node_with_property(dn, prop_name) \
1112 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1113 dn = of_find_node_with_property(dn, prop_name))
1114
1115static inline int of_get_child_count(const struct device_node *np)
1116{
1117 struct device_node *child;
1118 int num = 0;
1119
1120 for_each_child_of_node(np, child)
1121 num++;
1122
1123 return num;
1124}
1125
1126static inline int of_get_available_child_count(const struct device_node *np)
1127{
1128 struct device_node *child;
1129 int num = 0;
1130
1131 for_each_available_child_of_node(np, child)
1132 num++;
1133
1134 return num;
1135}
1136
Masahiro Yamada71f50c62016-01-22 01:33:51 +09001137#if defined(CONFIG_OF) && !defined(MODULE)
Rob Herring54196cc2014-05-08 16:09:24 -05001138#define _OF_DECLARE(table, name, compat, fn, fn_type) \
1139 static const struct of_device_id __of_table_##name \
1140 __used __section(__##table##_of_table) \
1141 = { .compatible = compat, \
1142 .data = (fn == (fn_type)NULL) ? fn : fn }
1143#else
Thierry Reding5f563582014-10-02 16:01:10 +02001144#define _OF_DECLARE(table, name, compat, fn, fn_type) \
Rob Herring54196cc2014-05-08 16:09:24 -05001145 static const struct of_device_id __of_table_##name \
1146 __attribute__((unused)) \
1147 = { .compatible = compat, \
1148 .data = (fn == (fn_type)NULL) ? fn : fn }
1149#endif
1150
1151typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
Daniel Lezcanoc35d9292016-04-18 23:06:48 +02001152typedef int (*of_init_fn_1_ret)(struct device_node *);
Rob Herring54196cc2014-05-08 16:09:24 -05001153typedef void (*of_init_fn_1)(struct device_node *);
1154
1155#define OF_DECLARE_1(table, name, compat, fn) \
1156 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
Daniel Lezcanoc35d9292016-04-18 23:06:48 +02001157#define OF_DECLARE_1_RET(table, name, compat, fn) \
1158 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
Rob Herring54196cc2014-05-08 16:09:24 -05001159#define OF_DECLARE_2(table, name, compat, fn) \
1160 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1161
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001162/**
1163 * struct of_changeset_entry - Holds a changeset entry
1164 *
1165 * @node: list_head for the log list
1166 * @action: notifier action
1167 * @np: pointer to the device node affected
1168 * @prop: pointer to the property affected
1169 * @old_prop: hold a pointer to the original property
1170 *
1171 * Every modification of the device tree during a changeset
1172 * is held in a list of of_changeset_entry structures.
1173 * That way we can recover from a partial application, or we can
1174 * revert the changeset
1175 */
1176struct of_changeset_entry {
1177 struct list_head node;
1178 unsigned long action;
1179 struct device_node *np;
1180 struct property *prop;
1181 struct property *old_prop;
1182};
1183
1184/**
1185 * struct of_changeset - changeset tracker structure
1186 *
1187 * @entries: list_head for the changeset entries
1188 *
1189 * changesets are a convenient way to apply bulk changes to the
1190 * live tree. In case of an error, changes are rolled-back.
1191 * changesets live on after initial application, and if not
1192 * destroyed after use, they can be reverted in one single call.
1193 */
1194struct of_changeset {
1195 struct list_head entries;
1196};
1197
Pantelis Antonioub53a2342014-10-28 22:33:53 +02001198enum of_reconfig_change {
1199 OF_RECONFIG_NO_CHANGE = 0,
1200 OF_RECONFIG_CHANGE_ADD,
1201 OF_RECONFIG_CHANGE_REMOVE,
1202};
1203
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001204#ifdef CONFIG_OF_DYNAMIC
Grant Likelyf6892d12014-11-21 15:14:58 +00001205extern int of_reconfig_notifier_register(struct notifier_block *);
1206extern int of_reconfig_notifier_unregister(struct notifier_block *);
Grant Likelyf5242e52014-11-24 17:58:01 +00001207extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1208extern int of_reconfig_get_state_change(unsigned long action,
1209 struct of_reconfig_data *arg);
Grant Likelyf6892d12014-11-21 15:14:58 +00001210
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001211extern void of_changeset_init(struct of_changeset *ocs);
1212extern void of_changeset_destroy(struct of_changeset *ocs);
1213extern int of_changeset_apply(struct of_changeset *ocs);
1214extern int of_changeset_revert(struct of_changeset *ocs);
1215extern int of_changeset_action(struct of_changeset *ocs,
1216 unsigned long action, struct device_node *np,
1217 struct property *prop);
1218
1219static inline int of_changeset_attach_node(struct of_changeset *ocs,
1220 struct device_node *np)
1221{
1222 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1223}
1224
1225static inline int of_changeset_detach_node(struct of_changeset *ocs,
1226 struct device_node *np)
1227{
1228 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1229}
1230
1231static inline int of_changeset_add_property(struct of_changeset *ocs,
1232 struct device_node *np, struct property *prop)
1233{
1234 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1235}
1236
1237static inline int of_changeset_remove_property(struct of_changeset *ocs,
1238 struct device_node *np, struct property *prop)
1239{
1240 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1241}
1242
1243static inline int of_changeset_update_property(struct of_changeset *ocs,
1244 struct device_node *np, struct property *prop)
1245{
1246 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1247}
Grant Likelyf6892d12014-11-21 15:14:58 +00001248#else /* CONFIG_OF_DYNAMIC */
1249static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1250{
1251 return -EINVAL;
1252}
1253static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1254{
1255 return -EINVAL;
1256}
Grant Likelyf5242e52014-11-24 17:58:01 +00001257static inline int of_reconfig_notify(unsigned long action,
1258 struct of_reconfig_data *arg)
Grant Likelyf6892d12014-11-21 15:14:58 +00001259{
1260 return -EINVAL;
1261}
Grant Likelyf5242e52014-11-24 17:58:01 +00001262static inline int of_reconfig_get_state_change(unsigned long action,
1263 struct of_reconfig_data *arg)
Grant Likelyf6892d12014-11-21 15:14:58 +00001264{
1265 return -EINVAL;
1266}
1267#endif /* CONFIG_OF_DYNAMIC */
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001268
Pantelis Antoniou7941b272014-07-04 19:59:20 +03001269/* CONFIG_OF_RESOLVE api */
1270extern int of_resolve_phandles(struct device_node *tree);
1271
Romain Periera4b4e042014-10-14 06:31:09 +00001272/**
Romain Perier8f731102014-11-25 12:28:25 +00001273 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
Romain Periera4b4e042014-10-14 06:31:09 +00001274 * @np: Pointer to the given device_node
1275 *
1276 * return true if present false otherwise
1277 */
Romain Perier8f731102014-11-25 12:28:25 +00001278static inline bool of_device_is_system_power_controller(const struct device_node *np)
Romain Periera4b4e042014-10-14 06:31:09 +00001279{
Romain Perier8f731102014-11-25 12:28:25 +00001280 return of_property_read_bool(np, "system-power-controller");
Romain Periera4b4e042014-10-14 06:31:09 +00001281}
1282
Linus Torvalds7ef58b32014-12-11 13:06:58 -08001283/**
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001284 * Overlay support
1285 */
1286
Alan Tull39a842e2016-11-01 14:14:22 -05001287enum of_overlay_notify_action {
1288 OF_OVERLAY_PRE_APPLY,
1289 OF_OVERLAY_POST_APPLY,
1290 OF_OVERLAY_PRE_REMOVE,
1291 OF_OVERLAY_POST_REMOVE,
1292};
1293
1294struct of_overlay_notify_data {
1295 struct device_node *overlay;
1296 struct device_node *target;
1297};
1298
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001299#ifdef CONFIG_OF_OVERLAY
1300
1301/* ID based overlays; the API for external users */
1302int of_overlay_create(struct device_node *tree);
1303int of_overlay_destroy(int id);
1304int of_overlay_destroy_all(void);
1305
Alan Tull39a842e2016-11-01 14:14:22 -05001306int of_overlay_notifier_register(struct notifier_block *nb);
1307int of_overlay_notifier_unregister(struct notifier_block *nb);
1308
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001309#else
1310
1311static inline int of_overlay_create(struct device_node *tree)
1312{
1313 return -ENOTSUPP;
1314}
1315
1316static inline int of_overlay_destroy(int id)
1317{
1318 return -ENOTSUPP;
1319}
1320
1321static inline int of_overlay_destroy_all(void)
1322{
1323 return -ENOTSUPP;
1324}
1325
Alan Tull39a842e2016-11-01 14:14:22 -05001326static inline int of_overlay_notifier_register(struct notifier_block *nb)
1327{
1328 return 0;
1329}
1330
1331static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1332{
1333 return 0;
1334}
1335
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001336#endif
1337
Stephen Rothwell76c1ce72007-05-01 16:19:07 +10001338#endif /* _LINUX_OF_H */