blob: 096d84b7b6fe3c77b79e9234ed1f11a2cd23cb21 [file] [log] [blame]
Simon Glass6494d702014-02-26 15:59:18 -07001/*
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 <linker_lists.h>
16#include <linux/list.h>
17
18struct driver_info;
19
20/* Driver is active (probed). Cleared when it is removed */
21#define DM_FLAG_ACTIVATED (1 << 0)
22
23/* DM is responsible for allocating and freeing platdata */
Simon Glassf2bc6fc2014-06-11 23:29:54 -060024#define DM_FLAG_ALLOC_PDATA (1 << 1)
Simon Glass6494d702014-02-26 15:59:18 -070025
Simon Glass00606d72014-07-23 06:55:03 -060026/* DM should init this device prior to relocation */
27#define DM_FLAG_PRE_RELOC (1 << 2)
28
Simon Glasscdc133b2015-01-25 08:27:01 -070029/* DM is responsible for allocating and freeing parent_platdata */
30#define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
31
Simon Glass6494d702014-02-26 15:59:18 -070032/**
Heiko Schocher54c5d082014-05-22 12:43:05 +020033 * struct udevice - An instance of a driver
Simon Glass6494d702014-02-26 15:59:18 -070034 *
35 * This holds information about a device, which is a driver bound to a
36 * particular port or peripheral (essentially a driver instance).
37 *
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.
44 *
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.
48 *
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
Simon Glasscdc133b2015-01-25 08:27:01 -070052 * @parent_platdata: The parent bus's configuration data for this device
Simon Glass6494d702014-02-26 15:59:18 -070053 * @of_offset: Device tree node offset for this device (- for none)
Simon Glass2ef249b2014-11-11 10:46:18 -070054 * @of_id: Pointer to the udevice_id structure which created the device
Simon Glass6494d702014-02-26 15:59:18 -070055 * @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
Simon Glasse59f4582014-07-23 06:55:20 -060059 * @parent_priv: The parent's private data for this device
Simon Glass6494d702014-02-26 15:59:18 -070060 * @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_...
Simon Glass5a66a8f2014-07-23 06:55:12 -060064 * @req_seq: Requested sequence number for this device (-1 = any)
Simon Glass547cea12014-10-13 23:41:51 -060065 * @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.
Simon Glass6494d702014-02-26 15:59:18 -070067 */
Heiko Schocher54c5d082014-05-22 12:43:05 +020068struct udevice {
Simon Glass6494d702014-02-26 15:59:18 -070069 struct driver *driver;
70 const char *name;
71 void *platdata;
Simon Glasscdc133b2015-01-25 08:27:01 -070072 void *parent_platdata;
Simon Glass6494d702014-02-26 15:59:18 -070073 int of_offset;
Simon Glass2ef249b2014-11-11 10:46:18 -070074 const struct udevice_id *of_id;
Heiko Schocher54c5d082014-05-22 12:43:05 +020075 struct udevice *parent;
Simon Glass6494d702014-02-26 15:59:18 -070076 void *priv;
77 struct uclass *uclass;
78 void *uclass_priv;
Simon Glasse59f4582014-07-23 06:55:20 -060079 void *parent_priv;
Simon Glass6494d702014-02-26 15:59:18 -070080 struct list_head uclass_node;
81 struct list_head child_head;
82 struct list_head sibling_node;
83 uint32_t flags;
Simon Glass5a66a8f2014-07-23 06:55:12 -060084 int req_seq;
85 int seq;
Simon Glass6494d702014-02-26 15:59:18 -070086};
87
Simon Glass5a66a8f2014-07-23 06:55:12 -060088/* Maximum sequence number supported */
89#define DM_MAX_SEQ 999
90
Simon Glass6494d702014-02-26 15:59:18 -070091/* Returns the operations for a device */
92#define device_get_ops(dev) (dev->driver->ops)
93
94/* Returns non-zero if the device is active (probed and not removed) */
95#define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
96
97/**
Simon Glassae7f4512014-06-11 23:29:45 -060098 * struct udevice_id - Lists the compatible strings supported by a driver
Simon Glass6494d702014-02-26 15:59:18 -070099 * @compatible: Compatible string
100 * @data: Data for this compatible string
101 */
Simon Glassae7f4512014-06-11 23:29:45 -0600102struct udevice_id {
Simon Glass6494d702014-02-26 15:59:18 -0700103 const char *compatible;
104 ulong data;
105};
106
Masahiro Yamadaf887cb62014-10-07 14:51:31 +0900107#ifdef CONFIG_OF_CONTROL
108#define of_match_ptr(_ptr) (_ptr)
109#else
110#define of_match_ptr(_ptr) NULL
111#endif /* CONFIG_OF_CONTROL */
112
Simon Glass6494d702014-02-26 15:59:18 -0700113/**
114 * struct driver - A driver for a feature or peripheral
115 *
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).
120 *
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
124 * it.
125 *
126 * @name: Device name
127 * @id: Identiies the uclass we belong to
128 * @of_match: List of compatible strings to match, and any identifying data
129 * for each.
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
Simon Glassa327dee2014-07-23 06:55:21 -0600135 * @child_pre_probe: Called before a child device is probed. The device has
136 * memory allocated but it has not yet been probed.
137 * @child_post_remove: Called after a child device is removed. The device
138 * has memory allocated but its device_remove() method has been called.
Simon Glass6494d702014-02-26 15:59:18 -0700139 * @priv_auto_alloc_size: If non-zero this is the size of the private data
140 * to be allocated in the device's ->priv pointer. If zero, then the driver
141 * is responsible for allocating any data required.
142 * @platdata_auto_alloc_size: If non-zero this is the size of the
143 * platform data to be allocated in the device's ->platdata pointer.
144 * This is typically only useful for device-tree-aware drivers (those with
145 * an of_match), since drivers which use platdata will have the data
146 * provided in the U_BOOT_DEVICE() instantiation.
Simon Glasse59f4582014-07-23 06:55:20 -0600147 * @per_child_auto_alloc_size: Each device can hold private data owned by
148 * its parent. If required this will be automatically allocated if this
149 * value is non-zero.
Simon Glassaccd4b12014-10-13 23:41:50 -0600150 * TODO(sjg@chromium.org): I'm considering dropping this, and just having
151 * device_probe_child() pass it in. So far the use case for allocating it
152 * is SPI, but I found that unsatisfactory. Since it is here I will leave it
153 * until things are clearer.
Simon Glasscdc133b2015-01-25 08:27:01 -0700154 * @per_child_platdata_auto_alloc_size: A bus likes to store information about
155 * its children. If non-zero this is the size of this data, to be allocated
156 * in the child's parent_platdata pointer.
Simon Glass0040b942014-07-23 06:55:17 -0600157 * @ops: Driver-specific operations. This is typically a list of function
Simon Glass6494d702014-02-26 15:59:18 -0700158 * pointers defined by the driver, to implement driver functions required by
159 * the uclass.
Simon Glass00606d72014-07-23 06:55:03 -0600160 * @flags: driver flags - see DM_FLAGS_...
Simon Glass6494d702014-02-26 15:59:18 -0700161 */
162struct driver {
163 char *name;
164 enum uclass_id id;
Simon Glassae7f4512014-06-11 23:29:45 -0600165 const struct udevice_id *of_match;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200166 int (*bind)(struct udevice *dev);
167 int (*probe)(struct udevice *dev);
168 int (*remove)(struct udevice *dev);
169 int (*unbind)(struct udevice *dev);
170 int (*ofdata_to_platdata)(struct udevice *dev);
Simon Glassa327dee2014-07-23 06:55:21 -0600171 int (*child_pre_probe)(struct udevice *dev);
172 int (*child_post_remove)(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700173 int priv_auto_alloc_size;
174 int platdata_auto_alloc_size;
Simon Glasse59f4582014-07-23 06:55:20 -0600175 int per_child_auto_alloc_size;
Simon Glasscdc133b2015-01-25 08:27:01 -0700176 int per_child_platdata_auto_alloc_size;
Simon Glass6494d702014-02-26 15:59:18 -0700177 const void *ops; /* driver-specific operations */
Simon Glass00606d72014-07-23 06:55:03 -0600178 uint32_t flags;
Simon Glass6494d702014-02-26 15:59:18 -0700179};
180
181/* Declare a new U-Boot driver */
182#define U_BOOT_DRIVER(__name) \
183 ll_entry_declare(struct driver, __name, driver)
184
185/**
186 * dev_get_platdata() - Get the platform data for a device
187 *
188 * This checks that dev is not NULL, but no other checks for now
189 *
190 * @dev Device to check
191 * @return platform data, or NULL if none
192 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200193void *dev_get_platdata(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700194
195/**
Simon Glasscdc133b2015-01-25 08:27:01 -0700196 * dev_get_parent_platdata() - Get the parent platform data for a device
197 *
198 * This checks that dev is not NULL, but no other checks for now
199 *
200 * @dev Device to check
201 * @return parent's platform data, or NULL if none
202 */
203void *dev_get_parent_platdata(struct udevice *dev);
204
205/**
Simon Glasse59f4582014-07-23 06:55:20 -0600206 * dev_get_parentdata() - Get the parent data for a device
207 *
208 * The parent data is data stored in the device but owned by the parent.
209 * For example, a USB device may have parent data which contains information
210 * about how to talk to the device over USB.
211 *
212 * This checks that dev is not NULL, but no other checks for now
213 *
214 * @dev Device to check
215 * @return parent data, or NULL if none
216 */
217void *dev_get_parentdata(struct udevice *dev);
218
219/**
Simon Glass6494d702014-02-26 15:59:18 -0700220 * dev_get_priv() - Get the private data for a device
221 *
222 * This checks that dev is not NULL, but no other checks for now
223 *
224 * @dev Device to check
225 * @return private data, or NULL if none
226 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200227void *dev_get_priv(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700228
Simon Glass5a66a8f2014-07-23 06:55:12 -0600229/**
Simon Glass479728c2014-11-11 10:46:19 -0700230 * struct dev_get_parent() - Get the parent of a device
231 *
232 * @child: Child to check
233 * @return parent of child, or NULL if this is the root device
234 */
235struct udevice *dev_get_parent(struct udevice *child);
236
237/**
Simon Glass2ef249b2014-11-11 10:46:18 -0700238 * dev_get_of_data() - get the device tree data used to bind a device
239 *
240 * When a device is bound using a device tree node, it matches a
241 * particular compatible string as in struct udevice_id. This function
242 * returns the associated data value for that compatible string
243 */
244ulong dev_get_of_data(struct udevice *dev);
245
246/**
Simon Glass997c87b2014-07-23 06:55:19 -0600247 * device_get_child() - Get the child of a device by index
248 *
249 * Returns the numbered child, 0 being the first. This does not use
250 * sequence numbers, only the natural order.
251 *
252 * @dev: Parent device to check
253 * @index: Child index
254 * @devp: Returns pointer to device
255 */
256int device_get_child(struct udevice *parent, int index, struct udevice **devp);
257
258/**
Simon Glass5a66a8f2014-07-23 06:55:12 -0600259 * device_find_child_by_seq() - Find a child device based on a sequence
260 *
261 * This searches for a device with the given seq or req_seq.
262 *
263 * For seq, if an active device has this sequence it will be returned.
264 * If there is no such device then this will return -ENODEV.
265 *
266 * For req_seq, if a device (whether activated or not) has this req_seq
267 * value, that device will be returned. This is a strong indication that
268 * the device will receive that sequence when activated.
269 *
270 * @parent: Parent device
271 * @seq_or_req_seq: Sequence number to find (0=first)
272 * @find_req_seq: true to find req_seq, false to find seq
273 * @devp: Returns pointer to device (there is only one per for each seq).
274 * Set to NULL if none is found
275 * @return 0 if OK, -ve on error
276 */
277int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
278 bool find_req_seq, struct udevice **devp);
279
Simon Glass997c87b2014-07-23 06:55:19 -0600280/**
281 * device_get_child_by_seq() - Get a child device based on a sequence
282 *
283 * If an active device has this sequence it will be returned. If there is no
284 * such device then this will check for a device that is requesting this
285 * sequence.
286 *
287 * The device is probed to activate it ready for use.
288 *
289 * @parent: Parent device
290 * @seq: Sequence number to find (0=first)
291 * @devp: Returns pointer to device (there is only one per for each seq)
292 * Set to NULL if none is found
293 * @return 0 if OK, -ve on error
294 */
295int device_get_child_by_seq(struct udevice *parent, int seq,
296 struct udevice **devp);
297
298/**
299 * device_find_child_by_of_offset() - Find a child device based on FDT offset
300 *
301 * Locates a child device by its device tree offset.
302 *
303 * @parent: Parent device
304 * @of_offset: Device tree offset to find
305 * @devp: Returns pointer to device if found, otherwise this is set to NULL
306 * @return 0 if OK, -ve on error
307 */
308int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
309 struct udevice **devp);
310
311/**
312 * device_get_child_by_of_offset() - Get a child device based on FDT offset
313 *
314 * Locates a child device by its device tree offset.
315 *
316 * The device is probed to activate it ready for use.
317 *
318 * @parent: Parent device
319 * @of_offset: Device tree offset to find
320 * @devp: Returns pointer to device if found, otherwise this is set to NULL
321 * @return 0 if OK, -ve on error
322 */
323int device_get_child_by_of_offset(struct udevice *parent, int seq,
324 struct udevice **devp);
325
Simon Glassa8981d42014-10-13 23:41:49 -0600326/**
327 * device_find_first_child() - Find the first child of a device
328 *
329 * @parent: Parent device to search
330 * @devp: Returns first child device, or NULL if none
331 * @return 0
332 */
333int device_find_first_child(struct udevice *parent, struct udevice **devp);
334
335/**
336 * device_find_first_child() - Find the first child of a device
337 *
338 * @devp: Pointer to previous child device on entry. Returns pointer to next
339 * child device, or NULL if none
340 * @return 0
341 */
342int device_find_next_child(struct udevice **devp);
343
Simon Glass6494d702014-02-26 15:59:18 -0700344#endif