interconnect: Add a common standard aggregate function
[linux/fpc-iii.git] / sound / firewire / motu / motu-pcm.c
blob0059709310303c07a11bce1c04a551ac6502cdce
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * motu-pcm.c - a part of driver for MOTU FireWire series
5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 */
8 #include <sound/pcm_params.h>
9 #include "motu.h"
11 static int motu_rate_constraint(struct snd_pcm_hw_params *params,
12 struct snd_pcm_hw_rule *rule)
14 struct snd_motu_packet_format *formats = rule->private;
16 const struct snd_interval *c =
17 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
18 struct snd_interval *r =
19 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
20 struct snd_interval rates = {
21 .min = UINT_MAX, .max = 0, .integer = 1
23 unsigned int i, pcm_channels, rate, mode;
25 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
26 rate = snd_motu_clock_rates[i];
27 mode = i / 2;
29 pcm_channels = formats->fixed_part_pcm_chunks[mode] +
30 formats->differed_part_pcm_chunks[mode];
31 if (!snd_interval_test(c, pcm_channels))
32 continue;
34 rates.min = min(rates.min, rate);
35 rates.max = max(rates.max, rate);
38 return snd_interval_refine(r, &rates);
41 static int motu_channels_constraint(struct snd_pcm_hw_params *params,
42 struct snd_pcm_hw_rule *rule)
44 struct snd_motu_packet_format *formats = rule->private;
46 const struct snd_interval *r =
47 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
48 struct snd_interval *c =
49 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
50 struct snd_interval channels = {
51 .min = UINT_MAX, .max = 0, .integer = 1
53 unsigned int i, pcm_channels, rate, mode;
55 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
56 rate = snd_motu_clock_rates[i];
57 mode = i / 2;
59 if (!snd_interval_test(r, rate))
60 continue;
62 pcm_channels = formats->fixed_part_pcm_chunks[mode] +
63 formats->differed_part_pcm_chunks[mode];
64 channels.min = min(channels.min, pcm_channels);
65 channels.max = max(channels.max, pcm_channels);
68 return snd_interval_refine(c, &channels);
71 static void limit_channels_and_rates(struct snd_motu *motu,
72 struct snd_pcm_runtime *runtime,
73 struct snd_motu_packet_format *formats)
75 struct snd_pcm_hardware *hw = &runtime->hw;
76 unsigned int i, pcm_channels, rate, mode;
78 hw->channels_min = UINT_MAX;
79 hw->channels_max = 0;
81 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
82 rate = snd_motu_clock_rates[i];
83 mode = i / 2;
85 pcm_channels = formats->fixed_part_pcm_chunks[mode] +
86 formats->differed_part_pcm_chunks[mode];
87 if (pcm_channels == 0)
88 continue;
90 hw->rates |= snd_pcm_rate_to_rate_bit(rate);
91 hw->channels_min = min(hw->channels_min, pcm_channels);
92 hw->channels_max = max(hw->channels_max, pcm_channels);
95 snd_pcm_limit_hw_rates(runtime);
98 static int init_hw_info(struct snd_motu *motu,
99 struct snd_pcm_substream *substream)
101 struct snd_pcm_runtime *runtime = substream->runtime;
102 struct snd_pcm_hardware *hw = &runtime->hw;
103 struct amdtp_stream *stream;
104 struct snd_motu_packet_format *formats;
105 int err;
107 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
108 hw->formats = SNDRV_PCM_FMTBIT_S32;
109 stream = &motu->tx_stream;
110 formats = &motu->tx_packet_formats;
111 } else {
112 hw->formats = SNDRV_PCM_FMTBIT_S32;
113 stream = &motu->rx_stream;
114 formats = &motu->rx_packet_formats;
117 limit_channels_and_rates(motu, runtime, formats);
119 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
120 motu_rate_constraint, formats,
121 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
122 if (err < 0)
123 return err;
124 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
125 motu_channels_constraint, formats,
126 SNDRV_PCM_HW_PARAM_RATE, -1);
127 if (err < 0)
128 return err;
130 return amdtp_motu_add_pcm_hw_constraints(stream, runtime);
133 static int pcm_open(struct snd_pcm_substream *substream)
135 struct snd_motu *motu = substream->private_data;
136 const struct snd_motu_protocol *const protocol = motu->spec->protocol;
137 struct amdtp_domain *d = &motu->domain;
138 enum snd_motu_clock_source src;
139 int err;
141 err = snd_motu_stream_lock_try(motu);
142 if (err < 0)
143 return err;
145 mutex_lock(&motu->mutex);
147 err = snd_motu_stream_cache_packet_formats(motu);
148 if (err < 0)
149 goto err_locked;
151 err = init_hw_info(motu, substream);
152 if (err < 0)
153 goto err_locked;
155 err = protocol->get_clock_source(motu, &src);
156 if (err < 0)
157 goto err_locked;
159 // When source of clock is not internal or any stream is reserved for
160 // transmission of PCM frames, the available sampling rate is limited
161 // at current one.
162 if ((src != SND_MOTU_CLOCK_SOURCE_INTERNAL &&
163 src != SND_MOTU_CLOCK_SOURCE_SPH) ||
164 (motu->substreams_counter > 0 && d->events_per_period > 0)) {
165 unsigned int frames_per_period = d->events_per_period;
166 unsigned int frames_per_buffer = d->events_per_buffer;
167 unsigned int rate;
169 err = protocol->get_clock_rate(motu, &rate);
170 if (err < 0)
171 goto err_locked;
173 substream->runtime->hw.rate_min = rate;
174 substream->runtime->hw.rate_max = rate;
176 if (frames_per_period > 0) {
177 err = snd_pcm_hw_constraint_minmax(substream->runtime,
178 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
179 frames_per_period, frames_per_period);
180 if (err < 0)
181 goto err_locked;
183 err = snd_pcm_hw_constraint_minmax(substream->runtime,
184 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
185 frames_per_buffer, frames_per_buffer);
186 if (err < 0)
187 goto err_locked;
191 snd_pcm_set_sync(substream);
193 mutex_unlock(&motu->mutex);
195 return 0;
196 err_locked:
197 mutex_unlock(&motu->mutex);
198 snd_motu_stream_lock_release(motu);
199 return err;
202 static int pcm_close(struct snd_pcm_substream *substream)
204 struct snd_motu *motu = substream->private_data;
206 snd_motu_stream_lock_release(motu);
208 return 0;
211 static int pcm_hw_params(struct snd_pcm_substream *substream,
212 struct snd_pcm_hw_params *hw_params)
214 struct snd_motu *motu = substream->private_data;
215 int err;
217 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
218 if (err < 0)
219 return err;
221 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
222 unsigned int rate = params_rate(hw_params);
223 unsigned int frames_per_period = params_period_size(hw_params);
224 unsigned int frames_per_buffer = params_buffer_size(hw_params);
226 mutex_lock(&motu->mutex);
227 err = snd_motu_stream_reserve_duplex(motu, rate,
228 frames_per_period, frames_per_buffer);
229 if (err >= 0)
230 ++motu->substreams_counter;
231 mutex_unlock(&motu->mutex);
234 return err;
237 static int pcm_hw_free(struct snd_pcm_substream *substream)
239 struct snd_motu *motu = substream->private_data;
241 mutex_lock(&motu->mutex);
243 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
244 --motu->substreams_counter;
246 snd_motu_stream_stop_duplex(motu);
248 mutex_unlock(&motu->mutex);
250 return snd_pcm_lib_free_pages(substream);
253 static int capture_prepare(struct snd_pcm_substream *substream)
255 struct snd_motu *motu = substream->private_data;
256 int err;
258 mutex_lock(&motu->mutex);
259 err = snd_motu_stream_start_duplex(motu);
260 mutex_unlock(&motu->mutex);
261 if (err >= 0)
262 amdtp_stream_pcm_prepare(&motu->tx_stream);
264 return 0;
266 static int playback_prepare(struct snd_pcm_substream *substream)
268 struct snd_motu *motu = substream->private_data;
269 int err;
271 mutex_lock(&motu->mutex);
272 err = snd_motu_stream_start_duplex(motu);
273 mutex_unlock(&motu->mutex);
274 if (err >= 0)
275 amdtp_stream_pcm_prepare(&motu->rx_stream);
277 return err;
280 static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
282 struct snd_motu *motu = substream->private_data;
284 switch (cmd) {
285 case SNDRV_PCM_TRIGGER_START:
286 amdtp_stream_pcm_trigger(&motu->tx_stream, substream);
287 break;
288 case SNDRV_PCM_TRIGGER_STOP:
289 amdtp_stream_pcm_trigger(&motu->tx_stream, NULL);
290 break;
291 default:
292 return -EINVAL;
295 return 0;
297 static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
299 struct snd_motu *motu = substream->private_data;
301 switch (cmd) {
302 case SNDRV_PCM_TRIGGER_START:
303 amdtp_stream_pcm_trigger(&motu->rx_stream, substream);
304 break;
305 case SNDRV_PCM_TRIGGER_STOP:
306 amdtp_stream_pcm_trigger(&motu->rx_stream, NULL);
307 break;
308 default:
309 return -EINVAL;
312 return 0;
315 static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
317 struct snd_motu *motu = substream->private_data;
319 return amdtp_domain_stream_pcm_pointer(&motu->domain, &motu->tx_stream);
321 static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
323 struct snd_motu *motu = substream->private_data;
325 return amdtp_domain_stream_pcm_pointer(&motu->domain, &motu->rx_stream);
328 static int capture_ack(struct snd_pcm_substream *substream)
330 struct snd_motu *motu = substream->private_data;
332 return amdtp_domain_stream_pcm_ack(&motu->domain, &motu->tx_stream);
335 static int playback_ack(struct snd_pcm_substream *substream)
337 struct snd_motu *motu = substream->private_data;
339 return amdtp_domain_stream_pcm_ack(&motu->domain, &motu->rx_stream);
342 int snd_motu_create_pcm_devices(struct snd_motu *motu)
344 static const struct snd_pcm_ops capture_ops = {
345 .open = pcm_open,
346 .close = pcm_close,
347 .ioctl = snd_pcm_lib_ioctl,
348 .hw_params = pcm_hw_params,
349 .hw_free = pcm_hw_free,
350 .prepare = capture_prepare,
351 .trigger = capture_trigger,
352 .pointer = capture_pointer,
353 .ack = capture_ack,
355 static const struct snd_pcm_ops playback_ops = {
356 .open = pcm_open,
357 .close = pcm_close,
358 .ioctl = snd_pcm_lib_ioctl,
359 .hw_params = pcm_hw_params,
360 .hw_free = pcm_hw_free,
361 .prepare = playback_prepare,
362 .trigger = playback_trigger,
363 .pointer = playback_pointer,
364 .ack = playback_ack,
366 struct snd_pcm *pcm;
367 int err;
369 err = snd_pcm_new(motu->card, motu->card->driver, 0, 1, 1, &pcm);
370 if (err < 0)
371 return err;
372 pcm->private_data = motu;
373 strcpy(pcm->name, motu->card->shortname);
375 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
376 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
377 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
378 NULL, 0, 0);
380 return 0;