1 // SPDX-License-Identifier: GPL-2.0-only
3 * dice_pcm.c - a part of driver for DICE based devices
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
11 static int dice_rate_constraint(struct snd_pcm_hw_params
*params
,
12 struct snd_pcm_hw_rule
*rule
)
14 struct snd_pcm_substream
*substream
= rule
->private;
15 struct snd_dice
*dice
= substream
->private_data
;
16 unsigned int index
= substream
->pcm
->device
;
18 const struct snd_interval
*c
=
19 hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
20 struct snd_interval
*r
=
21 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
22 struct snd_interval rates
= {
23 .min
= UINT_MAX
, .max
= 0, .integer
= 1
25 unsigned int *pcm_channels
;
26 enum snd_dice_rate_mode mode
;
29 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
30 pcm_channels
= dice
->tx_pcm_chs
[index
];
32 pcm_channels
= dice
->rx_pcm_chs
[index
];
34 for (i
= 0; i
< ARRAY_SIZE(snd_dice_rates
); ++i
) {
35 rate
= snd_dice_rates
[i
];
36 if (snd_dice_stream_get_rate_mode(dice
, rate
, &mode
) < 0)
39 if (!snd_interval_test(c
, pcm_channels
[mode
]))
42 rates
.min
= min(rates
.min
, rate
);
43 rates
.max
= max(rates
.max
, rate
);
46 return snd_interval_refine(r
, &rates
);
49 static int dice_channels_constraint(struct snd_pcm_hw_params
*params
,
50 struct snd_pcm_hw_rule
*rule
)
52 struct snd_pcm_substream
*substream
= rule
->private;
53 struct snd_dice
*dice
= substream
->private_data
;
54 unsigned int index
= substream
->pcm
->device
;
56 const struct snd_interval
*r
=
57 hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_RATE
);
58 struct snd_interval
*c
=
59 hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
60 struct snd_interval channels
= {
61 .min
= UINT_MAX
, .max
= 0, .integer
= 1
63 unsigned int *pcm_channels
;
64 enum snd_dice_rate_mode mode
;
67 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
68 pcm_channels
= dice
->tx_pcm_chs
[index
];
70 pcm_channels
= dice
->rx_pcm_chs
[index
];
72 for (i
= 0; i
< ARRAY_SIZE(snd_dice_rates
); ++i
) {
73 rate
= snd_dice_rates
[i
];
74 if (snd_dice_stream_get_rate_mode(dice
, rate
, &mode
) < 0)
77 if (!snd_interval_test(r
, rate
))
80 channels
.min
= min(channels
.min
, pcm_channels
[mode
]);
81 channels
.max
= max(channels
.max
, pcm_channels
[mode
]);
84 return snd_interval_refine(c
, &channels
);
87 static int limit_channels_and_rates(struct snd_dice
*dice
,
88 struct snd_pcm_runtime
*runtime
,
89 enum amdtp_stream_direction dir
,
92 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
93 unsigned int *pcm_channels
;
96 if (dir
== AMDTP_IN_STREAM
)
97 pcm_channels
= dice
->tx_pcm_chs
[index
];
99 pcm_channels
= dice
->rx_pcm_chs
[index
];
101 hw
->channels_min
= UINT_MAX
;
102 hw
->channels_max
= 0;
104 for (i
= 0; i
< ARRAY_SIZE(snd_dice_rates
); ++i
) {
105 enum snd_dice_rate_mode mode
;
106 unsigned int rate
, channels
;
108 rate
= snd_dice_rates
[i
];
109 if (snd_dice_stream_get_rate_mode(dice
, rate
, &mode
) < 0)
111 hw
->rates
|= snd_pcm_rate_to_rate_bit(rate
);
113 channels
= pcm_channels
[mode
];
116 hw
->channels_min
= min(hw
->channels_min
, channels
);
117 hw
->channels_max
= max(hw
->channels_max
, channels
);
120 snd_pcm_limit_hw_rates(runtime
);
125 static int init_hw_info(struct snd_dice
*dice
,
126 struct snd_pcm_substream
*substream
)
128 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
129 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
130 unsigned int index
= substream
->pcm
->device
;
131 enum amdtp_stream_direction dir
;
132 struct amdtp_stream
*stream
;
135 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
136 hw
->formats
= AM824_IN_PCM_FORMAT_BITS
;
137 dir
= AMDTP_IN_STREAM
;
138 stream
= &dice
->tx_stream
[index
];
140 hw
->formats
= AM824_OUT_PCM_FORMAT_BITS
;
141 dir
= AMDTP_OUT_STREAM
;
142 stream
= &dice
->rx_stream
[index
];
145 err
= limit_channels_and_rates(dice
, substream
->runtime
, dir
,
150 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
151 dice_rate_constraint
, substream
,
152 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
155 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
156 dice_channels_constraint
, substream
,
157 SNDRV_PCM_HW_PARAM_RATE
, -1);
161 return amdtp_am824_add_pcm_hw_constraints(stream
, runtime
);
164 static int pcm_open(struct snd_pcm_substream
*substream
)
166 struct snd_dice
*dice
= substream
->private_data
;
167 struct amdtp_domain
*d
= &dice
->domain
;
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
:
199 mutex_lock(&dice
->mutex
);
201 // When source of clock is not internal or any stream is reserved for
202 // transmission of PCM frames, the available sampling rate is limited
205 (dice
->substreams_counter
> 0 && d
->events_per_period
> 0)) {
206 unsigned int frames_per_period
= d
->events_per_period
;
207 unsigned int frames_per_buffer
= d
->events_per_buffer
;
210 err
= snd_dice_transaction_get_rate(dice
, &rate
);
212 mutex_unlock(&dice
->mutex
);
216 substream
->runtime
->hw
.rate_min
= rate
;
217 substream
->runtime
->hw
.rate_max
= rate
;
219 if (frames_per_period
> 0) {
220 // For double_pcm_frame quirk.
222 frames_per_period
*= 2;
223 frames_per_buffer
*= 2;
226 err
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
227 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
228 frames_per_period
, frames_per_period
);
230 mutex_unlock(&dice
->mutex
);
234 err
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
235 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
236 frames_per_buffer
, frames_per_buffer
);
238 mutex_unlock(&dice
->mutex
);
244 mutex_unlock(&dice
->mutex
);
246 snd_pcm_set_sync(substream
);
250 snd_dice_stream_lock_release(dice
);
254 static int pcm_close(struct snd_pcm_substream
*substream
)
256 struct snd_dice
*dice
= substream
->private_data
;
258 snd_dice_stream_lock_release(dice
);
263 static int pcm_hw_params(struct snd_pcm_substream
*substream
,
264 struct snd_pcm_hw_params
*hw_params
)
266 struct snd_dice
*dice
= substream
->private_data
;
269 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
270 unsigned int rate
= params_rate(hw_params
);
271 unsigned int events_per_period
= params_period_size(hw_params
);
272 unsigned int events_per_buffer
= params_buffer_size(hw_params
);
274 mutex_lock(&dice
->mutex
);
275 // For double_pcm_frame quirk.
277 events_per_period
/= 2;
278 events_per_buffer
/= 2;
280 err
= snd_dice_stream_reserve_duplex(dice
, rate
,
281 events_per_period
, events_per_buffer
);
283 ++dice
->substreams_counter
;
284 mutex_unlock(&dice
->mutex
);
290 static int pcm_hw_free(struct snd_pcm_substream
*substream
)
292 struct snd_dice
*dice
= substream
->private_data
;
294 mutex_lock(&dice
->mutex
);
296 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
297 --dice
->substreams_counter
;
299 snd_dice_stream_stop_duplex(dice
);
301 mutex_unlock(&dice
->mutex
);
306 static int capture_prepare(struct snd_pcm_substream
*substream
)
308 struct snd_dice
*dice
= substream
->private_data
;
309 struct amdtp_stream
*stream
= &dice
->tx_stream
[substream
->pcm
->device
];
312 mutex_lock(&dice
->mutex
);
313 err
= snd_dice_stream_start_duplex(dice
);
314 mutex_unlock(&dice
->mutex
);
316 amdtp_stream_pcm_prepare(stream
);
320 static int playback_prepare(struct snd_pcm_substream
*substream
)
322 struct snd_dice
*dice
= substream
->private_data
;
323 struct amdtp_stream
*stream
= &dice
->rx_stream
[substream
->pcm
->device
];
326 mutex_lock(&dice
->mutex
);
327 err
= snd_dice_stream_start_duplex(dice
);
328 mutex_unlock(&dice
->mutex
);
330 amdtp_stream_pcm_prepare(stream
);
335 static int capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
337 struct snd_dice
*dice
= substream
->private_data
;
338 struct amdtp_stream
*stream
= &dice
->tx_stream
[substream
->pcm
->device
];
341 case SNDRV_PCM_TRIGGER_START
:
342 amdtp_stream_pcm_trigger(stream
, substream
);
344 case SNDRV_PCM_TRIGGER_STOP
:
345 amdtp_stream_pcm_trigger(stream
, NULL
);
353 static int playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
355 struct snd_dice
*dice
= substream
->private_data
;
356 struct amdtp_stream
*stream
= &dice
->rx_stream
[substream
->pcm
->device
];
359 case SNDRV_PCM_TRIGGER_START
:
360 amdtp_stream_pcm_trigger(stream
, substream
);
362 case SNDRV_PCM_TRIGGER_STOP
:
363 amdtp_stream_pcm_trigger(stream
, NULL
);
372 static snd_pcm_uframes_t
capture_pointer(struct snd_pcm_substream
*substream
)
374 struct snd_dice
*dice
= substream
->private_data
;
375 struct amdtp_stream
*stream
= &dice
->tx_stream
[substream
->pcm
->device
];
377 return amdtp_domain_stream_pcm_pointer(&dice
->domain
, stream
);
379 static snd_pcm_uframes_t
playback_pointer(struct snd_pcm_substream
*substream
)
381 struct snd_dice
*dice
= substream
->private_data
;
382 struct amdtp_stream
*stream
= &dice
->rx_stream
[substream
->pcm
->device
];
384 return amdtp_domain_stream_pcm_pointer(&dice
->domain
, stream
);
387 static int capture_ack(struct snd_pcm_substream
*substream
)
389 struct snd_dice
*dice
= substream
->private_data
;
390 struct amdtp_stream
*stream
= &dice
->tx_stream
[substream
->pcm
->device
];
392 return amdtp_domain_stream_pcm_ack(&dice
->domain
, stream
);
395 static int playback_ack(struct snd_pcm_substream
*substream
)
397 struct snd_dice
*dice
= substream
->private_data
;
398 struct amdtp_stream
*stream
= &dice
->rx_stream
[substream
->pcm
->device
];
400 return amdtp_domain_stream_pcm_ack(&dice
->domain
, stream
);
403 int snd_dice_create_pcm(struct snd_dice
*dice
)
405 static const struct snd_pcm_ops capture_ops
= {
408 .hw_params
= pcm_hw_params
,
409 .hw_free
= pcm_hw_free
,
410 .prepare
= capture_prepare
,
411 .trigger
= capture_trigger
,
412 .pointer
= capture_pointer
,
415 static const struct snd_pcm_ops playback_ops
= {
418 .hw_params
= pcm_hw_params
,
419 .hw_free
= pcm_hw_free
,
420 .prepare
= playback_prepare
,
421 .trigger
= playback_trigger
,
422 .pointer
= playback_pointer
,
426 unsigned int capture
, playback
;
430 for (i
= 0; i
< MAX_STREAMS
; i
++) {
431 capture
= playback
= 0;
432 for (j
= 0; j
< SND_DICE_RATE_MODE_COUNT
; ++j
) {
433 if (dice
->tx_pcm_chs
[i
][j
] > 0)
435 if (dice
->rx_pcm_chs
[i
][j
] > 0)
439 err
= snd_pcm_new(dice
->card
, "DICE", i
, playback
, capture
,
443 pcm
->private_data
= dice
;
444 strcpy(pcm
->name
, dice
->card
->shortname
);
447 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
451 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
454 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_VMALLOC
,