1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e453df44STakashi Sakamoto /*
3e453df44STakashi Sakamoto * tascam-pcm.c - a part of driver for TASCAM FireWire series
4e453df44STakashi Sakamoto *
5e453df44STakashi Sakamoto * Copyright (c) 2015 Takashi Sakamoto
6e453df44STakashi Sakamoto */
7e453df44STakashi Sakamoto
8e453df44STakashi Sakamoto #include "tascam.h"
9e453df44STakashi Sakamoto
pcm_init_hw_params(struct snd_tscm * tscm,struct snd_pcm_substream * substream)10e453df44STakashi Sakamoto static int pcm_init_hw_params(struct snd_tscm *tscm,
11e453df44STakashi Sakamoto struct snd_pcm_substream *substream)
12e453df44STakashi Sakamoto {
13e453df44STakashi Sakamoto struct snd_pcm_runtime *runtime = substream->runtime;
1455799c5aSTakashi Sakamoto struct snd_pcm_hardware *hw = &runtime->hw;
15e453df44STakashi Sakamoto struct amdtp_stream *stream;
16e453df44STakashi Sakamoto unsigned int pcm_channels;
17e453df44STakashi Sakamoto
18e453df44STakashi Sakamoto if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
19e453df44STakashi Sakamoto runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
20e453df44STakashi Sakamoto stream = &tscm->tx_stream;
21e453df44STakashi Sakamoto pcm_channels = tscm->spec->pcm_capture_analog_channels;
22e453df44STakashi Sakamoto } else {
23a02cb8f8STakashi Sakamoto runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
24e453df44STakashi Sakamoto stream = &tscm->rx_stream;
25e453df44STakashi Sakamoto pcm_channels = tscm->spec->pcm_playback_analog_channels;
26e453df44STakashi Sakamoto }
27e453df44STakashi Sakamoto
28e453df44STakashi Sakamoto if (tscm->spec->has_adat)
29e453df44STakashi Sakamoto pcm_channels += 8;
30e453df44STakashi Sakamoto if (tscm->spec->has_spdif)
31e453df44STakashi Sakamoto pcm_channels += 2;
32e453df44STakashi Sakamoto runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels;
33e453df44STakashi Sakamoto
3455799c5aSTakashi Sakamoto hw->rates = SNDRV_PCM_RATE_44100 |
3555799c5aSTakashi Sakamoto SNDRV_PCM_RATE_48000 |
3655799c5aSTakashi Sakamoto SNDRV_PCM_RATE_88200 |
3755799c5aSTakashi Sakamoto SNDRV_PCM_RATE_96000;
3855799c5aSTakashi Sakamoto snd_pcm_limit_hw_rates(runtime);
39e453df44STakashi Sakamoto
40e453df44STakashi Sakamoto return amdtp_tscm_add_pcm_hw_constraints(stream, runtime);
41e453df44STakashi Sakamoto }
42e453df44STakashi Sakamoto
pcm_open(struct snd_pcm_substream * substream)43e453df44STakashi Sakamoto static int pcm_open(struct snd_pcm_substream *substream)
44e453df44STakashi Sakamoto {
45e453df44STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
466669a11dSTakashi Sakamoto struct amdtp_domain *d = &tscm->domain;
47e453df44STakashi Sakamoto enum snd_tscm_clock clock;
48e453df44STakashi Sakamoto int err;
49e453df44STakashi Sakamoto
50e5e0c3ddSTakashi Sakamoto err = snd_tscm_stream_lock_try(tscm);
51e5e0c3ddSTakashi Sakamoto if (err < 0)
526669a11dSTakashi Sakamoto return err;
53e5e0c3ddSTakashi Sakamoto
54e453df44STakashi Sakamoto err = pcm_init_hw_params(tscm, substream);
55e453df44STakashi Sakamoto if (err < 0)
56e5e0c3ddSTakashi Sakamoto goto err_locked;
57e453df44STakashi Sakamoto
58e453df44STakashi Sakamoto err = snd_tscm_stream_get_clock(tscm, &clock);
592617120fSTakashi Sakamoto if (err < 0)
602617120fSTakashi Sakamoto goto err_locked;
612617120fSTakashi Sakamoto
626669a11dSTakashi Sakamoto mutex_lock(&tscm->mutex);
636669a11dSTakashi Sakamoto
646669a11dSTakashi Sakamoto // When source of clock is not internal or any stream is reserved for
656669a11dSTakashi Sakamoto // transmission of PCM frames, the available sampling rate is limited
666669a11dSTakashi Sakamoto // at current one.
676669a11dSTakashi Sakamoto if (clock != SND_TSCM_CLOCK_INTERNAL || tscm->substreams_counter > 0) {
686669a11dSTakashi Sakamoto unsigned int frames_per_period = d->events_per_period;
69128307d5STakashi Sakamoto unsigned int frames_per_buffer = d->events_per_buffer;
706669a11dSTakashi Sakamoto unsigned int rate;
716669a11dSTakashi Sakamoto
72e453df44STakashi Sakamoto err = snd_tscm_stream_get_rate(tscm, &rate);
736669a11dSTakashi Sakamoto if (err < 0) {
746669a11dSTakashi Sakamoto mutex_unlock(&tscm->mutex);
75e5e0c3ddSTakashi Sakamoto goto err_locked;
766669a11dSTakashi Sakamoto }
77e453df44STakashi Sakamoto substream->runtime->hw.rate_min = rate;
78e453df44STakashi Sakamoto substream->runtime->hw.rate_max = rate;
796669a11dSTakashi Sakamoto
806669a11dSTakashi Sakamoto err = snd_pcm_hw_constraint_minmax(substream->runtime,
816669a11dSTakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
826669a11dSTakashi Sakamoto frames_per_period, frames_per_period);
836669a11dSTakashi Sakamoto if (err < 0) {
846669a11dSTakashi Sakamoto mutex_unlock(&tscm->mutex);
856669a11dSTakashi Sakamoto goto err_locked;
866669a11dSTakashi Sakamoto }
87128307d5STakashi Sakamoto
88128307d5STakashi Sakamoto err = snd_pcm_hw_constraint_minmax(substream->runtime,
89128307d5STakashi Sakamoto SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
90128307d5STakashi Sakamoto frames_per_buffer, frames_per_buffer);
91128307d5STakashi Sakamoto if (err < 0) {
92128307d5STakashi Sakamoto mutex_unlock(&tscm->mutex);
93128307d5STakashi Sakamoto goto err_locked;
94128307d5STakashi Sakamoto }
95e453df44STakashi Sakamoto }
96e453df44STakashi Sakamoto
976669a11dSTakashi Sakamoto mutex_unlock(&tscm->mutex);
986669a11dSTakashi Sakamoto
99e453df44STakashi Sakamoto snd_pcm_set_sync(substream);
1006669a11dSTakashi Sakamoto
1016669a11dSTakashi Sakamoto return 0;
102e5e0c3ddSTakashi Sakamoto err_locked:
103e5e0c3ddSTakashi Sakamoto snd_tscm_stream_lock_release(tscm);
104e453df44STakashi Sakamoto return err;
105e453df44STakashi Sakamoto }
106e453df44STakashi Sakamoto
pcm_close(struct snd_pcm_substream * substream)107e453df44STakashi Sakamoto static int pcm_close(struct snd_pcm_substream *substream)
108e453df44STakashi Sakamoto {
109e5e0c3ddSTakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
110e5e0c3ddSTakashi Sakamoto
111e5e0c3ddSTakashi Sakamoto snd_tscm_stream_lock_release(tscm);
112e5e0c3ddSTakashi Sakamoto
113e453df44STakashi Sakamoto return 0;
114e453df44STakashi Sakamoto }
115e453df44STakashi Sakamoto
pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)116d8f291b7STakashi Sakamoto static int pcm_hw_params(struct snd_pcm_substream *substream,
117e453df44STakashi Sakamoto struct snd_pcm_hw_params *hw_params)
118e453df44STakashi Sakamoto {
119e453df44STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
1207641d549STakashi Iwai int err = 0;
121e453df44STakashi Sakamoto
122*23cb0767STakashi Iwai if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
12307b26642STakashi Sakamoto unsigned int rate = params_rate(hw_params);
124262542edSTakashi Sakamoto unsigned int frames_per_period = params_period_size(hw_params);
125128307d5STakashi Sakamoto unsigned int frames_per_buffer = params_buffer_size(hw_params);
12607b26642STakashi Sakamoto
127e453df44STakashi Sakamoto mutex_lock(&tscm->mutex);
128262542edSTakashi Sakamoto err = snd_tscm_stream_reserve_duplex(tscm, rate,
129128307d5STakashi Sakamoto frames_per_period, frames_per_buffer);
13007b26642STakashi Sakamoto if (err >= 0)
13107b26642STakashi Sakamoto ++tscm->substreams_counter;
132e453df44STakashi Sakamoto mutex_unlock(&tscm->mutex);
133e453df44STakashi Sakamoto }
134e453df44STakashi Sakamoto
13507b26642STakashi Sakamoto return err;
136e453df44STakashi Sakamoto }
137e453df44STakashi Sakamoto
pcm_hw_free(struct snd_pcm_substream * substream)138d8f291b7STakashi Sakamoto static int pcm_hw_free(struct snd_pcm_substream *substream)
139e453df44STakashi Sakamoto {
140e453df44STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
141e453df44STakashi Sakamoto
142e453df44STakashi Sakamoto mutex_lock(&tscm->mutex);
143e453df44STakashi Sakamoto
144*23cb0767STakashi Iwai if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
14507b26642STakashi Sakamoto --tscm->substreams_counter;
146e453df44STakashi Sakamoto
147e453df44STakashi Sakamoto snd_tscm_stream_stop_duplex(tscm);
148e453df44STakashi Sakamoto
149e453df44STakashi Sakamoto mutex_unlock(&tscm->mutex);
150e453df44STakashi Sakamoto
1517641d549STakashi Iwai return 0;
152e453df44STakashi Sakamoto }
153e453df44STakashi Sakamoto
pcm_capture_prepare(struct snd_pcm_substream * substream)154e453df44STakashi Sakamoto static int pcm_capture_prepare(struct snd_pcm_substream *substream)
155e453df44STakashi Sakamoto {
156e453df44STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
157e453df44STakashi Sakamoto struct snd_pcm_runtime *runtime = substream->runtime;
158e453df44STakashi Sakamoto int err;
159e453df44STakashi Sakamoto
160e453df44STakashi Sakamoto mutex_lock(&tscm->mutex);
161e453df44STakashi Sakamoto
162e453df44STakashi Sakamoto err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
163e453df44STakashi Sakamoto if (err >= 0)
164e453df44STakashi Sakamoto amdtp_stream_pcm_prepare(&tscm->tx_stream);
165e453df44STakashi Sakamoto
166e453df44STakashi Sakamoto mutex_unlock(&tscm->mutex);
167e453df44STakashi Sakamoto
168e453df44STakashi Sakamoto return err;
169e453df44STakashi Sakamoto }
170e453df44STakashi Sakamoto
pcm_playback_prepare(struct snd_pcm_substream * substream)171e453df44STakashi Sakamoto static int pcm_playback_prepare(struct snd_pcm_substream *substream)
172e453df44STakashi Sakamoto {
173e453df44STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
174e453df44STakashi Sakamoto struct snd_pcm_runtime *runtime = substream->runtime;
175e453df44STakashi Sakamoto int err;
176e453df44STakashi Sakamoto
177e453df44STakashi Sakamoto mutex_lock(&tscm->mutex);
178e453df44STakashi Sakamoto
179e453df44STakashi Sakamoto err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
180e453df44STakashi Sakamoto if (err >= 0)
181e453df44STakashi Sakamoto amdtp_stream_pcm_prepare(&tscm->rx_stream);
182e453df44STakashi Sakamoto
183e453df44STakashi Sakamoto mutex_unlock(&tscm->mutex);
184e453df44STakashi Sakamoto
185e453df44STakashi Sakamoto return err;
186e453df44STakashi Sakamoto }
187e453df44STakashi Sakamoto
pcm_capture_trigger(struct snd_pcm_substream * substream,int cmd)188e453df44STakashi Sakamoto static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
189e453df44STakashi Sakamoto {
190e453df44STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
191e453df44STakashi Sakamoto
192e453df44STakashi Sakamoto switch (cmd) {
193e453df44STakashi Sakamoto case SNDRV_PCM_TRIGGER_START:
194e453df44STakashi Sakamoto amdtp_stream_pcm_trigger(&tscm->tx_stream, substream);
195e453df44STakashi Sakamoto break;
196e453df44STakashi Sakamoto case SNDRV_PCM_TRIGGER_STOP:
197e453df44STakashi Sakamoto amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL);
198e453df44STakashi Sakamoto break;
199e453df44STakashi Sakamoto default:
200e453df44STakashi Sakamoto return -EINVAL;
201e453df44STakashi Sakamoto }
202e453df44STakashi Sakamoto
203e453df44STakashi Sakamoto return 0;
204e453df44STakashi Sakamoto }
205e453df44STakashi Sakamoto
pcm_playback_trigger(struct snd_pcm_substream * substream,int cmd)206e453df44STakashi Sakamoto static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
207e453df44STakashi Sakamoto {
208e453df44STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
209e453df44STakashi Sakamoto
210e453df44STakashi Sakamoto switch (cmd) {
211e453df44STakashi Sakamoto case SNDRV_PCM_TRIGGER_START:
212e453df44STakashi Sakamoto amdtp_stream_pcm_trigger(&tscm->rx_stream, substream);
213e453df44STakashi Sakamoto break;
214e453df44STakashi Sakamoto case SNDRV_PCM_TRIGGER_STOP:
215e453df44STakashi Sakamoto amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL);
216e453df44STakashi Sakamoto break;
217e453df44STakashi Sakamoto default:
218e453df44STakashi Sakamoto return -EINVAL;
219e453df44STakashi Sakamoto }
220e453df44STakashi Sakamoto
221e453df44STakashi Sakamoto return 0;
222e453df44STakashi Sakamoto }
223e453df44STakashi Sakamoto
pcm_capture_pointer(struct snd_pcm_substream * sbstrm)224e453df44STakashi Sakamoto static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
225e453df44STakashi Sakamoto {
226e453df44STakashi Sakamoto struct snd_tscm *tscm = sbstrm->private_data;
227e453df44STakashi Sakamoto
228f890f9a0STakashi Sakamoto return amdtp_domain_stream_pcm_pointer(&tscm->domain, &tscm->tx_stream);
229e453df44STakashi Sakamoto }
230e453df44STakashi Sakamoto
pcm_playback_pointer(struct snd_pcm_substream * sbstrm)231e453df44STakashi Sakamoto static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
232e453df44STakashi Sakamoto {
233e453df44STakashi Sakamoto struct snd_tscm *tscm = sbstrm->private_data;
234e453df44STakashi Sakamoto
235f890f9a0STakashi Sakamoto return amdtp_domain_stream_pcm_pointer(&tscm->domain, &tscm->rx_stream);
236e453df44STakashi Sakamoto }
237e453df44STakashi Sakamoto
pcm_capture_ack(struct snd_pcm_substream * substream)238875becf8STakashi Sakamoto static int pcm_capture_ack(struct snd_pcm_substream *substream)
239875becf8STakashi Sakamoto {
240875becf8STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
241875becf8STakashi Sakamoto
242e6dcc92fSTakashi Sakamoto return amdtp_domain_stream_pcm_ack(&tscm->domain, &tscm->tx_stream);
243875becf8STakashi Sakamoto }
244875becf8STakashi Sakamoto
pcm_playback_ack(struct snd_pcm_substream * substream)245875becf8STakashi Sakamoto static int pcm_playback_ack(struct snd_pcm_substream *substream)
246875becf8STakashi Sakamoto {
247875becf8STakashi Sakamoto struct snd_tscm *tscm = substream->private_data;
248875becf8STakashi Sakamoto
249e6dcc92fSTakashi Sakamoto return amdtp_domain_stream_pcm_ack(&tscm->domain, &tscm->rx_stream);
250875becf8STakashi Sakamoto }
251875becf8STakashi Sakamoto
snd_tscm_create_pcm_devices(struct snd_tscm * tscm)25292128236STakashi Sakamoto int snd_tscm_create_pcm_devices(struct snd_tscm *tscm)
25392128236STakashi Sakamoto {
25492128236STakashi Sakamoto static const struct snd_pcm_ops capture_ops = {
255e453df44STakashi Sakamoto .open = pcm_open,
256e453df44STakashi Sakamoto .close = pcm_close,
257d8f291b7STakashi Sakamoto .hw_params = pcm_hw_params,
258d8f291b7STakashi Sakamoto .hw_free = pcm_hw_free,
259e453df44STakashi Sakamoto .prepare = pcm_capture_prepare,
260e453df44STakashi Sakamoto .trigger = pcm_capture_trigger,
261e453df44STakashi Sakamoto .pointer = pcm_capture_pointer,
262875becf8STakashi Sakamoto .ack = pcm_capture_ack,
263e453df44STakashi Sakamoto };
26492128236STakashi Sakamoto static const struct snd_pcm_ops playback_ops = {
265e453df44STakashi Sakamoto .open = pcm_open,
266e453df44STakashi Sakamoto .close = pcm_close,
267d8f291b7STakashi Sakamoto .hw_params = pcm_hw_params,
268d8f291b7STakashi Sakamoto .hw_free = pcm_hw_free,
269e453df44STakashi Sakamoto .prepare = pcm_playback_prepare,
270e453df44STakashi Sakamoto .trigger = pcm_playback_trigger,
271e453df44STakashi Sakamoto .pointer = pcm_playback_pointer,
272875becf8STakashi Sakamoto .ack = pcm_playback_ack,
273e453df44STakashi Sakamoto };
274e453df44STakashi Sakamoto struct snd_pcm *pcm;
275e453df44STakashi Sakamoto int err;
276e453df44STakashi Sakamoto
277e453df44STakashi Sakamoto err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm);
278e453df44STakashi Sakamoto if (err < 0)
279e453df44STakashi Sakamoto return err;
280e453df44STakashi Sakamoto
281e453df44STakashi Sakamoto pcm->private_data = tscm;
282e453df44STakashi Sakamoto snprintf(pcm->name, sizeof(pcm->name),
283e453df44STakashi Sakamoto "%s PCM", tscm->card->shortname);
28492128236STakashi Sakamoto snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
28592128236STakashi Sakamoto snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
2867641d549STakashi Iwai snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
287e453df44STakashi Sakamoto
288e453df44STakashi Sakamoto return 0;
289e453df44STakashi Sakamoto }
290