device.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * (C) Copyright 2012
  5. * Pavel Herrmann <morpheus.ibis@gmail.com>
  6. * Marek Vasut <marex@denx.de>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #ifndef _DM_DEVICE_H
  11. #define _DM_DEVICE_H
  12. #include <dm/uclass-id.h>
  13. #include <linker_lists.h>
  14. #include <linux/list.h>
  15. struct driver_info;
  16. /* Driver is active (probed). Cleared when it is removed */
  17. #define DM_FLAG_ACTIVATED (1 << 0)
  18. /* DM is responsible for allocating and freeing platdata */
  19. #define DM_FLAG_ALLOC_PDATA (1 << 1)
  20. /* DM should init this device prior to relocation */
  21. #define DM_FLAG_PRE_RELOC (1 << 2)
  22. /**
  23. * struct udevice - An instance of a driver
  24. *
  25. * This holds information about a device, which is a driver bound to a
  26. * particular port or peripheral (essentially a driver instance).
  27. *
  28. * A device will come into existence through a 'bind' call, either due to
  29. * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
  30. * in the device tree (in which case of_offset is >= 0). In the latter case
  31. * we translate the device tree information into platdata in a function
  32. * implemented by the driver ofdata_to_platdata method (called just before the
  33. * probe method if the device has a device tree node.
  34. *
  35. * All three of platdata, priv and uclass_priv can be allocated by the
  36. * driver, or you can use the auto_alloc_size members of struct driver and
  37. * struct uclass_driver to have driver model do this automatically.
  38. *
  39. * @driver: The driver used by this device
  40. * @name: Name of device, typically the FDT node name
  41. * @platdata: Configuration data for this device
  42. * @of_offset: Device tree node offset for this device (- for none)
  43. * @parent: Parent of this device, or NULL for the top level device
  44. * @priv: Private data for this device
  45. * @uclass: Pointer to uclass for this device
  46. * @uclass_priv: The uclass's private data for this device
  47. * @uclass_node: Used by uclass to link its devices
  48. * @child_head: List of children of this device
  49. * @sibling_node: Next device in list of all devices
  50. * @flags: Flags for this device DM_FLAG_...
  51. * @req_seq: Requested sequence number for this device (-1 = any)
  52. * @seq: Allocated sequence number for this device (-1 = none)
  53. */
  54. struct udevice {
  55. struct driver *driver;
  56. const char *name;
  57. void *platdata;
  58. int of_offset;
  59. struct udevice *parent;
  60. void *priv;
  61. struct uclass *uclass;
  62. void *uclass_priv;
  63. struct list_head uclass_node;
  64. struct list_head child_head;
  65. struct list_head sibling_node;
  66. uint32_t flags;
  67. int req_seq;
  68. int seq;
  69. };
  70. /* Maximum sequence number supported */
  71. #define DM_MAX_SEQ 999
  72. /* Returns the operations for a device */
  73. #define device_get_ops(dev) (dev->driver->ops)
  74. /* Returns non-zero if the device is active (probed and not removed) */
  75. #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
  76. /**
  77. * struct udevice_id - Lists the compatible strings supported by a driver
  78. * @compatible: Compatible string
  79. * @data: Data for this compatible string
  80. */
  81. struct udevice_id {
  82. const char *compatible;
  83. ulong data;
  84. };
  85. /**
  86. * struct driver - A driver for a feature or peripheral
  87. *
  88. * This holds methods for setting up a new device, and also removing it.
  89. * The device needs information to set itself up - this is provided either
  90. * by platdata or a device tree node (which we find by looking up
  91. * matching compatible strings with of_match).
  92. *
  93. * Drivers all belong to a uclass, representing a class of devices of the
  94. * same type. Common elements of the drivers can be implemented in the uclass,
  95. * or the uclass can provide a consistent interface to the drivers within
  96. * it.
  97. *
  98. * @name: Device name
  99. * @id: Identiies the uclass we belong to
  100. * @of_match: List of compatible strings to match, and any identifying data
  101. * for each.
  102. * @bind: Called to bind a device to its driver
  103. * @probe: Called to probe a device, i.e. activate it
  104. * @remove: Called to remove a device, i.e. de-activate it
  105. * @unbind: Called to unbind a device from its driver
  106. * @ofdata_to_platdata: Called before probe to decode device tree data
  107. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  108. * to be allocated in the device's ->priv pointer. If zero, then the driver
  109. * is responsible for allocating any data required.
  110. * @platdata_auto_alloc_size: If non-zero this is the size of the
  111. * platform data to be allocated in the device's ->platdata pointer.
  112. * This is typically only useful for device-tree-aware drivers (those with
  113. * an of_match), since drivers which use platdata will have the data
  114. * provided in the U_BOOT_DEVICE() instantiation.
  115. * ops: Driver-specific operations. This is typically a list of function
  116. * pointers defined by the driver, to implement driver functions required by
  117. * the uclass.
  118. * @flags: driver flags - see DM_FLAGS_...
  119. */
  120. struct driver {
  121. char *name;
  122. enum uclass_id id;
  123. const struct udevice_id *of_match;
  124. int (*bind)(struct udevice *dev);
  125. int (*probe)(struct udevice *dev);
  126. int (*remove)(struct udevice *dev);
  127. int (*unbind)(struct udevice *dev);
  128. int (*ofdata_to_platdata)(struct udevice *dev);
  129. int priv_auto_alloc_size;
  130. int platdata_auto_alloc_size;
  131. const void *ops; /* driver-specific operations */
  132. uint32_t flags;
  133. };
  134. /* Declare a new U-Boot driver */
  135. #define U_BOOT_DRIVER(__name) \
  136. ll_entry_declare(struct driver, __name, driver)
  137. /**
  138. * dev_get_platdata() - Get the platform data for a device
  139. *
  140. * This checks that dev is not NULL, but no other checks for now
  141. *
  142. * @dev Device to check
  143. * @return platform data, or NULL if none
  144. */
  145. void *dev_get_platdata(struct udevice *dev);
  146. /**
  147. * dev_get_priv() - Get the private data for a device
  148. *
  149. * This checks that dev is not NULL, but no other checks for now
  150. *
  151. * @dev Device to check
  152. * @return private data, or NULL if none
  153. */
  154. void *dev_get_priv(struct udevice *dev);
  155. /**
  156. * device_find_child_by_seq() - Find a child device based on a sequence
  157. *
  158. * This searches for a device with the given seq or req_seq.
  159. *
  160. * For seq, if an active device has this sequence it will be returned.
  161. * If there is no such device then this will return -ENODEV.
  162. *
  163. * For req_seq, if a device (whether activated or not) has this req_seq
  164. * value, that device will be returned. This is a strong indication that
  165. * the device will receive that sequence when activated.
  166. *
  167. * @parent: Parent device
  168. * @seq_or_req_seq: Sequence number to find (0=first)
  169. * @find_req_seq: true to find req_seq, false to find seq
  170. * @devp: Returns pointer to device (there is only one per for each seq).
  171. * Set to NULL if none is found
  172. * @return 0 if OK, -ve on error
  173. */
  174. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  175. bool find_req_seq, struct udevice **devp);
  176. #endif