diff mbox series

[09/10] ASoC: Intel: sof_sdw: Add support for Cirrus Logic CS35L56

Message ID 20230217161410.915202-10-rf@opensource.cirrus.com (mailing list archive)
State New, archived
Headers show
Series ASoC: Initial support for Cirrus Logic CS35L56 | expand

Commit Message

Richard Fitzgerald Feb. 17, 2023, 4:14 p.m. UTC
The CS35L56 is a high-performance mono boosted audio amplifier.

This patch adds the initialization for one, two or four amps.

All amps receive the same 2-channel (stereo) audio. Left/right
channel-to-speaker mapping is not fixed. The CS35L56 on-board DSP
adjusts the mapping based on current posture (orientation).

Each CS35L56 has 6 feedback channels for monitoring but to
remain within limited SoundWire bandwidth only one channel per
amp is enabled.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
---
 MAINTAINERS                              |   1 +
 sound/soc/intel/boards/Kconfig           |   1 +
 sound/soc/intel/boards/Makefile          |   1 +
 sound/soc/intel/boards/sof_sdw.c         |   7 ++
 sound/soc/intel/boards/sof_sdw_common.h  |   7 ++
 sound/soc/intel/boards/sof_sdw_cs35l56.c | 115 +++++++++++++++++++++++
 6 files changed, 132 insertions(+)
 create mode 100644 sound/soc/intel/boards/sof_sdw_cs35l56.c

Comments

Pierre-Louis Bossart Feb. 21, 2023, 4:49 p.m. UTC | #1
> +static int cs35l56_init_multiple(struct snd_soc_pcm_runtime *rtd, int count)
> +{
> +	struct snd_soc_card *card = rtd->card;
> +	struct snd_soc_dai *codec_dai;
> +	int i, ret;
> +
> +	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
> +					  "%s hs:cs35l56",

the string is wrong here, this is an amplifier so it should be
amp:cs35l56 or spk:cs36l56 (not sure which of the two we ended-up using).

> +					  card->components);
> +	if (!card->components)
> +		return -ENOMEM;
> +
> +	ret = snd_soc_dapm_new_controls(&card->dapm,
> +					cs35l56_sof_widgets, ARRAY_SIZE(cs35l56_sof_widgets));
> +	if (ret) {
> +		dev_err(card->dev, "Widgets add failed: %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = snd_soc_dapm_add_routes(&card->dapm, cs35l56_sof_map, count);
> +	if (ret) {
> +		dev_err(card->dev, "Map add %d failed: %d\n", count, ret);
> +		return ret;
> +	}
> +
> +	/* Enable one feedback TX per amp on different slots */
> +	for_each_rtd_codec_dais(rtd, i, codec_dai) {
> +		ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 1 << i, 4, 16);

TDM slots? Not getting how this would work with SoundWire?

> +		if (ret < 0)
> +			return ret;
> +	}
> +
> +	return 0;
> +}
Richard Fitzgerald Feb. 21, 2023, 5:32 p.m. UTC | #2
On 21/02/2023 16:49, Pierre-Louis Bossart wrote:
> 
>> +static int cs35l56_init_multiple(struct snd_soc_pcm_runtime *rtd, int count)
>> +{
>> +	struct snd_soc_card *card = rtd->card;
>> +	struct snd_soc_dai *codec_dai;
>> +	int i, ret;
>> +
>> +	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
>> +					  "%s hs:cs35l56",
> 
> the string is wrong here, this is an amplifier so it should be
> amp:cs35l56 or spk:cs36l56 (not sure which of the two we ended-up using).
> 

Will change it.

>> +					  card->components);
>> +	if (!card->components)
>> +		return -ENOMEM;
>> +
>> +	ret = snd_soc_dapm_new_controls(&card->dapm,
>> +					cs35l56_sof_widgets, ARRAY_SIZE(cs35l56_sof_widgets));
>> +	if (ret) {
>> +		dev_err(card->dev, "Widgets add failed: %d\n", ret);
>> +		return ret;
>> +	}
>> +
>> +	ret = snd_soc_dapm_add_routes(&card->dapm, cs35l56_sof_map, count);
>> +	if (ret) {
>> +		dev_err(card->dev, "Map add %d failed: %d\n", count, ret);
>> +		return ret;
>> +	}
>> +
>> +	/* Enable one feedback TX per amp on different slots */
>> +	for_each_rtd_codec_dais(rtd, i, codec_dai) {
>> +		ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 1 << i, 4, 16);
> 
> TDM slots? Not getting how this would work with SoundWire?
> 

Strictly speaking Soundwire is TDM (the frame time is divided up into
slots for each sample...).

The problem is if you have N amps on the dailink all feeding back audio
on the same bus. Their DP slots are all programmed to the same positions
in the frame, same as for the playback. So you have 4 amps all trying to
send 6 audio channels in the same positions in the frame and you'll just
get a ton of bus clash interrupts.

So we use the set_tdm_slot() like we do with I2S TDM to set which slots
are active for each amp.

I can't see that there's any obvious "generic" way that the manager code
can automatically figure out how many channels to enable on each amp and
what order to map them, so we do it here. Just as with I2S TDM - you
have many slots and many codecs but the machine driver has to tell it
how to map those.

>> +		if (ret < 0)
>> +			return ret;
>> +	}
>> +
>> +	return 0;
>> +}
Pierre-Louis Bossart Feb. 21, 2023, 6:03 p.m. UTC | #3
>>> +                      card->components);
>>> +    if (!card->components)
>>> +        return -ENOMEM;
>>> +
>>> +    ret = snd_soc_dapm_new_controls(&card->dapm,
>>> +                    cs35l56_sof_widgets,
>>> ARRAY_SIZE(cs35l56_sof_widgets));
>>> +    if (ret) {
>>> +        dev_err(card->dev, "Widgets add failed: %d\n", ret);
>>> +        return ret;
>>> +    }
>>> +
>>> +    ret = snd_soc_dapm_add_routes(&card->dapm, cs35l56_sof_map, count);
>>> +    if (ret) {
>>> +        dev_err(card->dev, "Map add %d failed: %d\n", count, ret);
>>> +        return ret;
>>> +    }
>>> +
>>> +    /* Enable one feedback TX per amp on different slots */
>>> +    for_each_rtd_codec_dais(rtd, i, codec_dai) {
>>> +        ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 1 << i, 4, 16);
>>
>> TDM slots? Not getting how this would work with SoundWire?
>>
> 
> Strictly speaking Soundwire is TDM (the frame time is divided up into
> slots for each sample...).
> 
> The problem is if you have N amps on the dailink all feeding back audio
> on the same bus. Their DP slots are all programmed to the same positions
> in the frame, same as for the playback. So you have 4 amps all trying to
> send 6 audio channels in the same positions in the frame and you'll just
> get a ton of bus clash interrupts.
> 
> So we use the set_tdm_slot() like we do with I2S TDM to set which slots
> are active for each amp.
> 
> I can't see that there's any obvious "generic" way that the manager code
> can automatically figure out how many channels to enable on each amp and
> what order to map them, so we do it here. Just as with I2S TDM - you
> have many slots and many codecs but the machine driver has to tell it
> how to map those.

IIRC Bard did the same thing recently, and the order of the feedback
channels is really defined by the order in which the peripheral devices
are added in the dailink. See
https://github.com/thesofproject/linux/pull/4108

There's also another open related to the number of channels, we need to
patch what the CPU DAI can handle, see
https://github.com/thesofproject/linux/pull/4136 or
https://github.com/thesofproject/linux/pull/4134
Richard Fitzgerald Feb. 22, 2023, 11:39 a.m. UTC | #4
On 21/02/2023 18:03, Pierre-Louis Bossart wrote:
> 
>>>> +                      card->components);
>>>> +    if (!card->components)
>>>> +        return -ENOMEM;
>>>> +
>>>> +    ret = snd_soc_dapm_new_controls(&card->dapm,
>>>> +                    cs35l56_sof_widgets,
>>>> ARRAY_SIZE(cs35l56_sof_widgets));
>>>> +    if (ret) {
>>>> +        dev_err(card->dev, "Widgets add failed: %d\n", ret);
>>>> +        return ret;
>>>> +    }
>>>> +
>>>> +    ret = snd_soc_dapm_add_routes(&card->dapm, cs35l56_sof_map, count);
>>>> +    if (ret) {
>>>> +        dev_err(card->dev, "Map add %d failed: %d\n", count, ret);
>>>> +        return ret;
>>>> +    }
>>>> +
>>>> +    /* Enable one feedback TX per amp on different slots */
>>>> +    for_each_rtd_codec_dais(rtd, i, codec_dai) {
>>>> +        ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 1 << i, 4, 16);
>>>
>>> TDM slots? Not getting how this would work with SoundWire?
>>>
>>
>> Strictly speaking Soundwire is TDM (the frame time is divided up into
>> slots for each sample...).
>>
>> The problem is if you have N amps on the dailink all feeding back audio
>> on the same bus. Their DP slots are all programmed to the same positions
>> in the frame, same as for the playback. So you have 4 amps all trying to
>> send 6 audio channels in the same positions in the frame and you'll just
>> get a ton of bus clash interrupts.
>>
>> So we use the set_tdm_slot() like we do with I2S TDM to set which slots
>> are active for each amp.
>>
>> I can't see that there's any obvious "generic" way that the manager code
>> can automatically figure out how many channels to enable on each amp and
>> what order to map them, so we do it here. Just as with I2S TDM - you
>> have many slots and many codecs but the machine driver has to tell it
>> how to map those.
> 
> IIRC Bard did the same thing recently, and the order of the feedback
> channels is really defined by the order in which the peripheral devices
> are added in the dailink. See
> https://github.com/thesofproject/linux/pull/4108
> 

I don't really see how it is determining how to split the channels.
I see there is a discussion about that but I didn't see any explanation
that I could understand.

Also that only seems to have been done for IPCv4. What if my system
uses IPCv3 ?

Anyway, I'll drop the machine driver patches for now until the IPC code
is stable and consistent behavior for v3 and v4.

> There's also another open related to the number of channels, we need to
> patch what the CPU DAI can handle, see
> https://github.com/thesofproject/linux/pull/4136 or
> https://github.com/thesofproject/linux/pull/4134
diff mbox series

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index 3d7aaa42b854..c40bae5e90ea 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5039,6 +5039,7 @@  F:	include/sound/cs*
 F:	sound/pci/hda/cs*
 F:	sound/pci/hda/hda_cs_dsp_ctl.*
 F:	sound/soc/codecs/cs*
+F:	sound/soc/intel/boards/sof_sdw_cs35l56.c
 
 CIRRUS LOGIC DSP FIRMWARE DRIVER
 M:	Simon Trimmer <simont@opensource.cirrus.com>
diff --git a/sound/soc/intel/boards/Kconfig b/sound/soc/intel/boards/Kconfig
index 99308ed85277..3e483bbf3ebe 100644
--- a/sound/soc/intel/boards/Kconfig
+++ b/sound/soc/intel/boards/Kconfig
@@ -662,6 +662,7 @@  config SND_SOC_INTEL_SOUNDWIRE_SOF_MACH
 	depends on MFD_INTEL_LPSS || COMPILE_TEST
 	depends on SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES || COMPILE_TEST
 	depends on SOUNDWIRE
+	select SND_SOC_CS35L56_SDW
 	select SND_SOC_MAX98373_I2C
 	select SND_SOC_MAX98373_SDW
 	select SND_SOC_RT700_SDW
diff --git a/sound/soc/intel/boards/Makefile b/sound/soc/intel/boards/Makefile
index d1fd7a2b32db..64e37f8e4a4b 100644
--- a/sound/soc/intel/boards/Makefile
+++ b/sound/soc/intel/boards/Makefile
@@ -37,6 +37,7 @@  snd-soc-sof_da7219_max98373-objs := sof_da7219_max98373.o
 snd-soc-ehl-rt5660-objs := ehl_rt5660.o
 snd-soc-sof-ssp-amp-objs := sof_ssp_amp.o
 snd-soc-sof-sdw-objs += sof_sdw.o				\
+			sof_sdw_cs35l56.o			\
 			sof_sdw_max98373.o sof_sdw_rt_amp.o	\
 			sof_sdw_rt5682.o sof_sdw_rt700.o	\
 			sof_sdw_rt711.o sof_sdw_rt711_sdca.o 	\
diff --git a/sound/soc/intel/boards/sof_sdw.c b/sound/soc/intel/boards/sof_sdw.c
index d2ed807abde9..6c679b4b963f 100644
--- a/sound/soc/intel/boards/sof_sdw.c
+++ b/sound/soc/intel/boards/sof_sdw.c
@@ -544,6 +544,13 @@  static struct sof_sdw_codec_info codec_info_list[] = {
 		.exit = sof_sdw_rt711_exit,
 		.codec_type = SOF_SDW_CODEC_TYPE_JACK,
 	},
+	{
+		.part_id = 0x3556,
+		.direction = {true, true},
+		.dai_name = "cs35l56-sdw1",
+		.init = sof_sdw_cs35l56_init,
+		.codec_type = SOF_SDW_CODEC_TYPE_AMP,
+	},
 	{
 		.part_id = 0x1308,
 		.acpi_id = "10EC1308",
diff --git a/sound/soc/intel/boards/sof_sdw_common.h b/sound/soc/intel/boards/sof_sdw_common.h
index 350010b0e5f4..1bb8e4a34c56 100644
--- a/sound/soc/intel/boards/sof_sdw_common.h
+++ b/sound/soc/intel/boards/sof_sdw_common.h
@@ -102,6 +102,13 @@  int sof_sdw_hdmi_card_late_probe(struct snd_soc_card *card);
 /* DMIC support */
 int sof_sdw_dmic_init(struct snd_soc_pcm_runtime *rtd);
 
+/* CS35L56 support */
+int sof_sdw_cs35l56_init(struct snd_soc_card *card,
+			 const struct snd_soc_acpi_link_adr *link,
+			 struct snd_soc_dai_link *dai_links,
+			 struct sof_sdw_codec_info *info,
+			 bool playback);
+
 /* RT711 support */
 int sof_sdw_rt711_init(struct snd_soc_card *card,
 		       const struct snd_soc_acpi_link_adr *link,
diff --git a/sound/soc/intel/boards/sof_sdw_cs35l56.c b/sound/soc/intel/boards/sof_sdw_cs35l56.c
new file mode 100644
index 000000000000..b862567fd9e2
--- /dev/null
+++ b/sound/soc/intel/boards/sof_sdw_cs35l56.c
@@ -0,0 +1,115 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+// Based on sof_sdw_rt5682.c
+// Copyright (c) 2020 Intel Corporation
+
+/*
+ *  sof_sdw_cs35l56 - Helpers to handle CS35L56 from generic machine driver
+ */
+
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <sound/soc.h>
+#include <sound/soc-acpi.h>
+#include <sound/soc-dai.h>
+#include "sof_sdw_common.h"
+
+static const struct snd_soc_dapm_widget cs35l56_sof_widgets[] = {
+	SND_SOC_DAPM_SPK("Speaker", NULL),
+	SND_SOC_DAPM_LINE("Monitor", NULL),
+};
+
+static const struct snd_soc_dapm_route cs35l56_sof_map[] = {
+	{ "Speaker", NULL, "AMP1 SPK" },
+	{ "Speaker", NULL, "AMP2 SPK" },
+	{ "Speaker", NULL, "AMP3 SPK" },
+	{ "Speaker", NULL, "AMP4 SPK" },
+};
+
+static int cs35l56_init_multiple(struct snd_soc_pcm_runtime *rtd, int count)
+{
+	struct snd_soc_card *card = rtd->card;
+	struct snd_soc_dai *codec_dai;
+	int i, ret;
+
+	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
+					  "%s hs:cs35l56",
+					  card->components);
+	if (!card->components)
+		return -ENOMEM;
+
+	ret = snd_soc_dapm_new_controls(&card->dapm,
+					cs35l56_sof_widgets, ARRAY_SIZE(cs35l56_sof_widgets));
+	if (ret) {
+		dev_err(card->dev, "Widgets add failed: %d\n", ret);
+		return ret;
+	}
+
+	ret = snd_soc_dapm_add_routes(&card->dapm, cs35l56_sof_map, count);
+	if (ret) {
+		dev_err(card->dev, "Map add %d failed: %d\n", count, ret);
+		return ret;
+	}
+
+	/* Enable one feedback TX per amp on different slots */
+	for_each_rtd_codec_dais(rtd, i, codec_dai) {
+		ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 1 << i, 4, 16);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int cs35l56_init_one(struct snd_soc_pcm_runtime *rtd)
+{
+	return cs35l56_init_multiple(rtd, 1);
+}
+
+static int cs35l56_init_two(struct snd_soc_pcm_runtime *rtd)
+{
+	return cs35l56_init_multiple(rtd, 2);
+}
+
+static int cs35l56_init_three(struct snd_soc_pcm_runtime *rtd)
+{
+	return cs35l56_init_multiple(rtd, 3);
+}
+
+static int cs35l56_init_four(struct snd_soc_pcm_runtime *rtd)
+{
+	return cs35l56_init_multiple(rtd, 4);
+}
+
+int sof_sdw_cs35l56_init(struct snd_soc_card *card,
+			 const struct snd_soc_acpi_link_adr *link,
+			 struct snd_soc_dai_link *dai_links,
+			 struct sof_sdw_codec_info *info,
+			 bool playback)
+{
+	/* Init with playback dai link only. */
+	if (!playback)
+		return 0;
+
+	info->amp_num++;
+
+	switch (info->amp_num) {
+	case 1:
+		dai_links->init = cs35l56_init_one;
+		break;
+	case 2:
+		dai_links->init = cs35l56_init_two;
+		break;
+	case 3:
+		dai_links->init = cs35l56_init_three;
+		break;
+	case 4:
+		dai_links->init = cs35l56_init_four;
+		break;
+	default:
+		dev_err(card->dev, "%s: Unexpected number of amps %d\n", __func__, info->amp_num);
+		dai_links->init = cs35l56_init_four;
+		break;
+	}
+
+	return 0;
+}