Skip to content

Commit 3635bf0

Browse files
Nicolin Chenbroonie
authored andcommitted
ASoC: soc-pcm: add symmetry for channels and sample bits
Some SoCs can only work in mono or stereo mode at one time. So if we let them capture a mono stream while playing a stereo stream, there might be a problem occur to one of these two streams: double paced or slowed down. In soc-pcm.c, we have soc_pcm_apply_symmetry() to apply the rate symmetry. But we don't have one for channels. Likewise, we can treat symmetric_rate as a solution for those SoCs or CODECs which can not handle asymmetrical LRCLK. But it's also impossible for them to handle asymmetrical BCLK. And accodring to BCLK = LRCLK * channel number * slot size(fixed or sample bits), sample bits might also be a problem if they are not using a fixed slot size. Thus, this patch applys symmetry for channels and sample bits. Meanwhile, there might be a race between two substreams if starting simultaneously. Previously, we only added warning to compalin but still using conservative way to let it carry on. However, this patch rejects the second stream with any unmatched parameter to make sure the first existing stream won't be broken. Signed-off-by: Nicolin Chen <b42378@freescale.com> Signed-off-by: Mark Brown <broonie@linaro.org>
1 parent 6ce4eac commit 3635bf0

3 files changed

Lines changed: 115 additions & 23 deletions

File tree

‎include/sound/soc-dai.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ struct snd_soc_dai_driver {
220220
struct snd_soc_pcm_stream capture;
221221
struct snd_soc_pcm_stream playback;
222222
unsigned int symmetric_rates:1;
223+
unsigned int symmetric_channels:1;
224+
unsigned int symmetric_samplebits:1;
223225

224226
/* probe ordering - for components with runtime dependencies */
225227
int probe_order;
@@ -244,6 +246,8 @@ struct snd_soc_dai {
244246
unsigned int capture_active:1; /* stream is in use */
245247
unsigned int playback_active:1; /* stream is in use */
246248
unsigned int symmetric_rates:1;
249+
unsigned int symmetric_channels:1;
250+
unsigned int symmetric_samplebits:1;
247251
struct snd_pcm_runtime *runtime;
248252
unsigned int active;
249253
unsigned char probed:1;
@@ -258,6 +262,8 @@ struct snd_soc_dai {
258262

259263
/* Symmetry data - only valid if symmetry is being enforced */
260264
unsigned int rate;
265+
unsigned int channels;
266+
unsigned int sample_bits;
261267

262268
/* parent platform/codec */
263269
struct snd_soc_platform *platform;

‎include/sound/soc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,8 @@ struct snd_soc_dai_link {
879879

880880
/* Symmetry requirements */
881881
unsigned int symmetric_rates:1;
882+
unsigned int symmetric_channels:1;
883+
unsigned int symmetric_samplebits:1;
882884

883885
/* Do not create a PCM for this DAI link (Backend link) */
884886
unsigned int no_pcm:1;

‎sound/soc/soc-pcm.c‎

Lines changed: 107 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,97 @@ static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
8484
struct snd_soc_pcm_runtime *rtd = substream->private_data;
8585
int ret;
8686

87-
if (!soc_dai->driver->symmetric_rates &&
88-
!rtd->dai_link->symmetric_rates)
89-
return 0;
87+
if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
88+
rtd->dai_link->symmetric_rates)) {
89+
dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
90+
soc_dai->rate);
91+
92+
ret = snd_pcm_hw_constraint_minmax(substream->runtime,
93+
SNDRV_PCM_HW_PARAM_RATE,
94+
soc_dai->rate, soc_dai->rate);
95+
if (ret < 0) {
96+
dev_err(soc_dai->dev,
97+
"ASoC: Unable to apply rate constraint: %d\n",
98+
ret);
99+
return ret;
100+
}
101+
}
90102

91-
/* This can happen if multiple streams are starting simultaneously -
92-
* the second can need to get its constraints before the first has
93-
* picked a rate. Complain and allow the application to carry on.
94-
*/
95-
if (!soc_dai->rate) {
96-
dev_warn(soc_dai->dev,
97-
"ASoC: Not enforcing symmetric_rates due to race\n");
98-
return 0;
103+
if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
104+
rtd->dai_link->symmetric_channels)) {
105+
dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
106+
soc_dai->channels);
107+
108+
ret = snd_pcm_hw_constraint_minmax(substream->runtime,
109+
SNDRV_PCM_HW_PARAM_CHANNELS,
110+
soc_dai->channels,
111+
soc_dai->channels);
112+
if (ret < 0) {
113+
dev_err(soc_dai->dev,
114+
"ASoC: Unable to apply channel symmetry constraint: %d\n",
115+
ret);
116+
return ret;
117+
}
99118
}
100119

101-
dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
120+
if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
121+
rtd->dai_link->symmetric_samplebits)) {
122+
dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
123+
soc_dai->sample_bits);
102124

103-
ret = snd_pcm_hw_constraint_minmax(substream->runtime,
104-
SNDRV_PCM_HW_PARAM_RATE,
105-
soc_dai->rate, soc_dai->rate);
106-
if (ret < 0) {
107-
dev_err(soc_dai->dev,
108-
"ASoC: Unable to apply rate symmetry constraint: %d\n",
109-
ret);
110-
return ret;
125+
ret = snd_pcm_hw_constraint_minmax(substream->runtime,
126+
SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
127+
soc_dai->sample_bits,
128+
soc_dai->sample_bits);
129+
if (ret < 0) {
130+
dev_err(soc_dai->dev,
131+
"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
132+
ret);
133+
return ret;
134+
}
135+
}
136+
137+
return 0;
138+
}
139+
140+
static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
141+
struct snd_pcm_hw_params *params)
142+
{
143+
struct snd_soc_pcm_runtime *rtd = substream->private_data;
144+
struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
145+
struct snd_soc_dai *codec_dai = rtd->codec_dai;
146+
unsigned int rate, channels, sample_bits, symmetry;
147+
148+
rate = params_rate(params);
149+
channels = params_channels(params);
150+
sample_bits = snd_pcm_format_physical_width(params_format(params));
151+
152+
/* reject unmatched parameters when applying symmetry */
153+
symmetry = cpu_dai->driver->symmetric_rates ||
154+
codec_dai->driver->symmetric_rates ||
155+
rtd->dai_link->symmetric_rates;
156+
if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
157+
dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
158+
cpu_dai->rate, rate);
159+
return -EINVAL;
160+
}
161+
162+
symmetry = cpu_dai->driver->symmetric_channels ||
163+
codec_dai->driver->symmetric_channels ||
164+
rtd->dai_link->symmetric_channels;
165+
if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
166+
dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
167+
cpu_dai->channels, channels);
168+
return -EINVAL;
169+
}
170+
171+
symmetry = cpu_dai->driver->symmetric_samplebits ||
172+
codec_dai->driver->symmetric_samplebits ||
173+
rtd->dai_link->symmetric_samplebits;
174+
if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
175+
dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
176+
cpu_dai->sample_bits, sample_bits);
177+
return -EINVAL;
111178
}
112179

113180
return 0;
@@ -384,11 +451,17 @@ static int soc_pcm_close(struct snd_pcm_substream *substream)
384451
codec->active--;
385452

386453
/* clear the corresponding DAIs rate when inactive */
387-
if (!cpu_dai->active)
454+
if (!cpu_dai->active) {
388455
cpu_dai->rate = 0;
456+
cpu_dai->channels = 0;
457+
cpu_dai->sample_bits = 0;
458+
}
389459

390-
if (!codec_dai->active)
460+
if (!codec_dai->active) {
391461
codec_dai->rate = 0;
462+
codec_dai->channels = 0;
463+
codec_dai->sample_bits = 0;
464+
}
392465

393466
/* Muting the DAC suppresses artifacts caused during digital
394467
* shutdown, for example from stopping clocks.
@@ -525,6 +598,10 @@ static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
525598

526599
mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
527600

601+
ret = soc_pcm_params_symmetry(substream, params);
602+
if (ret)
603+
goto out;
604+
528605
if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
529606
ret = rtd->dai_link->ops->hw_params(substream, params);
530607
if (ret < 0) {
@@ -561,9 +638,16 @@ static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
561638
}
562639
}
563640

564-
/* store the rate for each DAIs */
641+
/* store the parameters for each DAIs */
565642
cpu_dai->rate = params_rate(params);
643+
cpu_dai->channels = params_channels(params);
644+
cpu_dai->sample_bits =
645+
snd_pcm_format_physical_width(params_format(params));
646+
566647
codec_dai->rate = params_rate(params);
648+
codec_dai->channels = params_channels(params);
649+
codec_dai->sample_bits =
650+
snd_pcm_format_physical_width(params_format(params));
567651

568652
out:
569653
mutex_unlock(&rtd->pcm_mutex);

0 commit comments

Comments
 (0)