1 // SPDX-License-Identifier: GPL-2.0-only
3 * fireworks_pcm.c - a part of driver for Fireworks based devices
5 * Copyright (c) 2009-2010 Clemens Ladisch
6 * Copyright (c) 2013-2014 Takashi Sakamoto
8 #include "./fireworks.h"
12 * Fireworks changes its AMDTP channels for PCM data according to its sampling
13 * rate. There are three modes. Here _XX is either _rx or _tx.
14 * 0: 32.0- 48.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels applied
15 * 1: 88.2- 96.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels_2x applied
16 * 2: 176.4-192.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels_4x applied
18 * The number of PCM channels for analog input and output are always fixed but
19 * the number of PCM channels for digital input and output are differed.
21 * Additionally, according to "AudioFire Owner's Manual Version 2.2", in some
22 * model, the number of PCM channels for digital input has more restriction
23 * depending on which digital interface is selected.
24 * - S/PDIF coaxial and optical : use input 1-2
25 * - ADAT optical at 32.0-48.0 kHz : use input 1-8
26 * - ADAT optical at 88.2-96.0 kHz : use input 1-4 (S/MUX format)
28 * The data in AMDTP channels for blank PCM channels are zero.
30 static const unsigned int freq_table[] = {
31 /* multiplier mode 0 */
35 /* multiplier mode 1 */
38 /* multiplier mode 2 */
43 static inline unsigned int
44 get_multiplier_mode_with_index(unsigned int index)
46 return ((int)index - 1) / 2;
49 int snd_efw_get_multiplier_mode(unsigned int sampling_rate, unsigned int *mode)
53 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
54 if (freq_table[i] == sampling_rate) {
55 *mode = get_multiplier_mode_with_index(i);
64 hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
66 unsigned int *pcm_channels = rule->private;
67 struct snd_interval *r =
68 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
69 const struct snd_interval *c =
70 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
71 struct snd_interval t = {
72 .min = UINT_MAX, .max = 0, .integer = 1
76 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
77 mode = get_multiplier_mode_with_index(i);
78 if (!snd_interval_test(c, pcm_channels[mode]))
81 t.min = min(t.min, freq_table[i]);
82 t.max = max(t.max, freq_table[i]);
85 return snd_interval_refine(r, &t);
89 hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
91 unsigned int *pcm_channels = rule->private;
92 struct snd_interval *c =
93 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
94 const struct snd_interval *r =
95 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
96 struct snd_interval t = {
97 .min = UINT_MAX, .max = 0, .integer = 1
101 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
102 mode = get_multiplier_mode_with_index(i);
103 if (!snd_interval_test(r, freq_table[i]))
106 t.min = min(t.min, pcm_channels[mode]);
107 t.max = max(t.max, pcm_channels[mode]);
110 return snd_interval_refine(c, &t);
114 limit_channels(struct snd_pcm_hardware *hw, unsigned int *pcm_channels)
116 unsigned int i, mode;
118 hw->channels_min = UINT_MAX;
119 hw->channels_max = 0;
121 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
122 mode = get_multiplier_mode_with_index(i);
123 if (pcm_channels[mode] == 0)
126 hw->channels_min = min(hw->channels_min, pcm_channels[mode]);
127 hw->channels_max = max(hw->channels_max, pcm_channels[mode]);
132 pcm_init_hw_params(struct snd_efw *efw,
133 struct snd_pcm_substream *substream)
135 struct snd_pcm_runtime *runtime = substream->runtime;
136 struct amdtp_stream *s;
137 unsigned int *pcm_channels;
140 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
141 runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
143 pcm_channels = efw->pcm_capture_channels;
145 runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
147 pcm_channels = efw->pcm_playback_channels;
151 runtime->hw.rates = efw->supported_sampling_rate,
152 snd_pcm_limit_hw_rates(runtime);
154 limit_channels(&runtime->hw, pcm_channels);
156 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
157 hw_rule_channels, pcm_channels,
158 SNDRV_PCM_HW_PARAM_RATE, -1);
162 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
163 hw_rule_rate, pcm_channels,
164 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
168 err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
173 static int pcm_open(struct snd_pcm_substream *substream)
175 struct snd_efw *efw = substream->private_data;
176 unsigned int sampling_rate;
177 enum snd_efw_clock_source clock_source;
180 err = snd_efw_stream_lock_try(efw);
184 err = pcm_init_hw_params(efw, substream);
188 err = snd_efw_command_get_clock_source(efw, &clock_source);
193 * When source of clock is not internal or any PCM streams are running,
194 * available sampling rate is limited at current sampling rate.
196 if ((clock_source != SND_EFW_CLOCK_SOURCE_INTERNAL) ||
197 amdtp_stream_pcm_running(&efw->tx_stream) ||
198 amdtp_stream_pcm_running(&efw->rx_stream)) {
199 err = snd_efw_command_get_sampling_rate(efw, &sampling_rate);
202 substream->runtime->hw.rate_min = sampling_rate;
203 substream->runtime->hw.rate_max = sampling_rate;
206 snd_pcm_set_sync(substream);
210 snd_efw_stream_lock_release(efw);
214 static int pcm_close(struct snd_pcm_substream *substream)
216 struct snd_efw *efw = substream->private_data;
217 snd_efw_stream_lock_release(efw);
221 static int pcm_hw_params(struct snd_pcm_substream *substream,
222 struct snd_pcm_hw_params *hw_params)
224 struct snd_efw *efw = substream->private_data;
227 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
228 params_buffer_bytes(hw_params));
232 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
233 unsigned int rate = params_rate(hw_params);
235 mutex_lock(&efw->mutex);
236 err = snd_efw_stream_reserve_duplex(efw, rate);
238 ++efw->substreams_counter;
239 mutex_unlock(&efw->mutex);
245 static int pcm_hw_free(struct snd_pcm_substream *substream)
247 struct snd_efw *efw = substream->private_data;
249 mutex_lock(&efw->mutex);
251 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
252 --efw->substreams_counter;
254 snd_efw_stream_stop_duplex(efw);
256 mutex_unlock(&efw->mutex);
258 return snd_pcm_lib_free_vmalloc_buffer(substream);
261 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
263 struct snd_efw *efw = substream->private_data;
266 err = snd_efw_stream_start_duplex(efw);
268 amdtp_stream_pcm_prepare(&efw->tx_stream);
272 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
274 struct snd_efw *efw = substream->private_data;
277 err = snd_efw_stream_start_duplex(efw);
279 amdtp_stream_pcm_prepare(&efw->rx_stream);
284 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
286 struct snd_efw *efw = substream->private_data;
289 case SNDRV_PCM_TRIGGER_START:
290 amdtp_stream_pcm_trigger(&efw->tx_stream, substream);
292 case SNDRV_PCM_TRIGGER_STOP:
293 amdtp_stream_pcm_trigger(&efw->tx_stream, NULL);
301 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
303 struct snd_efw *efw = substream->private_data;
306 case SNDRV_PCM_TRIGGER_START:
307 amdtp_stream_pcm_trigger(&efw->rx_stream, substream);
309 case SNDRV_PCM_TRIGGER_STOP:
310 amdtp_stream_pcm_trigger(&efw->rx_stream, NULL);
319 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
321 struct snd_efw *efw = sbstrm->private_data;
322 return amdtp_stream_pcm_pointer(&efw->tx_stream);
324 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
326 struct snd_efw *efw = sbstrm->private_data;
327 return amdtp_stream_pcm_pointer(&efw->rx_stream);
330 static int pcm_capture_ack(struct snd_pcm_substream *substream)
332 struct snd_efw *efw = substream->private_data;
334 return amdtp_stream_pcm_ack(&efw->tx_stream);
337 static int pcm_playback_ack(struct snd_pcm_substream *substream)
339 struct snd_efw *efw = substream->private_data;
341 return amdtp_stream_pcm_ack(&efw->rx_stream);
344 int snd_efw_create_pcm_devices(struct snd_efw *efw)
346 static const struct snd_pcm_ops capture_ops = {
349 .ioctl = snd_pcm_lib_ioctl,
350 .hw_params = pcm_hw_params,
351 .hw_free = pcm_hw_free,
352 .prepare = pcm_capture_prepare,
353 .trigger = pcm_capture_trigger,
354 .pointer = pcm_capture_pointer,
355 .ack = pcm_capture_ack,
356 .page = snd_pcm_lib_get_vmalloc_page,
358 static const struct snd_pcm_ops playback_ops = {
361 .ioctl = snd_pcm_lib_ioctl,
362 .hw_params = pcm_hw_params,
363 .hw_free = pcm_hw_free,
364 .prepare = pcm_playback_prepare,
365 .trigger = pcm_playback_trigger,
366 .pointer = pcm_playback_pointer,
367 .ack = pcm_playback_ack,
368 .page = snd_pcm_lib_get_vmalloc_page,
373 err = snd_pcm_new(efw->card, efw->card->driver, 0, 1, 1, &pcm);
377 pcm->private_data = efw;
378 snprintf(pcm->name, sizeof(pcm->name), "%s PCM", efw->card->shortname);
379 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
380 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);