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
144984de2bSSimon Glass #include <dm/ofnode.h>
156494d708SSimon Glass #include <dm/uclass-id.h>
16c9cac3f8SPeng Fan #include <fdtdec.h>
176494d708SSimon Glass #include <linker_lists.h>
182b07f685SMasahiro Yamada #include <linux/compat.h>
192b07f685SMasahiro Yamada #include <linux/kernel.h>
206494d708SSimon Glass #include <linux/list.h>
218f1ef3f5SMasahiro Yamada #include <linux/printk.h>
226494d708SSimon Glass
236494d708SSimon Glass struct driver_info;
246494d708SSimon Glass
256494d708SSimon Glass /* Driver is active (probed). Cleared when it is removed */
266494d708SSimon Glass #define DM_FLAG_ACTIVATED (1 << 0)
276494d708SSimon Glass
286494d708SSimon Glass /* DM is responsible for allocating and freeing platdata */
29f2bc6fc3SSimon Glass #define DM_FLAG_ALLOC_PDATA (1 << 1)
306494d708SSimon Glass
3100606d7eSSimon Glass /* DM should init this device prior to relocation */
3200606d7eSSimon Glass #define DM_FLAG_PRE_RELOC (1 << 2)
3300606d7eSSimon Glass
34cdc133bdSSimon Glass /* DM is responsible for allocating and freeing parent_platdata */
35cdc133bdSSimon Glass #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
36cdc133bdSSimon Glass
375eaed880SPrzemyslaw Marczak /* DM is responsible for allocating and freeing uclass_platdata */
385eaed880SPrzemyslaw Marczak #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
395eaed880SPrzemyslaw Marczak
402c03c463SSimon Glass /* Allocate driver private data on a DMA boundary */
415eaed880SPrzemyslaw Marczak #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
422c03c463SSimon Glass
43aed1a4ddSMasahiro Yamada /* Device is bound */
44aed1a4ddSMasahiro Yamada #define DM_FLAG_BOUND (1 << 6)
45aed1a4ddSMasahiro Yamada
46a2040facSSimon Glass /* Device name is allocated and should be freed on unbind() */
47fd1c2d9bSSimon Glass #define DM_FLAG_NAME_ALLOCED (1 << 7)
48a2040facSSimon Glass
499fa28190SSimon Glass #define DM_FLAG_OF_PLATDATA (1 << 8)
509fa28190SSimon Glass
51706865afSStefan Roese /*
52706865afSStefan Roese * Call driver remove function to stop currently active DMA transfers or
53706865afSStefan Roese * give DMA buffers back to the HW / controller. This may be needed for
54706865afSStefan Roese * some drivers to do some final stage cleanup before the OS is called
55706865afSStefan Roese * (U-Boot exit)
56706865afSStefan Roese */
57706865afSStefan Roese #define DM_FLAG_ACTIVE_DMA (1 << 9)
58706865afSStefan Roese
59706865afSStefan Roese /*
60426f99faSStefan Roese * Call driver remove function to do some final configuration, before
61426f99faSStefan Roese * U-Boot exits and the OS is started
62426f99faSStefan Roese */
63426f99faSStefan Roese #define DM_FLAG_OS_PREPARE (1 << 10)
64426f99faSStefan Roese
65*d9da4b44SJoseph Chen /* Device is from kernel dtb */
66*d9da4b44SJoseph Chen #define DM_FLAG_KNRL_DTB (1 << 31)
67*d9da4b44SJoseph Chen
68426f99faSStefan Roese /*
69706865afSStefan Roese * One or multiple of these flags are passed to device_remove() so that
70706865afSStefan Roese * a selective device removal as specified by the remove-stage and the
71706865afSStefan Roese * driver flags can be done.
72706865afSStefan Roese */
73706865afSStefan Roese enum {
74706865afSStefan Roese /* Normal remove, remove all devices */
75706865afSStefan Roese DM_REMOVE_NORMAL = 1 << 0,
76706865afSStefan Roese
77706865afSStefan Roese /* Remove devices with active DMA */
78706865afSStefan Roese DM_REMOVE_ACTIVE_DMA = DM_FLAG_ACTIVE_DMA,
79706865afSStefan Roese
80426f99faSStefan Roese /* Remove devices which need some final OS preparation steps */
81426f99faSStefan Roese DM_REMOVE_OS_PREPARE = DM_FLAG_OS_PREPARE,
82426f99faSStefan Roese
83706865afSStefan Roese /* Add more use cases here */
84706865afSStefan Roese
85706865afSStefan Roese /* Remove devices with any active flag */
86426f99faSStefan Roese DM_REMOVE_ACTIVE_ALL = DM_REMOVE_ACTIVE_DMA | DM_REMOVE_OS_PREPARE,
87706865afSStefan Roese };
88706865afSStefan Roese
896494d708SSimon Glass /**
9054c5d08aSHeiko Schocher * struct udevice - An instance of a driver
916494d708SSimon Glass *
926494d708SSimon Glass * This holds information about a device, which is a driver bound to a
936494d708SSimon Glass * particular port or peripheral (essentially a driver instance).
946494d708SSimon Glass *
956494d708SSimon Glass * A device will come into existence through a 'bind' call, either due to
966494d708SSimon Glass * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
976494d708SSimon Glass * in the device tree (in which case of_offset is >= 0). In the latter case
986494d708SSimon Glass * we translate the device tree information into platdata in a function
996494d708SSimon Glass * implemented by the driver ofdata_to_platdata method (called just before the
1006494d708SSimon Glass * probe method if the device has a device tree node.
1016494d708SSimon Glass *
1026494d708SSimon Glass * All three of platdata, priv and uclass_priv can be allocated by the
1036494d708SSimon Glass * driver, or you can use the auto_alloc_size members of struct driver and
1046494d708SSimon Glass * struct uclass_driver to have driver model do this automatically.
1056494d708SSimon Glass *
1066494d708SSimon Glass * @driver: The driver used by this device
1076494d708SSimon Glass * @name: Name of device, typically the FDT node name
1086494d708SSimon Glass * @platdata: Configuration data for this device
109cdc133bdSSimon Glass * @parent_platdata: The parent bus's configuration data for this device
1105eaed880SPrzemyslaw Marczak * @uclass_platdata: The uclass's configuration data for this device
1114984de2bSSimon Glass * @node: Reference to device tree node for this device
11239de8433SSimon Glass * @driver_data: Driver data word for the entry that matched this device with
11339de8433SSimon Glass * its driver
1146494d708SSimon Glass * @parent: Parent of this device, or NULL for the top level device
1156494d708SSimon Glass * @priv: Private data for this device
1166494d708SSimon Glass * @uclass: Pointer to uclass for this device
1176494d708SSimon Glass * @uclass_priv: The uclass's private data for this device
118e59f458dSSimon Glass * @parent_priv: The parent's private data for this device
1196494d708SSimon Glass * @uclass_node: Used by uclass to link its devices
1206494d708SSimon Glass * @child_head: List of children of this device
1216494d708SSimon Glass * @sibling_node: Next device in list of all devices
1226494d708SSimon Glass * @flags: Flags for this device DM_FLAG_...
1235a66a8ffSSimon Glass * @req_seq: Requested sequence number for this device (-1 = any)
124547cea19SSimon Glass * @seq: Allocated sequence number for this device (-1 = none). This is set up
125547cea19SSimon Glass * when the device is probed and will be unique within the device's uclass.
12693c7fe4aSSimon Glass * @devres_head: List of memory allocations associated with this device.
12793c7fe4aSSimon Glass * When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
12893c7fe4aSSimon Glass * add to this list. Memory so-allocated will be freed
12993c7fe4aSSimon Glass * automatically when the device is removed / unbound
1306494d708SSimon Glass */
13154c5d08aSHeiko Schocher struct udevice {
1323479253dSSimon Glass const struct driver *driver;
1336494d708SSimon Glass const char *name;
1346494d708SSimon Glass void *platdata;
135cdc133bdSSimon Glass void *parent_platdata;
1365eaed880SPrzemyslaw Marczak void *uclass_platdata;
1374984de2bSSimon Glass ofnode node;
13839de8433SSimon Glass ulong driver_data;
13954c5d08aSHeiko Schocher struct udevice *parent;
1406494d708SSimon Glass void *priv;
1416494d708SSimon Glass struct uclass *uclass;
1426494d708SSimon Glass void *uclass_priv;
143e59f458dSSimon Glass void *parent_priv;
1446494d708SSimon Glass struct list_head uclass_node;
1456494d708SSimon Glass struct list_head child_head;
1466494d708SSimon Glass struct list_head sibling_node;
1476494d708SSimon Glass uint32_t flags;
1485a66a8ffSSimon Glass int req_seq;
1495a66a8ffSSimon Glass int seq;
150e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES
151608f26c5SMasahiro Yamada struct list_head devres_head;
152e2282d70SMasahiro Yamada #endif
1536494d708SSimon Glass };
1546494d708SSimon Glass
1555a66a8ffSSimon Glass /* Maximum sequence number supported */
1565a66a8ffSSimon Glass #define DM_MAX_SEQ 999
1575a66a8ffSSimon Glass
1586494d708SSimon Glass /* Returns the operations for a device */
1596494d708SSimon Glass #define device_get_ops(dev) (dev->driver->ops)
1606494d708SSimon Glass
1616494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */
1626494d708SSimon Glass #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
1636494d708SSimon Glass
dev_of_offset(const struct udevice * dev)164e160f7d4SSimon Glass static inline int dev_of_offset(const struct udevice *dev)
165e160f7d4SSimon Glass {
1664984de2bSSimon Glass return ofnode_to_offset(dev->node);
167e160f7d4SSimon Glass }
168e160f7d4SSimon Glass
dev_set_of_offset(struct udevice * dev,int of_offset)169e160f7d4SSimon Glass static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
170e160f7d4SSimon Glass {
1714984de2bSSimon Glass dev->node = offset_to_ofnode(of_offset);
1724984de2bSSimon Glass }
1734984de2bSSimon Glass
dev_has_of_node(struct udevice * dev)1744984de2bSSimon Glass static inline bool dev_has_of_node(struct udevice *dev)
1754984de2bSSimon Glass {
1764984de2bSSimon Glass return ofnode_valid(dev->node);
177e160f7d4SSimon Glass }
178e160f7d4SSimon Glass
1796494d708SSimon Glass /**
180ae7f4513SSimon Glass * struct udevice_id - Lists the compatible strings supported by a driver
1816494d708SSimon Glass * @compatible: Compatible string
1826494d708SSimon Glass * @data: Data for this compatible string
1836494d708SSimon Glass */
184ae7f4513SSimon Glass struct udevice_id {
1856494d708SSimon Glass const char *compatible;
1866494d708SSimon Glass ulong data;
1876494d708SSimon Glass };
1886494d708SSimon Glass
1890f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
190f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr) (_ptr)
191f887cb6dSMasahiro Yamada #else
192f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr) NULL
1930f925822SMasahiro Yamada #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
194f887cb6dSMasahiro Yamada
1956494d708SSimon Glass /**
1966494d708SSimon Glass * struct driver - A driver for a feature or peripheral
1976494d708SSimon Glass *
1986494d708SSimon Glass * This holds methods for setting up a new device, and also removing it.
1996494d708SSimon Glass * The device needs information to set itself up - this is provided either
2006494d708SSimon Glass * by platdata or a device tree node (which we find by looking up
2016494d708SSimon Glass * matching compatible strings with of_match).
2026494d708SSimon Glass *
2036494d708SSimon Glass * Drivers all belong to a uclass, representing a class of devices of the
2046494d708SSimon Glass * same type. Common elements of the drivers can be implemented in the uclass,
2056494d708SSimon Glass * or the uclass can provide a consistent interface to the drivers within
2066494d708SSimon Glass * it.
2076494d708SSimon Glass *
2086494d708SSimon Glass * @name: Device name
2096494d708SSimon Glass * @id: Identiies the uclass we belong to
2106494d708SSimon Glass * @of_match: List of compatible strings to match, and any identifying data
2116494d708SSimon Glass * for each.
2126494d708SSimon Glass * @bind: Called to bind a device to its driver
2136494d708SSimon Glass * @probe: Called to probe a device, i.e. activate it
2146494d708SSimon Glass * @remove: Called to remove a device, i.e. de-activate it
2156494d708SSimon Glass * @unbind: Called to unbind a device from its driver
2166494d708SSimon Glass * @ofdata_to_platdata: Called before probe to decode device tree data
2170118ce79SSimon Glass * @child_post_bind: Called after a new child has been bound
218a327dee0SSimon Glass * @child_pre_probe: Called before a child device is probed. The device has
219a327dee0SSimon Glass * memory allocated but it has not yet been probed.
220a327dee0SSimon Glass * @child_post_remove: Called after a child device is removed. The device
221a327dee0SSimon Glass * has memory allocated but its device_remove() method has been called.
2226494d708SSimon Glass * @priv_auto_alloc_size: If non-zero this is the size of the private data
2236494d708SSimon Glass * to be allocated in the device's ->priv pointer. If zero, then the driver
2246494d708SSimon Glass * is responsible for allocating any data required.
2256494d708SSimon Glass * @platdata_auto_alloc_size: If non-zero this is the size of the
2266494d708SSimon Glass * platform data to be allocated in the device's ->platdata pointer.
2276494d708SSimon Glass * This is typically only useful for device-tree-aware drivers (those with
2286494d708SSimon Glass * an of_match), since drivers which use platdata will have the data
2296494d708SSimon Glass * provided in the U_BOOT_DEVICE() instantiation.
230e59f458dSSimon Glass * @per_child_auto_alloc_size: Each device can hold private data owned by
231e59f458dSSimon Glass * its parent. If required this will be automatically allocated if this
232e59f458dSSimon Glass * value is non-zero.
233cdc133bdSSimon Glass * @per_child_platdata_auto_alloc_size: A bus likes to store information about
234cdc133bdSSimon Glass * its children. If non-zero this is the size of this data, to be allocated
235cdc133bdSSimon Glass * in the child's parent_platdata pointer.
2360040b944SSimon Glass * @ops: Driver-specific operations. This is typically a list of function
2376494d708SSimon Glass * pointers defined by the driver, to implement driver functions required by
2386494d708SSimon Glass * the uclass.
23900606d7eSSimon Glass * @flags: driver flags - see DM_FLAGS_...
2406494d708SSimon Glass */
2416494d708SSimon Glass struct driver {
2426494d708SSimon Glass char *name;
2436494d708SSimon Glass enum uclass_id id;
244ae7f4513SSimon Glass const struct udevice_id *of_match;
24554c5d08aSHeiko Schocher int (*bind)(struct udevice *dev);
24654c5d08aSHeiko Schocher int (*probe)(struct udevice *dev);
24754c5d08aSHeiko Schocher int (*remove)(struct udevice *dev);
24854c5d08aSHeiko Schocher int (*unbind)(struct udevice *dev);
24954c5d08aSHeiko Schocher int (*ofdata_to_platdata)(struct udevice *dev);
2500118ce79SSimon Glass int (*child_post_bind)(struct udevice *dev);
251a327dee0SSimon Glass int (*child_pre_probe)(struct udevice *dev);
252a327dee0SSimon Glass int (*child_post_remove)(struct udevice *dev);
2536494d708SSimon Glass int priv_auto_alloc_size;
2546494d708SSimon Glass int platdata_auto_alloc_size;
255e59f458dSSimon Glass int per_child_auto_alloc_size;
256cdc133bdSSimon Glass int per_child_platdata_auto_alloc_size;
2576494d708SSimon Glass const void *ops; /* driver-specific operations */
25800606d7eSSimon Glass uint32_t flags;
2596494d708SSimon Glass };
2606494d708SSimon Glass
2616494d708SSimon Glass /* Declare a new U-Boot driver */
2626494d708SSimon Glass #define U_BOOT_DRIVER(__name) \
2636494d708SSimon Glass ll_entry_declare(struct driver, __name, driver)
2646494d708SSimon Glass
265c57f806bSSimon Glass /* Get a pointer to a given driver */
266c57f806bSSimon Glass #define DM_GET_DRIVER(__name) \
267c57f806bSSimon Glass ll_entry_get(struct driver, __name, driver)
268c57f806bSSimon Glass
2696494d708SSimon Glass /**
2706494d708SSimon Glass * dev_get_platdata() - Get the platform data for a device
2716494d708SSimon Glass *
2726494d708SSimon Glass * This checks that dev is not NULL, but no other checks for now
2736494d708SSimon Glass *
2746494d708SSimon Glass * @dev Device to check
2756494d708SSimon Glass * @return platform data, or NULL if none
2766494d708SSimon Glass */
27754c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev);
2786494d708SSimon Glass
2796494d708SSimon Glass /**
280cdc133bdSSimon Glass * dev_get_parent_platdata() - Get the parent platform data for a device
281cdc133bdSSimon Glass *
282cdc133bdSSimon Glass * This checks that dev is not NULL, but no other checks for now
283cdc133bdSSimon Glass *
284cdc133bdSSimon Glass * @dev Device to check
285cdc133bdSSimon Glass * @return parent's platform data, or NULL if none
286cdc133bdSSimon Glass */
287cdc133bdSSimon Glass void *dev_get_parent_platdata(struct udevice *dev);
288cdc133bdSSimon Glass
289cdc133bdSSimon Glass /**
2905eaed880SPrzemyslaw Marczak * dev_get_uclass_platdata() - Get the uclass platform data for a device
2915eaed880SPrzemyslaw Marczak *
2925eaed880SPrzemyslaw Marczak * This checks that dev is not NULL, but no other checks for now
2935eaed880SPrzemyslaw Marczak *
2945eaed880SPrzemyslaw Marczak * @dev Device to check
2955eaed880SPrzemyslaw Marczak * @return uclass's platform data, or NULL if none
2965eaed880SPrzemyslaw Marczak */
2975eaed880SPrzemyslaw Marczak void *dev_get_uclass_platdata(struct udevice *dev);
2985eaed880SPrzemyslaw Marczak
2995eaed880SPrzemyslaw Marczak /**
3009a79f6e6SSimon Glass * dev_get_priv() - Get the private data for a device
3019a79f6e6SSimon Glass *
3029a79f6e6SSimon Glass * This checks that dev is not NULL, but no other checks for now
3039a79f6e6SSimon Glass *
3049a79f6e6SSimon Glass * @dev Device to check
3059a79f6e6SSimon Glass * @return private data, or NULL if none
3069a79f6e6SSimon Glass */
3079a79f6e6SSimon Glass void *dev_get_priv(struct udevice *dev);
3089a79f6e6SSimon Glass
3099a79f6e6SSimon Glass /**
310bcbe3d15SSimon Glass * dev_get_parent_priv() - Get the parent private data for a device
311e59f458dSSimon Glass *
312bcbe3d15SSimon Glass * The parent private data is data stored in the device but owned by the
313bcbe3d15SSimon Glass * parent. For example, a USB device may have parent data which contains
314bcbe3d15SSimon Glass * information about how to talk to the device over USB.
315e59f458dSSimon Glass *
316e59f458dSSimon Glass * This checks that dev is not NULL, but no other checks for now
317e59f458dSSimon Glass *
318e59f458dSSimon Glass * @dev Device to check
319e59f458dSSimon Glass * @return parent data, or NULL if none
320e59f458dSSimon Glass */
321bcbe3d15SSimon Glass void *dev_get_parent_priv(struct udevice *dev);
322e59f458dSSimon Glass
323e59f458dSSimon Glass /**
324e564f054SSimon Glass * dev_get_uclass_priv() - Get the private uclass data for a device
325e564f054SSimon Glass *
326e564f054SSimon Glass * This checks that dev is not NULL, but no other checks for now
327e564f054SSimon Glass *
328e564f054SSimon Glass * @dev Device to check
329e564f054SSimon Glass * @return private uclass data for this device, or NULL if none
330e564f054SSimon Glass */
331e564f054SSimon Glass void *dev_get_uclass_priv(struct udevice *dev);
332e564f054SSimon Glass
333e564f054SSimon Glass /**
3349a79f6e6SSimon Glass * struct dev_get_parent() - Get the parent of a device
3359a79f6e6SSimon Glass *
3369a79f6e6SSimon Glass * @child: Child to check
3379a79f6e6SSimon Glass * @return parent of child, or NULL if this is the root device
3389a79f6e6SSimon Glass */
3399a79f6e6SSimon Glass struct udevice *dev_get_parent(struct udevice *child);
3409a79f6e6SSimon Glass
3419a79f6e6SSimon Glass /**
34239de8433SSimon Glass * dev_get_driver_data() - get the driver data used to bind a device
3432ef249b4SSimon Glass *
3442ef249b4SSimon Glass * When a device is bound using a device tree node, it matches a
3458d1f3a9dSSimon Glass * particular compatible string in struct udevice_id. This function
34639de8433SSimon Glass * returns the associated data value for that compatible string. This is
34739de8433SSimon Glass * the 'data' field in struct udevice_id.
34839de8433SSimon Glass *
3498d1f3a9dSSimon Glass * As an example, consider this structure:
3508d1f3a9dSSimon Glass * static const struct udevice_id tegra_i2c_ids[] = {
3518d1f3a9dSSimon Glass * { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
3528d1f3a9dSSimon Glass * { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
3538d1f3a9dSSimon Glass * { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
3548d1f3a9dSSimon Glass * { }
3558d1f3a9dSSimon Glass * };
3568d1f3a9dSSimon Glass *
3578d1f3a9dSSimon Glass * When driver model finds a driver for this it will store the 'data' value
3588d1f3a9dSSimon Glass * corresponding to the compatible string it matches. This function returns
3598d1f3a9dSSimon Glass * that value. This allows the driver to handle several variants of a device.
3608d1f3a9dSSimon Glass *
36139de8433SSimon Glass * For USB devices, this is the driver_info field in struct usb_device_id.
36239de8433SSimon Glass *
36339de8433SSimon Glass * @dev: Device to check
3648d1f3a9dSSimon Glass * @return driver data (0 if none is provided)
3652ef249b4SSimon Glass */
36639de8433SSimon Glass ulong dev_get_driver_data(struct udevice *dev);
3672ef249b4SSimon Glass
368cc73d37bSPrzemyslaw Marczak /**
369cc73d37bSPrzemyslaw Marczak * dev_get_driver_ops() - get the device's driver's operations
370cc73d37bSPrzemyslaw Marczak *
371cc73d37bSPrzemyslaw Marczak * This checks that dev is not NULL, and returns the pointer to device's
372cc73d37bSPrzemyslaw Marczak * driver's operations.
373cc73d37bSPrzemyslaw Marczak *
374cc73d37bSPrzemyslaw Marczak * @dev: Device to check
375cc73d37bSPrzemyslaw Marczak * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
376cc73d37bSPrzemyslaw Marczak */
377cc73d37bSPrzemyslaw Marczak const void *dev_get_driver_ops(struct udevice *dev);
378cc73d37bSPrzemyslaw Marczak
3798d1f3a9dSSimon Glass /**
380b3670531SSimon Glass * device_get_uclass_id() - return the uclass ID of a device
381b3670531SSimon Glass *
382b3670531SSimon Glass * @dev: Device to check
383b3670531SSimon Glass * @return uclass ID for the device
384b3670531SSimon Glass */
385b3670531SSimon Glass enum uclass_id device_get_uclass_id(struct udevice *dev);
386b3670531SSimon Glass
3878d1f3a9dSSimon Glass /**
388f9c370dcSPrzemyslaw Marczak * dev_get_uclass_name() - return the uclass name of a device
389f9c370dcSPrzemyslaw Marczak *
390f9c370dcSPrzemyslaw Marczak * This checks that dev is not NULL.
391f9c370dcSPrzemyslaw Marczak *
392f9c370dcSPrzemyslaw Marczak * @dev: Device to check
393f9c370dcSPrzemyslaw Marczak * @return pointer to the uclass name for the device
394f9c370dcSPrzemyslaw Marczak */
395f9c370dcSPrzemyslaw Marczak const char *dev_get_uclass_name(struct udevice *dev);
396f9c370dcSPrzemyslaw Marczak
3972ef249b4SSimon Glass /**
398997c87bbSSimon Glass * device_get_child() - Get the child of a device by index
399997c87bbSSimon Glass *
400997c87bbSSimon Glass * Returns the numbered child, 0 being the first. This does not use
401997c87bbSSimon Glass * sequence numbers, only the natural order.
402997c87bbSSimon Glass *
403997c87bbSSimon Glass * @dev: Parent device to check
404997c87bbSSimon Glass * @index: Child index
405997c87bbSSimon Glass * @devp: Returns pointer to device
4063f416f33SSimon Glass * @return 0 if OK, -ENODEV if no such device, other error if the device fails
4073f416f33SSimon Glass * to probe
408997c87bbSSimon Glass */
409997c87bbSSimon Glass int device_get_child(struct udevice *parent, int index, struct udevice **devp);
410997c87bbSSimon Glass
411997c87bbSSimon Glass /**
4125a66a8ffSSimon Glass * device_find_child_by_seq() - Find a child device based on a sequence
4135a66a8ffSSimon Glass *
4145a66a8ffSSimon Glass * This searches for a device with the given seq or req_seq.
4155a66a8ffSSimon Glass *
4165a66a8ffSSimon Glass * For seq, if an active device has this sequence it will be returned.
4175a66a8ffSSimon Glass * If there is no such device then this will return -ENODEV.
4185a66a8ffSSimon Glass *
4195a66a8ffSSimon Glass * For req_seq, if a device (whether activated or not) has this req_seq
4205a66a8ffSSimon Glass * value, that device will be returned. This is a strong indication that
4215a66a8ffSSimon Glass * the device will receive that sequence when activated.
4225a66a8ffSSimon Glass *
4235a66a8ffSSimon Glass * @parent: Parent device
4245a66a8ffSSimon Glass * @seq_or_req_seq: Sequence number to find (0=first)
4255a66a8ffSSimon Glass * @find_req_seq: true to find req_seq, false to find seq
4265a66a8ffSSimon Glass * @devp: Returns pointer to device (there is only one per for each seq).
4275a66a8ffSSimon Glass * Set to NULL if none is found
4285a66a8ffSSimon Glass * @return 0 if OK, -ve on error
4295a66a8ffSSimon Glass */
4305a66a8ffSSimon Glass int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
4315a66a8ffSSimon Glass bool find_req_seq, struct udevice **devp);
4325a66a8ffSSimon Glass
433997c87bbSSimon Glass /**
434997c87bbSSimon Glass * device_get_child_by_seq() - Get a child device based on a sequence
435997c87bbSSimon Glass *
436997c87bbSSimon Glass * If an active device has this sequence it will be returned. If there is no
437997c87bbSSimon Glass * such device then this will check for a device that is requesting this
438997c87bbSSimon Glass * sequence.
439997c87bbSSimon Glass *
440997c87bbSSimon Glass * The device is probed to activate it ready for use.
441997c87bbSSimon Glass *
442997c87bbSSimon Glass * @parent: Parent device
443997c87bbSSimon Glass * @seq: Sequence number to find (0=first)
444997c87bbSSimon Glass * @devp: Returns pointer to device (there is only one per for each seq)
445997c87bbSSimon Glass * Set to NULL if none is found
446997c87bbSSimon Glass * @return 0 if OK, -ve on error
447997c87bbSSimon Glass */
448997c87bbSSimon Glass int device_get_child_by_seq(struct udevice *parent, int seq,
449997c87bbSSimon Glass struct udevice **devp);
450997c87bbSSimon Glass
451997c87bbSSimon Glass /**
452997c87bbSSimon Glass * device_find_child_by_of_offset() - Find a child device based on FDT offset
453997c87bbSSimon Glass *
454997c87bbSSimon Glass * Locates a child device by its device tree offset.
455997c87bbSSimon Glass *
456997c87bbSSimon Glass * @parent: Parent device
457997c87bbSSimon Glass * @of_offset: Device tree offset to find
458997c87bbSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL
459997c87bbSSimon Glass * @return 0 if OK, -ve on error
460997c87bbSSimon Glass */
461997c87bbSSimon Glass int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
462997c87bbSSimon Glass struct udevice **devp);
463997c87bbSSimon Glass
464997c87bbSSimon Glass /**
465997c87bbSSimon Glass * device_get_child_by_of_offset() - Get a child device based on FDT offset
466997c87bbSSimon Glass *
467997c87bbSSimon Glass * Locates a child device by its device tree offset.
468997c87bbSSimon Glass *
469997c87bbSSimon Glass * The device is probed to activate it ready for use.
470997c87bbSSimon Glass *
471997c87bbSSimon Glass * @parent: Parent device
472997c87bbSSimon Glass * @of_offset: Device tree offset to find
473997c87bbSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL
474997c87bbSSimon Glass * @return 0 if OK, -ve on error
475997c87bbSSimon Glass */
476132f9bfcSSimon Glass int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
477997c87bbSSimon Glass struct udevice **devp);
478997c87bbSSimon Glass
479a8981d4fSSimon Glass /**
4802693047aSSimon Glass * device_get_global_by_of_offset() - Get a device based on FDT offset
4812693047aSSimon Glass *
4822693047aSSimon Glass * Locates a device by its device tree offset, searching globally throughout
4832693047aSSimon Glass * the all driver model devices.
4842693047aSSimon Glass *
4852693047aSSimon Glass * The device is probed to activate it ready for use.
4862693047aSSimon Glass *
4872693047aSSimon Glass * @of_offset: Device tree offset to find
4882693047aSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL
4892693047aSSimon Glass * @return 0 if OK, -ve on error
4902693047aSSimon Glass */
4912693047aSSimon Glass int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
4922693047aSSimon Glass
4932693047aSSimon Glass /**
494a8981d4fSSimon Glass * device_find_first_child() - Find the first child of a device
495a8981d4fSSimon Glass *
496a8981d4fSSimon Glass * @parent: Parent device to search
497a8981d4fSSimon Glass * @devp: Returns first child device, or NULL if none
498a8981d4fSSimon Glass * @return 0
499a8981d4fSSimon Glass */
500a8981d4fSSimon Glass int device_find_first_child(struct udevice *parent, struct udevice **devp);
501a8981d4fSSimon Glass
502a8981d4fSSimon Glass /**
5033f416f33SSimon Glass * device_find_next_child() - Find the next child of a device
504a8981d4fSSimon Glass *
505a8981d4fSSimon Glass * @devp: Pointer to previous child device on entry. Returns pointer to next
506a8981d4fSSimon Glass * child device, or NULL if none
507a8981d4fSSimon Glass * @return 0
508a8981d4fSSimon Glass */
509a8981d4fSSimon Glass int device_find_next_child(struct udevice **devp);
510a8981d4fSSimon Glass
511c9cac3f8SPeng Fan /**
512c5785673SSimon Glass * device_has_children() - check if a device has any children
513c5785673SSimon Glass *
514c5785673SSimon Glass * @dev: Device to check
515c5785673SSimon Glass * @return true if the device has one or more children
516c5785673SSimon Glass */
517c5785673SSimon Glass bool device_has_children(struct udevice *dev);
518c5785673SSimon Glass
519c5785673SSimon Glass /**
520c5785673SSimon Glass * device_has_active_children() - check if a device has any active children
521c5785673SSimon Glass *
522c5785673SSimon Glass * @dev: Device to check
523c5785673SSimon Glass * @return true if the device has one or more children and at least one of
524c5785673SSimon Glass * them is active (probed).
525c5785673SSimon Glass */
526c5785673SSimon Glass bool device_has_active_children(struct udevice *dev);
527c5785673SSimon Glass
528c5785673SSimon Glass /**
529c5785673SSimon Glass * device_is_last_sibling() - check if a device is the last sibling
530c5785673SSimon Glass *
531c5785673SSimon Glass * This function can be useful for display purposes, when special action needs
532c5785673SSimon Glass * to be taken when displaying the last sibling. This can happen when a tree
533c5785673SSimon Glass * view of devices is being displayed.
534c5785673SSimon Glass *
535c5785673SSimon Glass * @dev: Device to check
536c5785673SSimon Glass * @return true if there are no more siblings after this one - i.e. is it
537c5785673SSimon Glass * last in the list.
538c5785673SSimon Glass */
539c5785673SSimon Glass bool device_is_last_sibling(struct udevice *dev);
540c5785673SSimon Glass
541f5c67ea0SSimon Glass /**
542f5c67ea0SSimon Glass * device_set_name() - set the name of a device
543f5c67ea0SSimon Glass *
544f5c67ea0SSimon Glass * This must be called in the device's bind() method and no later. Normally
545f5c67ea0SSimon Glass * this is unnecessary but for probed devices which don't get a useful name
546f5c67ea0SSimon Glass * this function can be helpful.
547f5c67ea0SSimon Glass *
548a2040facSSimon Glass * The name is allocated and will be freed automatically when the device is
549a2040facSSimon Glass * unbound.
550a2040facSSimon Glass *
551f5c67ea0SSimon Glass * @dev: Device to update
552f5c67ea0SSimon Glass * @name: New name (this string is allocated new memory and attached to
553f5c67ea0SSimon Glass * the device)
554f5c67ea0SSimon Glass * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
555f5c67ea0SSimon Glass * string
556f5c67ea0SSimon Glass */
557f5c67ea0SSimon Glass int device_set_name(struct udevice *dev, const char *name);
558f5c67ea0SSimon Glass
5591e0f2263SBin Meng /**
560a2040facSSimon Glass * device_set_name_alloced() - note that a device name is allocated
561a2040facSSimon Glass *
562fd1c2d9bSSimon Glass * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
563a2040facSSimon Glass * unbound the name will be freed. This avoids memory leaks.
564a2040facSSimon Glass *
565a2040facSSimon Glass * @dev: Device to update
566a2040facSSimon Glass */
567a2040facSSimon Glass void device_set_name_alloced(struct udevice *dev);
568a2040facSSimon Glass
569a2040facSSimon Glass /**
570911f3aefSSimon Glass * device_is_compatible() - check if the device is compatible with the compat
57173443b9eSMugunthan V N *
57273443b9eSMugunthan V N * This allows to check whether the device is comaptible with the compat.
57373443b9eSMugunthan V N *
57473443b9eSMugunthan V N * @dev: udevice pointer for which compatible needs to be verified.
57573443b9eSMugunthan V N * @compat: Compatible string which needs to verified in the given
57673443b9eSMugunthan V N * device
57773443b9eSMugunthan V N * @return true if OK, false if the compatible is not found
57873443b9eSMugunthan V N */
579911f3aefSSimon Glass bool device_is_compatible(struct udevice *dev, const char *compat);
58073443b9eSMugunthan V N
58173443b9eSMugunthan V N /**
58273443b9eSMugunthan V N * of_machine_is_compatible() - check if the machine is compatible with
58373443b9eSMugunthan V N * the compat
58473443b9eSMugunthan V N *
58573443b9eSMugunthan V N * This allows to check whether the machine is comaptible with the compat.
58673443b9eSMugunthan V N *
58773443b9eSMugunthan V N * @compat: Compatible string which needs to verified
58873443b9eSMugunthan V N * @return true if OK, false if the compatible is not found
58973443b9eSMugunthan V N */
59073443b9eSMugunthan V N bool of_machine_is_compatible(const char *compat);
59173443b9eSMugunthan V N
59273443b9eSMugunthan V N /**
5931e0f2263SBin Meng * device_is_on_pci_bus - Test if a device is on a PCI bus
5941e0f2263SBin Meng *
5951e0f2263SBin Meng * @dev: device to test
5961e0f2263SBin Meng * @return: true if it is on a PCI bus, false otherwise
5971e0f2263SBin Meng */
device_is_on_pci_bus(struct udevice * dev)5981e0f2263SBin Meng static inline bool device_is_on_pci_bus(struct udevice *dev)
5991e0f2263SBin Meng {
6001e0f2263SBin Meng return device_get_uclass_id(dev->parent) == UCLASS_PCI;
6011e0f2263SBin Meng }
6021e0f2263SBin Meng
6037aeac5bcSSimon Glass /**
6047aeac5bcSSimon Glass * device_foreach_child_safe() - iterate through child devices safely
6057aeac5bcSSimon Glass *
6067aeac5bcSSimon Glass * This allows the @pos child to be removed in the loop if required.
6077aeac5bcSSimon Glass *
6087aeac5bcSSimon Glass * @pos: struct udevice * for the current device
6097aeac5bcSSimon Glass * @next: struct udevice * for the next device
6107aeac5bcSSimon Glass * @parent: parent device to scan
6117aeac5bcSSimon Glass */
6127aeac5bcSSimon Glass #define device_foreach_child_safe(pos, next, parent) \
6137aeac5bcSSimon Glass list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
6147aeac5bcSSimon Glass
615cc7f66f7SSimon Glass /**
616cc7f66f7SSimon Glass * dm_scan_fdt_dev() - Bind child device in a the device tree
617cc7f66f7SSimon Glass *
618cc7f66f7SSimon Glass * This handles device which have sub-nodes in the device tree. It scans all
619cc7f66f7SSimon Glass * sub-nodes and binds drivers for each node where a driver can be found.
620cc7f66f7SSimon Glass *
621cc7f66f7SSimon Glass * If this is called prior to relocation, only pre-relocation devices will be
622cc7f66f7SSimon Glass * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
623cc7f66f7SSimon Glass * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
624cc7f66f7SSimon Glass * be bound.
625cc7f66f7SSimon Glass *
626cc7f66f7SSimon Glass * @dev: Device to scan
627cc7f66f7SSimon Glass * @return 0 if OK, -ve on error
628cc7f66f7SSimon Glass */
629cc7f66f7SSimon Glass int dm_scan_fdt_dev(struct udevice *dev);
630cc7f66f7SSimon Glass
631608f26c5SMasahiro Yamada /* device resource management */
632608f26c5SMasahiro Yamada typedef void (*dr_release_t)(struct udevice *dev, void *res);
633608f26c5SMasahiro Yamada typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
634608f26c5SMasahiro Yamada
635e2282d70SMasahiro Yamada #ifdef CONFIG_DEVRES
636e2282d70SMasahiro Yamada
637608f26c5SMasahiro Yamada #ifdef CONFIG_DEBUG_DEVRES
638608f26c5SMasahiro Yamada void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
639608f26c5SMasahiro Yamada const char *name);
640608f26c5SMasahiro Yamada #define _devres_alloc(release, size, gfp) \
641608f26c5SMasahiro Yamada __devres_alloc(release, size, gfp, #release)
642608f26c5SMasahiro Yamada #else
643608f26c5SMasahiro Yamada void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
644608f26c5SMasahiro Yamada #endif
645608f26c5SMasahiro Yamada
646608f26c5SMasahiro Yamada /**
64793c7fe4aSSimon Glass * devres_alloc() - Allocate device resource data
648608f26c5SMasahiro Yamada * @release: Release function devres will be associated with
649608f26c5SMasahiro Yamada * @size: Allocation size
650608f26c5SMasahiro Yamada * @gfp: Allocation flags
651608f26c5SMasahiro Yamada *
652608f26c5SMasahiro Yamada * Allocate devres of @size bytes. The allocated area is associated
653608f26c5SMasahiro Yamada * with @release. The returned pointer can be passed to
654608f26c5SMasahiro Yamada * other devres_*() functions.
655608f26c5SMasahiro Yamada *
656608f26c5SMasahiro Yamada * RETURNS:
657608f26c5SMasahiro Yamada * Pointer to allocated devres on success, NULL on failure.
658608f26c5SMasahiro Yamada */
659608f26c5SMasahiro Yamada #define devres_alloc(release, size, gfp) \
660608f26c5SMasahiro Yamada _devres_alloc(release, size, gfp | __GFP_ZERO)
661608f26c5SMasahiro Yamada
662608f26c5SMasahiro Yamada /**
66393c7fe4aSSimon Glass * devres_free() - Free device resource data
664608f26c5SMasahiro Yamada * @res: Pointer to devres data to free
665608f26c5SMasahiro Yamada *
666608f26c5SMasahiro Yamada * Free devres created with devres_alloc().
667608f26c5SMasahiro Yamada */
668608f26c5SMasahiro Yamada void devres_free(void *res);
669608f26c5SMasahiro Yamada
670608f26c5SMasahiro Yamada /**
67193c7fe4aSSimon Glass * devres_add() - Register device resource
672608f26c5SMasahiro Yamada * @dev: Device to add resource to
673608f26c5SMasahiro Yamada * @res: Resource to register
674608f26c5SMasahiro Yamada *
675608f26c5SMasahiro Yamada * Register devres @res to @dev. @res should have been allocated
676608f26c5SMasahiro Yamada * using devres_alloc(). On driver detach, the associated release
677608f26c5SMasahiro Yamada * function will be invoked and devres will be freed automatically.
678608f26c5SMasahiro Yamada */
679608f26c5SMasahiro Yamada void devres_add(struct udevice *dev, void *res);
680608f26c5SMasahiro Yamada
681608f26c5SMasahiro Yamada /**
68293c7fe4aSSimon Glass * devres_find() - Find device resource
683608f26c5SMasahiro Yamada * @dev: Device to lookup resource from
684608f26c5SMasahiro Yamada * @release: Look for resources associated with this release function
685608f26c5SMasahiro Yamada * @match: Match function (optional)
686608f26c5SMasahiro Yamada * @match_data: Data for the match function
687608f26c5SMasahiro Yamada *
688608f26c5SMasahiro Yamada * Find the latest devres of @dev which is associated with @release
689608f26c5SMasahiro Yamada * and for which @match returns 1. If @match is NULL, it's considered
690608f26c5SMasahiro Yamada * to match all.
691608f26c5SMasahiro Yamada *
69293c7fe4aSSimon Glass * @return pointer to found devres, NULL if not found.
693608f26c5SMasahiro Yamada */
694608f26c5SMasahiro Yamada void *devres_find(struct udevice *dev, dr_release_t release,
695608f26c5SMasahiro Yamada dr_match_t match, void *match_data);
696608f26c5SMasahiro Yamada
697608f26c5SMasahiro Yamada /**
69893c7fe4aSSimon Glass * devres_get() - Find devres, if non-existent, add one atomically
699608f26c5SMasahiro Yamada * @dev: Device to lookup or add devres for
700608f26c5SMasahiro Yamada * @new_res: Pointer to new initialized devres to add if not found
701608f26c5SMasahiro Yamada * @match: Match function (optional)
702608f26c5SMasahiro Yamada * @match_data: Data for the match function
703608f26c5SMasahiro Yamada *
704608f26c5SMasahiro Yamada * Find the latest devres of @dev which has the same release function
705608f26c5SMasahiro Yamada * as @new_res and for which @match return 1. If found, @new_res is
706608f26c5SMasahiro Yamada * freed; otherwise, @new_res is added atomically.
707608f26c5SMasahiro Yamada *
70893c7fe4aSSimon Glass * @return ointer to found or added devres.
709608f26c5SMasahiro Yamada */
710608f26c5SMasahiro Yamada void *devres_get(struct udevice *dev, void *new_res,
711608f26c5SMasahiro Yamada dr_match_t match, void *match_data);
712608f26c5SMasahiro Yamada
713608f26c5SMasahiro Yamada /**
71493c7fe4aSSimon Glass * devres_remove() - Find a device resource and remove it
715608f26c5SMasahiro Yamada * @dev: Device to find resource from
716608f26c5SMasahiro Yamada * @release: Look for resources associated with this release function
717608f26c5SMasahiro Yamada * @match: Match function (optional)
718608f26c5SMasahiro Yamada * @match_data: Data for the match function
719608f26c5SMasahiro Yamada *
720608f26c5SMasahiro Yamada * Find the latest devres of @dev associated with @release and for
721608f26c5SMasahiro Yamada * which @match returns 1. If @match is NULL, it's considered to
722608f26c5SMasahiro Yamada * match all. If found, the resource is removed atomically and
723608f26c5SMasahiro Yamada * returned.
724608f26c5SMasahiro Yamada *
72593c7fe4aSSimon Glass * @return ointer to removed devres on success, NULL if not found.
726608f26c5SMasahiro Yamada */
727608f26c5SMasahiro Yamada void *devres_remove(struct udevice *dev, dr_release_t release,
728608f26c5SMasahiro Yamada dr_match_t match, void *match_data);
729608f26c5SMasahiro Yamada
730608f26c5SMasahiro Yamada /**
73193c7fe4aSSimon Glass * devres_destroy() - Find a device resource and destroy it
732608f26c5SMasahiro Yamada * @dev: Device to find resource from
733608f26c5SMasahiro Yamada * @release: Look for resources associated with this release function
734608f26c5SMasahiro Yamada * @match: Match function (optional)
735608f26c5SMasahiro Yamada * @match_data: Data for the match function
736608f26c5SMasahiro Yamada *
737608f26c5SMasahiro Yamada * Find the latest devres of @dev associated with @release and for
738608f26c5SMasahiro Yamada * which @match returns 1. If @match is NULL, it's considered to
739608f26c5SMasahiro Yamada * match all. If found, the resource is removed atomically and freed.
740608f26c5SMasahiro Yamada *
741608f26c5SMasahiro Yamada * Note that the release function for the resource will not be called,
742608f26c5SMasahiro Yamada * only the devres-allocated data will be freed. The caller becomes
743608f26c5SMasahiro Yamada * responsible for freeing any other data.
744608f26c5SMasahiro Yamada *
74593c7fe4aSSimon Glass * @return 0 if devres is found and freed, -ENOENT if not found.
746608f26c5SMasahiro Yamada */
747608f26c5SMasahiro Yamada int devres_destroy(struct udevice *dev, dr_release_t release,
748608f26c5SMasahiro Yamada dr_match_t match, void *match_data);
749608f26c5SMasahiro Yamada
750608f26c5SMasahiro Yamada /**
75193c7fe4aSSimon Glass * devres_release() - Find a device resource and destroy it, calling release
752608f26c5SMasahiro Yamada * @dev: Device to find resource from
753608f26c5SMasahiro Yamada * @release: Look for resources associated with this release function
754608f26c5SMasahiro Yamada * @match: Match function (optional)
755608f26c5SMasahiro Yamada * @match_data: Data for the match function
756608f26c5SMasahiro Yamada *
757608f26c5SMasahiro Yamada * Find the latest devres of @dev associated with @release and for
758608f26c5SMasahiro Yamada * which @match returns 1. If @match is NULL, it's considered to
759608f26c5SMasahiro Yamada * match all. If found, the resource is removed atomically, the
760608f26c5SMasahiro Yamada * release function called and the resource freed.
761608f26c5SMasahiro Yamada *
76293c7fe4aSSimon Glass * @return 0 if devres is found and freed, -ENOENT if not found.
763608f26c5SMasahiro Yamada */
764608f26c5SMasahiro Yamada int devres_release(struct udevice *dev, dr_release_t release,
765608f26c5SMasahiro Yamada dr_match_t match, void *match_data);
766608f26c5SMasahiro Yamada
7672b07f685SMasahiro Yamada /* managed devm_k.alloc/kfree for device drivers */
7682b07f685SMasahiro Yamada /**
76993c7fe4aSSimon Glass * devm_kmalloc() - Resource-managed kmalloc
7702b07f685SMasahiro Yamada * @dev: Device to allocate memory for
7712b07f685SMasahiro Yamada * @size: Allocation size
7722b07f685SMasahiro Yamada * @gfp: Allocation gfp flags
7732b07f685SMasahiro Yamada *
7742b07f685SMasahiro Yamada * Managed kmalloc. Memory allocated with this function is
7752b07f685SMasahiro Yamada * automatically freed on driver detach. Like all other devres
7762b07f685SMasahiro Yamada * resources, guaranteed alignment is unsigned long long.
7772b07f685SMasahiro Yamada *
77893c7fe4aSSimon Glass * @return pointer to allocated memory on success, NULL on failure.
7792b07f685SMasahiro Yamada */
7802b07f685SMasahiro Yamada void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
devm_kzalloc(struct udevice * dev,size_t size,gfp_t gfp)7812b07f685SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
7822b07f685SMasahiro Yamada {
7832b07f685SMasahiro Yamada return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
7842b07f685SMasahiro Yamada }
devm_kmalloc_array(struct udevice * dev,size_t n,size_t size,gfp_t flags)7852b07f685SMasahiro Yamada static inline void *devm_kmalloc_array(struct udevice *dev,
7862b07f685SMasahiro Yamada size_t n, size_t size, gfp_t flags)
7872b07f685SMasahiro Yamada {
7882b07f685SMasahiro Yamada if (size != 0 && n > SIZE_MAX / size)
7892b07f685SMasahiro Yamada return NULL;
7902b07f685SMasahiro Yamada return devm_kmalloc(dev, n * size, flags);
7912b07f685SMasahiro Yamada }
devm_kcalloc(struct udevice * dev,size_t n,size_t size,gfp_t flags)7922b07f685SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev,
7932b07f685SMasahiro Yamada size_t n, size_t size, gfp_t flags)
7942b07f685SMasahiro Yamada {
7952b07f685SMasahiro Yamada return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
7962b07f685SMasahiro Yamada }
7972b07f685SMasahiro Yamada
7982b07f685SMasahiro Yamada /**
79993c7fe4aSSimon Glass * devm_kfree() - Resource-managed kfree
8002b07f685SMasahiro Yamada * @dev: Device this memory belongs to
80193c7fe4aSSimon Glass * @ptr: Memory to free
8022b07f685SMasahiro Yamada *
8032b07f685SMasahiro Yamada * Free memory allocated with devm_kmalloc().
8042b07f685SMasahiro Yamada */
80593c7fe4aSSimon Glass void devm_kfree(struct udevice *dev, void *ptr);
8062b07f685SMasahiro Yamada
807e2282d70SMasahiro Yamada #else /* ! CONFIG_DEVRES */
808e2282d70SMasahiro Yamada
devres_alloc(dr_release_t release,size_t size,gfp_t gfp)809e2282d70SMasahiro Yamada static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
810e2282d70SMasahiro Yamada {
811e2282d70SMasahiro Yamada return kzalloc(size, gfp);
812e2282d70SMasahiro Yamada }
813e2282d70SMasahiro Yamada
devres_free(void * res)814e2282d70SMasahiro Yamada static inline void devres_free(void *res)
815e2282d70SMasahiro Yamada {
816e2282d70SMasahiro Yamada kfree(res);
817e2282d70SMasahiro Yamada }
818e2282d70SMasahiro Yamada
devres_add(struct udevice * dev,void * res)819e2282d70SMasahiro Yamada static inline void devres_add(struct udevice *dev, void *res)
820e2282d70SMasahiro Yamada {
821e2282d70SMasahiro Yamada }
822e2282d70SMasahiro Yamada
devres_find(struct udevice * dev,dr_release_t release,dr_match_t match,void * match_data)823e2282d70SMasahiro Yamada static inline void *devres_find(struct udevice *dev, dr_release_t release,
824e2282d70SMasahiro Yamada dr_match_t match, void *match_data)
825e2282d70SMasahiro Yamada {
826e2282d70SMasahiro Yamada return NULL;
827e2282d70SMasahiro Yamada }
828e2282d70SMasahiro Yamada
devres_get(struct udevice * dev,void * new_res,dr_match_t match,void * match_data)829e2282d70SMasahiro Yamada static inline void *devres_get(struct udevice *dev, void *new_res,
830e2282d70SMasahiro Yamada dr_match_t match, void *match_data)
831e2282d70SMasahiro Yamada {
832e2282d70SMasahiro Yamada return NULL;
833e2282d70SMasahiro Yamada }
834e2282d70SMasahiro Yamada
devres_remove(struct udevice * dev,dr_release_t release,dr_match_t match,void * match_data)835e2282d70SMasahiro Yamada static inline void *devres_remove(struct udevice *dev, dr_release_t release,
836e2282d70SMasahiro Yamada dr_match_t match, void *match_data)
837e2282d70SMasahiro Yamada {
838e2282d70SMasahiro Yamada return NULL;
839e2282d70SMasahiro Yamada }
840e2282d70SMasahiro Yamada
devres_destroy(struct udevice * dev,dr_release_t release,dr_match_t match,void * match_data)841e2282d70SMasahiro Yamada static inline int devres_destroy(struct udevice *dev, dr_release_t release,
842e2282d70SMasahiro Yamada dr_match_t match, void *match_data)
843e2282d70SMasahiro Yamada {
844e2282d70SMasahiro Yamada return 0;
845e2282d70SMasahiro Yamada }
846e2282d70SMasahiro Yamada
devres_release(struct udevice * dev,dr_release_t release,dr_match_t match,void * match_data)847e2282d70SMasahiro Yamada static inline int devres_release(struct udevice *dev, dr_release_t release,
848e2282d70SMasahiro Yamada dr_match_t match, void *match_data)
849e2282d70SMasahiro Yamada {
850e2282d70SMasahiro Yamada return 0;
851e2282d70SMasahiro Yamada }
852e2282d70SMasahiro Yamada
devm_kmalloc(struct udevice * dev,size_t size,gfp_t gfp)853e2282d70SMasahiro Yamada static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
854e2282d70SMasahiro Yamada {
855e2282d70SMasahiro Yamada return kmalloc(size, gfp);
856e2282d70SMasahiro Yamada }
857e2282d70SMasahiro Yamada
devm_kzalloc(struct udevice * dev,size_t size,gfp_t gfp)858e2282d70SMasahiro Yamada static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
859e2282d70SMasahiro Yamada {
860e2282d70SMasahiro Yamada return kzalloc(size, gfp);
861e2282d70SMasahiro Yamada }
862e2282d70SMasahiro Yamada
devm_kmaloc_array(struct udevice * dev,size_t n,size_t size,gfp_t flags)863e2282d70SMasahiro Yamada static inline void *devm_kmaloc_array(struct udevice *dev,
864e2282d70SMasahiro Yamada size_t n, size_t size, gfp_t flags)
865e2282d70SMasahiro Yamada {
866e2282d70SMasahiro Yamada /* TODO: add kmalloc_array() to linux/compat.h */
867e2282d70SMasahiro Yamada if (size != 0 && n > SIZE_MAX / size)
868e2282d70SMasahiro Yamada return NULL;
869e2282d70SMasahiro Yamada return kmalloc(n * size, flags);
870e2282d70SMasahiro Yamada }
871e2282d70SMasahiro Yamada
devm_kcalloc(struct udevice * dev,size_t n,size_t size,gfp_t flags)872e2282d70SMasahiro Yamada static inline void *devm_kcalloc(struct udevice *dev,
873e2282d70SMasahiro Yamada size_t n, size_t size, gfp_t flags)
874e2282d70SMasahiro Yamada {
875e2282d70SMasahiro Yamada /* TODO: add kcalloc() to linux/compat.h */
876e2282d70SMasahiro Yamada return kmalloc(n * size, flags | __GFP_ZERO);
877e2282d70SMasahiro Yamada }
878e2282d70SMasahiro Yamada
devm_kfree(struct udevice * dev,void * ptr)87993c7fe4aSSimon Glass static inline void devm_kfree(struct udevice *dev, void *ptr)
880e2282d70SMasahiro Yamada {
88193c7fe4aSSimon Glass kfree(ptr);
882e2282d70SMasahiro Yamada }
883e2282d70SMasahiro Yamada
884e2282d70SMasahiro Yamada #endif /* ! CONFIG_DEVRES */
8852b07f685SMasahiro Yamada
8868f1ef3f5SMasahiro Yamada /*
8878f1ef3f5SMasahiro Yamada * REVISIT:
8888f1ef3f5SMasahiro Yamada * remove the following after resolving conflicts with <linux/compat.h>
8898f1ef3f5SMasahiro Yamada */
8908f1ef3f5SMasahiro Yamada #ifdef dev_dbg
8918f1ef3f5SMasahiro Yamada #undef dev_dbg
8928f1ef3f5SMasahiro Yamada #endif
8938f1ef3f5SMasahiro Yamada #ifdef dev_vdbg
8948f1ef3f5SMasahiro Yamada #undef dev_vdbg
8958f1ef3f5SMasahiro Yamada #endif
8968f1ef3f5SMasahiro Yamada #ifdef dev_info
8978f1ef3f5SMasahiro Yamada #undef dev_info
8988f1ef3f5SMasahiro Yamada #endif
8998f1ef3f5SMasahiro Yamada #ifdef dev_err
9008f1ef3f5SMasahiro Yamada #undef dev_err
9018f1ef3f5SMasahiro Yamada #endif
9028f1ef3f5SMasahiro Yamada #ifdef dev_warn
9038f1ef3f5SMasahiro Yamada #undef dev_warn
9048f1ef3f5SMasahiro Yamada #endif
9058f1ef3f5SMasahiro Yamada
9068f1ef3f5SMasahiro Yamada /*
9078f1ef3f5SMasahiro Yamada * REVISIT:
9088f1ef3f5SMasahiro Yamada * print device name like Linux
9098f1ef3f5SMasahiro Yamada */
9108f1ef3f5SMasahiro Yamada #define dev_printk(dev, fmt, ...) \
9118f1ef3f5SMasahiro Yamada ({ \
9128f1ef3f5SMasahiro Yamada printk(fmt, ##__VA_ARGS__); \
9138f1ef3f5SMasahiro Yamada })
9148f1ef3f5SMasahiro Yamada
9158f1ef3f5SMasahiro Yamada #define __dev_printk(level, dev, fmt, ...) \
9168f1ef3f5SMasahiro Yamada ({ \
9178f1ef3f5SMasahiro Yamada if (level < CONFIG_VAL(LOGLEVEL)) \
9188f1ef3f5SMasahiro Yamada dev_printk(dev, fmt, ##__VA_ARGS__); \
9198f1ef3f5SMasahiro Yamada })
9208f1ef3f5SMasahiro Yamada
9218f1ef3f5SMasahiro Yamada #define dev_emerg(dev, fmt, ...) \
9228f1ef3f5SMasahiro Yamada __dev_printk(0, dev, fmt, ##__VA_ARGS__)
9238f1ef3f5SMasahiro Yamada #define dev_alert(dev, fmt, ...) \
9248f1ef3f5SMasahiro Yamada __dev_printk(1, dev, fmt, ##__VA_ARGS__)
9258f1ef3f5SMasahiro Yamada #define dev_crit(dev, fmt, ...) \
9268f1ef3f5SMasahiro Yamada __dev_printk(2, dev, fmt, ##__VA_ARGS__)
9278f1ef3f5SMasahiro Yamada #define dev_err(dev, fmt, ...) \
9288f1ef3f5SMasahiro Yamada __dev_printk(3, dev, fmt, ##__VA_ARGS__)
9298f1ef3f5SMasahiro Yamada #define dev_warn(dev, fmt, ...) \
9308f1ef3f5SMasahiro Yamada __dev_printk(4, dev, fmt, ##__VA_ARGS__)
9318f1ef3f5SMasahiro Yamada #define dev_notice(dev, fmt, ...) \
9328f1ef3f5SMasahiro Yamada __dev_printk(5, dev, fmt, ##__VA_ARGS__)
9338f1ef3f5SMasahiro Yamada #define dev_info(dev, fmt, ...) \
9348f1ef3f5SMasahiro Yamada __dev_printk(6, dev, fmt, ##__VA_ARGS__)
9358f1ef3f5SMasahiro Yamada
9368f1ef3f5SMasahiro Yamada #ifdef DEBUG
9378f1ef3f5SMasahiro Yamada #define dev_dbg(dev, fmt, ...) \
9388f1ef3f5SMasahiro Yamada __dev_printk(7, dev, fmt, ##__VA_ARGS__)
9398f1ef3f5SMasahiro Yamada #else
9408f1ef3f5SMasahiro Yamada #define dev_dbg(dev, fmt, ...) \
9418f1ef3f5SMasahiro Yamada ({ \
9428f1ef3f5SMasahiro Yamada if (0) \
9438f1ef3f5SMasahiro Yamada __dev_printk(7, dev, fmt, ##__VA_ARGS__); \
9448f1ef3f5SMasahiro Yamada })
9458f1ef3f5SMasahiro Yamada #endif
9468f1ef3f5SMasahiro Yamada
9478f1ef3f5SMasahiro Yamada #ifdef VERBOSE_DEBUG
9488f1ef3f5SMasahiro Yamada #define dev_vdbg dev_dbg
9498f1ef3f5SMasahiro Yamada #else
9508f1ef3f5SMasahiro Yamada #define dev_vdbg(dev, fmt, ...) \
9518f1ef3f5SMasahiro Yamada ({ \
9528f1ef3f5SMasahiro Yamada if (0) \
9538f1ef3f5SMasahiro Yamada __dev_printk(7, dev, fmt, ##__VA_ARGS__); \
9548f1ef3f5SMasahiro Yamada })
9558f1ef3f5SMasahiro Yamada #endif
9568f1ef3f5SMasahiro Yamada
9576494d708SSimon Glass #endif
958