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)
29 /* DM is responsible for allocating and freeing parent_platdata */
30 #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
33 * struct udevice - An instance of a driver
35 * This holds information about a device, which is a driver bound to a
36 * particular port or peripheral (essentially a driver instance).
38 * A device will come into existence through a 'bind' call, either due to
39 * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
40 * in the device tree (in which case of_offset is >= 0). In the latter case
41 * we translate the device tree information into platdata in a function
42 * implemented by the driver ofdata_to_platdata method (called just before the
43 * probe method if the device has a device tree node.
45 * All three of platdata, priv and uclass_priv can be allocated by the
46 * driver, or you can use the auto_alloc_size members of struct driver and
47 * struct uclass_driver to have driver model do this automatically.
49 * @driver: The driver used by this device
50 * @name: Name of device, typically the FDT node name
51 * @platdata: Configuration data for this device
52 * @parent_platdata: The parent bus's configuration data for this device
53 * @of_offset: Device tree node offset for this device (- for none)
54 * @of_id: Pointer to the udevice_id structure which created the device
55 * @parent: Parent of this device, or NULL for the top level device
56 * @priv: Private data for this device
57 * @uclass: Pointer to uclass for this device
58 * @uclass_priv: The uclass's private data for this device
59 * @parent_priv: The parent's private data for this device
60 * @uclass_node: Used by uclass to link its devices
61 * @child_head: List of children of this device
62 * @sibling_node: Next device in list of all devices
63 * @flags: Flags for this device DM_FLAG_...
64 * @req_seq: Requested sequence number for this device (-1 = any)
65 * @seq: Allocated sequence number for this device (-1 = none). This is set up
66 * when the device is probed and will be unique within the device's uclass.
69 struct driver *driver;
72 void *parent_platdata;
74 const struct udevice_id *of_id;
75 struct udevice *parent;
77 struct uclass *uclass;
80 struct list_head uclass_node;
81 struct list_head child_head;
82 struct list_head sibling_node;
88 /* Maximum sequence number supported */
89 #define DM_MAX_SEQ 999
91 /* Returns the operations for a device */
92 #define device_get_ops(dev) (dev->driver->ops)
94 /* Returns non-zero if the device is active (probed and not removed) */
95 #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
98 * struct udevice_id - Lists the compatible strings supported by a driver
99 * @compatible: Compatible string
100 * @data: Data for this compatible string
103 const char *compatible;
107 #ifdef CONFIG_OF_CONTROL
108 #define of_match_ptr(_ptr) (_ptr)
110 #define of_match_ptr(_ptr) NULL
111 #endif /* CONFIG_OF_CONTROL */
114 * struct driver - A driver for a feature or peripheral
116 * This holds methods for setting up a new device, and also removing it.
117 * The device needs information to set itself up - this is provided either
118 * by platdata or a device tree node (which we find by looking up
119 * matching compatible strings with of_match).
121 * Drivers all belong to a uclass, representing a class of devices of the
122 * same type. Common elements of the drivers can be implemented in the uclass,
123 * or the uclass can provide a consistent interface to the drivers within
127 * @id: Identiies the uclass we belong to
128 * @of_match: List of compatible strings to match, and any identifying data
130 * @bind: Called to bind a device to its driver
131 * @probe: Called to probe a device, i.e. activate it
132 * @remove: Called to remove a device, i.e. de-activate it
133 * @unbind: Called to unbind a device from its driver
134 * @ofdata_to_platdata: Called before probe to decode device tree data
135 * @child_post_bind: Called after a new child has been bound
136 * @child_pre_probe: Called before a child device is probed. The device has
137 * memory allocated but it has not yet been probed.
138 * @child_post_remove: Called after a child device is removed. The device
139 * has memory allocated but its device_remove() method has been called.
140 * @priv_auto_alloc_size: If non-zero this is the size of the private data
141 * to be allocated in the device's ->priv pointer. If zero, then the driver
142 * is responsible for allocating any data required.
143 * @platdata_auto_alloc_size: If non-zero this is the size of the
144 * platform data to be allocated in the device's ->platdata pointer.
145 * This is typically only useful for device-tree-aware drivers (those with
146 * an of_match), since drivers which use platdata will have the data
147 * provided in the U_BOOT_DEVICE() instantiation.
148 * @per_child_auto_alloc_size: Each device can hold private data owned by
149 * its parent. If required this will be automatically allocated if this
151 * TODO(sjg@chromium.org): I'm considering dropping this, and just having
152 * device_probe_child() pass it in. So far the use case for allocating it
153 * is SPI, but I found that unsatisfactory. Since it is here I will leave it
154 * until things are clearer.
155 * @per_child_platdata_auto_alloc_size: A bus likes to store information about
156 * its children. If non-zero this is the size of this data, to be allocated
157 * in the child's parent_platdata pointer.
158 * @ops: Driver-specific operations. This is typically a list of function
159 * pointers defined by the driver, to implement driver functions required by
161 * @flags: driver flags - see DM_FLAGS_...
166 const struct udevice_id *of_match;
167 int (*bind)(struct udevice *dev);
168 int (*probe)(struct udevice *dev);
169 int (*remove)(struct udevice *dev);
170 int (*unbind)(struct udevice *dev);
171 int (*ofdata_to_platdata)(struct udevice *dev);
172 int (*child_post_bind)(struct udevice *dev);
173 int (*child_pre_probe)(struct udevice *dev);
174 int (*child_post_remove)(struct udevice *dev);
175 int priv_auto_alloc_size;
176 int platdata_auto_alloc_size;
177 int per_child_auto_alloc_size;
178 int per_child_platdata_auto_alloc_size;
179 const void *ops; /* driver-specific operations */
183 /* Declare a new U-Boot driver */
184 #define U_BOOT_DRIVER(__name) \
185 ll_entry_declare(struct driver, __name, driver)
188 * dev_get_platdata() - Get the platform 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 platform data, or NULL if none
195 void *dev_get_platdata(struct udevice *dev);
198 * dev_get_parent_platdata() - Get the parent platform data for a device
200 * This checks that dev is not NULL, but no other checks for now
202 * @dev Device to check
203 * @return parent's platform data, or NULL if none
205 void *dev_get_parent_platdata(struct udevice *dev);
208 * dev_get_parentdata() - Get the parent data for a device
210 * The parent data is data stored in the device but owned by the parent.
211 * For example, a USB device may have parent data which contains information
212 * about how to talk to the device over USB.
214 * This checks that dev is not NULL, but no other checks for now
216 * @dev Device to check
217 * @return parent data, or NULL if none
219 void *dev_get_parentdata(struct udevice *dev);
222 * dev_get_priv() - Get the private data for a device
224 * This checks that dev is not NULL, but no other checks for now
226 * @dev Device to check
227 * @return private data, or NULL if none
229 void *dev_get_priv(struct udevice *dev);
232 * struct dev_get_parent() - Get the parent of a device
234 * @child: Child to check
235 * @return parent of child, or NULL if this is the root device
237 struct udevice *dev_get_parent(struct udevice *child);
240 * dev_get_of_data() - get the device tree data used to bind a device
242 * When a device is bound using a device tree node, it matches a
243 * particular compatible string as in struct udevice_id. This function
244 * returns the associated data value for that compatible string
246 ulong dev_get_of_data(struct udevice *dev);
249 * device_get_uclass_id() - return the uclass ID of a device
251 * @dev: Device to check
252 * @return uclass ID for the device
254 enum uclass_id device_get_uclass_id(struct udevice *dev);
257 * device_get_child() - Get the child of a device by index
259 * Returns the numbered child, 0 being the first. This does not use
260 * sequence numbers, only the natural order.
262 * @dev: Parent device to check
263 * @index: Child index
264 * @devp: Returns pointer to device
266 int device_get_child(struct udevice *parent, int index, struct udevice **devp);
269 * device_find_child_by_seq() - Find a child device based on a sequence
271 * This searches for a device with the given seq or req_seq.
273 * For seq, if an active device has this sequence it will be returned.
274 * If there is no such device then this will return -ENODEV.
276 * For req_seq, if a device (whether activated or not) has this req_seq
277 * value, that device will be returned. This is a strong indication that
278 * the device will receive that sequence when activated.
280 * @parent: Parent device
281 * @seq_or_req_seq: Sequence number to find (0=first)
282 * @find_req_seq: true to find req_seq, false to find seq
283 * @devp: Returns pointer to device (there is only one per for each seq).
284 * Set to NULL if none is found
285 * @return 0 if OK, -ve on error
287 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
288 bool find_req_seq, struct udevice **devp);
291 * device_get_child_by_seq() - Get a child device based on a sequence
293 * If an active device has this sequence it will be returned. If there is no
294 * such device then this will check for a device that is requesting this
297 * The device is probed to activate it ready for use.
299 * @parent: Parent device
300 * @seq: Sequence number to find (0=first)
301 * @devp: Returns pointer to device (there is only one per for each seq)
302 * Set to NULL if none is found
303 * @return 0 if OK, -ve on error
305 int device_get_child_by_seq(struct udevice *parent, int seq,
306 struct udevice **devp);
309 * device_find_child_by_of_offset() - Find a child device based on FDT offset
311 * Locates a child device by its device tree offset.
313 * @parent: Parent device
314 * @of_offset: Device tree offset to find
315 * @devp: Returns pointer to device if found, otherwise this is set to NULL
316 * @return 0 if OK, -ve on error
318 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
319 struct udevice **devp);
322 * device_get_child_by_of_offset() - Get a child device based on FDT offset
324 * Locates a child device by its device tree offset.
326 * The device is probed to activate it ready for use.
328 * @parent: Parent device
329 * @of_offset: Device tree offset to find
330 * @devp: Returns pointer to device if found, otherwise this is set to NULL
331 * @return 0 if OK, -ve on error
333 int device_get_child_by_of_offset(struct udevice *parent, int seq,
334 struct udevice **devp);
337 * device_find_first_child() - Find the first child of a device
339 * @parent: Parent device to search
340 * @devp: Returns first child device, or NULL if none
343 int device_find_first_child(struct udevice *parent, struct udevice **devp);
346 * device_find_first_child() - Find the first child of a device
348 * @devp: Pointer to previous child device on entry. Returns pointer to next
349 * child device, or NULL if none
352 int device_find_next_child(struct udevice **devp);