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 30 #include <linux/acpi.h> 31 #include <linux/bitmap.h> 32 #include <linux/gpio/driver.h> 33 #include <linux/interrupt.h> 34 #include <linux/io.h> 35 #include <linux/kernel.h> 36 #include <linux/module.h> 37 #include <linux/platform_data/x86/soc.h> 38 #include <linux/platform_device.h> 39 #include <linux/slab.h> 40 #include <linux/suspend.h> 41 42 #define DRV_NAME "INT0002 Virtual GPIO" 43 44 /* For some reason the virtual GPIO pin tied to the GPE is numbered pin 2 */ 45 #define GPE0A_PME_B0_VIRT_GPIO_PIN 2 46 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 52 struct int0002_data { 53 struct gpio_chip chip; 54 int parent_irq; 55 int wake_enable_count; 56 }; 57 58 /* 59 * As this is not a real GPIO at all, but just a hack to model an event in 60 * ACPI the get / set functions are dummy functions. 61 */ 62 63 static int int0002_gpio_get(struct gpio_chip *chip, unsigned int offset) 64 { 65 return 0; 66 } 67 68 static void int0002_gpio_set(struct gpio_chip *chip, unsigned int offset, 69 int value) 70 { 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 79 static void int0002_irq_ack(struct irq_data *data) 80 { 81 outl(GPE0A_PME_B0_STS_BIT, GPE0A_STS_PORT); 82 } 83 84 static void int0002_irq_unmask(struct irq_data *data) 85 { 86 struct gpio_chip *gc = irq_data_get_irq_chip_data(data); 87 irq_hw_number_t hwirq = irqd_to_hwirq(data); 88 u32 gpe_en_reg; 89 90 gpiochip_enable_irq(gc, hwirq); 91 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 97 static void int0002_irq_mask(struct irq_data *data) 98 { 99 struct gpio_chip *gc = irq_data_get_irq_chip_data(data); 100 irq_hw_number_t hwirq = irqd_to_hwirq(data); 101 u32 gpe_en_reg; 102 103 gpe_en_reg = inl(GPE0A_EN_PORT); 104 gpe_en_reg &= ~GPE0A_PME_B0_EN_BIT; 105 outl(gpe_en_reg, GPE0A_EN_PORT); 106 107 gpiochip_disable_irq(gc, hwirq); 108 } 109 110 static int int0002_irq_set_wake(struct irq_data *data, unsigned int on) 111 { 112 struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 113 struct int0002_data *int0002 = container_of(chip, struct int0002_data, chip); 114 115 /* 116 * Applying of the wakeup flag to our parent IRQ is delayed till system 117 * suspend, because we only want to do this when using s2idle. 118 */ 119 if (on) 120 int0002->wake_enable_count++; 121 else 122 int0002->wake_enable_count--; 123 124 return 0; 125 } 126 127 static irqreturn_t int0002_irq(int irq, void *data) 128 { 129 struct gpio_chip *chip = data; 130 u32 gpe_sts_reg; 131 132 gpe_sts_reg = inl(GPE0A_STS_PORT); 133 if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT)) 134 return IRQ_NONE; 135 136 generic_handle_domain_irq_safe(chip->irq.domain, GPE0A_PME_B0_VIRT_GPIO_PIN); 137 138 pm_wakeup_hard_event(chip->parent); 139 140 return IRQ_HANDLED; 141 } 142 143 static bool int0002_check_wake(void *data) 144 { 145 u32 gpe_sts_reg; 146 147 gpe_sts_reg = inl(GPE0A_STS_PORT); 148 return (gpe_sts_reg & GPE0A_PME_B0_STS_BIT); 149 } 150 151 static const struct irq_chip int0002_irqchip = { 152 .name = DRV_NAME, 153 .irq_ack = int0002_irq_ack, 154 .irq_mask = int0002_irq_mask, 155 .irq_unmask = int0002_irq_unmask, 156 .irq_set_wake = int0002_irq_set_wake, 157 .flags = IRQCHIP_IMMUTABLE, 158 GPIOCHIP_IRQ_RESOURCE_HELPERS, 159 }; 160 161 static void int0002_init_irq_valid_mask(struct gpio_chip *chip, 162 unsigned long *valid_mask, 163 unsigned int ngpios) 164 { 165 bitmap_clear(valid_mask, 0, GPE0A_PME_B0_VIRT_GPIO_PIN); 166 } 167 168 static int int0002_probe(struct platform_device *pdev) 169 { 170 struct device *dev = &pdev->dev; 171 struct int0002_data *int0002; 172 struct gpio_irq_chip *girq; 173 struct gpio_chip *chip; 174 int irq, ret; 175 176 /* Menlow has a different INT0002 device? <sigh> */ 177 if (!soc_intel_is_byt() && !soc_intel_is_cht()) 178 return -ENODEV; 179 180 irq = platform_get_irq(pdev, 0); 181 if (irq < 0) 182 return irq; 183 184 int0002 = devm_kzalloc(dev, sizeof(*int0002), GFP_KERNEL); 185 if (!int0002) 186 return -ENOMEM; 187 188 int0002->parent_irq = irq; 189 190 chip = &int0002->chip; 191 chip->label = DRV_NAME; 192 chip->parent = dev; 193 chip->owner = THIS_MODULE; 194 chip->get = int0002_gpio_get; 195 chip->set = int0002_gpio_set; 196 chip->direction_input = int0002_gpio_get; 197 chip->direction_output = int0002_gpio_direction_output; 198 chip->base = -1; 199 chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1; 200 chip->irq.init_valid_mask = int0002_init_irq_valid_mask; 201 202 /* 203 * We directly request the irq here instead of passing a flow-handler 204 * to gpiochip_set_chained_irqchip, because the irq is shared. 205 * FIXME: augment this if we managed to pull handling of shared 206 * IRQs into gpiolib. 207 */ 208 ret = devm_request_irq(dev, irq, int0002_irq, 209 IRQF_ONESHOT | IRQF_SHARED, "INT0002", chip); 210 if (ret) { 211 dev_err(dev, "Error requesting IRQ %d: %d\n", irq, ret); 212 return ret; 213 } 214 215 girq = &chip->irq; 216 gpio_irq_chip_set_chip(girq, &int0002_irqchip); 217 /* This let us handle the parent IRQ in the driver */ 218 girq->parent_handler = NULL; 219 girq->num_parents = 0; 220 girq->parents = NULL; 221 girq->default_type = IRQ_TYPE_NONE; 222 girq->handler = handle_edge_irq; 223 224 ret = devm_gpiochip_add_data(dev, chip, NULL); 225 if (ret) { 226 dev_err(dev, "Error adding gpio chip: %d\n", ret); 227 return ret; 228 } 229 230 acpi_register_wakeup_handler(irq, int0002_check_wake, NULL); 231 device_init_wakeup(dev, true); 232 dev_set_drvdata(dev, int0002); 233 return 0; 234 } 235 236 static void int0002_remove(struct platform_device *pdev) 237 { 238 device_init_wakeup(&pdev->dev, false); 239 acpi_unregister_wakeup_handler(int0002_check_wake, NULL); 240 } 241 242 static int int0002_suspend(struct device *dev) 243 { 244 struct int0002_data *int0002 = dev_get_drvdata(dev); 245 246 /* 247 * The INT0002 parent IRQ is often shared with the ACPI GPE IRQ, don't 248 * muck with it when firmware based suspend is used, otherwise we may 249 * cause spurious wakeups from firmware managed suspend. 250 */ 251 if (!pm_suspend_via_firmware() && int0002->wake_enable_count) 252 enable_irq_wake(int0002->parent_irq); 253 254 return 0; 255 } 256 257 static int int0002_resume(struct device *dev) 258 { 259 struct int0002_data *int0002 = dev_get_drvdata(dev); 260 261 if (!pm_suspend_via_firmware() && int0002->wake_enable_count) 262 disable_irq_wake(int0002->parent_irq); 263 264 return 0; 265 } 266 267 static const struct dev_pm_ops int0002_pm_ops = { 268 .suspend = int0002_suspend, 269 .resume = int0002_resume, 270 }; 271 272 static const struct acpi_device_id int0002_acpi_ids[] = { 273 { "INT0002", 0 }, 274 { }, 275 }; 276 MODULE_DEVICE_TABLE(acpi, int0002_acpi_ids); 277 278 static struct platform_driver int0002_driver = { 279 .driver = { 280 .name = DRV_NAME, 281 .acpi_match_table = int0002_acpi_ids, 282 .pm = &int0002_pm_ops, 283 }, 284 .probe = int0002_probe, 285 .remove = int0002_remove, 286 }; 287 288 module_platform_driver(int0002_driver); 289 290 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); 291 MODULE_DESCRIPTION("Intel INT0002 Virtual GPIO driver"); 292 MODULE_LICENSE("GPL v2"); 293