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