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> 15c9cac3f8SPeng Fan #include <fdtdec.h> 166494d708SSimon Glass #include <linker_lists.h> 172b07f685SMasahiro Yamada #include <linux/compat.h> 182b07f685SMasahiro Yamada #include <linux/kernel.h> 196494d708SSimon Glass #include <linux/list.h> 206494d708SSimon Glass 216494d708SSimon Glass struct driver_info; 226494d708SSimon Glass 236494d708SSimon Glass /* Driver is active (probed). Cleared when it is removed */ 246494d708SSimon Glass #define DM_FLAG_ACTIVATED (1 << 0) 256494d708SSimon Glass 266494d708SSimon Glass /* DM is responsible for allocating and freeing platdata */ 27f2bc6fc3SSimon Glass #define DM_FLAG_ALLOC_PDATA (1 << 1) 286494d708SSimon Glass 2900606d7eSSimon Glass /* DM should init this device prior to relocation */ 3000606d7eSSimon Glass #define DM_FLAG_PRE_RELOC (1 << 2) 3100606d7eSSimon Glass 32cdc133bdSSimon Glass /* DM is responsible for allocating and freeing parent_platdata */ 33cdc133bdSSimon Glass #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3) 34cdc133bdSSimon Glass 355eaed880SPrzemyslaw Marczak /* DM is responsible for allocating and freeing uclass_platdata */ 365eaed880SPrzemyslaw Marczak #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4) 375eaed880SPrzemyslaw Marczak 382c03c463SSimon Glass /* Allocate driver private data on a DMA boundary */ 395eaed880SPrzemyslaw Marczak #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5) 402c03c463SSimon Glass 41aed1a4ddSMasahiro Yamada /* Device is bound */ 42aed1a4ddSMasahiro Yamada #define DM_FLAG_BOUND (1 << 6) 43aed1a4ddSMasahiro Yamada 446494d708SSimon Glass /** 4554c5d08aSHeiko Schocher * struct udevice - An instance of a driver 466494d708SSimon Glass * 476494d708SSimon Glass * This holds information about a device, which is a driver bound to a 486494d708SSimon Glass * particular port or peripheral (essentially a driver instance). 496494d708SSimon Glass * 506494d708SSimon Glass * A device will come into existence through a 'bind' call, either due to 516494d708SSimon Glass * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node 526494d708SSimon Glass * in the device tree (in which case of_offset is >= 0). In the latter case 536494d708SSimon Glass * we translate the device tree information into platdata in a function 546494d708SSimon Glass * implemented by the driver ofdata_to_platdata method (called just before the 556494d708SSimon Glass * probe method if the device has a device tree node. 566494d708SSimon Glass * 576494d708SSimon Glass * All three of platdata, priv and uclass_priv can be allocated by the 586494d708SSimon Glass * driver, or you can use the auto_alloc_size members of struct driver and 596494d708SSimon Glass * struct uclass_driver to have driver model do this automatically. 606494d708SSimon Glass * 616494d708SSimon Glass * @driver: The driver used by this device 626494d708SSimon Glass * @name: Name of device, typically the FDT node name 636494d708SSimon Glass * @platdata: Configuration data for this device 64cdc133bdSSimon Glass * @parent_platdata: The parent bus's configuration data for this device 655eaed880SPrzemyslaw Marczak * @uclass_platdata: The uclass's configuration data for this device 666494d708SSimon Glass * @of_offset: Device tree node offset for this device (- for none) 6739de8433SSimon Glass * @driver_data: Driver data word for the entry that matched this device with 6839de8433SSimon Glass * its driver 696494d708SSimon Glass * @parent: Parent of this device, or NULL for the top level device 706494d708SSimon Glass * @priv: Private data for this device 716494d708SSimon Glass * @uclass: Pointer to uclass for this device 726494d708SSimon Glass * @uclass_priv: The uclass's private data for this device 73e59f458dSSimon Glass * @parent_priv: The parent's private data for this device 746494d708SSimon Glass * @uclass_node: Used by uclass to link its devices 756494d708SSimon Glass * @child_head: List of children of this device 766494d708SSimon Glass * @sibling_node: Next device in list of all devices 776494d708SSimon Glass * @flags: Flags for this device DM_FLAG_... 785a66a8ffSSimon Glass * @req_seq: Requested sequence number for this device (-1 = any) 79547cea19SSimon Glass * @seq: Allocated sequence number for this device (-1 = none). This is set up 80547cea19SSimon Glass * when the device is probed and will be unique within the device's uclass. 816494d708SSimon Glass */ 8254c5d08aSHeiko Schocher struct udevice { 833479253dSSimon Glass const struct driver *driver; 846494d708SSimon Glass const char *name; 856494d708SSimon Glass void *platdata; 86cdc133bdSSimon Glass void *parent_platdata; 875eaed880SPrzemyslaw Marczak void *uclass_platdata; 886494d708SSimon Glass int of_offset; 8939de8433SSimon Glass ulong driver_data; 9054c5d08aSHeiko Schocher struct udevice *parent; 916494d708SSimon Glass void *priv; 926494d708SSimon Glass struct uclass *uclass; 936494d708SSimon Glass void *uclass_priv; 94e59f458dSSimon Glass void *parent_priv; 956494d708SSimon Glass struct list_head uclass_node; 966494d708SSimon Glass struct list_head child_head; 976494d708SSimon Glass struct list_head sibling_node; 986494d708SSimon Glass uint32_t flags; 995a66a8ffSSimon Glass int req_seq; 1005a66a8ffSSimon Glass int seq; 101*e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES 102608f26c5SMasahiro Yamada struct list_head devres_head; 103*e2282d70SMasahiro Yamada #endif 1046494d708SSimon Glass }; 1056494d708SSimon Glass 1065a66a8ffSSimon Glass /* Maximum sequence number supported */ 1075a66a8ffSSimon Glass #define DM_MAX_SEQ 999 1085a66a8ffSSimon Glass 1096494d708SSimon Glass /* Returns the operations for a device */ 1106494d708SSimon Glass #define device_get_ops(dev) (dev->driver->ops) 1116494d708SSimon Glass 1126494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */ 1136494d708SSimon Glass #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED) 1146494d708SSimon Glass 1156494d708SSimon Glass /** 116ae7f4513SSimon Glass * struct udevice_id - Lists the compatible strings supported by a driver 1176494d708SSimon Glass * @compatible: Compatible string 1186494d708SSimon Glass * @data: Data for this compatible string 1196494d708SSimon Glass */ 120ae7f4513SSimon Glass struct udevice_id { 1216494d708SSimon Glass const char *compatible; 1226494d708SSimon Glass ulong data; 1236494d708SSimon Glass }; 1246494d708SSimon Glass 125f887cb6dSMasahiro Yamada #ifdef CONFIG_OF_CONTROL 126f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr) (_ptr) 127f887cb6dSMasahiro Yamada #else 128f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr) NULL 129f887cb6dSMasahiro Yamada #endif /* CONFIG_OF_CONTROL */ 130f887cb6dSMasahiro Yamada 1316494d708SSimon Glass /** 1326494d708SSimon Glass * struct driver - A driver for a feature or peripheral 1336494d708SSimon Glass * 1346494d708SSimon Glass * This holds methods for setting up a new device, and also removing it. 1356494d708SSimon Glass * The device needs information to set itself up - this is provided either 1366494d708SSimon Glass * by platdata or a device tree node (which we find by looking up 1376494d708SSimon Glass * matching compatible strings with of_match). 1386494d708SSimon Glass * 1396494d708SSimon Glass * Drivers all belong to a uclass, representing a class of devices of the 1406494d708SSimon Glass * same type. Common elements of the drivers can be implemented in the uclass, 1416494d708SSimon Glass * or the uclass can provide a consistent interface to the drivers within 1426494d708SSimon Glass * it. 1436494d708SSimon Glass * 1446494d708SSimon Glass * @name: Device name 1456494d708SSimon Glass * @id: Identiies the uclass we belong to 1466494d708SSimon Glass * @of_match: List of compatible strings to match, and any identifying data 1476494d708SSimon Glass * for each. 1486494d708SSimon Glass * @bind: Called to bind a device to its driver 1496494d708SSimon Glass * @probe: Called to probe a device, i.e. activate it 1506494d708SSimon Glass * @remove: Called to remove a device, i.e. de-activate it 1516494d708SSimon Glass * @unbind: Called to unbind a device from its driver 1526494d708SSimon Glass * @ofdata_to_platdata: Called before probe to decode device tree data 1530118ce79SSimon Glass * @child_post_bind: Called after a new child has been bound 154a327dee0SSimon Glass * @child_pre_probe: Called before a child device is probed. The device has 155a327dee0SSimon Glass * memory allocated but it has not yet been probed. 156a327dee0SSimon Glass * @child_post_remove: Called after a child device is removed. The device 157a327dee0SSimon Glass * has memory allocated but its device_remove() method has been called. 1586494d708SSimon Glass * @priv_auto_alloc_size: If non-zero this is the size of the private data 1596494d708SSimon Glass * to be allocated in the device's ->priv pointer. If zero, then the driver 1606494d708SSimon Glass * is responsible for allocating any data required. 1616494d708SSimon Glass * @platdata_auto_alloc_size: If non-zero this is the size of the 1626494d708SSimon Glass * platform data to be allocated in the device's ->platdata pointer. 1636494d708SSimon Glass * This is typically only useful for device-tree-aware drivers (those with 1646494d708SSimon Glass * an of_match), since drivers which use platdata will have the data 1656494d708SSimon Glass * provided in the U_BOOT_DEVICE() instantiation. 166e59f458dSSimon Glass * @per_child_auto_alloc_size: Each device can hold private data owned by 167e59f458dSSimon Glass * its parent. If required this will be automatically allocated if this 168e59f458dSSimon Glass * value is non-zero. 169accd4b19SSimon Glass * TODO(sjg@chromium.org): I'm considering dropping this, and just having 170accd4b19SSimon Glass * device_probe_child() pass it in. So far the use case for allocating it 171accd4b19SSimon Glass * is SPI, but I found that unsatisfactory. Since it is here I will leave it 172accd4b19SSimon Glass * until things are clearer. 173cdc133bdSSimon Glass * @per_child_platdata_auto_alloc_size: A bus likes to store information about 174cdc133bdSSimon Glass * its children. If non-zero this is the size of this data, to be allocated 175cdc133bdSSimon Glass * in the child's parent_platdata pointer. 1760040b944SSimon Glass * @ops: Driver-specific operations. This is typically a list of function 1776494d708SSimon Glass * pointers defined by the driver, to implement driver functions required by 1786494d708SSimon Glass * the uclass. 17900606d7eSSimon Glass * @flags: driver flags - see DM_FLAGS_... 1806494d708SSimon Glass */ 1816494d708SSimon Glass struct driver { 1826494d708SSimon Glass char *name; 1836494d708SSimon Glass enum uclass_id id; 184ae7f4513SSimon Glass const struct udevice_id *of_match; 18554c5d08aSHeiko Schocher int (*bind)(struct udevice *dev); 18654c5d08aSHeiko Schocher int (*probe)(struct udevice *dev); 18754c5d08aSHeiko Schocher int (*remove)(struct udevice *dev); 18854c5d08aSHeiko Schocher int (*unbind)(struct udevice *dev); 18954c5d08aSHeiko Schocher int (*ofdata_to_platdata)(struct udevice *dev); 1900118ce79SSimon Glass int (*child_post_bind)(struct udevice *dev); 191a327dee0SSimon Glass int (*child_pre_probe)(struct udevice *dev); 192a327dee0SSimon Glass int (*child_post_remove)(struct udevice *dev); 1936494d708SSimon Glass int priv_auto_alloc_size; 1946494d708SSimon Glass int platdata_auto_alloc_size; 195e59f458dSSimon Glass int per_child_auto_alloc_size; 196cdc133bdSSimon Glass int per_child_platdata_auto_alloc_size; 1976494d708SSimon Glass const void *ops; /* driver-specific operations */ 19800606d7eSSimon Glass uint32_t flags; 1996494d708SSimon Glass }; 2006494d708SSimon Glass 2016494d708SSimon Glass /* Declare a new U-Boot driver */ 2026494d708SSimon Glass #define U_BOOT_DRIVER(__name) \ 2036494d708SSimon Glass ll_entry_declare(struct driver, __name, driver) 2046494d708SSimon Glass 2056494d708SSimon Glass /** 2066494d708SSimon Glass * dev_get_platdata() - Get the platform data for a device 2076494d708SSimon Glass * 2086494d708SSimon Glass * This checks that dev is not NULL, but no other checks for now 2096494d708SSimon Glass * 2106494d708SSimon Glass * @dev Device to check 2116494d708SSimon Glass * @return platform data, or NULL if none 2126494d708SSimon Glass */ 21354c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev); 2146494d708SSimon Glass 2156494d708SSimon Glass /** 216cdc133bdSSimon Glass * dev_get_parent_platdata() - Get the parent platform data for a device 217cdc133bdSSimon Glass * 218cdc133bdSSimon Glass * This checks that dev is not NULL, but no other checks for now 219cdc133bdSSimon Glass * 220cdc133bdSSimon Glass * @dev Device to check 221cdc133bdSSimon Glass * @return parent's platform data, or NULL if none 222cdc133bdSSimon Glass */ 223cdc133bdSSimon Glass void *dev_get_parent_platdata(struct udevice *dev); 224cdc133bdSSimon Glass 225cdc133bdSSimon Glass /** 2265eaed880SPrzemyslaw Marczak * dev_get_uclass_platdata() - Get the uclass platform data for a device 2275eaed880SPrzemyslaw Marczak * 2285eaed880SPrzemyslaw Marczak * This checks that dev is not NULL, but no other checks for now 2295eaed880SPrzemyslaw Marczak * 2305eaed880SPrzemyslaw Marczak * @dev Device to check 2315eaed880SPrzemyslaw Marczak * @return uclass's platform data, or NULL if none 2325eaed880SPrzemyslaw Marczak */ 2335eaed880SPrzemyslaw Marczak void *dev_get_uclass_platdata(struct udevice *dev); 2345eaed880SPrzemyslaw Marczak 2355eaed880SPrzemyslaw Marczak /** 236e59f458dSSimon Glass * dev_get_parentdata() - Get the parent data for a device 237e59f458dSSimon Glass * 238e59f458dSSimon Glass * The parent data is data stored in the device but owned by the parent. 239e59f458dSSimon Glass * For example, a USB device may have parent data which contains information 240e59f458dSSimon Glass * about how to talk to the device over USB. 241e59f458dSSimon Glass * 242e59f458dSSimon Glass * This checks that dev is not NULL, but no other checks for now 243e59f458dSSimon Glass * 244e59f458dSSimon Glass * @dev Device to check 245e59f458dSSimon Glass * @return parent data, or NULL if none 246e59f458dSSimon Glass */ 247e59f458dSSimon Glass void *dev_get_parentdata(struct udevice *dev); 248e59f458dSSimon Glass 249e59f458dSSimon Glass /** 2506494d708SSimon Glass * dev_get_priv() - Get the private data for a device 2516494d708SSimon Glass * 2526494d708SSimon Glass * This checks that dev is not NULL, but no other checks for now 2536494d708SSimon Glass * 2546494d708SSimon Glass * @dev Device to check 2556494d708SSimon Glass * @return private data, or NULL if none 2566494d708SSimon Glass */ 25754c5d08aSHeiko Schocher void *dev_get_priv(struct udevice *dev); 2586494d708SSimon Glass 2595a66a8ffSSimon Glass /** 260479728cbSSimon Glass * struct dev_get_parent() - Get the parent of a device 261479728cbSSimon Glass * 262479728cbSSimon Glass * @child: Child to check 263479728cbSSimon Glass * @return parent of child, or NULL if this is the root device 264479728cbSSimon Glass */ 265479728cbSSimon Glass struct udevice *dev_get_parent(struct udevice *child); 266479728cbSSimon Glass 267479728cbSSimon Glass /** 268e564f054SSimon Glass * dev_get_uclass_priv() - Get the private uclass data for a device 269e564f054SSimon Glass * 270e564f054SSimon Glass * This checks that dev is not NULL, but no other checks for now 271e564f054SSimon Glass * 272e564f054SSimon Glass * @dev Device to check 273e564f054SSimon Glass * @return private uclass data for this device, or NULL if none 274e564f054SSimon Glass */ 275e564f054SSimon Glass void *dev_get_uclass_priv(struct udevice *dev); 276e564f054SSimon Glass 277e564f054SSimon Glass /** 27839de8433SSimon Glass * dev_get_driver_data() - get the driver data used to bind a device 2792ef249b4SSimon Glass * 2802ef249b4SSimon Glass * When a device is bound using a device tree node, it matches a 2812ef249b4SSimon Glass * particular compatible string as in struct udevice_id. This function 28239de8433SSimon Glass * returns the associated data value for that compatible string. This is 28339de8433SSimon Glass * the 'data' field in struct udevice_id. 28439de8433SSimon Glass * 28539de8433SSimon Glass * For USB devices, this is the driver_info field in struct usb_device_id. 28639de8433SSimon Glass * 28739de8433SSimon Glass * @dev: Device to check 2882ef249b4SSimon Glass */ 28939de8433SSimon Glass ulong dev_get_driver_data(struct udevice *dev); 2902ef249b4SSimon Glass 291cc73d37bSPrzemyslaw Marczak /** 292cc73d37bSPrzemyslaw Marczak * dev_get_driver_ops() - get the device's driver's operations 293cc73d37bSPrzemyslaw Marczak * 294cc73d37bSPrzemyslaw Marczak * This checks that dev is not NULL, and returns the pointer to device's 295cc73d37bSPrzemyslaw Marczak * driver's operations. 296cc73d37bSPrzemyslaw Marczak * 297cc73d37bSPrzemyslaw Marczak * @dev: Device to check 298cc73d37bSPrzemyslaw Marczak * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops 299cc73d37bSPrzemyslaw Marczak */ 300cc73d37bSPrzemyslaw Marczak const void *dev_get_driver_ops(struct udevice *dev); 301cc73d37bSPrzemyslaw Marczak 302b3670531SSimon Glass /* 303b3670531SSimon Glass * device_get_uclass_id() - return the uclass ID of a device 304b3670531SSimon Glass * 305b3670531SSimon Glass * @dev: Device to check 306b3670531SSimon Glass * @return uclass ID for the device 307b3670531SSimon Glass */ 308b3670531SSimon Glass enum uclass_id device_get_uclass_id(struct udevice *dev); 309b3670531SSimon Glass 310f9c370dcSPrzemyslaw Marczak /* 311f9c370dcSPrzemyslaw Marczak * dev_get_uclass_name() - return the uclass name of a device 312f9c370dcSPrzemyslaw Marczak * 313f9c370dcSPrzemyslaw Marczak * This checks that dev is not NULL. 314f9c370dcSPrzemyslaw Marczak * 315f9c370dcSPrzemyslaw Marczak * @dev: Device to check 316f9c370dcSPrzemyslaw Marczak * @return pointer to the uclass name for the device 317f9c370dcSPrzemyslaw Marczak */ 318f9c370dcSPrzemyslaw Marczak const char *dev_get_uclass_name(struct udevice *dev); 319f9c370dcSPrzemyslaw Marczak 3202ef249b4SSimon Glass /** 321997c87bbSSimon Glass * device_get_child() - Get the child of a device by index 322997c87bbSSimon Glass * 323997c87bbSSimon Glass * Returns the numbered child, 0 being the first. This does not use 324997c87bbSSimon Glass * sequence numbers, only the natural order. 325997c87bbSSimon Glass * 326997c87bbSSimon Glass * @dev: Parent device to check 327997c87bbSSimon Glass * @index: Child index 328997c87bbSSimon Glass * @devp: Returns pointer to device 3293f416f33SSimon Glass * @return 0 if OK, -ENODEV if no such device, other error if the device fails 3303f416f33SSimon Glass * to probe 331997c87bbSSimon Glass */ 332997c87bbSSimon Glass int device_get_child(struct udevice *parent, int index, struct udevice **devp); 333997c87bbSSimon Glass 334997c87bbSSimon Glass /** 3355a66a8ffSSimon Glass * device_find_child_by_seq() - Find a child device based on a sequence 3365a66a8ffSSimon Glass * 3375a66a8ffSSimon Glass * This searches for a device with the given seq or req_seq. 3385a66a8ffSSimon Glass * 3395a66a8ffSSimon Glass * For seq, if an active device has this sequence it will be returned. 3405a66a8ffSSimon Glass * If there is no such device then this will return -ENODEV. 3415a66a8ffSSimon Glass * 3425a66a8ffSSimon Glass * For req_seq, if a device (whether activated or not) has this req_seq 3435a66a8ffSSimon Glass * value, that device will be returned. This is a strong indication that 3445a66a8ffSSimon Glass * the device will receive that sequence when activated. 3455a66a8ffSSimon Glass * 3465a66a8ffSSimon Glass * @parent: Parent device 3475a66a8ffSSimon Glass * @seq_or_req_seq: Sequence number to find (0=first) 3485a66a8ffSSimon Glass * @find_req_seq: true to find req_seq, false to find seq 3495a66a8ffSSimon Glass * @devp: Returns pointer to device (there is only one per for each seq). 3505a66a8ffSSimon Glass * Set to NULL if none is found 3515a66a8ffSSimon Glass * @return 0 if OK, -ve on error 3525a66a8ffSSimon Glass */ 3535a66a8ffSSimon Glass int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, 3545a66a8ffSSimon Glass bool find_req_seq, struct udevice **devp); 3555a66a8ffSSimon Glass 356997c87bbSSimon Glass /** 357997c87bbSSimon Glass * device_get_child_by_seq() - Get a child device based on a sequence 358997c87bbSSimon Glass * 359997c87bbSSimon Glass * If an active device has this sequence it will be returned. If there is no 360997c87bbSSimon Glass * such device then this will check for a device that is requesting this 361997c87bbSSimon Glass * sequence. 362997c87bbSSimon Glass * 363997c87bbSSimon Glass * The device is probed to activate it ready for use. 364997c87bbSSimon Glass * 365997c87bbSSimon Glass * @parent: Parent device 366997c87bbSSimon Glass * @seq: Sequence number to find (0=first) 367997c87bbSSimon Glass * @devp: Returns pointer to device (there is only one per for each seq) 368997c87bbSSimon Glass * Set to NULL if none is found 369997c87bbSSimon Glass * @return 0 if OK, -ve on error 370997c87bbSSimon Glass */ 371997c87bbSSimon Glass int device_get_child_by_seq(struct udevice *parent, int seq, 372997c87bbSSimon Glass struct udevice **devp); 373997c87bbSSimon Glass 374997c87bbSSimon Glass /** 375997c87bbSSimon Glass * device_find_child_by_of_offset() - Find a child device based on FDT offset 376997c87bbSSimon Glass * 377997c87bbSSimon Glass * Locates a child device by its device tree offset. 378997c87bbSSimon Glass * 379997c87bbSSimon Glass * @parent: Parent device 380997c87bbSSimon Glass * @of_offset: Device tree offset to find 381997c87bbSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL 382997c87bbSSimon Glass * @return 0 if OK, -ve on error 383997c87bbSSimon Glass */ 384997c87bbSSimon Glass int device_find_child_by_of_offset(struct udevice *parent, int of_offset, 385997c87bbSSimon Glass struct udevice **devp); 386997c87bbSSimon Glass 387997c87bbSSimon Glass /** 388997c87bbSSimon Glass * device_get_child_by_of_offset() - Get a child device based on FDT offset 389997c87bbSSimon Glass * 390997c87bbSSimon Glass * Locates a child device by its device tree offset. 391997c87bbSSimon Glass * 392997c87bbSSimon Glass * The device is probed to activate it ready for use. 393997c87bbSSimon Glass * 394997c87bbSSimon Glass * @parent: Parent device 395997c87bbSSimon Glass * @of_offset: Device tree offset to find 396997c87bbSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL 397997c87bbSSimon Glass * @return 0 if OK, -ve on error 398997c87bbSSimon Glass */ 399132f9bfcSSimon Glass int device_get_child_by_of_offset(struct udevice *parent, int of_offset, 400997c87bbSSimon Glass struct udevice **devp); 401997c87bbSSimon Glass 402a8981d4fSSimon Glass /** 4032693047aSSimon Glass * device_get_global_by_of_offset() - Get a device based on FDT offset 4042693047aSSimon Glass * 4052693047aSSimon Glass * Locates a device by its device tree offset, searching globally throughout 4062693047aSSimon Glass * the all driver model devices. 4072693047aSSimon Glass * 4082693047aSSimon Glass * The device is probed to activate it ready for use. 4092693047aSSimon Glass * 4102693047aSSimon Glass * @of_offset: Device tree offset to find 4112693047aSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL 4122693047aSSimon Glass * @return 0 if OK, -ve on error 4132693047aSSimon Glass */ 4142693047aSSimon Glass int device_get_global_by_of_offset(int of_offset, struct udevice **devp); 4152693047aSSimon Glass 4162693047aSSimon Glass /** 417a8981d4fSSimon Glass * device_find_first_child() - Find the first child of a device 418a8981d4fSSimon Glass * 419a8981d4fSSimon Glass * @parent: Parent device to search 420a8981d4fSSimon Glass * @devp: Returns first child device, or NULL if none 421a8981d4fSSimon Glass * @return 0 422a8981d4fSSimon Glass */ 423a8981d4fSSimon Glass int device_find_first_child(struct udevice *parent, struct udevice **devp); 424a8981d4fSSimon Glass 425a8981d4fSSimon Glass /** 4263f416f33SSimon Glass * device_find_next_child() - Find the next child of a device 427a8981d4fSSimon Glass * 428a8981d4fSSimon Glass * @devp: Pointer to previous child device on entry. Returns pointer to next 429a8981d4fSSimon Glass * child device, or NULL if none 430a8981d4fSSimon Glass * @return 0 431a8981d4fSSimon Glass */ 432a8981d4fSSimon Glass int device_find_next_child(struct udevice **devp); 433a8981d4fSSimon Glass 434c9cac3f8SPeng Fan /** 435c9cac3f8SPeng Fan * dev_get_addr() - Get the reg property of a device 436c9cac3f8SPeng Fan * 437c9cac3f8SPeng Fan * @dev: Pointer to a device 438c9cac3f8SPeng Fan * 439c9cac3f8SPeng Fan * @return addr 440c9cac3f8SPeng Fan */ 441c9cac3f8SPeng Fan fdt_addr_t dev_get_addr(struct udevice *dev); 442c9cac3f8SPeng Fan 443c5785673SSimon Glass /** 444c5785673SSimon Glass * device_has_children() - check if a device has any children 445c5785673SSimon Glass * 446c5785673SSimon Glass * @dev: Device to check 447c5785673SSimon Glass * @return true if the device has one or more children 448c5785673SSimon Glass */ 449c5785673SSimon Glass bool device_has_children(struct udevice *dev); 450c5785673SSimon Glass 451c5785673SSimon Glass /** 452c5785673SSimon Glass * device_has_active_children() - check if a device has any active children 453c5785673SSimon Glass * 454c5785673SSimon Glass * @dev: Device to check 455c5785673SSimon Glass * @return true if the device has one or more children and at least one of 456c5785673SSimon Glass * them is active (probed). 457c5785673SSimon Glass */ 458c5785673SSimon Glass bool device_has_active_children(struct udevice *dev); 459c5785673SSimon Glass 460c5785673SSimon Glass /** 461c5785673SSimon Glass * device_is_last_sibling() - check if a device is the last sibling 462c5785673SSimon Glass * 463c5785673SSimon Glass * This function can be useful for display purposes, when special action needs 464c5785673SSimon Glass * to be taken when displaying the last sibling. This can happen when a tree 465c5785673SSimon Glass * view of devices is being displayed. 466c5785673SSimon Glass * 467c5785673SSimon Glass * @dev: Device to check 468c5785673SSimon Glass * @return true if there are no more siblings after this one - i.e. is it 469c5785673SSimon Glass * last in the list. 470c5785673SSimon Glass */ 471c5785673SSimon Glass bool device_is_last_sibling(struct udevice *dev); 472c5785673SSimon Glass 473608f26c5SMasahiro Yamada /* device resource management */ 474608f26c5SMasahiro Yamada typedef void (*dr_release_t)(struct udevice *dev, void *res); 475608f26c5SMasahiro Yamada typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data); 476608f26c5SMasahiro Yamada 477*e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES 478*e2282d70SMasahiro Yamada 479608f26c5SMasahiro Yamada #ifdef CONFIG_DEBUG_DEVRES 480608f26c5SMasahiro Yamada void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp, 481608f26c5SMasahiro Yamada const char *name); 482608f26c5SMasahiro Yamada #define _devres_alloc(release, size, gfp) \ 483608f26c5SMasahiro Yamada __devres_alloc(release, size, gfp, #release) 484608f26c5SMasahiro Yamada #else 485608f26c5SMasahiro Yamada void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp); 486608f26c5SMasahiro Yamada #endif 487608f26c5SMasahiro Yamada 488608f26c5SMasahiro Yamada /** 489608f26c5SMasahiro Yamada * devres_alloc - Allocate device resource data 490608f26c5SMasahiro Yamada * @release: Release function devres will be associated with 491608f26c5SMasahiro Yamada * @size: Allocation size 492608f26c5SMasahiro Yamada * @gfp: Allocation flags 493608f26c5SMasahiro Yamada * 494608f26c5SMasahiro Yamada * Allocate devres of @size bytes. The allocated area is associated 495608f26c5SMasahiro Yamada * with @release. The returned pointer can be passed to 496608f26c5SMasahiro Yamada * other devres_*() functions. 497608f26c5SMasahiro Yamada * 498608f26c5SMasahiro Yamada * RETURNS: 499608f26c5SMasahiro Yamada * Pointer to allocated devres on success, NULL on failure. 500608f26c5SMasahiro Yamada */ 501608f26c5SMasahiro Yamada #define devres_alloc(release, size, gfp) \ 502608f26c5SMasahiro Yamada _devres_alloc(release, size, gfp | __GFP_ZERO) 503608f26c5SMasahiro Yamada 504608f26c5SMasahiro Yamada /** 505608f26c5SMasahiro Yamada * devres_free - Free device resource data 506608f26c5SMasahiro Yamada * @res: Pointer to devres data to free 507608f26c5SMasahiro Yamada * 508608f26c5SMasahiro Yamada * Free devres created with devres_alloc(). 509608f26c5SMasahiro Yamada */ 510608f26c5SMasahiro Yamada void devres_free(void *res); 511608f26c5SMasahiro Yamada 512608f26c5SMasahiro Yamada /** 513608f26c5SMasahiro Yamada * devres_add - Register device resource 514608f26c5SMasahiro Yamada * @dev: Device to add resource to 515608f26c5SMasahiro Yamada * @res: Resource to register 516608f26c5SMasahiro Yamada * 517608f26c5SMasahiro Yamada * Register devres @res to @dev. @res should have been allocated 518608f26c5SMasahiro Yamada * using devres_alloc(). On driver detach, the associated release 519608f26c5SMasahiro Yamada * function will be invoked and devres will be freed automatically. 520608f26c5SMasahiro Yamada */ 521608f26c5SMasahiro Yamada void devres_add(struct udevice *dev, void *res); 522608f26c5SMasahiro Yamada 523608f26c5SMasahiro Yamada /** 524608f26c5SMasahiro Yamada * devres_find - Find device resource 525608f26c5SMasahiro Yamada * @dev: Device to lookup resource from 526608f26c5SMasahiro Yamada * @release: Look for resources associated with this release function 527608f26c5SMasahiro Yamada * @match: Match function (optional) 528608f26c5SMasahiro Yamada * @match_data: Data for the match function 529608f26c5SMasahiro Yamada * 530608f26c5SMasahiro Yamada * Find the latest devres of @dev which is associated with @release 531608f26c5SMasahiro Yamada * and for which @match returns 1. If @match is NULL, it's considered 532608f26c5SMasahiro Yamada * to match all. 533608f26c5SMasahiro Yamada * 534608f26c5SMasahiro Yamada * RETURNS: 535608f26c5SMasahiro Yamada * Pointer to found devres, NULL if not found. 536608f26c5SMasahiro Yamada */ 537608f26c5SMasahiro Yamada void *devres_find(struct udevice *dev, dr_release_t release, 538608f26c5SMasahiro Yamada dr_match_t match, void *match_data); 539608f26c5SMasahiro Yamada 540608f26c5SMasahiro Yamada /** 541608f26c5SMasahiro Yamada * devres_get - Find devres, if non-existent, add one atomically 542608f26c5SMasahiro Yamada * @dev: Device to lookup or add devres for 543608f26c5SMasahiro Yamada * @new_res: Pointer to new initialized devres to add if not found 544608f26c5SMasahiro Yamada * @match: Match function (optional) 545608f26c5SMasahiro Yamada * @match_data: Data for the match function 546608f26c5SMasahiro Yamada * 547608f26c5SMasahiro Yamada * Find the latest devres of @dev which has the same release function 548608f26c5SMasahiro Yamada * as @new_res and for which @match return 1. If found, @new_res is 549608f26c5SMasahiro Yamada * freed; otherwise, @new_res is added atomically. 550608f26c5SMasahiro Yamada * 551608f26c5SMasahiro Yamada * RETURNS: 552608f26c5SMasahiro Yamada * Pointer to found or added devres. 553608f26c5SMasahiro Yamada */ 554608f26c5SMasahiro Yamada void *devres_get(struct udevice *dev, void *new_res, 555608f26c5SMasahiro Yamada dr_match_t match, void *match_data); 556608f26c5SMasahiro Yamada 557608f26c5SMasahiro Yamada /** 558608f26c5SMasahiro Yamada * devres_remove - Find a device resource and remove it 559608f26c5SMasahiro Yamada * @dev: Device to find resource from 560608f26c5SMasahiro Yamada * @release: Look for resources associated with this release function 561608f26c5SMasahiro Yamada * @match: Match function (optional) 562608f26c5SMasahiro Yamada * @match_data: Data for the match function 563608f26c5SMasahiro Yamada * 564608f26c5SMasahiro Yamada * Find the latest devres of @dev associated with @release and for 565608f26c5SMasahiro Yamada * which @match returns 1. If @match is NULL, it's considered to 566608f26c5SMasahiro Yamada * match all. If found, the resource is removed atomically and 567608f26c5SMasahiro Yamada * returned. 568608f26c5SMasahiro Yamada * 569608f26c5SMasahiro Yamada * RETURNS: 570608f26c5SMasahiro Yamada * Pointer to removed devres on success, NULL if not found. 571608f26c5SMasahiro Yamada */ 572608f26c5SMasahiro Yamada void *devres_remove(struct udevice *dev, dr_release_t release, 573608f26c5SMasahiro Yamada dr_match_t match, void *match_data); 574608f26c5SMasahiro Yamada 575608f26c5SMasahiro Yamada /** 576608f26c5SMasahiro Yamada * devres_destroy - Find a device resource and destroy it 577608f26c5SMasahiro Yamada * @dev: Device to find resource from 578608f26c5SMasahiro Yamada * @release: Look for resources associated with this release function 579608f26c5SMasahiro Yamada * @match: Match function (optional) 580608f26c5SMasahiro Yamada * @match_data: Data for the match function 581608f26c5SMasahiro Yamada * 582608f26c5SMasahiro Yamada * Find the latest devres of @dev associated with @release and for 583608f26c5SMasahiro Yamada * which @match returns 1. If @match is NULL, it's considered to 584608f26c5SMasahiro Yamada * match all. If found, the resource is removed atomically and freed. 585608f26c5SMasahiro Yamada * 586608f26c5SMasahiro Yamada * Note that the release function for the resource will not be called, 587608f26c5SMasahiro Yamada * only the devres-allocated data will be freed. The caller becomes 588608f26c5SMasahiro Yamada * responsible for freeing any other data. 589608f26c5SMasahiro Yamada * 590608f26c5SMasahiro Yamada * RETURNS: 591608f26c5SMasahiro Yamada * 0 if devres is found and freed, -ENOENT if not found. 592608f26c5SMasahiro Yamada */ 593608f26c5SMasahiro Yamada int devres_destroy(struct udevice *dev, dr_release_t release, 594608f26c5SMasahiro Yamada dr_match_t match, void *match_data); 595608f26c5SMasahiro Yamada 596608f26c5SMasahiro Yamada /** 597608f26c5SMasahiro Yamada * devres_release - Find a device resource and destroy it, calling release 598608f26c5SMasahiro Yamada * @dev: Device to find resource from 599608f26c5SMasahiro Yamada * @release: Look for resources associated with this release function 600608f26c5SMasahiro Yamada * @match: Match function (optional) 601608f26c5SMasahiro Yamada * @match_data: Data for the match function 602608f26c5SMasahiro Yamada * 603608f26c5SMasahiro Yamada * Find the latest devres of @dev associated with @release and for 604608f26c5SMasahiro Yamada * which @match returns 1. If @match is NULL, it's considered to 605608f26c5SMasahiro Yamada * match all. If found, the resource is removed atomically, the 606608f26c5SMasahiro Yamada * release function called and the resource freed. 607608f26c5SMasahiro Yamada * 608608f26c5SMasahiro Yamada * RETURNS: 609608f26c5SMasahiro Yamada * 0 if devres is found and freed, -ENOENT if not found. 610608f26c5SMasahiro Yamada */ 611608f26c5SMasahiro Yamada int devres_release(struct udevice *dev, dr_release_t release, 612608f26c5SMasahiro Yamada dr_match_t match, void *match_data); 613608f26c5SMasahiro Yamada 6142b07f685SMasahiro Yamada /* managed devm_k.alloc/kfree for device drivers */ 6152b07f685SMasahiro Yamada /** 6162b07f685SMasahiro Yamada * devm_kmalloc - Resource-managed kmalloc 6172b07f685SMasahiro Yamada * @dev: Device to allocate memory for 6182b07f685SMasahiro Yamada * @size: Allocation size 6192b07f685SMasahiro Yamada * @gfp: Allocation gfp flags 6202b07f685SMasahiro Yamada * 6212b07f685SMasahiro Yamada * Managed kmalloc. Memory allocated with this function is 6222b07f685SMasahiro Yamada * automatically freed on driver detach. Like all other devres 6232b07f685SMasahiro Yamada * resources, guaranteed alignment is unsigned long long. 6242b07f685SMasahiro Yamada * 6252b07f685SMasahiro Yamada * RETURNS: 6262b07f685SMasahiro Yamada * Pointer to allocated memory on success, NULL on failure. 6272b07f685SMasahiro Yamada */ 6282b07f685SMasahiro Yamada void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp); 6292b07f685SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp) 6302b07f685SMasahiro Yamada { 6312b07f685SMasahiro Yamada return devm_kmalloc(dev, size, gfp | __GFP_ZERO); 6322b07f685SMasahiro Yamada } 6332b07f685SMasahiro Yamada static inline void *devm_kmalloc_array(struct udevice *dev, 6342b07f685SMasahiro Yamada size_t n, size_t size, gfp_t flags) 6352b07f685SMasahiro Yamada { 6362b07f685SMasahiro Yamada if (size != 0 && n > SIZE_MAX / size) 6372b07f685SMasahiro Yamada return NULL; 6382b07f685SMasahiro Yamada return devm_kmalloc(dev, n * size, flags); 6392b07f685SMasahiro Yamada } 6402b07f685SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev, 6412b07f685SMasahiro Yamada size_t n, size_t size, gfp_t flags) 6422b07f685SMasahiro Yamada { 6432b07f685SMasahiro Yamada return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO); 6442b07f685SMasahiro Yamada } 6452b07f685SMasahiro Yamada 6462b07f685SMasahiro Yamada /** 6472b07f685SMasahiro Yamada * devm_kfree - Resource-managed kfree 6482b07f685SMasahiro Yamada * @dev: Device this memory belongs to 6492b07f685SMasahiro Yamada * @p: Memory to free 6502b07f685SMasahiro Yamada * 6512b07f685SMasahiro Yamada * Free memory allocated with devm_kmalloc(). 6522b07f685SMasahiro Yamada */ 6532b07f685SMasahiro Yamada void devm_kfree(struct udevice *dev, void *p); 6542b07f685SMasahiro Yamada 655*e2282d70SMasahiro Yamada #else /* ! CONFIG_DEVRES */ 656*e2282d70SMasahiro Yamada 657*e2282d70SMasahiro Yamada static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp) 658*e2282d70SMasahiro Yamada { 659*e2282d70SMasahiro Yamada return kzalloc(size, gfp); 660*e2282d70SMasahiro Yamada } 661*e2282d70SMasahiro Yamada 662*e2282d70SMasahiro Yamada static inline void devres_free(void *res) 663*e2282d70SMasahiro Yamada { 664*e2282d70SMasahiro Yamada kfree(res); 665*e2282d70SMasahiro Yamada } 666*e2282d70SMasahiro Yamada 667*e2282d70SMasahiro Yamada static inline void devres_add(struct udevice *dev, void *res) 668*e2282d70SMasahiro Yamada { 669*e2282d70SMasahiro Yamada } 670*e2282d70SMasahiro Yamada 671*e2282d70SMasahiro Yamada static inline void *devres_find(struct udevice *dev, dr_release_t release, 672*e2282d70SMasahiro Yamada dr_match_t match, void *match_data) 673*e2282d70SMasahiro Yamada { 674*e2282d70SMasahiro Yamada return NULL; 675*e2282d70SMasahiro Yamada } 676*e2282d70SMasahiro Yamada 677*e2282d70SMasahiro Yamada static inline void *devres_get(struct udevice *dev, void *new_res, 678*e2282d70SMasahiro Yamada dr_match_t match, void *match_data) 679*e2282d70SMasahiro Yamada { 680*e2282d70SMasahiro Yamada return NULL; 681*e2282d70SMasahiro Yamada } 682*e2282d70SMasahiro Yamada 683*e2282d70SMasahiro Yamada static inline void *devres_remove(struct udevice *dev, dr_release_t release, 684*e2282d70SMasahiro Yamada dr_match_t match, void *match_data) 685*e2282d70SMasahiro Yamada { 686*e2282d70SMasahiro Yamada return NULL; 687*e2282d70SMasahiro Yamada } 688*e2282d70SMasahiro Yamada 689*e2282d70SMasahiro Yamada static inline int devres_destroy(struct udevice *dev, dr_release_t release, 690*e2282d70SMasahiro Yamada dr_match_t match, void *match_data) 691*e2282d70SMasahiro Yamada { 692*e2282d70SMasahiro Yamada return 0; 693*e2282d70SMasahiro Yamada } 694*e2282d70SMasahiro Yamada 695*e2282d70SMasahiro Yamada static inline int devres_release(struct udevice *dev, dr_release_t release, 696*e2282d70SMasahiro Yamada dr_match_t match, void *match_data) 697*e2282d70SMasahiro Yamada { 698*e2282d70SMasahiro Yamada return 0; 699*e2282d70SMasahiro Yamada } 700*e2282d70SMasahiro Yamada 701*e2282d70SMasahiro Yamada static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp) 702*e2282d70SMasahiro Yamada { 703*e2282d70SMasahiro Yamada return kmalloc(size, gfp); 704*e2282d70SMasahiro Yamada } 705*e2282d70SMasahiro Yamada 706*e2282d70SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp) 707*e2282d70SMasahiro Yamada { 708*e2282d70SMasahiro Yamada return kzalloc(size, gfp); 709*e2282d70SMasahiro Yamada } 710*e2282d70SMasahiro Yamada 711*e2282d70SMasahiro Yamada static inline void *devm_kmaloc_array(struct udevice *dev, 712*e2282d70SMasahiro Yamada size_t n, size_t size, gfp_t flags) 713*e2282d70SMasahiro Yamada { 714*e2282d70SMasahiro Yamada /* TODO: add kmalloc_array() to linux/compat.h */ 715*e2282d70SMasahiro Yamada if (size != 0 && n > SIZE_MAX / size) 716*e2282d70SMasahiro Yamada return NULL; 717*e2282d70SMasahiro Yamada return kmalloc(n * size, flags); 718*e2282d70SMasahiro Yamada } 719*e2282d70SMasahiro Yamada 720*e2282d70SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev, 721*e2282d70SMasahiro Yamada size_t n, size_t size, gfp_t flags) 722*e2282d70SMasahiro Yamada { 723*e2282d70SMasahiro Yamada /* TODO: add kcalloc() to linux/compat.h */ 724*e2282d70SMasahiro Yamada return kmalloc(n * size, flags | __GFP_ZERO); 725*e2282d70SMasahiro Yamada } 726*e2282d70SMasahiro Yamada 727*e2282d70SMasahiro Yamada static inline void devm_kfree(struct udevice *dev, void *p) 728*e2282d70SMasahiro Yamada { 729*e2282d70SMasahiro Yamada kfree(p); 730*e2282d70SMasahiro Yamada } 731*e2282d70SMasahiro Yamada 732*e2282d70SMasahiro Yamada #endif /* ! CONFIG_DEVRES */ 7332b07f685SMasahiro Yamada 7346494d708SSimon Glass #endif 735