device-internal.h 5.2 KB

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