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>
10 #include <device/path.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"
22 /* Write empty word value and return pointer to it */
23 static void *acpi_device_write_zero_len(void)
25 char *p
= acpigen_get_current();
30 /* Fill in length value from start to current at specified location */
31 static void acpi_device_fill_from_len(char *ptr
, char *start
)
33 uint16_t len
= acpigen_get_current() - start
;
35 ptr
[1] = (len
>> 8) & 0xff;
39 * Fill in the length field with the value calculated from after
40 * the 16bit field to acpigen current as this length value does
41 * not include the length field itself.
43 static void acpi_device_fill_len(void *ptr
)
45 acpi_device_fill_from_len(ptr
, ptr
+ sizeof(uint16_t));
48 /* Locate and return the ACPI name for this device */
49 const char *acpi_device_name(const struct device
*dev
)
51 const struct device
*pdev
= dev
;
52 const char *name
= NULL
;
57 /* Check for device specific handler */
58 if (dev
->ops
&& dev
->ops
->acpi_name
)
59 return dev
->ops
->acpi_name(dev
);
61 /* Walk up the tree to find if any parent can identify this device */
63 pdev
= pdev
->bus
->dev
;
66 if (pdev
->path
.type
== DEVICE_PATH_ROOT
)
68 if (pdev
->ops
&& pdev
->ops
->acpi_name
)
69 name
= pdev
->ops
->acpi_name(dev
);
77 /* Locate and return the ACPI _HID (Hardware ID) for this device */
78 const char *acpi_device_hid(const struct device
*dev
)
83 /* Check for device specific handler */
84 if (dev
->ops
->acpi_hid
)
85 return dev
->ops
->acpi_hid(dev
);
88 * Don't walk up the tree to find any parent that can identify this device, as
89 * PNP devices are hard to identify.
96 * Generate unique ID based on the ACPI path.
97 * Collisions on the same _HID are possible but very unlikely.
99 uint32_t acpi_device_uid(const struct device
*dev
)
101 const char *path
= acpi_device_path(dev
);
105 return CRC(path
, strlen(path
), crc32_byte
);
108 /* Recursive function to find the root device and print a path from there */
109 static ssize_t
acpi_device_path_fill(const struct device
*dev
, char *buf
,
110 size_t buf_len
, size_t cur
)
112 const char *name
= acpi_device_name(dev
);
119 * Make sure this name segment will fit, including the path segment
120 * separator and possible NUL terminator if this is the last segment.
122 if (!dev
|| (cur
+ strlen(name
) + 2) > buf_len
)
125 /* Walk up the tree to the root device */
126 if (dev
->path
.type
!= DEVICE_PATH_ROOT
&& dev
->bus
&& dev
->bus
->dev
)
127 next
= acpi_device_path_fill(dev
->bus
->dev
, buf
, buf_len
, cur
);
131 /* Fill in the path from the root device */
132 next
+= snprintf(buf
+ next
, buf_len
- next
, "%s%s",
133 (dev
->path
.type
== DEVICE_PATH_ROOT
134 || (strlen(name
) == 0)) ?
141 * Warning: just as with dev_path() this uses a static buffer
142 * so should not be called multiple times in one statement
144 const char *acpi_device_path(const struct device
*dev
)
146 static char buf
[DEVICE_PATH_MAX
] = {};
151 if (acpi_device_path_fill(dev
, buf
, sizeof(buf
), 0) <= 0)
157 /* Return the path of the parent device as the ACPI Scope for this device */
158 const char *acpi_device_scope(const struct device
*dev
)
160 static char buf
[DEVICE_PATH_MAX
] = {};
162 if (!dev
|| !dev
->bus
|| !dev
->bus
->dev
)
165 if (acpi_device_path_fill(dev
->bus
->dev
, buf
, sizeof(buf
), 0) <= 0)
171 /* Concatenate the device path and provided name suffix */
172 const char *acpi_device_path_join(const struct device
*dev
, const char *name
)
174 static char buf
[DEVICE_PATH_MAX
] = {};
180 /* Build the path of this device */
181 len
= acpi_device_path_fill(dev
, buf
, sizeof(buf
), 0);
185 /* Ensure there is room for the added name, separator, and NUL */
186 if ((len
+ strlen(name
) + 2) > sizeof(buf
))
188 snprintf(buf
+ len
, sizeof(buf
) - len
, ".%s", name
);
193 int acpi_device_status(const struct device
*dev
)
196 return ACPI_STATUS_DEVICE_ALL_OFF
;
198 return ACPI_STATUS_DEVICE_HIDDEN_ON
;
199 return ACPI_STATUS_DEVICE_ALL_ON
;
202 /* Write the unique _UID based on ACPI device path. */
203 void acpi_device_write_uid(const struct device
*dev
)
205 acpigen_write_name_integer("_UID", acpi_device_uid(dev
));
208 /* ACPI 6.1 section 6.4.3.6: Extended Interrupt Descriptor */
209 void acpi_device_write_interrupt(const struct acpi_irq
*irq
)
214 if (!irq
|| !irq
->pin
)
217 /* This is supported by GpioInt() but not Interrupt() */
218 if (irq
->polarity
== ACPI_IRQ_ACTIVE_BOTH
)
221 /* Byte 0: Descriptor Type */
222 acpigen_emit_byte(ACPI_DESCRIPTOR_INTERRUPT
);
224 /* Byte 1-2: Length (filled in later) */
225 desc_length
= acpi_device_write_zero_len();
230 * [4]: Wake (0=NO_WAKE 1=WAKE)
231 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
232 * [2]: Polarity (0=HIGH 1=LOW)
233 * [1]: Mode (0=LEVEL 1=EDGE)
234 * [0]: Resource (0=PRODUCER 1=CONSUMER)
236 flags
= 1 << 0; /* ResourceConsumer */
237 if (irq
->mode
== ACPI_IRQ_EDGE_TRIGGERED
)
239 if (irq
->polarity
== ACPI_IRQ_ACTIVE_LOW
)
241 if (irq
->shared
== ACPI_IRQ_SHARED
)
243 if (irq
->wake
== ACPI_IRQ_WAKE
)
245 acpigen_emit_byte(flags
);
247 /* Byte 4: Interrupt Table Entry Count */
248 acpigen_emit_byte(1);
250 /* Byte 5-8: Interrupt Number */
251 acpigen_emit_dword(irq
->pin
);
253 /* Fill in Descriptor Length (account for len word) */
254 acpi_device_fill_len(desc_length
);
257 /* ACPI 6.1 section 6.4.3.8.1 - GPIO Interrupt or I/O */
258 void acpi_device_write_gpio(const struct acpi_gpio
*gpio
)
260 void *start
, *desc_length
;
261 void *pin_table_offset
, *vendor_data_offset
, *resource_offset
;
265 if (!gpio
|| gpio
->type
> ACPI_GPIO_TYPE_IO
)
268 start
= acpigen_get_current();
270 /* Byte 0: Descriptor Type */
271 acpigen_emit_byte(ACPI_DESCRIPTOR_GPIO
);
273 /* Byte 1-2: Length (fill in later) */
274 desc_length
= acpi_device_write_zero_len();
276 /* Byte 3: Revision ID */
277 acpigen_emit_byte(ACPI_GPIO_REVISION_ID
);
279 /* Byte 4: GpioIo or GpioInt */
280 acpigen_emit_byte(gpio
->type
);
283 * Byte 5-6: General Flags
284 * [15:1]: 0 => Reserved
285 * [0]: 1 => ResourceConsumer
287 acpigen_emit_word(1 << 0);
289 switch (gpio
->type
) {
290 case ACPI_GPIO_TYPE_INTERRUPT
:
292 * Byte 7-8: GPIO Interrupt Flags
293 * [15:5]: 0 => Reserved
294 * [4]: Wake (0=NO_WAKE 1=WAKE)
295 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
296 * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
297 * [0]: Mode (0=LEVEL 1=EDGE)
299 if (gpio
->irq
.mode
== ACPI_IRQ_EDGE_TRIGGERED
)
301 if (gpio
->irq
.shared
== ACPI_IRQ_SHARED
)
303 if (gpio
->irq
.wake
== ACPI_IRQ_WAKE
)
306 switch (gpio
->irq
.polarity
) {
307 case ACPI_IRQ_ACTIVE_HIGH
:
310 case ACPI_IRQ_ACTIVE_LOW
:
313 case ACPI_IRQ_ACTIVE_BOTH
:
319 case ACPI_GPIO_TYPE_IO
:
321 * Byte 7-8: GPIO IO Flags
322 * [15:4]: 0 => Reserved
323 * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
325 * [1:0]: IO Restriction
326 * 0 => IoRestrictionNone
327 * 1 => IoRestrictionInputOnly
328 * 2 => IoRestrictionOutputOnly
329 * 3 => IoRestrictionNoneAndPreserve
331 flags
|= gpio
->io_restrict
& 3;
336 acpigen_emit_word(flags
);
339 * Byte 9: Pin Configuration
340 * 0x01 => Default (no configuration applied)
343 * 0x04-0x7F => Reserved
344 * 0x80-0xff => Vendor defined
346 acpigen_emit_byte(gpio
->pull
);
348 /* Byte 10-11: Output Drive Strength in 1/100 mA */
349 acpigen_emit_word(gpio
->output_drive_strength
);
351 /* Byte 12-13: Debounce Timeout in 1/100 ms */
352 acpigen_emit_word(gpio
->interrupt_debounce_timeout
);
354 /* Byte 14-15: Pin Table Offset, relative to start */
355 pin_table_offset
= acpi_device_write_zero_len();
357 /* Byte 16: Reserved */
358 acpigen_emit_byte(0);
360 /* Byte 17-18: Resource Source Name Offset, relative to start */
361 resource_offset
= acpi_device_write_zero_len();
363 /* Byte 19-20: Vendor Data Offset, relative to start */
364 vendor_data_offset
= acpi_device_write_zero_len();
366 /* Byte 21-22: Vendor Data Length */
367 acpigen_emit_word(0);
369 /* Fill in Pin Table Offset */
370 acpi_device_fill_from_len(pin_table_offset
, start
);
372 /* Pin Table, one word for each pin */
373 for (pin
= 0; pin
< gpio
->pin_count
; pin
++) {
374 uint16_t acpi_pin
= gpio
->pins
[pin
];
375 #if CONFIG(GENERIC_GPIO_LIB)
376 acpi_pin
= gpio_acpi_pin(acpi_pin
);
378 acpigen_emit_word(acpi_pin
);
381 /* Fill in Resource Source Name Offset */
382 acpi_device_fill_from_len(resource_offset
, start
);
384 /* Resource Source Name String */
385 #if CONFIG(GENERIC_GPIO_LIB)
386 acpigen_emit_string(gpio
->resource
? : gpio_acpi_path(gpio
->pins
[0]));
388 acpigen_emit_string(gpio
->resource
);
391 /* Fill in Vendor Data Offset */
392 acpi_device_fill_from_len(vendor_data_offset
, start
);
394 /* Fill in GPIO Descriptor Length (account for len word) */
395 acpi_device_fill_len(desc_length
);
398 /* ACPI 6.1 section 6.4.3.8.2.1 - I2cSerialBus() */
399 void acpi_device_write_i2c(const struct acpi_i2c
*i2c
)
401 void *desc_length
, *type_length
;
403 /* Byte 0: Descriptor Type */
404 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS
);
406 /* Byte 1+2: Length (filled in later) */
407 desc_length
= acpi_device_write_zero_len();
409 /* Byte 3: Revision ID */
410 acpigen_emit_byte(ACPI_I2C_SERIAL_BUS_REVISION_ID
);
412 /* Byte 4: Resource Source Index is Reserved */
413 acpigen_emit_byte(0);
415 /* Byte 5: Serial Bus Type is I2C */
416 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_I2C
);
420 * [7:2]: 0 => Reserved
421 * [1]: 1 => ResourceConsumer
422 * [0]: 0 => ControllerInitiated
424 acpigen_emit_byte(1 << 1);
427 * Byte 7-8: Type Specific Flags
428 * [15:1]: 0 => Reserved
429 * [0]: 0 => 7bit, 1 => 10bit
431 acpigen_emit_word(i2c
->mode_10bit
);
433 /* Byte 9: Type Specific Revision ID */
434 acpigen_emit_byte(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID
);
436 /* Byte 10-11: I2C Type Data Length */
437 type_length
= acpi_device_write_zero_len();
439 /* Byte 12-15: I2C Bus Speed */
440 acpigen_emit_dword(i2c
->speed
);
442 /* Byte 16-17: I2C Slave Address */
443 acpigen_emit_word(i2c
->address
);
445 /* Fill in Type Data Length */
446 acpi_device_fill_len(type_length
);
448 /* Byte 18+: ResourceSource */
449 acpigen_emit_string(i2c
->resource
);
451 /* Fill in I2C Descriptor Length */
452 acpi_device_fill_len(desc_length
);
455 /* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
456 void acpi_device_write_spi(const struct acpi_spi
*spi
)
458 void *desc_length
, *type_length
;
461 /* Byte 0: Descriptor Type */
462 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS
);
464 /* Byte 1+2: Length (filled in later) */
465 desc_length
= acpi_device_write_zero_len();
467 /* Byte 3: Revision ID */
468 acpigen_emit_byte(ACPI_SPI_SERIAL_BUS_REVISION_ID
);
470 /* Byte 4: Resource Source Index is Reserved */
471 acpigen_emit_byte(0);
473 /* Byte 5: Serial Bus Type is SPI */
474 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_SPI
);
478 * [7:2]: 0 => Reserved
479 * [1]: 1 => ResourceConsumer
480 * [0]: 0 => ControllerInitiated
482 acpigen_emit_byte(1 << 1);
485 * Byte 7-8: Type Specific Flags
486 * [15:2]: 0 => Reserved
487 * [1]: 0 => ActiveLow, 1 => ActiveHigh
488 * [0]: 0 => FourWire, 1 => ThreeWire
490 if (spi
->wire_mode
== SPI_3_WIRE_MODE
)
492 if (spi
->device_select_polarity
== SPI_POLARITY_HIGH
)
494 acpigen_emit_word(flags
);
496 /* Byte 9: Type Specific Revision ID */
497 acpigen_emit_byte(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID
);
499 /* Byte 10-11: SPI Type Data Length */
500 type_length
= acpi_device_write_zero_len();
502 /* Byte 12-15: Connection Speed */
503 acpigen_emit_dword(spi
->speed
);
505 /* Byte 16: Data Bit Length */
506 acpigen_emit_byte(spi
->data_bit_length
);
508 /* Byte 17: Clock Phase */
509 acpigen_emit_byte(spi
->clock_phase
);
511 /* Byte 18: Clock Polarity */
512 acpigen_emit_byte(spi
->clock_polarity
);
514 /* Byte 19-20: Device Selection */
515 acpigen_emit_word(spi
->device_select
);
517 /* Fill in Type Data Length */
518 acpi_device_fill_len(type_length
);
520 /* Byte 21+: ResourceSource String */
521 acpigen_emit_string(spi
->resource
);
523 /* Fill in SPI Descriptor Length */
524 acpi_device_fill_len(desc_length
);
527 /* UART Serial Bus - UARTSerialBusV2() */
528 void acpi_device_write_uart(const struct acpi_uart
*uart
)
530 void *desc_length
, *type_length
;
533 /* Byte 0: Descriptor Type */
534 acpigen_emit_byte(ACPI_DESCRIPTOR_SERIAL_BUS
);
536 /* Byte 1+2: Length (filled in later) */
537 desc_length
= acpi_device_write_zero_len();
539 /* Byte 3: Revision ID */
540 acpigen_emit_byte(ACPI_UART_SERIAL_BUS_REVISION_ID
);
542 /* Byte 4: Resource Source Index is Reserved */
543 acpigen_emit_byte(0);
545 /* Byte 5: Serial Bus Type is UART */
546 acpigen_emit_byte(ACPI_SERIAL_BUS_TYPE_UART
);
550 * [7:2]: 0 => Reserved
551 * [1]: 1 => ResourceConsumer
552 * [0]: 0 => ControllerInitiated
554 acpigen_emit_byte(BIT(1));
557 * Byte 7-8: Type Specific Flags
558 * [15:8]: 0 => Reserved
559 * [7]: 0 => Little Endian, 1 => Big Endian
562 * [1:0]: Flow control
564 flags
= uart
->flow_control
& 3;
565 flags
|= (uart
->stop_bits
& 3) << 2;
566 flags
|= (uart
->data_bits
& 7) << 4;
567 flags
|= (uart
->endian
& 1) << 7;
568 acpigen_emit_word(flags
);
570 /* Byte 9: Type Specific Revision ID */
571 acpigen_emit_byte(ACPI_UART_TYPE_SPECIFIC_REVISION_ID
);
573 /* Byte 10-11: Type Data Length */
574 type_length
= acpi_device_write_zero_len();
576 /* Byte 12-15: Initial Baud Rate */
577 acpigen_emit_dword(uart
->initial_baud_rate
);
579 /* Byte 16-17: RX FIFO size */
580 acpigen_emit_word(uart
->rx_fifo_bytes
);
582 /* Byte 18-19: TX FIFO size */
583 acpigen_emit_word(uart
->tx_fifo_bytes
);
585 /* Byte 20: Parity */
586 acpigen_emit_byte(uart
->parity
);
588 /* Byte 21: Lines Enabled */
589 acpigen_emit_byte(uart
->lines_in_use
);
591 /* Fill in Type Data Length */
592 acpi_device_fill_len(type_length
);
594 /* Byte 22+: ResourceSource */
595 acpigen_emit_string(uart
->resource
);
597 /* Fill in Descriptor Length */
598 acpi_device_fill_len(desc_length
);
601 #define ACPI_POWER_RESOURCE_STATUS_ON_OP ONE_OP
602 #define ACPI_POWER_RESOURCE_STATUS_OFF_OP ZERO_OP
605 * Writes an ACPI fragment that will check the GPIO and return 0 if the GPIO
606 * state does not match the active parameter.
608 static void acpigen_write_gpio_STA(const struct acpi_gpio
*gpio
, bool active
)
610 if (!gpio
|| !gpio
->pin_count
)
613 /* Read current GPIO status into Local0. */
614 acpigen_get_tx_gpio(gpio
);
624 acpigen_emit_byte(LNOT_OP
);
625 acpigen_emit_byte(LOCAL0_OP
);
626 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_OFF_OP
);
627 acpigen_write_if_end();
630 static void acpigen_write_power_res_STA(const struct acpi_power_res_params
*params
)
632 acpigen_write_method_serialized("_STA", 0);
634 /* Verify all the GPIOs are in the ON state, otherwise return 0 */
635 acpigen_write_gpio_STA(params
->enable_gpio
, true);
636 acpigen_write_gpio_STA(params
->reset_gpio
, false);
637 acpigen_write_gpio_STA(params
->stop_gpio
, false);
639 /* All GPIOs are in the ON state */
640 acpigen_write_return_op(ACPI_POWER_RESOURCE_STATUS_ON_OP
);
642 acpigen_pop_len(); /* Method */
645 /* PowerResource() with Enable and/or Reset control */
646 void acpi_device_add_power_res(const struct acpi_power_res_params
*params
)
649 static const char * const power_res_dev_states
[] = { "_PR0", "_PR3" };
650 unsigned int reset_gpio
= params
->reset_gpio
? params
->reset_gpio
->pins
[0] : 0;
651 unsigned int enable_gpio
= params
->enable_gpio
? params
->enable_gpio
->pins
[0] : 0;
652 unsigned int stop_gpio
= params
->stop_gpio
? params
->stop_gpio
->pins
[0] : 0;
653 char pr_name
[ACPI_NAME_BUFFER_SIZE
];
655 if (!reset_gpio
&& !enable_gpio
&& !stop_gpio
)
658 snprintf(pr_name
, sizeof(pr_name
), "PR%02X", id
++);
660 /* PowerResource (PR##, 0, 0) */
661 acpigen_write_power_res(pr_name
, 0, 0, power_res_dev_states
,
662 ARRAY_SIZE(power_res_dev_states
));
664 if (params
->use_gpio_for_status
) {
665 acpigen_write_power_res_STA(params
);
667 /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
668 acpigen_write_STA(ACPI_POWER_RESOURCE_STATUS_ON_OP
);
671 /* Method (_ON, 0, Serialized) */
672 acpigen_write_method_serialized("_ON", 0);
673 /* Call _STA and early return if the device is already enabled, since the Linux
674 kernel doesn't check the device status before calling _ON. This avoids
675 unnecessary delays while booting. */
676 if (params
->use_gpio_for_status
) {
677 /* Local0 = _STA () */
678 acpigen_write_store();
679 acpigen_emit_namestring("_STA");
680 acpigen_emit_byte(LOCAL0_OP
);
681 /* If (( Local0 == ACPI_POWER_RESOURCE_STATUS_ON_OP)) */
682 acpigen_write_if_lequal_op_op(LOCAL0_OP
, ACPI_POWER_RESOURCE_STATUS_ON_OP
);
683 acpigen_write_return_op(ZERO_OP
);
684 acpigen_write_if_end();
687 acpigen_enable_tx_gpio(params
->reset_gpio
);
689 acpigen_enable_tx_gpio(params
->enable_gpio
);
690 if (params
->enable_delay_ms
)
691 acpigen_write_sleep(params
->enable_delay_ms
);
694 acpigen_disable_tx_gpio(params
->reset_gpio
);
695 if (params
->reset_delay_ms
)
696 acpigen_write_sleep(params
->reset_delay_ms
);
699 acpigen_disable_tx_gpio(params
->stop_gpio
);
700 if (params
->stop_delay_ms
)
701 acpigen_write_sleep(params
->stop_delay_ms
);
703 acpigen_pop_len(); /* _ON method */
705 /* Method (_OFF, 0, Serialized) */
706 acpigen_write_method_serialized("_OFF", 0);
708 acpigen_enable_tx_gpio(params
->stop_gpio
);
709 if (params
->stop_off_delay_ms
)
710 acpigen_write_sleep(params
->stop_off_delay_ms
);
713 acpigen_enable_tx_gpio(params
->reset_gpio
);
714 if (params
->reset_off_delay_ms
)
715 acpigen_write_sleep(params
->reset_off_delay_ms
);
718 acpigen_disable_tx_gpio(params
->enable_gpio
);
719 if (params
->enable_off_delay_ms
)
720 acpigen_write_sleep(params
->enable_off_delay_ms
);
722 acpigen_pop_len(); /* _OFF method */
724 acpigen_pop_len(); /* PowerResource PR## */
727 static void acpi_dp_write_array(const struct acpi_dp
*array
);
728 static void acpi_dp_write_value(const struct acpi_dp
*prop
)
730 switch (prop
->type
) {
731 case ACPI_DP_TYPE_INTEGER
:
732 acpigen_write_integer(prop
->integer
);
734 case ACPI_DP_TYPE_STRING
:
735 case ACPI_DP_TYPE_CHILD
:
736 acpigen_write_string(prop
->string
);
738 case ACPI_DP_TYPE_REFERENCE
:
739 acpigen_emit_namestring(prop
->string
);
741 case ACPI_DP_TYPE_ARRAY
:
742 acpi_dp_write_array(prop
->array
);
749 /* Package (2) { "prop->name", VALUE } */
750 static void acpi_dp_write_property(const struct acpi_dp
*prop
)
752 acpigen_write_package(2);
753 acpigen_write_string(prop
->name
);
754 acpi_dp_write_value(prop
);
758 /* Write array of Device Properties */
759 static void acpi_dp_write_array(const struct acpi_dp
*array
)
761 const struct acpi_dp
*dp
;
764 /* Package element count determined as it is populated */
765 pkg_count
= acpigen_write_package(0);
768 * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
769 * DP_TYPE_TABLE does not have a value to be written. Thus, start
770 * the loop from next type in the array.
772 for (dp
= array
->next
; dp
; dp
= dp
->next
) {
773 acpi_dp_write_value(dp
);
780 static void acpi_dp_free(struct acpi_dp
*dp
)
783 struct acpi_dp
*p
= dp
->next
;
786 case ACPI_DP_TYPE_CHILD
:
787 acpi_dp_free(dp
->child
);
789 case ACPI_DP_TYPE_ARRAY
:
790 acpi_dp_free(dp
->array
);
801 static bool acpi_dp_write_properties(struct acpi_dp
*prop
, const char *uuid
)
804 char *prop_count
= NULL
;
806 /* Print base properties */
807 for (dp
= prop
; dp
; dp
= dp
->next
) {
808 if (dp
->type
== ACPI_DP_TYPE_TABLE
||
809 dp
->type
== ACPI_DP_TYPE_CHILD
||
810 dp
->type
== ACPI_DP_TYPE_PACKAGE
)
814 * The UUID and package is only added when
815 * we come across the first property. This
816 * is to avoid creating a zero-length package
817 * in situations where there are only children.
820 /* ToUUID (dp->uuid) */
821 acpigen_write_uuid(uuid
);
823 * Package (PROP), element count determined as
826 prop_count
= acpigen_write_package(0);
829 acpi_dp_write_property(dp
);
832 /* Package (PROP) length, if a package was written */
839 static void acpi_dp_write_(struct acpi_dp
*table
)
841 struct acpi_dp
*dp
, *prop
;
845 if (!table
|| table
->type
!= ACPI_DP_TYPE_TABLE
|| !table
->next
)
849 acpigen_write_name(table
->name
);
851 /* Device Property list starts with the next entry */
854 /* Package (DP), default to assuming no properties or children */
855 dp_count
= acpigen_write_package(0);
857 /* Print base properties */
858 if (acpi_dp_write_properties(prop
, table
->uuid
))
861 /* Count child properties */
862 for (dp
= prop
; dp
; dp
= dp
->next
)
863 if (dp
->type
== ACPI_DP_TYPE_CHILD
)
866 /* Add child properties to the base table */
868 /* Update DP package count */
870 /* ToUUID (ACPI_DP_CHILD_UUID) */
871 acpigen_write_uuid(ACPI_DP_CHILD_UUID
);
873 /* Print child pointer properties */
874 acpigen_write_package(child_count
);
876 for (dp
= prop
; dp
; dp
= dp
->next
)
877 if (dp
->type
== ACPI_DP_TYPE_CHILD
)
878 acpi_dp_write_property(dp
);
879 /* Package (CHILD) length */
883 /* Write packages of properties with unique UUID */
884 for (dp
= prop
; dp
; dp
= dp
->next
)
885 if (dp
->type
== ACPI_DP_TYPE_PACKAGE
)
886 if (acpi_dp_write_properties(dp
->child
, dp
->uuid
))
889 /* Package (DP) length */
892 /* Recursively parse children into separate tables */
893 for (dp
= prop
; dp
; dp
= dp
->next
)
894 if (dp
->type
== ACPI_DP_TYPE_CHILD
)
895 acpi_dp_write_(dp
->child
);
898 void acpi_dp_write(struct acpi_dp
*table
)
900 acpi_dp_write_(table
);
906 static struct acpi_dp
*acpi_dp_new(struct acpi_dp
*dp
, enum acpi_dp_type type
,
911 new = malloc(sizeof(struct acpi_dp
));
915 memset(new, 0, sizeof(*new));
918 new->uuid
= ACPI_DP_UUID
;
921 /* Add to end of property list */
930 struct acpi_dp
*acpi_dp_new_table(const char *name
)
932 return acpi_dp_new(NULL
, ACPI_DP_TYPE_TABLE
, name
);
935 size_t acpi_dp_add_property_list(struct acpi_dp
*dp
,
936 const struct acpi_dp
*property_list
,
937 size_t property_count
)
939 const struct acpi_dp
*prop
;
940 size_t i
, properties_added
= 0;
942 if (!dp
|| !property_list
)
945 for (i
= 0; i
< property_count
; i
++) {
946 prop
= &property_list
[i
];
948 if (prop
->type
== ACPI_DP_TYPE_UNKNOWN
|| !prop
->name
)
951 switch (prop
->type
) {
952 case ACPI_DP_TYPE_INTEGER
:
953 acpi_dp_add_integer(dp
, prop
->name
, prop
->integer
);
955 case ACPI_DP_TYPE_STRING
:
956 acpi_dp_add_string(dp
, prop
->name
, prop
->string
);
958 case ACPI_DP_TYPE_REFERENCE
:
959 acpi_dp_add_reference(dp
, prop
->name
, prop
->string
);
961 case ACPI_DP_TYPE_ARRAY
:
962 acpi_dp_add_array(dp
, prop
->array
);
964 case ACPI_DP_TYPE_CHILD
:
965 acpi_dp_add_child(dp
, prop
->name
, prop
->child
);
974 return properties_added
;
977 struct acpi_dp
*acpi_dp_add_integer(struct acpi_dp
*dp
, const char *name
,
983 struct acpi_dp
*new = acpi_dp_new(dp
, ACPI_DP_TYPE_INTEGER
, name
);
986 new->integer
= value
;
991 struct acpi_dp
*acpi_dp_add_string(struct acpi_dp
*dp
, const char *name
,
997 struct acpi_dp
*new = acpi_dp_new(dp
, ACPI_DP_TYPE_STRING
, name
);
1000 new->string
= string
;
1005 struct acpi_dp
*acpi_dp_add_reference(struct acpi_dp
*dp
, const char *name
,
1006 const char *reference
)
1011 struct acpi_dp
*new = acpi_dp_new(dp
, ACPI_DP_TYPE_REFERENCE
, name
);
1014 new->string
= reference
;
1019 struct acpi_dp
*acpi_dp_add_child(struct acpi_dp
*dp
, const char *name
,
1020 struct acpi_dp
*child
)
1022 struct acpi_dp
*new;
1024 if (!dp
|| !child
|| child
->type
!= ACPI_DP_TYPE_TABLE
)
1027 new = acpi_dp_new(dp
, ACPI_DP_TYPE_CHILD
, name
);
1030 new->string
= child
->name
;
1036 struct acpi_dp
*acpi_dp_add_package(struct acpi_dp
*dp
, struct acpi_dp
*package
)
1038 struct acpi_dp
*new;
1040 if (!dp
|| !package
|| package
->type
!= ACPI_DP_TYPE_TABLE
)
1043 new = acpi_dp_new(dp
, ACPI_DP_TYPE_PACKAGE
, NULL
);
1045 new->uuid
= package
->name
;
1046 new->child
= package
;
1052 struct acpi_dp
*acpi_dp_add_array(struct acpi_dp
*dp
, struct acpi_dp
*array
)
1054 struct acpi_dp
*new;
1056 if (!dp
|| !array
|| array
->type
!= ACPI_DP_TYPE_TABLE
)
1059 new = acpi_dp_new(dp
, ACPI_DP_TYPE_ARRAY
, array
->name
);
1066 struct acpi_dp
*acpi_dp_add_integer_array(struct acpi_dp
*dp
, const char *name
,
1067 const uint64_t *array
, int len
)
1069 struct acpi_dp
*dp_array
;
1072 if (!dp
|| len
<= 0)
1075 dp_array
= acpi_dp_new_table(name
);
1079 for (i
= 0; i
< len
; i
++)
1080 if (!acpi_dp_add_integer(dp_array
, NULL
, array
[i
]))
1083 acpi_dp_add_array(dp
, dp_array
);
1088 struct acpi_dp
*acpi_dp_add_gpio_array(struct acpi_dp
*dp
, const char *name
,
1089 const struct acpi_gpio_res_params
*params
,
1092 struct acpi_dp
*gpio
;
1095 if (!dp
|| !param_count
)
1098 gpio
= acpi_dp_new_table(name
);
1103 * Generate ACPI identifiers as follows:
1105 * name, // e.g. cs-gpios
1107 * ref, index, pin, active_low, // GPIO-0 (params[0])
1108 * ref, index, pin, active_low, // GPIO-1 (params[1])
1113 for (i
= 0; i
< param_count
; i
++, params
++) {
1115 * If refs is NULL, leave a hole in the gpio array. This can be used in
1116 * conditions where some controllers use both GPIOs and native signals.
1119 acpi_dp_add_integer(gpio
, NULL
, 0);
1123 /* The device that has _CRS containing GpioIO()/GpioInt() */
1124 acpi_dp_add_reference(gpio
, NULL
, params
->ref
);
1126 /* Index of the GPIO resource in _CRS starting from zero */
1127 acpi_dp_add_integer(gpio
, NULL
, params
->index
);
1129 /* Pin in the GPIO resource, typically zero */
1130 acpi_dp_add_integer(gpio
, NULL
, params
->pin
);
1132 /* Set if pin is active low */
1133 acpi_dp_add_integer(gpio
, NULL
, params
->active_low
);
1135 acpi_dp_add_array(dp
, gpio
);
1142 struct acpi_dp
*acpi_dp_add_gpio(struct acpi_dp
*dp
, const char *name
,
1143 const char *ref
, int index
, int pin
,
1146 struct acpi_gpio_res_params param
= {
1150 .active_low
= active_low
,
1153 return acpi_dp_add_gpio_array(dp
, name
, ¶m
, 1);
1157 * This function writes a PCI device with _ADR object:
1163 * Name (_ADR, 0x0000000000000000)
1164 * Method (_STA, 0, NotSerialized) { Return (status) }
1168 void acpi_device_write_pci_dev(const struct device
*dev
)
1170 const char *scope
= acpi_device_scope(dev
);
1171 const char *name
= acpi_device_name(dev
);
1173 assert(dev
->path
.type
== DEVICE_PATH_PCI
);
1177 acpigen_write_scope(scope
);
1178 acpigen_write_device(name
);
1180 acpigen_write_ADR_pci_device(dev
);
1181 acpigen_write_STA(acpi_device_status(dev
));
1183 acpigen_pop_len(); /* Device */
1184 acpigen_pop_len(); /* Scope */