blob: 3c2eea6f0c4b3679c13274f55f0e992b1f48b0ec [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassf11c7ab2017-05-18 20:09:03 -06002/*
3 * Function to read values from the device tree node attached to a udevice.
4 *
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glassf11c7ab2017-05-18 20:09:03 -06007 */
8
9#ifndef _DM_READ_H
10#define _DM_READ_H
11
Dan Murphy9beacb62020-07-23 07:01:38 -050012#include <linux/errno.h>
13
Simon Glass2a64ada2020-07-19 10:15:39 -060014#include <dm/device.h>
Simon Glassf11c7ab2017-05-18 20:09:03 -060015#include <dm/fdtaddr.h>
16#include <dm/ofnode.h>
17#include <dm/uclass.h>
18
Simon Glassa4481012017-06-12 06:21:29 -060019struct resource;
20
Simon Glassf11c7ab2017-05-18 20:09:03 -060021#if CONFIG_IS_ENABLED(OF_LIVE)
Simon Glass88b3a372020-01-27 08:49:40 -070022static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glassf11c7ab2017-05-18 20:09:03 -060023{
Simon Glassf10643c2020-12-19 10:40:14 -070024 return ofnode_to_np(dev_ofnode(dev));
Simon Glassf11c7ab2017-05-18 20:09:03 -060025}
26#else
Simon Glass88b3a372020-01-27 08:49:40 -070027static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glassf11c7ab2017-05-18 20:09:03 -060028{
29 return NULL;
30}
31#endif
32
Simon Glassef79ef22021-01-21 13:57:10 -070033#if !defined(CONFIG_DM_DEV_READ_INLINE) || CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass47a0fd32017-05-18 20:09:04 -060034/**
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +020035 * dev_read_u8() - read a 8-bit integer from a device's DT property
36 *
37 * @dev: device to read DT property from
38 * @propname: name of the property to read from
39 * @outp: place to put value (if found)
40 * Return: 0 if OK, -ve on error
41 */
42int dev_read_u8(const struct udevice *dev, const char *propname, u8 *outp);
43
44/**
45 * dev_read_u8_default() - read a 8-bit integer from a device's DT property
46 *
47 * @dev: device to read DT property from
48 * @propname: name of the property to read from
49 * @def: default value to return if the property has no value
50 * Return: property value, or @def if not found
51 */
52u8 dev_read_u8_default(const struct udevice *dev, const char *propname, u8 def);
53
54/**
55 * dev_read_u16() - read a 16-bit integer from a device's DT property
56 *
57 * @dev: device to read DT property from
58 * @propname: name of the property to read from
59 * @outp: place to put value (if found)
60 * Return: 0 if OK, -ve on error
61 */
62int dev_read_u16(const struct udevice *dev, const char *propname, u16 *outp);
63
64/**
65 * dev_read_u16_default() - read a 16-bit integer from a device's DT property
66 *
67 * @dev: device to read DT property from
68 * @propname: name of the property to read from
69 * @def: default value to return if the property has no value
70 * Return: property value, or @def if not found
71 */
72u16 dev_read_u16_default(const struct udevice *dev, const char *propname,
73 u16 def);
74
75/**
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090076 * dev_read_u32() - read a 32-bit integer from a device's DT property
77 *
78 * @dev: device to read DT property from
79 * @propname: name of the property to read from
80 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +010081 * Return: 0 if OK, -ve on error
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090082 */
Simon Glass88b3a372020-01-27 08:49:40 -070083int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
Masahiro Yamada3ab48f62017-12-30 02:00:05 +090084
85/**
Simon Glass47a0fd32017-05-18 20:09:04 -060086 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
87 *
88 * @dev: device to read DT property from
89 * @propname: name of the property to read from
90 * @def: default value to return if the property has no value
Patrick Delaunay6de6a612022-01-12 10:53:48 +010091 * Return: property value, or @def if not found
Simon Glass47a0fd32017-05-18 20:09:04 -060092 */
Simon Glass88b3a372020-01-27 08:49:40 -070093int dev_read_u32_default(const struct udevice *dev, const char *propname,
94 int def);
Simon Glassf11c7ab2017-05-18 20:09:03 -060095
96/**
Dario Binacchi4bb70752020-03-29 18:04:41 +020097 * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
98 * property
99 *
100 * @dev: device to read DT property from
101 * @propname: name of the property to read from
102 * @index: index of the integer to return
103 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100104 * Return: 0 if OK, -ve on error
Dario Binacchi4bb70752020-03-29 18:04:41 +0200105 */
106int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
107 u32 *outp);
108
109/**
110 * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
111 * DT property
112 *
113 * @dev: device to read DT property from
114 * @propname: name of the property to read from
115 * @index: index of the integer to return
116 * @def: default value to return if the property has no value
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100117 * Return: property value, or @def if not found
Dario Binacchi4bb70752020-03-29 18:04:41 +0200118 */
119u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
120 int index, u32 def);
121
122/**
Simon Glassa1b17e42018-12-10 10:37:37 -0700123 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
124 *
125 * @dev: device to read DT property from
126 * @propname: name of the property to read from
127 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100128 * Return: 0 if OK, -ve on error
Simon Glassa1b17e42018-12-10 10:37:37 -0700129 */
Simon Glass88b3a372020-01-27 08:49:40 -0700130int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
Simon Glassa1b17e42018-12-10 10:37:37 -0700131
132/**
133 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
134 *
135 * @dev: device to read DT property from
136 * @propname: name of the property to read from
137 * @def: default value to return if the property has no value
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100138 * Return: property value, or @def if not found
Simon Glassa1b17e42018-12-10 10:37:37 -0700139 */
Simon Glass88b3a372020-01-27 08:49:40 -0700140int dev_read_s32_default(const struct udevice *dev, const char *propname,
141 int def);
Simon Glassa1b17e42018-12-10 10:37:37 -0700142
143/**
144 * dev_read_u32u() - read a 32-bit integer from a device's DT property
145 *
146 * This version uses a standard uint type.
147 *
148 * @dev: device to read DT property from
149 * @propname: name of the property to read from
150 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100151 * Return: 0 if OK, -ve on error
Simon Glassa1b17e42018-12-10 10:37:37 -0700152 */
Simon Glass88b3a372020-01-27 08:49:40 -0700153int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
Simon Glassa1b17e42018-12-10 10:37:37 -0700154
155/**
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200156 * dev_read_u64() - read a 64-bit integer from a device's DT property
157 *
158 * @dev: device to read DT property from
159 * @propname: name of the property to read from
160 * @outp: place to put value (if found)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100161 * Return: 0 if OK, -ve on error
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200162 */
Simon Glass88b3a372020-01-27 08:49:40 -0700163int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200164
165/**
166 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
167 *
168 * @dev: device to read DT property from
169 * @propname: name of the property to read from
170 * @def: default value to return if the property has no value
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100171 * Return: property value, or @def if not found
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200172 */
Simon Glass88b3a372020-01-27 08:49:40 -0700173u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
174 u64 def);
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200175
176/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600177 * dev_read_string() - Read a string from a device's DT property
178 *
179 * @dev: device to read DT property from
180 * @propname: name of the property to read
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100181 * Return: string from property value, or NULL if there is no such property
Simon Glassf11c7ab2017-05-18 20:09:03 -0600182 */
Simon Glass88b3a372020-01-27 08:49:40 -0700183const char *dev_read_string(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600184
185/**
186 * dev_read_bool() - read a boolean value from a device's DT property
187 *
188 * @dev: device to read DT property from
189 * @propname: name of property to read
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100190 * Return: true if property is present (meaning true), false if not present
Simon Glassf11c7ab2017-05-18 20:09:03 -0600191 */
Simon Glass88b3a372020-01-27 08:49:40 -0700192bool dev_read_bool(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600193
194/**
195 * dev_read_subnode() - find a named subnode of a device
196 *
197 * @dev: device whose DT node contains the subnode
198 * @subnode_name: name of subnode to find
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100199 * Return: reference to subnode (which can be invalid if there is no such
Simon Glassf11c7ab2017-05-18 20:09:03 -0600200 * subnode)
201 */
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100202ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600203
204/**
205 * dev_read_size() - read the size of a property
206 *
207 * @dev: device to check
208 * @propname: property to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100209 * Return: size of property if present, or -EINVAL if not
Simon Glassf11c7ab2017-05-18 20:09:03 -0600210 */
Simon Glass88b3a372020-01-27 08:49:40 -0700211int dev_read_size(const struct udevice *dev, const char *propname);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600212
213/**
214 * dev_read_addr_index() - Get the indexed reg property of a device
215 *
216 * @dev: Device to read from
217 * @index: the 'reg' property can hold a list of <addr, size> pairs
218 * and @index is used to select which one is required
219 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100220 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glassf11c7ab2017-05-18 20:09:03 -0600221 */
Simon Glass88b3a372020-01-27 08:49:40 -0700222fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600223
224/**
Bin Mengbdce9032021-09-12 11:15:13 +0800225 * dev_read_addr_index_ptr() - Get the indexed reg property of a device
226 * as a pointer
227 *
228 * @dev: Device to read from
229 * @index: the 'reg' property can hold a list of <addr, size> pairs
230 * and @index is used to select which one is required
231 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100232 * Return: pointer or NULL if not found
Bin Mengbdce9032021-09-12 11:15:13 +0800233 */
234void *dev_read_addr_index_ptr(const struct udevice *dev, int index);
235
236/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530237 * dev_read_addr_size_index() - Get the indexed reg property of a device
238 *
239 * @dev: Device to read from
240 * @index: the 'reg' property can hold a list of <addr, size> pairs
241 * and @index is used to select which one is required
242 * @size: place to put size value (on success)
243 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100244 * Return: address or FDT_ADDR_T_NONE if not found
Sekhar Norif5b90472019-08-01 19:12:56 +0530245 */
Simon Glass88b3a372020-01-27 08:49:40 -0700246fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif5b90472019-08-01 19:12:56 +0530247 fdt_size_t *size);
248
249/**
Jonas Karlman5e030632023-07-22 13:30:15 +0000250 * dev_read_addr_size_index_ptr() - Get the indexed reg property of a device
251 * as a pointer
252 *
253 * @dev: Device to read from
254 * @index: the 'reg' property can hold a list of <addr, size> pairs
255 * and @index is used to select which one is required
256 * @size: place to put size value (on success)
257 *
258 * Return: pointer or NULL if not found
259 */
260void *dev_read_addr_size_index_ptr(const struct udevice *dev, int index,
261 fdt_size_t *size);
262
263/**
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200264 * dev_remap_addr_index() - Get the indexed reg property of a device
265 * as a memory-mapped I/O pointer
266 *
267 * @dev: Device to read from
268 * @index: the 'reg' property can hold a list of <addr, size> pairs
269 * and @index is used to select which one is required
270 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100271 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200272 */
Simon Glass88b3a372020-01-27 08:49:40 -0700273void *dev_remap_addr_index(const struct udevice *dev, int index);
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200274
275/**
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100276 * dev_read_addr_name() - Get the reg property of a device, indexed by name
277 *
278 * @dev: Device to read from
279 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
280 * 'reg-names' property providing named-based identification. @index
281 * indicates the value to search for in 'reg-names'.
282 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100283 * Return: address or FDT_ADDR_T_NONE if not found
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100284 */
Simon Glass88b3a372020-01-27 08:49:40 -0700285fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100286
287/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530288 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
289 *
290 * @dev: Device to read from
291 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
292 * 'reg-names' property providing named-based identification. @index
293 * indicates the value to search for in 'reg-names'.
294 * @size: place to put size value (on success)
295 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100296 * Return: address or FDT_ADDR_T_NONE if not found
Sekhar Norif5b90472019-08-01 19:12:56 +0530297 */
Simon Glass88b3a372020-01-27 08:49:40 -0700298fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif5b90472019-08-01 19:12:56 +0530299 fdt_size_t *size);
300
301/**
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100302 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
303 * as a memory-mapped I/O pointer
304 *
305 * @dev: Device to read from
306 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
307 * 'reg-names' property providing named-based identification. @index
308 * indicates the value to search for in 'reg-names'.
309 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100310 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100311 */
Simon Glass88b3a372020-01-27 08:49:40 -0700312void *dev_remap_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100313
314/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600315 * dev_read_addr() - Get the reg property of a device
316 *
317 * @dev: Device to read from
318 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100319 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glassf11c7ab2017-05-18 20:09:03 -0600320 */
Simon Glass88b3a372020-01-27 08:49:40 -0700321fdt_addr_t dev_read_addr(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600322
323/**
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200324 * dev_read_addr_ptr() - Get the reg property of a device
325 * as a pointer
326 *
327 * @dev: Device to read from
328 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100329 * Return: pointer or NULL if not found
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200330 */
Simon Glass88b3a372020-01-27 08:49:40 -0700331void *dev_read_addr_ptr(const struct udevice *dev);
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200332
333/**
Simon Glass33c215a2019-09-15 12:08:58 -0600334 * dev_read_addr_pci() - Read an address and handle PCI address translation
335 *
336 * At present U-Boot does not have address translation logic for PCI in the
337 * livetree implementation (of_addr.c). This special function supports this for
338 * the flat tree implementation.
339 *
340 * This function should be removed (and code should use dev_read() instead)
341 * once:
342 *
343 * 1. PCI address translation is added; and either
344 * 2. everything uses livetree where PCI translation is used (which is feasible
345 * in SPL and U-Boot proper) or PCI address translation is added to
346 * fdtdec_get_addr() and friends.
347 *
348 * @dev: Device to read from
Simon Glassf69d3d62023-09-26 08:14:58 -0600349 * @sizep: If non-NULL, returns size of address space found
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100350 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glass33c215a2019-09-15 12:08:58 -0600351 */
Simon Glassf69d3d62023-09-26 08:14:58 -0600352fdt_addr_t dev_read_addr_pci(const struct udevice *dev, fdt_size_t *sizep);
Simon Glass33c215a2019-09-15 12:08:58 -0600353
354/**
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200355 * dev_remap_addr() - Get the reg property of a device as a
356 * memory-mapped I/O pointer
357 *
358 * @dev: Device to read from
359 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100360 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200361 */
Simon Glass88b3a372020-01-27 08:49:40 -0700362void *dev_remap_addr(const struct udevice *dev);
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200363
364/**
John Keeping77224322023-06-01 15:11:19 +0100365 * dev_read_addr_size() - Get the reg property of a device
Simon Glassf11c7ab2017-05-18 20:09:03 -0600366 *
367 * @dev: Device to read from
Simon Glassf11c7ab2017-05-18 20:09:03 -0600368 * @sizep: place to put size value (on success)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100369 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glassf11c7ab2017-05-18 20:09:03 -0600370 */
John Keeping77224322023-06-01 15:11:19 +0100371fdt_addr_t dev_read_addr_size(const struct udevice *dev, fdt_size_t *sizep);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600372
373/**
374 * dev_read_name() - get the name of a device's node
375 *
Bin Meng15d61d02019-07-05 09:23:18 -0700376 * @dev: Device to read from
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100377 * Return: name of node
Simon Glassf11c7ab2017-05-18 20:09:03 -0600378 */
Simon Glass88b3a372020-01-27 08:49:40 -0700379const char *dev_read_name(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600380
381/**
382 * dev_read_stringlist_search() - find string in a string list and return index
383 *
384 * Note that it is possible for this function to succeed on property values
385 * that are not NUL-terminated. That's because the function will stop after
386 * finding the first occurrence of @string. This can for example happen with
387 * small-valued cell properties, such as #address-cells, when searching for
388 * the empty string.
389 *
390 * @dev: device to check
391 * @propname: name of the property containing the string list
392 * @string: string to look up in the string list
393 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100394 * Return:
Simon Glassf11c7ab2017-05-18 20:09:03 -0600395 * the index of the string in the list of strings
396 * -ENODATA if the property is not found
397 * -EINVAL on some other error
398 */
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100399int dev_read_stringlist_search(const struct udevice *dev, const char *propname,
Simon Glass88b3a372020-01-27 08:49:40 -0700400 const char *string);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600401
402/**
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200403 * dev_read_string_index() - obtain an indexed string from a string list
404 *
405 * @dev: device to examine
406 * @propname: name of the property containing the string list
407 * @index: index of the string to return
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100408 * @outp: return location for the string
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200409 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100410 * Return:
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200411 * length of string, if found or -ve error value if not found
412 */
Simon Glass88b3a372020-01-27 08:49:40 -0700413int dev_read_string_index(const struct udevice *dev, const char *propname,
414 int index, const char **outp);
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200415
416/**
417 * dev_read_string_count() - find the number of strings in a string list
418 *
419 * @dev: device to examine
420 * @propname: name of the property containing the string list
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100421 * Return:
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200422 * number of strings in the list, or -ve error value if not found
423 */
Simon Glass88b3a372020-01-27 08:49:40 -0700424int dev_read_string_count(const struct udevice *dev, const char *propname);
Simon Glass075bfc92021-10-23 17:26:07 -0600425
426/**
427 * dev_read_string_list() - read a list of strings
428 *
429 * This produces a list of string pointers with each one pointing to a string
430 * in the string list. If the property does not exist, it returns {NULL}.
431 *
432 * The data is allocated and the caller is reponsible for freeing the return
433 * value (the list of string pointers). The strings themselves may not be
434 * changed as they point directly into the devicetree property.
435 *
436 * @dev: device to examine
437 * @propname: name of the property containing the string list
438 * @listp: returns an allocated, NULL-terminated list of strings if the return
439 * value is > 0, else is set to NULL
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100440 * Return:
441 * number of strings in list, 0 if none, -ENOMEM if out of memory,
442 * -ENOENT if no such property
Simon Glass075bfc92021-10-23 17:26:07 -0600443 */
444int dev_read_string_list(const struct udevice *dev, const char *propname,
445 const char ***listp);
446
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200447/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600448 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
449 *
450 * This function is useful to parse lists of phandles and their arguments.
451 * Returns 0 on success and fills out_args, on error returns appropriate
452 * errno value.
453 *
454 * Caller is responsible to call of_node_put() on the returned out_args->np
455 * pointer.
456 *
457 * Example:
458 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100459 * .. code-block::
Simon Glassf11c7ab2017-05-18 20:09:03 -0600460 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100461 * phandle1: node1 {
462 * #list-cells = <2>;
463 * };
464 * phandle2: node2 {
465 * #list-cells = <1>;
466 * };
467 * node3 {
468 * list = <&phandle1 1 2 &phandle2 3>;
469 * };
Simon Glassf11c7ab2017-05-18 20:09:03 -0600470 *
471 * To get a device_node of the `node2' node you may call this:
472 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
473 *
474 * @dev: device whose node containing a list
475 * @list_name: property name that contains a list
476 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100477 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glassf11c7ab2017-05-18 20:09:03 -0600478 * @index: index of a phandle to parse out
479 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100480 * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if
Simon Glassf11c7ab2017-05-18 20:09:03 -0600481 * @list_name does not exist, -EINVAL if a phandle was not found,
482 * @cells_name could not be found, the arguments were truncated or there
483 * were too many arguments.
484 */
Simon Glass88b3a372020-01-27 08:49:40 -0700485int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
486 const char *cells_name, int cell_count,
487 int index, struct ofnode_phandle_args *out_args);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600488
489/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200490 * dev_count_phandle_with_args() - Return phandle number in a list
491 *
492 * This function is usefull to get phandle number contained in a property list.
493 * For example, this allows to allocate the right amount of memory to keep
494 * clock's reference contained into the "clocks" property.
495 *
Patrice Chotard642346a2017-07-18 11:57:08 +0200496 * @dev: device whose node containing a list
497 * @list_name: property name that contains a list
498 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100499 * @cell_count: Cell count to use if @cells_name is NULL
500 * Return: number of phandle found on success, on error returns appropriate
Patrice Chotard642346a2017-07-18 11:57:08 +0200501 * errno value.
502 */
503
Simon Glass88b3a372020-01-27 08:49:40 -0700504int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200505 const char *list_name, const char *cells_name,
506 int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200507
508/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600509 * dev_read_addr_cells() - Get the number of address cells for a device's node
510 *
511 * This walks back up the tree to find the closest #address-cells property
512 * which controls the given node.
513 *
Mario Six7ba50412018-01-15 11:09:36 +0100514 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100515 * Return: number of address cells this node uses
Simon Glassf11c7ab2017-05-18 20:09:03 -0600516 */
Simon Glass88b3a372020-01-27 08:49:40 -0700517int dev_read_addr_cells(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600518
519/**
520 * dev_read_size_cells() - Get the number of size cells for a device's node
521 *
522 * This walks back up the tree to find the closest #size-cells property
523 * which controls the given node.
524 *
Mario Six7ba50412018-01-15 11:09:36 +0100525 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100526 * Return: number of size cells this node uses
Simon Glassf11c7ab2017-05-18 20:09:03 -0600527 */
Simon Glass88b3a372020-01-27 08:49:40 -0700528int dev_read_size_cells(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600529
530/**
Simon Glass878d68c2017-06-12 06:21:31 -0600531 * dev_read_addr_cells() - Get the address cells property in a node
532 *
533 * This function matches fdt_address_cells().
534 *
Mario Six7ba50412018-01-15 11:09:36 +0100535 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100536 * Return: number of address cells this node uses
Simon Glass878d68c2017-06-12 06:21:31 -0600537 */
Simon Glass88b3a372020-01-27 08:49:40 -0700538int dev_read_simple_addr_cells(const struct udevice *dev);
Simon Glass878d68c2017-06-12 06:21:31 -0600539
540/**
541 * dev_read_size_cells() - Get the size cells property in a node
542 *
543 * This function matches fdt_size_cells().
544 *
Mario Six7ba50412018-01-15 11:09:36 +0100545 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100546 * Return: number of size cells this node uses
Simon Glass878d68c2017-06-12 06:21:31 -0600547 */
Simon Glass88b3a372020-01-27 08:49:40 -0700548int dev_read_simple_size_cells(const struct udevice *dev);
Simon Glass878d68c2017-06-12 06:21:31 -0600549
550/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600551 * dev_read_phandle() - Get the phandle from a device
552 *
553 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100554 * Return: phandle (1 or greater), or 0 if no phandle or other error
Simon Glassf11c7ab2017-05-18 20:09:03 -0600555 */
Simon Glass88b3a372020-01-27 08:49:40 -0700556int dev_read_phandle(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600557
558/**
559 * dev_read_prop()- - read a property from a device's node
560 *
561 * @dev: device to check
562 * @propname: property to read
563 * @lenp: place to put length on success
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100564 * Return: pointer to property, or NULL if not found
Simon Glassf11c7ab2017-05-18 20:09:03 -0600565 */
Simon Glass88b3a372020-01-27 08:49:40 -0700566const void *dev_read_prop(const struct udevice *dev, const char *propname,
567 int *lenp);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600568
569/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100570 * dev_read_first_prop()- get the reference of the first property
571 *
572 * Get reference to the first property of the node, it is used to iterate
573 * and read all the property with dev_read_prop_by_prop().
574 *
575 * @dev: device to check
576 * @prop: place to put argument reference
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100577 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100578 */
579int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
580
581/**
Simon Glass4b1f5712022-09-06 20:27:13 -0600582 * ofnode_next_property() - get the reference of the next property
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100583 *
584 * Get reference to the next property of the node, it is used to iterate
585 * and read all the property with dev_read_prop_by_prop().
586 *
587 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100588 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100589 */
590int dev_read_next_prop(struct ofprop *prop);
591
592/**
593 * dev_read_prop_by_prop() - get a pointer to the value of a property
594 *
595 * Get value for the property identified by the provided reference.
596 *
597 * @prop: reference on property
598 * @propname: If non-NULL, place to property name on success,
599 * @lenp: If non-NULL, place to put length on success
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100600 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100601 */
602const void *dev_read_prop_by_prop(struct ofprop *prop,
603 const char **propname, int *lenp);
604
605/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600606 * dev_read_alias_seq() - Get the alias sequence number of a node
607 *
608 * This works out whether a node is pointed to by an alias, and if so, the
609 * sequence number of that alias. Aliases are of the form <base><num> where
610 * <num> is the sequence number. For example spi2 would be sequence number 2.
611 *
612 * @dev: device to look up
613 * @devnump: set to the sequence number if one is found
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100614 * Return: 0 if a sequence was found, -ve if not
Simon Glassf11c7ab2017-05-18 20:09:03 -0600615 */
Simon Glass88b3a372020-01-27 08:49:40 -0700616int dev_read_alias_seq(const struct udevice *dev, int *devnump);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600617
618/**
619 * dev_read_u32_array() - Find and read an array of 32 bit integers
620 *
621 * Search for a property in a device node and read 32-bit value(s) from
622 * it.
623 *
624 * The out_values is modified only if a valid u32 value can be decoded.
625 *
626 * @dev: device to look up
627 * @propname: name of the property to read
628 * @out_values: pointer to return value, modified only if return value is 0
629 * @sz: number of array elements to read
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100630 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
Simon Glassf11c7ab2017-05-18 20:09:03 -0600631 * property does not have a value, and -EOVERFLOW if the property data isn't
632 * large enough.
633 */
Simon Glass88b3a372020-01-27 08:49:40 -0700634int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glass47a0fd32017-05-18 20:09:04 -0600635 u32 *out_values, size_t sz);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600636
637/**
638 * dev_read_first_subnode() - find the first subnode of a device's node
639 *
640 * @dev: device to look up
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100641 * Return: reference to the first subnode (which can be invalid if the device's
Simon Glassf11c7ab2017-05-18 20:09:03 -0600642 * node has no subnodes)
643 */
Simon Glass88b3a372020-01-27 08:49:40 -0700644ofnode dev_read_first_subnode(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600645
646/**
647 * ofnode_next_subnode() - find the next sibling of a subnode
648 *
649 * @node: valid reference to previous node (sibling)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100650 * Return: reference to the next subnode (which can be invalid if the node
Simon Glassf11c7ab2017-05-18 20:09:03 -0600651 * has no more siblings)
652 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600653ofnode dev_read_next_subnode(ofnode node);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600654
655/**
656 * dev_read_u8_array_ptr() - find an 8-bit array
657 *
658 * Look up a device's node property and return a pointer to its contents as a
659 * byte array of given length. The property must have at least enough data
660 * for the array (count bytes). It may have more, but this will be ignored.
661 * The data is not copied.
662 *
663 * @dev: device to look up
664 * @propname: name of property to find
665 * @sz: number of array elements
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100666 * Return:
667 * pointer to byte array if found, or NULL if the property is not found or
668 * there is not enough data
Simon Glassf11c7ab2017-05-18 20:09:03 -0600669 */
Simon Glass88b3a372020-01-27 08:49:40 -0700670const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
671 const char *propname, size_t sz);
Simon Glass47a0fd32017-05-18 20:09:04 -0600672
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600673/**
674 * dev_read_enabled() - check whether a node is enabled
675 *
676 * This looks for a 'status' property. If this exists, then returns 1 if
677 * the status is 'ok' and 0 otherwise. If there is no status property,
678 * it returns 1 on the assumption that anything mentioned should be enabled
679 * by default.
680 *
681 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100682 * Return: integer value 0 (not enabled) or 1 (enabled)
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600683 */
Simon Glass88b3a372020-01-27 08:49:40 -0700684int dev_read_enabled(const struct udevice *dev);
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600685
Simon Glassdcf98852017-07-25 08:29:55 -0600686/**
687 * dev_read_resource() - obtain an indexed resource from a device.
688 *
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900689 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100690 * @index: index of the resource to retrieve (0 = first)
691 * @res: returns the resource
692 * Return: 0 if ok, negative on error
Simon Glassdcf98852017-07-25 08:29:55 -0600693 */
Simon Glass88b3a372020-01-27 08:49:40 -0700694int dev_read_resource(const struct udevice *dev, uint index,
695 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600696
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900697/**
698 * dev_read_resource_byname() - obtain a named resource from a device.
699 *
700 * @dev: device to examine
701 * @name: name of the resource to retrieve
702 * @res: returns the resource
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100703 * Return: 0 if ok, negative on error
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900704 */
Simon Glass88b3a372020-01-27 08:49:40 -0700705int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900706 struct resource *res);
707
Mario Six147c6072018-01-15 11:07:19 +0100708/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200709 * dev_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100710 *
711 * Translate an address from the device-tree into a CPU physical address. This
712 * function walks up the tree and applies the various bus mappings along the
713 * way.
714 *
715 * @dev: device giving the context in which to translate the address
716 * @in_addr: pointer to the address to translate
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100717 * Return: the translated address; OF_BAD_ADDR on error
Mario Six147c6072018-01-15 11:07:19 +0100718 */
Simon Glass88b3a372020-01-27 08:49:40 -0700719u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
Michal Simek83e4c7e2019-01-31 16:30:59 +0100720
721/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200722 * dev_translate_dma_address() - Translate a device-tree DMA address
723 *
724 * Translate a DMA address from the device-tree into a CPU physical address.
725 * This function walks up the tree and applies the various bus mappings along
726 * the way.
727 *
728 * @dev: device giving the context in which to translate the DMA address
729 * @in_addr: pointer to the DMA address to translate
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100730 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne641067f2019-05-31 15:11:30 +0200731 */
Simon Glass88b3a372020-01-27 08:49:40 -0700732u64 dev_translate_dma_address(const struct udevice *dev,
733 const fdt32_t *in_addr);
Fabien Dessenne641067f2019-05-31 15:11:30 +0200734
735/**
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +0100736 * dev_get_dma_range() - Get a device's DMA constraints
737 *
738 * Provide the address bases and size of the linear mapping between the CPU and
739 * a device's BUS address space.
740 *
741 * @dev: device giving the context in which to translate the DMA address
742 * @cpu: base address for CPU's view of memory
743 * @bus: base address for BUS's view of memory
744 * @size: size of the address space
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100745 * Return: 0 if ok, negative on error
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +0100746 */
747int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
748 dma_addr_t *bus, u64 *size);
749
750/**
Michal Simek83e4c7e2019-01-31 16:30:59 +0100751 * dev_read_alias_highest_id - Get highest alias id for the given stem
752 * @stem: Alias stem to be examined
753 *
754 * The function travels the lookup table to get the highest alias id for the
755 * given alias stem.
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100756 * Return: alias ID, if found, else -1
Michal Simek83e4c7e2019-01-31 16:30:59 +0100757 */
758int dev_read_alias_highest_id(const char *stem);
759
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200760/**
761 * dev_get_child_count() - get the child count of a device
762 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100763 * @dev: device to use for interation (`struct udevice *`)
764 * Return: the count of child subnode
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200765 */
766int dev_get_child_count(const struct udevice *dev);
767
Stefan Roese68f81b82020-08-05 13:56:11 +0200768/**
769 * dev_read_pci_bus_range - Read PCI bus-range resource
770 *
771 * Look at the bus range property of a device node and return the pci bus
772 * range for this node.
773 *
774 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100775 * @res: returns the resource
776 * Return: 0 if ok, negative on error
Stefan Roese68f81b82020-08-05 13:56:11 +0200777 */
778int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
779
Dario Binacchi15daa482020-12-30 00:16:26 +0100780/**
781 * dev_decode_display_timing() - decode display timings
782 *
783 * Decode display timings from the supplied 'display-timings' node.
784 * See doc/device-tree-bindings/video/display-timing.txt for binding
785 * information.
786 *
787 * @dev: device to read DT display timings from. The node linked to the device
788 * contains a child node called 'display-timings' which in turn contains
789 * one or more display timing nodes.
790 * @index: index number to read (0=first timing subnode)
791 * @config: place to put timings
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100792 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Dario Binacchi15daa482020-12-30 00:16:26 +0100793 */
794int dev_decode_display_timing(const struct udevice *dev, int index,
795 struct display_timing *config);
796
Marek BehĂșnf3dd2132022-04-07 00:32:57 +0200797/**
Nikhil M Jain0347cc72023-01-31 15:35:14 +0530798 * dev_decode_panel_timing() - decode panel timings
799 *
800 * Decode display timings from the supplied 'panel-timings' node.
801 *
802 * @dev: device to read DT display timings from. The node linked to the device
803 * contains a child node called 'display-timings' which in turn contains
804 * one or more display timing nodes.
805 * @config: place to put timings
806 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
807 */
808int dev_decode_panel_timing(const struct udevice *dev,
809 struct display_timing *config);
810
811/**
Marek BehĂșnf3dd2132022-04-07 00:32:57 +0200812 * dev_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
813 *
814 * This function parses PHY handle from the Ethernet controller's ofnode
815 * (trying all possible PHY handle property names), and returns the PHY ofnode.
816 *
817 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
818 * if the result to that is true, this function should not be called.
819 *
820 * @dev: device representing the MAC
821 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
822 */
823ofnode dev_get_phy_node(const struct udevice *dev);
824
Marek BehĂșn123ca112022-04-07 00:33:01 +0200825/**
826 * dev_read_phy_mode() - Read PHY connection type from a MAC
827 *
828 * This function parses the "phy-mode" / "phy-connection-type" property and
829 * returns the corresponding PHY interface type.
830 *
831 * @dev: device representing the MAC
Marek BehĂșnffb0f6f2022-04-07 00:33:03 +0200832 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
Marek BehĂșn123ca112022-04-07 00:33:01 +0200833 * error
834 */
835phy_interface_t dev_read_phy_mode(const struct udevice *dev);
836
Simon Glass47a0fd32017-05-18 20:09:04 -0600837#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
Simon Glass401d1c42020-10-30 21:38:53 -0600838#include <asm/global_data.h>
Simon Glass47a0fd32017-05-18 20:09:04 -0600839
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +0200840static inline int dev_read_u8(const struct udevice *dev,
841 const char *propname, u8 *outp)
842{
843 return ofnode_read_u8(dev_ofnode(dev), propname, outp);
844}
845
846static inline int dev_read_u8_default(const struct udevice *dev,
847 const char *propname, u8 def)
848{
849 return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
850}
851
852static inline int dev_read_u16(const struct udevice *dev,
853 const char *propname, u16 *outp)
854{
855 return ofnode_read_u16(dev_ofnode(dev), propname, outp);
856}
857
858static inline int dev_read_u16_default(const struct udevice *dev,
859 const char *propname, u16 def)
860{
861 return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
862}
863
Simon Glass88b3a372020-01-27 08:49:40 -0700864static inline int dev_read_u32(const struct udevice *dev,
Masahiro Yamada3ab48f62017-12-30 02:00:05 +0900865 const char *propname, u32 *outp)
866{
867 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
868}
869
Simon Glass88b3a372020-01-27 08:49:40 -0700870static inline int dev_read_u32_default(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600871 const char *propname, int def)
872{
873 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
874}
875
Dario Binacchi4bb70752020-03-29 18:04:41 +0200876static inline int dev_read_u32_index(struct udevice *dev,
877 const char *propname, int index, u32 *outp)
878{
879 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
880}
881
882static inline u32 dev_read_u32_index_default(struct udevice *dev,
883 const char *propname, int index,
884 u32 def)
885{
886 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
887 def);
888}
889
Simon Glass88b3a372020-01-27 08:49:40 -0700890static inline int dev_read_s32(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700891 const char *propname, s32 *outp)
892{
893 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
894}
895
Simon Glass88b3a372020-01-27 08:49:40 -0700896static inline int dev_read_s32_default(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700897 const char *propname, int def)
898{
899 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
900}
901
Simon Glass88b3a372020-01-27 08:49:40 -0700902static inline int dev_read_u32u(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700903 const char *propname, uint *outp)
904{
905 u32 val;
906 int ret;
907
908 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
909 if (ret)
910 return ret;
911 *outp = val;
912
913 return 0;
914}
915
Simon Glass88b3a372020-01-27 08:49:40 -0700916static inline int dev_read_u64(const struct udevice *dev,
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200917 const char *propname, u64 *outp)
918{
919 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
920}
921
Simon Glass88b3a372020-01-27 08:49:40 -0700922static inline u64 dev_read_u64_default(const struct udevice *dev,
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200923 const char *propname, u64 def)
924{
925 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
926}
927
Simon Glass88b3a372020-01-27 08:49:40 -0700928static inline const char *dev_read_string(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600929 const char *propname)
930{
931 return ofnode_read_string(dev_ofnode(dev), propname);
932}
933
Simon Glass88b3a372020-01-27 08:49:40 -0700934static inline bool dev_read_bool(const struct udevice *dev,
935 const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600936{
937 return ofnode_read_bool(dev_ofnode(dev), propname);
938}
939
Simon Glass88b3a372020-01-27 08:49:40 -0700940static inline ofnode dev_read_subnode(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600941 const char *subbnode_name)
942{
943 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
944}
945
Simon Glass88b3a372020-01-27 08:49:40 -0700946static inline int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600947{
948 return ofnode_read_size(dev_ofnode(dev), propname);
949}
950
Simon Glass88b3a372020-01-27 08:49:40 -0700951static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
952 int index)
Simon Glass47a0fd32017-05-18 20:09:04 -0600953{
954 return devfdt_get_addr_index(dev, index);
955}
956
Bin Mengbdce9032021-09-12 11:15:13 +0800957static inline void *dev_read_addr_index_ptr(const struct udevice *dev,
958 int index)
959{
960 return devfdt_get_addr_index_ptr(dev, index);
961}
962
Simon Glass88b3a372020-01-27 08:49:40 -0700963static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
Sekhar Norif5b90472019-08-01 19:12:56 +0530964 int index,
965 fdt_size_t *size)
966{
967 return devfdt_get_addr_size_index(dev, index, size);
968}
969
Jonas Karlman5e030632023-07-22 13:30:15 +0000970static inline void *dev_read_addr_size_index_ptr(const struct udevice *dev,
971 int index,
972 fdt_size_t *size)
973{
974 return devfdt_get_addr_size_index_ptr(dev, index, size);
975}
976
Simon Glass88b3a372020-01-27 08:49:40 -0700977static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100978 const char *name)
979{
980 return devfdt_get_addr_name(dev, name);
981}
982
Simon Glass88b3a372020-01-27 08:49:40 -0700983static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
Sekhar Norif5b90472019-08-01 19:12:56 +0530984 const char *name,
985 fdt_size_t *size)
986{
987 return devfdt_get_addr_size_name(dev, name, size);
988}
989
Simon Glass88b3a372020-01-27 08:49:40 -0700990static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600991{
992 return devfdt_get_addr(dev);
993}
994
Simon Glass88b3a372020-01-27 08:49:40 -0700995static inline void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200996{
Ovidiu Panait3fe69d32020-08-03 22:17:35 +0300997 return devfdt_get_addr_ptr(dev);
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200998}
999
Simon Glassf69d3d62023-09-26 08:14:58 -06001000static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev,
1001 fdt_size_t *sizep)
Simon Glass33c215a2019-09-15 12:08:58 -06001002{
Simon Glassf69d3d62023-09-26 08:14:58 -06001003 return devfdt_get_addr_pci(dev, sizep);
Simon Glass33c215a2019-09-15 12:08:58 -06001004}
1005
Simon Glass88b3a372020-01-27 08:49:40 -07001006static inline void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +02001007{
1008 return devfdt_remap_addr(dev);
1009}
1010
Simon Glass88b3a372020-01-27 08:49:40 -07001011static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +02001012{
1013 return devfdt_remap_addr_index(dev, index);
1014}
1015
Simon Glass88b3a372020-01-27 08:49:40 -07001016static inline void *dev_remap_addr_name(const struct udevice *dev,
1017 const char *name)
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +01001018{
1019 return devfdt_remap_addr_name(dev, name);
1020}
1021
Simon Glass88b3a372020-01-27 08:49:40 -07001022static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -06001023 fdt_size_t *sizep)
1024{
John Keeping77224322023-06-01 15:11:19 +01001025 return dev_read_addr_size_index(dev, 0, sizep);
Simon Glass47a0fd32017-05-18 20:09:04 -06001026}
1027
Simon Glass88b3a372020-01-27 08:49:40 -07001028static inline const char *dev_read_name(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001029{
1030 return ofnode_get_name(dev_ofnode(dev));
1031}
1032
Simon Glass88b3a372020-01-27 08:49:40 -07001033static inline int dev_read_stringlist_search(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -06001034 const char *propname,
1035 const char *string)
1036{
1037 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
1038}
1039
Simon Glass88b3a372020-01-27 08:49:40 -07001040static inline int dev_read_string_index(const struct udevice *dev,
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +02001041 const char *propname, int index,
1042 const char **outp)
1043{
1044 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
1045}
1046
Simon Glass88b3a372020-01-27 08:49:40 -07001047static inline int dev_read_string_count(const struct udevice *dev,
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +02001048 const char *propname)
1049{
1050 return ofnode_read_string_count(dev_ofnode(dev), propname);
1051}
1052
Simon Glass075bfc92021-10-23 17:26:07 -06001053static inline int dev_read_string_list(const struct udevice *dev,
1054 const char *propname,
1055 const char ***listp)
1056{
1057 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
1058}
1059
Simon Glass88b3a372020-01-27 08:49:40 -07001060static inline int dev_read_phandle_with_args(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -06001061 const char *list_name, const char *cells_name, int cell_count,
1062 int index, struct ofnode_phandle_args *out_args)
1063{
1064 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
1065 cells_name, cell_count, index,
1066 out_args);
1067}
1068
Simon Glass88b3a372020-01-27 08:49:40 -07001069static inline int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunay89f68302020-09-25 09:41:14 +02001070 const char *list_name, const char *cells_name, int cell_count)
Patrice Chotard642346a2017-07-18 11:57:08 +02001071{
1072 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +02001073 cells_name, cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +02001074}
1075
Simon Glass88b3a372020-01-27 08:49:40 -07001076static inline int dev_read_addr_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001077{
Heinrich Schuchardtae6b33d2020-07-25 21:38:49 +02001078 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1079
1080 return fdt_address_cells(gd->fdt_blob, parent);
Simon Glass47a0fd32017-05-18 20:09:04 -06001081}
1082
Simon Glass88b3a372020-01-27 08:49:40 -07001083static inline int dev_read_size_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001084{
Heinrich Schuchardtae6b33d2020-07-25 21:38:49 +02001085 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1086
1087 return fdt_size_cells(gd->fdt_blob, parent);
Simon Glass878d68c2017-06-12 06:21:31 -06001088}
1089
Simon Glass88b3a372020-01-27 08:49:40 -07001090static inline int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -06001091{
1092 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
1093}
1094
Simon Glass88b3a372020-01-27 08:49:40 -07001095static inline int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -06001096{
Simon Glass47a0fd32017-05-18 20:09:04 -06001097 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
1098}
1099
Simon Glass88b3a372020-01-27 08:49:40 -07001100static inline int dev_read_phandle(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001101{
1102 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
1103}
1104
Simon Glass88b3a372020-01-27 08:49:40 -07001105static inline const void *dev_read_prop(const struct udevice *dev,
Masahiro Yamadafd736212017-07-17 12:18:39 +09001106 const char *propname, int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -06001107{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +09001108 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -06001109}
1110
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001111static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
1112{
Simon Glass4b1f5712022-09-06 20:27:13 -06001113 return ofnode_first_property(dev_ofnode(dev), prop);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001114}
1115
1116static inline int dev_read_next_prop(struct ofprop *prop)
1117{
Simon Glass4b1f5712022-09-06 20:27:13 -06001118 return ofnode_next_property(prop);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001119}
1120
1121static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
1122 const char **propname,
1123 int *lenp)
1124{
Simon Glass92432242022-09-06 20:27:14 -06001125 return ofprop_get_property(prop, propname, lenp);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001126}
1127
Simon Glass88b3a372020-01-27 08:49:40 -07001128static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glass47a0fd32017-05-18 20:09:04 -06001129{
Marcel Ziswiler45224e82019-05-20 02:44:57 +02001130#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass47a0fd32017-05-18 20:09:04 -06001131 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
1132 dev_of_offset(dev), devnump);
Marcel Ziswiler45224e82019-05-20 02:44:57 +02001133#else
1134 return -ENOTSUPP;
1135#endif
Simon Glass47a0fd32017-05-18 20:09:04 -06001136}
1137
Simon Glass88b3a372020-01-27 08:49:40 -07001138static inline int dev_read_u32_array(const struct udevice *dev,
1139 const char *propname, u32 *out_values,
1140 size_t sz)
Simon Glass47a0fd32017-05-18 20:09:04 -06001141{
1142 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
1143}
1144
Simon Glass88b3a372020-01-27 08:49:40 -07001145static inline ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001146{
1147 return ofnode_first_subnode(dev_ofnode(dev));
1148}
1149
1150static inline ofnode dev_read_next_subnode(ofnode node)
1151{
1152 return ofnode_next_subnode(node);
1153}
1154
Simon Glass88b3a372020-01-27 08:49:40 -07001155static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
Simon Glassf262d4c2020-01-27 08:49:47 -07001156 const char *propname,
1157 size_t sz)
Simon Glassf11c7ab2017-05-18 20:09:03 -06001158{
1159 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
1160}
1161
Simon Glass88b3a372020-01-27 08:49:40 -07001162static inline int dev_read_enabled(const struct udevice *dev)
Simon Glassf7d6fcf2017-06-12 06:21:30 -06001163{
1164 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
1165}
1166
Simon Glass88b3a372020-01-27 08:49:40 -07001167static inline int dev_read_resource(const struct udevice *dev, uint index,
Simon Glassdcf98852017-07-25 08:29:55 -06001168 struct resource *res)
1169{
1170 return ofnode_read_resource(dev_ofnode(dev), index, res);
1171}
1172
Simon Glass88b3a372020-01-27 08:49:40 -07001173static inline int dev_read_resource_byname(const struct udevice *dev,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +09001174 const char *name,
1175 struct resource *res)
1176{
1177 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
1178}
1179
Simon Glass88b3a372020-01-27 08:49:40 -07001180static inline u64 dev_translate_address(const struct udevice *dev,
1181 const fdt32_t *in_addr)
Mario Six147c6072018-01-15 11:07:19 +01001182{
1183 return ofnode_translate_address(dev_ofnode(dev), in_addr);
1184}
1185
Simon Glass88b3a372020-01-27 08:49:40 -07001186static inline u64 dev_translate_dma_address(const struct udevice *dev,
1187 const fdt32_t *in_addr)
Fabien Dessenne641067f2019-05-31 15:11:30 +02001188{
1189 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1190}
1191
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001192static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
1193 dma_addr_t *bus, u64 *size)
1194{
1195 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
1196}
1197
Michal Simek83e4c7e2019-01-31 16:30:59 +01001198static inline int dev_read_alias_highest_id(const char *stem)
1199{
Stanislav Pinchuka00e0f72021-01-20 21:52:23 +03001200 if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
Michael Walle0a6b75f2020-06-02 01:47:08 +02001201 return -1;
Michal Simek83e4c7e2019-01-31 16:30:59 +01001202 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1203}
1204
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001205static inline int dev_get_child_count(const struct udevice *dev)
1206{
1207 return ofnode_get_child_count(dev_ofnode(dev));
1208}
1209
Dario Binacchi15daa482020-12-30 00:16:26 +01001210static inline int dev_decode_display_timing(const struct udevice *dev,
1211 int index,
1212 struct display_timing *config)
1213{
1214 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
1215}
1216
Nikhil M Jain0347cc72023-01-31 15:35:14 +05301217static inline int dev_decode_panel_timing(const struct udevice *dev,
1218 struct display_timing *config)
1219{
1220 return ofnode_decode_panel_timing(dev_ofnode(dev), config);
1221}
1222
Marek BehĂșnf3dd2132022-04-07 00:32:57 +02001223static inline ofnode dev_get_phy_node(const struct udevice *dev)
1224{
1225 return ofnode_get_phy_node(dev_ofnode(dev));
1226}
1227
Marek BehĂșn123ca112022-04-07 00:33:01 +02001228static inline phy_interface_t dev_read_phy_mode(const struct udevice *dev)
1229{
1230 return ofnode_read_phy_mode(dev_ofnode(dev));
1231}
1232
Simon Glassf11c7ab2017-05-18 20:09:03 -06001233#endif /* CONFIG_DM_DEV_READ_INLINE */
1234
1235/**
1236 * dev_for_each_subnode() - Helper function to iterate through subnodes
1237 *
1238 * This creates a for() loop which works through the subnodes in a device's
1239 * device-tree node.
1240 *
1241 * @subnode: ofnode holding the current subnode
Patrick Delaunay6de6a612022-01-12 10:53:48 +01001242 * @dev: device to use for interation (`struct udevice *`)
Simon Glassf11c7ab2017-05-18 20:09:03 -06001243 */
1244#define dev_for_each_subnode(subnode, dev) \
1245 for (subnode = dev_read_first_subnode(dev); \
1246 ofnode_valid(subnode); \
1247 subnode = ofnode_next_subnode(subnode))
1248
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001249/**
1250 * dev_for_each_property() - Helper function to iterate through property
1251 *
1252 * This creates a for() loop which works through the property in a device's
1253 * device-tree node.
1254 *
1255 * @prop: struct ofprop holding the current property
Patrick Delaunay6de6a612022-01-12 10:53:48 +01001256 * @dev: device to use for interation (`struct udevice *`)
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001257 */
1258#define dev_for_each_property(prop, dev) \
1259 for (int ret_prop = dev_read_first_prop(dev, &prop); \
1260 !ret_prop; \
1261 ret_prop = dev_read_next_prop(&prop))
1262
Simon Glassf11c7ab2017-05-18 20:09:03 -06001263#endif