* [PATCH 0/3] ASoC: wm8962: make GPIO-based HP detection work
@ 2026-11-27 17:00 Laurentiu Mihalcea
2026-11-27 17:00 ` [PATCH 1/3] ASoC: wm8962: implement GPIO .get callback Laurentiu Mihalcea
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Laurentiu Mihalcea @ 2026-11-27 17:00 UTC (permalink / raw)
To: Liam Girdwood, Mark Brown, Charles Keepax, Sebastian Krzyszkowiak,
elena.popa, cosmin.stoica
Cc: patches, linux-sound, linux-kernel
From: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
Hello everyone,
This series contains a little bit of a mix of patches (2 features and 1
bug fix) that are needed to (partially) make the headphone detection
using the GPIO2 and GPIO5 pins work on NXP's MR-NAVQ95 board.
A little bit about the setup:
I'm using a MR-NAVQ95 board. This uses the codec's GPIO5 pin to detect
if the headphones are plugged into the jack and the GPIO2 pin to signal
if a codec interrupt has occurred.
Also, the WM8962 codec has an external XTAL tied to the XTI/MCLK pins and
is configured to use the FLL as the MCLK source.
For this to work, I've confiured GPIO5 as a GPIO input and GPIO2 to
operate in IRQ mode (i.e. IRQ function) using the "gpio-cfg" DT property.
About the patches:
1) ASoC: wm8962: implement GPIO .get callback
- needed because the soc-jack core needs to be able to read the value of
the headphone detection pin (i.e. GPIO5).
2) ASoC: wm8962: add GPIO interrupt controller support
- needed because the soc-jack core needs to be notified whenever an IRQ
was raised by the codec (which would happen if GPIO5 changes its state).
3) ASoC: wm8962: fix interrupt mis-firing during runtime_resume()
- fixes an annoying issue that would cause some unwanted transitions on
the codec's IRQ line. This would sometimes make the downstream IRQ
controller think an interrupt was raised by the codec.
Apart from this, there's still an issue with the FLL lock interrupt, which
I haven't managed to get working right yet. What I've tried is enabling
SYSCLK after configuring and enabling the FLL but this doesn't seem to do
the trick all the time since there's still some cases in which the IRQ
doesn't fire. The datasheet doesn't seem to specify the conditions under
which said interrupt should fire?
Laurentiu Mihalcea (3):
ASoC: wm8962: implement GPIO .get callback
ASoC: wm8962: add GPIO interrupt controller support
ASoC: wm8962: fix interrupt mis-firing during runtime_resume()
sound/soc/codecs/wm8962.c | 201 ++++++++++++++++++++++++++++++++++----
1 file changed, 183 insertions(+), 18 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/3] ASoC: wm8962: implement GPIO .get callback
2026-11-27 17:00 [PATCH 0/3] ASoC: wm8962: make GPIO-based HP detection work Laurentiu Mihalcea
@ 2026-11-27 17:00 ` Laurentiu Mihalcea
2026-11-27 17:22 ` sashiko-bot
2026-11-27 17:00 ` [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support Laurentiu Mihalcea
2026-11-27 17:00 ` [PATCH 3/3] ASoC: wm8962: fix interrupt mis-firing during runtime_resume() Laurentiu Mihalcea
2 siblings, 1 reply; 7+ messages in thread
From: Laurentiu Mihalcea @ 2026-11-27 17:00 UTC (permalink / raw)
To: Liam Girdwood, Mark Brown, Charles Keepax, Sebastian Krzyszkowiak,
elena.popa, cosmin.stoica
Cc: patches, linux-sound, linux-kernel
From: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
The WM8962 codec provides two GPIO pins: GPIO5 and GPIO6, which can be
used as inputs. Implement the GPIO .get callback to allow consumers to
make use of them.
One important detail to remember is that the get() callback will not
function correctly if the input pins have debouncing enabled and DBCLK
is not enabled. In particular, for MR-NAVQ95 where GPIO5 is used for
headphone detection (LOW = unplugged, HIGH = plugged), what would happen
is the get() function would always return 0 even if the headphones were
plugged in. This would happen whenever DBCLK wasn't enabled (or, rather,
when SYSCLK wasn't enabled since DBCLK should be enabled automatically).
We might be able to get around the aforementioned limitation by force
enabling SYSCLK before reading the pin's state. See TODO from the code.
Signed-off-by: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
---
sound/soc/codecs/wm8962.c | 51 +++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/sound/soc/codecs/wm8962.c b/sound/soc/codecs/wm8962.c
index 1832685aca40..7122d93a742c 100644
--- a/sound/soc/codecs/wm8962.c
+++ b/sound/soc/codecs/wm8962.c
@@ -3505,12 +3505,63 @@ static int wm8962_gpio_direction_out(struct gpio_chip *chip,
return 0;
}
+/*
+ * If your input pins have debouncing enabled, then make sure DBCLK is enabled
+ * before using this function! Otherwise, the value returned by this function
+ * may not be correct!
+ *
+ * TODO: can we work around this limitation by force enabling SYSCLK before
+ * attempting to read the pin's state? Is SYSCLK's source clock guaranteed
+ * to be configured by the time this function is called?
+ */
+static int wm8962_gpio_get(struct gpio_chip *chip, unsigned int offset)
+{
+ struct wm8962_priv *wm8962 = gpiochip_get_data(chip);
+ struct snd_soc_component *component = wm8962->component;
+ unsigned int val;
+ int ret;
+
+ /*
+ * only GPIO5/GPIO6 can be used as inputs! Note the difference between the GPIO
+ * index and the register offset.
+ */
+ if (offset != 4 && offset != 5) {
+ dev_err(component->dev, "Only GPIO5/GPIO6 can be used as inputs!\n");
+ return -EINVAL;
+ }
+
+ /*
+ * we are forced to resume the device and bypass the regmap cache for the
+ * read operation. This is because the GPIO level could change at any time.
+ * Remember that the GPIO registers are still marked as cacheable. This is
+ * because the configuration bits and the GPIO LVL bits share the same registers.
+ */
+ ret = pm_runtime_resume_and_get(component->dev);
+ if (ret < 0) {
+ dev_err(component->dev, "Failed to resume device: %d\n", ret);
+ return ret;
+ }
+
+ ret = regmap_read_bypassed(component->regmap, WM8962_GPIO_BASE + offset, &val);
+ if (ret < 0) {
+ dev_err(component->dev, "Failed to read GP%d_LVL: %d\n", offset + 1, ret);
+ pm_runtime_put(component->dev);
+ return ret;
+ }
+
+ pm_runtime_put(component->dev);
+
+ /* mask is intentional! We (ab)use the fact that GPn_LVL fields share the same mask */
+ return (val & WM8962_GP5_LVL) >> WM8962_GP5_LVL_SHIFT;
+}
+
static const struct gpio_chip wm8962_template_chip = {
.label = "wm8962",
.owner = THIS_MODULE,
.request = wm8962_gpio_request,
.direction_output = wm8962_gpio_direction_out,
.set = wm8962_gpio_set,
+ .get = wm8962_gpio_get,
.can_sleep = 1,
};
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support
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
2026-11-27 17:36 ` sashiko-bot
2026-11-27 17:00 ` [PATCH 3/3] ASoC: wm8962: fix interrupt mis-firing during runtime_resume() Laurentiu Mihalcea
2 siblings, 1 reply; 7+ messages in thread
From: Laurentiu Mihalcea @ 2026-11-27 17:00 UTC (permalink / raw)
To: Liam Girdwood, Mark Brown, Charles Keepax, Sebastian Krzyszkowiak,
elena.popa, cosmin.stoica
Cc: patches, linux-sound, linux-kernel
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
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 3/3] ASoC: wm8962: fix interrupt mis-firing during runtime_resume()
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 ` [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support Laurentiu Mihalcea
@ 2026-11-27 17:00 ` Laurentiu Mihalcea
2026-11-27 17:30 ` sashiko-bot
2 siblings, 1 reply; 7+ messages in thread
From: Laurentiu Mihalcea @ 2026-11-27 17:00 UTC (permalink / raw)
To: Liam Girdwood, Mark Brown, Charles Keepax, Sebastian Krzyszkowiak,
elena.popa, cosmin.stoica
Cc: patches, linux-sound, linux-kernel
From: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
During testing, it has been observed that the runtime_resume() function
may cause the codec interrupt to become asserted. Interestingly enough,
for the MR-NAVQ95 platform, the logic HIGH signal would sometimes peak at
about 50% of DBVDD (1.8V), which seems to be enough to make the downstream
PCAL6416 chip (which is edge triggered) think an interrupt has occurred
and report it downstream.
A sample of the captured waveform is shown below (sample is taken around
the time wm8962_i2c_probe() is called). WM8962_GPIO2 is the GPIO2 pin of
the codec and is configured to generate interrupts for the downstream PCAL
chip. PCAL6416_INT_N is the PCAL's INT_N pin. The GPIO2 active HIGH signal
peaks at about 0.7V in this particular snippet.
3.3V
WM8962_GPIO2
-------- --------
| | | |
0V ____________| |__________________| |_____
3.3V
_______________________________________
|
PCAL6416_INT_N |
|
0V |_____________
The fix for this would be to mask interrupts during runtime_supend() and
unmask them during runtime_resume() only after all of the operations have
finished (i.e. before returning from the function). This requires the
IRQ_DISABLE_UNLAZY IRQ flag be set to handle interrupt controllers which
don't implement the .irq_disable() callback. This ought to make sure that
the interrupt is at least masked after disable_irq() is called.
Signed-off-by: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
---
sound/soc/codecs/wm8962.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/sound/soc/codecs/wm8962.c b/sound/soc/codecs/wm8962.c
index 3bbaacb08aaf..8c9523d60219 100644
--- a/sound/soc/codecs/wm8962.c
+++ b/sound/soc/codecs/wm8962.c
@@ -4021,7 +4021,7 @@ static int wm8962_i2c_probe(struct i2c_client *i2c)
ret = devm_request_threaded_irq(&i2c->dev, wm8962->irq, NULL,
wm8962_irq,
- trigger | IRQF_ONESHOT,
+ trigger | IRQF_ONESHOT | IRQF_NO_AUTOEN,
"wm8962", &i2c->dev);
if (ret != 0) {
dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
@@ -4039,6 +4039,15 @@ static int wm8962_i2c_probe(struct i2c_client *i2c)
/* enable GPIO IRQs if need be */
wm8962_init_gpio_irq(wm8962);
}
+
+ /*
+ * we are forced to keep interrupts disabled while doing runtime_resume().
+ * Otherwise, we may end up with some unwanted transitions on the interrupt
+ * line. Some interrupt controllers may not implement the .irq_disable()
+ * callback. In such cases, the interrupts will only be masked if
+ * IRQ_DISABLE_UNLAZY is set.
+ */
+ irq_set_status_flags(wm8962->irq, IRQ_DISABLE_UNLAZY);
}
pm_runtime_enable(&i2c->dev);
@@ -4131,6 +4140,10 @@ static int wm8962_runtime_resume(struct device *dev)
WM8962_MSTR, WM8962_MSTR);
msleep(5);
+ /* keep this last - keeps the interrupt line from mis-firing */
+ if (wm8962->irq)
+ enable_irq(wm8962->irq);
+
return 0;
cache_sync_err:
@@ -4147,6 +4160,9 @@ static int wm8962_runtime_suspend(struct device *dev)
{
struct wm8962_priv *wm8962 = dev_get_drvdata(dev);
+ if (wm8962->irq)
+ disable_irq(wm8962->irq);
+
if (wm8962->master_flag)
regmap_update_bits(wm8962->regmap, WM8962_AUDIO_INTERFACE_0,
WM8962_MSTR, 0);
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/3] ASoC: wm8962: implement GPIO .get callback
2026-11-27 17:00 ` [PATCH 1/3] ASoC: wm8962: implement GPIO .get callback Laurentiu Mihalcea
@ 2026-11-27 17:22 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-27 17:22 UTC (permalink / raw)
To: Laurentiu Mihalcea; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ASoC: wm8962: missing direction_input callback on
wm8962_template_chip
- [Medium] ASoC: wm8962: wm8962_gpio_get() rejects valid output pins
GPIO2 and GPIO3
--
commit b87e152ac0d0fb816585617c6713e1d54f3b94a7
Author: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
ASoC: wm8962: implement GPIO .get callback
This commit implements the .get callback for the WM8962 gpio_chip to support
reading the logic level of GPIO5 and GPIO6.
> diff --git a/sound/soc/codecs/wm8962.c b/sound/soc/codecs/wm8962.c
> index 1832685aca40d..7122d93a742c5 100644
> --- a/sound/soc/codecs/wm8962.c
> +++ b/sound/soc/codecs/wm8962.c
> @@ -3505,12 +3505,63 @@ static int wm8962_gpio_direction_out(struct gpio_chip *chip,
> return 0;
> }
>
> +/*
> + * If your input pins have debouncing enabled, then make sure DBCLK is enabled
> + * before using this function! Otherwise, the value returned by this function
> + * may not be correct!
> + *
> + * TODO: can we work around this limitation by force enabling SYSCLK before
> + * attempting to read the pin's state? Is SYSCLK's source clock guaranteed
> + * to be configured by the time this function is called?
> + */
> +static int wm8962_gpio_get(struct gpio_chip *chip, unsigned int offset)
> +{
> + struct wm8962_priv *wm8962 = gpiochip_get_data(chip);
> + struct snd_soc_component *component = wm8962->component;
> + unsigned int val;
> + int ret;
> +
> + /*
> + * only GPIO5/GPIO6 can be used as inputs! Note the difference between the GPIO
> + * index and the register offset.
> + */
> + if (offset != 4 && offset != 5) {
> + dev_err(component->dev, "Only GPIO5/GPIO6 can be used as inputs!\n");
> + return -EINVAL;
> + }
[Severity: Medium]
Should wm8962_gpio_get() also accept output pins GPIO2 (offset 1) and GPIO3
(offset 2)?
gpiolib calls .get() on requested output lines too, such as from
gpiolib_dbg_show() when reading /sys/kernel/debug/gpio or when querying an
output's state via gpiod_get_value_cansleep(). WM8962_GPIO_2 and
WM8962_GPIO_3 are readable registers and expose WM8962_GP2_LVL and
WM8962_GP3_LVL at bit 6 just like GPIO5 and GPIO6, so rejecting offsets 1
and 2 here breaks output value readback and logs a dev_err() whenever
/sys/kernel/debug/gpio is read while GPIO2 or GPIO3 is requested.
> +
> + /*
> + * we are forced to resume the device and bypass the regmap cache for the
> + * read operation. This is because the GPIO level could change at any time.
> + * Remember that the GPIO registers are still marked as cacheable. This is
> + * because the configuration bits and the GPIO LVL bits share the same registers.
> + */
> + ret = pm_runtime_resume_and_get(component->dev);
> + if (ret < 0) {
> + dev_err(component->dev, "Failed to resume device: %d\n", ret);
> + return ret;
> + }
> +
> + ret = regmap_read_bypassed(component->regmap, WM8962_GPIO_BASE + offset, &val);
> + if (ret < 0) {
> + dev_err(component->dev, "Failed to read GP%d_LVL: %d\n", offset + 1, ret);
> + pm_runtime_put(component->dev);
> + return ret;
> + }
> +
> + pm_runtime_put(component->dev);
> +
> + /* mask is intentional! We (ab)use the fact that GPn_LVL fields share the same mask */
> + return (val & WM8962_GP5_LVL) >> WM8962_GP5_LVL_SHIFT;
> +}
> +
> static const struct gpio_chip wm8962_template_chip = {
> .label = "wm8962",
> .owner = THIS_MODULE,
> .request = wm8962_gpio_request,
> .direction_output = wm8962_gpio_direction_out,
> .set = wm8962_gpio_set,
> + .get = wm8962_gpio_get,
> .can_sleep = 1,
> };
[Severity: Medium]
Does wm8962_template_chip also need a .direction_input (and .get_direction)
callback to support GPIO5 and GPIO6 as inputs?
When both .get_direction and .direction_input are NULL,
gpiochip_add_data_with_key() marks every line with GPIOD_FLAG_IS_OUT at
registration time, even though GPIO5 and GPIO6 default to inputs in hardware,
causing consumers that request them with GPIOD_ASIS or lock them as IRQs via
gpiochip_lock_as_irq() to fail with -EIO. In addition,
gpiod_direction_input_nonotify() becomes a software-only flag clear when both
callbacks are absent:
drivers/gpio/gpiolib.c:gpiod_direction_input_nonotify() {
...
if (guard.gc->direction_input) {
ret = gpiochip_direction_input(guard.gc,
gpiod_hwgpio(desc));
} else if (guard.gc->get_direction) {
dir = gpiochip_get_direction(guard.gc, gpiod_hwgpio(desc));
...
}
if (ret == 0) {
clear_bit(GPIOD_FLAG_IS_OUT, &desc->flags);
ret = gpio_set_bias(desc);
}
}
As a result, calling gpiod_direction_input() on GPIO5 or GPIO6 never sets
WM8962_GP5_DIR or WM8962_GP6_DIR in hardware if the pin was previously driven
as an output, and requesting output-only pins GPIO2 or GPIO3 with GPIOD_IN
silently succeeds instead of returning -EINVAL.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127170039.663-1-laurentiumihalcea111@gmail.com?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 3/3] ASoC: wm8962: fix interrupt mis-firing during runtime_resume()
2026-11-27 17:00 ` [PATCH 3/3] ASoC: wm8962: fix interrupt mis-firing during runtime_resume() Laurentiu Mihalcea
@ 2026-11-27 17:30 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-27 17:30 UTC (permalink / raw)
To: Laurentiu Mihalcea; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ASoC: wm8962: deadlock between wm8962_runtime_suspend() and
wm8962_irq()
- [High] ASoC: wm8962: disabling IRQ in wm8962_runtime_suspend() breaks
idle IRQs
- [Medium] ASoC: wm8962: irq_set_status_flags() called on IRQ 0 in
wm8962_i2c_probe()
--
commit 917ccfcd5e1f9cc84d87be9fd3e359058c229f93
Author: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
ASoC: wm8962: fix interrupt mis-firing during runtime_resume()
This commit requests wm8962->irq with IRQF_NO_AUTOEN and IRQ_DISABLE_UNLAZY,
and enables or disables the IRQ across runtime resume and runtime suspend to
avoid spurious interrupt assertions during power transitions.
> diff --git a/sound/soc/codecs/wm8962.c b/sound/soc/codecs/wm8962.c
> index 3bbaacb08aafa..8c9523d60219b 100644
> --- a/sound/soc/codecs/wm8962.c
> +++ b/sound/soc/codecs/wm8962.c
> @@ -4021,7 +4021,7 @@ static int wm8962_i2c_probe(struct i2c_client *i2c)
>
> ret = devm_request_threaded_irq(&i2c->dev, wm8962->irq, NULL,
> wm8962_irq,
> - trigger | IRQF_ONESHOT,
> + trigger | IRQF_ONESHOT | IRQF_NO_AUTOEN,
> "wm8962", &i2c->dev);
> if (ret != 0) {
> dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
> @@ -4039,6 +4039,15 @@ static int wm8962_i2c_probe(struct i2c_client *i2c)
> /* enable GPIO IRQs if need be */
> wm8962_init_gpio_irq(wm8962);
> }
> +
> + /*
> + * we are forced to keep interrupts disabled while doing runtime_resume().
> + * Otherwise, we may end up with some unwanted transitions on the interrupt
> + * line. Some interrupt controllers may not implement the .irq_disable()
> + * callback. In such cases, the interrupts will only be masked if
> + * IRQ_DISABLE_UNLAZY is set.
> + */
> + irq_set_status_flags(wm8962->irq, IRQ_DISABLE_UNLAZY);
> }
[Severity: Medium]
Should irq_set_status_flags() be inside the else branch above? When
devm_request_threaded_irq() fails, the error path sets wm8962->irq = 0
and treats the failure as non-fatal, so execution falls through and
calls irq_set_status_flags(0, IRQ_DISABLE_UNLAZY) on IRQ 0.
> @@ -4131,6 +4140,10 @@ static int wm8962_runtime_resume(struct device *dev)
> WM8962_MSTR, WM8962_MSTR);
> msleep(5);
>
> + /* keep this last - keeps the interrupt line from mis-firing */
> + if (wm8962->irq)
> + enable_irq(wm8962->irq);
> +
> return 0;
>
> cache_sync_err:
> @@ -4147,6 +4160,9 @@ static int wm8962_runtime_suspend(struct device *dev)
> {
> struct wm8962_priv *wm8962 = dev_get_drvdata(dev);
>
> + if (wm8962->irq)
> + disable_irq(wm8962->irq);
[Severity: High]
Can disable_irq() in wm8962_runtime_suspend() deadlock with wm8962_irq()?
sound/soc/codecs/wm8962.c:wm8962_irq() {
...
ret = pm_runtime_resume_and_get(dev);
if (ret < 0) {
...
}
If an interrupt fires just before disable_irq() masks the line, the two
contexts wait on each other:
CPU0 CPU1
rpm_suspend()
runtime_status = RPM_SUSPENDING
wm8962_runtime_suspend()
wm8962_irq()
pm_runtime_resume_and_get()
rpm_resume()
/* waits for RPM_SUSPENDING */
disable_irq()
synchronize_irq()
/* waits for wm8962_irq() */
*** DEADLOCK ***
[Severity: High]
Does keeping wm8962->irq disabled while runtime suspended prevent GPIO
interrupts from firing when the codec is idle?
soc_component_dev_wm8962 does not set idle_bias_on, and neither
wm8962_init_gpio() nor wm8962_irq_chip holds a runtime PM reference
while child GPIO IRQs are active. Whenever no audio stream is active
(and after wm8962_gpio_get() drops its temporary runtime PM reference),
the codec stays in RPM_SUSPENDED with wm8962->irq masked at the parent
interrupt controller, so asynchronous GPIO5/GPIO6 events such as jack
detection cannot wake the device via wm8962_irq().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127170039.663-1-laurentiumihalcea111@gmail.com?part=3
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support
2026-11-27 17:00 ` [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support Laurentiu Mihalcea
@ 2026-11-27 17:36 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-27 17:36 UTC (permalink / raw)
To: Laurentiu Mihalcea; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] ASoC: wm8962: build failure when CONFIG_GPIOLIB or
GPIOLIB_IRQCHIP is disabled
- [High] ASoC: wm8962: setting gpio_chip.init_valid_mask disables GPIO2
and GPIO3
- [High] ASoC: wm8962: lifecycle mismatch and UAF on gc->irq.domain in
wm8962_irq()
- [High] ASoC: wm8962: lost GPIO IRQs due to runtime suspend and reset
in wm8962_irq()
- [Medium] ASoC: wm8962: bitwise AND with WM8962_GPIO_FN_IRQ in
wm8962_init_gpio_irq()
- [Medium] ASoC: wm8962: missing direction_input/get_direction breaks
direct GPIO IRQ use
--
commit 836469865b058b5ef16dbf8d43d225333a1a1717
Author: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>
ASoC: wm8962: add GPIO interrupt controller support
This commit registers a GPIO irqchip on the WM8962 codec so that GPIO5 and
GPIO6 can be used as interrupt sources by consumer devices.
> diff --git a/sound/soc/codecs/wm8962.c b/sound/soc/codecs/wm8962.c
> index 7122d93a742c5..3bbaacb08aafa 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;
[Severity: High]
Does this fail to build when CONFIG_GPIOLIB or CONFIG_GPIOLIB_IRQCHIP is
disabled?
SND_SOC_WM8962 neither depends on GPIOLIB nor selects GPIOLIB_IRQCHIP.
wm8962_priv.gpio_chip is only declared under #ifdef CONFIG_GPIOLIB, and
gpio_chip.irq as well as GPIOCHIP_IRQ_RESOURCE_HELPERS are only available
under CONFIG_GPIOLIB_IRQCHIP, so referencing wm8962->gpio_chip,
gc->irq.domain, and wm8962_irq_chip breaks builds without those options.
>
> 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);
[Severity: High]
Are GPIO5 and GPIO6 interrupts lost whenever the codec is runtime
suspended?
Nothing holds a runtime PM reference on the codec while a GPIO IRQ is
requested (for example via irq_domain_set_pm_device()). When the codec is
idle in RPM_SUSPENDED and a GPIO interrupt fires, pm_runtime_resume_and_get()
runs wm8962_runtime_resume() -> wm8962_reset(), which resets the hardware and
clears WM8962_INTERRUPT_STATUS_1 before wm8962_irq_status_masked() reads it.
In addition, commit "ASoC: wm8962: fix interrupt mis-firing during
runtime_resume()" later in this series disables wm8962->irq in
wm8962_runtime_suspend(), preventing GPIO interrupts from firing at all while
suspended.
[ ... ]
> @@ -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");
> + }
[Severity: High]
Can irq_find_mapping() dereference a freed gc->irq.domain or pass NULL
before the ASoC component is bound?
wm8962_irq() is requested in wm8962_i2c_probe() and wm8962_init_gpio_irq()
unmasks GP5 and GP6 interrupts immediately, whereas gpio_chip is added in
wm8962_probe() and removed in wm8962_remove() -> wm8962_free_gpio().
Before wm8962_probe() runs, gc->irq.domain is NULL, which causes
__irq_resolve_mapping() to fall back to irq_default_domain. When the sound
card unbinds, gpiochip_remove() frees gc->irq.domain while GP5/GP6
interrupts remain unmasked and wm8962_irq() is still active, causing a
use-after-free on subsequent GP5/GP6 interrupts.
[ ... ]
> @@ -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,
> };
[Severity: High]
Does setting wm8962_init_valid_mask on gpio_chip.init_valid_mask disable
GPIO2 and GPIO3 as output GPIOs?
gpio_chip.init_valid_mask restricts which GPIO lines exist for all gpiolib
operations, so clearing everything except offsets 4 and 5 causes
gpiod_request_commit() to reject GPIO2 (offset 1) and GPIO3 (offset 2) with
-EINVAL. To restrict only which lines can be mapped as interrupts, this
callback should be assigned to girq->init_valid_mask instead.
>
> +/*
> + * 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,
> +};
[Severity: Medium]
Can gpiochip_lock_as_irq() succeed when wm8962_template_chip defines
neither .direction_input nor .get_direction?
During gpiochip_add_data_with_key(), gpiolib sets GPIOD_FLAG_IS_OUT on every
line when both .get_direction and .direction_input are NULL. When a consumer
requests a GPIO5 or GPIO6 interrupt, GPIOCHIP_IRQ_RESOURCE_HELPERS calls
gpiochip_lock_as_irq(), which sees GPIOD_FLAG_IS_OUT set and rejects the
interrupt request with -EIO.
> +
> +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;
[Severity: Medium]
Should these checks mask with WM8962_GP5_FN_MASK / WM8962_GP6_FN_MASK and
compare equal to WM8962_GPIO_FN_IRQ instead of using a bitwise AND?
GP5_FN and GP6_FN are 5-bit function codes where WM8962_GPIO_FN_IRQ is 3,
so testing with & WM8962_GPIO_FN_IRQ matches any non-IRQ function with bit
0 or bit 1 set (such as WM8962_GPIO_FN_LOGIC or WM8962_GPIO_FN_MICD) and
unmasks GP5/GP6 interrupts when the pins are not configured for IRQ output.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127170039.663-1-laurentiumihalcea111@gmail.com?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-27 17:36 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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:22 ` sashiko-bot
2026-11-27 17:00 ` [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support Laurentiu Mihalcea
2026-11-27 17:36 ` sashiko-bot
2026-11-27 17:00 ` [PATCH 3/3] ASoC: wm8962: fix interrupt mis-firing during runtime_resume() Laurentiu Mihalcea
2026-11-27 17:30 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox