Skip to contentSkip to conversation

ASoC: rt5659: fix teardown order and stale state across card rebind

PATCH Superseded Tristan Madani 2 participants 2 messages Nov 7

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: rt5659: fix teardown order and stale state across card rebind

From: Tristan Madani <[email protected]>

The cleanup in rt5659_remove() does not reset hs_jack, jack_type, or the component pointer, and does not mark the regmap cache dirty after the codec reset. On card rebind this leaves a dangling hs_jack pointer and causes regmap_update_bits() to skip re-programming jack-detection registers because the cache still holds the pre-reset values.

Additionally, headset detection registers from a previous session remain cached after the hardware reset. regcache_sync() on rebind restores this stale state, which can leave the SAR source on button mode or HDA plug flags in an incorrect state. Clear the headset state by calling rt5659_headset_detect(component, 0) before the codec reset, and reset the HDA header plug tracking flags.

If the I2C device unbinds without a card bound, rt5659_remove() is never called and the IRQ keeps queuing jack_detect_work until devres frees the private data. Register a devm action to disable the IRQ and cancel the delayed work on any I2C teardown path. Guard disable_irq() in rt5659_i2c_shutdown() with the irq_disabled flag to avoid an unbalanced disable when rt5659_remove() already ran.

Store the I2C client pointer in rt5659_priv so all paths can reference it directly.

Fixes d3cb2de2479b ("ASoC: rt5659: add rt5659 codec driver")
---
 sound/soc/codecs/rt5659.c | 39 ++++++++++++++++++++++++++++++++++++---
 sound/soc/codecs/rt5659.h |  1 +
 2 files changed, 37 insertions(+), 3 deletions(-)
sound/soc/codecs/rt5659.c +36 -3
index 68f7d5673e190..14111f40384e5 100644
@@ -3660,6 +3660,16 @@ static int rt5659_set_bias_level(struct snd_soc_component *component,
 	return 0;
 }
 
+static void rt5659_i2c_disable_irq_and_cancel_work(void *data)
+{
+	struct rt5659_priv *rt5659 = data;
+
+	if (rt5659->irq_acquired && !rt5659->irq_disabled)
+		disable_irq(rt5659->i2c->irq);
+	if (rt5659->jack_detect_work_inited)
+		cancel_delayed_work_sync(&rt5659->jack_detect_work);
+}
+
 static int rt5659_probe(struct snd_soc_component *component)
 {
 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
@@ -3667,8 +3677,10 @@ static int rt5659_probe(struct snd_soc_component *component)
 
 	rt5659->component = component;
 
+	regcache_sync(rt5659->regmap);
+
 	if (rt5659->irq_disabled) {
-		enable_irq(to_i2c_client(component->dev)->irq);
+		enable_irq(rt5659->i2c->irq);
 		rt5659->irq_disabled = false;
 	}
 
@@ -3689,17 +3701,26 @@ static int rt5659_probe(struct snd_soc_component *component)
 static void rt5659_remove(struct snd_soc_component *component)
 {
 	struct rt5659_priv *rt5659 = snd_soc_component_get_drvdata(component);
-	struct i2c_client *i2c = to_i2c_client(component->dev);
 
 	if (rt5659->irq_acquired) {
-		disable_irq(i2c->irq);
+		disable_irq(rt5659->i2c->irq);
 		rt5659->irq_disabled = true;
 	}
 
 	if (rt5659->jack_detect_work_inited)
 		cancel_delayed_work_sync(&rt5659->jack_detect_work);
 
+	if (rt5659->jack_type)
+		rt5659_headset_detect(component, 0);
+
+	rt5659->hs_jack = NULL;
+	rt5659->jack_type = 0;
+	rt5659->hda_hp_plugged = false;
+	rt5659->hda_mic_plugged = false;
+	rt5659->component = NULL;
+
 	regmap_write(rt5659->regmap, RT5659_RESET, 0);
+	regcache_mark_dirty(rt5659->regmap);
 }
 
 #ifdef CONFIG_PM
@@ -4328,6 +4349,13 @@ static int rt5659_i2c_probe(struct i2c_client *i2c)
 				   RT5659_GP1_PIN_MASK, RT5659_GP1_PIN_IRQ);
 	}
 
+	rt5659->i2c = i2c;
+	ret = devm_add_action_or_reset(&i2c->dev,
+				      rt5659_i2c_disable_irq_and_cancel_work,
+				      rt5659);
+	if (ret)
+		return ret;
+
 	return devm_snd_soc_register_component(&i2c->dev,
 			&soc_component_dev_rt5659,
 			rt5659_dai, ARRAY_SIZE(rt5659_dai));
@@ -4337,6 +4365,11 @@ static void rt5659_i2c_shutdown(struct i2c_client *client)
 {
 	struct rt5659_priv *rt5659 = i2c_get_clientdata(client);
 
+	if (rt5659->irq_acquired && !rt5659->irq_disabled)
+		disable_irq(client->irq);
+	if (rt5659->jack_detect_work_inited)
+		cancel_delayed_work_sync(&rt5659->jack_detect_work);
+
 	regmap_write(rt5659->regmap, RT5659_RESET, 0);
 }
 
sound/soc/codecs/rt5659.h +1 -0
index e10b3844501b9..1118e6ab1887d 100644
@@ -1793,6 +1793,7 @@ struct rt5659_priv {
 	struct snd_soc_component *component;
 	struct rt5659_platform_data pdata;
 	struct regmap *regmap;
+	struct i2c_client *i2c;
 	struct gpio_desc *gpiod_ldo1_en;
 	struct gpio_desc *gpiod_reset;
 	struct snd_soc_jack *hs_jack;
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ASoC: rt5659: rt5659_remove() breaks HDA header mic detect on rebind - [Medium] ASoC: rt5659…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] ASoC: rt5659: rt5659_remove() breaks HDA header mic detect on rebind
- [Medium] ASoC: rt5659: rt5659_remove() desynchronizes HDA IRQ polarity cache
--

commit b7c86ad3bd52c1cf96525f2f34febf94cfef4a2d
Author: Tristan Madani <[email protected]>

ASoC: rt5659: fix teardown order and stale state across card rebind

This commit resets component and jack state on removal, marks and resyncs the regmap cache across card rebind, and registers a devm action to disable the IRQ and cancel jack detection work on I2C teardown.

Quoted 4 lines
sound/soc/codecs/rt5659.c +0 -0
index 68f7d5673e190..14111f40384e5 100644

[ ... ]

Quoted 7 lines
sound/soc/codecs/rt5659.c +0 -1
@@ -3689,17 +3701,26 @@ static int rt5659_probe(struct snd_soc_component *component)
 static void rt5659_remove(struct snd_soc_component *component)
 {
 	struct rt5659_priv *rt5659 = snd_soc_component_get_drvdata(component);
-	struct i2c_client *i2c = to_i2c_client(component->dev);
 
 	if (rt5659->irq_acquired) {

sound/soc/codecs/rt5659.c static int rt5659_probe(struct snd_soc_component *component) · line 3714

Quoted 10 lines
sound/soc/codecs/rt5659.c +3 -1
-		disable_irq(i2c->irq);
+		disable_irq(rt5659->i2c->irq);
 		rt5659->irq_disabled = true;
 	}
 
 	if (rt5659->jack_detect_work_inited)
 		cancel_delayed_work_sync(&rt5659->jack_detect_work);
 
+	if (rt5659->jack_type)
+		rt5659_headset_detect(component, 0);

[Severity: Medium]
Should this call to rt5659_headset_detect() be guarded by checking that rt5659->pdata.jd_src is RT5659_JD3?

When jd_src is RT5659_JD_HDA_HEADER, rt5659_probe() skips registering rt5659_particular_dapm_widgets ("Mic Det Power"), and rt5659_intel_hd_header_probe_setup() enables RT5659_4BTN_IL_EN and RT5659_IL_IRQ_EN once during I2C probe. If both headphone and microphone are plugged into the HDA header on removal, rt5659->jack_type equals SND_JACK_HEADSET, so rt5659_headset_detect(component, 0) logs an unknown DAPM pin error for "Mic Det Power" and calls rt5659_enable_push_button_irq(component, false), clearing RT5659_4BTN_IL_EN and RT5659_IL_IRQ_EN in the regmap cache. On card rebind, regcache_sync() writes those cleared bits to the codec without re-running rt5659_intel_hd_header_probe_setup(), leaving HDA header microphone detection disabled.

sound/soc/codecs/rt5659.c static int rt5659_probe(struct snd_soc_component *component) · line 3724

Quoted 10 lines
sound/soc/codecs/rt5659.c +8 -0
+
+	rt5659->hs_jack = NULL;
+	rt5659->jack_type = 0;
+	rt5659->hda_hp_plugged = false;
+	rt5659->hda_mic_plugged = false;
+	rt5659->component = NULL;
+
 	regmap_write(rt5659->regmap, RT5659_RESET, 0);
+	regcache_mark_dirty(rt5659->regmap);
 }

[Severity: Medium]
Does resetting hda_hp_plugged and hda_mic_plugged to false without updating RT5659_IRQ_CTRL_1 and RT5659_IRQ_CTRL_2 leave the cached HDA IRQ polarity inverted after rebind?

In rt5659_jack_detect_intel_hd_header(), plugging in a jack flips the IRQ polarity bits in RT5659_IRQ_CTRL_1 (bit 0x10 to 0x0) and RT5659_IRQ_CTRL_2 (bit 0x2 to 0x2):

sound/soc/codecs/rt5659.c:rt5659_jack_detect_intel_hd_header() {
	...
	if (hp_flag != rt5659->hda_hp_plugged) {
		rt5659->hda_hp_plugged = hp_flag;

		if (hp_flag) {
			regmap_update_bits(rt5659->regmap, RT5659_IRQ_CTRL_1,
				0x10, 0x0);
			rt5659->jack_type |= SND_JACK_HEADPHONE;
		} else {
			regmap_update_bits(rt5659->regmap, RT5659_IRQ_CTRL_1,
				0x10, 0x10);
	...
}

Because the regmap cache retains the plugged-in polarity across RT5659_RESET, regcache_sync() in rt5659_probe() restores that plugged-in polarity to the hardware while hda_hp_plugged and hda_mic_plugged are false. If a jack is unplugged while the card is unbound, hp_flag != rt5659->hda_hp_plugged (or mic_flag != rt5659->hda_mic_plugged) evaluates to false, so the polarity is never restored to the unplugged state and subsequent plug insertions will not generate an interrupt.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1