[PATCH 1/3] ASoC: wm8962: implement GPIO .get callback

From: Laurentiu Mihalcea

Date: Fri Nov 27 2026 - 12:06:25 EST


From: Laurentiu Mihalcea <laurentiu.mihalcea@xxxxxxx>

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@xxxxxxx>
---
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