blob: c2615f72f4059b975994ba081f0138b8c7a87a0e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass34ef0c22017-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 Glass34ef0c22017-05-18 20:09:03 -06007 */
8
9#ifndef _DM_READ_H
10#define _DM_READ_H
11
Dan Murphyd3aa90e2020-07-23 07:01:38 -050012#include <linux/errno.h>
13
Simon Glass992b6032020-07-19 10:15:39 -060014#include <dm/device.h>
Simon Glass34ef0c22017-05-18 20:09:03 -060015#include <dm/fdtaddr.h>
16#include <dm/ofnode.h>
17#include <dm/uclass.h>
18
Simon Glass170e17c2017-06-12 06:21:29 -060019struct resource;
20
Simon Glass34ef0c22017-05-18 20:09:03 -060021#if CONFIG_IS_ENABLED(OF_LIVE)
Simon Glass2f9a6122020-01-27 08:49:40 -070022static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glass34ef0c22017-05-18 20:09:03 -060023{
Simon Glassa7ece582020-12-19 10:40:14 -070024 return ofnode_to_np(dev_ofnode(dev));
Simon Glass34ef0c22017-05-18 20:09:03 -060025}
26#else
Simon Glass2f9a6122020-01-27 08:49:40 -070027static inline const struct device_node *dev_np(const struct udevice *dev)
Simon Glass34ef0c22017-05-18 20:09:03 -060028{
29 return NULL;
30}
31#endif
32
Simon Glass77f7c1e2021-01-21 13:57:10 -070033#if !defined(CONFIG_DM_DEV_READ_INLINE) || CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasscfdae5c2017-05-18 20:09:04 -060034/**
Stefan Herbrechtsmeier1b090e62022-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 Yamada71d115f2017-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 Delaunaycb3d6d62022-01-12 10:53:48 +010081 * Return: 0 if OK, -ve on error
Masahiro Yamada71d115f2017-12-30 02:00:05 +090082 */
Simon Glass2f9a6122020-01-27 08:49:40 -070083int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
Masahiro Yamada71d115f2017-12-30 02:00:05 +090084
85/**
Simon Glasscfdae5c2017-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 Delaunaycb3d6d62022-01-12 10:53:48 +010091 * Return: property value, or @def if not found
Simon Glasscfdae5c2017-05-18 20:09:04 -060092 */
Simon Glass2f9a6122020-01-27 08:49:40 -070093int dev_read_u32_default(const struct udevice *dev, const char *propname,
94 int def);
Simon Glass34ef0c22017-05-18 20:09:03 -060095
96/**
Dario Binacchi81d80b52020-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100104 * Return: 0 if OK, -ve on error
Dario Binacchi81d80b52020-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100117 * Return: property value, or @def if not found
Dario Binacchi81d80b52020-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 Glass6df01f92018-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100128 * Return: 0 if OK, -ve on error
Simon Glass6df01f92018-12-10 10:37:37 -0700129 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700130int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
Simon Glass6df01f92018-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100138 * Return: property value, or @def if not found
Simon Glass6df01f92018-12-10 10:37:37 -0700139 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700140int dev_read_s32_default(const struct udevice *dev, const char *propname,
141 int def);
Simon Glass6df01f92018-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100151 * Return: 0 if OK, -ve on error
Simon Glass6df01f92018-12-10 10:37:37 -0700152 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700153int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
Simon Glass6df01f92018-12-10 10:37:37 -0700154
155/**
T Karthik Reddy478860d2019-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100161 * Return: 0 if OK, -ve on error
T Karthik Reddy478860d2019-09-02 16:34:30 +0200162 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700163int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
T Karthik Reddy478860d2019-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100171 * Return: property value, or @def if not found
T Karthik Reddy478860d2019-09-02 16:34:30 +0200172 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700173u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
174 u64 def);
T Karthik Reddy478860d2019-09-02 16:34:30 +0200175
176/**
Simon Glass34ef0c22017-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100181 * Return: string from property value, or NULL if there is no such property
Simon Glass34ef0c22017-05-18 20:09:03 -0600182 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700183const char *dev_read_string(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100190 * Return: true if property is present (meaning true), false if not present
Simon Glass34ef0c22017-05-18 20:09:03 -0600191 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700192bool dev_read_bool(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100199 * Return: reference to subnode (which can be invalid if there is no such
Simon Glass34ef0c22017-05-18 20:09:03 -0600200 * subnode)
201 */
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100202ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name);
Simon Glass34ef0c22017-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100209 * Return: size of property if present, or -EINVAL if not
Simon Glass34ef0c22017-05-18 20:09:03 -0600210 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700211int dev_read_size(const struct udevice *dev, const char *propname);
Simon Glass34ef0c22017-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100220 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glass34ef0c22017-05-18 20:09:03 -0600221 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700222fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
Simon Glass34ef0c22017-05-18 20:09:03 -0600223
224/**
Bin Meng21cdfea2021-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100232 * Return: pointer or NULL if not found
Bin Meng21cdfea2021-09-12 11:15:13 +0800233 */
234void *dev_read_addr_index_ptr(const struct udevice *dev, int index);
235
236/**
Sekhar Norif677c6f2019-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100244 * Return: address or FDT_ADDR_T_NONE if not found
Sekhar Norif677c6f2019-08-01 19:12:56 +0530245 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700246fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530247 fdt_size_t *size);
248
249/**
Jonas Karlman3b007302023-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 Rojas670361292018-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100271 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas670361292018-04-29 21:56:54 +0200272 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700273void *dev_remap_addr_index(const struct udevice *dev, int index);
Álvaro Fernåndez Rojas670361292018-04-29 21:56:54 +0200274
275/**
Álvaro Fernåndez Rojasa3181152018-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100283 * Return: address or FDT_ADDR_T_NONE if not found
Álvaro Fernåndez Rojasa3181152018-12-03 19:37:09 +0100284 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700285fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernåndez Rojasa3181152018-12-03 19:37:09 +0100286
287/**
Sekhar Norif677c6f2019-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100296 * Return: address or FDT_ADDR_T_NONE if not found
Sekhar Norif677c6f2019-08-01 19:12:56 +0530297 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700298fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530299 fdt_size_t *size);
300
301/**
Álvaro Fernåndez Rojasa3181152018-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100310 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojasa3181152018-12-03 19:37:09 +0100311 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700312void *dev_remap_addr_name(const struct udevice *dev, const char *name);
Álvaro Fernåndez Rojasa3181152018-12-03 19:37:09 +0100313
314/**
Simon Glass34ef0c22017-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100319 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glass34ef0c22017-05-18 20:09:03 -0600320 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700321fdt_addr_t dev_read_addr(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600322
323/**
Philipp Tomsich7719b392017-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 Delaunaycb3d6d62022-01-12 10:53:48 +0100329 * Return: pointer or NULL if not found
Philipp Tomsich7719b392017-09-11 22:04:12 +0200330 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700331void *dev_read_addr_ptr(const struct udevice *dev);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200332
333/**
Simon Glass23b27592019-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
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100349 * Return: address or FDT_ADDR_T_NONE if not found
Simon Glass23b27592019-09-15 12:08:58 -0600350 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700351fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
Simon Glass23b27592019-09-15 12:08:58 -0600352
353/**
Álvaro Fernåndez Rojas670361292018-04-29 21:56:54 +0200354 * dev_remap_addr() - Get the reg property of a device as a
355 * memory-mapped I/O pointer
356 *
357 * @dev: Device to read from
358 *
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100359 * Return: pointer or NULL if not found
Álvaro Fernåndez Rojas670361292018-04-29 21:56:54 +0200360 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700361void *dev_remap_addr(const struct udevice *dev);
Álvaro Fernåndez Rojas670361292018-04-29 21:56:54 +0200362
363/**
John Keepingab62d0b2023-06-01 15:11:19 +0100364 * dev_read_addr_size() - Get the reg property of a device
Simon Glass34ef0c22017-05-18 20:09:03 -0600365 *
366 * @dev: Device to read from
Simon Glass34ef0c22017-05-18 20:09:03 -0600367 * @sizep: place to put size value (on success)
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100368 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glass34ef0c22017-05-18 20:09:03 -0600369 */
John Keepingab62d0b2023-06-01 15:11:19 +0100370fdt_addr_t dev_read_addr_size(const struct udevice *dev, fdt_size_t *sizep);
Simon Glass34ef0c22017-05-18 20:09:03 -0600371
372/**
373 * dev_read_name() - get the name of a device's node
374 *
Bin Meng4cf914d2019-07-05 09:23:18 -0700375 * @dev: Device to read from
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100376 * Return: name of node
Simon Glass34ef0c22017-05-18 20:09:03 -0600377 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700378const char *dev_read_name(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600379
380/**
381 * dev_read_stringlist_search() - find string in a string list and return index
382 *
383 * Note that it is possible for this function to succeed on property values
384 * that are not NUL-terminated. That's because the function will stop after
385 * finding the first occurrence of @string. This can for example happen with
386 * small-valued cell properties, such as #address-cells, when searching for
387 * the empty string.
388 *
389 * @dev: device to check
390 * @propname: name of the property containing the string list
391 * @string: string to look up in the string list
392 *
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100393 * Return:
Simon Glass34ef0c22017-05-18 20:09:03 -0600394 * the index of the string in the list of strings
395 * -ENODATA if the property is not found
396 * -EINVAL on some other error
397 */
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100398int dev_read_stringlist_search(const struct udevice *dev, const char *propname,
Simon Glass2f9a6122020-01-27 08:49:40 -0700399 const char *string);
Simon Glass34ef0c22017-05-18 20:09:03 -0600400
401/**
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200402 * dev_read_string_index() - obtain an indexed string from a string list
403 *
404 * @dev: device to examine
405 * @propname: name of the property containing the string list
406 * @index: index of the string to return
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100407 * @outp: return location for the string
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200408 *
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100409 * Return:
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200410 * length of string, if found or -ve error value if not found
411 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700412int dev_read_string_index(const struct udevice *dev, const char *propname,
413 int index, const char **outp);
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200414
415/**
416 * dev_read_string_count() - find the number of strings in a string list
417 *
418 * @dev: device to examine
419 * @propname: name of the property containing the string list
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100420 * Return:
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200421 * number of strings in the list, or -ve error value if not found
422 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700423int dev_read_string_count(const struct udevice *dev, const char *propname);
Simon Glass9580bfc2021-10-23 17:26:07 -0600424
425/**
426 * dev_read_string_list() - read a list of strings
427 *
428 * This produces a list of string pointers with each one pointing to a string
429 * in the string list. If the property does not exist, it returns {NULL}.
430 *
431 * The data is allocated and the caller is reponsible for freeing the return
432 * value (the list of string pointers). The strings themselves may not be
433 * changed as they point directly into the devicetree property.
434 *
435 * @dev: device to examine
436 * @propname: name of the property containing the string list
437 * @listp: returns an allocated, NULL-terminated list of strings if the return
438 * value is > 0, else is set to NULL
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100439 * Return:
440 * number of strings in list, 0 if none, -ENOMEM if out of memory,
441 * -ENOENT if no such property
Simon Glass9580bfc2021-10-23 17:26:07 -0600442 */
443int dev_read_string_list(const struct udevice *dev, const char *propname,
444 const char ***listp);
445
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +0200446/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600447 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
448 *
449 * This function is useful to parse lists of phandles and their arguments.
450 * Returns 0 on success and fills out_args, on error returns appropriate
451 * errno value.
452 *
453 * Caller is responsible to call of_node_put() on the returned out_args->np
454 * pointer.
455 *
456 * Example:
457 *
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100458 * .. code-block::
Simon Glass34ef0c22017-05-18 20:09:03 -0600459 *
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100460 * phandle1: node1 {
461 * #list-cells = <2>;
462 * };
463 * phandle2: node2 {
464 * #list-cells = <1>;
465 * };
466 * node3 {
467 * list = <&phandle1 1 2 &phandle2 3>;
468 * };
Simon Glass34ef0c22017-05-18 20:09:03 -0600469 *
470 * To get a device_node of the `node2' node you may call this:
471 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
472 *
473 * @dev: device whose node containing a list
474 * @list_name: property name that contains a list
475 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100476 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glass34ef0c22017-05-18 20:09:03 -0600477 * @index: index of a phandle to parse out
478 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100479 * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if
Simon Glass34ef0c22017-05-18 20:09:03 -0600480 * @list_name does not exist, -EINVAL if a phandle was not found,
481 * @cells_name could not be found, the arguments were truncated or there
482 * were too many arguments.
483 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700484int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
485 const char *cells_name, int cell_count,
486 int index, struct ofnode_phandle_args *out_args);
Simon Glass34ef0c22017-05-18 20:09:03 -0600487
488/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200489 * dev_count_phandle_with_args() - Return phandle number in a list
490 *
491 * This function is usefull to get phandle number contained in a property list.
492 * For example, this allows to allocate the right amount of memory to keep
493 * clock's reference contained into the "clocks" property.
494 *
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200495 * @dev: device whose node containing a list
496 * @list_name: property name that contains a list
497 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100498 * @cell_count: Cell count to use if @cells_name is NULL
499 * Return: number of phandle found on success, on error returns appropriate
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200500 * errno value.
501 */
502
Simon Glass2f9a6122020-01-27 08:49:40 -0700503int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200504 const char *list_name, const char *cells_name,
505 int cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200506
507/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600508 * dev_read_addr_cells() - Get the number of address cells for a device's node
509 *
510 * This walks back up the tree to find the closest #address-cells property
511 * which controls the given node.
512 *
Mario Six16408ee2018-01-15 11:09:36 +0100513 * @dev: device to check
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100514 * Return: number of address cells this node uses
Simon Glass34ef0c22017-05-18 20:09:03 -0600515 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700516int dev_read_addr_cells(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600517
518/**
519 * dev_read_size_cells() - Get the number of size cells for a device's node
520 *
521 * This walks back up the tree to find the closest #size-cells property
522 * which controls the given node.
523 *
Mario Six16408ee2018-01-15 11:09:36 +0100524 * @dev: device to check
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100525 * Return: number of size cells this node uses
Simon Glass34ef0c22017-05-18 20:09:03 -0600526 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700527int dev_read_size_cells(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600528
529/**
Simon Glass4191dc12017-06-12 06:21:31 -0600530 * dev_read_addr_cells() - Get the address cells property in a node
531 *
532 * This function matches fdt_address_cells().
533 *
Mario Six16408ee2018-01-15 11:09:36 +0100534 * @dev: device to check
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100535 * Return: number of address cells this node uses
Simon Glass4191dc12017-06-12 06:21:31 -0600536 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700537int dev_read_simple_addr_cells(const struct udevice *dev);
Simon Glass4191dc12017-06-12 06:21:31 -0600538
539/**
540 * dev_read_size_cells() - Get the size cells property in a node
541 *
542 * This function matches fdt_size_cells().
543 *
Mario Six16408ee2018-01-15 11:09:36 +0100544 * @dev: device to check
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100545 * Return: number of size cells this node uses
Simon Glass4191dc12017-06-12 06:21:31 -0600546 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700547int dev_read_simple_size_cells(const struct udevice *dev);
Simon Glass4191dc12017-06-12 06:21:31 -0600548
549/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600550 * dev_read_phandle() - Get the phandle from a device
551 *
552 * @dev: device to check
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100553 * Return: phandle (1 or greater), or 0 if no phandle or other error
Simon Glass34ef0c22017-05-18 20:09:03 -0600554 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700555int dev_read_phandle(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600556
557/**
558 * dev_read_prop()- - read a property from a device's node
559 *
560 * @dev: device to check
561 * @propname: property to read
562 * @lenp: place to put length on success
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100563 * Return: pointer to property, or NULL if not found
Simon Glass34ef0c22017-05-18 20:09:03 -0600564 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700565const void *dev_read_prop(const struct udevice *dev, const char *propname,
566 int *lenp);
Simon Glass34ef0c22017-05-18 20:09:03 -0600567
568/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100569 * dev_read_first_prop()- get the reference of the first property
570 *
571 * Get reference to the first property of the node, it is used to iterate
572 * and read all the property with dev_read_prop_by_prop().
573 *
574 * @dev: device to check
575 * @prop: place to put argument reference
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100576 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100577 */
578int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
579
580/**
Simon Glassfec058d2022-09-06 20:27:13 -0600581 * ofnode_next_property() - get the reference of the next property
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100582 *
583 * Get reference to the next property of the node, it is used to iterate
584 * and read all the property with dev_read_prop_by_prop().
585 *
586 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100587 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100588 */
589int dev_read_next_prop(struct ofprop *prop);
590
591/**
592 * dev_read_prop_by_prop() - get a pointer to the value of a property
593 *
594 * Get value for the property identified by the provided reference.
595 *
596 * @prop: reference on property
597 * @propname: If non-NULL, place to property name on success,
598 * @lenp: If non-NULL, place to put length on success
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100599 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100600 */
601const void *dev_read_prop_by_prop(struct ofprop *prop,
602 const char **propname, int *lenp);
603
604/**
Simon Glass34ef0c22017-05-18 20:09:03 -0600605 * dev_read_alias_seq() - Get the alias sequence number of a node
606 *
607 * This works out whether a node is pointed to by an alias, and if so, the
608 * sequence number of that alias. Aliases are of the form <base><num> where
609 * <num> is the sequence number. For example spi2 would be sequence number 2.
610 *
611 * @dev: device to look up
612 * @devnump: set to the sequence number if one is found
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100613 * Return: 0 if a sequence was found, -ve if not
Simon Glass34ef0c22017-05-18 20:09:03 -0600614 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700615int dev_read_alias_seq(const struct udevice *dev, int *devnump);
Simon Glass34ef0c22017-05-18 20:09:03 -0600616
617/**
618 * dev_read_u32_array() - Find and read an array of 32 bit integers
619 *
620 * Search for a property in a device node and read 32-bit value(s) from
621 * it.
622 *
623 * The out_values is modified only if a valid u32 value can be decoded.
624 *
625 * @dev: device to look up
626 * @propname: name of the property to read
627 * @out_values: pointer to return value, modified only if return value is 0
628 * @sz: number of array elements to read
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100629 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
Simon Glass34ef0c22017-05-18 20:09:03 -0600630 * property does not have a value, and -EOVERFLOW if the property data isn't
631 * large enough.
632 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700633int dev_read_u32_array(const struct udevice *dev, const char *propname,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600634 u32 *out_values, size_t sz);
Simon Glass34ef0c22017-05-18 20:09:03 -0600635
636/**
637 * dev_read_first_subnode() - find the first subnode of a device's node
638 *
639 * @dev: device to look up
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100640 * Return: reference to the first subnode (which can be invalid if the device's
Simon Glass34ef0c22017-05-18 20:09:03 -0600641 * node has no subnodes)
642 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700643ofnode dev_read_first_subnode(const struct udevice *dev);
Simon Glass34ef0c22017-05-18 20:09:03 -0600644
645/**
646 * ofnode_next_subnode() - find the next sibling of a subnode
647 *
648 * @node: valid reference to previous node (sibling)
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100649 * Return: reference to the next subnode (which can be invalid if the node
Simon Glass34ef0c22017-05-18 20:09:03 -0600650 * has no more siblings)
651 */
Simon Glasscfdae5c2017-05-18 20:09:04 -0600652ofnode dev_read_next_subnode(ofnode node);
Simon Glass34ef0c22017-05-18 20:09:03 -0600653
654/**
655 * dev_read_u8_array_ptr() - find an 8-bit array
656 *
657 * Look up a device's node property and return a pointer to its contents as a
658 * byte array of given length. The property must have at least enough data
659 * for the array (count bytes). It may have more, but this will be ignored.
660 * The data is not copied.
661 *
662 * @dev: device to look up
663 * @propname: name of property to find
664 * @sz: number of array elements
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100665 * Return:
666 * pointer to byte array if found, or NULL if the property is not found or
667 * there is not enough data
Simon Glass34ef0c22017-05-18 20:09:03 -0600668 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700669const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
670 const char *propname, size_t sz);
Simon Glasscfdae5c2017-05-18 20:09:04 -0600671
Simon Glassfa031f82017-06-12 06:21:30 -0600672/**
673 * dev_read_enabled() - check whether a node is enabled
674 *
675 * This looks for a 'status' property. If this exists, then returns 1 if
676 * the status is 'ok' and 0 otherwise. If there is no status property,
677 * it returns 1 on the assumption that anything mentioned should be enabled
678 * by default.
679 *
680 * @dev: device to examine
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100681 * Return: integer value 0 (not enabled) or 1 (enabled)
Simon Glassfa031f82017-06-12 06:21:30 -0600682 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700683int dev_read_enabled(const struct udevice *dev);
Simon Glassfa031f82017-06-12 06:21:30 -0600684
Simon Glassf7bfcc42017-07-25 08:29:55 -0600685/**
686 * dev_read_resource() - obtain an indexed resource from a device.
687 *
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900688 * @dev: device to examine
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100689 * @index: index of the resource to retrieve (0 = first)
690 * @res: returns the resource
691 * Return: 0 if ok, negative on error
Simon Glassf7bfcc42017-07-25 08:29:55 -0600692 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700693int dev_read_resource(const struct udevice *dev, uint index,
694 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -0600695
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900696/**
697 * dev_read_resource_byname() - obtain a named resource from a device.
698 *
699 * @dev: device to examine
700 * @name: name of the resource to retrieve
701 * @res: returns the resource
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100702 * Return: 0 if ok, negative on error
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900703 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700704int dev_read_resource_byname(const struct udevice *dev, const char *name,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900705 struct resource *res);
706
Mario Sixaefac062018-01-15 11:07:19 +0100707/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200708 * dev_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +0100709 *
710 * Translate an address from the device-tree into a CPU physical address. This
711 * function walks up the tree and applies the various bus mappings along the
712 * way.
713 *
714 * @dev: device giving the context in which to translate the address
715 * @in_addr: pointer to the address to translate
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100716 * Return: the translated address; OF_BAD_ADDR on error
Mario Sixaefac062018-01-15 11:07:19 +0100717 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700718u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
Michal Simekd0e30a02019-01-31 16:30:59 +0100719
720/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200721 * dev_translate_dma_address() - Translate a device-tree DMA address
722 *
723 * Translate a DMA address from the device-tree into a CPU physical address.
724 * This function walks up the tree and applies the various bus mappings along
725 * the way.
726 *
727 * @dev: device giving the context in which to translate the DMA address
728 * @in_addr: pointer to the DMA address to translate
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100729 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne22236e02019-05-31 15:11:30 +0200730 */
Simon Glass2f9a6122020-01-27 08:49:40 -0700731u64 dev_translate_dma_address(const struct udevice *dev,
732 const fdt32_t *in_addr);
Fabien Dessenne22236e02019-05-31 15:11:30 +0200733
734/**
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +0100735 * dev_get_dma_range() - Get a device's DMA constraints
736 *
737 * Provide the address bases and size of the linear mapping between the CPU and
738 * a device's BUS address space.
739 *
740 * @dev: device giving the context in which to translate the DMA address
741 * @cpu: base address for CPU's view of memory
742 * @bus: base address for BUS's view of memory
743 * @size: size of the address space
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100744 * Return: 0 if ok, negative on error
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +0100745 */
746int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
747 dma_addr_t *bus, u64 *size);
748
749/**
Michal Simekd0e30a02019-01-31 16:30:59 +0100750 * dev_read_alias_highest_id - Get highest alias id for the given stem
751 * @stem: Alias stem to be examined
752 *
753 * The function travels the lookup table to get the highest alias id for the
754 * given alias stem.
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100755 * Return: alias ID, if found, else -1
Michal Simekd0e30a02019-01-31 16:30:59 +0100756 */
757int dev_read_alias_highest_id(const char *stem);
758
developerd93c8b42020-05-02 11:35:09 +0200759/**
760 * dev_get_child_count() - get the child count of a device
761 *
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100762 * @dev: device to use for interation (`struct udevice *`)
763 * Return: the count of child subnode
developerd93c8b42020-05-02 11:35:09 +0200764 */
765int dev_get_child_count(const struct udevice *dev);
766
Stefan Roese0a9ecc52020-08-05 13:56:11 +0200767/**
768 * dev_read_pci_bus_range - Read PCI bus-range resource
769 *
770 * Look at the bus range property of a device node and return the pci bus
771 * range for this node.
772 *
773 * @dev: device to examine
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100774 * @res: returns the resource
775 * Return: 0 if ok, negative on error
Stefan Roese0a9ecc52020-08-05 13:56:11 +0200776 */
777int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
778
Dario Binacchi836cc9d2020-12-30 00:16:26 +0100779/**
780 * dev_decode_display_timing() - decode display timings
781 *
782 * Decode display timings from the supplied 'display-timings' node.
783 * See doc/device-tree-bindings/video/display-timing.txt for binding
784 * information.
785 *
786 * @dev: device to read DT display timings from. The node linked to the device
787 * contains a child node called 'display-timings' which in turn contains
788 * one or more display timing nodes.
789 * @index: index number to read (0=first timing subnode)
790 * @config: place to put timings
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +0100791 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Dario Binacchi836cc9d2020-12-30 00:16:26 +0100792 */
793int dev_decode_display_timing(const struct udevice *dev, int index,
794 struct display_timing *config);
795
Marek BehĂșnf4f1ddc2022-04-07 00:32:57 +0200796/**
Nikhil M Jainff407062023-01-31 15:35:14 +0530797 * dev_decode_panel_timing() - decode panel timings
798 *
799 * Decode display timings from the supplied 'panel-timings' node.
800 *
801 * @dev: device to read DT display timings from. The node linked to the device
802 * contains a child node called 'display-timings' which in turn contains
803 * one or more display timing nodes.
804 * @config: place to put timings
805 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
806 */
807int dev_decode_panel_timing(const struct udevice *dev,
808 struct display_timing *config);
809
810/**
Marek BehĂșnf4f1ddc2022-04-07 00:32:57 +0200811 * dev_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
812 *
813 * This function parses PHY handle from the Ethernet controller's ofnode
814 * (trying all possible PHY handle property names), and returns the PHY ofnode.
815 *
816 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
817 * if the result to that is true, this function should not be called.
818 *
819 * @dev: device representing the MAC
820 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
821 */
822ofnode dev_get_phy_node(const struct udevice *dev);
823
Marek BehĂșnbc194772022-04-07 00:33:01 +0200824/**
825 * dev_read_phy_mode() - Read PHY connection type from a MAC
826 *
827 * This function parses the "phy-mode" / "phy-connection-type" property and
828 * returns the corresponding PHY interface type.
829 *
830 * @dev: device representing the MAC
Marek BehĂșn48631e42022-04-07 00:33:03 +0200831 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
Marek BehĂșnbc194772022-04-07 00:33:01 +0200832 * error
833 */
834phy_interface_t dev_read_phy_mode(const struct udevice *dev);
835
Simon Glasscfdae5c2017-05-18 20:09:04 -0600836#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
Simon Glass3ba929a2020-10-30 21:38:53 -0600837#include <asm/global_data.h>
Simon Glasscfdae5c2017-05-18 20:09:04 -0600838
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200839static inline int dev_read_u8(const struct udevice *dev,
840 const char *propname, u8 *outp)
841{
842 return ofnode_read_u8(dev_ofnode(dev), propname, outp);
843}
844
845static inline int dev_read_u8_default(const struct udevice *dev,
846 const char *propname, u8 def)
847{
848 return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
849}
850
851static inline int dev_read_u16(const struct udevice *dev,
852 const char *propname, u16 *outp)
853{
854 return ofnode_read_u16(dev_ofnode(dev), propname, outp);
855}
856
857static inline int dev_read_u16_default(const struct udevice *dev,
858 const char *propname, u16 def)
859{
860 return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
861}
862
Simon Glass2f9a6122020-01-27 08:49:40 -0700863static inline int dev_read_u32(const struct udevice *dev,
Masahiro Yamada71d115f2017-12-30 02:00:05 +0900864 const char *propname, u32 *outp)
865{
866 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
867}
868
Simon Glass2f9a6122020-01-27 08:49:40 -0700869static inline int dev_read_u32_default(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600870 const char *propname, int def)
871{
872 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
873}
874
Dario Binacchi81d80b52020-03-29 18:04:41 +0200875static inline int dev_read_u32_index(struct udevice *dev,
876 const char *propname, int index, u32 *outp)
877{
878 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
879}
880
881static inline u32 dev_read_u32_index_default(struct udevice *dev,
882 const char *propname, int index,
883 u32 def)
884{
885 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
886 def);
887}
888
Simon Glass2f9a6122020-01-27 08:49:40 -0700889static inline int dev_read_s32(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700890 const char *propname, s32 *outp)
891{
892 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
893}
894
Simon Glass2f9a6122020-01-27 08:49:40 -0700895static inline int dev_read_s32_default(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700896 const char *propname, int def)
897{
898 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
899}
900
Simon Glass2f9a6122020-01-27 08:49:40 -0700901static inline int dev_read_u32u(const struct udevice *dev,
Simon Glass6df01f92018-12-10 10:37:37 -0700902 const char *propname, uint *outp)
903{
904 u32 val;
905 int ret;
906
907 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
908 if (ret)
909 return ret;
910 *outp = val;
911
912 return 0;
913}
914
Simon Glass2f9a6122020-01-27 08:49:40 -0700915static inline int dev_read_u64(const struct udevice *dev,
T Karthik Reddy478860d2019-09-02 16:34:30 +0200916 const char *propname, u64 *outp)
917{
918 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
919}
920
Simon Glass2f9a6122020-01-27 08:49:40 -0700921static inline u64 dev_read_u64_default(const struct udevice *dev,
T Karthik Reddy478860d2019-09-02 16:34:30 +0200922 const char *propname, u64 def)
923{
924 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
925}
926
Simon Glass2f9a6122020-01-27 08:49:40 -0700927static inline const char *dev_read_string(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600928 const char *propname)
929{
930 return ofnode_read_string(dev_ofnode(dev), propname);
931}
932
Simon Glass2f9a6122020-01-27 08:49:40 -0700933static inline bool dev_read_bool(const struct udevice *dev,
934 const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600935{
936 return ofnode_read_bool(dev_ofnode(dev), propname);
937}
938
Simon Glass2f9a6122020-01-27 08:49:40 -0700939static inline ofnode dev_read_subnode(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -0600940 const char *subbnode_name)
941{
942 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
943}
944
Simon Glass2f9a6122020-01-27 08:49:40 -0700945static inline int dev_read_size(const struct udevice *dev, const char *propname)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600946{
947 return ofnode_read_size(dev_ofnode(dev), propname);
948}
949
Simon Glass2f9a6122020-01-27 08:49:40 -0700950static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
951 int index)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600952{
953 return devfdt_get_addr_index(dev, index);
954}
955
Bin Meng21cdfea2021-09-12 11:15:13 +0800956static inline void *dev_read_addr_index_ptr(const struct udevice *dev,
957 int index)
958{
959 return devfdt_get_addr_index_ptr(dev, index);
960}
961
Simon Glass2f9a6122020-01-27 08:49:40 -0700962static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530963 int index,
964 fdt_size_t *size)
965{
966 return devfdt_get_addr_size_index(dev, index, size);
967}
968
Jonas Karlman3b007302023-07-22 13:30:15 +0000969static inline void *dev_read_addr_size_index_ptr(const struct udevice *dev,
970 int index,
971 fdt_size_t *size)
972{
973 return devfdt_get_addr_size_index_ptr(dev, index, size);
974}
975
Simon Glass2f9a6122020-01-27 08:49:40 -0700976static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
Álvaro Fernåndez Rojasa3181152018-12-03 19:37:09 +0100977 const char *name)
978{
979 return devfdt_get_addr_name(dev, name);
980}
981
Simon Glass2f9a6122020-01-27 08:49:40 -0700982static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
Sekhar Norif677c6f2019-08-01 19:12:56 +0530983 const char *name,
984 fdt_size_t *size)
985{
986 return devfdt_get_addr_size_name(dev, name, size);
987}
988
Simon Glass2f9a6122020-01-27 08:49:40 -0700989static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -0600990{
991 return devfdt_get_addr(dev);
992}
993
Simon Glass2f9a6122020-01-27 08:49:40 -0700994static inline void *dev_read_addr_ptr(const struct udevice *dev)
Philipp Tomsich7719b392017-09-11 22:04:12 +0200995{
Ovidiu Panaita633f002020-08-03 22:17:35 +0300996 return devfdt_get_addr_ptr(dev);
Philipp Tomsich7719b392017-09-11 22:04:12 +0200997}
998
Simon Glass2f9a6122020-01-27 08:49:40 -0700999static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
Simon Glass23b27592019-09-15 12:08:58 -06001000{
1001 return devfdt_get_addr_pci(dev);
1002}
1003
Simon Glass2f9a6122020-01-27 08:49:40 -07001004static inline void *dev_remap_addr(const struct udevice *dev)
Álvaro Fernåndez Rojas670361292018-04-29 21:56:54 +02001005{
1006 return devfdt_remap_addr(dev);
1007}
1008
Simon Glass2f9a6122020-01-27 08:49:40 -07001009static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
Álvaro Fernåndez Rojas670361292018-04-29 21:56:54 +02001010{
1011 return devfdt_remap_addr_index(dev, index);
1012}
1013
Simon Glass2f9a6122020-01-27 08:49:40 -07001014static inline void *dev_remap_addr_name(const struct udevice *dev,
1015 const char *name)
Álvaro Fernåndez Rojasa3181152018-12-03 19:37:09 +01001016{
1017 return devfdt_remap_addr_name(dev, name);
1018}
1019
Simon Glass2f9a6122020-01-27 08:49:40 -07001020static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -06001021 fdt_size_t *sizep)
1022{
John Keepingab62d0b2023-06-01 15:11:19 +01001023 return dev_read_addr_size_index(dev, 0, sizep);
Simon Glasscfdae5c2017-05-18 20:09:04 -06001024}
1025
Simon Glass2f9a6122020-01-27 08:49:40 -07001026static inline const char *dev_read_name(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001027{
1028 return ofnode_get_name(dev_ofnode(dev));
1029}
1030
Simon Glass2f9a6122020-01-27 08:49:40 -07001031static inline int dev_read_stringlist_search(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -06001032 const char *propname,
1033 const char *string)
1034{
1035 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
1036}
1037
Simon Glass2f9a6122020-01-27 08:49:40 -07001038static inline int dev_read_string_index(const struct udevice *dev,
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +02001039 const char *propname, int index,
1040 const char **outp)
1041{
1042 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
1043}
1044
Simon Glass2f9a6122020-01-27 08:49:40 -07001045static inline int dev_read_string_count(const struct udevice *dev,
Jean-Jacques Hiblot8365ebd2017-09-21 17:03:09 +02001046 const char *propname)
1047{
1048 return ofnode_read_string_count(dev_ofnode(dev), propname);
1049}
1050
Simon Glass9580bfc2021-10-23 17:26:07 -06001051static inline int dev_read_string_list(const struct udevice *dev,
1052 const char *propname,
1053 const char ***listp)
1054{
1055 return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
1056}
1057
Simon Glass2f9a6122020-01-27 08:49:40 -07001058static inline int dev_read_phandle_with_args(const struct udevice *dev,
Simon Glasscfdae5c2017-05-18 20:09:04 -06001059 const char *list_name, const char *cells_name, int cell_count,
1060 int index, struct ofnode_phandle_args *out_args)
1061{
1062 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
1063 cells_name, cell_count, index,
1064 out_args);
1065}
1066
Simon Glass2f9a6122020-01-27 08:49:40 -07001067static inline int dev_count_phandle_with_args(const struct udevice *dev,
Patrick Delaunayd776a842020-09-25 09:41:14 +02001068 const char *list_name, const char *cells_name, int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +02001069{
1070 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +02001071 cells_name, cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +02001072}
1073
Simon Glass2f9a6122020-01-27 08:49:40 -07001074static inline int dev_read_addr_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001075{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001076 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1077
1078 return fdt_address_cells(gd->fdt_blob, parent);
Simon Glasscfdae5c2017-05-18 20:09:04 -06001079}
1080
Simon Glass2f9a6122020-01-27 08:49:40 -07001081static inline int dev_read_size_cells(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001082{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +02001083 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1084
1085 return fdt_size_cells(gd->fdt_blob, parent);
Simon Glass4191dc12017-06-12 06:21:31 -06001086}
1087
Simon Glass2f9a6122020-01-27 08:49:40 -07001088static inline int dev_read_simple_addr_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -06001089{
1090 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
1091}
1092
Simon Glass2f9a6122020-01-27 08:49:40 -07001093static inline int dev_read_simple_size_cells(const struct udevice *dev)
Simon Glass4191dc12017-06-12 06:21:31 -06001094{
Simon Glasscfdae5c2017-05-18 20:09:04 -06001095 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
1096}
1097
Simon Glass2f9a6122020-01-27 08:49:40 -07001098static inline int dev_read_phandle(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001099{
1100 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
1101}
1102
Simon Glass2f9a6122020-01-27 08:49:40 -07001103static inline const void *dev_read_prop(const struct udevice *dev,
Masahiro Yamadabaebec12017-07-17 12:18:39 +09001104 const char *propname, int *lenp)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001105{
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +09001106 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
Simon Glasscfdae5c2017-05-18 20:09:04 -06001107}
1108
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001109static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
1110{
Simon Glassfec058d2022-09-06 20:27:13 -06001111 return ofnode_first_property(dev_ofnode(dev), prop);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001112}
1113
1114static inline int dev_read_next_prop(struct ofprop *prop)
1115{
Simon Glassfec058d2022-09-06 20:27:13 -06001116 return ofnode_next_property(prop);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001117}
1118
1119static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
1120 const char **propname,
1121 int *lenp)
1122{
Simon Glassd0aff8b2022-09-06 20:27:14 -06001123 return ofprop_get_property(prop, propname, lenp);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001124}
1125
Simon Glass2f9a6122020-01-27 08:49:40 -07001126static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001127{
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +02001128#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001129 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
1130 dev_of_offset(dev), devnump);
Marcel Ziswiler65c2bf62019-05-20 02:44:57 +02001131#else
1132 return -ENOTSUPP;
1133#endif
Simon Glasscfdae5c2017-05-18 20:09:04 -06001134}
1135
Simon Glass2f9a6122020-01-27 08:49:40 -07001136static inline int dev_read_u32_array(const struct udevice *dev,
1137 const char *propname, u32 *out_values,
1138 size_t sz)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001139{
1140 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
1141}
1142
Simon Glass2f9a6122020-01-27 08:49:40 -07001143static inline ofnode dev_read_first_subnode(const struct udevice *dev)
Simon Glasscfdae5c2017-05-18 20:09:04 -06001144{
1145 return ofnode_first_subnode(dev_ofnode(dev));
1146}
1147
1148static inline ofnode dev_read_next_subnode(ofnode node)
1149{
1150 return ofnode_next_subnode(node);
1151}
1152
Simon Glass2f9a6122020-01-27 08:49:40 -07001153static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
Simon Glass000676b2020-01-27 08:49:47 -07001154 const char *propname,
1155 size_t sz)
Simon Glass34ef0c22017-05-18 20:09:03 -06001156{
1157 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
1158}
1159
Simon Glass2f9a6122020-01-27 08:49:40 -07001160static inline int dev_read_enabled(const struct udevice *dev)
Simon Glassfa031f82017-06-12 06:21:30 -06001161{
1162 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
1163}
1164
Simon Glass2f9a6122020-01-27 08:49:40 -07001165static inline int dev_read_resource(const struct udevice *dev, uint index,
Simon Glassf7bfcc42017-07-25 08:29:55 -06001166 struct resource *res)
1167{
1168 return ofnode_read_resource(dev_ofnode(dev), index, res);
1169}
1170
Simon Glass2f9a6122020-01-27 08:49:40 -07001171static inline int dev_read_resource_byname(const struct udevice *dev,
Masahiro Yamada4dada2c2017-08-26 01:12:30 +09001172 const char *name,
1173 struct resource *res)
1174{
1175 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
1176}
1177
Simon Glass2f9a6122020-01-27 08:49:40 -07001178static inline u64 dev_translate_address(const struct udevice *dev,
1179 const fdt32_t *in_addr)
Mario Sixaefac062018-01-15 11:07:19 +01001180{
1181 return ofnode_translate_address(dev_ofnode(dev), in_addr);
1182}
1183
Simon Glass2f9a6122020-01-27 08:49:40 -07001184static inline u64 dev_translate_dma_address(const struct udevice *dev,
1185 const fdt32_t *in_addr)
Fabien Dessenne22236e02019-05-31 15:11:30 +02001186{
1187 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1188}
1189
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001190static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
1191 dma_addr_t *bus, u64 *size)
1192{
1193 return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
1194}
1195
Michal Simekd0e30a02019-01-31 16:30:59 +01001196static inline int dev_read_alias_highest_id(const char *stem)
1197{
Stanislav Pinchuke8649402021-01-20 21:52:23 +03001198 if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
Michael Walleeccf2d02020-06-02 01:47:08 +02001199 return -1;
Michal Simekd0e30a02019-01-31 16:30:59 +01001200 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1201}
1202
developerd93c8b42020-05-02 11:35:09 +02001203static inline int dev_get_child_count(const struct udevice *dev)
1204{
1205 return ofnode_get_child_count(dev_ofnode(dev));
1206}
1207
Dario Binacchi836cc9d2020-12-30 00:16:26 +01001208static inline int dev_decode_display_timing(const struct udevice *dev,
1209 int index,
1210 struct display_timing *config)
1211{
1212 return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
1213}
1214
Nikhil M Jainff407062023-01-31 15:35:14 +05301215static inline int dev_decode_panel_timing(const struct udevice *dev,
1216 struct display_timing *config)
1217{
1218 return ofnode_decode_panel_timing(dev_ofnode(dev), config);
1219}
1220
Marek BehĂșnf4f1ddc2022-04-07 00:32:57 +02001221static inline ofnode dev_get_phy_node(const struct udevice *dev)
1222{
1223 return ofnode_get_phy_node(dev_ofnode(dev));
1224}
1225
Marek BehĂșnbc194772022-04-07 00:33:01 +02001226static inline phy_interface_t dev_read_phy_mode(const struct udevice *dev)
1227{
1228 return ofnode_read_phy_mode(dev_ofnode(dev));
1229}
1230
Simon Glass34ef0c22017-05-18 20:09:03 -06001231#endif /* CONFIG_DM_DEV_READ_INLINE */
1232
1233/**
1234 * dev_for_each_subnode() - Helper function to iterate through subnodes
1235 *
1236 * This creates a for() loop which works through the subnodes in a device's
1237 * device-tree node.
1238 *
1239 * @subnode: ofnode holding the current subnode
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +01001240 * @dev: device to use for interation (`struct udevice *`)
Simon Glass34ef0c22017-05-18 20:09:03 -06001241 */
1242#define dev_for_each_subnode(subnode, dev) \
1243 for (subnode = dev_read_first_subnode(dev); \
1244 ofnode_valid(subnode); \
1245 subnode = ofnode_next_subnode(subnode))
1246
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001247/**
1248 * dev_for_each_property() - Helper function to iterate through property
1249 *
1250 * This creates a for() loop which works through the property in a device's
1251 * device-tree node.
1252 *
1253 * @prop: struct ofprop holding the current property
Patrick Delaunaycb3d6d62022-01-12 10:53:48 +01001254 * @dev: device to use for interation (`struct udevice *`)
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001255 */
1256#define dev_for_each_property(prop, dev) \
1257 for (int ret_prop = dev_read_first_prop(dev, &prop); \
1258 !ret_prop; \
1259 ret_prop = dev_read_next_prop(&prop))
1260
Simon Glass34ef0c22017-05-18 20:09:03 -06001261#endif