intel_int0002_vgpio.c 5.7 KB

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