blob: 9ce95a834e75ee54d6d7edefb21ee0f848f97e0f [file] [log] [blame]
Simon Glassdd6ab882014-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 Glass4dbb5cf2014-06-11 23:29:54 -060024#define DM_FLAG_ALLOC_PDATA (1 << 1)
Simon Glassdd6ab882014-02-26 15:59:18 -070025
Simon Glassfef72b72014-07-23 06:55:03 -060026/* DM should init this device prior to relocation */
27#define DM_FLAG_PRE_RELOC (1 << 2)
28
Simon Glassdd6ab882014-02-26 15:59:18 -070029/**
Heiko Schocherb74fcb42014-05-22 12:43:05 +020030 * struct udevice - An instance of a driver
Simon Glassdd6ab882014-02-26 15:59:18 -070031 *
32 * This holds information about a device, which is a driver bound to a
33 * particular port or peripheral (essentially a driver instance).
34 *
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.
41 *
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.
45 *
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
Simon Glass60d971b2014-07-23 06:55:20 -060054 * @parent_priv: The parent's private data for this device
Simon Glassdd6ab882014-02-26 15:59:18 -070055 * @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_...
Simon Glassdb6f0202014-07-23 06:55:12 -060059 * @req_seq: Requested sequence number for this device (-1 = any)
Simon Glass47424ec2014-10-13 23:41:51 -060060 * @seq: Allocated sequence number for this device (-1 = none). This is set up
61 * when the device is probed and will be unique within the device's uclass.
Simon Glassdd6ab882014-02-26 15:59:18 -070062 */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020063struct udevice {
Simon Glassdd6ab882014-02-26 15:59:18 -070064 struct driver *driver;
65 const char *name;
66 void *platdata;
67 int of_offset;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020068 struct udevice *parent;
Simon Glassdd6ab882014-02-26 15:59:18 -070069 void *priv;
70 struct uclass *uclass;
71 void *uclass_priv;
Simon Glass60d971b2014-07-23 06:55:20 -060072 void *parent_priv;
Simon Glassdd6ab882014-02-26 15:59:18 -070073 struct list_head uclass_node;
74 struct list_head child_head;
75 struct list_head sibling_node;
76 uint32_t flags;
Simon Glassdb6f0202014-07-23 06:55:12 -060077 int req_seq;
78 int seq;
Simon Glassdd6ab882014-02-26 15:59:18 -070079};
80
Simon Glassdb6f0202014-07-23 06:55:12 -060081/* Maximum sequence number supported */
82#define DM_MAX_SEQ 999
83
Simon Glassdd6ab882014-02-26 15:59:18 -070084/* Returns the operations for a device */
85#define device_get_ops(dev) (dev->driver->ops)
86
87/* Returns non-zero if the device is active (probed and not removed) */
88#define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
89
90/**
Simon Glass767827a2014-06-11 23:29:45 -060091 * struct udevice_id - Lists the compatible strings supported by a driver
Simon Glassdd6ab882014-02-26 15:59:18 -070092 * @compatible: Compatible string
93 * @data: Data for this compatible string
94 */
Simon Glass767827a2014-06-11 23:29:45 -060095struct udevice_id {
Simon Glassdd6ab882014-02-26 15:59:18 -070096 const char *compatible;
97 ulong data;
98};
99
Masahiro Yamadaf797bc22014-10-07 14:51:31 +0900100#ifdef CONFIG_OF_CONTROL
101#define of_match_ptr(_ptr) (_ptr)
102#else
103#define of_match_ptr(_ptr) NULL
104#endif /* CONFIG_OF_CONTROL */
105
Simon Glassdd6ab882014-02-26 15:59:18 -0700106/**
107 * struct driver - A driver for a feature or peripheral
108 *
109 * This holds methods for setting up a new device, and also removing it.
110 * The device needs information to set itself up - this is provided either
111 * by platdata or a device tree node (which we find by looking up
112 * matching compatible strings with of_match).
113 *
114 * Drivers all belong to a uclass, representing a class of devices of the
115 * same type. Common elements of the drivers can be implemented in the uclass,
116 * or the uclass can provide a consistent interface to the drivers within
117 * it.
118 *
119 * @name: Device name
120 * @id: Identiies the uclass we belong to
121 * @of_match: List of compatible strings to match, and any identifying data
122 * for each.
123 * @bind: Called to bind a device to its driver
124 * @probe: Called to probe a device, i.e. activate it
125 * @remove: Called to remove a device, i.e. de-activate it
126 * @unbind: Called to unbind a device from its driver
127 * @ofdata_to_platdata: Called before probe to decode device tree data
Simon Glassd45560d2014-07-23 06:55:21 -0600128 * @child_pre_probe: Called before a child device is probed. The device has
129 * memory allocated but it has not yet been probed.
130 * @child_post_remove: Called after a child device is removed. The device
131 * has memory allocated but its device_remove() method has been called.
Simon Glassdd6ab882014-02-26 15:59:18 -0700132 * @priv_auto_alloc_size: If non-zero this is the size of the private data
133 * to be allocated in the device's ->priv pointer. If zero, then the driver
134 * is responsible for allocating any data required.
135 * @platdata_auto_alloc_size: If non-zero this is the size of the
136 * platform data to be allocated in the device's ->platdata pointer.
137 * This is typically only useful for device-tree-aware drivers (those with
138 * an of_match), since drivers which use platdata will have the data
139 * provided in the U_BOOT_DEVICE() instantiation.
Simon Glass60d971b2014-07-23 06:55:20 -0600140 * @per_child_auto_alloc_size: Each device can hold private data owned by
141 * its parent. If required this will be automatically allocated if this
142 * value is non-zero.
Simon Glass1586a842014-10-13 23:41:50 -0600143 * TODO(sjg@chromium.org): I'm considering dropping this, and just having
144 * device_probe_child() pass it in. So far the use case for allocating it
145 * is SPI, but I found that unsatisfactory. Since it is here I will leave it
146 * until things are clearer.
Simon Glasscebcebb2014-07-23 06:55:17 -0600147 * @ops: Driver-specific operations. This is typically a list of function
Simon Glassdd6ab882014-02-26 15:59:18 -0700148 * pointers defined by the driver, to implement driver functions required by
149 * the uclass.
Simon Glassfef72b72014-07-23 06:55:03 -0600150 * @flags: driver flags - see DM_FLAGS_...
Simon Glassdd6ab882014-02-26 15:59:18 -0700151 */
152struct driver {
153 char *name;
154 enum uclass_id id;
Simon Glass767827a2014-06-11 23:29:45 -0600155 const struct udevice_id *of_match;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200156 int (*bind)(struct udevice *dev);
157 int (*probe)(struct udevice *dev);
158 int (*remove)(struct udevice *dev);
159 int (*unbind)(struct udevice *dev);
160 int (*ofdata_to_platdata)(struct udevice *dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600161 int (*child_pre_probe)(struct udevice *dev);
162 int (*child_post_remove)(struct udevice *dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700163 int priv_auto_alloc_size;
164 int platdata_auto_alloc_size;
Simon Glass60d971b2014-07-23 06:55:20 -0600165 int per_child_auto_alloc_size;
Simon Glassdd6ab882014-02-26 15:59:18 -0700166 const void *ops; /* driver-specific operations */
Simon Glassfef72b72014-07-23 06:55:03 -0600167 uint32_t flags;
Simon Glassdd6ab882014-02-26 15:59:18 -0700168};
169
170/* Declare a new U-Boot driver */
171#define U_BOOT_DRIVER(__name) \
172 ll_entry_declare(struct driver, __name, driver)
173
174/**
175 * dev_get_platdata() - Get the platform data for a device
176 *
177 * This checks that dev is not NULL, but no other checks for now
178 *
179 * @dev Device to check
180 * @return platform data, or NULL if none
181 */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200182void *dev_get_platdata(struct udevice *dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700183
184/**
Simon Glass60d971b2014-07-23 06:55:20 -0600185 * dev_get_parentdata() - Get the parent data for a device
186 *
187 * The parent data is data stored in the device but owned by the parent.
188 * For example, a USB device may have parent data which contains information
189 * about how to talk to the device over USB.
190 *
191 * This checks that dev is not NULL, but no other checks for now
192 *
193 * @dev Device to check
194 * @return parent data, or NULL if none
195 */
196void *dev_get_parentdata(struct udevice *dev);
197
198/**
Simon Glassdd6ab882014-02-26 15:59:18 -0700199 * dev_get_priv() - Get the private data for a device
200 *
201 * This checks that dev is not NULL, but no other checks for now
202 *
203 * @dev Device to check
204 * @return private data, or NULL if none
205 */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200206void *dev_get_priv(struct udevice *dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700207
Simon Glassdb6f0202014-07-23 06:55:12 -0600208/**
Simon Glass48d4e292014-07-23 06:55:19 -0600209 * device_get_child() - Get the child of a device by index
210 *
211 * Returns the numbered child, 0 being the first. This does not use
212 * sequence numbers, only the natural order.
213 *
214 * @dev: Parent device to check
215 * @index: Child index
216 * @devp: Returns pointer to device
217 */
218int device_get_child(struct udevice *parent, int index, struct udevice **devp);
219
220/**
Simon Glassdb6f0202014-07-23 06:55:12 -0600221 * device_find_child_by_seq() - Find a child device based on a sequence
222 *
223 * This searches for a device with the given seq or req_seq.
224 *
225 * For seq, if an active device has this sequence it will be returned.
226 * If there is no such device then this will return -ENODEV.
227 *
228 * For req_seq, if a device (whether activated or not) has this req_seq
229 * value, that device will be returned. This is a strong indication that
230 * the device will receive that sequence when activated.
231 *
232 * @parent: Parent device
233 * @seq_or_req_seq: Sequence number to find (0=first)
234 * @find_req_seq: true to find req_seq, false to find seq
235 * @devp: Returns pointer to device (there is only one per for each seq).
236 * Set to NULL if none is found
237 * @return 0 if OK, -ve on error
238 */
239int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
240 bool find_req_seq, struct udevice **devp);
241
Simon Glass48d4e292014-07-23 06:55:19 -0600242/**
243 * device_get_child_by_seq() - Get a child device based on a sequence
244 *
245 * If an active device has this sequence it will be returned. If there is no
246 * such device then this will check for a device that is requesting this
247 * sequence.
248 *
249 * The device is probed to activate it ready for use.
250 *
251 * @parent: Parent device
252 * @seq: Sequence number to find (0=first)
253 * @devp: Returns pointer to device (there is only one per for each seq)
254 * Set to NULL if none is found
255 * @return 0 if OK, -ve on error
256 */
257int device_get_child_by_seq(struct udevice *parent, int seq,
258 struct udevice **devp);
259
260/**
261 * device_find_child_by_of_offset() - Find a child device based on FDT offset
262 *
263 * Locates a child device by its device tree offset.
264 *
265 * @parent: Parent device
266 * @of_offset: Device tree offset to find
267 * @devp: Returns pointer to device if found, otherwise this is set to NULL
268 * @return 0 if OK, -ve on error
269 */
270int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
271 struct udevice **devp);
272
273/**
274 * device_get_child_by_of_offset() - Get a child device based on FDT offset
275 *
276 * Locates a child device by its device tree offset.
277 *
278 * The device is probed to activate it ready for use.
279 *
280 * @parent: Parent device
281 * @of_offset: Device tree offset to find
282 * @devp: Returns pointer to device if found, otherwise this is set to NULL
283 * @return 0 if OK, -ve on error
284 */
285int device_get_child_by_of_offset(struct udevice *parent, int seq,
286 struct udevice **devp);
287
Simon Glass44da7352014-10-13 23:41:49 -0600288/**
289 * device_find_first_child() - Find the first child of a device
290 *
291 * @parent: Parent device to search
292 * @devp: Returns first child device, or NULL if none
293 * @return 0
294 */
295int device_find_first_child(struct udevice *parent, struct udevice **devp);
296
297/**
298 * device_find_first_child() - Find the first child of a device
299 *
300 * @devp: Pointer to previous child device on entry. Returns pointer to next
301 * child device, or NULL if none
302 * @return 0
303 */
304int device_find_next_child(struct udevice **devp);
305
Simon Glassdd6ab882014-02-26 15:59:18 -0700306#endif