Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Intel INT0002 "Virtual GPIO" driver
0004  *
0005  * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
0006  *
0007  * Loosely based on android x86 kernel code which is:
0008  *
0009  * Copyright (c) 2014, Intel Corporation.
0010  *
0011  * Author: Dyut Kumar Sil <dyut.k.sil@intel.com>
0012  *
0013  * Some peripherals on Bay Trail and Cherry Trail platforms signal a Power
0014  * Management Event (PME) to the Power Management Controller (PMC) to wakeup
0015  * the system. When this happens software needs to clear the PME bus 0 status
0016  * bit in the GPE0a_STS register to avoid an IRQ storm on IRQ 9.
0017  *
0018  * This is modelled in ACPI through the INT0002 ACPI device, which is
0019  * called a "Virtual GPIO controller" in ACPI because it defines the event
0020  * handler to call when the PME triggers through _AEI and _L02 / _E02
0021  * methods as would be done for a real GPIO interrupt in ACPI. Note this
0022  * is a hack to define an AML event handler for the PME while using existing
0023  * ACPI mechanisms, this is not a real GPIO at all.
0024  *
0025  * This driver will bind to the INT0002 device, and register as a GPIO
0026  * controller, letting gpiolib-acpi.c call the _L02 handler as it would
0027  * for a real GPIO controller.
0028  */
0029 
0030 #include <linux/acpi.h>
0031 #include <linux/bitmap.h>
0032 #include <linux/gpio/driver.h>
0033 #include <linux/interrupt.h>
0034 #include <linux/io.h>
0035 #include <linux/kernel.h>
0036 #include <linux/module.h>
0037 #include <linux/platform_data/x86/soc.h>
0038 #include <linux/platform_device.h>
0039 #include <linux/slab.h>
0040 #include <linux/suspend.h>
0041 
0042 #define DRV_NAME            "INT0002 Virtual GPIO"
0043 
0044 /* For some reason the virtual GPIO pin tied to the GPE is numbered pin 2 */
0045 #define GPE0A_PME_B0_VIRT_GPIO_PIN  2
0046 
0047 #define GPE0A_PME_B0_STS_BIT        BIT(13)
0048 #define GPE0A_PME_B0_EN_BIT     BIT(13)
0049 #define GPE0A_STS_PORT          0x420
0050 #define GPE0A_EN_PORT           0x428
0051 
0052 struct int0002_data {
0053     struct gpio_chip chip;
0054     int parent_irq;
0055     int wake_enable_count;
0056 };
0057 
0058 /*
0059  * As this is not a real GPIO at all, but just a hack to model an event in
0060  * ACPI the get / set functions are dummy functions.
0061  */
0062 
0063 static int int0002_gpio_get(struct gpio_chip *chip, unsigned int offset)
0064 {
0065     return 0;
0066 }
0067 
0068 static void int0002_gpio_set(struct gpio_chip *chip, unsigned int offset,
0069                  int value)
0070 {
0071 }
0072 
0073 static int int0002_gpio_direction_output(struct gpio_chip *chip,
0074                      unsigned int offset, int value)
0075 {
0076     return 0;
0077 }
0078 
0079 static void int0002_irq_ack(struct irq_data *data)
0080 {
0081     outl(GPE0A_PME_B0_STS_BIT, GPE0A_STS_PORT);
0082 }
0083 
0084 static void int0002_irq_unmask(struct irq_data *data)
0085 {
0086     u32 gpe_en_reg;
0087 
0088     gpe_en_reg = inl(GPE0A_EN_PORT);
0089     gpe_en_reg |= GPE0A_PME_B0_EN_BIT;
0090     outl(gpe_en_reg, GPE0A_EN_PORT);
0091 }
0092 
0093 static void int0002_irq_mask(struct irq_data *data)
0094 {
0095     u32 gpe_en_reg;
0096 
0097     gpe_en_reg = inl(GPE0A_EN_PORT);
0098     gpe_en_reg &= ~GPE0A_PME_B0_EN_BIT;
0099     outl(gpe_en_reg, GPE0A_EN_PORT);
0100 }
0101 
0102 static int int0002_irq_set_wake(struct irq_data *data, unsigned int on)
0103 {
0104     struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
0105     struct int0002_data *int0002 = container_of(chip, struct int0002_data, chip);
0106 
0107     /*
0108      * Applying of the wakeup flag to our parent IRQ is delayed till system
0109      * suspend, because we only want to do this when using s2idle.
0110      */
0111     if (on)
0112         int0002->wake_enable_count++;
0113     else
0114         int0002->wake_enable_count--;
0115 
0116     return 0;
0117 }
0118 
0119 static irqreturn_t int0002_irq(int irq, void *data)
0120 {
0121     struct gpio_chip *chip = data;
0122     u32 gpe_sts_reg;
0123 
0124     gpe_sts_reg = inl(GPE0A_STS_PORT);
0125     if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT))
0126         return IRQ_NONE;
0127 
0128     generic_handle_irq(irq_find_mapping(chip->irq.domain,
0129                         GPE0A_PME_B0_VIRT_GPIO_PIN));
0130 
0131     pm_wakeup_hard_event(chip->parent);
0132 
0133     return IRQ_HANDLED;
0134 }
0135 
0136 static bool int0002_check_wake(void *data)
0137 {
0138     u32 gpe_sts_reg;
0139 
0140     gpe_sts_reg = inl(GPE0A_STS_PORT);
0141     return (gpe_sts_reg & GPE0A_PME_B0_STS_BIT);
0142 }
0143 
0144 static struct irq_chip int0002_irqchip = {
0145     .name           = DRV_NAME,
0146     .irq_ack        = int0002_irq_ack,
0147     .irq_mask       = int0002_irq_mask,
0148     .irq_unmask     = int0002_irq_unmask,
0149     .irq_set_wake       = int0002_irq_set_wake,
0150 };
0151 
0152 static void int0002_init_irq_valid_mask(struct gpio_chip *chip,
0153                     unsigned long *valid_mask,
0154                     unsigned int ngpios)
0155 {
0156     bitmap_clear(valid_mask, 0, GPE0A_PME_B0_VIRT_GPIO_PIN);
0157 }
0158 
0159 static int int0002_probe(struct platform_device *pdev)
0160 {
0161     struct device *dev = &pdev->dev;
0162     struct int0002_data *int0002;
0163     struct gpio_irq_chip *girq;
0164     struct gpio_chip *chip;
0165     int irq, ret;
0166 
0167     /* Menlow has a different INT0002 device? <sigh> */
0168     if (!soc_intel_is_byt() && !soc_intel_is_cht())
0169         return -ENODEV;
0170 
0171     irq = platform_get_irq(pdev, 0);
0172     if (irq < 0)
0173         return irq;
0174 
0175     int0002 = devm_kzalloc(dev, sizeof(*int0002), GFP_KERNEL);
0176     if (!int0002)
0177         return -ENOMEM;
0178 
0179     int0002->parent_irq = irq;
0180 
0181     chip = &int0002->chip;
0182     chip->label = DRV_NAME;
0183     chip->parent = dev;
0184     chip->owner = THIS_MODULE;
0185     chip->get = int0002_gpio_get;
0186     chip->set = int0002_gpio_set;
0187     chip->direction_input = int0002_gpio_get;
0188     chip->direction_output = int0002_gpio_direction_output;
0189     chip->base = -1;
0190     chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1;
0191     chip->irq.init_valid_mask = int0002_init_irq_valid_mask;
0192 
0193     /*
0194      * We directly request the irq here instead of passing a flow-handler
0195      * to gpiochip_set_chained_irqchip, because the irq is shared.
0196      * FIXME: augment this if we managed to pull handling of shared
0197      * IRQs into gpiolib.
0198      */
0199     ret = devm_request_irq(dev, irq, int0002_irq,
0200                    IRQF_SHARED, "INT0002", chip);
0201     if (ret) {
0202         dev_err(dev, "Error requesting IRQ %d: %d\n", irq, ret);
0203         return ret;
0204     }
0205 
0206     girq = &chip->irq;
0207     girq->chip = &int0002_irqchip;
0208     /* This let us handle the parent IRQ in the driver */
0209     girq->parent_handler = NULL;
0210     girq->num_parents = 0;
0211     girq->parents = NULL;
0212     girq->default_type = IRQ_TYPE_NONE;
0213     girq->handler = handle_edge_irq;
0214 
0215     ret = devm_gpiochip_add_data(dev, chip, NULL);
0216     if (ret) {
0217         dev_err(dev, "Error adding gpio chip: %d\n", ret);
0218         return ret;
0219     }
0220 
0221     acpi_register_wakeup_handler(irq, int0002_check_wake, NULL);
0222     device_init_wakeup(dev, true);
0223     dev_set_drvdata(dev, int0002);
0224     return 0;
0225 }
0226 
0227 static int int0002_remove(struct platform_device *pdev)
0228 {
0229     device_init_wakeup(&pdev->dev, false);
0230     acpi_unregister_wakeup_handler(int0002_check_wake, NULL);
0231     return 0;
0232 }
0233 
0234 static int int0002_suspend(struct device *dev)
0235 {
0236     struct int0002_data *int0002 = dev_get_drvdata(dev);
0237 
0238     /*
0239      * The INT0002 parent IRQ is often shared with the ACPI GPE IRQ, don't
0240      * muck with it when firmware based suspend is used, otherwise we may
0241      * cause spurious wakeups from firmware managed suspend.
0242      */
0243     if (!pm_suspend_via_firmware() && int0002->wake_enable_count)
0244         enable_irq_wake(int0002->parent_irq);
0245 
0246     return 0;
0247 }
0248 
0249 static int int0002_resume(struct device *dev)
0250 {
0251     struct int0002_data *int0002 = dev_get_drvdata(dev);
0252 
0253     if (!pm_suspend_via_firmware() && int0002->wake_enable_count)
0254         disable_irq_wake(int0002->parent_irq);
0255 
0256     return 0;
0257 }
0258 
0259 static const struct dev_pm_ops int0002_pm_ops = {
0260     .suspend = int0002_suspend,
0261     .resume = int0002_resume,
0262 };
0263 
0264 static const struct acpi_device_id int0002_acpi_ids[] = {
0265     { "INT0002", 0 },
0266     { },
0267 };
0268 MODULE_DEVICE_TABLE(acpi, int0002_acpi_ids);
0269 
0270 static struct platform_driver int0002_driver = {
0271     .driver = {
0272         .name           = DRV_NAME,
0273         .acpi_match_table   = int0002_acpi_ids,
0274         .pm         = &int0002_pm_ops,
0275     },
0276     .probe  = int0002_probe,
0277     .remove = int0002_remove,
0278 };
0279 
0280 module_platform_driver(int0002_driver);
0281 
0282 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0283 MODULE_DESCRIPTION("Intel INT0002 Virtual GPIO driver");
0284 MODULE_LICENSE("GPL v2");