device.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. * @of_id: Pointer to the udevice_id structure which created the device
  44. * @parent: Parent of this device, or NULL for the top level device
  45. * @priv: Private data for this device
  46. * @uclass: Pointer to uclass for this device
  47. * @uclass_priv: The uclass's private data for this device
  48. * @parent_priv: The parent's private data for this device
  49. * @uclass_node: Used by uclass to link its devices
  50. * @child_head: List of children of this device
  51. * @sibling_node: Next device in list of all devices
  52. * @flags: Flags for this device DM_FLAG_...
  53. * @req_seq: Requested sequence number for this device (-1 = any)
  54. * @seq: Allocated sequence number for this device (-1 = none). This is set up
  55. * when the device is probed and will be unique within the device's uclass.
  56. */
  57. struct udevice {
  58. struct driver *driver;
  59. const char *name;
  60. void *platdata;
  61. int of_offset;
  62. const struct udevice_id *of_id;
  63. struct udevice *parent;
  64. void *priv;
  65. struct uclass *uclass;
  66. void *uclass_priv;
  67. void *parent_priv;
  68. struct list_head uclass_node;
  69. struct list_head child_head;
  70. struct list_head sibling_node;
  71. uint32_t flags;
  72. int req_seq;
  73. int seq;
  74. };
  75. /* Maximum sequence number supported */
  76. #define DM_MAX_SEQ 999
  77. /* Returns the operations for a device */
  78. #define device_get_ops(dev) (dev->driver->ops)
  79. /* Returns non-zero if the device is active (probed and not removed) */
  80. #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
  81. /**
  82. * struct udevice_id - Lists the compatible strings supported by a driver
  83. * @compatible: Compatible string
  84. * @data: Data for this compatible string
  85. */
  86. struct udevice_id {
  87. const char *compatible;
  88. ulong data;
  89. };
  90. #ifdef CONFIG_OF_CONTROL
  91. #define of_match_ptr(_ptr) (_ptr)
  92. #else
  93. #define of_match_ptr(_ptr) NULL
  94. #endif /* CONFIG_OF_CONTROL */
  95. /**
  96. * struct driver - A driver for a feature or peripheral
  97. *
  98. * This holds methods for setting up a new device, and also removing it.
  99. * The device needs information to set itself up - this is provided either
  100. * by platdata or a device tree node (which we find by looking up
  101. * matching compatible strings with of_match).
  102. *
  103. * Drivers all belong to a uclass, representing a class of devices of the
  104. * same type. Common elements of the drivers can be implemented in the uclass,
  105. * or the uclass can provide a consistent interface to the drivers within
  106. * it.
  107. *
  108. * @name: Device name
  109. * @id: Identiies the uclass we belong to
  110. * @of_match: List of compatible strings to match, and any identifying data
  111. * for each.
  112. * @bind: Called to bind a device to its driver
  113. * @probe: Called to probe a device, i.e. activate it
  114. * @remove: Called to remove a device, i.e. de-activate it
  115. * @unbind: Called to unbind a device from its driver
  116. * @ofdata_to_platdata: Called before probe to decode device tree data
  117. * @child_pre_probe: Called before a child device is probed. The device has
  118. * memory allocated but it has not yet been probed.
  119. * @child_post_remove: Called after a child device is removed. The device
  120. * has memory allocated but its device_remove() method has been called.
  121. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  122. * to be allocated in the device's ->priv pointer. If zero, then the driver
  123. * is responsible for allocating any data required.
  124. * @platdata_auto_alloc_size: If non-zero this is the size of the
  125. * platform data to be allocated in the device's ->platdata pointer.
  126. * This is typically only useful for device-tree-aware drivers (those with
  127. * an of_match), since drivers which use platdata will have the data
  128. * provided in the U_BOOT_DEVICE() instantiation.
  129. * @per_child_auto_alloc_size: Each device can hold private data owned by
  130. * its parent. If required this will be automatically allocated if this
  131. * value is non-zero.
  132. * TODO(sjg@chromium.org): I'm considering dropping this, and just having
  133. * device_probe_child() pass it in. So far the use case for allocating it
  134. * is SPI, but I found that unsatisfactory. Since it is here I will leave it
  135. * until things are clearer.
  136. * @ops: Driver-specific operations. This is typically a list of function
  137. * pointers defined by the driver, to implement driver functions required by
  138. * the uclass.
  139. * @flags: driver flags - see DM_FLAGS_...
  140. */
  141. struct driver {
  142. char *name;
  143. enum uclass_id id;
  144. const struct udevice_id *of_match;
  145. int (*bind)(struct udevice *dev);
  146. int (*probe)(struct udevice *dev);
  147. int (*remove)(struct udevice *dev);
  148. int (*unbind)(struct udevice *dev);
  149. int (*ofdata_to_platdata)(struct udevice *dev);
  150. int (*child_pre_probe)(struct udevice *dev);
  151. int (*child_post_remove)(struct udevice *dev);
  152. int priv_auto_alloc_size;
  153. int platdata_auto_alloc_size;
  154. int per_child_auto_alloc_size;
  155. const void *ops; /* driver-specific operations */
  156. uint32_t flags;
  157. };
  158. /* Declare a new U-Boot driver */
  159. #define U_BOOT_DRIVER(__name) \
  160. ll_entry_declare(struct driver, __name, driver)
  161. /**
  162. * dev_get_platdata() - Get the platform data for a device
  163. *
  164. * This checks that dev is not NULL, but no other checks for now
  165. *
  166. * @dev Device to check
  167. * @return platform data, or NULL if none
  168. */
  169. void *dev_get_platdata(struct udevice *dev);
  170. /**
  171. * dev_get_parentdata() - Get the parent data for a device
  172. *
  173. * The parent data is data stored in the device but owned by the parent.
  174. * For example, a USB device may have parent data which contains information
  175. * about how to talk to the device over USB.
  176. *
  177. * This checks that dev is not NULL, but no other checks for now
  178. *
  179. * @dev Device to check
  180. * @return parent data, or NULL if none
  181. */
  182. void *dev_get_parentdata(struct udevice *dev);
  183. /**
  184. * dev_get_priv() - Get the private data for a device
  185. *
  186. * This checks that dev is not NULL, but no other checks for now
  187. *
  188. * @dev Device to check
  189. * @return private data, or NULL if none
  190. */
  191. void *dev_get_priv(struct udevice *dev);
  192. /**
  193. * struct dev_get_parent() - Get the parent of a device
  194. *
  195. * @child: Child to check
  196. * @return parent of child, or NULL if this is the root device
  197. */
  198. struct udevice *dev_get_parent(struct udevice *child);
  199. /**
  200. * dev_get_of_data() - get the device tree data used to bind a device
  201. *
  202. * When a device is bound using a device tree node, it matches a
  203. * particular compatible string as in struct udevice_id. This function
  204. * returns the associated data value for that compatible string
  205. */
  206. ulong dev_get_of_data(struct udevice *dev);
  207. /**
  208. * device_get_child() - Get the child of a device by index
  209. *
  210. * Returns the numbered child, 0 being the first. This does not use
  211. * sequence numbers, only the natural order.
  212. *
  213. * @dev: Parent device to check
  214. * @index: Child index
  215. * @devp: Returns pointer to device
  216. */
  217. int device_get_child(struct udevice *parent, int index, struct udevice **devp);
  218. /**
  219. * device_find_child_by_seq() - Find a child device based on a sequence
  220. *
  221. * This searches for a device with the given seq or req_seq.
  222. *
  223. * For seq, if an active device has this sequence it will be returned.
  224. * If there is no such device then this will return -ENODEV.
  225. *
  226. * For req_seq, if a device (whether activated or not) has this req_seq
  227. * value, that device will be returned. This is a strong indication that
  228. * the device will receive that sequence when activated.
  229. *
  230. * @parent: Parent device
  231. * @seq_or_req_seq: Sequence number to find (0=first)
  232. * @find_req_seq: true to find req_seq, false to find seq
  233. * @devp: Returns pointer to device (there is only one per for each seq).
  234. * Set to NULL if none is found
  235. * @return 0 if OK, -ve on error
  236. */
  237. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  238. bool find_req_seq, struct udevice **devp);
  239. /**
  240. * device_get_child_by_seq() - Get a child device based on a sequence
  241. *
  242. * If an active device has this sequence it will be returned. If there is no
  243. * such device then this will check for a device that is requesting this
  244. * sequence.
  245. *
  246. * The device is probed to activate it ready for use.
  247. *
  248. * @parent: Parent device
  249. * @seq: Sequence number to find (0=first)
  250. * @devp: Returns pointer to device (there is only one per for each seq)
  251. * Set to NULL if none is found
  252. * @return 0 if OK, -ve on error
  253. */
  254. int device_get_child_by_seq(struct udevice *parent, int seq,
  255. struct udevice **devp);
  256. /**
  257. * device_find_child_by_of_offset() - Find a child device based on FDT offset
  258. *
  259. * Locates a child device by its device tree offset.
  260. *
  261. * @parent: Parent device
  262. * @of_offset: Device tree offset to find
  263. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  264. * @return 0 if OK, -ve on error
  265. */
  266. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  267. struct udevice **devp);
  268. /**
  269. * device_get_child_by_of_offset() - Get a child device based on FDT offset
  270. *
  271. * Locates a child device by its device tree offset.
  272. *
  273. * The device is probed to activate it ready for use.
  274. *
  275. * @parent: Parent device
  276. * @of_offset: Device tree offset to find
  277. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  278. * @return 0 if OK, -ve on error
  279. */
  280. int device_get_child_by_of_offset(struct udevice *parent, int seq,
  281. struct udevice **devp);
  282. /**
  283. * device_find_first_child() - Find the first child of a device
  284. *
  285. * @parent: Parent device to search
  286. * @devp: Returns first child device, or NULL if none
  287. * @return 0
  288. */
  289. int device_find_first_child(struct udevice *parent, struct udevice **devp);
  290. /**
  291. * device_find_first_child() - Find the first child of a device
  292. *
  293. * @devp: Pointer to previous child device on entry. Returns pointer to next
  294. * child device, or NULL if none
  295. * @return 0
  296. */
  297. int device_find_next_child(struct udevice **devp);
  298. #endif