evged.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. char ev_name[5];
  84. u8 trigger;
  85. if (ares->type == ACPI_RESOURCE_TYPE_END_TAG)
  86. return AE_OK;
  87. if (!acpi_dev_resource_interrupt(ares, 0, &r)) {
  88. dev_err(dev, "unable to parse IRQ resource\n");
  89. return AE_ERROR;
  90. }
  91. if (ares->type == ACPI_RESOURCE_TYPE_IRQ) {
  92. gsi = p->interrupts[0];
  93. trigger = p->triggering;
  94. } else {
  95. gsi = pext->interrupts[0];
  96. trigger = pext->triggering;
  97. }
  98. irq = r.start;
  99. switch (gsi) {
  100. case 0 ... 255:
  101. sprintf(ev_name, "_%c%02X",
  102. trigger == ACPI_EDGE_SENSITIVE ? 'E' : 'L', gsi);
  103. if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
  104. break;
  105. /* fall through */
  106. default:
  107. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
  108. break;
  109. dev_err(dev, "cannot locate _EVT method\n");
  110. return AE_ERROR;
  111. }
  112. event = devm_kzalloc(dev, sizeof(*event), GFP_KERNEL);
  113. if (!event)
  114. return AE_ERROR;
  115. event->gsi = gsi;
  116. event->dev = dev;
  117. event->irq = irq;
  118. event->handle = evt_handle;
  119. if (r.flags & IORESOURCE_IRQ_SHAREABLE)
  120. irqflags |= IRQF_SHARED;
  121. if (request_threaded_irq(irq, NULL, acpi_ged_irq_handler,
  122. irqflags, "ACPI:Ged", event)) {
  123. dev_err(dev, "failed to setup event handler for irq %u\n", irq);
  124. return AE_ERROR;
  125. }
  126. dev_dbg(dev, "GED listening GSI %u @ IRQ %u\n", gsi, irq);
  127. list_add_tail(&event->node, &geddev->event_list);
  128. return AE_OK;
  129. }
  130. static int ged_probe(struct platform_device *pdev)
  131. {
  132. struct acpi_ged_device *geddev;
  133. acpi_status acpi_ret;
  134. geddev = devm_kzalloc(&pdev->dev, sizeof(*geddev), GFP_KERNEL);
  135. if (!geddev)
  136. return -ENOMEM;
  137. geddev->dev = &pdev->dev;
  138. INIT_LIST_HEAD(&geddev->event_list);
  139. acpi_ret = acpi_walk_resources(ACPI_HANDLE(&pdev->dev), "_CRS",
  140. acpi_ged_request_interrupt, geddev);
  141. if (ACPI_FAILURE(acpi_ret)) {
  142. dev_err(&pdev->dev, "unable to parse the _CRS record\n");
  143. return -EINVAL;
  144. }
  145. platform_set_drvdata(pdev, geddev);
  146. return 0;
  147. }
  148. static void ged_shutdown(struct platform_device *pdev)
  149. {
  150. struct acpi_ged_device *geddev = platform_get_drvdata(pdev);
  151. struct acpi_ged_event *event, *next;
  152. list_for_each_entry_safe(event, next, &geddev->event_list, node) {
  153. free_irq(event->irq, event);
  154. list_del(&event->node);
  155. dev_dbg(geddev->dev, "GED releasing GSI %u @ IRQ %u\n",
  156. event->gsi, event->irq);
  157. }
  158. }
  159. static int ged_remove(struct platform_device *pdev)
  160. {
  161. ged_shutdown(pdev);
  162. return 0;
  163. }
  164. static const struct acpi_device_id ged_acpi_ids[] = {
  165. {"ACPI0013"},
  166. {},
  167. };
  168. static struct platform_driver ged_driver = {
  169. .probe = ged_probe,
  170. .remove = ged_remove,
  171. .shutdown = ged_shutdown,
  172. .driver = {
  173. .name = MODULE_NAME,
  174. .acpi_match_table = ACPI_PTR(ged_acpi_ids),
  175. },
  176. };
  177. builtin_platform_driver(ged_driver);