evged.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_device {
  50. struct device *dev;
  51. struct list_head event_list;
  52. };
  53. struct acpi_ged_event {
  54. struct list_head node;
  55. struct device *dev;
  56. unsigned int gsi;
  57. unsigned int irq;
  58. acpi_handle handle;
  59. };
  60. static irqreturn_t acpi_ged_irq_handler(int irq, void *data)
  61. {
  62. struct acpi_ged_event *event = data;
  63. acpi_status acpi_ret;
  64. acpi_ret = acpi_execute_simple_method(event->handle, NULL, event->gsi);
  65. if (ACPI_FAILURE(acpi_ret))
  66. dev_err_once(event->dev, "IRQ method execution failed\n");
  67. return IRQ_HANDLED;
  68. }
  69. static acpi_status acpi_ged_request_interrupt(struct acpi_resource *ares,
  70. void *context)
  71. {
  72. struct acpi_ged_event *event;
  73. unsigned int irq;
  74. unsigned int gsi;
  75. unsigned int irqflags = IRQF_ONESHOT;
  76. struct acpi_ged_device *geddev = context;
  77. struct device *dev = geddev->dev;
  78. acpi_handle handle = ACPI_HANDLE(dev);
  79. acpi_handle evt_handle;
  80. struct resource r;
  81. struct acpi_resource_irq *p = &ares->data.irq;
  82. struct acpi_resource_extended_irq *pext = &ares->data.extended_irq;
  83. if (ares->type == ACPI_RESOURCE_TYPE_END_TAG)
  84. return AE_OK;
  85. if (!acpi_dev_resource_interrupt(ares, 0, &r)) {
  86. dev_err(dev, "unable to parse IRQ resource\n");
  87. return AE_ERROR;
  88. }
  89. if (ares->type == ACPI_RESOURCE_TYPE_IRQ)
  90. gsi = p->interrupts[0];
  91. else
  92. gsi = pext->interrupts[0];
  93. irq = r.start;
  94. if (ACPI_FAILURE(acpi_get_handle(handle, "_EVT", &evt_handle))) {
  95. dev_err(dev, "cannot locate _EVT method\n");
  96. return AE_ERROR;
  97. }
  98. event = devm_kzalloc(dev, sizeof(*event), GFP_KERNEL);
  99. if (!event)
  100. return AE_ERROR;
  101. event->gsi = gsi;
  102. event->dev = dev;
  103. event->irq = irq;
  104. event->handle = evt_handle;
  105. if (r.flags & IORESOURCE_IRQ_SHAREABLE)
  106. irqflags |= IRQF_SHARED;
  107. if (request_threaded_irq(irq, NULL, acpi_ged_irq_handler,
  108. irqflags, "ACPI:Ged", event)) {
  109. dev_err(dev, "failed to setup event handler for irq %u\n", irq);
  110. return AE_ERROR;
  111. }
  112. dev_dbg(dev, "GED listening GSI %u @ IRQ %u\n", gsi, irq);
  113. list_add_tail(&event->node, &geddev->event_list);
  114. return AE_OK;
  115. }
  116. static int ged_probe(struct platform_device *pdev)
  117. {
  118. struct acpi_ged_device *geddev;
  119. acpi_status acpi_ret;
  120. geddev = devm_kzalloc(&pdev->dev, sizeof(*geddev), GFP_KERNEL);
  121. if (!geddev)
  122. return -ENOMEM;
  123. geddev->dev = &pdev->dev;
  124. INIT_LIST_HEAD(&geddev->event_list);
  125. acpi_ret = acpi_walk_resources(ACPI_HANDLE(&pdev->dev), "_CRS",
  126. acpi_ged_request_interrupt, geddev);
  127. if (ACPI_FAILURE(acpi_ret)) {
  128. dev_err(&pdev->dev, "unable to parse the _CRS record\n");
  129. return -EINVAL;
  130. }
  131. platform_set_drvdata(pdev, geddev);
  132. return 0;
  133. }
  134. static void ged_shutdown(struct platform_device *pdev)
  135. {
  136. struct acpi_ged_device *geddev = platform_get_drvdata(pdev);
  137. struct acpi_ged_event *event, *next;
  138. list_for_each_entry_safe(event, next, &geddev->event_list, node) {
  139. free_irq(event->irq, event);
  140. list_del(&event->node);
  141. dev_dbg(geddev->dev, "GED releasing GSI %u @ IRQ %u\n",
  142. event->gsi, event->irq);
  143. }
  144. }
  145. static int ged_remove(struct platform_device *pdev)
  146. {
  147. ged_shutdown(pdev);
  148. return 0;
  149. }
  150. static const struct acpi_device_id ged_acpi_ids[] = {
  151. {"ACPI0013"},
  152. {},
  153. };
  154. static struct platform_driver ged_driver = {
  155. .probe = ged_probe,
  156. .remove = ged_remove,
  157. .shutdown = ged_shutdown,
  158. .driver = {
  159. .name = MODULE_NAME,
  160. .acpi_match_table = ACPI_PTR(ged_acpi_ids),
  161. },
  162. };
  163. builtin_platform_driver(ged_driver);