2 * Copyright (c) 2013 Google, Inc
5 * Pavel Herrmann <morpheus.ibis@gmail.com>
6 * Marek Vasut <marex@denx.de>
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <dm/uclass-id.h>
15 #include <linker_lists.h>
16 #include <linux/list.h>
20 /* Driver is active (probed). Cleared when it is removed */
21 #define DM_FLAG_ACTIVATED (1 << 0)
23 /* DM is responsible for allocating and freeing platdata */
24 #define DM_FLAG_ALLOC_PDATA (1 << 1)
26 /* DM should init this device prior to relocation */
27 #define DM_FLAG_PRE_RELOC (1 << 2)
30 * struct udevice - An instance of a driver
32 * This holds information about a device, which is a driver bound to a
33 * particular port or peripheral (essentially a driver instance).
35 * A device will come into existence through a 'bind' call, either due to
36 * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
37 * in the device tree (in which case of_offset is >= 0). In the latter case
38 * we translate the device tree information into platdata in a function
39 * implemented by the driver ofdata_to_platdata method (called just before the
40 * probe method if the device has a device tree node.
42 * All three of platdata, priv and uclass_priv can be allocated by the
43 * driver, or you can use the auto_alloc_size members of struct driver and
44 * struct uclass_driver to have driver model do this automatically.
46 * @driver: The driver used by this device
47 * @name: Name of device, typically the FDT node name
48 * @platdata: Configuration data for this device
49 * @of_offset: Device tree node offset for this device (- for none)
50 * @parent: Parent of this device, or NULL for the top level device
51 * @priv: Private data for this device
52 * @uclass: Pointer to uclass for this device
53 * @uclass_priv: The uclass's private data for this device
54 * @parent_priv: The parent's private data for this device
55 * @uclass_node: Used by uclass to link its devices
56 * @child_head: List of children of this device
57 * @sibling_node: Next device in list of all devices
58 * @flags: Flags for this device DM_FLAG_...
59 * @req_seq: Requested sequence number for this device (-1 = any)
60 * @seq: Allocated sequence number for this device (-1 = none)
63 struct driver *driver;
67 struct udevice *parent;
69 struct uclass *uclass;
72 struct list_head uclass_node;
73 struct list_head child_head;
74 struct list_head sibling_node;
80 /* Maximum sequence number supported */
81 #define DM_MAX_SEQ 999
83 /* Returns the operations for a device */
84 #define device_get_ops(dev) (dev->driver->ops)
86 /* Returns non-zero if the device is active (probed and not removed) */
87 #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
90 * struct udevice_id - Lists the compatible strings supported by a driver
91 * @compatible: Compatible string
92 * @data: Data for this compatible string
95 const char *compatible;
100 * struct driver - A driver for a feature or peripheral
102 * This holds methods for setting up a new device, and also removing it.
103 * The device needs information to set itself up - this is provided either
104 * by platdata or a device tree node (which we find by looking up
105 * matching compatible strings with of_match).
107 * Drivers all belong to a uclass, representing a class of devices of the
108 * same type. Common elements of the drivers can be implemented in the uclass,
109 * or the uclass can provide a consistent interface to the drivers within
113 * @id: Identiies the uclass we belong to
114 * @of_match: List of compatible strings to match, and any identifying data
116 * @bind: Called to bind a device to its driver
117 * @probe: Called to probe a device, i.e. activate it
118 * @remove: Called to remove a device, i.e. de-activate it
119 * @unbind: Called to unbind a device from its driver
120 * @ofdata_to_platdata: Called before probe to decode device tree data
121 * @child_pre_probe: Called before a child device is probed. The device has
122 * memory allocated but it has not yet been probed.
123 * @child_post_remove: Called after a child device is removed. The device
124 * has memory allocated but its device_remove() method has been called.
125 * @priv_auto_alloc_size: If non-zero this is the size of the private data
126 * to be allocated in the device's ->priv pointer. If zero, then the driver
127 * is responsible for allocating any data required.
128 * @platdata_auto_alloc_size: If non-zero this is the size of the
129 * platform data to be allocated in the device's ->platdata pointer.
130 * This is typically only useful for device-tree-aware drivers (those with
131 * an of_match), since drivers which use platdata will have the data
132 * provided in the U_BOOT_DEVICE() instantiation.
133 * @per_child_auto_alloc_size: Each device can hold private data owned by
134 * its parent. If required this will be automatically allocated if this
136 * @ops: Driver-specific operations. This is typically a list of function
137 * pointers defined by the driver, to implement driver functions required by
139 * @flags: driver flags - see DM_FLAGS_...
144 const struct udevice_id *of_match;
145 int (*bind)(struct udevice *dev);
146 int (*probe)(struct udevice *dev);
147 int (*remove)(struct udevice *dev);
148 int (*unbind)(struct udevice *dev);
149 int (*ofdata_to_platdata)(struct udevice *dev);
150 int (*child_pre_probe)(struct udevice *dev);
151 int (*child_post_remove)(struct udevice *dev);
152 int priv_auto_alloc_size;
153 int platdata_auto_alloc_size;
154 int per_child_auto_alloc_size;
155 const void *ops; /* driver-specific operations */
159 /* Declare a new U-Boot driver */
160 #define U_BOOT_DRIVER(__name) \
161 ll_entry_declare(struct driver, __name, driver)
164 * dev_get_platdata() - Get the platform data for a device
166 * This checks that dev is not NULL, but no other checks for now
168 * @dev Device to check
169 * @return platform data, or NULL if none
171 void *dev_get_platdata(struct udevice *dev);
174 * dev_get_parentdata() - Get the parent data for a device
176 * The parent data is data stored in the device but owned by the parent.
177 * For example, a USB device may have parent data which contains information
178 * about how to talk to the device over USB.
180 * This checks that dev is not NULL, but no other checks for now
182 * @dev Device to check
183 * @return parent data, or NULL if none
185 void *dev_get_parentdata(struct udevice *dev);
188 * dev_get_priv() - Get the private data for a device
190 * This checks that dev is not NULL, but no other checks for now
192 * @dev Device to check
193 * @return private data, or NULL if none
195 void *dev_get_priv(struct udevice *dev);
198 * device_get_child() - Get the child of a device by index
200 * Returns the numbered child, 0 being the first. This does not use
201 * sequence numbers, only the natural order.
203 * @dev: Parent device to check
204 * @index: Child index
205 * @devp: Returns pointer to device
207 int device_get_child(struct udevice *parent, int index, struct udevice **devp);
210 * device_find_child_by_seq() - Find a child device based on a sequence
212 * This searches for a device with the given seq or req_seq.
214 * For seq, if an active device has this sequence it will be returned.
215 * If there is no such device then this will return -ENODEV.
217 * For req_seq, if a device (whether activated or not) has this req_seq
218 * value, that device will be returned. This is a strong indication that
219 * the device will receive that sequence when activated.
221 * @parent: Parent device
222 * @seq_or_req_seq: Sequence number to find (0=first)
223 * @find_req_seq: true to find req_seq, false to find seq
224 * @devp: Returns pointer to device (there is only one per for each seq).
225 * Set to NULL if none is found
226 * @return 0 if OK, -ve on error
228 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
229 bool find_req_seq, struct udevice **devp);
232 * device_get_child_by_seq() - Get a child device based on a sequence
234 * If an active device has this sequence it will be returned. If there is no
235 * such device then this will check for a device that is requesting this
238 * The device is probed to activate it ready for use.
240 * @parent: Parent device
241 * @seq: Sequence number to find (0=first)
242 * @devp: Returns pointer to device (there is only one per for each seq)
243 * Set to NULL if none is found
244 * @return 0 if OK, -ve on error
246 int device_get_child_by_seq(struct udevice *parent, int seq,
247 struct udevice **devp);
250 * device_find_child_by_of_offset() - Find a child device based on FDT offset
252 * Locates a child device by its device tree offset.
254 * @parent: Parent device
255 * @of_offset: Device tree offset to find
256 * @devp: Returns pointer to device if found, otherwise this is set to NULL
257 * @return 0 if OK, -ve on error
259 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
260 struct udevice **devp);
263 * device_get_child_by_of_offset() - Get a child device based on FDT offset
265 * Locates a child device by its device tree offset.
267 * The device is probed to activate it ready for use.
269 * @parent: Parent device
270 * @of_offset: Device tree offset to find
271 * @devp: Returns pointer to device if found, otherwise this is set to NULL
272 * @return 0 if OK, -ve on error
274 int device_get_child_by_of_offset(struct udevice *parent, int seq,
275 struct udevice **devp);