2 * fireworks_pcm.c - a part of driver for Fireworks based devices
4 * Copyright (c) 2009-2010 Clemens Ladisch
5 * Copyright (c) 2013-2014 Takashi Sakamoto
7 * Licensed under the terms of the GNU General Public License, version 2.
9 #include "./fireworks.h"
13 * Fireworks changes its AMDTP channels for PCM data according to its sampling
14 * rate. There are three modes. Here _XX is either _rx or _tx.
15 * 0: 32.0- 48.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels applied
16 * 1: 88.2- 96.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels_2x applied
17 * 2: 176.4-192.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels_4x applied
19 * The number of PCM channels for analog input and output are always fixed but
20 * the number of PCM channels for digital input and output are differed.
22 * Additionally, according to "AudioFire Owner's Manual Version 2.2", in some
23 * model, the number of PCM channels for digital input has more restriction
24 * depending on which digital interface is selected.
25 * - S/PDIF coaxial and optical : use input 1-2
26 * - ADAT optical at 32.0-48.0 kHz : use input 1-8
27 * - ADAT optical at 88.2-96.0 kHz : use input 1-4 (S/MUX format)
29 * The data in AMDTP channels for blank PCM channels are zero.
31 static const unsigned int freq_table[] = {
32 /* multiplier mode 0 */
36 /* multiplier mode 1 */
39 /* multiplier mode 2 */
44 static inline unsigned int
45 get_multiplier_mode_with_index(unsigned int index)
47 return ((int)index - 1) / 2;
50 int snd_efw_get_multiplier_mode(unsigned int sampling_rate, unsigned int *mode)
54 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
55 if (freq_table[i] == sampling_rate) {
56 *mode = get_multiplier_mode_with_index(i);
65 hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
67 unsigned int *pcm_channels = rule->private;
68 struct snd_interval *r =
69 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
70 const struct snd_interval *c =
71 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
72 struct snd_interval t = {
73 .min = UINT_MAX, .max = 0, .integer = 1
77 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
78 mode = get_multiplier_mode_with_index(i);
79 if (!snd_interval_test(c, pcm_channels[mode]))
82 t.min = min(t.min, freq_table[i]);
83 t.max = max(t.max, freq_table[i]);
86 return snd_interval_refine(r, &t);
90 hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
92 unsigned int *pcm_channels = rule->private;
93 struct snd_interval *c =
94 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
95 const struct snd_interval *r =
96 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
97 struct snd_interval t = {
98 .min = UINT_MAX, .max = 0, .integer = 1
100 unsigned int i, mode;
102 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
103 mode = get_multiplier_mode_with_index(i);
104 if (!snd_interval_test(r, freq_table[i]))
107 t.min = min(t.min, pcm_channels[mode]);
108 t.max = max(t.max, pcm_channels[mode]);
111 return snd_interval_refine(c, &t);
115 limit_channels(struct snd_pcm_hardware *hw, unsigned int *pcm_channels)
117 unsigned int i, mode;
119 hw->channels_min = UINT_MAX;
120 hw->channels_max = 0;
122 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
123 mode = get_multiplier_mode_with_index(i);
124 if (pcm_channels[mode] == 0)
127 hw->channels_min = min(hw->channels_min, pcm_channels[mode]);
128 hw->channels_max = max(hw->channels_max, pcm_channels[mode]);
133 limit_period_and_buffer(struct snd_pcm_hardware *hw)
135 hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
136 hw->periods_max = UINT_MAX;
138 hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
140 /* Just to prevent from allocating much pages. */
141 hw->period_bytes_max = hw->period_bytes_min * 2048;
142 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
146 pcm_init_hw_params(struct snd_efw *efw,
147 struct snd_pcm_substream *substream)
149 struct snd_pcm_runtime *runtime = substream->runtime;
150 struct amdtp_stream *s;
151 unsigned int *pcm_channels;
154 runtime->hw.info = SNDRV_PCM_INFO_BATCH |
155 SNDRV_PCM_INFO_BLOCK_TRANSFER |
156 SNDRV_PCM_INFO_INTERLEAVED |
157 SNDRV_PCM_INFO_JOINT_DUPLEX |
158 SNDRV_PCM_INFO_MMAP |
159 SNDRV_PCM_INFO_MMAP_VALID;
161 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
162 runtime->hw.formats = AMDTP_IN_PCM_FORMAT_BITS;
164 pcm_channels = efw->pcm_capture_channels;
166 runtime->hw.formats = AMDTP_OUT_PCM_FORMAT_BITS;
168 pcm_channels = efw->pcm_playback_channels;
172 runtime->hw.rates = efw->supported_sampling_rate,
173 snd_pcm_limit_hw_rates(runtime);
175 limit_channels(&runtime->hw, pcm_channels);
176 limit_period_and_buffer(&runtime->hw);
178 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
179 hw_rule_channels, pcm_channels,
180 SNDRV_PCM_HW_PARAM_RATE, -1);
184 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
185 hw_rule_rate, pcm_channels,
186 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
190 err = amdtp_stream_add_pcm_hw_constraints(s, runtime);
195 static int pcm_open(struct snd_pcm_substream *substream)
197 struct snd_efw *efw = substream->private_data;
198 unsigned int sampling_rate;
199 enum snd_efw_clock_source clock_source;
202 err = snd_efw_stream_lock_try(efw);
206 err = pcm_init_hw_params(efw, substream);
210 err = snd_efw_command_get_clock_source(efw, &clock_source);
215 * When source of clock is not internal or any PCM streams are running,
216 * available sampling rate is limited at current sampling rate.
218 if ((clock_source != SND_EFW_CLOCK_SOURCE_INTERNAL) ||
219 amdtp_stream_pcm_running(&efw->tx_stream) ||
220 amdtp_stream_pcm_running(&efw->rx_stream)) {
221 err = snd_efw_command_get_sampling_rate(efw, &sampling_rate);
224 substream->runtime->hw.rate_min = sampling_rate;
225 substream->runtime->hw.rate_max = sampling_rate;
228 snd_pcm_set_sync(substream);
232 snd_efw_stream_lock_release(efw);
236 static int pcm_close(struct snd_pcm_substream *substream)
238 struct snd_efw *efw = substream->private_data;
239 snd_efw_stream_lock_release(efw);
243 static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
244 struct snd_pcm_hw_params *hw_params)
246 struct snd_efw *efw = substream->private_data;
248 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
249 atomic_inc(&efw->capture_substreams);
250 amdtp_stream_set_pcm_format(&efw->tx_stream, params_format(hw_params));
252 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
253 params_buffer_bytes(hw_params));
255 static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
256 struct snd_pcm_hw_params *hw_params)
258 struct snd_efw *efw = substream->private_data;
260 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
261 atomic_inc(&efw->playback_substreams);
262 amdtp_stream_set_pcm_format(&efw->rx_stream, params_format(hw_params));
264 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
265 params_buffer_bytes(hw_params));
268 static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
270 struct snd_efw *efw = substream->private_data;
272 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
273 atomic_dec(&efw->capture_substreams);
275 snd_efw_stream_stop_duplex(efw);
277 return snd_pcm_lib_free_vmalloc_buffer(substream);
279 static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
281 struct snd_efw *efw = substream->private_data;
283 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
284 atomic_dec(&efw->playback_substreams);
286 snd_efw_stream_stop_duplex(efw);
288 return snd_pcm_lib_free_vmalloc_buffer(substream);
291 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
293 struct snd_efw *efw = substream->private_data;
294 struct snd_pcm_runtime *runtime = substream->runtime;
297 err = snd_efw_stream_start_duplex(efw, runtime->rate);
299 amdtp_stream_pcm_prepare(&efw->tx_stream);
303 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
305 struct snd_efw *efw = substream->private_data;
306 struct snd_pcm_runtime *runtime = substream->runtime;
309 err = snd_efw_stream_start_duplex(efw, runtime->rate);
311 amdtp_stream_pcm_prepare(&efw->rx_stream);
316 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
318 struct snd_efw *efw = substream->private_data;
321 case SNDRV_PCM_TRIGGER_START:
322 amdtp_stream_pcm_trigger(&efw->tx_stream, substream);
324 case SNDRV_PCM_TRIGGER_STOP:
325 amdtp_stream_pcm_trigger(&efw->tx_stream, NULL);
333 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
335 struct snd_efw *efw = substream->private_data;
338 case SNDRV_PCM_TRIGGER_START:
339 amdtp_stream_pcm_trigger(&efw->rx_stream, substream);
341 case SNDRV_PCM_TRIGGER_STOP:
342 amdtp_stream_pcm_trigger(&efw->rx_stream, NULL);
351 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
353 struct snd_efw *efw = sbstrm->private_data;
354 return amdtp_stream_pcm_pointer(&efw->tx_stream);
356 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
358 struct snd_efw *efw = sbstrm->private_data;
359 return amdtp_stream_pcm_pointer(&efw->rx_stream);
362 static const struct snd_pcm_ops pcm_capture_ops = {
365 .ioctl = snd_pcm_lib_ioctl,
366 .hw_params = pcm_capture_hw_params,
367 .hw_free = pcm_capture_hw_free,
368 .prepare = pcm_capture_prepare,
369 .trigger = pcm_capture_trigger,
370 .pointer = pcm_capture_pointer,
371 .page = snd_pcm_lib_get_vmalloc_page,
374 static const struct snd_pcm_ops pcm_playback_ops = {
377 .ioctl = snd_pcm_lib_ioctl,
378 .hw_params = pcm_playback_hw_params,
379 .hw_free = pcm_playback_hw_free,
380 .prepare = pcm_playback_prepare,
381 .trigger = pcm_playback_trigger,
382 .pointer = pcm_playback_pointer,
383 .page = snd_pcm_lib_get_vmalloc_page,
384 .mmap = snd_pcm_lib_mmap_vmalloc,
387 int snd_efw_create_pcm_devices(struct snd_efw *efw)
392 err = snd_pcm_new(efw->card, efw->card->driver, 0, 1, 1, &pcm);
396 pcm->private_data = efw;
397 snprintf(pcm->name, sizeof(pcm->name), "%s PCM", efw->card->shortname);
398 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
399 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);