enumeration.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 const 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. DMA support
  51. ~~~~~~~~~~~
  52. DMA controllers enumerated via ACPI should be registered in the system to
  53. provide generic access to their resources. For example, a driver that would
  54. like to be accessible to slave devices via generic API call
  55. dma_request_slave_channel() must register itself at the end of the probe
  56. function like this:
  57. err = devm_acpi_dma_controller_register(dev, xlate_func, dw);
  58. /* Handle the error if it's not a case of !CONFIG_ACPI */
  59. and implement custom xlate function if needed (usually acpi_dma_simple_xlate()
  60. is enough) which converts the FixedDMA resource provided by struct
  61. acpi_dma_spec into the corresponding DMA channel. A piece of code for that case
  62. could look like:
  63. #ifdef CONFIG_ACPI
  64. struct filter_args {
  65. /* Provide necessary information for the filter_func */
  66. ...
  67. };
  68. static bool filter_func(struct dma_chan *chan, void *param)
  69. {
  70. /* Choose the proper channel */
  71. ...
  72. }
  73. static struct dma_chan *xlate_func(struct acpi_dma_spec *dma_spec,
  74. struct acpi_dma *adma)
  75. {
  76. dma_cap_mask_t cap;
  77. struct filter_args args;
  78. /* Prepare arguments for filter_func */
  79. ...
  80. return dma_request_channel(cap, filter_func, &args);
  81. }
  82. #else
  83. static struct dma_chan *xlate_func(struct acpi_dma_spec *dma_spec,
  84. struct acpi_dma *adma)
  85. {
  86. return NULL;
  87. }
  88. #endif
  89. dma_request_slave_channel() will call xlate_func() for each registered DMA
  90. controller. In the xlate function the proper channel must be chosen based on
  91. information in struct acpi_dma_spec and the properties of the controller
  92. provided by struct acpi_dma.
  93. Clients must call dma_request_slave_channel() with the string parameter that
  94. corresponds to a specific FixedDMA resource. By default "tx" means the first
  95. entry of the FixedDMA resource array, "rx" means the second entry. The table
  96. below shows a layout:
  97. Device (I2C0)
  98. {
  99. ...
  100. Method (_CRS, 0, NotSerialized)
  101. {
  102. Name (DBUF, ResourceTemplate ()
  103. {
  104. FixedDMA (0x0018, 0x0004, Width32bit, _Y48)
  105. FixedDMA (0x0019, 0x0005, Width32bit, )
  106. })
  107. ...
  108. }
  109. }
  110. So, the FixedDMA with request line 0x0018 is "tx" and next one is "rx" in
  111. this example.
  112. In robust cases the client unfortunately needs to call
  113. acpi_dma_request_slave_chan_by_index() directly and therefore choose the
  114. specific FixedDMA resource by its index.
  115. SPI serial bus support
  116. ~~~~~~~~~~~~~~~~~~~~~~
  117. Slave devices behind SPI bus have SpiSerialBus resource attached to them.
  118. This is extracted automatically by the SPI core and the slave devices are
  119. enumerated once spi_register_master() is called by the bus driver.
  120. Here is what the ACPI namespace for a SPI slave might look like:
  121. Device (EEP0)
  122. {
  123. Name (_ADR, 1)
  124. Name (_CID, Package() {
  125. "ATML0025",
  126. "AT25",
  127. })
  128. ...
  129. Method (_CRS, 0, NotSerialized)
  130. {
  131. SPISerialBus(1, PolarityLow, FourWireMode, 8,
  132. ControllerInitiated, 1000000, ClockPolarityLow,
  133. ClockPhaseFirst, "\\_SB.PCI0.SPI1",)
  134. }
  135. ...
  136. The SPI device drivers only need to add ACPI IDs in a similar way than with
  137. the platform device drivers. Below is an example where we add ACPI support
  138. to at25 SPI eeprom driver (this is meant for the above ACPI snippet):
  139. #ifdef CONFIG_ACPI
  140. static const struct acpi_device_id at25_acpi_match[] = {
  141. { "AT25", 0 },
  142. { },
  143. };
  144. MODULE_DEVICE_TABLE(acpi, at25_acpi_match);
  145. #endif
  146. static struct spi_driver at25_driver = {
  147. .driver = {
  148. ...
  149. .acpi_match_table = ACPI_PTR(at25_acpi_match),
  150. },
  151. };
  152. Note that this driver actually needs more information like page size of the
  153. eeprom etc. but at the time writing this there is no standard way of
  154. passing those. One idea is to return this in _DSM method like:
  155. Device (EEP0)
  156. {
  157. ...
  158. Method (_DSM, 4, NotSerialized)
  159. {
  160. Store (Package (6)
  161. {
  162. "byte-len", 1024,
  163. "addr-mode", 2,
  164. "page-size, 32
  165. }, Local0)
  166. // Check UUIDs etc.
  167. Return (Local0)
  168. }
  169. Then the at25 SPI driver can get this configuration by calling _DSM on its
  170. ACPI handle like:
  171. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  172. struct acpi_object_list input;
  173. acpi_status status;
  174. /* Fill in the input buffer */
  175. status = acpi_evaluate_object(ACPI_HANDLE(&spi->dev), "_DSM",
  176. &input, &output);
  177. if (ACPI_FAILURE(status))
  178. /* Handle the error */
  179. /* Extract the data here */
  180. kfree(output.pointer);
  181. I2C serial bus support
  182. ~~~~~~~~~~~~~~~~~~~~~~
  183. The slaves behind I2C bus controller only need to add the ACPI IDs like
  184. with the platform and SPI drivers. The I2C core automatically enumerates
  185. any slave devices behind the controller device once the adapter is
  186. registered.
  187. Below is an example of how to add ACPI support to the existing mpu3050
  188. input driver:
  189. #ifdef CONFIG_ACPI
  190. static const struct acpi_device_id mpu3050_acpi_match[] = {
  191. { "MPU3050", 0 },
  192. { },
  193. };
  194. MODULE_DEVICE_TABLE(acpi, mpu3050_acpi_match);
  195. #endif
  196. static struct i2c_driver mpu3050_i2c_driver = {
  197. .driver = {
  198. .name = "mpu3050",
  199. .owner = THIS_MODULE,
  200. .pm = &mpu3050_pm,
  201. .of_match_table = mpu3050_of_match,
  202. .acpi_match_table = ACPI_PTR(mpu3050_acpi_match),
  203. },
  204. .probe = mpu3050_probe,
  205. .remove = mpu3050_remove,
  206. .id_table = mpu3050_ids,
  207. };
  208. GPIO support
  209. ~~~~~~~~~~~~
  210. ACPI 5 introduced two new resources to describe GPIO connections: GpioIo
  211. and GpioInt. These resources can be used to pass GPIO numbers used by
  212. the device to the driver. ACPI 5.1 extended this with _DSD (Device
  213. Specific Data) which made it possible to name the GPIOs among other things.
  214. For example:
  215. Device (DEV)
  216. {
  217. Method (_CRS, 0, NotSerialized)
  218. {
  219. Name (SBUF, ResourceTemplate()
  220. {
  221. ...
  222. // Used to power on/off the device
  223. GpioIo (Exclusive, PullDefault, 0x0000, 0x0000,
  224. IoRestrictionOutputOnly, "\\_SB.PCI0.GPI0",
  225. 0x00, ResourceConsumer,,)
  226. {
  227. // Pin List
  228. 0x0055
  229. }
  230. // Interrupt for the device
  231. GpioInt (Edge, ActiveHigh, ExclusiveAndWake, PullNone,
  232. 0x0000, "\\_SB.PCI0.GPI0", 0x00, ResourceConsumer,,)
  233. {
  234. // Pin list
  235. 0x0058
  236. }
  237. ...
  238. }
  239. Return (SBUF)
  240. }
  241. // ACPI 5.1 _DSD used for naming the GPIOs
  242. Name (_DSD, Package ()
  243. {
  244. ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
  245. Package ()
  246. {
  247. Package () {"power-gpios", Package() {^DEV, 0, 0, 0 }},
  248. Package () {"irq-gpios", Package() {^DEV, 1, 0, 0 }},
  249. }
  250. })
  251. ...
  252. These GPIO numbers are controller relative and path "\\_SB.PCI0.GPI0"
  253. specifies the path to the controller. In order to use these GPIOs in Linux
  254. we need to translate them to the corresponding Linux GPIO descriptors.
  255. There is a standard GPIO API for that and is documented in
  256. Documentation/gpio/.
  257. In the above example we can get the corresponding two GPIO descriptors with
  258. a code like this:
  259. #include <linux/gpio/consumer.h>
  260. ...
  261. struct gpio_desc *irq_desc, *power_desc;
  262. irq_desc = gpiod_get(dev, "irq");
  263. if (IS_ERR(irq_desc))
  264. /* handle error */
  265. power_desc = gpiod_get(dev, "power");
  266. if (IS_ERR(power_desc))
  267. /* handle error */
  268. /* Now we can use the GPIO descriptors */
  269. There are also devm_* versions of these functions which release the
  270. descriptors once the device is released.
  271. See Documentation/acpi/gpio-properties.txt for more information about the
  272. _DSD binding related to GPIOs.
  273. MFD devices
  274. ~~~~~~~~~~~
  275. The MFD devices register their children as platform devices. For the child
  276. devices there needs to be an ACPI handle that they can use to reference
  277. parts of the ACPI namespace that relate to them. In the Linux MFD subsystem
  278. we provide two ways:
  279. o The children share the parent ACPI handle.
  280. o The MFD cell can specify the ACPI id of the device.
  281. For the first case, the MFD drivers do not need to do anything. The
  282. resulting child platform device will have its ACPI_COMPANION() set to point
  283. to the parent device.
  284. If the ACPI namespace has a device that we can match using an ACPI id or ACPI
  285. adr, the cell should be set like:
  286. static struct mfd_cell_acpi_match my_subdevice_cell_acpi_match = {
  287. .pnpid = "XYZ0001",
  288. .adr = 0,
  289. };
  290. static struct mfd_cell my_subdevice_cell = {
  291. .name = "my_subdevice",
  292. /* set the resources relative to the parent */
  293. .acpi_match = &my_subdevice_cell_acpi_match,
  294. };
  295. The ACPI id "XYZ0001" is then used to lookup an ACPI device directly under
  296. the MFD device and if found, that ACPI companion device is bound to the
  297. resulting child platform device.
  298. Device Tree namespace link device ID
  299. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  300. The Device Tree protocol uses device indentification based on the "compatible"
  301. property whose value is a string or an array of strings recognized as device
  302. identifiers by drivers and the driver core. The set of all those strings may be
  303. regarded as a device indentification namespace analogous to the ACPI/PNP device
  304. ID namespace. Consequently, in principle it should not be necessary to allocate
  305. a new (and arguably redundant) ACPI/PNP device ID for a devices with an existing
  306. identification string in the Device Tree (DT) namespace, especially if that ID
  307. is only needed to indicate that a given device is compatible with another one,
  308. presumably having a matching driver in the kernel already.
  309. In ACPI, the device identification object called _CID (Compatible ID) is used to
  310. list the IDs of devices the given one is compatible with, but those IDs must
  311. belong to one of the namespaces prescribed by the ACPI specification (see
  312. Section 6.1.2 of ACPI 6.0 for details) and the DT namespace is not one of them.
  313. Moreover, the specification mandates that either a _HID or an _ADR identificaion
  314. object be present for all ACPI objects representing devices (Section 6.1 of ACPI
  315. 6.0). For non-enumerable bus types that object must be _HID and its value must
  316. be a device ID from one of the namespaces prescribed by the specification too.
  317. The special DT namespace link device ID, PRP0001, provides a means to use the
  318. existing DT-compatible device identification in ACPI and to satisfy the above
  319. requirements following from the ACPI specification at the same time. Namely,
  320. if PRP0001 is returned by _HID, the ACPI subsystem will look for the
  321. "compatible" property in the device object's _DSD and will use the value of that
  322. property to identify the corresponding device in analogy with the original DT
  323. device identification algorithm. If the "compatible" property is not present
  324. or its value is not valid, the device will not be enumerated by the ACPI
  325. subsystem. Otherwise, it will be enumerated automatically as a platform device
  326. (except when an I2C or SPI link from the device to its parent is present, in
  327. which case the ACPI core will leave the device enumeration to the parent's
  328. driver) and the identification strings from the "compatible" property value will
  329. be used to find a driver for the device along with the device IDs listed by _CID
  330. (if present).
  331. Analogously, if PRP0001 is present in the list of device IDs returned by _CID,
  332. the identification strings listed by the "compatible" property value (if present
  333. and valid) will be used to look for a driver matching the device, but in that
  334. case their relative priority with respect to the other device IDs listed by
  335. _HID and _CID depends on the position of PRP0001 in the _CID return package.
  336. Specifically, the device IDs returned by _HID and preceding PRP0001 in the _CID
  337. return package will be checked first. Also in that case the bus type the device
  338. will be enumerated to depends on the device ID returned by _HID.
  339. It is valid to define device objects with a _HID returning PRP0001 and without
  340. the "compatible" property in the _DSD or a _CID as long as one of their
  341. ancestors provides a _DSD with a valid "compatible" property. Such device
  342. objects are then simply regarded as additional "blocks" providing hierarchical
  343. configuration information to the driver of the composite ancestor device.