16494d708SSimon Glass /* 26494d708SSimon Glass * Copyright (c) 2013 Google, Inc 36494d708SSimon Glass * 46494d708SSimon Glass * (C) Copyright 2012 56494d708SSimon Glass * Pavel Herrmann <morpheus.ibis@gmail.com> 66494d708SSimon Glass * Marek Vasut <marex@denx.de> 76494d708SSimon Glass * 86494d708SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 96494d708SSimon Glass */ 106494d708SSimon Glass 116494d708SSimon Glass #ifndef _DM_DEVICE_H 126494d708SSimon Glass #define _DM_DEVICE_H 136494d708SSimon Glass 146494d708SSimon Glass #include <dm/uclass-id.h> 156494d708SSimon Glass #include <linker_lists.h> 166494d708SSimon Glass #include <linux/list.h> 176494d708SSimon Glass 186494d708SSimon Glass struct driver_info; 196494d708SSimon Glass 206494d708SSimon Glass /* Driver is active (probed). Cleared when it is removed */ 216494d708SSimon Glass #define DM_FLAG_ACTIVATED (1 << 0) 226494d708SSimon Glass 236494d708SSimon Glass /* DM is responsible for allocating and freeing platdata */ 24f2bc6fc3SSimon Glass #define DM_FLAG_ALLOC_PDATA (1 << 1) 256494d708SSimon Glass 2600606d7eSSimon Glass /* DM should init this device prior to relocation */ 2700606d7eSSimon Glass #define DM_FLAG_PRE_RELOC (1 << 2) 2800606d7eSSimon Glass 296494d708SSimon Glass /** 3054c5d08aSHeiko Schocher * struct udevice - An instance of a driver 316494d708SSimon Glass * 326494d708SSimon Glass * This holds information about a device, which is a driver bound to a 336494d708SSimon Glass * particular port or peripheral (essentially a driver instance). 346494d708SSimon Glass * 356494d708SSimon Glass * A device will come into existence through a 'bind' call, either due to 366494d708SSimon Glass * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node 376494d708SSimon Glass * in the device tree (in which case of_offset is >= 0). In the latter case 386494d708SSimon Glass * we translate the device tree information into platdata in a function 396494d708SSimon Glass * implemented by the driver ofdata_to_platdata method (called just before the 406494d708SSimon Glass * probe method if the device has a device tree node. 416494d708SSimon Glass * 426494d708SSimon Glass * All three of platdata, priv and uclass_priv can be allocated by the 436494d708SSimon Glass * driver, or you can use the auto_alloc_size members of struct driver and 446494d708SSimon Glass * struct uclass_driver to have driver model do this automatically. 456494d708SSimon Glass * 466494d708SSimon Glass * @driver: The driver used by this device 476494d708SSimon Glass * @name: Name of device, typically the FDT node name 486494d708SSimon Glass * @platdata: Configuration data for this device 496494d708SSimon Glass * @of_offset: Device tree node offset for this device (- for none) 50*2ef249b4SSimon Glass * @of_id: Pointer to the udevice_id structure which created the device 516494d708SSimon Glass * @parent: Parent of this device, or NULL for the top level device 526494d708SSimon Glass * @priv: Private data for this device 536494d708SSimon Glass * @uclass: Pointer to uclass for this device 546494d708SSimon Glass * @uclass_priv: The uclass's private data for this device 55e59f458dSSimon Glass * @parent_priv: The parent's private data for this device 566494d708SSimon Glass * @uclass_node: Used by uclass to link its devices 576494d708SSimon Glass * @child_head: List of children of this device 586494d708SSimon Glass * @sibling_node: Next device in list of all devices 596494d708SSimon Glass * @flags: Flags for this device DM_FLAG_... 605a66a8ffSSimon Glass * @req_seq: Requested sequence number for this device (-1 = any) 61547cea19SSimon Glass * @seq: Allocated sequence number for this device (-1 = none). This is set up 62547cea19SSimon Glass * when the device is probed and will be unique within the device's uclass. 636494d708SSimon Glass */ 6454c5d08aSHeiko Schocher struct udevice { 656494d708SSimon Glass struct driver *driver; 666494d708SSimon Glass const char *name; 676494d708SSimon Glass void *platdata; 686494d708SSimon Glass int of_offset; 69*2ef249b4SSimon Glass const struct udevice_id *of_id; 7054c5d08aSHeiko Schocher struct udevice *parent; 716494d708SSimon Glass void *priv; 726494d708SSimon Glass struct uclass *uclass; 736494d708SSimon Glass void *uclass_priv; 74e59f458dSSimon Glass void *parent_priv; 756494d708SSimon Glass struct list_head uclass_node; 766494d708SSimon Glass struct list_head child_head; 776494d708SSimon Glass struct list_head sibling_node; 786494d708SSimon Glass uint32_t flags; 795a66a8ffSSimon Glass int req_seq; 805a66a8ffSSimon Glass int seq; 816494d708SSimon Glass }; 826494d708SSimon Glass 835a66a8ffSSimon Glass /* Maximum sequence number supported */ 845a66a8ffSSimon Glass #define DM_MAX_SEQ 999 855a66a8ffSSimon Glass 866494d708SSimon Glass /* Returns the operations for a device */ 876494d708SSimon Glass #define device_get_ops(dev) (dev->driver->ops) 886494d708SSimon Glass 896494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */ 906494d708SSimon Glass #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED) 916494d708SSimon Glass 926494d708SSimon Glass /** 93ae7f4513SSimon Glass * struct udevice_id - Lists the compatible strings supported by a driver 946494d708SSimon Glass * @compatible: Compatible string 956494d708SSimon Glass * @data: Data for this compatible string 966494d708SSimon Glass */ 97ae7f4513SSimon Glass struct udevice_id { 986494d708SSimon Glass const char *compatible; 996494d708SSimon Glass ulong data; 1006494d708SSimon Glass }; 1016494d708SSimon Glass 102f887cb6dSMasahiro Yamada #ifdef CONFIG_OF_CONTROL 103f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr) (_ptr) 104f887cb6dSMasahiro Yamada #else 105f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr) NULL 106f887cb6dSMasahiro Yamada #endif /* CONFIG_OF_CONTROL */ 107f887cb6dSMasahiro Yamada 1086494d708SSimon Glass /** 1096494d708SSimon Glass * struct driver - A driver for a feature or peripheral 1106494d708SSimon Glass * 1116494d708SSimon Glass * This holds methods for setting up a new device, and also removing it. 1126494d708SSimon Glass * The device needs information to set itself up - this is provided either 1136494d708SSimon Glass * by platdata or a device tree node (which we find by looking up 1146494d708SSimon Glass * matching compatible strings with of_match). 1156494d708SSimon Glass * 1166494d708SSimon Glass * Drivers all belong to a uclass, representing a class of devices of the 1176494d708SSimon Glass * same type. Common elements of the drivers can be implemented in the uclass, 1186494d708SSimon Glass * or the uclass can provide a consistent interface to the drivers within 1196494d708SSimon Glass * it. 1206494d708SSimon Glass * 1216494d708SSimon Glass * @name: Device name 1226494d708SSimon Glass * @id: Identiies the uclass we belong to 1236494d708SSimon Glass * @of_match: List of compatible strings to match, and any identifying data 1246494d708SSimon Glass * for each. 1256494d708SSimon Glass * @bind: Called to bind a device to its driver 1266494d708SSimon Glass * @probe: Called to probe a device, i.e. activate it 1276494d708SSimon Glass * @remove: Called to remove a device, i.e. de-activate it 1286494d708SSimon Glass * @unbind: Called to unbind a device from its driver 1296494d708SSimon Glass * @ofdata_to_platdata: Called before probe to decode device tree data 130a327dee0SSimon Glass * @child_pre_probe: Called before a child device is probed. The device has 131a327dee0SSimon Glass * memory allocated but it has not yet been probed. 132a327dee0SSimon Glass * @child_post_remove: Called after a child device is removed. The device 133a327dee0SSimon Glass * has memory allocated but its device_remove() method has been called. 1346494d708SSimon Glass * @priv_auto_alloc_size: If non-zero this is the size of the private data 1356494d708SSimon Glass * to be allocated in the device's ->priv pointer. If zero, then the driver 1366494d708SSimon Glass * is responsible for allocating any data required. 1376494d708SSimon Glass * @platdata_auto_alloc_size: If non-zero this is the size of the 1386494d708SSimon Glass * platform data to be allocated in the device's ->platdata pointer. 1396494d708SSimon Glass * This is typically only useful for device-tree-aware drivers (those with 1406494d708SSimon Glass * an of_match), since drivers which use platdata will have the data 1416494d708SSimon Glass * provided in the U_BOOT_DEVICE() instantiation. 142e59f458dSSimon Glass * @per_child_auto_alloc_size: Each device can hold private data owned by 143e59f458dSSimon Glass * its parent. If required this will be automatically allocated if this 144e59f458dSSimon Glass * value is non-zero. 145accd4b19SSimon Glass * TODO(sjg@chromium.org): I'm considering dropping this, and just having 146accd4b19SSimon Glass * device_probe_child() pass it in. So far the use case for allocating it 147accd4b19SSimon Glass * is SPI, but I found that unsatisfactory. Since it is here I will leave it 148accd4b19SSimon Glass * until things are clearer. 1490040b944SSimon Glass * @ops: Driver-specific operations. This is typically a list of function 1506494d708SSimon Glass * pointers defined by the driver, to implement driver functions required by 1516494d708SSimon Glass * the uclass. 15200606d7eSSimon Glass * @flags: driver flags - see DM_FLAGS_... 1536494d708SSimon Glass */ 1546494d708SSimon Glass struct driver { 1556494d708SSimon Glass char *name; 1566494d708SSimon Glass enum uclass_id id; 157ae7f4513SSimon Glass const struct udevice_id *of_match; 15854c5d08aSHeiko Schocher int (*bind)(struct udevice *dev); 15954c5d08aSHeiko Schocher int (*probe)(struct udevice *dev); 16054c5d08aSHeiko Schocher int (*remove)(struct udevice *dev); 16154c5d08aSHeiko Schocher int (*unbind)(struct udevice *dev); 16254c5d08aSHeiko Schocher int (*ofdata_to_platdata)(struct udevice *dev); 163a327dee0SSimon Glass int (*child_pre_probe)(struct udevice *dev); 164a327dee0SSimon Glass int (*child_post_remove)(struct udevice *dev); 1656494d708SSimon Glass int priv_auto_alloc_size; 1666494d708SSimon Glass int platdata_auto_alloc_size; 167e59f458dSSimon Glass int per_child_auto_alloc_size; 1686494d708SSimon Glass const void *ops; /* driver-specific operations */ 16900606d7eSSimon Glass uint32_t flags; 1706494d708SSimon Glass }; 1716494d708SSimon Glass 1726494d708SSimon Glass /* Declare a new U-Boot driver */ 1736494d708SSimon Glass #define U_BOOT_DRIVER(__name) \ 1746494d708SSimon Glass ll_entry_declare(struct driver, __name, driver) 1756494d708SSimon Glass 1766494d708SSimon Glass /** 1776494d708SSimon Glass * dev_get_platdata() - Get the platform data for a device 1786494d708SSimon Glass * 1796494d708SSimon Glass * This checks that dev is not NULL, but no other checks for now 1806494d708SSimon Glass * 1816494d708SSimon Glass * @dev Device to check 1826494d708SSimon Glass * @return platform data, or NULL if none 1836494d708SSimon Glass */ 18454c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev); 1856494d708SSimon Glass 1866494d708SSimon Glass /** 187e59f458dSSimon Glass * dev_get_parentdata() - Get the parent data for a device 188e59f458dSSimon Glass * 189e59f458dSSimon Glass * The parent data is data stored in the device but owned by the parent. 190e59f458dSSimon Glass * For example, a USB device may have parent data which contains information 191e59f458dSSimon Glass * about how to talk to the device over USB. 192e59f458dSSimon Glass * 193e59f458dSSimon Glass * This checks that dev is not NULL, but no other checks for now 194e59f458dSSimon Glass * 195e59f458dSSimon Glass * @dev Device to check 196e59f458dSSimon Glass * @return parent data, or NULL if none 197e59f458dSSimon Glass */ 198e59f458dSSimon Glass void *dev_get_parentdata(struct udevice *dev); 199e59f458dSSimon Glass 200e59f458dSSimon Glass /** 2016494d708SSimon Glass * dev_get_priv() - Get the private data for a device 2026494d708SSimon Glass * 2036494d708SSimon Glass * This checks that dev is not NULL, but no other checks for now 2046494d708SSimon Glass * 2056494d708SSimon Glass * @dev Device to check 2066494d708SSimon Glass * @return private data, or NULL if none 2076494d708SSimon Glass */ 20854c5d08aSHeiko Schocher void *dev_get_priv(struct udevice *dev); 2096494d708SSimon Glass 2105a66a8ffSSimon Glass /** 211*2ef249b4SSimon Glass * dev_get_of_data() - get the device tree data used to bind a device 212*2ef249b4SSimon Glass * 213*2ef249b4SSimon Glass * When a device is bound using a device tree node, it matches a 214*2ef249b4SSimon Glass * particular compatible string as in struct udevice_id. This function 215*2ef249b4SSimon Glass * returns the associated data value for that compatible string 216*2ef249b4SSimon Glass */ 217*2ef249b4SSimon Glass ulong dev_get_of_data(struct udevice *dev); 218*2ef249b4SSimon Glass 219*2ef249b4SSimon Glass /** 220997c87bbSSimon Glass * device_get_child() - Get the child of a device by index 221997c87bbSSimon Glass * 222997c87bbSSimon Glass * Returns the numbered child, 0 being the first. This does not use 223997c87bbSSimon Glass * sequence numbers, only the natural order. 224997c87bbSSimon Glass * 225997c87bbSSimon Glass * @dev: Parent device to check 226997c87bbSSimon Glass * @index: Child index 227997c87bbSSimon Glass * @devp: Returns pointer to device 228997c87bbSSimon Glass */ 229997c87bbSSimon Glass int device_get_child(struct udevice *parent, int index, struct udevice **devp); 230997c87bbSSimon Glass 231997c87bbSSimon Glass /** 2325a66a8ffSSimon Glass * device_find_child_by_seq() - Find a child device based on a sequence 2335a66a8ffSSimon Glass * 2345a66a8ffSSimon Glass * This searches for a device with the given seq or req_seq. 2355a66a8ffSSimon Glass * 2365a66a8ffSSimon Glass * For seq, if an active device has this sequence it will be returned. 2375a66a8ffSSimon Glass * If there is no such device then this will return -ENODEV. 2385a66a8ffSSimon Glass * 2395a66a8ffSSimon Glass * For req_seq, if a device (whether activated or not) has this req_seq 2405a66a8ffSSimon Glass * value, that device will be returned. This is a strong indication that 2415a66a8ffSSimon Glass * the device will receive that sequence when activated. 2425a66a8ffSSimon Glass * 2435a66a8ffSSimon Glass * @parent: Parent device 2445a66a8ffSSimon Glass * @seq_or_req_seq: Sequence number to find (0=first) 2455a66a8ffSSimon Glass * @find_req_seq: true to find req_seq, false to find seq 2465a66a8ffSSimon Glass * @devp: Returns pointer to device (there is only one per for each seq). 2475a66a8ffSSimon Glass * Set to NULL if none is found 2485a66a8ffSSimon Glass * @return 0 if OK, -ve on error 2495a66a8ffSSimon Glass */ 2505a66a8ffSSimon Glass int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, 2515a66a8ffSSimon Glass bool find_req_seq, struct udevice **devp); 2525a66a8ffSSimon Glass 253997c87bbSSimon Glass /** 254997c87bbSSimon Glass * device_get_child_by_seq() - Get a child device based on a sequence 255997c87bbSSimon Glass * 256997c87bbSSimon Glass * If an active device has this sequence it will be returned. If there is no 257997c87bbSSimon Glass * such device then this will check for a device that is requesting this 258997c87bbSSimon Glass * sequence. 259997c87bbSSimon Glass * 260997c87bbSSimon Glass * The device is probed to activate it ready for use. 261997c87bbSSimon Glass * 262997c87bbSSimon Glass * @parent: Parent device 263997c87bbSSimon Glass * @seq: Sequence number to find (0=first) 264997c87bbSSimon Glass * @devp: Returns pointer to device (there is only one per for each seq) 265997c87bbSSimon Glass * Set to NULL if none is found 266997c87bbSSimon Glass * @return 0 if OK, -ve on error 267997c87bbSSimon Glass */ 268997c87bbSSimon Glass int device_get_child_by_seq(struct udevice *parent, int seq, 269997c87bbSSimon Glass struct udevice **devp); 270997c87bbSSimon Glass 271997c87bbSSimon Glass /** 272997c87bbSSimon Glass * device_find_child_by_of_offset() - Find a child device based on FDT offset 273997c87bbSSimon Glass * 274997c87bbSSimon Glass * Locates a child device by its device tree offset. 275997c87bbSSimon Glass * 276997c87bbSSimon Glass * @parent: Parent device 277997c87bbSSimon Glass * @of_offset: Device tree offset to find 278997c87bbSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL 279997c87bbSSimon Glass * @return 0 if OK, -ve on error 280997c87bbSSimon Glass */ 281997c87bbSSimon Glass int device_find_child_by_of_offset(struct udevice *parent, int of_offset, 282997c87bbSSimon Glass struct udevice **devp); 283997c87bbSSimon Glass 284997c87bbSSimon Glass /** 285997c87bbSSimon Glass * device_get_child_by_of_offset() - Get a child device based on FDT offset 286997c87bbSSimon Glass * 287997c87bbSSimon Glass * Locates a child device by its device tree offset. 288997c87bbSSimon Glass * 289997c87bbSSimon Glass * The device is probed to activate it ready for use. 290997c87bbSSimon Glass * 291997c87bbSSimon Glass * @parent: Parent device 292997c87bbSSimon Glass * @of_offset: Device tree offset to find 293997c87bbSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL 294997c87bbSSimon Glass * @return 0 if OK, -ve on error 295997c87bbSSimon Glass */ 296997c87bbSSimon Glass int device_get_child_by_of_offset(struct udevice *parent, int seq, 297997c87bbSSimon Glass struct udevice **devp); 298997c87bbSSimon Glass 299a8981d4fSSimon Glass /** 300a8981d4fSSimon Glass * device_find_first_child() - Find the first child of a device 301a8981d4fSSimon Glass * 302a8981d4fSSimon Glass * @parent: Parent device to search 303a8981d4fSSimon Glass * @devp: Returns first child device, or NULL if none 304a8981d4fSSimon Glass * @return 0 305a8981d4fSSimon Glass */ 306a8981d4fSSimon Glass int device_find_first_child(struct udevice *parent, struct udevice **devp); 307a8981d4fSSimon Glass 308a8981d4fSSimon Glass /** 309a8981d4fSSimon Glass * device_find_first_child() - Find the first child of a device 310a8981d4fSSimon Glass * 311a8981d4fSSimon Glass * @devp: Pointer to previous child device on entry. Returns pointer to next 312a8981d4fSSimon Glass * child device, or NULL if none 313a8981d4fSSimon Glass * @return 0 314a8981d4fSSimon Glass */ 315a8981d4fSSimon Glass int device_find_next_child(struct udevice **devp); 316a8981d4fSSimon Glass 3176494d708SSimon Glass #endif 318