blob: 4ce2bc2d6e199ab9883bfa15ebb4e634a85670c4 [file] [log] [blame]
Simon Glassf11c7ab2017-05-18 20:09:03 -06001/*
2 * Function to read values from the device tree node attached to a udevice.
3 *
4 * Copyright (c) 2017 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#ifndef _DM_READ_H
11#define _DM_READ_H
12
13#include <dm/fdtaddr.h>
14#include <dm/ofnode.h>
15#include <dm/uclass.h>
16
17#if CONFIG_IS_ENABLED(OF_LIVE)
18static inline const struct device_node *dev_np(struct udevice *dev)
19{
20 return ofnode_to_np(dev->node);
21}
22#else
23static inline const struct device_node *dev_np(struct udevice *dev)
24{
25 return NULL;
26}
27#endif
28
29/**
30 * dev_ofnode() - get the DT node reference associated with a udevice
31 *
32 * @dev: device to check
33 * @return reference of the the device's DT node
34 */
35static inline ofnode dev_ofnode(struct udevice *dev)
36{
37 return dev->node;
38}
39
40static inline bool dev_of_valid(struct udevice *dev)
41{
42 return ofnode_valid(dev_ofnode(dev));
43}
44
45#ifdef CONFIG_DM_DEV_READ_INLINE
46
47static inline int dev_read_u32_default(struct udevice *dev,
48 const char *propname, int def)
49{
50 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
51}
52
53/**
54 * dev_read_string() - Read a string from a device's DT property
55 *
56 * @dev: device to read DT property from
57 * @propname: name of the property to read
58 * @return string from property value, or NULL if there is no such property
59 */
60static inline const char *dev_read_string(struct udevice *dev,
61 const char *propname)
62{
63 return ofnode_read_string(dev_ofnode(dev), propname);
64}
65
66/**
67 * dev_read_bool() - read a boolean value from a device's DT property
68 *
69 * @dev: device to read DT property from
70 * @propname: name of property to read
71 * @return true if property is present (meaning true), false if not present
72 */
73static inline bool dev_read_bool(struct udevice *dev, const char *propname)
74{
75 return ofnode_read_bool(dev_ofnode(dev), propname);
76}
77
78/**
79 * dev_read_subnode() - find a named subnode of a device
80 *
81 * @dev: device whose DT node contains the subnode
82 * @subnode_name: name of subnode to find
83 * @return reference to subnode (which can be invalid if there is no such
84 * subnode)
85 */
86static inline ofnode dev_read_subnode(struct udevice *dev,
87 const char *subbnode_name)
88{
89 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
90}
91
92/**
93 * dev_read_size() - read the size of a property
94 *
95 * @dev: device to check
96 * @propname: property to check
97 * @return size of property if present, or -EINVAL if not
98 */
99static inline int dev_read_size(struct udevice *dev, const char *propname)
100{
101 return ofnode_read_size(dev_ofnode(dev), propname);
102}
103
104/**
105 * dev_read_addr_index() - Get the indexed reg property of a device
106 *
107 * @dev: Device to read from
108 * @index: the 'reg' property can hold a list of <addr, size> pairs
109 * and @index is used to select which one is required
110 *
111 * @return address or FDT_ADDR_T_NONE if not found
112 */
113static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
114{
115 return devfdt_get_addr_index(dev, index);
116}
117
118/**
119 * dev_read_addr() - Get the reg property of a device
120 *
121 * @dev: Device to read from
122 *
123 * @return address or FDT_ADDR_T_NONE if not found
124 */
125static inline fdt_addr_t dev_read_addr(struct udevice *dev)
126{
127 return devfdt_get_addr(dev);
128}
129
130/**
131 * dev_read_addr_size() - get address and size from a device property
132 *
133 * This does no address translation. It simply reads an property that contains
134 * an address and a size value, one after the other.
135 *
136 * @dev: Device to read from
137 * @propname: property to read
138 * @sizep: place to put size value (on success)
139 * @return address value, or FDT_ADDR_T_NONE on error
140 */
141static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
142 const char *propname,
143 fdt_size_t *sizep)
144{
145 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
146}
147
148/**
149 * dev_read_name() - get the name of a device's node
150 *
151 * @node: valid node to look up
152 * @return name of node
153 */
154static inline const char *dev_read_name(struct udevice *dev)
155{
156 return ofnode_get_name(dev_ofnode(dev));
157}
158
159/**
160 * dev_read_stringlist_search() - find string in a string list and return index
161 *
162 * Note that it is possible for this function to succeed on property values
163 * that are not NUL-terminated. That's because the function will stop after
164 * finding the first occurrence of @string. This can for example happen with
165 * small-valued cell properties, such as #address-cells, when searching for
166 * the empty string.
167 *
168 * @dev: device to check
169 * @propname: name of the property containing the string list
170 * @string: string to look up in the string list
171 *
172 * @return:
173 * the index of the string in the list of strings
174 * -ENODATA if the property is not found
175 * -EINVAL on some other error
176 */
177static inline int dev_read_stringlist_search(struct udevice *dev,
178 const char *propname,
179 const char *string)
180{
181 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
182}
183
184/**
185 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
186 *
187 * This function is useful to parse lists of phandles and their arguments.
188 * Returns 0 on success and fills out_args, on error returns appropriate
189 * errno value.
190 *
191 * Caller is responsible to call of_node_put() on the returned out_args->np
192 * pointer.
193 *
194 * Example:
195 *
196 * phandle1: node1 {
197 * #list-cells = <2>;
198 * }
199 *
200 * phandle2: node2 {
201 * #list-cells = <1>;
202 * }
203 *
204 * node3 {
205 * list = <&phandle1 1 2 &phandle2 3>;
206 * }
207 *
208 * To get a device_node of the `node2' node you may call this:
209 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
210 *
211 * @dev: device whose node containing a list
212 * @list_name: property name that contains a list
213 * @cells_name: property name that specifies phandles' arguments count
214 * @cells_count: Cell count to use if @cells_name is NULL
215 * @index: index of a phandle to parse out
216 * @out_args: optional pointer to output arguments structure (will be filled)
217 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
218 * @list_name does not exist, -EINVAL if a phandle was not found,
219 * @cells_name could not be found, the arguments were truncated or there
220 * were too many arguments.
221 */
222static inline int dev_read_phandle_with_args(struct udevice *dev,
223 const char *list_name, const char *cells_name, int cell_count,
224 int index, struct ofnode_phandle_args *out_args)
225{
226 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
227 cells_name, cell_count, index,
228 out_args);
229}
230
231/**
232 * dev_read_addr_cells() - Get the number of address cells for a device's node
233 *
234 * This walks back up the tree to find the closest #address-cells property
235 * which controls the given node.
236 *
237 * @dev: devioe to check
238 * @return number of address cells this node uses
239 */
240static inline int dev_read_addr_cells(struct udevice *dev)
241{
242 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
243}
244
245/**
246 * dev_read_size_cells() - Get the number of size cells for a device's node
247 *
248 * This walks back up the tree to find the closest #size-cells property
249 * which controls the given node.
250 *
251 * @dev: devioe to check
252 * @return number of size cells this node uses
253 */
254static inline int dev_read_size_cells(struct udevice *dev)
255{
256 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
257}
258
259/**
260 * dev_read_phandle() - Get the phandle from a device
261 *
262 * @dev: device to check
263 * @return phandle (1 or greater), or 0 if no phandle or other error
264 */
265static inline int dev_read_phandle(struct udevice *dev)
266{
267 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
268}
269
270/**
271 * dev_read_prop()- - read a property from a device's node
272 *
273 * @dev: device to check
274 * @propname: property to read
275 * @lenp: place to put length on success
276 * @return pointer to property, or NULL if not found
277 */
278static inline const u32 *dev_read_prop(struct udevice *dev,
279 const char *propname, int *lenp)
280{
281 return ofnode_read_prop(dev_ofnode(dev), propname, lenp);
282}
283
284/**
285 * dev_read_alias_seq() - Get the alias sequence number of a node
286 *
287 * This works out whether a node is pointed to by an alias, and if so, the
288 * sequence number of that alias. Aliases are of the form <base><num> where
289 * <num> is the sequence number. For example spi2 would be sequence number 2.
290 *
291 * @dev: device to look up
292 * @devnump: set to the sequence number if one is found
293 * @return 0 if a sequence was found, -ve if not
294 */
295static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
296{
297 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
298 dev_of_offset(dev), devnump);
299}
300
301/**
302 * dev_read_u32_array() - Find and read an array of 32 bit integers
303 *
304 * Search for a property in a device node and read 32-bit value(s) from
305 * it.
306 *
307 * The out_values is modified only if a valid u32 value can be decoded.
308 *
309 * @dev: device to look up
310 * @propname: name of the property to read
311 * @out_values: pointer to return value, modified only if return value is 0
312 * @sz: number of array elements to read
313 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
314 * property does not have a value, and -EOVERFLOW if the property data isn't
315 * large enough.
316 */
317static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
318 u32 *out_values, size_t sz)
319{
320 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
321}
322
323/**
324 * dev_read_first_subnode() - find the first subnode of a device's node
325 *
326 * @dev: device to look up
327 * @return reference to the first subnode (which can be invalid if the device's
328 * node has no subnodes)
329 */
330static inline ofnode dev_read_first_subnode(struct udevice *dev)
331{
332 return ofnode_first_subnode(dev_ofnode(dev));
333}
334
335/**
336 * ofnode_next_subnode() - find the next sibling of a subnode
337 *
338 * @node: valid reference to previous node (sibling)
339 * @return reference to the next subnode (which can be invalid if the node
340 * has no more siblings)
341 */
342static inline ofnode dev_read_next_subnode(ofnode node)
343{
344 return ofnode_next_subnode(node);
345}
346
347/**
348 * dev_read_u8_array_ptr() - find an 8-bit array
349 *
350 * Look up a device's node property and return a pointer to its contents as a
351 * byte array of given length. The property must have at least enough data
352 * for the array (count bytes). It may have more, but this will be ignored.
353 * The data is not copied.
354 *
355 * @dev: device to look up
356 * @propname: name of property to find
357 * @sz: number of array elements
358 * @return pointer to byte array if found, or NULL if the property is not
359 * found or there is not enough data
360 */
361static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
362 const char *propname, size_t sz)
363{
364 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
365}
366
367#endif /* CONFIG_DM_DEV_READ_INLINE */
368
369/**
370 * dev_for_each_subnode() - Helper function to iterate through subnodes
371 *
372 * This creates a for() loop which works through the subnodes in a device's
373 * device-tree node.
374 *
375 * @subnode: ofnode holding the current subnode
376 * @dev: device to use for interation (struct udevice *)
377 */
378#define dev_for_each_subnode(subnode, dev) \
379 for (subnode = dev_read_first_subnode(dev); \
380 ofnode_valid(subnode); \
381 subnode = ofnode_next_subnode(subnode))
382
383#endif