device-internal.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_INTERNAL_H
  11. #define _DM_DEVICE_INTERNAL_H
  12. #include <dm/ofnode.h>
  13. struct device_node;
  14. struct udevice;
  15. /**
  16. * device_bind() - Create a device and bind it to a driver
  17. *
  18. * Called to set up a new device attached to a driver. The device will either
  19. * have platdata, or a device tree node which can be used to create the
  20. * platdata.
  21. *
  22. * Once bound a device exists but is not yet active until device_probe() is
  23. * called.
  24. *
  25. * @parent: Pointer to device's parent, under which this driver will exist
  26. * @drv: Device's driver
  27. * @name: Name of device (e.g. device tree node name)
  28. * @platdata: Pointer to data for this device - the structure is device-
  29. * specific but may include the device's I/O address, etc.. This is NULL for
  30. * devices which use device tree.
  31. * @of_offset: Offset of device tree node for this device. This is -1 for
  32. * devices which don't use device tree.
  33. * @devp: if non-NULL, returns a pointer to the bound device
  34. * @return 0 if OK, -ve on error
  35. */
  36. int device_bind(struct udevice *parent, const struct driver *drv,
  37. const char *name, void *platdata, int of_offset,
  38. struct udevice **devp);
  39. /**
  40. * device_bind_with_driver_data() - Create a device and bind it to a driver
  41. *
  42. * Called to set up a new device attached to a driver, in the case where the
  43. * driver was matched to the device by means of a match table that provides
  44. * driver_data.
  45. *
  46. * Once bound a device exists but is not yet active until device_probe() is
  47. * called.
  48. *
  49. * @parent: Pointer to device's parent, under which this driver will exist
  50. * @drv: Device's driver
  51. * @name: Name of device (e.g. device tree node name)
  52. * @driver_data: The driver_data field from the driver's match table.
  53. * @node: Device tree node for this device. This is invalid for devices which
  54. * don't use device tree.
  55. * @devp: if non-NULL, returns a pointer to the bound device
  56. * @return 0 if OK, -ve on error
  57. */
  58. int device_bind_with_driver_data(struct udevice *parent,
  59. const struct driver *drv, const char *name,
  60. ulong driver_data, ofnode node,
  61. struct udevice **devp);
  62. /**
  63. * device_bind_by_name: Create a device and bind it to a driver
  64. *
  65. * This is a helper function used to bind devices which do not use device
  66. * tree.
  67. *
  68. * @parent: Pointer to device's parent
  69. * @pre_reloc_only: If true, bind the driver only if its DM_INIT_F flag is set.
  70. * If false bind the driver always.
  71. * @info: Name and platdata for this device
  72. * @devp: if non-NULL, returns a pointer to the bound device
  73. * @return 0 if OK, -ve on error
  74. */
  75. int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
  76. const struct driver_info *info, struct udevice **devp);
  77. /**
  78. * device_probe() - Probe a device, activating it
  79. *
  80. * Activate a device so that it is ready for use. All its parents are probed
  81. * first.
  82. *
  83. * @dev: Pointer to device to probe
  84. * @return 0 if OK, -ve on error
  85. */
  86. int device_probe(struct udevice *dev);
  87. /**
  88. * device_remove() - Remove a device, de-activating it
  89. *
  90. * De-activate a device so that it is no longer ready for use. All its
  91. * children are deactivated first.
  92. *
  93. * @dev: Pointer to device to remove
  94. * @flags: Flags for selective device removal
  95. * @return 0 if OK, -ve on error (an error here is normally a very bad thing)
  96. */
  97. #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
  98. int device_remove(struct udevice *dev, uint flags);
  99. #else
  100. static inline int device_remove(struct udevice *dev, uint flags) { return 0; }
  101. #endif
  102. /**
  103. * device_unbind() - Unbind a device, destroying it
  104. *
  105. * Unbind a device and remove all memory used by it
  106. *
  107. * @dev: Pointer to device to unbind
  108. * @return 0 if OK, -ve on error
  109. */
  110. #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
  111. int device_unbind(struct udevice *dev);
  112. #else
  113. static inline int device_unbind(struct udevice *dev) { return 0; }
  114. #endif
  115. #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
  116. void device_free(struct udevice *dev);
  117. #else
  118. static inline void device_free(struct udevice *dev) {}
  119. #endif
  120. /**
  121. * simple_bus_translate() - translate a bus address to a system address
  122. *
  123. * This handles the 'ranges' property in a simple bus. It translates the
  124. * device address @addr to a system address using this property.
  125. *
  126. * @dev: Simple bus device (parent of target device)
  127. * @addr: Address to translate
  128. * @return new address
  129. */
  130. fdt_addr_t simple_bus_translate(struct udevice *dev, fdt_addr_t addr);
  131. /* Cast away any volatile pointer */
  132. #define DM_ROOT_NON_CONST (((gd_t *)gd)->dm_root)
  133. #define DM_UCLASS_ROOT_NON_CONST (((gd_t *)gd)->uclass_root)
  134. /* device resource management */
  135. #ifdef CONFIG_DEVRES
  136. /**
  137. * devres_release_probe - Release managed resources allocated after probing
  138. * @dev: Device to release resources for
  139. *
  140. * Release all resources allocated for @dev when it was probed or later.
  141. * This function is called on driver removal.
  142. */
  143. void devres_release_probe(struct udevice *dev);
  144. /**
  145. * devres_release_all - Release all managed resources
  146. * @dev: Device to release resources for
  147. *
  148. * Release all resources associated with @dev. This function is
  149. * called on driver unbinding.
  150. */
  151. void devres_release_all(struct udevice *dev);
  152. #else /* ! CONFIG_DEVRES */
  153. static inline void devres_release_probe(struct udevice *dev)
  154. {
  155. }
  156. static inline void devres_release_all(struct udevice *dev)
  157. {
  158. }
  159. #endif /* ! CONFIG_DEVRES */
  160. #endif