1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011 Broadcom Corporation. All rights reserved. */
4 #include <linux/interrupt.h>
5 #include <linux/slab.h>
7 #include <sound/asoundef.h>
11 /* hardware definition */
12 static const struct snd_pcm_hardware snd_bcm2835_playback_hw = {
13 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
14 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
15 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
16 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
21 .buffer_bytes_max = 128 * 1024,
22 .period_bytes_min = 1 * 1024,
23 .period_bytes_max = 128 * 1024,
28 static const struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
29 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
30 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
31 .formats = SNDRV_PCM_FMTBIT_S16_LE,
32 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
38 .buffer_bytes_max = 128 * 1024,
39 .period_bytes_min = 1 * 1024,
40 .period_bytes_max = 128 * 1024,
45 static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
47 audio_info("Freeing up alsa stream here ..\n");
48 kfree(runtime->private_data);
49 runtime->private_data = NULL;
52 void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream)
54 unsigned int consumed = 0;
57 audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
58 alsa_stream ? alsa_stream->substream : 0);
60 if (alsa_stream->open)
61 consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
63 /* We get called only if playback was triggered, So, the number of buffers we retrieve in
64 * each iteration are the buffers that have been played out already
67 if (alsa_stream->period_size) {
68 if ((alsa_stream->pos / alsa_stream->period_size) !=
69 ((alsa_stream->pos + consumed) / alsa_stream->period_size))
72 audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
75 alsa_stream->buffer_size,
76 (int) (alsa_stream->period_size * alsa_stream->substream->runtime->periods),
77 frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
79 if (alsa_stream->buffer_size) {
80 alsa_stream->pos += consumed & ~(1 << 30);
81 alsa_stream->pos %= alsa_stream->buffer_size;
84 if (alsa_stream->substream) {
86 snd_pcm_period_elapsed(alsa_stream->substream);
88 audio_warning(" unexpected NULL substream\n");
93 static int snd_bcm2835_playback_open_generic(
94 struct snd_pcm_substream *substream, int spdif)
96 struct bcm2835_chip *chip = snd_pcm_substream_chip(substream);
97 struct snd_pcm_runtime *runtime = substream->runtime;
98 struct bcm2835_alsa_stream *alsa_stream;
102 if (mutex_lock_interruptible(&chip->audio_mutex)) {
103 audio_error("Interrupted whilst waiting for lock\n");
106 audio_info("Alsa open (%d)\n", substream->number);
107 idx = substream->number;
109 if (spdif && chip->opened) {
112 } else if (!spdif && (chip->opened & (1 << idx))) {
116 if (idx >= MAX_SUBSTREAMS) {
118 ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
119 idx, MAX_SUBSTREAMS);
124 /* Check if we are ready */
125 if (!(chip->avail_substreams & (1 << idx))) {
126 /* We are not ready yet */
127 audio_error("substream(%d) device is not ready yet\n", idx);
132 alsa_stream = kzalloc(sizeof(*alsa_stream), GFP_KERNEL);
138 /* Initialise alsa_stream */
139 alsa_stream->chip = chip;
140 alsa_stream->substream = substream;
141 alsa_stream->idx = idx;
143 spin_lock_init(&alsa_stream->lock);
145 err = bcm2835_audio_open(alsa_stream);
150 runtime->private_data = alsa_stream;
151 runtime->private_free = snd_bcm2835_playback_free;
153 runtime->hw = snd_bcm2835_playback_spdif_hw;
155 /* clear spdif status, as we are not in spdif mode */
156 chip->spdif_status = 0;
157 runtime->hw = snd_bcm2835_playback_hw;
159 /* minimum 16 bytes alignment (for vchiq bulk transfers) */
160 snd_pcm_hw_constraint_step(runtime,
162 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
165 chip->alsa_stream[idx] = alsa_stream;
167 chip->opened |= (1 << idx);
168 alsa_stream->open = 1;
169 alsa_stream->draining = 1;
172 mutex_unlock(&chip->audio_mutex);
177 static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
179 return snd_bcm2835_playback_open_generic(substream, 0);
182 static int snd_bcm2835_playback_spdif_open(struct snd_pcm_substream *substream)
184 return snd_bcm2835_playback_open_generic(substream, 1);
188 static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
190 /* the hardware-specific codes will be here */
192 struct bcm2835_chip *chip;
193 struct snd_pcm_runtime *runtime;
194 struct bcm2835_alsa_stream *alsa_stream;
196 chip = snd_pcm_substream_chip(substream);
197 if (mutex_lock_interruptible(&chip->audio_mutex)) {
198 audio_error("Interrupted whilst waiting for lock\n");
201 runtime = substream->runtime;
202 alsa_stream = runtime->private_data;
204 audio_info("Alsa close\n");
207 * Call stop if it's still running. This happens when app
208 * is force killed and we don't get a stop trigger.
210 if (alsa_stream->running) {
213 err = bcm2835_audio_stop(alsa_stream);
214 alsa_stream->running = 0;
216 audio_error(" Failed to STOP alsa device\n");
219 alsa_stream->period_size = 0;
220 alsa_stream->buffer_size = 0;
222 if (alsa_stream->open) {
223 alsa_stream->open = 0;
224 bcm2835_audio_close(alsa_stream);
226 if (alsa_stream->chip)
227 alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
229 * Do not free up alsa_stream here, it will be freed up by
230 * runtime->private_free callback we registered in *_open above
233 chip->opened &= ~(1 << substream->number);
235 mutex_unlock(&chip->audio_mutex);
240 /* hw_params callback */
241 static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
242 struct snd_pcm_hw_params *params)
244 struct snd_pcm_runtime *runtime = substream->runtime;
245 struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
248 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
251 (" pcm_lib_malloc failed to allocated pages for buffers\n");
255 alsa_stream->channels = params_channels(params);
256 alsa_stream->params_rate = params_rate(params);
257 alsa_stream->pcm_format_width = snd_pcm_format_width(params_format(params));
262 /* hw_free callback */
263 static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
265 return snd_pcm_lib_free_pages(substream);
268 /* prepare callback */
269 static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
271 struct bcm2835_chip *chip = snd_pcm_substream_chip(substream);
272 struct snd_pcm_runtime *runtime = substream->runtime;
273 struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
277 if (mutex_lock_interruptible(&chip->audio_mutex))
280 /* notify the vchiq that it should enter spdif passthrough mode by
281 * setting channels=0 (see
282 * https://github.com/raspberrypi/linux/issues/528)
284 if (chip->spdif_status & IEC958_AES0_NONAUDIO)
287 channels = alsa_stream->channels;
289 err = bcm2835_audio_set_params(alsa_stream, channels,
290 alsa_stream->params_rate,
291 alsa_stream->pcm_format_width);
293 audio_error(" error setting hw params\n");
295 bcm2835_audio_setup(alsa_stream);
297 /* in preparation of the stream, set the controls (volume level) of the stream */
298 bcm2835_audio_set_ctls(alsa_stream->chip);
300 memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
302 alsa_stream->pcm_indirect.hw_buffer_size =
303 alsa_stream->pcm_indirect.sw_buffer_size =
304 snd_pcm_lib_buffer_bytes(substream);
306 alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
307 alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
308 alsa_stream->pos = 0;
310 audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
311 alsa_stream->buffer_size, alsa_stream->period_size,
312 alsa_stream->pos, runtime->frame_bits);
314 mutex_unlock(&chip->audio_mutex);
318 static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
319 struct snd_pcm_indirect *rec, size_t bytes)
321 struct snd_pcm_runtime *runtime = substream->runtime;
322 struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
323 void *src = (void *) (substream->runtime->dma_area + rec->sw_data);
326 err = bcm2835_audio_write(alsa_stream, bytes, src);
328 audio_error(" Failed to transfer to alsa device (%d)\n", err);
332 static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
334 struct snd_pcm_runtime *runtime = substream->runtime;
335 struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
336 struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
338 pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
339 return snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
340 snd_bcm2835_pcm_transfer);
343 /* trigger callback */
344 static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
346 struct snd_pcm_runtime *runtime = substream->runtime;
347 struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
351 case SNDRV_PCM_TRIGGER_START:
352 audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
353 alsa_stream->running);
354 if (!alsa_stream->running) {
355 err = bcm2835_audio_start(alsa_stream);
357 alsa_stream->pcm_indirect.hw_io =
358 alsa_stream->pcm_indirect.hw_data =
359 bytes_to_frames(runtime,
361 substream->ops->ack(substream);
362 alsa_stream->running = 1;
363 alsa_stream->draining = 1;
365 audio_error(" Failed to START alsa device (%d)\n", err);
369 case SNDRV_PCM_TRIGGER_STOP:
371 ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
372 alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
373 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
374 audio_info("DRAINING\n");
375 alsa_stream->draining = 1;
377 audio_info("DROPPING\n");
378 alsa_stream->draining = 0;
380 if (alsa_stream->running) {
381 err = bcm2835_audio_stop(alsa_stream);
383 audio_error(" Failed to STOP alsa device (%d)\n", err);
384 alsa_stream->running = 0;
394 /* pointer callback */
395 static snd_pcm_uframes_t
396 snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
398 struct snd_pcm_runtime *runtime = substream->runtime;
399 struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
401 audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
402 frames_to_bytes(runtime, runtime->status->hw_ptr),
403 frames_to_bytes(runtime, runtime->control->appl_ptr),
406 return snd_pcm_indirect_playback_pointer(substream,
407 &alsa_stream->pcm_indirect,
411 static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
412 unsigned int cmd, void *arg)
414 int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
416 audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
417 cmd, arg, arg ? *(unsigned int *)arg : 0, ret);
422 static const struct snd_pcm_ops snd_bcm2835_playback_ops = {
423 .open = snd_bcm2835_playback_open,
424 .close = snd_bcm2835_playback_close,
425 .ioctl = snd_bcm2835_pcm_lib_ioctl,
426 .hw_params = snd_bcm2835_pcm_hw_params,
427 .hw_free = snd_bcm2835_pcm_hw_free,
428 .prepare = snd_bcm2835_pcm_prepare,
429 .trigger = snd_bcm2835_pcm_trigger,
430 .pointer = snd_bcm2835_pcm_pointer,
431 .ack = snd_bcm2835_pcm_ack,
434 static const struct snd_pcm_ops snd_bcm2835_playback_spdif_ops = {
435 .open = snd_bcm2835_playback_spdif_open,
436 .close = snd_bcm2835_playback_close,
437 .ioctl = snd_bcm2835_pcm_lib_ioctl,
438 .hw_params = snd_bcm2835_pcm_hw_params,
439 .hw_free = snd_bcm2835_pcm_hw_free,
440 .prepare = snd_bcm2835_pcm_prepare,
441 .trigger = snd_bcm2835_pcm_trigger,
442 .pointer = snd_bcm2835_pcm_pointer,
443 .ack = snd_bcm2835_pcm_ack,
446 /* create a pcm device */
447 int snd_bcm2835_new_pcm(struct bcm2835_chip *chip, u32 numchannels)
452 mutex_init(&chip->audio_mutex);
453 if (mutex_lock_interruptible(&chip->audio_mutex)) {
454 audio_error("Interrupted whilst waiting for lock\n");
457 err = snd_pcm_new(chip->card, "bcm2835 ALSA", 0, numchannels, 0, &pcm);
460 pcm->private_data = chip;
461 strcpy(pcm->name, "bcm2835 ALSA");
463 chip->dest = AUDIO_DEST_AUTO;
464 chip->volume = alsa2chip(0);
465 chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
467 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
468 &snd_bcm2835_playback_ops);
470 /* pre-allocation of buffers */
471 /* NOTE: this may fail */
472 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
473 snd_dma_continuous_data(GFP_KERNEL),
474 snd_bcm2835_playback_hw.buffer_bytes_max,
475 snd_bcm2835_playback_hw.buffer_bytes_max);
478 mutex_unlock(&chip->audio_mutex);
483 int snd_bcm2835_new_spdif_pcm(struct bcm2835_chip *chip)
488 if (mutex_lock_interruptible(&chip->audio_mutex)) {
489 audio_error("Interrupted whilst waiting for lock\n");
492 err = snd_pcm_new(chip->card, "bcm2835 ALSA", 1, 1, 0, &pcm);
496 pcm->private_data = chip;
497 strcpy(pcm->name, "bcm2835 IEC958/HDMI");
498 chip->pcm_spdif = pcm;
499 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
500 &snd_bcm2835_playback_spdif_ops);
502 /* pre-allocation of buffers */
503 /* NOTE: this may fail */
504 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
505 snd_dma_continuous_data(GFP_KERNEL),
506 snd_bcm2835_playback_spdif_hw.buffer_bytes_max, snd_bcm2835_playback_spdif_hw.buffer_bytes_max);
508 mutex_unlock(&chip->audio_mutex);
513 int snd_bcm2835_new_simple_pcm(struct bcm2835_chip *chip,
515 enum snd_bcm2835_route route,
521 mutex_init(&chip->audio_mutex);
523 err = snd_pcm_new(chip->card, name, 0, numchannels,
528 pcm->private_data = chip;
529 strcpy(pcm->name, name);
532 chip->volume = alsa2chip(0);
533 chip->mute = CTRL_VOL_UNMUTE;
535 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
536 &snd_bcm2835_playback_ops);
538 snd_pcm_lib_preallocate_pages_for_all(
540 SNDRV_DMA_TYPE_CONTINUOUS,
541 snd_dma_continuous_data(GFP_KERNEL),
542 snd_bcm2835_playback_hw.buffer_bytes_max,
543 snd_bcm2835_playback_hw.buffer_bytes_max);