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 limit_channels_and_rates(struct snd_dice *dice,
13 struct snd_pcm_runtime *runtime,
14 struct amdtp_stream *stream)
16 struct snd_pcm_hardware *hw = &runtime->hw;
22 * Retrieve current Multi Bit Linear Audio data channel and limit to
25 if (stream == &dice->tx_stream[0]) {
26 err = snd_dice_transaction_read_tx(dice, TX_NUMBER_AUDIO,
29 err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
35 hw->channels_min = hw->channels_max = be32_to_cpu(reg[0]);
37 /* Retrieve current sampling transfer frequency and limit to it. */
38 err = snd_dice_transaction_get_rate(dice, &rate);
42 hw->rates = snd_pcm_rate_to_rate_bit(rate);
43 snd_pcm_limit_hw_rates(runtime);
48 static void limit_period_and_buffer(struct snd_pcm_hardware *hw)
50 hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
51 hw->periods_max = UINT_MAX;
53 hw->period_bytes_min = 4 * hw->channels_max; /* byte for a frame */
55 /* Just to prevent from allocating much pages. */
56 hw->period_bytes_max = hw->period_bytes_min * 2048;
57 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
60 static int init_hw_info(struct snd_dice *dice,
61 struct snd_pcm_substream *substream)
63 struct snd_pcm_runtime *runtime = substream->runtime;
64 struct snd_pcm_hardware *hw = &runtime->hw;
65 struct amdtp_stream *stream;
68 hw->info = SNDRV_PCM_INFO_MMAP |
69 SNDRV_PCM_INFO_MMAP_VALID |
70 SNDRV_PCM_INFO_BATCH |
71 SNDRV_PCM_INFO_INTERLEAVED |
72 SNDRV_PCM_INFO_JOINT_DUPLEX |
73 SNDRV_PCM_INFO_BLOCK_TRANSFER;
75 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
76 hw->formats = AM824_IN_PCM_FORMAT_BITS;
77 stream = &dice->tx_stream[0];
79 hw->formats = AM824_OUT_PCM_FORMAT_BITS;
80 stream = &dice->rx_stream[0];
83 err = limit_channels_and_rates(dice, runtime, stream);
86 limit_period_and_buffer(hw);
88 return amdtp_am824_add_pcm_hw_constraints(stream, runtime);
91 static int pcm_open(struct snd_pcm_substream *substream)
93 struct snd_dice *dice = substream->private_data;
96 err = snd_dice_stream_lock_try(dice);
100 err = init_hw_info(dice, substream);
104 snd_pcm_set_sync(substream);
108 snd_dice_stream_lock_release(dice);
112 static int pcm_close(struct snd_pcm_substream *substream)
114 struct snd_dice *dice = substream->private_data;
116 snd_dice_stream_lock_release(dice);
121 static int capture_hw_params(struct snd_pcm_substream *substream,
122 struct snd_pcm_hw_params *hw_params)
124 struct snd_dice *dice = substream->private_data;
125 struct amdtp_stream *stream = &dice->tx_stream[0];
128 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
129 params_buffer_bytes(hw_params));
133 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
134 mutex_lock(&dice->mutex);
135 dice->substreams_counter++;
136 mutex_unlock(&dice->mutex);
139 amdtp_am824_set_pcm_format(stream, params_format(hw_params));
143 static int playback_hw_params(struct snd_pcm_substream *substream,
144 struct snd_pcm_hw_params *hw_params)
146 struct snd_dice *dice = substream->private_data;
147 struct amdtp_stream *stream = &dice->rx_stream[0];
150 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
151 params_buffer_bytes(hw_params));
155 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
156 mutex_lock(&dice->mutex);
157 dice->substreams_counter++;
158 mutex_unlock(&dice->mutex);
161 amdtp_am824_set_pcm_format(stream, params_format(hw_params));
166 static int capture_hw_free(struct snd_pcm_substream *substream)
168 struct snd_dice *dice = substream->private_data;
170 mutex_lock(&dice->mutex);
172 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
173 dice->substreams_counter--;
175 snd_dice_stream_stop_duplex(dice);
177 mutex_unlock(&dice->mutex);
179 return snd_pcm_lib_free_vmalloc_buffer(substream);
182 static int playback_hw_free(struct snd_pcm_substream *substream)
184 struct snd_dice *dice = substream->private_data;
186 mutex_lock(&dice->mutex);
188 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
189 dice->substreams_counter--;
191 snd_dice_stream_stop_duplex(dice);
193 mutex_unlock(&dice->mutex);
195 return snd_pcm_lib_free_vmalloc_buffer(substream);
198 static int capture_prepare(struct snd_pcm_substream *substream)
200 struct snd_dice *dice = substream->private_data;
201 struct amdtp_stream *stream = &dice->tx_stream[0];
204 mutex_lock(&dice->mutex);
205 err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
206 mutex_unlock(&dice->mutex);
208 amdtp_stream_pcm_prepare(stream);
212 static int playback_prepare(struct snd_pcm_substream *substream)
214 struct snd_dice *dice = substream->private_data;
215 struct amdtp_stream *stream = &dice->rx_stream[0];
218 mutex_lock(&dice->mutex);
219 err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
220 mutex_unlock(&dice->mutex);
222 amdtp_stream_pcm_prepare(stream);
227 static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
229 struct snd_dice *dice = substream->private_data;
230 struct amdtp_stream *stream = &dice->tx_stream[0];
233 case SNDRV_PCM_TRIGGER_START:
234 amdtp_stream_pcm_trigger(stream, substream);
236 case SNDRV_PCM_TRIGGER_STOP:
237 amdtp_stream_pcm_trigger(stream, NULL);
245 static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
247 struct snd_dice *dice = substream->private_data;
248 struct amdtp_stream *stream = &dice->rx_stream[0];
251 case SNDRV_PCM_TRIGGER_START:
252 amdtp_stream_pcm_trigger(stream, substream);
254 case SNDRV_PCM_TRIGGER_STOP:
255 amdtp_stream_pcm_trigger(stream, NULL);
264 static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
266 struct snd_dice *dice = substream->private_data;
267 struct amdtp_stream *stream = &dice->tx_stream[0];
269 return amdtp_stream_pcm_pointer(stream);
271 static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
273 struct snd_dice *dice = substream->private_data;
274 struct amdtp_stream *stream = &dice->rx_stream[0];
276 return amdtp_stream_pcm_pointer(stream);
279 int snd_dice_create_pcm(struct snd_dice *dice)
281 static struct snd_pcm_ops capture_ops = {
284 .ioctl = snd_pcm_lib_ioctl,
285 .hw_params = capture_hw_params,
286 .hw_free = capture_hw_free,
287 .prepare = capture_prepare,
288 .trigger = capture_trigger,
289 .pointer = capture_pointer,
290 .page = snd_pcm_lib_get_vmalloc_page,
291 .mmap = snd_pcm_lib_mmap_vmalloc,
293 static struct snd_pcm_ops playback_ops = {
296 .ioctl = snd_pcm_lib_ioctl,
297 .hw_params = playback_hw_params,
298 .hw_free = playback_hw_free,
299 .prepare = playback_prepare,
300 .trigger = playback_trigger,
301 .pointer = playback_pointer,
302 .page = snd_pcm_lib_get_vmalloc_page,
303 .mmap = snd_pcm_lib_mmap_vmalloc,
307 unsigned int capture, playback;
311 * Check whether PCM substreams are required.
313 * TODO: in the case that any PCM substreams are not avail at a certain
314 * sampling transfer frequency?
316 err = snd_dice_transaction_read_tx(dice, TX_NUMBER_AUDIO,
320 if (be32_to_cpu(reg) > 0)
323 err = snd_dice_transaction_read_rx(dice, RX_NUMBER_AUDIO,
327 if (be32_to_cpu(reg) > 0)
330 err = snd_pcm_new(dice->card, "DICE", 0, playback, capture, &pcm);
333 pcm->private_data = dice;
334 strcpy(pcm->name, dice->card->shortname);
337 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
340 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);