1 From 340dfdd9f8dfad3f60665cac7f19d774c001b0de Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Mon, 26 Mar 2012 22:15:50 +0100
4 Subject: [PATCH] bcm2708: alsa sound driver
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Signed-off-by: popcornmix <popcornmix@gmail.com>
11 alsa: add mmap support and some cleanups to bcm2835 ALSA driver
13 snd-bcm2835: Add support for spdif/hdmi passthrough
15 This adds a dedicated subdevice which can be used for passthrough of non-audio
16 formats (ie encoded a52) through the hdmi audio link. In addition to this
17 driver extension an appropriate card config is required to make alsa-lib
18 support the AES parameters for this device.
20 snd-bcm2708: Add mutex, improve logging
22 Fix for ALSA driver crash
24 Avoids an issue when closing and opening vchiq where a message can arrive before service handle has been written
26 alsa: reduce severity of expected warning message
28 snd-bcm2708: Fix dmesg spam for non-error case
30 alsa: Ensure mutexes are released through error paths
32 alsa: Make interrupted close paths quieter
34 BCM270x: Add onboard sound device to Device Tree
36 Add Device Tree support to alsa driver.
37 Add device to Device Tree.
38 Don't add platform devices when booting in DT mode.
40 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
42 bcm2835: access controls under the audio mutex
44 I don't think the ALSA framework provides any kind of automatic
45 synchronization within the control callbacks. We most likely need
46 to ensure this manually, so add locking around all access to shared
47 mutable data. In particular, bcm2835_audio_set_ctls() should
48 probably always be called under our own audio lock.
50 snd-bcm2835: Don't allow responses from VC to be interrupted by user signals
52 There should always be a response, and retry after a signal interruption is not handled, so don't report
55 See: https://github.com/raspberrypi/linux/issues/1560
57 snd-bcm2835: Use bcm2835_hw params in preallocate
59 sound/arm/Kconfig | 7 +
60 sound/arm/Makefile | 5 +
61 sound/arm/bcm2835-ctl.c | 350 +++++++++++++++
62 sound/arm/bcm2835-pcm.c | 563 +++++++++++++++++++++++
63 sound/arm/bcm2835-vchiq.c | 889 +++++++++++++++++++++++++++++++++++++
64 sound/arm/bcm2835.c | 511 +++++++++++++++++++++
65 sound/arm/bcm2835.h | 167 +++++++
66 sound/arm/vc_vchi_audioserv_defs.h | 116 +++++
67 8 files changed, 2608 insertions(+)
68 create mode 100755 sound/arm/bcm2835-ctl.c
69 create mode 100755 sound/arm/bcm2835-pcm.c
70 create mode 100755 sound/arm/bcm2835-vchiq.c
71 create mode 100644 sound/arm/bcm2835.c
72 create mode 100755 sound/arm/bcm2835.h
73 create mode 100644 sound/arm/vc_vchi_audioserv_defs.h
75 --- a/sound/arm/Kconfig
76 +++ b/sound/arm/Kconfig
77 @@ -32,6 +32,13 @@ config SND_PXA2XX_AC97
78 Say Y or M if you want to support any AC97 codec attached to
79 the PXA2xx AC97 interface.
82 + tristate "BCM2835 ALSA driver"
83 + depends on ARCH_BCM2835 && BCM2708_VCHIQ && SND
86 + Say Y or M if you want to support BCM2835 Alsa pcm card driver
91 --- a/sound/arm/Makefile
92 +++ b/sound/arm/Makefile
93 @@ -14,3 +14,8 @@ snd-pxa2xx-lib-$(CONFIG_SND_PXA2XX_LIB_A
95 obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
96 snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
98 +obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
99 +snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
101 +ccflags-y += -Idrivers/staging/vc04_services -Idrivers/staging/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
103 +++ b/sound/arm/bcm2835-ctl.c
105 +/*****************************************************************************
106 +* Copyright 2011 Broadcom Corporation. All rights reserved.
108 +* Unless you and Broadcom execute a separate written software license
109 +* agreement governing use of this software, this software is licensed to you
110 +* under the terms of the GNU General Public License version 2, available at
111 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
113 +* Notwithstanding the above, under no circumstances may you combine this
114 +* software in any way with any other Broadcom software provided under a
115 +* license other than the GPL, without Broadcom's express prior written
117 +*****************************************************************************/
119 +#include <linux/platform_device.h>
120 +#include <linux/init.h>
121 +#include <linux/io.h>
122 +#include <linux/jiffies.h>
123 +#include <linux/slab.h>
124 +#include <linux/time.h>
125 +#include <linux/wait.h>
126 +#include <linux/delay.h>
127 +#include <linux/moduleparam.h>
128 +#include <linux/sched.h>
130 +#include <sound/core.h>
131 +#include <sound/control.h>
132 +#include <sound/pcm.h>
133 +#include <sound/pcm_params.h>
134 +#include <sound/rawmidi.h>
135 +#include <sound/initval.h>
136 +#include <sound/tlv.h>
137 +#include <sound/asoundef.h>
139 +#include "bcm2835.h"
141 +/* volume maximum and minimum in terms of 0.01dB */
142 +#define CTRL_VOL_MAX 400
143 +#define CTRL_VOL_MIN -10239 /* originally -10240 */
146 +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
147 + struct snd_ctl_elem_info *uinfo)
149 + audio_info(" ... IN\n");
150 + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
151 + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
153 + uinfo->value.integer.min = CTRL_VOL_MIN;
154 + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
155 + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
156 + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
158 + uinfo->value.integer.min = 0;
159 + uinfo->value.integer.max = 1;
160 + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
161 + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
163 + uinfo->value.integer.min = 0;
164 + uinfo->value.integer.max = AUDIO_DEST_MAX-1;
166 + audio_info(" ... OUT\n");
170 +/* toggles mute on or off depending on the value of nmute, and returns
171 + * 1 if the mute value was changed, otherwise 0
173 +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
175 + /* if settings are ok, just return 0 */
176 + if(chip->mute == nmute)
179 + /* if the sound is muted then we need to unmute */
180 + if(chip->mute == CTRL_VOL_MUTE)
182 + chip->volume = chip->old_volume; /* copy the old volume back */
183 + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
185 + else /* otherwise we mute */
187 + chip->old_volume = chip->volume;
188 + chip->volume = 26214; /* set volume to minimum level AKA mute */
189 + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
192 + chip->mute = nmute;
196 +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
197 + struct snd_ctl_elem_value *ucontrol)
199 + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
201 + if (mutex_lock_interruptible(&chip->audio_mutex))
204 + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
206 + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
207 + ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
208 + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
209 + ucontrol->value.integer.value[0] = chip->mute;
210 + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
211 + ucontrol->value.integer.value[0] = chip->dest;
213 + mutex_unlock(&chip->audio_mutex);
217 +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
218 + struct snd_ctl_elem_value *ucontrol)
220 + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
223 + if (mutex_lock_interruptible(&chip->audio_mutex))
226 + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
227 + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
228 + if (chip->mute == CTRL_VOL_MUTE) {
229 + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
230 + changed = 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
234 + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
236 + chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
240 + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
241 + /* Now implemented */
242 + audio_info(" Mute attempted\n");
243 + changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
245 + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
246 + if (ucontrol->value.integer.value[0] != chip->dest) {
247 + chip->dest = ucontrol->value.integer.value[0];
253 + if (bcm2835_audio_set_ctls(chip))
254 + printk(KERN_ERR "Failed to set ALSA controls..\n");
258 + mutex_unlock(&chip->audio_mutex);
262 +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
264 +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
266 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
267 + .name = "PCM Playback Volume",
269 + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
270 + .private_value = PCM_PLAYBACK_VOLUME,
271 + .info = snd_bcm2835_ctl_info,
272 + .get = snd_bcm2835_ctl_get,
273 + .put = snd_bcm2835_ctl_put,
275 + .tlv = {.p = snd_bcm2835_db_scale}
278 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
279 + .name = "PCM Playback Switch",
281 + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
282 + .private_value = PCM_PLAYBACK_MUTE,
283 + .info = snd_bcm2835_ctl_info,
284 + .get = snd_bcm2835_ctl_get,
285 + .put = snd_bcm2835_ctl_put,
289 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
290 + .name = "PCM Playback Route",
292 + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
293 + .private_value = PCM_PLAYBACK_DEVICE,
294 + .info = snd_bcm2835_ctl_info,
295 + .get = snd_bcm2835_ctl_get,
296 + .put = snd_bcm2835_ctl_put,
301 +static int snd_bcm2835_spdif_default_info(struct snd_kcontrol *kcontrol,
302 + struct snd_ctl_elem_info *uinfo)
304 + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
309 +static int snd_bcm2835_spdif_default_get(struct snd_kcontrol *kcontrol,
310 + struct snd_ctl_elem_value *ucontrol)
312 + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
315 + if (mutex_lock_interruptible(&chip->audio_mutex))
318 + for (i = 0; i < 4; i++)
319 + ucontrol->value.iec958.status[i] =
320 + (chip->spdif_status >> (i * 8)) && 0xff;
322 + mutex_unlock(&chip->audio_mutex);
326 +static int snd_bcm2835_spdif_default_put(struct snd_kcontrol *kcontrol,
327 + struct snd_ctl_elem_value *ucontrol)
329 + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
330 + unsigned int val = 0;
333 + if (mutex_lock_interruptible(&chip->audio_mutex))
336 + for (i = 0; i < 4; i++)
337 + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
339 + change = val != chip->spdif_status;
340 + chip->spdif_status = val;
342 + mutex_unlock(&chip->audio_mutex);
346 +static int snd_bcm2835_spdif_mask_info(struct snd_kcontrol *kcontrol,
347 + struct snd_ctl_elem_info *uinfo)
349 + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
354 +static int snd_bcm2835_spdif_mask_get(struct snd_kcontrol *kcontrol,
355 + struct snd_ctl_elem_value *ucontrol)
357 + /* bcm2835 supports only consumer mode and sets all other format flags
358 + * automatically. So the only thing left is signalling non-audio
360 + ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO;
364 +static int snd_bcm2835_spdif_stream_info(struct snd_kcontrol *kcontrol,
365 + struct snd_ctl_elem_info *uinfo)
367 + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
372 +static int snd_bcm2835_spdif_stream_get(struct snd_kcontrol *kcontrol,
373 + struct snd_ctl_elem_value *ucontrol)
375 + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
378 + if (mutex_lock_interruptible(&chip->audio_mutex))
381 + for (i = 0; i < 4; i++)
382 + ucontrol->value.iec958.status[i] =
383 + (chip->spdif_status >> (i * 8)) & 0xff;
385 + mutex_unlock(&chip->audio_mutex);
389 +static int snd_bcm2835_spdif_stream_put(struct snd_kcontrol *kcontrol,
390 + struct snd_ctl_elem_value *ucontrol)
392 + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
393 + unsigned int val = 0;
396 + if (mutex_lock_interruptible(&chip->audio_mutex))
399 + for (i = 0; i < 4; i++)
400 + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
401 + change = val != chip->spdif_status;
402 + chip->spdif_status = val;
404 + mutex_unlock(&chip->audio_mutex);
408 +static struct snd_kcontrol_new snd_bcm2835_spdif[] = {
410 + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
411 + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
412 + .info = snd_bcm2835_spdif_default_info,
413 + .get = snd_bcm2835_spdif_default_get,
414 + .put = snd_bcm2835_spdif_default_put
417 + .access = SNDRV_CTL_ELEM_ACCESS_READ,
418 + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
419 + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
420 + .info = snd_bcm2835_spdif_mask_info,
421 + .get = snd_bcm2835_spdif_mask_get,
424 + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
425 + SNDRV_CTL_ELEM_ACCESS_INACTIVE,
426 + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
427 + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
428 + .info = snd_bcm2835_spdif_stream_info,
429 + .get = snd_bcm2835_spdif_stream_get,
430 + .put = snd_bcm2835_spdif_stream_put,
434 +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
439 + strcpy(chip->card->mixername, "Broadcom Mixer");
440 + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
442 + snd_ctl_add(chip->card,
443 + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
447 + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) {
448 + err = snd_ctl_add(chip->card,
449 + snd_ctl_new1(&snd_bcm2835_spdif[idx], chip));
456 +++ b/sound/arm/bcm2835-pcm.c
458 +/*****************************************************************************
459 +* Copyright 2011 Broadcom Corporation. All rights reserved.
461 +* Unless you and Broadcom execute a separate written software license
462 +* agreement governing use of this software, this software is licensed to you
463 +* under the terms of the GNU General Public License version 2, available at
464 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
466 +* Notwithstanding the above, under no circumstances may you combine this
467 +* software in any way with any other Broadcom software provided under a
468 +* license other than the GPL, without Broadcom's express prior written
470 +*****************************************************************************/
472 +#include <linux/interrupt.h>
473 +#include <linux/slab.h>
475 +#include <sound/asoundef.h>
477 +#include "bcm2835.h"
479 +/* hardware definition */
480 +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
481 + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
482 + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
483 + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
484 + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
489 + .buffer_bytes_max = 128 * 1024,
490 + .period_bytes_min = 1 * 1024,
491 + .period_bytes_max = 128 * 1024,
493 + .periods_max = 128,
496 +static struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
497 + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
498 + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
499 + .formats = SNDRV_PCM_FMTBIT_S16_LE,
500 + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
501 + SNDRV_PCM_RATE_48000,
506 + .buffer_bytes_max = 128 * 1024,
507 + .period_bytes_min = 1 * 1024,
508 + .period_bytes_max = 128 * 1024,
510 + .periods_max = 128,
513 +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
515 + audio_info("Freeing up alsa stream here ..\n");
516 + if (runtime->private_data)
517 + kfree(runtime->private_data);
518 + runtime->private_data = NULL;
521 +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
523 + bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
524 + uint32_t consumed = 0;
525 + int new_period = 0;
527 + audio_info(" .. IN\n");
529 + audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
530 + alsa_stream ? alsa_stream->substream : 0);
532 + if (alsa_stream->open)
533 + consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
535 + /* We get called only if playback was triggered, So, the number of buffers we retrieve in
536 + * each iteration are the buffers that have been played out already
539 + if (alsa_stream->period_size) {
540 + if ((alsa_stream->pos / alsa_stream->period_size) !=
541 + ((alsa_stream->pos + consumed) / alsa_stream->period_size))
544 + audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
547 + alsa_stream->buffer_size,
548 + (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
549 + frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
551 + if (alsa_stream->buffer_size) {
552 + alsa_stream->pos += consumed &~ (1<<30);
553 + alsa_stream->pos %= alsa_stream->buffer_size;
556 + if (alsa_stream->substream) {
558 + snd_pcm_period_elapsed(alsa_stream->substream);
560 + audio_warning(" unexpected NULL substream\n");
562 + audio_info(" .. OUT\n");
564 + return IRQ_HANDLED;
568 +static int snd_bcm2835_playback_open_generic(
569 + struct snd_pcm_substream *substream, int spdif)
571 + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
572 + struct snd_pcm_runtime *runtime = substream->runtime;
573 + bcm2835_alsa_stream_t *alsa_stream;
577 + audio_info(" .. IN (%d)\n", substream->number);
579 + if(mutex_lock_interruptible(&chip->audio_mutex))
581 + audio_error("Interrupted whilst waiting for lock\n");
584 + audio_info("Alsa open (%d)\n", substream->number);
585 + idx = substream->number;
587 + if (spdif && chip->opened != 0) {
591 + else if (!spdif && (chip->opened & (1 << idx))) {
595 + if (idx > MAX_SUBSTREAMS) {
597 + ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
598 + idx, MAX_SUBSTREAMS);
603 + /* Check if we are ready */
604 + if (!(chip->avail_substreams & (1 << idx))) {
605 + /* We are not ready yet */
606 + audio_error("substream(%d) device is not ready yet\n", idx);
611 + alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
612 + if (alsa_stream == NULL) {
617 + /* Initialise alsa_stream */
618 + alsa_stream->chip = chip;
619 + alsa_stream->substream = substream;
620 + alsa_stream->idx = idx;
622 + sema_init(&alsa_stream->buffers_update_sem, 0);
623 + sema_init(&alsa_stream->control_sem, 0);
624 + spin_lock_init(&alsa_stream->lock);
626 + /* Enabled in start trigger, called on each "fifo irq" after that */
627 + alsa_stream->enable_fifo_irq = 0;
628 + alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
630 + err = bcm2835_audio_open(alsa_stream);
632 + kfree(alsa_stream);
635 + runtime->private_data = alsa_stream;
636 + runtime->private_free = snd_bcm2835_playback_free;
638 + runtime->hw = snd_bcm2835_playback_spdif_hw;
640 + /* clear spdif status, as we are not in spdif mode */
641 + chip->spdif_status = 0;
642 + runtime->hw = snd_bcm2835_playback_hw;
644 + /* minimum 16 bytes alignment (for vchiq bulk transfers) */
645 + snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
648 + chip->alsa_stream[idx] = alsa_stream;
650 + chip->opened |= (1 << idx);
651 + alsa_stream->open = 1;
652 + alsa_stream->draining = 1;
655 + mutex_unlock(&chip->audio_mutex);
657 + audio_info(" .. OUT =%d\n", err);
662 +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
664 + return snd_bcm2835_playback_open_generic(substream, 0);
667 +static int snd_bcm2835_playback_spdif_open(struct snd_pcm_substream *substream)
669 + return snd_bcm2835_playback_open_generic(substream, 1);
672 +/* close callback */
673 +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
675 + /* the hardware-specific codes will be here */
677 + bcm2835_chip_t *chip;
678 + struct snd_pcm_runtime *runtime;
679 + bcm2835_alsa_stream_t *alsa_stream;
681 + audio_info(" .. IN\n");
683 + chip = snd_pcm_substream_chip(substream);
684 + if(mutex_lock_interruptible(&chip->audio_mutex))
686 + audio_error("Interrupted whilst waiting for lock\n");
689 + runtime = substream->runtime;
690 + alsa_stream = runtime->private_data;
692 + audio_info("Alsa close\n");
695 + * Call stop if it's still running. This happens when app
696 + * is force killed and we don't get a stop trigger.
698 + if (alsa_stream->running) {
700 + err = bcm2835_audio_stop(alsa_stream);
701 + alsa_stream->running = 0;
703 + audio_error(" Failed to STOP alsa device\n");
706 + alsa_stream->period_size = 0;
707 + alsa_stream->buffer_size = 0;
709 + if (alsa_stream->open) {
710 + alsa_stream->open = 0;
711 + bcm2835_audio_close(alsa_stream);
713 + if (alsa_stream->chip)
714 + alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
716 + * Do not free up alsa_stream here, it will be freed up by
717 + * runtime->private_free callback we registered in *_open above
720 + chip->opened &= ~(1 << substream->number);
722 + mutex_unlock(&chip->audio_mutex);
723 + audio_info(" .. OUT\n");
728 +/* hw_params callback */
729 +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
730 + struct snd_pcm_hw_params *params)
732 + struct snd_pcm_runtime *runtime = substream->runtime;
733 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
736 + audio_info(" .. IN\n");
738 + err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
741 + (" pcm_lib_malloc failed to allocated pages for buffers\n");
745 + alsa_stream->channels = params_channels(params);
746 + alsa_stream->params_rate = params_rate(params);
747 + alsa_stream->pcm_format_width = snd_pcm_format_width(params_format (params));
748 + audio_info(" .. OUT\n");
753 +/* hw_free callback */
754 +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
756 + audio_info(" .. IN\n");
757 + return snd_pcm_lib_free_pages(substream);
760 +/* prepare callback */
761 +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
763 + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
764 + struct snd_pcm_runtime *runtime = substream->runtime;
765 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
769 + audio_info(" .. IN\n");
771 + if (mutex_lock_interruptible(&chip->audio_mutex))
774 + /* notify the vchiq that it should enter spdif passthrough mode by
775 + * setting channels=0 (see
776 + * https://github.com/raspberrypi/linux/issues/528) */
777 + if (chip->spdif_status & IEC958_AES0_NONAUDIO)
780 + channels = alsa_stream->channels;
782 + err = bcm2835_audio_set_params(alsa_stream, channels,
783 + alsa_stream->params_rate,
784 + alsa_stream->pcm_format_width);
786 + audio_error(" error setting hw params\n");
789 + bcm2835_audio_setup(alsa_stream);
791 + /* in preparation of the stream, set the controls (volume level) of the stream */
792 + bcm2835_audio_set_ctls(alsa_stream->chip);
795 + memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
797 + alsa_stream->pcm_indirect.hw_buffer_size =
798 + alsa_stream->pcm_indirect.sw_buffer_size =
799 + snd_pcm_lib_buffer_bytes(substream);
801 + alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
802 + alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
803 + alsa_stream->pos = 0;
805 + audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
806 + alsa_stream->buffer_size, alsa_stream->period_size,
807 + alsa_stream->pos, runtime->frame_bits);
809 + mutex_unlock(&chip->audio_mutex);
810 + audio_info(" .. OUT\n");
814 +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
815 + struct snd_pcm_indirect *rec, size_t bytes)
817 + struct snd_pcm_runtime *runtime = substream->runtime;
818 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
819 + void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
822 + err = bcm2835_audio_write(alsa_stream, bytes, src);
824 + audio_error(" Failed to transfer to alsa device (%d)\n", err);
828 +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
830 + struct snd_pcm_runtime *runtime = substream->runtime;
831 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
832 + struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
834 + pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
835 + snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
836 + snd_bcm2835_pcm_transfer);
840 +/* trigger callback */
841 +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
843 + struct snd_pcm_runtime *runtime = substream->runtime;
844 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
847 + audio_info(" .. IN\n");
850 + case SNDRV_PCM_TRIGGER_START:
851 + audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
852 + alsa_stream->running);
853 + if (!alsa_stream->running) {
854 + err = bcm2835_audio_start(alsa_stream);
856 + alsa_stream->pcm_indirect.hw_io =
857 + alsa_stream->pcm_indirect.hw_data =
858 + bytes_to_frames(runtime,
860 + substream->ops->ack(substream);
861 + alsa_stream->running = 1;
862 + alsa_stream->draining = 1;
864 + audio_error(" Failed to START alsa device (%d)\n", err);
868 + case SNDRV_PCM_TRIGGER_STOP:
870 + ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
871 + alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
872 + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
873 + audio_info("DRAINING\n");
874 + alsa_stream->draining = 1;
876 + audio_info("DROPPING\n");
877 + alsa_stream->draining = 0;
879 + if (alsa_stream->running) {
880 + err = bcm2835_audio_stop(alsa_stream);
882 + audio_error(" Failed to STOP alsa device (%d)\n", err);
883 + alsa_stream->running = 0;
890 + audio_info(" .. OUT\n");
894 +/* pointer callback */
895 +static snd_pcm_uframes_t
896 +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
898 + struct snd_pcm_runtime *runtime = substream->runtime;
899 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
901 + audio_info(" .. IN\n");
903 + audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
904 + frames_to_bytes(runtime, runtime->status->hw_ptr),
905 + frames_to_bytes(runtime, runtime->control->appl_ptr),
908 + audio_info(" .. OUT\n");
909 + return snd_pcm_indirect_playback_pointer(substream,
910 + &alsa_stream->pcm_indirect,
914 +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
915 + unsigned int cmd, void *arg)
917 + int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
918 + audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
919 + cmd, arg, arg ? *(unsigned *)arg : 0, ret);
924 +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
925 + .open = snd_bcm2835_playback_open,
926 + .close = snd_bcm2835_playback_close,
927 + .ioctl = snd_bcm2835_pcm_lib_ioctl,
928 + .hw_params = snd_bcm2835_pcm_hw_params,
929 + .hw_free = snd_bcm2835_pcm_hw_free,
930 + .prepare = snd_bcm2835_pcm_prepare,
931 + .trigger = snd_bcm2835_pcm_trigger,
932 + .pointer = snd_bcm2835_pcm_pointer,
933 + .ack = snd_bcm2835_pcm_ack,
936 +static struct snd_pcm_ops snd_bcm2835_playback_spdif_ops = {
937 + .open = snd_bcm2835_playback_spdif_open,
938 + .close = snd_bcm2835_playback_close,
939 + .ioctl = snd_bcm2835_pcm_lib_ioctl,
940 + .hw_params = snd_bcm2835_pcm_hw_params,
941 + .hw_free = snd_bcm2835_pcm_hw_free,
942 + .prepare = snd_bcm2835_pcm_prepare,
943 + .trigger = snd_bcm2835_pcm_trigger,
944 + .pointer = snd_bcm2835_pcm_pointer,
945 + .ack = snd_bcm2835_pcm_ack,
948 +/* create a pcm device */
949 +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
951 + struct snd_pcm *pcm;
954 + audio_info(" .. IN\n");
955 + mutex_init(&chip->audio_mutex);
956 + if(mutex_lock_interruptible(&chip->audio_mutex))
958 + audio_error("Interrupted whilst waiting for lock\n");
962 + snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
965 + pcm->private_data = chip;
966 + strcpy(pcm->name, "bcm2835 ALSA");
968 + chip->dest = AUDIO_DEST_AUTO;
969 + chip->volume = alsa2chip(0);
970 + chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
971 + /* set operators */
972 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
973 + &snd_bcm2835_playback_ops);
975 + /* pre-allocation of buffers */
976 + /* NOTE: this may fail */
977 + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
978 + snd_dma_continuous_data (GFP_KERNEL),
979 + snd_bcm2835_playback_hw.buffer_bytes_max, snd_bcm2835_playback_hw.buffer_bytes_max);
983 + mutex_unlock(&chip->audio_mutex);
984 + audio_info(" .. OUT\n");
989 +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip)
991 + struct snd_pcm *pcm;
994 + audio_info(" .. IN\n");
995 + if(mutex_lock_interruptible(&chip->audio_mutex))
997 + audio_error("Interrupted whilst waiting for lock\n");
1000 + err = snd_pcm_new(chip->card, "bcm2835 ALSA", 1, 1, 0, &pcm);
1004 + pcm->private_data = chip;
1005 + strcpy(pcm->name, "bcm2835 IEC958/HDMI");
1006 + chip->pcm_spdif = pcm;
1007 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1008 + &snd_bcm2835_playback_spdif_ops);
1010 + /* pre-allocation of buffers */
1011 + /* NOTE: this may fail */
1012 + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1013 + snd_dma_continuous_data (GFP_KERNEL),
1014 + snd_bcm2835_playback_spdif_hw.buffer_bytes_max, snd_bcm2835_playback_spdif_hw.buffer_bytes_max);
1016 + mutex_unlock(&chip->audio_mutex);
1017 + audio_info(" .. OUT\n");
1022 +++ b/sound/arm/bcm2835-vchiq.c
1024 +/*****************************************************************************
1025 +* Copyright 2011 Broadcom Corporation. All rights reserved.
1027 +* Unless you and Broadcom execute a separate written software license
1028 +* agreement governing use of this software, this software is licensed to you
1029 +* under the terms of the GNU General Public License version 2, available at
1030 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1032 +* Notwithstanding the above, under no circumstances may you combine this
1033 +* software in any way with any other Broadcom software provided under a
1034 +* license other than the GPL, without Broadcom's express prior written
1036 +*****************************************************************************/
1038 +#include <linux/device.h>
1039 +#include <sound/core.h>
1040 +#include <sound/initval.h>
1041 +#include <sound/pcm.h>
1042 +#include <linux/io.h>
1043 +#include <linux/interrupt.h>
1044 +#include <linux/fs.h>
1045 +#include <linux/file.h>
1046 +#include <linux/mm.h>
1047 +#include <linux/syscalls.h>
1048 +#include <asm/uaccess.h>
1049 +#include <linux/slab.h>
1050 +#include <linux/delay.h>
1051 +#include <linux/atomic.h>
1052 +#include <linux/module.h>
1053 +#include <linux/completion.h>
1055 +#include "bcm2835.h"
1057 +/* ---- Include Files -------------------------------------------------------- */
1059 +#include "interface/vchi/vchi.h"
1060 +#include "vc_vchi_audioserv_defs.h"
1062 +/* ---- Private Constants and Types ------------------------------------------ */
1064 +#define BCM2835_AUDIO_STOP 0
1065 +#define BCM2835_AUDIO_START 1
1066 +#define BCM2835_AUDIO_WRITE 2
1068 +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
1069 +#ifdef AUDIO_DEBUG_ENABLE
1070 + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
1071 + #define LOG_WARN( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1072 + #define LOG_INFO( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1073 + #define LOG_DBG( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1075 + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
1076 + #define LOG_WARN( fmt, arg... )
1077 + #define LOG_INFO( fmt, arg... )
1078 + #define LOG_DBG( fmt, arg... )
1081 +typedef struct opaque_AUDIO_INSTANCE_T {
1082 + uint32_t num_connections;
1083 + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
1084 + struct completion msg_avail_comp;
1085 + struct mutex vchi_mutex;
1086 + bcm2835_alsa_stream_t *alsa_stream;
1088 + short peer_version;
1089 +} AUDIO_INSTANCE_T;
1091 +bool force_bulk = false;
1093 +/* ---- Private Variables ---------------------------------------------------- */
1095 +/* ---- Private Function Prototypes ------------------------------------------ */
1097 +/* ---- Private Functions ---------------------------------------------------- */
1099 +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
1100 +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
1101 +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
1102 + uint32_t count, void *src);
1105 + struct work_struct my_work;
1106 + bcm2835_alsa_stream_t *alsa_stream;
1112 +static void my_wq_function(struct work_struct *work)
1114 + my_work_t *w = (my_work_t *) work;
1116 + LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
1118 + case BCM2835_AUDIO_START:
1119 + ret = bcm2835_audio_start_worker(w->alsa_stream);
1121 + case BCM2835_AUDIO_STOP:
1122 + ret = bcm2835_audio_stop_worker(w->alsa_stream);
1124 + case BCM2835_AUDIO_WRITE:
1125 + ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
1129 + LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
1132 + kfree((void *)work);
1133 + LOG_DBG(" .. OUT %d\n", ret);
1136 +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
1139 + LOG_DBG(" .. IN\n");
1140 + if (alsa_stream->my_wq) {
1141 + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1142 + /*--- Queue some work (item 1) ---*/
1144 + INIT_WORK((struct work_struct *)work, my_wq_function);
1145 + work->alsa_stream = alsa_stream;
1146 + work->cmd = BCM2835_AUDIO_START;
1148 + (alsa_stream->my_wq, (struct work_struct *)work))
1151 + LOG_ERR(" .. Error: NULL work kmalloc\n");
1153 + LOG_DBG(" .. OUT %d\n", ret);
1157 +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
1160 + LOG_DBG(" .. IN\n");
1161 + if (alsa_stream->my_wq) {
1162 + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1163 + /*--- Queue some work (item 1) ---*/
1165 + INIT_WORK((struct work_struct *)work, my_wq_function);
1166 + work->alsa_stream = alsa_stream;
1167 + work->cmd = BCM2835_AUDIO_STOP;
1169 + (alsa_stream->my_wq, (struct work_struct *)work))
1172 + LOG_ERR(" .. Error: NULL work kmalloc\n");
1174 + LOG_DBG(" .. OUT %d\n", ret);
1178 +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
1179 + uint32_t count, void *src)
1182 + LOG_DBG(" .. IN\n");
1183 + if (alsa_stream->my_wq) {
1184 + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1185 + /*--- Queue some work (item 1) ---*/
1187 + INIT_WORK((struct work_struct *)work, my_wq_function);
1188 + work->alsa_stream = alsa_stream;
1189 + work->cmd = BCM2835_AUDIO_WRITE;
1191 + work->count = count;
1193 + (alsa_stream->my_wq, (struct work_struct *)work))
1196 + LOG_ERR(" .. Error: NULL work kmalloc\n");
1198 + LOG_DBG(" .. OUT %d\n", ret);
1202 +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
1204 + alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
1208 +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
1210 + if (alsa_stream->my_wq) {
1211 + flush_workqueue(alsa_stream->my_wq);
1212 + destroy_workqueue(alsa_stream->my_wq);
1213 + alsa_stream->my_wq = NULL;
1218 +static void audio_vchi_callback(void *param,
1219 + const VCHI_CALLBACK_REASON_T reason,
1222 + AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
1226 + LOG_DBG(" .. IN instance=%p, handle=%p, alsa=%p, reason=%d, handle=%p\n",
1227 + instance, instance ? instance->vchi_handle[0] : NULL, instance ? instance->alsa_stream : NULL, reason, msg_handle);
1229 + if (reason != VCHI_CALLBACK_MSG_AVAILABLE) {
1233 + LOG_ERR(" .. instance is null\n");
1237 + if (!instance->vchi_handle[0]) {
1238 + LOG_ERR(" .. instance->vchi_handle[0] is null\n");
1242 + status = vchi_msg_dequeue(instance->vchi_handle[0],
1243 + &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
1244 + if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
1246 + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
1247 + instance, m.u.result.success);
1248 + instance->result = m.u.result.success;
1249 + complete(&instance->msg_avail_comp);
1250 + } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
1251 + bcm2835_alsa_stream_t *alsa_stream = instance->alsa_stream;
1252 + irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
1254 + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
1255 + instance, m.u.complete.count);
1256 + if (alsa_stream && callback) {
1257 + atomic_add(m.u.complete.count, &alsa_stream->retrieved);
1258 + callback(0, alsa_stream);
1260 + LOG_ERR(" .. unexpected alsa_stream=%p, callback=%p\n",
1261 + alsa_stream, callback);
1264 + LOG_ERR(" .. unexpected m.type=%d\n", m.type);
1266 + LOG_DBG(" .. OUT\n");
1269 +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
1270 + VCHI_CONNECTION_T **
1272 + uint32_t num_connections)
1275 + AUDIO_INSTANCE_T *instance;
1278 + LOG_DBG("%s: start", __func__);
1280 + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
1281 + LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
1282 + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
1286 + /* Allocate memory for this instance */
1287 + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
1291 + memset(instance, 0, sizeof(*instance));
1292 + instance->num_connections = num_connections;
1294 + /* Create a lock for exclusive, serialized VCHI connection access */
1295 + mutex_init(&instance->vchi_mutex);
1296 + /* Open the VCHI service connections */
1297 + for (i = 0; i < num_connections; i++) {
1298 + SERVICE_CREATION_T params = {
1299 + VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
1300 + VC_AUDIO_SERVER_NAME, // 4cc service code
1301 + vchi_connections[i], // passed in fn pointers
1302 + 0, // rx fifo size (unused)
1303 + 0, // tx fifo size (unused)
1304 + audio_vchi_callback, // service callback
1305 + instance, // service callback parameter
1306 + 1, //TODO: remove VCOS_FALSE, // unaligned bulk recieves
1307 + 1, //TODO: remove VCOS_FALSE, // unaligned bulk transmits
1308 + 0 // want crc check on bulk transfers
1311 + LOG_DBG("%s: about to open %i\n", __func__, i);
1312 + status = vchi_service_open(vchi_instance, ¶ms,
1313 + &instance->vchi_handle[i]);
1314 + LOG_DBG("%s: opened %i: %p=%d\n", __func__, i, instance->vchi_handle[i], status);
1317 + ("%s: failed to open VCHI service connection (status=%d)\n",
1318 + __func__, status);
1320 + goto err_close_services;
1322 + /* Finished with the service for now */
1323 + vchi_service_release(instance->vchi_handle[i]);
1326 + LOG_DBG("%s: okay\n", __func__);
1329 +err_close_services:
1330 + for (i = 0; i < instance->num_connections; i++) {
1331 + LOG_ERR("%s: closing %i: %p\n", __func__, i, instance->vchi_handle[i]);
1332 + if (instance->vchi_handle[i])
1333 + vchi_service_close(instance->vchi_handle[i]);
1337 + LOG_ERR("%s: error\n", __func__);
1342 +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
1346 + LOG_DBG(" .. IN\n");
1348 + if (instance == NULL) {
1349 + LOG_ERR("%s: invalid handle %p\n", __func__, instance);
1354 + LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
1355 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1357 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1361 + /* Close all VCHI service connections */
1362 + for (i = 0; i < instance->num_connections; i++) {
1364 + LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
1365 + vchi_service_use(instance->vchi_handle[i]);
1367 + success = vchi_service_close(instance->vchi_handle[i]);
1368 + if (success != 0) {
1370 + ("%s: failed to close VCHI service connection (status=%d)\n",
1371 + __func__, success);
1375 + mutex_unlock(&instance->vchi_mutex);
1379 + LOG_DBG(" .. OUT\n");
1384 +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
1386 + static VCHI_INSTANCE_T vchi_instance;
1387 + static VCHI_CONNECTION_T *vchi_connection;
1388 + static int initted;
1389 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1391 + LOG_DBG(" .. IN\n");
1393 + LOG_INFO("%s: start\n", __func__);
1396 + LOG_ERR("%s: VCHI instance already open (%p)\n",
1397 + __func__, instance);
1398 + instance->alsa_stream = alsa_stream;
1399 + alsa_stream->instance = instance;
1400 + ret = 0; // xxx todo -1;
1401 + goto err_free_mem;
1404 + /* Initialize and create a VCHI connection */
1406 + ret = vchi_initialise(&vchi_instance);
1408 + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
1412 + goto err_free_mem;
1414 + ret = vchi_connect(NULL, 0, vchi_instance);
1416 + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
1420 + goto err_free_mem;
1425 + /* Initialize an instance of the audio service */
1426 + instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
1428 + if (instance == NULL) {
1429 + LOG_ERR("%s: failed to initialize audio service\n", __func__);
1432 + goto err_free_mem;
1435 + instance->alsa_stream = alsa_stream;
1436 + alsa_stream->instance = instance;
1438 + LOG_DBG(" success !\n");
1440 + LOG_DBG(" .. OUT\n");
1445 +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
1447 + AUDIO_INSTANCE_T *instance;
1451 + LOG_DBG(" .. IN\n");
1453 + my_workqueue_init(alsa_stream);
1455 + ret = bcm2835_audio_open_connection(alsa_stream);
1460 + instance = alsa_stream->instance;
1461 + LOG_DBG(" instance (%p)\n", instance);
1463 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1465 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1468 + vchi_service_use(instance->vchi_handle[0]);
1470 + m.type = VC_AUDIO_MSG_TYPE_OPEN;
1472 + /* Send the message to the videocore */
1473 + success = vchi_msg_queue(instance->vchi_handle[0],
1475 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1477 + if (success != 0) {
1478 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1479 + __func__, success);
1488 + vchi_service_release(instance->vchi_handle[0]);
1489 + mutex_unlock(&instance->vchi_mutex);
1491 + LOG_DBG(" .. OUT\n");
1495 +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
1496 + bcm2835_chip_t * chip)
1499 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1502 + LOG_DBG(" .. IN\n");
1505 + (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
1507 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1509 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1512 + vchi_service_use(instance->vchi_handle[0]);
1514 + instance->result = -1;
1516 + m.type = VC_AUDIO_MSG_TYPE_CONTROL;
1517 + m.u.control.dest = chip->dest;
1518 + m.u.control.volume = chip->volume;
1520 + /* Create the message available completion */
1521 + init_completion(&instance->msg_avail_comp);
1523 + /* Send the message to the videocore */
1524 + success = vchi_msg_queue(instance->vchi_handle[0],
1526 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1528 + if (success != 0) {
1529 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1530 + __func__, success);
1536 + /* We are expecting a reply from the videocore */
1537 + wait_for_completion(&instance->msg_avail_comp);
1539 + if (instance->result != 0) {
1540 + LOG_ERR("%s: result=%d\n", __func__, instance->result);
1549 + vchi_service_release(instance->vchi_handle[0]);
1550 + mutex_unlock(&instance->vchi_mutex);
1552 + LOG_DBG(" .. OUT\n");
1556 +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
1560 + LOG_DBG(" .. IN\n");
1561 + LOG_DBG(" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
1563 + /* change ctls for all substreams */
1564 + for (i = 0; i < MAX_SUBSTREAMS; i++) {
1565 + if (chip->avail_substreams & (1 << i)) {
1566 + if (!chip->alsa_stream[i])
1568 + LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
1571 + else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
1572 + (chip->alsa_stream[i], chip) != 0)
1574 + LOG_ERR("Couldn't set the controls for stream %d\n", i);
1577 + else LOG_DBG(" Controls set for stream %d\n", i);
1580 + LOG_DBG(" .. OUT ret=%d\n", ret);
1584 +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
1585 + uint32_t channels, uint32_t samplerate,
1589 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1592 + LOG_DBG(" .. IN\n");
1595 + (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
1596 + channels, samplerate, bps);
1598 + /* resend ctls - alsa_stream may not have been open when first send */
1599 + ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
1601 + LOG_ERR(" Alsa controls not supported\n");
1605 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1607 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1610 + vchi_service_use(instance->vchi_handle[0]);
1612 + instance->result = -1;
1614 + m.type = VC_AUDIO_MSG_TYPE_CONFIG;
1615 + m.u.config.channels = channels;
1616 + m.u.config.samplerate = samplerate;
1617 + m.u.config.bps = bps;
1619 + /* Create the message available completion */
1620 + init_completion(&instance->msg_avail_comp);
1622 + /* Send the message to the videocore */
1623 + success = vchi_msg_queue(instance->vchi_handle[0],
1625 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1627 + if (success != 0) {
1628 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1629 + __func__, success);
1635 + /* We are expecting a reply from the videocore */
1636 + wait_for_completion(&instance->msg_avail_comp);
1638 + if (instance->result != 0) {
1639 + LOG_ERR("%s: result=%d", __func__, instance->result);
1648 + vchi_service_release(instance->vchi_handle[0]);
1649 + mutex_unlock(&instance->vchi_mutex);
1651 + LOG_DBG(" .. OUT\n");
1655 +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
1657 + LOG_DBG(" .. IN\n");
1659 + LOG_DBG(" .. OUT\n");
1664 +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
1667 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1670 + LOG_DBG(" .. IN\n");
1672 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1674 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1677 + vchi_service_use(instance->vchi_handle[0]);
1679 + m.type = VC_AUDIO_MSG_TYPE_START;
1681 + /* Send the message to the videocore */
1682 + success = vchi_msg_queue(instance->vchi_handle[0],
1684 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1686 + if (success != 0) {
1687 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1688 + __func__, success);
1697 + vchi_service_release(instance->vchi_handle[0]);
1698 + mutex_unlock(&instance->vchi_mutex);
1699 + LOG_DBG(" .. OUT\n");
1703 +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
1706 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1709 + LOG_DBG(" .. IN\n");
1711 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1713 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1716 + vchi_service_use(instance->vchi_handle[0]);
1718 + m.type = VC_AUDIO_MSG_TYPE_STOP;
1719 + m.u.stop.draining = alsa_stream->draining;
1721 + /* Send the message to the videocore */
1722 + success = vchi_msg_queue(instance->vchi_handle[0],
1724 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1726 + if (success != 0) {
1727 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1728 + __func__, success);
1737 + vchi_service_release(instance->vchi_handle[0]);
1738 + mutex_unlock(&instance->vchi_mutex);
1739 + LOG_DBG(" .. OUT\n");
1743 +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
1746 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1749 + LOG_DBG(" .. IN\n");
1751 + my_workqueue_quit(alsa_stream);
1753 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1755 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1758 + vchi_service_use(instance->vchi_handle[0]);
1760 + m.type = VC_AUDIO_MSG_TYPE_CLOSE;
1762 + /* Create the message available completion */
1763 + init_completion(&instance->msg_avail_comp);
1765 + /* Send the message to the videocore */
1766 + success = vchi_msg_queue(instance->vchi_handle[0],
1768 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1770 + if (success != 0) {
1771 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1772 + __func__, success);
1777 + /* We are expecting a reply from the videocore */
1778 + wait_for_completion(&instance->msg_avail_comp);
1780 + if (instance->result != 0) {
1781 + LOG_ERR("%s: failed result (result=%d)\n",
1782 + __func__, instance->result);
1791 + vchi_service_release(instance->vchi_handle[0]);
1792 + mutex_unlock(&instance->vchi_mutex);
1794 + /* Stop the audio service */
1796 + vc_vchi_audio_deinit(instance);
1797 + alsa_stream->instance = NULL;
1799 + LOG_DBG(" .. OUT\n");
1803 +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
1804 + uint32_t count, void *src)
1807 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1811 + LOG_DBG(" .. IN\n");
1813 + LOG_INFO(" Writing %d bytes from %p\n", count, src);
1815 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1817 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1820 + vchi_service_use(instance->vchi_handle[0]);
1822 + if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
1823 + LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
1825 + m.type = VC_AUDIO_MSG_TYPE_WRITE;
1826 + m.u.write.count = count;
1827 + // old version uses bulk, new version uses control
1828 + m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
1829 + m.u.write.callback = alsa_stream->fifo_irq_handler;
1830 + m.u.write.cookie = alsa_stream;
1831 + m.u.write.silence = src == NULL;
1833 + /* Send the message to the videocore */
1834 + success = vchi_msg_queue(instance->vchi_handle[0],
1836 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1838 + if (success != 0) {
1839 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1840 + __func__, success);
1845 + if (!m.u.write.silence) {
1846 + if (m.u.write.max_packet == 0) {
1847 + /* Send the message to the videocore */
1848 + success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
1851 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED
1854 + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
1857 + while (count > 0) {
1858 + int bytes = min((int)m.u.write.max_packet, (int)count);
1859 + success = vchi_msg_queue(instance->vchi_handle[0],
1861 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1862 + src = (char *)src + bytes;
1866 + if (success != 0) {
1868 + ("%s: failed on vchi_bulk_queue_transmit (status=%d)\n",
1869 + __func__, success);
1878 + vchi_service_release(instance->vchi_handle[0]);
1879 + mutex_unlock(&instance->vchi_mutex);
1880 + LOG_DBG(" .. OUT\n");
1885 + * Returns all buffers from arm->vc
1887 +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
1889 + LOG_DBG(" .. IN\n");
1890 + LOG_DBG(" .. OUT\n");
1895 + * Forces VC to flush(drop) its filled playback buffers and
1896 + * return them the us. (VC->ARM)
1898 +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
1900 + LOG_DBG(" .. IN\n");
1901 + LOG_DBG(" .. OUT\n");
1904 +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
1906 + uint32_t count = atomic_read(&alsa_stream->retrieved);
1907 + atomic_sub(count, &alsa_stream->retrieved);
1911 +module_param(force_bulk, bool, 0444);
1912 +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
1914 +++ b/sound/arm/bcm2835.c
1916 +/*****************************************************************************
1917 +* Copyright 2011 Broadcom Corporation. All rights reserved.
1919 +* Unless you and Broadcom execute a separate written software license
1920 +* agreement governing use of this software, this software is licensed to you
1921 +* under the terms of the GNU General Public License version 2, available at
1922 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1924 +* Notwithstanding the above, under no circumstances may you combine this
1925 +* software in any way with any other Broadcom software provided under a
1926 +* license other than the GPL, without Broadcom's express prior written
1928 +*****************************************************************************/
1930 +#include <linux/platform_device.h>
1932 +#include <linux/init.h>
1933 +#include <linux/slab.h>
1934 +#include <linux/module.h>
1935 +#include <linux/of.h>
1937 +#include "bcm2835.h"
1939 +/* module parameters (see "Module Parameters") */
1940 +/* SNDRV_CARDS: maximum number of cards supported by this module */
1941 +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
1942 +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
1943 +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
1945 +/* HACKY global pointers needed for successive probes to work : ssp
1946 + * But compared against the changes we will have to do in VC audio_ipc code
1947 + * to export 8 audio_ipc devices as a single IPC device and then monitor all
1948 + * four devices in a thread, this gets things done quickly and should be easier
1949 + * to debug if we run into issues
1952 +static struct snd_card *g_card = NULL;
1953 +static bcm2835_chip_t *g_chip = NULL;
1955 +static int snd_bcm2835_free(bcm2835_chip_t * chip)
1961 +/* component-destructor
1962 + * (see "Management of Cards and Components")
1964 +static int snd_bcm2835_dev_free(struct snd_device *device)
1966 + return snd_bcm2835_free(device->device_data);
1969 +/* chip-specific constructor
1970 + * (see "Management of Cards and Components")
1972 +static int snd_bcm2835_create(struct snd_card *card,
1973 + struct platform_device *pdev,
1974 + bcm2835_chip_t ** rchip)
1976 + bcm2835_chip_t *chip;
1978 + static struct snd_device_ops ops = {
1979 + .dev_free = snd_bcm2835_dev_free,
1984 + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1988 + chip->card = card;
1990 + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1992 + snd_bcm2835_free(chip);
2000 +static int snd_bcm2835_alsa_probe_dt(struct platform_device *pdev)
2002 + struct device *dev = &pdev->dev;
2003 + bcm2835_chip_t *chip;
2004 + struct snd_card *card;
2008 + err = of_property_read_u32(dev->of_node, "brcm,pwm-channels",
2011 + dev_err(dev, "Failed to get DT property 'brcm,pwm-channels'");
2015 + if (numchans == 0 || numchans > MAX_SUBSTREAMS) {
2016 + numchans = MAX_SUBSTREAMS;
2017 + dev_warn(dev, "Illegal 'brcm,pwm-channels' value, will use %u\n",
2021 + err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
2023 + dev_err(dev, "Failed to create soundcard structure\n");
2027 + snd_card_set_dev(card, dev);
2028 + strcpy(card->driver, "bcm2835");
2029 + strcpy(card->shortname, "bcm2835 ALSA");
2030 + sprintf(card->longname, "%s", card->shortname);
2032 + err = snd_bcm2835_create(card, pdev, &chip);
2034 + dev_err(dev, "Failed to create bcm2835 chip\n");
2038 + err = snd_bcm2835_new_pcm(chip);
2040 + dev_err(dev, "Failed to create new bcm2835 pcm device\n");
2044 + err = snd_bcm2835_new_spdif_pcm(chip);
2046 + dev_err(dev, "Failed to create new bcm2835 spdif pcm device\n");
2050 + err = snd_bcm2835_new_ctl(chip);
2052 + dev_err(dev, "Failed to create new bcm2835 ctl\n");
2056 + for (i = 0; i < numchans; i++) {
2057 + chip->avail_substreams |= (1 << i);
2058 + chip->pdev[i] = pdev;
2061 + err = snd_card_register(card);
2063 + dev_err(dev, "Failed to register bcm2835 ALSA card \n");
2069 + platform_set_drvdata(pdev, card);
2070 + audio_info("bcm2835 ALSA card created with %u channels\n", numchans);
2075 + snd_card_free(card);
2080 +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
2083 + bcm2835_chip_t *chip;
2084 + struct snd_card *card;
2087 + if (pdev->dev.of_node)
2088 + return snd_bcm2835_alsa_probe_dt(pdev);
2090 + if (dev >= MAX_SUBSTREAMS)
2093 + if (!enable[dev]) {
2099 + goto add_register_map;
2101 + err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE, 0, &g_card);
2105 + snd_card_set_dev(g_card, &pdev->dev);
2106 + strcpy(g_card->driver, "bcm2835");
2107 + strcpy(g_card->shortname, "bcm2835 ALSA");
2108 + sprintf(g_card->longname, "%s", g_card->shortname);
2110 + err = snd_bcm2835_create(g_card, pdev, &chip);
2112 + dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
2113 + goto out_bcm2835_create;
2117 + err = snd_bcm2835_new_pcm(chip);
2119 + dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
2120 + goto out_bcm2835_new_pcm;
2123 + err = snd_bcm2835_new_spdif_pcm(chip);
2125 + dev_err(&pdev->dev, "Failed to create new BCM2835 spdif pcm device\n");
2126 + goto out_bcm2835_new_spdif;
2129 + err = snd_bcm2835_new_ctl(chip);
2131 + dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
2132 + goto out_bcm2835_new_ctl;
2139 + BUG_ON(!(card && chip));
2141 + chip->avail_substreams |= (1 << dev);
2142 + chip->pdev[dev] = pdev;
2145 + err = snd_card_register(card);
2147 + dev_err(&pdev->dev,
2148 + "Failed to register bcm2835 ALSA card \n");
2149 + goto out_card_register;
2151 + platform_set_drvdata(pdev, card);
2152 + audio_info("bcm2835 ALSA card created!\n");
2154 + audio_info("bcm2835 ALSA chip created!\n");
2155 + platform_set_drvdata(pdev, (void *)dev);
2163 +out_bcm2835_new_ctl:
2164 +out_bcm2835_new_spdif:
2165 +out_bcm2835_new_pcm:
2166 +out_bcm2835_create:
2168 + if (snd_card_free(g_card))
2169 + dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
2172 + dev = SNDRV_CARDS; /* stop more avail_substreams from being probed */
2173 + dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
2177 +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
2182 + drv_data = platform_get_drvdata(pdev);
2184 + if (drv_data == (void *)g_card) {
2185 + /* This is the card device */
2186 + snd_card_free((struct snd_card *)drv_data);
2190 + idx = (uint32_t) drv_data;
2191 + if (g_card != NULL) {
2193 + /* We pass chip device numbers in audio ipc devices
2194 + * other than the one we registered our card with
2196 + idx = (uint32_t) drv_data;
2197 + BUG_ON(!idx || idx > MAX_SUBSTREAMS);
2198 + g_chip->avail_substreams &= ~(1 << idx);
2199 + /* There should be atleast one substream registered
2200 + * after we are done here, as it wil be removed when
2201 + * the *remove* is called for the card device
2203 + BUG_ON(!g_chip->avail_substreams);
2207 + platform_set_drvdata(pdev, NULL);
2213 +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
2214 + pm_message_t state)
2219 +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
2226 +static const struct of_device_id snd_bcm2835_of_match_table[] = {
2227 + { .compatible = "brcm,bcm2835-audio", },
2230 +MODULE_DEVICE_TABLE(of, snd_bcm2835_of_match_table);
2232 +static struct platform_driver bcm2835_alsa0_driver = {
2233 + .probe = snd_bcm2835_alsa_probe,
2234 + .remove = snd_bcm2835_alsa_remove,
2236 + .suspend = snd_bcm2835_alsa_suspend,
2237 + .resume = snd_bcm2835_alsa_resume,
2240 + .name = "bcm2835_AUD0",
2241 + .owner = THIS_MODULE,
2242 + .of_match_table = snd_bcm2835_of_match_table,
2246 +static struct platform_driver bcm2835_alsa1_driver = {
2247 + .probe = snd_bcm2835_alsa_probe,
2248 + .remove = snd_bcm2835_alsa_remove,
2250 + .suspend = snd_bcm2835_alsa_suspend,
2251 + .resume = snd_bcm2835_alsa_resume,
2254 + .name = "bcm2835_AUD1",
2255 + .owner = THIS_MODULE,
2259 +static struct platform_driver bcm2835_alsa2_driver = {
2260 + .probe = snd_bcm2835_alsa_probe,
2261 + .remove = snd_bcm2835_alsa_remove,
2263 + .suspend = snd_bcm2835_alsa_suspend,
2264 + .resume = snd_bcm2835_alsa_resume,
2267 + .name = "bcm2835_AUD2",
2268 + .owner = THIS_MODULE,
2272 +static struct platform_driver bcm2835_alsa3_driver = {
2273 + .probe = snd_bcm2835_alsa_probe,
2274 + .remove = snd_bcm2835_alsa_remove,
2276 + .suspend = snd_bcm2835_alsa_suspend,
2277 + .resume = snd_bcm2835_alsa_resume,
2280 + .name = "bcm2835_AUD3",
2281 + .owner = THIS_MODULE,
2285 +static struct platform_driver bcm2835_alsa4_driver = {
2286 + .probe = snd_bcm2835_alsa_probe,
2287 + .remove = snd_bcm2835_alsa_remove,
2289 + .suspend = snd_bcm2835_alsa_suspend,
2290 + .resume = snd_bcm2835_alsa_resume,
2293 + .name = "bcm2835_AUD4",
2294 + .owner = THIS_MODULE,
2298 +static struct platform_driver bcm2835_alsa5_driver = {
2299 + .probe = snd_bcm2835_alsa_probe,
2300 + .remove = snd_bcm2835_alsa_remove,
2302 + .suspend = snd_bcm2835_alsa_suspend,
2303 + .resume = snd_bcm2835_alsa_resume,
2306 + .name = "bcm2835_AUD5",
2307 + .owner = THIS_MODULE,
2311 +static struct platform_driver bcm2835_alsa6_driver = {
2312 + .probe = snd_bcm2835_alsa_probe,
2313 + .remove = snd_bcm2835_alsa_remove,
2315 + .suspend = snd_bcm2835_alsa_suspend,
2316 + .resume = snd_bcm2835_alsa_resume,
2319 + .name = "bcm2835_AUD6",
2320 + .owner = THIS_MODULE,
2324 +static struct platform_driver bcm2835_alsa7_driver = {
2325 + .probe = snd_bcm2835_alsa_probe,
2326 + .remove = snd_bcm2835_alsa_remove,
2328 + .suspend = snd_bcm2835_alsa_suspend,
2329 + .resume = snd_bcm2835_alsa_resume,
2332 + .name = "bcm2835_AUD7",
2333 + .owner = THIS_MODULE,
2337 +static int bcm2835_alsa_device_init(void)
2340 + err = platform_driver_register(&bcm2835_alsa0_driver);
2342 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2346 + err = platform_driver_register(&bcm2835_alsa1_driver);
2348 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2349 + goto unregister_0;
2352 + err = platform_driver_register(&bcm2835_alsa2_driver);
2354 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2355 + goto unregister_1;
2358 + err = platform_driver_register(&bcm2835_alsa3_driver);
2360 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2361 + goto unregister_2;
2364 + err = platform_driver_register(&bcm2835_alsa4_driver);
2366 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2367 + goto unregister_3;
2370 + err = platform_driver_register(&bcm2835_alsa5_driver);
2372 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2373 + goto unregister_4;
2376 + err = platform_driver_register(&bcm2835_alsa6_driver);
2378 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2379 + goto unregister_5;
2382 + err = platform_driver_register(&bcm2835_alsa7_driver);
2384 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2385 + goto unregister_6;
2391 + platform_driver_unregister(&bcm2835_alsa6_driver);
2393 + platform_driver_unregister(&bcm2835_alsa5_driver);
2395 + platform_driver_unregister(&bcm2835_alsa4_driver);
2397 + platform_driver_unregister(&bcm2835_alsa3_driver);
2399 + platform_driver_unregister(&bcm2835_alsa2_driver);
2401 + platform_driver_unregister(&bcm2835_alsa1_driver);
2403 + platform_driver_unregister(&bcm2835_alsa0_driver);
2408 +static void bcm2835_alsa_device_exit(void)
2410 + platform_driver_unregister(&bcm2835_alsa0_driver);
2411 + platform_driver_unregister(&bcm2835_alsa1_driver);
2412 + platform_driver_unregister(&bcm2835_alsa2_driver);
2413 + platform_driver_unregister(&bcm2835_alsa3_driver);
2414 + platform_driver_unregister(&bcm2835_alsa4_driver);
2415 + platform_driver_unregister(&bcm2835_alsa5_driver);
2416 + platform_driver_unregister(&bcm2835_alsa6_driver);
2417 + platform_driver_unregister(&bcm2835_alsa7_driver);
2420 +late_initcall(bcm2835_alsa_device_init);
2421 +module_exit(bcm2835_alsa_device_exit);
2423 +MODULE_AUTHOR("Dom Cobley");
2424 +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
2425 +MODULE_LICENSE("GPL");
2426 +MODULE_ALIAS("platform:bcm2835_alsa");
2428 +++ b/sound/arm/bcm2835.h
2430 +/*****************************************************************************
2431 +* Copyright 2011 Broadcom Corporation. All rights reserved.
2433 +* Unless you and Broadcom execute a separate written software license
2434 +* agreement governing use of this software, this software is licensed to you
2435 +* under the terms of the GNU General Public License version 2, available at
2436 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
2438 +* Notwithstanding the above, under no circumstances may you combine this
2439 +* software in any way with any other Broadcom software provided under a
2440 +* license other than the GPL, without Broadcom's express prior written
2442 +*****************************************************************************/
2444 +#ifndef __SOUND_ARM_BCM2835_H
2445 +#define __SOUND_ARM_BCM2835_H
2447 +#include <linux/device.h>
2448 +#include <linux/list.h>
2449 +#include <linux/interrupt.h>
2450 +#include <linux/wait.h>
2451 +#include <sound/core.h>
2452 +#include <sound/initval.h>
2453 +#include <sound/pcm.h>
2454 +#include <sound/pcm_params.h>
2455 +#include <sound/pcm-indirect.h>
2456 +#include <linux/workqueue.h>
2459 +#define AUDIO_DEBUG_ENABLE
2460 +#define AUDIO_VERBOSE_DEBUG_ENABLE
2465 +#ifdef AUDIO_DEBUG_ENABLE
2466 +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
2468 +#define audio_debug(fmt, arg...) \
2469 + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
2471 +#define audio_info(fmt, arg...) \
2472 + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
2476 +#define audio_debug(fmt, arg...)
2478 +#define audio_info(fmt, arg...)
2480 +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
2484 +#define audio_debug(fmt, arg...)
2486 +#define audio_info(fmt, arg...)
2488 +#endif /* AUDIO_DEBUG_ENABLE */
2490 +#define audio_error(fmt, arg...) \
2491 + printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
2493 +#define audio_warning(fmt, arg...) \
2494 + printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
2496 +#define audio_alert(fmt, arg...) \
2497 + printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
2499 +#define MAX_SUBSTREAMS (8)
2500 +#define AVAIL_SUBSTREAMS_MASK (0xff)
2506 +/* macros for alsa2chip and chip2alsa, instead of functions */
2508 +#define alsa2chip(vol) (uint)(-((vol << 8) / 100)) /* convert alsa to chip volume (defined as macro rather than function call) */
2509 +#define chip2alsa(vol) -((vol * 100) >> 8) /* convert chip to alsa volume */
2511 +/* Some constants for values .. */
2513 + AUDIO_DEST_AUTO = 0,
2514 + AUDIO_DEST_HEADPHONES = 1,
2515 + AUDIO_DEST_HDMI = 2,
2517 +} SND_BCM2835_ROUTE_T;
2520 + PCM_PLAYBACK_VOLUME,
2521 + PCM_PLAYBACK_MUTE,
2522 + PCM_PLAYBACK_DEVICE,
2523 +} SND_BCM2835_CTRL_T;
2525 +/* definition of the chip-specific record */
2526 +typedef struct bcm2835_chip {
2527 + struct snd_card *card;
2528 + struct snd_pcm *pcm;
2529 + struct snd_pcm *pcm_spdif;
2530 + /* Bitmat for valid reg_base and irq numbers */
2531 + uint32_t avail_substreams;
2532 + struct platform_device *pdev[MAX_SUBSTREAMS];
2533 + struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
2536 + int old_volume; /* stores the volume value whist muted */
2540 + unsigned int opened;
2541 + unsigned int spdif_status;
2542 + struct mutex audio_mutex;
2545 +typedef struct bcm2835_alsa_stream {
2546 + bcm2835_chip_t *chip;
2547 + struct snd_pcm_substream *substream;
2548 + struct snd_pcm_indirect pcm_indirect;
2550 + struct semaphore buffers_update_sem;
2551 + struct semaphore control_sem;
2553 + volatile uint32_t control;
2554 + volatile uint32_t status;
2562 + int pcm_format_width;
2565 + unsigned int buffer_size;
2566 + unsigned int period_size;
2568 + uint32_t enable_fifo_irq;
2569 + irq_handler_t fifo_irq_handler;
2571 + atomic_t retrieved;
2572 + struct opaque_AUDIO_INSTANCE_T *instance;
2573 + struct workqueue_struct *my_wq;
2575 +} bcm2835_alsa_stream_t;
2577 +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
2578 +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
2579 +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip);
2581 +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
2582 +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
2583 +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
2584 + uint32_t channels, uint32_t samplerate,
2586 +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
2587 +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
2588 +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
2589 +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
2590 +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
2592 +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
2593 +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
2594 +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
2596 +#endif /* __SOUND_ARM_BCM2835_H */
2598 +++ b/sound/arm/vc_vchi_audioserv_defs.h
2600 +/*****************************************************************************
2601 +* Copyright 2011 Broadcom Corporation. All rights reserved.
2603 +* Unless you and Broadcom execute a separate written software license
2604 +* agreement governing use of this software, this software is licensed to you
2605 +* under the terms of the GNU General Public License version 2, available at
2606 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
2608 +* Notwithstanding the above, under no circumstances may you combine this
2609 +* software in any way with any other Broadcom software provided under a
2610 +* license other than the GPL, without Broadcom's express prior written
2612 +*****************************************************************************/
2614 +#ifndef _VC_AUDIO_DEFS_H_
2615 +#define _VC_AUDIO_DEFS_H_
2617 +#define VC_AUDIOSERV_MIN_VER 1
2618 +#define VC_AUDIOSERV_VER 2
2620 +// FourCC code used for VCHI connection
2621 +#define VC_AUDIO_SERVER_NAME MAKE_FOURCC("AUDS")
2623 +// Maximum message length
2624 +#define VC_AUDIO_MAX_MSG_LEN (sizeof( VC_AUDIO_MSG_T ))
2626 +// List of screens that are currently supported
2627 +// All message types supported for HOST->VC direction
2629 + VC_AUDIO_MSG_TYPE_RESULT, // Generic result
2630 + VC_AUDIO_MSG_TYPE_COMPLETE, // Generic result
2631 + VC_AUDIO_MSG_TYPE_CONFIG, // Configure audio
2632 + VC_AUDIO_MSG_TYPE_CONTROL, // Configure audio
2633 + VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
2634 + VC_AUDIO_MSG_TYPE_CLOSE, // Configure audio
2635 + VC_AUDIO_MSG_TYPE_START, // Configure audio
2636 + VC_AUDIO_MSG_TYPE_STOP, // Configure audio
2637 + VC_AUDIO_MSG_TYPE_WRITE, // Configure audio
2638 + VC_AUDIO_MSG_TYPE_MAX
2639 +} VC_AUDIO_MSG_TYPE;
2641 +// configure the audio
2643 + uint32_t channels;
2644 + uint32_t samplerate;
2647 +} VC_AUDIO_CONFIG_T;
2653 +} VC_AUDIO_CONTROL_T;
2665 +} VC_AUDIO_CLOSE_T;
2670 +} VC_AUDIO_START_T;
2673 + uint32_t draining;
2677 +// configure the write audio samples
2679 + uint32_t count; // in bytes
2683 + uint16_t max_packet;
2684 +} VC_AUDIO_WRITE_T;
2686 +// Generic result for a request (VC->HOST)
2688 + int32_t success; // Success value
2690 +} VC_AUDIO_RESULT_T;
2692 +// Generic result for a request (VC->HOST)
2694 + int32_t count; // Success value
2697 +} VC_AUDIO_COMPLETE_T;
2699 +// Message header for all messages in HOST->VC direction
2701 + int32_t type; // Message type (VC_AUDIO_MSG_TYPE)
2703 + VC_AUDIO_CONFIG_T config;
2704 + VC_AUDIO_CONTROL_T control;
2705 + VC_AUDIO_OPEN_T open;
2706 + VC_AUDIO_CLOSE_T close;
2707 + VC_AUDIO_START_T start;
2708 + VC_AUDIO_STOP_T stop;
2709 + VC_AUDIO_WRITE_T write;
2710 + VC_AUDIO_RESULT_T result;
2711 + VC_AUDIO_COMPLETE_T complete;
2715 +#endif // _VC_AUDIO_DEFS_H_