[PATCH] ASoC: cs35l56: Allow factory calibration through ALSA controls

Richard Fitzgerald posted 1 patch 1 week, 2 days ago
include/sound/cs35l56.h           |  1 +
sound/soc/codecs/Kconfig          | 13 +++++
sound/soc/codecs/cs35l56-shared.c |  9 ++++
sound/soc/codecs/cs35l56.c        | 89 +++++++++++++++++++++++++++++++
4 files changed, 112 insertions(+)
[PATCH] ASoC: cs35l56: Allow factory calibration through ALSA controls
Posted by Richard Fitzgerald 1 week, 2 days ago
Add support for using ALSA controls to trigger a factory calibration.
This is protected by a new Kconfig option so that it is only available
if explicitly enabled in the kernel. By default it is not enabled.

Factory calibration is normally done through debugfs files.
Google have requested that factory calibration can be performed by
repair shops. These repair shops only have access to the standard
"user" kernel, which does not include debugfs.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
---
 include/sound/cs35l56.h           |  1 +
 sound/soc/codecs/Kconfig          | 13 +++++
 sound/soc/codecs/cs35l56-shared.c |  9 ++++
 sound/soc/codecs/cs35l56.c        | 89 +++++++++++++++++++++++++++++++
 4 files changed, 112 insertions(+)

diff --git a/include/sound/cs35l56.h b/include/sound/cs35l56.h
index 7ca25487030a..c3b10587cb4c 100644
--- a/include/sound/cs35l56.h
+++ b/include/sound/cs35l56.h
@@ -435,6 +435,7 @@ ssize_t cs35l56_cal_data_debugfs_read(struct cs35l56_base *cs35l56_base,
 ssize_t cs35l56_cal_data_debugfs_write(struct cs35l56_base *cs35l56_base,
 				       const char __user *from, size_t count,
 				       loff_t *ppos);
+int cs35l56_factory_calibrate(struct cs35l56_base *cs35l56_base);
 void cs35l56_create_cal_debugfs(struct cs35l56_base *cs35l56_base,
 				const struct cs35l56_cal_debugfs_fops *fops);
 void cs35l56_remove_cal_debugfs(struct cs35l56_base *cs35l56_base);
diff --git a/sound/soc/codecs/Kconfig b/sound/soc/codecs/Kconfig
index d6104796db4f..ca3e47db126e 100644
--- a/sound/soc/codecs/Kconfig
+++ b/sound/soc/codecs/Kconfig
@@ -921,6 +921,19 @@ config SND_SOC_CS35L56_CAL_SET_CTRL
 
 	  If unsure select "N".
 
+config SND_SOC_CS35L56_CAL_PERFORM_CTRL
+	bool "CS35L56 ALSA control to perform factory calibration"
+	default N
+	select SND_SOC_CS35L56_CAL_DEBUGFS_COMMON
+	help
+	  Allow performing factory calibration data through an ALSA
+	  control. It is recommended to use the debugfs method instead
+	  because debugfs has restricted access permissions.
+
+	  On most platforms this is not needed.
+
+	  If unsure select "N".
+
 config SND_SOC_CS35L56_TEST
 	tristate "KUnit test for Cirrus Logic cs35l56 driver" if !KUNIT_ALL_TESTS
 	depends on SND_SOC_CS35L56 && KUNIT
diff --git a/sound/soc/codecs/cs35l56-shared.c b/sound/soc/codecs/cs35l56-shared.c
index af87ebae98cb..e05d975ba794 100644
--- a/sound/soc/codecs/cs35l56-shared.c
+++ b/sound/soc/codecs/cs35l56-shared.c
@@ -1185,6 +1185,15 @@ ssize_t cs35l56_calibrate_debugfs_write(struct cs35l56_base *cs35l56_base,
 }
 EXPORT_SYMBOL_NS_GPL(cs35l56_calibrate_debugfs_write, "SND_SOC_CS35L56_SHARED");
 
+int cs35l56_factory_calibrate(struct cs35l56_base *cs35l56_base)
+{
+	if (!IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_PERFORM_CTRL))
+		return -ENXIO;
+
+	return cs35l56_perform_calibration(cs35l56_base);
+}
+EXPORT_SYMBOL_NS_GPL(cs35l56_factory_calibrate, "SND_SOC_CS35L56_SHARED");
+
 ssize_t cs35l56_cal_ambient_debugfs_write(struct cs35l56_base *cs35l56_base,
 					  const char __user *from, size_t count,
 					  loff_t *ppos)
diff --git a/sound/soc/codecs/cs35l56.c b/sound/soc/codecs/cs35l56.c
index 9d35797e000a..d9b6a9d2453d 100644
--- a/sound/soc/codecs/cs35l56.c
+++ b/sound/soc/codecs/cs35l56.c
@@ -1109,6 +1109,82 @@ static int cs35l56_cal_data_ctl_set(struct snd_kcontrol *kcontrol,
 	return 1;
 }
 
+static int cs35l56_cal_ambient_ctl_get(struct snd_kcontrol *kcontrol,
+				       struct snd_ctl_elem_value *ucontrol)
+{
+	/*
+	 * This is write-only but we need to return a value for user libraries
+	 * that assume all controls are readable.
+	 */
+	ucontrol->value.integer.value[0] = 0;
+
+	return 0;
+}
+
+static int cs35l56_cal_ambient_ctl_set(struct snd_kcontrol *kcontrol,
+				       struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
+	struct snd_soc_dapm_context *dapm;
+	int ret;
+
+	if (!in_range(ucontrol->value.integer.value[0], 5, 35))
+		return -EINVAL;
+
+	dapm = cs35l56_power_up_for_cal(cs35l56);
+	if (IS_ERR(dapm))
+		return PTR_ERR(dapm);
+
+	ret = cs_amp_write_ambient_temp(&cs35l56->dsp.cs_dsp,
+					cs35l56->base.calibration_controls,
+					ucontrol->value.integer.value[0]);
+	cs35l56_power_down_after_cal(cs35l56);
+
+	return ret;
+}
+
+static int cs35l56_calibrate_ctl_get(struct snd_kcontrol *kcontrol,
+				     struct snd_ctl_elem_value *ucontrol)
+{
+	/*
+	 * We must return a value because of user-side libraries that assume
+	 * all controls are readable. But always return false to prevent
+	 * dumb save-restore tools like alsactl accidentically triggering a
+	 * factory calibration when they restore.
+	 */
+	ucontrol->value.enumerated.item[0] = 0;
+
+	return 0;
+}
+
+static int cs35l56_calibrate_ctl_set(struct snd_kcontrol *kcontrol,
+				     struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
+	struct snd_soc_dapm_context *dapm;
+	int ret;
+
+	switch (ucontrol->value.enumerated.item[0]) {
+	case 0:
+		return 0;
+	case 1:
+		dapm = cs35l56_power_up_for_cal(cs35l56);
+		if (IS_ERR(dapm))
+			return PTR_ERR(dapm);
+
+		snd_soc_dapm_mutex_lock(dapm);
+		ret = cs35l56_factory_calibrate(&cs35l56->base);
+		snd_soc_dapm_mutex_unlock(dapm);
+		cs35l56_power_down_after_cal(cs35l56);
+
+		return ret;
+	default:
+		return -EINVAL;
+	}
+}
+
 static const struct snd_kcontrol_new cs35l56_cal_data_restore_controls[] = {
 	SND_SOC_BYTES_E("CAL_DATA", 0, sizeof(struct cirrus_amp_cal_data) / sizeof(u32),
 			cs35l56_cal_data_ctl_get, cs35l56_cal_data_ctl_set),
@@ -1117,6 +1193,13 @@ static const struct snd_kcontrol_new cs35l56_cal_data_restore_controls[] = {
 			SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE),
 };
 
+static const struct snd_kcontrol_new cs35l56_cal_perform_controls[] = {
+	SOC_SINGLE_EXT("CAL_AMBIENT", SND_SOC_NOPM, 0, 40, 0,
+		       cs35l56_cal_ambient_ctl_get, cs35l56_cal_ambient_ctl_set),
+	SOC_SINGLE_BOOL_EXT("Calibrate", 0,
+			    cs35l56_calibrate_ctl_get, cs35l56_calibrate_ctl_set),
+};
+
 VISIBLE_IF_KUNIT int cs35l56_set_fw_suffix(struct cs35l56_private *cs35l56)
 {
 	unsigned short vendor, device;
@@ -1290,6 +1373,12 @@ static int cs35l56_component_probe(struct snd_soc_component *component)
 						     ARRAY_SIZE(cs35l56_cal_data_restore_controls));
 	}
 
+	if (!ret && IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_PERFORM_CTRL)) {
+		ret = snd_soc_add_component_controls(component,
+						     cs35l56_cal_perform_controls,
+						     ARRAY_SIZE(cs35l56_cal_perform_controls));
+	}
+
 	if (ret)
 		return dev_err_probe(cs35l56->base.dev, ret, "unable to add controls\n");
 
-- 
2.47.3
Re: [PATCH] ASoC: cs35l56: Allow factory calibration through ALSA controls
Posted by Mark Brown 1 week, 2 days ago
On Tue, Mar 24, 2026 at 11:03:02AM +0000, Richard Fitzgerald wrote:

> +static int cs35l56_cal_ambient_ctl_set(struct snd_kcontrol *kcontrol,
> +				       struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
> +	struct snd_soc_dapm_context *dapm;
> +	int ret;
> +
> +	if (!in_range(ucontrol->value.integer.value[0], 5, 35))
> +		return -EINVAL;

The values range from 5..35...

> +static const struct snd_kcontrol_new cs35l56_cal_perform_controls[] = {
> +	SOC_SINGLE_EXT("CAL_AMBIENT", SND_SOC_NOPM, 0, 40, 0,
> +		       cs35l56_cal_ambient_ctl_get, cs35l56_cal_ambient_ctl_set),

...but the range is from 0..40?  I'd expect mixer-test to complain about
both these controls too, they're not flagged as volatile.
Re: [PATCH] ASoC: cs35l56: Allow factory calibration through ALSA controls
Posted by Richard Fitzgerald 1 week, 2 days ago
On 24/03/2026 2:02 pm, Mark Brown wrote:
> On Tue, Mar 24, 2026 at 11:03:02AM +0000, Richard Fitzgerald wrote:
> 
>> +static int cs35l56_cal_ambient_ctl_set(struct snd_kcontrol *kcontrol,
>> +				       struct snd_ctl_elem_value *ucontrol)
>> +{
>> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
>> +	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
>> +	struct snd_soc_dapm_context *dapm;
>> +	int ret;
>> +
>> +	if (!in_range(ucontrol->value.integer.value[0], 5, 35))
>> +		return -EINVAL;
> 
> The values range from 5..35...
> 

The second argument to in_range() isn't a max.
But if I'm sending a new version to make them volatile I can also change
it to not use the in_range() macro.

>> +static const struct snd_kcontrol_new cs35l56_cal_perform_controls[] = {
>> +	SOC_SINGLE_EXT("CAL_AMBIENT", SND_SOC_NOPM, 0, 40, 0,
>> +		       cs35l56_cal_ambient_ctl_get, cs35l56_cal_ambient_ctl_set),
> 
> ...but the range is from 0..40?  I'd expect mixer-test to complain about
> both these controls too, they're not flagged as volatile.
Re: [PATCH] ASoC: cs35l56: Allow factory calibration through ALSA controls
Posted by Mark Brown 1 week, 2 days ago
On Tue, Mar 24, 2026 at 02:24:21PM +0000, Richard Fitzgerald wrote:
> On 24/03/2026 2:02 pm, Mark Brown wrote:
> > On Tue, Mar 24, 2026 at 11:03:02AM +0000, Richard Fitzgerald wrote:

> > > +	if (!in_range(ucontrol->value.integer.value[0], 5, 35))
> > > +		return -EINVAL;

> > The values range from 5..35...
> 
> The second argument to in_range() isn't a max.
> But if I'm sending a new version to make them volatile I can also change
> it to not use the in_range() macro.

Ah, indeed - that's a bit of a footgun!  Still, there's also the 0..4
range that's advertised to userspace but not actually valid.