blob: 28a4e02ef242d46c63375ac6afece92865d5abdc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass6494d702014-02-26 15:59:18 -07002/*
3 * Copyright (c) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
7 * Marek Vasut <marex@denx.de>
Simon Glass6494d702014-02-26 15:59:18 -07008 */
9
10#ifndef _DM_DEVICE_H
11#define _DM_DEVICE_H
12
Simon Glass4984de22017-05-17 17:18:10 -060013#include <dm/ofnode.h>
Simon Glass930a3dd2022-05-08 04:39:24 -060014#include <dm/tag.h>
Simon Glass6494d702014-02-26 15:59:18 -070015#include <dm/uclass-id.h>
Peng Fanc9cac3f2015-02-10 14:46:32 +080016#include <fdtdec.h>
Simon Glass6494d702014-02-26 15:59:18 -070017#include <linker_lists.h>
Masahiro Yamada2b07f682015-07-25 21:52:36 +090018#include <linux/kernel.h>
Simon Glass6494d702014-02-26 15:59:18 -070019#include <linux/list.h>
Masahiro Yamadac898cba2017-09-26 11:58:29 +090020#include <linux/printk.h>
xia.jinf5b1b742024-06-11 08:06:58 +000021#ifdef CONFIG_ARMV8_MULTIENTRY
22#undef spin_lock_init
23#undef spin_lock
24#undef spin_unlock
25#include <spinlock.h>
26#endif
Simon Glass6494d702014-02-26 15:59:18 -070027
28struct driver_info;
29
30/* Driver is active (probed). Cleared when it is removed */
Simon Glassfb04c9d2015-09-28 23:32:03 -060031#define DM_FLAG_ACTIVATED (1 << 0)
Simon Glass6494d702014-02-26 15:59:18 -070032
Simon Glasscaa4daa2020-12-03 16:55:18 -070033/* DM is responsible for allocating and freeing plat */
Simon Glassfb04c9d2015-09-28 23:32:03 -060034#define DM_FLAG_ALLOC_PDATA (1 << 1)
Simon Glass6494d702014-02-26 15:59:18 -070035
Simon Glass00606d72014-07-23 06:55:03 -060036/* DM should init this device prior to relocation */
Simon Glassfb04c9d2015-09-28 23:32:03 -060037#define DM_FLAG_PRE_RELOC (1 << 2)
Simon Glass00606d72014-07-23 06:55:03 -060038
Simon Glasscaa4daa2020-12-03 16:55:18 -070039/* DM is responsible for allocating and freeing parent_plat */
Simon Glasscdc133b2015-01-25 08:27:01 -070040#define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
41
Simon Glasscaa4daa2020-12-03 16:55:18 -070042/* DM is responsible for allocating and freeing uclass_plat */
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +020043#define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
44
Simon Glass2c03c462015-03-25 12:21:53 -060045/* Allocate driver private data on a DMA boundary */
Simon Glassfb04c9d2015-09-28 23:32:03 -060046#define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
Simon Glass2c03c462015-03-25 12:21:53 -060047
Masahiro Yamadaaed1a4d2015-07-25 21:52:34 +090048/* Device is bound */
Simon Glassfb04c9d2015-09-28 23:32:03 -060049#define DM_FLAG_BOUND (1 << 6)
Masahiro Yamadaaed1a4d2015-07-25 21:52:34 +090050
Simon Glassa2040fa2016-05-01 13:52:23 -060051/* Device name is allocated and should be freed on unbind() */
Simon Glassfd1c2d92016-07-04 11:58:15 -060052#define DM_FLAG_NAME_ALLOCED (1 << 7)
Simon Glassa2040fa2016-05-01 13:52:23 -060053
Simon Glass04e19ff2019-12-29 21:19:19 -070054/* Device has platform data provided by of-platdata */
Simon Glass9fa28192016-07-04 11:58:18 -060055#define DM_FLAG_OF_PLATDATA (1 << 8)
56
Stefan Roese706865a2017-03-20 12:51:48 +010057/*
58 * Call driver remove function to stop currently active DMA transfers or
59 * give DMA buffers back to the HW / controller. This may be needed for
60 * some drivers to do some final stage cleanup before the OS is called
61 * (U-Boot exit)
62 */
63#define DM_FLAG_ACTIVE_DMA (1 << 9)
64
65/*
Stefan Roese426f99f2017-04-24 09:48:02 +020066 * Call driver remove function to do some final configuration, before
67 * U-Boot exits and the OS is started
68 */
69#define DM_FLAG_OS_PREPARE (1 << 10)
70
Lokesh Vutlaaf94ad42019-09-27 13:48:12 +053071/* DM does not enable/disable the power domains corresponding to this device */
72#define DM_FLAG_DEFAULT_PD_CTRL_OFF (1 << 11)
73
Simon Glasscaa4daa2020-12-03 16:55:18 -070074/* Driver plat has been read. Cleared when the device is removed */
Simon Glass153851d2019-12-29 21:19:21 -070075#define DM_FLAG_PLATDATA_VALID (1 << 12)
76
Stefan Roese426f99f2017-04-24 09:48:02 +020077/*
Anatolij Gustschin5349e252020-02-17 12:36:43 +010078 * Device is removed without switching off its power domain. This might
79 * be required, i. e. for serial console (debug) output when booting OS.
80 */
Simon Glassa547fcb2021-01-24 14:32:42 -070081#define DM_FLAG_LEAVE_PD_ON (1 << 13)
Anatolij Gustschin5349e252020-02-17 12:36:43 +010082
83/*
Marek Vasutcc6f4c82021-01-24 14:32:46 -070084 * Device is vital to the operation of other devices. It is possible to remove
85 * removed this device after all regular devices are removed. This is useful
86 * e.g. for clock, which need to be active during the device-removal phase.
87 */
88#define DM_FLAG_VITAL (1 << 14)
89
Marek Vasut9cc32bf2022-04-22 15:15:53 +020090/* Device must be probed after it was bound */
91#define DM_FLAG_PROBE_AFTER_BIND (1 << 15)
92
Marek Vasutcc6f4c82021-01-24 14:32:46 -070093/*
Stefan Roese706865a2017-03-20 12:51:48 +010094 * One or multiple of these flags are passed to device_remove() so that
95 * a selective device removal as specified by the remove-stage and the
96 * driver flags can be done.
Simon Glass4d7bab12021-01-24 14:32:43 -070097 *
98 * DO NOT use these flags in your driver's @flags value...
99 * use the above DM_FLAG_... values instead
Stefan Roese706865a2017-03-20 12:51:48 +0100100 */
101enum {
102 /* Normal remove, remove all devices */
Simon Glassced10802020-03-28 14:03:48 -0600103 DM_REMOVE_NORMAL = 1 << 0,
Stefan Roese706865a2017-03-20 12:51:48 +0100104
105 /* Remove devices with active DMA */
Simon Glassced10802020-03-28 14:03:48 -0600106 DM_REMOVE_ACTIVE_DMA = DM_FLAG_ACTIVE_DMA,
Stefan Roese706865a2017-03-20 12:51:48 +0100107
Stefan Roese426f99f2017-04-24 09:48:02 +0200108 /* Remove devices which need some final OS preparation steps */
Simon Glassced10802020-03-28 14:03:48 -0600109 DM_REMOVE_OS_PREPARE = DM_FLAG_OS_PREPARE,
Stefan Roese426f99f2017-04-24 09:48:02 +0200110
Marek Vasutcc6f4c82021-01-24 14:32:46 -0700111 /* Remove only devices that are not marked vital */
112 DM_REMOVE_NON_VITAL = DM_FLAG_VITAL,
Stefan Roese706865a2017-03-20 12:51:48 +0100113
114 /* Remove devices with any active flag */
Simon Glassced10802020-03-28 14:03:48 -0600115 DM_REMOVE_ACTIVE_ALL = DM_REMOVE_ACTIVE_DMA | DM_REMOVE_OS_PREPARE,
116
117 /* Don't power down any attached power domains */
118 DM_REMOVE_NO_PD = 1 << 1,
Stefan Roese706865a2017-03-20 12:51:48 +0100119};
120
Simon Glass6494d702014-02-26 15:59:18 -0700121/**
Heiko Schocher54c5d082014-05-22 12:43:05 +0200122 * struct udevice - An instance of a driver
Simon Glass6494d702014-02-26 15:59:18 -0700123 *
124 * This holds information about a device, which is a driver bound to a
125 * particular port or peripheral (essentially a driver instance).
126 *
127 * A device will come into existence through a 'bind' call, either due to
Simon Glass20e442a2020-12-28 20:34:54 -0700128 * a U_BOOT_DRVINFO() macro (in which case plat is non-NULL) or a node
Simon Glass6494d702014-02-26 15:59:18 -0700129 * in the device tree (in which case of_offset is >= 0). In the latter case
Simon Glasscaa4daa2020-12-03 16:55:18 -0700130 * we translate the device tree information into plat in a function
Simon Glassd1998a92020-12-03 16:55:21 -0700131 * implemented by the driver of_to_plat method (called just before the
Simon Glass6494d702014-02-26 15:59:18 -0700132 * probe method if the device has a device tree node.
133 *
Simon Glasscaa4daa2020-12-03 16:55:18 -0700134 * All three of plat, priv and uclass_priv can be allocated by the
Dario Binacchifabae872021-01-23 19:48:57 +0100135 * driver, or you can use the auto members of struct driver and
Simon Glass6494d702014-02-26 15:59:18 -0700136 * struct uclass_driver to have driver model do this automatically.
137 *
138 * @driver: The driver used by this device
139 * @name: Name of device, typically the FDT node name
Simon Glassfb8c9fb2020-12-22 19:30:30 -0700140 * @plat_: Configuration data for this device (do not access outside driver
141 * model)
142 * @parent_plat_: The parent bus's configuration data for this device (do not
143 * access outside driver model)
144 * @uclass_plat_: The uclass's configuration data for this device (do not access
145 * outside driver model)
Simon Glass39de8432015-03-25 12:21:55 -0600146 * @driver_data: Driver data word for the entry that matched this device with
147 * its driver
Simon Glass6494d702014-02-26 15:59:18 -0700148 * @parent: Parent of this device, or NULL for the top level device
Simon Glassfb8c9fb2020-12-22 19:30:30 -0700149 * @priv_: Private data for this device (do not access outside driver model)
Simon Glass6494d702014-02-26 15:59:18 -0700150 * @uclass: Pointer to uclass for this device
Simon Glassfb8c9fb2020-12-22 19:30:30 -0700151 * @uclass_priv_: The uclass's private data for this device (do not access
152 * outside driver model)
153 * @parent_priv_: The parent's private data for this device (do not access
154 * outside driver model)
Simon Glass6494d702014-02-26 15:59:18 -0700155 * @uclass_node: Used by uclass to link its devices
156 * @child_head: List of children of this device
157 * @sibling_node: Next device in list of all devices
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100158 * @flags_: Flags for this device `DM_FLAG_...` (do not access outside driver
Simon Glass156004f2020-12-19 10:40:11 -0700159 * model)
Simon Glass24621392020-12-19 10:40:09 -0700160 * @seq_: Allocated sequence number for this device (-1 = none). This is set up
Simon Glasscd53e5b2020-12-16 21:20:09 -0700161 * when the device is bound and is unique within the device's uclass. If the
162 * device has an alias in the devicetree then that is used to set the sequence
163 * number. Otherwise, the next available number is used. Sequence numbers are
Simon Glass24621392020-12-19 10:40:09 -0700164 * used by certain commands that need device to be numbered (e.g. 'mmc dev').
165 * (do not access outside driver model)
Simon Glass84a42ae2020-12-19 10:40:15 -0700166 * @node_: Reference to device tree node for this device (do not access outside
167 * driver model)
Simon Glass93c7fe42015-09-28 23:32:04 -0600168 * @devres_head: List of memory allocations associated with this device.
169 * When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
170 * add to this list. Memory so-allocated will be freed
171 * automatically when the device is removed / unbound
Nicolas Saenz Julienne4abf68d2021-01-12 13:55:24 +0100172 * @dma_offset: Offset between the physical address space (CPU's) and the
173 * device's bus address space
Simon Glass6494d702014-02-26 15:59:18 -0700174 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200175struct udevice {
Simon Glass34792532015-03-25 12:21:54 -0600176 const struct driver *driver;
Simon Glass6494d702014-02-26 15:59:18 -0700177 const char *name;
Simon Glassfb8c9fb2020-12-22 19:30:30 -0700178 void *plat_;
179 void *parent_plat_;
180 void *uclass_plat_;
Simon Glass39de8432015-03-25 12:21:55 -0600181 ulong driver_data;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200182 struct udevice *parent;
Simon Glassfb8c9fb2020-12-22 19:30:30 -0700183 void *priv_;
Simon Glass6494d702014-02-26 15:59:18 -0700184 struct uclass *uclass;
Simon Glassfb8c9fb2020-12-22 19:30:30 -0700185 void *uclass_priv_;
186 void *parent_priv_;
Simon Glass6494d702014-02-26 15:59:18 -0700187 struct list_head uclass_node;
188 struct list_head child_head;
189 struct list_head sibling_node;
Simon Glass6f644ef2021-03-15 17:25:37 +1300190#if !CONFIG_IS_ENABLED(OF_PLATDATA_RT)
Simon Glass156004f2020-12-19 10:40:11 -0700191 u32 flags_;
Simon Glass6f644ef2021-03-15 17:25:37 +1300192#endif
Simon Glass24621392020-12-19 10:40:09 -0700193 int seq_;
Simon Glass95397382021-08-07 07:24:04 -0600194#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass84a42ae2020-12-19 10:40:15 -0700195 ofnode node_;
196#endif
Simon Glass092d5c22022-03-27 14:26:19 -0600197#if CONFIG_IS_ENABLED(DEVRES)
Masahiro Yamada608f26c2015-07-25 21:52:35 +0900198 struct list_head devres_head;
Masahiro Yamadae2282d72015-07-25 21:52:37 +0900199#endif
Nicolas Saenz Julienne4abf68d2021-01-12 13:55:24 +0100200#if CONFIG_IS_ENABLED(DM_DMA)
201 ulong dma_offset;
202#endif
xia.jinf5b1b742024-06-11 08:06:58 +0000203#ifdef CONFIG_ARMV8_MULTIENTRY
204 spin_lock_t lock;
205#endif
Simon Glass6494d702014-02-26 15:59:18 -0700206};
207
Simon Glasscdd73e72022-03-27 14:26:20 -0600208static inline int dm_udevice_size(void)
209{
210 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT))
211 return ALIGN(sizeof(struct udevice), CONFIG_LINKER_LIST_ALIGN);
212
213 return sizeof(struct udevice);
214}
215
Simon Glassab933d82021-03-15 17:25:36 +1300216/**
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100217 * struct udevice_rt - runtime information set up by U-Boot
Simon Glassab933d82021-03-15 17:25:36 +1300218 *
219 * This is only used with OF_PLATDATA_RT
220 *
221 * There is one of these for every udevice in the linker list, indexed by
222 * the udevice_info idx value.
223 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100224 * @flags_: Flags for this device `DM_FLAG_...` (do not access outside driver
Simon Glassab933d82021-03-15 17:25:36 +1300225 * model)
226 */
227struct udevice_rt {
228 u32 flags_;
229};
230
Patrick Delaunaya4f2d832021-09-22 18:29:08 +0200231/* Maximum sequence number supported and associated string length */
Simon Glass5a66a8f2014-07-23 06:55:12 -0600232#define DM_MAX_SEQ 999
Patrick Delaunaya4f2d832021-09-22 18:29:08 +0200233#define DM_MAX_SEQ_STR 3
Simon Glass5a66a8f2014-07-23 06:55:12 -0600234
Simon Glass6494d702014-02-26 15:59:18 -0700235/* Returns the operations for a device */
Sughosh Ganu82ee8bf2022-01-30 00:51:08 +0530236#define device_get_ops(dev) ((dev)->driver->ops)
Simon Glass6494d702014-02-26 15:59:18 -0700237
Simon Glass6f644ef2021-03-15 17:25:37 +1300238#if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
239u32 dev_get_flags(const struct udevice *dev);
240void dev_or_flags(const struct udevice *dev, u32 or);
241void dev_bic_flags(const struct udevice *dev, u32 bic);
242#else
Simon Glass73466df2020-12-19 10:40:10 -0700243static inline u32 dev_get_flags(const struct udevice *dev)
244{
Simon Glass156004f2020-12-19 10:40:11 -0700245 return dev->flags_;
Simon Glass73466df2020-12-19 10:40:10 -0700246}
247
248static inline void dev_or_flags(struct udevice *dev, u32 or)
249{
Simon Glass156004f2020-12-19 10:40:11 -0700250 dev->flags_ |= or;
Simon Glass73466df2020-12-19 10:40:10 -0700251}
252
253static inline void dev_bic_flags(struct udevice *dev, u32 bic)
254{
Simon Glass156004f2020-12-19 10:40:11 -0700255 dev->flags_ &= ~bic;
Simon Glass73466df2020-12-19 10:40:10 -0700256}
Simon Glass6f644ef2021-03-15 17:25:37 +1300257#endif /* OF_PLATDATA_RT */
Simon Glass73466df2020-12-19 10:40:10 -0700258
Simon Glass7d14ee42020-12-19 10:40:13 -0700259/**
260 * dev_ofnode() - get the DT node reference associated with a udevice
261 *
262 * @dev: device to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100263 * Return: reference of the device's DT node
Simon Glass7d14ee42020-12-19 10:40:13 -0700264 */
Simon Glass17013592022-09-06 20:27:12 -0600265static inline __attribute_const__ ofnode dev_ofnode(const struct udevice *dev)
Simon Glass7d14ee42020-12-19 10:40:13 -0700266{
Simon Glass95397382021-08-07 07:24:04 -0600267#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass84a42ae2020-12-19 10:40:15 -0700268 return dev->node_;
Simon Glassf10643c2020-12-19 10:40:14 -0700269#else
270 return ofnode_null();
271#endif
Simon Glass7d14ee42020-12-19 10:40:13 -0700272}
273
Simon Glass156004f2020-12-19 10:40:11 -0700274/* Returns non-zero if the device is active (probed and not removed) */
275#define device_active(dev) (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
276
Nicolas Saenz Julienne4abf68d2021-01-12 13:55:24 +0100277#if CONFIG_IS_ENABLED(DM_DMA)
278#define dev_set_dma_offset(_dev, _offset) _dev->dma_offset = _offset
279#define dev_get_dma_offset(_dev) _dev->dma_offset
280#else
281#define dev_set_dma_offset(_dev, _offset)
282#define dev_get_dma_offset(_dev) 0
283#endif
284
Bo Lv72d0e902023-01-02 14:27:34 +0000285#ifdef CONFIG_AMLOGIC_MODIFY
286static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
287{
288 dev->node_ = offset_to_ofnode(of_offset);
289}
290#endif
291
Simon Glass17013592022-09-06 20:27:12 -0600292static inline __attribute_const__ int dev_of_offset(const struct udevice *dev)
Simon Glasse160f7d2017-01-17 16:52:55 -0700293{
Simon Glass95397382021-08-07 07:24:04 -0600294#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassf10643c2020-12-19 10:40:14 -0700295 return ofnode_to_offset(dev_ofnode(dev));
296#else
297 return -1;
298#endif
Simon Glasse160f7d2017-01-17 16:52:55 -0700299}
300
Simon Glass17013592022-09-06 20:27:12 -0600301static inline __attribute_const__ bool dev_has_ofnode(const struct udevice *dev)
Simon Glass4984de22017-05-17 17:18:10 -0600302{
Simon Glass95397382021-08-07 07:24:04 -0600303#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassf10643c2020-12-19 10:40:14 -0700304 return ofnode_valid(dev_ofnode(dev));
305#else
306 return false;
307#endif
308}
309
310static inline void dev_set_ofnode(struct udevice *dev, ofnode node)
311{
Simon Glass95397382021-08-07 07:24:04 -0600312#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass84a42ae2020-12-19 10:40:15 -0700313 dev->node_ = node;
Simon Glassf10643c2020-12-19 10:40:14 -0700314#endif
Simon Glasse160f7d2017-01-17 16:52:55 -0700315}
316
Simon Glass8b85dfc2020-12-16 21:20:07 -0700317static inline int dev_seq(const struct udevice *dev)
318{
Simon Glass24621392020-12-19 10:40:09 -0700319 return dev->seq_;
Simon Glass8b85dfc2020-12-16 21:20:07 -0700320}
321
Simon Glass6494d702014-02-26 15:59:18 -0700322/**
Simon Glassae7f4512014-06-11 23:29:45 -0600323 * struct udevice_id - Lists the compatible strings supported by a driver
Simon Glass6494d702014-02-26 15:59:18 -0700324 * @compatible: Compatible string
325 * @data: Data for this compatible string
326 */
Simon Glassae7f4512014-06-11 23:29:45 -0600327struct udevice_id {
Simon Glass6494d702014-02-26 15:59:18 -0700328 const char *compatible;
329 ulong data;
330};
331
Simon Glass414cc152021-08-07 07:24:03 -0600332#if CONFIG_IS_ENABLED(OF_REAL)
Masahiro Yamadaf887cb62014-10-07 14:51:31 +0900333#define of_match_ptr(_ptr) (_ptr)
334#else
335#define of_match_ptr(_ptr) NULL
Masahiro Yamada0f925822015-08-12 07:31:55 +0900336#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
Masahiro Yamadaf887cb62014-10-07 14:51:31 +0900337
Simon Glass6494d702014-02-26 15:59:18 -0700338/**
339 * struct driver - A driver for a feature or peripheral
340 *
341 * This holds methods for setting up a new device, and also removing it.
342 * The device needs information to set itself up - this is provided either
Simon Glasscaa4daa2020-12-03 16:55:18 -0700343 * by plat or a device tree node (which we find by looking up
Simon Glass6494d702014-02-26 15:59:18 -0700344 * matching compatible strings with of_match).
345 *
346 * Drivers all belong to a uclass, representing a class of devices of the
347 * same type. Common elements of the drivers can be implemented in the uclass,
348 * or the uclass can provide a consistent interface to the drivers within
349 * it.
350 *
351 * @name: Device name
Heinrich Schuchardt80ee4fc2018-02-12 12:38:36 +0100352 * @id: Identifies the uclass we belong to
Simon Glass6494d702014-02-26 15:59:18 -0700353 * @of_match: List of compatible strings to match, and any identifying data
354 * for each.
355 * @bind: Called to bind a device to its driver
356 * @probe: Called to probe a device, i.e. activate it
357 * @remove: Called to remove a device, i.e. de-activate it
358 * @unbind: Called to unbind a device from its driver
Simon Glassd1998a92020-12-03 16:55:21 -0700359 * @of_to_plat: Called before probe to decode device tree data
Simon Glass0118ce72015-01-25 08:27:03 -0700360 * @child_post_bind: Called after a new child has been bound
Simon Glassa327dee2014-07-23 06:55:21 -0600361 * @child_pre_probe: Called before a child device is probed. The device has
362 * memory allocated but it has not yet been probed.
363 * @child_post_remove: Called after a child device is removed. The device
364 * has memory allocated but its device_remove() method has been called.
Simon Glass41575d82020-12-03 16:55:17 -0700365 * @priv_auto: If non-zero this is the size of the private data
Simon Glass6494d702014-02-26 15:59:18 -0700366 * to be allocated in the device's ->priv pointer. If zero, then the driver
367 * is responsible for allocating any data required.
Simon Glasscaa4daa2020-12-03 16:55:18 -0700368 * @plat_auto: If non-zero this is the size of the
369 * platform data to be allocated in the device's ->plat pointer.
Simon Glass6494d702014-02-26 15:59:18 -0700370 * This is typically only useful for device-tree-aware drivers (those with
Simon Glasscaa4daa2020-12-03 16:55:18 -0700371 * an of_match), since drivers which use plat will have the data
Simon Glass20e442a2020-12-28 20:34:54 -0700372 * provided in the U_BOOT_DRVINFO() instantiation.
Simon Glass41575d82020-12-03 16:55:17 -0700373 * @per_child_auto: Each device can hold private data owned by
Simon Glasse59f4582014-07-23 06:55:20 -0600374 * its parent. If required this will be automatically allocated if this
375 * value is non-zero.
Simon Glasscaa4daa2020-12-03 16:55:18 -0700376 * @per_child_plat_auto: A bus likes to store information about
Simon Glasscdc133b2015-01-25 08:27:01 -0700377 * its children. If non-zero this is the size of this data, to be allocated
Simon Glasscaa4daa2020-12-03 16:55:18 -0700378 * in the child's parent_plat pointer.
Simon Glass0040b942014-07-23 06:55:17 -0600379 * @ops: Driver-specific operations. This is typically a list of function
Simon Glass6494d702014-02-26 15:59:18 -0700380 * pointers defined by the driver, to implement driver functions required by
381 * the uclass.
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100382 * @flags: driver flags - see `DM_FLAGS_...`
Simon Glass7ca28502020-04-09 10:27:38 -0600383 * @acpi_ops: Advanced Configuration and Power Interface (ACPI) operations,
384 * allowing the device to add things to the ACPI tables passed to Linux
Simon Glass6494d702014-02-26 15:59:18 -0700385 */
386struct driver {
387 char *name;
388 enum uclass_id id;
Simon Glassae7f4512014-06-11 23:29:45 -0600389 const struct udevice_id *of_match;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200390 int (*bind)(struct udevice *dev);
391 int (*probe)(struct udevice *dev);
392 int (*remove)(struct udevice *dev);
393 int (*unbind)(struct udevice *dev);
Simon Glassd1998a92020-12-03 16:55:21 -0700394 int (*of_to_plat)(struct udevice *dev);
Simon Glass0118ce72015-01-25 08:27:03 -0700395 int (*child_post_bind)(struct udevice *dev);
Simon Glassa327dee2014-07-23 06:55:21 -0600396 int (*child_pre_probe)(struct udevice *dev);
397 int (*child_post_remove)(struct udevice *dev);
Simon Glass41575d82020-12-03 16:55:17 -0700398 int priv_auto;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700399 int plat_auto;
Simon Glass41575d82020-12-03 16:55:17 -0700400 int per_child_auto;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700401 int per_child_plat_auto;
Simon Glass6494d702014-02-26 15:59:18 -0700402 const void *ops; /* driver-specific operations */
Simon Glass00606d72014-07-23 06:55:03 -0600403 uint32_t flags;
Simon Glass7ca28502020-04-09 10:27:38 -0600404#if CONFIG_IS_ENABLED(ACPIGEN)
405 struct acpi_ops *acpi_ops;
406#endif
Simon Glass6494d702014-02-26 15:59:18 -0700407};
408
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100409/**
410 * U_BOOT_DRIVER() - Declare a new U-Boot driver
411 * @__name: name of the driver
412 */
Simon Glass6494d702014-02-26 15:59:18 -0700413#define U_BOOT_DRIVER(__name) \
414 ll_entry_declare(struct driver, __name, driver)
415
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100416/**
417 * DM_DRIVER_GET() - Get a pointer to a given driver
418 *
419 * This is useful in code for referencing a driver at build time.
420 * Before this is used, an extern U_BOOT_DRIVER() must have been
421 * declared.
422 *
423 * @__name: Name of the driver. This must be a valid C identifier,
424 * used by the linker_list
425 * Return: struct driver * for the driver
426 */
Simon Glass65e25be2020-12-28 20:34:56 -0700427#define DM_DRIVER_GET(__name) \
Simon Glassc57f8062016-07-17 15:23:15 -0600428 ll_entry_get(struct driver, __name, driver)
429
Simon Glass6494d702014-02-26 15:59:18 -0700430/**
Simon Glass607f9bc2021-03-15 17:25:14 +1300431 * DM_DRIVER_REF() - Get a reference to a driver
432 *
433 * This is useful in data structures and code for referencing a driver at
434 * build time. Before this is used, an extern U_BOOT_DRIVER() must have been
435 * declared.
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100436 * This is like DM_DRIVER_GET, but without the extra code, so it is suitable
437 * for putting into data structures.
Simon Glass607f9bc2021-03-15 17:25:14 +1300438 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100439 * For example::
Simon Glass607f9bc2021-03-15 17:25:14 +1300440 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100441 * extern U_BOOT_DRIVER(sandbox_fixed_clock);
442 * struct driver *drvs[] = {
443 * DM_DRIVER_REF(sandbox_fixed_clock),
444 * };
Simon Glass607f9bc2021-03-15 17:25:14 +1300445 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100446 * @_name: Name of the driver. This must be a valid C identifier,
447 * used by the linker_list
448 * Return: struct driver * for the driver
Simon Glass607f9bc2021-03-15 17:25:14 +1300449 */
450#define DM_DRIVER_REF(_name) \
451 ll_entry_ref(struct driver, _name, driver)
452
453/**
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100454 * DM_DRIVER_ALIAS() - Declare a macro to state an alias for a driver name
455 *
456 * This macro will produce no code but its information will be parsed by tools
457 * like dtoc
458 *
459 * @__name: name of driver
460 * @__alias: alias for the driver name
Walter Lozanoaddf3582020-06-25 01:10:06 -0300461 */
Simon Glassbdf8fd72020-12-28 20:34:57 -0700462#define DM_DRIVER_ALIAS(__name, __alias)
Walter Lozanoaddf3582020-06-25 01:10:06 -0300463
464/**
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100465 * DM_PHASE() - Declare a macro to indicate which phase of U-Boot this driver is for.
Simon Glassb00f0062021-02-03 06:01:02 -0700466 *
467 * This macro produces no code but its information will be parsed by dtoc. The
468 * macro can be only be used once in a driver. Put it within the U_BOOT_DRIVER()
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100469 * declaration, e.g.::
Simon Glassb00f0062021-02-03 06:01:02 -0700470 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100471 * U_BOOT_DRIVER(cpu) = {
472 * .name = ...
473 * ...
474 * DM_PHASE(tpl)
475 * };
476 *
477 * @_phase: Associated phase of U-Boot ("spl", "tpl")
Simon Glassb00f0062021-02-03 06:01:02 -0700478 */
479#define DM_PHASE(_phase)
480
481/**
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100482 * DM_HEADER() - Declare a macro to declare a header needed for a driver.
483 *
484 * Often the correct header can be found automatically, but only for struct
485 * declarations. For enums and #defines used in the driver declaration and
486 * declared in a different header from the structs, this macro must be used.
Simon Glass735ddfc2021-02-03 06:01:04 -0700487 *
488 * This macro produces no code but its information will be parsed by dtoc. The
489 * macro can be used multiple times with different headers, for the same driver.
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100490 * Put it within the U_BOOT_DRIVER() declaration, e.g.::
Simon Glass735ddfc2021-02-03 06:01:04 -0700491 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100492 * U_BOOT_DRIVER(cpu) = {
493 * .name = ...
494 * ...
495 * DM_HEADER(<asm/cpu.h>)
496 * };
497 *
498 * @_hdr: header needed for a driver
Simon Glass735ddfc2021-02-03 06:01:04 -0700499 */
500#define DM_HEADER(_hdr)
501
502/**
Simon Glassc69cda22020-12-03 16:55:20 -0700503 * dev_get_plat() - Get the platform data for a device
Simon Glass6494d702014-02-26 15:59:18 -0700504 *
505 * This checks that dev is not NULL, but no other checks for now
506 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100507 * @dev: Device to check
508 * Return: platform data, or NULL if none
Simon Glass6494d702014-02-26 15:59:18 -0700509 */
Simon Glassc69cda22020-12-03 16:55:20 -0700510void *dev_get_plat(const struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700511
512/**
Simon Glasscaa4daa2020-12-03 16:55:18 -0700513 * dev_get_parent_plat() - Get the parent platform data for a device
Simon Glasscdc133b2015-01-25 08:27:01 -0700514 *
515 * This checks that dev is not NULL, but no other checks for now
516 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100517 * @dev: Device to check
518 * Return: parent's platform data, or NULL if none
Simon Glasscdc133b2015-01-25 08:27:01 -0700519 */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700520void *dev_get_parent_plat(const struct udevice *dev);
Simon Glasscdc133b2015-01-25 08:27:01 -0700521
522/**
Simon Glasscaa4daa2020-12-03 16:55:18 -0700523 * dev_get_uclass_plat() - Get the uclass platform data for a device
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200524 *
525 * This checks that dev is not NULL, but no other checks for now
526 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100527 * @dev: Device to check
528 * Return: uclass's platform data, or NULL if none
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200529 */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700530void *dev_get_uclass_plat(const struct udevice *dev);
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200531
532/**
Simon Glass9a79f6e2015-09-28 23:32:02 -0600533 * dev_get_priv() - Get the private data for a device
534 *
535 * This checks that dev is not NULL, but no other checks for now
536 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100537 * @dev: Device to check
538 * Return: private data, or NULL if none
Simon Glass9a79f6e2015-09-28 23:32:02 -0600539 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600540void *dev_get_priv(const struct udevice *dev);
Simon Glass9a79f6e2015-09-28 23:32:02 -0600541
542/**
Simon Glassbcbe3d12015-09-28 23:32:01 -0600543 * dev_get_parent_priv() - Get the parent private data for a device
Simon Glasse59f4582014-07-23 06:55:20 -0600544 *
Simon Glassbcbe3d12015-09-28 23:32:01 -0600545 * The parent private data is data stored in the device but owned by the
546 * parent. For example, a USB device may have parent data which contains
547 * information about how to talk to the device over USB.
Simon Glasse59f4582014-07-23 06:55:20 -0600548 *
549 * This checks that dev is not NULL, but no other checks for now
550 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100551 * @dev: Device to check
552 * Return: parent data, or NULL if none
Simon Glasse59f4582014-07-23 06:55:20 -0600553 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600554void *dev_get_parent_priv(const struct udevice *dev);
Simon Glasse59f4582014-07-23 06:55:20 -0600555
556/**
Simon Glasse564f052015-03-05 12:25:20 -0700557 * dev_get_uclass_priv() - Get the private uclass data for a device
558 *
559 * This checks that dev is not NULL, but no other checks for now
560 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100561 * @dev: Device to check
562 * Return: private uclass data for this device, or NULL if none
Simon Glasse564f052015-03-05 12:25:20 -0700563 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600564void *dev_get_uclass_priv(const struct udevice *dev);
Simon Glasse564f052015-03-05 12:25:20 -0700565
566/**
Simon Glass930a3dd2022-05-08 04:39:24 -0600567 * dev_get_attach_ptr() - Get the value of an attached pointed tag
568 *
569 * The tag is assumed to hold a pointer, if it exists
570 *
571 * @dev: Device to look at
572 * @tag: Tag to access
573 * @return value of tag, or NULL if there is no tag of this type
574 */
575void *dev_get_attach_ptr(const struct udevice *dev, enum dm_tag_t tag);
576
577/**
578 * dev_get_attach_size() - Get the size of an attached tag
579 *
580 * Core tags have an automatic-allocation mechanism where the allocated size is
581 * defined by the device, parent or uclass. This returns the size associated
582 * with a particular tag
583 *
584 * @dev: Device to look at
585 * @tag: Tag to access
586 * @return size of auto-allocated data, 0 if none
587 */
588int dev_get_attach_size(const struct udevice *dev, enum dm_tag_t tag);
589
590/**
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100591 * dev_get_parent() - Get the parent of a device
Simon Glass9a79f6e2015-09-28 23:32:02 -0600592 *
593 * @child: Child to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100594 * Return: parent of child, or NULL if this is the root device
Simon Glass9a79f6e2015-09-28 23:32:02 -0600595 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600596struct udevice *dev_get_parent(const struct udevice *child);
Simon Glass9a79f6e2015-09-28 23:32:02 -0600597
598/**
Simon Glass39de8432015-03-25 12:21:55 -0600599 * dev_get_driver_data() - get the driver data used to bind a device
Simon Glass2ef249b2014-11-11 10:46:18 -0700600 *
601 * When a device is bound using a device tree node, it matches a
Simon Glass8d1f3a92015-09-28 23:32:06 -0600602 * particular compatible string in struct udevice_id. This function
Simon Glass39de8432015-03-25 12:21:55 -0600603 * returns the associated data value for that compatible string. This is
604 * the 'data' field in struct udevice_id.
605 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100606 * As an example, consider this structure::
607 *
608 * static const struct udevice_id tegra_i2c_ids[] = {
609 * { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
610 * { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
611 * { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
612 * { }
613 * };
Simon Glass8d1f3a92015-09-28 23:32:06 -0600614 *
615 * When driver model finds a driver for this it will store the 'data' value
616 * corresponding to the compatible string it matches. This function returns
617 * that value. This allows the driver to handle several variants of a device.
618 *
Simon Glass39de8432015-03-25 12:21:55 -0600619 * For USB devices, this is the driver_info field in struct usb_device_id.
620 *
621 * @dev: Device to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100622 * Return: driver data (0 if none is provided)
Simon Glass2ef249b2014-11-11 10:46:18 -0700623 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600624ulong dev_get_driver_data(const struct udevice *dev);
Simon Glass2ef249b2014-11-11 10:46:18 -0700625
Przemyslaw Marczakcc73d372015-04-15 13:07:24 +0200626/**
627 * dev_get_driver_ops() - get the device's driver's operations
628 *
629 * This checks that dev is not NULL, and returns the pointer to device's
630 * driver's operations.
631 *
632 * @dev: Device to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100633 * Return: void pointer to driver's operations or NULL for NULL-dev or NULL-ops
Przemyslaw Marczakcc73d372015-04-15 13:07:24 +0200634 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600635const void *dev_get_driver_ops(const struct udevice *dev);
Przemyslaw Marczakcc73d372015-04-15 13:07:24 +0200636
Simon Glass8d1f3a92015-09-28 23:32:06 -0600637/**
Simon Glassb3670532015-01-25 08:27:04 -0700638 * device_get_uclass_id() - return the uclass ID of a device
639 *
640 * @dev: Device to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100641 * Return: uclass ID for the device
Simon Glassb3670532015-01-25 08:27:04 -0700642 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600643enum uclass_id device_get_uclass_id(const struct udevice *dev);
Simon Glassb3670532015-01-25 08:27:04 -0700644
Simon Glass8d1f3a92015-09-28 23:32:06 -0600645/**
Przemyslaw Marczakf9c370d2015-04-15 13:07:25 +0200646 * dev_get_uclass_name() - return the uclass name of a device
647 *
648 * This checks that dev is not NULL.
649 *
650 * @dev: Device to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100651 * Return: pointer to the uclass name for the device
Przemyslaw Marczakf9c370d2015-04-15 13:07:25 +0200652 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600653const char *dev_get_uclass_name(const struct udevice *dev);
Przemyslaw Marczakf9c370d2015-04-15 13:07:25 +0200654
Simon Glass2ef249b2014-11-11 10:46:18 -0700655/**
Simon Glass997c87b2014-07-23 06:55:19 -0600656 * device_get_child() - Get the child of a device by index
657 *
658 * Returns the numbered child, 0 being the first. This does not use
659 * sequence numbers, only the natural order.
660 *
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100661 * @parent: Parent device to check
Simon Glass997c87b2014-07-23 06:55:19 -0600662 * @index: Child index
663 * @devp: Returns pointer to device
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100664 * Return:
665 * 0 if OK, -ENODEV if no such device, other error if the device fails to probe
Simon Glass997c87b2014-07-23 06:55:19 -0600666 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700667int device_get_child(const struct udevice *parent, int index,
668 struct udevice **devp);
Simon Glass997c87b2014-07-23 06:55:19 -0600669
670/**
Simon Glass6476c4d2021-12-16 20:59:32 -0700671 * device_get_child_count() - Get the child count of a device
Lokesh Vutla240b9322019-09-04 16:01:26 +0530672 *
673 * Returns the number of children to a device.
674 *
675 * @parent: Parent device to check
676 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700677int device_get_child_count(const struct udevice *parent);
Lokesh Vutla240b9322019-09-04 16:01:26 +0530678
679/**
Simon Glass6476c4d2021-12-16 20:59:32 -0700680 * device_get_decendent_count() - Get the total number of decendents of a device
681 *
682 * Returns the total number of decendents, including all children
683 *
684 * @parent: Parent device to check
685 */
686int device_get_decendent_count(const struct udevice *parent);
687
688/**
Simon Glass5a66a8f2014-07-23 06:55:12 -0600689 * device_find_child_by_seq() - Find a child device based on a sequence
690 *
Simon Glass99175912020-12-16 21:20:29 -0700691 * This searches for a device with the given seq.
Simon Glass5a66a8f2014-07-23 06:55:12 -0600692 *
693 * @parent: Parent device
Simon Glass99175912020-12-16 21:20:29 -0700694 * @seq: Sequence number to find (0=first)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600695 * @devp: Returns pointer to device (there is only one per for each seq).
696 * Set to NULL if none is found
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100697 * Return: 0 if OK, -ENODEV if not found
Simon Glass5a66a8f2014-07-23 06:55:12 -0600698 */
Simon Glass99175912020-12-16 21:20:29 -0700699int device_find_child_by_seq(const struct udevice *parent, int seq,
700 struct udevice **devp);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600701
Simon Glass997c87b2014-07-23 06:55:19 -0600702/**
703 * device_get_child_by_seq() - Get a child device based on a sequence
704 *
705 * If an active device has this sequence it will be returned. If there is no
706 * such device then this will check for a device that is requesting this
707 * sequence.
708 *
709 * The device is probed to activate it ready for use.
710 *
711 * @parent: Parent device
712 * @seq: Sequence number to find (0=first)
713 * @devp: Returns pointer to device (there is only one per for each seq)
714 * Set to NULL if none is found
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100715 * Return: 0 if OK, -ve on error
Simon Glass997c87b2014-07-23 06:55:19 -0600716 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700717int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass997c87b2014-07-23 06:55:19 -0600718 struct udevice **devp);
719
720/**
721 * device_find_child_by_of_offset() - Find a child device based on FDT offset
722 *
723 * Locates a child device by its device tree offset.
724 *
725 * @parent: Parent device
726 * @of_offset: Device tree offset to find
727 * @devp: Returns pointer to device if found, otherwise this is set to NULL
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100728 * Return: 0 if OK, -ve on error
Simon Glass997c87b2014-07-23 06:55:19 -0600729 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700730int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass997c87b2014-07-23 06:55:19 -0600731 struct udevice **devp);
732
733/**
734 * device_get_child_by_of_offset() - Get a child device based on FDT offset
735 *
736 * Locates a child device by its device tree offset.
737 *
738 * The device is probed to activate it ready for use.
739 *
740 * @parent: Parent device
741 * @of_offset: Device tree offset to find
742 * @devp: Returns pointer to device if found, otherwise this is set to NULL
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100743 * Return: 0 if OK, -ve on error
Simon Glass997c87b2014-07-23 06:55:19 -0600744 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700745int device_get_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass997c87b2014-07-23 06:55:19 -0600746 struct udevice **devp);
747
Simon Glassa8981d42014-10-13 23:41:49 -0600748/**
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200749 * device_find_global_by_ofnode() - Get a device based on ofnode
Simon Glass26930472015-06-23 15:38:37 -0600750 *
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200751 * Locates a device by its device tree ofnode, searching globally throughout
752 * the all driver model devices.
753 *
754 * The device is NOT probed
755 *
756 * @node: Device tree ofnode to find
757 * @devp: Returns pointer to device if found, otherwise this is set to NULL
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100758 * Return: 0 if OK, -ve on error
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200759 */
760
761int device_find_global_by_ofnode(ofnode node, struct udevice **devp);
762
763/**
764 * device_get_global_by_ofnode() - Get a device based on ofnode
765 *
766 * Locates a device by its device tree ofnode, searching globally throughout
Simon Glass26930472015-06-23 15:38:37 -0600767 * the all driver model devices.
768 *
769 * The device is probed to activate it ready for use.
770 *
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200771 * @node: Device tree ofnode to find
Simon Glass26930472015-06-23 15:38:37 -0600772 * @devp: Returns pointer to device if found, otherwise this is set to NULL
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100773 * Return: 0 if OK, -ve on error
Simon Glass26930472015-06-23 15:38:37 -0600774 */
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200775int device_get_global_by_ofnode(ofnode node, struct udevice **devp);
Simon Glass26930472015-06-23 15:38:37 -0600776
777/**
Simon Glasscc469b72021-03-15 17:25:28 +1300778 * device_get_by_ofplat_idx() - Get a device based on of-platdata index
Simon Glass8a38abf2020-10-03 11:31:40 -0600779 *
Simon Glasscc469b72021-03-15 17:25:28 +1300780 * Locates a device by either its struct driver_info index, or its
781 * struct udevice index. The latter is used with OF_PLATDATA_INST, since we have
782 * a list of build-time instantiated struct udevice records, The former is used
783 * with !OF_PLATDATA_INST since in that case we have a list of
784 * struct driver_info records.
785 *
786 * The index number is written into the idx field of struct phandle_1_arg, etc.
787 * It is the position of this driver_info/udevice in its linker list.
Simon Glass8a38abf2020-10-03 11:31:40 -0600788 *
789 * The device is probed to activate it ready for use.
790 *
Simon Glasscc469b72021-03-15 17:25:28 +1300791 * @idx: Index number of the driver_info/udevice structure (0=first)
Simon Glass8a38abf2020-10-03 11:31:40 -0600792 * @devp: Returns pointer to device if found, otherwise this is set to NULL
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100793 * Return: 0 if OK, -ve on error
Simon Glass8a38abf2020-10-03 11:31:40 -0600794 */
Simon Glasscc469b72021-03-15 17:25:28 +1300795int device_get_by_ofplat_idx(uint idx, struct udevice **devp);
Simon Glass8a38abf2020-10-03 11:31:40 -0600796
797/**
Simon Glassa8981d42014-10-13 23:41:49 -0600798 * device_find_first_child() - Find the first child of a device
799 *
800 * @parent: Parent device to search
801 * @devp: Returns first child device, or NULL if none
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100802 * Return: 0
Simon Glassa8981d42014-10-13 23:41:49 -0600803 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700804int device_find_first_child(const struct udevice *parent,
805 struct udevice **devp);
Simon Glassa8981d42014-10-13 23:41:49 -0600806
807/**
Simon Glass3f416f32015-07-27 15:47:19 -0600808 * device_find_next_child() - Find the next child of a device
Simon Glassa8981d42014-10-13 23:41:49 -0600809 *
810 * @devp: Pointer to previous child device on entry. Returns pointer to next
811 * child device, or NULL if none
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100812 * Return: 0
Simon Glassa8981d42014-10-13 23:41:49 -0600813 */
814int device_find_next_child(struct udevice **devp);
815
Peng Fanc9cac3f2015-02-10 14:46:32 +0800816/**
Simon Glasscdb6aa02018-10-01 12:22:07 -0600817 * device_find_first_inactive_child() - Find the first inactive child
818 *
819 * This is used to locate an existing child of a device which is of a given
820 * uclass.
821 *
Simon Glass3abe1112018-11-18 08:14:31 -0700822 * The device is NOT probed
823 *
Simon Glasscdb6aa02018-10-01 12:22:07 -0600824 * @parent: Parent device to search
825 * @uclass_id: Uclass to look for
Simon Glass79b3f362021-08-18 21:40:29 -0600826 * @devp: Returns device found, if any, else NULL
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100827 * Return: 0 if found, else -ENODEV
Simon Glasscdb6aa02018-10-01 12:22:07 -0600828 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700829int device_find_first_inactive_child(const struct udevice *parent,
Simon Glasscdb6aa02018-10-01 12:22:07 -0600830 enum uclass_id uclass_id,
831 struct udevice **devp);
832
833/**
Simon Glass3abe1112018-11-18 08:14:31 -0700834 * device_find_first_child_by_uclass() - Find the first child of a device in uc
835 *
836 * @parent: Parent device to search
837 * @uclass_id: Uclass to look for
Simon Glass79b3f362021-08-18 21:40:29 -0600838 * @devp: Returns first child device in that uclass, if any, else NULL
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100839 * Return: 0 if found, else -ENODEV
Simon Glass3abe1112018-11-18 08:14:31 -0700840 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700841int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glass3abe1112018-11-18 08:14:31 -0700842 enum uclass_id uclass_id,
843 struct udevice **devp);
844
845/**
Simon Glass4e0710a2022-04-24 23:31:00 -0600846 * device_find_child_by_namelen() - Find a child by device name
Simon Glass3abe1112018-11-18 08:14:31 -0700847 *
848 * @parent: Parent device to search
849 * @name: Name to look for
Simon Glass4b030172021-10-23 17:26:08 -0600850 * @len: Length of the name
851 * @devp: Returns device found, if any
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100852 * Return: 0 if found, else -ENODEV
Simon Glass4b030172021-10-23 17:26:08 -0600853 */
854int device_find_child_by_namelen(const struct udevice *parent, const char *name,
855 int len, struct udevice **devp);
856
857/**
858 * device_find_child_by_name() - Find a child by device name
859 *
860 * @parent: Parent device to search
861 * @name: Name to look for
Simon Glass3abe1112018-11-18 08:14:31 -0700862 * @devp: Returns device found, if any
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100863 * Return: 0 if found, else -ENODEV
Simon Glass3abe1112018-11-18 08:14:31 -0700864 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700865int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glass3abe1112018-11-18 08:14:31 -0700866 struct udevice **devp);
867
868/**
Simon Glasscaa4daa2020-12-03 16:55:18 -0700869 * device_first_child_ofdata_err() - Find the first child and reads its plat
Simon Glassf262d4c2020-01-27 08:49:47 -0700870 *
Simon Glassd1998a92020-12-03 16:55:21 -0700871 * The of_to_plat() method is called on the child before it is returned,
Simon Glassf262d4c2020-01-27 08:49:47 -0700872 * but the child is not probed.
873 *
874 * @parent: Parent to check
875 * @devp: Returns child that was found, if any
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100876 * Return: 0 on success, -ENODEV if no children, other -ve on error
Simon Glassf262d4c2020-01-27 08:49:47 -0700877 */
878int device_first_child_ofdata_err(struct udevice *parent,
879 struct udevice **devp);
880
881/*
Simon Glasscaa4daa2020-12-03 16:55:18 -0700882 * device_next_child_ofdata_err() - Find the next child and read its plat
Simon Glassf262d4c2020-01-27 08:49:47 -0700883 *
Simon Glassd1998a92020-12-03 16:55:21 -0700884 * The of_to_plat() method is called on the child before it is returned,
Simon Glassf262d4c2020-01-27 08:49:47 -0700885 * but the child is not probed.
886 *
887 * @devp: On entry, points to the previous child; on exit returns the child that
888 * was found, if any
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100889 * Return: 0 on success, -ENODEV if no children, other -ve on error
Simon Glassf262d4c2020-01-27 08:49:47 -0700890 */
891int device_next_child_ofdata_err(struct udevice **devp);
892
893/**
Simon Glass903e83e2020-01-27 08:49:48 -0700894 * device_first_child_err() - Get the first child of a device
895 *
896 * The device returned is probed if necessary, and ready for use
897 *
898 * @parent: Parent device to search
899 * @devp: Returns device found, if any
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100900 * Return: 0 if found, -ENODEV if not, -ve error if device failed to probe
Simon Glass903e83e2020-01-27 08:49:48 -0700901 */
902int device_first_child_err(struct udevice *parent, struct udevice **devp);
903
904/**
905 * device_next_child_err() - Get the next child of a parent device
906 *
907 * The device returned is probed if necessary, and ready for use
908 *
909 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
910 * to the next sibling if no error occurred
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100911 * Return: 0 if found, -ENODEV if not, -ve error if device failed to probe
Simon Glass903e83e2020-01-27 08:49:48 -0700912 */
913int device_next_child_err(struct udevice **devp);
914
915/**
Simon Glassc5785672015-03-25 12:21:57 -0600916 * device_has_children() - check if a device has any children
917 *
918 * @dev: Device to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100919 * Return: true if the device has one or more children
Simon Glassc5785672015-03-25 12:21:57 -0600920 */
Simon Glass9f15cc12018-10-01 12:22:06 -0600921bool device_has_children(const struct udevice *dev);
Simon Glassc5785672015-03-25 12:21:57 -0600922
923/**
924 * device_has_active_children() - check if a device has any active children
925 *
926 * @dev: Device to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100927 * Return: true if the device has one or more children and at least one of
Simon Glassc5785672015-03-25 12:21:57 -0600928 * them is active (probed).
929 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700930bool device_has_active_children(const struct udevice *dev);
Simon Glassc5785672015-03-25 12:21:57 -0600931
932/**
933 * device_is_last_sibling() - check if a device is the last sibling
934 *
935 * This function can be useful for display purposes, when special action needs
936 * to be taken when displaying the last sibling. This can happen when a tree
937 * view of devices is being displayed.
938 *
939 * @dev: Device to check
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100940 * Return: true if there are no more siblings after this one - i.e. is it
Simon Glassc5785672015-03-25 12:21:57 -0600941 * last in the list.
942 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700943bool device_is_last_sibling(const struct udevice *dev);
Simon Glassc5785672015-03-25 12:21:57 -0600944
Simon Glassf5c67ea2015-07-30 13:40:39 -0600945/**
946 * device_set_name() - set the name of a device
947 *
948 * This must be called in the device's bind() method and no later. Normally
949 * this is unnecessary but for probed devices which don't get a useful name
950 * this function can be helpful.
951 *
Simon Glassa2040fa2016-05-01 13:52:23 -0600952 * The name is allocated and will be freed automatically when the device is
953 * unbound.
954 *
Simon Glassf5c67ea2015-07-30 13:40:39 -0600955 * @dev: Device to update
956 * @name: New name (this string is allocated new memory and attached to
957 * the device)
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100958 * Return: 0 if OK, -ENOMEM if there is not enough memory to allocate the
Simon Glassf5c67ea2015-07-30 13:40:39 -0600959 * string
960 */
961int device_set_name(struct udevice *dev, const char *name);
962
Bin Meng1e0f2262015-09-11 03:24:34 -0700963/**
Simon Glassa2040fa2016-05-01 13:52:23 -0600964 * device_set_name_alloced() - note that a device name is allocated
965 *
Simon Glassfd1c2d92016-07-04 11:58:15 -0600966 * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
Simon Glassa2040fa2016-05-01 13:52:23 -0600967 * unbound the name will be freed. This avoids memory leaks.
968 *
969 * @dev: Device to update
970 */
971void device_set_name_alloced(struct udevice *dev);
972
973/**
Simon Glass911f3ae2017-05-18 20:08:57 -0600974 * device_is_compatible() - check if the device is compatible with the compat
Mugunthan V N73443b92016-04-28 15:36:02 +0530975 *
976 * This allows to check whether the device is comaptible with the compat.
977 *
978 * @dev: udevice pointer for which compatible needs to be verified.
979 * @compat: Compatible string which needs to verified in the given
980 * device
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100981 * Return: true if OK, false if the compatible is not found
Mugunthan V N73443b92016-04-28 15:36:02 +0530982 */
Simon Glassfc347fb2020-01-27 08:49:36 -0700983bool device_is_compatible(const struct udevice *dev, const char *compat);
Mugunthan V N73443b92016-04-28 15:36:02 +0530984
985/**
986 * of_machine_is_compatible() - check if the machine is compatible with
987 * the compat
988 *
989 * This allows to check whether the machine is comaptible with the compat.
990 *
991 * @compat: Compatible string which needs to verified
Patrick Delaunay9fb1c772022-01-12 10:53:46 +0100992 * Return: true if OK, false if the compatible is not found
Mugunthan V N73443b92016-04-28 15:36:02 +0530993 */
994bool of_machine_is_compatible(const char *compat);
995
996/**
Mario Sixe4c98a52018-06-26 08:46:50 +0200997 * dev_disable_by_path() - Disable a device given its device tree path
998 *
999 * @path: The device tree path identifying the device to be disabled
Patrick Delaunay9fb1c772022-01-12 10:53:46 +01001000 * Return: 0 on success, -ve on error
Mario Sixe4c98a52018-06-26 08:46:50 +02001001 */
1002int dev_disable_by_path(const char *path);
1003
1004/**
1005 * dev_enable_by_path() - Enable a device given its device tree path
1006 *
1007 * @path: The device tree path identifying the device to be enabled
Patrick Delaunay9fb1c772022-01-12 10:53:46 +01001008 * Return: 0 on success, -ve on error
Mario Sixe4c98a52018-06-26 08:46:50 +02001009 */
1010int dev_enable_by_path(const char *path);
1011
1012/**
Bin Meng1e0f2262015-09-11 03:24:34 -07001013 * device_is_on_pci_bus - Test if a device is on a PCI bus
1014 *
1015 * @dev: device to test
Patrick Delaunay9fb1c772022-01-12 10:53:46 +01001016 * Return: true if it is on a PCI bus, false otherwise
Bin Meng1e0f2262015-09-11 03:24:34 -07001017 */
Simon Glassfc347fb2020-01-27 08:49:36 -07001018static inline bool device_is_on_pci_bus(const struct udevice *dev)
Bin Meng1e0f2262015-09-11 03:24:34 -07001019{
Simon Glass20349782020-07-07 13:12:10 -06001020 return dev->parent && device_get_uclass_id(dev->parent) == UCLASS_PCI;
Bin Meng1e0f2262015-09-11 03:24:34 -07001021}
1022
Simon Glass7aeac5b2015-11-08 23:47:52 -07001023/**
1024 * device_foreach_child_safe() - iterate through child devices safely
1025 *
1026 * This allows the @pos child to be removed in the loop if required.
1027 *
1028 * @pos: struct udevice * for the current device
1029 * @next: struct udevice * for the next device
1030 * @parent: parent device to scan
1031 */
1032#define device_foreach_child_safe(pos, next, parent) \
1033 list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
1034
Simon Glasscc7f66f2016-07-05 17:10:08 -06001035/**
Simon Glasse5f73902019-09-25 08:55:56 -06001036 * device_foreach_child() - iterate through child devices
1037 *
1038 * @pos: struct udevice * for the current device
1039 * @parent: parent device to scan
1040 */
1041#define device_foreach_child(pos, parent) \
1042 list_for_each_entry(pos, &parent->child_head, sibling_node)
1043
1044/**
Simon Glassd1998a92020-12-03 16:55:21 -07001045 * device_foreach_child_of_to_plat() - iterate through children
Simon Glassf262d4c2020-01-27 08:49:47 -07001046 *
1047 * This stops when it gets an error, with @pos set to the device that failed to
1048 * read ofdata.
Patrick Delaunay9fb1c772022-01-12 10:53:46 +01001049 *
Simon Glassf262d4c2020-01-27 08:49:47 -07001050 * This creates a for() loop which works through the available children of
1051 * a device in order from start to end. Device ofdata is read by calling
Simon Glassd1998a92020-12-03 16:55:21 -07001052 * device_of_to_plat() on each one. The devices are not probed.
Simon Glassf262d4c2020-01-27 08:49:47 -07001053 *
1054 * @pos: struct udevice * for the current device
1055 * @parent: parent device to scan
1056 */
Simon Glassd1998a92020-12-03 16:55:21 -07001057#define device_foreach_child_of_to_plat(pos, parent) \
AKASHI Takahiro3cfc0422021-12-10 15:49:36 +09001058 for (int _ret = device_first_child_ofdata_err(parent, &pos); !_ret; \
1059 _ret = device_next_child_ofdata_err(&pos))
Simon Glassf262d4c2020-01-27 08:49:47 -07001060
1061/**
Simon Glass903e83e2020-01-27 08:49:48 -07001062 * device_foreach_child_probe() - iterate through children, probing them
1063 *
1064 * This creates a for() loop which works through the available children of
1065 * a device in order from start to end. Devices are probed if necessary,
1066 * and ready for use.
1067 *
1068 * This stops when it gets an error, with @pos set to the device that failed to
1069 * probe
1070 *
1071 * @pos: struct udevice * for the current device
1072 * @parent: parent device to scan
1073 */
1074#define device_foreach_child_probe(pos, parent) \
AKASHI Takahiro3cfc0422021-12-10 15:49:36 +09001075 for (int _ret = device_first_child_err(parent, &pos); !_ret; \
1076 _ret = device_next_child_err(&pos))
Simon Glass903e83e2020-01-27 08:49:48 -07001077
1078/**
Dario Binacchi970cd912020-10-11 14:27:07 +02001079 * dm_scan_fdt_dev() - Bind child device in the device tree
Simon Glasscc7f66f2016-07-05 17:10:08 -06001080 *
1081 * This handles device which have sub-nodes in the device tree. It scans all
1082 * sub-nodes and binds drivers for each node where a driver can be found.
1083 *
1084 * If this is called prior to relocation, only pre-relocation devices will be
1085 * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
1086 * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
1087 * be bound.
1088 *
1089 * @dev: Device to scan
Patrick Delaunay9fb1c772022-01-12 10:53:46 +01001090 * Return: 0 if OK, -ve on error
Simon Glasscc7f66f2016-07-05 17:10:08 -06001091 */
1092int dm_scan_fdt_dev(struct udevice *dev);
1093
Simon Glass6494d702014-02-26 15:59:18 -07001094#endif