2 +++ b/sound/arm/bcm2835.c
4 +/*****************************************************************************
5 +* Copyright 2011 Broadcom Corporation. All rights reserved.
7 +* Unless you and Broadcom execute a separate written software license
8 +* agreement governing use of this software, this software is licensed to you
9 +* under the terms of the GNU General Public License version 2, available at
10 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
12 +* Notwithstanding the above, under no circumstances may you combine this
13 +* software in any way with any other Broadcom software provided under a
14 +* license other than the GPL, without Broadcom's express prior written
16 +*****************************************************************************/
18 +#include <linux/platform_device.h>
20 +#include <linux/init.h>
21 +#include <linux/slab.h>
22 +#include <linux/module.h>
26 +/* module parameters (see "Module Parameters") */
27 +/* SNDRV_CARDS: maximum number of cards supported by this module */
28 +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
29 +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
30 +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
32 +/* HACKY global pointers needed for successive probes to work : ssp
33 + * But compared against the changes we will have to do in VC audio_ipc code
34 + * to export 8 audio_ipc devices as a single IPC device and then monitor all
35 + * four devices in a thread, this gets things done quickly and should be easier
36 + * to debug if we run into issues
39 +static struct snd_card *g_card = NULL;
40 +static bcm2835_chip_t *g_chip = NULL;
42 +static int snd_bcm2835_free(bcm2835_chip_t * chip)
48 +/* component-destructor
49 + * (see "Management of Cards and Components")
51 +static int snd_bcm2835_dev_free(struct snd_device *device)
53 + return snd_bcm2835_free(device->device_data);
56 +/* chip-specific constructor
57 + * (see "Management of Cards and Components")
59 +static int snd_bcm2835_create(struct snd_card *card,
60 + struct platform_device *pdev,
61 + bcm2835_chip_t ** rchip)
63 + bcm2835_chip_t *chip;
65 + static struct snd_device_ops ops = {
66 + .dev_free = snd_bcm2835_dev_free,
71 + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
77 + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
79 + snd_bcm2835_free(chip);
87 +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
90 + bcm2835_chip_t *chip;
91 + struct snd_card *card;
94 + if (dev >= MAX_SUBSTREAMS)
103 + goto add_register_map;
105 + err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &g_card);
109 + snd_card_set_dev(g_card, &pdev->dev);
110 + strcpy(g_card->driver, "BRCM bcm2835 ALSA Driver");
111 + strcpy(g_card->shortname, "bcm2835 ALSA");
112 + sprintf(g_card->longname, "%s", g_card->shortname);
114 + err = snd_bcm2835_create(g_card, pdev, &chip);
116 + dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
117 + goto out_bcm2835_create;
121 + err = snd_bcm2835_new_pcm(chip);
123 + dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
124 + goto out_bcm2835_new_pcm;
127 + err = snd_bcm2835_new_ctl(chip);
129 + dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
130 + goto out_bcm2835_new_ctl;
137 + BUG_ON(!(card && chip));
139 + chip->avail_substreams |= (1 << dev);
140 + chip->pdev[dev] = pdev;
143 + err = snd_card_register(card);
145 + dev_err(&pdev->dev,
146 + "Failed to register bcm2835 ALSA card \n");
147 + goto out_card_register;
149 + platform_set_drvdata(pdev, card);
150 + audio_info("bcm2835 ALSA card created!\n");
152 + audio_info("bcm2835 ALSA chip created!\n");
153 + platform_set_drvdata(pdev, (void *)dev);
161 +out_bcm2835_new_ctl:
162 +out_bcm2835_new_pcm:
165 + if (snd_card_free(g_card))
166 + dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
169 + dev = SNDRV_CARDS; /* stop more avail_substreams from being probed */
170 + dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
174 +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
179 + drv_data = platform_get_drvdata(pdev);
181 + if (drv_data == (void *)g_card) {
182 + /* This is the card device */
183 + snd_card_free((struct snd_card *)drv_data);
187 + idx = (uint32_t) drv_data;
188 + if (g_card != NULL) {
190 + /* We pass chip device numbers in audio ipc devices
191 + * other than the one we registered our card with
193 + idx = (uint32_t) drv_data;
194 + BUG_ON(!idx || idx > MAX_SUBSTREAMS);
195 + g_chip->avail_substreams &= ~(1 << idx);
196 + /* There should be atleast one substream registered
197 + * after we are done here, as it wil be removed when
198 + * the *remove* is called for the card device
200 + BUG_ON(!g_chip->avail_substreams);
204 + platform_set_drvdata(pdev, NULL);
210 +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
211 + pm_message_t state)
216 +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
223 +static struct platform_driver bcm2835_alsa0_driver = {
224 + .probe = snd_bcm2835_alsa_probe,
225 + .remove = snd_bcm2835_alsa_remove,
227 + .suspend = snd_bcm2835_alsa_suspend,
228 + .resume = snd_bcm2835_alsa_resume,
231 + .name = "bcm2835_AUD0",
232 + .owner = THIS_MODULE,
236 +static struct platform_driver bcm2835_alsa1_driver = {
237 + .probe = snd_bcm2835_alsa_probe,
238 + .remove = snd_bcm2835_alsa_remove,
240 + .suspend = snd_bcm2835_alsa_suspend,
241 + .resume = snd_bcm2835_alsa_resume,
244 + .name = "bcm2835_AUD1",
245 + .owner = THIS_MODULE,
249 +static struct platform_driver bcm2835_alsa2_driver = {
250 + .probe = snd_bcm2835_alsa_probe,
251 + .remove = snd_bcm2835_alsa_remove,
253 + .suspend = snd_bcm2835_alsa_suspend,
254 + .resume = snd_bcm2835_alsa_resume,
257 + .name = "bcm2835_AUD2",
258 + .owner = THIS_MODULE,
262 +static struct platform_driver bcm2835_alsa3_driver = {
263 + .probe = snd_bcm2835_alsa_probe,
264 + .remove = snd_bcm2835_alsa_remove,
266 + .suspend = snd_bcm2835_alsa_suspend,
267 + .resume = snd_bcm2835_alsa_resume,
270 + .name = "bcm2835_AUD3",
271 + .owner = THIS_MODULE,
275 +static struct platform_driver bcm2835_alsa4_driver = {
276 + .probe = snd_bcm2835_alsa_probe,
277 + .remove = snd_bcm2835_alsa_remove,
279 + .suspend = snd_bcm2835_alsa_suspend,
280 + .resume = snd_bcm2835_alsa_resume,
283 + .name = "bcm2835_AUD4",
284 + .owner = THIS_MODULE,
288 +static struct platform_driver bcm2835_alsa5_driver = {
289 + .probe = snd_bcm2835_alsa_probe,
290 + .remove = snd_bcm2835_alsa_remove,
292 + .suspend = snd_bcm2835_alsa_suspend,
293 + .resume = snd_bcm2835_alsa_resume,
296 + .name = "bcm2835_AUD5",
297 + .owner = THIS_MODULE,
301 +static struct platform_driver bcm2835_alsa6_driver = {
302 + .probe = snd_bcm2835_alsa_probe,
303 + .remove = snd_bcm2835_alsa_remove,
305 + .suspend = snd_bcm2835_alsa_suspend,
306 + .resume = snd_bcm2835_alsa_resume,
309 + .name = "bcm2835_AUD6",
310 + .owner = THIS_MODULE,
314 +static struct platform_driver bcm2835_alsa7_driver = {
315 + .probe = snd_bcm2835_alsa_probe,
316 + .remove = snd_bcm2835_alsa_remove,
318 + .suspend = snd_bcm2835_alsa_suspend,
319 + .resume = snd_bcm2835_alsa_resume,
322 + .name = "bcm2835_AUD7",
323 + .owner = THIS_MODULE,
327 +static int bcm2835_alsa_device_init(void)
330 + err = platform_driver_register(&bcm2835_alsa0_driver);
332 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
336 + err = platform_driver_register(&bcm2835_alsa1_driver);
338 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
342 + err = platform_driver_register(&bcm2835_alsa2_driver);
344 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
348 + err = platform_driver_register(&bcm2835_alsa3_driver);
350 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
354 + err = platform_driver_register(&bcm2835_alsa4_driver);
356 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
360 + err = platform_driver_register(&bcm2835_alsa5_driver);
362 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
366 + err = platform_driver_register(&bcm2835_alsa6_driver);
368 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
372 + err = platform_driver_register(&bcm2835_alsa7_driver);
374 + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
381 + platform_driver_unregister(&bcm2835_alsa6_driver);
383 + platform_driver_unregister(&bcm2835_alsa5_driver);
385 + platform_driver_unregister(&bcm2835_alsa4_driver);
387 + platform_driver_unregister(&bcm2835_alsa3_driver);
389 + platform_driver_unregister(&bcm2835_alsa2_driver);
391 + platform_driver_unregister(&bcm2835_alsa1_driver);
393 + platform_driver_unregister(&bcm2835_alsa0_driver);
398 +static void bcm2835_alsa_device_exit(void)
400 + platform_driver_unregister(&bcm2835_alsa0_driver);
401 + platform_driver_unregister(&bcm2835_alsa1_driver);
402 + platform_driver_unregister(&bcm2835_alsa2_driver);
403 + platform_driver_unregister(&bcm2835_alsa3_driver);
404 + platform_driver_unregister(&bcm2835_alsa4_driver);
405 + platform_driver_unregister(&bcm2835_alsa5_driver);
406 + platform_driver_unregister(&bcm2835_alsa6_driver);
407 + platform_driver_unregister(&bcm2835_alsa7_driver);
410 +late_initcall(bcm2835_alsa_device_init);
411 +module_exit(bcm2835_alsa_device_exit);
413 +MODULE_AUTHOR("Dom Cobley");
414 +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
415 +MODULE_LICENSE("GPL");
416 +MODULE_ALIAS("platform:bcm2835_alsa");
418 +++ b/sound/arm/bcm2835-ctl.c
420 +/*****************************************************************************
421 +* Copyright 2011 Broadcom Corporation. All rights reserved.
423 +* Unless you and Broadcom execute a separate written software license
424 +* agreement governing use of this software, this software is licensed to you
425 +* under the terms of the GNU General Public License version 2, available at
426 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
428 +* Notwithstanding the above, under no circumstances may you combine this
429 +* software in any way with any other Broadcom software provided under a
430 +* license other than the GPL, without Broadcom's express prior written
432 +*****************************************************************************/
434 +#include <linux/platform_device.h>
435 +#include <linux/init.h>
436 +#include <linux/io.h>
437 +#include <linux/jiffies.h>
438 +#include <linux/slab.h>
439 +#include <linux/time.h>
440 +#include <linux/wait.h>
441 +#include <linux/delay.h>
442 +#include <linux/moduleparam.h>
443 +#include <linux/sched.h>
445 +#include <sound/core.h>
446 +#include <sound/control.h>
447 +#include <sound/pcm.h>
448 +#include <sound/pcm_params.h>
449 +#include <sound/rawmidi.h>
450 +#include <sound/initval.h>
451 +#include <sound/tlv.h>
453 +#include "bcm2835.h"
455 +/* volume maximum and minimum in terms of 0.01dB */
456 +#define CTRL_VOL_MAX 400
457 +#define CTRL_VOL_MIN -10239 /* originally -10240 */
460 +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
461 + struct snd_ctl_elem_info *uinfo)
463 + audio_info(" ... IN\n");
464 + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
465 + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
467 + uinfo->value.integer.min = CTRL_VOL_MIN;
468 + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
469 + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
470 + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
472 + uinfo->value.integer.min = 0;
473 + uinfo->value.integer.max = 1;
474 + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
475 + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
477 + uinfo->value.integer.min = 0;
478 + uinfo->value.integer.max = AUDIO_DEST_MAX-1;
480 + audio_info(" ... OUT\n");
484 +/* toggles mute on or off depending on the value of nmute, and returns
485 + * 1 if the mute value was changed, otherwise 0
487 +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
489 + /* if settings are ok, just return 0 */
490 + if(chip->mute == nmute)
493 + /* if the sound is muted then we need to unmute */
494 + if(chip->mute == CTRL_VOL_MUTE)
496 + chip->volume = chip->old_volume; /* copy the old volume back */
497 + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
499 + else /* otherwise we mute */
501 + chip->old_volume = chip->volume;
502 + chip->volume = 26214; /* set volume to minimum level AKA mute */
503 + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
506 + chip->mute = nmute;
510 +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
511 + struct snd_ctl_elem_value *ucontrol)
513 + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
515 + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
517 + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
518 + ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
519 + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
520 + ucontrol->value.integer.value[0] = chip->mute;
521 + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
522 + ucontrol->value.integer.value[0] = chip->dest;
527 +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
528 + struct snd_ctl_elem_value *ucontrol)
530 + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
533 + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
534 + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
535 + if (chip->mute == CTRL_VOL_MUTE) {
536 + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
537 + return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
540 + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
542 + chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
546 + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
547 + /* Now implemented */
548 + audio_info(" Mute attempted\n");
549 + changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
551 + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
552 + if (ucontrol->value.integer.value[0] != chip->dest) {
553 + chip->dest = ucontrol->value.integer.value[0];
559 + if (bcm2835_audio_set_ctls(chip))
560 + printk(KERN_ERR "Failed to set ALSA controls..\n");
566 +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
568 +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
570 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
571 + .name = "PCM Playback Volume",
573 + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
574 + .private_value = PCM_PLAYBACK_VOLUME,
575 + .info = snd_bcm2835_ctl_info,
576 + .get = snd_bcm2835_ctl_get,
577 + .put = snd_bcm2835_ctl_put,
579 + .tlv = {.p = snd_bcm2835_db_scale}
582 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
583 + .name = "PCM Playback Switch",
585 + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
586 + .private_value = PCM_PLAYBACK_MUTE,
587 + .info = snd_bcm2835_ctl_info,
588 + .get = snd_bcm2835_ctl_get,
589 + .put = snd_bcm2835_ctl_put,
593 + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
594 + .name = "PCM Playback Route",
596 + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
597 + .private_value = PCM_PLAYBACK_DEVICE,
598 + .info = snd_bcm2835_ctl_info,
599 + .get = snd_bcm2835_ctl_get,
600 + .put = snd_bcm2835_ctl_put,
605 +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
610 + strcpy(chip->card->mixername, "Broadcom Mixer");
611 + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
613 + snd_ctl_add(chip->card,
614 + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
621 +++ b/sound/arm/bcm2835.h
623 +/*****************************************************************************
624 +* Copyright 2011 Broadcom Corporation. All rights reserved.
626 +* Unless you and Broadcom execute a separate written software license
627 +* agreement governing use of this software, this software is licensed to you
628 +* under the terms of the GNU General Public License version 2, available at
629 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
631 +* Notwithstanding the above, under no circumstances may you combine this
632 +* software in any way with any other Broadcom software provided under a
633 +* license other than the GPL, without Broadcom's express prior written
635 +*****************************************************************************/
637 +#ifndef __SOUND_ARM_BCM2835_H
638 +#define __SOUND_ARM_BCM2835_H
640 +#include <linux/device.h>
641 +#include <linux/list.h>
642 +#include <linux/interrupt.h>
643 +#include <linux/wait.h>
644 +#include <sound/core.h>
645 +#include <sound/initval.h>
646 +#include <sound/pcm.h>
647 +#include <sound/pcm_params.h>
648 +#include <sound/pcm-indirect.h>
649 +#include <linux/workqueue.h>
652 +#define AUDIO_DEBUG_ENABLE
653 +#define AUDIO_VERBOSE_DEBUG_ENABLE
658 +#ifdef AUDIO_DEBUG_ENABLE
659 +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
661 +#define audio_debug(fmt, arg...) \
662 + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
664 +#define audio_info(fmt, arg...) \
665 + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
669 +#define audio_debug(fmt, arg...)
671 +#define audio_info(fmt, arg...)
673 +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
677 +#define audio_debug(fmt, arg...)
679 +#define audio_info(fmt, arg...)
681 +#endif /* AUDIO_DEBUG_ENABLE */
683 +#define audio_error(fmt, arg...) \
684 + printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
686 +#define audio_warning(fmt, arg...) \
687 + printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
689 +#define audio_alert(fmt, arg...) \
690 + printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
692 +#define MAX_SUBSTREAMS (8)
693 +#define AVAIL_SUBSTREAMS_MASK (0xff)
699 +/* macros for alsa2chip and chip2alsa, instead of functions */
701 +#define alsa2chip(vol) (uint)(-((vol << 8) / 100)) /* convert alsa to chip volume (defined as macro rather than function call) */
702 +#define chip2alsa(vol) -((vol * 100) >> 8) /* convert chip to alsa volume */
704 +/* Some constants for values .. */
706 + AUDIO_DEST_AUTO = 0,
707 + AUDIO_DEST_HEADPHONES = 1,
708 + AUDIO_DEST_HDMI = 2,
710 +} SND_BCM2835_ROUTE_T;
713 + PCM_PLAYBACK_VOLUME,
715 + PCM_PLAYBACK_DEVICE,
716 +} SND_BCM2835_CTRL_T;
718 +/* definition of the chip-specific record */
719 +typedef struct bcm2835_chip {
720 + struct snd_card *card;
721 + struct snd_pcm *pcm;
722 + /* Bitmat for valid reg_base and irq numbers */
723 + uint32_t avail_substreams;
724 + struct platform_device *pdev[MAX_SUBSTREAMS];
725 + struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
728 + int old_volume; /* stores the volume value whist muted */
733 +typedef struct bcm2835_alsa_stream {
734 + bcm2835_chip_t *chip;
735 + struct snd_pcm_substream *substream;
736 + struct snd_pcm_indirect pcm_indirect;
738 + struct semaphore buffers_update_sem;
739 + struct semaphore control_sem;
741 + volatile uint32_t control;
742 + volatile uint32_t status;
749 + unsigned int buffer_size;
750 + unsigned int period_size;
752 + uint32_t enable_fifo_irq;
753 + irq_handler_t fifo_irq_handler;
755 + atomic_t retrieved;
756 + struct opaque_AUDIO_INSTANCE_T *instance;
757 + struct workqueue_struct *my_wq;
759 +} bcm2835_alsa_stream_t;
761 +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
762 +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
764 +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
765 +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
766 +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
767 + uint32_t channels, uint32_t samplerate,
769 +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
770 +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
771 +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
772 +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
773 +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
775 +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
776 +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
777 +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
779 +#endif /* __SOUND_ARM_BCM2835_H */
781 +++ b/sound/arm/bcm2835-pcm.c
783 +/*****************************************************************************
784 +* Copyright 2011 Broadcom Corporation. All rights reserved.
786 +* Unless you and Broadcom execute a separate written software license
787 +* agreement governing use of this software, this software is licensed to you
788 +* under the terms of the GNU General Public License version 2, available at
789 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
791 +* Notwithstanding the above, under no circumstances may you combine this
792 +* software in any way with any other Broadcom software provided under a
793 +* license other than the GPL, without Broadcom's express prior written
795 +*****************************************************************************/
797 +#include <linux/interrupt.h>
798 +#include <linux/slab.h>
800 +#include "bcm2835.h"
802 +/* hardware definition */
803 +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
804 + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
805 + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
806 + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
807 + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
812 + .buffer_bytes_max = 128 * 1024,
813 + .period_bytes_min = 1 * 1024,
814 + .period_bytes_max = 128 * 1024,
816 + .periods_max = 128,
819 +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
821 + audio_info("Freeing up alsa stream here ..\n");
822 + if (runtime->private_data)
823 + kfree(runtime->private_data);
824 + runtime->private_data = NULL;
827 +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
829 + bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
830 + uint32_t consumed = 0;
831 + int new_period = 0;
833 + audio_info(" .. IN\n");
835 + audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
836 + alsa_stream ? alsa_stream->substream : 0);
838 + if (alsa_stream->open)
839 + consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
841 + /* We get called only if playback was triggered, So, the number of buffers we retrieve in
842 + * each iteration are the buffers that have been played out already
845 + if (alsa_stream->period_size) {
846 + if ((alsa_stream->pos / alsa_stream->period_size) !=
847 + ((alsa_stream->pos + consumed) / alsa_stream->period_size))
850 + audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
853 + alsa_stream->buffer_size,
854 + (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
855 + frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
857 + if (alsa_stream->buffer_size) {
858 + alsa_stream->pos += consumed &~ (1<<30);
859 + alsa_stream->pos %= alsa_stream->buffer_size;
862 + if (alsa_stream->substream) {
864 + snd_pcm_period_elapsed(alsa_stream->substream);
866 + audio_warning(" unexpected NULL substream\n");
868 + audio_info(" .. OUT\n");
870 + return IRQ_HANDLED;
874 +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
876 + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
877 + struct snd_pcm_runtime *runtime = substream->runtime;
878 + bcm2835_alsa_stream_t *alsa_stream;
882 + audio_info(" .. IN (%d)\n", substream->number);
884 + audio_info("Alsa open (%d)\n", substream->number);
885 + idx = substream->number;
887 + if (idx > MAX_SUBSTREAMS) {
889 + ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
890 + idx, MAX_SUBSTREAMS);
895 + /* Check if we are ready */
896 + if (!(chip->avail_substreams & (1 << idx))) {
897 + /* We are not ready yet */
898 + audio_error("substream(%d) device is not ready yet\n", idx);
903 + alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
904 + if (alsa_stream == NULL) {
908 + /* Initialise alsa_stream */
909 + alsa_stream->chip = chip;
910 + alsa_stream->substream = substream;
911 + alsa_stream->idx = idx;
912 + chip->alsa_stream[idx] = alsa_stream;
914 + sema_init(&alsa_stream->buffers_update_sem, 0);
915 + sema_init(&alsa_stream->control_sem, 0);
916 + spin_lock_init(&alsa_stream->lock);
918 + /* Enabled in start trigger, called on each "fifo irq" after that */
919 + alsa_stream->enable_fifo_irq = 0;
920 + alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
922 + runtime->private_data = alsa_stream;
923 + runtime->private_free = snd_bcm2835_playback_free;
924 + runtime->hw = snd_bcm2835_playback_hw;
925 + /* minimum 16 bytes alignment (for vchiq bulk transfers) */
926 + snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
929 + err = bcm2835_audio_open(alsa_stream);
931 + kfree(alsa_stream);
935 + alsa_stream->open = 1;
936 + alsa_stream->draining = 1;
939 + audio_info(" .. OUT =%d\n", err);
944 +/* close callback */
945 +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
947 + /* the hardware-specific codes will be here */
949 + struct snd_pcm_runtime *runtime = substream->runtime;
950 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
952 + audio_info(" .. IN\n");
953 + audio_info("Alsa close\n");
956 + * Call stop if it's still running. This happens when app
957 + * is force killed and we don't get a stop trigger.
959 + if (alsa_stream->running) {
961 + err = bcm2835_audio_stop(alsa_stream);
962 + alsa_stream->running = 0;
964 + audio_error(" Failed to STOP alsa device\n");
967 + alsa_stream->period_size = 0;
968 + alsa_stream->buffer_size = 0;
970 + if (alsa_stream->open) {
971 + alsa_stream->open = 0;
972 + bcm2835_audio_close(alsa_stream);
974 + if (alsa_stream->chip)
975 + alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
977 + * Do not free up alsa_stream here, it will be freed up by
978 + * runtime->private_free callback we registered in *_open above
981 + audio_info(" .. OUT\n");
986 +/* hw_params callback */
987 +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
988 + struct snd_pcm_hw_params *params)
991 + struct snd_pcm_runtime *runtime = substream->runtime;
992 + bcm2835_alsa_stream_t *alsa_stream =
993 + (bcm2835_alsa_stream_t *) runtime->private_data;
995 + audio_info(" .. IN\n");
997 + err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
1000 + (" pcm_lib_malloc failed to allocated pages for buffers\n");
1004 + err = bcm2835_audio_set_params(alsa_stream, params_channels(params),
1005 + params_rate(params),
1006 + snd_pcm_format_width(params_format
1009 + audio_error(" error setting hw params\n");
1012 + bcm2835_audio_setup(alsa_stream);
1014 + /* in preparation of the stream, set the controls (volume level) of the stream */
1015 + bcm2835_audio_set_ctls(alsa_stream->chip);
1017 + audio_info(" .. OUT\n");
1022 +/* hw_free callback */
1023 +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
1025 + audio_info(" .. IN\n");
1026 + return snd_pcm_lib_free_pages(substream);
1029 +/* prepare callback */
1030 +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
1032 + struct snd_pcm_runtime *runtime = substream->runtime;
1033 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
1035 + audio_info(" .. IN\n");
1037 + memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
1039 + alsa_stream->pcm_indirect.hw_buffer_size =
1040 + alsa_stream->pcm_indirect.sw_buffer_size =
1041 + snd_pcm_lib_buffer_bytes(substream);
1043 + alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
1044 + alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
1045 + alsa_stream->pos = 0;
1047 + audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
1048 + alsa_stream->buffer_size, alsa_stream->period_size,
1049 + alsa_stream->pos, runtime->frame_bits);
1051 + audio_info(" .. OUT\n");
1055 +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
1056 + struct snd_pcm_indirect *rec, size_t bytes)
1058 + struct snd_pcm_runtime *runtime = substream->runtime;
1059 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
1060 + void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
1063 + err = bcm2835_audio_write(alsa_stream, bytes, src);
1065 + audio_error(" Failed to transfer to alsa device (%d)\n", err);
1069 +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
1071 + struct snd_pcm_runtime *runtime = substream->runtime;
1072 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
1073 + struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
1075 + pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
1076 + snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
1077 + snd_bcm2835_pcm_transfer);
1081 +/* trigger callback */
1082 +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1084 + struct snd_pcm_runtime *runtime = substream->runtime;
1085 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
1088 + audio_info(" .. IN\n");
1091 + case SNDRV_PCM_TRIGGER_START:
1092 + audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
1093 + alsa_stream->running);
1094 + if (!alsa_stream->running) {
1095 + err = bcm2835_audio_start(alsa_stream);
1097 + alsa_stream->pcm_indirect.hw_io =
1098 + alsa_stream->pcm_indirect.hw_data =
1099 + bytes_to_frames(runtime,
1100 + alsa_stream->pos);
1101 + substream->ops->ack(substream);
1102 + alsa_stream->running = 1;
1103 + alsa_stream->draining = 1;
1105 + audio_error(" Failed to START alsa device (%d)\n", err);
1109 + case SNDRV_PCM_TRIGGER_STOP:
1111 + ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
1112 + alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
1113 + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1114 + audio_info("DRAINING\n");
1115 + alsa_stream->draining = 1;
1117 + audio_info("DROPPING\n");
1118 + alsa_stream->draining = 0;
1120 + if (alsa_stream->running) {
1121 + err = bcm2835_audio_stop(alsa_stream);
1123 + audio_error(" Failed to STOP alsa device (%d)\n", err);
1124 + alsa_stream->running = 0;
1131 + audio_info(" .. OUT\n");
1135 +/* pointer callback */
1136 +static snd_pcm_uframes_t
1137 +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
1139 + struct snd_pcm_runtime *runtime = substream->runtime;
1140 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
1142 + audio_info(" .. IN\n");
1144 + audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
1145 + frames_to_bytes(runtime, runtime->status->hw_ptr),
1146 + frames_to_bytes(runtime, runtime->control->appl_ptr),
1147 + alsa_stream->pos);
1149 + audio_info(" .. OUT\n");
1150 + return snd_pcm_indirect_playback_pointer(substream,
1151 + &alsa_stream->pcm_indirect,
1152 + alsa_stream->pos);
1155 +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1156 + unsigned int cmd, void *arg)
1158 + int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
1159 + audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
1160 + cmd, arg, arg ? *(unsigned *)arg : 0, ret);
1165 +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
1166 + .open = snd_bcm2835_playback_open,
1167 + .close = snd_bcm2835_playback_close,
1168 + .ioctl = snd_bcm2835_pcm_lib_ioctl,
1169 + .hw_params = snd_bcm2835_pcm_hw_params,
1170 + .hw_free = snd_bcm2835_pcm_hw_free,
1171 + .prepare = snd_bcm2835_pcm_prepare,
1172 + .trigger = snd_bcm2835_pcm_trigger,
1173 + .pointer = snd_bcm2835_pcm_pointer,
1174 + .ack = snd_bcm2835_pcm_ack,
1177 +/* create a pcm device */
1178 +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
1180 + struct snd_pcm *pcm;
1183 + audio_info(" .. IN\n");
1185 + snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
1188 + pcm->private_data = chip;
1189 + strcpy(pcm->name, "bcm2835 ALSA");
1191 + chip->dest = AUDIO_DEST_AUTO;
1192 + chip->volume = alsa2chip(0);
1193 + chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
1194 + /* set operators */
1195 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1196 + &snd_bcm2835_playback_ops);
1198 + /* pre-allocation of buffers */
1199 + /* NOTE: this may fail */
1200 + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1201 + snd_dma_continuous_data
1202 + (GFP_KERNEL), 64 * 1024,
1205 + audio_info(" .. OUT\n");
1210 +++ b/sound/arm/bcm2835-vchiq.c
1212 +/*****************************************************************************
1213 +* Copyright 2011 Broadcom Corporation. All rights reserved.
1215 +* Unless you and Broadcom execute a separate written software license
1216 +* agreement governing use of this software, this software is licensed to you
1217 +* under the terms of the GNU General Public License version 2, available at
1218 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1220 +* Notwithstanding the above, under no circumstances may you combine this
1221 +* software in any way with any other Broadcom software provided under a
1222 +* license other than the GPL, without Broadcom's express prior written
1224 +*****************************************************************************/
1226 +#include <linux/device.h>
1227 +#include <sound/core.h>
1228 +#include <sound/initval.h>
1229 +#include <sound/pcm.h>
1230 +#include <linux/io.h>
1231 +#include <linux/interrupt.h>
1232 +#include <linux/fs.h>
1233 +#include <linux/file.h>
1234 +#include <linux/mm.h>
1235 +#include <linux/syscalls.h>
1236 +#include <asm/uaccess.h>
1237 +#include <linux/slab.h>
1238 +#include <linux/delay.h>
1239 +#include <linux/atomic.h>
1240 +#include <linux/module.h>
1241 +#include <linux/completion.h>
1243 +#include "bcm2835.h"
1245 +/* ---- Include Files -------------------------------------------------------- */
1247 +#include "interface/vchi/vchi.h"
1248 +#include "vc_vchi_audioserv_defs.h"
1250 +/* ---- Private Constants and Types ------------------------------------------ */
1252 +#define BCM2835_AUDIO_STOP 0
1253 +#define BCM2835_AUDIO_START 1
1254 +#define BCM2835_AUDIO_WRITE 2
1256 +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
1257 +#ifdef AUDIO_DEBUG_ENABLE
1258 + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
1259 + #define LOG_WARN( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1260 + #define LOG_INFO( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1261 + #define LOG_DBG( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1263 + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
1264 + #define LOG_WARN( fmt, arg... )
1265 + #define LOG_INFO( fmt, arg... )
1266 + #define LOG_DBG( fmt, arg... )
1269 +typedef struct opaque_AUDIO_INSTANCE_T {
1270 + uint32_t num_connections;
1271 + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
1272 + struct completion msg_avail_comp;
1273 + struct mutex vchi_mutex;
1274 + bcm2835_alsa_stream_t *alsa_stream;
1276 + short peer_version;
1277 +} AUDIO_INSTANCE_T;
1279 +bool force_bulk = false;
1281 +/* ---- Private Variables ---------------------------------------------------- */
1283 +/* ---- Private Function Prototypes ------------------------------------------ */
1285 +/* ---- Private Functions ---------------------------------------------------- */
1287 +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
1288 +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
1289 +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
1290 + uint32_t count, void *src);
1293 + struct work_struct my_work;
1294 + bcm2835_alsa_stream_t *alsa_stream;
1300 +static void my_wq_function(struct work_struct *work)
1302 + my_work_t *w = (my_work_t *) work;
1304 + LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
1306 + case BCM2835_AUDIO_START:
1307 + ret = bcm2835_audio_start_worker(w->alsa_stream);
1309 + case BCM2835_AUDIO_STOP:
1310 + ret = bcm2835_audio_stop_worker(w->alsa_stream);
1312 + case BCM2835_AUDIO_WRITE:
1313 + ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
1317 + LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
1320 + kfree((void *)work);
1321 + LOG_DBG(" .. OUT %d\n", ret);
1324 +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
1327 + LOG_DBG(" .. IN\n");
1328 + if (alsa_stream->my_wq) {
1329 + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1330 + /*--- Queue some work (item 1) ---*/
1332 + INIT_WORK((struct work_struct *)work, my_wq_function);
1333 + work->alsa_stream = alsa_stream;
1334 + work->cmd = BCM2835_AUDIO_START;
1336 + (alsa_stream->my_wq, (struct work_struct *)work))
1339 + LOG_ERR(" .. Error: NULL work kmalloc\n");
1341 + LOG_DBG(" .. OUT %d\n", ret);
1345 +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
1348 + LOG_DBG(" .. IN\n");
1349 + if (alsa_stream->my_wq) {
1350 + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1351 + /*--- Queue some work (item 1) ---*/
1353 + INIT_WORK((struct work_struct *)work, my_wq_function);
1354 + work->alsa_stream = alsa_stream;
1355 + work->cmd = BCM2835_AUDIO_STOP;
1357 + (alsa_stream->my_wq, (struct work_struct *)work))
1360 + LOG_ERR(" .. Error: NULL work kmalloc\n");
1362 + LOG_DBG(" .. OUT %d\n", ret);
1366 +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
1367 + uint32_t count, void *src)
1370 + LOG_DBG(" .. IN\n");
1371 + if (alsa_stream->my_wq) {
1372 + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1373 + /*--- Queue some work (item 1) ---*/
1375 + INIT_WORK((struct work_struct *)work, my_wq_function);
1376 + work->alsa_stream = alsa_stream;
1377 + work->cmd = BCM2835_AUDIO_WRITE;
1379 + work->count = count;
1381 + (alsa_stream->my_wq, (struct work_struct *)work))
1384 + LOG_ERR(" .. Error: NULL work kmalloc\n");
1386 + LOG_DBG(" .. OUT %d\n", ret);
1390 +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
1392 + alsa_stream->my_wq = create_workqueue("my_queue");
1396 +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
1398 + if (alsa_stream->my_wq) {
1399 + flush_workqueue(alsa_stream->my_wq);
1400 + destroy_workqueue(alsa_stream->my_wq);
1401 + alsa_stream->my_wq = NULL;
1406 +static void audio_vchi_callback(void *param,
1407 + const VCHI_CALLBACK_REASON_T reason,
1410 + AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
1414 + bcm2835_alsa_stream_t *alsa_stream = 0;
1415 + LOG_DBG(" .. IN instance=%p, param=%p, reason=%d, handle=%p\n",
1416 + instance, param, reason, msg_handle);
1418 + if (!instance || reason != VCHI_CALLBACK_MSG_AVAILABLE) {
1421 + alsa_stream = instance->alsa_stream;
1422 + status = vchi_msg_dequeue(instance->vchi_handle[0],
1423 + &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
1424 + if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
1426 + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
1427 + instance, m.u.result.success);
1428 + instance->result = m.u.result.success;
1429 + complete(&instance->msg_avail_comp);
1430 + } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
1431 + irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
1433 + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
1434 + instance, m.u.complete.count);
1435 + if (alsa_stream && callback) {
1436 + atomic_add(m.u.complete.count, &alsa_stream->retrieved);
1437 + callback(0, alsa_stream);
1439 + LOG_DBG(" .. unexpected alsa_stream=%p, callback=%p\n",
1440 + alsa_stream, callback);
1443 + LOG_DBG(" .. unexpected m.type=%d\n", m.type);
1445 + LOG_DBG(" .. OUT\n");
1448 +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
1449 + VCHI_CONNECTION_T **
1451 + uint32_t num_connections)
1454 + AUDIO_INSTANCE_T *instance;
1457 + LOG_DBG("%s: start", __func__);
1459 + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
1460 + LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
1461 + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
1465 + /* Allocate memory for this instance */
1466 + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
1468 + memset(instance, 0, sizeof(*instance));
1469 + instance->num_connections = num_connections;
1471 + /* Create a lock for exclusive, serialized VCHI connection access */
1472 + mutex_init(&instance->vchi_mutex);
1473 + /* Open the VCHI service connections */
1474 + for (i = 0; i < num_connections; i++) {
1475 + SERVICE_CREATION_T params = {
1476 + VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
1477 + VC_AUDIO_SERVER_NAME, // 4cc service code
1478 + vchi_connections[i], // passed in fn pointers
1479 + 0, // rx fifo size (unused)
1480 + 0, // tx fifo size (unused)
1481 + audio_vchi_callback, // service callback
1482 + instance, // service callback parameter
1483 + 1, //TODO: remove VCOS_FALSE, // unaligned bulk recieves
1484 + 1, //TODO: remove VCOS_FALSE, // unaligned bulk transmits
1485 + 0 // want crc check on bulk transfers
1488 + status = vchi_service_open(vchi_instance, ¶ms,
1489 + &instance->vchi_handle[i]);
1492 + ("%s: failed to open VCHI service connection (status=%d)\n",
1493 + __func__, status);
1495 + goto err_close_services;
1497 + /* Finished with the service for now */
1498 + vchi_service_release(instance->vchi_handle[i]);
1503 +err_close_services:
1504 + for (i = 0; i < instance->num_connections; i++) {
1505 + vchi_service_close(instance->vchi_handle[i]);
1513 +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
1517 + LOG_DBG(" .. IN\n");
1519 + if (instance == NULL) {
1520 + LOG_ERR("%s: invalid handle %p\n", __func__, instance);
1525 + LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
1526 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1528 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1532 + /* Close all VCHI service connections */
1533 + for (i = 0; i < instance->num_connections; i++) {
1535 + LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
1536 + vchi_service_use(instance->vchi_handle[i]);
1538 + success = vchi_service_close(instance->vchi_handle[i]);
1539 + if (success != 0) {
1541 + ("%s: failed to close VCHI service connection (status=%d)\n",
1542 + __func__, success);
1546 + mutex_unlock(&instance->vchi_mutex);
1550 + LOG_DBG(" .. OUT\n");
1555 +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
1557 + static VCHI_INSTANCE_T vchi_instance;
1558 + static VCHI_CONNECTION_T *vchi_connection;
1559 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1561 + LOG_DBG(" .. IN\n");
1563 + LOG_INFO("%s: start", __func__);
1564 + //BUG_ON(instance);
1566 + LOG_ERR("%s: VCHI instance already open (%p)\n",
1567 + __func__, instance);
1568 + instance->alsa_stream = alsa_stream;
1569 + alsa_stream->instance = instance;
1570 + ret = 0; // xxx todo -1;
1571 + goto err_free_mem;
1574 + /* Initialize and create a VCHI connection */
1575 + ret = vchi_initialise(&vchi_instance);
1577 + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
1581 + goto err_free_mem;
1583 + ret = vchi_connect(NULL, 0, vchi_instance);
1585 + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
1589 + goto err_free_mem;
1592 + /* Initialize an instance of the audio service */
1593 + instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
1595 + if (instance == NULL /*|| audio_handle != instance */ ) {
1596 + LOG_ERR("%s: failed to initialize audio service\n", __func__);
1599 + goto err_free_mem;
1602 + instance->alsa_stream = alsa_stream;
1603 + alsa_stream->instance = instance;
1605 + LOG_DBG(" success !\n");
1607 + LOG_DBG(" .. OUT\n");
1612 +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
1614 + AUDIO_INSTANCE_T *instance;
1618 + LOG_DBG(" .. IN\n");
1620 + my_workqueue_init(alsa_stream);
1622 + ret = bcm2835_audio_open_connection(alsa_stream);
1627 + instance = alsa_stream->instance;
1629 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1631 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1634 + vchi_service_use(instance->vchi_handle[0]);
1636 + m.type = VC_AUDIO_MSG_TYPE_OPEN;
1638 + /* Send the message to the videocore */
1639 + success = vchi_msg_queue(instance->vchi_handle[0],
1641 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1643 + if (success != 0) {
1644 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1645 + __func__, success);
1654 + vchi_service_release(instance->vchi_handle[0]);
1655 + mutex_unlock(&instance->vchi_mutex);
1657 + LOG_DBG(" .. OUT\n");
1661 +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
1662 + bcm2835_chip_t * chip)
1665 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1668 + LOG_DBG(" .. IN\n");
1671 + (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
1673 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1675 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1678 + vchi_service_use(instance->vchi_handle[0]);
1680 + instance->result = -1;
1682 + m.type = VC_AUDIO_MSG_TYPE_CONTROL;
1683 + m.u.control.dest = chip->dest;
1684 + m.u.control.volume = chip->volume;
1686 + /* Create the message available completion */
1687 + init_completion(&instance->msg_avail_comp);
1689 + /* Send the message to the videocore */
1690 + success = vchi_msg_queue(instance->vchi_handle[0],
1692 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1694 + if (success != 0) {
1695 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1696 + __func__, success);
1702 + /* We are expecting a reply from the videocore */
1703 + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
1705 + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
1706 + __func__, success);
1710 + if (instance->result != 0) {
1711 + LOG_ERR("%s: result=%d\n", __func__, instance->result);
1720 + vchi_service_release(instance->vchi_handle[0]);
1721 + mutex_unlock(&instance->vchi_mutex);
1723 + LOG_DBG(" .. OUT\n");
1727 +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
1731 + LOG_DBG(" .. IN\n");
1733 + /* change ctls for all substreams */
1734 + for (i = 0; i < MAX_SUBSTREAMS; i++) {
1735 + if (chip->avail_substreams & (1 << i)) {
1736 + if (!chip->alsa_stream[i])
1738 + LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
1741 + else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
1742 + (chip->alsa_stream[i], chip) != 0)
1744 + LOG_DBG("Couldn't set the controls for stream %d\n", i);
1747 + else LOG_DBG(" Controls set for stream %d\n", i);
1750 + LOG_DBG(" .. OUT ret=%d\n", ret);
1754 +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
1755 + uint32_t channels, uint32_t samplerate,
1759 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1762 + LOG_DBG(" .. IN\n");
1765 + (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
1766 + channels, samplerate, bps);
1768 + /* resend ctls - alsa_stream may not have been open when first send */
1769 + ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
1771 + LOG_ERR(" Alsa controls not supported\n");
1775 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1777 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1780 + vchi_service_use(instance->vchi_handle[0]);
1782 + instance->result = -1;
1784 + m.type = VC_AUDIO_MSG_TYPE_CONFIG;
1785 + m.u.config.channels = channels;
1786 + m.u.config.samplerate = samplerate;
1787 + m.u.config.bps = bps;
1789 + /* Create the message available completion */
1790 + init_completion(&instance->msg_avail_comp);
1792 + /* Send the message to the videocore */
1793 + success = vchi_msg_queue(instance->vchi_handle[0],
1795 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1797 + if (success != 0) {
1798 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1799 + __func__, success);
1805 + /* We are expecting a reply from the videocore */
1806 + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
1808 + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
1809 + __func__, success);
1813 + if (instance->result != 0) {
1814 + LOG_ERR("%s: result=%d", __func__, instance->result);
1823 + vchi_service_release(instance->vchi_handle[0]);
1824 + mutex_unlock(&instance->vchi_mutex);
1826 + LOG_DBG(" .. OUT\n");
1830 +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
1832 + LOG_DBG(" .. IN\n");
1834 + LOG_DBG(" .. OUT\n");
1839 +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
1842 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1845 + LOG_DBG(" .. IN\n");
1847 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1849 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1852 + vchi_service_use(instance->vchi_handle[0]);
1854 + m.type = VC_AUDIO_MSG_TYPE_START;
1856 + /* Send the message to the videocore */
1857 + success = vchi_msg_queue(instance->vchi_handle[0],
1859 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1861 + if (success != 0) {
1862 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
1863 + __func__, success);
1872 + vchi_service_release(instance->vchi_handle[0]);
1873 + mutex_unlock(&instance->vchi_mutex);
1874 + LOG_DBG(" .. OUT\n");
1878 +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
1881 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1884 + LOG_DBG(" .. IN\n");
1886 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1888 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1891 + vchi_service_use(instance->vchi_handle[0]);
1893 + m.type = VC_AUDIO_MSG_TYPE_STOP;
1894 + m.u.stop.draining = alsa_stream->draining;
1896 + /* Send the message to the videocore */
1897 + success = vchi_msg_queue(instance->vchi_handle[0],
1899 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1901 + if (success != 0) {
1902 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
1903 + __func__, success);
1912 + vchi_service_release(instance->vchi_handle[0]);
1913 + mutex_unlock(&instance->vchi_mutex);
1914 + LOG_DBG(" .. OUT\n");
1918 +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
1921 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1924 + LOG_DBG(" .. IN\n");
1926 + my_workqueue_quit(alsa_stream);
1928 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1930 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1933 + vchi_service_use(instance->vchi_handle[0]);
1935 + m.type = VC_AUDIO_MSG_TYPE_CLOSE;
1937 + /* Create the message available completion */
1938 + init_completion(&instance->msg_avail_comp);
1940 + /* Send the message to the videocore */
1941 + success = vchi_msg_queue(instance->vchi_handle[0],
1943 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1945 + if (success != 0) {
1946 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
1947 + __func__, success);
1952 + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
1954 + LOG_ERR("%s: failed on waiting for event (status=%d)",
1955 + __func__, success);
1958 + if (instance->result != 0) {
1959 + LOG_ERR("%s: failed result (status=%d)",
1960 + __func__, instance->result);
1969 + vchi_service_release(instance->vchi_handle[0]);
1970 + mutex_unlock(&instance->vchi_mutex);
1972 + /* Stop the audio service */
1974 + vc_vchi_audio_deinit(instance);
1975 + alsa_stream->instance = NULL;
1977 + LOG_DBG(" .. OUT\n");
1981 +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
1982 + uint32_t count, void *src)
1985 + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1989 + LOG_DBG(" .. IN\n");
1991 + LOG_INFO(" Writing %d bytes from %p\n", count, src);
1993 + if(mutex_lock_interruptible(&instance->vchi_mutex))
1995 + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1998 + vchi_service_use(instance->vchi_handle[0]);
2000 + if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
2001 + LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
2003 + m.type = VC_AUDIO_MSG_TYPE_WRITE;
2004 + m.u.write.count = count;
2005 + // old version uses bulk, new version uses control
2006 + m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
2007 + m.u.write.callback = alsa_stream->fifo_irq_handler;
2008 + m.u.write.cookie = alsa_stream;
2009 + m.u.write.silence = src == NULL;
2011 + /* Send the message to the videocore */
2012 + success = vchi_msg_queue(instance->vchi_handle[0],
2014 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
2016 + if (success != 0) {
2017 + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
2018 + __func__, success);
2023 + if (!m.u.write.silence) {
2024 + if (m.u.write.max_packet == 0) {
2025 + /* Send the message to the videocore */
2026 + success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
2029 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED
2032 + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
2035 + while (count > 0) {
2036 + int bytes = min((int)m.u.write.max_packet, (int)count);
2037 + success = vchi_msg_queue(instance->vchi_handle[0],
2039 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
2040 + src = (char *)src + bytes;
2044 + if (success != 0) {
2046 + ("%s: failed on vchi_bulk_queue_transmit (status=%d)",
2047 + __func__, success);
2056 + vchi_service_release(instance->vchi_handle[0]);
2057 + mutex_unlock(&instance->vchi_mutex);
2058 + LOG_DBG(" .. OUT\n");
2063 + * Returns all buffers from arm->vc
2065 +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
2067 + LOG_DBG(" .. IN\n");
2068 + LOG_DBG(" .. OUT\n");
2073 + * Forces VC to flush(drop) its filled playback buffers and
2074 + * return them the us. (VC->ARM)
2076 +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
2078 + LOG_DBG(" .. IN\n");
2079 + LOG_DBG(" .. OUT\n");
2082 +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
2084 + uint32_t count = atomic_read(&alsa_stream->retrieved);
2085 + atomic_sub(count, &alsa_stream->retrieved);
2089 +module_param(force_bulk, bool, 0444);
2090 +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
2091 --- a/sound/arm/Kconfig
2092 +++ b/sound/arm/Kconfig
2093 @@ -39,5 +39,12 @@ config SND_PXA2XX_AC97
2094 Say Y or M if you want to support any AC97 codec attached to
2095 the PXA2xx AC97 interface.
2098 + tristate "BCM2835 ALSA driver"
2099 + depends on ARCH_BCM2708 && BCM2708_VCHIQ && SND
2102 + Say Y or M if you want to support BCM2835 Alsa pcm card driver
2106 --- a/sound/arm/Makefile
2107 +++ b/sound/arm/Makefile
2108 @@ -14,3 +14,9 @@ snd-pxa2xx-lib-$(CONFIG_SND_PXA2XX_LIB_A
2110 obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
2111 snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
2113 +obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
2114 +snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
2116 +EXTRA_CFLAGS += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
2119 +++ b/sound/arm/vc_vchi_audioserv_defs.h
2121 +/*****************************************************************************
2122 +* Copyright 2011 Broadcom Corporation. All rights reserved.
2124 +* Unless you and Broadcom execute a separate written software license
2125 +* agreement governing use of this software, this software is licensed to you
2126 +* under the terms of the GNU General Public License version 2, available at
2127 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
2129 +* Notwithstanding the above, under no circumstances may you combine this
2130 +* software in any way with any other Broadcom software provided under a
2131 +* license other than the GPL, without Broadcom's express prior written
2133 +*****************************************************************************/
2135 +#ifndef _VC_AUDIO_DEFS_H_
2136 +#define _VC_AUDIO_DEFS_H_
2138 +#define VC_AUDIOSERV_MIN_VER 1
2139 +#define VC_AUDIOSERV_VER 2
2141 +// FourCC code used for VCHI connection
2142 +#define VC_AUDIO_SERVER_NAME MAKE_FOURCC("AUDS")
2144 +// Maximum message length
2145 +#define VC_AUDIO_MAX_MSG_LEN (sizeof( VC_AUDIO_MSG_T ))
2147 +// List of screens that are currently supported
2148 +// All message types supported for HOST->VC direction
2150 + VC_AUDIO_MSG_TYPE_RESULT, // Generic result
2151 + VC_AUDIO_MSG_TYPE_COMPLETE, // Generic result
2152 + VC_AUDIO_MSG_TYPE_CONFIG, // Configure audio
2153 + VC_AUDIO_MSG_TYPE_CONTROL, // Configure audio
2154 + VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
2155 + VC_AUDIO_MSG_TYPE_CLOSE, // Configure audio
2156 + VC_AUDIO_MSG_TYPE_START, // Configure audio
2157 + VC_AUDIO_MSG_TYPE_STOP, // Configure audio
2158 + VC_AUDIO_MSG_TYPE_WRITE, // Configure audio
2159 + VC_AUDIO_MSG_TYPE_MAX
2160 +} VC_AUDIO_MSG_TYPE;
2162 +// configure the audio
2164 + uint32_t channels;
2165 + uint32_t samplerate;
2168 +} VC_AUDIO_CONFIG_T;
2174 +} VC_AUDIO_CONTROL_T;
2186 +} VC_AUDIO_CLOSE_T;
2191 +} VC_AUDIO_START_T;
2194 + uint32_t draining;
2198 +// configure the write audio samples
2200 + uint32_t count; // in bytes
2204 + uint16_t max_packet;
2205 +} VC_AUDIO_WRITE_T;
2207 +// Generic result for a request (VC->HOST)
2209 + int32_t success; // Success value
2211 +} VC_AUDIO_RESULT_T;
2213 +// Generic result for a request (VC->HOST)
2215 + int32_t count; // Success value
2218 +} VC_AUDIO_COMPLETE_T;
2220 +// Message header for all messages in HOST->VC direction
2222 + int32_t type; // Message type (VC_AUDIO_MSG_TYPE)
2224 + VC_AUDIO_CONFIG_T config;
2225 + VC_AUDIO_CONTROL_T control;
2226 + VC_AUDIO_OPEN_T open;
2227 + VC_AUDIO_CLOSE_T close;
2228 + VC_AUDIO_START_T start;
2229 + VC_AUDIO_STOP_T stop;
2230 + VC_AUDIO_WRITE_T write;
2231 + VC_AUDIO_RESULT_T result;
2232 + VC_AUDIO_COMPLETE_T complete;
2236 +#endif // _VC_AUDIO_DEFS_H_