xref: /rk3399_rockchip-uboot/include/dm/device.h (revision 706865afe54eee83c1f3d7e9ea2f51db8e986d7b)
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 
49*706865afSStefan Roese /*
50*706865afSStefan Roese  * Call driver remove function to stop currently active DMA transfers or
51*706865afSStefan Roese  * give DMA buffers back to the HW / controller. This may be needed for
52*706865afSStefan Roese  * some drivers to do some final stage cleanup before the OS is called
53*706865afSStefan Roese  * (U-Boot exit)
54*706865afSStefan Roese  */
55*706865afSStefan Roese #define DM_FLAG_ACTIVE_DMA		(1 << 9)
56*706865afSStefan Roese 
57*706865afSStefan Roese /*
58*706865afSStefan Roese  * One or multiple of these flags are passed to device_remove() so that
59*706865afSStefan Roese  * a selective device removal as specified by the remove-stage and the
60*706865afSStefan Roese  * driver flags can be done.
61*706865afSStefan Roese  */
62*706865afSStefan Roese enum {
63*706865afSStefan Roese 	/* Normal remove, remove all devices */
64*706865afSStefan Roese 	DM_REMOVE_NORMAL     = 1 << 0,
65*706865afSStefan Roese 
66*706865afSStefan Roese 	/* Remove devices with active DMA */
67*706865afSStefan Roese 	DM_REMOVE_ACTIVE_DMA = DM_FLAG_ACTIVE_DMA,
68*706865afSStefan Roese 
69*706865afSStefan Roese 	/* Add more use cases here */
70*706865afSStefan Roese 
71*706865afSStefan Roese 	/* Remove devices with any active flag */
72*706865afSStefan Roese 	DM_REMOVE_ACTIVE_ALL = DM_REMOVE_ACTIVE_DMA,
73*706865afSStefan Roese };
74*706865afSStefan Roese 
756494d708SSimon Glass /**
7654c5d08aSHeiko Schocher  * struct udevice - An instance of a driver
776494d708SSimon Glass  *
786494d708SSimon Glass  * This holds information about a device, which is a driver bound to a
796494d708SSimon Glass  * particular port or peripheral (essentially a driver instance).
806494d708SSimon Glass  *
816494d708SSimon Glass  * A device will come into existence through a 'bind' call, either due to
826494d708SSimon Glass  * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
836494d708SSimon Glass  * in the device tree (in which case of_offset is >= 0). In the latter case
846494d708SSimon Glass  * we translate the device tree information into platdata in a function
856494d708SSimon Glass  * implemented by the driver ofdata_to_platdata method (called just before the
866494d708SSimon Glass  * probe method if the device has a device tree node.
876494d708SSimon Glass  *
886494d708SSimon Glass  * All three of platdata, priv and uclass_priv can be allocated by the
896494d708SSimon Glass  * driver, or you can use the auto_alloc_size members of struct driver and
906494d708SSimon Glass  * struct uclass_driver to have driver model do this automatically.
916494d708SSimon Glass  *
926494d708SSimon Glass  * @driver: The driver used by this device
936494d708SSimon Glass  * @name: Name of device, typically the FDT node name
946494d708SSimon Glass  * @platdata: Configuration data for this device
95cdc133bdSSimon Glass  * @parent_platdata: The parent bus's configuration data for this device
965eaed880SPrzemyslaw Marczak  * @uclass_platdata: The uclass's configuration data for this device
976494d708SSimon Glass  * @of_offset: Device tree node offset for this device (- for none)
9839de8433SSimon Glass  * @driver_data: Driver data word for the entry that matched this device with
9939de8433SSimon Glass  *		its driver
1006494d708SSimon Glass  * @parent: Parent of this device, or NULL for the top level device
1016494d708SSimon Glass  * @priv: Private data for this device
1026494d708SSimon Glass  * @uclass: Pointer to uclass for this device
1036494d708SSimon Glass  * @uclass_priv: The uclass's private data for this device
104e59f458dSSimon Glass  * @parent_priv: The parent's private data for this device
1056494d708SSimon Glass  * @uclass_node: Used by uclass to link its devices
1066494d708SSimon Glass  * @child_head: List of children of this device
1076494d708SSimon Glass  * @sibling_node: Next device in list of all devices
1086494d708SSimon Glass  * @flags: Flags for this device DM_FLAG_...
1095a66a8ffSSimon Glass  * @req_seq: Requested sequence number for this device (-1 = any)
110547cea19SSimon Glass  * @seq: Allocated sequence number for this device (-1 = none). This is set up
111547cea19SSimon Glass  * when the device is probed and will be unique within the device's uclass.
11293c7fe4aSSimon Glass  * @devres_head: List of memory allocations associated with this device.
11393c7fe4aSSimon Glass  *		When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
11493c7fe4aSSimon Glass  *		add to this list. Memory so-allocated will be freed
11593c7fe4aSSimon Glass  *		automatically when the device is removed / unbound
1166494d708SSimon Glass  */
11754c5d08aSHeiko Schocher struct udevice {
1183479253dSSimon Glass 	const struct driver *driver;
1196494d708SSimon Glass 	const char *name;
1206494d708SSimon Glass 	void *platdata;
121cdc133bdSSimon Glass 	void *parent_platdata;
1225eaed880SPrzemyslaw Marczak 	void *uclass_platdata;
1236494d708SSimon Glass 	int of_offset;
12439de8433SSimon Glass 	ulong driver_data;
12554c5d08aSHeiko Schocher 	struct udevice *parent;
1266494d708SSimon Glass 	void *priv;
1276494d708SSimon Glass 	struct uclass *uclass;
1286494d708SSimon Glass 	void *uclass_priv;
129e59f458dSSimon Glass 	void *parent_priv;
1306494d708SSimon Glass 	struct list_head uclass_node;
1316494d708SSimon Glass 	struct list_head child_head;
1326494d708SSimon Glass 	struct list_head sibling_node;
1336494d708SSimon Glass 	uint32_t flags;
1345a66a8ffSSimon Glass 	int req_seq;
1355a66a8ffSSimon Glass 	int seq;
136e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES
137608f26c5SMasahiro Yamada 	struct list_head devres_head;
138e2282d70SMasahiro Yamada #endif
1396494d708SSimon Glass };
1406494d708SSimon Glass 
1415a66a8ffSSimon Glass /* Maximum sequence number supported */
1425a66a8ffSSimon Glass #define DM_MAX_SEQ	999
1435a66a8ffSSimon Glass 
1446494d708SSimon Glass /* Returns the operations for a device */
1456494d708SSimon Glass #define device_get_ops(dev)	(dev->driver->ops)
1466494d708SSimon Glass 
1476494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */
1486494d708SSimon Glass #define device_active(dev)	((dev)->flags & DM_FLAG_ACTIVATED)
1496494d708SSimon Glass 
150e160f7d4SSimon Glass static inline int dev_of_offset(const struct udevice *dev)
151e160f7d4SSimon Glass {
152e160f7d4SSimon Glass 	return dev->of_offset;
153e160f7d4SSimon Glass }
154e160f7d4SSimon Glass 
155e160f7d4SSimon Glass static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
156e160f7d4SSimon Glass {
157e160f7d4SSimon Glass 	dev->of_offset = of_offset;
158e160f7d4SSimon Glass }
159e160f7d4SSimon Glass 
1606494d708SSimon Glass /**
161ae7f4513SSimon Glass  * struct udevice_id - Lists the compatible strings supported by a driver
1626494d708SSimon Glass  * @compatible: Compatible string
1636494d708SSimon Glass  * @data: Data for this compatible string
1646494d708SSimon Glass  */
165ae7f4513SSimon Glass struct udevice_id {
1666494d708SSimon Glass 	const char *compatible;
1676494d708SSimon Glass 	ulong data;
1686494d708SSimon Glass };
1696494d708SSimon Glass 
1700f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
171f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr)	(_ptr)
172f887cb6dSMasahiro Yamada #else
173f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr)	NULL
1740f925822SMasahiro Yamada #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
175f887cb6dSMasahiro Yamada 
1766494d708SSimon Glass /**
1776494d708SSimon Glass  * struct driver - A driver for a feature or peripheral
1786494d708SSimon Glass  *
1796494d708SSimon Glass  * This holds methods for setting up a new device, and also removing it.
1806494d708SSimon Glass  * The device needs information to set itself up - this is provided either
1816494d708SSimon Glass  * by platdata or a device tree node (which we find by looking up
1826494d708SSimon Glass  * matching compatible strings with of_match).
1836494d708SSimon Glass  *
1846494d708SSimon Glass  * Drivers all belong to a uclass, representing a class of devices of the
1856494d708SSimon Glass  * same type. Common elements of the drivers can be implemented in the uclass,
1866494d708SSimon Glass  * or the uclass can provide a consistent interface to the drivers within
1876494d708SSimon Glass  * it.
1886494d708SSimon Glass  *
1896494d708SSimon Glass  * @name: Device name
1906494d708SSimon Glass  * @id: Identiies the uclass we belong to
1916494d708SSimon Glass  * @of_match: List of compatible strings to match, and any identifying data
1926494d708SSimon Glass  * for each.
1936494d708SSimon Glass  * @bind: Called to bind a device to its driver
1946494d708SSimon Glass  * @probe: Called to probe a device, i.e. activate it
1956494d708SSimon Glass  * @remove: Called to remove a device, i.e. de-activate it
1966494d708SSimon Glass  * @unbind: Called to unbind a device from its driver
1976494d708SSimon Glass  * @ofdata_to_platdata: Called before probe to decode device tree data
1980118ce79SSimon Glass  * @child_post_bind: Called after a new child has been bound
199a327dee0SSimon Glass  * @child_pre_probe: Called before a child device is probed. The device has
200a327dee0SSimon Glass  * memory allocated but it has not yet been probed.
201a327dee0SSimon Glass  * @child_post_remove: Called after a child device is removed. The device
202a327dee0SSimon Glass  * has memory allocated but its device_remove() method has been called.
2036494d708SSimon Glass  * @priv_auto_alloc_size: If non-zero this is the size of the private data
2046494d708SSimon Glass  * to be allocated in the device's ->priv pointer. If zero, then the driver
2056494d708SSimon Glass  * is responsible for allocating any data required.
2066494d708SSimon Glass  * @platdata_auto_alloc_size: If non-zero this is the size of the
2076494d708SSimon Glass  * platform data to be allocated in the device's ->platdata pointer.
2086494d708SSimon Glass  * This is typically only useful for device-tree-aware drivers (those with
2096494d708SSimon Glass  * an of_match), since drivers which use platdata will have the data
2106494d708SSimon Glass  * provided in the U_BOOT_DEVICE() instantiation.
211e59f458dSSimon Glass  * @per_child_auto_alloc_size: Each device can hold private data owned by
212e59f458dSSimon Glass  * its parent. If required this will be automatically allocated if this
213e59f458dSSimon Glass  * value is non-zero.
214cdc133bdSSimon Glass  * @per_child_platdata_auto_alloc_size: A bus likes to store information about
215cdc133bdSSimon Glass  * its children. If non-zero this is the size of this data, to be allocated
216cdc133bdSSimon Glass  * in the child's parent_platdata pointer.
2170040b944SSimon Glass  * @ops: Driver-specific operations. This is typically a list of function
2186494d708SSimon Glass  * pointers defined by the driver, to implement driver functions required by
2196494d708SSimon Glass  * the uclass.
22000606d7eSSimon Glass  * @flags: driver flags - see DM_FLAGS_...
2216494d708SSimon Glass  */
2226494d708SSimon Glass struct driver {
2236494d708SSimon Glass 	char *name;
2246494d708SSimon Glass 	enum uclass_id id;
225ae7f4513SSimon Glass 	const struct udevice_id *of_match;
22654c5d08aSHeiko Schocher 	int (*bind)(struct udevice *dev);
22754c5d08aSHeiko Schocher 	int (*probe)(struct udevice *dev);
22854c5d08aSHeiko Schocher 	int (*remove)(struct udevice *dev);
22954c5d08aSHeiko Schocher 	int (*unbind)(struct udevice *dev);
23054c5d08aSHeiko Schocher 	int (*ofdata_to_platdata)(struct udevice *dev);
2310118ce79SSimon Glass 	int (*child_post_bind)(struct udevice *dev);
232a327dee0SSimon Glass 	int (*child_pre_probe)(struct udevice *dev);
233a327dee0SSimon Glass 	int (*child_post_remove)(struct udevice *dev);
2346494d708SSimon Glass 	int priv_auto_alloc_size;
2356494d708SSimon Glass 	int platdata_auto_alloc_size;
236e59f458dSSimon Glass 	int per_child_auto_alloc_size;
237cdc133bdSSimon Glass 	int per_child_platdata_auto_alloc_size;
2386494d708SSimon Glass 	const void *ops;	/* driver-specific operations */
23900606d7eSSimon Glass 	uint32_t flags;
2406494d708SSimon Glass };
2416494d708SSimon Glass 
2426494d708SSimon Glass /* Declare a new U-Boot driver */
2436494d708SSimon Glass #define U_BOOT_DRIVER(__name)						\
2446494d708SSimon Glass 	ll_entry_declare(struct driver, __name, driver)
2456494d708SSimon Glass 
246c57f806bSSimon Glass /* Get a pointer to a given driver */
247c57f806bSSimon Glass #define DM_GET_DRIVER(__name)						\
248c57f806bSSimon Glass 	ll_entry_get(struct driver, __name, driver)
249c57f806bSSimon Glass 
2506494d708SSimon Glass /**
2516494d708SSimon Glass  * dev_get_platdata() - Get the platform data for a device
2526494d708SSimon Glass  *
2536494d708SSimon Glass  * This checks that dev is not NULL, but no other checks for now
2546494d708SSimon Glass  *
2556494d708SSimon Glass  * @dev		Device to check
2566494d708SSimon Glass  * @return platform data, or NULL if none
2576494d708SSimon Glass  */
25854c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev);
2596494d708SSimon Glass 
2606494d708SSimon Glass /**
261cdc133bdSSimon Glass  * dev_get_parent_platdata() - Get the parent platform data for a device
262cdc133bdSSimon Glass  *
263cdc133bdSSimon Glass  * This checks that dev is not NULL, but no other checks for now
264cdc133bdSSimon Glass  *
265cdc133bdSSimon Glass  * @dev		Device to check
266cdc133bdSSimon Glass  * @return parent's platform data, or NULL if none
267cdc133bdSSimon Glass  */
268cdc133bdSSimon Glass void *dev_get_parent_platdata(struct udevice *dev);
269cdc133bdSSimon Glass 
270cdc133bdSSimon Glass /**
2715eaed880SPrzemyslaw Marczak  * dev_get_uclass_platdata() - Get the uclass platform data for a device
2725eaed880SPrzemyslaw Marczak  *
2735eaed880SPrzemyslaw Marczak  * This checks that dev is not NULL, but no other checks for now
2745eaed880SPrzemyslaw Marczak  *
2755eaed880SPrzemyslaw Marczak  * @dev		Device to check
2765eaed880SPrzemyslaw Marczak  * @return uclass's platform data, or NULL if none
2775eaed880SPrzemyslaw Marczak  */
2785eaed880SPrzemyslaw Marczak void *dev_get_uclass_platdata(struct udevice *dev);
2795eaed880SPrzemyslaw Marczak 
2805eaed880SPrzemyslaw Marczak /**
2819a79f6e6SSimon Glass  * dev_get_priv() - Get the private data for a device
2829a79f6e6SSimon Glass  *
2839a79f6e6SSimon Glass  * This checks that dev is not NULL, but no other checks for now
2849a79f6e6SSimon Glass  *
2859a79f6e6SSimon Glass  * @dev		Device to check
2869a79f6e6SSimon Glass  * @return private data, or NULL if none
2879a79f6e6SSimon Glass  */
2889a79f6e6SSimon Glass void *dev_get_priv(struct udevice *dev);
2899a79f6e6SSimon Glass 
2909a79f6e6SSimon Glass /**
291bcbe3d15SSimon Glass  * dev_get_parent_priv() - Get the parent private data for a device
292e59f458dSSimon Glass  *
293bcbe3d15SSimon Glass  * The parent private data is data stored in the device but owned by the
294bcbe3d15SSimon Glass  * parent. For example, a USB device may have parent data which contains
295bcbe3d15SSimon Glass  * information about how to talk to the device over USB.
296e59f458dSSimon Glass  *
297e59f458dSSimon Glass  * This checks that dev is not NULL, but no other checks for now
298e59f458dSSimon Glass  *
299e59f458dSSimon Glass  * @dev		Device to check
300e59f458dSSimon Glass  * @return parent data, or NULL if none
301e59f458dSSimon Glass  */
302bcbe3d15SSimon Glass void *dev_get_parent_priv(struct udevice *dev);
303e59f458dSSimon Glass 
304e59f458dSSimon Glass /**
305e564f054SSimon Glass  * dev_get_uclass_priv() - Get the private uclass data for a device
306e564f054SSimon Glass  *
307e564f054SSimon Glass  * This checks that dev is not NULL, but no other checks for now
308e564f054SSimon Glass  *
309e564f054SSimon Glass  * @dev		Device to check
310e564f054SSimon Glass  * @return private uclass data for this device, or NULL if none
311e564f054SSimon Glass  */
312e564f054SSimon Glass void *dev_get_uclass_priv(struct udevice *dev);
313e564f054SSimon Glass 
314e564f054SSimon Glass /**
3159a79f6e6SSimon Glass  * struct dev_get_parent() - Get the parent of a device
3169a79f6e6SSimon Glass  *
3179a79f6e6SSimon Glass  * @child:	Child to check
3189a79f6e6SSimon Glass  * @return parent of child, or NULL if this is the root device
3199a79f6e6SSimon Glass  */
3209a79f6e6SSimon Glass struct udevice *dev_get_parent(struct udevice *child);
3219a79f6e6SSimon Glass 
3229a79f6e6SSimon Glass /**
32339de8433SSimon Glass  * dev_get_driver_data() - get the driver data used to bind a device
3242ef249b4SSimon Glass  *
3252ef249b4SSimon Glass  * When a device is bound using a device tree node, it matches a
3268d1f3a9dSSimon Glass  * particular compatible string in struct udevice_id. This function
32739de8433SSimon Glass  * returns the associated data value for that compatible string. This is
32839de8433SSimon Glass  * the 'data' field in struct udevice_id.
32939de8433SSimon Glass  *
3308d1f3a9dSSimon Glass  * As an example, consider this structure:
3318d1f3a9dSSimon Glass  * static const struct udevice_id tegra_i2c_ids[] = {
3328d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
3338d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
3348d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
3358d1f3a9dSSimon Glass  *	{ }
3368d1f3a9dSSimon Glass  * };
3378d1f3a9dSSimon Glass  *
3388d1f3a9dSSimon Glass  * When driver model finds a driver for this it will store the 'data' value
3398d1f3a9dSSimon Glass  * corresponding to the compatible string it matches. This function returns
3408d1f3a9dSSimon Glass  * that value. This allows the driver to handle several variants of a device.
3418d1f3a9dSSimon Glass  *
34239de8433SSimon Glass  * For USB devices, this is the driver_info field in struct usb_device_id.
34339de8433SSimon Glass  *
34439de8433SSimon Glass  * @dev:	Device to check
3458d1f3a9dSSimon Glass  * @return driver data (0 if none is provided)
3462ef249b4SSimon Glass  */
34739de8433SSimon Glass ulong dev_get_driver_data(struct udevice *dev);
3482ef249b4SSimon Glass 
349cc73d37bSPrzemyslaw Marczak /**
350cc73d37bSPrzemyslaw Marczak  * dev_get_driver_ops() - get the device's driver's operations
351cc73d37bSPrzemyslaw Marczak  *
352cc73d37bSPrzemyslaw Marczak  * This checks that dev is not NULL, and returns the pointer to device's
353cc73d37bSPrzemyslaw Marczak  * driver's operations.
354cc73d37bSPrzemyslaw Marczak  *
355cc73d37bSPrzemyslaw Marczak  * @dev:	Device to check
356cc73d37bSPrzemyslaw Marczak  * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
357cc73d37bSPrzemyslaw Marczak  */
358cc73d37bSPrzemyslaw Marczak const void *dev_get_driver_ops(struct udevice *dev);
359cc73d37bSPrzemyslaw Marczak 
3608d1f3a9dSSimon Glass /**
361b3670531SSimon Glass  * device_get_uclass_id() - return the uclass ID of a device
362b3670531SSimon Glass  *
363b3670531SSimon Glass  * @dev:	Device to check
364b3670531SSimon Glass  * @return uclass ID for the device
365b3670531SSimon Glass  */
366b3670531SSimon Glass enum uclass_id device_get_uclass_id(struct udevice *dev);
367b3670531SSimon Glass 
3688d1f3a9dSSimon Glass /**
369f9c370dcSPrzemyslaw Marczak  * dev_get_uclass_name() - return the uclass name of a device
370f9c370dcSPrzemyslaw Marczak  *
371f9c370dcSPrzemyslaw Marczak  * This checks that dev is not NULL.
372f9c370dcSPrzemyslaw Marczak  *
373f9c370dcSPrzemyslaw Marczak  * @dev:	Device to check
374f9c370dcSPrzemyslaw Marczak  * @return  pointer to the uclass name for the device
375f9c370dcSPrzemyslaw Marczak  */
376f9c370dcSPrzemyslaw Marczak const char *dev_get_uclass_name(struct udevice *dev);
377f9c370dcSPrzemyslaw Marczak 
3782ef249b4SSimon Glass /**
379997c87bbSSimon Glass  * device_get_child() - Get the child of a device by index
380997c87bbSSimon Glass  *
381997c87bbSSimon Glass  * Returns the numbered child, 0 being the first. This does not use
382997c87bbSSimon Glass  * sequence numbers, only the natural order.
383997c87bbSSimon Glass  *
384997c87bbSSimon Glass  * @dev:	Parent device to check
385997c87bbSSimon Glass  * @index:	Child index
386997c87bbSSimon Glass  * @devp:	Returns pointer to device
3873f416f33SSimon Glass  * @return 0 if OK, -ENODEV if no such device, other error if the device fails
3883f416f33SSimon Glass  *	   to probe
389997c87bbSSimon Glass  */
390997c87bbSSimon Glass int device_get_child(struct udevice *parent, int index, struct udevice **devp);
391997c87bbSSimon Glass 
392997c87bbSSimon Glass /**
3935a66a8ffSSimon Glass  * device_find_child_by_seq() - Find a child device based on a sequence
3945a66a8ffSSimon Glass  *
3955a66a8ffSSimon Glass  * This searches for a device with the given seq or req_seq.
3965a66a8ffSSimon Glass  *
3975a66a8ffSSimon Glass  * For seq, if an active device has this sequence it will be returned.
3985a66a8ffSSimon Glass  * If there is no such device then this will return -ENODEV.
3995a66a8ffSSimon Glass  *
4005a66a8ffSSimon Glass  * For req_seq, if a device (whether activated or not) has this req_seq
4015a66a8ffSSimon Glass  * value, that device will be returned. This is a strong indication that
4025a66a8ffSSimon Glass  * the device will receive that sequence when activated.
4035a66a8ffSSimon Glass  *
4045a66a8ffSSimon Glass  * @parent: Parent device
4055a66a8ffSSimon Glass  * @seq_or_req_seq: Sequence number to find (0=first)
4065a66a8ffSSimon Glass  * @find_req_seq: true to find req_seq, false to find seq
4075a66a8ffSSimon Glass  * @devp: Returns pointer to device (there is only one per for each seq).
4085a66a8ffSSimon Glass  * Set to NULL if none is found
4095a66a8ffSSimon Glass  * @return 0 if OK, -ve on error
4105a66a8ffSSimon Glass  */
4115a66a8ffSSimon Glass int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
4125a66a8ffSSimon Glass 			     bool find_req_seq, struct udevice **devp);
4135a66a8ffSSimon Glass 
414997c87bbSSimon Glass /**
415997c87bbSSimon Glass  * device_get_child_by_seq() - Get a child device based on a sequence
416997c87bbSSimon Glass  *
417997c87bbSSimon Glass  * If an active device has this sequence it will be returned. If there is no
418997c87bbSSimon Glass  * such device then this will check for a device that is requesting this
419997c87bbSSimon Glass  * sequence.
420997c87bbSSimon Glass  *
421997c87bbSSimon Glass  * The device is probed to activate it ready for use.
422997c87bbSSimon Glass  *
423997c87bbSSimon Glass  * @parent: Parent device
424997c87bbSSimon Glass  * @seq: Sequence number to find (0=first)
425997c87bbSSimon Glass  * @devp: Returns pointer to device (there is only one per for each seq)
426997c87bbSSimon Glass  * Set to NULL if none is found
427997c87bbSSimon Glass  * @return 0 if OK, -ve on error
428997c87bbSSimon Glass  */
429997c87bbSSimon Glass int device_get_child_by_seq(struct udevice *parent, int seq,
430997c87bbSSimon Glass 			    struct udevice **devp);
431997c87bbSSimon Glass 
432997c87bbSSimon Glass /**
433997c87bbSSimon Glass  * device_find_child_by_of_offset() - Find a child device based on FDT offset
434997c87bbSSimon Glass  *
435997c87bbSSimon Glass  * Locates a child device by its device tree offset.
436997c87bbSSimon Glass  *
437997c87bbSSimon Glass  * @parent: Parent device
438997c87bbSSimon Glass  * @of_offset: Device tree offset to find
439997c87bbSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
440997c87bbSSimon Glass  * @return 0 if OK, -ve on error
441997c87bbSSimon Glass  */
442997c87bbSSimon Glass int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
443997c87bbSSimon Glass 				   struct udevice **devp);
444997c87bbSSimon Glass 
445997c87bbSSimon Glass /**
446997c87bbSSimon Glass  * device_get_child_by_of_offset() - Get a child device based on FDT offset
447997c87bbSSimon Glass  *
448997c87bbSSimon Glass  * Locates a child device by its device tree offset.
449997c87bbSSimon Glass  *
450997c87bbSSimon Glass  * The device is probed to activate it ready for use.
451997c87bbSSimon Glass  *
452997c87bbSSimon Glass  * @parent: Parent device
453997c87bbSSimon Glass  * @of_offset: Device tree offset to find
454997c87bbSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
455997c87bbSSimon Glass  * @return 0 if OK, -ve on error
456997c87bbSSimon Glass  */
457132f9bfcSSimon Glass int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
458997c87bbSSimon Glass 				  struct udevice **devp);
459997c87bbSSimon Glass 
460a8981d4fSSimon Glass /**
4612693047aSSimon Glass  * device_get_global_by_of_offset() - Get a device based on FDT offset
4622693047aSSimon Glass  *
4632693047aSSimon Glass  * Locates a device by its device tree offset, searching globally throughout
4642693047aSSimon Glass  * the all driver model devices.
4652693047aSSimon Glass  *
4662693047aSSimon Glass  * The device is probed to activate it ready for use.
4672693047aSSimon Glass  *
4682693047aSSimon Glass  * @of_offset: Device tree offset to find
4692693047aSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
4702693047aSSimon Glass  * @return 0 if OK, -ve on error
4712693047aSSimon Glass  */
4722693047aSSimon Glass int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
4732693047aSSimon Glass 
4742693047aSSimon Glass /**
475a8981d4fSSimon Glass  * device_find_first_child() - Find the first child of a device
476a8981d4fSSimon Glass  *
477a8981d4fSSimon Glass  * @parent: Parent device to search
478a8981d4fSSimon Glass  * @devp: Returns first child device, or NULL if none
479a8981d4fSSimon Glass  * @return 0
480a8981d4fSSimon Glass  */
481a8981d4fSSimon Glass int device_find_first_child(struct udevice *parent, struct udevice **devp);
482a8981d4fSSimon Glass 
483a8981d4fSSimon Glass /**
4843f416f33SSimon Glass  * device_find_next_child() - Find the next child of a device
485a8981d4fSSimon Glass  *
486a8981d4fSSimon Glass  * @devp: Pointer to previous child device on entry. Returns pointer to next
487a8981d4fSSimon Glass  *		child device, or NULL if none
488a8981d4fSSimon Glass  * @return 0
489a8981d4fSSimon Glass  */
490a8981d4fSSimon Glass int device_find_next_child(struct udevice **devp);
491a8981d4fSSimon Glass 
492c9cac3f8SPeng Fan /**
493c9cac3f8SPeng Fan  * dev_get_addr() - Get the reg property of a device
494c9cac3f8SPeng Fan  *
495c9cac3f8SPeng Fan  * @dev: Pointer to a device
496c9cac3f8SPeng Fan  *
497c9cac3f8SPeng Fan  * @return addr
498c9cac3f8SPeng Fan  */
499c9cac3f8SPeng Fan fdt_addr_t dev_get_addr(struct udevice *dev);
500c9cac3f8SPeng Fan 
501c5785673SSimon Glass /**
50228027521SStefan Roese  * dev_get_addr_ptr() - Return pointer to the address of the reg property
50328027521SStefan Roese  *                      of a device
50428027521SStefan Roese  *
50528027521SStefan Roese  * @dev: Pointer to a device
50628027521SStefan Roese  *
50728027521SStefan Roese  * @return Pointer to addr, or NULL if there is no such property
50828027521SStefan Roese  */
50928027521SStefan Roese void *dev_get_addr_ptr(struct udevice *dev);
51028027521SStefan Roese 
51128027521SStefan Roese /**
5127c616862SVignesh R  * dev_map_physmem() - Read device address from reg property of the
5137c616862SVignesh R  *                     device node and map the address into CPU address
5147c616862SVignesh R  *                     space.
5157c616862SVignesh R  *
5167c616862SVignesh R  * @dev: Pointer to device
5177c616862SVignesh R  * @size: size of the memory to map
5187c616862SVignesh R  *
5197c616862SVignesh R  * @return  mapped address, or NULL if the device does not have reg
5207c616862SVignesh R  *          property.
5217c616862SVignesh R  */
5227c616862SVignesh R void *dev_map_physmem(struct udevice *dev, unsigned long size);
5237c616862SVignesh R 
5247c616862SVignesh R /**
52569b41388SMugunthan V N  * dev_get_addr_index() - Get the indexed reg property of a device
52669b41388SMugunthan V N  *
52769b41388SMugunthan V N  * @dev: Pointer to a device
52869b41388SMugunthan V N  * @index: the 'reg' property can hold a list of <addr, size> pairs
52969b41388SMugunthan V N  *	   and @index is used to select which one is required
53069b41388SMugunthan V N  *
53169b41388SMugunthan V N  * @return addr
53269b41388SMugunthan V N  */
53369b41388SMugunthan V N fdt_addr_t dev_get_addr_index(struct udevice *dev, int index);
53469b41388SMugunthan V N 
53569b41388SMugunthan V N /**
53613f3fcacSStefan Roese  * dev_get_addr_size_index() - Get the indexed reg property of a device
53713f3fcacSStefan Roese  *
53813f3fcacSStefan Roese  * Returns the address and size specified in the 'reg' property of a device.
53913f3fcacSStefan Roese  *
54013f3fcacSStefan Roese  * @dev: Pointer to a device
54113f3fcacSStefan Roese  * @index: the 'reg' property can hold a list of <addr, size> pairs
54213f3fcacSStefan Roese  *	   and @index is used to select which one is required
54313f3fcacSStefan Roese  * @size: Pointer to size varible - this function returns the size
54413f3fcacSStefan Roese  *        specified in the 'reg' property here
54513f3fcacSStefan Roese  *
54613f3fcacSStefan Roese  * @return addr
54713f3fcacSStefan Roese  */
54813f3fcacSStefan Roese fdt_addr_t dev_get_addr_size_index(struct udevice *dev, int index,
54913f3fcacSStefan Roese 				   fdt_size_t *size);
55013f3fcacSStefan Roese 
55113f3fcacSStefan Roese /**
55243c4d44eSStephen Warren  * dev_get_addr_name() - Get the reg property of a device, indexed by name
55343c4d44eSStephen Warren  *
55443c4d44eSStephen Warren  * @dev: Pointer to a device
55543c4d44eSStephen Warren  * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
55643c4d44eSStephen Warren  *	  'reg-names' property providing named-based identification. @index
55743c4d44eSStephen Warren  *	  indicates the value to search for in 'reg-names'.
55843c4d44eSStephen Warren  *
55943c4d44eSStephen Warren  * @return addr
56043c4d44eSStephen Warren  */
56143c4d44eSStephen Warren fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name);
56243c4d44eSStephen Warren 
56343c4d44eSStephen Warren /**
564c5785673SSimon Glass  * device_has_children() - check if a device has any children
565c5785673SSimon Glass  *
566c5785673SSimon Glass  * @dev:	Device to check
567c5785673SSimon Glass  * @return true if the device has one or more children
568c5785673SSimon Glass  */
569c5785673SSimon Glass bool device_has_children(struct udevice *dev);
570c5785673SSimon Glass 
571c5785673SSimon Glass /**
572c5785673SSimon Glass  * device_has_active_children() - check if a device has any active children
573c5785673SSimon Glass  *
574c5785673SSimon Glass  * @dev:	Device to check
575c5785673SSimon Glass  * @return true if the device has one or more children and at least one of
576c5785673SSimon Glass  * them is active (probed).
577c5785673SSimon Glass  */
578c5785673SSimon Glass bool device_has_active_children(struct udevice *dev);
579c5785673SSimon Glass 
580c5785673SSimon Glass /**
581c5785673SSimon Glass  * device_is_last_sibling() - check if a device is the last sibling
582c5785673SSimon Glass  *
583c5785673SSimon Glass  * This function can be useful for display purposes, when special action needs
584c5785673SSimon Glass  * to be taken when displaying the last sibling. This can happen when a tree
585c5785673SSimon Glass  * view of devices is being displayed.
586c5785673SSimon Glass  *
587c5785673SSimon Glass  * @dev:	Device to check
588c5785673SSimon Glass  * @return true if there are no more siblings after this one - i.e. is it
589c5785673SSimon Glass  * last in the list.
590c5785673SSimon Glass  */
591c5785673SSimon Glass bool device_is_last_sibling(struct udevice *dev);
592c5785673SSimon Glass 
593f5c67ea0SSimon Glass /**
594f5c67ea0SSimon Glass  * device_set_name() - set the name of a device
595f5c67ea0SSimon Glass  *
596f5c67ea0SSimon Glass  * This must be called in the device's bind() method and no later. Normally
597f5c67ea0SSimon Glass  * this is unnecessary but for probed devices which don't get a useful name
598f5c67ea0SSimon Glass  * this function can be helpful.
599f5c67ea0SSimon Glass  *
600a2040facSSimon Glass  * The name is allocated and will be freed automatically when the device is
601a2040facSSimon Glass  * unbound.
602a2040facSSimon Glass  *
603f5c67ea0SSimon Glass  * @dev:	Device to update
604f5c67ea0SSimon Glass  * @name:	New name (this string is allocated new memory and attached to
605f5c67ea0SSimon Glass  *		the device)
606f5c67ea0SSimon Glass  * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
607f5c67ea0SSimon Glass  * string
608f5c67ea0SSimon Glass  */
609f5c67ea0SSimon Glass int device_set_name(struct udevice *dev, const char *name);
610f5c67ea0SSimon Glass 
6111e0f2263SBin Meng /**
612a2040facSSimon Glass  * device_set_name_alloced() - note that a device name is allocated
613a2040facSSimon Glass  *
614fd1c2d9bSSimon Glass  * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
615a2040facSSimon Glass  * unbound the name will be freed. This avoids memory leaks.
616a2040facSSimon Glass  *
617a2040facSSimon Glass  * @dev:	Device to update
618a2040facSSimon Glass  */
619a2040facSSimon Glass void device_set_name_alloced(struct udevice *dev);
620a2040facSSimon Glass 
621a2040facSSimon Glass /**
62273443b9eSMugunthan V N  * of_device_is_compatible() - check if the device is compatible with the compat
62373443b9eSMugunthan V N  *
62473443b9eSMugunthan V N  * This allows to check whether the device is comaptible with the compat.
62573443b9eSMugunthan V N  *
62673443b9eSMugunthan V N  * @dev:	udevice pointer for which compatible needs to be verified.
62773443b9eSMugunthan V N  * @compat:	Compatible string which needs to verified in the given
62873443b9eSMugunthan V N  *		device
62973443b9eSMugunthan V N  * @return true if OK, false if the compatible is not found
63073443b9eSMugunthan V N  */
63173443b9eSMugunthan V N bool of_device_is_compatible(struct udevice *dev, const char *compat);
63273443b9eSMugunthan V N 
63373443b9eSMugunthan V N /**
63473443b9eSMugunthan V N  * of_machine_is_compatible() - check if the machine is compatible with
63573443b9eSMugunthan V N  *				the compat
63673443b9eSMugunthan V N  *
63773443b9eSMugunthan V N  * This allows to check whether the machine is comaptible with the compat.
63873443b9eSMugunthan V N  *
63973443b9eSMugunthan V N  * @compat:	Compatible string which needs to verified
64073443b9eSMugunthan V N  * @return true if OK, false if the compatible is not found
64173443b9eSMugunthan V N  */
64273443b9eSMugunthan V N bool of_machine_is_compatible(const char *compat);
64373443b9eSMugunthan V N 
64473443b9eSMugunthan V N /**
6451e0f2263SBin Meng  * device_is_on_pci_bus - Test if a device is on a PCI bus
6461e0f2263SBin Meng  *
6471e0f2263SBin Meng  * @dev:	device to test
6481e0f2263SBin Meng  * @return:	true if it is on a PCI bus, false otherwise
6491e0f2263SBin Meng  */
6501e0f2263SBin Meng static inline bool device_is_on_pci_bus(struct udevice *dev)
6511e0f2263SBin Meng {
6521e0f2263SBin Meng 	return device_get_uclass_id(dev->parent) == UCLASS_PCI;
6531e0f2263SBin Meng }
6541e0f2263SBin Meng 
6557aeac5bcSSimon Glass /**
6567aeac5bcSSimon Glass  * device_foreach_child_safe() - iterate through child devices safely
6577aeac5bcSSimon Glass  *
6587aeac5bcSSimon Glass  * This allows the @pos child to be removed in the loop if required.
6597aeac5bcSSimon Glass  *
6607aeac5bcSSimon Glass  * @pos: struct udevice * for the current device
6617aeac5bcSSimon Glass  * @next: struct udevice * for the next device
6627aeac5bcSSimon Glass  * @parent: parent device to scan
6637aeac5bcSSimon Glass  */
6647aeac5bcSSimon Glass #define device_foreach_child_safe(pos, next, parent)	\
6657aeac5bcSSimon Glass 	list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
6667aeac5bcSSimon Glass 
667cc7f66f7SSimon Glass /**
668cc7f66f7SSimon Glass  * dm_scan_fdt_dev() - Bind child device in a the device tree
669cc7f66f7SSimon Glass  *
670cc7f66f7SSimon Glass  * This handles device which have sub-nodes in the device tree. It scans all
671cc7f66f7SSimon Glass  * sub-nodes and binds drivers for each node where a driver can be found.
672cc7f66f7SSimon Glass  *
673cc7f66f7SSimon Glass  * If this is called prior to relocation, only pre-relocation devices will be
674cc7f66f7SSimon Glass  * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
675cc7f66f7SSimon Glass  * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
676cc7f66f7SSimon Glass  * be bound.
677cc7f66f7SSimon Glass  *
678cc7f66f7SSimon Glass  * @dev:	Device to scan
679cc7f66f7SSimon Glass  * @return 0 if OK, -ve on error
680cc7f66f7SSimon Glass  */
681cc7f66f7SSimon Glass int dm_scan_fdt_dev(struct udevice *dev);
682cc7f66f7SSimon Glass 
683608f26c5SMasahiro Yamada /* device resource management */
684608f26c5SMasahiro Yamada typedef void (*dr_release_t)(struct udevice *dev, void *res);
685608f26c5SMasahiro Yamada typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
686608f26c5SMasahiro Yamada 
687e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES
688e2282d70SMasahiro Yamada 
689608f26c5SMasahiro Yamada #ifdef CONFIG_DEBUG_DEVRES
690608f26c5SMasahiro Yamada void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
691608f26c5SMasahiro Yamada 		     const char *name);
692608f26c5SMasahiro Yamada #define _devres_alloc(release, size, gfp) \
693608f26c5SMasahiro Yamada 	__devres_alloc(release, size, gfp, #release)
694608f26c5SMasahiro Yamada #else
695608f26c5SMasahiro Yamada void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
696608f26c5SMasahiro Yamada #endif
697608f26c5SMasahiro Yamada 
698608f26c5SMasahiro Yamada /**
69993c7fe4aSSimon Glass  * devres_alloc() - Allocate device resource data
700608f26c5SMasahiro Yamada  * @release: Release function devres will be associated with
701608f26c5SMasahiro Yamada  * @size: Allocation size
702608f26c5SMasahiro Yamada  * @gfp: Allocation flags
703608f26c5SMasahiro Yamada  *
704608f26c5SMasahiro Yamada  * Allocate devres of @size bytes.  The allocated area is associated
705608f26c5SMasahiro Yamada  * with @release.  The returned pointer can be passed to
706608f26c5SMasahiro Yamada  * other devres_*() functions.
707608f26c5SMasahiro Yamada  *
708608f26c5SMasahiro Yamada  * RETURNS:
709608f26c5SMasahiro Yamada  * Pointer to allocated devres on success, NULL on failure.
710608f26c5SMasahiro Yamada  */
711608f26c5SMasahiro Yamada #define devres_alloc(release, size, gfp) \
712608f26c5SMasahiro Yamada 	_devres_alloc(release, size, gfp | __GFP_ZERO)
713608f26c5SMasahiro Yamada 
714608f26c5SMasahiro Yamada /**
71593c7fe4aSSimon Glass  * devres_free() - Free device resource data
716608f26c5SMasahiro Yamada  * @res: Pointer to devres data to free
717608f26c5SMasahiro Yamada  *
718608f26c5SMasahiro Yamada  * Free devres created with devres_alloc().
719608f26c5SMasahiro Yamada  */
720608f26c5SMasahiro Yamada void devres_free(void *res);
721608f26c5SMasahiro Yamada 
722608f26c5SMasahiro Yamada /**
72393c7fe4aSSimon Glass  * devres_add() - Register device resource
724608f26c5SMasahiro Yamada  * @dev: Device to add resource to
725608f26c5SMasahiro Yamada  * @res: Resource to register
726608f26c5SMasahiro Yamada  *
727608f26c5SMasahiro Yamada  * Register devres @res to @dev.  @res should have been allocated
728608f26c5SMasahiro Yamada  * using devres_alloc().  On driver detach, the associated release
729608f26c5SMasahiro Yamada  * function will be invoked and devres will be freed automatically.
730608f26c5SMasahiro Yamada  */
731608f26c5SMasahiro Yamada void devres_add(struct udevice *dev, void *res);
732608f26c5SMasahiro Yamada 
733608f26c5SMasahiro Yamada /**
73493c7fe4aSSimon Glass  * devres_find() - Find device resource
735608f26c5SMasahiro Yamada  * @dev: Device to lookup resource from
736608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
737608f26c5SMasahiro Yamada  * @match: Match function (optional)
738608f26c5SMasahiro Yamada  * @match_data: Data for the match function
739608f26c5SMasahiro Yamada  *
740608f26c5SMasahiro Yamada  * Find the latest devres of @dev which is associated with @release
741608f26c5SMasahiro Yamada  * and for which @match returns 1.  If @match is NULL, it's considered
742608f26c5SMasahiro Yamada  * to match all.
743608f26c5SMasahiro Yamada  *
74493c7fe4aSSimon Glass  * @return pointer to found devres, NULL if not found.
745608f26c5SMasahiro Yamada  */
746608f26c5SMasahiro Yamada void *devres_find(struct udevice *dev, dr_release_t release,
747608f26c5SMasahiro Yamada 		  dr_match_t match, void *match_data);
748608f26c5SMasahiro Yamada 
749608f26c5SMasahiro Yamada /**
75093c7fe4aSSimon Glass  * devres_get() - Find devres, if non-existent, add one atomically
751608f26c5SMasahiro Yamada  * @dev: Device to lookup or add devres for
752608f26c5SMasahiro Yamada  * @new_res: Pointer to new initialized devres to add if not found
753608f26c5SMasahiro Yamada  * @match: Match function (optional)
754608f26c5SMasahiro Yamada  * @match_data: Data for the match function
755608f26c5SMasahiro Yamada  *
756608f26c5SMasahiro Yamada  * Find the latest devres of @dev which has the same release function
757608f26c5SMasahiro Yamada  * as @new_res and for which @match return 1.  If found, @new_res is
758608f26c5SMasahiro Yamada  * freed; otherwise, @new_res is added atomically.
759608f26c5SMasahiro Yamada  *
76093c7fe4aSSimon Glass  * @return ointer to found or added devres.
761608f26c5SMasahiro Yamada  */
762608f26c5SMasahiro Yamada void *devres_get(struct udevice *dev, void *new_res,
763608f26c5SMasahiro Yamada 		 dr_match_t match, void *match_data);
764608f26c5SMasahiro Yamada 
765608f26c5SMasahiro Yamada /**
76693c7fe4aSSimon Glass  * devres_remove() - Find a device resource and remove it
767608f26c5SMasahiro Yamada  * @dev: Device to find resource from
768608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
769608f26c5SMasahiro Yamada  * @match: Match function (optional)
770608f26c5SMasahiro Yamada  * @match_data: Data for the match function
771608f26c5SMasahiro Yamada  *
772608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
773608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
774608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically and
775608f26c5SMasahiro Yamada  * returned.
776608f26c5SMasahiro Yamada  *
77793c7fe4aSSimon Glass  * @return ointer to removed devres on success, NULL if not found.
778608f26c5SMasahiro Yamada  */
779608f26c5SMasahiro Yamada void *devres_remove(struct udevice *dev, dr_release_t release,
780608f26c5SMasahiro Yamada 		    dr_match_t match, void *match_data);
781608f26c5SMasahiro Yamada 
782608f26c5SMasahiro Yamada /**
78393c7fe4aSSimon Glass  * devres_destroy() - Find a device resource and destroy it
784608f26c5SMasahiro Yamada  * @dev: Device to find resource from
785608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
786608f26c5SMasahiro Yamada  * @match: Match function (optional)
787608f26c5SMasahiro Yamada  * @match_data: Data for the match function
788608f26c5SMasahiro Yamada  *
789608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
790608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
791608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically and freed.
792608f26c5SMasahiro Yamada  *
793608f26c5SMasahiro Yamada  * Note that the release function for the resource will not be called,
794608f26c5SMasahiro Yamada  * only the devres-allocated data will be freed.  The caller becomes
795608f26c5SMasahiro Yamada  * responsible for freeing any other data.
796608f26c5SMasahiro Yamada  *
79793c7fe4aSSimon Glass  * @return 0 if devres is found and freed, -ENOENT if not found.
798608f26c5SMasahiro Yamada  */
799608f26c5SMasahiro Yamada int devres_destroy(struct udevice *dev, dr_release_t release,
800608f26c5SMasahiro Yamada 		   dr_match_t match, void *match_data);
801608f26c5SMasahiro Yamada 
802608f26c5SMasahiro Yamada /**
80393c7fe4aSSimon Glass  * devres_release() - Find a device resource and destroy it, calling release
804608f26c5SMasahiro Yamada  * @dev: Device to find resource from
805608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
806608f26c5SMasahiro Yamada  * @match: Match function (optional)
807608f26c5SMasahiro Yamada  * @match_data: Data for the match function
808608f26c5SMasahiro Yamada  *
809608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
810608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
811608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically, the
812608f26c5SMasahiro Yamada  * release function called and the resource freed.
813608f26c5SMasahiro Yamada  *
81493c7fe4aSSimon Glass  * @return 0 if devres is found and freed, -ENOENT if not found.
815608f26c5SMasahiro Yamada  */
816608f26c5SMasahiro Yamada int devres_release(struct udevice *dev, dr_release_t release,
817608f26c5SMasahiro Yamada 		   dr_match_t match, void *match_data);
818608f26c5SMasahiro Yamada 
8192b07f685SMasahiro Yamada /* managed devm_k.alloc/kfree for device drivers */
8202b07f685SMasahiro Yamada /**
82193c7fe4aSSimon Glass  * devm_kmalloc() - Resource-managed kmalloc
8222b07f685SMasahiro Yamada  * @dev: Device to allocate memory for
8232b07f685SMasahiro Yamada  * @size: Allocation size
8242b07f685SMasahiro Yamada  * @gfp: Allocation gfp flags
8252b07f685SMasahiro Yamada  *
8262b07f685SMasahiro Yamada  * Managed kmalloc.  Memory allocated with this function is
8272b07f685SMasahiro Yamada  * automatically freed on driver detach.  Like all other devres
8282b07f685SMasahiro Yamada  * resources, guaranteed alignment is unsigned long long.
8292b07f685SMasahiro Yamada  *
83093c7fe4aSSimon Glass  * @return pointer to allocated memory on success, NULL on failure.
8312b07f685SMasahiro Yamada  */
8322b07f685SMasahiro Yamada void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
8332b07f685SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
8342b07f685SMasahiro Yamada {
8352b07f685SMasahiro Yamada 	return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
8362b07f685SMasahiro Yamada }
8372b07f685SMasahiro Yamada static inline void *devm_kmalloc_array(struct udevice *dev,
8382b07f685SMasahiro Yamada 				       size_t n, size_t size, gfp_t flags)
8392b07f685SMasahiro Yamada {
8402b07f685SMasahiro Yamada 	if (size != 0 && n > SIZE_MAX / size)
8412b07f685SMasahiro Yamada 		return NULL;
8422b07f685SMasahiro Yamada 	return devm_kmalloc(dev, n * size, flags);
8432b07f685SMasahiro Yamada }
8442b07f685SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev,
8452b07f685SMasahiro Yamada 				 size_t n, size_t size, gfp_t flags)
8462b07f685SMasahiro Yamada {
8472b07f685SMasahiro Yamada 	return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
8482b07f685SMasahiro Yamada }
8492b07f685SMasahiro Yamada 
8502b07f685SMasahiro Yamada /**
85193c7fe4aSSimon Glass  * devm_kfree() - Resource-managed kfree
8522b07f685SMasahiro Yamada  * @dev: Device this memory belongs to
85393c7fe4aSSimon Glass  * @ptr: Memory to free
8542b07f685SMasahiro Yamada  *
8552b07f685SMasahiro Yamada  * Free memory allocated with devm_kmalloc().
8562b07f685SMasahiro Yamada  */
85793c7fe4aSSimon Glass void devm_kfree(struct udevice *dev, void *ptr);
8582b07f685SMasahiro Yamada 
859e2282d70SMasahiro Yamada #else /* ! CONFIG_DEVRES */
860e2282d70SMasahiro Yamada 
861e2282d70SMasahiro Yamada static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
862e2282d70SMasahiro Yamada {
863e2282d70SMasahiro Yamada 	return kzalloc(size, gfp);
864e2282d70SMasahiro Yamada }
865e2282d70SMasahiro Yamada 
866e2282d70SMasahiro Yamada static inline void devres_free(void *res)
867e2282d70SMasahiro Yamada {
868e2282d70SMasahiro Yamada 	kfree(res);
869e2282d70SMasahiro Yamada }
870e2282d70SMasahiro Yamada 
871e2282d70SMasahiro Yamada static inline void devres_add(struct udevice *dev, void *res)
872e2282d70SMasahiro Yamada {
873e2282d70SMasahiro Yamada }
874e2282d70SMasahiro Yamada 
875e2282d70SMasahiro Yamada static inline void *devres_find(struct udevice *dev, dr_release_t release,
876e2282d70SMasahiro Yamada 				dr_match_t match, void *match_data)
877e2282d70SMasahiro Yamada {
878e2282d70SMasahiro Yamada 	return NULL;
879e2282d70SMasahiro Yamada }
880e2282d70SMasahiro Yamada 
881e2282d70SMasahiro Yamada static inline void *devres_get(struct udevice *dev, void *new_res,
882e2282d70SMasahiro Yamada 			       dr_match_t match, void *match_data)
883e2282d70SMasahiro Yamada {
884e2282d70SMasahiro Yamada 	return NULL;
885e2282d70SMasahiro Yamada }
886e2282d70SMasahiro Yamada 
887e2282d70SMasahiro Yamada static inline void *devres_remove(struct udevice *dev, dr_release_t release,
888e2282d70SMasahiro Yamada 				  dr_match_t match, void *match_data)
889e2282d70SMasahiro Yamada {
890e2282d70SMasahiro Yamada 	return NULL;
891e2282d70SMasahiro Yamada }
892e2282d70SMasahiro Yamada 
893e2282d70SMasahiro Yamada static inline int devres_destroy(struct udevice *dev, dr_release_t release,
894e2282d70SMasahiro Yamada 				 dr_match_t match, void *match_data)
895e2282d70SMasahiro Yamada {
896e2282d70SMasahiro Yamada 	return 0;
897e2282d70SMasahiro Yamada }
898e2282d70SMasahiro Yamada 
899e2282d70SMasahiro Yamada static inline int devres_release(struct udevice *dev, dr_release_t release,
900e2282d70SMasahiro Yamada 				 dr_match_t match, void *match_data)
901e2282d70SMasahiro Yamada {
902e2282d70SMasahiro Yamada 	return 0;
903e2282d70SMasahiro Yamada }
904e2282d70SMasahiro Yamada 
905e2282d70SMasahiro Yamada static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
906e2282d70SMasahiro Yamada {
907e2282d70SMasahiro Yamada 	return kmalloc(size, gfp);
908e2282d70SMasahiro Yamada }
909e2282d70SMasahiro Yamada 
910e2282d70SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
911e2282d70SMasahiro Yamada {
912e2282d70SMasahiro Yamada 	return kzalloc(size, gfp);
913e2282d70SMasahiro Yamada }
914e2282d70SMasahiro Yamada 
915e2282d70SMasahiro Yamada static inline void *devm_kmaloc_array(struct udevice *dev,
916e2282d70SMasahiro Yamada 				      size_t n, size_t size, gfp_t flags)
917e2282d70SMasahiro Yamada {
918e2282d70SMasahiro Yamada 	/* TODO: add kmalloc_array() to linux/compat.h */
919e2282d70SMasahiro Yamada 	if (size != 0 && n > SIZE_MAX / size)
920e2282d70SMasahiro Yamada 		return NULL;
921e2282d70SMasahiro Yamada 	return kmalloc(n * size, flags);
922e2282d70SMasahiro Yamada }
923e2282d70SMasahiro Yamada 
924e2282d70SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev,
925e2282d70SMasahiro Yamada 				 size_t n, size_t size, gfp_t flags)
926e2282d70SMasahiro Yamada {
927e2282d70SMasahiro Yamada 	/* TODO: add kcalloc() to linux/compat.h */
928e2282d70SMasahiro Yamada 	return kmalloc(n * size, flags | __GFP_ZERO);
929e2282d70SMasahiro Yamada }
930e2282d70SMasahiro Yamada 
93193c7fe4aSSimon Glass static inline void devm_kfree(struct udevice *dev, void *ptr)
932e2282d70SMasahiro Yamada {
93393c7fe4aSSimon Glass 	kfree(ptr);
934e2282d70SMasahiro Yamada }
935e2282d70SMasahiro Yamada 
936e2282d70SMasahiro Yamada #endif /* ! CONFIG_DEVRES */
9372b07f685SMasahiro Yamada 
93866eaea6cSStefan Roese /**
93966eaea6cSStefan Roese  * dm_set_translation_offset() - Set translation offset
94066eaea6cSStefan Roese  * @offs: Translation offset
94166eaea6cSStefan Roese  *
94266eaea6cSStefan Roese  * Some platforms need a special address translation. Those
94366eaea6cSStefan Roese  * platforms (e.g. mvebu in SPL) can configure a translation
94466eaea6cSStefan Roese  * offset in the DM by calling this function. It will be
94566eaea6cSStefan Roese  * added to all addresses returned in dev_get_addr().
94666eaea6cSStefan Roese  */
94766eaea6cSStefan Roese void dm_set_translation_offset(fdt_addr_t offs);
94866eaea6cSStefan Roese 
94966eaea6cSStefan Roese /**
95066eaea6cSStefan Roese  * dm_get_translation_offset() - Get translation offset
95166eaea6cSStefan Roese  *
95266eaea6cSStefan Roese  * This function returns the translation offset that can
95366eaea6cSStefan Roese  * be configured by calling dm_set_translation_offset().
95466eaea6cSStefan Roese  *
95566eaea6cSStefan Roese  * @return translation offset for the device address (0 as default).
95666eaea6cSStefan Roese  */
95766eaea6cSStefan Roese fdt_addr_t dm_get_translation_offset(void);
95866eaea6cSStefan Roese 
9596494d708SSimon Glass #endif
960