uclass.h 13 KB

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