ASoC: es8328: Fix deemph control and improve error handling
cover ASoC: es8328: Fix deemph control and improve error handling
This series improves the ES8328 codec driver's reliability by ensuring that register I/O operations and state transitions properly propagate error codes.
It also fixes a DAC deemphasis control sequencing bug and adds proper resource unwinding during system resume.
Summary of changes:
- Fix DAC deemphasis control: update cached state before hardware.
- Propagate error codes in set_bias_level().
- Add proper unwind handling in resume().
- Check return values of snd_soc_component_update_bits() in set_dai_fmt().
Hsieh Hung-En (3):
ASoC: es8328: Fix DAC deemphasis control handling
ASoC: es8328: Propagate errors in set_bias_level()
ASoC: es8328: Check errors in set_dai_fmt() and resume()
sound/soc/codecs/es8328.c | 116 ++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 43 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 ASoC: es8328: Fix DAC deemphasis control handling
The DAC deemphasis control updated the hardware before updating the cached state, causing the previous setting to be applied.
Update the cached deemphasis state first and then apply the setting.
Also check and propagate errors from es8328_set_deemph() in hw_params().
--- sound/soc/codecs/es8328.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
sound/soc/codecs/es8328.c +4 -3
@@ -163,12 +163,11 @@ static int es8328_put_deemph(struct snd_kcontrol *kcontrol,if (es8328->deemph == deemph)return 0 ; + es8328->deemph = deemph; ret =es8328_set_deemph (component);if (ret <0 )return ret; - es8328->deemph = deemph; -return 1 ; }
@@ -530,7 +529,9 @@ static int es8328_hw_params(struct snd_pcm_substream *substream,return ret; es8328->playback_fs =params_rate (params); -es8328_set_deemph (component); + ret =es8328_set_deemph (component); +if (ret <0 ) +return ret; }else { ret = snd_soc_component_update_bits(component, ES8328_ADCCONTROL4, ES8328_ADCCONTROL4_ADCWL_MASK,
Signature · 1 line
2.34.1
PATCH 2/3 ASoC: es8328: Propagate errors in set_bias_level()
In reply to Hsieh Hung-EnRegister writes and updates in set_bias_level() ignored return values, potentially masking I/O failures during bias level transitions.
Check and propagate errors from component register writes and updates.
--- sound/soc/codecs/es8328.c | 60 ++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 23 deletions(-)
sound/soc/codecs/es8328.c +37 -23
@@ -647,6 +647,7 @@ static int es8328_set_bias_level(struct snd_soc_component *component, enum snd_soc_bias_level level) {struct snd_soc_dapm_context *dapm =snd_soc_component_to_dapm (component); +int ret;switch (level) {case SND_SOC_BIAS_ON :
@@ -654,43 +655,56 @@ static int es8328_set_bias_level(struct snd_soc_component *component,case SND_SOC_BIAS_PREPARE :/* VREF, VMID=2x50k, digital enabled */ -snd_soc_component_write (component,ES8328_CHIPPOWER ,0 ); -snd_soc_component_update_bits (component,ES8328_CONTROL1 , -ES8328_CONTROL1_VMIDSEL_MASK | -ES8328_CONTROL1_ENREF , - ES8328_CONTROL1_VMIDSEL_50k | -ES8328_CONTROL1_ENREF ); + ret =snd_soc_component_write (component,ES8328_CHIPPOWER ,0 ); +if (ret <0 ) +return ret; + + ret =snd_soc_component_update_bits (component,ES8328_CONTROL1 , +ES8328_CONTROL1_VMIDSEL_MASK | +ES8328_CONTROL1_ENREF , + ES8328_CONTROL1_VMIDSEL_50k | +ES8328_CONTROL1_ENREF ); +if (ret <0 ) +return ret;break ;case SND_SOC_BIAS_STANDBY :if (snd_soc_dapm_get_bias_level (dapm) ==SND_SOC_BIAS_OFF ) { -snd_soc_component_update_bits (component,ES8328_CONTROL1 , -ES8328_CONTROL1_VMIDSEL_MASK | -ES8328_CONTROL1_ENREF , - ES8328_CONTROL1_VMIDSEL_5k | -ES8328_CONTROL1_ENREF ); + ret =snd_soc_component_update_bits (component,ES8328_CONTROL1 , +ES8328_CONTROL1_VMIDSEL_MASK | +ES8328_CONTROL1_ENREF , + ES8328_CONTROL1_VMIDSEL_5k | +ES8328_CONTROL1_ENREF ); +if (ret <0 ) +return ret;/* Charge caps */ msleep (100 ); } -snd_soc_component_write (component,ES8328_CONTROL2 , -ES8328_CONTROL2_OVERCURRENT_ON | -ES8328_CONTROL2_THERMAL_SHUTDOWN_ON ); + ret =snd_soc_component_write (component,ES8328_CONTROL2 , +ES8328_CONTROL2_OVERCURRENT_ON | +ES8328_CONTROL2_THERMAL_SHUTDOWN_ON ); +if (ret <0 ) +return ret;/* VREF, VMID=2*500k, digital stopped */ -snd_soc_component_update_bits (component,ES8328_CONTROL1 , -ES8328_CONTROL1_VMIDSEL_MASK | -ES8328_CONTROL1_ENREF , - ES8328_CONTROL1_VMIDSEL_500k | -ES8328_CONTROL1_ENREF ); + ret =snd_soc_component_update_bits (component,ES8328_CONTROL1 , +ES8328_CONTROL1_VMIDSEL_MASK | +ES8328_CONTROL1_ENREF , + ES8328_CONTROL1_VMIDSEL_500k | +ES8328_CONTROL1_ENREF ); +if (ret <0 ) +return ret;break ;case SND_SOC_BIAS_OFF : -snd_soc_component_update_bits (component,ES8328_CONTROL1 , -ES8328_CONTROL1_VMIDSEL_MASK | -ES8328_CONTROL1_ENREF , -0 ); + ret =snd_soc_component_update_bits (component,ES8328_CONTROL1 , +ES8328_CONTROL1_VMIDSEL_MASK | +ES8328_CONTROL1_ENREF , +0 ); +if (ret <0 ) +return ret;break ; }return 0 ;
Signature · 1 line
2.34.1
PATCH 3/3 ASoC: es8328: Check errors in set_dai_fmt() and resume()
In reply to Hsieh Hung-EnCheck and propagate errors from register updates in es8328_set_dai_fmt().
Also use the cached private regmap in resume() and add proper unwind for clock/regulator enable and regcache sync failures.
--- sound/soc/codecs/es8328.c | 49 +++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 17 deletions(-)
sound/soc/codecs/es8328.c +32 -17
@@ -592,21 +592,26 @@ static int es8328_set_dai_fmt(struct snd_soc_dai *codec_dai, {struct snd_soc_component *component = codec_dai->component;struct es8328_priv *es8328 =snd_soc_component_get_drvdata (component); +int ret;u8 dac_mode =0 ;u8 adc_mode =0 ;switch (fmt &SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK ) {case SND_SOC_DAIFMT_CBP_CFP :/* Master serial port mode, with BCLK generated automatically */ -snd_soc_component_update_bits (component,ES8328_MASTERMODE , -ES8328_MASTERMODE_MSC , -ES8328_MASTERMODE_MSC ); + ret =snd_soc_component_update_bits (component,ES8328_MASTERMODE , +ES8328_MASTERMODE_MSC , +ES8328_MASTERMODE_MSC ); +if (ret <0 ) +return ret; es8328->provider = true;break ;case SND_SOC_DAIFMT_CBC_CFC :/* Slave serial port mode */ -snd_soc_component_update_bits (component,ES8328_MASTERMODE , -ES8328_MASTERMODE_MSC ,0 ); + ret =snd_soc_component_update_bits (component,ES8328_MASTERMODE , +ES8328_MASTERMODE_MSC ,0 ); +if (ret <0 ) +return ret; es8328->provider = false;break ;default :
@@ -635,10 +640,17 @@ static int es8328_set_dai_fmt(struct snd_soc_dai *codec_dai,if ((fmt &SND_SOC_DAIFMT_INV_MASK ) !=SND_SOC_DAIFMT_NB_NF )return -EINVAL ; -snd_soc_component_update_bits (component,ES8328_DACCONTROL1 , -ES8328_DACCONTROL1_DACFORMAT_MASK , dac_mode); -snd_soc_component_update_bits (component,ES8328_ADCCONTROL4 , -ES8328_ADCCONTROL4_ADCFORMAT_MASK , adc_mode); + ret =snd_soc_component_update_bits (component,ES8328_DACCONTROL1 , +ES8328_DACCONTROL1_DACFORMAT_MASK , + dac_mode); +if (ret <0 ) +return ret; + + ret =snd_soc_component_update_bits (component,ES8328_ADCCONTROL4 , +ES8328_ADCCONTROL4_ADCFORMAT_MASK , + adc_mode); +if (ret <0 ) +return ret;return 0 ; }
@@ -759,12 +771,9 @@ static int es8328_suspend(struct snd_soc_component *component)static int es8328_resume (struct snd_soc_component *component) { -struct regmap *regmap =dev_get_regmap (component->dev,NULL ); -struct es8328_priv *es8328; +struct es8328_priv *es8328 =snd_soc_component_get_drvdata (component);int ret; - es8328 =snd_soc_component_get_drvdata (component); - ret =clk_prepare_enable (es8328->clk);if (ret) {dev_err (component->dev,"unable to enable clock\n" );
@@ -775,17 +784,23 @@ static int es8328_resume(struct snd_soc_component *component) es8328->supplies);if (ret) {dev_err (component->dev,"unable to enable regulators\n" ); -return ret; + goto err_clk; } -regcache_mark_dirty (regmap); - ret =regcache_sync (regmap); +regcache_mark_dirty (es8328->regmap); + ret =regcache_sync (es8328->regmap);if (ret) {dev_err (component->dev,"unable to sync regcache\n" ); -return ret; + goto err_regulators; }return 0 ; + +err_regulators: +regulator_bulk_disable (ARRAY_SIZE (es8328->supplies), es8328->supplies); +err_clk: +clk_disable_unprepare (es8328->clk); +return ret; }static int es8328_component_probe (struct snd_soc_component *component)
Signature · 1 line
2.34.1
Mark Brown
maintainerRe: [PATCH 3/3] ASoC: es8328: Check errors in set_dai_fmt() and resume()
Quoted 4 lines · Hsieh Hung-En
Check and propagate errors from register updates in
es8328_set_dai_fmt().Also use the cached private regmap in
resume()and add proper unwind for clock/regulator enable and regcache sync failures.
These are separate changes and should be split at least three patches.
v2 cover ASoC: es8328: error handling and resume fixes
In reply to Hsieh Hung-EnThis is v2 of the es8328 robustness series.
Changes since v1:
- Split the previous "set_dai_fmt() and resume()" patch into three patches:
* Check and propagate errors in set_dai_fmt()
* Use cached regmap on resume for regcache sync
* Add error unwind in resume to avoid leaking enabled resources
No functional changes intended beyond improving reviewability by splitting the patch.
Hsieh Hung-En (5):
ASoC: es8328: Fix DAC deemphasis control handling
ASoC: es8328: Propagate errors in set_bias_level()
ASoC: es8328: Check errors in set_dai_fmt()
ASoC: es8328: Use cached regmap on resume
ASoC: es8328: Add error unwind in resume
sound/soc/codecs/es8328.c | 116 ++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 43 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/5 ASoC: es8328: Fix DAC deemphasis control handling
The DAC deemphasis control updated the hardware before updating the cached state, causing the previous setting to be applied.
Update the cached deemphasis state first and then apply the setting.
Also check and propagate errors from es8328_set_deemph() in hw_params().
--- sound/soc/codecs/es8328.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
sound/soc/codecs/es8328.c +4 -3
@@ -163,12 +163,11 @@ static int es8328_put_deemph(struct snd_kcontrol *kcontrol,if (es8328->deemph == deemph)return 0 ; + es8328->deemph = deemph; ret =es8328_set_deemph (component);if (ret <0 )return ret; - es8328->deemph = deemph; -return 1 ; }
@@ -530,7 +529,9 @@ static int es8328_hw_params(struct snd_pcm_substream *substream,return ret; es8328->playback_fs =params_rate (params); -es8328_set_deemph (component); + ret =es8328_set_deemph (component); +if (ret <0 ) +return ret; }else { ret = snd_soc_component_update_bits(component, ES8328_ADCCONTROL4, ES8328_ADCCONTROL4_ADCWL_MASK,
Signature · 1 line
2.34.1
PATCH v2 2/5 ASoC: es8328: Propagate errors in set_bias_level()
In reply to Hsieh Hung-EnRegister writes and updates in set_bias_level() ignored return values, potentially masking I/O failures during bias level transitions.
Check and propagate errors from component register writes and updates.
--- sound/soc/codecs/es8328.c | 60 ++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 23 deletions(-)
sound/soc/codecs/es8328.c +37 -23
@@ -647,6 +647,7 @@ static int es8328_set_bias_level(struct snd_soc_component *component, enum snd_soc_bias_level level) {struct snd_soc_dapm_context *dapm =snd_soc_component_to_dapm (component); +int ret;switch (level) {case SND_SOC_BIAS_ON :
@@ -654,43 +655,56 @@ static int es8328_set_bias_level(struct snd_soc_component *component,case SND_SOC_BIAS_PREPARE :/* VREF, VMID=2x50k, digital enabled */ -snd_soc_component_write (component,ES8328_CHIPPOWER ,0 ); -snd_soc_component_update_bits (component,ES8328_CONTROL1 , -ES8328_CONTROL1_VMIDSEL_MASK | -ES8328_CONTROL1_ENREF , - ES8328_CONTROL1_VMIDSEL_50k | -ES8328_CONTROL1_ENREF ); + ret =snd_soc_component_write (component,ES8328_CHIPPOWER ,0 ); +if (ret <0 ) +return ret; + + ret =snd_soc_component_update_bits (component,ES8328_CONTROL1 , +ES8328_CONTROL1_VMIDSEL_MASK | +ES8328_CONTROL1_ENREF , + ES8328_CONTROL1_VMIDSEL_50k | +ES8328_CONTROL1_ENREF ); +if (ret <0 ) +return ret;break ;case SND_SOC_BIAS_STANDBY :if (snd_soc_dapm_get_bias_level (dapm) ==SND_SOC_BIAS_OFF ) { -snd_soc_component_update_bits (component,ES8328_CONTROL1 , -ES8328_CONTROL1_VMIDSEL_MASK | -ES8328_CONTROL1_ENREF , - ES8328_CONTROL1_VMIDSEL_5k | -ES8328_CONTROL1_ENREF ); + ret =snd_soc_component_update_bits (component,ES8328_CONTROL1 , +ES8328_CONTROL1_VMIDSEL_MASK | +ES8328_CONTROL1_ENREF , + ES8328_CONTROL1_VMIDSEL_5k | +ES8328_CONTROL1_ENREF ); +if (ret <0 ) +return ret;/* Charge caps */ msleep (100 ); } -snd_soc_component_write (component,ES8328_CONTROL2 , -ES8328_CONTROL2_OVERCURRENT_ON | -ES8328_CONTROL2_THERMAL_SHUTDOWN_ON ); + ret =snd_soc_component_write (component,ES8328_CONTROL2 , +ES8328_CONTROL2_OVERCURRENT_ON | +ES8328_CONTROL2_THERMAL_SHUTDOWN_ON ); +if (ret <0 ) +return ret;/* VREF, VMID=2*500k, digital stopped */ -snd_soc_component_update_bits (component,ES8328_CONTROL1 , -ES8328_CONTROL1_VMIDSEL_MASK | -ES8328_CONTROL1_ENREF , - ES8328_CONTROL1_VMIDSEL_500k | -ES8328_CONTROL1_ENREF ); + ret =snd_soc_component_update_bits (component,ES8328_CONTROL1 , +ES8328_CONTROL1_VMIDSEL_MASK | +ES8328_CONTROL1_ENREF , + ES8328_CONTROL1_VMIDSEL_500k | +ES8328_CONTROL1_ENREF ); +if (ret <0 ) +return ret;break ;case SND_SOC_BIAS_OFF : -snd_soc_component_update_bits (component,ES8328_CONTROL1 , -ES8328_CONTROL1_VMIDSEL_MASK | -ES8328_CONTROL1_ENREF , -0 ); + ret =snd_soc_component_update_bits (component,ES8328_CONTROL1 , +ES8328_CONTROL1_VMIDSEL_MASK | +ES8328_CONTROL1_ENREF , +0 ); +if (ret <0 ) +return ret;break ; }return 0 ;
Signature · 1 line
2.34.1
PATCH v2 3/5 ASoC: es8328: Check errors in set_dai_fmt()
In reply to Hsieh Hung-EnCheck and propagate return values from snd_soc_component_update_bits() in es8328_set_dai_fmt().
This avoids silent failures when register updates fail and ensures the DAI format is not left in an inconsistent state.
--- sound/soc/codecs/es8328.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
sound/soc/codecs/es8328.c +21 -9
@@ -592,21 +592,26 @@ static int es8328_set_dai_fmt(struct snd_soc_dai *codec_dai, {struct snd_soc_component *component = codec_dai->component;struct es8328_priv *es8328 =snd_soc_component_get_drvdata (component); +int ret;u8 dac_mode =0 ;u8 adc_mode =0 ;switch (fmt &SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK ) {case SND_SOC_DAIFMT_CBP_CFP :/* Master serial port mode, with BCLK generated automatically */ -snd_soc_component_update_bits (component,ES8328_MASTERMODE , -ES8328_MASTERMODE_MSC , -ES8328_MASTERMODE_MSC ); + ret =snd_soc_component_update_bits (component,ES8328_MASTERMODE , +ES8328_MASTERMODE_MSC , +ES8328_MASTERMODE_MSC ); +if (ret <0 ) +return ret; es8328->provider = true;break ;case SND_SOC_DAIFMT_CBC_CFC :/* Slave serial port mode */ -snd_soc_component_update_bits (component,ES8328_MASTERMODE , -ES8328_MASTERMODE_MSC ,0 ); + ret =snd_soc_component_update_bits (component,ES8328_MASTERMODE , +ES8328_MASTERMODE_MSC ,0 ); +if (ret <0 ) +return ret; es8328->provider = false;break ;default :
@@ -635,10 +640,17 @@ static int es8328_set_dai_fmt(struct snd_soc_dai *codec_dai,if ((fmt &SND_SOC_DAIFMT_INV_MASK ) !=SND_SOC_DAIFMT_NB_NF )return -EINVAL ; -snd_soc_component_update_bits (component,ES8328_DACCONTROL1 , -ES8328_DACCONTROL1_DACFORMAT_MASK , dac_mode); -snd_soc_component_update_bits (component,ES8328_ADCCONTROL4 , -ES8328_ADCCONTROL4_ADCFORMAT_MASK , adc_mode); + ret =snd_soc_component_update_bits (component,ES8328_DACCONTROL1 , +ES8328_DACCONTROL1_DACFORMAT_MASK , + dac_mode); +if (ret <0 ) +return ret; + + ret =snd_soc_component_update_bits (component,ES8328_ADCCONTROL4 , +ES8328_ADCCONTROL4_ADCFORMAT_MASK , + adc_mode); +if (ret <0 ) +return ret;return 0 ; }
Signature · 1 line
2.34.1
PATCH v2 4/5 ASoC: es8328: Use cached regmap on resume
In reply to Hsieh Hung-EnUse the regmap stored in the driver private data when restoring the register cache on resume, instead of looking it up from the device.
This keeps the resume path consistent with the regmap instance used by the driver and avoids relying on a separate dev_get_regmap() lookup.
--- sound/soc/codecs/es8328.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
sound/soc/codecs/es8328.c +3 -6
@@ -771,12 +771,9 @@ static int es8328_suspend(struct snd_soc_component *component)static int es8328_resume (struct snd_soc_component *component) { -struct regmap *regmap =dev_get_regmap (component->dev,NULL ); -struct es8328_priv *es8328; +struct es8328_priv *es8328 =snd_soc_component_get_drvdata (component);int ret; - es8328 =snd_soc_component_get_drvdata (component); - ret =clk_prepare_enable (es8328->clk);if (ret) {dev_err (component->dev,"unable to enable clock\n" );
@@ -790,8 +787,8 @@ static int es8328_resume(struct snd_soc_component *component)return ret; } -regcache_mark_dirty (regmap); - ret =regcache_sync (regmap); +regcache_mark_dirty (es8328->regmap); + ret =regcache_sync (es8328->regmap);if (ret) {dev_err (component->dev,"unable to sync regcache\n" );return ret;
Signature · 1 line
2.34.1
PATCH v2 5/5 ASoC: es8328: Add error unwind in resume
In reply to Hsieh Hung-EnHandle failures in the resume path by unwinding previously enabled resources.
If enabling regulators or syncing the regcache fails, disable regulators and unprepare the clock to avoid leaking resources and leaving the device in a partially resumed state.
--- sound/soc/codecs/es8328.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
sound/soc/codecs/es8328.c +8 -2
@@ -784,17 +784,23 @@ static int es8328_resume(struct snd_soc_component *component) es8328->supplies);if (ret) {dev_err (component->dev,"unable to enable regulators\n" ); -return ret; + goto err_clk; }regcache_mark_dirty (es8328->regmap); ret =regcache_sync (es8328->regmap);if (ret) {dev_err (component->dev,"unable to sync regcache\n" ); -return ret; + goto err_regulators; }return 0 ; + +err_regulators: +regulator_bulk_disable (ARRAY_SIZE (es8328->supplies), es8328->supplies); +err_clk: +clk_disable_unprepare (es8328->clk); +return ret; }static int es8328_component_probe (struct snd_soc_component *component)
Signature · 1 line
2.34.1
Mark Brown
maintainerRe: [PATCH v2 0/5] ASoC: es8328: error handling and resume fixes
In reply to Hsieh Hung-EnQuoted 9 lines · Hsieh Hung-En
This is v2 of the es8328 robustness series.
Changes since v1:
- Split the previous "set_dai_fmt()andresume()" patch into three patches:
* Check and propagate errors inset_dai_fmt()
* Use cached regmap on resume for regcache sync
* Add error unwind in resume to avoid leaking enabled resources[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next
Thanks!
[1/5] ASoC: es8328: Fix DAC deemphasis control handling
commit: 4c6b74d58766ce7cd66ae8e14babf877039adef6
[2/5] ASoC: es8328: Propagate errors in set_bias_level()
commit: 0801a03a317b4848ca8c10a98f9b84028b7c8fc6
[3/5] ASoC: es8328: Check errors in set_dai_fmt()
commit: 3570e8eef217e60178d23eb490a34d64249bee45
[4/5] ASoC: es8328: Use cached regmap on resume
commit: 9917d99f44231b531a1c704bbbd58059e78c2f59
[5/5] ASoC: es8328: Add error unwind in resume
commit: 8232e6079ae6f8d3a61d87973cb427385aa469b9All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark