]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ALSA: hda/cs35l56: Create debugfs files for factory calibration
authorRichard Fitzgerald <rf@opensource.cirrus.com>
Tue, 21 Oct 2025 10:50:16 +0000 (11:50 +0100)
committerMark Brown <broonie@kernel.org>
Mon, 27 Oct 2025 14:07:45 +0000 (14:07 +0000)
Create debugfs files that can be used to perform factory calibration.

During manufacture, the production line must perform a factory calibration
of the amps. This commit adds this functionality via debugfs files.

As this is only needed during manufacture, there is no need for this to be
available in a normal system so a Kconfig item has been added to enable
this. The new Kconfig option is inside a sub-menu because items do not
group and indent if the parent is invisible or there are multiple parent
dependencies. Anyway the sub-menu reduces the clutter.

cs35l56_hda_apply_calibration() has been changed to return an error code
that can be reported back through the debugfs write. The original call to
this function doesn't check the return code because in normal use it
doesn't matter whether this fails - the firmware will default to a safe
calibration for the platform. But tooling using the debugfs files might
want to know if there was an error.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20251021105022.1013685-6-rf@opensource.cirrus.com
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/hda/codecs/side-codecs/Kconfig
sound/hda/codecs/side-codecs/cs35l56_hda.c
sound/hda/codecs/side-codecs/cs35l56_hda.h

index cbf1847896bc92b08c466686a7f0e9bf77ede42d..f674e9a9c7d7af4755c8f08e8c33ce920f270e09 100644 (file)
@@ -88,6 +88,21 @@ config SND_HDA_SCODEC_CS35L56_SPI
          Say Y or M here to include CS35L56 amplifier support with
          SPI control.
 
+menu "CS35L56 driver options"
+       depends on SND_HDA_SCODEC_CS35L56
+
+config SND_HDA_SCODEC_CS35L56_CAL_DEBUGFS
+       bool "CS35L56 create debugfs for factory calibration"
+       default N
+       depends on DEBUG_FS
+       select SND_SOC_CS35L56_CAL_DEBUGFS_COMMON
+       help
+         Create debugfs entries used during factory-line manufacture
+         for factory calibration.
+
+         If unsure select "N".
+endmenu
+
 config SND_HDA_SCODEC_TAS2781
        tristate
        select SND_HDA_GENERIC
index 5bb1c4ebeaf3cb88d2e4da7e07a8ac114270d607..03f565312459726a3d9b8565756345cf2f9e0e47 100644 (file)
@@ -548,20 +548,24 @@ static void cs35l56_hda_release_firmware_files(const struct firmware *wmfw_firmw
        kfree(coeff_filename);
 }
 
-static void cs35l56_hda_apply_calibration(struct cs35l56_hda *cs35l56)
+static int cs35l56_hda_apply_calibration(struct cs35l56_hda *cs35l56)
 {
        int ret;
 
        if (!cs35l56->base.cal_data_valid || cs35l56->base.secured)
-               return;
+               return -EACCES;
 
        ret = cs_amp_write_cal_coeffs(&cs35l56->cs_dsp,
                                      &cs35l56_calibration_controls,
                                      &cs35l56->base.cal_data);
-       if (ret < 0)
+       if (ret < 0) {
                dev_warn(cs35l56->base.dev, "Failed to write calibration: %d\n", ret);
-       else
-               dev_info(cs35l56->base.dev, "Calibration applied\n");
+               return ret;
+       }
+
+       dev_info(cs35l56->base.dev, "Calibration applied\n");
+
+       return 0;
 }
 
 static void cs35l56_hda_fw_load(struct cs35l56_hda *cs35l56)
@@ -669,7 +673,9 @@ static void cs35l56_hda_fw_load(struct cs35l56_hda *cs35l56)
        if (ret)
                dev_dbg(cs35l56->base.dev, "%s: cs_dsp_run ret %d\n", __func__, ret);
 
+       /* Don't need to check return code, it's not fatal if this fails */
        cs35l56_hda_apply_calibration(cs35l56);
+
        ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
        if (ret)
                cs_dsp_stop(&cs35l56->cs_dsp);
@@ -695,6 +701,100 @@ static void cs35l56_hda_dsp_work(struct work_struct *work)
        cs35l56_hda_fw_load(cs35l56);
 }
 
+static ssize_t cs35l56_hda_debugfs_calibrate_write(struct file *file,
+                                                  const char __user *from,
+                                                  size_t count, loff_t *ppos)
+{
+       struct cs35l56_base *cs35l56_base = file->private_data;
+       ssize_t ret;
+
+       ret = pm_runtime_resume_and_get(cs35l56_base->dev);
+       if (ret)
+               return ret;
+
+       ret = cs35l56_calibrate_debugfs_write(cs35l56_base, from, count, ppos);
+       pm_runtime_autosuspend(cs35l56_base->dev);
+
+       return ret;
+}
+
+static ssize_t cs35l56_hda_debugfs_cal_temperature_write(struct file *file,
+                                                        const char __user *from,
+                                                        size_t count, loff_t *ppos)
+{
+       struct cs35l56_base *cs35l56_base = file->private_data;
+       ssize_t ret;
+
+       ret = pm_runtime_resume_and_get(cs35l56_base->dev);
+       if (ret)
+               return ret;
+
+       ret = cs35l56_cal_ambient_debugfs_write(cs35l56_base, from, count, ppos);
+       pm_runtime_autosuspend(cs35l56_base->dev);
+
+       return ret;
+}
+
+static ssize_t cs35l56_hda_debugfs_cal_data_read(struct file *file,
+                                                char __user *to,
+                                                size_t count, loff_t *ppos)
+{
+       struct cs35l56_base *cs35l56_base = file->private_data;
+       ssize_t ret;
+
+       ret = pm_runtime_resume_and_get(cs35l56_base->dev);
+       if (ret)
+               return ret;
+
+       ret = cs35l56_cal_data_debugfs_read(cs35l56_base, to, count, ppos);
+       pm_runtime_autosuspend(cs35l56_base->dev);
+
+       return ret;
+}
+
+static ssize_t cs35l56_hda_debugfs_cal_data_write(struct file *file,
+                                                 const char __user *from,
+                                                 size_t count, loff_t *ppos)
+{
+       struct cs35l56_base *cs35l56_base = file->private_data;
+       struct cs35l56_hda *cs35l56 = cs35l56_hda_from_base(cs35l56_base);
+       ssize_t ret;
+
+       ret = cs35l56_cal_data_debugfs_write(cs35l56_base, from, count, ppos);
+       if (ret == -ENODATA)
+               return count;   /* Ignore writes of empty cal blobs */
+
+       if (ret < 0)
+               return ret;
+
+       ret = pm_runtime_resume_and_get(cs35l56_base->dev);
+       if (ret)
+               return ret;
+
+       ret = cs35l56_hda_apply_calibration(cs35l56);
+       if (ret == 0)
+               cs35l56_mbox_send(cs35l56_base, CS35L56_MBOX_CMD_AUDIO_REINIT);
+       else
+               count = -EIO;
+
+       pm_runtime_autosuspend(cs35l56_base->dev);
+
+       return count;
+}
+
+static const struct cs35l56_cal_debugfs_fops cs35l56_hda_cal_debugfs_fops = {
+       .calibrate = {
+               .write = cs35l56_hda_debugfs_calibrate_write,
+       },
+       .cal_temperature = {
+               .write = cs35l56_hda_debugfs_cal_temperature_write,
+       },
+       .cal_data = {
+               .read = cs35l56_hda_debugfs_cal_data_read,
+               .write = cs35l56_hda_debugfs_cal_data_write,
+       },
+};
+
 static int cs35l56_hda_bind(struct device *dev, struct device *master, void *master_data)
 {
        struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
@@ -722,6 +822,9 @@ static int cs35l56_hda_bind(struct device *dev, struct device *master, void *mas
        cs_dsp_init_debugfs(&cs35l56->cs_dsp, cs35l56->debugfs_root);
 #endif
 
+       if (IS_ENABLED(CONFIG_SND_HDA_SCODEC_CS35L56_CAL_DEBUGFS))
+               cs35l56_create_cal_debugfs(&cs35l56->base, &cs35l56_hda_cal_debugfs_fops);
+
        dev_dbg(cs35l56->base.dev, "Bound\n");
 
        return 0;
@@ -735,6 +838,7 @@ static void cs35l56_hda_unbind(struct device *dev, struct device *master, void *
 
        cancel_work_sync(&cs35l56->dsp_work);
 
+       cs35l56_remove_cal_debugfs(&cs35l56->base);
        cs35l56_hda_remove_controls(cs35l56);
 
 #if IS_ENABLED(CONFIG_SND_DEBUG)
index 38d94fb213a50a999136707a91893798b76357b8..cb4b5e7356a35842643541229e5a29d4e9045f5c 100644 (file)
@@ -9,6 +9,7 @@
 #ifndef __CS35L56_HDA_H__
 #define __CS35L56_HDA_H__
 
+#include <linux/container_of.h>
 #include <linux/device.h>
 #include <linux/gpio/consumer.h>
 #include <linux/firmware/cirrus/cs_dsp.h>
@@ -42,6 +43,11 @@ struct cs35l56_hda {
 #endif
 };
 
+static inline struct cs35l56_hda *cs35l56_hda_from_base(struct cs35l56_base *cs35l56_base)
+{
+       return container_of(cs35l56_base, struct cs35l56_hda, base);
+}
+
 extern const struct dev_pm_ops cs35l56_hda_pm_ops;
 
 int cs35l56_hda_common_probe(struct cs35l56_hda *cs35l56, int hid, int id);