blob: 7eb94b7fbcf38a21e1be99346881d5110979729c [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
114/* true when node is initialized */
115static inline int of_node_is_initialized(struct device_node *node)
116{
117 return node && node->kobj.state_initialized;
118}
119
120/* true when node is attached (i.e. present on sysfs) */
121static inline int of_node_is_attached(struct device_node *node)
122{
123 return node && node->kobj.state_in_sysfs;
124}
125
Grant Likely0f22dd32012-02-15 20:38:40 -0700126#ifdef CONFIG_OF_DYNAMIC
127extern struct device_node *of_node_get(struct device_node *node);
128extern void of_node_put(struct device_node *node);
129#else /* CONFIG_OF_DYNAMIC */
Rob Herring3ecdd052011-12-13 09:13:54 -0600130/* Dummy ref counting routines - to be implemented later */
131static inline struct device_node *of_node_get(struct device_node *node)
132{
133 return node;
134}
Grant Likely0f22dd32012-02-15 20:38:40 -0700135static inline void of_node_put(struct device_node *node) { }
136#endif /* !CONFIG_OF_DYNAMIC */
Rob Herring3ecdd052011-12-13 09:13:54 -0600137
Grant Likely41f88002009-11-23 20:07:01 -0700138/* Pointer for first entry in chain of all nodes. */
Grant Likely5063e252014-10-03 16:28:27 +0100139extern struct device_node *of_root;
Grant Likelyfc0bdae2010-02-14 07:13:55 -0700140extern struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +0800141extern struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -0700142extern struct device_node *of_stdout;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500143extern raw_spinlock_t devtree_lock;
Grant Likely41f88002009-11-23 20:07:01 -0700144
Pantelis Antoniou391b4592015-04-24 12:41:56 +0300145/* flag descriptions (need to be visible even when !CONFIG_OF) */
146#define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
147#define OF_DETACHED 2 /* node has been detached from the device tree */
148#define OF_POPULATED 3 /* device already created for the node */
149#define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
150
Guenter Roeckb1d06b62015-11-06 19:28:22 -0800151#define OF_BAD_ADDR ((u64)-1)
152
Hans de Goede6d09dc62014-11-14 13:26:52 +0100153#ifdef CONFIG_OF
Sudeep Holla194ec9362015-05-14 15:28:24 +0100154void of_core_init(void);
155
Sakari Ailusd20dc142017-05-24 17:53:55 +0300156static inline bool is_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100157{
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300158 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100159}
160
Sakari Ailusd20dc142017-05-24 17:53:55 +0300161#define to_of_node(__fwnode) \
162 ({ \
163 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
164 \
165 is_of_node(__to_of_node_fwnode) ? \
166 container_of(__to_of_node_fwnode, \
167 struct device_node, fwnode) : \
168 NULL; \
169 })
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100170
Sakari Ailusdebd3a32017-05-24 17:53:54 +0300171#define of_fwnode_handle(node) \
172 ({ \
173 typeof(node) __of_fwnode_handle_node = (node); \
174 \
175 __of_fwnode_handle_node ? \
176 &__of_fwnode_handle_node->fwnode : NULL; \
177 })
Sakari Ailus67831832017-03-28 10:52:23 +0300178
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100179static inline bool of_have_populated_dt(void)
180{
Grant Likely5063e252014-10-03 16:28:27 +0100181 return of_root != NULL;
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100182}
183
Andres Salomon035ebef2010-07-13 09:42:26 +0000184static inline bool of_node_is_root(const struct device_node *node)
185{
186 return node && (node->parent == NULL);
187}
188
Grant Likely50436312009-10-15 10:57:58 -0600189static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
190{
191 return test_bit(flag, &n->_flags);
192}
193
Pawel Mollc6e126d2014-05-15 16:55:24 +0100194static inline int of_node_test_and_set_flag(struct device_node *n,
195 unsigned long flag)
196{
197 return test_and_set_bit(flag, &n->_flags);
198}
199
Grant Likely50436312009-10-15 10:57:58 -0600200static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
201{
202 set_bit(flag, &n->_flags);
203}
204
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200205static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
206{
207 clear_bit(flag, &n->_flags);
208}
209
Rob Herring16bba302017-10-04 14:30:02 -0500210#if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200211static inline int of_property_check_flag(struct property *p, unsigned long flag)
212{
213 return test_bit(flag, &p->_flags);
214}
215
216static inline void of_property_set_flag(struct property *p, unsigned long flag)
217{
218 set_bit(flag, &p->_flags);
219}
220
221static inline void of_property_clear_flag(struct property *p, unsigned long flag)
222{
223 clear_bit(flag, &p->_flags);
224}
Rob Herring16bba302017-10-04 14:30:02 -0500225#endif
Pantelis Antoniou588453c2013-11-08 17:03:56 +0200226
Grant Likely5063e252014-10-03 16:28:27 +0100227extern struct device_node *__of_find_all_nodes(struct device_node *prev);
Grant Likelye91edcf2009-10-15 10:58:09 -0600228extern struct device_node *of_find_all_nodes(struct device_node *prev);
229
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600230/*
Lennert Buytenhek3d6b8822011-02-22 18:18:51 +0100231 * OF address retrieval & translation
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600232 */
233
234/* Helper to read a big number; size is in cells (not bytes) */
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700235static inline u64 of_read_number(const __be32 *cell, int size)
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600236{
237 u64 r = 0;
238 while (size--)
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700239 r = (r << 32) | be32_to_cpu(*(cell++));
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600240 return r;
241}
242
243/* Like of_read_number, but we want an unsigned long result */
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700244static inline unsigned long of_read_ulong(const __be32 *cell, int size)
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600245{
Grant Likely2be09cb2009-11-23 20:16:46 -0700246 /* toss away upper bits if unsigned long is smaller than u64 */
247 return of_read_number(cell, size);
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600248}
Grant Likelyb6caf2a2009-10-15 10:58:00 -0600249
Rob Herringb5b4bb32013-09-07 14:08:20 -0500250#if defined(CONFIG_SPARC)
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000251#include <asm/prom.h>
Rob Herringb5b4bb32013-09-07 14:08:20 -0500252#endif
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000253
Grant Likely7c7b60c2010-02-14 07:13:50 -0700254/* Default #address and #size cells. Allow arch asm/prom.h to override */
255#if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
256#define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
257#define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
258#endif
259
Grant Likely61e955d2009-10-15 10:57:51 -0600260#define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
261#define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
262
Steffen Trumtrarc0a05bf2012-12-18 11:32:03 +0100263static inline const char *of_node_full_name(const struct device_node *np)
Grant Likely74a7f082012-06-15 11:50:25 -0600264{
265 return np ? np->full_name : "<no-node>";
266}
267
Grant Likely5063e252014-10-03 16:28:27 +0100268#define for_each_of_allnodes_from(from, dn) \
269 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
270#define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000271extern struct device_node *of_find_node_by_name(struct device_node *from,
272 const char *name);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000273extern struct device_node *of_find_node_by_type(struct device_node *from,
274 const char *type);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000275extern struct device_node *of_find_compatible_node(struct device_node *from,
276 const char *type, const char *compat);
Stephen Warren50c8af42012-11-20 16:12:20 -0700277extern struct device_node *of_find_matching_node_and_match(
278 struct device_node *from,
279 const struct of_device_id *matches,
280 const struct of_device_id **match);
Rob Herring662372e2014-02-03 08:53:44 -0600281
Leif Lindholm75c28c02014-11-28 11:34:28 +0000282extern struct device_node *of_find_node_opts_by_path(const char *path,
283 const char **opts);
284static inline struct device_node *of_find_node_by_path(const char *path)
285{
286 return of_find_node_opts_by_path(path, NULL);
287}
288
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000289extern struct device_node *of_find_node_by_phandle(phandle handle);
290extern struct device_node *of_get_parent(const struct device_node *node);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000291extern struct device_node *of_get_next_parent(struct device_node *node);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000292extern struct device_node *of_get_next_child(const struct device_node *node,
293 struct device_node *prev);
Timur Tabi32961932012-08-14 13:20:23 +0000294extern struct device_node *of_get_next_available_child(
295 const struct device_node *node, struct device_node *prev);
296
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100297extern struct device_node *of_get_child_by_name(const struct device_node *node,
298 const char *name);
Bryan Wu954e04b2013-09-24 10:38:26 -0700299
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +0100300/* cache lookup */
301extern struct device_node *of_find_next_cache_node(const struct device_node *);
Sudeep Holla5fa23532017-01-16 10:40:43 +0000302extern int of_find_last_cache_level(unsigned int cpu);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000303extern struct device_node *of_find_node_with_property(
304 struct device_node *from, const char *prop_name);
Michael Ellerman1e291b12008-11-12 18:54:42 +0000305
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000306extern struct property *of_find_property(const struct device_node *np,
307 const char *name,
308 int *lenp);
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100309extern int of_property_count_elems_of_size(const struct device_node *np,
310 const char *propname, int elem_size);
Tony Prisk3daf3722013-03-23 17:02:15 +1300311extern int of_property_read_u32_index(const struct device_node *np,
312 const char *propname,
313 u32 index, u32 *out_value);
Alistair Popple2475a2b2017-04-03 19:51:42 +1000314extern int of_property_read_u64_index(const struct device_node *np,
315 const char *propname,
316 u32 index, u64 *out_value);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100317extern int of_property_read_variable_u8_array(const struct device_node *np,
318 const char *propname, u8 *out_values,
319 size_t sz_min, size_t sz_max);
320extern int of_property_read_variable_u16_array(const struct device_node *np,
321 const char *propname, u16 *out_values,
322 size_t sz_min, size_t sz_max);
323extern int of_property_read_variable_u32_array(const struct device_node *np,
324 const char *propname,
325 u32 *out_values,
326 size_t sz_min,
327 size_t sz_max);
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100328extern int of_property_read_u64(const struct device_node *np,
329 const char *propname, u64 *out_value);
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100330extern int of_property_read_variable_u64_array(const struct device_node *np,
331 const char *propname,
332 u64 *out_values,
333 size_t sz_min,
334 size_t sz_max);
Rob Herring0e373632011-07-06 15:42:58 -0500335
David Rivshinfe99c702016-03-02 16:35:51 -0500336extern int of_property_read_string(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100337 const char *propname,
338 const char **out_string);
David Rivshinfe99c702016-03-02 16:35:51 -0500339extern int of_property_match_string(const struct device_node *np,
Grant Likely7aff0fe2011-12-12 09:25:58 -0700340 const char *propname,
341 const char *string);
David Rivshinfe99c702016-03-02 16:35:51 -0500342extern int of_property_read_string_helper(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000343 const char *propname,
344 const char **out_strs, size_t sz, int index);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000345extern int of_device_is_compatible(const struct device_node *device,
346 const char *);
Benjamin Herrenschmidtb9c13fe2016-07-08 08:35:59 +1000347extern int of_device_compatible_match(struct device_node *device,
348 const char *const *compat);
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800349extern bool of_device_is_available(const struct device_node *device);
Kevin Cernekee37786c72015-04-09 13:05:14 -0700350extern bool of_device_is_big_endian(const struct device_node *device);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000351extern const void *of_get_property(const struct device_node *node,
352 const char *name,
353 int *lenp);
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100354extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
Dong Aisheng8af0da92011-12-22 20:19:24 +0800355#define for_each_property_of_node(dn, pp) \
356 for (pp = dn->properties; pp != NULL; pp = pp->next)
Shawn Guo611cad72011-08-15 15:28:14 +0800357
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000358extern int of_n_addr_cells(struct device_node *np);
359extern int of_n_size_cells(struct device_node *np);
Grant Likely283029d2008-01-09 06:20:40 +1100360extern const struct of_device_id *of_match_node(
361 const struct of_device_id *matches, const struct device_node *node);
Grant Likely3f07af42008-07-25 22:25:13 -0400362extern int of_modalias_node(struct device_node *node, char *modalias, int len);
Grant Likely624cfca2013-10-11 22:05:10 +0100363extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100364extern struct device_node *of_parse_phandle(const struct device_node *np,
Grant Likely739649c2009-04-25 12:52:40 +0000365 const char *phandle_name,
366 int index);
Guennadi Liakhovetski93c667c2012-12-10 20:41:30 +0100367extern int of_parse_phandle_with_args(const struct device_node *np,
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000368 const char *list_name, const char *cells_name, int index,
Grant Likely15c9a0a2011-12-12 09:25:57 -0700369 struct of_phandle_args *out_args);
Stephen Warren035fd942013-08-14 15:27:10 -0600370extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
371 const char *list_name, int cells_count, int index,
372 struct of_phandle_args *out_args);
Grant Likelybd69f732013-02-10 22:57:21 +0000373extern int of_count_phandle_with_args(const struct device_node *np,
374 const char *list_name, const char *cells_name);
Stephen Rothwell76c1ce72007-05-01 16:19:07 +1000375
Joerg Roedel74e1fbb2016-04-04 17:49:17 +0200376/* phandle iterator functions */
377extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
378 const struct device_node *np,
379 const char *list_name,
380 const char *cells_name,
381 int cell_count);
382
Joerg Roedelcd209b42016-04-04 17:49:18 +0200383extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
Joerg Roedelabdaa772016-04-04 17:49:21 +0200384extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
385 uint32_t *args,
386 int size);
Joerg Roedelcd209b42016-04-04 17:49:18 +0200387
Shawn Guo611cad72011-08-15 15:28:14 +0800388extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
389extern int of_alias_get_id(struct device_node *np, const char *stem);
Wolfram Sang351d2242015-03-12 17:17:58 +0100390extern int of_alias_get_highest_id(const char *stem);
Shawn Guo611cad72011-08-15 15:28:14 +0800391
Grant Likely21b082e2010-02-14 07:13:38 -0700392extern int of_machine_is_compatible(const char *compat);
393
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000394extern int of_add_property(struct device_node *np, struct property *prop);
395extern int of_remove_property(struct device_node *np, struct property *prop);
396extern int of_update_property(struct device_node *np, struct property *newprop);
Grant Likely21b082e2010-02-14 07:13:38 -0700397
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700398/* For updating the device tree at runtime */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000399#define OF_RECONFIG_ATTACH_NODE 0x0001
400#define OF_RECONFIG_DETACH_NODE 0x0002
401#define OF_RECONFIG_ADD_PROPERTY 0x0003
402#define OF_RECONFIG_REMOVE_PROPERTY 0x0004
403#define OF_RECONFIG_UPDATE_PROPERTY 0x0005
404
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000405extern int of_attach_node(struct device_node *);
406extern int of_detach_node(struct device_node *);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700407
Ben Dooks3a1e3622011-08-03 10:11:42 +0100408#define of_match_ptr(_ptr) (_ptr)
Stephen Warrenc541adc2012-04-04 09:27:46 -0600409
Richard Fitzgeralda67e9472016-09-12 14:01:29 +0100410/**
411 * of_property_read_u8_array - Find and read an array of u8 from a property.
412 *
413 * @np: device node from which the property value is to be read.
414 * @propname: name of the property to be searched.
415 * @out_values: pointer to return value, modified only if return value is 0.
416 * @sz: number of array elements to read
417 *
418 * Search for a property in a device node and read 8-bit value(s) from
419 * it. Returns 0 on success, -EINVAL if the property does not exist,
420 * -ENODATA if property does not have a value, and -EOVERFLOW if the
421 * property data isn't large enough.
422 *
423 * dts entry of array should be like:
424 * property = /bits/ 8 <0x50 0x60 0x70>;
425 *
426 * The out_values is modified only if a valid u8 value can be decoded.
427 */
428static inline int of_property_read_u8_array(const struct device_node *np,
429 const char *propname,
430 u8 *out_values, size_t sz)
431{
432 int ret = of_property_read_variable_u8_array(np, propname, out_values,
433 sz, 0);
434 if (ret >= 0)
435 return 0;
436 else
437 return ret;
438}
439
440/**
441 * of_property_read_u16_array - Find and read an array of u16 from a property.
442 *
443 * @np: device node from which the property value is to be read.
444 * @propname: name of the property to be searched.
445 * @out_values: pointer to return value, modified only if return value is 0.
446 * @sz: number of array elements to read
447 *
448 * Search for a property in a device node and read 16-bit value(s) from
449 * it. Returns 0 on success, -EINVAL if the property does not exist,
450 * -ENODATA if property does not have a value, and -EOVERFLOW if the
451 * property data isn't large enough.
452 *
453 * dts entry of array should be like:
454 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
455 *
456 * The out_values is modified only if a valid u16 value can be decoded.
457 */
458static inline int of_property_read_u16_array(const struct device_node *np,
459 const char *propname,
460 u16 *out_values, size_t sz)
461{
462 int ret = of_property_read_variable_u16_array(np, propname, out_values,
463 sz, 0);
464 if (ret >= 0)
465 return 0;
466 else
467 return ret;
468}
469
470/**
471 * of_property_read_u32_array - Find and read an array of 32 bit integers
472 * from a property.
473 *
474 * @np: device node from which the property value is to be read.
475 * @propname: name of the property to be searched.
476 * @out_values: pointer to return value, modified only if return value is 0.
477 * @sz: number of array elements to read
478 *
479 * Search for a property in a device node and read 32-bit value(s) from
480 * it. Returns 0 on success, -EINVAL if the property does not exist,
481 * -ENODATA if property does not have a value, and -EOVERFLOW if the
482 * property data isn't large enough.
483 *
484 * The out_values is modified only if a valid u32 value can be decoded.
485 */
486static inline int of_property_read_u32_array(const struct device_node *np,
487 const char *propname,
488 u32 *out_values, size_t sz)
489{
490 int ret = of_property_read_variable_u32_array(np, propname, out_values,
491 sz, 0);
492 if (ret >= 0)
493 return 0;
494 else
495 return ret;
496}
497
498/**
499 * of_property_read_u64_array - Find and read an array of 64 bit integers
500 * from a property.
501 *
502 * @np: device node from which the property value is to be read.
503 * @propname: name of the property to be searched.
504 * @out_values: pointer to return value, modified only if return value is 0.
505 * @sz: number of array elements to read
506 *
507 * Search for a property in a device node and read 64-bit value(s) from
508 * it. Returns 0 on success, -EINVAL if the property does not exist,
509 * -ENODATA if property does not have a value, and -EOVERFLOW if the
510 * property data isn't large enough.
511 *
512 * The out_values is modified only if a valid u64 value can be decoded.
513 */
514static inline int of_property_read_u64_array(const struct device_node *np,
515 const char *propname,
516 u64 *out_values, size_t sz)
517{
518 int ret = of_property_read_variable_u64_array(np, propname, out_values,
519 sz, 0);
520 if (ret >= 0)
521 return 0;
522 else
523 return ret;
524}
525
Stephen Warrenc541adc2012-04-04 09:27:46 -0600526/*
527 * struct property *prop;
528 * const __be32 *p;
529 * u32 u;
530 *
531 * of_property_for_each_u32(np, "propname", prop, p, u)
532 * printk("U32 value: %x\n", u);
533 */
534const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
535 u32 *pu);
Stephen Warrenc541adc2012-04-04 09:27:46 -0600536/*
537 * struct property *prop;
538 * const char *s;
539 *
540 * of_property_for_each_string(np, "propname", prop, s)
541 * printk("String value: %s\n", s);
542 */
543const char *of_prop_next_string(struct property *prop, const char *cur);
Stephen Warrenc541adc2012-04-04 09:27:46 -0600544
Grant Likely3482f2c2014-03-27 17:18:55 -0700545bool of_console_check(struct device_node *dn, char *name, int index);
Sascha Hauer5c19e952013-08-05 14:40:44 +0200546
Shawn Guob98c0232011-07-08 16:27:33 +0800547#else /* CONFIG_OF */
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100548
Sudeep Holla194ec9362015-05-14 15:28:24 +0100549static inline void of_core_init(void)
550{
551}
552
Sakari Ailusd20dc142017-05-24 17:53:55 +0300553static inline bool is_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100554{
555 return false;
556}
557
Sakari Ailusd20dc142017-05-24 17:53:55 +0300558static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +0100559{
560 return NULL;
561}
562
Stephen Rothwell4c358e12014-05-15 14:44:30 +1000563static inline const char* of_node_full_name(const struct device_node *np)
Grant Likely74a7f082012-06-15 11:50:25 -0600564{
565 return "<no-node>";
566}
567
Peter Ujfalusi1cc44f42012-09-10 13:46:24 +0300568static inline struct device_node *of_find_node_by_name(struct device_node *from,
569 const char *name)
570{
571 return NULL;
572}
573
Rob Herring662372e2014-02-03 08:53:44 -0600574static inline struct device_node *of_find_node_by_type(struct device_node *from,
575 const char *type)
576{
577 return NULL;
578}
579
580static inline struct device_node *of_find_matching_node_and_match(
581 struct device_node *from,
582 const struct of_device_id *matches,
583 const struct of_device_id **match)
584{
585 return NULL;
586}
587
Alexander Shiyan20cd4772014-04-16 10:49:20 +0400588static inline struct device_node *of_find_node_by_path(const char *path)
589{
590 return NULL;
591}
592
Leif Lindholm75c28c02014-11-28 11:34:28 +0000593static inline struct device_node *of_find_node_opts_by_path(const char *path,
594 const char **opts)
595{
596 return NULL;
597}
598
Suman Annace16b9d2015-06-17 11:53:53 -0500599static inline struct device_node *of_find_node_by_phandle(phandle handle)
600{
601 return NULL;
602}
603
Alexander Shiyan066ec1d2013-04-09 19:47:40 +0400604static inline struct device_node *of_get_parent(const struct device_node *node)
605{
606 return NULL;
607}
608
Rob Herring662372e2014-02-03 08:53:44 -0600609static inline struct device_node *of_get_next_child(
610 const struct device_node *node, struct device_node *prev)
611{
612 return NULL;
613}
614
615static inline struct device_node *of_get_next_available_child(
616 const struct device_node *node, struct device_node *prev)
617{
618 return NULL;
619}
620
621static inline struct device_node *of_find_node_with_property(
622 struct device_node *from, const char *prop_name)
623{
624 return NULL;
625}
626
Sakari Ailus67831832017-03-28 10:52:23 +0300627#define of_fwnode_handle(node) NULL
628
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +0100629static inline bool of_have_populated_dt(void)
630{
631 return false;
632}
633
Olof Johansson25c040c2012-10-07 10:40:54 -0700634static inline struct device_node *of_get_child_by_name(
635 const struct device_node *node,
636 const char *name)
637{
638 return NULL;
639}
640
Rajendra Nayak36a09042011-10-10 21:49:35 +0530641static inline int of_device_is_compatible(const struct device_node *device,
642 const char *name)
643{
644 return 0;
645}
646
Geert Uytterhoeven3bc16302017-04-25 19:38:48 +0200647static inline int of_device_compatible_match(struct device_node *device,
648 const char *const *compat)
649{
650 return 0;
651}
652
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800653static inline bool of_device_is_available(const struct device_node *device)
Rob Herringd7195692013-03-20 16:56:18 -0500654{
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800655 return false;
Rob Herringd7195692013-03-20 16:56:18 -0500656}
657
Kevin Cernekee37786c72015-04-09 13:05:14 -0700658static inline bool of_device_is_big_endian(const struct device_node *device)
659{
660 return false;
661}
662
Stephen Warrenaba3dff2011-09-21 13:23:10 -0600663static inline struct property *of_find_property(const struct device_node *np,
664 const char *name,
665 int *lenp)
666{
667 return NULL;
668}
669
Shawn Guo2261cc62012-02-15 10:47:42 -0800670static inline struct device_node *of_find_compatible_node(
671 struct device_node *from,
672 const char *type,
673 const char *compat)
674{
675 return NULL;
676}
677
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100678static inline int of_property_count_elems_of_size(const struct device_node *np,
679 const char *propname, int elem_size)
680{
681 return -ENOSYS;
682}
683
Tony Prisk3daf3722013-03-23 17:02:15 +1300684static inline int of_property_read_u32_index(const struct device_node *np,
685 const char *propname, u32 index, u32 *out_value)
686{
687 return -ENOSYS;
688}
689
Viresh Kumarbe193242012-11-20 10:15:19 +0530690static inline int of_property_read_u8_array(const struct device_node *np,
691 const char *propname, u8 *out_values, size_t sz)
692{
693 return -ENOSYS;
694}
695
696static inline int of_property_read_u16_array(const struct device_node *np,
697 const char *propname, u16 *out_values, size_t sz)
698{
699 return -ENOSYS;
700}
701
Shawn Guob98c0232011-07-08 16:27:33 +0800702static inline int of_property_read_u32_array(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100703 const char *propname,
704 u32 *out_values, size_t sz)
Shawn Guob98c0232011-07-08 16:27:33 +0800705{
706 return -ENOSYS;
707}
708
Rafael J. Wysockib31384f2014-11-04 01:28:56 +0100709static inline int of_property_read_u64_array(const struct device_node *np,
710 const char *propname,
711 u64 *out_values, size_t sz)
712{
713 return -ENOSYS;
714}
715
David Rivshinfe99c702016-03-02 16:35:51 -0500716static inline int of_property_read_string(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +0100717 const char *propname,
718 const char **out_string)
Shawn Guob98c0232011-07-08 16:27:33 +0800719{
720 return -ENOSYS;
721}
722
David Rivshinfe99c702016-03-02 16:35:51 -0500723static inline int of_property_read_string_helper(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000724 const char *propname,
725 const char **out_strs, size_t sz, int index)
Benoit Cousson4fcd15a2011-09-27 17:45:43 +0200726{
727 return -ENOSYS;
728}
729
Stephen Warren89272b82011-08-05 16:50:30 -0600730static inline const void *of_get_property(const struct device_node *node,
731 const char *name,
732 int *lenp)
733{
734 return NULL;
735}
736
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100737static inline struct device_node *of_get_cpu_node(int cpu,
738 unsigned int *thread)
739{
740 return NULL;
741}
742
Jamie Iles4cd7f7a2011-09-14 20:49:59 +0100743static inline int of_property_read_u64(const struct device_node *np,
744 const char *propname, u64 *out_value)
745{
746 return -ENOSYS;
747}
748
David Rivshinfe99c702016-03-02 16:35:51 -0500749static inline int of_property_match_string(const struct device_node *np,
Thierry Redingbd3d5502012-04-13 16:18:34 +0200750 const char *propname,
751 const char *string)
752{
753 return -ENOSYS;
754}
755
Steffen Trumtrarb8fbdc42012-11-22 12:16:43 +0100756static inline struct device_node *of_parse_phandle(const struct device_node *np,
Rajendra Nayak36a09042011-10-10 21:49:35 +0530757 const char *phandle_name,
758 int index)
759{
760 return NULL;
761}
762
Kuninori Morimotoe93aeea2016-05-25 01:15:04 +0000763static inline int of_parse_phandle_with_args(const struct device_node *np,
Thierry Redinge05e5072012-04-13 16:19:21 +0200764 const char *list_name,
765 const char *cells_name,
766 int index,
767 struct of_phandle_args *out_args)
768{
769 return -ENOSYS;
770}
771
Stephen Warren035fd942013-08-14 15:27:10 -0600772static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
773 const char *list_name, int cells_count, int index,
774 struct of_phandle_args *out_args)
775{
776 return -ENOSYS;
777}
778
Grant Likelybd69f732013-02-10 22:57:21 +0000779static inline int of_count_phandle_with_args(struct device_node *np,
780 const char *list_name,
781 const char *cells_name)
782{
783 return -ENOSYS;
784}
785
Joerg Roedel74e1fbb2016-04-04 17:49:17 +0200786static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
787 const struct device_node *np,
788 const char *list_name,
789 const char *cells_name,
790 int cell_count)
791{
792 return -ENOSYS;
793}
794
Joerg Roedelcd209b42016-04-04 17:49:18 +0200795static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
796{
797 return -ENOSYS;
798}
799
Joerg Roedelabdaa772016-04-04 17:49:21 +0200800static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
801 uint32_t *args,
802 int size)
803{
804 return 0;
805}
806
Nicolas Ferreed5f8862011-10-27 11:07:28 +0200807static inline int of_alias_get_id(struct device_node *np, const char *stem)
808{
809 return -ENOSYS;
810}
811
Wolfram Sang351d2242015-03-12 17:17:58 +0100812static inline int of_alias_get_highest_id(const char *stem)
813{
814 return -ENOSYS;
815}
816
Stephen Warren50e07f82011-10-25 14:01:26 +0200817static inline int of_machine_is_compatible(const char *compat)
818{
819 return 0;
820}
821
Grant Likely3482f2c2014-03-27 17:18:55 -0700822static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +0200823{
Grant Likely3482f2c2014-03-27 17:18:55 -0700824 return false;
Sascha Hauer5c19e952013-08-05 14:40:44 +0200825}
826
Sebastian Andrzej Siewior2adfffa2013-06-17 16:48:13 +0200827static inline const __be32 *of_prop_next_u32(struct property *prop,
828 const __be32 *cur, u32 *pu)
829{
830 return NULL;
831}
832
833static inline const char *of_prop_next_string(struct property *prop,
834 const char *cur)
835{
836 return NULL;
837}
838
Pantelis Antoniou0384e8c2015-01-21 19:05:57 +0200839static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
840{
841 return 0;
842}
843
844static inline int of_node_test_and_set_flag(struct device_node *n,
845 unsigned long flag)
846{
847 return 0;
848}
849
850static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
851{
852}
853
854static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
855{
856}
857
858static inline int of_property_check_flag(struct property *p, unsigned long flag)
859{
860 return 0;
861}
862
863static inline void of_property_set_flag(struct property *p, unsigned long flag)
864{
865}
866
867static inline void of_property_clear_flag(struct property *p, unsigned long flag)
868{
869}
870
Ben Dooks3a1e3622011-08-03 10:11:42 +0100871#define of_match_ptr(_ptr) NULL
Nicolas Ferre5762c202011-10-24 11:53:32 +0200872#define of_match_node(_matches, _node) NULL
Jeremy Kerr9dfbf202010-02-14 07:13:43 -0700873#endif /* CONFIG_OF */
Shawn Guob98c0232011-07-08 16:27:33 +0800874
Adam Thomson613e9722016-06-21 18:50:20 +0100875/* Default string compare functions, Allow arch asm/prom.h to override */
876#if !defined(of_compat_cmp)
877#define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
878#define of_prop_cmp(s1, s2) strcmp((s1), (s2))
879#define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
880#endif
881
Rob Herring0c3f0612013-09-17 10:42:50 -0500882#if defined(CONFIG_OF) && defined(CONFIG_NUMA)
883extern int of_node_to_nid(struct device_node *np);
884#else
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +0300885static inline int of_node_to_nid(struct device_node *device)
886{
887 return NUMA_NO_NODE;
888}
Paul Mundt5ca4db62012-06-03 22:04:34 -0700889#endif
890
David Daney298535c2016-04-08 15:50:25 -0700891#ifdef CONFIG_OF_NUMA
892extern int of_numa_init(void);
893#else
894static inline int of_numa_init(void)
895{
896 return -ENOSYS;
897}
898#endif
899
Rob Herring662372e2014-02-03 08:53:44 -0600900static inline struct device_node *of_find_matching_node(
901 struct device_node *from,
902 const struct of_device_id *matches)
903{
904 return of_find_matching_node_and_match(from, matches, NULL);
905}
906
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +0800907/**
Heiko Stuebnerad54a0c2014-02-12 01:00:34 +0100908 * of_property_count_u8_elems - Count the number of u8 elements in a property
909 *
910 * @np: device node from which the property value is to be read.
911 * @propname: name of the property to be searched.
912 *
913 * Search for a property in a device node and count the number of u8 elements
914 * in it. Returns number of elements on sucess, -EINVAL if the property does
915 * not exist or its length does not match a multiple of u8 and -ENODATA if the
916 * property does not have a value.
917 */
918static inline int of_property_count_u8_elems(const struct device_node *np,
919 const char *propname)
920{
921 return of_property_count_elems_of_size(np, propname, sizeof(u8));
922}
923
924/**
925 * of_property_count_u16_elems - Count the number of u16 elements in a property
926 *
927 * @np: device node from which the property value is to be read.
928 * @propname: name of the property to be searched.
929 *
930 * Search for a property in a device node and count the number of u16 elements
931 * in it. Returns number of elements on sucess, -EINVAL if the property does
932 * not exist or its length does not match a multiple of u16 and -ENODATA if the
933 * property does not have a value.
934 */
935static inline int of_property_count_u16_elems(const struct device_node *np,
936 const char *propname)
937{
938 return of_property_count_elems_of_size(np, propname, sizeof(u16));
939}
940
941/**
942 * of_property_count_u32_elems - Count the number of u32 elements in a property
943 *
944 * @np: device node from which the property value is to be read.
945 * @propname: name of the property to be searched.
946 *
947 * Search for a property in a device node and count the number of u32 elements
948 * in it. Returns number of elements on sucess, -EINVAL if the property does
949 * not exist or its length does not match a multiple of u32 and -ENODATA if the
950 * property does not have a value.
951 */
952static inline int of_property_count_u32_elems(const struct device_node *np,
953 const char *propname)
954{
955 return of_property_count_elems_of_size(np, propname, sizeof(u32));
956}
957
958/**
959 * of_property_count_u64_elems - Count the number of u64 elements in a property
960 *
961 * @np: device node from which the property value is to be read.
962 * @propname: name of the property to be searched.
963 *
964 * Search for a property in a device node and count the number of u64 elements
965 * in it. Returns number of elements on sucess, -EINVAL if the property does
966 * not exist or its length does not match a multiple of u64 and -ENODATA if the
967 * property does not have a value.
968 */
969static inline int of_property_count_u64_elems(const struct device_node *np,
970 const char *propname)
971{
972 return of_property_count_elems_of_size(np, propname, sizeof(u64));
973}
974
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +0800975/**
Grant Likelya87fa1d2014-11-03 15:15:35 +0000976 * of_property_read_string_array() - Read an array of strings from a multiple
977 * strings property.
978 * @np: device node from which the property value is to be read.
979 * @propname: name of the property to be searched.
980 * @out_strs: output array of string pointers.
981 * @sz: number of array elements to read.
982 *
983 * Search for a property in a device tree node and retrieve a list of
984 * terminated string values (pointer to data, not a copy) in that property.
985 *
986 * If @out_strs is NULL, the number of strings in the property is returned.
987 */
David Rivshinfe99c702016-03-02 16:35:51 -0500988static inline int of_property_read_string_array(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +0000989 const char *propname, const char **out_strs,
990 size_t sz)
991{
992 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
993}
994
995/**
996 * of_property_count_strings() - Find and return the number of strings from a
997 * multiple strings property.
998 * @np: device node from which the property value is to be read.
999 * @propname: name of the property to be searched.
1000 *
1001 * Search for a property in a device tree node and retrieve the number of null
1002 * terminated string contain in it. Returns the number of strings on
1003 * success, -EINVAL if the property does not exist, -ENODATA if property
1004 * does not have a value, and -EILSEQ if the string is not null-terminated
1005 * within the length of the property data.
1006 */
David Rivshinfe99c702016-03-02 16:35:51 -05001007static inline int of_property_count_strings(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +00001008 const char *propname)
1009{
1010 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1011}
1012
1013/**
1014 * of_property_read_string_index() - Find and read a string from a multiple
1015 * strings property.
1016 * @np: device node from which the property value is to be read.
1017 * @propname: name of the property to be searched.
1018 * @index: index of the string in the list of strings
1019 * @out_string: pointer to null terminated return string, modified only if
1020 * return value is 0.
1021 *
1022 * Search for a property in a device tree node and retrieve a null
1023 * terminated string value (pointer to data, not a copy) in the list of strings
1024 * contained in that property.
1025 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1026 * property does not have a value, and -EILSEQ if the string is not
1027 * null-terminated within the length of the property data.
1028 *
1029 * The out_string pointer is modified only if a valid string can be decoded.
1030 */
David Rivshinfe99c702016-03-02 16:35:51 -05001031static inline int of_property_read_string_index(const struct device_node *np,
Grant Likelya87fa1d2014-11-03 15:15:35 +00001032 const char *propname,
1033 int index, const char **output)
1034{
1035 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1036 return rc < 0 ? rc : 0;
1037}
1038
1039/**
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001040 * of_property_read_bool - Findfrom a property
1041 * @np: device node from which the property value is to be read.
1042 * @propname: name of the property to be searched.
1043 *
1044 * Search for a property in a device node.
Geert Uytterhoeven31712c92015-05-04 19:42:19 +02001045 * Returns true if the property exists false otherwise.
Jean-Christophe PLAGNIOL-VILLARDfa4d34c2012-02-07 12:12:51 +08001046 */
1047static inline bool of_property_read_bool(const struct device_node *np,
1048 const char *propname)
1049{
1050 struct property *prop = of_find_property(np, propname, NULL);
1051
1052 return prop ? true : false;
1053}
1054
Viresh Kumarbe193242012-11-20 10:15:19 +05301055static inline int of_property_read_u8(const struct device_node *np,
1056 const char *propname,
1057 u8 *out_value)
1058{
1059 return of_property_read_u8_array(np, propname, out_value, 1);
1060}
1061
1062static inline int of_property_read_u16(const struct device_node *np,
1063 const char *propname,
1064 u16 *out_value)
1065{
1066 return of_property_read_u16_array(np, propname, out_value, 1);
1067}
1068
Shawn Guob98c0232011-07-08 16:27:33 +08001069static inline int of_property_read_u32(const struct device_node *np,
Jamie Ilesaac285c2011-08-02 15:45:07 +01001070 const char *propname,
Shawn Guob98c0232011-07-08 16:27:33 +08001071 u32 *out_value)
1072{
1073 return of_property_read_u32_array(np, propname, out_value, 1);
1074}
1075
Sebastian Reichele7a00e42014-04-06 12:52:11 +02001076static inline int of_property_read_s32(const struct device_node *np,
1077 const char *propname,
1078 s32 *out_value)
1079{
1080 return of_property_read_u32(np, propname, (u32*) out_value);
1081}
1082
Joerg Roedelf623ce92016-04-04 17:49:20 +02001083#define of_for_each_phandle(it, err, np, ln, cn, cc) \
1084 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1085 err = of_phandle_iterator_next(it); \
1086 err == 0; \
1087 err = of_phandle_iterator_next(it))
1088
Sebastian Andrzej Siewior2adfffa2013-06-17 16:48:13 +02001089#define of_property_for_each_u32(np, propname, prop, p, u) \
1090 for (prop = of_find_property(np, propname, NULL), \
1091 p = of_prop_next_u32(prop, NULL, &u); \
1092 p; \
1093 p = of_prop_next_u32(prop, p, &u))
1094
1095#define of_property_for_each_string(np, propname, prop, s) \
1096 for (prop = of_find_property(np, propname, NULL), \
1097 s = of_prop_next_string(prop, NULL); \
1098 s; \
1099 s = of_prop_next_string(prop, s))
1100
Rob Herring662372e2014-02-03 08:53:44 -06001101#define for_each_node_by_name(dn, name) \
1102 for (dn = of_find_node_by_name(NULL, name); dn; \
1103 dn = of_find_node_by_name(dn, name))
1104#define for_each_node_by_type(dn, type) \
1105 for (dn = of_find_node_by_type(NULL, type); dn; \
1106 dn = of_find_node_by_type(dn, type))
1107#define for_each_compatible_node(dn, type, compatible) \
1108 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1109 dn = of_find_compatible_node(dn, type, compatible))
1110#define for_each_matching_node(dn, matches) \
1111 for (dn = of_find_matching_node(NULL, matches); dn; \
1112 dn = of_find_matching_node(dn, matches))
1113#define for_each_matching_node_and_match(dn, matches, match) \
1114 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1115 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1116
1117#define for_each_child_of_node(parent, child) \
1118 for (child = of_get_next_child(parent, NULL); child != NULL; \
1119 child = of_get_next_child(parent, child))
1120#define for_each_available_child_of_node(parent, child) \
1121 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1122 child = of_get_next_available_child(parent, child))
1123
1124#define for_each_node_with_property(dn, prop_name) \
1125 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1126 dn = of_find_node_with_property(dn, prop_name))
1127
1128static inline int of_get_child_count(const struct device_node *np)
1129{
1130 struct device_node *child;
1131 int num = 0;
1132
1133 for_each_child_of_node(np, child)
1134 num++;
1135
1136 return num;
1137}
1138
1139static inline int of_get_available_child_count(const struct device_node *np)
1140{
1141 struct device_node *child;
1142 int num = 0;
1143
1144 for_each_available_child_of_node(np, child)
1145 num++;
1146
1147 return num;
1148}
1149
Masahiro Yamada71f50c62016-01-22 01:33:51 +09001150#if defined(CONFIG_OF) && !defined(MODULE)
Rob Herring54196cc2014-05-08 16:09:24 -05001151#define _OF_DECLARE(table, name, compat, fn, fn_type) \
1152 static const struct of_device_id __of_table_##name \
1153 __used __section(__##table##_of_table) \
1154 = { .compatible = compat, \
1155 .data = (fn == (fn_type)NULL) ? fn : fn }
1156#else
Thierry Reding5f563582014-10-02 16:01:10 +02001157#define _OF_DECLARE(table, name, compat, fn, fn_type) \
Rob Herring54196cc2014-05-08 16:09:24 -05001158 static const struct of_device_id __of_table_##name \
1159 __attribute__((unused)) \
1160 = { .compatible = compat, \
1161 .data = (fn == (fn_type)NULL) ? fn : fn }
1162#endif
1163
1164typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
Daniel Lezcanoc35d9292016-04-18 23:06:48 +02001165typedef int (*of_init_fn_1_ret)(struct device_node *);
Rob Herring54196cc2014-05-08 16:09:24 -05001166typedef void (*of_init_fn_1)(struct device_node *);
1167
1168#define OF_DECLARE_1(table, name, compat, fn) \
1169 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
Daniel Lezcanoc35d9292016-04-18 23:06:48 +02001170#define OF_DECLARE_1_RET(table, name, compat, fn) \
1171 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
Rob Herring54196cc2014-05-08 16:09:24 -05001172#define OF_DECLARE_2(table, name, compat, fn) \
1173 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1174
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001175/**
1176 * struct of_changeset_entry - Holds a changeset entry
1177 *
1178 * @node: list_head for the log list
1179 * @action: notifier action
1180 * @np: pointer to the device node affected
1181 * @prop: pointer to the property affected
1182 * @old_prop: hold a pointer to the original property
1183 *
1184 * Every modification of the device tree during a changeset
1185 * is held in a list of of_changeset_entry structures.
1186 * That way we can recover from a partial application, or we can
1187 * revert the changeset
1188 */
1189struct of_changeset_entry {
1190 struct list_head node;
1191 unsigned long action;
1192 struct device_node *np;
1193 struct property *prop;
1194 struct property *old_prop;
1195};
1196
1197/**
1198 * struct of_changeset - changeset tracker structure
1199 *
1200 * @entries: list_head for the changeset entries
1201 *
1202 * changesets are a convenient way to apply bulk changes to the
1203 * live tree. In case of an error, changes are rolled-back.
1204 * changesets live on after initial application, and if not
1205 * destroyed after use, they can be reverted in one single call.
1206 */
1207struct of_changeset {
1208 struct list_head entries;
1209};
1210
Pantelis Antonioub53a2342014-10-28 22:33:53 +02001211enum of_reconfig_change {
1212 OF_RECONFIG_NO_CHANGE = 0,
1213 OF_RECONFIG_CHANGE_ADD,
1214 OF_RECONFIG_CHANGE_REMOVE,
1215};
1216
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001217#ifdef CONFIG_OF_DYNAMIC
Grant Likelyf6892d12014-11-21 15:14:58 +00001218extern int of_reconfig_notifier_register(struct notifier_block *);
1219extern int of_reconfig_notifier_unregister(struct notifier_block *);
Grant Likelyf5242e52014-11-24 17:58:01 +00001220extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1221extern int of_reconfig_get_state_change(unsigned long action,
1222 struct of_reconfig_data *arg);
Grant Likelyf6892d12014-11-21 15:14:58 +00001223
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001224extern void of_changeset_init(struct of_changeset *ocs);
1225extern void of_changeset_destroy(struct of_changeset *ocs);
1226extern int of_changeset_apply(struct of_changeset *ocs);
1227extern int of_changeset_revert(struct of_changeset *ocs);
1228extern int of_changeset_action(struct of_changeset *ocs,
1229 unsigned long action, struct device_node *np,
1230 struct property *prop);
1231
1232static inline int of_changeset_attach_node(struct of_changeset *ocs,
1233 struct device_node *np)
1234{
1235 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1236}
1237
1238static inline int of_changeset_detach_node(struct of_changeset *ocs,
1239 struct device_node *np)
1240{
1241 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1242}
1243
1244static inline int of_changeset_add_property(struct of_changeset *ocs,
1245 struct device_node *np, struct property *prop)
1246{
1247 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1248}
1249
1250static inline int of_changeset_remove_property(struct of_changeset *ocs,
1251 struct device_node *np, struct property *prop)
1252{
1253 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1254}
1255
1256static inline int of_changeset_update_property(struct of_changeset *ocs,
1257 struct device_node *np, struct property *prop)
1258{
1259 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1260}
Grant Likelyf6892d12014-11-21 15:14:58 +00001261#else /* CONFIG_OF_DYNAMIC */
1262static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1263{
1264 return -EINVAL;
1265}
1266static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1267{
1268 return -EINVAL;
1269}
Grant Likelyf5242e52014-11-24 17:58:01 +00001270static inline int of_reconfig_notify(unsigned long action,
1271 struct of_reconfig_data *arg)
Grant Likelyf6892d12014-11-21 15:14:58 +00001272{
1273 return -EINVAL;
1274}
Grant Likelyf5242e52014-11-24 17:58:01 +00001275static inline int of_reconfig_get_state_change(unsigned long action,
1276 struct of_reconfig_data *arg)
Grant Likelyf6892d12014-11-21 15:14:58 +00001277{
1278 return -EINVAL;
1279}
1280#endif /* CONFIG_OF_DYNAMIC */
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001281
Pantelis Antoniou7941b272014-07-04 19:59:20 +03001282/* CONFIG_OF_RESOLVE api */
1283extern int of_resolve_phandles(struct device_node *tree);
1284
Romain Periera4b4e042014-10-14 06:31:09 +00001285/**
Romain Perier8f731102014-11-25 12:28:25 +00001286 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
Romain Periera4b4e042014-10-14 06:31:09 +00001287 * @np: Pointer to the given device_node
1288 *
1289 * return true if present false otherwise
1290 */
Romain Perier8f731102014-11-25 12:28:25 +00001291static inline bool of_device_is_system_power_controller(const struct device_node *np)
Romain Periera4b4e042014-10-14 06:31:09 +00001292{
Romain Perier8f731102014-11-25 12:28:25 +00001293 return of_property_read_bool(np, "system-power-controller");
Romain Periera4b4e042014-10-14 06:31:09 +00001294}
1295
Linus Torvalds7ef58b32014-12-11 13:06:58 -08001296/**
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001297 * Overlay support
1298 */
1299
Alan Tull39a842e2016-11-01 14:14:22 -05001300enum of_overlay_notify_action {
1301 OF_OVERLAY_PRE_APPLY,
1302 OF_OVERLAY_POST_APPLY,
1303 OF_OVERLAY_PRE_REMOVE,
1304 OF_OVERLAY_POST_REMOVE,
1305};
1306
1307struct of_overlay_notify_data {
1308 struct device_node *overlay;
1309 struct device_node *target;
1310};
1311
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001312#ifdef CONFIG_OF_OVERLAY
1313
1314/* ID based overlays; the API for external users */
1315int of_overlay_create(struct device_node *tree);
1316int of_overlay_destroy(int id);
1317int of_overlay_destroy_all(void);
1318
Alan Tull39a842e2016-11-01 14:14:22 -05001319int of_overlay_notifier_register(struct notifier_block *nb);
1320int of_overlay_notifier_unregister(struct notifier_block *nb);
1321
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001322#else
1323
1324static inline int of_overlay_create(struct device_node *tree)
1325{
1326 return -ENOTSUPP;
1327}
1328
1329static inline int of_overlay_destroy(int id)
1330{
1331 return -ENOTSUPP;
1332}
1333
1334static inline int of_overlay_destroy_all(void)
1335{
1336 return -ENOTSUPP;
1337}
1338
Alan Tull39a842e2016-11-01 14:14:22 -05001339static inline int of_overlay_notifier_register(struct notifier_block *nb)
1340{
1341 return 0;
1342}
1343
1344static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1345{
1346 return 0;
1347}
1348
Pantelis Antoniou7518b5892014-10-28 22:35:58 +02001349#endif
1350
Stephen Rothwell76c1ce72007-05-01 16:19:07 +10001351#endif /* _LINUX_OF_H */