1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2013 Google, Inc
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
9 #ifndef _DM_UCLASS_INTERNAL_H
10 #define _DM_UCLASS_INTERNAL_H
12 #include <dm/ofnode.h>
15 * These next two macros DM_UCLASS_INST() and DM_UCLASS_REF() are only allowed
16 * in code generated by dtoc, because the ordering is important and if other
17 * instances creep in then they may mess up the ordering expected by dtoc.
19 * It is OK to use them with 'extern' though, since that does not actually
20 * add a new record to the linker_list.
24 * DM_UCLASS_INST() - Declare a uclass ready for run-time use
26 * This adds an actual struct uclass to a list which is found by driver model
31 * DM_UCLASS_INST(clk) = {
32 * .uc_drv = DM_UCLASS_DRIVER_REF(clk),
36 * @_name: Name of the uclass. This must be a valid C identifier, used by the
39 #define DM_UCLASS_INST(_name) \
40 ll_entry_declare(struct uclass, _name, uclass)
43 * DM_UCLASS_REF() - Get a reference to a uclass
45 * This is useful for referencing a uclass at build time. Before this is used,
46 * an extern DM_UCLASS_INST() must have been declared.
50 * extern DM_UCLASS_INST(clk);
52 * struct uclass *ucs[] = {
56 * @_name: Name of the uclass. This must be a valid C identifier, used by the
58 * Return: struct uclass * for the device
60 #define DM_UCLASS_REF(_name) \
61 ll_entry_ref(struct uclass, _name, uclass)
64 * uclass_set_priv() - Set the private data for a uclass
66 * This is normally handled by driver model, which automatically allocates
67 * private data when an 'auto' size if provided by the uclass driver.
69 * Use this function to override normal operation for special situations, such
70 * as needing to allocate a variable amount of data.
72 * If OF_PLATDATA_RT is enabled, this function cannot be used out of core driver
73 * model code, since the pointer must be within the gd->dm_priv_base region.
75 * @uc Uclass to update
76 * @priv New private-data pointer
78 void uclass_set_priv(struct uclass *uc, void *priv);
81 * uclass_find_next_free_seq() - Get the next free sequence number
83 * This returns the next free sequence number. This is useful only if
84 * OF_CONTROL is not used. The next free sequence number is simply the
85 * maximum sequence number used by all devices in the uclass + 1. The value
86 * returned is always greater than the largest alias, if DM_SEQ_ALIAS is enabled
87 * and the uclass has the DM_UC_FLAG_SEQ_ALIAS flag.
89 * This allows assigning the sequence number in the binding order.
91 * @uc: uclass to check
92 * Return: The next free sequence number
94 int uclass_find_next_free_seq(struct uclass *uc);
97 * uclass_get_device_tail() - handle the end of a get_device call
99 * This handles returning an error or probing a device as needed.
101 * @dev: Device that needs to be probed
102 * @ret: Error to return. If non-zero then the device is not probed
103 * @devp: Returns the value of 'dev' if there is no error
104 * Return: ret, if non-zero, else the result of the device_probe() call
106 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp);
109 * dev_get_uclass_index() - Get uclass and index of device
110 * @dev: - in - Device that we want the uclass/index of
111 * @ucp: - out - A pointer to the uclass the device belongs to
113 * The device is not prepared for use - this is an internal function.
115 * Return: the index of the device in the uclass list or -ENODEV if not found.
117 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp);
120 * uclass_find_device() - Return n-th child of uclass
121 * @id: Id number of the uclass
122 * @index: Position of the child in uclass's list
123 * @devp: Returns pointer to device, or NULL on error
125 * The device is not prepared for use - this is an internal function.
126 * The function uclass_get_device_tail() can be used to probe the device.
128 * Return: the uclass pointer of a child at the given index or
129 * return NULL on error.
131 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp);
134 * uclass_find_first_device() - Return the first device in a uclass
135 * @id: Id number of the uclass
136 * @devp: Returns pointer to device, or NULL on error
138 * The device is not prepared for use - this is an internal function.
139 * The function uclass_get_device_tail() can be used to probe the device.
141 * Return: 0 if OK (found or not found), -ve on error
143 int uclass_find_first_device(enum uclass_id id, struct udevice **devp);
146 * uclass_find_next_device() - Return the next device in a uclass
147 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
148 * to the next device in the same uclass, or NULL if none
150 * The device is not prepared for use - this is an internal function.
151 * The function uclass_get_device_tail() can be used to probe the device.
153 * Return: 0 if OK (found or not found), -ve on error
155 int uclass_find_next_device(struct udevice **devp);
158 * uclass_find_device_by_name() - Find uclass device based on ID and name
160 * This searches for a device with the exactly given name.
162 * The device is NOT probed, it is merely returned.
165 * @name: name of a device to find
166 * @devp: Returns pointer to device (the first one with the name)
167 * Return: 0 if OK, -ve on error
169 int uclass_find_device_by_name(enum uclass_id id, const char *name,
170 struct udevice **devp);
173 * uclass_find_device_by_seq() - Find uclass device based on ID and sequence
175 * This searches for a device with the given seq.
177 * The device is NOT probed, it is merely returned.
180 * @seq: Sequence number to find (0=first)
181 * @devp: Returns pointer to device (there is only one per for each seq)
182 * Return: 0 if OK, -ENODEV if not found
184 int uclass_find_device_by_seq(enum uclass_id id, int seq,
185 struct udevice **devp);
188 * uclass_find_device_by_of_offset() - Find a uclass device by device tree node
190 * This searches the devices in the uclass for one attached to the given
193 * The device is NOT probed, it is merely returned.
196 * @node: Device tree offset to search for (if -ve then -ENODEV is returned)
197 * @devp: Returns pointer to device (there is only one for each node)
198 * Return: 0 if OK, -ve on error
200 int uclass_find_device_by_of_offset(enum uclass_id id, int node,
201 struct udevice **devp);
204 * uclass_find_device_by_of_node() - Find a uclass device by device tree node
206 * This searches the devices in the uclass for one attached to the given
209 * The device is NOT probed, it is merely returned.
212 * @node: Device tree offset to search for (if NULL then -ENODEV is returned)
213 * @devp: Returns pointer to device (there is only one for each node)
214 * Return: 0 if OK, -ve on error
216 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
217 struct udevice **devp);
220 * uclass_find_device_by_phandle() - Find a uclass device by phandle
222 * This searches the devices in the uclass for one with the given phandle.
224 * The device is NOT probed, it is merely returned.
227 * @parent: Parent device containing the phandle pointer
228 * @name: Name of property in the parent device node
229 * @devp: Returns pointer to device (there is only one for each node)
230 * Return: 0 if OK, -ENOENT if there is no @name present in the node, other
233 int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
234 const char *name, struct udevice **devp);
237 * uclass_bind_device() - Associate device with a uclass
239 * Connect the device into uclass's list of devices.
241 * @dev: Pointer to the device
242 * Return: 0 on success, -ve on error
244 int uclass_bind_device(struct udevice *dev);
246 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
248 * uclass_pre_unbind_device() - Prepare to deassociate device with a uclass
250 * Call any handled needed before uclass_unbind_device() is called
252 * @dev: Pointer to the device
253 * Return: 0 on success, -ve on error
255 int uclass_pre_unbind_device(struct udevice *dev);
258 * uclass_unbind_device() - Deassociate device with a uclass
260 * Disconnect the device from uclass's list of devices.
262 * @dev: Pointer to the device
263 * Return: 0 on success, -ve on error
265 int uclass_unbind_device(struct udevice *dev);
268 static inline int uclass_pre_unbind_device(struct udevice *dev) { return 0; }
269 static inline int uclass_unbind_device(struct udevice *dev) { return 0; }
273 * uclass_pre_probe_device() - Deal with a device that is about to be probed
275 * Perform any pre-processing that is needed by the uclass before it can be
276 * probed. This includes the uclass' pre-probe() method and the parent
277 * uclass' child_pre_probe() method.
279 * @dev: Pointer to the device
280 * Return: 0 on success, -ve on error
282 int uclass_pre_probe_device(struct udevice *dev);
285 * uclass_post_probe_device() - Deal with a device that has just been probed
287 * Perform any post-processing of a probed device that is needed by the
290 * @dev: Pointer to the device
291 * Return: 0 on success, -ve on error
293 int uclass_post_probe_device(struct udevice *dev);
296 * uclass_pre_remove_device() - Handle a device which is about to be removed
298 * Perform any pre-processing of a device that is about to be removed.
300 * @dev: Pointer to the device
301 * Return: 0 on success, -ve on error
303 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
304 int uclass_pre_remove_device(struct udevice *dev);
306 static inline int uclass_pre_remove_device(struct udevice *dev) { return 0; }
310 * uclass_get_count() - Get the number of uclasses
312 * Returns the number of uclasses instantiated in driver model
314 int uclass_get_count(void);
317 * uclass_find() - Find uclass by its id
319 * @id: Id to serach for
320 * Return: pointer to uclass, or NULL if not found
322 struct uclass *uclass_find(enum uclass_id key);
325 * uclass_destroy() - Destroy a uclass
327 * Destroy a uclass and all its devices
329 * @uc: uclass to destroy
330 * Return: 0 on success, -ve on error
332 int uclass_destroy(struct uclass *uc);