device.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  21. * struct udevice - An instance of a driver
  22. *
  23. * This holds information about a device, which is a driver bound to a
  24. * particular port or peripheral (essentially a driver instance).
  25. *
  26. * A device will come into existence through a 'bind' call, either due to
  27. * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
  28. * in the device tree (in which case of_offset is >= 0). In the latter case
  29. * we translate the device tree information into platdata in a function
  30. * implemented by the driver ofdata_to_platdata method (called just before the
  31. * probe method if the device has a device tree node.
  32. *
  33. * All three of platdata, priv and uclass_priv can be allocated by the
  34. * driver, or you can use the auto_alloc_size members of struct driver and
  35. * struct uclass_driver to have driver model do this automatically.
  36. *
  37. * @driver: The driver used by this device
  38. * @name: Name of device, typically the FDT node name
  39. * @platdata: Configuration data for this device
  40. * @of_offset: Device tree node offset for this device (- for none)
  41. * @parent: Parent of this device, or NULL for the top level device
  42. * @priv: Private data for this device
  43. * @uclass: Pointer to uclass for this device
  44. * @uclass_priv: The uclass's private data for this device
  45. * @uclass_node: Used by uclass to link its devices
  46. * @child_head: List of children of this device
  47. * @sibling_node: Next device in list of all devices
  48. * @flags: Flags for this device DM_FLAG_...
  49. */
  50. struct udevice {
  51. struct driver *driver;
  52. const char *name;
  53. void *platdata;
  54. int of_offset;
  55. struct udevice *parent;
  56. void *priv;
  57. struct uclass *uclass;
  58. void *uclass_priv;
  59. struct list_head uclass_node;
  60. struct list_head child_head;
  61. struct list_head sibling_node;
  62. uint32_t flags;
  63. };
  64. /* Returns the operations for a device */
  65. #define device_get_ops(dev) (dev->driver->ops)
  66. /* Returns non-zero if the device is active (probed and not removed) */
  67. #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
  68. /**
  69. * struct udevice_id - Lists the compatible strings supported by a driver
  70. * @compatible: Compatible string
  71. * @data: Data for this compatible string
  72. */
  73. struct udevice_id {
  74. const char *compatible;
  75. ulong data;
  76. };
  77. /**
  78. * struct driver - A driver for a feature or peripheral
  79. *
  80. * This holds methods for setting up a new device, and also removing it.
  81. * The device needs information to set itself up - this is provided either
  82. * by platdata or a device tree node (which we find by looking up
  83. * matching compatible strings with of_match).
  84. *
  85. * Drivers all belong to a uclass, representing a class of devices of the
  86. * same type. Common elements of the drivers can be implemented in the uclass,
  87. * or the uclass can provide a consistent interface to the drivers within
  88. * it.
  89. *
  90. * @name: Device name
  91. * @id: Identiies the uclass we belong to
  92. * @of_match: List of compatible strings to match, and any identifying data
  93. * for each.
  94. * @bind: Called to bind a device to its driver
  95. * @probe: Called to probe a device, i.e. activate it
  96. * @remove: Called to remove a device, i.e. de-activate it
  97. * @unbind: Called to unbind a device from its driver
  98. * @ofdata_to_platdata: Called before probe to decode device tree data
  99. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  100. * to be allocated in the device's ->priv pointer. If zero, then the driver
  101. * is responsible for allocating any data required.
  102. * @platdata_auto_alloc_size: If non-zero this is the size of the
  103. * platform data to be allocated in the device's ->platdata pointer.
  104. * This is typically only useful for device-tree-aware drivers (those with
  105. * an of_match), since drivers which use platdata will have the data
  106. * provided in the U_BOOT_DEVICE() instantiation.
  107. * ops: Driver-specific operations. This is typically a list of function
  108. * pointers defined by the driver, to implement driver functions required by
  109. * the uclass.
  110. */
  111. struct driver {
  112. char *name;
  113. enum uclass_id id;
  114. const struct udevice_id *of_match;
  115. int (*bind)(struct udevice *dev);
  116. int (*probe)(struct udevice *dev);
  117. int (*remove)(struct udevice *dev);
  118. int (*unbind)(struct udevice *dev);
  119. int (*ofdata_to_platdata)(struct udevice *dev);
  120. int priv_auto_alloc_size;
  121. int platdata_auto_alloc_size;
  122. const void *ops; /* driver-specific operations */
  123. };
  124. /* Declare a new U-Boot driver */
  125. #define U_BOOT_DRIVER(__name) \
  126. ll_entry_declare(struct driver, __name, driver)
  127. /**
  128. * dev_get_platdata() - Get the platform data for a device
  129. *
  130. * This checks that dev is not NULL, but no other checks for now
  131. *
  132. * @dev Device to check
  133. * @return platform data, or NULL if none
  134. */
  135. void *dev_get_platdata(struct udevice *dev);
  136. /**
  137. * dev_get_priv() - Get the private data for a device
  138. *
  139. * This checks that dev is not NULL, but no other checks for now
  140. *
  141. * @dev Device to check
  142. * @return private data, or NULL if none
  143. */
  144. void *dev_get_priv(struct udevice *dev);
  145. #endif