Skip to contentSkip to conversation

ASoC: core: Add support for DAI multicodec

RFC Superseded Benoit Cousson 2 participants 6 messages Mar 11, 2014 – Mar 13, 2014

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 ASoC: core: Add support for DAI multicodec

From: Misael Lopez Cruz <[email protected]>

DAI link assumes a one to one mapping between CPU DAI and CODEC. In some cases, the same CPU DAI can be connected to several codecs. This is the case for example, if you connect two mono codecs to the same I2S link in order to have a stereo card.
The current ASoC implementation does not allow such setup.

Add support for DAI links composed of a single CPU DAI and multiple CODECs. Sound cards have to pass the CODECs array in the corresponding DAI link through a new 'snd_soc_dai_link_codec' struct. Each CODEC in this array is described in the same manner single CODEC DAIs are (either DT/OF node or codec_name).

CPU DAI in a multicodec DAI link can have more channels than what each CODEC has. The number of channels each CODEC is responsible for is machine specific, hence it's fixed up in machine drivers in a similar way to what DPCM does.

Add few helpers to make the code more readable and fix a trailing space in the header as well.

[[email protected]: Adapt the original series to 3.14]

---

Hi Mark and Liam,

I'm not sure this is the only way to address that problem, but both Misa and myself were facing the same issue on different platforms, and we didn't find any straighforward way to do that.

To avoid breaking all the exiting ASoC implementation, this approach is adding extra attributes for multiple codecs and does not enforce moving every single codec pointer to a multicodec array.

The patch is based on 3.14-rc6.

Comments are welcome.

Thanks,
Benoit

---
 include/sound/soc.h  |  20 ++
 sound/soc/soc-core.c | 511 ++++++++++++++++++++++++++++++++++-----------------
 sound/soc/soc-dapm.c |  39 ++--
 sound/soc/soc-pcm.c  | 412 ++++++++++++++++++++++++++++-------------
 4 files changed, 677 insertions(+), 305 deletions(-)
include/sound/soc.h +20 -0
index 9a00147..c17351c 100644
@@ -818,6 +818,19 @@ struct snd_soc_platform_driver {
 	int (*bespoke_trigger)(struct snd_pcm_substream *, int);
 };
 
+struct snd_soc_dai_link_codec {
+	const char *codec_name;
+	const struct device_node *codec_of_node;
+	const char *codec_dai_name;
+
+	struct snd_soc_codec *codec;
+	struct snd_soc_dai *codec_dai;
+
+	int (*hw_params_fixup)(struct snd_soc_pcm_runtime *rtd,
+			       struct snd_pcm_hw_params *params);
+	struct snd_pcm_hw_params hw_params;
+};
+
 struct snd_soc_platform {
 	const char *name;
 	int id;
@@ -866,6 +879,10 @@ struct snd_soc_dai_link {
 	const struct device_node *codec_of_node;
 	/* You MUST specify the DAI name within the codec */
 	const char *codec_dai_name;
+
+	struct snd_soc_dai_link_codec *codecs;
+	int num_codecs;
+
 	/*
 	 * You MAY specify the link's platform/PCM/DMA driver, either by
 	 * device name, or by DT/OF node, but not both. Some forms of link
@@ -1055,6 +1072,9 @@ struct snd_soc_pcm_runtime {
 	struct snd_soc_dai *codec_dai;
 	struct snd_soc_dai *cpu_dai;
 
+	struct snd_soc_dai_link_codec *codecs;
+	int num_codecs;
+
 	struct delayed_work delayed_work;
 #ifdef CONFIG_DEBUG_FS
 	struct dentry *debugfs_dpcm_root;
sound/soc/soc-core.c +345 -166

741 lines not on this page · raw message

sound/soc/soc-dapm.c +28 -11
index b9dc6ac..f4b4e2a 100644
@@ -2192,12 +2192,8 @@ int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
 }
 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
 
-/* show dapm widget status in sys fs */
-static ssize_t dapm_widget_show(struct device *dev,
-	struct device_attribute *attr, char *buf)
+static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
 {
-	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
-	struct snd_soc_codec *codec =rtd->codec;
 	struct snd_soc_dapm_widget *w;
 	int count = 0;
 	char *state = "not set";
@@ -2250,6 +2246,22 @@ static ssize_t dapm_widget_show(struct device *dev,
 	return count;
 }
 
+/* show dapm widget status in sys fs */
+static ssize_t dapm_widget_show(struct device *dev,
+	struct device_attribute *attr, char *buf)
+{
+	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
+	struct snd_soc_codec *codec;
+	int i, count = 0;
+
+	for (i = 0; i < rtd->num_codecs; i++) {
+		codec = rtd->codecs[i].codec;
+		count += dapm_widget_show_codec(codec, buf + count);
+	}
+
+	return count;
+}
+
 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
 
 int snd_soc_dapm_sys_add(struct device *dev)
@@ -3684,13 +3696,12 @@ void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
 	}
 }
 
-static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
-	int event)
+static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
+				  struct snd_soc_dai *cpu_dai,
+				  struct snd_soc_dai *codec_dai,
+				  int stream, int event)
 {
-
 	struct snd_soc_dapm_widget *w_cpu, *w_codec;
-	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
-	struct snd_soc_dai *codec_dai = rtd->codec_dai;
 
 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
 		w_cpu = cpu_dai->playback_widget;
@@ -3756,9 +3767,15 @@ void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
 			      int event)
 {
 	struct snd_soc_card *card = rtd->card;
+	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
+	struct snd_soc_dai *codec_dai;
+	int i;
 
 	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
-	soc_dapm_stream_event(rtd, stream, event);
+	for (i = 0; i < rtd->num_codecs; i++) {
+		codec_dai = rtd->codecs[i].codec_dai;
+		soc_dapm_stream_event(rtd, cpu_dai, codec_dai, stream, event);
+	}
 	mutex_unlock(&card->dapm_mutex);
 }
 
sound/soc/soc-pcm.c +284 -128

676 lines not on this page · raw message

Signature · 1 line
1.8.3.2
maintainer
#1 lore raw
Quoted 7 lines · Benoit Cousson

From: Misael Lopez Cruz <[email protected]>

DAI link assumes a one to one mapping between CPU DAI and CODEC. In some cases, the same CPU DAI can be connected to several codecs. This is the case for example, if you connect two mono codecs to the same I2S link in order to have a stereo card.
The current ASoC implementation does not allow such setup.

First up thanks for working on this, it's a feature which has been requested for a long time but nobody stepped forward to do it before now.

This is rather large so I've not had time to review it today, I'll try to get at least a first pass at that done tomorrow. I did notice that in your comment about rebasing you mentioned a series - it'd be good if we could see this as a series, splitting it up would make review easier.

Quoted 4 lines

CPU DAI in a multicodec DAI link can have more channels than what each CODEC has. The number of channels each CODEC is responsible for is machine specific, hence it's fixed up in machine drivers in a similar way to what DPCM does.

This one is interesting. It feels like most things will want a static mapping because that's what the hardware does but there will doubtless be things that could use flexibility. Liam has looked at this in the past (more for TDM IIRC, I thought about it for that as well and I seem to recall Liam's ideas covering it). It feels like we should start out with static mappings and build up dynamic later on but equally well getting something merged would mean we could improve on it.

Quoted 1 line

The patch is based on 3.14-rc6.

For such an invasive set of changes it's probably worth working off -next, I forsee conflicts.

#2 lore raw

Hi Mark,

Quoted 9 lines · Mark Brown
Quoted 7 lines · Benoit Cousson

From: Misael Lopez Cruz <[email protected]>

DAI link assumes a one to one mapping between CPU DAI and CODEC. In some cases, the same CPU DAI can be connected to several codecs. This is the case for example, if you connect two mono codecs to the same I2S link in order to have a stereo card.
The current ASoC implementation does not allow such setup.

Quoted 8 lines

First up thanks for working on this, it's a feature which has been requested for a long time but nobody stepped forward to do it before now.

This is rather large so I've not had time to review it today, I'll try to get at least a first pass at that done tomorrow. I did notice that in your comment about rebasing you mentioned a series - it'd be good if we could see this as a series, splitting it up would make review easier.

Yeah, I know. The issue is that the original series was done on a 3.8 Android branch with few refactor patches before that one to introduce some helpers. During the 3.8 -> 3.13 rebase then 3.13 -> 3.14 rebase, a lot of them did not survive due to internal asoc changes and cleanups that happened since 3.8.

At least, there are still few helpers that can be introduced sooner, but it will still make the main patch pretty huge. I don't think we can make the transition in smaller chunks.

Quoted 13 lines
Quoted 4 lines

CPU DAI in a multicodec DAI link can have more channels than what each CODEC has. The number of channels each CODEC is responsible for is machine specific, hence it's fixed up in machine drivers in a similar way to what DPCM does.

This one is interesting. It feels like most things will want a static mapping because that's what the hardware does but there will doubtless be things that could use flexibility. Liam has looked at this in the past (more for TDM IIRC, I thought about it for that as well and I seem to recall Liam's ideas covering it). It feels like we should start out with static mappings and build up dynamic later on but equally well getting something merged would mean we could improve on it.

Quoted 4 lines
Quoted 1 line

The patch is based on 3.14-rc6.

For such an invasive set of changes it's probably worth working off -next, I forsee conflicts.

Indeed, I've just tried rebasing it on asoc/next and it does conflict :-( But not a major conflict anyway. I'll do that for the repost.

Thanks for the comments,
Benoit

Signature · 4 lines
Benoît Cousson
BayLibre
Embedded Linux Technology Lab
www.baylibre.com
maintainer
#3 lore raw
In reply to Benoit Cousson

include/sound/soc.h struct snd_soc_platform_driver { · line 832

Quoted 12 lines · Benoit Cousson
include/sound/soc.h +12 -0
+struct snd_soc_dai_link_codec {
+	const char *codec_name;
+	const struct device_node *codec_of_node;
+	const char *codec_dai_name;
+
+	struct snd_soc_codec *codec;
+	struct snd_soc_dai *codec_dai;
+
+	int (*hw_params_fixup)(struct snd_soc_pcm_runtime *rtd,
+			       struct snd_pcm_hw_params *params);
+	struct snd_pcm_hw_params hw_params;
+};

The implementation looks basically fine, it's possible there's something nasty in there but the patch is rather large and not quite repetitive enough. Most of the interface seems good - I'm not super thrilled with having the separate CODECs list but equally well the idea of updating all the machine drivers isn't super awesome either and should be punted for a cleanup run later.

I would like to see something nicer for the fixup though - I think we can avoid doing it if we use the TDM API to specify the slots that are in use by a CODEC. Xiubo has done some nice work there recently which is handy. Instead of having a fixup function if we specified a TDM and channel map configuration then the core core could override the params so that the channel count was clamped by how many channels are actually being sent to the device - so if there's two TDM slots active the device would be told to play stereo. Would that work for your use cases?

#4 lore raw
Quoted 18 lines · Mark Brown

include/sound/soc.h struct snd_soc_platform_driver { · line 832

Quoted 12 lines · Benoit Cousson
include/sound/soc.h +12 -0
+struct snd_soc_dai_link_codec {
+	const char *codec_name;
+	const struct device_node *codec_of_node;
+	const char *codec_dai_name;
+
+	struct snd_soc_codec *codec;
+	struct snd_soc_dai *codec_dai;
+
+	int (*hw_params_fixup)(struct snd_soc_pcm_runtime *rtd,
+			       struct snd_pcm_hw_params *params);
+	struct snd_pcm_hw_params hw_params;
+};

The implementation looks basically fine, it's possible there's something nasty in there but the patch is rather large and not quite repetitive enough.

I think some non-regression tests on that series will be good, because, so far it was used on Misa platform (dra7-evm AFAIR) and my own BBB + audio cape platform and BBB + 2 mono Codecs from NXP (to be upstreamed soon :-)).

We need to ensure that it will not break the thousand ASoC implementations we have in mainline today.

Quoted 4 lines

Most of the interface seems good - I'm not super thrilled with having the separate CODECs list but equally well the idea of updating all the machine drivers isn't super awesome either and should be punted for a cleanup run later.

Yes, that's as well the point I don't like, but couldn't find an easy and smooth transition solution. And I guess Misa had the same concern before.

Quoted 9 lines

I would like to see something nicer for the fixup though - I think we can avoid doing it if we use the TDM API to specify the slots that are in use by a CODEC. Xiubo has done some nice work there recently which is handy. Instead of having a fixup function if we specified a TDM and channel map configuration then the core core could override the params so that the channel count was clamped by how many channels are actually being sent to the device - so if there's two TDM slots active the device would be told to play stereo. Would that work for your use cases?

Yes, I think so. My current setup is pretty basic: 2 mono Class D amplifiers connected to the same I2S link to build a stereo card.

OK, so I'll rebase the patch to asoc/next. I'll try to split the huge patch at least into a series of 2 or 3 patches, and I'll remove the fixup part.

Thanks,
Benoit

Signature · 4 lines
Benoît Cousson
BayLibre
Embedded Linux Technology Lab
www.baylibre.com
maintainer
#5 lore raw
Quoted 10 lines · Benoit Cousson
Quoted 8 lines · Mark Brown

On Tue, Mar 11, 2014 at 12:17:24PM +0100, Benoit Cousson wrote:

is handy. Instead of having a fixup function if we specified a TDM and channel map configuration then the core core could override the params so that the channel count was clamped by how many channels are actually being sent to the device - so if there's two TDM slots active the device would be told to play stereo. Would that work for your use cases?

Quoted 5 lines

Yes, I think so. My current setup is pretty basic: 2 mono Class D amplifiers connected to the same I2S link to build a stereo card.

OK, so I'll rebase the patch to asoc/next. I'll try to split the huge patch at least into a series of 2 or 3 patches, and I'll remove the fixup part.

Sounds good. We will need some configuration here so I think adding TDM parameters to the various structs (I guess slot size and overall counts should go in the link rather than per CODEC) but we could probably even get away with doing this after the core patch.