xref: /openbmc/u-boot/include/dm/device.h (revision ed09a554)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * (C) Copyright 2012
5  * Pavel Herrmann <morpheus.ibis@gmail.com>
6  * Marek Vasut <marex@denx.de>
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 
11 #ifndef _DM_DEVICE_H
12 #define _DM_DEVICE_H
13 
14 #include <dm/uclass-id.h>
15 #include <fdtdec.h>
16 #include <linker_lists.h>
17 #include <linux/list.h>
18 
19 struct driver_info;
20 
21 /* Driver is active (probed). Cleared when it is removed */
22 #define DM_FLAG_ACTIVATED	(1 << 0)
23 
24 /* DM is responsible for allocating and freeing platdata */
25 #define DM_FLAG_ALLOC_PDATA	(1 << 1)
26 
27 /* DM should init this device prior to relocation */
28 #define DM_FLAG_PRE_RELOC	(1 << 2)
29 
30 /* DM is responsible for allocating and freeing parent_platdata */
31 #define DM_FLAG_ALLOC_PARENT_PDATA	(1 << 3)
32 
33 /* Allocate driver private data on a DMA boundary */
34 #define DM_FLAG_ALLOC_PRIV_DMA	(1 << 4)
35 
36 /**
37  * struct udevice - An instance of a driver
38  *
39  * This holds information about a device, which is a driver bound to a
40  * particular port or peripheral (essentially a driver instance).
41  *
42  * A device will come into existence through a 'bind' call, either due to
43  * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
44  * in the device tree (in which case of_offset is >= 0). In the latter case
45  * we translate the device tree information into platdata in a function
46  * implemented by the driver ofdata_to_platdata method (called just before the
47  * probe method if the device has a device tree node.
48  *
49  * All three of platdata, priv and uclass_priv can be allocated by the
50  * driver, or you can use the auto_alloc_size members of struct driver and
51  * struct uclass_driver to have driver model do this automatically.
52  *
53  * @driver: The driver used by this device
54  * @name: Name of device, typically the FDT node name
55  * @platdata: Configuration data for this device
56  * @parent_platdata: The parent bus's configuration data for this device
57  * @of_offset: Device tree node offset for this device (- for none)
58  * @driver_data: Driver data word for the entry that matched this device with
59  *		its driver
60  * @parent: Parent of this device, or NULL for the top level device
61  * @priv: Private data for this device
62  * @uclass: Pointer to uclass for this device
63  * @uclass_priv: The uclass's private data for this device
64  * @parent_priv: The parent's private data for this device
65  * @uclass_node: Used by uclass to link its devices
66  * @child_head: List of children of this device
67  * @sibling_node: Next device in list of all devices
68  * @flags: Flags for this device DM_FLAG_...
69  * @req_seq: Requested sequence number for this device (-1 = any)
70  * @seq: Allocated sequence number for this device (-1 = none). This is set up
71  * when the device is probed and will be unique within the device's uclass.
72  */
73 struct udevice {
74 	const struct driver *driver;
75 	const char *name;
76 	void *platdata;
77 	void *parent_platdata;
78 	int of_offset;
79 	ulong driver_data;
80 	struct udevice *parent;
81 	void *priv;
82 	struct uclass *uclass;
83 	void *uclass_priv;
84 	void *parent_priv;
85 	struct list_head uclass_node;
86 	struct list_head child_head;
87 	struct list_head sibling_node;
88 	uint32_t flags;
89 	int req_seq;
90 	int seq;
91 };
92 
93 /* Maximum sequence number supported */
94 #define DM_MAX_SEQ	999
95 
96 /* Returns the operations for a device */
97 #define device_get_ops(dev)	(dev->driver->ops)
98 
99 /* Returns non-zero if the device is active (probed and not removed) */
100 #define device_active(dev)	((dev)->flags & DM_FLAG_ACTIVATED)
101 
102 /**
103  * struct udevice_id - Lists the compatible strings supported by a driver
104  * @compatible: Compatible string
105  * @data: Data for this compatible string
106  */
107 struct udevice_id {
108 	const char *compatible;
109 	ulong data;
110 };
111 
112 #ifdef CONFIG_OF_CONTROL
113 #define of_match_ptr(_ptr)	(_ptr)
114 #else
115 #define of_match_ptr(_ptr)	NULL
116 #endif /* CONFIG_OF_CONTROL */
117 
118 /**
119  * struct driver - A driver for a feature or peripheral
120  *
121  * This holds methods for setting up a new device, and also removing it.
122  * The device needs information to set itself up - this is provided either
123  * by platdata or a device tree node (which we find by looking up
124  * matching compatible strings with of_match).
125  *
126  * Drivers all belong to a uclass, representing a class of devices of the
127  * same type. Common elements of the drivers can be implemented in the uclass,
128  * or the uclass can provide a consistent interface to the drivers within
129  * it.
130  *
131  * @name: Device name
132  * @id: Identiies the uclass we belong to
133  * @of_match: List of compatible strings to match, and any identifying data
134  * for each.
135  * @bind: Called to bind a device to its driver
136  * @probe: Called to probe a device, i.e. activate it
137  * @remove: Called to remove a device, i.e. de-activate it
138  * @unbind: Called to unbind a device from its driver
139  * @ofdata_to_platdata: Called before probe to decode device tree data
140  * @child_post_bind: Called after a new child has been bound
141  * @child_pre_probe: Called before a child device is probed. The device has
142  * memory allocated but it has not yet been probed.
143  * @child_post_remove: Called after a child device is removed. The device
144  * has memory allocated but its device_remove() method has been called.
145  * @priv_auto_alloc_size: If non-zero this is the size of the private data
146  * to be allocated in the device's ->priv pointer. If zero, then the driver
147  * is responsible for allocating any data required.
148  * @platdata_auto_alloc_size: If non-zero this is the size of the
149  * platform data to be allocated in the device's ->platdata pointer.
150  * This is typically only useful for device-tree-aware drivers (those with
151  * an of_match), since drivers which use platdata will have the data
152  * provided in the U_BOOT_DEVICE() instantiation.
153  * @per_child_auto_alloc_size: Each device can hold private data owned by
154  * its parent. If required this will be automatically allocated if this
155  * value is non-zero.
156  * TODO(sjg@chromium.org): I'm considering dropping this, and just having
157  * device_probe_child() pass it in. So far the use case for allocating it
158  * is SPI, but I found that unsatisfactory. Since it is here I will leave it
159  * until things are clearer.
160  * @per_child_platdata_auto_alloc_size: A bus likes to store information about
161  * its children. If non-zero this is the size of this data, to be allocated
162  * in the child's parent_platdata pointer.
163  * @ops: Driver-specific operations. This is typically a list of function
164  * pointers defined by the driver, to implement driver functions required by
165  * the uclass.
166  * @flags: driver flags - see DM_FLAGS_...
167  */
168 struct driver {
169 	char *name;
170 	enum uclass_id id;
171 	const struct udevice_id *of_match;
172 	int (*bind)(struct udevice *dev);
173 	int (*probe)(struct udevice *dev);
174 	int (*remove)(struct udevice *dev);
175 	int (*unbind)(struct udevice *dev);
176 	int (*ofdata_to_platdata)(struct udevice *dev);
177 	int (*child_post_bind)(struct udevice *dev);
178 	int (*child_pre_probe)(struct udevice *dev);
179 	int (*child_post_remove)(struct udevice *dev);
180 	int priv_auto_alloc_size;
181 	int platdata_auto_alloc_size;
182 	int per_child_auto_alloc_size;
183 	int per_child_platdata_auto_alloc_size;
184 	const void *ops;	/* driver-specific operations */
185 	uint32_t flags;
186 };
187 
188 /* Declare a new U-Boot driver */
189 #define U_BOOT_DRIVER(__name)						\
190 	ll_entry_declare(struct driver, __name, driver)
191 
192 /**
193  * dev_get_platdata() - Get the platform data for a device
194  *
195  * This checks that dev is not NULL, but no other checks for now
196  *
197  * @dev		Device to check
198  * @return platform data, or NULL if none
199  */
200 void *dev_get_platdata(struct udevice *dev);
201 
202 /**
203  * dev_get_parent_platdata() - Get the parent platform data for a device
204  *
205  * This checks that dev is not NULL, but no other checks for now
206  *
207  * @dev		Device to check
208  * @return parent's platform data, or NULL if none
209  */
210 void *dev_get_parent_platdata(struct udevice *dev);
211 
212 /**
213  * dev_get_parentdata() - Get the parent data for a device
214  *
215  * The parent data is data stored in the device but owned by the parent.
216  * For example, a USB device may have parent data which contains information
217  * about how to talk to the device over USB.
218  *
219  * This checks that dev is not NULL, but no other checks for now
220  *
221  * @dev		Device to check
222  * @return parent data, or NULL if none
223  */
224 void *dev_get_parentdata(struct udevice *dev);
225 
226 /**
227  * dev_get_priv() - Get the private data for a device
228  *
229  * This checks that dev is not NULL, but no other checks for now
230  *
231  * @dev		Device to check
232  * @return private data, or NULL if none
233  */
234 void *dev_get_priv(struct udevice *dev);
235 
236 /**
237  * struct dev_get_parent() - Get the parent of a device
238  *
239  * @child:	Child to check
240  * @return parent of child, or NULL if this is the root device
241  */
242 struct udevice *dev_get_parent(struct udevice *child);
243 
244 /**
245  * dev_get_uclass_priv() - Get the private uclass data for a device
246  *
247  * This checks that dev is not NULL, but no other checks for now
248  *
249  * @dev		Device to check
250  * @return private uclass data for this device, or NULL if none
251  */
252 void *dev_get_uclass_priv(struct udevice *dev);
253 
254 /**
255  * dev_get_driver_data() - get the driver data used to bind a device
256  *
257  * When a device is bound using a device tree node, it matches a
258  * particular compatible string as in struct udevice_id. This function
259  * returns the associated data value for that compatible string. This is
260  * the 'data' field in struct udevice_id.
261  *
262  * For USB devices, this is the driver_info field in struct usb_device_id.
263  *
264  * @dev:	Device to check
265  */
266 ulong dev_get_driver_data(struct udevice *dev);
267 
268 /*
269  * device_get_uclass_id() - return the uclass ID of a device
270  *
271  * @dev:	Device to check
272  * @return uclass ID for the device
273  */
274 enum uclass_id device_get_uclass_id(struct udevice *dev);
275 
276 /**
277  * device_get_child() - Get the child of a device by index
278  *
279  * Returns the numbered child, 0 being the first. This does not use
280  * sequence numbers, only the natural order.
281  *
282  * @dev:	Parent device to check
283  * @index:	Child index
284  * @devp:	Returns pointer to device
285  */
286 int device_get_child(struct udevice *parent, int index, struct udevice **devp);
287 
288 /**
289  * device_find_child_by_seq() - Find a child device based on a sequence
290  *
291  * This searches for a device with the given seq or req_seq.
292  *
293  * For seq, if an active device has this sequence it will be returned.
294  * If there is no such device then this will return -ENODEV.
295  *
296  * For req_seq, if a device (whether activated or not) has this req_seq
297  * value, that device will be returned. This is a strong indication that
298  * the device will receive that sequence when activated.
299  *
300  * @parent: Parent device
301  * @seq_or_req_seq: Sequence number to find (0=first)
302  * @find_req_seq: true to find req_seq, false to find seq
303  * @devp: Returns pointer to device (there is only one per for each seq).
304  * Set to NULL if none is found
305  * @return 0 if OK, -ve on error
306  */
307 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
308 			     bool find_req_seq, struct udevice **devp);
309 
310 /**
311  * device_get_child_by_seq() - Get a child device based on a sequence
312  *
313  * If an active device has this sequence it will be returned. If there is no
314  * such device then this will check for a device that is requesting this
315  * sequence.
316  *
317  * The device is probed to activate it ready for use.
318  *
319  * @parent: Parent device
320  * @seq: Sequence number to find (0=first)
321  * @devp: Returns pointer to device (there is only one per for each seq)
322  * Set to NULL if none is found
323  * @return 0 if OK, -ve on error
324  */
325 int device_get_child_by_seq(struct udevice *parent, int seq,
326 			    struct udevice **devp);
327 
328 /**
329  * device_find_child_by_of_offset() - Find a child device based on FDT offset
330  *
331  * Locates a child device by its device tree offset.
332  *
333  * @parent: Parent device
334  * @of_offset: Device tree offset to find
335  * @devp: Returns pointer to device if found, otherwise this is set to NULL
336  * @return 0 if OK, -ve on error
337  */
338 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
339 				   struct udevice **devp);
340 
341 /**
342  * device_get_child_by_of_offset() - Get a child device based on FDT offset
343  *
344  * Locates a child device by its device tree offset.
345  *
346  * The device is probed to activate it ready for use.
347  *
348  * @parent: Parent device
349  * @of_offset: Device tree offset to find
350  * @devp: Returns pointer to device if found, otherwise this is set to NULL
351  * @return 0 if OK, -ve on error
352  */
353 int device_get_child_by_of_offset(struct udevice *parent, int seq,
354 				  struct udevice **devp);
355 
356 /**
357  * device_find_first_child() - Find the first child of a device
358  *
359  * @parent: Parent device to search
360  * @devp: Returns first child device, or NULL if none
361  * @return 0
362  */
363 int device_find_first_child(struct udevice *parent, struct udevice **devp);
364 
365 /**
366  * device_find_first_child() - Find the first child of a device
367  *
368  * @devp: Pointer to previous child device on entry. Returns pointer to next
369  *		child device, or NULL if none
370  * @return 0
371  */
372 int device_find_next_child(struct udevice **devp);
373 
374 /**
375  * dev_get_addr() - Get the reg property of a device
376  *
377  * @dev: Pointer to a device
378  *
379  * @return addr
380  */
381 fdt_addr_t dev_get_addr(struct udevice *dev);
382 
383 /**
384  * device_has_children() - check if a device has any children
385  *
386  * @dev:	Device to check
387  * @return true if the device has one or more children
388  */
389 bool device_has_children(struct udevice *dev);
390 
391 /**
392  * device_has_active_children() - check if a device has any active children
393  *
394  * @dev:	Device to check
395  * @return true if the device has one or more children and at least one of
396  * them is active (probed).
397  */
398 bool device_has_active_children(struct udevice *dev);
399 
400 /**
401  * device_is_last_sibling() - check if a device is the last sibling
402  *
403  * This function can be useful for display purposes, when special action needs
404  * to be taken when displaying the last sibling. This can happen when a tree
405  * view of devices is being displayed.
406  *
407  * @dev:	Device to check
408  * @return true if there are no more siblings after this one - i.e. is it
409  * last in the list.
410  */
411 bool device_is_last_sibling(struct udevice *dev);
412 
413 #endif
414