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 <linker_lists.h> 16 #include <linux/list.h> 17 18 struct 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 */ 24 #define DM_FLAG_ALLOC_PDATA (1 << 1) 25 26 /* DM should init this device prior to relocation */ 27 #define DM_FLAG_PRE_RELOC (1 << 2) 28 29 /** 30 * struct udevice - An instance of a driver 31 * 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 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) 61 */ 62 struct udevice { 63 struct driver *driver; 64 const char *name; 65 void *platdata; 66 int of_offset; 67 struct udevice *parent; 68 void *priv; 69 struct uclass *uclass; 70 void *uclass_priv; 71 void *parent_priv; 72 struct list_head uclass_node; 73 struct list_head child_head; 74 struct list_head sibling_node; 75 uint32_t flags; 76 int req_seq; 77 int seq; 78 }; 79 80 /* Maximum sequence number supported */ 81 #define DM_MAX_SEQ 999 82 83 /* Returns the operations for a device */ 84 #define device_get_ops(dev) (dev->driver->ops) 85 86 /* Returns non-zero if the device is active (probed and not removed) */ 87 #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED) 88 89 /** 90 * struct udevice_id - Lists the compatible strings supported by a driver 91 * @compatible: Compatible string 92 * @data: Data for this compatible string 93 */ 94 struct udevice_id { 95 const char *compatible; 96 ulong data; 97 }; 98 99 #ifdef CONFIG_OF_CONTROL 100 #define of_match_ptr(_ptr) (_ptr) 101 #else 102 #define of_match_ptr(_ptr) NULL 103 #endif /* CONFIG_OF_CONTROL */ 104 105 /** 106 * struct driver - A driver for a feature or peripheral 107 * 108 * This holds methods for setting up a new device, and also removing it. 109 * The device needs information to set itself up - this is provided either 110 * by platdata or a device tree node (which we find by looking up 111 * matching compatible strings with of_match). 112 * 113 * Drivers all belong to a uclass, representing a class of devices of the 114 * same type. Common elements of the drivers can be implemented in the uclass, 115 * or the uclass can provide a consistent interface to the drivers within 116 * it. 117 * 118 * @name: Device name 119 * @id: Identiies the uclass we belong to 120 * @of_match: List of compatible strings to match, and any identifying data 121 * for each. 122 * @bind: Called to bind a device to its driver 123 * @probe: Called to probe a device, i.e. activate it 124 * @remove: Called to remove a device, i.e. de-activate it 125 * @unbind: Called to unbind a device from its driver 126 * @ofdata_to_platdata: Called before probe to decode device tree data 127 * @child_pre_probe: Called before a child device is probed. The device has 128 * memory allocated but it has not yet been probed. 129 * @child_post_remove: Called after a child device is removed. The device 130 * has memory allocated but its device_remove() method has been called. 131 * @priv_auto_alloc_size: If non-zero this is the size of the private data 132 * to be allocated in the device's ->priv pointer. If zero, then the driver 133 * is responsible for allocating any data required. 134 * @platdata_auto_alloc_size: If non-zero this is the size of the 135 * platform data to be allocated in the device's ->platdata pointer. 136 * This is typically only useful for device-tree-aware drivers (those with 137 * an of_match), since drivers which use platdata will have the data 138 * provided in the U_BOOT_DEVICE() instantiation. 139 * @per_child_auto_alloc_size: Each device can hold private data owned by 140 * its parent. If required this will be automatically allocated if this 141 * value is non-zero. 142 * TODO(sjg@chromium.org): I'm considering dropping this, and just having 143 * device_probe_child() pass it in. So far the use case for allocating it 144 * is SPI, but I found that unsatisfactory. Since it is here I will leave it 145 * until things are clearer. 146 * @ops: Driver-specific operations. This is typically a list of function 147 * pointers defined by the driver, to implement driver functions required by 148 * the uclass. 149 * @flags: driver flags - see DM_FLAGS_... 150 */ 151 struct driver { 152 char *name; 153 enum uclass_id id; 154 const struct udevice_id *of_match; 155 int (*bind)(struct udevice *dev); 156 int (*probe)(struct udevice *dev); 157 int (*remove)(struct udevice *dev); 158 int (*unbind)(struct udevice *dev); 159 int (*ofdata_to_platdata)(struct udevice *dev); 160 int (*child_pre_probe)(struct udevice *dev); 161 int (*child_post_remove)(struct udevice *dev); 162 int priv_auto_alloc_size; 163 int platdata_auto_alloc_size; 164 int per_child_auto_alloc_size; 165 const void *ops; /* driver-specific operations */ 166 uint32_t flags; 167 }; 168 169 /* Declare a new U-Boot driver */ 170 #define U_BOOT_DRIVER(__name) \ 171 ll_entry_declare(struct driver, __name, driver) 172 173 /** 174 * dev_get_platdata() - Get the platform data for a device 175 * 176 * This checks that dev is not NULL, but no other checks for now 177 * 178 * @dev Device to check 179 * @return platform data, or NULL if none 180 */ 181 void *dev_get_platdata(struct udevice *dev); 182 183 /** 184 * dev_get_parentdata() - Get the parent data for a device 185 * 186 * The parent data is data stored in the device but owned by the parent. 187 * For example, a USB device may have parent data which contains information 188 * about how to talk to the device over USB. 189 * 190 * This checks that dev is not NULL, but no other checks for now 191 * 192 * @dev Device to check 193 * @return parent data, or NULL if none 194 */ 195 void *dev_get_parentdata(struct udevice *dev); 196 197 /** 198 * dev_get_priv() - Get the private data for a device 199 * 200 * This checks that dev is not NULL, but no other checks for now 201 * 202 * @dev Device to check 203 * @return private data, or NULL if none 204 */ 205 void *dev_get_priv(struct udevice *dev); 206 207 /** 208 * device_get_child() - Get the child of a device by index 209 * 210 * Returns the numbered child, 0 being the first. This does not use 211 * sequence numbers, only the natural order. 212 * 213 * @dev: Parent device to check 214 * @index: Child index 215 * @devp: Returns pointer to device 216 */ 217 int device_get_child(struct udevice *parent, int index, struct udevice **devp); 218 219 /** 220 * device_find_child_by_seq() - Find a child device based on a sequence 221 * 222 * This searches for a device with the given seq or req_seq. 223 * 224 * For seq, if an active device has this sequence it will be returned. 225 * If there is no such device then this will return -ENODEV. 226 * 227 * For req_seq, if a device (whether activated or not) has this req_seq 228 * value, that device will be returned. This is a strong indication that 229 * the device will receive that sequence when activated. 230 * 231 * @parent: Parent device 232 * @seq_or_req_seq: Sequence number to find (0=first) 233 * @find_req_seq: true to find req_seq, false to find seq 234 * @devp: Returns pointer to device (there is only one per for each seq). 235 * Set to NULL if none is found 236 * @return 0 if OK, -ve on error 237 */ 238 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, 239 bool find_req_seq, struct udevice **devp); 240 241 /** 242 * device_get_child_by_seq() - Get a child device based on a sequence 243 * 244 * If an active device has this sequence it will be returned. If there is no 245 * such device then this will check for a device that is requesting this 246 * sequence. 247 * 248 * The device is probed to activate it ready for use. 249 * 250 * @parent: Parent device 251 * @seq: Sequence number to find (0=first) 252 * @devp: Returns pointer to device (there is only one per for each seq) 253 * Set to NULL if none is found 254 * @return 0 if OK, -ve on error 255 */ 256 int device_get_child_by_seq(struct udevice *parent, int seq, 257 struct udevice **devp); 258 259 /** 260 * device_find_child_by_of_offset() - Find a child device based on FDT offset 261 * 262 * Locates a child device by its device tree offset. 263 * 264 * @parent: Parent device 265 * @of_offset: Device tree offset to find 266 * @devp: Returns pointer to device if found, otherwise this is set to NULL 267 * @return 0 if OK, -ve on error 268 */ 269 int device_find_child_by_of_offset(struct udevice *parent, int of_offset, 270 struct udevice **devp); 271 272 /** 273 * device_get_child_by_of_offset() - Get a child device based on FDT offset 274 * 275 * Locates a child device by its device tree offset. 276 * 277 * The device is probed to activate it ready for use. 278 * 279 * @parent: Parent device 280 * @of_offset: Device tree offset to find 281 * @devp: Returns pointer to device if found, otherwise this is set to NULL 282 * @return 0 if OK, -ve on error 283 */ 284 int device_get_child_by_of_offset(struct udevice *parent, int seq, 285 struct udevice **devp); 286 287 /** 288 * device_find_first_child() - Find the first child of a device 289 * 290 * @parent: Parent device to search 291 * @devp: Returns first child device, or NULL if none 292 * @return 0 293 */ 294 int device_find_first_child(struct udevice *parent, struct udevice **devp); 295 296 /** 297 * device_find_first_child() - Find the first child of a device 298 * 299 * @devp: Pointer to previous child device on entry. Returns pointer to next 300 * child device, or NULL if none 301 * @return 0 302 */ 303 int device_find_next_child(struct udevice **devp); 304 305 #endif 306