1 // SPDX-License-Identifier: GPL-2.0-only
3 * ff-pcm.c - a part of driver for RME Fireface series
5 * Copyright (c) 2015-2017 Takashi Sakamoto
10 static int hw_rule_rate(struct snd_pcm_hw_params *params,
11 struct snd_pcm_hw_rule *rule)
13 const unsigned int *pcm_channels = rule->private;
14 struct snd_interval *r =
15 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
16 const struct snd_interval *c =
17 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
18 struct snd_interval t = {
19 .min = UINT_MAX, .max = 0, .integer = 1
23 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
24 enum snd_ff_stream_mode mode;
27 err = snd_ff_stream_get_multiplier_mode(i, &mode);
31 if (!snd_interval_test(c, pcm_channels[mode]))
34 t.min = min(t.min, amdtp_rate_table[i]);
35 t.max = max(t.max, amdtp_rate_table[i]);
38 return snd_interval_refine(r, &t);
41 static int hw_rule_channels(struct snd_pcm_hw_params *params,
42 struct snd_pcm_hw_rule *rule)
44 const unsigned int *pcm_channels = rule->private;
45 struct snd_interval *c =
46 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
47 const struct snd_interval *r =
48 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
49 struct snd_interval t = {
50 .min = UINT_MAX, .max = 0, .integer = 1
54 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
55 enum snd_ff_stream_mode mode;
58 err = snd_ff_stream_get_multiplier_mode(i, &mode);
62 if (!snd_interval_test(r, amdtp_rate_table[i]))
65 t.min = min(t.min, pcm_channels[mode]);
66 t.max = max(t.max, pcm_channels[mode]);
69 return snd_interval_refine(c, &t);
72 static void limit_channels_and_rates(struct snd_pcm_hardware *hw,
73 const unsigned int *pcm_channels)
75 unsigned int rate, channels;
78 hw->channels_min = UINT_MAX;
80 hw->rate_min = UINT_MAX;
83 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
84 enum snd_ff_stream_mode mode;
87 err = snd_ff_stream_get_multiplier_mode(i, &mode);
91 channels = pcm_channels[mode];
92 if (pcm_channels[mode] == 0)
94 hw->channels_min = min(hw->channels_min, channels);
95 hw->channels_max = max(hw->channels_max, channels);
97 rate = amdtp_rate_table[i];
98 hw->rates |= snd_pcm_rate_to_rate_bit(rate);
99 hw->rate_min = min(hw->rate_min, rate);
100 hw->rate_max = max(hw->rate_max, rate);
104 static int pcm_init_hw_params(struct snd_ff *ff,
105 struct snd_pcm_substream *substream)
107 struct snd_pcm_runtime *runtime = substream->runtime;
108 struct amdtp_stream *s;
109 const unsigned int *pcm_channels;
112 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
113 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
115 pcm_channels = ff->spec->pcm_capture_channels;
117 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
119 pcm_channels = ff->spec->pcm_playback_channels;
122 limit_channels_and_rates(&runtime->hw, pcm_channels);
124 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
125 hw_rule_channels, (void *)pcm_channels,
126 SNDRV_PCM_HW_PARAM_RATE, -1);
130 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
131 hw_rule_rate, (void *)pcm_channels,
132 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
136 return amdtp_ff_add_pcm_hw_constraints(s, runtime);
139 static int pcm_open(struct snd_pcm_substream *substream)
141 struct snd_ff *ff = substream->private_data;
143 enum snd_ff_clock_src src;
146 err = snd_ff_stream_lock_try(ff);
150 err = pcm_init_hw_params(ff, substream);
154 err = ff->spec->protocol->get_clock(ff, &rate, &src);
158 if (src != SND_FF_CLOCK_SRC_INTERNAL) {
159 for (i = 0; i < CIP_SFC_COUNT; ++i) {
160 if (amdtp_rate_table[i] == rate)
164 * The unit is configured at sampling frequency which packet
165 * streaming engine can't support.
167 if (i >= CIP_SFC_COUNT) {
172 substream->runtime->hw.rate_min = rate;
173 substream->runtime->hw.rate_max = rate;
175 if (amdtp_stream_pcm_running(&ff->rx_stream) ||
176 amdtp_stream_pcm_running(&ff->tx_stream)) {
177 rate = amdtp_rate_table[ff->rx_stream.sfc];
178 substream->runtime->hw.rate_min = rate;
179 substream->runtime->hw.rate_max = rate;
183 snd_pcm_set_sync(substream);
188 snd_ff_stream_lock_release(ff);
192 static int pcm_close(struct snd_pcm_substream *substream)
194 struct snd_ff *ff = substream->private_data;
196 snd_ff_stream_lock_release(ff);
201 static int pcm_hw_params(struct snd_pcm_substream *substream,
202 struct snd_pcm_hw_params *hw_params)
204 struct snd_ff *ff = substream->private_data;
207 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
208 params_buffer_bytes(hw_params));
212 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
213 unsigned int rate = params_rate(hw_params);
215 mutex_lock(&ff->mutex);
216 err = snd_ff_stream_reserve_duplex(ff, rate);
218 ++ff->substreams_counter;
219 mutex_unlock(&ff->mutex);
225 static int pcm_hw_free(struct snd_pcm_substream *substream)
227 struct snd_ff *ff = substream->private_data;
229 mutex_lock(&ff->mutex);
231 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
232 --ff->substreams_counter;
234 snd_ff_stream_stop_duplex(ff);
236 mutex_unlock(&ff->mutex);
238 return snd_pcm_lib_free_vmalloc_buffer(substream);
241 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
243 struct snd_ff *ff = substream->private_data;
244 struct snd_pcm_runtime *runtime = substream->runtime;
247 mutex_lock(&ff->mutex);
249 err = snd_ff_stream_start_duplex(ff, runtime->rate);
251 amdtp_stream_pcm_prepare(&ff->tx_stream);
253 mutex_unlock(&ff->mutex);
258 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
260 struct snd_ff *ff = substream->private_data;
261 struct snd_pcm_runtime *runtime = substream->runtime;
264 mutex_lock(&ff->mutex);
266 err = snd_ff_stream_start_duplex(ff, runtime->rate);
268 amdtp_stream_pcm_prepare(&ff->rx_stream);
270 mutex_unlock(&ff->mutex);
275 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
277 struct snd_ff *ff = substream->private_data;
280 case SNDRV_PCM_TRIGGER_START:
281 amdtp_stream_pcm_trigger(&ff->tx_stream, substream);
283 case SNDRV_PCM_TRIGGER_STOP:
284 amdtp_stream_pcm_trigger(&ff->tx_stream, NULL);
293 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
295 struct snd_ff *ff = substream->private_data;
298 case SNDRV_PCM_TRIGGER_START:
299 amdtp_stream_pcm_trigger(&ff->rx_stream, substream);
301 case SNDRV_PCM_TRIGGER_STOP:
302 amdtp_stream_pcm_trigger(&ff->rx_stream, NULL);
311 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
313 struct snd_ff *ff = sbstrm->private_data;
315 return amdtp_stream_pcm_pointer(&ff->tx_stream);
318 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
320 struct snd_ff *ff = sbstrm->private_data;
322 return amdtp_stream_pcm_pointer(&ff->rx_stream);
325 static int pcm_capture_ack(struct snd_pcm_substream *substream)
327 struct snd_ff *ff = substream->private_data;
329 return amdtp_stream_pcm_ack(&ff->tx_stream);
332 static int pcm_playback_ack(struct snd_pcm_substream *substream)
334 struct snd_ff *ff = substream->private_data;
336 return amdtp_stream_pcm_ack(&ff->rx_stream);
339 int snd_ff_create_pcm_devices(struct snd_ff *ff)
341 static const struct snd_pcm_ops pcm_capture_ops = {
344 .ioctl = snd_pcm_lib_ioctl,
345 .hw_params = pcm_hw_params,
346 .hw_free = pcm_hw_free,
347 .prepare = pcm_capture_prepare,
348 .trigger = pcm_capture_trigger,
349 .pointer = pcm_capture_pointer,
350 .ack = pcm_capture_ack,
351 .page = snd_pcm_lib_get_vmalloc_page,
353 static const struct snd_pcm_ops pcm_playback_ops = {
356 .ioctl = snd_pcm_lib_ioctl,
357 .hw_params = pcm_hw_params,
358 .hw_free = pcm_hw_free,
359 .prepare = pcm_playback_prepare,
360 .trigger = pcm_playback_trigger,
361 .pointer = pcm_playback_pointer,
362 .ack = pcm_playback_ack,
363 .page = snd_pcm_lib_get_vmalloc_page,
368 err = snd_pcm_new(ff->card, ff->card->driver, 0, 1, 1, &pcm);
372 pcm->private_data = ff;
373 snprintf(pcm->name, sizeof(pcm->name),
374 "%s PCM", ff->card->shortname);
375 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
376 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);