uclass.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2013 Google, Inc
  4. *
  5. * (C) Copyright 2012
  6. * Pavel Herrmann <morpheus.ibis@gmail.com>
  7. */
  8. #ifndef _DM_UCLASS_H
  9. #define _DM_UCLASS_H
  10. #include <dm/ofnode.h>
  11. #include <dm/uclass-id.h>
  12. #include <linker_lists.h>
  13. #include <linux/list.h>
  14. /**
  15. * struct uclass - a U-Boot drive class, collecting together similar drivers
  16. *
  17. * A uclass provides an interface to a particular function, which is
  18. * implemented by one or more drivers. Every driver belongs to a uclass even
  19. * if it is the only driver in that uclass. An example uclass is GPIO, which
  20. * provides the ability to change read inputs, set and clear outputs, etc.
  21. * There may be drivers for on-chip SoC GPIO banks, I2C GPIO expanders and
  22. * PMIC IO lines, all made available in a unified way through the uclass.
  23. *
  24. * @priv: Private data for this uclass
  25. * @uc_drv: The driver for the uclass itself, not to be confused with a
  26. * 'struct driver'
  27. * @dev_head: List of devices in this uclass (devices are attached to their
  28. * uclass when their bind method is called)
  29. * @sibling_node: Next uclass in the linked list of uclasses
  30. */
  31. struct uclass {
  32. void *priv;
  33. struct uclass_driver *uc_drv;
  34. struct list_head dev_head;
  35. struct list_head sibling_node;
  36. };
  37. struct driver;
  38. struct udevice;
  39. /* Members of this uclass sequence themselves with aliases */
  40. #define DM_UC_FLAG_SEQ_ALIAS (1 << 0)
  41. /* Same as DM_FLAG_ALLOC_PRIV_DMA */
  42. #define DM_UC_FLAG_ALLOC_PRIV_DMA (1 << 5)
  43. /**
  44. * struct uclass_driver - Driver for the uclass
  45. *
  46. * A uclass_driver provides a consistent interface to a set of related
  47. * drivers.
  48. *
  49. * @name: Name of uclass driver
  50. * @id: ID number of this uclass
  51. * @post_bind: Called after a new device is bound to this uclass
  52. * @pre_unbind: Called before a device is unbound from this uclass
  53. * @pre_probe: Called before a new device is probed
  54. * @post_probe: Called after a new device is probed
  55. * @pre_remove: Called before a device is removed
  56. * @child_post_bind: Called after a child is bound to a device in this uclass
  57. * @child_pre_probe: Called before a child in this uclass is probed
  58. * @child_post_probe: Called after a child in this uclass is probed
  59. * @init: Called to set up the uclass
  60. * @destroy: Called to destroy the uclass
  61. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  62. * to be allocated in the uclass's ->priv pointer. If zero, then the uclass
  63. * driver is responsible for allocating any data required.
  64. * @per_device_auto_alloc_size: Each device can hold private data owned
  65. * by the uclass. If required this will be automatically allocated if this
  66. * value is non-zero.
  67. * @per_device_platdata_auto_alloc_size: Each device can hold platform data
  68. * owned by the uclass as 'dev->uclass_platdata'. If the value is non-zero,
  69. * then this will be automatically allocated.
  70. * @per_child_auto_alloc_size: Each child device (of a parent in this
  71. * uclass) can hold parent data for the device/uclass. This value is only
  72. * used as a fallback if this member is 0 in the driver.
  73. * @per_child_platdata_auto_alloc_size: A bus likes to store information about
  74. * its children. If non-zero this is the size of this data, to be allocated
  75. * in the child device's parent_platdata pointer. This value is only used as
  76. * a fallback if this member is 0 in the driver.
  77. * @ops: Uclass operations, providing the consistent interface to devices
  78. * within the uclass.
  79. * @flags: Flags for this uclass (DM_UC_...)
  80. */
  81. struct uclass_driver {
  82. const char *name;
  83. enum uclass_id id;
  84. int (*post_bind)(struct udevice *dev);
  85. int (*pre_unbind)(struct udevice *dev);
  86. int (*pre_probe)(struct udevice *dev);
  87. int (*post_probe)(struct udevice *dev);
  88. int (*pre_remove)(struct udevice *dev);
  89. int (*child_post_bind)(struct udevice *dev);
  90. int (*child_pre_probe)(struct udevice *dev);
  91. int (*child_post_probe)(struct udevice *dev);
  92. int (*init)(struct uclass *class);
  93. int (*destroy)(struct uclass *class);
  94. int priv_auto_alloc_size;
  95. int per_device_auto_alloc_size;
  96. int per_device_platdata_auto_alloc_size;
  97. int per_child_auto_alloc_size;
  98. int per_child_platdata_auto_alloc_size;
  99. const void *ops;
  100. uint32_t flags;
  101. };
  102. /* Declare a new uclass_driver */
  103. #define UCLASS_DRIVER(__name) \
  104. ll_entry_declare(struct uclass_driver, __name, uclass)
  105. /**
  106. * uclass_get() - Get a uclass based on an ID, creating it if needed
  107. *
  108. * Every uclass is identified by an ID, a number from 0 to n-1 where n is
  109. * the number of uclasses. This function allows looking up a uclass by its
  110. * ID.
  111. *
  112. * @key: ID to look up
  113. * @ucp: Returns pointer to uclass (there is only one per ID)
  114. * @return 0 if OK, -ve on error
  115. */
  116. int uclass_get(enum uclass_id key, struct uclass **ucp);
  117. /**
  118. * uclass_get_name() - Get the name of a uclass driver
  119. *
  120. * @id: ID to look up
  121. * @returns the name of the uclass driver for that ID, or NULL if none
  122. */
  123. const char *uclass_get_name(enum uclass_id id);
  124. /**
  125. * uclass_get_by_name() - Look up a uclass by its driver name
  126. *
  127. * @name: Name to look up
  128. * @returns the associated uclass ID, or UCLASS_INVALID if not found
  129. */
  130. enum uclass_id uclass_get_by_name(const char *name);
  131. /**
  132. * uclass_get_device() - Get a uclass device based on an ID and index
  133. *
  134. * The device is probed to activate it ready for use.
  135. *
  136. * @id: ID to look up
  137. * @index: Device number within that uclass (0=first)
  138. * @devp: Returns pointer to device (there is only one per for each ID)
  139. * @return 0 if OK, -ve on error
  140. */
  141. int uclass_get_device(enum uclass_id id, int index, struct udevice **devp);
  142. /**
  143. * uclass_get_device_by_name() - Get a uclass device by its name
  144. *
  145. * This searches the devices in the uclass for one with the exactly given name.
  146. *
  147. * The device is probed to activate it ready for use.
  148. *
  149. * @id: ID to look up
  150. * @name: name of a device to get
  151. * @devp: Returns pointer to device (the first one with the name)
  152. * @return 0 if OK, -ve on error
  153. */
  154. int uclass_get_device_by_name(enum uclass_id id, const char *name,
  155. struct udevice **devp);
  156. /**
  157. * uclass_get_device_by_seq() - Get a uclass device based on an ID and sequence
  158. *
  159. * If an active device has this sequence it will be returned. If there is no
  160. * such device then this will check for a device that is requesting this
  161. * sequence.
  162. *
  163. * The device is probed to activate it ready for use.
  164. *
  165. * @id: ID to look up
  166. * @seq: Sequence number to find (0=first)
  167. * @devp: Returns pointer to device (there is only one for each seq)
  168. * @return 0 if OK, -ve on error
  169. */
  170. int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
  171. /**
  172. * uclass_get_device_by_of_offset() - Get a uclass device by device tree node
  173. *
  174. * This searches the devices in the uclass for one attached to the given
  175. * device tree node.
  176. *
  177. * The device is probed to activate it ready for use.
  178. *
  179. * @id: ID to look up
  180. * @node: Device tree offset to search for (if -ve then -ENODEV is returned)
  181. * @devp: Returns pointer to device (there is only one for each node)
  182. * @return 0 if OK, -ve on error
  183. */
  184. int uclass_get_device_by_of_offset(enum uclass_id id, int node,
  185. struct udevice **devp);
  186. /**
  187. * uclass_get_device_by_ofnode() - Get a uclass device by device tree node
  188. *
  189. * This searches the devices in the uclass for one attached to the given
  190. * device tree node.
  191. *
  192. * The device is probed to activate it ready for use.
  193. *
  194. * @id: ID to look up
  195. * @np: Device tree node to search for (if NULL then -ENODEV is returned)
  196. * @devp: Returns pointer to device (there is only one for each node)
  197. * @return 0 if OK, -ve on error
  198. */
  199. int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
  200. struct udevice **devp);
  201. /**
  202. * uclass_get_device_by_phandle_id() - Get a uclass device by phandle id
  203. *
  204. * This searches the devices in the uclass for one with the given phandle id.
  205. *
  206. * The device is probed to activate it ready for use.
  207. *
  208. * @id: uclass ID to look up
  209. * @phandle_id: the phandle id to look up
  210. * @devp: Returns pointer to device (there is only one for each node)
  211. * @return 0 if OK, -ENODEV if there is no device match the phandle, other
  212. * -ve on error
  213. */
  214. int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
  215. struct udevice **devp);
  216. /**
  217. * uclass_get_device_by_phandle() - Get 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 probed to activate it ready for use.
  222. *
  223. * @id: uclass 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. */
  230. int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
  231. const char *name, struct udevice **devp);
  232. /**
  233. * uclass_get_device_by_driver() - Get a uclass device for a driver
  234. *
  235. * This searches the devices in the uclass for one that uses the given
  236. * driver. Use DM_GET_DRIVER(name) for the @drv argument, where 'name' is
  237. * the driver name - as used in U_BOOT_DRIVER(name).
  238. *
  239. * The device is probed to activate it ready for use.
  240. *
  241. * @id: ID to look up
  242. * @drv: Driver to look for
  243. * @devp: Returns pointer to the first device with that driver
  244. * @return 0 if OK, -ve on error
  245. */
  246. int uclass_get_device_by_driver(enum uclass_id id, const struct driver *drv,
  247. struct udevice **devp);
  248. /**
  249. * uclass_first_device() - Get the first device in a uclass
  250. *
  251. * The device returned is probed if necessary, and ready for use
  252. *
  253. * This function is useful to start iterating through a list of devices which
  254. * are functioning correctly and can be probed.
  255. *
  256. * @id: Uclass ID to look up
  257. * @devp: Returns pointer to the first device in that uclass if no error
  258. * occurred, or NULL if there is no first device, or an error occurred with
  259. * that device.
  260. * @return 0 if OK (found or not found), other -ve on error
  261. */
  262. int uclass_first_device(enum uclass_id id, struct udevice **devp);
  263. /**
  264. * uclass_first_device_err() - Get the first device in a uclass
  265. *
  266. * The device returned is probed if necessary, and ready for use
  267. *
  268. * @id: Uclass ID to look up
  269. * @devp: Returns pointer to the first device in that uclass, or NULL if none
  270. * @return 0 if found, -ENODEV if not found, other -ve on error
  271. */
  272. int uclass_first_device_err(enum uclass_id id, struct udevice **devp);
  273. /**
  274. * uclass_next_device() - Get the next device in a uclass
  275. *
  276. * The device returned is probed if necessary, and ready for use
  277. *
  278. * This function is useful to iterate through a list of devices which
  279. * are functioning correctly and can be probed.
  280. *
  281. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  282. * to the next device in the uclass if no error occurred, or NULL if there is
  283. * no next device, or an error occurred with that next device.
  284. * @return 0 if OK (found or not found), other -ve on error
  285. */
  286. int uclass_next_device(struct udevice **devp);
  287. /**
  288. * uclass_next_device_err() - Get the next device in a uclass
  289. *
  290. * The device returned is probed if necessary, and ready for use
  291. *
  292. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  293. * to the next device in the uclass if no error occurred, or -ENODEV if
  294. * there is no next device.
  295. * @return 0 if found, -ENODEV if not found, other -ve on error
  296. */
  297. int uclass_next_device_err(struct udevice **devp);
  298. /**
  299. * uclass_first_device_check() - Get the first device in a uclass
  300. *
  301. * The device returned is probed if necessary, and ready for use
  302. *
  303. * This function is useful to start iterating through a list of devices which
  304. * are functioning correctly and can be probed.
  305. *
  306. * @id: Uclass ID to look up
  307. * @devp: Returns pointer to the first device in that uclass, or NULL if there
  308. * is no first device
  309. * @return 0 if OK (found or not found), other -ve on error. If an error occurs
  310. * it is still possible to move to the next device.
  311. */
  312. int uclass_first_device_check(enum uclass_id id, struct udevice **devp);
  313. /**
  314. * uclass_next_device_check() - Get the next device in a uclass
  315. *
  316. * The device returned is probed if necessary, and ready for use
  317. *
  318. * This function is useful to start iterating through a list of devices which
  319. * are functioning correctly and can be probed.
  320. *
  321. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  322. * to the next device in the uclass if any
  323. * @return 0 if OK (found or not found), other -ve on error. If an error occurs
  324. * it is still possible to move to the next device.
  325. */
  326. int uclass_next_device_check(struct udevice **devp);
  327. /**
  328. * uclass_resolve_seq() - Resolve a device's sequence number
  329. *
  330. * On entry dev->seq is -1, and dev->req_seq may be -1 (to allocate a
  331. * sequence number automatically, or >= 0 to select a particular number.
  332. * If the requested sequence number is in use, then this device will
  333. * be allocated another one.
  334. *
  335. * Note that the device's seq value is not changed by this function.
  336. *
  337. * @dev: Device for which to allocate sequence number
  338. * @return sequence number allocated, or -ve on error
  339. */
  340. int uclass_resolve_seq(struct udevice *dev);
  341. /**
  342. * uclass_foreach_dev() - Helper function to iteration through devices
  343. *
  344. * This creates a for() loop which works through the available devices in
  345. * a uclass in order from start to end.
  346. *
  347. * @pos: struct udevice * to hold the current device. Set to NULL when there
  348. * are no more devices.
  349. * @uc: uclass to scan
  350. */
  351. #define uclass_foreach_dev(pos, uc) \
  352. list_for_each_entry(pos, &uc->dev_head, uclass_node)
  353. /**
  354. * uclass_foreach_dev_safe() - Helper function to safely iteration through devs
  355. *
  356. * This creates a for() loop which works through the available devices in
  357. * a uclass in order from start to end. Inside the loop, it is safe to remove
  358. * @pos if required.
  359. *
  360. * @pos: struct udevice * to hold the current device. Set to NULL when there
  361. * are no more devices.
  362. * @next: struct udevice * to hold the next next
  363. * @uc: uclass to scan
  364. */
  365. #define uclass_foreach_dev_safe(pos, next, uc) \
  366. list_for_each_entry_safe(pos, next, &uc->dev_head, uclass_node)
  367. /**
  368. * uclass_foreach_dev_probe() - Helper function to iteration through devices
  369. * of given uclass
  370. *
  371. * This creates a for() loop which works through the available devices in
  372. * a uclass in order from start to end. Devices are probed if necessary,
  373. * and ready for use.
  374. *
  375. * @id: Uclass ID
  376. * @dev: struct udevice * to hold the current device. Set to NULL when there
  377. * are no more devices.
  378. */
  379. #define uclass_foreach_dev_probe(id, dev) \
  380. for (int _ret = uclass_first_device_err(id, &dev); !_ret && dev; \
  381. _ret = uclass_next_device_err(&dev))
  382. #endif