Thomas Gleixner | da607e1 | 2019-05-29 16:57:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 2 | /* |
| 3 | * tascam-pcm.c - a part of driver for TASCAM FireWire series |
| 4 | * |
| 5 | * Copyright (c) 2015 Takashi Sakamoto |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "tascam.h" |
| 9 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 10 | static int pcm_init_hw_params(struct snd_tscm *tscm, |
| 11 | struct snd_pcm_substream *substream) |
| 12 | { |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 13 | struct snd_pcm_runtime *runtime = substream->runtime; |
Takashi Sakamoto | 55799c5 | 2017-06-08 09:11:03 +0900 | [diff] [blame] | 14 | struct snd_pcm_hardware *hw = &runtime->hw; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 15 | struct amdtp_stream *stream; |
| 16 | unsigned int pcm_channels; |
| 17 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 18 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 19 | runtime->hw.formats = SNDRV_PCM_FMTBIT_S32; |
| 20 | stream = &tscm->tx_stream; |
| 21 | pcm_channels = tscm->spec->pcm_capture_analog_channels; |
| 22 | } else { |
Takashi Sakamoto | a02cb8f | 2017-05-22 22:22:21 +0900 | [diff] [blame] | 23 | runtime->hw.formats = SNDRV_PCM_FMTBIT_S32; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 24 | stream = &tscm->rx_stream; |
| 25 | pcm_channels = tscm->spec->pcm_playback_analog_channels; |
| 26 | } |
| 27 | |
| 28 | if (tscm->spec->has_adat) |
| 29 | pcm_channels += 8; |
| 30 | if (tscm->spec->has_spdif) |
| 31 | pcm_channels += 2; |
| 32 | runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels; |
| 33 | |
Takashi Sakamoto | 55799c5 | 2017-06-08 09:11:03 +0900 | [diff] [blame] | 34 | hw->rates = SNDRV_PCM_RATE_44100 | |
| 35 | SNDRV_PCM_RATE_48000 | |
| 36 | SNDRV_PCM_RATE_88200 | |
| 37 | SNDRV_PCM_RATE_96000; |
| 38 | snd_pcm_limit_hw_rates(runtime); |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 39 | |
| 40 | return amdtp_tscm_add_pcm_hw_constraints(stream, runtime); |
| 41 | } |
| 42 | |
| 43 | static int pcm_open(struct snd_pcm_substream *substream) |
| 44 | { |
| 45 | struct snd_tscm *tscm = substream->private_data; |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 46 | struct amdtp_domain *d = &tscm->domain; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 47 | enum snd_tscm_clock clock; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 48 | int err; |
| 49 | |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 50 | err = snd_tscm_stream_lock_try(tscm); |
| 51 | if (err < 0) |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 52 | return err; |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 53 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 54 | err = pcm_init_hw_params(tscm, substream); |
| 55 | if (err < 0) |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 56 | goto err_locked; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 57 | |
| 58 | err = snd_tscm_stream_get_clock(tscm, &clock); |
Takashi Sakamoto | 2617120 | 2019-09-10 22:51:51 +0900 | [diff] [blame] | 59 | if (err < 0) |
| 60 | goto err_locked; |
| 61 | |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 62 | mutex_lock(&tscm->mutex); |
| 63 | |
| 64 | // When source of clock is not internal or any stream is reserved for |
| 65 | // transmission of PCM frames, the available sampling rate is limited |
| 66 | // at current one. |
| 67 | if (clock != SND_TSCM_CLOCK_INTERNAL || tscm->substreams_counter > 0) { |
| 68 | unsigned int frames_per_period = d->events_per_period; |
Takashi Sakamoto | 128307d | 2019-10-18 00:54:19 +0900 | [diff] [blame] | 69 | unsigned int frames_per_buffer = d->events_per_buffer; |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 70 | unsigned int rate; |
| 71 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 72 | err = snd_tscm_stream_get_rate(tscm, &rate); |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 73 | if (err < 0) { |
| 74 | mutex_unlock(&tscm->mutex); |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 75 | goto err_locked; |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 76 | } |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 77 | substream->runtime->hw.rate_min = rate; |
| 78 | substream->runtime->hw.rate_max = rate; |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 79 | |
| 80 | err = snd_pcm_hw_constraint_minmax(substream->runtime, |
| 81 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 82 | frames_per_period, frames_per_period); |
| 83 | if (err < 0) { |
| 84 | mutex_unlock(&tscm->mutex); |
| 85 | goto err_locked; |
| 86 | } |
Takashi Sakamoto | 128307d | 2019-10-18 00:54:19 +0900 | [diff] [blame] | 87 | |
| 88 | err = snd_pcm_hw_constraint_minmax(substream->runtime, |
| 89 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 90 | frames_per_buffer, frames_per_buffer); |
| 91 | if (err < 0) { |
| 92 | mutex_unlock(&tscm->mutex); |
| 93 | goto err_locked; |
| 94 | } |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 95 | } |
| 96 | |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 97 | mutex_unlock(&tscm->mutex); |
| 98 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 99 | snd_pcm_set_sync(substream); |
Takashi Sakamoto | 6669a11 | 2019-10-07 20:05:30 +0900 | [diff] [blame] | 100 | |
| 101 | return 0; |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 102 | err_locked: |
| 103 | snd_tscm_stream_lock_release(tscm); |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 104 | return err; |
| 105 | } |
| 106 | |
| 107 | static int pcm_close(struct snd_pcm_substream *substream) |
| 108 | { |
Takashi Sakamoto | e5e0c3d | 2015-10-01 22:02:17 +0900 | [diff] [blame] | 109 | struct snd_tscm *tscm = substream->private_data; |
| 110 | |
| 111 | snd_tscm_stream_lock_release(tscm); |
| 112 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Takashi Sakamoto | d8f291b | 2019-06-02 16:12:51 +0900 | [diff] [blame] | 116 | static int pcm_hw_params(struct snd_pcm_substream *substream, |
| 117 | struct snd_pcm_hw_params *hw_params) |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 118 | { |
| 119 | struct snd_tscm *tscm = substream->private_data; |
Takashi Iwai | 7641d54 | 2019-12-09 10:48:41 +0100 | [diff] [blame] | 120 | int err = 0; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 121 | |
| 122 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
Takashi Sakamoto | 07b2664 | 2019-06-02 16:12:48 +0900 | [diff] [blame] | 123 | unsigned int rate = params_rate(hw_params); |
Takashi Sakamoto | 262542e | 2019-10-07 20:05:22 +0900 | [diff] [blame] | 124 | unsigned int frames_per_period = params_period_size(hw_params); |
Takashi Sakamoto | 128307d | 2019-10-18 00:54:19 +0900 | [diff] [blame] | 125 | unsigned int frames_per_buffer = params_buffer_size(hw_params); |
Takashi Sakamoto | 07b2664 | 2019-06-02 16:12:48 +0900 | [diff] [blame] | 126 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 127 | mutex_lock(&tscm->mutex); |
Takashi Sakamoto | 262542e | 2019-10-07 20:05:22 +0900 | [diff] [blame] | 128 | err = snd_tscm_stream_reserve_duplex(tscm, rate, |
Takashi Sakamoto | 128307d | 2019-10-18 00:54:19 +0900 | [diff] [blame] | 129 | frames_per_period, frames_per_buffer); |
Takashi Sakamoto | 07b2664 | 2019-06-02 16:12:48 +0900 | [diff] [blame] | 130 | if (err >= 0) |
| 131 | ++tscm->substreams_counter; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 132 | mutex_unlock(&tscm->mutex); |
| 133 | } |
| 134 | |
Takashi Sakamoto | 07b2664 | 2019-06-02 16:12:48 +0900 | [diff] [blame] | 135 | return err; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 136 | } |
| 137 | |
Takashi Sakamoto | d8f291b | 2019-06-02 16:12:51 +0900 | [diff] [blame] | 138 | static int pcm_hw_free(struct snd_pcm_substream *substream) |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 139 | { |
| 140 | struct snd_tscm *tscm = substream->private_data; |
| 141 | |
| 142 | mutex_lock(&tscm->mutex); |
| 143 | |
| 144 | if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
Takashi Sakamoto | 07b2664 | 2019-06-02 16:12:48 +0900 | [diff] [blame] | 145 | --tscm->substreams_counter; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 146 | |
| 147 | snd_tscm_stream_stop_duplex(tscm); |
| 148 | |
| 149 | mutex_unlock(&tscm->mutex); |
| 150 | |
Takashi Iwai | 7641d54 | 2019-12-09 10:48:41 +0100 | [diff] [blame] | 151 | return 0; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static int pcm_capture_prepare(struct snd_pcm_substream *substream) |
| 155 | { |
| 156 | struct snd_tscm *tscm = substream->private_data; |
| 157 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 158 | int err; |
| 159 | |
| 160 | mutex_lock(&tscm->mutex); |
| 161 | |
| 162 | err = snd_tscm_stream_start_duplex(tscm, runtime->rate); |
| 163 | if (err >= 0) |
| 164 | amdtp_stream_pcm_prepare(&tscm->tx_stream); |
| 165 | |
| 166 | mutex_unlock(&tscm->mutex); |
| 167 | |
| 168 | return err; |
| 169 | } |
| 170 | |
| 171 | static int pcm_playback_prepare(struct snd_pcm_substream *substream) |
| 172 | { |
| 173 | struct snd_tscm *tscm = substream->private_data; |
| 174 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 175 | int err; |
| 176 | |
| 177 | mutex_lock(&tscm->mutex); |
| 178 | |
| 179 | err = snd_tscm_stream_start_duplex(tscm, runtime->rate); |
| 180 | if (err >= 0) |
| 181 | amdtp_stream_pcm_prepare(&tscm->rx_stream); |
| 182 | |
| 183 | mutex_unlock(&tscm->mutex); |
| 184 | |
| 185 | return err; |
| 186 | } |
| 187 | |
| 188 | static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd) |
| 189 | { |
| 190 | struct snd_tscm *tscm = substream->private_data; |
| 191 | |
| 192 | switch (cmd) { |
| 193 | case SNDRV_PCM_TRIGGER_START: |
| 194 | amdtp_stream_pcm_trigger(&tscm->tx_stream, substream); |
| 195 | break; |
| 196 | case SNDRV_PCM_TRIGGER_STOP: |
| 197 | amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL); |
| 198 | break; |
| 199 | default: |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd) |
| 207 | { |
| 208 | struct snd_tscm *tscm = substream->private_data; |
| 209 | |
| 210 | switch (cmd) { |
| 211 | case SNDRV_PCM_TRIGGER_START: |
| 212 | amdtp_stream_pcm_trigger(&tscm->rx_stream, substream); |
| 213 | break; |
| 214 | case SNDRV_PCM_TRIGGER_STOP: |
| 215 | amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL); |
| 216 | break; |
| 217 | default: |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm) |
| 225 | { |
| 226 | struct snd_tscm *tscm = sbstrm->private_data; |
| 227 | |
Takashi Sakamoto | f890f9a | 2019-10-18 15:19:07 +0900 | [diff] [blame] | 228 | return amdtp_domain_stream_pcm_pointer(&tscm->domain, &tscm->tx_stream); |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm) |
| 232 | { |
| 233 | struct snd_tscm *tscm = sbstrm->private_data; |
| 234 | |
Takashi Sakamoto | f890f9a | 2019-10-18 15:19:07 +0900 | [diff] [blame] | 235 | return amdtp_domain_stream_pcm_pointer(&tscm->domain, &tscm->rx_stream); |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 236 | } |
| 237 | |
Takashi Sakamoto | 875becf | 2017-06-07 09:38:05 +0900 | [diff] [blame] | 238 | static int pcm_capture_ack(struct snd_pcm_substream *substream) |
| 239 | { |
| 240 | struct snd_tscm *tscm = substream->private_data; |
| 241 | |
Takashi Sakamoto | e6dcc92 | 2019-10-18 15:19:08 +0900 | [diff] [blame] | 242 | return amdtp_domain_stream_pcm_ack(&tscm->domain, &tscm->tx_stream); |
Takashi Sakamoto | 875becf | 2017-06-07 09:38:05 +0900 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static int pcm_playback_ack(struct snd_pcm_substream *substream) |
| 246 | { |
| 247 | struct snd_tscm *tscm = substream->private_data; |
| 248 | |
Takashi Sakamoto | e6dcc92 | 2019-10-18 15:19:08 +0900 | [diff] [blame] | 249 | return amdtp_domain_stream_pcm_ack(&tscm->domain, &tscm->rx_stream); |
Takashi Sakamoto | 875becf | 2017-06-07 09:38:05 +0900 | [diff] [blame] | 250 | } |
| 251 | |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 252 | int snd_tscm_create_pcm_devices(struct snd_tscm *tscm) |
| 253 | { |
Takashi Sakamoto | 9212823 | 2017-01-05 21:48:12 +0900 | [diff] [blame] | 254 | static const struct snd_pcm_ops capture_ops = { |
| 255 | .open = pcm_open, |
| 256 | .close = pcm_close, |
Takashi Sakamoto | d8f291b | 2019-06-02 16:12:51 +0900 | [diff] [blame] | 257 | .hw_params = pcm_hw_params, |
| 258 | .hw_free = pcm_hw_free, |
Takashi Sakamoto | 9212823 | 2017-01-05 21:48:12 +0900 | [diff] [blame] | 259 | .prepare = pcm_capture_prepare, |
| 260 | .trigger = pcm_capture_trigger, |
| 261 | .pointer = pcm_capture_pointer, |
Takashi Sakamoto | 875becf | 2017-06-07 09:38:05 +0900 | [diff] [blame] | 262 | .ack = pcm_capture_ack, |
Takashi Sakamoto | 9212823 | 2017-01-05 21:48:12 +0900 | [diff] [blame] | 263 | }; |
| 264 | static const struct snd_pcm_ops playback_ops = { |
| 265 | .open = pcm_open, |
| 266 | .close = pcm_close, |
Takashi Sakamoto | d8f291b | 2019-06-02 16:12:51 +0900 | [diff] [blame] | 267 | .hw_params = pcm_hw_params, |
| 268 | .hw_free = pcm_hw_free, |
Takashi Sakamoto | 9212823 | 2017-01-05 21:48:12 +0900 | [diff] [blame] | 269 | .prepare = pcm_playback_prepare, |
| 270 | .trigger = pcm_playback_trigger, |
| 271 | .pointer = pcm_playback_pointer, |
Takashi Sakamoto | 875becf | 2017-06-07 09:38:05 +0900 | [diff] [blame] | 272 | .ack = pcm_playback_ack, |
Takashi Sakamoto | 9212823 | 2017-01-05 21:48:12 +0900 | [diff] [blame] | 273 | }; |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 274 | struct snd_pcm *pcm; |
| 275 | int err; |
| 276 | |
| 277 | err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm); |
| 278 | if (err < 0) |
| 279 | return err; |
| 280 | |
| 281 | pcm->private_data = tscm; |
| 282 | snprintf(pcm->name, sizeof(pcm->name), |
| 283 | "%s PCM", tscm->card->shortname); |
Takashi Sakamoto | 9212823 | 2017-01-05 21:48:12 +0900 | [diff] [blame] | 284 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops); |
| 285 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops); |
Takashi Iwai | 7641d54 | 2019-12-09 10:48:41 +0100 | [diff] [blame] | 286 | snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); |
Takashi Sakamoto | e453df4 | 2015-10-01 22:02:16 +0900 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |