uclass.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/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. /**
  42. * struct uclass_driver - Driver for the uclass
  43. *
  44. * A uclass_driver provides a consistent interface to a set of related
  45. * drivers.
  46. *
  47. * @name: Name of uclass driver
  48. * @id: ID number of this uclass
  49. * @post_bind: Called after a new device is bound to this uclass
  50. * @pre_unbind: Called before a device is unbound from this uclass
  51. * @pre_probe: Called before a new device is probed
  52. * @post_probe: Called after a new device is probed
  53. * @pre_remove: Called before a device is removed
  54. * @child_post_bind: Called after a child is bound to a device in this uclass
  55. * @init: Called to set up the uclass
  56. * @destroy: Called to destroy the uclass
  57. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  58. * to be allocated in the uclass's ->priv pointer. If zero, then the uclass
  59. * driver is responsible for allocating any data required.
  60. * @per_device_auto_alloc_size: Each device can hold private data owned
  61. * by the uclass. If required this will be automatically allocated if this
  62. * value is non-zero.
  63. * @per_device_platdata_auto_alloc_size: Each device can hold platform data
  64. * owned by the uclass as 'dev->uclass_platdata'. If the value is non-zero,
  65. * then this will be automatically allocated.
  66. * @per_child_auto_alloc_size: Each child device (of a parent in this
  67. * uclass) can hold parent data for the device/uclass. This value is only
  68. * used as a falback if this member is 0 in the driver.
  69. * @per_child_platdata_auto_alloc_size: A bus likes to store information about
  70. * its children. If non-zero this is the size of this data, to be allocated
  71. * in the child device's parent_platdata pointer. This value is only used as
  72. * a falback if this member is 0 in the driver.
  73. * @ops: Uclass operations, providing the consistent interface to devices
  74. * within the uclass.
  75. * @flags: Flags for this uclass (DM_UC_...)
  76. */
  77. struct uclass_driver {
  78. const char *name;
  79. enum uclass_id id;
  80. int (*post_bind)(struct udevice *dev);
  81. int (*pre_unbind)(struct udevice *dev);
  82. int (*pre_probe)(struct udevice *dev);
  83. int (*post_probe)(struct udevice *dev);
  84. int (*pre_remove)(struct udevice *dev);
  85. int (*child_post_bind)(struct udevice *dev);
  86. int (*child_pre_probe)(struct udevice *dev);
  87. int (*init)(struct uclass *class);
  88. int (*destroy)(struct uclass *class);
  89. int priv_auto_alloc_size;
  90. int per_device_auto_alloc_size;
  91. int per_device_platdata_auto_alloc_size;
  92. int per_child_auto_alloc_size;
  93. int per_child_platdata_auto_alloc_size;
  94. const void *ops;
  95. uint32_t flags;
  96. };
  97. /* Declare a new uclass_driver */
  98. #define UCLASS_DRIVER(__name) \
  99. ll_entry_declare(struct uclass_driver, __name, uclass)
  100. /**
  101. * uclass_get() - Get a uclass based on an ID, creating it if needed
  102. *
  103. * Every uclass is identified by an ID, a number from 0 to n-1 where n is
  104. * the number of uclasses. This function allows looking up a uclass by its
  105. * ID.
  106. *
  107. * @key: ID to look up
  108. * @ucp: Returns pointer to uclass (there is only one per ID)
  109. * @return 0 if OK, -ve on error
  110. */
  111. int uclass_get(enum uclass_id key, struct uclass **ucp);
  112. /**
  113. * uclass_get_name() - Get the name of a uclass driver
  114. *
  115. * @id: ID to look up
  116. * @returns the name of the uclass driver for that ID, or NULL if none
  117. */
  118. const char *uclass_get_name(enum uclass_id id);
  119. /**
  120. * uclass_get_device() - Get a uclass device based on an ID and index
  121. *
  122. * The device is probed to activate it ready for use.
  123. *
  124. * @id: ID to look up
  125. * @index: Device number within that uclass (0=first)
  126. * @devp: Returns pointer to device (there is only one per for each ID)
  127. * @return 0 if OK, -ve on error
  128. */
  129. int uclass_get_device(enum uclass_id id, int index, struct udevice **devp);
  130. /**
  131. * uclass_get_device_by_name() - Get a uclass device by its name
  132. *
  133. * This searches the devices in the uclass for one with the exactly given name.
  134. *
  135. * The device is probed to activate it ready for use.
  136. *
  137. * @id: ID to look up
  138. * @name: name of a device to get
  139. * @devp: Returns pointer to device (the first one with the name)
  140. * @return 0 if OK, -ve on error
  141. */
  142. int uclass_get_device_by_name(enum uclass_id id, const char *name,
  143. struct udevice **devp);
  144. /**
  145. * uclass_get_device_by_seq() - Get a uclass device based on an ID and sequence
  146. *
  147. * If an active device has this sequence it will be returned. If there is no
  148. * such device then this will check for a device that is requesting this
  149. * sequence.
  150. *
  151. * The device is probed to activate it ready for use.
  152. *
  153. * @id: ID to look up
  154. * @seq: Sequence number to find (0=first)
  155. * @devp: Returns pointer to device (there is only one for each seq)
  156. * @return 0 if OK, -ve on error
  157. */
  158. int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
  159. /**
  160. * uclass_get_device_by_of_offset() - Get a uclass device by device tree node
  161. *
  162. * This searches the devices in the uclass for one attached to the given
  163. * device tree node.
  164. *
  165. * The device is probed to activate it ready for use.
  166. *
  167. * @id: ID to look up
  168. * @node: Device tree offset to search for (if -ve then -ENODEV is returned)
  169. * @devp: Returns pointer to device (there is only one for each node)
  170. * @return 0 if OK, -ve on error
  171. */
  172. int uclass_get_device_by_of_offset(enum uclass_id id, int node,
  173. struct udevice **devp);
  174. /**
  175. * uclass_get_device_by_phandle() - Get a uclass device by phandle
  176. *
  177. * This searches the devices in the uclass for one with the given phandle.
  178. *
  179. * The device is probed to activate it ready for use.
  180. *
  181. * @id: uclass ID to look up
  182. * @parent: Parent device containing the phandle pointer
  183. * @name: Name of property in the parent device node
  184. * @devp: Returns pointer to device (there is only one for each node)
  185. * @return 0 if OK, -ENOENT if there is no @name present in the node, other
  186. * -ve on error
  187. */
  188. int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
  189. const char *name, struct udevice **devp);
  190. /**
  191. * uclass_get_device_by_driver() - Get a uclass device for a driver
  192. *
  193. * This searches the devices in the uclass for one that uses the given
  194. * driver. Use DM_GET_DRIVER(name) for the @drv argument, where 'name' is
  195. * the driver name - as used in U_BOOT_DRIVER(name).
  196. *
  197. * The device is probed to activate it ready for use.
  198. *
  199. * @id: ID to look up
  200. * @drv: Driver to look for
  201. * @devp: Returns pointer to the first device with that driver
  202. * @return 0 if OK, -ve on error
  203. */
  204. int uclass_get_device_by_driver(enum uclass_id id, const struct driver *drv,
  205. struct udevice **devp);
  206. /**
  207. * uclass_first_device() - Get the first device in a uclass
  208. *
  209. * The device returned is probed if necessary, and ready for use
  210. *
  211. * @id: Uclass ID to look up
  212. * @devp: Returns pointer to the first device in that uclass, or NULL if none
  213. * @return 0 if OK (found or not found), other -ve on error
  214. */
  215. int uclass_first_device(enum uclass_id id, struct udevice **devp);
  216. /**
  217. * uclass_first_device_err() - Get the first device in a uclass
  218. *
  219. * The device returned is probed if necessary, and ready for use
  220. *
  221. * @id: Uclass ID to look up
  222. * @devp: Returns pointer to the first device in that uclass, or NULL if none
  223. * @return 0 if found, -ENODEV if not found, other -ve on error
  224. */
  225. int uclass_first_device_err(enum uclass_id id, struct udevice **devp);
  226. /**
  227. * uclass_next_device() - Get the next device in a uclass
  228. *
  229. * The device returned is probed if necessary, and ready for use
  230. *
  231. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  232. * to the next device in the same uclass, or NULL if none
  233. * @return 0 if OK (found or not found), other -ve on error
  234. */
  235. int uclass_next_device(struct udevice **devp);
  236. /**
  237. * uclass_resolve_seq() - Resolve a device's sequence number
  238. *
  239. * On entry dev->seq is -1, and dev->req_seq may be -1 (to allocate a
  240. * sequence number automatically, or >= 0 to select a particular number.
  241. * If the requested sequence number is in use, then this device will
  242. * be allocated another one.
  243. *
  244. * Note that the device's seq value is not changed by this function.
  245. *
  246. * @dev: Device for which to allocate sequence number
  247. * @return sequence number allocated, or -ve on error
  248. */
  249. int uclass_resolve_seq(struct udevice *dev);
  250. /**
  251. * uclass_foreach_dev() - Helper function to iteration through devices
  252. *
  253. * This creates a for() loop which works through the available devices in
  254. * a uclass in order from start to end.
  255. *
  256. * @pos: struct udevice * to hold the current device. Set to NULL when there
  257. * are no more devices.
  258. * @uc: uclass to scan
  259. */
  260. #define uclass_foreach_dev(pos, uc) \
  261. list_for_each_entry(pos, &uc->dev_head, uclass_node)
  262. /**
  263. * uclass_foreach_dev_safe() - Helper function to safely iteration through devs
  264. *
  265. * This creates a for() loop which works through the available devices in
  266. * a uclass in order from start to end. Inside the loop, it is safe to remove
  267. * @pos if required.
  268. *
  269. * @pos: struct udevice * to hold the current device. Set to NULL when there
  270. * are no more devices.
  271. * @next: struct udevice * to hold the next next
  272. * @uc: uclass to scan
  273. */
  274. #define uclass_foreach_dev_safe(pos, next, uc) \
  275. list_for_each_entry_safe(pos, next, &uc->dev_head, uclass_node)
  276. #endif