linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nuno Sá via B4 Relay" <devnull+nuno.sa.analog.com@kernel.org>
To: linux-gpio@vger.kernel.org, linux-pwm@vger.kernel.org,
	 devicetree@vger.kernel.org, linux-input@vger.kernel.org
Cc: "Lee Jones" <lee@kernel.org>, "Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Uwe Kleine-König" <ukleinek@kernel.org>,
	"Linus Walleij" <linus.walleij@linaro.org>,
	"Bartosz Golaszewski" <brgl@bgdev.pl>,
	"Dmitry Torokhov" <dmitry.torokhov@gmail.com>,
	"Laurent Pinchart" <laurent.pinchart@ideasonboard.com>,
	"Liu Ying" <victor.liu@nxp.com>
Subject: [PATCH 11/18] gpio: adp5585: support gpi events
Date: Thu, 13 Mar 2025 14:19:28 +0000	[thread overview]
Message-ID: <20250313-dev-adp5589-fw-v1-11-20e80d4bd4ea@analog.com> (raw)
In-Reply-To: <20250313-dev-adp5589-fw-v1-0-20e80d4bd4ea@analog.com>

From: Nuno Sá <nuno.sa@analog.com>

Add support for adding GPIs to the event FIFO. This is done by adding
irq_chip support. Like this, one can use the input gpio_keys driver as a
"frontend" device and input handler.

As part of this change, we now implement .init_valid_mask() as we can't
blindly consume all available pins as GPIOs (example: some pins can be
used for forming a keymap matrix).

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
---
 drivers/gpio/Kconfig        |   1 +
 drivers/gpio/gpio-adp5585.c | 178 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 179 insertions(+)

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 98b4d1633b258b93d05ba66d53f647e7ec6ac364..e8dc5cb9df0f62b111810d90ae67ce68b382d434 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -1238,6 +1238,7 @@ config GPIO_ADP5520
 config GPIO_ADP5585
 	tristate "GPIO Support for ADP5585"
 	depends on MFD_ADP5585
+	select GPIOLIB_IRQCHIP
 	help
 	  This option enables support for the GPIO function found in the Analog
 	  Devices ADP5585.
diff --git a/drivers/gpio/gpio-adp5585.c b/drivers/gpio/gpio-adp5585.c
index fded9ff35a59fc1a0ce173c58dbd7b6af176958f..cfd0aab1f7414f76287714eda44184c615491194 100644
--- a/drivers/gpio/gpio-adp5585.c
+++ b/drivers/gpio/gpio-adp5585.c
@@ -11,6 +11,7 @@
 #include <linux/gpio/driver.h>
 #include <linux/mfd/adp5585.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
 #include <linux/types.h>
@@ -27,6 +28,11 @@ struct adp5585_gpio_dev {
 	const struct adp5585_gpio_chip *info;
 	struct regmap *regmap;
 	const struct adp5585_regs *regs;
+	unsigned long irq_mask;
+	unsigned long irq_en;
+	unsigned long irq_active_high;
+	/* used for irqchip bus locking */
+	struct mutex bus_lock;
 };
 
 static int adp5585_gpio_bank(unsigned int off)
@@ -208,12 +214,155 @@ static int adp5585_gpio_set_config(struct gpio_chip *chip, unsigned int off,
 	};
 }
 
+static int adp5585_gpio_init_valid_mask(struct gpio_chip *chip,
+					unsigned long *valid_mask,
+					unsigned int ngpios)
+{
+	struct device *dev = chip->parent;
+	struct adp5585_dev *adp5585 = dev_get_drvdata(dev->parent);
+
+	bitmap_complement(valid_mask, adp5585->keypad, ngpios);
+	/*
+	 * the keypad won't have (nor can't) have any special pin enabled which
+	 * means bitmap_complement() will set them to 1. Make sure we clear them.
+	 */
+	if (adp5585->has_pwm)
+		__clear_bit(ADP5585_ROW3, valid_mask);
+	if (adp5585->nkeys_reset1)
+		__clear_bit(ADP5585_ROW4, valid_mask);
+	if (adp5585->nkeys_reset2)
+		__clear_bit(adp5585->info->max_rows + ADP5585_COL4, valid_mask);
+	if (!adp5585->info->has_row5)
+		__clear_bit(ADP5585_ROW5, valid_mask);
+
+	return 0;
+}
+
+static void adp5585_gpio_key_event(struct device *dev, unsigned int off,
+				   bool key_press)
+{
+	struct adp5585_gpio_dev *adp5585_gpio = dev_get_drvdata(dev);
+	bool active_high = test_bit(off, &adp5585_gpio->irq_active_high);
+	unsigned int irq, irq_type;
+	struct irq_data *irqd;
+
+	irq = irq_find_mapping(adp5585_gpio->gpio_chip.irq.domain, off);
+	if (!irq)
+		return;
+
+	irqd = irq_get_irq_data(irq);
+	if (!irqd) {
+		dev_err(dev, "Could not get irq(%u) data\n", irq);
+		return;
+	}
+
+	dev_dbg_ratelimited(dev, "gpio-keys event(%u) press=%u, a_high=%u\n",
+			    off, key_press, active_high);
+
+	if (!active_high)
+		key_press = !key_press;
+
+	irq_type = irqd_get_trigger_type(irqd);
+
+	if ((irq_type & IRQ_TYPE_EDGE_RISING && key_press) ||
+	    (irq_type & IRQ_TYPE_EDGE_FALLING && !key_press))
+		handle_nested_irq(irq);
+}
+
+static void adp5585_irq_bus_lock(struct irq_data *d)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+	struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(gc);
+
+	mutex_lock(&adp5585_gpio->bus_lock);
+}
+
+static void adp5585_irq_bus_sync_unlock(struct irq_data *d)
+{
+	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
+	struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(chip);
+	const struct adp5585_regs *regs = adp5585_gpio->regs;
+	irq_hw_number_t hwirq = irqd_to_hwirq(d);
+	bool active_high = test_bit(hwirq, &adp5585_gpio->irq_active_high);
+	bool enabled = test_bit(hwirq, &adp5585_gpio->irq_en);
+	bool masked = test_bit(hwirq, &adp5585_gpio->irq_mask);
+	unsigned int bank = adp5585_gpio->info->bank(hwirq);
+	unsigned int bit = adp5585_gpio->info->bit(hwirq);
+
+	if (masked && !enabled)
+		goto out_unlock;
+	if (!masked && enabled)
+		goto out_unlock;
+
+	regmap_update_bits(adp5585_gpio->regmap, regs->gpi_int_lvl_a + bank, bit,
+			   active_high ? bit : 0);
+	regmap_update_bits(adp5585_gpio->regmap, regs->gpi_ev_a + bank, bit,
+			   masked ? 0 : bit);
+	assign_bit(hwirq, &adp5585_gpio->irq_en, !masked);
+
+out_unlock:
+	mutex_unlock(&adp5585_gpio->bus_lock);
+}
+
+static void adp5585_irq_mask(struct irq_data *d)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+	struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(gc);
+	irq_hw_number_t hwirq = irqd_to_hwirq(d);
+
+	__set_bit(hwirq, &adp5585_gpio->irq_mask);
+	gpiochip_disable_irq(gc, hwirq);
+}
+
+static void adp5585_irq_unmask(struct irq_data *d)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+	struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(gc);
+	irq_hw_number_t hwirq = irqd_to_hwirq(d);
+
+	gpiochip_enable_irq(gc, hwirq);
+	__clear_bit(hwirq, &adp5585_gpio->irq_mask);
+}
+
+static int adp5585_irq_set_type(struct irq_data *d, unsigned int type)
+{
+	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+	struct adp5585_gpio_dev *adp5585_gpio = gpiochip_get_data(gc);
+	irq_hw_number_t hwirq = irqd_to_hwirq(d);
+
+	if (!(type & IRQ_TYPE_EDGE_BOTH))
+		return -EINVAL;
+
+	assign_bit(hwirq, &adp5585_gpio->irq_active_high,
+		   type == IRQ_TYPE_EDGE_RISING);
+
+	irq_set_handler_locked(d, handle_edge_irq);
+	return 0;
+}
+
+static const struct irq_chip adp5585_irq_chip = {
+	.name = "adp5585",
+	.irq_mask = adp5585_irq_mask,
+	.irq_unmask = adp5585_irq_unmask,
+	.irq_bus_lock = adp5585_irq_bus_lock,
+	.irq_bus_sync_unlock = adp5585_irq_bus_sync_unlock,
+	.irq_set_type = adp5585_irq_set_type,
+	.flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_IMMUTABLE,
+	GPIOCHIP_IRQ_RESOURCE_HELPERS,
+};
+
+static void adp5585_gpio_ev_handle_clean(void *adp5585)
+{
+	adp5585_gpio_ev_handle_set(adp5585, NULL, NULL);
+}
+
 static int adp5585_gpio_probe(struct platform_device *pdev)
 {
 	struct adp5585_dev *adp5585 = dev_get_drvdata(pdev->dev.parent);
 	const struct platform_device_id *id = platform_get_device_id(pdev);
 	struct adp5585_gpio_dev *adp5585_gpio;
 	struct device *dev = &pdev->dev;
+	struct gpio_irq_chip *girq;
 	struct gpio_chip *gc;
 	int ret;
 
@@ -238,6 +387,7 @@ static int adp5585_gpio_probe(struct platform_device *pdev)
 	gc->get = adp5585_gpio_get_value;
 	gc->set = adp5585_gpio_set_value;
 	gc->set_config = adp5585_gpio_set_config;
+	gc->init_valid_mask = adp5585_gpio_init_valid_mask;
 	gc->can_sleep = true;
 
 	gc->base = -1;
@@ -245,6 +395,34 @@ static int adp5585_gpio_probe(struct platform_device *pdev)
 	gc->label = pdev->name;
 	gc->owner = THIS_MODULE;
 
+	if (device_property_present(dev->parent, "interrupt-controller")) {
+		if (!adp5585->irq)
+			return dev_err_probe(dev, -EINVAL,
+					     "Unable to serve as interrupt controller without IRQ\n");
+
+		girq = &adp5585_gpio->gpio_chip.irq;
+		gpio_irq_chip_set_chip(girq, &adp5585_irq_chip);
+		girq->handler = handle_bad_irq;
+		girq->threaded = true;
+
+		platform_set_drvdata(pdev, adp5585_gpio);
+		adp5585_gpio_ev_handle_set(adp5585, adp5585_gpio_key_event,
+					   dev);
+
+		ret = devm_add_action_or_reset(dev,
+					       adp5585_gpio_ev_handle_clean,
+					       adp5585);
+		if (ret)
+			return ret;
+	}
+
+	/* everything masked by default */
+	adp5585_gpio->irq_mask = ~0UL;
+
+	ret = devm_mutex_init(dev, &adp5585_gpio->bus_lock);
+	if (ret)
+		return ret;
+
 	ret = devm_gpiochip_add_data(dev, &adp5585_gpio->gpio_chip,
 				     adp5585_gpio);
 	if (ret)

-- 
2.48.1



  parent reply	other threads:[~2025-03-13 14:19 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13 14:19 [PATCH 00/18] mfd: adp5585: support keymap events and drop legacy Input driver Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 01/18] dt-bindings: mfd: adp5585: ease on the required properties Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 02/18] mfd: adp5585: enable oscilator during probe Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 03/18] pwm: adp5585: don't control OSC_EN in the pwm driver Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 04/18] mfd: adp5585: make use of MFD_CELL_NAME() Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 05/18] dt-bindings: mfd: adp5585: document adp5589 I/O expander Nuno Sá via B4 Relay
2025-03-14  8:49   ` Krzysztof Kozlowski
2025-03-14  9:38     ` Nuno Sá
2025-03-17  7:41       ` Krzysztof Kozlowski
2025-03-17  9:30         ` Nuno Sá
2025-03-17 10:41           ` Krzysztof Kozlowski
2025-03-13 14:19 ` [PATCH 06/18] mfd: adp5585: add support for adp5589 Nuno Sá via B4 Relay
2025-03-14  9:12   ` kernel test robot
2025-03-14  9:23   ` kernel test robot
2025-03-14  9:32     ` Nuno Sá
2025-03-13 14:19 ` [PATCH 07/18] gpio: adp5585: add support for the ad5589 expander Nuno Sá via B4 Relay
2025-03-14 10:53   ` Linus Walleij
2025-03-13 14:19 ` [PATCH 08/18] pwm: adp5585: add support for adp5589 Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 09/18] dt-bindings: mfd: adp5585: add properties for input events Nuno Sá via B4 Relay
2025-03-17 15:53   ` Rob Herring
2025-03-17 16:08     ` Nuno Sá
2025-03-13 14:19 ` [PATCH 10/18] mfd: adp5585: add support for key events Nuno Sá via B4 Relay
2025-03-14  9:43   ` kernel test robot
2025-03-13 14:19 ` Nuno Sá via B4 Relay [this message]
2025-03-14 10:54   ` [PATCH 11/18] gpio: adp5585: support gpi events Linus Walleij
2025-03-13 14:19 ` [PATCH 12/18] Input: adp5585: Add Analog Devices ADP5585/89 support Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 13/18] Input: adp5589: remove the driver Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 14/18] mfd: adp5585: support getting vdd regulator Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 15/18] dt-bindings: mfd: adp5585: document reset gpio Nuno Sá via B4 Relay
2025-03-14  8:52   ` Krzysztof Kozlowski
2025-03-13 14:19 ` [PATCH 16/18] mfd: adp5585: add support for a reset pin Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 17/18] pwm: adp5585: make sure to include mod_devicetable.h Nuno Sá via B4 Relay
2025-03-13 14:19 ` [PATCH 18/18] gpio: " Nuno Sá via B4 Relay

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=20250313-dev-adp5589-fw-v1-11-20e80d4bd4ea@analog.com \
    --to=devnull+nuno.sa.analog.com@kernel.org \
    --cc=brgl@bgdev.pl \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=lee@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=ukleinek@kernel.org \
    --cc=victor.liu@nxp.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).