xref: /rk3399_rockchip-uboot/include/dm/device.h (revision 426f99fa98c3725fe0ca1eb8438d1215a2c6bd6b)
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 
44a2040facSSimon Glass /* Device name is allocated and should be freed on unbind() */
45fd1c2d9bSSimon Glass #define DM_FLAG_NAME_ALLOCED		(1 << 7)
46a2040facSSimon Glass 
479fa28190SSimon Glass #define DM_FLAG_OF_PLATDATA		(1 << 8)
489fa28190SSimon Glass 
49706865afSStefan Roese /*
50706865afSStefan Roese  * Call driver remove function to stop currently active DMA transfers or
51706865afSStefan Roese  * give DMA buffers back to the HW / controller. This may be needed for
52706865afSStefan Roese  * some drivers to do some final stage cleanup before the OS is called
53706865afSStefan Roese  * (U-Boot exit)
54706865afSStefan Roese  */
55706865afSStefan Roese #define DM_FLAG_ACTIVE_DMA		(1 << 9)
56706865afSStefan Roese 
57706865afSStefan Roese /*
58*426f99faSStefan Roese  * Call driver remove function to do some final configuration, before
59*426f99faSStefan Roese  * U-Boot exits and the OS is started
60*426f99faSStefan Roese  */
61*426f99faSStefan Roese #define DM_FLAG_OS_PREPARE		(1 << 10)
62*426f99faSStefan Roese 
63*426f99faSStefan Roese /*
64706865afSStefan Roese  * One or multiple of these flags are passed to device_remove() so that
65706865afSStefan Roese  * a selective device removal as specified by the remove-stage and the
66706865afSStefan Roese  * driver flags can be done.
67706865afSStefan Roese  */
68706865afSStefan Roese enum {
69706865afSStefan Roese 	/* Normal remove, remove all devices */
70706865afSStefan Roese 	DM_REMOVE_NORMAL     = 1 << 0,
71706865afSStefan Roese 
72706865afSStefan Roese 	/* Remove devices with active DMA */
73706865afSStefan Roese 	DM_REMOVE_ACTIVE_DMA = DM_FLAG_ACTIVE_DMA,
74706865afSStefan Roese 
75*426f99faSStefan Roese 	/* Remove devices which need some final OS preparation steps */
76*426f99faSStefan Roese 	DM_REMOVE_OS_PREPARE = DM_FLAG_OS_PREPARE,
77*426f99faSStefan Roese 
78706865afSStefan Roese 	/* Add more use cases here */
79706865afSStefan Roese 
80706865afSStefan Roese 	/* Remove devices with any active flag */
81*426f99faSStefan Roese 	DM_REMOVE_ACTIVE_ALL = DM_REMOVE_ACTIVE_DMA | DM_REMOVE_OS_PREPARE,
82706865afSStefan Roese };
83706865afSStefan Roese 
846494d708SSimon Glass /**
8554c5d08aSHeiko Schocher  * struct udevice - An instance of a driver
866494d708SSimon Glass  *
876494d708SSimon Glass  * This holds information about a device, which is a driver bound to a
886494d708SSimon Glass  * particular port or peripheral (essentially a driver instance).
896494d708SSimon Glass  *
906494d708SSimon Glass  * A device will come into existence through a 'bind' call, either due to
916494d708SSimon Glass  * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
926494d708SSimon Glass  * in the device tree (in which case of_offset is >= 0). In the latter case
936494d708SSimon Glass  * we translate the device tree information into platdata in a function
946494d708SSimon Glass  * implemented by the driver ofdata_to_platdata method (called just before the
956494d708SSimon Glass  * probe method if the device has a device tree node.
966494d708SSimon Glass  *
976494d708SSimon Glass  * All three of platdata, priv and uclass_priv can be allocated by the
986494d708SSimon Glass  * driver, or you can use the auto_alloc_size members of struct driver and
996494d708SSimon Glass  * struct uclass_driver to have driver model do this automatically.
1006494d708SSimon Glass  *
1016494d708SSimon Glass  * @driver: The driver used by this device
1026494d708SSimon Glass  * @name: Name of device, typically the FDT node name
1036494d708SSimon Glass  * @platdata: Configuration data for this device
104cdc133bdSSimon Glass  * @parent_platdata: The parent bus's configuration data for this device
1055eaed880SPrzemyslaw Marczak  * @uclass_platdata: The uclass's configuration data for this device
1066494d708SSimon Glass  * @of_offset: Device tree node offset for this device (- for none)
10739de8433SSimon Glass  * @driver_data: Driver data word for the entry that matched this device with
10839de8433SSimon Glass  *		its driver
1096494d708SSimon Glass  * @parent: Parent of this device, or NULL for the top level device
1106494d708SSimon Glass  * @priv: Private data for this device
1116494d708SSimon Glass  * @uclass: Pointer to uclass for this device
1126494d708SSimon Glass  * @uclass_priv: The uclass's private data for this device
113e59f458dSSimon Glass  * @parent_priv: The parent's private data for this device
1146494d708SSimon Glass  * @uclass_node: Used by uclass to link its devices
1156494d708SSimon Glass  * @child_head: List of children of this device
1166494d708SSimon Glass  * @sibling_node: Next device in list of all devices
1176494d708SSimon Glass  * @flags: Flags for this device DM_FLAG_...
1185a66a8ffSSimon Glass  * @req_seq: Requested sequence number for this device (-1 = any)
119547cea19SSimon Glass  * @seq: Allocated sequence number for this device (-1 = none). This is set up
120547cea19SSimon Glass  * when the device is probed and will be unique within the device's uclass.
12193c7fe4aSSimon Glass  * @devres_head: List of memory allocations associated with this device.
12293c7fe4aSSimon Glass  *		When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
12393c7fe4aSSimon Glass  *		add to this list. Memory so-allocated will be freed
12493c7fe4aSSimon Glass  *		automatically when the device is removed / unbound
1256494d708SSimon Glass  */
12654c5d08aSHeiko Schocher struct udevice {
1273479253dSSimon Glass 	const struct driver *driver;
1286494d708SSimon Glass 	const char *name;
1296494d708SSimon Glass 	void *platdata;
130cdc133bdSSimon Glass 	void *parent_platdata;
1315eaed880SPrzemyslaw Marczak 	void *uclass_platdata;
1326494d708SSimon Glass 	int of_offset;
13339de8433SSimon Glass 	ulong driver_data;
13454c5d08aSHeiko Schocher 	struct udevice *parent;
1356494d708SSimon Glass 	void *priv;
1366494d708SSimon Glass 	struct uclass *uclass;
1376494d708SSimon Glass 	void *uclass_priv;
138e59f458dSSimon Glass 	void *parent_priv;
1396494d708SSimon Glass 	struct list_head uclass_node;
1406494d708SSimon Glass 	struct list_head child_head;
1416494d708SSimon Glass 	struct list_head sibling_node;
1426494d708SSimon Glass 	uint32_t flags;
1435a66a8ffSSimon Glass 	int req_seq;
1445a66a8ffSSimon Glass 	int seq;
145e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES
146608f26c5SMasahiro Yamada 	struct list_head devres_head;
147e2282d70SMasahiro Yamada #endif
1486494d708SSimon Glass };
1496494d708SSimon Glass 
1505a66a8ffSSimon Glass /* Maximum sequence number supported */
1515a66a8ffSSimon Glass #define DM_MAX_SEQ	999
1525a66a8ffSSimon Glass 
1536494d708SSimon Glass /* Returns the operations for a device */
1546494d708SSimon Glass #define device_get_ops(dev)	(dev->driver->ops)
1556494d708SSimon Glass 
1566494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */
1576494d708SSimon Glass #define device_active(dev)	((dev)->flags & DM_FLAG_ACTIVATED)
1586494d708SSimon Glass 
159e160f7d4SSimon Glass static inline int dev_of_offset(const struct udevice *dev)
160e160f7d4SSimon Glass {
161e160f7d4SSimon Glass 	return dev->of_offset;
162e160f7d4SSimon Glass }
163e160f7d4SSimon Glass 
164e160f7d4SSimon Glass static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
165e160f7d4SSimon Glass {
166e160f7d4SSimon Glass 	dev->of_offset = of_offset;
167e160f7d4SSimon Glass }
168e160f7d4SSimon Glass 
1696494d708SSimon Glass /**
170ae7f4513SSimon Glass  * struct udevice_id - Lists the compatible strings supported by a driver
1716494d708SSimon Glass  * @compatible: Compatible string
1726494d708SSimon Glass  * @data: Data for this compatible string
1736494d708SSimon Glass  */
174ae7f4513SSimon Glass struct udevice_id {
1756494d708SSimon Glass 	const char *compatible;
1766494d708SSimon Glass 	ulong data;
1776494d708SSimon Glass };
1786494d708SSimon Glass 
1790f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
180f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr)	(_ptr)
181f887cb6dSMasahiro Yamada #else
182f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr)	NULL
1830f925822SMasahiro Yamada #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
184f887cb6dSMasahiro Yamada 
1856494d708SSimon Glass /**
1866494d708SSimon Glass  * struct driver - A driver for a feature or peripheral
1876494d708SSimon Glass  *
1886494d708SSimon Glass  * This holds methods for setting up a new device, and also removing it.
1896494d708SSimon Glass  * The device needs information to set itself up - this is provided either
1906494d708SSimon Glass  * by platdata or a device tree node (which we find by looking up
1916494d708SSimon Glass  * matching compatible strings with of_match).
1926494d708SSimon Glass  *
1936494d708SSimon Glass  * Drivers all belong to a uclass, representing a class of devices of the
1946494d708SSimon Glass  * same type. Common elements of the drivers can be implemented in the uclass,
1956494d708SSimon Glass  * or the uclass can provide a consistent interface to the drivers within
1966494d708SSimon Glass  * it.
1976494d708SSimon Glass  *
1986494d708SSimon Glass  * @name: Device name
1996494d708SSimon Glass  * @id: Identiies the uclass we belong to
2006494d708SSimon Glass  * @of_match: List of compatible strings to match, and any identifying data
2016494d708SSimon Glass  * for each.
2026494d708SSimon Glass  * @bind: Called to bind a device to its driver
2036494d708SSimon Glass  * @probe: Called to probe a device, i.e. activate it
2046494d708SSimon Glass  * @remove: Called to remove a device, i.e. de-activate it
2056494d708SSimon Glass  * @unbind: Called to unbind a device from its driver
2066494d708SSimon Glass  * @ofdata_to_platdata: Called before probe to decode device tree data
2070118ce79SSimon Glass  * @child_post_bind: Called after a new child has been bound
208a327dee0SSimon Glass  * @child_pre_probe: Called before a child device is probed. The device has
209a327dee0SSimon Glass  * memory allocated but it has not yet been probed.
210a327dee0SSimon Glass  * @child_post_remove: Called after a child device is removed. The device
211a327dee0SSimon Glass  * has memory allocated but its device_remove() method has been called.
2126494d708SSimon Glass  * @priv_auto_alloc_size: If non-zero this is the size of the private data
2136494d708SSimon Glass  * to be allocated in the device's ->priv pointer. If zero, then the driver
2146494d708SSimon Glass  * is responsible for allocating any data required.
2156494d708SSimon Glass  * @platdata_auto_alloc_size: If non-zero this is the size of the
2166494d708SSimon Glass  * platform data to be allocated in the device's ->platdata pointer.
2176494d708SSimon Glass  * This is typically only useful for device-tree-aware drivers (those with
2186494d708SSimon Glass  * an of_match), since drivers which use platdata will have the data
2196494d708SSimon Glass  * provided in the U_BOOT_DEVICE() instantiation.
220e59f458dSSimon Glass  * @per_child_auto_alloc_size: Each device can hold private data owned by
221e59f458dSSimon Glass  * its parent. If required this will be automatically allocated if this
222e59f458dSSimon Glass  * value is non-zero.
223cdc133bdSSimon Glass  * @per_child_platdata_auto_alloc_size: A bus likes to store information about
224cdc133bdSSimon Glass  * its children. If non-zero this is the size of this data, to be allocated
225cdc133bdSSimon Glass  * in the child's parent_platdata pointer.
2260040b944SSimon Glass  * @ops: Driver-specific operations. This is typically a list of function
2276494d708SSimon Glass  * pointers defined by the driver, to implement driver functions required by
2286494d708SSimon Glass  * the uclass.
22900606d7eSSimon Glass  * @flags: driver flags - see DM_FLAGS_...
2306494d708SSimon Glass  */
2316494d708SSimon Glass struct driver {
2326494d708SSimon Glass 	char *name;
2336494d708SSimon Glass 	enum uclass_id id;
234ae7f4513SSimon Glass 	const struct udevice_id *of_match;
23554c5d08aSHeiko Schocher 	int (*bind)(struct udevice *dev);
23654c5d08aSHeiko Schocher 	int (*probe)(struct udevice *dev);
23754c5d08aSHeiko Schocher 	int (*remove)(struct udevice *dev);
23854c5d08aSHeiko Schocher 	int (*unbind)(struct udevice *dev);
23954c5d08aSHeiko Schocher 	int (*ofdata_to_platdata)(struct udevice *dev);
2400118ce79SSimon Glass 	int (*child_post_bind)(struct udevice *dev);
241a327dee0SSimon Glass 	int (*child_pre_probe)(struct udevice *dev);
242a327dee0SSimon Glass 	int (*child_post_remove)(struct udevice *dev);
2436494d708SSimon Glass 	int priv_auto_alloc_size;
2446494d708SSimon Glass 	int platdata_auto_alloc_size;
245e59f458dSSimon Glass 	int per_child_auto_alloc_size;
246cdc133bdSSimon Glass 	int per_child_platdata_auto_alloc_size;
2476494d708SSimon Glass 	const void *ops;	/* driver-specific operations */
24800606d7eSSimon Glass 	uint32_t flags;
2496494d708SSimon Glass };
2506494d708SSimon Glass 
2516494d708SSimon Glass /* Declare a new U-Boot driver */
2526494d708SSimon Glass #define U_BOOT_DRIVER(__name)						\
2536494d708SSimon Glass 	ll_entry_declare(struct driver, __name, driver)
2546494d708SSimon Glass 
255c57f806bSSimon Glass /* Get a pointer to a given driver */
256c57f806bSSimon Glass #define DM_GET_DRIVER(__name)						\
257c57f806bSSimon Glass 	ll_entry_get(struct driver, __name, driver)
258c57f806bSSimon Glass 
2596494d708SSimon Glass /**
2606494d708SSimon Glass  * dev_get_platdata() - Get the platform data for a device
2616494d708SSimon Glass  *
2626494d708SSimon Glass  * This checks that dev is not NULL, but no other checks for now
2636494d708SSimon Glass  *
2646494d708SSimon Glass  * @dev		Device to check
2656494d708SSimon Glass  * @return platform data, or NULL if none
2666494d708SSimon Glass  */
26754c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev);
2686494d708SSimon Glass 
2696494d708SSimon Glass /**
270cdc133bdSSimon Glass  * dev_get_parent_platdata() - Get the parent platform data for a device
271cdc133bdSSimon Glass  *
272cdc133bdSSimon Glass  * This checks that dev is not NULL, but no other checks for now
273cdc133bdSSimon Glass  *
274cdc133bdSSimon Glass  * @dev		Device to check
275cdc133bdSSimon Glass  * @return parent's platform data, or NULL if none
276cdc133bdSSimon Glass  */
277cdc133bdSSimon Glass void *dev_get_parent_platdata(struct udevice *dev);
278cdc133bdSSimon Glass 
279cdc133bdSSimon Glass /**
2805eaed880SPrzemyslaw Marczak  * dev_get_uclass_platdata() - Get the uclass platform data for a device
2815eaed880SPrzemyslaw Marczak  *
2825eaed880SPrzemyslaw Marczak  * This checks that dev is not NULL, but no other checks for now
2835eaed880SPrzemyslaw Marczak  *
2845eaed880SPrzemyslaw Marczak  * @dev		Device to check
2855eaed880SPrzemyslaw Marczak  * @return uclass's platform data, or NULL if none
2865eaed880SPrzemyslaw Marczak  */
2875eaed880SPrzemyslaw Marczak void *dev_get_uclass_platdata(struct udevice *dev);
2885eaed880SPrzemyslaw Marczak 
2895eaed880SPrzemyslaw Marczak /**
2909a79f6e6SSimon Glass  * dev_get_priv() - Get the private data for a device
2919a79f6e6SSimon Glass  *
2929a79f6e6SSimon Glass  * This checks that dev is not NULL, but no other checks for now
2939a79f6e6SSimon Glass  *
2949a79f6e6SSimon Glass  * @dev		Device to check
2959a79f6e6SSimon Glass  * @return private data, or NULL if none
2969a79f6e6SSimon Glass  */
2979a79f6e6SSimon Glass void *dev_get_priv(struct udevice *dev);
2989a79f6e6SSimon Glass 
2999a79f6e6SSimon Glass /**
300bcbe3d15SSimon Glass  * dev_get_parent_priv() - Get the parent private data for a device
301e59f458dSSimon Glass  *
302bcbe3d15SSimon Glass  * The parent private data is data stored in the device but owned by the
303bcbe3d15SSimon Glass  * parent. For example, a USB device may have parent data which contains
304bcbe3d15SSimon Glass  * information about how to talk to the device over USB.
305e59f458dSSimon Glass  *
306e59f458dSSimon Glass  * This checks that dev is not NULL, but no other checks for now
307e59f458dSSimon Glass  *
308e59f458dSSimon Glass  * @dev		Device to check
309e59f458dSSimon Glass  * @return parent data, or NULL if none
310e59f458dSSimon Glass  */
311bcbe3d15SSimon Glass void *dev_get_parent_priv(struct udevice *dev);
312e59f458dSSimon Glass 
313e59f458dSSimon Glass /**
314e564f054SSimon Glass  * dev_get_uclass_priv() - Get the private uclass data for a device
315e564f054SSimon Glass  *
316e564f054SSimon Glass  * This checks that dev is not NULL, but no other checks for now
317e564f054SSimon Glass  *
318e564f054SSimon Glass  * @dev		Device to check
319e564f054SSimon Glass  * @return private uclass data for this device, or NULL if none
320e564f054SSimon Glass  */
321e564f054SSimon Glass void *dev_get_uclass_priv(struct udevice *dev);
322e564f054SSimon Glass 
323e564f054SSimon Glass /**
3249a79f6e6SSimon Glass  * struct dev_get_parent() - Get the parent of a device
3259a79f6e6SSimon Glass  *
3269a79f6e6SSimon Glass  * @child:	Child to check
3279a79f6e6SSimon Glass  * @return parent of child, or NULL if this is the root device
3289a79f6e6SSimon Glass  */
3299a79f6e6SSimon Glass struct udevice *dev_get_parent(struct udevice *child);
3309a79f6e6SSimon Glass 
3319a79f6e6SSimon Glass /**
33239de8433SSimon Glass  * dev_get_driver_data() - get the driver data used to bind a device
3332ef249b4SSimon Glass  *
3342ef249b4SSimon Glass  * When a device is bound using a device tree node, it matches a
3358d1f3a9dSSimon Glass  * particular compatible string in struct udevice_id. This function
33639de8433SSimon Glass  * returns the associated data value for that compatible string. This is
33739de8433SSimon Glass  * the 'data' field in struct udevice_id.
33839de8433SSimon Glass  *
3398d1f3a9dSSimon Glass  * As an example, consider this structure:
3408d1f3a9dSSimon Glass  * static const struct udevice_id tegra_i2c_ids[] = {
3418d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
3428d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
3438d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
3448d1f3a9dSSimon Glass  *	{ }
3458d1f3a9dSSimon Glass  * };
3468d1f3a9dSSimon Glass  *
3478d1f3a9dSSimon Glass  * When driver model finds a driver for this it will store the 'data' value
3488d1f3a9dSSimon Glass  * corresponding to the compatible string it matches. This function returns
3498d1f3a9dSSimon Glass  * that value. This allows the driver to handle several variants of a device.
3508d1f3a9dSSimon Glass  *
35139de8433SSimon Glass  * For USB devices, this is the driver_info field in struct usb_device_id.
35239de8433SSimon Glass  *
35339de8433SSimon Glass  * @dev:	Device to check
3548d1f3a9dSSimon Glass  * @return driver data (0 if none is provided)
3552ef249b4SSimon Glass  */
35639de8433SSimon Glass ulong dev_get_driver_data(struct udevice *dev);
3572ef249b4SSimon Glass 
358cc73d37bSPrzemyslaw Marczak /**
359cc73d37bSPrzemyslaw Marczak  * dev_get_driver_ops() - get the device's driver's operations
360cc73d37bSPrzemyslaw Marczak  *
361cc73d37bSPrzemyslaw Marczak  * This checks that dev is not NULL, and returns the pointer to device's
362cc73d37bSPrzemyslaw Marczak  * driver's operations.
363cc73d37bSPrzemyslaw Marczak  *
364cc73d37bSPrzemyslaw Marczak  * @dev:	Device to check
365cc73d37bSPrzemyslaw Marczak  * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
366cc73d37bSPrzemyslaw Marczak  */
367cc73d37bSPrzemyslaw Marczak const void *dev_get_driver_ops(struct udevice *dev);
368cc73d37bSPrzemyslaw Marczak 
3698d1f3a9dSSimon Glass /**
370b3670531SSimon Glass  * device_get_uclass_id() - return the uclass ID of a device
371b3670531SSimon Glass  *
372b3670531SSimon Glass  * @dev:	Device to check
373b3670531SSimon Glass  * @return uclass ID for the device
374b3670531SSimon Glass  */
375b3670531SSimon Glass enum uclass_id device_get_uclass_id(struct udevice *dev);
376b3670531SSimon Glass 
3778d1f3a9dSSimon Glass /**
378f9c370dcSPrzemyslaw Marczak  * dev_get_uclass_name() - return the uclass name of a device
379f9c370dcSPrzemyslaw Marczak  *
380f9c370dcSPrzemyslaw Marczak  * This checks that dev is not NULL.
381f9c370dcSPrzemyslaw Marczak  *
382f9c370dcSPrzemyslaw Marczak  * @dev:	Device to check
383f9c370dcSPrzemyslaw Marczak  * @return  pointer to the uclass name for the device
384f9c370dcSPrzemyslaw Marczak  */
385f9c370dcSPrzemyslaw Marczak const char *dev_get_uclass_name(struct udevice *dev);
386f9c370dcSPrzemyslaw Marczak 
3872ef249b4SSimon Glass /**
388997c87bbSSimon Glass  * device_get_child() - Get the child of a device by index
389997c87bbSSimon Glass  *
390997c87bbSSimon Glass  * Returns the numbered child, 0 being the first. This does not use
391997c87bbSSimon Glass  * sequence numbers, only the natural order.
392997c87bbSSimon Glass  *
393997c87bbSSimon Glass  * @dev:	Parent device to check
394997c87bbSSimon Glass  * @index:	Child index
395997c87bbSSimon Glass  * @devp:	Returns pointer to device
3963f416f33SSimon Glass  * @return 0 if OK, -ENODEV if no such device, other error if the device fails
3973f416f33SSimon Glass  *	   to probe
398997c87bbSSimon Glass  */
399997c87bbSSimon Glass int device_get_child(struct udevice *parent, int index, struct udevice **devp);
400997c87bbSSimon Glass 
401997c87bbSSimon Glass /**
4025a66a8ffSSimon Glass  * device_find_child_by_seq() - Find a child device based on a sequence
4035a66a8ffSSimon Glass  *
4045a66a8ffSSimon Glass  * This searches for a device with the given seq or req_seq.
4055a66a8ffSSimon Glass  *
4065a66a8ffSSimon Glass  * For seq, if an active device has this sequence it will be returned.
4075a66a8ffSSimon Glass  * If there is no such device then this will return -ENODEV.
4085a66a8ffSSimon Glass  *
4095a66a8ffSSimon Glass  * For req_seq, if a device (whether activated or not) has this req_seq
4105a66a8ffSSimon Glass  * value, that device will be returned. This is a strong indication that
4115a66a8ffSSimon Glass  * the device will receive that sequence when activated.
4125a66a8ffSSimon Glass  *
4135a66a8ffSSimon Glass  * @parent: Parent device
4145a66a8ffSSimon Glass  * @seq_or_req_seq: Sequence number to find (0=first)
4155a66a8ffSSimon Glass  * @find_req_seq: true to find req_seq, false to find seq
4165a66a8ffSSimon Glass  * @devp: Returns pointer to device (there is only one per for each seq).
4175a66a8ffSSimon Glass  * Set to NULL if none is found
4185a66a8ffSSimon Glass  * @return 0 if OK, -ve on error
4195a66a8ffSSimon Glass  */
4205a66a8ffSSimon Glass int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
4215a66a8ffSSimon Glass 			     bool find_req_seq, struct udevice **devp);
4225a66a8ffSSimon Glass 
423997c87bbSSimon Glass /**
424997c87bbSSimon Glass  * device_get_child_by_seq() - Get a child device based on a sequence
425997c87bbSSimon Glass  *
426997c87bbSSimon Glass  * If an active device has this sequence it will be returned. If there is no
427997c87bbSSimon Glass  * such device then this will check for a device that is requesting this
428997c87bbSSimon Glass  * sequence.
429997c87bbSSimon Glass  *
430997c87bbSSimon Glass  * The device is probed to activate it ready for use.
431997c87bbSSimon Glass  *
432997c87bbSSimon Glass  * @parent: Parent device
433997c87bbSSimon Glass  * @seq: Sequence number to find (0=first)
434997c87bbSSimon Glass  * @devp: Returns pointer to device (there is only one per for each seq)
435997c87bbSSimon Glass  * Set to NULL if none is found
436997c87bbSSimon Glass  * @return 0 if OK, -ve on error
437997c87bbSSimon Glass  */
438997c87bbSSimon Glass int device_get_child_by_seq(struct udevice *parent, int seq,
439997c87bbSSimon Glass 			    struct udevice **devp);
440997c87bbSSimon Glass 
441997c87bbSSimon Glass /**
442997c87bbSSimon Glass  * device_find_child_by_of_offset() - Find a child device based on FDT offset
443997c87bbSSimon Glass  *
444997c87bbSSimon Glass  * Locates a child device by its device tree offset.
445997c87bbSSimon Glass  *
446997c87bbSSimon Glass  * @parent: Parent device
447997c87bbSSimon Glass  * @of_offset: Device tree offset to find
448997c87bbSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
449997c87bbSSimon Glass  * @return 0 if OK, -ve on error
450997c87bbSSimon Glass  */
451997c87bbSSimon Glass int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
452997c87bbSSimon Glass 				   struct udevice **devp);
453997c87bbSSimon Glass 
454997c87bbSSimon Glass /**
455997c87bbSSimon Glass  * device_get_child_by_of_offset() - Get a child device based on FDT offset
456997c87bbSSimon Glass  *
457997c87bbSSimon Glass  * Locates a child device by its device tree offset.
458997c87bbSSimon Glass  *
459997c87bbSSimon Glass  * The device is probed to activate it ready for use.
460997c87bbSSimon Glass  *
461997c87bbSSimon Glass  * @parent: Parent device
462997c87bbSSimon Glass  * @of_offset: Device tree offset to find
463997c87bbSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
464997c87bbSSimon Glass  * @return 0 if OK, -ve on error
465997c87bbSSimon Glass  */
466132f9bfcSSimon Glass int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
467997c87bbSSimon Glass 				  struct udevice **devp);
468997c87bbSSimon Glass 
469a8981d4fSSimon Glass /**
4702693047aSSimon Glass  * device_get_global_by_of_offset() - Get a device based on FDT offset
4712693047aSSimon Glass  *
4722693047aSSimon Glass  * Locates a device by its device tree offset, searching globally throughout
4732693047aSSimon Glass  * the all driver model devices.
4742693047aSSimon Glass  *
4752693047aSSimon Glass  * The device is probed to activate it ready for use.
4762693047aSSimon Glass  *
4772693047aSSimon Glass  * @of_offset: Device tree offset to find
4782693047aSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
4792693047aSSimon Glass  * @return 0 if OK, -ve on error
4802693047aSSimon Glass  */
4812693047aSSimon Glass int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
4822693047aSSimon Glass 
4832693047aSSimon Glass /**
484a8981d4fSSimon Glass  * device_find_first_child() - Find the first child of a device
485a8981d4fSSimon Glass  *
486a8981d4fSSimon Glass  * @parent: Parent device to search
487a8981d4fSSimon Glass  * @devp: Returns first child device, or NULL if none
488a8981d4fSSimon Glass  * @return 0
489a8981d4fSSimon Glass  */
490a8981d4fSSimon Glass int device_find_first_child(struct udevice *parent, struct udevice **devp);
491a8981d4fSSimon Glass 
492a8981d4fSSimon Glass /**
4933f416f33SSimon Glass  * device_find_next_child() - Find the next child of a device
494a8981d4fSSimon Glass  *
495a8981d4fSSimon Glass  * @devp: Pointer to previous child device on entry. Returns pointer to next
496a8981d4fSSimon Glass  *		child device, or NULL if none
497a8981d4fSSimon Glass  * @return 0
498a8981d4fSSimon Glass  */
499a8981d4fSSimon Glass int device_find_next_child(struct udevice **devp);
500a8981d4fSSimon Glass 
501c9cac3f8SPeng Fan /**
502c9cac3f8SPeng Fan  * dev_get_addr() - Get the reg property of a device
503c9cac3f8SPeng Fan  *
504c9cac3f8SPeng Fan  * @dev: Pointer to a device
505c9cac3f8SPeng Fan  *
506c9cac3f8SPeng Fan  * @return addr
507c9cac3f8SPeng Fan  */
508c9cac3f8SPeng Fan fdt_addr_t dev_get_addr(struct udevice *dev);
509c9cac3f8SPeng Fan 
510c5785673SSimon Glass /**
51128027521SStefan Roese  * dev_get_addr_ptr() - Return pointer to the address of the reg property
51228027521SStefan Roese  *                      of a device
51328027521SStefan Roese  *
51428027521SStefan Roese  * @dev: Pointer to a device
51528027521SStefan Roese  *
51628027521SStefan Roese  * @return Pointer to addr, or NULL if there is no such property
51728027521SStefan Roese  */
51828027521SStefan Roese void *dev_get_addr_ptr(struct udevice *dev);
51928027521SStefan Roese 
52028027521SStefan Roese /**
5217c616862SVignesh R  * dev_map_physmem() - Read device address from reg property of the
5227c616862SVignesh R  *                     device node and map the address into CPU address
5237c616862SVignesh R  *                     space.
5247c616862SVignesh R  *
5257c616862SVignesh R  * @dev: Pointer to device
5267c616862SVignesh R  * @size: size of the memory to map
5277c616862SVignesh R  *
5287c616862SVignesh R  * @return  mapped address, or NULL if the device does not have reg
5297c616862SVignesh R  *          property.
5307c616862SVignesh R  */
5317c616862SVignesh R void *dev_map_physmem(struct udevice *dev, unsigned long size);
5327c616862SVignesh R 
5337c616862SVignesh R /**
53469b41388SMugunthan V N  * dev_get_addr_index() - Get the indexed reg property of a device
53569b41388SMugunthan V N  *
53669b41388SMugunthan V N  * @dev: Pointer to a device
53769b41388SMugunthan V N  * @index: the 'reg' property can hold a list of <addr, size> pairs
53869b41388SMugunthan V N  *	   and @index is used to select which one is required
53969b41388SMugunthan V N  *
54069b41388SMugunthan V N  * @return addr
54169b41388SMugunthan V N  */
54269b41388SMugunthan V N fdt_addr_t dev_get_addr_index(struct udevice *dev, int index);
54369b41388SMugunthan V N 
54469b41388SMugunthan V N /**
54513f3fcacSStefan Roese  * dev_get_addr_size_index() - Get the indexed reg property of a device
54613f3fcacSStefan Roese  *
54713f3fcacSStefan Roese  * Returns the address and size specified in the 'reg' property of a device.
54813f3fcacSStefan Roese  *
54913f3fcacSStefan Roese  * @dev: Pointer to a device
55013f3fcacSStefan Roese  * @index: the 'reg' property can hold a list of <addr, size> pairs
55113f3fcacSStefan Roese  *	   and @index is used to select which one is required
55213f3fcacSStefan Roese  * @size: Pointer to size varible - this function returns the size
55313f3fcacSStefan Roese  *        specified in the 'reg' property here
55413f3fcacSStefan Roese  *
55513f3fcacSStefan Roese  * @return addr
55613f3fcacSStefan Roese  */
55713f3fcacSStefan Roese fdt_addr_t dev_get_addr_size_index(struct udevice *dev, int index,
55813f3fcacSStefan Roese 				   fdt_size_t *size);
55913f3fcacSStefan Roese 
56013f3fcacSStefan Roese /**
56143c4d44eSStephen Warren  * dev_get_addr_name() - Get the reg property of a device, indexed by name
56243c4d44eSStephen Warren  *
56343c4d44eSStephen Warren  * @dev: Pointer to a device
56443c4d44eSStephen Warren  * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
56543c4d44eSStephen Warren  *	  'reg-names' property providing named-based identification. @index
56643c4d44eSStephen Warren  *	  indicates the value to search for in 'reg-names'.
56743c4d44eSStephen Warren  *
56843c4d44eSStephen Warren  * @return addr
56943c4d44eSStephen Warren  */
57043c4d44eSStephen Warren fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name);
57143c4d44eSStephen Warren 
57243c4d44eSStephen Warren /**
573c5785673SSimon Glass  * device_has_children() - check if a device has any children
574c5785673SSimon Glass  *
575c5785673SSimon Glass  * @dev:	Device to check
576c5785673SSimon Glass  * @return true if the device has one or more children
577c5785673SSimon Glass  */
578c5785673SSimon Glass bool device_has_children(struct udevice *dev);
579c5785673SSimon Glass 
580c5785673SSimon Glass /**
581c5785673SSimon Glass  * device_has_active_children() - check if a device has any active children
582c5785673SSimon Glass  *
583c5785673SSimon Glass  * @dev:	Device to check
584c5785673SSimon Glass  * @return true if the device has one or more children and at least one of
585c5785673SSimon Glass  * them is active (probed).
586c5785673SSimon Glass  */
587c5785673SSimon Glass bool device_has_active_children(struct udevice *dev);
588c5785673SSimon Glass 
589c5785673SSimon Glass /**
590c5785673SSimon Glass  * device_is_last_sibling() - check if a device is the last sibling
591c5785673SSimon Glass  *
592c5785673SSimon Glass  * This function can be useful for display purposes, when special action needs
593c5785673SSimon Glass  * to be taken when displaying the last sibling. This can happen when a tree
594c5785673SSimon Glass  * view of devices is being displayed.
595c5785673SSimon Glass  *
596c5785673SSimon Glass  * @dev:	Device to check
597c5785673SSimon Glass  * @return true if there are no more siblings after this one - i.e. is it
598c5785673SSimon Glass  * last in the list.
599c5785673SSimon Glass  */
600c5785673SSimon Glass bool device_is_last_sibling(struct udevice *dev);
601c5785673SSimon Glass 
602f5c67ea0SSimon Glass /**
603f5c67ea0SSimon Glass  * device_set_name() - set the name of a device
604f5c67ea0SSimon Glass  *
605f5c67ea0SSimon Glass  * This must be called in the device's bind() method and no later. Normally
606f5c67ea0SSimon Glass  * this is unnecessary but for probed devices which don't get a useful name
607f5c67ea0SSimon Glass  * this function can be helpful.
608f5c67ea0SSimon Glass  *
609a2040facSSimon Glass  * The name is allocated and will be freed automatically when the device is
610a2040facSSimon Glass  * unbound.
611a2040facSSimon Glass  *
612f5c67ea0SSimon Glass  * @dev:	Device to update
613f5c67ea0SSimon Glass  * @name:	New name (this string is allocated new memory and attached to
614f5c67ea0SSimon Glass  *		the device)
615f5c67ea0SSimon Glass  * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
616f5c67ea0SSimon Glass  * string
617f5c67ea0SSimon Glass  */
618f5c67ea0SSimon Glass int device_set_name(struct udevice *dev, const char *name);
619f5c67ea0SSimon Glass 
6201e0f2263SBin Meng /**
621a2040facSSimon Glass  * device_set_name_alloced() - note that a device name is allocated
622a2040facSSimon Glass  *
623fd1c2d9bSSimon Glass  * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
624a2040facSSimon Glass  * unbound the name will be freed. This avoids memory leaks.
625a2040facSSimon Glass  *
626a2040facSSimon Glass  * @dev:	Device to update
627a2040facSSimon Glass  */
628a2040facSSimon Glass void device_set_name_alloced(struct udevice *dev);
629a2040facSSimon Glass 
630a2040facSSimon Glass /**
63173443b9eSMugunthan V N  * of_device_is_compatible() - check if the device is compatible with the compat
63273443b9eSMugunthan V N  *
63373443b9eSMugunthan V N  * This allows to check whether the device is comaptible with the compat.
63473443b9eSMugunthan V N  *
63573443b9eSMugunthan V N  * @dev:	udevice pointer for which compatible needs to be verified.
63673443b9eSMugunthan V N  * @compat:	Compatible string which needs to verified in the given
63773443b9eSMugunthan V N  *		device
63873443b9eSMugunthan V N  * @return true if OK, false if the compatible is not found
63973443b9eSMugunthan V N  */
64073443b9eSMugunthan V N bool of_device_is_compatible(struct udevice *dev, const char *compat);
64173443b9eSMugunthan V N 
64273443b9eSMugunthan V N /**
64373443b9eSMugunthan V N  * of_machine_is_compatible() - check if the machine is compatible with
64473443b9eSMugunthan V N  *				the compat
64573443b9eSMugunthan V N  *
64673443b9eSMugunthan V N  * This allows to check whether the machine is comaptible with the compat.
64773443b9eSMugunthan V N  *
64873443b9eSMugunthan V N  * @compat:	Compatible string which needs to verified
64973443b9eSMugunthan V N  * @return true if OK, false if the compatible is not found
65073443b9eSMugunthan V N  */
65173443b9eSMugunthan V N bool of_machine_is_compatible(const char *compat);
65273443b9eSMugunthan V N 
65373443b9eSMugunthan V N /**
6541e0f2263SBin Meng  * device_is_on_pci_bus - Test if a device is on a PCI bus
6551e0f2263SBin Meng  *
6561e0f2263SBin Meng  * @dev:	device to test
6571e0f2263SBin Meng  * @return:	true if it is on a PCI bus, false otherwise
6581e0f2263SBin Meng  */
6591e0f2263SBin Meng static inline bool device_is_on_pci_bus(struct udevice *dev)
6601e0f2263SBin Meng {
6611e0f2263SBin Meng 	return device_get_uclass_id(dev->parent) == UCLASS_PCI;
6621e0f2263SBin Meng }
6631e0f2263SBin Meng 
6647aeac5bcSSimon Glass /**
6657aeac5bcSSimon Glass  * device_foreach_child_safe() - iterate through child devices safely
6667aeac5bcSSimon Glass  *
6677aeac5bcSSimon Glass  * This allows the @pos child to be removed in the loop if required.
6687aeac5bcSSimon Glass  *
6697aeac5bcSSimon Glass  * @pos: struct udevice * for the current device
6707aeac5bcSSimon Glass  * @next: struct udevice * for the next device
6717aeac5bcSSimon Glass  * @parent: parent device to scan
6727aeac5bcSSimon Glass  */
6737aeac5bcSSimon Glass #define device_foreach_child_safe(pos, next, parent)	\
6747aeac5bcSSimon Glass 	list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
6757aeac5bcSSimon Glass 
676cc7f66f7SSimon Glass /**
677cc7f66f7SSimon Glass  * dm_scan_fdt_dev() - Bind child device in a the device tree
678cc7f66f7SSimon Glass  *
679cc7f66f7SSimon Glass  * This handles device which have sub-nodes in the device tree. It scans all
680cc7f66f7SSimon Glass  * sub-nodes and binds drivers for each node where a driver can be found.
681cc7f66f7SSimon Glass  *
682cc7f66f7SSimon Glass  * If this is called prior to relocation, only pre-relocation devices will be
683cc7f66f7SSimon Glass  * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
684cc7f66f7SSimon Glass  * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
685cc7f66f7SSimon Glass  * be bound.
686cc7f66f7SSimon Glass  *
687cc7f66f7SSimon Glass  * @dev:	Device to scan
688cc7f66f7SSimon Glass  * @return 0 if OK, -ve on error
689cc7f66f7SSimon Glass  */
690cc7f66f7SSimon Glass int dm_scan_fdt_dev(struct udevice *dev);
691cc7f66f7SSimon Glass 
692608f26c5SMasahiro Yamada /* device resource management */
693608f26c5SMasahiro Yamada typedef void (*dr_release_t)(struct udevice *dev, void *res);
694608f26c5SMasahiro Yamada typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
695608f26c5SMasahiro Yamada 
696e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES
697e2282d70SMasahiro Yamada 
698608f26c5SMasahiro Yamada #ifdef CONFIG_DEBUG_DEVRES
699608f26c5SMasahiro Yamada void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
700608f26c5SMasahiro Yamada 		     const char *name);
701608f26c5SMasahiro Yamada #define _devres_alloc(release, size, gfp) \
702608f26c5SMasahiro Yamada 	__devres_alloc(release, size, gfp, #release)
703608f26c5SMasahiro Yamada #else
704608f26c5SMasahiro Yamada void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
705608f26c5SMasahiro Yamada #endif
706608f26c5SMasahiro Yamada 
707608f26c5SMasahiro Yamada /**
70893c7fe4aSSimon Glass  * devres_alloc() - Allocate device resource data
709608f26c5SMasahiro Yamada  * @release: Release function devres will be associated with
710608f26c5SMasahiro Yamada  * @size: Allocation size
711608f26c5SMasahiro Yamada  * @gfp: Allocation flags
712608f26c5SMasahiro Yamada  *
713608f26c5SMasahiro Yamada  * Allocate devres of @size bytes.  The allocated area is associated
714608f26c5SMasahiro Yamada  * with @release.  The returned pointer can be passed to
715608f26c5SMasahiro Yamada  * other devres_*() functions.
716608f26c5SMasahiro Yamada  *
717608f26c5SMasahiro Yamada  * RETURNS:
718608f26c5SMasahiro Yamada  * Pointer to allocated devres on success, NULL on failure.
719608f26c5SMasahiro Yamada  */
720608f26c5SMasahiro Yamada #define devres_alloc(release, size, gfp) \
721608f26c5SMasahiro Yamada 	_devres_alloc(release, size, gfp | __GFP_ZERO)
722608f26c5SMasahiro Yamada 
723608f26c5SMasahiro Yamada /**
72493c7fe4aSSimon Glass  * devres_free() - Free device resource data
725608f26c5SMasahiro Yamada  * @res: Pointer to devres data to free
726608f26c5SMasahiro Yamada  *
727608f26c5SMasahiro Yamada  * Free devres created with devres_alloc().
728608f26c5SMasahiro Yamada  */
729608f26c5SMasahiro Yamada void devres_free(void *res);
730608f26c5SMasahiro Yamada 
731608f26c5SMasahiro Yamada /**
73293c7fe4aSSimon Glass  * devres_add() - Register device resource
733608f26c5SMasahiro Yamada  * @dev: Device to add resource to
734608f26c5SMasahiro Yamada  * @res: Resource to register
735608f26c5SMasahiro Yamada  *
736608f26c5SMasahiro Yamada  * Register devres @res to @dev.  @res should have been allocated
737608f26c5SMasahiro Yamada  * using devres_alloc().  On driver detach, the associated release
738608f26c5SMasahiro Yamada  * function will be invoked and devres will be freed automatically.
739608f26c5SMasahiro Yamada  */
740608f26c5SMasahiro Yamada void devres_add(struct udevice *dev, void *res);
741608f26c5SMasahiro Yamada 
742608f26c5SMasahiro Yamada /**
74393c7fe4aSSimon Glass  * devres_find() - Find device resource
744608f26c5SMasahiro Yamada  * @dev: Device to lookup resource from
745608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
746608f26c5SMasahiro Yamada  * @match: Match function (optional)
747608f26c5SMasahiro Yamada  * @match_data: Data for the match function
748608f26c5SMasahiro Yamada  *
749608f26c5SMasahiro Yamada  * Find the latest devres of @dev which is associated with @release
750608f26c5SMasahiro Yamada  * and for which @match returns 1.  If @match is NULL, it's considered
751608f26c5SMasahiro Yamada  * to match all.
752608f26c5SMasahiro Yamada  *
75393c7fe4aSSimon Glass  * @return pointer to found devres, NULL if not found.
754608f26c5SMasahiro Yamada  */
755608f26c5SMasahiro Yamada void *devres_find(struct udevice *dev, dr_release_t release,
756608f26c5SMasahiro Yamada 		  dr_match_t match, void *match_data);
757608f26c5SMasahiro Yamada 
758608f26c5SMasahiro Yamada /**
75993c7fe4aSSimon Glass  * devres_get() - Find devres, if non-existent, add one atomically
760608f26c5SMasahiro Yamada  * @dev: Device to lookup or add devres for
761608f26c5SMasahiro Yamada  * @new_res: Pointer to new initialized devres to add if not found
762608f26c5SMasahiro Yamada  * @match: Match function (optional)
763608f26c5SMasahiro Yamada  * @match_data: Data for the match function
764608f26c5SMasahiro Yamada  *
765608f26c5SMasahiro Yamada  * Find the latest devres of @dev which has the same release function
766608f26c5SMasahiro Yamada  * as @new_res and for which @match return 1.  If found, @new_res is
767608f26c5SMasahiro Yamada  * freed; otherwise, @new_res is added atomically.
768608f26c5SMasahiro Yamada  *
76993c7fe4aSSimon Glass  * @return ointer to found or added devres.
770608f26c5SMasahiro Yamada  */
771608f26c5SMasahiro Yamada void *devres_get(struct udevice *dev, void *new_res,
772608f26c5SMasahiro Yamada 		 dr_match_t match, void *match_data);
773608f26c5SMasahiro Yamada 
774608f26c5SMasahiro Yamada /**
77593c7fe4aSSimon Glass  * devres_remove() - Find a device resource and remove it
776608f26c5SMasahiro Yamada  * @dev: Device to find resource from
777608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
778608f26c5SMasahiro Yamada  * @match: Match function (optional)
779608f26c5SMasahiro Yamada  * @match_data: Data for the match function
780608f26c5SMasahiro Yamada  *
781608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
782608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
783608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically and
784608f26c5SMasahiro Yamada  * returned.
785608f26c5SMasahiro Yamada  *
78693c7fe4aSSimon Glass  * @return ointer to removed devres on success, NULL if not found.
787608f26c5SMasahiro Yamada  */
788608f26c5SMasahiro Yamada void *devres_remove(struct udevice *dev, dr_release_t release,
789608f26c5SMasahiro Yamada 		    dr_match_t match, void *match_data);
790608f26c5SMasahiro Yamada 
791608f26c5SMasahiro Yamada /**
79293c7fe4aSSimon Glass  * devres_destroy() - Find a device resource and destroy it
793608f26c5SMasahiro Yamada  * @dev: Device to find resource from
794608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
795608f26c5SMasahiro Yamada  * @match: Match function (optional)
796608f26c5SMasahiro Yamada  * @match_data: Data for the match function
797608f26c5SMasahiro Yamada  *
798608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
799608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
800608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically and freed.
801608f26c5SMasahiro Yamada  *
802608f26c5SMasahiro Yamada  * Note that the release function for the resource will not be called,
803608f26c5SMasahiro Yamada  * only the devres-allocated data will be freed.  The caller becomes
804608f26c5SMasahiro Yamada  * responsible for freeing any other data.
805608f26c5SMasahiro Yamada  *
80693c7fe4aSSimon Glass  * @return 0 if devres is found and freed, -ENOENT if not found.
807608f26c5SMasahiro Yamada  */
808608f26c5SMasahiro Yamada int devres_destroy(struct udevice *dev, dr_release_t release,
809608f26c5SMasahiro Yamada 		   dr_match_t match, void *match_data);
810608f26c5SMasahiro Yamada 
811608f26c5SMasahiro Yamada /**
81293c7fe4aSSimon Glass  * devres_release() - Find a device resource and destroy it, calling release
813608f26c5SMasahiro Yamada  * @dev: Device to find resource from
814608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
815608f26c5SMasahiro Yamada  * @match: Match function (optional)
816608f26c5SMasahiro Yamada  * @match_data: Data for the match function
817608f26c5SMasahiro Yamada  *
818608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
819608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
820608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically, the
821608f26c5SMasahiro Yamada  * release function called and the resource freed.
822608f26c5SMasahiro Yamada  *
82393c7fe4aSSimon Glass  * @return 0 if devres is found and freed, -ENOENT if not found.
824608f26c5SMasahiro Yamada  */
825608f26c5SMasahiro Yamada int devres_release(struct udevice *dev, dr_release_t release,
826608f26c5SMasahiro Yamada 		   dr_match_t match, void *match_data);
827608f26c5SMasahiro Yamada 
8282b07f685SMasahiro Yamada /* managed devm_k.alloc/kfree for device drivers */
8292b07f685SMasahiro Yamada /**
83093c7fe4aSSimon Glass  * devm_kmalloc() - Resource-managed kmalloc
8312b07f685SMasahiro Yamada  * @dev: Device to allocate memory for
8322b07f685SMasahiro Yamada  * @size: Allocation size
8332b07f685SMasahiro Yamada  * @gfp: Allocation gfp flags
8342b07f685SMasahiro Yamada  *
8352b07f685SMasahiro Yamada  * Managed kmalloc.  Memory allocated with this function is
8362b07f685SMasahiro Yamada  * automatically freed on driver detach.  Like all other devres
8372b07f685SMasahiro Yamada  * resources, guaranteed alignment is unsigned long long.
8382b07f685SMasahiro Yamada  *
83993c7fe4aSSimon Glass  * @return pointer to allocated memory on success, NULL on failure.
8402b07f685SMasahiro Yamada  */
8412b07f685SMasahiro Yamada void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
8422b07f685SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
8432b07f685SMasahiro Yamada {
8442b07f685SMasahiro Yamada 	return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
8452b07f685SMasahiro Yamada }
8462b07f685SMasahiro Yamada static inline void *devm_kmalloc_array(struct udevice *dev,
8472b07f685SMasahiro Yamada 				       size_t n, size_t size, gfp_t flags)
8482b07f685SMasahiro Yamada {
8492b07f685SMasahiro Yamada 	if (size != 0 && n > SIZE_MAX / size)
8502b07f685SMasahiro Yamada 		return NULL;
8512b07f685SMasahiro Yamada 	return devm_kmalloc(dev, n * size, flags);
8522b07f685SMasahiro Yamada }
8532b07f685SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev,
8542b07f685SMasahiro Yamada 				 size_t n, size_t size, gfp_t flags)
8552b07f685SMasahiro Yamada {
8562b07f685SMasahiro Yamada 	return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
8572b07f685SMasahiro Yamada }
8582b07f685SMasahiro Yamada 
8592b07f685SMasahiro Yamada /**
86093c7fe4aSSimon Glass  * devm_kfree() - Resource-managed kfree
8612b07f685SMasahiro Yamada  * @dev: Device this memory belongs to
86293c7fe4aSSimon Glass  * @ptr: Memory to free
8632b07f685SMasahiro Yamada  *
8642b07f685SMasahiro Yamada  * Free memory allocated with devm_kmalloc().
8652b07f685SMasahiro Yamada  */
86693c7fe4aSSimon Glass void devm_kfree(struct udevice *dev, void *ptr);
8672b07f685SMasahiro Yamada 
868e2282d70SMasahiro Yamada #else /* ! CONFIG_DEVRES */
869e2282d70SMasahiro Yamada 
870e2282d70SMasahiro Yamada static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
871e2282d70SMasahiro Yamada {
872e2282d70SMasahiro Yamada 	return kzalloc(size, gfp);
873e2282d70SMasahiro Yamada }
874e2282d70SMasahiro Yamada 
875e2282d70SMasahiro Yamada static inline void devres_free(void *res)
876e2282d70SMasahiro Yamada {
877e2282d70SMasahiro Yamada 	kfree(res);
878e2282d70SMasahiro Yamada }
879e2282d70SMasahiro Yamada 
880e2282d70SMasahiro Yamada static inline void devres_add(struct udevice *dev, void *res)
881e2282d70SMasahiro Yamada {
882e2282d70SMasahiro Yamada }
883e2282d70SMasahiro Yamada 
884e2282d70SMasahiro Yamada static inline void *devres_find(struct udevice *dev, dr_release_t release,
885e2282d70SMasahiro Yamada 				dr_match_t match, void *match_data)
886e2282d70SMasahiro Yamada {
887e2282d70SMasahiro Yamada 	return NULL;
888e2282d70SMasahiro Yamada }
889e2282d70SMasahiro Yamada 
890e2282d70SMasahiro Yamada static inline void *devres_get(struct udevice *dev, void *new_res,
891e2282d70SMasahiro Yamada 			       dr_match_t match, void *match_data)
892e2282d70SMasahiro Yamada {
893e2282d70SMasahiro Yamada 	return NULL;
894e2282d70SMasahiro Yamada }
895e2282d70SMasahiro Yamada 
896e2282d70SMasahiro Yamada static inline void *devres_remove(struct udevice *dev, dr_release_t release,
897e2282d70SMasahiro Yamada 				  dr_match_t match, void *match_data)
898e2282d70SMasahiro Yamada {
899e2282d70SMasahiro Yamada 	return NULL;
900e2282d70SMasahiro Yamada }
901e2282d70SMasahiro Yamada 
902e2282d70SMasahiro Yamada static inline int devres_destroy(struct udevice *dev, dr_release_t release,
903e2282d70SMasahiro Yamada 				 dr_match_t match, void *match_data)
904e2282d70SMasahiro Yamada {
905e2282d70SMasahiro Yamada 	return 0;
906e2282d70SMasahiro Yamada }
907e2282d70SMasahiro Yamada 
908e2282d70SMasahiro Yamada static inline int devres_release(struct udevice *dev, dr_release_t release,
909e2282d70SMasahiro Yamada 				 dr_match_t match, void *match_data)
910e2282d70SMasahiro Yamada {
911e2282d70SMasahiro Yamada 	return 0;
912e2282d70SMasahiro Yamada }
913e2282d70SMasahiro Yamada 
914e2282d70SMasahiro Yamada static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
915e2282d70SMasahiro Yamada {
916e2282d70SMasahiro Yamada 	return kmalloc(size, gfp);
917e2282d70SMasahiro Yamada }
918e2282d70SMasahiro Yamada 
919e2282d70SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
920e2282d70SMasahiro Yamada {
921e2282d70SMasahiro Yamada 	return kzalloc(size, gfp);
922e2282d70SMasahiro Yamada }
923e2282d70SMasahiro Yamada 
924e2282d70SMasahiro Yamada static inline void *devm_kmaloc_array(struct udevice *dev,
925e2282d70SMasahiro Yamada 				      size_t n, size_t size, gfp_t flags)
926e2282d70SMasahiro Yamada {
927e2282d70SMasahiro Yamada 	/* TODO: add kmalloc_array() to linux/compat.h */
928e2282d70SMasahiro Yamada 	if (size != 0 && n > SIZE_MAX / size)
929e2282d70SMasahiro Yamada 		return NULL;
930e2282d70SMasahiro Yamada 	return kmalloc(n * size, flags);
931e2282d70SMasahiro Yamada }
932e2282d70SMasahiro Yamada 
933e2282d70SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev,
934e2282d70SMasahiro Yamada 				 size_t n, size_t size, gfp_t flags)
935e2282d70SMasahiro Yamada {
936e2282d70SMasahiro Yamada 	/* TODO: add kcalloc() to linux/compat.h */
937e2282d70SMasahiro Yamada 	return kmalloc(n * size, flags | __GFP_ZERO);
938e2282d70SMasahiro Yamada }
939e2282d70SMasahiro Yamada 
94093c7fe4aSSimon Glass static inline void devm_kfree(struct udevice *dev, void *ptr)
941e2282d70SMasahiro Yamada {
94293c7fe4aSSimon Glass 	kfree(ptr);
943e2282d70SMasahiro Yamada }
944e2282d70SMasahiro Yamada 
945e2282d70SMasahiro Yamada #endif /* ! CONFIG_DEVRES */
9462b07f685SMasahiro Yamada 
94766eaea6cSStefan Roese /**
94866eaea6cSStefan Roese  * dm_set_translation_offset() - Set translation offset
94966eaea6cSStefan Roese  * @offs: Translation offset
95066eaea6cSStefan Roese  *
95166eaea6cSStefan Roese  * Some platforms need a special address translation. Those
95266eaea6cSStefan Roese  * platforms (e.g. mvebu in SPL) can configure a translation
95366eaea6cSStefan Roese  * offset in the DM by calling this function. It will be
95466eaea6cSStefan Roese  * added to all addresses returned in dev_get_addr().
95566eaea6cSStefan Roese  */
95666eaea6cSStefan Roese void dm_set_translation_offset(fdt_addr_t offs);
95766eaea6cSStefan Roese 
95866eaea6cSStefan Roese /**
95966eaea6cSStefan Roese  * dm_get_translation_offset() - Get translation offset
96066eaea6cSStefan Roese  *
96166eaea6cSStefan Roese  * This function returns the translation offset that can
96266eaea6cSStefan Roese  * be configured by calling dm_set_translation_offset().
96366eaea6cSStefan Roese  *
96466eaea6cSStefan Roese  * @return translation offset for the device address (0 as default).
96566eaea6cSStefan Roese  */
96666eaea6cSStefan Roese fdt_addr_t dm_get_translation_offset(void);
96766eaea6cSStefan Roese 
9686494d708SSimon Glass #endif
969