evged.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Generic Event Device for ACPI.
  3. *
  4. * Copyright (c) 2016, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Generic Event Device allows platforms to handle interrupts in ACPI
  16. * ASL statements. It follows very similar to _EVT method approach
  17. * from GPIO events. All interrupts are listed in _CRS and the handler
  18. * is written in _EVT method. Here is an example.
  19. *
  20. * Device (GED0)
  21. * {
  22. *
  23. * Name (_HID, "ACPI0013")
  24. * Name (_UID, 0)
  25. * Method (_CRS, 0x0, Serialized)
  26. * {
  27. * Name (RBUF, ResourceTemplate ()
  28. * {
  29. * Interrupt(ResourceConsumer, Edge, ActiveHigh, Shared, , , )
  30. * {123}
  31. * }
  32. * })
  33. *
  34. * Method (_EVT, 1) {
  35. * if (Lequal(123, Arg0))
  36. * {
  37. * }
  38. * }
  39. * }
  40. *
  41. */
  42. #include <linux/err.h>
  43. #include <linux/init.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/list.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/acpi.h>
  48. #define MODULE_NAME "acpi-ged"
  49. struct acpi_ged_event {
  50. struct list_head node;
  51. struct device *dev;
  52. unsigned int gsi;
  53. unsigned int irq;
  54. acpi_handle handle;
  55. };
  56. static irqreturn_t acpi_ged_irq_handler(int irq, void *data)
  57. {
  58. struct acpi_ged_event *event = data;
  59. acpi_status acpi_ret;
  60. acpi_ret = acpi_execute_simple_method(event->handle, NULL, event->gsi);
  61. if (ACPI_FAILURE(acpi_ret))
  62. dev_err_once(event->dev, "IRQ method execution failed\n");
  63. return IRQ_HANDLED;
  64. }
  65. static acpi_status acpi_ged_request_interrupt(struct acpi_resource *ares,
  66. void *context)
  67. {
  68. struct acpi_ged_event *event;
  69. unsigned int irq;
  70. unsigned int gsi;
  71. unsigned int irqflags = IRQF_ONESHOT;
  72. struct device *dev = context;
  73. acpi_handle handle = ACPI_HANDLE(dev);
  74. acpi_handle evt_handle;
  75. struct resource r;
  76. struct acpi_resource_irq *p = &ares->data.irq;
  77. struct acpi_resource_extended_irq *pext = &ares->data.extended_irq;
  78. if (ares->type == ACPI_RESOURCE_TYPE_END_TAG)
  79. return AE_OK;
  80. if (!acpi_dev_resource_interrupt(ares, 0, &r)) {
  81. dev_err(dev, "unable to parse IRQ resource\n");
  82. return AE_ERROR;
  83. }
  84. if (ares->type == ACPI_RESOURCE_TYPE_IRQ)
  85. gsi = p->interrupts[0];
  86. else
  87. gsi = pext->interrupts[0];
  88. irq = r.start;
  89. if (ACPI_FAILURE(acpi_get_handle(handle, "_EVT", &evt_handle))) {
  90. dev_err(dev, "cannot locate _EVT method\n");
  91. return AE_ERROR;
  92. }
  93. dev_info(dev, "GED listening GSI %u @ IRQ %u\n", gsi, irq);
  94. event = devm_kzalloc(dev, sizeof(*event), GFP_KERNEL);
  95. if (!event)
  96. return AE_ERROR;
  97. event->gsi = gsi;
  98. event->dev = dev;
  99. event->irq = irq;
  100. event->handle = evt_handle;
  101. if (r.flags & IORESOURCE_IRQ_SHAREABLE)
  102. irqflags |= IRQF_SHARED;
  103. if (devm_request_threaded_irq(dev, irq, NULL, acpi_ged_irq_handler,
  104. irqflags, "ACPI:Ged", event)) {
  105. dev_err(dev, "failed to setup event handler for irq %u\n", irq);
  106. return AE_ERROR;
  107. }
  108. return AE_OK;
  109. }
  110. static int ged_probe(struct platform_device *pdev)
  111. {
  112. acpi_status acpi_ret;
  113. acpi_ret = acpi_walk_resources(ACPI_HANDLE(&pdev->dev), "_CRS",
  114. acpi_ged_request_interrupt, &pdev->dev);
  115. if (ACPI_FAILURE(acpi_ret)) {
  116. dev_err(&pdev->dev, "unable to parse the _CRS record\n");
  117. return -EINVAL;
  118. }
  119. return 0;
  120. }
  121. static const struct acpi_device_id ged_acpi_ids[] = {
  122. {"ACPI0013"},
  123. {},
  124. };
  125. static struct platform_driver ged_driver = {
  126. .probe = ged_probe,
  127. .driver = {
  128. .name = MODULE_NAME,
  129. .acpi_match_table = ACPI_PTR(ged_acpi_ids),
  130. },
  131. };
  132. builtin_platform_driver(ged_driver);