All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hans de Goede <hdegoede@redhat.com>
To: Mark Gross <markgross@kernel.org>, Andy Shevchenko <andy@infradead.org>
Cc: Hans de Goede <hdegoede@redhat.com>, platform-driver-x86@vger.kernel.org
Subject: [PATCH v2 1/2] platform/x86: Add x86-acpi-irq-helpers.h
Date: Tue, 21 Dec 2021 16:12:42 +0100	[thread overview]
Message-ID: <20211221151243.66216-1-hdegoede@redhat.com> (raw)

Add helper code to get Linux IRQ numbers given a description of the IRQ
source (either IOAPIC index, or GPIO chip name + pin-number).

This is intended to be used to lookup Linux IRQ numbers in cases where the
ACPI description for a device somehow lacks this info. This is only meant
for use on x86 ACPI platforms.

This code is big/complex enough to warrant sharing, but too small to live
in its own module, therefor x86_acpi_irq_helper_get() is defined as
a static inline helper function.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
Changes in v2:
- New patch in v2 of this patch-set
---
 drivers/platform/x86/x86-acpi-irq-helpers.h | 82 +++++++++++++++++++++
 1 file changed, 82 insertions(+)
 create mode 100644 drivers/platform/x86/x86-acpi-irq-helpers.h

diff --git a/drivers/platform/x86/x86-acpi-irq-helpers.h b/drivers/platform/x86/x86-acpi-irq-helpers.h
new file mode 100644
index 000000000000..2b3c02c47563
--- /dev/null
+++ b/drivers/platform/x86/x86-acpi-irq-helpers.h
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Helper code to get Linux IRQ numbers given a description of the IRQ source
+ * (either IOAPIC index, or GPIO chip name + pin-number).
+ *
+ * This is intended to be used to lookup Linux IRQ numbers in cases where the
+ * ACPI description for a device somehow lacks this info. This is only meant
+ * for use on x86 ACPI platforms.
+ */
+
+#include <linux/acpi.h>
+#include <linux/irq.h>
+#include <linux/string.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+
+/* For gpio_get_desc which is EXPORT_SYMBOL_GPL() */
+#include "../../gpio/gpiolib.h"
+
+enum x86_acpi_irq_type {
+	X86_ACPI_IRQ_TYPE_NONE,
+	X86_ACPI_IRQ_TYPE_APIC,
+	X86_ACPI_IRQ_TYPE_GPIOINT,
+};
+
+struct x86_acpi_irq_data {
+	char *gpio_chip; /* GPIO chip label for X86_ACPI_IRQ_TYPE_GPIOINT */
+	enum x86_acpi_irq_type type;
+	int index;
+	int trigger;  /* ACPI_EDGE_SENSITIVE / ACPI_LEVEL_SENSITIVE */
+	int polarity; /* ACPI_ACTIVE_HIGH / ACPI_ACTIVE_LOW / ACPI_ACTIVE_BOTH */
+};
+
+static int x86_acpi_irq_helper_gpiochip_find(struct gpio_chip *gc, void *data)
+{
+	return gc->label && !strcmp(gc->label, data);
+}
+
+static inline int x86_acpi_irq_helper_get(const struct x86_acpi_irq_data *data)
+{
+	struct gpio_desc *gpiod;
+	struct gpio_chip *chip;
+	unsigned int irq_type;
+	int irq, ret;
+
+	switch (data->type) {
+	case X86_ACPI_IRQ_TYPE_APIC:
+		irq = acpi_register_gsi(NULL, data->index, data->trigger, data->polarity);
+		if (irq < 0)
+			pr_err("error %d getting APIC IRQ %d\n", irq, data->index);
+
+		return irq;
+	case X86_ACPI_IRQ_TYPE_GPIOINT:
+		/* Like acpi_dev_gpio_irq_get(), but without parsing ACPI resources */
+		chip = gpiochip_find(data->gpio_chip, x86_acpi_irq_helper_gpiochip_find);
+		if (!chip)
+			return -EPROBE_DEFER;
+
+		gpiod = gpiochip_get_desc(chip, data->index);
+		if (IS_ERR(gpiod)) {
+			ret = PTR_ERR(gpiod);
+			pr_err("error %d getting GPIO %s %d\n", ret,
+			       data->gpio_chip, data->index);
+			return ret;
+		}
+
+		irq = gpiod_to_irq(gpiod);
+		if (irq < 0) {
+			pr_err("error %d getting IRQ %s %d\n", irq,
+			       data->gpio_chip, data->index);
+			return irq;
+		}
+
+		irq_type = acpi_dev_get_irq_type(data->trigger, data->polarity);
+		if (irq_type != IRQ_TYPE_NONE && irq_type != irq_get_trigger_type(irq))
+			irq_set_irq_type(irq, irq_type);
+
+		return irq;
+	default:
+		return 0;
+	}
+}
-- 
2.33.1


             reply	other threads:[~2021-12-21 15:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-21 15:12 Hans de Goede [this message]
2021-12-21 15:12 ` [PATCH v2 2/2] platform/x86: x86-android-tablets: New driver for x86 Android tablets Hans de Goede
2021-12-21 15:39   ` Andy Shevchenko
2021-12-21 19:05     ` Hans de Goede
2021-12-21 19:12       ` Hans de Goede
2021-12-21 15:27 ` [PATCH v2 1/2] platform/x86: Add x86-acpi-irq-helpers.h Andy Shevchenko
2021-12-21 18:58   ` Hans de Goede
2021-12-22 12:45     ` Mika Westerberg
2021-12-22 12:49       ` Andy Shevchenko
2021-12-23 17:26       ` Hans de Goede

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211221151243.66216-1-hdegoede@redhat.com \
    --to=hdegoede@redhat.com \
    --cc=andy@infradead.org \
    --cc=markgross@kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.