mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Laurentiu Mihalcea <laurentiumihalcea111@gmail.com>
To: Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Charles Keepax <ckeepax@opensource.cirrus.com>,
	Sebastian Krzyszkowiak <sebastian.krzyszkowiak@puri.sm>,
	elena.popa@nxp.com, cosmin.stoica@nxp.com
Cc: patches@opensource.cirrus.com, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support
Date: Fri, 27 Nov 2026 19:00:38 +0200	[thread overview]
Message-ID: <20261127170039.663-3-laurentiumihalcea111@gmail.com> (raw)
In-Reply-To: <20261127170039.663-1-laurentiumihalcea111@gmail.com>

From: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>

The WM8962 codec provides two GPIO pins: GPIO5/GPIO6 which can act as
interrupt sources. Therefore, in the context of Linux, GPIO consumer
devices (e.g. in this case, the sound card), could make use of these two
pins to handle certain events (e.g. HP detection).

Add support for this by modelling the WM8962 codec as a GPIO interrupt
controller.

Signed-off-by: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
---
 sound/soc/codecs/wm8962.c | 132 +++++++++++++++++++++++++++++++++-----
 1 file changed, 115 insertions(+), 17 deletions(-)

diff --git a/sound/soc/codecs/wm8962.c b/sound/soc/codecs/wm8962.c
index 7122d93a742c..3bbaacb08aaf 100644
--- a/sound/soc/codecs/wm8962.c
+++ b/sound/soc/codecs/wm8962.c
@@ -3141,13 +3141,34 @@ static void wm8962_mic_work(struct work_struct *work)
 			    WM8962_MICD_IRQ_POL, irq_pol);
 }
 
+static int wm8962_irq_status_masked(struct wm8962_priv *wm8962,
+				    unsigned int status_reg,
+				    unsigned int mask_reg,
+				    unsigned int *status)
+{
+	int ret;
+	unsigned int mask;
+
+	ret = regmap_read(wm8962->regmap, mask_reg, &mask);
+	if (ret != 0)
+		return ret;
+
+	ret = regmap_read(wm8962->regmap, status_reg, status);
+	if (ret != 0)
+		return ret;
+
+	*status &= ~mask;
+
+	return 0;
+}
+
 static irqreturn_t wm8962_irq(int irq, void *data)
 {
 	struct device *dev = data;
 	struct wm8962_priv *wm8962 = dev_get_drvdata(dev);
-	unsigned int mask;
-	unsigned int active;
-	int reg, ret;
+	unsigned int active_1, active_2;
+	struct gpio_chip *gc = &wm8962->gpio_chip;
+	int reg, ret, nirq;
 
 	ret = pm_runtime_resume_and_get(dev);
 	if (ret < 0) {
@@ -3155,43 +3176,51 @@ static irqreturn_t wm8962_irq(int irq, void *data)
 		return IRQ_NONE;
 	}
 
-	ret = regmap_read(wm8962->regmap, WM8962_INTERRUPT_STATUS_2_MASK,
-			  &mask);
+	ret = wm8962_irq_status_masked(wm8962,
+				       WM8962_INTERRUPT_STATUS_1,
+				       WM8962_INTERRUPT_STATUS_1_MASK,
+				       &active_1);
 	if (ret != 0) {
 		pm_runtime_put(dev);
-		dev_err(dev, "Failed to read interrupt mask: %d\n",
+		dev_err(dev, "Failed to read interrupt 1: %d\n",
 			ret);
 		return IRQ_NONE;
 	}
 
-	ret = regmap_read(wm8962->regmap, WM8962_INTERRUPT_STATUS_2, &active);
+	ret = wm8962_irq_status_masked(wm8962,
+				       WM8962_INTERRUPT_STATUS_2,
+				       WM8962_INTERRUPT_STATUS_2_MASK,
+				       &active_2);
 	if (ret != 0) {
 		pm_runtime_put(dev);
-		dev_err(dev, "Failed to read interrupt: %d\n", ret);
+		dev_err(dev, "Failed to read interrupt 2: %d\n",
+			ret);
 		return IRQ_NONE;
 	}
 
-	active &= ~mask;
-
-	if (!active) {
+	if (!active_1 && !active_2) {
 		pm_runtime_put(dev);
 		return IRQ_NONE;
 	}
 
 	/* Acknowledge the interrupts */
-	ret = regmap_write(wm8962->regmap, WM8962_INTERRUPT_STATUS_2, active);
+	ret = regmap_write(wm8962->regmap, WM8962_INTERRUPT_STATUS_1, active_1);
+	if (ret != 0)
+		dev_warn(dev, "Failed to ack interrupt 1: %d\n", ret);
+
+	ret = regmap_write(wm8962->regmap, WM8962_INTERRUPT_STATUS_2, active_2);
 	if (ret != 0)
-		dev_warn(dev, "Failed to ack interrupt: %d\n", ret);
+		dev_warn(dev, "Failed to ack interrupt 2: %d\n", ret);
 
-	if (active & WM8962_FLL_LOCK_EINT) {
+	if (active_2 & WM8962_FLL_LOCK_EINT) {
 		dev_dbg(dev, "FLL locked\n");
 		complete(&wm8962->fll_lock);
 	}
 
-	if (active & WM8962_FIFOS_ERR_EINT)
+	if (active_2 & WM8962_FIFOS_ERR_EINT)
 		dev_err(dev, "FIFO error\n");
 
-	if (active & WM8962_TEMP_SHUT_EINT) {
+	if (active_2 & WM8962_TEMP_SHUT_EINT) {
 		dev_crit(dev, "Thermal shutdown\n");
 
 		ret = regmap_read(wm8962->regmap,
@@ -3212,7 +3241,7 @@ static irqreturn_t wm8962_irq(int irq, void *data)
 			dev_crit(dev, "Speaker thermal warning\n");
 	}
 
-	if (active & (WM8962_MICSCD_EINT | WM8962_MICD_EINT)) {
+	if (active_2 & (WM8962_MICSCD_EINT | WM8962_MICD_EINT)) {
 		dev_dbg(dev, "Microphone event detected\n");
 
 #ifndef CONFIG_SND_SOC_WM8962_MODULE
@@ -3226,6 +3255,22 @@ static irqreturn_t wm8962_irq(int irq, void *data)
 				   msecs_to_jiffies(250));
 	}
 
+	if (active_1 & WM8962_GP5_EINT) {
+		nirq = irq_find_mapping(gc->irq.domain, 4);
+		if (likely(nirq > 0))
+			handle_nested_irq(nirq);
+		else
+			dev_warn(dev, "Unmapped GP5 interrupt\n");
+	}
+
+	if (active_1 & WM8962_GP6_EINT) {
+		nirq = irq_find_mapping(gc->irq.domain, 5);
+		if (likely(nirq > 0))
+			handle_nested_irq(nirq);
+		else
+			dev_warn(dev, "Unmapped GP6 interrupt\n");
+	}
+
 	pm_runtime_put(dev);
 
 	return IRQ_HANDLED;
@@ -3555,6 +3600,18 @@ static int wm8962_gpio_get(struct gpio_chip *chip, unsigned int offset)
 	return (val & WM8962_GP5_LVL) >> WM8962_GP5_LVL_SHIFT;
 }
 
+static int wm8962_init_valid_mask(struct gpio_chip *chip,
+				  unsigned long *valid_mask,
+				  unsigned int ngpios)
+{
+	bitmap_zero(valid_mask, ngpios);
+
+	/* only GPIO5/GPIO6 can be used as interrupt sources! */
+	bitmap_set(valid_mask, 4, 2);
+
+	return 0;
+}
+
 static const struct gpio_chip wm8962_template_chip = {
 	.label			= "wm8962",
 	.owner			= THIS_MODULE,
@@ -3562,13 +3619,42 @@ static const struct gpio_chip wm8962_template_chip = {
 	.direction_output	= wm8962_gpio_direction_out,
 	.set			= wm8962_gpio_set,
 	.get			= wm8962_gpio_get,
+	.init_valid_mask	= wm8962_init_valid_mask,
 	.can_sleep		= 1,
 };
 
+/*
+ * TODO: implement mask()/unmask()/bus_sync() if required. At the moment, we
+ * operate under the assumption that the pins' function is set to IRQ using
+ * the "gpio-cfg" DT property. The interrupts are unmasked regardless of the
+ * pins' function!
+ */
+static const struct irq_chip wm8962_irq_chip = {
+	.flags			= IRQCHIP_IMMUTABLE,
+	GPIOCHIP_IRQ_RESOURCE_HELPERS,
+};
+
+static void wm8962_init_gpio_irq(struct wm8962_priv *wm8962)
+{
+	unsigned int val = ~(WM8962_IM_GP5_EINT | WM8962_IM_GP6_EINT);
+	unsigned int mask = WM8962_IM_GP5_EINT | WM8962_IM_GP6_EINT;
+
+	if (!(wm8962->pdata.gpio_init[4] & WM8962_GPIO_FN_IRQ))
+		val |= WM8962_IM_GP5_EINT;
+
+	if (!(wm8962->pdata.gpio_init[5] & WM8962_GPIO_FN_IRQ))
+		val |= WM8962_IM_GP6_EINT;
+
+	regmap_update_bits(wm8962->regmap,
+			   WM8962_INTERRUPT_STATUS_1_MASK,
+			   mask, val);
+}
+
 static void wm8962_init_gpio(struct snd_soc_component *component)
 {
 	struct wm8962_priv *wm8962 = snd_soc_component_get_drvdata(component);
 	struct wm8962_pdata *pdata = &wm8962->pdata;
+	struct gpio_irq_chip *girq;
 	int ret;
 
 	wm8962->gpio_chip = wm8962_template_chip;
@@ -3580,6 +3666,11 @@ static void wm8962_init_gpio(struct snd_soc_component *component)
 	else
 		wm8962->gpio_chip.base = -1;
 
+	girq = &wm8962->gpio_chip.irq;
+	girq->threaded = true;
+
+	gpio_irq_chip_set_chip(girq, &wm8962_irq_chip);
+
 	ret = gpiochip_add_data(&wm8962->gpio_chip, wm8962);
 	if (ret != 0)
 		dev_err(component->dev, "Failed to add GPIOs: %d\n", ret);
@@ -3599,6 +3690,10 @@ static void wm8962_init_gpio(struct snd_soc_component *component)
 static void wm8962_free_gpio(struct snd_soc_component *component)
 {
 }
+
+static void wm8962_init_gpio_irq(struct wm8962_priv *wm8962)
+{
+}
 #endif
 
 static int wm8962_probe(struct snd_soc_component *component)
@@ -3940,6 +4035,9 @@ static int wm8962_i2c_probe(struct i2c_client *i2c)
 					   WM8962_FLL_LOCK_EINT |
 					   WM8962_TEMP_SHUT_EINT |
 					   WM8962_FIFOS_ERR_EINT, 0);
+
+			/* enable GPIO IRQs if need be */
+			wm8962_init_gpio_irq(wm8962);
 		}
 	}
 
-- 
2.53.0


  parent reply	other threads:[~2026-11-27 17:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 17:00 [PATCH 0/3] ASoC: wm8962: make GPIO-based HP detection work Laurentiu Mihalcea
2026-11-27 17:00 ` [PATCH 1/3] ASoC: wm8962: implement GPIO .get callback Laurentiu Mihalcea
2026-11-27 17:00 ` Laurentiu Mihalcea [this message]
2026-11-27 17:00 ` [PATCH 3/3] ASoC: wm8962: fix interrupt mis-firing during runtime_resume() Laurentiu Mihalcea

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=20261127170039.663-3-laurentiumihalcea111@gmail.com \
    --to=laurentiumihalcea111@gmail.com \
    --cc=broonie@kernel.org \
    --cc=ckeepax@opensource.cirrus.com \
    --cc=cosmin.stoica@nxp.com \
    --cc=elena.popa@nxp.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=patches@opensource.cirrus.com \
    --cc=sebastian.krzyszkowiak@puri.sm \
    /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

all inboxes | Powered by JetHome®