enumeration.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. SPI serial bus support
  56. ~~~~~~~~~~~~~~~~~~~~~~
  57. Slave devices behind SPI bus have SpiSerialBus resource attached to them.
  58. This is extracted automatically by the SPI core and the slave devices are
  59. enumerated once spi_register_master() is called by the bus driver.
  60. Here is what the ACPI namespace for a SPI slave might look like:
  61. Device (EEP0)
  62. {
  63. Name (_ADR, 1)
  64. Name (_CID, Package() {
  65. "ATML0025",
  66. "AT25",
  67. })
  68. ...
  69. Method (_CRS, 0, NotSerialized)
  70. {
  71. SPISerialBus(1, PolarityLow, FourWireMode, 8,
  72. ControllerInitiated, 1000000, ClockPolarityLow,
  73. ClockPhaseFirst, "\\_SB.PCI0.SPI1",)
  74. }
  75. ...
  76. The SPI device drivers only need to add ACPI IDs in a similar way than with
  77. the platform device drivers. Below is an example where we add ACPI support
  78. to at25 SPI eeprom driver (this is meant for the above ACPI snippet):
  79. #ifdef CONFIG_ACPI
  80. static struct acpi_device_id at25_acpi_match[] = {
  81. { "AT25", 0 },
  82. { },
  83. };
  84. MODULE_DEVICE_TABLE(acpi, at25_acpi_match);
  85. #endif
  86. static struct spi_driver at25_driver = {
  87. .driver = {
  88. ...
  89. .acpi_match_table = ACPI_PTR(at25_acpi_match),
  90. },
  91. };
  92. Note that this driver actually needs more information like page size of the
  93. eeprom etc. but at the time writing this there is no standard way of
  94. passing those. One idea is to return this in _DSM method like:
  95. Device (EEP0)
  96. {
  97. ...
  98. Method (_DSM, 4, NotSerialized)
  99. {
  100. Store (Package (6)
  101. {
  102. "byte-len", 1024,
  103. "addr-mode", 2,
  104. "page-size, 32
  105. }, Local0)
  106. // Check UUIDs etc.
  107. Return (Local0)
  108. }
  109. Then the at25 SPI driver can get this configation by calling _DSM on its
  110. ACPI handle like:
  111. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  112. struct acpi_object_list input;
  113. acpi_status status;
  114. /* Fill in the input buffer */
  115. status = acpi_evaluate_object(ACPI_HANDLE(&spi->dev), "_DSM",
  116. &input, &output);
  117. if (ACPI_FAILURE(status))
  118. /* Handle the error */
  119. /* Extract the data here */
  120. kfree(output.pointer);
  121. I2C serial bus support
  122. ~~~~~~~~~~~~~~~~~~~~~~
  123. The slaves behind I2C bus controller only need to add the ACPI IDs like
  124. with the platform and SPI drivers. However the I2C bus controller driver
  125. needs to call acpi_i2c_register_devices() after it has added the adapter.
  126. An I2C bus (controller) driver does:
  127. ...
  128. ret = i2c_add_numbered_adapter(adapter);
  129. if (ret)
  130. /* handle error */
  131. of_i2c_register_devices(adapter);
  132. /* Enumerate the slave devices behind this bus via ACPI */
  133. acpi_i2c_register_devices(adapter);
  134. Below is an example of how to add ACPI support to the existing mpu3050
  135. input driver:
  136. #ifdef CONFIG_ACPI
  137. static struct acpi_device_id mpu3050_acpi_match[] = {
  138. { "MPU3050", 0 },
  139. { },
  140. };
  141. MODULE_DEVICE_TABLE(acpi, mpu3050_acpi_match);
  142. #endif
  143. static struct i2c_driver mpu3050_i2c_driver = {
  144. .driver = {
  145. .name = "mpu3050",
  146. .owner = THIS_MODULE,
  147. .pm = &mpu3050_pm,
  148. .of_match_table = mpu3050_of_match,
  149. .acpi_match_table ACPI_PTR(mpu3050_acpi_match),
  150. },
  151. .probe = mpu3050_probe,
  152. .remove = mpu3050_remove,
  153. .id_table = mpu3050_ids,
  154. };
  155. GPIO support
  156. ~~~~~~~~~~~~
  157. ACPI 5 introduced two new resources to describe GPIO connections: GpioIo
  158. and GpioInt. These resources are used be used to pass GPIO numbers used by
  159. the device to the driver. For example:
  160. Method (_CRS, 0, NotSerialized)
  161. {
  162. Name (SBUF, ResourceTemplate()
  163. {
  164. GpioIo (Exclusive, PullDefault, 0x0000, 0x0000,
  165. IoRestrictionOutputOnly, "\\_SB.PCI0.GPI0",
  166. 0x00, ResourceConsumer,,)
  167. {
  168. // Pin List
  169. 0x0055
  170. }
  171. ...
  172. Return (SBUF)
  173. }
  174. }
  175. These GPIO numbers are controller relative and path "\\_SB.PCI0.GPI0"
  176. specifies the path to the controller. In order to use these GPIOs in Linux
  177. we need to translate them to the Linux GPIO numbers.
  178. The driver can do this by including <linux/acpi_gpio.h> and then calling
  179. acpi_get_gpio(path, gpio). This will return the Linux GPIO number or
  180. negative errno if there was no translation found.
  181. Other GpioIo parameters must be converted first by the driver to be
  182. suitable to the gpiolib before passing them.
  183. In case of GpioInt resource an additional call to gpio_to_irq() must be
  184. done before calling request_irq().