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.
7 In addition we are starting to see peripherals integrated in the
8 SoC/Chipset to appear only in ACPI namespace. These are typically devices
9 that are accessed through memory-mapped registers.
11 In order to support this and re-use the existing drivers as much as
12 possible we decided to do following:
14 o Devices that have no bus connector resource are represented as
17 o Devices behind real busses where there is a connector resource
18 are represented as struct spi_device or struct i2c_device
19 (standard UARTs are not busses so there is no struct uart_device).
21 As both ACPI and Device Tree represent a tree of devices (and their
22 resources) this implementation follows the Device Tree way as much as
25 The ACPI implementation enumerates devices behind busses (platform, SPI and
26 I2C), creates the physical devices and binds them to their ACPI handle in
29 This means that when ACPI_HANDLE(dev) returns non-NULL the device was
30 enumerated from ACPI namespace. This handle can be used to extract other
31 device-specific configuration. There is an example of this below.
35 Since we are using platform devices to represent devices that are not
36 connected to any physical bus we only need to implement a platform driver
37 for the device and add supported ACPI IDs. If this same IP-block is used on
38 some other non-ACPI platform, the driver might work out of the box or needs
41 Adding ACPI support for an existing driver should be pretty
42 straightforward. Here is the simplest example:
45 static struct acpi_device_id mydrv_acpi_match[] = {
49 MODULE_DEVICE_TABLE(acpi, mydrv_acpi_match);
52 static struct platform_driver my_driver = {
55 .acpi_match_table = ACPI_PTR(mydrv_acpi_match),
59 If the driver needs to perform more complex initialization like getting and
60 configuring GPIOs it can get its ACPI handle and extract this information
65 DMA controllers enumerated via ACPI should be registered in the system to
66 provide generic access to their resources. For example, a driver that would
67 like to be accessible to slave devices via generic API call
68 dma_request_slave_channel() must register itself at the end of the probe
71 err = devm_acpi_dma_controller_register(dev, xlate_func, dw);
72 /* Handle the error if it's not a case of !CONFIG_ACPI */
74 and implement custom xlate function if needed (usually acpi_dma_simple_xlate()
75 is enough) which converts the FixedDMA resource provided by struct
76 acpi_dma_spec into the corresponding DMA channel. A piece of code for that case
81 /* Provide necessary information for the filter_func */
85 static bool filter_func(struct dma_chan *chan, void *param)
87 /* Choose the proper channel */
91 static struct dma_chan *xlate_func(struct acpi_dma_spec *dma_spec,
92 struct acpi_dma *adma)
95 struct filter_args args;
97 /* Prepare arguments for filter_func */
99 return dma_request_channel(cap, filter_func, &args);
102 static struct dma_chan *xlate_func(struct acpi_dma_spec *dma_spec,
103 struct acpi_dma *adma)
109 dma_request_slave_channel() will call xlate_func() for each registered DMA
110 controller. In the xlate function the proper channel must be chosen based on
111 information in struct acpi_dma_spec and the properties of the controller
112 provided by struct acpi_dma.
114 Clients must call dma_request_slave_channel() with the string parameter that
115 corresponds to a specific FixedDMA resource. By default "tx" means the first
116 entry of the FixedDMA resource array, "rx" means the second entry. The table
117 below shows a layout:
122 Method (_CRS, 0, NotSerialized)
124 Name (DBUF, ResourceTemplate ()
126 FixedDMA (0x0018, 0x0004, Width32bit, _Y48)
127 FixedDMA (0x0019, 0x0005, Width32bit, )
133 So, the FixedDMA with request line 0x0018 is "tx" and next one is "rx" in
136 In robust cases the client unfortunately needs to call
137 acpi_dma_request_slave_chan_by_index() directly and therefore choose the
138 specific FixedDMA resource by its index.
140 SPI serial bus support
141 ~~~~~~~~~~~~~~~~~~~~~~
142 Slave devices behind SPI bus have SpiSerialBus resource attached to them.
143 This is extracted automatically by the SPI core and the slave devices are
144 enumerated once spi_register_master() is called by the bus driver.
146 Here is what the ACPI namespace for a SPI slave might look like:
151 Name (_CID, Package() {
156 Method (_CRS, 0, NotSerialized)
158 SPISerialBus(1, PolarityLow, FourWireMode, 8,
159 ControllerInitiated, 1000000, ClockPolarityLow,
160 ClockPhaseFirst, "\\_SB.PCI0.SPI1",)
164 The SPI device drivers only need to add ACPI IDs in a similar way than with
165 the platform device drivers. Below is an example where we add ACPI support
166 to at25 SPI eeprom driver (this is meant for the above ACPI snippet):
169 static struct acpi_device_id at25_acpi_match[] = {
173 MODULE_DEVICE_TABLE(acpi, at25_acpi_match);
176 static struct spi_driver at25_driver = {
179 .acpi_match_table = ACPI_PTR(at25_acpi_match),
183 Note that this driver actually needs more information like page size of the
184 eeprom etc. but at the time writing this there is no standard way of
185 passing those. One idea is to return this in _DSM method like:
190 Method (_DSM, 4, NotSerialized)
204 Then the at25 SPI driver can get this configuration by calling _DSM on its
207 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
208 struct acpi_object_list input;
211 /* Fill in the input buffer */
213 status = acpi_evaluate_object(ACPI_HANDLE(&spi->dev), "_DSM",
215 if (ACPI_FAILURE(status))
216 /* Handle the error */
218 /* Extract the data here */
220 kfree(output.pointer);
222 I2C serial bus support
223 ~~~~~~~~~~~~~~~~~~~~~~
224 The slaves behind I2C bus controller only need to add the ACPI IDs like
225 with the platform and SPI drivers. The I2C core automatically enumerates
226 any slave devices behind the controller device once the adapter is
229 Below is an example of how to add ACPI support to the existing mpu3050
233 static struct acpi_device_id mpu3050_acpi_match[] = {
237 MODULE_DEVICE_TABLE(acpi, mpu3050_acpi_match);
240 static struct i2c_driver mpu3050_i2c_driver = {
243 .owner = THIS_MODULE,
245 .of_match_table = mpu3050_of_match,
246 .acpi_match_table ACPI_PTR(mpu3050_acpi_match),
248 .probe = mpu3050_probe,
249 .remove = mpu3050_remove,
250 .id_table = mpu3050_ids,
255 ACPI 5 introduced two new resources to describe GPIO connections: GpioIo
256 and GpioInt. These resources are used be used to pass GPIO numbers used by
257 the device to the driver. For example:
259 Method (_CRS, 0, NotSerialized)
261 Name (SBUF, ResourceTemplate()
264 // Used to power on/off the device
265 GpioIo (Exclusive, PullDefault, 0x0000, 0x0000,
266 IoRestrictionOutputOnly, "\\_SB.PCI0.GPI0",
267 0x00, ResourceConsumer,,)
273 // Interrupt for the device
274 GpioInt (Edge, ActiveHigh, ExclusiveAndWake, PullNone,
275 0x0000, "\\_SB.PCI0.GPI0", 0x00, ResourceConsumer,,)
288 These GPIO numbers are controller relative and path "\\_SB.PCI0.GPI0"
289 specifies the path to the controller. In order to use these GPIOs in Linux
290 we need to translate them to the corresponding Linux GPIO descriptors.
292 There is a standard GPIO API for that and is documented in
295 In the above example we can get the corresponding two GPIO descriptors with
298 #include <linux/gpio/consumer.h>
301 struct gpio_desc *irq_desc, *power_desc;
303 irq_desc = gpiod_get_index(dev, NULL, 1);
304 if (IS_ERR(irq_desc))
307 power_desc = gpiod_get_index(dev, NULL, 0);
308 if (IS_ERR(power_desc))
311 /* Now we can use the GPIO descriptors */
313 There are also devm_* versions of these functions which release the
314 descriptors once the device is released.
318 The MFD devices register their children as platform devices. For the child
319 devices there needs to be an ACPI handle that they can use to reference
320 parts of the ACPI namespace that relate to them. In the Linux MFD subsystem
323 o The children share the parent ACPI handle.
324 o The MFD cell can specify the ACPI id of the device.
326 For the first case, the MFD drivers do not need to do anything. The
327 resulting child platform device will have its ACPI_COMPANION() set to point
328 to the parent device.
330 If the ACPI namespace has a device that we can match using an ACPI id,
331 the id should be set like:
333 static struct mfd_cell my_subdevice_cell = {
334 .name = "my_subdevice",
335 /* set the resources relative to the parent */
336 .acpi_pnpid = "XYZ0001",
339 The ACPI id "XYZ0001" is then used to lookup an ACPI device directly under
340 the MFD device and if found, that ACPI companion device is bound to the
341 resulting child platform device.