uclass.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <linux/list.h>
  13. /**
  14. * struct uclass - a U-Boot drive class, collecting together similar drivers
  15. *
  16. * A uclass provides an interface to a particular function, which is
  17. * implemented by one or more drivers. Every driver belongs to a uclass even
  18. * if it is the only driver in that uclass. An example uclass is GPIO, which
  19. * provides the ability to change read inputs, set and clear outputs, etc.
  20. * There may be drivers for on-chip SoC GPIO banks, I2C GPIO expanders and
  21. * PMIC IO lines, all made available in a unified way through the uclass.
  22. *
  23. * @priv: Private data for this uclass
  24. * @uc_drv: The driver for the uclass itself, not to be confused with a
  25. * 'struct driver'
  26. * @dev_head: List of devices in this uclass (devices are attached to their
  27. * uclass when their bind method is called)
  28. * @sibling_node: Next uclass in the linked list of uclasses
  29. */
  30. struct uclass {
  31. void *priv;
  32. struct uclass_driver *uc_drv;
  33. struct list_head dev_head;
  34. struct list_head sibling_node;
  35. };
  36. struct udevice;
  37. /**
  38. * struct uclass_driver - Driver for the uclass
  39. *
  40. * A uclass_driver provides a consistent interface to a set of related
  41. * drivers.
  42. *
  43. * @name: Name of uclass driver
  44. * @id: ID number of this uclass
  45. * @post_bind: Called after a new device is bound to this uclass
  46. * @pre_unbind: Called before a device is unbound from this uclass
  47. * @post_probe: Called after a new device is probed
  48. * @pre_remove: Called before a device is removed
  49. * @init: Called to set up the uclass
  50. * @destroy: Called to destroy the uclass
  51. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  52. * to be allocated in the uclass's ->priv pointer. If zero, then the uclass
  53. * driver is responsible for allocating any data required.
  54. * @per_device_auto_alloc_size: Each device can hold private data owned
  55. * by the uclass. If required this will be automatically allocated if this
  56. * value is non-zero.
  57. * @ops: Uclass operations, providing the consistent interface to devices
  58. * within the uclass.
  59. */
  60. struct uclass_driver {
  61. const char *name;
  62. enum uclass_id id;
  63. int (*post_bind)(struct udevice *dev);
  64. int (*pre_unbind)(struct udevice *dev);
  65. int (*post_probe)(struct udevice *dev);
  66. int (*pre_remove)(struct udevice *dev);
  67. int (*init)(struct uclass *class);
  68. int (*destroy)(struct uclass *class);
  69. int priv_auto_alloc_size;
  70. int per_device_auto_alloc_size;
  71. const void *ops;
  72. };
  73. /* Declare a new uclass_driver */
  74. #define UCLASS_DRIVER(__name) \
  75. ll_entry_declare(struct uclass_driver, __name, uclass)
  76. /**
  77. * uclass_get() - Get a uclass based on an ID, creating it if needed
  78. *
  79. * Every uclass is identified by an ID, a number from 0 to n-1 where n is
  80. * the number of uclasses. This function allows looking up a uclass by its
  81. * ID.
  82. *
  83. * @key: ID to look up
  84. * @ucp: Returns pointer to uclass (there is only one per ID)
  85. * @return 0 if OK, -ve on error
  86. */
  87. int uclass_get(enum uclass_id key, struct uclass **ucp);
  88. /**
  89. * uclass_get_device() - Get a uclass device based on an ID and index
  90. *
  91. * The device is probed to activate it ready for use.
  92. *
  93. * id: ID to look up
  94. * @index: Device number within that uclass (0=first)
  95. * @devp: Returns pointer to device (there is only one per for each ID)
  96. * @return 0 if OK, -ve on error
  97. */
  98. int uclass_get_device(enum uclass_id id, int index, struct udevice **devp);
  99. /**
  100. * uclass_get_device_by_seq() - Get a uclass device based on an ID and sequence
  101. *
  102. * If an active device has this sequence it will be returned. If there is no
  103. * such device then this will check for a device that is requesting this
  104. * sequence.
  105. *
  106. * The device is probed to activate it ready for use.
  107. *
  108. * @id: ID to look up
  109. * @seq: Sequence number to find (0=first)
  110. * @devp: Returns pointer to device (there is only one for each seq)
  111. * @return 0 if OK, -ve on error
  112. */
  113. int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
  114. /**
  115. * uclass_first_device() - Get the first device in a uclass
  116. *
  117. * @id: Uclass ID to look up
  118. * @devp: Returns pointer to the first device in that uclass, or NULL if none
  119. * @return 0 if OK (found or not found), -1 on error
  120. */
  121. int uclass_first_device(enum uclass_id id, struct udevice **devp);
  122. /**
  123. * uclass_next_device() - Get the next device in a uclass
  124. *
  125. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  126. * to the next device in the same uclass, or NULL if none
  127. * @return 0 if OK (found or not found), -1 on error
  128. */
  129. int uclass_next_device(struct udevice **devp);
  130. /**
  131. * uclass_resolve_seq() - Resolve a device's sequence number
  132. *
  133. * On entry dev->seq is -1, and dev->req_seq may be -1 (to allocate a
  134. * sequence number automatically, or >= 0 to select a particular number.
  135. * If the requested sequence number is in use, then this device will
  136. * be allocated another one.
  137. *
  138. * Note that the device's seq value is not changed by this function.
  139. *
  140. * @dev: Device for which to allocate sequence number
  141. * @return sequence number allocated, or -ve on error
  142. */
  143. int uclass_resolve_seq(struct udevice *dev);
  144. /**
  145. * uclass_foreach_dev() - Helper function to iteration through devices
  146. *
  147. * This creates a for() loop which works through the available devices in
  148. * a uclass in order from start to end.
  149. *
  150. * @pos: struct udevice * to hold the current device. Set to NULL when there
  151. * are no more devices.
  152. * @uc: uclass to scan
  153. */
  154. #define uclass_foreach_dev(pos, uc) \
  155. for (pos = list_entry((&(uc)->dev_head)->next, typeof(*pos), \
  156. uclass_node); \
  157. prefetch(pos->uclass_node.next), \
  158. &pos->uclass_node != (&(uc)->dev_head); \
  159. pos = list_entry(pos->uclass_node.next, typeof(*pos), \
  160. uclass_node))
  161. #endif