blob: 57cbce4fd8368782f12957f1f97efa97efb5ca59 [file] [log] [blame]
Thomas Gleixnerda607e12019-05-29 16:57:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +09002/*
3 * digi00x-pcm.c - a part of driver for Digidesign Digi 002/003 family
4 *
5 * Copyright (c) 2014-2015 Takashi Sakamoto
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +09006 */
7
8#include "digi00x.h"
9
10static int hw_rule_rate(struct snd_pcm_hw_params *params,
11 struct snd_pcm_hw_rule *rule)
12{
13 struct snd_interval *r =
14 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
15 const struct snd_interval *c =
16 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
17 struct snd_interval t = {
18 .min = UINT_MAX, .max = 0, .integer = 1,
19 };
20 unsigned int i;
21
22 for (i = 0; i < SND_DG00X_RATE_COUNT; i++) {
23 if (!snd_interval_test(c,
24 snd_dg00x_stream_pcm_channels[i]))
25 continue;
26
27 t.min = min(t.min, snd_dg00x_stream_rates[i]);
28 t.max = max(t.max, snd_dg00x_stream_rates[i]);
29 }
30
31 return snd_interval_refine(r, &t);
32}
33
34static int hw_rule_channels(struct snd_pcm_hw_params *params,
35 struct snd_pcm_hw_rule *rule)
36{
37 struct snd_interval *c =
38 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
39 const struct snd_interval *r =
40 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
41 struct snd_interval t = {
42 .min = UINT_MAX, .max = 0, .integer = 1,
43 };
44 unsigned int i;
45
46 for (i = 0; i < SND_DG00X_RATE_COUNT; i++) {
47 if (!snd_interval_test(r, snd_dg00x_stream_rates[i]))
48 continue;
49
50 t.min = min(t.min, snd_dg00x_stream_pcm_channels[i]);
51 t.max = max(t.max, snd_dg00x_stream_pcm_channels[i]);
52 }
53
54 return snd_interval_refine(c, &t);
55}
56
57static int pcm_init_hw_params(struct snd_dg00x *dg00x,
58 struct snd_pcm_substream *substream)
59{
Takashi Sakamoto55799c52017-06-08 09:11:03 +090060 struct snd_pcm_runtime *runtime = substream->runtime;
61 struct snd_pcm_hardware *hw = &runtime->hw;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +090062 struct amdtp_stream *s;
63 int err;
64
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +090065
66 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
67 substream->runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
68 s = &dg00x->tx_stream;
69 } else {
Takashi Sakamotoa02cb8f2017-05-22 22:22:21 +090070 substream->runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +090071 s = &dg00x->rx_stream;
72 }
73
Takashi Sakamoto55799c52017-06-08 09:11:03 +090074 hw->channels_min = 10;
75 hw->channels_max = 18;
76
77 hw->rates = SNDRV_PCM_RATE_44100 |
78 SNDRV_PCM_RATE_48000 |
79 SNDRV_PCM_RATE_88200 |
80 SNDRV_PCM_RATE_96000;
81 snd_pcm_limit_hw_rates(runtime);
82
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +090083 err = snd_pcm_hw_rule_add(substream->runtime, 0,
84 SNDRV_PCM_HW_PARAM_CHANNELS,
85 hw_rule_channels, NULL,
86 SNDRV_PCM_HW_PARAM_RATE, -1);
87 if (err < 0)
88 return err;
89
90 err = snd_pcm_hw_rule_add(substream->runtime, 0,
91 SNDRV_PCM_HW_PARAM_RATE,
92 hw_rule_rate, NULL,
93 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
94 if (err < 0)
95 return err;
96
97 return amdtp_dot_add_pcm_hw_constraints(s, substream->runtime);
98}
99
100static int pcm_open(struct snd_pcm_substream *substream)
101{
102 struct snd_dg00x *dg00x = substream->private_data;
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900103 struct amdtp_domain *d = &dg00x->domain;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900104 enum snd_dg00x_clock clock;
105 bool detect;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900106 int err;
107
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900108 err = snd_dg00x_stream_lock_try(dg00x);
109 if (err < 0)
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900110 return err;
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900111
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900112 err = pcm_init_hw_params(dg00x, substream);
113 if (err < 0)
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900114 goto err_locked;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900115
116 /* Check current clock source. */
117 err = snd_dg00x_stream_get_clock(dg00x, &clock);
118 if (err < 0)
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900119 goto err_locked;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900120 if (clock != SND_DG00X_CLOCK_INTERNAL) {
121 err = snd_dg00x_stream_check_external_clock(dg00x, &detect);
122 if (err < 0)
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900123 goto err_locked;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900124 if (!detect) {
125 err = -EBUSY;
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900126 goto err_locked;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900127 }
128 }
129
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900130 mutex_lock(&dg00x->mutex);
131
132 // When source of clock is not internal or any stream is reserved for
133 // transmission of PCM frames, the available sampling rate is limited
134 // at current one.
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900135 if ((clock != SND_DG00X_CLOCK_INTERNAL) ||
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900136 (dg00x->substreams_counter > 0 && d->events_per_period > 0)) {
137 unsigned int frames_per_period = d->events_per_period;
Takashi Sakamoto76c4ecb2019-10-18 00:54:18 +0900138 unsigned int frames_per_buffer = d->events_per_buffer;
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900139 unsigned int rate;
140
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900141 err = snd_dg00x_stream_get_external_rate(dg00x, &rate);
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900142 if (err < 0) {
143 mutex_unlock(&dg00x->mutex);
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900144 goto err_locked;
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900145 }
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900146 substream->runtime->hw.rate_min = rate;
147 substream->runtime->hw.rate_max = rate;
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900148
149 if (frames_per_period > 0) {
150 err = snd_pcm_hw_constraint_minmax(substream->runtime,
151 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
152 frames_per_period, frames_per_period);
153 if (err < 0) {
154 mutex_unlock(&dg00x->mutex);
155 goto err_locked;
156 }
Takashi Sakamoto76c4ecb2019-10-18 00:54:18 +0900157
158 err = snd_pcm_hw_constraint_minmax(substream->runtime,
159 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
160 frames_per_buffer, frames_per_buffer);
161 if (err < 0) {
162 mutex_unlock(&dg00x->mutex);
163 goto err_locked;
164 }
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900165 }
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900166 }
167
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900168 mutex_unlock(&dg00x->mutex);
169
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900170 snd_pcm_set_sync(substream);
Takashi Sakamotoc36f8fc2019-10-07 20:05:29 +0900171
172 return 0;
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900173err_locked:
174 snd_dg00x_stream_lock_release(dg00x);
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900175 return err;
176}
177
178static int pcm_close(struct snd_pcm_substream *substream)
179{
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900180 struct snd_dg00x *dg00x = substream->private_data;
181
182 snd_dg00x_stream_lock_release(dg00x);
183
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900184 return 0;
185}
186
Takashi Sakamoto478f3a52019-06-11 22:21:13 +0900187static int pcm_hw_params(struct snd_pcm_substream *substream,
188 struct snd_pcm_hw_params *hw_params)
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900189{
190 struct snd_dg00x *dg00x = substream->private_data;
191 int err;
192
Takashi Iwaia3f4f2d2019-11-05 16:18:42 +0100193 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900194 if (err < 0)
195 return err;
196
197 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
Takashi Sakamotoae8ffbb2019-06-11 22:21:11 +0900198 unsigned int rate = params_rate(hw_params);
Takashi Sakamoto18b7f182019-10-07 20:05:21 +0900199 unsigned int frames_per_period = params_period_size(hw_params);
Takashi Sakamoto76c4ecb2019-10-18 00:54:18 +0900200 unsigned int frames_per_buffer = params_buffer_size(hw_params);
Takashi Sakamotoae8ffbb2019-06-11 22:21:11 +0900201
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900202 mutex_lock(&dg00x->mutex);
Takashi Sakamoto18b7f182019-10-07 20:05:21 +0900203 err = snd_dg00x_stream_reserve_duplex(dg00x, rate,
Takashi Sakamoto76c4ecb2019-10-18 00:54:18 +0900204 frames_per_period, frames_per_buffer);
Takashi Sakamotoae8ffbb2019-06-11 22:21:11 +0900205 if (err >= 0)
206 ++dg00x->substreams_counter;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900207 mutex_unlock(&dg00x->mutex);
208 }
209
Takashi Sakamotoae8ffbb2019-06-11 22:21:11 +0900210 return err;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900211}
212
Takashi Sakamoto478f3a52019-06-11 22:21:13 +0900213static int pcm_hw_free(struct snd_pcm_substream *substream)
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900214{
215 struct snd_dg00x *dg00x = substream->private_data;
216
217 mutex_lock(&dg00x->mutex);
218
219 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
Takashi Sakamotoae8ffbb2019-06-11 22:21:11 +0900220 --dg00x->substreams_counter;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900221
222 snd_dg00x_stream_stop_duplex(dg00x);
223
224 mutex_unlock(&dg00x->mutex);
225
Takashi Iwaia3f4f2d2019-11-05 16:18:42 +0100226 return snd_pcm_lib_free_pages(substream);
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900227}
228
229static int pcm_capture_prepare(struct snd_pcm_substream *substream)
230{
231 struct snd_dg00x *dg00x = substream->private_data;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900232 int err;
233
234 mutex_lock(&dg00x->mutex);
235
Takashi Sakamotoae8ffbb2019-06-11 22:21:11 +0900236 err = snd_dg00x_stream_start_duplex(dg00x);
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900237 if (err >= 0)
238 amdtp_stream_pcm_prepare(&dg00x->tx_stream);
239
240 mutex_unlock(&dg00x->mutex);
241
242 return err;
243}
244
245static int pcm_playback_prepare(struct snd_pcm_substream *substream)
246{
247 struct snd_dg00x *dg00x = substream->private_data;
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900248 int err;
249
250 mutex_lock(&dg00x->mutex);
251
Takashi Sakamotoae8ffbb2019-06-11 22:21:11 +0900252 err = snd_dg00x_stream_start_duplex(dg00x);
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900253 if (err >= 0) {
254 amdtp_stream_pcm_prepare(&dg00x->rx_stream);
255 amdtp_dot_reset(&dg00x->rx_stream);
256 }
257
258 mutex_unlock(&dg00x->mutex);
259
260 return err;
261}
262
263static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
264{
265 struct snd_dg00x *dg00x = substream->private_data;
266
267 switch (cmd) {
268 case SNDRV_PCM_TRIGGER_START:
269 amdtp_stream_pcm_trigger(&dg00x->tx_stream, substream);
270 break;
271 case SNDRV_PCM_TRIGGER_STOP:
272 amdtp_stream_pcm_trigger(&dg00x->tx_stream, NULL);
273 break;
274 default:
275 return -EINVAL;
276 }
277
278 return 0;
279}
280
281static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
282{
283 struct snd_dg00x *dg00x = substream->private_data;
284
285 switch (cmd) {
286 case SNDRV_PCM_TRIGGER_START:
287 amdtp_stream_pcm_trigger(&dg00x->rx_stream, substream);
288 break;
289 case SNDRV_PCM_TRIGGER_STOP:
290 amdtp_stream_pcm_trigger(&dg00x->rx_stream, NULL);
291 break;
292 default:
293 return -EINVAL;
294 }
295
296 return 0;
297}
298
299static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
300{
301 struct snd_dg00x *dg00x = sbstrm->private_data;
302
Takashi Sakamotof890f9a2019-10-18 15:19:07 +0900303 return amdtp_domain_stream_pcm_pointer(&dg00x->domain, &dg00x->tx_stream);
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900304}
305
306static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
307{
308 struct snd_dg00x *dg00x = sbstrm->private_data;
309
Takashi Sakamotof890f9a2019-10-18 15:19:07 +0900310 return amdtp_domain_stream_pcm_pointer(&dg00x->domain, &dg00x->rx_stream);
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900311}
312
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900313static int pcm_capture_ack(struct snd_pcm_substream *substream)
314{
315 struct snd_dg00x *dg00x = substream->private_data;
316
Takashi Sakamotoe6dcc922019-10-18 15:19:08 +0900317 return amdtp_domain_stream_pcm_ack(&dg00x->domain, &dg00x->tx_stream);
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900318}
319
320static int pcm_playback_ack(struct snd_pcm_substream *substream)
321{
322 struct snd_dg00x *dg00x = substream->private_data;
323
Takashi Sakamotoe6dcc922019-10-18 15:19:08 +0900324 return amdtp_domain_stream_pcm_ack(&dg00x->domain, &dg00x->rx_stream);
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900325}
326
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900327int snd_dg00x_create_pcm_devices(struct snd_dg00x *dg00x)
328{
Takashi Sakamotoa4e86cb2017-01-05 21:48:11 +0900329 static const struct snd_pcm_ops capture_ops = {
330 .open = pcm_open,
331 .close = pcm_close,
332 .ioctl = snd_pcm_lib_ioctl,
Takashi Sakamoto478f3a52019-06-11 22:21:13 +0900333 .hw_params = pcm_hw_params,
334 .hw_free = pcm_hw_free,
Takashi Sakamotoa4e86cb2017-01-05 21:48:11 +0900335 .prepare = pcm_capture_prepare,
336 .trigger = pcm_capture_trigger,
337 .pointer = pcm_capture_pointer,
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900338 .ack = pcm_capture_ack,
Takashi Sakamotoa4e86cb2017-01-05 21:48:11 +0900339 };
340 static const struct snd_pcm_ops playback_ops = {
341 .open = pcm_open,
342 .close = pcm_close,
343 .ioctl = snd_pcm_lib_ioctl,
Takashi Sakamoto478f3a52019-06-11 22:21:13 +0900344 .hw_params = pcm_hw_params,
345 .hw_free = pcm_hw_free,
Takashi Sakamotoa4e86cb2017-01-05 21:48:11 +0900346 .prepare = pcm_playback_prepare,
347 .trigger = pcm_playback_trigger,
348 .pointer = pcm_playback_pointer,
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900349 .ack = pcm_playback_ack,
Takashi Sakamotoa4e86cb2017-01-05 21:48:11 +0900350 };
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900351 struct snd_pcm *pcm;
352 int err;
353
354 err = snd_pcm_new(dg00x->card, dg00x->card->driver, 0, 1, 1, &pcm);
355 if (err < 0)
356 return err;
357
358 pcm->private_data = dg00x;
359 snprintf(pcm->name, sizeof(pcm->name),
360 "%s PCM", dg00x->card->shortname);
Takashi Sakamotoa4e86cb2017-01-05 21:48:11 +0900361 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
362 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
Takashi Iwaia3f4f2d2019-11-05 16:18:42 +0100363 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
364 NULL, 0, 0);
Takashi Sakamoto0120d0f2015-09-30 09:39:20 +0900365
366 return 0;
367}