enumeration.txt 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. ACPI based device enumeration
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. ACPI 5 introduced a set of new resources (UartTSerialBus, I2cSerialBus,
  4. SpiSerialBus, GpioIo and GpioInt) which can be used in enumerating slave
  5. devices behind serial bus controllers.
  6. In addition we are starting to see peripherals integrated in the
  7. SoC/Chipset to appear only in ACPI namespace. These are typically devices
  8. that are accessed through memory-mapped registers.
  9. In order to support this and re-use the existing drivers as much as
  10. possible we decided to do following:
  11. o Devices that have no bus connector resource are represented as
  12. platform devices.
  13. o Devices behind real busses where there is a connector resource
  14. are represented as struct spi_device or struct i2c_device
  15. (standard UARTs are not busses so there is no struct uart_device).
  16. As both ACPI and Device Tree represent a tree of devices (and their
  17. resources) this implementation follows the Device Tree way as much as
  18. possible.
  19. The ACPI implementation enumerates devices behind busses (platform, SPI and
  20. I2C), creates the physical devices and binds them to their ACPI handle in
  21. the ACPI namespace.
  22. This means that when ACPI_HANDLE(dev) returns non-NULL the device was
  23. enumerated from ACPI namespace. This handle can be used to extract other
  24. device-specific configuration. There is an example of this below.
  25. Platform bus support
  26. ~~~~~~~~~~~~~~~~~~~~
  27. Since we are using platform devices to represent devices that are not
  28. connected to any physical bus we only need to implement a platform driver
  29. for the device and add supported ACPI IDs. If this same IP-block is used on
  30. some other non-ACPI platform, the driver might work out of the box or needs
  31. some minor changes.
  32. Adding ACPI support for an existing driver should be pretty
  33. straightforward. Here is the simplest example:
  34. #ifdef CONFIG_ACPI
  35. static struct acpi_device_id mydrv_acpi_match[] = {
  36. /* ACPI IDs here */
  37. { }
  38. };
  39. MODULE_DEVICE_TABLE(acpi, mydrv_acpi_match);
  40. #endif
  41. static struct platform_driver my_driver = {
  42. ...
  43. .driver = {
  44. .acpi_match_table = ACPI_PTR(mydrv_acpi_match),
  45. },
  46. };
  47. If the driver needs to perform more complex initialization like getting and
  48. configuring GPIOs it can get its ACPI handle and extract this information
  49. from ACPI tables.
  50. Currently the kernel is not able to automatically determine from which ACPI
  51. device it should make the corresponding platform device so we need to add
  52. the ACPI device explicitly to acpi_platform_device_ids list defined in
  53. drivers/acpi/acpi_platform.c. This limitation is only for the platform
  54. devices, SPI and I2C devices are created automatically as described below.
  55. DMA support
  56. ~~~~~~~~~~~
  57. DMA controllers enumerated via ACPI should be registered in the system to
  58. provide generic access to their resources. For example, a driver that would
  59. like to be accessible to slave devices via generic API call
  60. dma_request_slave_channel() must register itself at the end of the probe
  61. function like this:
  62. err = devm_acpi_dma_controller_register(dev, xlate_func, dw);
  63. /* Handle the error if it's not a case of !CONFIG_ACPI */
  64. and implement custom xlate function if needed (usually acpi_dma_simple_xlate()
  65. is enough) which converts the FixedDMA resource provided by struct
  66. acpi_dma_spec into the corresponding DMA channel. A piece of code for that case
  67. could look like:
  68. #ifdef CONFIG_ACPI
  69. struct filter_args {
  70. /* Provide necessary information for the filter_func */
  71. ...
  72. };
  73. static bool filter_func(struct dma_chan *chan, void *param)
  74. {
  75. /* Choose the proper channel */
  76. ...
  77. }
  78. static struct dma_chan *xlate_func(struct acpi_dma_spec *dma_spec,
  79. struct acpi_dma *adma)
  80. {
  81. dma_cap_mask_t cap;
  82. struct filter_args args;
  83. /* Prepare arguments for filter_func */
  84. ...
  85. return dma_request_channel(cap, filter_func, &args);
  86. }
  87. #else
  88. static struct dma_chan *xlate_func(struct acpi_dma_spec *dma_spec,
  89. struct acpi_dma *adma)
  90. {
  91. return NULL;
  92. }
  93. #endif
  94. dma_request_slave_channel() will call xlate_func() for each registered DMA
  95. controller. In the xlate function the proper channel must be chosen based on
  96. information in struct acpi_dma_spec and the properties of the controller
  97. provided by struct acpi_dma.
  98. Clients must call dma_request_slave_channel() with the string parameter that
  99. corresponds to a specific FixedDMA resource. By default "tx" means the first
  100. entry of the FixedDMA resource array, "rx" means the second entry. The table
  101. below shows a layout:
  102. Device (I2C0)
  103. {
  104. ...
  105. Method (_CRS, 0, NotSerialized)
  106. {
  107. Name (DBUF, ResourceTemplate ()
  108. {
  109. FixedDMA (0x0018, 0x0004, Width32bit, _Y48)
  110. FixedDMA (0x0019, 0x0005, Width32bit, )
  111. })
  112. ...
  113. }
  114. }
  115. So, the FixedDMA with request line 0x0018 is "tx" and next one is "rx" in
  116. this example.
  117. In robust cases the client unfortunately needs to call
  118. acpi_dma_request_slave_chan_by_index() directly and therefore choose the
  119. specific FixedDMA resource by its index.
  120. SPI serial bus support
  121. ~~~~~~~~~~~~~~~~~~~~~~
  122. Slave devices behind SPI bus have SpiSerialBus resource attached to them.
  123. This is extracted automatically by the SPI core and the slave devices are
  124. enumerated once spi_register_master() is called by the bus driver.
  125. Here is what the ACPI namespace for a SPI slave might look like:
  126. Device (EEP0)
  127. {
  128. Name (_ADR, 1)
  129. Name (_CID, Package() {
  130. "ATML0025",
  131. "AT25",
  132. })
  133. ...
  134. Method (_CRS, 0, NotSerialized)
  135. {
  136. SPISerialBus(1, PolarityLow, FourWireMode, 8,
  137. ControllerInitiated, 1000000, ClockPolarityLow,
  138. ClockPhaseFirst, "\\_SB.PCI0.SPI1",)
  139. }
  140. ...
  141. The SPI device drivers only need to add ACPI IDs in a similar way than with
  142. the platform device drivers. Below is an example where we add ACPI support
  143. to at25 SPI eeprom driver (this is meant for the above ACPI snippet):
  144. #ifdef CONFIG_ACPI
  145. static struct acpi_device_id at25_acpi_match[] = {
  146. { "AT25", 0 },
  147. { },
  148. };
  149. MODULE_DEVICE_TABLE(acpi, at25_acpi_match);
  150. #endif
  151. static struct spi_driver at25_driver = {
  152. .driver = {
  153. ...
  154. .acpi_match_table = ACPI_PTR(at25_acpi_match),
  155. },
  156. };
  157. Note that this driver actually needs more information like page size of the
  158. eeprom etc. but at the time writing this there is no standard way of
  159. passing those. One idea is to return this in _DSM method like:
  160. Device (EEP0)
  161. {
  162. ...
  163. Method (_DSM, 4, NotSerialized)
  164. {
  165. Store (Package (6)
  166. {
  167. "byte-len", 1024,
  168. "addr-mode", 2,
  169. "page-size, 32
  170. }, Local0)
  171. // Check UUIDs etc.
  172. Return (Local0)
  173. }
  174. Then the at25 SPI driver can get this configuration by calling _DSM on its
  175. ACPI handle like:
  176. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  177. struct acpi_object_list input;
  178. acpi_status status;
  179. /* Fill in the input buffer */
  180. status = acpi_evaluate_object(ACPI_HANDLE(&spi->dev), "_DSM",
  181. &input, &output);
  182. if (ACPI_FAILURE(status))
  183. /* Handle the error */
  184. /* Extract the data here */
  185. kfree(output.pointer);
  186. I2C serial bus support
  187. ~~~~~~~~~~~~~~~~~~~~~~
  188. The slaves behind I2C bus controller only need to add the ACPI IDs like
  189. with the platform and SPI drivers. The I2C core automatically enumerates
  190. any slave devices behind the controller device once the adapter is
  191. registered.
  192. Below is an example of how to add ACPI support to the existing mpu3050
  193. input driver:
  194. #ifdef CONFIG_ACPI
  195. static struct acpi_device_id mpu3050_acpi_match[] = {
  196. { "MPU3050", 0 },
  197. { },
  198. };
  199. MODULE_DEVICE_TABLE(acpi, mpu3050_acpi_match);
  200. #endif
  201. static struct i2c_driver mpu3050_i2c_driver = {
  202. .driver = {
  203. .name = "mpu3050",
  204. .owner = THIS_MODULE,
  205. .pm = &mpu3050_pm,
  206. .of_match_table = mpu3050_of_match,
  207. .acpi_match_table ACPI_PTR(mpu3050_acpi_match),
  208. },
  209. .probe = mpu3050_probe,
  210. .remove = mpu3050_remove,
  211. .id_table = mpu3050_ids,
  212. };
  213. GPIO support
  214. ~~~~~~~~~~~~
  215. ACPI 5 introduced two new resources to describe GPIO connections: GpioIo
  216. and GpioInt. These resources are used be used to pass GPIO numbers used by
  217. the device to the driver. For example:
  218. Method (_CRS, 0, NotSerialized)
  219. {
  220. Name (SBUF, ResourceTemplate()
  221. {
  222. ...
  223. // Used to power on/off the device
  224. GpioIo (Exclusive, PullDefault, 0x0000, 0x0000,
  225. IoRestrictionOutputOnly, "\\_SB.PCI0.GPI0",
  226. 0x00, ResourceConsumer,,)
  227. {
  228. // Pin List
  229. 0x0055
  230. }
  231. // Interrupt for the device
  232. GpioInt (Edge, ActiveHigh, ExclusiveAndWake, PullNone,
  233. 0x0000, "\\_SB.PCI0.GPI0", 0x00, ResourceConsumer,,)
  234. {
  235. // Pin list
  236. 0x0058
  237. }
  238. ...
  239. }
  240. Return (SBUF)
  241. }
  242. These GPIO numbers are controller relative and path "\\_SB.PCI0.GPI0"
  243. specifies the path to the controller. In order to use these GPIOs in Linux
  244. we need to translate them to the Linux GPIO numbers.
  245. The driver can do this by including <linux/acpi_gpio.h> and then calling
  246. acpi_get_gpio(path, gpio). This will return the Linux GPIO number or
  247. negative errno if there was no translation found.
  248. In a simple case of just getting the Linux GPIO number from device
  249. resources one can use acpi_get_gpio_by_index() helper function. It takes
  250. pointer to the device and index of the GpioIo/GpioInt descriptor in the
  251. device resources list. For example:
  252. int gpio_irq, gpio_power;
  253. int ret;
  254. gpio_irq = acpi_get_gpio_by_index(dev, 1, NULL);
  255. if (gpio_irq < 0)
  256. /* handle error */
  257. gpio_power = acpi_get_gpio_by_index(dev, 0, NULL);
  258. if (gpio_power < 0)
  259. /* handle error */
  260. /* Now we can use the GPIO numbers */
  261. Other GpioIo parameters must be converted first by the driver to be
  262. suitable to the gpiolib before passing them.
  263. In case of GpioInt resource an additional call to gpio_to_irq() must be
  264. done before calling request_irq().