xref: /rk3399_rockchip-uboot/include/dm/device.h (revision e160f7d430f163bc42757aff3bf2bcac0a459f02)
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 
496494d708SSimon Glass /**
5054c5d08aSHeiko Schocher  * struct udevice - An instance of a driver
516494d708SSimon Glass  *
526494d708SSimon Glass  * This holds information about a device, which is a driver bound to a
536494d708SSimon Glass  * particular port or peripheral (essentially a driver instance).
546494d708SSimon Glass  *
556494d708SSimon Glass  * A device will come into existence through a 'bind' call, either due to
566494d708SSimon Glass  * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
576494d708SSimon Glass  * in the device tree (in which case of_offset is >= 0). In the latter case
586494d708SSimon Glass  * we translate the device tree information into platdata in a function
596494d708SSimon Glass  * implemented by the driver ofdata_to_platdata method (called just before the
606494d708SSimon Glass  * probe method if the device has a device tree node.
616494d708SSimon Glass  *
626494d708SSimon Glass  * All three of platdata, priv and uclass_priv can be allocated by the
636494d708SSimon Glass  * driver, or you can use the auto_alloc_size members of struct driver and
646494d708SSimon Glass  * struct uclass_driver to have driver model do this automatically.
656494d708SSimon Glass  *
666494d708SSimon Glass  * @driver: The driver used by this device
676494d708SSimon Glass  * @name: Name of device, typically the FDT node name
686494d708SSimon Glass  * @platdata: Configuration data for this device
69cdc133bdSSimon Glass  * @parent_platdata: The parent bus's configuration data for this device
705eaed880SPrzemyslaw Marczak  * @uclass_platdata: The uclass's configuration data for this device
716494d708SSimon Glass  * @of_offset: Device tree node offset for this device (- for none)
7239de8433SSimon Glass  * @driver_data: Driver data word for the entry that matched this device with
7339de8433SSimon Glass  *		its driver
746494d708SSimon Glass  * @parent: Parent of this device, or NULL for the top level device
756494d708SSimon Glass  * @priv: Private data for this device
766494d708SSimon Glass  * @uclass: Pointer to uclass for this device
776494d708SSimon Glass  * @uclass_priv: The uclass's private data for this device
78e59f458dSSimon Glass  * @parent_priv: The parent's private data for this device
796494d708SSimon Glass  * @uclass_node: Used by uclass to link its devices
806494d708SSimon Glass  * @child_head: List of children of this device
816494d708SSimon Glass  * @sibling_node: Next device in list of all devices
826494d708SSimon Glass  * @flags: Flags for this device DM_FLAG_...
835a66a8ffSSimon Glass  * @req_seq: Requested sequence number for this device (-1 = any)
84547cea19SSimon Glass  * @seq: Allocated sequence number for this device (-1 = none). This is set up
85547cea19SSimon Glass  * when the device is probed and will be unique within the device's uclass.
8693c7fe4aSSimon Glass  * @devres_head: List of memory allocations associated with this device.
8793c7fe4aSSimon Glass  *		When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
8893c7fe4aSSimon Glass  *		add to this list. Memory so-allocated will be freed
8993c7fe4aSSimon Glass  *		automatically when the device is removed / unbound
906494d708SSimon Glass  */
9154c5d08aSHeiko Schocher struct udevice {
923479253dSSimon Glass 	const struct driver *driver;
936494d708SSimon Glass 	const char *name;
946494d708SSimon Glass 	void *platdata;
95cdc133bdSSimon Glass 	void *parent_platdata;
965eaed880SPrzemyslaw Marczak 	void *uclass_platdata;
976494d708SSimon Glass 	int of_offset;
9839de8433SSimon Glass 	ulong driver_data;
9954c5d08aSHeiko Schocher 	struct udevice *parent;
1006494d708SSimon Glass 	void *priv;
1016494d708SSimon Glass 	struct uclass *uclass;
1026494d708SSimon Glass 	void *uclass_priv;
103e59f458dSSimon Glass 	void *parent_priv;
1046494d708SSimon Glass 	struct list_head uclass_node;
1056494d708SSimon Glass 	struct list_head child_head;
1066494d708SSimon Glass 	struct list_head sibling_node;
1076494d708SSimon Glass 	uint32_t flags;
1085a66a8ffSSimon Glass 	int req_seq;
1095a66a8ffSSimon Glass 	int seq;
110e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES
111608f26c5SMasahiro Yamada 	struct list_head devres_head;
112e2282d70SMasahiro Yamada #endif
1136494d708SSimon Glass };
1146494d708SSimon Glass 
1155a66a8ffSSimon Glass /* Maximum sequence number supported */
1165a66a8ffSSimon Glass #define DM_MAX_SEQ	999
1175a66a8ffSSimon Glass 
1186494d708SSimon Glass /* Returns the operations for a device */
1196494d708SSimon Glass #define device_get_ops(dev)	(dev->driver->ops)
1206494d708SSimon Glass 
1216494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */
1226494d708SSimon Glass #define device_active(dev)	((dev)->flags & DM_FLAG_ACTIVATED)
1236494d708SSimon Glass 
124*e160f7d4SSimon Glass static inline int dev_of_offset(const struct udevice *dev)
125*e160f7d4SSimon Glass {
126*e160f7d4SSimon Glass 	return dev->of_offset;
127*e160f7d4SSimon Glass }
128*e160f7d4SSimon Glass 
129*e160f7d4SSimon Glass static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
130*e160f7d4SSimon Glass {
131*e160f7d4SSimon Glass 	dev->of_offset = of_offset;
132*e160f7d4SSimon Glass }
133*e160f7d4SSimon Glass 
1346494d708SSimon Glass /**
135ae7f4513SSimon Glass  * struct udevice_id - Lists the compatible strings supported by a driver
1366494d708SSimon Glass  * @compatible: Compatible string
1376494d708SSimon Glass  * @data: Data for this compatible string
1386494d708SSimon Glass  */
139ae7f4513SSimon Glass struct udevice_id {
1406494d708SSimon Glass 	const char *compatible;
1416494d708SSimon Glass 	ulong data;
1426494d708SSimon Glass };
1436494d708SSimon Glass 
1440f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
145f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr)	(_ptr)
146f887cb6dSMasahiro Yamada #else
147f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr)	NULL
1480f925822SMasahiro Yamada #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
149f887cb6dSMasahiro Yamada 
1506494d708SSimon Glass /**
1516494d708SSimon Glass  * struct driver - A driver for a feature or peripheral
1526494d708SSimon Glass  *
1536494d708SSimon Glass  * This holds methods for setting up a new device, and also removing it.
1546494d708SSimon Glass  * The device needs information to set itself up - this is provided either
1556494d708SSimon Glass  * by platdata or a device tree node (which we find by looking up
1566494d708SSimon Glass  * matching compatible strings with of_match).
1576494d708SSimon Glass  *
1586494d708SSimon Glass  * Drivers all belong to a uclass, representing a class of devices of the
1596494d708SSimon Glass  * same type. Common elements of the drivers can be implemented in the uclass,
1606494d708SSimon Glass  * or the uclass can provide a consistent interface to the drivers within
1616494d708SSimon Glass  * it.
1626494d708SSimon Glass  *
1636494d708SSimon Glass  * @name: Device name
1646494d708SSimon Glass  * @id: Identiies the uclass we belong to
1656494d708SSimon Glass  * @of_match: List of compatible strings to match, and any identifying data
1666494d708SSimon Glass  * for each.
1676494d708SSimon Glass  * @bind: Called to bind a device to its driver
1686494d708SSimon Glass  * @probe: Called to probe a device, i.e. activate it
1696494d708SSimon Glass  * @remove: Called to remove a device, i.e. de-activate it
1706494d708SSimon Glass  * @unbind: Called to unbind a device from its driver
1716494d708SSimon Glass  * @ofdata_to_platdata: Called before probe to decode device tree data
1720118ce79SSimon Glass  * @child_post_bind: Called after a new child has been bound
173a327dee0SSimon Glass  * @child_pre_probe: Called before a child device is probed. The device has
174a327dee0SSimon Glass  * memory allocated but it has not yet been probed.
175a327dee0SSimon Glass  * @child_post_remove: Called after a child device is removed. The device
176a327dee0SSimon Glass  * has memory allocated but its device_remove() method has been called.
1776494d708SSimon Glass  * @priv_auto_alloc_size: If non-zero this is the size of the private data
1786494d708SSimon Glass  * to be allocated in the device's ->priv pointer. If zero, then the driver
1796494d708SSimon Glass  * is responsible for allocating any data required.
1806494d708SSimon Glass  * @platdata_auto_alloc_size: If non-zero this is the size of the
1816494d708SSimon Glass  * platform data to be allocated in the device's ->platdata pointer.
1826494d708SSimon Glass  * This is typically only useful for device-tree-aware drivers (those with
1836494d708SSimon Glass  * an of_match), since drivers which use platdata will have the data
1846494d708SSimon Glass  * provided in the U_BOOT_DEVICE() instantiation.
185e59f458dSSimon Glass  * @per_child_auto_alloc_size: Each device can hold private data owned by
186e59f458dSSimon Glass  * its parent. If required this will be automatically allocated if this
187e59f458dSSimon Glass  * value is non-zero.
188cdc133bdSSimon Glass  * @per_child_platdata_auto_alloc_size: A bus likes to store information about
189cdc133bdSSimon Glass  * its children. If non-zero this is the size of this data, to be allocated
190cdc133bdSSimon Glass  * in the child's parent_platdata pointer.
1910040b944SSimon Glass  * @ops: Driver-specific operations. This is typically a list of function
1926494d708SSimon Glass  * pointers defined by the driver, to implement driver functions required by
1936494d708SSimon Glass  * the uclass.
19400606d7eSSimon Glass  * @flags: driver flags - see DM_FLAGS_...
1956494d708SSimon Glass  */
1966494d708SSimon Glass struct driver {
1976494d708SSimon Glass 	char *name;
1986494d708SSimon Glass 	enum uclass_id id;
199ae7f4513SSimon Glass 	const struct udevice_id *of_match;
20054c5d08aSHeiko Schocher 	int (*bind)(struct udevice *dev);
20154c5d08aSHeiko Schocher 	int (*probe)(struct udevice *dev);
20254c5d08aSHeiko Schocher 	int (*remove)(struct udevice *dev);
20354c5d08aSHeiko Schocher 	int (*unbind)(struct udevice *dev);
20454c5d08aSHeiko Schocher 	int (*ofdata_to_platdata)(struct udevice *dev);
2050118ce79SSimon Glass 	int (*child_post_bind)(struct udevice *dev);
206a327dee0SSimon Glass 	int (*child_pre_probe)(struct udevice *dev);
207a327dee0SSimon Glass 	int (*child_post_remove)(struct udevice *dev);
2086494d708SSimon Glass 	int priv_auto_alloc_size;
2096494d708SSimon Glass 	int platdata_auto_alloc_size;
210e59f458dSSimon Glass 	int per_child_auto_alloc_size;
211cdc133bdSSimon Glass 	int per_child_platdata_auto_alloc_size;
2126494d708SSimon Glass 	const void *ops;	/* driver-specific operations */
21300606d7eSSimon Glass 	uint32_t flags;
2146494d708SSimon Glass };
2156494d708SSimon Glass 
2166494d708SSimon Glass /* Declare a new U-Boot driver */
2176494d708SSimon Glass #define U_BOOT_DRIVER(__name)						\
2186494d708SSimon Glass 	ll_entry_declare(struct driver, __name, driver)
2196494d708SSimon Glass 
220c57f806bSSimon Glass /* Get a pointer to a given driver */
221c57f806bSSimon Glass #define DM_GET_DRIVER(__name)						\
222c57f806bSSimon Glass 	ll_entry_get(struct driver, __name, driver)
223c57f806bSSimon Glass 
2246494d708SSimon Glass /**
2256494d708SSimon Glass  * dev_get_platdata() - Get the platform data for a device
2266494d708SSimon Glass  *
2276494d708SSimon Glass  * This checks that dev is not NULL, but no other checks for now
2286494d708SSimon Glass  *
2296494d708SSimon Glass  * @dev		Device to check
2306494d708SSimon Glass  * @return platform data, or NULL if none
2316494d708SSimon Glass  */
23254c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev);
2336494d708SSimon Glass 
2346494d708SSimon Glass /**
235cdc133bdSSimon Glass  * dev_get_parent_platdata() - Get the parent platform data for a device
236cdc133bdSSimon Glass  *
237cdc133bdSSimon Glass  * This checks that dev is not NULL, but no other checks for now
238cdc133bdSSimon Glass  *
239cdc133bdSSimon Glass  * @dev		Device to check
240cdc133bdSSimon Glass  * @return parent's platform data, or NULL if none
241cdc133bdSSimon Glass  */
242cdc133bdSSimon Glass void *dev_get_parent_platdata(struct udevice *dev);
243cdc133bdSSimon Glass 
244cdc133bdSSimon Glass /**
2455eaed880SPrzemyslaw Marczak  * dev_get_uclass_platdata() - Get the uclass platform data for a device
2465eaed880SPrzemyslaw Marczak  *
2475eaed880SPrzemyslaw Marczak  * This checks that dev is not NULL, but no other checks for now
2485eaed880SPrzemyslaw Marczak  *
2495eaed880SPrzemyslaw Marczak  * @dev		Device to check
2505eaed880SPrzemyslaw Marczak  * @return uclass's platform data, or NULL if none
2515eaed880SPrzemyslaw Marczak  */
2525eaed880SPrzemyslaw Marczak void *dev_get_uclass_platdata(struct udevice *dev);
2535eaed880SPrzemyslaw Marczak 
2545eaed880SPrzemyslaw Marczak /**
2559a79f6e6SSimon Glass  * dev_get_priv() - Get the private data for a device
2569a79f6e6SSimon Glass  *
2579a79f6e6SSimon Glass  * This checks that dev is not NULL, but no other checks for now
2589a79f6e6SSimon Glass  *
2599a79f6e6SSimon Glass  * @dev		Device to check
2609a79f6e6SSimon Glass  * @return private data, or NULL if none
2619a79f6e6SSimon Glass  */
2629a79f6e6SSimon Glass void *dev_get_priv(struct udevice *dev);
2639a79f6e6SSimon Glass 
2649a79f6e6SSimon Glass /**
265bcbe3d15SSimon Glass  * dev_get_parent_priv() - Get the parent private data for a device
266e59f458dSSimon Glass  *
267bcbe3d15SSimon Glass  * The parent private data is data stored in the device but owned by the
268bcbe3d15SSimon Glass  * parent. For example, a USB device may have parent data which contains
269bcbe3d15SSimon Glass  * information about how to talk to the device over USB.
270e59f458dSSimon Glass  *
271e59f458dSSimon Glass  * This checks that dev is not NULL, but no other checks for now
272e59f458dSSimon Glass  *
273e59f458dSSimon Glass  * @dev		Device to check
274e59f458dSSimon Glass  * @return parent data, or NULL if none
275e59f458dSSimon Glass  */
276bcbe3d15SSimon Glass void *dev_get_parent_priv(struct udevice *dev);
277e59f458dSSimon Glass 
278e59f458dSSimon Glass /**
279e564f054SSimon Glass  * dev_get_uclass_priv() - Get the private uclass data for a device
280e564f054SSimon Glass  *
281e564f054SSimon Glass  * This checks that dev is not NULL, but no other checks for now
282e564f054SSimon Glass  *
283e564f054SSimon Glass  * @dev		Device to check
284e564f054SSimon Glass  * @return private uclass data for this device, or NULL if none
285e564f054SSimon Glass  */
286e564f054SSimon Glass void *dev_get_uclass_priv(struct udevice *dev);
287e564f054SSimon Glass 
288e564f054SSimon Glass /**
2899a79f6e6SSimon Glass  * struct dev_get_parent() - Get the parent of a device
2909a79f6e6SSimon Glass  *
2919a79f6e6SSimon Glass  * @child:	Child to check
2929a79f6e6SSimon Glass  * @return parent of child, or NULL if this is the root device
2939a79f6e6SSimon Glass  */
2949a79f6e6SSimon Glass struct udevice *dev_get_parent(struct udevice *child);
2959a79f6e6SSimon Glass 
2969a79f6e6SSimon Glass /**
29739de8433SSimon Glass  * dev_get_driver_data() - get the driver data used to bind a device
2982ef249b4SSimon Glass  *
2992ef249b4SSimon Glass  * When a device is bound using a device tree node, it matches a
3008d1f3a9dSSimon Glass  * particular compatible string in struct udevice_id. This function
30139de8433SSimon Glass  * returns the associated data value for that compatible string. This is
30239de8433SSimon Glass  * the 'data' field in struct udevice_id.
30339de8433SSimon Glass  *
3048d1f3a9dSSimon Glass  * As an example, consider this structure:
3058d1f3a9dSSimon Glass  * static const struct udevice_id tegra_i2c_ids[] = {
3068d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
3078d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
3088d1f3a9dSSimon Glass  *	{ .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
3098d1f3a9dSSimon Glass  *	{ }
3108d1f3a9dSSimon Glass  * };
3118d1f3a9dSSimon Glass  *
3128d1f3a9dSSimon Glass  * When driver model finds a driver for this it will store the 'data' value
3138d1f3a9dSSimon Glass  * corresponding to the compatible string it matches. This function returns
3148d1f3a9dSSimon Glass  * that value. This allows the driver to handle several variants of a device.
3158d1f3a9dSSimon Glass  *
31639de8433SSimon Glass  * For USB devices, this is the driver_info field in struct usb_device_id.
31739de8433SSimon Glass  *
31839de8433SSimon Glass  * @dev:	Device to check
3198d1f3a9dSSimon Glass  * @return driver data (0 if none is provided)
3202ef249b4SSimon Glass  */
32139de8433SSimon Glass ulong dev_get_driver_data(struct udevice *dev);
3222ef249b4SSimon Glass 
323cc73d37bSPrzemyslaw Marczak /**
324cc73d37bSPrzemyslaw Marczak  * dev_get_driver_ops() - get the device's driver's operations
325cc73d37bSPrzemyslaw Marczak  *
326cc73d37bSPrzemyslaw Marczak  * This checks that dev is not NULL, and returns the pointer to device's
327cc73d37bSPrzemyslaw Marczak  * driver's operations.
328cc73d37bSPrzemyslaw Marczak  *
329cc73d37bSPrzemyslaw Marczak  * @dev:	Device to check
330cc73d37bSPrzemyslaw Marczak  * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
331cc73d37bSPrzemyslaw Marczak  */
332cc73d37bSPrzemyslaw Marczak const void *dev_get_driver_ops(struct udevice *dev);
333cc73d37bSPrzemyslaw Marczak 
3348d1f3a9dSSimon Glass /**
335b3670531SSimon Glass  * device_get_uclass_id() - return the uclass ID of a device
336b3670531SSimon Glass  *
337b3670531SSimon Glass  * @dev:	Device to check
338b3670531SSimon Glass  * @return uclass ID for the device
339b3670531SSimon Glass  */
340b3670531SSimon Glass enum uclass_id device_get_uclass_id(struct udevice *dev);
341b3670531SSimon Glass 
3428d1f3a9dSSimon Glass /**
343f9c370dcSPrzemyslaw Marczak  * dev_get_uclass_name() - return the uclass name of a device
344f9c370dcSPrzemyslaw Marczak  *
345f9c370dcSPrzemyslaw Marczak  * This checks that dev is not NULL.
346f9c370dcSPrzemyslaw Marczak  *
347f9c370dcSPrzemyslaw Marczak  * @dev:	Device to check
348f9c370dcSPrzemyslaw Marczak  * @return  pointer to the uclass name for the device
349f9c370dcSPrzemyslaw Marczak  */
350f9c370dcSPrzemyslaw Marczak const char *dev_get_uclass_name(struct udevice *dev);
351f9c370dcSPrzemyslaw Marczak 
3522ef249b4SSimon Glass /**
353997c87bbSSimon Glass  * device_get_child() - Get the child of a device by index
354997c87bbSSimon Glass  *
355997c87bbSSimon Glass  * Returns the numbered child, 0 being the first. This does not use
356997c87bbSSimon Glass  * sequence numbers, only the natural order.
357997c87bbSSimon Glass  *
358997c87bbSSimon Glass  * @dev:	Parent device to check
359997c87bbSSimon Glass  * @index:	Child index
360997c87bbSSimon Glass  * @devp:	Returns pointer to device
3613f416f33SSimon Glass  * @return 0 if OK, -ENODEV if no such device, other error if the device fails
3623f416f33SSimon Glass  *	   to probe
363997c87bbSSimon Glass  */
364997c87bbSSimon Glass int device_get_child(struct udevice *parent, int index, struct udevice **devp);
365997c87bbSSimon Glass 
366997c87bbSSimon Glass /**
3675a66a8ffSSimon Glass  * device_find_child_by_seq() - Find a child device based on a sequence
3685a66a8ffSSimon Glass  *
3695a66a8ffSSimon Glass  * This searches for a device with the given seq or req_seq.
3705a66a8ffSSimon Glass  *
3715a66a8ffSSimon Glass  * For seq, if an active device has this sequence it will be returned.
3725a66a8ffSSimon Glass  * If there is no such device then this will return -ENODEV.
3735a66a8ffSSimon Glass  *
3745a66a8ffSSimon Glass  * For req_seq, if a device (whether activated or not) has this req_seq
3755a66a8ffSSimon Glass  * value, that device will be returned. This is a strong indication that
3765a66a8ffSSimon Glass  * the device will receive that sequence when activated.
3775a66a8ffSSimon Glass  *
3785a66a8ffSSimon Glass  * @parent: Parent device
3795a66a8ffSSimon Glass  * @seq_or_req_seq: Sequence number to find (0=first)
3805a66a8ffSSimon Glass  * @find_req_seq: true to find req_seq, false to find seq
3815a66a8ffSSimon Glass  * @devp: Returns pointer to device (there is only one per for each seq).
3825a66a8ffSSimon Glass  * Set to NULL if none is found
3835a66a8ffSSimon Glass  * @return 0 if OK, -ve on error
3845a66a8ffSSimon Glass  */
3855a66a8ffSSimon Glass int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
3865a66a8ffSSimon Glass 			     bool find_req_seq, struct udevice **devp);
3875a66a8ffSSimon Glass 
388997c87bbSSimon Glass /**
389997c87bbSSimon Glass  * device_get_child_by_seq() - Get a child device based on a sequence
390997c87bbSSimon Glass  *
391997c87bbSSimon Glass  * If an active device has this sequence it will be returned. If there is no
392997c87bbSSimon Glass  * such device then this will check for a device that is requesting this
393997c87bbSSimon Glass  * sequence.
394997c87bbSSimon Glass  *
395997c87bbSSimon Glass  * The device is probed to activate it ready for use.
396997c87bbSSimon Glass  *
397997c87bbSSimon Glass  * @parent: Parent device
398997c87bbSSimon Glass  * @seq: Sequence number to find (0=first)
399997c87bbSSimon Glass  * @devp: Returns pointer to device (there is only one per for each seq)
400997c87bbSSimon Glass  * Set to NULL if none is found
401997c87bbSSimon Glass  * @return 0 if OK, -ve on error
402997c87bbSSimon Glass  */
403997c87bbSSimon Glass int device_get_child_by_seq(struct udevice *parent, int seq,
404997c87bbSSimon Glass 			    struct udevice **devp);
405997c87bbSSimon Glass 
406997c87bbSSimon Glass /**
407997c87bbSSimon Glass  * device_find_child_by_of_offset() - Find a child device based on FDT offset
408997c87bbSSimon Glass  *
409997c87bbSSimon Glass  * Locates a child device by its device tree offset.
410997c87bbSSimon Glass  *
411997c87bbSSimon Glass  * @parent: Parent device
412997c87bbSSimon Glass  * @of_offset: Device tree offset to find
413997c87bbSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
414997c87bbSSimon Glass  * @return 0 if OK, -ve on error
415997c87bbSSimon Glass  */
416997c87bbSSimon Glass int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
417997c87bbSSimon Glass 				   struct udevice **devp);
418997c87bbSSimon Glass 
419997c87bbSSimon Glass /**
420997c87bbSSimon Glass  * device_get_child_by_of_offset() - Get a child device based on FDT offset
421997c87bbSSimon Glass  *
422997c87bbSSimon Glass  * Locates a child device by its device tree offset.
423997c87bbSSimon Glass  *
424997c87bbSSimon Glass  * The device is probed to activate it ready for use.
425997c87bbSSimon Glass  *
426997c87bbSSimon Glass  * @parent: Parent device
427997c87bbSSimon Glass  * @of_offset: Device tree offset to find
428997c87bbSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
429997c87bbSSimon Glass  * @return 0 if OK, -ve on error
430997c87bbSSimon Glass  */
431132f9bfcSSimon Glass int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
432997c87bbSSimon Glass 				  struct udevice **devp);
433997c87bbSSimon Glass 
434a8981d4fSSimon Glass /**
4352693047aSSimon Glass  * device_get_global_by_of_offset() - Get a device based on FDT offset
4362693047aSSimon Glass  *
4372693047aSSimon Glass  * Locates a device by its device tree offset, searching globally throughout
4382693047aSSimon Glass  * the all driver model devices.
4392693047aSSimon Glass  *
4402693047aSSimon Glass  * The device is probed to activate it ready for use.
4412693047aSSimon Glass  *
4422693047aSSimon Glass  * @of_offset: Device tree offset to find
4432693047aSSimon Glass  * @devp: Returns pointer to device if found, otherwise this is set to NULL
4442693047aSSimon Glass  * @return 0 if OK, -ve on error
4452693047aSSimon Glass  */
4462693047aSSimon Glass int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
4472693047aSSimon Glass 
4482693047aSSimon Glass /**
449a8981d4fSSimon Glass  * device_find_first_child() - Find the first child of a device
450a8981d4fSSimon Glass  *
451a8981d4fSSimon Glass  * @parent: Parent device to search
452a8981d4fSSimon Glass  * @devp: Returns first child device, or NULL if none
453a8981d4fSSimon Glass  * @return 0
454a8981d4fSSimon Glass  */
455a8981d4fSSimon Glass int device_find_first_child(struct udevice *parent, struct udevice **devp);
456a8981d4fSSimon Glass 
457a8981d4fSSimon Glass /**
4583f416f33SSimon Glass  * device_find_next_child() - Find the next child of a device
459a8981d4fSSimon Glass  *
460a8981d4fSSimon Glass  * @devp: Pointer to previous child device on entry. Returns pointer to next
461a8981d4fSSimon Glass  *		child device, or NULL if none
462a8981d4fSSimon Glass  * @return 0
463a8981d4fSSimon Glass  */
464a8981d4fSSimon Glass int device_find_next_child(struct udevice **devp);
465a8981d4fSSimon Glass 
466c9cac3f8SPeng Fan /**
467c9cac3f8SPeng Fan  * dev_get_addr() - Get the reg property of a device
468c9cac3f8SPeng Fan  *
469c9cac3f8SPeng Fan  * @dev: Pointer to a device
470c9cac3f8SPeng Fan  *
471c9cac3f8SPeng Fan  * @return addr
472c9cac3f8SPeng Fan  */
473c9cac3f8SPeng Fan fdt_addr_t dev_get_addr(struct udevice *dev);
474c9cac3f8SPeng Fan 
475c5785673SSimon Glass /**
47628027521SStefan Roese  * dev_get_addr_ptr() - Return pointer to the address of the reg property
47728027521SStefan Roese  *                      of a device
47828027521SStefan Roese  *
47928027521SStefan Roese  * @dev: Pointer to a device
48028027521SStefan Roese  *
48128027521SStefan Roese  * @return Pointer to addr, or NULL if there is no such property
48228027521SStefan Roese  */
48328027521SStefan Roese void *dev_get_addr_ptr(struct udevice *dev);
48428027521SStefan Roese 
48528027521SStefan Roese /**
4867c616862SVignesh R  * dev_map_physmem() - Read device address from reg property of the
4877c616862SVignesh R  *                     device node and map the address into CPU address
4887c616862SVignesh R  *                     space.
4897c616862SVignesh R  *
4907c616862SVignesh R  * @dev: Pointer to device
4917c616862SVignesh R  * @size: size of the memory to map
4927c616862SVignesh R  *
4937c616862SVignesh R  * @return  mapped address, or NULL if the device does not have reg
4947c616862SVignesh R  *          property.
4957c616862SVignesh R  */
4967c616862SVignesh R void *dev_map_physmem(struct udevice *dev, unsigned long size);
4977c616862SVignesh R 
4987c616862SVignesh R /**
49969b41388SMugunthan V N  * dev_get_addr_index() - Get the indexed reg property of a device
50069b41388SMugunthan V N  *
50169b41388SMugunthan V N  * @dev: Pointer to a device
50269b41388SMugunthan V N  * @index: the 'reg' property can hold a list of <addr, size> pairs
50369b41388SMugunthan V N  *	   and @index is used to select which one is required
50469b41388SMugunthan V N  *
50569b41388SMugunthan V N  * @return addr
50669b41388SMugunthan V N  */
50769b41388SMugunthan V N fdt_addr_t dev_get_addr_index(struct udevice *dev, int index);
50869b41388SMugunthan V N 
50969b41388SMugunthan V N /**
51013f3fcacSStefan Roese  * dev_get_addr_size_index() - Get the indexed reg property of a device
51113f3fcacSStefan Roese  *
51213f3fcacSStefan Roese  * Returns the address and size specified in the 'reg' property of a device.
51313f3fcacSStefan Roese  *
51413f3fcacSStefan Roese  * @dev: Pointer to a device
51513f3fcacSStefan Roese  * @index: the 'reg' property can hold a list of <addr, size> pairs
51613f3fcacSStefan Roese  *	   and @index is used to select which one is required
51713f3fcacSStefan Roese  * @size: Pointer to size varible - this function returns the size
51813f3fcacSStefan Roese  *        specified in the 'reg' property here
51913f3fcacSStefan Roese  *
52013f3fcacSStefan Roese  * @return addr
52113f3fcacSStefan Roese  */
52213f3fcacSStefan Roese fdt_addr_t dev_get_addr_size_index(struct udevice *dev, int index,
52313f3fcacSStefan Roese 				   fdt_size_t *size);
52413f3fcacSStefan Roese 
52513f3fcacSStefan Roese /**
52643c4d44eSStephen Warren  * dev_get_addr_name() - Get the reg property of a device, indexed by name
52743c4d44eSStephen Warren  *
52843c4d44eSStephen Warren  * @dev: Pointer to a device
52943c4d44eSStephen Warren  * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
53043c4d44eSStephen Warren  *	  'reg-names' property providing named-based identification. @index
53143c4d44eSStephen Warren  *	  indicates the value to search for in 'reg-names'.
53243c4d44eSStephen Warren  *
53343c4d44eSStephen Warren  * @return addr
53443c4d44eSStephen Warren  */
53543c4d44eSStephen Warren fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name);
53643c4d44eSStephen Warren 
53743c4d44eSStephen Warren /**
538c5785673SSimon Glass  * device_has_children() - check if a device has any children
539c5785673SSimon Glass  *
540c5785673SSimon Glass  * @dev:	Device to check
541c5785673SSimon Glass  * @return true if the device has one or more children
542c5785673SSimon Glass  */
543c5785673SSimon Glass bool device_has_children(struct udevice *dev);
544c5785673SSimon Glass 
545c5785673SSimon Glass /**
546c5785673SSimon Glass  * device_has_active_children() - check if a device has any active children
547c5785673SSimon Glass  *
548c5785673SSimon Glass  * @dev:	Device to check
549c5785673SSimon Glass  * @return true if the device has one or more children and at least one of
550c5785673SSimon Glass  * them is active (probed).
551c5785673SSimon Glass  */
552c5785673SSimon Glass bool device_has_active_children(struct udevice *dev);
553c5785673SSimon Glass 
554c5785673SSimon Glass /**
555c5785673SSimon Glass  * device_is_last_sibling() - check if a device is the last sibling
556c5785673SSimon Glass  *
557c5785673SSimon Glass  * This function can be useful for display purposes, when special action needs
558c5785673SSimon Glass  * to be taken when displaying the last sibling. This can happen when a tree
559c5785673SSimon Glass  * view of devices is being displayed.
560c5785673SSimon Glass  *
561c5785673SSimon Glass  * @dev:	Device to check
562c5785673SSimon Glass  * @return true if there are no more siblings after this one - i.e. is it
563c5785673SSimon Glass  * last in the list.
564c5785673SSimon Glass  */
565c5785673SSimon Glass bool device_is_last_sibling(struct udevice *dev);
566c5785673SSimon Glass 
567f5c67ea0SSimon Glass /**
568f5c67ea0SSimon Glass  * device_set_name() - set the name of a device
569f5c67ea0SSimon Glass  *
570f5c67ea0SSimon Glass  * This must be called in the device's bind() method and no later. Normally
571f5c67ea0SSimon Glass  * this is unnecessary but for probed devices which don't get a useful name
572f5c67ea0SSimon Glass  * this function can be helpful.
573f5c67ea0SSimon Glass  *
574a2040facSSimon Glass  * The name is allocated and will be freed automatically when the device is
575a2040facSSimon Glass  * unbound.
576a2040facSSimon Glass  *
577f5c67ea0SSimon Glass  * @dev:	Device to update
578f5c67ea0SSimon Glass  * @name:	New name (this string is allocated new memory and attached to
579f5c67ea0SSimon Glass  *		the device)
580f5c67ea0SSimon Glass  * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
581f5c67ea0SSimon Glass  * string
582f5c67ea0SSimon Glass  */
583f5c67ea0SSimon Glass int device_set_name(struct udevice *dev, const char *name);
584f5c67ea0SSimon Glass 
5851e0f2263SBin Meng /**
586a2040facSSimon Glass  * device_set_name_alloced() - note that a device name is allocated
587a2040facSSimon Glass  *
588fd1c2d9bSSimon Glass  * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
589a2040facSSimon Glass  * unbound the name will be freed. This avoids memory leaks.
590a2040facSSimon Glass  *
591a2040facSSimon Glass  * @dev:	Device to update
592a2040facSSimon Glass  */
593a2040facSSimon Glass void device_set_name_alloced(struct udevice *dev);
594a2040facSSimon Glass 
595a2040facSSimon Glass /**
59673443b9eSMugunthan V N  * of_device_is_compatible() - check if the device is compatible with the compat
59773443b9eSMugunthan V N  *
59873443b9eSMugunthan V N  * This allows to check whether the device is comaptible with the compat.
59973443b9eSMugunthan V N  *
60073443b9eSMugunthan V N  * @dev:	udevice pointer for which compatible needs to be verified.
60173443b9eSMugunthan V N  * @compat:	Compatible string which needs to verified in the given
60273443b9eSMugunthan V N  *		device
60373443b9eSMugunthan V N  * @return true if OK, false if the compatible is not found
60473443b9eSMugunthan V N  */
60573443b9eSMugunthan V N bool of_device_is_compatible(struct udevice *dev, const char *compat);
60673443b9eSMugunthan V N 
60773443b9eSMugunthan V N /**
60873443b9eSMugunthan V N  * of_machine_is_compatible() - check if the machine is compatible with
60973443b9eSMugunthan V N  *				the compat
61073443b9eSMugunthan V N  *
61173443b9eSMugunthan V N  * This allows to check whether the machine is comaptible with the compat.
61273443b9eSMugunthan V N  *
61373443b9eSMugunthan V N  * @compat:	Compatible string which needs to verified
61473443b9eSMugunthan V N  * @return true if OK, false if the compatible is not found
61573443b9eSMugunthan V N  */
61673443b9eSMugunthan V N bool of_machine_is_compatible(const char *compat);
61773443b9eSMugunthan V N 
61873443b9eSMugunthan V N /**
6191e0f2263SBin Meng  * device_is_on_pci_bus - Test if a device is on a PCI bus
6201e0f2263SBin Meng  *
6211e0f2263SBin Meng  * @dev:	device to test
6221e0f2263SBin Meng  * @return:	true if it is on a PCI bus, false otherwise
6231e0f2263SBin Meng  */
6241e0f2263SBin Meng static inline bool device_is_on_pci_bus(struct udevice *dev)
6251e0f2263SBin Meng {
6261e0f2263SBin Meng 	return device_get_uclass_id(dev->parent) == UCLASS_PCI;
6271e0f2263SBin Meng }
6281e0f2263SBin Meng 
6297aeac5bcSSimon Glass /**
6307aeac5bcSSimon Glass  * device_foreach_child_safe() - iterate through child devices safely
6317aeac5bcSSimon Glass  *
6327aeac5bcSSimon Glass  * This allows the @pos child to be removed in the loop if required.
6337aeac5bcSSimon Glass  *
6347aeac5bcSSimon Glass  * @pos: struct udevice * for the current device
6357aeac5bcSSimon Glass  * @next: struct udevice * for the next device
6367aeac5bcSSimon Glass  * @parent: parent device to scan
6377aeac5bcSSimon Glass  */
6387aeac5bcSSimon Glass #define device_foreach_child_safe(pos, next, parent)	\
6397aeac5bcSSimon Glass 	list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
6407aeac5bcSSimon Glass 
641cc7f66f7SSimon Glass /**
642cc7f66f7SSimon Glass  * dm_scan_fdt_dev() - Bind child device in a the device tree
643cc7f66f7SSimon Glass  *
644cc7f66f7SSimon Glass  * This handles device which have sub-nodes in the device tree. It scans all
645cc7f66f7SSimon Glass  * sub-nodes and binds drivers for each node where a driver can be found.
646cc7f66f7SSimon Glass  *
647cc7f66f7SSimon Glass  * If this is called prior to relocation, only pre-relocation devices will be
648cc7f66f7SSimon Glass  * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
649cc7f66f7SSimon Glass  * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
650cc7f66f7SSimon Glass  * be bound.
651cc7f66f7SSimon Glass  *
652cc7f66f7SSimon Glass  * @dev:	Device to scan
653cc7f66f7SSimon Glass  * @return 0 if OK, -ve on error
654cc7f66f7SSimon Glass  */
655cc7f66f7SSimon Glass int dm_scan_fdt_dev(struct udevice *dev);
656cc7f66f7SSimon Glass 
657608f26c5SMasahiro Yamada /* device resource management */
658608f26c5SMasahiro Yamada typedef void (*dr_release_t)(struct udevice *dev, void *res);
659608f26c5SMasahiro Yamada typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
660608f26c5SMasahiro Yamada 
661e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES
662e2282d70SMasahiro Yamada 
663608f26c5SMasahiro Yamada #ifdef CONFIG_DEBUG_DEVRES
664608f26c5SMasahiro Yamada void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
665608f26c5SMasahiro Yamada 		     const char *name);
666608f26c5SMasahiro Yamada #define _devres_alloc(release, size, gfp) \
667608f26c5SMasahiro Yamada 	__devres_alloc(release, size, gfp, #release)
668608f26c5SMasahiro Yamada #else
669608f26c5SMasahiro Yamada void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
670608f26c5SMasahiro Yamada #endif
671608f26c5SMasahiro Yamada 
672608f26c5SMasahiro Yamada /**
67393c7fe4aSSimon Glass  * devres_alloc() - Allocate device resource data
674608f26c5SMasahiro Yamada  * @release: Release function devres will be associated with
675608f26c5SMasahiro Yamada  * @size: Allocation size
676608f26c5SMasahiro Yamada  * @gfp: Allocation flags
677608f26c5SMasahiro Yamada  *
678608f26c5SMasahiro Yamada  * Allocate devres of @size bytes.  The allocated area is associated
679608f26c5SMasahiro Yamada  * with @release.  The returned pointer can be passed to
680608f26c5SMasahiro Yamada  * other devres_*() functions.
681608f26c5SMasahiro Yamada  *
682608f26c5SMasahiro Yamada  * RETURNS:
683608f26c5SMasahiro Yamada  * Pointer to allocated devres on success, NULL on failure.
684608f26c5SMasahiro Yamada  */
685608f26c5SMasahiro Yamada #define devres_alloc(release, size, gfp) \
686608f26c5SMasahiro Yamada 	_devres_alloc(release, size, gfp | __GFP_ZERO)
687608f26c5SMasahiro Yamada 
688608f26c5SMasahiro Yamada /**
68993c7fe4aSSimon Glass  * devres_free() - Free device resource data
690608f26c5SMasahiro Yamada  * @res: Pointer to devres data to free
691608f26c5SMasahiro Yamada  *
692608f26c5SMasahiro Yamada  * Free devres created with devres_alloc().
693608f26c5SMasahiro Yamada  */
694608f26c5SMasahiro Yamada void devres_free(void *res);
695608f26c5SMasahiro Yamada 
696608f26c5SMasahiro Yamada /**
69793c7fe4aSSimon Glass  * devres_add() - Register device resource
698608f26c5SMasahiro Yamada  * @dev: Device to add resource to
699608f26c5SMasahiro Yamada  * @res: Resource to register
700608f26c5SMasahiro Yamada  *
701608f26c5SMasahiro Yamada  * Register devres @res to @dev.  @res should have been allocated
702608f26c5SMasahiro Yamada  * using devres_alloc().  On driver detach, the associated release
703608f26c5SMasahiro Yamada  * function will be invoked and devres will be freed automatically.
704608f26c5SMasahiro Yamada  */
705608f26c5SMasahiro Yamada void devres_add(struct udevice *dev, void *res);
706608f26c5SMasahiro Yamada 
707608f26c5SMasahiro Yamada /**
70893c7fe4aSSimon Glass  * devres_find() - Find device resource
709608f26c5SMasahiro Yamada  * @dev: Device to lookup resource from
710608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
711608f26c5SMasahiro Yamada  * @match: Match function (optional)
712608f26c5SMasahiro Yamada  * @match_data: Data for the match function
713608f26c5SMasahiro Yamada  *
714608f26c5SMasahiro Yamada  * Find the latest devres of @dev which is associated with @release
715608f26c5SMasahiro Yamada  * and for which @match returns 1.  If @match is NULL, it's considered
716608f26c5SMasahiro Yamada  * to match all.
717608f26c5SMasahiro Yamada  *
71893c7fe4aSSimon Glass  * @return pointer to found devres, NULL if not found.
719608f26c5SMasahiro Yamada  */
720608f26c5SMasahiro Yamada void *devres_find(struct udevice *dev, dr_release_t release,
721608f26c5SMasahiro Yamada 		  dr_match_t match, void *match_data);
722608f26c5SMasahiro Yamada 
723608f26c5SMasahiro Yamada /**
72493c7fe4aSSimon Glass  * devres_get() - Find devres, if non-existent, add one atomically
725608f26c5SMasahiro Yamada  * @dev: Device to lookup or add devres for
726608f26c5SMasahiro Yamada  * @new_res: Pointer to new initialized devres to add if not found
727608f26c5SMasahiro Yamada  * @match: Match function (optional)
728608f26c5SMasahiro Yamada  * @match_data: Data for the match function
729608f26c5SMasahiro Yamada  *
730608f26c5SMasahiro Yamada  * Find the latest devres of @dev which has the same release function
731608f26c5SMasahiro Yamada  * as @new_res and for which @match return 1.  If found, @new_res is
732608f26c5SMasahiro Yamada  * freed; otherwise, @new_res is added atomically.
733608f26c5SMasahiro Yamada  *
73493c7fe4aSSimon Glass  * @return ointer to found or added devres.
735608f26c5SMasahiro Yamada  */
736608f26c5SMasahiro Yamada void *devres_get(struct udevice *dev, void *new_res,
737608f26c5SMasahiro Yamada 		 dr_match_t match, void *match_data);
738608f26c5SMasahiro Yamada 
739608f26c5SMasahiro Yamada /**
74093c7fe4aSSimon Glass  * devres_remove() - Find a device resource and remove it
741608f26c5SMasahiro Yamada  * @dev: Device to find resource from
742608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
743608f26c5SMasahiro Yamada  * @match: Match function (optional)
744608f26c5SMasahiro Yamada  * @match_data: Data for the match function
745608f26c5SMasahiro Yamada  *
746608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
747608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
748608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically and
749608f26c5SMasahiro Yamada  * returned.
750608f26c5SMasahiro Yamada  *
75193c7fe4aSSimon Glass  * @return ointer to removed devres on success, NULL if not found.
752608f26c5SMasahiro Yamada  */
753608f26c5SMasahiro Yamada void *devres_remove(struct udevice *dev, dr_release_t release,
754608f26c5SMasahiro Yamada 		    dr_match_t match, void *match_data);
755608f26c5SMasahiro Yamada 
756608f26c5SMasahiro Yamada /**
75793c7fe4aSSimon Glass  * devres_destroy() - Find a device resource and destroy it
758608f26c5SMasahiro Yamada  * @dev: Device to find resource from
759608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
760608f26c5SMasahiro Yamada  * @match: Match function (optional)
761608f26c5SMasahiro Yamada  * @match_data: Data for the match function
762608f26c5SMasahiro Yamada  *
763608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
764608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
765608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically and freed.
766608f26c5SMasahiro Yamada  *
767608f26c5SMasahiro Yamada  * Note that the release function for the resource will not be called,
768608f26c5SMasahiro Yamada  * only the devres-allocated data will be freed.  The caller becomes
769608f26c5SMasahiro Yamada  * responsible for freeing any other data.
770608f26c5SMasahiro Yamada  *
77193c7fe4aSSimon Glass  * @return 0 if devres is found and freed, -ENOENT if not found.
772608f26c5SMasahiro Yamada  */
773608f26c5SMasahiro Yamada int devres_destroy(struct udevice *dev, dr_release_t release,
774608f26c5SMasahiro Yamada 		   dr_match_t match, void *match_data);
775608f26c5SMasahiro Yamada 
776608f26c5SMasahiro Yamada /**
77793c7fe4aSSimon Glass  * devres_release() - Find a device resource and destroy it, calling release
778608f26c5SMasahiro Yamada  * @dev: Device to find resource from
779608f26c5SMasahiro Yamada  * @release: Look for resources associated with this release function
780608f26c5SMasahiro Yamada  * @match: Match function (optional)
781608f26c5SMasahiro Yamada  * @match_data: Data for the match function
782608f26c5SMasahiro Yamada  *
783608f26c5SMasahiro Yamada  * Find the latest devres of @dev associated with @release and for
784608f26c5SMasahiro Yamada  * which @match returns 1.  If @match is NULL, it's considered to
785608f26c5SMasahiro Yamada  * match all.  If found, the resource is removed atomically, the
786608f26c5SMasahiro Yamada  * release function called and the resource freed.
787608f26c5SMasahiro Yamada  *
78893c7fe4aSSimon Glass  * @return 0 if devres is found and freed, -ENOENT if not found.
789608f26c5SMasahiro Yamada  */
790608f26c5SMasahiro Yamada int devres_release(struct udevice *dev, dr_release_t release,
791608f26c5SMasahiro Yamada 		   dr_match_t match, void *match_data);
792608f26c5SMasahiro Yamada 
7932b07f685SMasahiro Yamada /* managed devm_k.alloc/kfree for device drivers */
7942b07f685SMasahiro Yamada /**
79593c7fe4aSSimon Glass  * devm_kmalloc() - Resource-managed kmalloc
7962b07f685SMasahiro Yamada  * @dev: Device to allocate memory for
7972b07f685SMasahiro Yamada  * @size: Allocation size
7982b07f685SMasahiro Yamada  * @gfp: Allocation gfp flags
7992b07f685SMasahiro Yamada  *
8002b07f685SMasahiro Yamada  * Managed kmalloc.  Memory allocated with this function is
8012b07f685SMasahiro Yamada  * automatically freed on driver detach.  Like all other devres
8022b07f685SMasahiro Yamada  * resources, guaranteed alignment is unsigned long long.
8032b07f685SMasahiro Yamada  *
80493c7fe4aSSimon Glass  * @return pointer to allocated memory on success, NULL on failure.
8052b07f685SMasahiro Yamada  */
8062b07f685SMasahiro Yamada void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
8072b07f685SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
8082b07f685SMasahiro Yamada {
8092b07f685SMasahiro Yamada 	return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
8102b07f685SMasahiro Yamada }
8112b07f685SMasahiro Yamada static inline void *devm_kmalloc_array(struct udevice *dev,
8122b07f685SMasahiro Yamada 				       size_t n, size_t size, gfp_t flags)
8132b07f685SMasahiro Yamada {
8142b07f685SMasahiro Yamada 	if (size != 0 && n > SIZE_MAX / size)
8152b07f685SMasahiro Yamada 		return NULL;
8162b07f685SMasahiro Yamada 	return devm_kmalloc(dev, n * size, flags);
8172b07f685SMasahiro Yamada }
8182b07f685SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev,
8192b07f685SMasahiro Yamada 				 size_t n, size_t size, gfp_t flags)
8202b07f685SMasahiro Yamada {
8212b07f685SMasahiro Yamada 	return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
8222b07f685SMasahiro Yamada }
8232b07f685SMasahiro Yamada 
8242b07f685SMasahiro Yamada /**
82593c7fe4aSSimon Glass  * devm_kfree() - Resource-managed kfree
8262b07f685SMasahiro Yamada  * @dev: Device this memory belongs to
82793c7fe4aSSimon Glass  * @ptr: Memory to free
8282b07f685SMasahiro Yamada  *
8292b07f685SMasahiro Yamada  * Free memory allocated with devm_kmalloc().
8302b07f685SMasahiro Yamada  */
83193c7fe4aSSimon Glass void devm_kfree(struct udevice *dev, void *ptr);
8322b07f685SMasahiro Yamada 
833e2282d70SMasahiro Yamada #else /* ! CONFIG_DEVRES */
834e2282d70SMasahiro Yamada 
835e2282d70SMasahiro Yamada static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
836e2282d70SMasahiro Yamada {
837e2282d70SMasahiro Yamada 	return kzalloc(size, gfp);
838e2282d70SMasahiro Yamada }
839e2282d70SMasahiro Yamada 
840e2282d70SMasahiro Yamada static inline void devres_free(void *res)
841e2282d70SMasahiro Yamada {
842e2282d70SMasahiro Yamada 	kfree(res);
843e2282d70SMasahiro Yamada }
844e2282d70SMasahiro Yamada 
845e2282d70SMasahiro Yamada static inline void devres_add(struct udevice *dev, void *res)
846e2282d70SMasahiro Yamada {
847e2282d70SMasahiro Yamada }
848e2282d70SMasahiro Yamada 
849e2282d70SMasahiro Yamada static inline void *devres_find(struct udevice *dev, dr_release_t release,
850e2282d70SMasahiro Yamada 				dr_match_t match, void *match_data)
851e2282d70SMasahiro Yamada {
852e2282d70SMasahiro Yamada 	return NULL;
853e2282d70SMasahiro Yamada }
854e2282d70SMasahiro Yamada 
855e2282d70SMasahiro Yamada static inline void *devres_get(struct udevice *dev, void *new_res,
856e2282d70SMasahiro Yamada 			       dr_match_t match, void *match_data)
857e2282d70SMasahiro Yamada {
858e2282d70SMasahiro Yamada 	return NULL;
859e2282d70SMasahiro Yamada }
860e2282d70SMasahiro Yamada 
861e2282d70SMasahiro Yamada static inline void *devres_remove(struct udevice *dev, dr_release_t release,
862e2282d70SMasahiro Yamada 				  dr_match_t match, void *match_data)
863e2282d70SMasahiro Yamada {
864e2282d70SMasahiro Yamada 	return NULL;
865e2282d70SMasahiro Yamada }
866e2282d70SMasahiro Yamada 
867e2282d70SMasahiro Yamada static inline int devres_destroy(struct udevice *dev, dr_release_t release,
868e2282d70SMasahiro Yamada 				 dr_match_t match, void *match_data)
869e2282d70SMasahiro Yamada {
870e2282d70SMasahiro Yamada 	return 0;
871e2282d70SMasahiro Yamada }
872e2282d70SMasahiro Yamada 
873e2282d70SMasahiro Yamada static inline int devres_release(struct udevice *dev, dr_release_t release,
874e2282d70SMasahiro Yamada 				 dr_match_t match, void *match_data)
875e2282d70SMasahiro Yamada {
876e2282d70SMasahiro Yamada 	return 0;
877e2282d70SMasahiro Yamada }
878e2282d70SMasahiro Yamada 
879e2282d70SMasahiro Yamada static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
880e2282d70SMasahiro Yamada {
881e2282d70SMasahiro Yamada 	return kmalloc(size, gfp);
882e2282d70SMasahiro Yamada }
883e2282d70SMasahiro Yamada 
884e2282d70SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
885e2282d70SMasahiro Yamada {
886e2282d70SMasahiro Yamada 	return kzalloc(size, gfp);
887e2282d70SMasahiro Yamada }
888e2282d70SMasahiro Yamada 
889e2282d70SMasahiro Yamada static inline void *devm_kmaloc_array(struct udevice *dev,
890e2282d70SMasahiro Yamada 				      size_t n, size_t size, gfp_t flags)
891e2282d70SMasahiro Yamada {
892e2282d70SMasahiro Yamada 	/* TODO: add kmalloc_array() to linux/compat.h */
893e2282d70SMasahiro Yamada 	if (size != 0 && n > SIZE_MAX / size)
894e2282d70SMasahiro Yamada 		return NULL;
895e2282d70SMasahiro Yamada 	return kmalloc(n * size, flags);
896e2282d70SMasahiro Yamada }
897e2282d70SMasahiro Yamada 
898e2282d70SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev,
899e2282d70SMasahiro Yamada 				 size_t n, size_t size, gfp_t flags)
900e2282d70SMasahiro Yamada {
901e2282d70SMasahiro Yamada 	/* TODO: add kcalloc() to linux/compat.h */
902e2282d70SMasahiro Yamada 	return kmalloc(n * size, flags | __GFP_ZERO);
903e2282d70SMasahiro Yamada }
904e2282d70SMasahiro Yamada 
90593c7fe4aSSimon Glass static inline void devm_kfree(struct udevice *dev, void *ptr)
906e2282d70SMasahiro Yamada {
90793c7fe4aSSimon Glass 	kfree(ptr);
908e2282d70SMasahiro Yamada }
909e2282d70SMasahiro Yamada 
910e2282d70SMasahiro Yamada #endif /* ! CONFIG_DEVRES */
9112b07f685SMasahiro Yamada 
91266eaea6cSStefan Roese /**
91366eaea6cSStefan Roese  * dm_set_translation_offset() - Set translation offset
91466eaea6cSStefan Roese  * @offs: Translation offset
91566eaea6cSStefan Roese  *
91666eaea6cSStefan Roese  * Some platforms need a special address translation. Those
91766eaea6cSStefan Roese  * platforms (e.g. mvebu in SPL) can configure a translation
91866eaea6cSStefan Roese  * offset in the DM by calling this function. It will be
91966eaea6cSStefan Roese  * added to all addresses returned in dev_get_addr().
92066eaea6cSStefan Roese  */
92166eaea6cSStefan Roese void dm_set_translation_offset(fdt_addr_t offs);
92266eaea6cSStefan Roese 
92366eaea6cSStefan Roese /**
92466eaea6cSStefan Roese  * dm_get_translation_offset() - Get translation offset
92566eaea6cSStefan Roese  *
92666eaea6cSStefan Roese  * This function returns the translation offset that can
92766eaea6cSStefan Roese  * be configured by calling dm_set_translation_offset().
92866eaea6cSStefan Roese  *
92966eaea6cSStefan Roese  * @return translation offset for the device address (0 as default).
93066eaea6cSStefan Roese  */
93166eaea6cSStefan Roese fdt_addr_t dm_get_translation_offset(void);
93266eaea6cSStefan Roese 
9336494d708SSimon Glass #endif
934