intel_int0002_vgpio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel INT0002 "Virtual GPIO" driver
  4. *
  5. * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
  6. *
  7. * Loosely based on android x86 kernel code which is:
  8. *
  9. * Copyright (c) 2014, Intel Corporation.
  10. *
  11. * Author: Dyut Kumar Sil <dyut.k.sil@intel.com>
  12. *
  13. * Some peripherals on Bay Trail and Cherry Trail platforms signal a Power
  14. * Management Event (PME) to the Power Management Controller (PMC) to wakeup
  15. * the system. When this happens software needs to clear the PME bus 0 status
  16. * bit in the GPE0a_STS register to avoid an IRQ storm on IRQ 9.
  17. *
  18. * This is modelled in ACPI through the INT0002 ACPI device, which is
  19. * called a "Virtual GPIO controller" in ACPI because it defines the event
  20. * handler to call when the PME triggers through _AEI and _L02 / _E02
  21. * methods as would be done for a real GPIO interrupt in ACPI. Note this
  22. * is a hack to define an AML event handler for the PME while using existing
  23. * ACPI mechanisms, this is not a real GPIO at all.
  24. *
  25. * This driver will bind to the INT0002 device, and register as a GPIO
  26. * controller, letting gpiolib-acpi.c call the _L02 handler as it would
  27. * for a real GPIO controller.
  28. */
  29. #include <linux/acpi.h>
  30. #include <linux/bitmap.h>
  31. #include <linux/gpio/driver.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/io.h>
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/slab.h>
  38. #include <linux/suspend.h>
  39. #include <asm/cpu_device_id.h>
  40. #include <asm/intel-family.h>
  41. #define DRV_NAME "INT0002 Virtual GPIO"
  42. /* For some reason the virtual GPIO pin tied to the GPE is numbered pin 2 */
  43. #define GPE0A_PME_B0_VIRT_GPIO_PIN 2
  44. #define GPE0A_PME_B0_STS_BIT BIT(13)
  45. #define GPE0A_PME_B0_EN_BIT BIT(13)
  46. #define GPE0A_STS_PORT 0x420
  47. #define GPE0A_EN_PORT 0x428
  48. #define ICPU(model) { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, }
  49. static const struct x86_cpu_id int0002_cpu_ids[] = {
  50. ICPU(INTEL_FAM6_ATOM_SILVERMONT), /* Valleyview, Bay Trail */
  51. ICPU(INTEL_FAM6_ATOM_AIRMONT), /* Braswell, Cherry Trail */
  52. {}
  53. };
  54. /*
  55. * As this is not a real GPIO at all, but just a hack to model an event in
  56. * ACPI the get / set functions are dummy functions.
  57. */
  58. static int int0002_gpio_get(struct gpio_chip *chip, unsigned int offset)
  59. {
  60. return 0;
  61. }
  62. static void int0002_gpio_set(struct gpio_chip *chip, unsigned int offset,
  63. int value)
  64. {
  65. }
  66. static int int0002_gpio_direction_output(struct gpio_chip *chip,
  67. unsigned int offset, int value)
  68. {
  69. return 0;
  70. }
  71. static void int0002_irq_ack(struct irq_data *data)
  72. {
  73. outl(GPE0A_PME_B0_STS_BIT, GPE0A_STS_PORT);
  74. }
  75. static void int0002_irq_unmask(struct irq_data *data)
  76. {
  77. u32 gpe_en_reg;
  78. gpe_en_reg = inl(GPE0A_EN_PORT);
  79. gpe_en_reg |= GPE0A_PME_B0_EN_BIT;
  80. outl(gpe_en_reg, GPE0A_EN_PORT);
  81. }
  82. static void int0002_irq_mask(struct irq_data *data)
  83. {
  84. u32 gpe_en_reg;
  85. gpe_en_reg = inl(GPE0A_EN_PORT);
  86. gpe_en_reg &= ~GPE0A_PME_B0_EN_BIT;
  87. outl(gpe_en_reg, GPE0A_EN_PORT);
  88. }
  89. static int int0002_irq_set_wake(struct irq_data *data, unsigned int on)
  90. {
  91. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  92. struct platform_device *pdev = to_platform_device(chip->parent);
  93. int irq = platform_get_irq(pdev, 0);
  94. /* Propagate to parent irq */
  95. if (on)
  96. enable_irq_wake(irq);
  97. else
  98. disable_irq_wake(irq);
  99. return 0;
  100. }
  101. static irqreturn_t int0002_irq(int irq, void *data)
  102. {
  103. struct gpio_chip *chip = data;
  104. u32 gpe_sts_reg;
  105. gpe_sts_reg = inl(GPE0A_STS_PORT);
  106. if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT))
  107. return IRQ_NONE;
  108. generic_handle_irq(irq_find_mapping(chip->irq.domain,
  109. GPE0A_PME_B0_VIRT_GPIO_PIN));
  110. pm_system_wakeup();
  111. return IRQ_HANDLED;
  112. }
  113. static struct irq_chip int0002_irqchip = {
  114. .name = DRV_NAME,
  115. .irq_ack = int0002_irq_ack,
  116. .irq_mask = int0002_irq_mask,
  117. .irq_unmask = int0002_irq_unmask,
  118. .irq_set_wake = int0002_irq_set_wake,
  119. };
  120. static int int0002_probe(struct platform_device *pdev)
  121. {
  122. struct device *dev = &pdev->dev;
  123. const struct x86_cpu_id *cpu_id;
  124. struct gpio_chip *chip;
  125. int irq, ret;
  126. /* Menlow has a different INT0002 device? <sigh> */
  127. cpu_id = x86_match_cpu(int0002_cpu_ids);
  128. if (!cpu_id)
  129. return -ENODEV;
  130. irq = platform_get_irq(pdev, 0);
  131. if (irq < 0) {
  132. dev_err(dev, "Error getting IRQ: %d\n", irq);
  133. return irq;
  134. }
  135. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  136. if (!chip)
  137. return -ENOMEM;
  138. chip->label = DRV_NAME;
  139. chip->parent = dev;
  140. chip->owner = THIS_MODULE;
  141. chip->get = int0002_gpio_get;
  142. chip->set = int0002_gpio_set;
  143. chip->direction_input = int0002_gpio_get;
  144. chip->direction_output = int0002_gpio_direction_output;
  145. chip->base = -1;
  146. chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1;
  147. chip->irq.need_valid_mask = true;
  148. ret = devm_gpiochip_add_data(&pdev->dev, chip, NULL);
  149. if (ret) {
  150. dev_err(dev, "Error adding gpio chip: %d\n", ret);
  151. return ret;
  152. }
  153. bitmap_clear(chip->irq.valid_mask, 0, GPE0A_PME_B0_VIRT_GPIO_PIN);
  154. /*
  155. * We manually request the irq here instead of passing a flow-handler
  156. * to gpiochip_set_chained_irqchip, because the irq is shared.
  157. */
  158. ret = devm_request_irq(dev, irq, int0002_irq,
  159. IRQF_SHARED, "INT0002", chip);
  160. if (ret) {
  161. dev_err(dev, "Error requesting IRQ %d: %d\n", irq, ret);
  162. return ret;
  163. }
  164. ret = gpiochip_irqchip_add(chip, &int0002_irqchip, 0, handle_edge_irq,
  165. IRQ_TYPE_NONE);
  166. if (ret) {
  167. dev_err(dev, "Error adding irqchip: %d\n", ret);
  168. return ret;
  169. }
  170. gpiochip_set_chained_irqchip(chip, &int0002_irqchip, irq, NULL);
  171. return 0;
  172. }
  173. static const struct acpi_device_id int0002_acpi_ids[] = {
  174. { "INT0002", 0 },
  175. { },
  176. };
  177. MODULE_DEVICE_TABLE(acpi, int0002_acpi_ids);
  178. static struct platform_driver int0002_driver = {
  179. .driver = {
  180. .name = DRV_NAME,
  181. .acpi_match_table = int0002_acpi_ids,
  182. },
  183. .probe = int0002_probe,
  184. };
  185. module_platform_driver(int0002_driver);
  186. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  187. MODULE_DESCRIPTION("Intel INT0002 Virtual GPIO driver");
  188. MODULE_LICENSE("GPL v2");