intel_int0002_vgpio.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. struct int0002_data {
  49. struct gpio_chip chip;
  50. int parent_irq;
  51. int wake_enable_count;
  52. };
  53. /*
  54. * As this is not a real GPIO at all, but just a hack to model an event in
  55. * ACPI the get / set functions are dummy functions.
  56. */
  57. static int int0002_gpio_get(struct gpio_chip *chip, unsigned int offset)
  58. {
  59. return 0;
  60. }
  61. static void int0002_gpio_set(struct gpio_chip *chip, unsigned int offset,
  62. int value)
  63. {
  64. }
  65. static int int0002_gpio_direction_output(struct gpio_chip *chip,
  66. unsigned int offset, int value)
  67. {
  68. return 0;
  69. }
  70. static void int0002_irq_ack(struct irq_data *data)
  71. {
  72. outl(GPE0A_PME_B0_STS_BIT, GPE0A_STS_PORT);
  73. }
  74. static void int0002_irq_unmask(struct irq_data *data)
  75. {
  76. u32 gpe_en_reg;
  77. gpe_en_reg = inl(GPE0A_EN_PORT);
  78. gpe_en_reg |= GPE0A_PME_B0_EN_BIT;
  79. outl(gpe_en_reg, GPE0A_EN_PORT);
  80. }
  81. static void int0002_irq_mask(struct irq_data *data)
  82. {
  83. u32 gpe_en_reg;
  84. gpe_en_reg = inl(GPE0A_EN_PORT);
  85. gpe_en_reg &= ~GPE0A_PME_B0_EN_BIT;
  86. outl(gpe_en_reg, GPE0A_EN_PORT);
  87. }
  88. static int int0002_irq_set_wake(struct irq_data *data, unsigned int on)
  89. {
  90. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  91. struct int0002_data *int0002 = container_of(chip, struct int0002_data, chip);
  92. /*
  93. * Applying of the wakeup flag to our parent IRQ is delayed till system
  94. * suspend, because we only want to do this when using s2idle.
  95. */
  96. if (on)
  97. int0002->wake_enable_count++;
  98. else
  99. int0002->wake_enable_count--;
  100. return 0;
  101. }
  102. static irqreturn_t int0002_irq(int irq, void *data)
  103. {
  104. struct gpio_chip *chip = data;
  105. u32 gpe_sts_reg;
  106. gpe_sts_reg = inl(GPE0A_STS_PORT);
  107. if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT))
  108. return IRQ_NONE;
  109. generic_handle_irq(irq_find_mapping(chip->irq.domain,
  110. GPE0A_PME_B0_VIRT_GPIO_PIN));
  111. pm_wakeup_hard_event(chip->parent);
  112. return IRQ_HANDLED;
  113. }
  114. static bool int0002_check_wake(void *data)
  115. {
  116. u32 gpe_sts_reg;
  117. gpe_sts_reg = inl(GPE0A_STS_PORT);
  118. return (gpe_sts_reg & GPE0A_PME_B0_STS_BIT);
  119. }
  120. static struct irq_chip int0002_irqchip = {
  121. .name = DRV_NAME,
  122. .irq_ack = int0002_irq_ack,
  123. .irq_mask = int0002_irq_mask,
  124. .irq_unmask = int0002_irq_unmask,
  125. .irq_set_wake = int0002_irq_set_wake,
  126. };
  127. static const struct x86_cpu_id int0002_cpu_ids[] = {
  128. X86_MATCH_INTEL_FAM6_MODEL(ATOM_SILVERMONT, NULL),
  129. X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT, NULL),
  130. {}
  131. };
  132. static void int0002_init_irq_valid_mask(struct gpio_chip *chip,
  133. unsigned long *valid_mask,
  134. unsigned int ngpios)
  135. {
  136. bitmap_clear(valid_mask, 0, GPE0A_PME_B0_VIRT_GPIO_PIN);
  137. }
  138. static int int0002_probe(struct platform_device *pdev)
  139. {
  140. struct device *dev = &pdev->dev;
  141. const struct x86_cpu_id *cpu_id;
  142. struct int0002_data *int0002;
  143. struct gpio_irq_chip *girq;
  144. struct gpio_chip *chip;
  145. int irq, ret;
  146. /* Menlow has a different INT0002 device? <sigh> */
  147. cpu_id = x86_match_cpu(int0002_cpu_ids);
  148. if (!cpu_id)
  149. return -ENODEV;
  150. irq = platform_get_irq(pdev, 0);
  151. if (irq < 0)
  152. return irq;
  153. int0002 = devm_kzalloc(dev, sizeof(*int0002), GFP_KERNEL);
  154. if (!int0002)
  155. return -ENOMEM;
  156. int0002->parent_irq = irq;
  157. chip = &int0002->chip;
  158. chip->label = DRV_NAME;
  159. chip->parent = dev;
  160. chip->owner = THIS_MODULE;
  161. chip->get = int0002_gpio_get;
  162. chip->set = int0002_gpio_set;
  163. chip->direction_input = int0002_gpio_get;
  164. chip->direction_output = int0002_gpio_direction_output;
  165. chip->base = -1;
  166. chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1;
  167. chip->irq.init_valid_mask = int0002_init_irq_valid_mask;
  168. /*
  169. * We directly request the irq here instead of passing a flow-handler
  170. * to gpiochip_set_chained_irqchip, because the irq is shared.
  171. * FIXME: augment this if we managed to pull handling of shared
  172. * IRQs into gpiolib.
  173. */
  174. ret = devm_request_irq(dev, irq, int0002_irq,
  175. IRQF_SHARED, "INT0002", chip);
  176. if (ret) {
  177. dev_err(dev, "Error requesting IRQ %d: %d\n", irq, ret);
  178. return ret;
  179. }
  180. girq = &chip->irq;
  181. girq->chip = &int0002_irqchip;
  182. /* This let us handle the parent IRQ in the driver */
  183. girq->parent_handler = NULL;
  184. girq->num_parents = 0;
  185. girq->parents = NULL;
  186. girq->default_type = IRQ_TYPE_NONE;
  187. girq->handler = handle_edge_irq;
  188. ret = devm_gpiochip_add_data(dev, chip, NULL);
  189. if (ret) {
  190. dev_err(dev, "Error adding gpio chip: %d\n", ret);
  191. return ret;
  192. }
  193. acpi_register_wakeup_handler(irq, int0002_check_wake, NULL);
  194. device_init_wakeup(dev, true);
  195. dev_set_drvdata(dev, int0002);
  196. return 0;
  197. }
  198. static int int0002_remove(struct platform_device *pdev)
  199. {
  200. device_init_wakeup(&pdev->dev, false);
  201. acpi_unregister_wakeup_handler(int0002_check_wake, NULL);
  202. return 0;
  203. }
  204. static int int0002_suspend(struct device *dev)
  205. {
  206. struct int0002_data *int0002 = dev_get_drvdata(dev);
  207. /*
  208. * The INT0002 parent IRQ is often shared with the ACPI GPE IRQ, don't
  209. * muck with it when firmware based suspend is used, otherwise we may
  210. * cause spurious wakeups from firmware managed suspend.
  211. */
  212. if (!pm_suspend_via_firmware() && int0002->wake_enable_count)
  213. enable_irq_wake(int0002->parent_irq);
  214. return 0;
  215. }
  216. static int int0002_resume(struct device *dev)
  217. {
  218. struct int0002_data *int0002 = dev_get_drvdata(dev);
  219. if (!pm_suspend_via_firmware() && int0002->wake_enable_count)
  220. disable_irq_wake(int0002->parent_irq);
  221. return 0;
  222. }
  223. static const struct dev_pm_ops int0002_pm_ops = {
  224. .suspend = int0002_suspend,
  225. .resume = int0002_resume,
  226. };
  227. static const struct acpi_device_id int0002_acpi_ids[] = {
  228. { "INT0002", 0 },
  229. { },
  230. };
  231. MODULE_DEVICE_TABLE(acpi, int0002_acpi_ids);
  232. static struct platform_driver int0002_driver = {
  233. .driver = {
  234. .name = DRV_NAME,
  235. .acpi_match_table = int0002_acpi_ids,
  236. .pm = &int0002_pm_ops,
  237. },
  238. .probe = int0002_probe,
  239. .remove = int0002_remove,
  240. };
  241. module_platform_driver(int0002_driver);
  242. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  243. MODULE_DESCRIPTION("Intel INT0002 Virtual GPIO driver");
  244. MODULE_LICENSE("GPL v2");