uclass.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 udevice;
  38. /* Members of this uclass sequence themselves with aliases */
  39. #define DM_UC_FLAG_SEQ_ALIAS (1 << 0)
  40. /**
  41. * struct uclass_driver - Driver for the uclass
  42. *
  43. * A uclass_driver provides a consistent interface to a set of related
  44. * drivers.
  45. *
  46. * @name: Name of uclass driver
  47. * @id: ID number of this uclass
  48. * @post_bind: Called after a new device is bound to this uclass
  49. * @pre_unbind: Called before a device is unbound from this uclass
  50. * @pre_probe: Called before a new device is probed
  51. * @post_probe: Called after a new device is probed
  52. * @pre_remove: Called before a device is removed
  53. * @child_post_bind: Called after a child is bound to a device in this uclass
  54. * @init: Called to set up the uclass
  55. * @destroy: Called to destroy the uclass
  56. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  57. * to be allocated in the uclass's ->priv pointer. If zero, then the uclass
  58. * driver is responsible for allocating any data required.
  59. * @per_device_auto_alloc_size: Each device can hold private data owned
  60. * by the uclass. If required this will be automatically allocated if this
  61. * value is non-zero.
  62. * @per_child_auto_alloc_size: Each child device (of a parent in this
  63. * uclass) can hold parent data for the device/uclass. This value is only
  64. * used as a falback if this member is 0 in the driver.
  65. * @per_child_platdata_auto_alloc_size: A bus likes to store information about
  66. * its children. If non-zero this is the size of this data, to be allocated
  67. * in the child device's parent_platdata pointer. This value is only used as
  68. * a falback if this member is 0 in the driver.
  69. * @ops: Uclass operations, providing the consistent interface to devices
  70. * within the uclass.
  71. * @flags: Flags for this uclass (DM_UC_...)
  72. */
  73. struct uclass_driver {
  74. const char *name;
  75. enum uclass_id id;
  76. int (*post_bind)(struct udevice *dev);
  77. int (*pre_unbind)(struct udevice *dev);
  78. int (*pre_probe)(struct udevice *dev);
  79. int (*post_probe)(struct udevice *dev);
  80. int (*pre_remove)(struct udevice *dev);
  81. int (*child_post_bind)(struct udevice *dev);
  82. int (*child_pre_probe)(struct udevice *dev);
  83. int (*init)(struct uclass *class);
  84. int (*destroy)(struct uclass *class);
  85. int priv_auto_alloc_size;
  86. int per_device_auto_alloc_size;
  87. int per_child_auto_alloc_size;
  88. int per_child_platdata_auto_alloc_size;
  89. const void *ops;
  90. uint32_t flags;
  91. };
  92. /* Declare a new uclass_driver */
  93. #define UCLASS_DRIVER(__name) \
  94. ll_entry_declare(struct uclass_driver, __name, uclass)
  95. /**
  96. * uclass_get() - Get a uclass based on an ID, creating it if needed
  97. *
  98. * Every uclass is identified by an ID, a number from 0 to n-1 where n is
  99. * the number of uclasses. This function allows looking up a uclass by its
  100. * ID.
  101. *
  102. * @key: ID to look up
  103. * @ucp: Returns pointer to uclass (there is only one per ID)
  104. * @return 0 if OK, -ve on error
  105. */
  106. int uclass_get(enum uclass_id key, struct uclass **ucp);
  107. /**
  108. * uclass_get_device() - Get a uclass device based on an ID and index
  109. *
  110. * The device is probed to activate it ready for use.
  111. *
  112. * @id: ID to look up
  113. * @index: Device number within that uclass (0=first)
  114. * @devp: Returns pointer to device (there is only one per for each ID)
  115. * @return 0 if OK, -ve on error
  116. */
  117. int uclass_get_device(enum uclass_id id, int index, struct udevice **devp);
  118. /**
  119. * uclass_get_device_by_seq() - Get a uclass device based on an ID and sequence
  120. *
  121. * If an active device has this sequence it will be returned. If there is no
  122. * such device then this will check for a device that is requesting this
  123. * sequence.
  124. *
  125. * The device is probed to activate it ready for use.
  126. *
  127. * @id: ID to look up
  128. * @seq: Sequence number to find (0=first)
  129. * @devp: Returns pointer to device (there is only one for each seq)
  130. * @return 0 if OK, -ve on error
  131. */
  132. int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
  133. /**
  134. * uclass_get_device_by_of_offset() - Get a uclass device by device tree node
  135. *
  136. * This searches the devices in the uclass for one attached to the given
  137. * device tree node.
  138. *
  139. * The device is probed to activate it ready for use.
  140. *
  141. * @id: ID to look up
  142. * @node: Device tree offset to search for (if -ve then -ENODEV is returned)
  143. * @devp: Returns pointer to device (there is only one for each node)
  144. * @return 0 if OK, -ve on error
  145. */
  146. int uclass_get_device_by_of_offset(enum uclass_id id, int node,
  147. struct udevice **devp);
  148. /**
  149. * uclass_first_device() - Get the first device in a uclass
  150. *
  151. * The device returned is probed if necessary, and ready for use
  152. *
  153. * @id: Uclass ID to look up
  154. * @devp: Returns pointer to the first device in that uclass, or NULL if none
  155. * @return 0 if OK (found or not found), -1 on error
  156. */
  157. int uclass_first_device(enum uclass_id id, struct udevice **devp);
  158. /**
  159. * uclass_next_device() - Get the next device in a uclass
  160. *
  161. * The device returned is probed if necessary, and ready for use
  162. *
  163. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  164. * to the next device in the same uclass, or NULL if none
  165. * @return 0 if OK (found or not found), -1 on error
  166. */
  167. int uclass_next_device(struct udevice **devp);
  168. /**
  169. * uclass_resolve_seq() - Resolve a device's sequence number
  170. *
  171. * On entry dev->seq is -1, and dev->req_seq may be -1 (to allocate a
  172. * sequence number automatically, or >= 0 to select a particular number.
  173. * If the requested sequence number is in use, then this device will
  174. * be allocated another one.
  175. *
  176. * Note that the device's seq value is not changed by this function.
  177. *
  178. * @dev: Device for which to allocate sequence number
  179. * @return sequence number allocated, or -ve on error
  180. */
  181. int uclass_resolve_seq(struct udevice *dev);
  182. /**
  183. * uclass_foreach_dev() - Helper function to iteration through devices
  184. *
  185. * This creates a for() loop which works through the available devices in
  186. * a uclass in order from start to end.
  187. *
  188. * @pos: struct udevice * to hold the current device. Set to NULL when there
  189. * are no more devices.
  190. * @uc: uclass to scan
  191. */
  192. #define uclass_foreach_dev(pos, uc) \
  193. for (pos = list_entry((&(uc)->dev_head)->next, typeof(*pos), \
  194. uclass_node); \
  195. prefetch(pos->uclass_node.next), \
  196. &pos->uclass_node != (&(uc)->dev_head); \
  197. pos = list_entry(pos->uclass_node.next, typeof(*pos), \
  198. uclass_node))
  199. #endif