]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
acpi: Support generation of a GPIO/irq for a device
authorSimon Glass <sjg@chromium.org>
Tue, 7 Jul 2020 19:11:47 +0000 (13:11 -0600)
committerBin Meng <bmeng.cn@gmail.com>
Fri, 17 Jul 2020 06:32:24 +0000 (14:32 +0800)
Some devices use interrupts but some use GPIOs. Since these are fully
specified in the device tree we can automatically produce the correct ACPI
descriptor for a device.

Add a function to handle this.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Wolfgang Wallner <wolfgang.wallner@br-automation.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
include/acpi/acpi_device.h
lib/acpi/acpi_device.c
test/dm/acpigen.c

index 002e8356448e47a219aba977d1dfd7118b44972f..67a242eb75d67ca0154eb9476e17df4005f02ffd 100644 (file)
@@ -255,4 +255,19 @@ int acpi_device_write_gpio(struct acpi_ctx *ctx, const struct acpi_gpio *gpio);
 int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
                                const struct gpio_desc *desc);
 
+/**
+ * acpi_device_write_interrupt_or_gpio() - Write interrupt or GPIO to ACPI
+ *
+ * This reads an interrupt from the device tree "interrupts-extended" property,
+ * if available. If not it reads the first GPIO with the name @prop.
+ *
+ * If an interrupt is found, an ACPI interrupt descriptor is written to the ACPI
+ * output. If not, but if a GPIO is found, a GPIO descriptor is written.
+ *
+ * @return irq or GPIO pin number if OK, -ve if neither an interrupt nor a GPIO
+ *     could be found, or some other error occurred
+ */
+int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
+                                       struct udevice *dev, const char *prop);
+
 #endif
index bbe1cfc57ad92c8744c670a294cb828d88d827d8..c93af2e58370c9016b47aab6d06b6057b661f940 100644 (file)
@@ -354,5 +354,34 @@ int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
        if (ret < 0)
                return log_msg_ret("gpio", ret);
 
-       return 0;
+       return ret;
+}
+
+int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
+                                       struct udevice *dev, const char *prop)
+{
+       struct irq req_irq;
+       int pin;
+       int ret;
+
+       ret = irq_get_by_index(dev, 0, &req_irq);
+       if (!ret) {
+               ret = acpi_device_write_interrupt_irq(ctx, &req_irq);
+               if (ret < 0)
+                       return log_msg_ret("irq", ret);
+               pin = ret;
+       } else {
+               struct gpio_desc req_gpio;
+
+               ret = gpio_request_by_name(dev, prop, 0, &req_gpio,
+                                          GPIOD_IS_IN);
+               if (ret)
+                       return log_msg_ret("no gpio", ret);
+               ret = acpi_device_write_gpio_desc(ctx, &req_gpio);
+               if (ret < 0)
+                       return log_msg_ret("gpio", ret);
+               pin = ret;
+       }
+
+       return pin;
 }
index d15273d6bff5adfae4263e63fca682d00468483c..7d81652295e291be5e58dcc90edd4dbbdce6b602 100644 (file)
@@ -16,6 +16,7 @@
 #include <asm/unaligned.h>
 #include <dm/acpi.h>
 #include <dm/test.h>
+#include <dm/uclass-internal.h>
 #include <test/ut.h>
 
 /* Maximum size of the ACPI context needed for most tests */
@@ -235,3 +236,43 @@ static int dm_test_acpi_gpio_irq(struct unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_acpi_gpio_irq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test emitting either a GPIO or interrupt descriptor */
+static int dm_test_acpi_interrupt_or_gpio(struct unit_test_state *uts)
+{
+       struct acpi_ctx *ctx;
+       struct udevice *dev;
+       u8 *ptr;
+
+       ut_assertok(alloc_context(&ctx));
+
+       ptr = acpigen_get_current(ctx);
+
+       /* This should produce an interrupt, even though it also has a GPIO */
+       ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
+       ut_asserteq_str("a-test", dev->name);
+       ut_asserteq(3, acpi_device_write_interrupt_or_gpio(ctx, dev,
+                                                          "test2-gpios"));
+       ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
+
+       /* This has no interrupt so should produce a GPIO */
+       ptr = ctx->current;
+       ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &dev));
+       ut_asserteq(1, acpi_device_write_interrupt_or_gpio(ctx, dev,
+                                                          "enable-gpios"));
+       ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
+
+       /* This one has neither */
+       ptr = acpigen_get_current(ctx);
+       ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
+       ut_asserteq_str("b-test", dev->name);
+       ut_asserteq(-ENOENT,
+                   acpi_device_write_interrupt_or_gpio(ctx, dev,
+                                                       "enable-gpios"));
+
+       free_context(&ctx);
+
+       return 0;
+}
+DM_TEST(dm_test_acpi_interrupt_or_gpio,
+       DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);