blob: f2a780682b486e7f93cb94dfac78a38658e6235f [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>
Simon Glass6494d702014-02-26 15:59:18 -07007 */
8
9#ifndef _DM_UCLASS_INTERNAL_H
10#define _DM_UCLASS_INTERNAL_H
11
Simon Glass40bb6372017-05-18 20:09:09 -060012#include <dm/ofnode.h>
13
Simon Glass607f9bc2021-03-15 17:25:14 +130014/*
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.
18 *
19 * It is OK to use them with 'extern' though, since that does not actually
20 * add a new record to the linker_list.
21 */
22
23/**
24 * DM_UCLASS_INST() - Declare a uclass ready for run-time use
25 *
26 * This adds an actual struct uclass to a list which is found by driver model
27 * on start-up.
28 *
29 * For example:
30 *
31 * DM_UCLASS_INST(clk) = {
32 * .uc_drv = DM_UCLASS_DRIVER_REF(clk),
33 * ...
34 * };
35 *
36 * @_name: Name of the uclass. This must be a valid C identifier, used by the
37 * linker_list.
38 */
39#define DM_UCLASS_INST(_name) \
40 ll_entry_declare(struct uclass, _name, uclass)
41
42/**
43 * DM_UCLASS_REF() - Get a reference to a uclass
44 *
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.
47 *
48 * For example:
49 *
50 * extern DM_UCLASS_INST(clk);
51 *
52 * struct uclass *ucs[] = {
53 * DM_UCLASS_REF(clk),
54 * }
55 *
56 * @_name: Name of the uclass. This must be a valid C identifier, used by the
57 * linker_list
58 * @returns struct uclass * for the device
59 */
60#define DM_UCLASS_REF(_name) \
61 ll_entry_ref(struct uclass, _name, uclass)
62
Simon Glass6494d702014-02-26 15:59:18 -070063/**
Simon Glass80647392020-12-22 19:30:26 -070064 * uclass_set_priv() - Set the private data for a uclass
65 *
66 * This is normally handled by driver model, which automatically allocates
67 * private data when an 'auto' size if provided by the uclass driver.
68 *
69 * Use this function to override normal operation for special situations, such
70 * as needing to allocate a variable amount of data.
71 *
72 * @uc Uclass to update
73 * @priv New private-data pointer
74 */
75void uclass_set_priv(struct uclass *uc, void *priv);
76
77/**
Simon Glassa133e212020-12-16 21:20:30 -070078 * uclass_find_next_free_seq() - Get the next free sequence number
Jean-Jacques Hiblot3542ff22018-12-07 14:50:39 +010079 *
Simon Glassa133e212020-12-16 21:20:30 -070080 * This returns the next free sequence number. This is useful only if
81 * OF_CONTROL is not used. The next free sequence number is simply the
82 * maximum sequence number used by all devices in the uclass + 1. The value
83 * returned is always greater than the largest alias, if DM_SEQ_ALIAS is enabled
84 * and the uclass has the DM_UC_FLAG_SEQ_ALIAS flag.
85 *
86 * This allows assigning the sequence number in the binding order.
Jean-Jacques Hiblot3542ff22018-12-07 14:50:39 +010087 *
Simon Glassd03adb42020-12-16 21:20:08 -070088 * @uc: uclass to check
Simon Glassa133e212020-12-16 21:20:30 -070089 * @return The next free sequence number
Jean-Jacques Hiblot3542ff22018-12-07 14:50:39 +010090 */
Simon Glassa133e212020-12-16 21:20:30 -070091int uclass_find_next_free_seq(struct uclass *uc);
Jean-Jacques Hiblot3542ff22018-12-07 14:50:39 +010092
93/**
Przemyslaw Marczak794d5212015-04-20 13:32:32 +020094 * uclass_get_device_tail() - handle the end of a get_device call
95 *
96 * This handles returning an error or probing a device as needed.
97 *
98 * @dev: Device that needs to be probed
99 * @ret: Error to return. If non-zero then the device is not probed
100 * @devp: Returns the value of 'dev' if there is no error
101 * @return ret, if non-zero, else the result of the device_probe() call
102 */
103int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp);
104
105/**
Jean-Jacques Hiblote7c86562018-08-09 16:17:42 +0200106 * dev_get_uclass_index() - Get uclass and index of device
107 * @dev: - in - Device that we want the uclass/index of
108 * @ucp: - out - A pointer to the uclass the device belongs to
109 *
110 * The device is not prepared for use - this is an internal function.
111 *
112 * @return the index of the device in the uclass list or -ENODEV if not found.
113 */
114int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp);
115
116/**
Simon Glass6494d702014-02-26 15:59:18 -0700117 * uclass_find_device() - Return n-th child of uclass
118 * @id: Id number of the uclass
119 * @index: Position of the child in uclass's list
120 * #devp: Returns pointer to device, or NULL on error
121 *
Przemyslaw Marczak794d5212015-04-20 13:32:32 +0200122 * The device is not prepared for use - this is an internal function.
123 * The function uclass_get_device_tail() can be used to probe the device.
Simon Glass6494d702014-02-26 15:59:18 -0700124 *
125 * @return the uclass pointer of a child at the given index or
126 * return NULL on error.
127 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200128int uclass_find_device(enum uclass_id id, int index, struct udevice **devp);
Simon Glass6494d702014-02-26 15:59:18 -0700129
130/**
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200131 * uclass_find_first_device() - Return the first device in a uclass
132 * @id: Id number of the uclass
133 * #devp: Returns pointer to device, or NULL on error
134 *
Przemyslaw Marczak794d5212015-04-20 13:32:32 +0200135 * The device is not prepared for use - this is an internal function.
136 * The function uclass_get_device_tail() can be used to probe the device.
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200137 *
Simon Glass4805a7a2019-09-25 08:55:55 -0600138 * @return 0 if OK (found or not found), -ve on error
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200139 */
140int uclass_find_first_device(enum uclass_id id, struct udevice **devp);
141
142/**
143 * uclass_find_next_device() - Return the next device in a uclass
144 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
145 * to the next device in the same uclass, or NULL if none
146 *
Przemyslaw Marczak794d5212015-04-20 13:32:32 +0200147 * The device is not prepared for use - this is an internal function.
148 * The function uclass_get_device_tail() can be used to probe the device.
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200149 *
Simon Glass4805a7a2019-09-25 08:55:55 -0600150 * @return 0 if OK (found or not found), -ve on error
Przemyslaw Marczakc1d6f912015-04-15 13:07:17 +0200151 */
152int uclass_find_next_device(struct udevice **devp);
153
154/**
Przemyslaw Marczake0735a42015-04-15 13:07:22 +0200155 * uclass_find_device_by_name() - Find uclass device based on ID and name
156 *
Przemyslaw Marczaka7b82502015-04-20 13:32:34 +0200157 * This searches for a device with the exactly given name.
Przemyslaw Marczake0735a42015-04-15 13:07:22 +0200158 *
159 * The device is NOT probed, it is merely returned.
160 *
161 * @id: ID to look up
162 * @name: name of a device to find
163 * @devp: Returns pointer to device (the first one with the name)
164 * @return 0 if OK, -ve on error
165 */
166int uclass_find_device_by_name(enum uclass_id id, const char *name,
167 struct udevice **devp);
168
169/**
170 * uclass_find_device_by_seq() - Find uclass device based on ID and sequence
171 *
Simon Glass99175912020-12-16 21:20:29 -0700172 * This searches for a device with the given seq.
Przemyslaw Marczake0735a42015-04-15 13:07:22 +0200173 *
174 * The device is NOT probed, it is merely returned.
175 *
176 * @id: ID to look up
Simon Glass99175912020-12-16 21:20:29 -0700177 * @seq: Sequence number to find (0=first)
Przemyslaw Marczake0735a42015-04-15 13:07:22 +0200178 * @devp: Returns pointer to device (there is only one per for each seq)
Simon Glass99175912020-12-16 21:20:29 -0700179 * @return 0 if OK, -ENODEV if not found
Przemyslaw Marczake0735a42015-04-15 13:07:22 +0200180 */
Simon Glass99175912020-12-16 21:20:29 -0700181int uclass_find_device_by_seq(enum uclass_id id, int seq,
182 struct udevice **devp);
Przemyslaw Marczake0735a42015-04-15 13:07:22 +0200183
184/**
Simon Glass1b30d612016-01-21 19:43:57 -0700185 * uclass_find_device_by_of_offset() - Find a uclass device by device tree node
186 *
187 * This searches the devices in the uclass for one attached to the given
188 * device tree node.
189 *
190 * The device is NOT probed, it is merely returned.
191 *
192 * @id: ID to look up
193 * @node: Device tree offset to search for (if -ve then -ENODEV is returned)
194 * @devp: Returns pointer to device (there is only one for each node)
195 * @return 0 if OK, -ve on error
196 */
197int uclass_find_device_by_of_offset(enum uclass_id id, int node,
198 struct udevice **devp);
199
200/**
Simon Glass40bb6372017-05-18 20:09:09 -0600201 * uclass_find_device_by_of_node() - Find a uclass device by device tree node
202 *
203 * This searches the devices in the uclass for one attached to the given
204 * device tree node.
205 *
206 * The device is NOT probed, it is merely returned.
207 *
208 * @id: ID to look up
209 * @node: Device tree offset to search for (if NULL then -ENODEV is returned)
210 * @devp: Returns pointer to device (there is only one for each node)
211 * @return 0 if OK, -ve on error
212 */
213int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
214 struct udevice **devp);
215
216/**
Simon Glassd0b4f682018-11-18 08:14:30 -0700217 * uclass_find_device_by_phandle() - Find a uclass device by phandle
218 *
219 * This searches the devices in the uclass for one with the given phandle.
220 *
221 * The device is NOT probed, it is merely returned.
222 *
223 * @id: ID to look up
224 * @parent: Parent device containing the phandle pointer
225 * @name: Name of property in the parent device node
226 * @devp: Returns pointer to device (there is only one for each node)
227 * @return 0 if OK, -ENOENT if there is no @name present in the node, other
228 * -ve on error
229 */
230int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
231 const char *name, struct udevice **devp);
232
233/**
Simon Glass6494d702014-02-26 15:59:18 -0700234 * uclass_bind_device() - Associate device with a uclass
235 *
236 * Connect the device into uclass's list of devices.
237 *
238 * @dev: Pointer to the device
239 * #return 0 on success, -ve on error
240 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200241int uclass_bind_device(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700242
243/**
244 * uclass_unbind_device() - Deassociate device with a uclass
245 *
246 * Disconnect the device from uclass's list of devices.
247 *
248 * @dev: Pointer to the device
249 * #return 0 on success, -ve on error
250 */
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900251#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocher54c5d082014-05-22 12:43:05 +0200252int uclass_unbind_device(struct udevice *dev);
Simon Glass7f9875e2015-02-27 22:06:31 -0700253#else
254static inline int uclass_unbind_device(struct udevice *dev) { return 0; }
255#endif
Simon Glass6494d702014-02-26 15:59:18 -0700256
257/**
Simon Glass02c07b32015-03-05 12:25:22 -0700258 * uclass_pre_probe_device() - Deal with a device that is about to be probed
Simon Glass83c7e432015-01-25 08:27:10 -0700259 *
260 * Perform any pre-processing that is needed by the uclass before it can be
Simon Glass02c07b32015-03-05 12:25:22 -0700261 * probed. This includes the uclass' pre-probe() method and the parent
262 * uclass' child_pre_probe() method.
Simon Glass83c7e432015-01-25 08:27:10 -0700263 *
264 * @dev: Pointer to the device
265 * #return 0 on success, -ve on error
266 */
Simon Glass02c07b32015-03-05 12:25:22 -0700267int uclass_pre_probe_device(struct udevice *dev);
Simon Glass83c7e432015-01-25 08:27:10 -0700268
269/**
Simon Glass6494d702014-02-26 15:59:18 -0700270 * uclass_post_probe_device() - Deal with a device that has just been probed
271 *
272 * Perform any post-processing of a probed device that is needed by the
273 * uclass.
274 *
275 * @dev: Pointer to the device
276 * #return 0 on success, -ve on error
277 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200278int uclass_post_probe_device(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700279
280/**
281 * uclass_pre_remove_device() - Handle a device which is about to be removed
282 *
283 * Perform any pre-processing of a device that is about to be removed.
284 *
285 * @dev: Pointer to the device
286 * #return 0 on success, -ve on error
287 */
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900288#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocher54c5d082014-05-22 12:43:05 +0200289int uclass_pre_remove_device(struct udevice *dev);
Simon Glass7f9875e2015-02-27 22:06:31 -0700290#else
291static inline int uclass_pre_remove_device(struct udevice *dev) { return 0; }
292#endif
Simon Glass6494d702014-02-26 15:59:18 -0700293
294/**
295 * uclass_find() - Find uclass by its id
296 *
297 * @id: Id to serach for
298 * @return pointer to uclass, or NULL if not found
299 */
300struct uclass *uclass_find(enum uclass_id key);
301
302/**
303 * uclass_destroy() - Destroy a uclass
304 *
305 * Destroy a uclass and all its devices
306 *
307 * @uc: uclass to destroy
308 * @return 0 on success, -ve on error
309 */
310int uclass_destroy(struct uclass *uc);
311
Simon Glass6494d702014-02-26 15:59:18 -0700312#endif