blob: 137f2a52a298d3b696d6528af5f31651bd5ca572 [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/**
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200250 * dev_remap_addr_index() - Get the indexed reg property of a device
251 * as a memory-mapped I/O 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 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100257 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200258 */
Simon Glass88b3a372020-01-27 08:49:40 -0700259void *dev_remap_addr_index(const struct udevice *dev, int index);
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200260
261/**
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100262 * dev_read_addr_name() - Get the reg property of a device, indexed by name
263 *
264 * @dev: Device to read from
265 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
266 * 'reg-names' property providing named-based identification. @index
267 * indicates the value to search for in 'reg-names'.
268 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100269 * Return: address or FDT_ADDR_T_NONE if not found
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100270 */
Simon Glass88b3a372020-01-27 08:49:40 -0700271fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100272
273/**
Sekhar Norif5b90472019-08-01 19:12:56 +0530274 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
275 *
276 * @dev: Device to read from
277 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
278 * 'reg-names' property providing named-based identification. @index
279 * indicates the value to search for in 'reg-names'.
280 * @size: place to put size value (on success)
281 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100282 * Return: address or FDT_ADDR_T_NONE if not found
Sekhar Norif5b90472019-08-01 19:12:56 +0530283 */
Simon Glass88b3a372020-01-27 08:49:40 -0700284fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif5b90472019-08-01 19:12:56 +0530285 fdt_size_t *size);
286
287/**
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100288 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
289 * as a memory-mapped I/O pointer
290 *
291 * @dev: Device to read from
292 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
293 * 'reg-names' property providing named-based identification. @index
294 * indicates the value to search for in 'reg-names'.
295 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100296 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100297 */
Simon Glass88b3a372020-01-27 08:49:40 -0700298void *dev_remap_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100299
300/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600301 * dev_read_addr() - Get the reg property of a device
302 *
303 * @dev: Device to read from
304 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100305 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glassf11c7ab2017-05-18 20:09:03 -0600306 */
Simon Glass88b3a372020-01-27 08:49:40 -0700307fdt_addr_t dev_read_addr(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600308
309/**
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200310 * dev_read_addr_ptr() - Get the reg property of a device
311 * as a pointer
312 *
313 * @dev: Device to read from
314 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100315 * Return: pointer or NULL if not found
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200316 */
Simon Glass88b3a372020-01-27 08:49:40 -0700317void *dev_read_addr_ptr(const struct udevice *dev);
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200318
319/**
Simon Glass33c215a2019-09-15 12:08:58 -0600320 * dev_read_addr_pci() - Read an address and handle PCI address translation
321 *
322 * At present U-Boot does not have address translation logic for PCI in the
323 * livetree implementation (of_addr.c). This special function supports this for
324 * the flat tree implementation.
325 *
326 * This function should be removed (and code should use dev_read() instead)
327 * once:
328 *
329 * 1. PCI address translation is added; and either
330 * 2. everything uses livetree where PCI translation is used (which is feasible
331 * in SPL and U-Boot proper) or PCI address translation is added to
332 * fdtdec_get_addr() and friends.
333 *
334 * @dev: Device to read from
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100335 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glass33c215a2019-09-15 12:08:58 -0600336 */
Simon Glass88b3a372020-01-27 08:49:40 -0700337fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
Simon Glass33c215a2019-09-15 12:08:58 -0600338
339/**
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200340 * dev_remap_addr() - Get the reg property of a device as a
341 * memory-mapped I/O pointer
342 *
343 * @dev: Device to read from
344 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100345 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200346 */
Simon Glass88b3a372020-01-27 08:49:40 -0700347void *dev_remap_addr(const struct udevice *dev);
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200348
349/**
John Keeping77224322023-06-01 15:11:19 +0100350 * dev_read_addr_size() - Get the reg property of a device
Simon Glassf11c7ab2017-05-18 20:09:03 -0600351 *
352 * @dev: Device to read from
Simon Glassf11c7ab2017-05-18 20:09:03 -0600353 * @sizep: place to put size value (on success)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100354 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glassf11c7ab2017-05-18 20:09:03 -0600355 */
John Keeping77224322023-06-01 15:11:19 +0100356fdt_addr_t dev_read_addr_size(const struct udevice *dev, fdt_size_t *sizep);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600357
358/**
359 * dev_read_name() - get the name of a device's node
360 *
Bin Meng15d61d02019-07-05 09:23:18 -0700361 * @dev: Device to read from
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100362 * Return: name of node
Simon Glassf11c7ab2017-05-18 20:09:03 -0600363 */
Simon Glass88b3a372020-01-27 08:49:40 -0700364const char *dev_read_name(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600365
366/**
367 * dev_read_stringlist_search() - find string in a string list and return index
368 *
369 * Note that it is possible for this function to succeed on property values
370 * that are not NUL-terminated. That's because the function will stop after
371 * finding the first occurrence of @string. This can for example happen with
372 * small-valued cell properties, such as #address-cells, when searching for
373 * the empty string.
374 *
375 * @dev: device to check
376 * @propname: name of the property containing the string list
377 * @string: string to look up in the string list
378 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100379 * Return:
Simon Glassf11c7ab2017-05-18 20:09:03 -0600380 * the index of the string in the list of strings
381 * -ENODATA if the property is not found
382 * -EINVAL on some other error
383 */
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100384int dev_read_stringlist_search(const struct udevice *dev, const char *propname,
Simon Glass88b3a372020-01-27 08:49:40 -0700385 const char *string);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600386
387/**
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200388 * dev_read_string_index() - obtain an indexed string from a string list
389 *
390 * @dev: device to examine
391 * @propname: name of the property containing the string list
392 * @index: index of the string to return
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100393 * @outp: return location for the string
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200394 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100395 * Return:
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200396 * length of string, if found or -ve error value if not found
397 */
Simon Glass88b3a372020-01-27 08:49:40 -0700398int dev_read_string_index(const struct udevice *dev, const char *propname,
399 int index, const char **outp);
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200400
401/**
402 * dev_read_string_count() - find the number of strings in a string list
403 *
404 * @dev: device to examine
405 * @propname: name of the property containing the string list
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100406 * Return:
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200407 * number of strings in the list, or -ve error value if not found
408 */
Simon Glass88b3a372020-01-27 08:49:40 -0700409int dev_read_string_count(const struct udevice *dev, const char *propname);
Simon Glass075bfc92021-10-23 17:26:07 -0600410
411/**
412 * dev_read_string_list() - read a list of strings
413 *
414 * This produces a list of string pointers with each one pointing to a string
415 * in the string list. If the property does not exist, it returns {NULL}.
416 *
417 * The data is allocated and the caller is reponsible for freeing the return
418 * value (the list of string pointers). The strings themselves may not be
419 * changed as they point directly into the devicetree property.
420 *
421 * @dev: device to examine
422 * @propname: name of the property containing the string list
423 * @listp: returns an allocated, NULL-terminated list of strings if the return
424 * value is > 0, else is set to NULL
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100425 * Return:
426 * number of strings in list, 0 if none, -ENOMEM if out of memory,
427 * -ENOENT if no such property
Simon Glass075bfc92021-10-23 17:26:07 -0600428 */
429int dev_read_string_list(const struct udevice *dev, const char *propname,
430 const char ***listp);
431
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +0200432/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600433 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
434 *
435 * This function is useful to parse lists of phandles and their arguments.
436 * Returns 0 on success and fills out_args, on error returns appropriate
437 * errno value.
438 *
439 * Caller is responsible to call of_node_put() on the returned out_args->np
440 * pointer.
441 *
442 * Example:
443 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100444 * .. code-block::
Simon Glassf11c7ab2017-05-18 20:09:03 -0600445 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100446 * phandle1: node1 {
447 * #list-cells = <2>;
448 * };
449 * phandle2: node2 {
450 * #list-cells = <1>;
451 * };
452 * node3 {
453 * list = <&phandle1 1 2 &phandle2 3>;
454 * };
Simon Glassf11c7ab2017-05-18 20:09:03 -0600455 *
456 * To get a device_node of the `node2' node you may call this:
457 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
458 *
459 * @dev: device whose node containing a list
460 * @list_name: property name that contains a list
461 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100462 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glassf11c7ab2017-05-18 20:09:03 -0600463 * @index: index of a phandle to parse out
464 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100465 * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if
Simon Glassf11c7ab2017-05-18 20:09:03 -0600466 * @list_name does not exist, -EINVAL if a phandle was not found,
467 * @cells_name could not be found, the arguments were truncated or there
468 * were too many arguments.
469 */
Simon Glass88b3a372020-01-27 08:49:40 -0700470int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
471 const char *cells_name, int cell_count,
472 int index, struct ofnode_phandle_args *out_args);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600473
474/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200475 * dev_count_phandle_with_args() - Return phandle number in a list
476 *
477 * This function is usefull to get phandle number contained in a property list.
478 * For example, this allows to allocate the right amount of memory to keep
479 * clock's reference contained into the "clocks" property.
480 *
Patrice Chotard642346a2017-07-18 11:57:08 +0200481 * @dev: device whose node containing a list
482 * @list_name: property name that contains a list
483 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100484 * @cell_count: Cell count to use if @cells_name is NULL
485 * Return: number of phandle found on success, on error returns appropriate
Patrice Chotard642346a2017-07-18 11:57:08 +0200486 * errno value.
487 */
488
Simon Glass88b3a372020-01-27 08:49:40 -0700489int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200490 const char *list_name, const char *cells_name,
491 int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200492
493/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600494 * dev_read_addr_cells() - Get the number of address cells for a device's node
495 *
496 * This walks back up the tree to find the closest #address-cells property
497 * which controls the given node.
498 *
Mario Six7ba50412018-01-15 11:09:36 +0100499 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100500 * Return: number of address cells this node uses
Simon Glassf11c7ab2017-05-18 20:09:03 -0600501 */
Simon Glass88b3a372020-01-27 08:49:40 -0700502int dev_read_addr_cells(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600503
504/**
505 * dev_read_size_cells() - Get the number of size cells for a device's node
506 *
507 * This walks back up the tree to find the closest #size-cells property
508 * which controls the given node.
509 *
Mario Six7ba50412018-01-15 11:09:36 +0100510 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100511 * Return: number of size cells this node uses
Simon Glassf11c7ab2017-05-18 20:09:03 -0600512 */
Simon Glass88b3a372020-01-27 08:49:40 -0700513int dev_read_size_cells(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600514
515/**
Simon Glass878d68c2017-06-12 06:21:31 -0600516 * dev_read_addr_cells() - Get the address cells property in a node
517 *
518 * This function matches fdt_address_cells().
519 *
Mario Six7ba50412018-01-15 11:09:36 +0100520 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100521 * Return: number of address cells this node uses
Simon Glass878d68c2017-06-12 06:21:31 -0600522 */
Simon Glass88b3a372020-01-27 08:49:40 -0700523int dev_read_simple_addr_cells(const struct udevice *dev);
Simon Glass878d68c2017-06-12 06:21:31 -0600524
525/**
526 * dev_read_size_cells() - Get the size cells property in a node
527 *
528 * This function matches fdt_size_cells().
529 *
Mario Six7ba50412018-01-15 11:09:36 +0100530 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100531 * Return: number of size cells this node uses
Simon Glass878d68c2017-06-12 06:21:31 -0600532 */
Simon Glass88b3a372020-01-27 08:49:40 -0700533int dev_read_simple_size_cells(const struct udevice *dev);
Simon Glass878d68c2017-06-12 06:21:31 -0600534
535/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600536 * dev_read_phandle() - Get the phandle from a device
537 *
538 * @dev: device to check
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100539 * Return: phandle (1 or greater), or 0 if no phandle or other error
Simon Glassf11c7ab2017-05-18 20:09:03 -0600540 */
Simon Glass88b3a372020-01-27 08:49:40 -0700541int dev_read_phandle(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600542
543/**
544 * dev_read_prop()- - read a property from a device's node
545 *
546 * @dev: device to check
547 * @propname: property to read
548 * @lenp: place to put length on success
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100549 * Return: pointer to property, or NULL if not found
Simon Glassf11c7ab2017-05-18 20:09:03 -0600550 */
Simon Glass88b3a372020-01-27 08:49:40 -0700551const void *dev_read_prop(const struct udevice *dev, const char *propname,
552 int *lenp);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600553
554/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100555 * dev_read_first_prop()- get the reference of the first property
556 *
557 * Get reference to the first property of the node, it is used to iterate
558 * and read all the property with dev_read_prop_by_prop().
559 *
560 * @dev: device to check
561 * @prop: place to put argument reference
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100562 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100563 */
564int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
565
566/**
Simon Glass4b1f5712022-09-06 20:27:13 -0600567 * ofnode_next_property() - get the reference of the next property
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100568 *
569 * Get reference to the next property of the node, it is used to iterate
570 * and read all the property with dev_read_prop_by_prop().
571 *
572 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100573 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100574 */
575int dev_read_next_prop(struct ofprop *prop);
576
577/**
578 * dev_read_prop_by_prop() - get a pointer to the value of a property
579 *
580 * Get value for the property identified by the provided reference.
581 *
582 * @prop: reference on property
583 * @propname: If non-NULL, place to property name on success,
584 * @lenp: If non-NULL, place to put length on success
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100585 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100586 */
587const void *dev_read_prop_by_prop(struct ofprop *prop,
588 const char **propname, int *lenp);
589
590/**
Simon Glassf11c7ab2017-05-18 20:09:03 -0600591 * dev_read_alias_seq() - Get the alias sequence number of a node
592 *
593 * This works out whether a node is pointed to by an alias, and if so, the
594 * sequence number of that alias. Aliases are of the form <base><num> where
595 * <num> is the sequence number. For example spi2 would be sequence number 2.
596 *
597 * @dev: device to look up
598 * @devnump: set to the sequence number if one is found
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100599 * Return: 0 if a sequence was found, -ve if not
Simon Glassf11c7ab2017-05-18 20:09:03 -0600600 */
Simon Glass88b3a372020-01-27 08:49:40 -0700601int dev_read_alias_seq(const struct udevice *dev, int *devnump);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600602
603/**
604 * dev_read_u32_array() - Find and read an array of 32 bit integers
605 *
606 * Search for a property in a device node and read 32-bit value(s) from
607 * it.
608 *
609 * The out_values is modified only if a valid u32 value can be decoded.
610 *
611 * @dev: device to look up
612 * @propname: name of the property to read
613 * @out_values: pointer to return value, modified only if return value is 0
614 * @sz: number of array elements to read
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100615 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
Simon Glassf11c7ab2017-05-18 20:09:03 -0600616 * property does not have a value, and -EOVERFLOW if the property data isn't
617 * large enough.
618 */
Simon Glass88b3a372020-01-27 08:49:40 -0700619int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glass47a0fd32017-05-18 20:09:04 -0600620 u32 *out_values, size_t sz);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600621
622/**
623 * dev_read_first_subnode() - find the first subnode of a device's node
624 *
625 * @dev: device to look up
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100626 * Return: reference to the first subnode (which can be invalid if the device's
Simon Glassf11c7ab2017-05-18 20:09:03 -0600627 * node has no subnodes)
628 */
Simon Glass88b3a372020-01-27 08:49:40 -0700629ofnode dev_read_first_subnode(const struct udevice *dev);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600630
631/**
632 * ofnode_next_subnode() - find the next sibling of a subnode
633 *
634 * @node: valid reference to previous node (sibling)
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100635 * Return: reference to the next subnode (which can be invalid if the node
Simon Glassf11c7ab2017-05-18 20:09:03 -0600636 * has no more siblings)
637 */
Simon Glass47a0fd32017-05-18 20:09:04 -0600638ofnode dev_read_next_subnode(ofnode node);
Simon Glassf11c7ab2017-05-18 20:09:03 -0600639
640/**
641 * dev_read_u8_array_ptr() - find an 8-bit array
642 *
643 * Look up a device's node property and return a pointer to its contents as a
644 * byte array of given length. The property must have at least enough data
645 * for the array (count bytes). It may have more, but this will be ignored.
646 * The data is not copied.
647 *
648 * @dev: device to look up
649 * @propname: name of property to find
650 * @sz: number of array elements
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100651 * Return:
652 * pointer to byte array if found, or NULL if the property is not found or
653 * there is not enough data
Simon Glassf11c7ab2017-05-18 20:09:03 -0600654 */
Simon Glass88b3a372020-01-27 08:49:40 -0700655const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
656 const char *propname, size_t sz);
Simon Glass47a0fd32017-05-18 20:09:04 -0600657
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600658/**
659 * dev_read_enabled() - check whether a node is enabled
660 *
661 * This looks for a 'status' property. If this exists, then returns 1 if
662 * the status is 'ok' and 0 otherwise. If there is no status property,
663 * it returns 1 on the assumption that anything mentioned should be enabled
664 * by default.
665 *
666 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100667 * Return: integer value 0 (not enabled) or 1 (enabled)
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600668 */
Simon Glass88b3a372020-01-27 08:49:40 -0700669int dev_read_enabled(const struct udevice *dev);
Simon Glassf7d6fcf2017-06-12 06:21:30 -0600670
Simon Glassdcf98852017-07-25 08:29:55 -0600671/**
672 * dev_read_resource() - obtain an indexed resource from a device.
673 *
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900674 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100675 * @index: index of the resource to retrieve (0 = first)
676 * @res: returns the resource
677 * Return: 0 if ok, negative on error
Simon Glassdcf98852017-07-25 08:29:55 -0600678 */
Simon Glass88b3a372020-01-27 08:49:40 -0700679int dev_read_resource(const struct udevice *dev, uint index,
680 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600681
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900682/**
683 * dev_read_resource_byname() - obtain a named resource from a device.
684 *
685 * @dev: device to examine
686 * @name: name of the resource to retrieve
687 * @res: returns the resource
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100688 * Return: 0 if ok, negative on error
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900689 */
Simon Glass88b3a372020-01-27 08:49:40 -0700690int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900691 struct resource *res);
692
Mario Six147c6072018-01-15 11:07:19 +0100693/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200694 * dev_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100695 *
696 * Translate an address from the device-tree into a CPU physical address. This
697 * function walks up the tree and applies the various bus mappings along the
698 * way.
699 *
700 * @dev: device giving the context in which to translate the address
701 * @in_addr: pointer to the address to translate
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100702 * Return: the translated address; OF_BAD_ADDR on error
Mario Six147c6072018-01-15 11:07:19 +0100703 */
Simon Glass88b3a372020-01-27 08:49:40 -0700704u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
Michal Simek83e4c7e2019-01-31 16:30:59 +0100705
706/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200707 * dev_translate_dma_address() - Translate a device-tree DMA address
708 *
709 * Translate a DMA address from the device-tree into a CPU physical address.
710 * This function walks up the tree and applies the various bus mappings along
711 * the way.
712 *
713 * @dev: device giving the context in which to translate the DMA address
714 * @in_addr: pointer to the DMA address to translate
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100715 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne641067f2019-05-31 15:11:30 +0200716 */
Simon Glass88b3a372020-01-27 08:49:40 -0700717u64 dev_translate_dma_address(const struct udevice *dev,
718 const fdt32_t *in_addr);
Fabien Dessenne641067f2019-05-31 15:11:30 +0200719
720/**
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +0100721 * dev_get_dma_range() - Get a device's DMA constraints
722 *
723 * Provide the address bases and size of the linear mapping between the CPU and
724 * a device's BUS address space.
725 *
726 * @dev: device giving the context in which to translate the DMA address
727 * @cpu: base address for CPU's view of memory
728 * @bus: base address for BUS's view of memory
729 * @size: size of the address space
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100730 * Return: 0 if ok, negative on error
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +0100731 */
732int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
733 dma_addr_t *bus, u64 *size);
734
735/**
Michal Simek83e4c7e2019-01-31 16:30:59 +0100736 * dev_read_alias_highest_id - Get highest alias id for the given stem
737 * @stem: Alias stem to be examined
738 *
739 * The function travels the lookup table to get the highest alias id for the
740 * given alias stem.
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100741 * Return: alias ID, if found, else -1
Michal Simek83e4c7e2019-01-31 16:30:59 +0100742 */
743int dev_read_alias_highest_id(const char *stem);
744
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200745/**
746 * dev_get_child_count() - get the child count of a device
747 *
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100748 * @dev: device to use for interation (`struct udevice *`)
749 * Return: the count of child subnode
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200750 */
751int dev_get_child_count(const struct udevice *dev);
752
Stefan Roese68f81b82020-08-05 13:56:11 +0200753/**
754 * dev_read_pci_bus_range - Read PCI bus-range resource
755 *
756 * Look at the bus range property of a device node and return the pci bus
757 * range for this node.
758 *
759 * @dev: device to examine
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100760 * @res: returns the resource
761 * Return: 0 if ok, negative on error
Stefan Roese68f81b82020-08-05 13:56:11 +0200762 */
763int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
764
Dario Binacchi15daa482020-12-30 00:16:26 +0100765/**
766 * dev_decode_display_timing() - decode display timings
767 *
768 * Decode display timings from the supplied 'display-timings' node.
769 * See doc/device-tree-bindings/video/display-timing.txt for binding
770 * information.
771 *
772 * @dev: device to read DT display timings from. The node linked to the device
773 * contains a child node called 'display-timings' which in turn contains
774 * one or more display timing nodes.
775 * @index: index number to read (0=first timing subnode)
776 * @config: place to put timings
Patrick Delaunay6de6a612022-01-12 10:53:48 +0100777 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Dario Binacchi15daa482020-12-30 00:16:26 +0100778 */
779int dev_decode_display_timing(const struct udevice *dev, int index,
780 struct display_timing *config);
781
Marek BehĂșnf3dd2132022-04-07 00:32:57 +0200782/**
Nikhil M Jain0347cc72023-01-31 15:35:14 +0530783 * dev_decode_panel_timing() - decode panel timings
784 *
785 * Decode display timings from the supplied 'panel-timings' node.
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 * @config: place to put timings
791 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
792 */
793int dev_decode_panel_timing(const struct udevice *dev,
794 struct display_timing *config);
795
796/**
Marek BehĂșnf3dd2132022-04-07 00:32:57 +0200797 * dev_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
798 *
799 * This function parses PHY handle from the Ethernet controller's ofnode
800 * (trying all possible PHY handle property names), and returns the PHY ofnode.
801 *
802 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
803 * if the result to that is true, this function should not be called.
804 *
805 * @dev: device representing the MAC
806 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
807 */
808ofnode dev_get_phy_node(const struct udevice *dev);
809
Marek BehĂșn123ca112022-04-07 00:33:01 +0200810/**
811 * dev_read_phy_mode() - Read PHY connection type from a MAC
812 *
813 * This function parses the "phy-mode" / "phy-connection-type" property and
814 * returns the corresponding PHY interface type.
815 *
816 * @dev: device representing the MAC
Marek BehĂșnffb0f6f2022-04-07 00:33:03 +0200817 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
Marek BehĂșn123ca112022-04-07 00:33:01 +0200818 * error
819 */
820phy_interface_t dev_read_phy_mode(const struct udevice *dev);
821
Simon Glass47a0fd32017-05-18 20:09:04 -0600822#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
Simon Glass401d1c42020-10-30 21:38:53 -0600823#include <asm/global_data.h>
Simon Glass47a0fd32017-05-18 20:09:04 -0600824
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +0200825static inline int dev_read_u8(const struct udevice *dev,
826 const char *propname, u8 *outp)
827{
828 return ofnode_read_u8(dev_ofnode(dev), propname, outp);
829}
830
831static inline int dev_read_u8_default(const struct udevice *dev,
832 const char *propname, u8 def)
833{
834 return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
835}
836
837static inline int dev_read_u16(const struct udevice *dev,
838 const char *propname, u16 *outp)
839{
840 return ofnode_read_u16(dev_ofnode(dev), propname, outp);
841}
842
843static inline int dev_read_u16_default(const struct udevice *dev,
844 const char *propname, u16 def)
845{
846 return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
847}
848
Simon Glass88b3a372020-01-27 08:49:40 -0700849static inline int dev_read_u32(const struct udevice *dev,
Masahiro Yamada3ab48f62017-12-30 02:00:05 +0900850 const char *propname, u32 *outp)
851{
852 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
853}
854
Simon Glass88b3a372020-01-27 08:49:40 -0700855static inline int dev_read_u32_default(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600856 const char *propname, int def)
857{
858 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
859}
860
Dario Binacchi4bb70752020-03-29 18:04:41 +0200861static inline int dev_read_u32_index(struct udevice *dev,
862 const char *propname, int index, u32 *outp)
863{
864 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
865}
866
867static inline u32 dev_read_u32_index_default(struct udevice *dev,
868 const char *propname, int index,
869 u32 def)
870{
871 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
872 def);
873}
874
Simon Glass88b3a372020-01-27 08:49:40 -0700875static inline int dev_read_s32(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700876 const char *propname, s32 *outp)
877{
878 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
879}
880
Simon Glass88b3a372020-01-27 08:49:40 -0700881static inline int dev_read_s32_default(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700882 const char *propname, int def)
883{
884 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
885}
886
Simon Glass88b3a372020-01-27 08:49:40 -0700887static inline int dev_read_u32u(const struct udevice *dev,
Simon Glassa1b17e42018-12-10 10:37:37 -0700888 const char *propname, uint *outp)
889{
890 u32 val;
891 int ret;
892
893 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
894 if (ret)
895 return ret;
896 *outp = val;
897
898 return 0;
899}
900
Simon Glass88b3a372020-01-27 08:49:40 -0700901static inline int dev_read_u64(const struct udevice *dev,
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200902 const char *propname, u64 *outp)
903{
904 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
905}
906
Simon Glass88b3a372020-01-27 08:49:40 -0700907static inline u64 dev_read_u64_default(const struct udevice *dev,
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200908 const char *propname, u64 def)
909{
910 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
911}
912
Simon Glass88b3a372020-01-27 08:49:40 -0700913static inline const char *dev_read_string(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600914 const char *propname)
915{
916 return ofnode_read_string(dev_ofnode(dev), propname);
917}
918
Simon Glass88b3a372020-01-27 08:49:40 -0700919static inline bool dev_read_bool(const struct udevice *dev,
920 const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600921{
922 return ofnode_read_bool(dev_ofnode(dev), propname);
923}
924
Simon Glass88b3a372020-01-27 08:49:40 -0700925static inline ofnode dev_read_subnode(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -0600926 const char *subbnode_name)
927{
928 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
929}
930
Simon Glass88b3a372020-01-27 08:49:40 -0700931static inline int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glass47a0fd32017-05-18 20:09:04 -0600932{
933 return ofnode_read_size(dev_ofnode(dev), propname);
934}
935
Simon Glass88b3a372020-01-27 08:49:40 -0700936static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
937 int index)
Simon Glass47a0fd32017-05-18 20:09:04 -0600938{
939 return devfdt_get_addr_index(dev, index);
940}
941
Bin Mengbdce9032021-09-12 11:15:13 +0800942static inline void *dev_read_addr_index_ptr(const struct udevice *dev,
943 int index)
944{
945 return devfdt_get_addr_index_ptr(dev, index);
946}
947
Simon Glass88b3a372020-01-27 08:49:40 -0700948static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
Sekhar Norif5b90472019-08-01 19:12:56 +0530949 int index,
950 fdt_size_t *size)
951{
952 return devfdt_get_addr_size_index(dev, index, size);
953}
954
Simon Glass88b3a372020-01-27 08:49:40 -0700955static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100956 const char *name)
957{
958 return devfdt_get_addr_name(dev, name);
959}
960
Simon Glass88b3a372020-01-27 08:49:40 -0700961static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
Sekhar Norif5b90472019-08-01 19:12:56 +0530962 const char *name,
963 fdt_size_t *size)
964{
965 return devfdt_get_addr_size_name(dev, name, size);
966}
967
Simon Glass88b3a372020-01-27 08:49:40 -0700968static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -0600969{
970 return devfdt_get_addr(dev);
971}
972
Simon Glass88b3a372020-01-27 08:49:40 -0700973static inline void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200974{
Ovidiu Panait3fe69d32020-08-03 22:17:35 +0300975 return devfdt_get_addr_ptr(dev);
Philipp Tomsichc131c8b2017-09-11 22:04:12 +0200976}
977
Simon Glass88b3a372020-01-27 08:49:40 -0700978static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass33c215a2019-09-15 12:08:58 -0600979{
980 return devfdt_get_addr_pci(dev);
981}
982
Simon Glass88b3a372020-01-27 08:49:40 -0700983static inline void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200984{
985 return devfdt_remap_addr(dev);
986}
987
Simon Glass88b3a372020-01-27 08:49:40 -0700988static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernåndez Rojas30a90f52018-04-29 21:56:54 +0200989{
990 return devfdt_remap_addr_index(dev, index);
991}
992
Simon Glass88b3a372020-01-27 08:49:40 -0700993static inline void *dev_remap_addr_name(const struct udevice *dev,
994 const char *name)
Álvaro Fernåndez Rojas79598822018-12-03 19:37:09 +0100995{
996 return devfdt_remap_addr_name(dev, name);
997}
998
Simon Glass88b3a372020-01-27 08:49:40 -0700999static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -06001000 fdt_size_t *sizep)
1001{
John Keeping77224322023-06-01 15:11:19 +01001002 return dev_read_addr_size_index(dev, 0, sizep);
Simon Glass47a0fd32017-05-18 20:09:04 -06001003}
1004
Simon Glass88b3a372020-01-27 08:49:40 -07001005static inline const char *dev_read_name(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001006{
1007 return ofnode_get_name(dev_ofnode(dev));
1008}
1009
Simon Glass88b3a372020-01-27 08:49:40 -07001010static inline int dev_read_stringlist_search(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -06001011 const char *propname,
1012 const char *string)
1013{
1014 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
1015}
1016
Simon Glass88b3a372020-01-27 08:49:40 -07001017static inline int dev_read_string_index(const struct udevice *dev,
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +02001018 const char *propname, int index,
1019 const char **outp)
1020{
1021 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
1022}
1023
Simon Glass88b3a372020-01-27 08:49:40 -07001024static inline int dev_read_string_count(const struct udevice *dev,
Jean-Jacques Hiblotb5a144a2017-09-21 17:03:09 +02001025 const char *propname)
1026{
1027 return ofnode_read_string_count(dev_ofnode(dev), propname);
1028}
1029
Simon Glass075bfc92021-10-23 17:26:07 -06001030static inline int dev_read_string_list(const struct udevice *dev,
1031 const char *propname,
1032 const char ***listp)
1033{
1034 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
1035}
1036
Simon Glass88b3a372020-01-27 08:49:40 -07001037static inline int dev_read_phandle_with_args(const struct udevice *dev,
Simon Glass47a0fd32017-05-18 20:09:04 -06001038 const char *list_name, const char *cells_name, int cell_count,
1039 int index, struct ofnode_phandle_args *out_args)
1040{
1041 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
1042 cells_name, cell_count, index,
1043 out_args);
1044}
1045
Simon Glass88b3a372020-01-27 08:49:40 -07001046static inline int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunay89f68302020-09-25 09:41:14 +02001047 const char *list_name, const char *cells_name, int cell_count)
Patrice Chotard642346a2017-07-18 11:57:08 +02001048{
1049 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +02001050 cells_name, cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +02001051}
1052
Simon Glass88b3a372020-01-27 08:49:40 -07001053static inline int dev_read_addr_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001054{
Heinrich Schuchardtae6b33d2020-07-25 21:38:49 +02001055 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1056
1057 return fdt_address_cells(gd->fdt_blob, parent);
Simon Glass47a0fd32017-05-18 20:09:04 -06001058}
1059
Simon Glass88b3a372020-01-27 08:49:40 -07001060static inline int dev_read_size_cells(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001061{
Heinrich Schuchardtae6b33d2020-07-25 21:38:49 +02001062 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1063
1064 return fdt_size_cells(gd->fdt_blob, parent);
Simon Glass878d68c2017-06-12 06:21:31 -06001065}
1066
Simon Glass88b3a372020-01-27 08:49:40 -07001067static inline int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -06001068{
1069 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
1070}
1071
Simon Glass88b3a372020-01-27 08:49:40 -07001072static inline int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass878d68c2017-06-12 06:21:31 -06001073{
Simon Glass47a0fd32017-05-18 20:09:04 -06001074 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
1075}
1076
Simon Glass88b3a372020-01-27 08:49:40 -07001077static inline int dev_read_phandle(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001078{
1079 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
1080}
1081
Simon Glass88b3a372020-01-27 08:49:40 -07001082static inline const void *dev_read_prop(const struct udevice *dev,
Masahiro Yamadafd736212017-07-17 12:18:39 +09001083 const char *propname, int *lenp)
Simon Glass47a0fd32017-05-18 20:09:04 -06001084{
Masahiro Yamada61e51ba2017-06-22 16:54:05 +09001085 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glass47a0fd32017-05-18 20:09:04 -06001086}
1087
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001088static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
1089{
Simon Glass4b1f5712022-09-06 20:27:13 -06001090 return ofnode_first_property(dev_ofnode(dev), prop);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001091}
1092
1093static inline int dev_read_next_prop(struct ofprop *prop)
1094{
Simon Glass4b1f5712022-09-06 20:27:13 -06001095 return ofnode_next_property(prop);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001096}
1097
1098static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
1099 const char **propname,
1100 int *lenp)
1101{
Simon Glass92432242022-09-06 20:27:14 -06001102 return ofprop_get_property(prop, propname, lenp);
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001103}
1104
Simon Glass88b3a372020-01-27 08:49:40 -07001105static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glass47a0fd32017-05-18 20:09:04 -06001106{
Marcel Ziswiler45224e82019-05-20 02:44:57 +02001107#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass47a0fd32017-05-18 20:09:04 -06001108 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
1109 dev_of_offset(dev), devnump);
Marcel Ziswiler45224e82019-05-20 02:44:57 +02001110#else
1111 return -ENOTSUPP;
1112#endif
Simon Glass47a0fd32017-05-18 20:09:04 -06001113}
1114
Simon Glass88b3a372020-01-27 08:49:40 -07001115static inline int dev_read_u32_array(const struct udevice *dev,
1116 const char *propname, u32 *out_values,
1117 size_t sz)
Simon Glass47a0fd32017-05-18 20:09:04 -06001118{
1119 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
1120}
1121
Simon Glass88b3a372020-01-27 08:49:40 -07001122static inline ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glass47a0fd32017-05-18 20:09:04 -06001123{
1124 return ofnode_first_subnode(dev_ofnode(dev));
1125}
1126
1127static inline ofnode dev_read_next_subnode(ofnode node)
1128{
1129 return ofnode_next_subnode(node);
1130}
1131
Simon Glass88b3a372020-01-27 08:49:40 -07001132static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
Simon Glassf262d4c2020-01-27 08:49:47 -07001133 const char *propname,
1134 size_t sz)
Simon Glassf11c7ab2017-05-18 20:09:03 -06001135{
1136 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
1137}
1138
Simon Glass88b3a372020-01-27 08:49:40 -07001139static inline int dev_read_enabled(const struct udevice *dev)
Simon Glassf7d6fcf2017-06-12 06:21:30 -06001140{
1141 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
1142}
1143
Simon Glass88b3a372020-01-27 08:49:40 -07001144static inline int dev_read_resource(const struct udevice *dev, uint index,
Simon Glassdcf98852017-07-25 08:29:55 -06001145 struct resource *res)
1146{
1147 return ofnode_read_resource(dev_ofnode(dev), index, res);
1148}
1149
Simon Glass88b3a372020-01-27 08:49:40 -07001150static inline int dev_read_resource_byname(const struct udevice *dev,
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +09001151 const char *name,
1152 struct resource *res)
1153{
1154 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
1155}
1156
Simon Glass88b3a372020-01-27 08:49:40 -07001157static inline u64 dev_translate_address(const struct udevice *dev,
1158 const fdt32_t *in_addr)
Mario Six147c6072018-01-15 11:07:19 +01001159{
1160 return ofnode_translate_address(dev_ofnode(dev), in_addr);
1161}
1162
Simon Glass88b3a372020-01-27 08:49:40 -07001163static inline u64 dev_translate_dma_address(const struct udevice *dev,
1164 const fdt32_t *in_addr)
Fabien Dessenne641067f2019-05-31 15:11:30 +02001165{
1166 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1167}
1168
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001169static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
1170 dma_addr_t *bus, u64 *size)
1171{
1172 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
1173}
1174
Michal Simek83e4c7e2019-01-31 16:30:59 +01001175static inline int dev_read_alias_highest_id(const char *stem)
1176{
Stanislav Pinchuka00e0f72021-01-20 21:52:23 +03001177 if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
Michael Walle0a6b75f2020-06-02 01:47:08 +02001178 return -1;
Michal Simek83e4c7e2019-01-31 16:30:59 +01001179 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1180}
1181
Chunfeng Yun89b84b82020-05-02 11:35:09 +02001182static inline int dev_get_child_count(const struct udevice *dev)
1183{
1184 return ofnode_get_child_count(dev_ofnode(dev));
1185}
1186
Dario Binacchi15daa482020-12-30 00:16:26 +01001187static inline int dev_decode_display_timing(const struct udevice *dev,
1188 int index,
1189 struct display_timing *config)
1190{
1191 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
1192}
1193
Nikhil M Jain0347cc72023-01-31 15:35:14 +05301194static inline int dev_decode_panel_timing(const struct udevice *dev,
1195 struct display_timing *config)
1196{
1197 return ofnode_decode_panel_timing(dev_ofnode(dev), config);
1198}
1199
Marek BehĂșnf3dd2132022-04-07 00:32:57 +02001200static inline ofnode dev_get_phy_node(const struct udevice *dev)
1201{
1202 return ofnode_get_phy_node(dev_ofnode(dev));
1203}
1204
Marek BehĂșn123ca112022-04-07 00:33:01 +02001205static inline phy_interface_t dev_read_phy_mode(const struct udevice *dev)
1206{
1207 return ofnode_read_phy_mode(dev_ofnode(dev));
1208}
1209
Simon Glassf11c7ab2017-05-18 20:09:03 -06001210#endif /* CONFIG_DM_DEV_READ_INLINE */
1211
1212/**
1213 * dev_for_each_subnode() - Helper function to iterate through subnodes
1214 *
1215 * This creates a for() loop which works through the subnodes in a device's
1216 * device-tree node.
1217 *
1218 * @subnode: ofnode holding the current subnode
Patrick Delaunay6de6a612022-01-12 10:53:48 +01001219 * @dev: device to use for interation (`struct udevice *`)
Simon Glassf11c7ab2017-05-18 20:09:03 -06001220 */
1221#define dev_for_each_subnode(subnode, dev) \
1222 for (subnode = dev_read_first_subnode(dev); \
1223 ofnode_valid(subnode); \
1224 subnode = ofnode_next_subnode(subnode))
1225
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001226/**
1227 * dev_for_each_property() - Helper function to iterate through property
1228 *
1229 * This creates a for() loop which works through the property in a device's
1230 * device-tree node.
1231 *
1232 * @prop: struct ofprop holding the current property
Patrick Delaunay6de6a612022-01-12 10:53:48 +01001233 * @dev: device to use for interation (`struct udevice *`)
Patrick Delaunayce891fca2020-01-13 11:34:56 +01001234 */
1235#define dev_for_each_property(prop, dev) \
1236 for (int ret_prop = dev_read_first_prop(dev, &prop); \
1237 !ret_prop; \
1238 ret_prop = dev_read_next_prop(&prop))
1239
Simon Glassf11c7ab2017-05-18 20:09:03 -06001240#endif