[PATCH 3/4] ASoC: codecs: twl6040: Allocate private data in the platform probe

From: Arina Kharlamova via B4 Relay

Date: Tue Dec 01 2026 - 14:12:36 EST


From: Arina Kharlamova <arina.kharlamova@xxxxxxxxxxxx>

twl6040_probe() is the component probe and runs every time a sound
card binds the codec, but it allocates struct twl6040_data with
devm_kzalloc_obj() on the codec platform device. That memory is
released only when the platform device is unbound, so every card bind
leaves one more copy behind. The same happens when twl6040_probe()
fails after the allocation.

Allocate the data once in twl6040_codec_probe() and initialize the
mutex and the jack work there too, so that they are not initialized
again on the next bind. The data now survives a card rebind, so clear
the jack pointer in twl6040_probe(). Otherwise a plug interrupt could
report to the jack of the previous card before the machine driver
calls twl6040_hs_jack_detect() again.

Fixes: 9523fcdcc02e ("ASoC: twl6040: Convert to use devm_* when possible")
Signed-off-by: Arina Kharlamova <arina.kharlamova@xxxxxxxxxxxx>
---
sound/soc/codecs/twl6040.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/sound/soc/codecs/twl6040.c b/sound/soc/codecs/twl6040.c
index aab3273f2ecc..dc111e85b998 100644
--- a/sound/soc/codecs/twl6040.c
+++ b/sound/soc/codecs/twl6040.c
@@ -1095,27 +1095,18 @@ static struct snd_soc_dai_driver twl6040_dai[] = {

static int twl6040_probe(struct snd_soc_component *component)
{
- struct twl6040_data *priv;
+ struct twl6040_data *priv = snd_soc_component_get_drvdata(component);
struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
struct platform_device *pdev = to_platform_device(component->dev);
int ret = 0;

- priv = devm_kzalloc_obj(component->dev, *priv);
- if (priv == NULL)
- return -ENOMEM;
-
- snd_soc_component_set_drvdata(component, priv);
-
priv->component = component;
+ priv->hs_jack.jack = NULL;

priv->plug_irq = platform_get_irq(pdev, 0);
if (priv->plug_irq < 0)
return priv->plug_irq;

- INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
-
- mutex_init(&priv->mutex);
-
ret = request_threaded_irq(priv->plug_irq, NULL,
twl6040_audio_handler,
IRQF_NO_SUSPEND | IRQF_ONESHOT,
@@ -1158,6 +1149,17 @@ static const struct snd_soc_component_driver soc_component_dev_twl6040 = {

static int twl6040_codec_probe(struct platform_device *pdev)
{
+ struct twl6040_data *priv;
+
+ priv = devm_kzalloc_obj(&pdev->dev, *priv);
+ if (priv == NULL)
+ return -ENOMEM;
+
+ INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
+ mutex_init(&priv->mutex);
+
+ platform_set_drvdata(pdev, priv);
+
return devm_snd_soc_register_component(&pdev->dev,
&soc_component_dev_twl6040,
twl6040_dai, ARRAY_SIZE(twl6040_dai));

--
Git-157)