3ff7c26
From 3bbfe49a1d965b951527cde0da48f5d7677db264 Mon Sep 17 00:00:00 2001
3ff7c26
From: Hans de Goede <hdegoede@redhat.com>
3ff7c26
Date: Sun, 21 May 2017 13:15:11 +0200
3ff7c26
Subject: [PATCH 01/16] platform/x86: Add driver for ACPI INT0002 Virtual GPIO
3ff7c26
 device
3ff7c26
3ff7c26
Some peripherals on Bay Trail and Cherry Trail platforms signal a
3ff7c26
Power Management Event (PME) to the Power Management Controller (PMC)
3ff7c26
to wakeup the system. When this happens software needs to explicitly
3ff7c26
clear the PME bus 0 status bit in the GPE0a_STS register to avoid an
3ff7c26
IRQ storm on IRQ 9.
3ff7c26
3ff7c26
This is modelled in ACPI through the INT0002 ACPI device, which is
3ff7c26
called a "Virtual GPIO controller" in ACPI because it defines the
3ff7c26
event handler to call when the PME triggers through _AEI and _L02
3ff7c26
methods as would be done for a real GPIO interrupt in ACPI.
3ff7c26
3ff7c26
This commit adds a driver which registers the Virtual GPIOs expected
3ff7c26
by the DSDT on these devices, letting gpiolib-acpi claim the
3ff7c26
virtual GPIO and install a GPIO-interrupt handler which call the _L02
3ff7c26
handler as it would for a real GPIO controller.
3ff7c26
3ff7c26
Cc: joeyli <jlee@suse.com>
3ff7c26
Cc: Takashi Iwai <tiwai@suse.de>
3ff7c26
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
3ff7c26
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
3ff7c26
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
3ff7c26
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
3ff7c26
---
3ff7c26
Changes in v2:
3ff7c26
-Remove dev_err after malloc failure
3ff7c26
-Remove unused empty runtime pm callbacks
3ff7c26
-s/GPE0A_PME_/GPE0A_PME_B0_/
3ff7c26
-Fixed some checkpatch warnings (I forgot to run checkpatch on v1)
3ff7c26
3ff7c26
Changes in v3:
3ff7c26
-Rewrite as gpiochip driver letting gpiolib-acpi deal with claiming the pin
3ff7c26
 0x0002 and calling the _L02 event handler when the virtual gpio-irq triggers
3ff7c26
-Rebase on 4.12-rc1
3ff7c26
3ff7c26
Changes in v4:
3ff7c26
-Drop device_init_wakeup() from _probe(), use pm_system_wakeup() instead
3ff7c26
 of pm_wakeup_hard_event(chip->parent)
3ff7c26
-Improve commit message
3ff7c26
3ff7c26
Changes in v5:
3ff7c26
-Use BIT() macro for FOO_BIT defines
3ff7c26
-Drop unneeded ACPI_PTR macro usage
3ff7c26
3ff7c26
Changes in v6:
3ff7c26
-Move back to drivers/platform/x86
3ff7c26
-Expand certain acronyms (PME, PMC)
3ff7c26
-Use linux/gpio/driver.h include instead of linux/gpio.h
3ff7c26
-Document why the get / set / direction_output functions are dummys
3ff7c26
-No functional changes
3ff7c26
3ff7c26
Changes in v7:
3ff7c26
-Some minor cleanups from Andy:
3ff7c26
 -Move asm/ includes below linux/ includes
3ff7c26
 -s/APCI/ACPI/
3ff7c26
 -Use bitmap_clear on chip->irq_valid_mask
3ff7c26
-Add Linus Walleij's Reviewed-by
3ff7c26
---
3ff7c26
 drivers/platform/x86/Kconfig               |  19 +++
3ff7c26
 drivers/platform/x86/Makefile              |   1 +
3ff7c26
 drivers/platform/x86/intel_int0002_vgpio.c | 219 +++++++++++++++++++++++++++++
3ff7c26
 3 files changed, 239 insertions(+)
3ff7c26
 create mode 100644 drivers/platform/x86/intel_int0002_vgpio.c
3ff7c26
3ff7c26
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
3ff7c26
index 8489020ecf44..a3ccc3c795a5 100644
3ff7c26
--- a/drivers/platform/x86/Kconfig
3ff7c26
+++ b/drivers/platform/x86/Kconfig
3ff7c26
@@ -794,6 +794,25 @@ config INTEL_CHT_INT33FE
3ff7c26
 	  This driver instantiates i2c-clients for these, so that standard
3ff7c26
 	  i2c drivers for these chips can bind to the them.
3ff7c26
 
3ff7c26
+config INTEL_INT0002_VGPIO
3ff7c26
+	tristate "Intel ACPI INT0002 Virtual GPIO driver"
3ff7c26
+	depends on GPIOLIB && ACPI
3ff7c26
+	select GPIOLIB_IRQCHIP
3ff7c26
+	---help---
3ff7c26
+	  Some peripherals on Bay Trail and Cherry Trail platforms signal a
3ff7c26
+	  Power Management Event (PME) to the Power Management Controller (PMC)
3ff7c26
+	  to wakeup the system. When this happens software needs to explicitly
3ff7c26
+	  clear the PME bus 0 status bit in the GPE0a_STS register to avoid an
3ff7c26
+	  IRQ storm on IRQ 9.
3ff7c26
+
3ff7c26
+	  This is modelled in ACPI through the INT0002 ACPI device, which is
3ff7c26
+	  called a "Virtual GPIO controller" in ACPI because it defines the
3ff7c26
+	  event handler to call when the PME triggers through _AEI and _L02
3ff7c26
+	  methods as would be done for a real GPIO interrupt in ACPI.
3ff7c26
+
3ff7c26
+	  To compile this driver as a module, choose M here: the module will
3ff7c26
+	  be called intel_int0002_vgpio.
3ff7c26
+
3ff7c26
 config INTEL_HID_EVENT
3ff7c26
 	tristate "INTEL HID Event"
3ff7c26
 	depends on ACPI
3ff7c26
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
3ff7c26
index 182a3ed6605a..ab22ce77fb66 100644
3ff7c26
--- a/drivers/platform/x86/Makefile
3ff7c26
+++ b/drivers/platform/x86/Makefile
3ff7c26
@@ -46,6 +46,7 @@ obj-$(CONFIG_TOSHIBA_BT_RFKILL)	+= toshiba_bluetooth.o
3ff7c26
 obj-$(CONFIG_TOSHIBA_HAPS)	+= toshiba_haps.o
3ff7c26
 obj-$(CONFIG_TOSHIBA_WMI)	+= toshiba-wmi.o
3ff7c26
 obj-$(CONFIG_INTEL_CHT_INT33FE)	+= intel_cht_int33fe.o
3ff7c26
+obj-$(CONFIG_INTEL_INT0002_VGPIO) += intel_int0002_vgpio.o
3ff7c26
 obj-$(CONFIG_INTEL_HID_EVENT)	+= intel-hid.o
3ff7c26
 obj-$(CONFIG_INTEL_VBTN)	+= intel-vbtn.o
3ff7c26
 obj-$(CONFIG_INTEL_SCU_IPC)	+= intel_scu_ipc.o
3ff7c26
diff --git a/drivers/platform/x86/intel_int0002_vgpio.c b/drivers/platform/x86/intel_int0002_vgpio.c
3ff7c26
new file mode 100644
3ff7c26
index 000000000000..92dc230ef5b2
3ff7c26
--- /dev/null
3ff7c26
+++ b/drivers/platform/x86/intel_int0002_vgpio.c
3ff7c26
@@ -0,0 +1,219 @@
3ff7c26
+/*
3ff7c26
+ * Intel INT0002 "Virtual GPIO" driver
3ff7c26
+ *
3ff7c26
+ * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
3ff7c26
+ *
3ff7c26
+ * Loosely based on android x86 kernel code which is:
3ff7c26
+ *
3ff7c26
+ * Copyright (c) 2014, Intel Corporation.
3ff7c26
+ *
3ff7c26
+ * Author: Dyut Kumar Sil <dyut.k.sil@intel.com>
3ff7c26
+ *
3ff7c26
+ * This program is free software; you can redistribute it and/or modify
3ff7c26
+ * it under the terms of the GNU General Public License version 2 as
3ff7c26
+ * published by the Free Software Foundation.
3ff7c26
+ *
3ff7c26
+ * Some peripherals on Bay Trail and Cherry Trail platforms signal a Power
3ff7c26
+ * Management Event (PME) to the Power Management Controller (PMC) to wakeup
3ff7c26
+ * the system. When this happens software needs to clear the PME bus 0 status
3ff7c26
+ * bit in the GPE0a_STS register to avoid an IRQ storm on IRQ 9.
3ff7c26
+ *
3ff7c26
+ * This is modelled in ACPI through the INT0002 ACPI device, which is
3ff7c26
+ * called a "Virtual GPIO controller" in ACPI because it defines the event
3ff7c26
+ * handler to call when the PME triggers through _AEI and _L02 / _E02
3ff7c26
+ * methods as would be done for a real GPIO interrupt in ACPI. Note this
3ff7c26
+ * is a hack to define an AML event handler for the PME while using existing
3ff7c26
+ * ACPI mechanisms, this is not a real GPIO at all.
3ff7c26
+ *
3ff7c26
+ * This driver will bind to the INT0002 device, and register as a GPIO
3ff7c26
+ * controller, letting gpiolib-acpi.c call the _L02 handler as it would
3ff7c26
+ * for a real GPIO controller.
3ff7c26
+ */
3ff7c26
+
3ff7c26
+#include <linux/acpi.h>
3ff7c26
+#include <linux/bitmap.h>
3ff7c26
+#include <linux/gpio/driver.h>
3ff7c26
+#include <linux/interrupt.h>
3ff7c26
+#include <linux/io.h>
3ff7c26
+#include <linux/kernel.h>
3ff7c26
+#include <linux/module.h>
3ff7c26
+#include <linux/platform_device.h>
3ff7c26
+#include <linux/slab.h>
3ff7c26
+#include <linux/suspend.h>
3ff7c26
+
3ff7c26
+#include <asm/cpu_device_id.h>
3ff7c26
+#include <asm/intel-family.h>
3ff7c26
+
3ff7c26
+#define DRV_NAME			"INT0002 Virtual GPIO"
3ff7c26
+
3ff7c26
+/* For some reason the virtual GPIO pin tied to the GPE is numbered pin 2 */
3ff7c26
+#define GPE0A_PME_B0_VIRT_GPIO_PIN	2
3ff7c26
+
3ff7c26
+#define GPE0A_PME_B0_STS_BIT		BIT(13)
3ff7c26
+#define GPE0A_PME_B0_EN_BIT		BIT(13)
3ff7c26
+#define GPE0A_STS_PORT			0x420
3ff7c26
+#define GPE0A_EN_PORT			0x428
3ff7c26
+
3ff7c26
+#define ICPU(model)	{ X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, }
3ff7c26
+
3ff7c26
+static const struct x86_cpu_id int0002_cpu_ids[] = {
3ff7c26
+/*
3ff7c26
+ * Limit ourselves to Cherry Trail for now, until testing shows we
3ff7c26
+ * need to handle the INT0002 device on Baytrail too.
3ff7c26
+ *	ICPU(INTEL_FAM6_ATOM_SILVERMONT1),	 * Valleyview, Bay Trail *
3ff7c26
+ */
3ff7c26
+	ICPU(INTEL_FAM6_ATOM_AIRMONT),		/* Braswell, Cherry Trail */
3ff7c26
+	{}
3ff7c26
+};
3ff7c26
+
3ff7c26
+/*
3ff7c26
+ * As this is not a real GPIO at all, but just a hack to model an event in
3ff7c26
+ * ACPI the get / set functions are dummy functions.
3ff7c26
+ */
3ff7c26
+
3ff7c26
+static int int0002_gpio_get(struct gpio_chip *chip, unsigned int offset)
3ff7c26
+{
3ff7c26
+	return 0;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static void int0002_gpio_set(struct gpio_chip *chip, unsigned int offset,
3ff7c26
+			     int value)
3ff7c26
+{
3ff7c26
+}
3ff7c26
+
3ff7c26
+static int int0002_gpio_direction_output(struct gpio_chip *chip,
3ff7c26
+					 unsigned int offset, int value)
3ff7c26
+{
3ff7c26
+	return 0;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static void int0002_irq_ack(struct irq_data *data)
3ff7c26
+{
3ff7c26
+	outl(GPE0A_PME_B0_STS_BIT, GPE0A_STS_PORT);
3ff7c26
+}
3ff7c26
+
3ff7c26
+static void int0002_irq_unmask(struct irq_data *data)
3ff7c26
+{
3ff7c26
+	u32 gpe_en_reg;
3ff7c26
+
3ff7c26
+	gpe_en_reg = inl(GPE0A_EN_PORT);
3ff7c26
+	gpe_en_reg |= GPE0A_PME_B0_EN_BIT;
3ff7c26
+	outl(gpe_en_reg, GPE0A_EN_PORT);
3ff7c26
+}
3ff7c26
+
3ff7c26
+static void int0002_irq_mask(struct irq_data *data)
3ff7c26
+{
3ff7c26
+	u32 gpe_en_reg;
3ff7c26
+
3ff7c26
+	gpe_en_reg = inl(GPE0A_EN_PORT);
3ff7c26
+	gpe_en_reg &= ~GPE0A_PME_B0_EN_BIT;
3ff7c26
+	outl(gpe_en_reg, GPE0A_EN_PORT);
3ff7c26
+}
3ff7c26
+
3ff7c26
+static irqreturn_t int0002_irq(int irq, void *data)
3ff7c26
+{
3ff7c26
+	struct gpio_chip *chip = data;
3ff7c26
+	u32 gpe_sts_reg;
3ff7c26
+
3ff7c26
+	gpe_sts_reg = inl(GPE0A_STS_PORT);
3ff7c26
+	if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT))
3ff7c26
+		return IRQ_NONE;
3ff7c26
+
3ff7c26
+	generic_handle_irq(irq_find_mapping(chip->irqdomain,
3ff7c26
+					    GPE0A_PME_B0_VIRT_GPIO_PIN));
3ff7c26
+
3ff7c26
+	pm_system_wakeup();
3ff7c26
+
3ff7c26
+	return IRQ_HANDLED;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static struct irq_chip int0002_irqchip = {
3ff7c26
+	.name			= DRV_NAME,
3ff7c26
+	.irq_ack		= int0002_irq_ack,
3ff7c26
+	.irq_mask		= int0002_irq_mask,
3ff7c26
+	.irq_unmask		= int0002_irq_unmask,
3ff7c26
+};
3ff7c26
+
3ff7c26
+static int int0002_probe(struct platform_device *pdev)
3ff7c26
+{
3ff7c26
+	struct device *dev = &pdev->dev;
3ff7c26
+	const struct x86_cpu_id *cpu_id;
3ff7c26
+	struct gpio_chip *chip;
3ff7c26
+	int irq, ret;
3ff7c26
+
3ff7c26
+	/* Menlow has a different INT0002 device? <sigh> */
3ff7c26
+	cpu_id = x86_match_cpu(int0002_cpu_ids);
3ff7c26
+	if (!cpu_id)
3ff7c26
+		return -ENODEV;
3ff7c26
+
3ff7c26
+	irq = platform_get_irq(pdev, 0);
3ff7c26
+	if (irq < 0) {
3ff7c26
+		dev_err(dev, "Error getting IRQ: %d\n", irq);
3ff7c26
+		return irq;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3ff7c26
+	if (!chip)
3ff7c26
+		return -ENOMEM;
3ff7c26
+
3ff7c26
+	chip->label = DRV_NAME;
3ff7c26
+	chip->parent = dev;
3ff7c26
+	chip->owner = THIS_MODULE;
3ff7c26
+	chip->get = int0002_gpio_get;
3ff7c26
+	chip->set = int0002_gpio_set;
3ff7c26
+	chip->direction_input = int0002_gpio_get;
3ff7c26
+	chip->direction_output = int0002_gpio_direction_output;
3ff7c26
+	chip->base = -1;
3ff7c26
+	chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1;
3ff7c26
+	chip->irq_need_valid_mask = true;
3ff7c26
+
3ff7c26
+	ret = devm_gpiochip_add_data(&pdev->dev, chip, NULL);
3ff7c26
+	if (ret) {
3ff7c26
+		dev_err(dev, "Error adding gpio chip: %d\n", ret);
3ff7c26
+		return ret;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	bitmap_clear(chip->irq_valid_mask, 0, GPE0A_PME_B0_VIRT_GPIO_PIN);
3ff7c26
+
3ff7c26
+	/*
3ff7c26
+	 * We manually request the irq here instead of passing a flow-handler
3ff7c26
+	 * to gpiochip_set_chained_irqchip, because the irq is shared.
3ff7c26
+	 */
3ff7c26
+	ret = devm_request_irq(dev, irq, int0002_irq,
3ff7c26
+			       IRQF_SHARED | IRQF_NO_THREAD, "INT0002", chip);
3ff7c26
+	if (ret) {
3ff7c26
+		dev_err(dev, "Error requesting IRQ %d: %d\n", irq, ret);
3ff7c26
+		return ret;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	ret = gpiochip_irqchip_add(chip, &int0002_irqchip, 0, handle_edge_irq,
3ff7c26
+				   IRQ_TYPE_NONE);
3ff7c26
+	if (ret) {
3ff7c26
+		dev_err(dev, "Error adding irqchip: %d\n", ret);
3ff7c26
+		return ret;
3ff7c26
+	}
3ff7c26
+
3ff7c26
+	gpiochip_set_chained_irqchip(chip, &int0002_irqchip, irq, NULL);
3ff7c26
+
3ff7c26
+	return 0;
3ff7c26
+}
3ff7c26
+
3ff7c26
+static const struct acpi_device_id int0002_acpi_ids[] = {
3ff7c26
+	{ "INT0002", 0 },
3ff7c26
+	{ },
3ff7c26
+};
3ff7c26
+MODULE_DEVICE_TABLE(acpi, int0002_acpi_ids);
3ff7c26
+
3ff7c26
+static struct platform_driver int0002_driver = {
3ff7c26
+	.driver = {
3ff7c26
+		.name			= DRV_NAME,
3ff7c26
+		.acpi_match_table	= int0002_acpi_ids,
3ff7c26
+	},
3ff7c26
+	.probe	= int0002_probe,
3ff7c26
+};
3ff7c26
+
3ff7c26
+module_platform_driver(int0002_driver);
3ff7c26
+
3ff7c26
+MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
3ff7c26
+MODULE_DESCRIPTION("Intel INT0002 Virtual GPIO driver");
3ff7c26
+MODULE_LICENSE("GPL");
3ff7c26
-- 
3ff7c26
2.13.0
3ff7c26