blob: 2d685e76940903332981aa91e484630dfdab6bf3 [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
Grant Likely75b57ec2014-02-20 18:02:11 +000046 struct bin_attribute attr;
Grant Likely731581e2009-10-15 10:57:46 -060047};
48
Grant Likely6f192492009-10-15 10:57:49 -060049#if defined(CONFIG_SPARC)
50struct of_irq_controller;
51#endif
52
53struct device_node {
54 const char *name;
55 const char *type;
Grant Likely6016a362010-01-28 14:06:53 -070056 phandle phandle;
Grant Likelyc22618a2012-11-14 22:37:12 +000057 const char *full_name;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +010058 struct fwnode_handle fwnode;
Grant Likely6f192492009-10-15 10:57:49 -060059
60 struct property *properties;
61 struct property *deadprops; /* removed properties */
62 struct device_node *parent;
63 struct device_node *child;
64 struct device_node *sibling;
Grant Likely75b57ec2014-02-20 18:02:11 +000065 struct kobject kobj;
Grant Likely6f192492009-10-15 10:57:49 -060066 unsigned long _flags;
67 void *data;
68#if defined(CONFIG_SPARC)
Grant Likelyc22618a2012-11-14 22:37:12 +000069 const char *path_component_name;
Grant Likely6f192492009-10-15 10:57:49 -060070 unsigned int unique_id;
71 struct of_irq_controller *irq_trans;
72#endif
73};
74
Andreas Herrmannb66548e22014-01-17 12:08:30 +010075#define MAX_PHANDLE_ARGS 16
Grant Likely15c9a0a2011-12-12 09:25:57 -070076struct of_phandle_args {
77 struct device_node *np;
78 int args_count;
79 uint32_t args[MAX_PHANDLE_ARGS];
80};
81
Joerg Roedel74e1fbb2016-04-04 17:49:17 +020082struct of_phandle_iterator {
83 /* Common iterator information */
84 const char *cells_name;
85 int cell_count;
86 const struct device_node *parent;
87
88 /* List size information */
89 const __be32 *list_end;
90 const __be32 *phandle_end;
91
92 /* Current position state */
93 const __be32 *cur;
94 uint32_t cur_count;
95 phandle phandle;
96 struct device_node *node;
97};
98
Grant Likelyf5242e52014-11-24 17:58:01 +000099struct of_reconfig_data {
100 struct device_node *dn;
101 struct property *prop;
102 struct property *old_prop;
103};
104
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200105/* initialize a node */
106extern struct kobj_type of_node_ktype;
Sakari Ailus37081842017-06-06 12:37:37 +0300107extern const struct fwnode_operations of_fwnode_ops;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200108static inline void of_node_init(struct device_node *node)
109{
110 kobject_init(&node->kobj, &of_node_ktype);
Sakari Ailus37081842017-06-06 12:37:37 +0300111 node->fwnode.ops = &of_fwnode_ops;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200112}
113
Rob Herring0c3c2342017-10-04 14:04:01 -0500114#define of_node_kobj(n) (&(n)->kobj)
115
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200116/* true when node is initialized */
117static inline int of_node_is_initialized(struct device_node *node)
118{
119 return node && node->kobj.state_initialized;
120}
121
122/* true when node is attached (i.e. present on sysfs) */
123static inline int of_node_is_attached(struct device_node *node)
124{
125 return node && node->kobj.state_in_sysfs;
126}
127
Grant Likely0f22dd32012-02-15 20:38:40 -0700128#ifdef CONFIG_OF_DYNAMIC
129extern struct device_node *of_node_get(struct device_node *node);
130extern void of_node_put(struct device_node *node);
131#else /* CONFIG_OF_DYNAMIC */
Rob Herring3ecdd052011-12-13 09:13:54 -0600132/* Dummy ref counting routines - to be implemented later */
133static inline struct device_node *of_node_get(struct device_node *node)
134{
135 return node;
136}
Grant Likely0f22dd32012-02-15 20:38:40 -0700137static inline void of_node_put(struct device_node *node) { }
138#endif /* !CONFIG_OF_DYNAMIC */
Rob Herring3ecdd052011-12-13 09:13:54 -0600139
Grant Likely41f88002009-11-23 20:07:01 -0700140/* Pointer for first entry in chain of all nodes. */
Grant Likely5063e252014-10-03 16:28:27 +0100141extern struct device_node *of_root;
Grant Likelyfc0bdae2010-02-14 07:13:55 -0700142extern struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +0800143extern struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -0700144extern struct device_node *of_stdout;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500145extern raw_spinlock_t devtree_lock;
Grant Likely41f88002009-11-23 20:07:01 -0700146
Pantelis Antoniou391b4592015-04-24 12:41:56 +0300147/* flag descriptions (need to be visible even when !CONFIG_OF) */
148#define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
149#define OF_DETACHED 2 /* node has been detached from the device tree */
150#define OF_POPULATED 3 /* device already created for the node */
151#define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
152
Guenter Roeckb1d06b62015-11-06 19:28:22 -0800153#define OF_BAD_ADDR ((u64)-1)
154
Hans de Goede6d09dc62014-11-14 13:26:52 +0100155#ifdef CONFIG_OF
Sudeep Holla194ec932015-05-14 15:28:24 +0100156void of_core_init(void);
157
Sakari Ailusd20dc142017-05-24 17:53:55 +0300158static inline bool is_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100159{
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300160 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100161}
162
Sakari Ailusd20dc142017-05-24 17:53:55 +0300163#define to_of_node(__fwnode) \
164 ({ \
165 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
166 \
167 is_of_node(__to_of_node_fwnode) ? \
168 container_of(__to_of_node_fwnode, \
169 struct device_node, fwnode) : \
170 NULL; \
171 })
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100172
Sakari Ailusdebd3a32017-05-24 17:53:54 +0300173#define of_fwnode_handle(node) \
174 ({ \
175 typeof(node) __of_fwnode_handle_node = (node); \
176 \
177 __of_fwnode_handle_node ? \
178 &__of_fwnode_handle_node->fwnode : NULL; \
179 })
Sakari Ailus67831832017-03-28 10:52:23 +0300180
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100181static inline bool of_have_populated_dt(void)
182{
Grant Likely5063e252014-10-03 16:28:27 +0100183 return of_root != NULL;
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100184}
185
Andres Salomon035ebef2010-07-13 09:42:26 +0000186static inline bool of_node_is_root(const struct device_node *node)
187{
188 return node && (node->parent == NULL);
189}
190
Grant Likely50436312009-10-15 10:57:58 -0600191static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
192{
193 return test_bit(flag, &n->_flags);
194}
195
Pawel Mollc6e126d2014-05-15 16:55:24 +0100196static inline int of_node_test_and_set_flag(struct device_node *n,
197 unsigned long flag)
198{
199 return test_and_set_bit(flag, &n->_flags);
200}
201
Grant Likely50436312009-10-15 10:57:58 -0600202static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
203{
204 set_bit(flag, &n->_flags);
205}
206
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200207static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
208{
209 clear_bit(flag, &n->_flags);
210}
211
Rob Herring16bba302017-10-04 14:30:02 -0500212#if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200213static inline int of_property_check_flag(struct property *p, unsigned long flag)
214{
215 return test_bit(flag, &p->_flags);
216}
217
218static inline void of_property_set_flag(struct property *p, unsigned long flag)
219{
220 set_bit(flag, &p->_flags);
221}
222
223static inline void of_property_clear_flag(struct property *p, unsigned long flag)
224{
225 clear_bit(flag, &p->_flags);
226}
Rob Herring16bba302017-10-04 14:30:02 -0500227#endif
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200228
Grant Likely5063e252014-10-03 16:28:27 +0100229extern struct device_node *__of_find_all_nodes(struct device_node *prev);
Grant Likelye91edcf2009-10-15 10:58:09 -0600230extern struct device_node *of_find_all_nodes(struct device_node *prev);
231
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600232/*
Lennert Buytenhek3d6b8822011-02-22 18:18:51 +0100233 * OF address retrieval & translation
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600234 */
235
236/* Helper to read a big number; size is in cells (not bytes) */
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700237static inline u64 of_read_number(const __be32 *cell, int size)
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600238{
239 u64 r = 0;
240 while (size--)
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700241 r = (r << 32) | be32_to_cpu(*(cell++));
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600242 return r;
243}
244
245/* Like of_read_number, but we want an unsigned long result */
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700246static inline unsigned long of_read_ulong(const __be32 *cell, int size)
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600247{
Grant Likely2be09cb2009-11-23 20:16:46 -0700248 /* toss away upper bits if unsigned long is smaller than u64 */
249 return of_read_number(cell, size);
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600250}
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600251
Rob Herringb5b4bb32013-09-07 14:08:20 -0500252#if defined(CONFIG_SPARC)
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000253#include <asm/prom.h>
Rob Herringb5b4bb32013-09-07 14:08:20 -0500254#endif
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000255
Grant Likely7c7b60c2010-02-14 07:13:50 -0700256/* Default #address and #size cells. Allow arch asm/prom.h to override */
257#if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
258#define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
259#define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
260#endif
261
Grant Likely61e955d2009-10-15 10:57:51 -0600262#define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
263#define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
264
Steffen Trumtrarc0a05bf2012-12-18 11:32:03 +0100265static inline const char *of_node_full_name(const struct device_node *np)
Grant Likely74a7f082012-06-15 11:50:25 -0600266{
267 return np ? np->full_name : "<no-node>";
268}
269
Grant Likely5063e252014-10-03 16:28:27 +0100270#define for_each_of_allnodes_from(from, dn) \
271 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
272#define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000273extern struct device_node *of_find_node_by_name(struct device_node *from,
274 const char *name);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000275extern struct device_node *of_find_node_by_type(struct device_node *from,
276 const char *type);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000277extern struct device_node *of_find_compatible_node(struct device_node *from,
278 const char *type, const char *compat);
Stephen Warren50c8af42012-11-20 16:12:20 -0700279extern struct device_node *of_find_matching_node_and_match(
280 struct device_node *from,
281 const struct of_device_id *matches,
282 const struct of_device_id **match);
Rob Herring662372e2014-02-03 08:53:44 -0600283
Leif Lindholm75c28c02014-11-28 11:34:28 +0000284extern struct device_node *of_find_node_opts_by_path(const char *path,
285 const char **opts);
286static inline struct device_node *of_find_node_by_path(const char *path)
287{
288 return of_find_node_opts_by_path(path, NULL);
289}
290
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000291extern struct device_node *of_find_node_by_phandle(phandle handle);
292extern struct device_node *of_get_parent(const struct device_node *node);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000293extern struct device_node *of_get_next_parent(struct device_node *node);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000294extern struct device_node *of_get_next_child(const struct device_node *node,
295 struct device_node *prev);
Timur Tabi32961932012-08-14 13:20:23 +0000296extern struct device_node *of_get_next_available_child(
297 const struct device_node *node, struct device_node *prev);
298
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100299extern struct device_node *of_get_child_by_name(const struct device_node *node,
300 const char *name);
Bryan Wu954e04b2013-09-24 10:38:26 -0700301
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +0100302/* cache lookup */
303extern struct device_node *of_find_next_cache_node(const struct device_node *);
Sudeep Holla5fa23532017-01-16 10:40:43 +0000304extern int of_find_last_cache_level(unsigned int cpu);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000305extern struct device_node *of_find_node_with_property(
306 struct device_node *from, const char *prop_name);
Michael Ellerman1e291b142008-11-12 18:54:42 +0000307
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000308extern struct property *of_find_property(const struct device_node *np,
309 const char *name,
310 int *lenp);
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100311extern int of_property_count_elems_of_size(const struct device_node *np,
312 const char *propname, int elem_size);
Tony Prisk3daf3722013-03-23 17:02:15 +1300313extern int of_property_read_u32_index(const struct device_node *np,
314 const char *propname,
315 u32 index, u32 *out_value);
Alistair Popple2475a2b2017-04-03 19:51:42 +1000316extern int of_property_read_u64_index(const struct device_node *np,
317 const char *propname,
318 u32 index, u64 *out_value);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100319extern int of_property_read_variable_u8_array(const struct device_node *np,
320 const char *propname, u8 *out_values,
321 size_t sz_min, size_t sz_max);
322extern int of_property_read_variable_u16_array(const struct device_node *np,
323 const char *propname, u16 *out_values,
324 size_t sz_min, size_t sz_max);
325extern int of_property_read_variable_u32_array(const struct device_node *np,
326 const char *propname,
327 u32 *out_values,
328 size_t sz_min,
329 size_t sz_max);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100330extern int of_property_read_u64(const struct device_node *np,
331 const char *propname, u64 *out_value);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100332extern int of_property_read_variable_u64_array(const struct device_node *np,
333 const char *propname,
334 u64 *out_values,
335 size_t sz_min,
336 size_t sz_max);
Rob Herring0e373632011-07-06 15:42:58 -0500337
David Rivshinfe99c702016-03-02 16:35:51 -0500338extern int of_property_read_string(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100339 const char *propname,
340 const char **out_string);
David Rivshinfe99c702016-03-02 16:35:51 -0500341extern int of_property_match_string(const struct device_node *np,
Grant Likely7aff0fe2011-12-12 09:25:58 -0700342 const char *propname,
343 const char *string);
David Rivshinfe99c702016-03-02 16:35:51 -0500344extern int of_property_read_string_helper(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000345 const char *propname,
346 const char **out_strs, size_t sz, int index);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000347extern int of_device_is_compatible(const struct device_node *device,
348 const char *);
Benjamin Herrenschmidtb9c13fe2016-07-08 08:35:59 +1000349extern int of_device_compatible_match(struct device_node *device,
350 const char *const *compat);
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800351extern bool of_device_is_available(const struct device_node *device);
Kevin Cernekee37786c72015-04-09 13:05:14 -0700352extern bool of_device_is_big_endian(const struct device_node *device);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000353extern const void *of_get_property(const struct device_node *node,
354 const char *name,
355 int *lenp);
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100356extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
Dong Aisheng8af0da92011-12-22 20:19:24 +0800357#define for_each_property_of_node(dn, pp) \
358 for (pp = dn->properties; pp != NULL; pp = pp->next)
Shawn Guo611cad72011-08-15 15:28:14 +0800359
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000360extern int of_n_addr_cells(struct device_node *np);
361extern int of_n_size_cells(struct device_node *np);
Grant Likely283029d2008-01-09 06:20:40 +1100362extern const struct of_device_id *of_match_node(
363 const struct of_device_id *matches, const struct device_node *node);
Grant Likely3f07af42008-07-25 22:25:13 -0400364extern int of_modalias_node(struct device_node *node, char *modalias, int len);
Grant Likely624cfca2013-10-11 22:05:10 +0100365extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100366extern struct device_node *of_parse_phandle(const struct device_node *np,
Grant Likely739649c2009-04-25 12:52:40 +0000367 const char *phandle_name,
368 int index);
Guennadi Liakhovetski93c667c2012-12-10 20:41:30 +0100369extern int of_parse_phandle_with_args(const struct device_node *np,
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000370 const char *list_name, const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -0700371 struct of_phandle_args *out_args);
Stephen Warren035fd942013-08-14 15:27:10 -0600372extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
373 const char *list_name, int cells_count, int index,
374 struct of_phandle_args *out_args);
Grant Likelybd69f732013-02-10 22:57:21 +0000375extern int of_count_phandle_with_args(const struct device_node *np,
376 const char *list_name, const char *cells_name);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000377
Joerg Roedel74e1fbb2016-04-04 17:49:17 +0200378/* phandle iterator functions */
379extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
380 const struct device_node *np,
381 const char *list_name,
382 const char *cells_name,
383 int cell_count);
384
Joerg Roedelcd209b42016-04-04 17:49:18 +0200385extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
Joerg Roedelabdaa772016-04-04 17:49:21 +0200386extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
387 uint32_t *args,
388 int size);
Joerg Roedelcd209b42016-04-04 17:49:18 +0200389
Shawn Guo611cad72011-08-15 15:28:14 +0800390extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
391extern int of_alias_get_id(struct device_node *np, const char *stem);
Wolfram Sang351d2242015-03-12 17:17:58 +0100392extern int of_alias_get_highest_id(const char *stem);
Shawn Guo611cad72011-08-15 15:28:14 +0800393
Grant Likely21b082e2010-02-14 07:13:38 -0700394extern int of_machine_is_compatible(const char *compat);
395
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000396extern int of_add_property(struct device_node *np, struct property *prop);
397extern int of_remove_property(struct device_node *np, struct property *prop);
398extern int of_update_property(struct device_node *np, struct property *newprop);
Grant Likely21b082e2010-02-14 07:13:38 -0700399
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700400/* For updating the device tree at runtime */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000401#define OF_RECONFIG_ATTACH_NODE 0x0001
402#define OF_RECONFIG_DETACH_NODE 0x0002
403#define OF_RECONFIG_ADD_PROPERTY 0x0003
404#define OF_RECONFIG_REMOVE_PROPERTY 0x0004
405#define OF_RECONFIG_UPDATE_PROPERTY 0x0005
406
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000407extern int of_attach_node(struct device_node *);
408extern int of_detach_node(struct device_node *);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700409
Ben Dooks3a1e3622011-08-03 10:11:42 +0100410#define of_match_ptr(_ptr) (_ptr)
Stephen Warrenc541adc2012-04-04 09:27:46 -0600411
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100412/**
413 * of_property_read_u8_array - Find and read an array of u8 from a property.
414 *
415 * @np: device node from which the property value is to be read.
416 * @propname: name of the property to be searched.
417 * @out_values: pointer to return value, modified only if return value is 0.
418 * @sz: number of array elements to read
419 *
420 * Search for a property in a device node and read 8-bit value(s) from
421 * it. Returns 0 on success, -EINVAL if the property does not exist,
422 * -ENODATA if property does not have a value, and -EOVERFLOW if the
423 * property data isn't large enough.
424 *
425 * dts entry of array should be like:
426 * property = /bits/ 8 <0x50 0x60 0x70>;
427 *
428 * The out_values is modified only if a valid u8 value can be decoded.
429 */
430static inline int of_property_read_u8_array(const struct device_node *np,
431 const char *propname,
432 u8 *out_values, size_t sz)
433{
434 int ret = of_property_read_variable_u8_array(np, propname, out_values,
435 sz, 0);
436 if (ret >= 0)
437 return 0;
438 else
439 return ret;
440}
441
442/**
443 * of_property_read_u16_array - Find and read an array of u16 from a property.
444 *
445 * @np: device node from which the property value is to be read.
446 * @propname: name of the property to be searched.
447 * @out_values: pointer to return value, modified only if return value is 0.
448 * @sz: number of array elements to read
449 *
450 * Search for a property in a device node and read 16-bit value(s) from
451 * it. Returns 0 on success, -EINVAL if the property does not exist,
452 * -ENODATA if property does not have a value, and -EOVERFLOW if the
453 * property data isn't large enough.
454 *
455 * dts entry of array should be like:
456 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
457 *
458 * The out_values is modified only if a valid u16 value can be decoded.
459 */
460static inline int of_property_read_u16_array(const struct device_node *np,
461 const char *propname,
462 u16 *out_values, size_t sz)
463{
464 int ret = of_property_read_variable_u16_array(np, propname, out_values,
465 sz, 0);
466 if (ret >= 0)
467 return 0;
468 else
469 return ret;
470}
471
472/**
473 * of_property_read_u32_array - Find and read an array of 32 bit integers
474 * from a property.
475 *
476 * @np: device node from which the property value is to be read.
477 * @propname: name of the property to be searched.
478 * @out_values: pointer to return value, modified only if return value is 0.
479 * @sz: number of array elements to read
480 *
481 * Search for a property in a device node and read 32-bit value(s) from
482 * it. Returns 0 on success, -EINVAL if the property does not exist,
483 * -ENODATA if property does not have a value, and -EOVERFLOW if the
484 * property data isn't large enough.
485 *
486 * The out_values is modified only if a valid u32 value can be decoded.
487 */
488static inline int of_property_read_u32_array(const struct device_node *np,
489 const char *propname,
490 u32 *out_values, size_t sz)
491{
492 int ret = of_property_read_variable_u32_array(np, propname, out_values,
493 sz, 0);
494 if (ret >= 0)
495 return 0;
496 else
497 return ret;
498}
499
500/**
501 * of_property_read_u64_array - Find and read an array of 64 bit integers
502 * from a property.
503 *
504 * @np: device node from which the property value is to be read.
505 * @propname: name of the property to be searched.
506 * @out_values: pointer to return value, modified only if return value is 0.
507 * @sz: number of array elements to read
508 *
509 * Search for a property in a device node and read 64-bit value(s) from
510 * it. Returns 0 on success, -EINVAL if the property does not exist,
511 * -ENODATA if property does not have a value, and -EOVERFLOW if the
512 * property data isn't large enough.
513 *
514 * The out_values is modified only if a valid u64 value can be decoded.
515 */
516static inline int of_property_read_u64_array(const struct device_node *np,
517 const char *propname,
518 u64 *out_values, size_t sz)
519{
520 int ret = of_property_read_variable_u64_array(np, propname, out_values,
521 sz, 0);
522 if (ret >= 0)
523 return 0;
524 else
525 return ret;
526}
527
Stephen Warrenc541adc2012-04-04 09:27:46 -0600528/*
529 * struct property *prop;
530 * const __be32 *p;
531 * u32 u;
532 *
533 * of_property_for_each_u32(np, "propname", prop, p, u)
534 * printk("U32 value: %x\n", u);
535 */
536const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
537 u32 *pu);
Stephen Warrenc541adc2012-04-04 09:27:46 -0600538/*
539 * struct property *prop;
540 * const char *s;
541 *
542 * of_property_for_each_string(np, "propname", prop, s)
543 * printk("String value: %s\n", s);
544 */
545const char *of_prop_next_string(struct property *prop, const char *cur);
Stephen Warrenc541adc2012-04-04 09:27:46 -0600546
Grant Likely3482f2c2014-03-27 17:18:55 -0700547bool of_console_check(struct device_node *dn, char *name, int index);
Sascha Hauer5c19e952013-08-05 14:40:44 +0200548
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
Tony Prisk3daf3722013-03-23 17:02:15 +1300686static inline int of_property_read_u32_index(const struct device_node *np,
687 const char *propname, u32 index, u32 *out_value)
688{
689 return -ENOSYS;
690}
691
Viresh Kumarbe193242012-11-20 10:15:19 +0530692static inline int of_property_read_u8_array(const struct device_node *np,
693 const char *propname, u8 *out_values, size_t sz)
694{
695 return -ENOSYS;
696}
697
698static inline int of_property_read_u16_array(const struct device_node *np,
699 const char *propname, u16 *out_values, size_t sz)
700{
701 return -ENOSYS;
702}
703
Shawn Guob98c0232011-07-08 16:27:33 +0800704static inline int of_property_read_u32_array(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100705 const char *propname,
706 u32 *out_values, size_t sz)
Shawn Guob98c0232011-07-08 16:27:33 +0800707{
708 return -ENOSYS;
709}
710
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100711static inline int of_property_read_u64_array(const struct device_node *np,
712 const char *propname,
713 u64 *out_values, size_t sz)
714{
715 return -ENOSYS;
716}
717
David Rivshinfe99c702016-03-02 16:35:51 -0500718static inline int of_property_read_string(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100719 const char *propname,
720 const char **out_string)
Shawn Guob98c0232011-07-08 16:27:33 +0800721{
722 return -ENOSYS;
723}
724
David Rivshinfe99c702016-03-02 16:35:51 -0500725static inline int of_property_read_string_helper(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000726 const char *propname,
727 const char **out_strs, size_t sz, int index)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200728{
729 return -ENOSYS;
730}
731
Stephen Warren89272b82011-08-05 16:50:30 -0600732static inline const void *of_get_property(const struct device_node *node,
733 const char *name,
734 int *lenp)
735{
736 return NULL;
737}
738
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100739static inline struct device_node *of_get_cpu_node(int cpu,
740 unsigned int *thread)
741{
742 return NULL;
743}
744
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100745static inline int of_property_read_u64(const struct device_node *np,
746 const char *propname, u64 *out_value)
747{
748 return -ENOSYS;
749}
750
David Rivshinfe99c702016-03-02 16:35:51 -0500751static inline int of_property_match_string(const struct device_node *np,
Thierry Redingbd3d5502012-04-13 16:18:34 +0200752 const char *propname,
753 const char *string)
754{
755 return -ENOSYS;
756}
757
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100758static inline struct device_node *of_parse_phandle(const struct device_node *np,
Rajendra Nayak36a09042011-10-10 21:49:35 +0530759 const char *phandle_name,
760 int index)
761{
762 return NULL;
763}
764
Kuninori Morimotoe93aeea2016-05-25 01:15:04 +0000765static inline int of_parse_phandle_with_args(const struct device_node *np,
Thierry Redinge05e5072012-04-13 16:19:21 +0200766 const char *list_name,
767 const char *cells_name,
768 int index,
769 struct of_phandle_args *out_args)
770{
771 return -ENOSYS;
772}
773
Stephen Warren035fd942013-08-14 15:27:10 -0600774static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
775 const char *list_name, int cells_count, int index,
776 struct of_phandle_args *out_args)
777{
778 return -ENOSYS;
779}
780
Grant Likelybd69f732013-02-10 22:57:21 +0000781static inline int of_count_phandle_with_args(struct device_node *np,
782 const char *list_name,
783 const char *cells_name)
784{
785 return -ENOSYS;
786}
787
Joerg Roedel74e1fbb2016-04-04 17:49:17 +0200788static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
789 const struct device_node *np,
790 const char *list_name,
791 const char *cells_name,
792 int cell_count)
793{
794 return -ENOSYS;
795}
796
Joerg Roedelcd209b42016-04-04 17:49:18 +0200797static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
798{
799 return -ENOSYS;
800}
801
Joerg Roedelabdaa772016-04-04 17:49:21 +0200802static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
803 uint32_t *args,
804 int size)
805{
806 return 0;
807}
808
Nicolas Ferreed5f8862011-10-27 11:07:28 +0200809static inline int of_alias_get_id(struct device_node *np, const char *stem)
810{
811 return -ENOSYS;
812}
813
Wolfram Sang351d2242015-03-12 17:17:58 +0100814static inline int of_alias_get_highest_id(const char *stem)
815{
816 return -ENOSYS;
817}
818
Stephen Warren50e07f82011-10-25 14:01:26 +0200819static inline int of_machine_is_compatible(const char *compat)
820{
821 return 0;
822}
823
Grant Likely3482f2c2014-03-27 17:18:55 -0700824static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +0200825{
Grant Likely3482f2c2014-03-27 17:18:55 -0700826 return false;
Sascha Hauer5c19e952013-08-05 14:40:44 +0200827}
828
Sebastian Andrzej Siewior2adfffa2013-06-17 16:48:13 +0200829static inline const __be32 *of_prop_next_u32(struct property *prop,
830 const __be32 *cur, u32 *pu)
831{
832 return NULL;
833}
834
835static inline const char *of_prop_next_string(struct property *prop,
836 const char *cur)
837{
838 return NULL;
839}
840
Pantelis Antoniou0384e8c2015-01-21 19:05:57 +0200841static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
842{
843 return 0;
844}
845
846static inline int of_node_test_and_set_flag(struct device_node *n,
847 unsigned long flag)
848{
849 return 0;
850}
851
852static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
853{
854}
855
856static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
857{
858}
859
860static inline int of_property_check_flag(struct property *p, unsigned long flag)
861{
862 return 0;
863}
864
865static inline void of_property_set_flag(struct property *p, unsigned long flag)
866{
867}
868
869static inline void of_property_clear_flag(struct property *p, unsigned long flag)
870{
871}
872
Ben Dooks3a1e3622011-08-03 10:11:42 +0100873#define of_match_ptr(_ptr) NULL
Nicolas Ferre5762c202011-10-24 11:53:32 +0200874#define of_match_node(_matches, _node) NULL
Jeremy Kerr9dfbf202010-02-14 07:13:43 -0700875#endif /* CONFIG_OF */
Shawn Guob98c0232011-07-08 16:27:33 +0800876
Adam Thomson613e9722016-06-21 18:50:20 +0100877/* Default string compare functions, Allow arch asm/prom.h to override */
878#if !defined(of_compat_cmp)
879#define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
880#define of_prop_cmp(s1, s2) strcmp((s1), (s2))
881#define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
882#endif
883
Rob Herring0c3f0612013-09-17 10:42:50 -0500884#if defined(CONFIG_OF) && defined(CONFIG_NUMA)
885extern int of_node_to_nid(struct device_node *np);
886#else
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +0300887static inline int of_node_to_nid(struct device_node *device)
888{
889 return NUMA_NO_NODE;
890}
Paul Mundt5ca4db62012-06-03 22:04:34 -0700891#endif
892
David Daney298535c2016-04-08 15:50:25 -0700893#ifdef CONFIG_OF_NUMA
894extern int of_numa_init(void);
895#else
896static inline int of_numa_init(void)
897{
898 return -ENOSYS;
899}
900#endif
901
Rob Herring662372e2014-02-03 08:53:44 -0600902static inline struct device_node *of_find_matching_node(
903 struct device_node *from,
904 const struct of_device_id *matches)
905{
906 return of_find_matching_node_and_match(from, matches, NULL);
907}
908
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +0800909/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100910 * of_property_count_u8_elems - Count the number of u8 elements in a property
911 *
912 * @np: device node from which the property value is to be read.
913 * @propname: name of the property to be searched.
914 *
915 * Search for a property in a device node and count the number of u8 elements
916 * in it. Returns number of elements on sucess, -EINVAL if the property does
917 * not exist or its length does not match a multiple of u8 and -ENODATA if the
918 * property does not have a value.
919 */
920static inline int of_property_count_u8_elems(const struct device_node *np,
921 const char *propname)
922{
923 return of_property_count_elems_of_size(np, propname, sizeof(u8));
924}
925
926/**
927 * of_property_count_u16_elems - Count the number of u16 elements in a property
928 *
929 * @np: device node from which the property value is to be read.
930 * @propname: name of the property to be searched.
931 *
932 * Search for a property in a device node and count the number of u16 elements
933 * in it. Returns number of elements on sucess, -EINVAL if the property does
934 * not exist or its length does not match a multiple of u16 and -ENODATA if the
935 * property does not have a value.
936 */
937static inline int of_property_count_u16_elems(const struct device_node *np,
938 const char *propname)
939{
940 return of_property_count_elems_of_size(np, propname, sizeof(u16));
941}
942
943/**
944 * of_property_count_u32_elems - Count the number of u32 elements in a property
945 *
946 * @np: device node from which the property value is to be read.
947 * @propname: name of the property to be searched.
948 *
949 * Search for a property in a device node and count the number of u32 elements
950 * in it. Returns number of elements on sucess, -EINVAL if the property does
951 * not exist or its length does not match a multiple of u32 and -ENODATA if the
952 * property does not have a value.
953 */
954static inline int of_property_count_u32_elems(const struct device_node *np,
955 const char *propname)
956{
957 return of_property_count_elems_of_size(np, propname, sizeof(u32));
958}
959
960/**
961 * of_property_count_u64_elems - Count the number of u64 elements in a property
962 *
963 * @np: device node from which the property value is to be read.
964 * @propname: name of the property to be searched.
965 *
966 * Search for a property in a device node and count the number of u64 elements
967 * in it. Returns number of elements on sucess, -EINVAL if the property does
968 * not exist or its length does not match a multiple of u64 and -ENODATA if the
969 * property does not have a value.
970 */
971static inline int of_property_count_u64_elems(const struct device_node *np,
972 const char *propname)
973{
974 return of_property_count_elems_of_size(np, propname, sizeof(u64));
975}
976
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +0800977/**
Grant Likelya87fa1d2014-11-03 15:15:35 +0000978 * of_property_read_string_array() - Read an array of strings from a multiple
979 * strings property.
980 * @np: device node from which the property value is to be read.
981 * @propname: name of the property to be searched.
982 * @out_strs: output array of string pointers.
983 * @sz: number of array elements to read.
984 *
985 * Search for a property in a device tree node and retrieve a list of
986 * terminated string values (pointer to data, not a copy) in that property.
987 *
988 * If @out_strs is NULL, the number of strings in the property is returned.
989 */
David Rivshinfe99c702016-03-02 16:35:51 -0500990static inline int of_property_read_string_array(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000991 const char *propname, const char **out_strs,
992 size_t sz)
993{
994 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
995}
996
997/**
998 * of_property_count_strings() - Find and return the number of strings from a
999 * multiple strings property.
1000 * @np: device node from which the property value is to be read.
1001 * @propname: name of the property to be searched.
1002 *
1003 * Search for a property in a device tree node and retrieve the number of null
1004 * terminated string contain in it. Returns the number of strings on
1005 * success, -EINVAL if the property does not exist, -ENODATA if property
1006 * does not have a value, and -EILSEQ if the string is not null-terminated
1007 * within the length of the property data.
1008 */
David Rivshinfe99c702016-03-02 16:35:51 -05001009static inline int of_property_count_strings(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +00001010 const char *propname)
1011{
1012 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1013}
1014
1015/**
1016 * of_property_read_string_index() - Find and read a string from a multiple
1017 * strings property.
1018 * @np: device node from which the property value is to be read.
1019 * @propname: name of the property to be searched.
1020 * @index: index of the string in the list of strings
1021 * @out_string: pointer to null terminated return string, modified only if
1022 * return value is 0.
1023 *
1024 * Search for a property in a device tree node and retrieve a null
1025 * terminated string value (pointer to data, not a copy) in the list of strings
1026 * contained in that property.
1027 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1028 * property does not have a value, and -EILSEQ if the string is not
1029 * null-terminated within the length of the property data.
1030 *
1031 * The out_string pointer is modified only if a valid string can be decoded.
1032 */
David Rivshinfe99c702016-03-02 16:35:51 -05001033static inline int of_property_read_string_index(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +00001034 const char *propname,
1035 int index, const char **output)
1036{
1037 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1038 return rc < 0 ? rc : 0;
1039}
1040
1041/**
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001042 * of_property_read_bool - Findfrom a property
1043 * @np: device node from which the property value is to be read.
1044 * @propname: name of the property to be searched.
1045 *
1046 * Search for a property in a device node.
Geert Uytterhoeven31712c92015-05-04 19:42:19 +02001047 * Returns true if the property exists false otherwise.
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001048 */
1049static inline bool of_property_read_bool(const struct device_node *np,
1050 const char *propname)
1051{
1052 struct property *prop = of_find_property(np, propname, NULL);
1053
1054 return prop ? true : false;
1055}
1056
Viresh Kumarbe193242012-11-20 10:15:19 +05301057static inline int of_property_read_u8(const struct device_node *np,
1058 const char *propname,
1059 u8 *out_value)
1060{
1061 return of_property_read_u8_array(np, propname, out_value, 1);
1062}
1063
1064static inline int of_property_read_u16(const struct device_node *np,
1065 const char *propname,
1066 u16 *out_value)
1067{
1068 return of_property_read_u16_array(np, propname, out_value, 1);
1069}
1070
Shawn Guob98c0232011-07-08 16:27:33 +08001071static inline int of_property_read_u32(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +01001072 const char *propname,
Shawn Guob98c0232011-07-08 16:27:33 +08001073 u32 *out_value)
1074{
1075 return of_property_read_u32_array(np, propname, out_value, 1);
1076}
1077
Sebastian Reichele7a00e42014-04-06 12:52:11 +02001078static inline int of_property_read_s32(const struct device_node *np,
1079 const char *propname,
1080 s32 *out_value)
1081{
1082 return of_property_read_u32(np, propname, (u32*) out_value);
1083}
1084
Joerg Roedelf623ce92016-04-04 17:49:20 +02001085#define of_for_each_phandle(it, err, np, ln, cn, cc) \
1086 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1087 err = of_phandle_iterator_next(it); \
1088 err == 0; \
1089 err = of_phandle_iterator_next(it))
1090
Sebastian Andrzej Siewior2adfffa2013-06-17 16:48:13 +02001091#define of_property_for_each_u32(np, propname, prop, p, u) \
1092 for (prop = of_find_property(np, propname, NULL), \
1093 p = of_prop_next_u32(prop, NULL, &u); \
1094 p; \
1095 p = of_prop_next_u32(prop, p, &u))
1096
1097#define of_property_for_each_string(np, propname, prop, s) \
1098 for (prop = of_find_property(np, propname, NULL), \
1099 s = of_prop_next_string(prop, NULL); \
1100 s; \
1101 s = of_prop_next_string(prop, s))
1102
Rob Herring662372e2014-02-03 08:53:44 -06001103#define for_each_node_by_name(dn, name) \
1104 for (dn = of_find_node_by_name(NULL, name); dn; \
1105 dn = of_find_node_by_name(dn, name))
1106#define for_each_node_by_type(dn, type) \
1107 for (dn = of_find_node_by_type(NULL, type); dn; \
1108 dn = of_find_node_by_type(dn, type))
1109#define for_each_compatible_node(dn, type, compatible) \
1110 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1111 dn = of_find_compatible_node(dn, type, compatible))
1112#define for_each_matching_node(dn, matches) \
1113 for (dn = of_find_matching_node(NULL, matches); dn; \
1114 dn = of_find_matching_node(dn, matches))
1115#define for_each_matching_node_and_match(dn, matches, match) \
1116 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1117 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1118
1119#define for_each_child_of_node(parent, child) \
1120 for (child = of_get_next_child(parent, NULL); child != NULL; \
1121 child = of_get_next_child(parent, child))
1122#define for_each_available_child_of_node(parent, child) \
1123 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1124 child = of_get_next_available_child(parent, child))
1125
1126#define for_each_node_with_property(dn, prop_name) \
1127 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1128 dn = of_find_node_with_property(dn, prop_name))
1129
1130static inline int of_get_child_count(const struct device_node *np)
1131{
1132 struct device_node *child;
1133 int num = 0;
1134
1135 for_each_child_of_node(np, child)
1136 num++;
1137
1138 return num;
1139}
1140
1141static inline int of_get_available_child_count(const struct device_node *np)
1142{
1143 struct device_node *child;
1144 int num = 0;
1145
1146 for_each_available_child_of_node(np, child)
1147 num++;
1148
1149 return num;
1150}
1151
Masahiro Yamada71f50c62016-01-22 01:33:51 +09001152#if defined(CONFIG_OF) && !defined(MODULE)
Rob Herring54196cc2014-05-08 16:09:24 -05001153#define _OF_DECLARE(table, name, compat, fn, fn_type) \
1154 static const struct of_device_id __of_table_##name \
1155 __used __section(__##table##_of_table) \
1156 = { .compatible = compat, \
1157 .data = (fn == (fn_type)NULL) ? fn : fn }
1158#else
Thierry Reding5f563582014-10-02 16:01:10 +02001159#define _OF_DECLARE(table, name, compat, fn, fn_type) \
Rob Herring54196cc2014-05-08 16:09:24 -05001160 static const struct of_device_id __of_table_##name \
1161 __attribute__((unused)) \
1162 = { .compatible = compat, \
1163 .data = (fn == (fn_type)NULL) ? fn : fn }
1164#endif
1165
1166typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
Daniel Lezcanoc35d9292016-04-18 23:06:48 +02001167typedef int (*of_init_fn_1_ret)(struct device_node *);
Rob Herring54196cc2014-05-08 16:09:24 -05001168typedef void (*of_init_fn_1)(struct device_node *);
1169
1170#define OF_DECLARE_1(table, name, compat, fn) \
1171 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
Daniel Lezcanoc35d9292016-04-18 23:06:48 +02001172#define OF_DECLARE_1_RET(table, name, compat, fn) \
1173 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
Rob Herring54196cc2014-05-08 16:09:24 -05001174#define OF_DECLARE_2(table, name, compat, fn) \
1175 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1176
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001177/**
1178 * struct of_changeset_entry - Holds a changeset entry
1179 *
1180 * @node: list_head for the log list
1181 * @action: notifier action
1182 * @np: pointer to the device node affected
1183 * @prop: pointer to the property affected
1184 * @old_prop: hold a pointer to the original property
1185 *
1186 * Every modification of the device tree during a changeset
1187 * is held in a list of of_changeset_entry structures.
1188 * That way we can recover from a partial application, or we can
1189 * revert the changeset
1190 */
1191struct of_changeset_entry {
1192 struct list_head node;
1193 unsigned long action;
1194 struct device_node *np;
1195 struct property *prop;
1196 struct property *old_prop;
1197};
1198
1199/**
1200 * struct of_changeset - changeset tracker structure
1201 *
1202 * @entries: list_head for the changeset entries
1203 *
1204 * changesets are a convenient way to apply bulk changes to the
1205 * live tree. In case of an error, changes are rolled-back.
1206 * changesets live on after initial application, and if not
1207 * destroyed after use, they can be reverted in one single call.
1208 */
1209struct of_changeset {
1210 struct list_head entries;
1211};
1212
Pantelis Antonioub53a2342014-10-28 22:33:53 +02001213enum of_reconfig_change {
1214 OF_RECONFIG_NO_CHANGE = 0,
1215 OF_RECONFIG_CHANGE_ADD,
1216 OF_RECONFIG_CHANGE_REMOVE,
1217};
1218
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001219#ifdef CONFIG_OF_DYNAMIC
Grant Likelyf6892d12014-11-21 15:14:58 +00001220extern int of_reconfig_notifier_register(struct notifier_block *);
1221extern int of_reconfig_notifier_unregister(struct notifier_block *);
Grant Likelyf5242e52014-11-24 17:58:01 +00001222extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1223extern int of_reconfig_get_state_change(unsigned long action,
1224 struct of_reconfig_data *arg);
Grant Likelyf6892d12014-11-21 15:14:58 +00001225
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001226extern void of_changeset_init(struct of_changeset *ocs);
1227extern void of_changeset_destroy(struct of_changeset *ocs);
1228extern int of_changeset_apply(struct of_changeset *ocs);
1229extern int of_changeset_revert(struct of_changeset *ocs);
1230extern int of_changeset_action(struct of_changeset *ocs,
1231 unsigned long action, struct device_node *np,
1232 struct property *prop);
1233
1234static inline int of_changeset_attach_node(struct of_changeset *ocs,
1235 struct device_node *np)
1236{
1237 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1238}
1239
1240static inline int of_changeset_detach_node(struct of_changeset *ocs,
1241 struct device_node *np)
1242{
1243 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1244}
1245
1246static inline int of_changeset_add_property(struct of_changeset *ocs,
1247 struct device_node *np, struct property *prop)
1248{
1249 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1250}
1251
1252static inline int of_changeset_remove_property(struct of_changeset *ocs,
1253 struct device_node *np, struct property *prop)
1254{
1255 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1256}
1257
1258static inline int of_changeset_update_property(struct of_changeset *ocs,
1259 struct device_node *np, struct property *prop)
1260{
1261 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1262}
Grant Likelyf6892d12014-11-21 15:14:58 +00001263#else /* CONFIG_OF_DYNAMIC */
1264static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1265{
1266 return -EINVAL;
1267}
1268static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1269{
1270 return -EINVAL;
1271}
Grant Likelyf5242e52014-11-24 17:58:01 +00001272static inline int of_reconfig_notify(unsigned long action,
1273 struct of_reconfig_data *arg)
Grant Likelyf6892d12014-11-21 15:14:58 +00001274{
1275 return -EINVAL;
1276}
Grant Likelyf5242e52014-11-24 17:58:01 +00001277static inline int of_reconfig_get_state_change(unsigned long action,
1278 struct of_reconfig_data *arg)
Grant Likelyf6892d12014-11-21 15:14:58 +00001279{
1280 return -EINVAL;
1281}
1282#endif /* CONFIG_OF_DYNAMIC */
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001283
Pantelis Antoniou7941b272014-07-04 19:59:20 +03001284/* CONFIG_OF_RESOLVE api */
1285extern int of_resolve_phandles(struct device_node *tree);
1286
Romain Periera4b4e042014-10-14 06:31:09 +00001287/**
Romain Perier8f731102014-11-25 12:28:25 +00001288 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
Romain Periera4b4e042014-10-14 06:31:09 +00001289 * @np: Pointer to the given device_node
1290 *
1291 * return true if present false otherwise
1292 */
Romain Perier8f731102014-11-25 12:28:25 +00001293static inline bool of_device_is_system_power_controller(const struct device_node *np)
Romain Periera4b4e042014-10-14 06:31:09 +00001294{
Romain Perier8f731102014-11-25 12:28:25 +00001295 return of_property_read_bool(np, "system-power-controller");
Romain Periera4b4e042014-10-14 06:31:09 +00001296}
1297
Linus Torvalds7ef58b32014-12-11 13:06:58 -08001298/**
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001299 * Overlay support
1300 */
1301
Alan Tull39a842e2016-11-01 14:14:22 -05001302enum of_overlay_notify_action {
1303 OF_OVERLAY_PRE_APPLY,
1304 OF_OVERLAY_POST_APPLY,
1305 OF_OVERLAY_PRE_REMOVE,
1306 OF_OVERLAY_POST_REMOVE,
1307};
1308
1309struct of_overlay_notify_data {
1310 struct device_node *overlay;
1311 struct device_node *target;
1312};
1313
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001314#ifdef CONFIG_OF_OVERLAY
1315
1316/* ID based overlays; the API for external users */
1317int of_overlay_create(struct device_node *tree);
1318int of_overlay_destroy(int id);
1319int of_overlay_destroy_all(void);
1320
Alan Tull39a842e2016-11-01 14:14:22 -05001321int of_overlay_notifier_register(struct notifier_block *nb);
1322int of_overlay_notifier_unregister(struct notifier_block *nb);
1323
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001324#else
1325
1326static inline int of_overlay_create(struct device_node *tree)
1327{
1328 return -ENOTSUPP;
1329}
1330
1331static inline int of_overlay_destroy(int id)
1332{
1333 return -ENOTSUPP;
1334}
1335
1336static inline int of_overlay_destroy_all(void)
1337{
1338 return -ENOTSUPP;
1339}
1340
Alan Tull39a842e2016-11-01 14:14:22 -05001341static inline int of_overlay_notifier_register(struct notifier_block *nb)
1342{
1343 return 0;
1344}
1345
1346static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1347{
1348 return 0;
1349}
1350
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001351#endif
1352
Stephen Rothwell76c1ce72007-05-01 16:19:07 +10001353#endif /* _LINUX_OF_H */