1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2013 Google, Inc
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * (C) Copyright 2012
5*4882a593Smuzhiyun * Pavel Herrmann <morpheus.ibis@gmail.com>
6*4882a593Smuzhiyun * Marek Vasut <marex@denx.de>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #ifndef _DM_DEVICE_H
12*4882a593Smuzhiyun #define _DM_DEVICE_H
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <dm/ofnode.h>
15*4882a593Smuzhiyun #include <dm/uclass-id.h>
16*4882a593Smuzhiyun #include <fdtdec.h>
17*4882a593Smuzhiyun #include <linker_lists.h>
18*4882a593Smuzhiyun #include <linux/compat.h>
19*4882a593Smuzhiyun #include <linux/kernel.h>
20*4882a593Smuzhiyun #include <linux/list.h>
21*4882a593Smuzhiyun #include <linux/printk.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun struct driver_info;
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /* Driver is active (probed). Cleared when it is removed */
26*4882a593Smuzhiyun #define DM_FLAG_ACTIVATED (1 << 0)
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* DM is responsible for allocating and freeing platdata */
29*4882a593Smuzhiyun #define DM_FLAG_ALLOC_PDATA (1 << 1)
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* DM should init this device prior to relocation */
32*4882a593Smuzhiyun #define DM_FLAG_PRE_RELOC (1 << 2)
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /* DM is responsible for allocating and freeing parent_platdata */
35*4882a593Smuzhiyun #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* DM is responsible for allocating and freeing uclass_platdata */
38*4882a593Smuzhiyun #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* Allocate driver private data on a DMA boundary */
41*4882a593Smuzhiyun #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* Device is bound */
44*4882a593Smuzhiyun #define DM_FLAG_BOUND (1 << 6)
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* Device name is allocated and should be freed on unbind() */
47*4882a593Smuzhiyun #define DM_FLAG_NAME_ALLOCED (1 << 7)
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define DM_FLAG_OF_PLATDATA (1 << 8)
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun * Call driver remove function to stop currently active DMA transfers or
53*4882a593Smuzhiyun * give DMA buffers back to the HW / controller. This may be needed for
54*4882a593Smuzhiyun * some drivers to do some final stage cleanup before the OS is called
55*4882a593Smuzhiyun * (U-Boot exit)
56*4882a593Smuzhiyun */
57*4882a593Smuzhiyun #define DM_FLAG_ACTIVE_DMA (1 << 9)
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * Call driver remove function to do some final configuration, before
61*4882a593Smuzhiyun * U-Boot exits and the OS is started
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun #define DM_FLAG_OS_PREPARE (1 << 10)
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* Device is from kernel dtb */
66*4882a593Smuzhiyun #define DM_FLAG_KNRL_DTB (1 << 31)
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * One or multiple of these flags are passed to device_remove() so that
70*4882a593Smuzhiyun * a selective device removal as specified by the remove-stage and the
71*4882a593Smuzhiyun * driver flags can be done.
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun enum {
74*4882a593Smuzhiyun /* Normal remove, remove all devices */
75*4882a593Smuzhiyun DM_REMOVE_NORMAL = 1 << 0,
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* Remove devices with active DMA */
78*4882a593Smuzhiyun DM_REMOVE_ACTIVE_DMA = DM_FLAG_ACTIVE_DMA,
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Remove devices which need some final OS preparation steps */
81*4882a593Smuzhiyun DM_REMOVE_OS_PREPARE = DM_FLAG_OS_PREPARE,
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Add more use cases here */
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* Remove devices with any active flag */
86*4882a593Smuzhiyun DM_REMOVE_ACTIVE_ALL = DM_REMOVE_ACTIVE_DMA | DM_REMOVE_OS_PREPARE,
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /**
90*4882a593Smuzhiyun * struct udevice - An instance of a driver
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * This holds information about a device, which is a driver bound to a
93*4882a593Smuzhiyun * particular port or peripheral (essentially a driver instance).
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * A device will come into existence through a 'bind' call, either due to
96*4882a593Smuzhiyun * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
97*4882a593Smuzhiyun * in the device tree (in which case of_offset is >= 0). In the latter case
98*4882a593Smuzhiyun * we translate the device tree information into platdata in a function
99*4882a593Smuzhiyun * implemented by the driver ofdata_to_platdata method (called just before the
100*4882a593Smuzhiyun * probe method if the device has a device tree node.
101*4882a593Smuzhiyun *
102*4882a593Smuzhiyun * All three of platdata, priv and uclass_priv can be allocated by the
103*4882a593Smuzhiyun * driver, or you can use the auto_alloc_size members of struct driver and
104*4882a593Smuzhiyun * struct uclass_driver to have driver model do this automatically.
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * @driver: The driver used by this device
107*4882a593Smuzhiyun * @name: Name of device, typically the FDT node name
108*4882a593Smuzhiyun * @platdata: Configuration data for this device
109*4882a593Smuzhiyun * @parent_platdata: The parent bus's configuration data for this device
110*4882a593Smuzhiyun * @uclass_platdata: The uclass's configuration data for this device
111*4882a593Smuzhiyun * @node: Reference to device tree node for this device
112*4882a593Smuzhiyun * @driver_data: Driver data word for the entry that matched this device with
113*4882a593Smuzhiyun * its driver
114*4882a593Smuzhiyun * @parent: Parent of this device, or NULL for the top level device
115*4882a593Smuzhiyun * @priv: Private data for this device
116*4882a593Smuzhiyun * @uclass: Pointer to uclass for this device
117*4882a593Smuzhiyun * @uclass_priv: The uclass's private data for this device
118*4882a593Smuzhiyun * @parent_priv: The parent's private data for this device
119*4882a593Smuzhiyun * @uclass_node: Used by uclass to link its devices
120*4882a593Smuzhiyun * @child_head: List of children of this device
121*4882a593Smuzhiyun * @sibling_node: Next device in list of all devices
122*4882a593Smuzhiyun * @flags: Flags for this device DM_FLAG_...
123*4882a593Smuzhiyun * @req_seq: Requested sequence number for this device (-1 = any)
124*4882a593Smuzhiyun * @seq: Allocated sequence number for this device (-1 = none). This is set up
125*4882a593Smuzhiyun * when the device is probed and will be unique within the device's uclass.
126*4882a593Smuzhiyun * @devres_head: List of memory allocations associated with this device.
127*4882a593Smuzhiyun * When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
128*4882a593Smuzhiyun * add to this list. Memory so-allocated will be freed
129*4882a593Smuzhiyun * automatically when the device is removed / unbound
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun struct udevice {
132*4882a593Smuzhiyun const struct driver *driver;
133*4882a593Smuzhiyun const char *name;
134*4882a593Smuzhiyun void *platdata;
135*4882a593Smuzhiyun void *parent_platdata;
136*4882a593Smuzhiyun void *uclass_platdata;
137*4882a593Smuzhiyun ofnode node;
138*4882a593Smuzhiyun ulong driver_data;
139*4882a593Smuzhiyun struct udevice *parent;
140*4882a593Smuzhiyun void *priv;
141*4882a593Smuzhiyun struct uclass *uclass;
142*4882a593Smuzhiyun void *uclass_priv;
143*4882a593Smuzhiyun void *parent_priv;
144*4882a593Smuzhiyun struct list_head uclass_node;
145*4882a593Smuzhiyun struct list_head child_head;
146*4882a593Smuzhiyun struct list_head sibling_node;
147*4882a593Smuzhiyun uint32_t flags;
148*4882a593Smuzhiyun int req_seq;
149*4882a593Smuzhiyun int seq;
150*4882a593Smuzhiyun #ifdef CONFIG_DEVRES
151*4882a593Smuzhiyun struct list_head devres_head;
152*4882a593Smuzhiyun #endif
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* Maximum sequence number supported */
156*4882a593Smuzhiyun #define DM_MAX_SEQ 999
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /* Returns the operations for a device */
159*4882a593Smuzhiyun #define device_get_ops(dev) (dev->driver->ops)
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* Returns non-zero if the device is active (probed and not removed) */
162*4882a593Smuzhiyun #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
163*4882a593Smuzhiyun
dev_of_offset(const struct udevice * dev)164*4882a593Smuzhiyun static inline int dev_of_offset(const struct udevice *dev)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun return ofnode_to_offset(dev->node);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
dev_set_of_offset(struct udevice * dev,int of_offset)169*4882a593Smuzhiyun static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun dev->node = offset_to_ofnode(of_offset);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
dev_has_of_node(struct udevice * dev)174*4882a593Smuzhiyun static inline bool dev_has_of_node(struct udevice *dev)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun return ofnode_valid(dev->node);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /**
180*4882a593Smuzhiyun * struct udevice_id - Lists the compatible strings supported by a driver
181*4882a593Smuzhiyun * @compatible: Compatible string
182*4882a593Smuzhiyun * @data: Data for this compatible string
183*4882a593Smuzhiyun */
184*4882a593Smuzhiyun struct udevice_id {
185*4882a593Smuzhiyun const char *compatible;
186*4882a593Smuzhiyun ulong data;
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_CONTROL)
190*4882a593Smuzhiyun #define of_match_ptr(_ptr) (_ptr)
191*4882a593Smuzhiyun #else
192*4882a593Smuzhiyun #define of_match_ptr(_ptr) NULL
193*4882a593Smuzhiyun #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /**
196*4882a593Smuzhiyun * struct driver - A driver for a feature or peripheral
197*4882a593Smuzhiyun *
198*4882a593Smuzhiyun * This holds methods for setting up a new device, and also removing it.
199*4882a593Smuzhiyun * The device needs information to set itself up - this is provided either
200*4882a593Smuzhiyun * by platdata or a device tree node (which we find by looking up
201*4882a593Smuzhiyun * matching compatible strings with of_match).
202*4882a593Smuzhiyun *
203*4882a593Smuzhiyun * Drivers all belong to a uclass, representing a class of devices of the
204*4882a593Smuzhiyun * same type. Common elements of the drivers can be implemented in the uclass,
205*4882a593Smuzhiyun * or the uclass can provide a consistent interface to the drivers within
206*4882a593Smuzhiyun * it.
207*4882a593Smuzhiyun *
208*4882a593Smuzhiyun * @name: Device name
209*4882a593Smuzhiyun * @id: Identiies the uclass we belong to
210*4882a593Smuzhiyun * @of_match: List of compatible strings to match, and any identifying data
211*4882a593Smuzhiyun * for each.
212*4882a593Smuzhiyun * @bind: Called to bind a device to its driver
213*4882a593Smuzhiyun * @probe: Called to probe a device, i.e. activate it
214*4882a593Smuzhiyun * @remove: Called to remove a device, i.e. de-activate it
215*4882a593Smuzhiyun * @unbind: Called to unbind a device from its driver
216*4882a593Smuzhiyun * @ofdata_to_platdata: Called before probe to decode device tree data
217*4882a593Smuzhiyun * @child_post_bind: Called after a new child has been bound
218*4882a593Smuzhiyun * @child_pre_probe: Called before a child device is probed. The device has
219*4882a593Smuzhiyun * memory allocated but it has not yet been probed.
220*4882a593Smuzhiyun * @child_post_remove: Called after a child device is removed. The device
221*4882a593Smuzhiyun * has memory allocated but its device_remove() method has been called.
222*4882a593Smuzhiyun * @priv_auto_alloc_size: If non-zero this is the size of the private data
223*4882a593Smuzhiyun * to be allocated in the device's ->priv pointer. If zero, then the driver
224*4882a593Smuzhiyun * is responsible for allocating any data required.
225*4882a593Smuzhiyun * @platdata_auto_alloc_size: If non-zero this is the size of the
226*4882a593Smuzhiyun * platform data to be allocated in the device's ->platdata pointer.
227*4882a593Smuzhiyun * This is typically only useful for device-tree-aware drivers (those with
228*4882a593Smuzhiyun * an of_match), since drivers which use platdata will have the data
229*4882a593Smuzhiyun * provided in the U_BOOT_DEVICE() instantiation.
230*4882a593Smuzhiyun * @per_child_auto_alloc_size: Each device can hold private data owned by
231*4882a593Smuzhiyun * its parent. If required this will be automatically allocated if this
232*4882a593Smuzhiyun * value is non-zero.
233*4882a593Smuzhiyun * @per_child_platdata_auto_alloc_size: A bus likes to store information about
234*4882a593Smuzhiyun * its children. If non-zero this is the size of this data, to be allocated
235*4882a593Smuzhiyun * in the child's parent_platdata pointer.
236*4882a593Smuzhiyun * @ops: Driver-specific operations. This is typically a list of function
237*4882a593Smuzhiyun * pointers defined by the driver, to implement driver functions required by
238*4882a593Smuzhiyun * the uclass.
239*4882a593Smuzhiyun * @flags: driver flags - see DM_FLAGS_...
240*4882a593Smuzhiyun */
241*4882a593Smuzhiyun struct driver {
242*4882a593Smuzhiyun char *name;
243*4882a593Smuzhiyun enum uclass_id id;
244*4882a593Smuzhiyun const struct udevice_id *of_match;
245*4882a593Smuzhiyun int (*bind)(struct udevice *dev);
246*4882a593Smuzhiyun int (*probe)(struct udevice *dev);
247*4882a593Smuzhiyun int (*remove)(struct udevice *dev);
248*4882a593Smuzhiyun int (*unbind)(struct udevice *dev);
249*4882a593Smuzhiyun int (*ofdata_to_platdata)(struct udevice *dev);
250*4882a593Smuzhiyun int (*child_post_bind)(struct udevice *dev);
251*4882a593Smuzhiyun int (*child_pre_probe)(struct udevice *dev);
252*4882a593Smuzhiyun int (*child_post_remove)(struct udevice *dev);
253*4882a593Smuzhiyun int priv_auto_alloc_size;
254*4882a593Smuzhiyun int platdata_auto_alloc_size;
255*4882a593Smuzhiyun int per_child_auto_alloc_size;
256*4882a593Smuzhiyun int per_child_platdata_auto_alloc_size;
257*4882a593Smuzhiyun const void *ops; /* driver-specific operations */
258*4882a593Smuzhiyun uint32_t flags;
259*4882a593Smuzhiyun };
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /* Declare a new U-Boot driver */
262*4882a593Smuzhiyun #define U_BOOT_DRIVER(__name) \
263*4882a593Smuzhiyun ll_entry_declare(struct driver, __name, driver)
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /* Get a pointer to a given driver */
266*4882a593Smuzhiyun #define DM_GET_DRIVER(__name) \
267*4882a593Smuzhiyun ll_entry_get(struct driver, __name, driver)
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /**
270*4882a593Smuzhiyun * dev_get_platdata() - Get the platform data for a device
271*4882a593Smuzhiyun *
272*4882a593Smuzhiyun * This checks that dev is not NULL, but no other checks for now
273*4882a593Smuzhiyun *
274*4882a593Smuzhiyun * @dev Device to check
275*4882a593Smuzhiyun * @return platform data, or NULL if none
276*4882a593Smuzhiyun */
277*4882a593Smuzhiyun void *dev_get_platdata(struct udevice *dev);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /**
280*4882a593Smuzhiyun * dev_get_parent_platdata() - Get the parent platform data for a device
281*4882a593Smuzhiyun *
282*4882a593Smuzhiyun * This checks that dev is not NULL, but no other checks for now
283*4882a593Smuzhiyun *
284*4882a593Smuzhiyun * @dev Device to check
285*4882a593Smuzhiyun * @return parent's platform data, or NULL if none
286*4882a593Smuzhiyun */
287*4882a593Smuzhiyun void *dev_get_parent_platdata(struct udevice *dev);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /**
290*4882a593Smuzhiyun * dev_get_uclass_platdata() - Get the uclass platform data for a device
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * This checks that dev is not NULL, but no other checks for now
293*4882a593Smuzhiyun *
294*4882a593Smuzhiyun * @dev Device to check
295*4882a593Smuzhiyun * @return uclass's platform data, or NULL if none
296*4882a593Smuzhiyun */
297*4882a593Smuzhiyun void *dev_get_uclass_platdata(struct udevice *dev);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /**
300*4882a593Smuzhiyun * dev_get_priv() - Get the private data for a device
301*4882a593Smuzhiyun *
302*4882a593Smuzhiyun * This checks that dev is not NULL, but no other checks for now
303*4882a593Smuzhiyun *
304*4882a593Smuzhiyun * @dev Device to check
305*4882a593Smuzhiyun * @return private data, or NULL if none
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun void *dev_get_priv(struct udevice *dev);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /**
310*4882a593Smuzhiyun * dev_get_parent_priv() - Get the parent private data for a device
311*4882a593Smuzhiyun *
312*4882a593Smuzhiyun * The parent private data is data stored in the device but owned by the
313*4882a593Smuzhiyun * parent. For example, a USB device may have parent data which contains
314*4882a593Smuzhiyun * information about how to talk to the device over USB.
315*4882a593Smuzhiyun *
316*4882a593Smuzhiyun * This checks that dev is not NULL, but no other checks for now
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * @dev Device to check
319*4882a593Smuzhiyun * @return parent data, or NULL if none
320*4882a593Smuzhiyun */
321*4882a593Smuzhiyun void *dev_get_parent_priv(struct udevice *dev);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /**
324*4882a593Smuzhiyun * dev_get_uclass_priv() - Get the private uclass data for a device
325*4882a593Smuzhiyun *
326*4882a593Smuzhiyun * This checks that dev is not NULL, but no other checks for now
327*4882a593Smuzhiyun *
328*4882a593Smuzhiyun * @dev Device to check
329*4882a593Smuzhiyun * @return private uclass data for this device, or NULL if none
330*4882a593Smuzhiyun */
331*4882a593Smuzhiyun void *dev_get_uclass_priv(struct udevice *dev);
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /**
334*4882a593Smuzhiyun * struct dev_get_parent() - Get the parent of a device
335*4882a593Smuzhiyun *
336*4882a593Smuzhiyun * @child: Child to check
337*4882a593Smuzhiyun * @return parent of child, or NULL if this is the root device
338*4882a593Smuzhiyun */
339*4882a593Smuzhiyun struct udevice *dev_get_parent(struct udevice *child);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /**
342*4882a593Smuzhiyun * dev_get_driver_data() - get the driver data used to bind a device
343*4882a593Smuzhiyun *
344*4882a593Smuzhiyun * When a device is bound using a device tree node, it matches a
345*4882a593Smuzhiyun * particular compatible string in struct udevice_id. This function
346*4882a593Smuzhiyun * returns the associated data value for that compatible string. This is
347*4882a593Smuzhiyun * the 'data' field in struct udevice_id.
348*4882a593Smuzhiyun *
349*4882a593Smuzhiyun * As an example, consider this structure:
350*4882a593Smuzhiyun * static const struct udevice_id tegra_i2c_ids[] = {
351*4882a593Smuzhiyun * { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
352*4882a593Smuzhiyun * { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
353*4882a593Smuzhiyun * { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
354*4882a593Smuzhiyun * { }
355*4882a593Smuzhiyun * };
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * When driver model finds a driver for this it will store the 'data' value
358*4882a593Smuzhiyun * corresponding to the compatible string it matches. This function returns
359*4882a593Smuzhiyun * that value. This allows the driver to handle several variants of a device.
360*4882a593Smuzhiyun *
361*4882a593Smuzhiyun * For USB devices, this is the driver_info field in struct usb_device_id.
362*4882a593Smuzhiyun *
363*4882a593Smuzhiyun * @dev: Device to check
364*4882a593Smuzhiyun * @return driver data (0 if none is provided)
365*4882a593Smuzhiyun */
366*4882a593Smuzhiyun ulong dev_get_driver_data(struct udevice *dev);
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /**
369*4882a593Smuzhiyun * dev_get_driver_ops() - get the device's driver's operations
370*4882a593Smuzhiyun *
371*4882a593Smuzhiyun * This checks that dev is not NULL, and returns the pointer to device's
372*4882a593Smuzhiyun * driver's operations.
373*4882a593Smuzhiyun *
374*4882a593Smuzhiyun * @dev: Device to check
375*4882a593Smuzhiyun * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
376*4882a593Smuzhiyun */
377*4882a593Smuzhiyun const void *dev_get_driver_ops(struct udevice *dev);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /**
380*4882a593Smuzhiyun * device_get_uclass_id() - return the uclass ID of a device
381*4882a593Smuzhiyun *
382*4882a593Smuzhiyun * @dev: Device to check
383*4882a593Smuzhiyun * @return uclass ID for the device
384*4882a593Smuzhiyun */
385*4882a593Smuzhiyun enum uclass_id device_get_uclass_id(struct udevice *dev);
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun /**
388*4882a593Smuzhiyun * dev_get_uclass_name() - return the uclass name of a device
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * This checks that dev is not NULL.
391*4882a593Smuzhiyun *
392*4882a593Smuzhiyun * @dev: Device to check
393*4882a593Smuzhiyun * @return pointer to the uclass name for the device
394*4882a593Smuzhiyun */
395*4882a593Smuzhiyun const char *dev_get_uclass_name(struct udevice *dev);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /**
398*4882a593Smuzhiyun * device_get_child() - Get the child of a device by index
399*4882a593Smuzhiyun *
400*4882a593Smuzhiyun * Returns the numbered child, 0 being the first. This does not use
401*4882a593Smuzhiyun * sequence numbers, only the natural order.
402*4882a593Smuzhiyun *
403*4882a593Smuzhiyun * @dev: Parent device to check
404*4882a593Smuzhiyun * @index: Child index
405*4882a593Smuzhiyun * @devp: Returns pointer to device
406*4882a593Smuzhiyun * @return 0 if OK, -ENODEV if no such device, other error if the device fails
407*4882a593Smuzhiyun * to probe
408*4882a593Smuzhiyun */
409*4882a593Smuzhiyun int device_get_child(struct udevice *parent, int index, struct udevice **devp);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun /**
412*4882a593Smuzhiyun * device_find_child_by_seq() - Find a child device based on a sequence
413*4882a593Smuzhiyun *
414*4882a593Smuzhiyun * This searches for a device with the given seq or req_seq.
415*4882a593Smuzhiyun *
416*4882a593Smuzhiyun * For seq, if an active device has this sequence it will be returned.
417*4882a593Smuzhiyun * If there is no such device then this will return -ENODEV.
418*4882a593Smuzhiyun *
419*4882a593Smuzhiyun * For req_seq, if a device (whether activated or not) has this req_seq
420*4882a593Smuzhiyun * value, that device will be returned. This is a strong indication that
421*4882a593Smuzhiyun * the device will receive that sequence when activated.
422*4882a593Smuzhiyun *
423*4882a593Smuzhiyun * @parent: Parent device
424*4882a593Smuzhiyun * @seq_or_req_seq: Sequence number to find (0=first)
425*4882a593Smuzhiyun * @find_req_seq: true to find req_seq, false to find seq
426*4882a593Smuzhiyun * @devp: Returns pointer to device (there is only one per for each seq).
427*4882a593Smuzhiyun * Set to NULL if none is found
428*4882a593Smuzhiyun * @return 0 if OK, -ve on error
429*4882a593Smuzhiyun */
430*4882a593Smuzhiyun int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
431*4882a593Smuzhiyun bool find_req_seq, struct udevice **devp);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /**
434*4882a593Smuzhiyun * device_get_child_by_seq() - Get a child device based on a sequence
435*4882a593Smuzhiyun *
436*4882a593Smuzhiyun * If an active device has this sequence it will be returned. If there is no
437*4882a593Smuzhiyun * such device then this will check for a device that is requesting this
438*4882a593Smuzhiyun * sequence.
439*4882a593Smuzhiyun *
440*4882a593Smuzhiyun * The device is probed to activate it ready for use.
441*4882a593Smuzhiyun *
442*4882a593Smuzhiyun * @parent: Parent device
443*4882a593Smuzhiyun * @seq: Sequence number to find (0=first)
444*4882a593Smuzhiyun * @devp: Returns pointer to device (there is only one per for each seq)
445*4882a593Smuzhiyun * Set to NULL if none is found
446*4882a593Smuzhiyun * @return 0 if OK, -ve on error
447*4882a593Smuzhiyun */
448*4882a593Smuzhiyun int device_get_child_by_seq(struct udevice *parent, int seq,
449*4882a593Smuzhiyun struct udevice **devp);
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun /**
452*4882a593Smuzhiyun * device_find_child_by_of_offset() - Find a child device based on FDT offset
453*4882a593Smuzhiyun *
454*4882a593Smuzhiyun * Locates a child device by its device tree offset.
455*4882a593Smuzhiyun *
456*4882a593Smuzhiyun * @parent: Parent device
457*4882a593Smuzhiyun * @of_offset: Device tree offset to find
458*4882a593Smuzhiyun * @devp: Returns pointer to device if found, otherwise this is set to NULL
459*4882a593Smuzhiyun * @return 0 if OK, -ve on error
460*4882a593Smuzhiyun */
461*4882a593Smuzhiyun int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
462*4882a593Smuzhiyun struct udevice **devp);
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun /**
465*4882a593Smuzhiyun * device_get_child_by_of_offset() - Get a child device based on FDT offset
466*4882a593Smuzhiyun *
467*4882a593Smuzhiyun * Locates a child device by its device tree offset.
468*4882a593Smuzhiyun *
469*4882a593Smuzhiyun * The device is probed to activate it ready for use.
470*4882a593Smuzhiyun *
471*4882a593Smuzhiyun * @parent: Parent device
472*4882a593Smuzhiyun * @of_offset: Device tree offset to find
473*4882a593Smuzhiyun * @devp: Returns pointer to device if found, otherwise this is set to NULL
474*4882a593Smuzhiyun * @return 0 if OK, -ve on error
475*4882a593Smuzhiyun */
476*4882a593Smuzhiyun int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
477*4882a593Smuzhiyun struct udevice **devp);
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun /**
480*4882a593Smuzhiyun * device_get_global_by_of_offset() - Get a device based on FDT offset
481*4882a593Smuzhiyun *
482*4882a593Smuzhiyun * Locates a device by its device tree offset, searching globally throughout
483*4882a593Smuzhiyun * the all driver model devices.
484*4882a593Smuzhiyun *
485*4882a593Smuzhiyun * The device is probed to activate it ready for use.
486*4882a593Smuzhiyun *
487*4882a593Smuzhiyun * @of_offset: Device tree offset to find
488*4882a593Smuzhiyun * @devp: Returns pointer to device if found, otherwise this is set to NULL
489*4882a593Smuzhiyun * @return 0 if OK, -ve on error
490*4882a593Smuzhiyun */
491*4882a593Smuzhiyun int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun /**
494*4882a593Smuzhiyun * device_find_first_child() - Find the first child of a device
495*4882a593Smuzhiyun *
496*4882a593Smuzhiyun * @parent: Parent device to search
497*4882a593Smuzhiyun * @devp: Returns first child device, or NULL if none
498*4882a593Smuzhiyun * @return 0
499*4882a593Smuzhiyun */
500*4882a593Smuzhiyun int device_find_first_child(struct udevice *parent, struct udevice **devp);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun /**
503*4882a593Smuzhiyun * device_find_next_child() - Find the next child of a device
504*4882a593Smuzhiyun *
505*4882a593Smuzhiyun * @devp: Pointer to previous child device on entry. Returns pointer to next
506*4882a593Smuzhiyun * child device, or NULL if none
507*4882a593Smuzhiyun * @return 0
508*4882a593Smuzhiyun */
509*4882a593Smuzhiyun int device_find_next_child(struct udevice **devp);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun * device_has_children() - check if a device has any children
513*4882a593Smuzhiyun *
514*4882a593Smuzhiyun * @dev: Device to check
515*4882a593Smuzhiyun * @return true if the device has one or more children
516*4882a593Smuzhiyun */
517*4882a593Smuzhiyun bool device_has_children(struct udevice *dev);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun /**
520*4882a593Smuzhiyun * device_has_active_children() - check if a device has any active children
521*4882a593Smuzhiyun *
522*4882a593Smuzhiyun * @dev: Device to check
523*4882a593Smuzhiyun * @return true if the device has one or more children and at least one of
524*4882a593Smuzhiyun * them is active (probed).
525*4882a593Smuzhiyun */
526*4882a593Smuzhiyun bool device_has_active_children(struct udevice *dev);
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun /**
529*4882a593Smuzhiyun * device_is_last_sibling() - check if a device is the last sibling
530*4882a593Smuzhiyun *
531*4882a593Smuzhiyun * This function can be useful for display purposes, when special action needs
532*4882a593Smuzhiyun * to be taken when displaying the last sibling. This can happen when a tree
533*4882a593Smuzhiyun * view of devices is being displayed.
534*4882a593Smuzhiyun *
535*4882a593Smuzhiyun * @dev: Device to check
536*4882a593Smuzhiyun * @return true if there are no more siblings after this one - i.e. is it
537*4882a593Smuzhiyun * last in the list.
538*4882a593Smuzhiyun */
539*4882a593Smuzhiyun bool device_is_last_sibling(struct udevice *dev);
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun /**
542*4882a593Smuzhiyun * device_set_name() - set the name of a device
543*4882a593Smuzhiyun *
544*4882a593Smuzhiyun * This must be called in the device's bind() method and no later. Normally
545*4882a593Smuzhiyun * this is unnecessary but for probed devices which don't get a useful name
546*4882a593Smuzhiyun * this function can be helpful.
547*4882a593Smuzhiyun *
548*4882a593Smuzhiyun * The name is allocated and will be freed automatically when the device is
549*4882a593Smuzhiyun * unbound.
550*4882a593Smuzhiyun *
551*4882a593Smuzhiyun * @dev: Device to update
552*4882a593Smuzhiyun * @name: New name (this string is allocated new memory and attached to
553*4882a593Smuzhiyun * the device)
554*4882a593Smuzhiyun * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
555*4882a593Smuzhiyun * string
556*4882a593Smuzhiyun */
557*4882a593Smuzhiyun int device_set_name(struct udevice *dev, const char *name);
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun /**
560*4882a593Smuzhiyun * device_set_name_alloced() - note that a device name is allocated
561*4882a593Smuzhiyun *
562*4882a593Smuzhiyun * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
563*4882a593Smuzhiyun * unbound the name will be freed. This avoids memory leaks.
564*4882a593Smuzhiyun *
565*4882a593Smuzhiyun * @dev: Device to update
566*4882a593Smuzhiyun */
567*4882a593Smuzhiyun void device_set_name_alloced(struct udevice *dev);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /**
570*4882a593Smuzhiyun * device_is_compatible() - check if the device is compatible with the compat
571*4882a593Smuzhiyun *
572*4882a593Smuzhiyun * This allows to check whether the device is comaptible with the compat.
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * @dev: udevice pointer for which compatible needs to be verified.
575*4882a593Smuzhiyun * @compat: Compatible string which needs to verified in the given
576*4882a593Smuzhiyun * device
577*4882a593Smuzhiyun * @return true if OK, false if the compatible is not found
578*4882a593Smuzhiyun */
579*4882a593Smuzhiyun bool device_is_compatible(struct udevice *dev, const char *compat);
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /**
582*4882a593Smuzhiyun * of_machine_is_compatible() - check if the machine is compatible with
583*4882a593Smuzhiyun * the compat
584*4882a593Smuzhiyun *
585*4882a593Smuzhiyun * This allows to check whether the machine is comaptible with the compat.
586*4882a593Smuzhiyun *
587*4882a593Smuzhiyun * @compat: Compatible string which needs to verified
588*4882a593Smuzhiyun * @return true if OK, false if the compatible is not found
589*4882a593Smuzhiyun */
590*4882a593Smuzhiyun bool of_machine_is_compatible(const char *compat);
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /**
593*4882a593Smuzhiyun * device_is_on_pci_bus - Test if a device is on a PCI bus
594*4882a593Smuzhiyun *
595*4882a593Smuzhiyun * @dev: device to test
596*4882a593Smuzhiyun * @return: true if it is on a PCI bus, false otherwise
597*4882a593Smuzhiyun */
device_is_on_pci_bus(struct udevice * dev)598*4882a593Smuzhiyun static inline bool device_is_on_pci_bus(struct udevice *dev)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun return device_get_uclass_id(dev->parent) == UCLASS_PCI;
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun /**
604*4882a593Smuzhiyun * device_foreach_child_safe() - iterate through child devices safely
605*4882a593Smuzhiyun *
606*4882a593Smuzhiyun * This allows the @pos child to be removed in the loop if required.
607*4882a593Smuzhiyun *
608*4882a593Smuzhiyun * @pos: struct udevice * for the current device
609*4882a593Smuzhiyun * @next: struct udevice * for the next device
610*4882a593Smuzhiyun * @parent: parent device to scan
611*4882a593Smuzhiyun */
612*4882a593Smuzhiyun #define device_foreach_child_safe(pos, next, parent) \
613*4882a593Smuzhiyun list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun /**
616*4882a593Smuzhiyun * dm_scan_fdt_dev() - Bind child device in a the device tree
617*4882a593Smuzhiyun *
618*4882a593Smuzhiyun * This handles device which have sub-nodes in the device tree. It scans all
619*4882a593Smuzhiyun * sub-nodes and binds drivers for each node where a driver can be found.
620*4882a593Smuzhiyun *
621*4882a593Smuzhiyun * If this is called prior to relocation, only pre-relocation devices will be
622*4882a593Smuzhiyun * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
623*4882a593Smuzhiyun * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
624*4882a593Smuzhiyun * be bound.
625*4882a593Smuzhiyun *
626*4882a593Smuzhiyun * @dev: Device to scan
627*4882a593Smuzhiyun * @return 0 if OK, -ve on error
628*4882a593Smuzhiyun */
629*4882a593Smuzhiyun int dm_scan_fdt_dev(struct udevice *dev);
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun /* device resource management */
632*4882a593Smuzhiyun typedef void (*dr_release_t)(struct udevice *dev, void *res);
633*4882a593Smuzhiyun typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun #ifdef CONFIG_DEVRES
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_DEVRES
638*4882a593Smuzhiyun void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
639*4882a593Smuzhiyun const char *name);
640*4882a593Smuzhiyun #define _devres_alloc(release, size, gfp) \
641*4882a593Smuzhiyun __devres_alloc(release, size, gfp, #release)
642*4882a593Smuzhiyun #else
643*4882a593Smuzhiyun void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
644*4882a593Smuzhiyun #endif
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun /**
647*4882a593Smuzhiyun * devres_alloc() - Allocate device resource data
648*4882a593Smuzhiyun * @release: Release function devres will be associated with
649*4882a593Smuzhiyun * @size: Allocation size
650*4882a593Smuzhiyun * @gfp: Allocation flags
651*4882a593Smuzhiyun *
652*4882a593Smuzhiyun * Allocate devres of @size bytes. The allocated area is associated
653*4882a593Smuzhiyun * with @release. The returned pointer can be passed to
654*4882a593Smuzhiyun * other devres_*() functions.
655*4882a593Smuzhiyun *
656*4882a593Smuzhiyun * RETURNS:
657*4882a593Smuzhiyun * Pointer to allocated devres on success, NULL on failure.
658*4882a593Smuzhiyun */
659*4882a593Smuzhiyun #define devres_alloc(release, size, gfp) \
660*4882a593Smuzhiyun _devres_alloc(release, size, gfp | __GFP_ZERO)
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun /**
663*4882a593Smuzhiyun * devres_free() - Free device resource data
664*4882a593Smuzhiyun * @res: Pointer to devres data to free
665*4882a593Smuzhiyun *
666*4882a593Smuzhiyun * Free devres created with devres_alloc().
667*4882a593Smuzhiyun */
668*4882a593Smuzhiyun void devres_free(void *res);
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun /**
671*4882a593Smuzhiyun * devres_add() - Register device resource
672*4882a593Smuzhiyun * @dev: Device to add resource to
673*4882a593Smuzhiyun * @res: Resource to register
674*4882a593Smuzhiyun *
675*4882a593Smuzhiyun * Register devres @res to @dev. @res should have been allocated
676*4882a593Smuzhiyun * using devres_alloc(). On driver detach, the associated release
677*4882a593Smuzhiyun * function will be invoked and devres will be freed automatically.
678*4882a593Smuzhiyun */
679*4882a593Smuzhiyun void devres_add(struct udevice *dev, void *res);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun /**
682*4882a593Smuzhiyun * devres_find() - Find device resource
683*4882a593Smuzhiyun * @dev: Device to lookup resource from
684*4882a593Smuzhiyun * @release: Look for resources associated with this release function
685*4882a593Smuzhiyun * @match: Match function (optional)
686*4882a593Smuzhiyun * @match_data: Data for the match function
687*4882a593Smuzhiyun *
688*4882a593Smuzhiyun * Find the latest devres of @dev which is associated with @release
689*4882a593Smuzhiyun * and for which @match returns 1. If @match is NULL, it's considered
690*4882a593Smuzhiyun * to match all.
691*4882a593Smuzhiyun *
692*4882a593Smuzhiyun * @return pointer to found devres, NULL if not found.
693*4882a593Smuzhiyun */
694*4882a593Smuzhiyun void *devres_find(struct udevice *dev, dr_release_t release,
695*4882a593Smuzhiyun dr_match_t match, void *match_data);
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun /**
698*4882a593Smuzhiyun * devres_get() - Find devres, if non-existent, add one atomically
699*4882a593Smuzhiyun * @dev: Device to lookup or add devres for
700*4882a593Smuzhiyun * @new_res: Pointer to new initialized devres to add if not found
701*4882a593Smuzhiyun * @match: Match function (optional)
702*4882a593Smuzhiyun * @match_data: Data for the match function
703*4882a593Smuzhiyun *
704*4882a593Smuzhiyun * Find the latest devres of @dev which has the same release function
705*4882a593Smuzhiyun * as @new_res and for which @match return 1. If found, @new_res is
706*4882a593Smuzhiyun * freed; otherwise, @new_res is added atomically.
707*4882a593Smuzhiyun *
708*4882a593Smuzhiyun * @return ointer to found or added devres.
709*4882a593Smuzhiyun */
710*4882a593Smuzhiyun void *devres_get(struct udevice *dev, void *new_res,
711*4882a593Smuzhiyun dr_match_t match, void *match_data);
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun /**
714*4882a593Smuzhiyun * devres_remove() - Find a device resource and remove it
715*4882a593Smuzhiyun * @dev: Device to find resource from
716*4882a593Smuzhiyun * @release: Look for resources associated with this release function
717*4882a593Smuzhiyun * @match: Match function (optional)
718*4882a593Smuzhiyun * @match_data: Data for the match function
719*4882a593Smuzhiyun *
720*4882a593Smuzhiyun * Find the latest devres of @dev associated with @release and for
721*4882a593Smuzhiyun * which @match returns 1. If @match is NULL, it's considered to
722*4882a593Smuzhiyun * match all. If found, the resource is removed atomically and
723*4882a593Smuzhiyun * returned.
724*4882a593Smuzhiyun *
725*4882a593Smuzhiyun * @return ointer to removed devres on success, NULL if not found.
726*4882a593Smuzhiyun */
727*4882a593Smuzhiyun void *devres_remove(struct udevice *dev, dr_release_t release,
728*4882a593Smuzhiyun dr_match_t match, void *match_data);
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun /**
731*4882a593Smuzhiyun * devres_destroy() - Find a device resource and destroy it
732*4882a593Smuzhiyun * @dev: Device to find resource from
733*4882a593Smuzhiyun * @release: Look for resources associated with this release function
734*4882a593Smuzhiyun * @match: Match function (optional)
735*4882a593Smuzhiyun * @match_data: Data for the match function
736*4882a593Smuzhiyun *
737*4882a593Smuzhiyun * Find the latest devres of @dev associated with @release and for
738*4882a593Smuzhiyun * which @match returns 1. If @match is NULL, it's considered to
739*4882a593Smuzhiyun * match all. If found, the resource is removed atomically and freed.
740*4882a593Smuzhiyun *
741*4882a593Smuzhiyun * Note that the release function for the resource will not be called,
742*4882a593Smuzhiyun * only the devres-allocated data will be freed. The caller becomes
743*4882a593Smuzhiyun * responsible for freeing any other data.
744*4882a593Smuzhiyun *
745*4882a593Smuzhiyun * @return 0 if devres is found and freed, -ENOENT if not found.
746*4882a593Smuzhiyun */
747*4882a593Smuzhiyun int devres_destroy(struct udevice *dev, dr_release_t release,
748*4882a593Smuzhiyun dr_match_t match, void *match_data);
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun /**
751*4882a593Smuzhiyun * devres_release() - Find a device resource and destroy it, calling release
752*4882a593Smuzhiyun * @dev: Device to find resource from
753*4882a593Smuzhiyun * @release: Look for resources associated with this release function
754*4882a593Smuzhiyun * @match: Match function (optional)
755*4882a593Smuzhiyun * @match_data: Data for the match function
756*4882a593Smuzhiyun *
757*4882a593Smuzhiyun * Find the latest devres of @dev associated with @release and for
758*4882a593Smuzhiyun * which @match returns 1. If @match is NULL, it's considered to
759*4882a593Smuzhiyun * match all. If found, the resource is removed atomically, the
760*4882a593Smuzhiyun * release function called and the resource freed.
761*4882a593Smuzhiyun *
762*4882a593Smuzhiyun * @return 0 if devres is found and freed, -ENOENT if not found.
763*4882a593Smuzhiyun */
764*4882a593Smuzhiyun int devres_release(struct udevice *dev, dr_release_t release,
765*4882a593Smuzhiyun dr_match_t match, void *match_data);
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun /* managed devm_k.alloc/kfree for device drivers */
768*4882a593Smuzhiyun /**
769*4882a593Smuzhiyun * devm_kmalloc() - Resource-managed kmalloc
770*4882a593Smuzhiyun * @dev: Device to allocate memory for
771*4882a593Smuzhiyun * @size: Allocation size
772*4882a593Smuzhiyun * @gfp: Allocation gfp flags
773*4882a593Smuzhiyun *
774*4882a593Smuzhiyun * Managed kmalloc. Memory allocated with this function is
775*4882a593Smuzhiyun * automatically freed on driver detach. Like all other devres
776*4882a593Smuzhiyun * resources, guaranteed alignment is unsigned long long.
777*4882a593Smuzhiyun *
778*4882a593Smuzhiyun * @return pointer to allocated memory on success, NULL on failure.
779*4882a593Smuzhiyun */
780*4882a593Smuzhiyun void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
devm_kzalloc(struct udevice * dev,size_t size,gfp_t gfp)781*4882a593Smuzhiyun static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
782*4882a593Smuzhiyun {
783*4882a593Smuzhiyun return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
784*4882a593Smuzhiyun }
devm_kmalloc_array(struct udevice * dev,size_t n,size_t size,gfp_t flags)785*4882a593Smuzhiyun static inline void *devm_kmalloc_array(struct udevice *dev,
786*4882a593Smuzhiyun size_t n, size_t size, gfp_t flags)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun if (size != 0 && n > SIZE_MAX / size)
789*4882a593Smuzhiyun return NULL;
790*4882a593Smuzhiyun return devm_kmalloc(dev, n * size, flags);
791*4882a593Smuzhiyun }
devm_kcalloc(struct udevice * dev,size_t n,size_t size,gfp_t flags)792*4882a593Smuzhiyun static inline void *devm_kcalloc(struct udevice *dev,
793*4882a593Smuzhiyun size_t n, size_t size, gfp_t flags)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun /**
799*4882a593Smuzhiyun * devm_kfree() - Resource-managed kfree
800*4882a593Smuzhiyun * @dev: Device this memory belongs to
801*4882a593Smuzhiyun * @ptr: Memory to free
802*4882a593Smuzhiyun *
803*4882a593Smuzhiyun * Free memory allocated with devm_kmalloc().
804*4882a593Smuzhiyun */
805*4882a593Smuzhiyun void devm_kfree(struct udevice *dev, void *ptr);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun #else /* ! CONFIG_DEVRES */
808*4882a593Smuzhiyun
devres_alloc(dr_release_t release,size_t size,gfp_t gfp)809*4882a593Smuzhiyun static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun return kzalloc(size, gfp);
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
devres_free(void * res)814*4882a593Smuzhiyun static inline void devres_free(void *res)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun kfree(res);
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun
devres_add(struct udevice * dev,void * res)819*4882a593Smuzhiyun static inline void devres_add(struct udevice *dev, void *res)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun
devres_find(struct udevice * dev,dr_release_t release,dr_match_t match,void * match_data)823*4882a593Smuzhiyun static inline void *devres_find(struct udevice *dev, dr_release_t release,
824*4882a593Smuzhiyun dr_match_t match, void *match_data)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun return NULL;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun
devres_get(struct udevice * dev,void * new_res,dr_match_t match,void * match_data)829*4882a593Smuzhiyun static inline void *devres_get(struct udevice *dev, void *new_res,
830*4882a593Smuzhiyun dr_match_t match, void *match_data)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun return NULL;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun
devres_remove(struct udevice * dev,dr_release_t release,dr_match_t match,void * match_data)835*4882a593Smuzhiyun static inline void *devres_remove(struct udevice *dev, dr_release_t release,
836*4882a593Smuzhiyun dr_match_t match, void *match_data)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun return NULL;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun
devres_destroy(struct udevice * dev,dr_release_t release,dr_match_t match,void * match_data)841*4882a593Smuzhiyun static inline int devres_destroy(struct udevice *dev, dr_release_t release,
842*4882a593Smuzhiyun dr_match_t match, void *match_data)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun return 0;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
devres_release(struct udevice * dev,dr_release_t release,dr_match_t match,void * match_data)847*4882a593Smuzhiyun static inline int devres_release(struct udevice *dev, dr_release_t release,
848*4882a593Smuzhiyun dr_match_t match, void *match_data)
849*4882a593Smuzhiyun {
850*4882a593Smuzhiyun return 0;
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun
devm_kmalloc(struct udevice * dev,size_t size,gfp_t gfp)853*4882a593Smuzhiyun static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun return kmalloc(size, gfp);
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun
devm_kzalloc(struct udevice * dev,size_t size,gfp_t gfp)858*4882a593Smuzhiyun static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
859*4882a593Smuzhiyun {
860*4882a593Smuzhiyun return kzalloc(size, gfp);
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun
devm_kmaloc_array(struct udevice * dev,size_t n,size_t size,gfp_t flags)863*4882a593Smuzhiyun static inline void *devm_kmaloc_array(struct udevice *dev,
864*4882a593Smuzhiyun size_t n, size_t size, gfp_t flags)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun /* TODO: add kmalloc_array() to linux/compat.h */
867*4882a593Smuzhiyun if (size != 0 && n > SIZE_MAX / size)
868*4882a593Smuzhiyun return NULL;
869*4882a593Smuzhiyun return kmalloc(n * size, flags);
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun
devm_kcalloc(struct udevice * dev,size_t n,size_t size,gfp_t flags)872*4882a593Smuzhiyun static inline void *devm_kcalloc(struct udevice *dev,
873*4882a593Smuzhiyun size_t n, size_t size, gfp_t flags)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun /* TODO: add kcalloc() to linux/compat.h */
876*4882a593Smuzhiyun return kmalloc(n * size, flags | __GFP_ZERO);
877*4882a593Smuzhiyun }
878*4882a593Smuzhiyun
devm_kfree(struct udevice * dev,void * ptr)879*4882a593Smuzhiyun static inline void devm_kfree(struct udevice *dev, void *ptr)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun kfree(ptr);
882*4882a593Smuzhiyun }
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun #endif /* ! CONFIG_DEVRES */
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun /*
887*4882a593Smuzhiyun * REVISIT:
888*4882a593Smuzhiyun * remove the following after resolving conflicts with <linux/compat.h>
889*4882a593Smuzhiyun */
890*4882a593Smuzhiyun #ifdef dev_dbg
891*4882a593Smuzhiyun #undef dev_dbg
892*4882a593Smuzhiyun #endif
893*4882a593Smuzhiyun #ifdef dev_vdbg
894*4882a593Smuzhiyun #undef dev_vdbg
895*4882a593Smuzhiyun #endif
896*4882a593Smuzhiyun #ifdef dev_info
897*4882a593Smuzhiyun #undef dev_info
898*4882a593Smuzhiyun #endif
899*4882a593Smuzhiyun #ifdef dev_err
900*4882a593Smuzhiyun #undef dev_err
901*4882a593Smuzhiyun #endif
902*4882a593Smuzhiyun #ifdef dev_warn
903*4882a593Smuzhiyun #undef dev_warn
904*4882a593Smuzhiyun #endif
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun /*
907*4882a593Smuzhiyun * REVISIT:
908*4882a593Smuzhiyun * print device name like Linux
909*4882a593Smuzhiyun */
910*4882a593Smuzhiyun #define dev_printk(dev, fmt, ...) \
911*4882a593Smuzhiyun ({ \
912*4882a593Smuzhiyun printk(fmt, ##__VA_ARGS__); \
913*4882a593Smuzhiyun })
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun #define __dev_printk(level, dev, fmt, ...) \
916*4882a593Smuzhiyun ({ \
917*4882a593Smuzhiyun if (level < CONFIG_VAL(LOGLEVEL)) \
918*4882a593Smuzhiyun dev_printk(dev, fmt, ##__VA_ARGS__); \
919*4882a593Smuzhiyun })
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun #define dev_emerg(dev, fmt, ...) \
922*4882a593Smuzhiyun __dev_printk(0, dev, fmt, ##__VA_ARGS__)
923*4882a593Smuzhiyun #define dev_alert(dev, fmt, ...) \
924*4882a593Smuzhiyun __dev_printk(1, dev, fmt, ##__VA_ARGS__)
925*4882a593Smuzhiyun #define dev_crit(dev, fmt, ...) \
926*4882a593Smuzhiyun __dev_printk(2, dev, fmt, ##__VA_ARGS__)
927*4882a593Smuzhiyun #define dev_err(dev, fmt, ...) \
928*4882a593Smuzhiyun __dev_printk(3, dev, fmt, ##__VA_ARGS__)
929*4882a593Smuzhiyun #define dev_warn(dev, fmt, ...) \
930*4882a593Smuzhiyun __dev_printk(4, dev, fmt, ##__VA_ARGS__)
931*4882a593Smuzhiyun #define dev_notice(dev, fmt, ...) \
932*4882a593Smuzhiyun __dev_printk(5, dev, fmt, ##__VA_ARGS__)
933*4882a593Smuzhiyun #define dev_info(dev, fmt, ...) \
934*4882a593Smuzhiyun __dev_printk(6, dev, fmt, ##__VA_ARGS__)
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun #ifdef DEBUG
937*4882a593Smuzhiyun #define dev_dbg(dev, fmt, ...) \
938*4882a593Smuzhiyun __dev_printk(7, dev, fmt, ##__VA_ARGS__)
939*4882a593Smuzhiyun #else
940*4882a593Smuzhiyun #define dev_dbg(dev, fmt, ...) \
941*4882a593Smuzhiyun ({ \
942*4882a593Smuzhiyun if (0) \
943*4882a593Smuzhiyun __dev_printk(7, dev, fmt, ##__VA_ARGS__); \
944*4882a593Smuzhiyun })
945*4882a593Smuzhiyun #endif
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun #ifdef VERBOSE_DEBUG
948*4882a593Smuzhiyun #define dev_vdbg dev_dbg
949*4882a593Smuzhiyun #else
950*4882a593Smuzhiyun #define dev_vdbg(dev, fmt, ...) \
951*4882a593Smuzhiyun ({ \
952*4882a593Smuzhiyun if (0) \
953*4882a593Smuzhiyun __dev_printk(7, dev, fmt, ##__VA_ARGS__); \
954*4882a593Smuzhiyun })
955*4882a593Smuzhiyun #endif
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun #endif
958