2 * Function to read values from the device tree node attached to a udevice.
4 * Copyright (c) 2017 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/fdtaddr.h>
14 #include <dm/ofnode.h>
15 #include <dm/uclass.h>
17 #if CONFIG_IS_ENABLED(OF_LIVE)
18 static inline const struct device_node *dev_np(struct udevice *dev)
20 return ofnode_to_np(dev->node);
23 static inline const struct device_node *dev_np(struct udevice *dev)
30 * dev_ofnode() - get the DT node reference associated with a udevice
32 * @dev: device to check
33 * @return reference of the the device's DT node
35 static inline ofnode dev_ofnode(struct udevice *dev)
40 static inline bool dev_of_valid(struct udevice *dev)
42 return ofnode_valid(dev_ofnode(dev));
45 #ifndef CONFIG_DM_DEV_READ_INLINE
47 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
49 * @dev: device to read DT property from
50 * @propname: name of the property to read from
51 * @def: default value to return if the property has no value
52 * @return property value, or @def if not found
54 int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
57 * dev_read_string() - Read a string from a device's DT property
59 * @dev: device to read DT property from
60 * @propname: name of the property to read
61 * @return string from property value, or NULL if there is no such property
63 const char *dev_read_string(struct udevice *dev, const char *propname);
66 * dev_read_bool() - read a boolean value from a device's DT property
68 * @dev: device to read DT property from
69 * @propname: name of property to read
70 * @return true if property is present (meaning true), false if not present
72 bool dev_read_bool(struct udevice *dev, const char *propname);
75 * dev_read_subnode() - find a named subnode of a device
77 * @dev: device whose DT node contains the subnode
78 * @subnode_name: name of subnode to find
79 * @return reference to subnode (which can be invalid if there is no such
82 ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
85 * dev_read_size() - read the size of a property
87 * @dev: device to check
88 * @propname: property to check
89 * @return size of property if present, or -EINVAL if not
91 int dev_read_size(struct udevice *dev, const char *propname);
94 * dev_read_addr_index() - Get the indexed reg property of a device
96 * @dev: Device to read from
97 * @index: the 'reg' property can hold a list of <addr, size> pairs
98 * and @index is used to select which one is required
100 * @return address or FDT_ADDR_T_NONE if not found
102 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
105 * dev_read_addr() - Get the reg property of a device
107 * @dev: Device to read from
109 * @return address or FDT_ADDR_T_NONE if not found
111 fdt_addr_t dev_read_addr(struct udevice *dev);
114 * dev_read_addr_size() - get address and size from a device property
116 * This does no address translation. It simply reads an property that contains
117 * an address and a size value, one after the other.
119 * @dev: Device to read from
120 * @propname: property to read
121 * @sizep: place to put size value (on success)
122 * @return address value, or FDT_ADDR_T_NONE on error
124 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
128 * dev_read_name() - get the name of a device's node
130 * @node: valid node to look up
131 * @return name of node
133 const char *dev_read_name(struct udevice *dev);
136 * dev_read_stringlist_search() - find string in a string list and return index
138 * Note that it is possible for this function to succeed on property values
139 * that are not NUL-terminated. That's because the function will stop after
140 * finding the first occurrence of @string. This can for example happen with
141 * small-valued cell properties, such as #address-cells, when searching for
144 * @dev: device to check
145 * @propname: name of the property containing the string list
146 * @string: string to look up in the string list
149 * the index of the string in the list of strings
150 * -ENODATA if the property is not found
151 * -EINVAL on some other error
153 int dev_read_stringlist_search(struct udevice *dev, const char *property,
157 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
159 * This function is useful to parse lists of phandles and their arguments.
160 * Returns 0 on success and fills out_args, on error returns appropriate
163 * Caller is responsible to call of_node_put() on the returned out_args->np
177 * list = <&phandle1 1 2 &phandle2 3>;
180 * To get a device_node of the `node2' node you may call this:
181 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
183 * @dev: device whose node containing a list
184 * @list_name: property name that contains a list
185 * @cells_name: property name that specifies phandles' arguments count
186 * @cells_count: Cell count to use if @cells_name is NULL
187 * @index: index of a phandle to parse out
188 * @out_args: optional pointer to output arguments structure (will be filled)
189 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
190 * @list_name does not exist, -EINVAL if a phandle was not found,
191 * @cells_name could not be found, the arguments were truncated or there
192 * were too many arguments.
194 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
195 const char *cells_name, int cell_count,
197 struct ofnode_phandle_args *out_args);
200 * dev_read_addr_cells() - Get the number of address cells for a device's node
202 * This walks back up the tree to find the closest #address-cells property
203 * which controls the given node.
205 * @dev: devioe to check
206 * @return number of address cells this node uses
208 int dev_read_addr_cells(struct udevice *dev);
211 * dev_read_size_cells() - Get the number of size cells for a device's node
213 * This walks back up the tree to find the closest #size-cells property
214 * which controls the given node.
216 * @dev: devioe to check
217 * @return number of size cells this node uses
219 int dev_read_size_cells(struct udevice *dev);
222 * dev_read_phandle() - Get the phandle from a device
224 * @dev: device to check
225 * @return phandle (1 or greater), or 0 if no phandle or other error
227 int dev_read_phandle(struct udevice *dev);
230 * dev_read_prop()- - read a property from a device's node
232 * @dev: device to check
233 * @propname: property to read
234 * @lenp: place to put length on success
235 * @return pointer to property, or NULL if not found
237 const u32 *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
240 * dev_read_alias_seq() - Get the alias sequence number of a node
242 * This works out whether a node is pointed to by an alias, and if so, the
243 * sequence number of that alias. Aliases are of the form <base><num> where
244 * <num> is the sequence number. For example spi2 would be sequence number 2.
246 * @dev: device to look up
247 * @devnump: set to the sequence number if one is found
248 * @return 0 if a sequence was found, -ve if not
250 int dev_read_alias_seq(struct udevice *dev, int *devnump);
253 * dev_read_u32_array() - Find and read an array of 32 bit integers
255 * Search for a property in a device node and read 32-bit value(s) from
258 * The out_values is modified only if a valid u32 value can be decoded.
260 * @dev: device to look up
261 * @propname: name of the property to read
262 * @out_values: pointer to return value, modified only if return value is 0
263 * @sz: number of array elements to read
264 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
265 * property does not have a value, and -EOVERFLOW if the property data isn't
268 int dev_read_u32_array(struct udevice *dev, const char *propname,
269 u32 *out_values, size_t sz);
272 * dev_read_first_subnode() - find the first subnode of a device's node
274 * @dev: device to look up
275 * @return reference to the first subnode (which can be invalid if the device's
276 * node has no subnodes)
278 ofnode dev_read_first_subnode(struct udevice *dev);
281 * ofnode_next_subnode() - find the next sibling of a subnode
283 * @node: valid reference to previous node (sibling)
284 * @return reference to the next subnode (which can be invalid if the node
285 * has no more siblings)
287 ofnode dev_read_next_subnode(ofnode node);
290 * dev_read_u8_array_ptr() - find an 8-bit array
292 * Look up a device's node property and return a pointer to its contents as a
293 * byte array of given length. The property must have at least enough data
294 * for the array (count bytes). It may have more, but this will be ignored.
295 * The data is not copied.
297 * @dev: device to look up
298 * @propname: name of property to find
299 * @sz: number of array elements
300 * @return pointer to byte array if found, or NULL if the property is not
301 * found or there is not enough data
303 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
306 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
308 static inline int dev_read_u32_default(struct udevice *dev,
309 const char *propname, int def)
311 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
314 static inline const char *dev_read_string(struct udevice *dev,
315 const char *propname)
317 return ofnode_read_string(dev_ofnode(dev), propname);
320 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
322 return ofnode_read_bool(dev_ofnode(dev), propname);
325 static inline ofnode dev_read_subnode(struct udevice *dev,
326 const char *subbnode_name)
328 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
331 static inline int dev_read_size(struct udevice *dev, const char *propname)
333 return ofnode_read_size(dev_ofnode(dev), propname);
336 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
338 return devfdt_get_addr_index(dev, index);
341 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
343 return devfdt_get_addr(dev);
346 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
347 const char *propname,
350 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
353 static inline const char *dev_read_name(struct udevice *dev)
355 return ofnode_get_name(dev_ofnode(dev));
358 static inline int dev_read_stringlist_search(struct udevice *dev,
359 const char *propname,
362 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
365 static inline int dev_read_phandle_with_args(struct udevice *dev,
366 const char *list_name, const char *cells_name, int cell_count,
367 int index, struct ofnode_phandle_args *out_args)
369 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
370 cells_name, cell_count, index,
374 static inline int dev_read_addr_cells(struct udevice *dev)
376 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
379 static inline int dev_read_size_cells(struct udevice *dev)
381 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
384 static inline int dev_read_phandle(struct udevice *dev)
386 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
389 static inline const u32 *dev_read_prop(struct udevice *dev,
390 const char *propname, int *lenp)
392 return ofnode_read_prop(dev_ofnode(dev), propname, lenp);
395 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
397 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
398 dev_of_offset(dev), devnump);
401 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
402 u32 *out_values, size_t sz)
404 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
407 static inline ofnode dev_read_first_subnode(struct udevice *dev)
409 return ofnode_first_subnode(dev_ofnode(dev));
412 static inline ofnode dev_read_next_subnode(ofnode node)
414 return ofnode_next_subnode(node);
417 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
418 const char *propname, size_t sz)
420 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
423 #endif /* CONFIG_DM_DEV_READ_INLINE */
426 * dev_for_each_subnode() - Helper function to iterate through subnodes
428 * This creates a for() loop which works through the subnodes in a device's
431 * @subnode: ofnode holding the current subnode
432 * @dev: device to use for interation (struct udevice *)
434 #define dev_for_each_subnode(subnode, dev) \
435 for (subnode = dev_read_first_subnode(dev); \
436 ofnode_valid(subnode); \
437 subnode = ofnode_next_subnode(subnode))