2 * dice_pcm.c - a part of driver for DICE based devices
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
7 * Licensed under the terms of the GNU General Public License, version 2.
12 static int dice_rate_constraint(struct snd_pcm_hw_params *params,
13 struct snd_pcm_hw_rule *rule)
15 struct snd_pcm_substream *substream = rule->private;
16 struct snd_dice *dice = substream->private_data;
17 unsigned int index = substream->pcm->device;
19 const struct snd_interval *c =
20 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
21 struct snd_interval *r =
22 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
23 struct snd_interval rates = {
24 .min = UINT_MAX, .max = 0, .integer = 1
26 unsigned int *pcm_channels;
27 enum snd_dice_rate_mode mode;
30 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
31 pcm_channels = dice->tx_pcm_chs[index];
33 pcm_channels = dice->rx_pcm_chs[index];
35 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
36 rate = snd_dice_rates[i];
37 if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
40 if (!snd_interval_test(c, pcm_channels[mode]))
43 rates.min = min(rates.min, rate);
44 rates.max = max(rates.max, rate);
47 return snd_interval_refine(r, &rates);
50 static int dice_channels_constraint(struct snd_pcm_hw_params *params,
51 struct snd_pcm_hw_rule *rule)
53 struct snd_pcm_substream *substream = rule->private;
54 struct snd_dice *dice = substream->private_data;
55 unsigned int index = substream->pcm->device;
57 const struct snd_interval *r =
58 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
59 struct snd_interval *c =
60 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
61 struct snd_interval channels = {
62 .min = UINT_MAX, .max = 0, .integer = 1
64 unsigned int *pcm_channels;
65 enum snd_dice_rate_mode mode;
68 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
69 pcm_channels = dice->tx_pcm_chs[index];
71 pcm_channels = dice->rx_pcm_chs[index];
73 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
74 rate = snd_dice_rates[i];
75 if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
78 if (!snd_interval_test(r, rate))
81 channels.min = min(channels.min, pcm_channels[mode]);
82 channels.max = max(channels.max, pcm_channels[mode]);
85 return snd_interval_refine(c, &channels);
88 static int limit_channels_and_rates(struct snd_dice *dice,
89 struct snd_pcm_runtime *runtime,
90 enum amdtp_stream_direction dir,
93 struct snd_pcm_hardware *hw = &runtime->hw;
94 unsigned int *pcm_channels;
97 if (dir == AMDTP_IN_STREAM)
98 pcm_channels = dice->tx_pcm_chs[index];
100 pcm_channels = dice->rx_pcm_chs[index];
102 hw->channels_min = UINT_MAX;
103 hw->channels_max = 0;
105 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
106 enum snd_dice_rate_mode mode;
107 unsigned int rate, channels;
109 rate = snd_dice_rates[i];
110 if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
112 hw->rates |= snd_pcm_rate_to_rate_bit(rate);
114 channels = pcm_channels[mode];
117 hw->channels_min = min(hw->channels_min, channels);
118 hw->channels_max = max(hw->channels_max, channels);
121 snd_pcm_limit_hw_rates(runtime);
126 static int init_hw_info(struct snd_dice *dice,
127 struct snd_pcm_substream *substream)
129 struct snd_pcm_runtime *runtime = substream->runtime;
130 struct snd_pcm_hardware *hw = &runtime->hw;
131 unsigned int index = substream->pcm->device;
132 enum amdtp_stream_direction dir;
133 struct amdtp_stream *stream;
136 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
137 hw->formats = AM824_IN_PCM_FORMAT_BITS;
138 dir = AMDTP_IN_STREAM;
139 stream = &dice->tx_stream[index];
141 hw->formats = AM824_OUT_PCM_FORMAT_BITS;
142 dir = AMDTP_OUT_STREAM;
143 stream = &dice->rx_stream[index];
146 err = limit_channels_and_rates(dice, substream->runtime, dir,
151 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
152 dice_rate_constraint, substream,
153 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
156 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
157 dice_channels_constraint, substream,
158 SNDRV_PCM_HW_PARAM_RATE, -1);
162 return amdtp_am824_add_pcm_hw_constraints(stream, runtime);
165 static int pcm_open(struct snd_pcm_substream *substream)
167 struct snd_dice *dice = substream->private_data;
172 err = snd_dice_stream_lock_try(dice);
176 err = init_hw_info(dice, substream);
180 err = snd_dice_transaction_get_clock_source(dice, &source);
184 case CLOCK_SOURCE_AES1:
185 case CLOCK_SOURCE_AES2:
186 case CLOCK_SOURCE_AES3:
187 case CLOCK_SOURCE_AES4:
188 case CLOCK_SOURCE_AES_ANY:
189 case CLOCK_SOURCE_ADAT:
190 case CLOCK_SOURCE_TDIF:
191 case CLOCK_SOURCE_WC:
200 * When source of clock is not internal or any PCM streams are running,
201 * available sampling rate is limited at current sampling rate.
204 amdtp_stream_pcm_running(&dice->tx_stream[0]) ||
205 amdtp_stream_pcm_running(&dice->tx_stream[1]) ||
206 amdtp_stream_pcm_running(&dice->rx_stream[0]) ||
207 amdtp_stream_pcm_running(&dice->rx_stream[1])) {
210 err = snd_dice_transaction_get_rate(dice, &rate);
213 substream->runtime->hw.rate_min = rate;
214 substream->runtime->hw.rate_max = rate;
217 snd_pcm_set_sync(substream);
221 snd_dice_stream_lock_release(dice);
225 static int pcm_close(struct snd_pcm_substream *substream)
227 struct snd_dice *dice = substream->private_data;
229 snd_dice_stream_lock_release(dice);
234 static int capture_hw_params(struct snd_pcm_substream *substream,
235 struct snd_pcm_hw_params *hw_params)
237 struct snd_dice *dice = substream->private_data;
240 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
241 params_buffer_bytes(hw_params));
245 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
246 mutex_lock(&dice->mutex);
247 dice->substreams_counter++;
248 mutex_unlock(&dice->mutex);
253 static int playback_hw_params(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *hw_params)
256 struct snd_dice *dice = substream->private_data;
259 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
260 params_buffer_bytes(hw_params));
264 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
265 mutex_lock(&dice->mutex);
266 dice->substreams_counter++;
267 mutex_unlock(&dice->mutex);
273 static int capture_hw_free(struct snd_pcm_substream *substream)
275 struct snd_dice *dice = substream->private_data;
277 mutex_lock(&dice->mutex);
279 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
280 dice->substreams_counter--;
282 snd_dice_stream_stop_duplex(dice);
284 mutex_unlock(&dice->mutex);
286 return snd_pcm_lib_free_vmalloc_buffer(substream);
289 static int playback_hw_free(struct snd_pcm_substream *substream)
291 struct snd_dice *dice = substream->private_data;
293 mutex_lock(&dice->mutex);
295 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
296 dice->substreams_counter--;
298 snd_dice_stream_stop_duplex(dice);
300 mutex_unlock(&dice->mutex);
302 return snd_pcm_lib_free_vmalloc_buffer(substream);
305 static int capture_prepare(struct snd_pcm_substream *substream)
307 struct snd_dice *dice = substream->private_data;
308 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
311 mutex_lock(&dice->mutex);
312 err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
313 mutex_unlock(&dice->mutex);
315 amdtp_stream_pcm_prepare(stream);
319 static int playback_prepare(struct snd_pcm_substream *substream)
321 struct snd_dice *dice = substream->private_data;
322 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
325 mutex_lock(&dice->mutex);
326 err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
327 mutex_unlock(&dice->mutex);
329 amdtp_stream_pcm_prepare(stream);
334 static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
336 struct snd_dice *dice = substream->private_data;
337 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
340 case SNDRV_PCM_TRIGGER_START:
341 amdtp_stream_pcm_trigger(stream, substream);
343 case SNDRV_PCM_TRIGGER_STOP:
344 amdtp_stream_pcm_trigger(stream, NULL);
352 static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
354 struct snd_dice *dice = substream->private_data;
355 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
358 case SNDRV_PCM_TRIGGER_START:
359 amdtp_stream_pcm_trigger(stream, substream);
361 case SNDRV_PCM_TRIGGER_STOP:
362 amdtp_stream_pcm_trigger(stream, NULL);
371 static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
373 struct snd_dice *dice = substream->private_data;
374 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
376 return amdtp_stream_pcm_pointer(stream);
378 static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
380 struct snd_dice *dice = substream->private_data;
381 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
383 return amdtp_stream_pcm_pointer(stream);
386 static int capture_ack(struct snd_pcm_substream *substream)
388 struct snd_dice *dice = substream->private_data;
389 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
391 return amdtp_stream_pcm_ack(stream);
394 static int playback_ack(struct snd_pcm_substream *substream)
396 struct snd_dice *dice = substream->private_data;
397 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
399 return amdtp_stream_pcm_ack(stream);
402 int snd_dice_create_pcm(struct snd_dice *dice)
404 static const struct snd_pcm_ops capture_ops = {
407 .ioctl = snd_pcm_lib_ioctl,
408 .hw_params = capture_hw_params,
409 .hw_free = capture_hw_free,
410 .prepare = capture_prepare,
411 .trigger = capture_trigger,
412 .pointer = capture_pointer,
414 .page = snd_pcm_lib_get_vmalloc_page,
416 static const struct snd_pcm_ops playback_ops = {
419 .ioctl = snd_pcm_lib_ioctl,
420 .hw_params = playback_hw_params,
421 .hw_free = playback_hw_free,
422 .prepare = playback_prepare,
423 .trigger = playback_trigger,
424 .pointer = playback_pointer,
426 .page = snd_pcm_lib_get_vmalloc_page,
429 unsigned int capture, playback;
433 for (i = 0; i < MAX_STREAMS; i++) {
434 capture = playback = 0;
435 for (j = 0; j < SND_DICE_RATE_MODE_COUNT; ++j) {
436 if (dice->tx_pcm_chs[i][j] > 0)
438 if (dice->rx_pcm_chs[i][j] > 0)
442 err = snd_pcm_new(dice->card, "DICE", i, playback, capture,
446 pcm->private_data = dice;
447 strcpy(pcm->name, dice->card->shortname);
450 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
454 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,