1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <acpi/acpi_device.h>
7 #include <acpi/acpigen.h>
8 #include <acpi/acpigen_pci.h>
9 #include <device/device.h>
15 #if CONFIG(GENERIC_GPIO_LIB)
19 #define ACPI_DP_UUID "daffd814-6eba-4d8c-8a91-bc9bbf4aa301"
20 #define ACPI_DP_CHILD_UUID "dbb8e3e6-5886-4ba6-8795-1319f52a966b"
23 * Below properties are defined at
24 * https://docs.microsoft.com/en-us/windows-hardware/drivers/pci/dsd-for-pcie-root-ports
26 #define ACPI_DSD_EXTERNAL_FACING_PORT_UUID "EFCC06CC-73AC-4BC3-BFF0-76143807C389"
27 #define ACPI_DSD_EXTERNAL_FACING_PORT_NAME "ExternalFacingPort"
29 #define ACPI_DSD_HOTPLUG_IN_D3_UUID "6211E2C0-58A3-4AF3-90E1-927A4E0C55A4"
30 #define ACPI_DSD_HOTPLUG_IN_D3_NAME "HotPlugSupportInD3"
32 /* ID for the DmaProperty _DSD */
33 #define ACPI_DSD_DMA_PROPERTY_UUID "70D24161-6DD5-4C9E-8070-705531292865"
34 #define ACPI_DSD_DMA_PROPERTY_NAME "DmaProperty"
37 * Below properties are defined at
38 * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/power-management-for-storage-hardware-devices-intro
40 #define ACPI_DSD_STORAGE_D3_UUID "5025030F-842F-4AB4-A561-99A5189762D0"
41 #define ACPI_DSD_STORAGE_D3_NAME "StorageD3Enable"
43 /* Write GPIO descriptor of DSD property */
44 int acpi_device_write_dsd_gpio(struct acpi_gpio
*gpio
, int *curr_index
)
48 if (!gpio
|| !curr_index
)
51 if (gpio
->pin_count
== 0)
54 acpi_device_write_gpio(gpio
);
55 ret
= (*curr_index
)++;
60 /* Write empty word value and return pointer to it */
61 static void *acpi_device_write_zero_len(void)
63 char *p
= acpigen_get_current();
68 /* Fill in length value from start to current at specified location */
69 static void acpi_device_fill_from_len(char *ptr
, char *start
)
71 uint16_t len
= acpigen_get_current() - start
;
73 ptr
[1] = (len
>> 8) & 0xff;
77 * Fill in the length field with the value calculated from after
78 * the 16bit field to acpigen current as this length value does
79 * not include the length field itself.
81 static void acpi_device_fill_len(void *ptr
)
83 acpi_device_fill_from_len(ptr
, ptr
+ sizeof(uint16_t));
86 /* Locate and return the ACPI name for this device */
87 const char *acpi_device_name(const struct device
*dev
)
89 const struct device
*pdev
= dev
;
90 const char *name
= NULL
;
95 /* Check for device specific handler */
96 if (dev
->ops
&& dev
->ops
->acpi_name
) {
97 name
= dev
->ops
->acpi_name(dev
);
102 /* Walk up the tree to find if any parent can identify this device */
103 while (pdev
->upstream
) {
104 pdev
= pdev
->upstream
->dev
;
107 if (is_root_device(pdev
))
109 if (pdev
->ops
&& pdev
->ops
->acpi_name
)
110 name
= pdev
->ops
->acpi_name(dev
);
118 /* Locate and return the ACPI _HID (Hardware ID) for this device */
119 const char *acpi_device_hid(const struct device
*dev
)
124 /* Check for device specific handler */
125 if (dev
->ops
->acpi_hid
)
126 return dev
->ops
->acpi_hid(dev
);
129 * Don't walk up the tree to find any parent that can identify this device, as
130 * PNP devices are hard to identify.
137 * Generate unique ID based on the ACPI path.
138 * Collisions on the same _HID are possible but very unlikely.
140 uint32_t acpi_device_uid(const struct device
*dev
)
142 const char *path
= acpi_device_path(dev
);
146 return CRC(path
, strlen(path
), crc32_byte
);
149 /* Recursive function to find the root device and print a path from there */
150 static ssize_t
acpi_device_path_fill(const struct device
*dev
, char *buf
,
151 size_t buf_len
, size_t cur
)
153 const char *name
= acpi_device_name(dev
);
160 * Make sure this name segment will fit, including the path segment
161 * separator and possible NUL terminator if this is the last segment.
163 if (!dev
|| (cur
+ strlen(name
) + 2) > buf_len
)
166 /* Walk up the tree to the root device */
167 if (!is_root_device(dev
) && dev
->upstream
&& dev
->upstream
->dev
)
168 next
= acpi_device_path_fill(dev
->upstream
->dev
, buf
, buf_len
, cur
);
172 /* Fill in the path from the root device */
173 next
+= snprintf(buf
+ next
, buf_len
- next
, "%s%s",
174 (is_root_device(dev
) || (strlen(name
) == 0)) ?
181 * Warning: just as with dev_path() this uses a static buffer
182 * so should not be called multiple times in one statement
184 const char *acpi_device_path(const struct device
*dev
)
186 static char buf
[DEVICE_PATH_MAX
] = {};
191 if (acpi_device_path_fill(dev
, buf
, sizeof(buf
), 0) <= 0)
197 /* Return the path of the parent device as the ACPI Scope for this device */
198 const char *acpi_device_scope(const struct device
*dev
)
200 static char buf
[DEVICE_PATH_MAX
] = {};
202 if (!dev
|| !dev
->upstream
|| !dev
->upstream
->dev
)
205 if (acpi_device_path_fill(dev
->upstream
->dev
, buf
, sizeof(buf
), 0) <= 0)
211 /* Concatenate the device path and provided name suffix */
212 const char *acpi_device_path_join(const struct device
*dev
, const char *name
)
214 static char buf
[DEVICE_PATH_MAX
] = {};
220 /* Build the path of this device */
221 len
= acpi_device_path_fill(dev
, buf
, sizeof(buf
), 0);
225 /* Ensure there is room for the added name, separator, and NUL */
226 if ((len
+ strlen(name
) + 2) > sizeof(buf
))
228 snprintf(buf
+ len
, sizeof(buf
) - len
, ".%s", name
);
233 int acpi_device_status(const struct device
*dev
)
236 return ACPI_STATUS_DEVICE_ALL_OFF
;
238 return ACPI_STATUS_DEVICE_HIDDEN_ON
;
239 return ACPI_STATUS_DEVICE_ALL_ON
;
242 /* Write the unique _UID based on ACPI device path. */
243 void acpi_device_write_uid(const struct device
*dev
)
245 acpigen_write_name_integer("_UID", acpi_device_uid(dev
));
248 /* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
249 void acpi_device_write_interrupt(const struct acpi_irq
*irq
)
254 if (!irq
|| !irq
->pin
)
257 /* This is supported by GpioInt() but not Interrupt() */
258 if (irq
->polarity
== ACPI_IRQ_ACTIVE_BOTH
)
261 /* Byte 0: Descriptor Type */
262 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT
);
264 /* Byte 1-2: Length (filled in later) */
265 desc_length
= acpi_device_write_zero_len();
270 * [4]: Wake (0=NO_WAKE 1=WAKE)
271 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
272 * [2]: Polarity (0=HIGH 1=LOW)
273 * [1]: Mode (0=LEVEL 1=EDGE)
274 * [0]: Resource (0=PRODUCER 1=CONSUMER)
276 flags
= 1 << 0; /* ResourceConsumer */
277 if (irq
->mode
== ACPI_IRQ_EDGE_TRIGGERED
)
279 if (irq
->polarity
== ACPI_IRQ_ACTIVE_LOW
)
281 if (irq
->shared
== ACPI_IRQ_SHARED
)
283 if (irq
->wake
== ACPI_IRQ_WAKE
)
285 acpigen_emit_byte(flags
);
287 /* Byte 4: Interrupt Table Entry Count */
288 acpigen_emit_byte(1);
290 /* Byte 5-8: Interrupt Number */
291 acpigen_emit_dword(irq
->pin
);
293 /* Fill in Descriptor Length (account for len word) */
294 acpi_device_fill_len(desc_length
);
297 /* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
298 void acpi_device_write_gpio(const struct acpi_gpio
*gpio
)
300 void *start
, *desc_length
;
301 void *pin_table_offset
, *vendor_data_offset
, *resource_offset
;
305 if (!gpio
|| gpio
->type
> ACPI_GPIO_TYPE_IO
)
308 start
= acpigen_get_current();
310 /* Byte 0: Descriptor Type */
311 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO
);
313 /* Byte 1-2: Length (fill in later) */
314 desc_length
= acpi_device_write_zero_len();
316 /* Byte 3: Revision ID */
317 acpigen_emit_byte(ACPI_GPIO_REVISION_ID
);
319 /* Byte 4: GpioIo or GpioInt */
320 acpigen_emit_byte(gpio
->type
);
323 * Byte 5-6: General Flags
324 * [15:1]: 0 => Reserved
325 * [0]: 1 => ResourceConsumer
327 acpigen_emit_word(1 << 0);
329 switch (gpio
->type
) {
330 case ACPI_GPIO_TYPE_INTERRUPT
:
332 * Byte 7-8: GPIO Interrupt Flags
333 * [15:5]: 0 => Reserved
334 * [4]: Wake (0=NO_WAKE 1=WAKE)
335 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
336 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
337 * [0]: Mode (0=LEVEL 1=EDGE)
339 if (gpio
->irq
.mode
== ACPI_IRQ_EDGE_TRIGGERED
)
341 if (gpio
->irq
.shared
== ACPI_IRQ_SHARED
)
343 if (gpio
->irq
.wake
== ACPI_IRQ_WAKE
)
346 switch (gpio
->irq
.polarity
) {
347 case ACPI_IRQ_ACTIVE_HIGH
:
350 case ACPI_IRQ_ACTIVE_LOW
:
353 case ACPI_IRQ_ACTIVE_BOTH
:
359 case ACPI_GPIO_TYPE_IO
:
361 * Byte 7-8: GPIO IO Flags
362 * [15:4]: 0 => Reserved
363 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
365 * [1:0]: IO Restriction
366 * 0 => IoRestrictionNone
367 * 1 => IoRestrictionInputOnly
368 * 2 => IoRestrictionOutputOnly
369 * 3 => IoRestrictionNoneAndPreserve
371 flags
|= gpio
->io_restrict
& 3;
376 acpigen_emit_word(flags
);
379 * Byte 9: Pin Configuration
380 * 0x01 => Default (no configuration applied)
383 * 0x04-0x7F => Reserved
384 * 0x80-0xff => Vendor defined
386 acpigen_emit_byte(gpio
->pull
);
388 /* Byte 10-11: Output Drive Strength in 1/100 mA */
389 acpigen_emit_word(gpio
->output_drive_strength
);
391 /* Byte 12-13: Debounce Timeout in 1/100 ms */
392 acpigen_emit_word(gpio
->interrupt_debounce_timeout
);
394 /* Byte 14-15: Pin Table Offset, relative to start */
395 pin_table_offset
= acpi_device_write_zero_len();
397 /* Byte 16: Reserved */
398 acpigen_emit_byte(0);
400 /* Byte 17-18: Resource Source Name Offset, relative to start */
401 resource_offset
= acpi_device_write_zero_len();
403 /* Byte 19-20: Vendor Data Offset, relative to start */
404 vendor_data_offset
= acpi_device_write_zero_len();
406 /* Byte 21-22: Vendor Data Length */
407 acpigen_emit_word(0);
409 /* Fill in Pin Table Offset */
410 acpi_device_fill_from_len(pin_table_offset
, start
);
412 /* Pin Table, one word for each pin */
413 for (pin
= 0; pin
< gpio
->pin_count
; pin
++) {
414 uint16_t acpi_pin
= gpio
->pins
[pin
];
415 #if CONFIG(GENERIC_GPIO_LIB)
416 acpi_pin
= gpio_acpi_pin(acpi_pin
);
418 acpigen_emit_word(acpi_pin
);
421 /* Fill in Resource Source Name Offset */
422 acpi_device_fill_from_len(resource_offset
, start
);
424 /* Resource Source Name String */
425 #if CONFIG(GENERIC_GPIO_LIB)
426 acpigen_emit_string(gpio
->resource
? : gpio_acpi_path(gpio
->pins
[0]));
428 acpigen_emit_string(gpio
->resource
);
431 /* Fill in Vendor Data Offset */
432 acpi_device_fill_from_len(vendor_data_offset
, start
);
434 /* Fill in GPIO Descriptor Length (account for len word) */
435 acpi_device_fill_len(desc_length
);
438 /* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
439 void acpi_device_write_i2c(const struct acpi_i2c
*i2c
)
441 void *desc_length
, *type_length
;
443 /* Byte 0: Descriptor Type */
444 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS
);
446 /* Byte 1+2: Length (filled in later) */
447 desc_length
= acpi_device_write_zero_len();
449 /* Byte 3: Revision ID */
450 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID
);
452 /* Byte 4: Resource Source Index is Reserved */
453 acpigen_emit_byte(0);
455 /* Byte 5: Serial Bus Type is I2C */
456 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C
);
460 * [7:2]: 0 => Reserved
461 * [1]: 1 => ResourceConsumer
462 * [0]: 0 => ControllerInitiated
464 acpigen_emit_byte(1 << 1);
467 * Byte 7-8: Type Specific Flags
468 * [15:1]: 0 => Reserved
469 * [0]: 0 => 7bit, 1 => 10bit
471 acpigen_emit_word(i2c
->mode_10bit
);
473 /* Byte 9: Type Specific Revision ID */
474 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID
);
476 /* Byte 10-11: I2C Type Data Length */
477 type_length
= acpi_device_write_zero_len();
479 /* Byte 12-15: I2C Bus Speed */
480 acpigen_emit_dword(i2c
->speed
);
482 /* Byte 16-17: I2C Slave Address */
483 acpigen_emit_word(i2c
->address
);
485 /* Fill in Type Data Length */
486 acpi_device_fill_len(type_length
);
488 /* Byte 18+: ResourceSource */
489 acpigen_emit_string(i2c
->resource
);
491 /* Fill in I2C Descriptor Length */
492 acpi_device_fill_len(desc_length
);
495 /* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
496 void acpi_device_write_spi(const struct acpi_spi
*spi
)
498 void *desc_length
, *type_length
;
501 /* Byte 0: Descriptor Type */
502 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS
);
504 /* Byte 1+2: Length (filled in later) */
505 desc_length
= acpi_device_write_zero_len();
507 /* Byte 3: Revision ID */
508 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID
);
510 /* Byte 4: Resource Source Index is Reserved */
511 acpigen_emit_byte(0);
513 /* Byte 5: Serial Bus Type is SPI */
514 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI
);
518 * [7:2]: 0 => Reserved
519 * [1]: 1 => ResourceConsumer
520 * [0]: 0 => ControllerInitiated
522 acpigen_emit_byte(1 << 1);
525 * Byte 7-8: Type Specific Flags
526 * [15:2]: 0 => Reserved
527 * [1]: 0 => ActiveLow, 1 => ActiveHigh
528 * [0]: 0 => FourWire, 1 => ThreeWire
530 if (spi
->wire_mode
== SPI_3_WIRE_MODE
)
532 if (spi
->device_select_polarity
== SPI_POLARITY_HIGH
)
534 acpigen_emit_word(flags
);
536 /* Byte 9: Type Specific Revision ID */
537 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID
);
539 /* Byte 10-11: SPI Type Data Length */
540 type_length
= acpi_device_write_zero_len();
542 /* Byte 12-15: Connection Speed */
543 acpigen_emit_dword(spi
->speed
);
545 /* Byte 16: Data Bit Length */
546 acpigen_emit_byte(spi
->data_bit_length
);
548 /* Byte 17: Clock Phase */
549 acpigen_emit_byte(spi
->clock_phase
);
551 /* Byte 18: Clock Polarity */
552 acpigen_emit_byte(spi
->clock_polarity
);
554 /* Byte 19-20: Device Selection */
555 acpigen_emit_word(spi
->device_select
);
557 /* Fill in Type Data Length */
558 acpi_device_fill_len(type_length
);
560 /* Byte 21+: ResourceSource String */
561 acpigen_emit_string(spi
->resource
);
563 /* Fill in SPI Descriptor Length */
564 acpi_device_fill_len(desc_length
);
567 /* UART Serial Bus - UARTSerialBusV2() */
568 void acpi_device_write_uart(const struct acpi_uart
*uart
)
570 void *desc_length
, *type_length
;
573 /* Byte 0: Descriptor Type */
574 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS
);
576 /* Byte 1+2: Length (filled in later) */
577 desc_length
= acpi_device_write_zero_len();
579 /* Byte 3: Revision ID */
580 acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID
);
582 /* Byte 4: Resource Source Index is Reserved */
583 acpigen_emit_byte(0);
585 /* Byte 5: Serial Bus Type is UART */
586 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART
);
590 * [7:2]: 0 => Reserved
591 * [1]: 1 => ResourceConsumer
592 * [0]: 0 => ControllerInitiated
594 acpigen_emit_byte(BIT(1));
597 * Byte 7-8: Type Specific Flags
598 * [15:8]: 0 => Reserved
599 * [7]: 0 => Little Endian, 1 => Big Endian
602 * [1:0]: Flow control
604 flags
= uart
->flow_control
& 3;
605 flags
|= (uart
->stop_bits
& 3) << 2;
606 flags
|= (uart
->data_bits
& 7) << 4;
607 flags
|= (uart
->endian
& 1) << 7;
608 acpigen_emit_word(flags
);
610 /* Byte 9: Type Specific Revision ID */
611 acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID
);
613 /* Byte 10-11: Type Data Length */
614 type_length
= acpi_device_write_zero_len();
616 /* Byte 12-15: Initial Baud Rate */
617 acpigen_emit_dword(uart
->initial_baud_rate
);
619 /* Byte 16-17: RX FIFO size */
620 acpigen_emit_word(uart
->rx_fifo_bytes
);
622 /* Byte 18-19: TX FIFO size */
623 acpigen_emit_word(uart
->tx_fifo_bytes
);
625 /* Byte 20: Parity */
626 acpigen_emit_byte(uart
->parity
);
628 /* Byte 21: Lines Enabled */
629 acpigen_emit_byte(uart
->lines_in_use
);
631 /* Fill in Type Data Length */
632 acpi_device_fill_len(type_length
);
634 /* Byte 22+: ResourceSource */
635 acpigen_emit_string(uart
->resource
);
637 /* Fill in Descriptor Length */
638 acpi_device_fill_len(desc_length
);
641 #define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP
642 #define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP
645 * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO
646 * state does not match the active parameter.
648 static void acpigen_write_gpio_STA(const struct acpi_gpio
*gpio
, bool active
)
650 if (!gpio
|| !gpio
->pin_count
)
653 /* Read current GPIO status into Local0. */
654 acpigen_get_tx_gpio(gpio
);
664 acpigen_emit_byte(LNOT_OP
);
665 acpigen_emit_byte(LOCAL0_OP
);
666 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_OFF_OP
);
667 acpigen_write_if_end();
670 static void acpigen_write_power_res_STA(const struct acpi_power_res_params
*params
)
672 acpigen_write_method_serialized("_STA", 0);
674 /* Verify all the GPIOs are in the ON state, otherwise return 0 */
675 acpigen_write_gpio_STA(params
->enable_gpio
, true);
676 acpigen_write_gpio_STA(params
->reset_gpio
, false);
677 acpigen_write_gpio_STA(params
->stop_gpio
, false);
679 /* All GPIOs are in the ON state */
680 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP
);
682 acpigen_pop_len(); /* Method */
685 /* PowerResource() with Enable and/or Reset control */
686 void acpi_device_add_power_res(const struct acpi_power_res_params
*params
)
689 static const char * const power_res_dev_states
[] = { "_PR0", "_PR3" };
690 unsigned int reset_gpio
= params
->reset_gpio
? params
->reset_gpio
->pins
[0] : 0;
691 unsigned int enable_gpio
= params
->enable_gpio
? params
->enable_gpio
->pins
[0] : 0;
692 unsigned int stop_gpio
= params
->stop_gpio
? params
->stop_gpio
->pins
[0] : 0;
693 char pr_name
[ACPI_NAME_BUFFER_SIZE
];
695 if (!reset_gpio
&& !enable_gpio
&& !stop_gpio
)
698 snprintf(pr_name
, sizeof(pr_name
), "PR%02X", id
++);
700 /* PowerResource (PR##, 0, 0) */
701 acpigen_write_power_res(pr_name
, 0, 0, power_res_dev_states
,
702 ARRAY_SIZE(power_res_dev_states
));
704 if (params
->use_gpio_for_status
) {
705 acpigen_write_power_res_STA(params
);
707 /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
708 acpigen_write_STA(ACPI_POWER_RESOURCE_STATUS_ON_OP
);
711 /* Method (_ON, 0, Serialized) */
712 acpigen_write_method_serialized("_ON", 0);
713 /* Call _STA and early return if the device is already enabled, since the Linux
714 kernel doesn't check the device status before calling _ON. This avoids
715 unnecessary delays while booting. */
716 if (params
->use_gpio_for_status
) {
717 /* Local0 = _STA () */
718 acpigen_write_store();
719 acpigen_emit_namestring("_STA");
720 acpigen_emit_byte(LOCAL0_OP
);
721 /* If (( Local0 == ACPI_POWER_RESOURCE_STATUS_ON_OP)) */
722 acpigen_write_if_lequal_op_op(LOCAL0_OP
, ACPI_POWER_RESOURCE_STATUS_ON_OP
);
723 acpigen_write_return_op(ZERO_OP
);
724 acpigen_write_if_end();
727 acpigen_enable_tx_gpio(params
->reset_gpio
);
729 acpigen_enable_tx_gpio(params
->enable_gpio
);
730 if (params
->enable_delay_ms
)
731 acpigen_write_sleep(params
->enable_delay_ms
);
734 acpigen_disable_tx_gpio(params
->reset_gpio
);
735 if (params
->reset_delay_ms
)
736 acpigen_write_sleep(params
->reset_delay_ms
);
739 acpigen_disable_tx_gpio(params
->stop_gpio
);
740 if (params
->stop_delay_ms
)
741 acpigen_write_sleep(params
->stop_delay_ms
);
743 acpigen_pop_len(); /* _ON method */
745 /* Method (_OFF, 0, Serialized) */
746 acpigen_write_method_serialized("_OFF", 0);
748 acpigen_enable_tx_gpio(params
->stop_gpio
);
749 if (params
->stop_off_delay_ms
)
750 acpigen_write_sleep(params
->stop_off_delay_ms
);
753 acpigen_enable_tx_gpio(params
->reset_gpio
);
754 if (params
->reset_off_delay_ms
)
755 acpigen_write_sleep(params
->reset_off_delay_ms
);
758 acpigen_disable_tx_gpio(params
->enable_gpio
);
759 if (params
->enable_off_delay_ms
)
760 acpigen_write_sleep(params
->enable_off_delay_ms
);
762 acpigen_pop_len(); /* _OFF method */
764 acpigen_pop_len(); /* PowerResource PR## */
767 static void acpi_dp_write_array(const struct acpi_dp
*array
);
768 static void acpi_dp_write_value(const struct acpi_dp
*prop
)
770 switch (prop
->type
) {
771 case ACPI_DP_TYPE_INTEGER
:
772 acpigen_write_integer(prop
->integer
);
774 case ACPI_DP_TYPE_STRING
:
775 case ACPI_DP_TYPE_CHILD
:
776 acpigen_write_string(prop
->string
);
778 case ACPI_DP_TYPE_REFERENCE
:
779 acpigen_emit_namestring(prop
->string
);
781 case ACPI_DP_TYPE_ARRAY
:
782 acpi_dp_write_array(prop
->array
);
789 /* Package (2) { "prop->name", VALUE } */
790 static void acpi_dp_write_property(const struct acpi_dp
*prop
)
792 acpigen_write_package(2);
793 acpigen_write_string(prop
->name
);
794 acpi_dp_write_value(prop
);
798 /* Write array of Device Properties */
799 static void acpi_dp_write_array(const struct acpi_dp
*array
)
801 const struct acpi_dp
*dp
;
804 /* Package element count determined as it is populated */
805 pkg_count
= acpigen_write_package(0);
808 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
809 * DP_TYPE_TABLE does not have a value to be written. Thus, start
810 * the loop from next type in the array.
812 for (dp
= array
->next
; dp
; dp
= dp
->next
) {
813 acpi_dp_write_value(dp
);
820 static void acpi_dp_free(struct acpi_dp
*dp
)
823 struct acpi_dp
*p
= dp
->next
;
826 case ACPI_DP_TYPE_CHILD
:
827 acpi_dp_free(dp
->child
);
829 case ACPI_DP_TYPE_ARRAY
:
830 acpi_dp_free(dp
->array
);
841 static bool acpi_dp_write_properties(struct acpi_dp
*prop
, const char *uuid
)
844 char *prop_count
= NULL
;
846 /* Print base properties */
847 for (dp
= prop
; dp
; dp
= dp
->next
) {
848 if (dp
->type
== ACPI_DP_TYPE_TABLE
||
849 dp
->type
== ACPI_DP_TYPE_CHILD
||
850 dp
->type
== ACPI_DP_TYPE_PACKAGE
)
854 * The UUID and package is only added when
855 * we come across the first property. This
856 * is to avoid creating a zero-length package
857 * in situations where there are only children.
860 /* ToUUID (dp->uuid) */
861 acpigen_write_uuid(uuid
);
863 * Package (PROP), element count determined as
866 prop_count
= acpigen_write_package(0);
869 acpi_dp_write_property(dp
);
872 /* Package (PROP) length, if a package was written */
879 static void acpi_dp_write_(struct acpi_dp
*table
)
881 struct acpi_dp
*dp
, *prop
;
885 if (!table
|| table
->type
!= ACPI_DP_TYPE_TABLE
|| !table
->next
)
889 acpigen_write_name(table
->name
);
891 /* Device Property list starts with the next entry */
894 /* Package (DP), default to assuming no properties or children */
895 dp_count
= acpigen_write_package(0);
897 /* Print base properties */
898 if (acpi_dp_write_properties(prop
, table
->uuid
))
901 /* Count child properties */
902 for (dp
= prop
; dp
; dp
= dp
->next
)
903 if (dp
->type
== ACPI_DP_TYPE_CHILD
)
906 /* Add child properties to the base table */
908 /* Update DP package count */
910 /* ToUUID (ACPI_DP_CHILD_UUID) */
911 acpigen_write_uuid(ACPI_DP_CHILD_UUID
);
913 /* Print child pointer properties */
914 acpigen_write_package(child_count
);
916 for (dp
= prop
; dp
; dp
= dp
->next
)
917 if (dp
->type
== ACPI_DP_TYPE_CHILD
)
918 acpi_dp_write_property(dp
);
919 /* Package (CHILD) length */
923 /* Write packages of properties with unique UUID */
924 for (dp
= prop
; dp
; dp
= dp
->next
)
925 if (dp
->type
== ACPI_DP_TYPE_PACKAGE
)
926 if (acpi_dp_write_properties(dp
->child
, dp
->uuid
))
929 /* Package (DP) length */
932 /* Recursively parse children into separate tables */
933 for (dp
= prop
; dp
; dp
= dp
->next
)
934 if (dp
->type
== ACPI_DP_TYPE_CHILD
)
935 acpi_dp_write_(dp
->child
);
938 void acpi_dp_write(struct acpi_dp
*table
)
940 acpi_dp_write_(table
);
946 static struct acpi_dp
*acpi_dp_new(struct acpi_dp
*dp
, enum acpi_dp_type type
,
951 new = malloc(sizeof(struct acpi_dp
));
955 memset(new, 0, sizeof(*new));
958 new->uuid
= ACPI_DP_UUID
;
961 /* Add to end of property list */
970 struct acpi_dp
*acpi_dp_new_table(const char *name
)
972 return acpi_dp_new(NULL
, ACPI_DP_TYPE_TABLE
, name
);
975 size_t acpi_dp_add_property_list(struct acpi_dp
*dp
,
976 const struct acpi_dp
*property_list
,
977 size_t property_count
)
979 const struct acpi_dp
*prop
;
980 size_t i
, properties_added
= 0;
982 if (!dp
|| !property_list
)
985 for (i
= 0; i
< property_count
; i
++) {
986 prop
= &property_list
[i
];
988 if (prop
->type
== ACPI_DP_TYPE_UNKNOWN
|| !prop
->name
)
991 switch (prop
->type
) {
992 case ACPI_DP_TYPE_INTEGER
:
993 acpi_dp_add_integer(dp
, prop
->name
, prop
->integer
);
995 case ACPI_DP_TYPE_STRING
:
996 acpi_dp_add_string(dp
, prop
->name
, prop
->string
);
998 case ACPI_DP_TYPE_REFERENCE
:
999 acpi_dp_add_reference(dp
, prop
->name
, prop
->string
);
1001 case ACPI_DP_TYPE_ARRAY
:
1002 acpi_dp_add_array(dp
, prop
->array
);
1004 case ACPI_DP_TYPE_CHILD
:
1005 acpi_dp_add_child(dp
, prop
->name
, prop
->child
);
1014 return properties_added
;
1017 struct acpi_dp
*acpi_dp_add_integer(struct acpi_dp
*dp
, const char *name
,
1023 struct acpi_dp
*new = acpi_dp_new(dp
, ACPI_DP_TYPE_INTEGER
, name
);
1026 new->integer
= value
;
1031 struct acpi_dp
*acpi_dp_add_string(struct acpi_dp
*dp
, const char *name
,
1037 struct acpi_dp
*new = acpi_dp_new(dp
, ACPI_DP_TYPE_STRING
, name
);
1040 new->string
= string
;
1045 struct acpi_dp
*acpi_dp_add_reference(struct acpi_dp
*dp
, const char *name
,
1046 const char *reference
)
1051 struct acpi_dp
*new = acpi_dp_new(dp
, ACPI_DP_TYPE_REFERENCE
, name
);
1054 new->string
= reference
;
1059 struct acpi_dp
*acpi_dp_add_child(struct acpi_dp
*dp
, const char *name
,
1060 struct acpi_dp
*child
)
1062 struct acpi_dp
*new;
1064 if (!dp
|| !child
|| child
->type
!= ACPI_DP_TYPE_TABLE
)
1067 new = acpi_dp_new(dp
, ACPI_DP_TYPE_CHILD
, name
);
1070 new->string
= child
->name
;
1076 struct acpi_dp
*acpi_dp_add_package(struct acpi_dp
*dp
, struct acpi_dp
*package
)
1078 struct acpi_dp
*new;
1080 if (!dp
|| !package
|| package
->type
!= ACPI_DP_TYPE_TABLE
)
1083 new = acpi_dp_new(dp
, ACPI_DP_TYPE_PACKAGE
, NULL
);
1085 new->uuid
= package
->name
;
1086 new->child
= package
;
1092 struct acpi_dp
*acpi_dp_add_array(struct acpi_dp
*dp
, struct acpi_dp
*array
)
1094 struct acpi_dp
*new;
1096 if (!dp
|| !array
|| array
->type
!= ACPI_DP_TYPE_TABLE
)
1099 new = acpi_dp_new(dp
, ACPI_DP_TYPE_ARRAY
, array
->name
);
1106 struct acpi_dp
*acpi_dp_add_integer_array(struct acpi_dp
*dp
, const char *name
,
1107 const uint64_t *array
, int len
)
1109 struct acpi_dp
*dp_array
;
1112 if (!dp
|| len
<= 0)
1115 dp_array
= acpi_dp_new_table(name
);
1119 for (i
= 0; i
< len
; i
++)
1120 if (!acpi_dp_add_integer(dp_array
, NULL
, array
[i
]))
1123 acpi_dp_add_array(dp
, dp_array
);
1128 struct acpi_dp
*acpi_dp_add_gpio_array(struct acpi_dp
*dp
, const char *name
,
1129 const struct acpi_gpio_res_params
*params
,
1132 struct acpi_dp
*gpio
;
1135 if (!dp
|| !param_count
)
1138 gpio
= acpi_dp_new_table(name
);
1143 * Generate ACPI identifiers as follows:
1145 * name, // e.g. cs-gpios
1147 * ref, index, pin, active_low, // GPIO-0 (params[0])
1148 * ref, index, pin, active_low, // GPIO-1 (params[1])
1153 for (i
= 0; i
< param_count
; i
++, params
++) {
1155 * If refs is NULL, leave a hole in the gpio array. This can be used in
1156 * conditions where some controllers use both GPIOs and native signals.
1159 acpi_dp_add_integer(gpio
, NULL
, 0);
1163 /* The device that has _CRS containing GpioIO()/GpioInt() */
1164 acpi_dp_add_reference(gpio
, NULL
, params
->ref
);
1166 /* Index of the GPIO resource in _CRS starting from zero */
1167 acpi_dp_add_integer(gpio
, NULL
, params
->index
);
1169 /* Pin in the GPIO resource, typically zero */
1170 acpi_dp_add_integer(gpio
, NULL
, params
->pin
);
1172 /* Set if pin is active low */
1173 acpi_dp_add_integer(gpio
, NULL
, params
->active_low
);
1175 acpi_dp_add_array(dp
, gpio
);
1181 struct acpi_dp
*acpi_dp_add_gpio(struct acpi_dp
*dp
, const char *name
,
1182 const char *ref
, int index
, int pin
,
1185 struct acpi_gpio_res_params param
= {
1189 .active_low
= active_low
,
1192 return acpi_dp_add_gpio_array(dp
, name
, ¶m
, 1);
1196 * This function writes a PCI device with _ADR object:
1202 * Name (_ADR, 0x0000000000000000)
1203 * Method (_STA, 0, NotSerialized) { Return (status) }
1207 void acpi_device_write_pci_dev(const struct device
*dev
)
1209 const char *scope
= acpi_device_scope(dev
);
1210 const char *name
= acpi_device_name(dev
);
1212 assert(dev
->path
.type
== DEVICE_PATH_PCI
);
1216 acpigen_write_scope(scope
);
1217 acpigen_write_device(name
);
1219 acpigen_write_ADR_pci_device(dev
);
1220 acpigen_write_STA(acpi_device_status(dev
));
1222 acpigen_pop_len(); /* Device */
1223 acpigen_pop_len(); /* Scope */
1227 * Helper function to add given integer property with an UUID to _DSD in the current scope.
1229 * dsd - Pointer to a _DSD object.
1230 * Append to existing _DSD object if not NULL.
1231 * Create new _DSD object and flush it if NULL.
1232 * uuid - Pointer to the UUID string.
1233 * name - Pointer to the property name string.
1234 * value - Value of the integer property.
1236 static void acpi_device_add_integer_property_with_uuid(struct acpi_dp
*dsd
,
1241 struct acpi_dp
*prev_dsd
= dsd
, *pkg
;
1242 if (prev_dsd
== NULL
)
1243 dsd
= acpi_dp_new_table("_DSD");
1244 pkg
= acpi_dp_new_table(uuid
);
1245 acpi_dp_add_integer(pkg
, name
, value
);
1246 acpi_dp_add_package(dsd
, pkg
);
1247 if (prev_dsd
== NULL
)
1251 /* _DSD with ExternalFacingPort */
1252 void acpi_device_add_external_facing_port(struct acpi_dp
*dsd
)
1254 acpi_device_add_integer_property_with_uuid(dsd
,
1255 ACPI_DSD_EXTERNAL_FACING_PORT_UUID
,
1256 ACPI_DSD_EXTERNAL_FACING_PORT_NAME
,
1260 /* _DSD with HotPlugSupportInD3 */
1261 void acpi_device_add_hotplug_support_in_d3(struct acpi_dp
*dsd
)
1263 acpi_device_add_integer_property_with_uuid(dsd
,
1264 ACPI_DSD_HOTPLUG_IN_D3_UUID
,
1265 ACPI_DSD_HOTPLUG_IN_D3_NAME
,
1269 /* _DSD with DmaProperty */
1270 void acpi_device_add_dma_property(struct acpi_dp
*dsd
)
1272 acpi_device_add_integer_property_with_uuid(dsd
,
1273 ACPI_DSD_DMA_PROPERTY_UUID
,
1274 ACPI_DSD_DMA_PROPERTY_NAME
,
1278 /* _DSD with StorageD3Enable */
1279 void acpi_device_add_storage_d3_enable(struct acpi_dp
*dsd
)
1281 acpi_device_add_integer_property_with_uuid(dsd
,
1282 ACPI_DSD_STORAGE_D3_UUID
,
1283 ACPI_DSD_STORAGE_D3_NAME
,