xref: /rk3399_rockchip-uboot/include/dm/device.h (revision 608f26c51bebc68db7f2edc7590ee513d2bc5465)
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>
176494d708SSimon Glass #include <linux/list.h>
186494d708SSimon Glass 
196494d708SSimon Glass struct driver_info;
206494d708SSimon Glass 
216494d708SSimon Glass /* Driver is active (probed). Cleared when it is removed */
226494d708SSimon Glass #define DM_FLAG_ACTIVATED	(1 << 0)
236494d708SSimon Glass 
246494d708SSimon Glass /* DM is responsible for allocating and freeing platdata */
25f2bc6fc3SSimon Glass #define DM_FLAG_ALLOC_PDATA	(1 << 1)
266494d708SSimon Glass 
2700606d7eSSimon Glass /* DM should init this device prior to relocation */
2800606d7eSSimon Glass #define DM_FLAG_PRE_RELOC	(1 << 2)
2900606d7eSSimon Glass 
30cdc133bdSSimon Glass /* DM is responsible for allocating and freeing parent_platdata */
31cdc133bdSSimon Glass #define DM_FLAG_ALLOC_PARENT_PDATA	(1 << 3)
32cdc133bdSSimon Glass 
335eaed880SPrzemyslaw Marczak /* DM is responsible for allocating and freeing uclass_platdata */
345eaed880SPrzemyslaw Marczak #define DM_FLAG_ALLOC_UCLASS_PDATA	(1 << 4)
355eaed880SPrzemyslaw Marczak 
362c03c463SSimon Glass /* Allocate driver private data on a DMA boundary */
375eaed880SPrzemyslaw Marczak #define DM_FLAG_ALLOC_PRIV_DMA	(1 << 5)
382c03c463SSimon Glass 
39aed1a4ddSMasahiro Yamada /* Device is bound */
40aed1a4ddSMasahiro Yamada #define DM_FLAG_BOUND	(1 << 6)
41aed1a4ddSMasahiro Yamada 
426494d708SSimon Glass /**
4354c5d08aSHeiko Schocher  * struct udevice - An instance of a driver
446494d708SSimon Glass  *
456494d708SSimon Glass  * This holds information about a device, which is a driver bound to a
466494d708SSimon Glass  * particular port or peripheral (essentially a driver instance).
476494d708SSimon Glass  *
486494d708SSimon Glass  * A device will come into existence through a 'bind' call, either due to
496494d708SSimon Glass  * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
506494d708SSimon Glass  * in the device tree (in which case of_offset is >= 0). In the latter case
516494d708SSimon Glass  * we translate the device tree information into platdata in a function
526494d708SSimon Glass  * implemented by the driver ofdata_to_platdata method (called just before the
536494d708SSimon Glass  * probe method if the device has a device tree node.
546494d708SSimon Glass  *
556494d708SSimon Glass  * All three of platdata, priv and uclass_priv can be allocated by the
566494d708SSimon Glass  * driver, or you can use the auto_alloc_size members of struct driver and
576494d708SSimon Glass  * struct uclass_driver to have driver model do this automatically.
586494d708SSimon Glass  *
596494d708SSimon Glass  * @driver: The driver used by this device
606494d708SSimon Glass  * @name: Name of device, typically the FDT node name
616494d708SSimon Glass  * @platdata: Configuration data for this device
62cdc133bdSSimon Glass  * @parent_platdata: The parent bus's configuration data for this device
635eaed880SPrzemyslaw Marczak  * @uclass_platdata: The uclass's configuration data for this device
646494d708SSimon Glass  * @of_offset: Device tree node offset for this device (- for none)
6539de8433SSimon Glass  * @driver_data: Driver data word for the entry that matched this device with
6639de8433SSimon Glass  *		its driver
676494d708SSimon Glass  * @parent: Parent of this device, or NULL for the top level device
686494d708SSimon Glass  * @priv: Private data for this device
696494d708SSimon Glass  * @uclass: Pointer to uclass for this device
706494d708SSimon Glass  * @uclass_priv: The uclass's private data for this device
71e59f458dSSimon Glass  * @parent_priv: The parent's private data for this device
726494d708SSimon Glass  * @uclass_node: Used by uclass to link its devices
736494d708SSimon Glass  * @child_head: List of children of this device
746494d708SSimon Glass  * @sibling_node: Next device in list of all devices
756494d708SSimon Glass  * @flags: Flags for this device DM_FLAG_...
765a66a8ffSSimon Glass  * @req_seq: Requested sequence number for this device (-1 = any)
77547cea19SSimon Glass  * @seq: Allocated sequence number for this device (-1 = none). This is set up
78547cea19SSimon Glass  * when the device is probed and will be unique within the device's uclass.
796494d708SSimon Glass  */
8054c5d08aSHeiko Schocher struct udevice {
813479253dSSimon Glass 	const struct driver *driver;
826494d708SSimon Glass 	const char *name;
836494d708SSimon Glass 	void *platdata;
84cdc133bdSSimon Glass 	void *parent_platdata;
855eaed880SPrzemyslaw Marczak 	void *uclass_platdata;
866494d708SSimon Glass 	int of_offset;
8739de8433SSimon Glass 	ulong driver_data;
8854c5d08aSHeiko Schocher 	struct udevice *parent;
896494d708SSimon Glass 	void *priv;
906494d708SSimon Glass 	struct uclass *uclass;
916494d708SSimon Glass 	void *uclass_priv;
92e59f458dSSimon Glass 	void *parent_priv;
936494d708SSimon Glass 	struct list_head uclass_node;
946494d708SSimon Glass 	struct list_head child_head;
956494d708SSimon Glass 	struct list_head sibling_node;
966494d708SSimon Glass 	uint32_t flags;
975a66a8ffSSimon Glass 	int req_seq;
985a66a8ffSSimon Glass 	int seq;
99*608f26c5SMasahiro Yamada 	struct list_head devres_head;
1006494d708SSimon Glass };
1016494d708SSimon Glass 
1025a66a8ffSSimon Glass /* Maximum sequence number supported */
1035a66a8ffSSimon Glass #define DM_MAX_SEQ	999
1045a66a8ffSSimon Glass 
1056494d708SSimon Glass /* Returns the operations for a device */
1066494d708SSimon Glass #define device_get_ops(dev)	(dev->driver->ops)
1076494d708SSimon Glass 
1086494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */
1096494d708SSimon Glass #define device_active(dev)	((dev)->flags & DM_FLAG_ACTIVATED)
1106494d708SSimon Glass 
1116494d708SSimon Glass /**
112ae7f4513SSimon Glass  * struct udevice_id - Lists the compatible strings supported by a driver
1136494d708SSimon Glass  * @compatible: Compatible string
1146494d708SSimon Glass  * @data: Data for this compatible string
1156494d708SSimon Glass  */
116ae7f4513SSimon Glass struct udevice_id {
1176494d708SSimon Glass 	const char *compatible;
1186494d708SSimon Glass 	ulong data;
1196494d708SSimon Glass };
1206494d708SSimon Glass 
121f887cb6dSMasahiro Yamada #ifdef CONFIG_OF_CONTROL
122f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr)	(_ptr)
123f887cb6dSMasahiro Yamada #else
124f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr)	NULL
125f887cb6dSMasahiro Yamada #endif /* CONFIG_OF_CONTROL */
126f887cb6dSMasahiro Yamada 
1276494d708SSimon Glass /**
1286494d708SSimon Glass  * struct driver - A driver for a feature or peripheral
1296494d708SSimon Glass  *
1306494d708SSimon Glass  * This holds methods for setting up a new device, and also removing it.
1316494d708SSimon Glass  * The device needs information to set itself up - this is provided either
1326494d708SSimon Glass  * by platdata or a device tree node (which we find by looking up
1336494d708SSimon Glass  * matching compatible strings with of_match).
1346494d708SSimon Glass  *
1356494d708SSimon Glass  * Drivers all belong to a uclass, representing a class of devices of the
1366494d708SSimon Glass  * same type. Common elements of the drivers can be implemented in the uclass,
1376494d708SSimon Glass  * or the uclass can provide a consistent interface to the drivers within
1386494d708SSimon Glass  * it.
1396494d708SSimon Glass  *
1406494d708SSimon Glass  * @name: Device name
1416494d708SSimon Glass  * @id: Identiies the uclass we belong to
1426494d708SSimon Glass  * @of_match: List of compatible strings to match, and any identifying data
1436494d708SSimon Glass  * for each.
1446494d708SSimon Glass  * @bind: Called to bind a device to its driver
1456494d708SSimon Glass  * @probe: Called to probe a device, i.e. activate it
1466494d708SSimon Glass  * @remove: Called to remove a device, i.e. de-activate it
1476494d708SSimon Glass  * @unbind: Called to unbind a device from its driver
1486494d708SSimon Glass  * @ofdata_to_platdata: Called before probe to decode device tree data
1490118ce79SSimon Glass  * @child_post_bind: Called after a new child has been bound
150a327dee0SSimon Glass  * @child_pre_probe: Called before a child device is probed. The device has
151a327dee0SSimon Glass  * memory allocated but it has not yet been probed.
152a327dee0SSimon Glass  * @child_post_remove: Called after a child device is removed. The device
153a327dee0SSimon Glass  * has memory allocated but its device_remove() method has been called.
1546494d708SSimon Glass  * @priv_auto_alloc_size: If non-zero this is the size of the private data
1556494d708SSimon Glass  * to be allocated in the device's ->priv pointer. If zero, then the driver
1566494d708SSimon Glass  * is responsible for allocating any data required.
1576494d708SSimon Glass  * @platdata_auto_alloc_size: If non-zero this is the size of the
1586494d708SSimon Glass  * platform data to be allocated in the device's ->platdata pointer.
1596494d708SSimon Glass  * This is typically only useful for device-tree-aware drivers (those with
1606494d708SSimon Glass  * an of_match), since drivers which use platdata will have the data
1616494d708SSimon Glass  * provided in the U_BOOT_DEVICE() instantiation.
162e59f458dSSimon Glass  * @per_child_auto_alloc_size: Each device can hold private data owned by
163e59f458dSSimon Glass  * its parent. If required this will be automatically allocated if this
164e59f458dSSimon Glass  * value is non-zero.
165accd4b19SSimon Glass  * TODO(sjg@chromium.org): I'm considering dropping this, and just having
166accd4b19SSimon Glass  * device_probe_child() pass it in. So far the use case for allocating it
167accd4b19SSimon Glass  * is SPI, but I found that unsatisfactory. Since it is here I will leave it
168accd4b19SSimon Glass  * until things are clearer.
169cdc133bdSSimon Glass  * @per_child_platdata_auto_alloc_size: A bus likes to store information about
170cdc133bdSSimon Glass  * its children. If non-zero this is the size of this data, to be allocated
171cdc133bdSSimon Glass  * in the child's parent_platdata pointer.
1720040b944SSimon Glass  * @ops: Driver-specific operations. This is typically a list of function
1736494d708SSimon Glass  * pointers defined by the driver, to implement driver functions required by
1746494d708SSimon Glass  * the uclass.
17500606d7eSSimon Glass  * @flags: driver flags - see DM_FLAGS_...
1766494d708SSimon Glass  */
1776494d708SSimon Glass struct driver {
1786494d708SSimon Glass 	char *name;
1796494d708SSimon Glass 	enum uclass_id id;
180ae7f4513SSimon Glass 	const struct udevice_id *of_match;
18154c5d08aSHeiko Schocher 	int (*bind)(struct udevice *dev);
18254c5d08aSHeiko Schocher 	int (*probe)(struct udevice *dev);
18354c5d08aSHeiko Schocher 	int (*remove)(struct udevice *dev);
18454c5d08aSHeiko Schocher 	int (*unbind)(struct udevice *dev);
18554c5d08aSHeiko Schocher 	int (*ofdata_to_platdata)(struct udevice *dev);
1860118ce79SSimon Glass 	int (*child_post_bind)(struct udevice *dev);
187a327dee0SSimon Glass 	int (*child_pre_probe)(struct udevice *dev);
188a327dee0SSimon Glass 	int (*child_post_remove)(struct udevice *dev);
1896494d708SSimon Glass 	int priv_auto_alloc_size;
1906494d708SSimon Glass 	int platdata_auto_alloc_size;
191e59f458dSSimon Glass 	int per_child_auto_alloc_size;
192cdc133bdSSimon Glass 	int per_child_platdata_auto_alloc_size;
1936494d708SSimon Glass 	const void *ops;	/* driver-specific operations */
19400606d7eSSimon Glass 	uint32_t flags;
1956494d708SSimon Glass };
1966494d708SSimon Glass 
1976494d708SSimon Glass /* Declare a new U-Boot driver */
1986494d708SSimon Glass #define U_BOOT_DRIVER(__name)						\
1996494d708SSimon Glass 	ll_entry_declare(struct driver, __name, driver)
2006494d708SSimon Glass 
2016494d708SSimon Glass /**
2026494d708SSimon Glass  * dev_get_platdata() - Get the platform data for a device
2036494d708SSimon Glass  *
2046494d708SSimon Glass  * This checks that dev is not NULL, but no other checks for now
2056494d708SSimon Glass  *
2066494d708SSimon Glass  * @dev		Device to check
2076494d708SSimon Glass  * @return platform data, or NULL if none
2086494d708SSimon Glass  */
20954c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev);
2106494d708SSimon Glass 
2116494d708SSimon Glass /**
212cdc133bdSSimon Glass  * dev_get_parent_platdata() - Get the parent platform data for a device
213cdc133bdSSimon Glass  *
214cdc133bdSSimon Glass  * This checks that dev is not NULL, but no other checks for now
215cdc133bdSSimon Glass  *
216cdc133bdSSimon Glass  * @dev		Device to check
217cdc133bdSSimon Glass  * @return parent's platform data, or NULL if none
218cdc133bdSSimon Glass  */
219cdc133bdSSimon Glass void *dev_get_parent_platdata(struct udevice *dev);
220cdc133bdSSimon Glass 
221cdc133bdSSimon Glass /**
2225eaed880SPrzemyslaw Marczak  * dev_get_uclass_platdata() - Get the uclass platform data for a device
2235eaed880SPrzemyslaw Marczak  *
2245eaed880SPrzemyslaw Marczak  * This checks that dev is not NULL, but no other checks for now
2255eaed880SPrzemyslaw Marczak  *
2265eaed880SPrzemyslaw Marczak  * @dev		Device to check
2275eaed880SPrzemyslaw Marczak  * @return uclass's platform data, or NULL if none
2285eaed880SPrzemyslaw Marczak  */
2295eaed880SPrzemyslaw Marczak void *dev_get_uclass_platdata(struct udevice *dev);
2305eaed880SPrzemyslaw Marczak 
2315eaed880SPrzemyslaw Marczak /**
232e59f458dSSimon Glass  * dev_get_parentdata() - Get the parent data for a device
233e59f458dSSimon Glass  *
234e59f458dSSimon Glass  * The parent data is data stored in the device but owned by the parent.
235e59f458dSSimon Glass  * For example, a USB device may have parent data which contains information
236e59f458dSSimon Glass  * about how to talk to the device over USB.
237e59f458dSSimon Glass  *
238e59f458dSSimon Glass  * This checks that dev is not NULL, but no other checks for now
239e59f458dSSimon Glass  *
240e59f458dSSimon Glass  * @dev		Device to check
241e59f458dSSimon Glass  * @return parent data, or NULL if none
242e59f458dSSimon Glass  */
243e59f458dSSimon Glass void *dev_get_parentdata(struct udevice *dev);
244e59f458dSSimon Glass 
245e59f458dSSimon Glass /**
2466494d708SSimon Glass  * dev_get_priv() - Get the private data for a device
2476494d708SSimon Glass  *
2486494d708SSimon Glass  * This checks that dev is not NULL, but no other checks for now
2496494d708SSimon Glass  *
2506494d708SSimon Glass  * @dev		Device to check
2516494d708SSimon Glass  * @return private data, or NULL if none
2526494d708SSimon Glass  */
25354c5d08aSHeiko Schocher void *dev_get_priv(struct udevice *dev);
2546494d708SSimon Glass 
2555a66a8ffSSimon Glass /**
256479728cbSSimon Glass  * struct dev_get_parent() - Get the parent of a device
257479728cbSSimon Glass  *
258479728cbSSimon Glass  * @child:	Child to check
259479728cbSSimon Glass  * @return parent of child, or NULL if this is the root device
260479728cbSSimon Glass  */
261479728cbSSimon Glass struct udevice *dev_get_parent(struct udevice *child);
262479728cbSSimon Glass 
263479728cbSSimon Glass /**
264e564f054SSimon Glass  * dev_get_uclass_priv() - Get the private uclass data for a device
265e564f054SSimon Glass  *
266e564f054SSimon Glass  * This checks that dev is not NULL, but no other checks for now
267e564f054SSimon Glass  *
268e564f054SSimon Glass  * @dev		Device to check
269e564f054SSimon Glass  * @return private uclass data for this device, or NULL if none
270e564f054SSimon Glass  */
271e564f054SSimon Glass void *dev_get_uclass_priv(struct udevice *dev);
272e564f054SSimon Glass 
273e564f054SSimon Glass /**
27439de8433SSimon Glass  * dev_get_driver_data() - get the driver data used to bind a device
2752ef249b4SSimon Glass  *
2762ef249b4SSimon Glass  * When a device is bound using a device tree node, it matches a
2772ef249b4SSimon Glass  * particular compatible string as in struct udevice_id. This function
27839de8433SSimon Glass  * returns the associated data value for that compatible string. This is
27939de8433SSimon Glass  * the 'data' field in struct udevice_id.
28039de8433SSimon Glass  *
28139de8433SSimon Glass  * For USB devices, this is the driver_info field in struct usb_device_id.
28239de8433SSimon Glass  *
28339de8433SSimon Glass  * @dev:	Device to check
2842ef249b4SSimon Glass  */
28539de8433SSimon Glass ulong dev_get_driver_data(struct udevice *dev);
2862ef249b4SSimon Glass 
287cc73d37bSPrzemyslaw Marczak /**
288cc73d37bSPrzemyslaw Marczak  * dev_get_driver_ops() - get the device's driver's operations
289cc73d37bSPrzemyslaw Marczak  *
290cc73d37bSPrzemyslaw Marczak  * This checks that dev is not NULL, and returns the pointer to device's
291cc73d37bSPrzemyslaw Marczak  * driver's operations.
292cc73d37bSPrzemyslaw Marczak  *
293cc73d37bSPrzemyslaw Marczak  * @dev:	Device to check
294cc73d37bSPrzemyslaw Marczak  * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
295cc73d37bSPrzemyslaw Marczak  */
296cc73d37bSPrzemyslaw Marczak const void *dev_get_driver_ops(struct udevice *dev);
297cc73d37bSPrzemyslaw Marczak 
298b3670531SSimon Glass /*
299b3670531SSimon Glass  * device_get_uclass_id() - return the uclass ID of a device
300b3670531SSimon Glass  *
301b3670531SSimon Glass  * @dev:	Device to check
302b3670531SSimon Glass  * @return uclass ID for the device
303b3670531SSimon Glass  */
304b3670531SSimon Glass enum uclass_id device_get_uclass_id(struct udevice *dev);
305b3670531SSimon Glass 
306f9c370dcSPrzemyslaw Marczak /*
307f9c370dcSPrzemyslaw Marczak  * dev_get_uclass_name() - return the uclass name of a device
308f9c370dcSPrzemyslaw Marczak  *
309f9c370dcSPrzemyslaw Marczak  * This checks that dev is not NULL.
310f9c370dcSPrzemyslaw Marczak  *
311f9c370dcSPrzemyslaw Marczak  * @dev:	Device to check
312f9c370dcSPrzemyslaw Marczak  * @return  pointer to the uclass name for the device
313f9c370dcSPrzemyslaw Marczak  */
314f9c370dcSPrzemyslaw Marczak const char *dev_get_uclass_name(struct udevice *dev);
315f9c370dcSPrzemyslaw Marczak 
3162ef249b4SSimon Glass /**
317997c87bbSSimon Glass  * device_get_child() - Get the child of a device by index
318997c87bbSSimon Glass  *
319997c87bbSSimon Glass  * Returns the numbered child, 0 being the first. This does not use
320997c87bbSSimon Glass  * sequence numbers, only the natural order.
321997c87bbSSimon Glass  *
322997c87bbSSimon Glass  * @dev:	Parent device to check
323997c87bbSSimon Glass  * @index:	Child index
324997c87bbSSimon Glass  * @devp:	Returns pointer to device
3253f416f33SSimon Glass  * @return 0 if OK, -ENODEV if no such device, other error if the device fails
3263f416f33SSimon Glass  *	   to probe
327997c87bbSSimon Glass  */
328997c87bbSSimon Glass int device_get_child(struct udevice *parent, int index, struct udevice **devp);
329997c87bbSSimon Glass 
330997c87bbSSimon Glass /**
3315a66a8ffSSimon Glass  * device_find_child_by_seq() - Find a child device based on a sequence
3325a66a8ffSSimon Glass  *
3335a66a8ffSSimon Glass  * This searches for a device with the given seq or req_seq.
3345a66a8ffSSimon Glass  *
3355a66a8ffSSimon Glass  * For seq, if an active device has this sequence it will be returned.
3365a66a8ffSSimon Glass  * If there is no such device then this will return -ENODEV.
3375a66a8ffSSimon Glass  *
3385a66a8ffSSimon Glass  * For req_seq, if a device (whether activated or not) has this req_seq
3395a66a8ffSSimon Glass  * value, that device will be returned. This is a strong indication that
3405a66a8ffSSimon Glass  * the device will receive that sequence when activated.
3415a66a8ffSSimon Glass  *
3425a66a8ffSSimon Glass  * @parent: Parent device
3435a66a8ffSSimon Glass  * @seq_or_req_seq: Sequence number to find (0=first)
3445a66a8ffSSimon Glass  * @find_req_seq: true to find req_seq, false to find seq
3455a66a8ffSSimon Glass  * @devp: Returns pointer to device (there is only one per for each seq).
3465a66a8ffSSimon Glass  * Set to NULL if none is found
3475a66a8ffSSimon Glass  * @return 0 if OK, -ve on error
3485a66a8ffSSimon Glass  */
3495a66a8ffSSimon Glass int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
3505a66a8ffSSimon Glass 			     bool find_req_seq, struct udevice **devp);
3515a66a8ffSSimon Glass 
352997c87bbSSimon Glass /**
353997c87bbSSimon Glass  * device_get_child_by_seq() - Get a child device based on a sequence
354997c87bbSSimon Glass  *
355997c87bbSSimon Glass  * If an active device has this sequence it will be returned. If there is no
356997c87bbSSimon Glass  * such device then this will check for a device that is requesting this
357997c87bbSSimon Glass  * sequence.
358997c87bbSSimon Glass  *
359997c87bbSSimon Glass  * The device is probed to activate it ready for use.
360997c87bbSSimon Glass  *
361997c87bbSSimon Glass  * @parent: Parent device
362997c87bbSSimon Glass  * @seq: Sequence number to find (0=first)
363997c87bbSSimon Glass  * @devp: Returns pointer to device (there is only one per for each seq)
364997c87bbSSimon Glass  * Set to NULL if none is found
365997c87bbSSimon Glass  * @return 0 if OK, -ve on error
366997c87bbSSimon Glass  */
367997c87bbSSimon Glass int device_get_child_by_seq(struct udevice *parent, int seq,
368997c87bbSSimon Glass 			    struct udevice **devp);
369997c87bbSSimon Glass 
370997c87bbSSimon Glass /**
371997c87bbSSimon Glass  * device_find_child_by_of_offset() - Find a child device based on FDT offset
372997c87bbSSimon Glass  *
373997c87bbSSimon Glass  * Locates a child device by its device tree offset.
374997c87bbSSimon Glass  *
375997c87bbSSimon Glass  * @parent: Parent device
376997c87bbSSimon Glass  * @of_offset: Device tree offset to find
377997c87bbSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
378997c87bbSSimon Glass  * @return 0 if OK, -ve on error
379997c87bbSSimon Glass  */
380997c87bbSSimon Glass int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
381997c87bbSSimon Glass 				   struct udevice **devp);
382997c87bbSSimon Glass 
383997c87bbSSimon Glass /**
384997c87bbSSimon Glass  * device_get_child_by_of_offset() - Get a child device based on FDT offset
385997c87bbSSimon Glass  *
386997c87bbSSimon Glass  * Locates a child device by its device tree offset.
387997c87bbSSimon Glass  *
388997c87bbSSimon Glass  * The device is probed to activate it ready for use.
389997c87bbSSimon Glass  *
390997c87bbSSimon Glass  * @parent: Parent device
391997c87bbSSimon Glass  * @of_offset: Device tree offset to find
392997c87bbSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
393997c87bbSSimon Glass  * @return 0 if OK, -ve on error
394997c87bbSSimon Glass  */
395132f9bfcSSimon Glass int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
396997c87bbSSimon Glass 				  struct udevice **devp);
397997c87bbSSimon Glass 
398a8981d4fSSimon Glass /**
3992693047aSSimon Glass  * device_get_global_by_of_offset() - Get a device based on FDT offset
4002693047aSSimon Glass  *
4012693047aSSimon Glass  * Locates a device by its device tree offset, searching globally throughout
4022693047aSSimon Glass  * the all driver model devices.
4032693047aSSimon Glass  *
4042693047aSSimon Glass  * The device is probed to activate it ready for use.
4052693047aSSimon Glass  *
4062693047aSSimon Glass  * @of_offset: Device tree offset to find
4072693047aSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
4082693047aSSimon Glass  * @return 0 if OK, -ve on error
4092693047aSSimon Glass  */
4102693047aSSimon Glass int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
4112693047aSSimon Glass 
4122693047aSSimon Glass /**
413a8981d4fSSimon Glass  * device_find_first_child() - Find the first child of a device
414a8981d4fSSimon Glass  *
415a8981d4fSSimon Glass  * @parent: Parent device to search
416a8981d4fSSimon Glass  * @devp: Returns first child device, or NULL if none
417a8981d4fSSimon Glass  * @return 0
418a8981d4fSSimon Glass  */
419a8981d4fSSimon Glass int device_find_first_child(struct udevice *parent, struct udevice **devp);
420a8981d4fSSimon Glass 
421a8981d4fSSimon Glass /**
4223f416f33SSimon Glass  * device_find_next_child() - Find the next child of a device
423a8981d4fSSimon Glass  *
424a8981d4fSSimon Glass  * @devp: Pointer to previous child device on entry. Returns pointer to next
425a8981d4fSSimon Glass  *		child device, or NULL if none
426a8981d4fSSimon Glass  * @return 0
427a8981d4fSSimon Glass  */
428a8981d4fSSimon Glass int device_find_next_child(struct udevice **devp);
429a8981d4fSSimon Glass 
430c9cac3f8SPeng Fan /**
431c9cac3f8SPeng Fan  * dev_get_addr() - Get the reg property of a device
432c9cac3f8SPeng Fan  *
433c9cac3f8SPeng Fan  * @dev: Pointer to a device
434c9cac3f8SPeng Fan  *
435c9cac3f8SPeng Fan  * @return addr
436c9cac3f8SPeng Fan  */
437c9cac3f8SPeng Fan fdt_addr_t dev_get_addr(struct udevice *dev);
438c9cac3f8SPeng Fan 
439c5785673SSimon Glass /**
440c5785673SSimon Glass  * device_has_children() - check if a device has any children
441c5785673SSimon Glass  *
442c5785673SSimon Glass  * @dev:	Device to check
443c5785673SSimon Glass  * @return true if the device has one or more children
444c5785673SSimon Glass  */
445c5785673SSimon Glass bool device_has_children(struct udevice *dev);
446c5785673SSimon Glass 
447c5785673SSimon Glass /**
448c5785673SSimon Glass  * device_has_active_children() - check if a device has any active children
449c5785673SSimon Glass  *
450c5785673SSimon Glass  * @dev:	Device to check
451c5785673SSimon Glass  * @return true if the device has one or more children and at least one of
452c5785673SSimon Glass  * them is active (probed).
453c5785673SSimon Glass  */
454c5785673SSimon Glass bool device_has_active_children(struct udevice *dev);
455c5785673SSimon Glass 
456c5785673SSimon Glass /**
457c5785673SSimon Glass  * device_is_last_sibling() - check if a device is the last sibling
458c5785673SSimon Glass  *
459c5785673SSimon Glass  * This function can be useful for display purposes, when special action needs
460c5785673SSimon Glass  * to be taken when displaying the last sibling. This can happen when a tree
461c5785673SSimon Glass  * view of devices is being displayed.
462c5785673SSimon Glass  *
463c5785673SSimon Glass  * @dev:	Device to check
464c5785673SSimon Glass  * @return true if there are no more siblings after this one - i.e. is it
465c5785673SSimon Glass  * last in the list.
466c5785673SSimon Glass  */
467c5785673SSimon Glass bool device_is_last_sibling(struct udevice *dev);
468c5785673SSimon Glass 
469*608f26c5SMasahiro Yamada /* device resource management */
470*608f26c5SMasahiro Yamada typedef void (*dr_release_t)(struct udevice *dev, void *res);
471*608f26c5SMasahiro Yamada typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
472*608f26c5SMasahiro Yamada 
473*608f26c5SMasahiro Yamada #ifdef CONFIG_DEBUG_DEVRES
474*608f26c5SMasahiro Yamada void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
475*608f26c5SMasahiro Yamada 		     const char *name);
476*608f26c5SMasahiro Yamada #define _devres_alloc(release, size, gfp) \
477*608f26c5SMasahiro Yamada 	__devres_alloc(release, size, gfp, #release)
478*608f26c5SMasahiro Yamada #else
479*608f26c5SMasahiro Yamada void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
480*608f26c5SMasahiro Yamada #endif
481*608f26c5SMasahiro Yamada 
482*608f26c5SMasahiro Yamada /**
483*608f26c5SMasahiro Yamada  * devres_alloc - Allocate device resource data
484*608f26c5SMasahiro Yamada  * @release: Release function devres will be associated with
485*608f26c5SMasahiro Yamada  * @size: Allocation size
486*608f26c5SMasahiro Yamada  * @gfp: Allocation flags
487*608f26c5SMasahiro Yamada  *
488*608f26c5SMasahiro Yamada  * Allocate devres of @size bytes.  The allocated area is associated
489*608f26c5SMasahiro Yamada  * with @release.  The returned pointer can be passed to
490*608f26c5SMasahiro Yamada  * other devres_*() functions.
491*608f26c5SMasahiro Yamada  *
492*608f26c5SMasahiro Yamada  * RETURNS:
493*608f26c5SMasahiro Yamada  * Pointer to allocated devres on success, NULL on failure.
494*608f26c5SMasahiro Yamada  */
495*608f26c5SMasahiro Yamada #define devres_alloc(release, size, gfp) \
496*608f26c5SMasahiro Yamada 	_devres_alloc(release, size, gfp | __GFP_ZERO)
497*608f26c5SMasahiro Yamada 
498*608f26c5SMasahiro Yamada /**
499*608f26c5SMasahiro Yamada  * devres_free - Free device resource data
500*608f26c5SMasahiro Yamada  * @res: Pointer to devres data to free
501*608f26c5SMasahiro Yamada  *
502*608f26c5SMasahiro Yamada  * Free devres created with devres_alloc().
503*608f26c5SMasahiro Yamada  */
504*608f26c5SMasahiro Yamada void devres_free(void *res);
505*608f26c5SMasahiro Yamada 
506*608f26c5SMasahiro Yamada /**
507*608f26c5SMasahiro Yamada  * devres_add - Register device resource
508*608f26c5SMasahiro Yamada  * @dev: Device to add resource to
509*608f26c5SMasahiro Yamada  * @res: Resource to register
510*608f26c5SMasahiro Yamada  *
511*608f26c5SMasahiro Yamada  * Register devres @res to @dev.  @res should have been allocated
512*608f26c5SMasahiro Yamada  * using devres_alloc().  On driver detach, the associated release
513*608f26c5SMasahiro Yamada  * function will be invoked and devres will be freed automatically.
514*608f26c5SMasahiro Yamada  */
515*608f26c5SMasahiro Yamada void devres_add(struct udevice *dev, void *res);
516*608f26c5SMasahiro Yamada 
517*608f26c5SMasahiro Yamada /**
518*608f26c5SMasahiro Yamada  * devres_find - Find device resource
519*608f26c5SMasahiro Yamada  * @dev: Device to lookup resource from
520*608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
521*608f26c5SMasahiro Yamada  * @match: Match function (optional)
522*608f26c5SMasahiro Yamada  * @match_data: Data for the match function
523*608f26c5SMasahiro Yamada  *
524*608f26c5SMasahiro Yamada  * Find the latest devres of @dev which is associated with @release
525*608f26c5SMasahiro Yamada  * and for which @match returns 1.  If @match is NULL, it's considered
526*608f26c5SMasahiro Yamada  * to match all.
527*608f26c5SMasahiro Yamada  *
528*608f26c5SMasahiro Yamada  * RETURNS:
529*608f26c5SMasahiro Yamada  * Pointer to found devres, NULL if not found.
530*608f26c5SMasahiro Yamada  */
531*608f26c5SMasahiro Yamada void *devres_find(struct udevice *dev, dr_release_t release,
532*608f26c5SMasahiro Yamada 		  dr_match_t match, void *match_data);
533*608f26c5SMasahiro Yamada 
534*608f26c5SMasahiro Yamada /**
535*608f26c5SMasahiro Yamada  * devres_get - Find devres, if non-existent, add one atomically
536*608f26c5SMasahiro Yamada  * @dev: Device to lookup or add devres for
537*608f26c5SMasahiro Yamada  * @new_res: Pointer to new initialized devres to add if not found
538*608f26c5SMasahiro Yamada  * @match: Match function (optional)
539*608f26c5SMasahiro Yamada  * @match_data: Data for the match function
540*608f26c5SMasahiro Yamada  *
541*608f26c5SMasahiro Yamada  * Find the latest devres of @dev which has the same release function
542*608f26c5SMasahiro Yamada  * as @new_res and for which @match return 1.  If found, @new_res is
543*608f26c5SMasahiro Yamada  * freed; otherwise, @new_res is added atomically.
544*608f26c5SMasahiro Yamada  *
545*608f26c5SMasahiro Yamada  * RETURNS:
546*608f26c5SMasahiro Yamada  * Pointer to found or added devres.
547*608f26c5SMasahiro Yamada  */
548*608f26c5SMasahiro Yamada void *devres_get(struct udevice *dev, void *new_res,
549*608f26c5SMasahiro Yamada 		 dr_match_t match, void *match_data);
550*608f26c5SMasahiro Yamada 
551*608f26c5SMasahiro Yamada /**
552*608f26c5SMasahiro Yamada  * devres_remove - Find a device resource and remove it
553*608f26c5SMasahiro Yamada  * @dev: Device to find resource from
554*608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
555*608f26c5SMasahiro Yamada  * @match: Match function (optional)
556*608f26c5SMasahiro Yamada  * @match_data: Data for the match function
557*608f26c5SMasahiro Yamada  *
558*608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
559*608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
560*608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically and
561*608f26c5SMasahiro Yamada  * returned.
562*608f26c5SMasahiro Yamada  *
563*608f26c5SMasahiro Yamada  * RETURNS:
564*608f26c5SMasahiro Yamada  * Pointer to removed devres on success, NULL if not found.
565*608f26c5SMasahiro Yamada  */
566*608f26c5SMasahiro Yamada void *devres_remove(struct udevice *dev, dr_release_t release,
567*608f26c5SMasahiro Yamada 		    dr_match_t match, void *match_data);
568*608f26c5SMasahiro Yamada 
569*608f26c5SMasahiro Yamada /**
570*608f26c5SMasahiro Yamada  * devres_destroy - Find a device resource and destroy it
571*608f26c5SMasahiro Yamada  * @dev: Device to find resource from
572*608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
573*608f26c5SMasahiro Yamada  * @match: Match function (optional)
574*608f26c5SMasahiro Yamada  * @match_data: Data for the match function
575*608f26c5SMasahiro Yamada  *
576*608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
577*608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
578*608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically and freed.
579*608f26c5SMasahiro Yamada  *
580*608f26c5SMasahiro Yamada  * Note that the release function for the resource will not be called,
581*608f26c5SMasahiro Yamada  * only the devres-allocated data will be freed.  The caller becomes
582*608f26c5SMasahiro Yamada  * responsible for freeing any other data.
583*608f26c5SMasahiro Yamada  *
584*608f26c5SMasahiro Yamada  * RETURNS:
585*608f26c5SMasahiro Yamada  * 0 if devres is found and freed, -ENOENT if not found.
586*608f26c5SMasahiro Yamada  */
587*608f26c5SMasahiro Yamada int devres_destroy(struct udevice *dev, dr_release_t release,
588*608f26c5SMasahiro Yamada 		   dr_match_t match, void *match_data);
589*608f26c5SMasahiro Yamada 
590*608f26c5SMasahiro Yamada /**
591*608f26c5SMasahiro Yamada  * devres_release - Find a device resource and destroy it, calling release
592*608f26c5SMasahiro Yamada  * @dev: Device to find resource from
593*608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
594*608f26c5SMasahiro Yamada  * @match: Match function (optional)
595*608f26c5SMasahiro Yamada  * @match_data: Data for the match function
596*608f26c5SMasahiro Yamada  *
597*608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
598*608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
599*608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically, the
600*608f26c5SMasahiro Yamada  * release function called and the resource freed.
601*608f26c5SMasahiro Yamada  *
602*608f26c5SMasahiro Yamada  * RETURNS:
603*608f26c5SMasahiro Yamada  * 0 if devres is found and freed, -ENOENT if not found.
604*608f26c5SMasahiro Yamada  */
605*608f26c5SMasahiro Yamada int devres_release(struct udevice *dev, dr_release_t release,
606*608f26c5SMasahiro Yamada 		   dr_match_t match, void *match_data);
607*608f26c5SMasahiro Yamada 
6086494d708SSimon Glass #endif
609