device.h 12 KB

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