Skip to content

Commit 647dc46

Browse files
committed
Merge remote-tracking branch 'asoc/topic/symmetry' into asoc-ssm2602
2 parents e42644c + 62e5f67 commit 647dc46

3 files changed

Lines changed: 138 additions & 28 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: 130 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,117 @@ 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+
}
111135
}
112136

113137
return 0;
114138
}
115139

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;
178+
}
179+
180+
return 0;
181+
}
182+
183+
static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
184+
{
185+
struct snd_soc_pcm_runtime *rtd = substream->private_data;
186+
struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
187+
struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver;
188+
struct snd_soc_dai_link *link = rtd->dai_link;
189+
190+
return cpu_driver->symmetric_rates || codec_driver->symmetric_rates ||
191+
link->symmetric_rates || cpu_driver->symmetric_channels ||
192+
codec_driver->symmetric_channels || link->symmetric_channels ||
193+
cpu_driver->symmetric_samplebits ||
194+
codec_driver->symmetric_samplebits ||
195+
link->symmetric_samplebits;
196+
}
197+
116198
/*
117199
* List of sample sizes that might go over the bus for parameter
118200
* application. There ought to be a wildcard sample size for things
@@ -242,6 +324,9 @@ static int soc_pcm_open(struct snd_pcm_substream *substream)
242324
&cpu_dai_drv->capture);
243325
}
244326

327+
if (soc_pcm_has_symmetry(substream))
328+
runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
329+
245330
ret = -EINVAL;
246331
snd_pcm_limit_hw_rates(runtime);
247332
if (!runtime->hw.rates) {
@@ -383,13 +468,6 @@ static int soc_pcm_close(struct snd_pcm_substream *substream)
383468
codec_dai->active--;
384469
codec->active--;
385470

386-
/* clear the corresponding DAIs rate when inactive */
387-
if (!cpu_dai->active)
388-
cpu_dai->rate = 0;
389-
390-
if (!codec_dai->active)
391-
codec_dai->rate = 0;
392-
393471
/* Muting the DAC suppresses artifacts caused during digital
394472
* shutdown, for example from stopping clocks.
395473
*/
@@ -525,6 +603,10 @@ static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
525603

526604
mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
527605

606+
ret = soc_pcm_params_symmetry(substream, params);
607+
if (ret)
608+
goto out;
609+
528610
if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
529611
ret = rtd->dai_link->ops->hw_params(substream, params);
530612
if (ret < 0) {
@@ -561,9 +643,16 @@ static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
561643
}
562644
}
563645

564-
/* store the rate for each DAIs */
646+
/* store the parameters for each DAIs */
565647
cpu_dai->rate = params_rate(params);
648+
cpu_dai->channels = params_channels(params);
649+
cpu_dai->sample_bits =
650+
snd_pcm_format_physical_width(params_format(params));
651+
566652
codec_dai->rate = params_rate(params);
653+
codec_dai->channels = params_channels(params);
654+
codec_dai->sample_bits =
655+
snd_pcm_format_physical_width(params_format(params));
567656

568657
out:
569658
mutex_unlock(&rtd->pcm_mutex);
@@ -598,6 +687,19 @@ static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
598687

599688
mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
600689

690+
/* clear the corresponding DAIs parameters when going to be inactive */
691+
if (cpu_dai->active == 1) {
692+
cpu_dai->rate = 0;
693+
cpu_dai->channels = 0;
694+
cpu_dai->sample_bits = 0;
695+
}
696+
697+
if (codec_dai->active == 1) {
698+
codec_dai->rate = 0;
699+
codec_dai->channels = 0;
700+
codec_dai->sample_bits = 0;
701+
}
702+
601703
/* apply codec digital mute */
602704
if (!codec->active)
603705
snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);

0 commit comments

Comments
 (0)