brcm2708: switch to linux 4.4 and update patches
[openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0036-bcm2708-alsa-sound-driver.patch
1 From 26d9897700046c008240842bafec44eed727df89 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 036/156] bcm2708: alsa sound driver
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: popcornmix <popcornmix@gmail.com>
10
11 alsa: add mmap support and some cleanups to bcm2835 ALSA driver
12
13 snd-bcm2835: Add support for spdif/hdmi passthrough
14
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.
19
20 snd-bcm2708: Add mutex, improve logging
21
22 Fix for ALSA driver crash
23
24 Avoids an issue when closing and opening vchiq where a message can arrive before service handle has been written
25
26 alsa: reduce severity of expected warning message
27
28 snd-bcm2708: Fix dmesg spam for non-error case
29
30 alsa: Ensure mutexes are released through error paths
31
32 alsa: Make interrupted close paths quieter
33
34 BCM270x: Add onboard sound device to Device Tree
35
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.
39
40 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
41 ---
42  sound/arm/Kconfig                  |   8 +
43  sound/arm/Makefile                 |   5 +
44  sound/arm/bcm2835-ctl.c            | 323 +++++++++++++
45  sound/arm/bcm2835-pcm.c            | 557 +++++++++++++++++++++++
46  sound/arm/bcm2835-vchiq.c          | 902 +++++++++++++++++++++++++++++++++++++
47  sound/arm/bcm2835.c                | 511 +++++++++++++++++++++
48  sound/arm/bcm2835.h                | 167 +++++++
49  sound/arm/vc_vchi_audioserv_defs.h | 116 +++++
50  8 files changed, 2589 insertions(+)
51  create mode 100755 sound/arm/bcm2835-ctl.c
52  create mode 100755 sound/arm/bcm2835-pcm.c
53  create mode 100755 sound/arm/bcm2835-vchiq.c
54  create mode 100644 sound/arm/bcm2835.c
55  create mode 100755 sound/arm/bcm2835.h
56  create mode 100644 sound/arm/vc_vchi_audioserv_defs.h
57
58 --- a/sound/arm/Kconfig
59 +++ b/sound/arm/Kconfig
60 @@ -40,5 +40,13 @@ config SND_PXA2XX_AC97
61           Say Y or M if you want to support any AC97 codec attached to
62           the PXA2xx AC97 interface.
63  
64 +config SND_BCM2835
65 +       tristate "BCM2835 ALSA driver"
66 +       depends on (ARCH_BCM2708 || ARCH_BCM2709 || ARCH_BCM2835) \
67 +                  && BCM2708_VCHIQ && SND
68 +       select SND_PCM
69 +       help
70 +         Say Y or M if you want to support BCM2835 Alsa pcm card driver
71 +
72  endif  # SND_ARM
73  
74 --- a/sound/arm/Makefile
75 +++ b/sound/arm/Makefile
76 @@ -14,3 +14,8 @@ snd-pxa2xx-lib-$(CONFIG_SND_PXA2XX_LIB_A
77  
78  obj-$(CONFIG_SND_PXA2XX_AC97)  += snd-pxa2xx-ac97.o
79  snd-pxa2xx-ac97-objs           := pxa2xx-ac97.o
80 +
81 +obj-$(CONFIG_SND_BCM2835)      += snd-bcm2835.o
82 +snd-bcm2835-objs               := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
83 +
84 +ccflags-y += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
85 --- /dev/null
86 +++ b/sound/arm/bcm2835-ctl.c
87 @@ -0,0 +1,323 @@
88 +/*****************************************************************************
89 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
90 +*
91 +* Unless you and Broadcom execute a separate written software license
92 +* agreement governing use of this software, this software is licensed to you
93 +* under the terms of the GNU General Public License version 2, available at
94 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
95 +*
96 +* Notwithstanding the above, under no circumstances may you combine this
97 +* software in any way with any other Broadcom software provided under a
98 +* license other than the GPL, without Broadcom's express prior written
99 +* consent.
100 +*****************************************************************************/
101 +
102 +#include <linux/platform_device.h>
103 +#include <linux/init.h>
104 +#include <linux/io.h>
105 +#include <linux/jiffies.h>
106 +#include <linux/slab.h>
107 +#include <linux/time.h>
108 +#include <linux/wait.h>
109 +#include <linux/delay.h>
110 +#include <linux/moduleparam.h>
111 +#include <linux/sched.h>
112 +
113 +#include <sound/core.h>
114 +#include <sound/control.h>
115 +#include <sound/pcm.h>
116 +#include <sound/pcm_params.h>
117 +#include <sound/rawmidi.h>
118 +#include <sound/initval.h>
119 +#include <sound/tlv.h>
120 +#include <sound/asoundef.h>
121 +
122 +#include "bcm2835.h"
123 +
124 +/* volume maximum and minimum in terms of 0.01dB */
125 +#define CTRL_VOL_MAX 400
126 +#define CTRL_VOL_MIN -10239 /* originally -10240 */
127 +
128 +
129 +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
130 +                               struct snd_ctl_elem_info *uinfo)
131 +{
132 +       audio_info(" ... IN\n");
133 +       if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
134 +               uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
135 +               uinfo->count = 1;
136 +               uinfo->value.integer.min = CTRL_VOL_MIN;
137 +               uinfo->value.integer.max = CTRL_VOL_MAX;      /* 2303 */
138 +       } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
139 +               uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
140 +               uinfo->count = 1;
141 +               uinfo->value.integer.min = 0;
142 +               uinfo->value.integer.max = 1;
143 +       } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
144 +               uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
145 +               uinfo->count = 1;
146 +               uinfo->value.integer.min = 0;
147 +               uinfo->value.integer.max = AUDIO_DEST_MAX-1;
148 +       }
149 +       audio_info(" ... OUT\n");
150 +       return 0;
151 +}
152 +
153 +/* toggles mute on or off depending on the value of nmute, and returns
154 + * 1 if the mute value was changed, otherwise 0
155 + */
156 +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
157 +{
158 +       /* if settings are ok, just return 0 */
159 +       if(chip->mute == nmute)
160 +               return 0;
161 +
162 +       /* if the sound is muted then we need to unmute */
163 +       if(chip->mute == CTRL_VOL_MUTE)
164 +       {
165 +               chip->volume = chip->old_volume; /* copy the old volume back */
166 +               audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
167 +       }
168 +       else /* otherwise we mute */
169 +       {
170 +               chip->old_volume = chip->volume;
171 +               chip->volume = 26214; /* set volume to minimum level AKA mute */
172 +               audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
173 +       }
174 +
175 +       chip->mute = nmute;
176 +       return 1;
177 +}
178 +
179 +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
180 +                              struct snd_ctl_elem_value *ucontrol)
181 +{
182 +       struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
183 +
184 +       BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
185 +
186 +       if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
187 +               ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
188 +       else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
189 +               ucontrol->value.integer.value[0] = chip->mute;
190 +       else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
191 +               ucontrol->value.integer.value[0] = chip->dest;
192 +
193 +       return 0;
194 +}
195 +
196 +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
197 +                              struct snd_ctl_elem_value *ucontrol)
198 +{
199 +       struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
200 +       int changed = 0;
201 +
202 +       if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
203 +               audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
204 +               if (chip->mute == CTRL_VOL_MUTE) {
205 +                       /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
206 +                       return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
207 +               }
208 +               if (changed
209 +                   || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
210 +
211 +                       chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
212 +                       changed = 1;
213 +               }
214 +
215 +       } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
216 +               /* Now implemented */
217 +               audio_info(" Mute attempted\n");
218 +               changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
219 +
220 +       } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
221 +               if (ucontrol->value.integer.value[0] != chip->dest) {
222 +                       chip->dest = ucontrol->value.integer.value[0];
223 +                       changed = 1;
224 +               }
225 +       }
226 +
227 +       if (changed) {
228 +               if (bcm2835_audio_set_ctls(chip))
229 +                       printk(KERN_ERR "Failed to set ALSA controls..\n");
230 +       }
231 +
232 +       return changed;
233 +}
234 +
235 +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
236 +
237 +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
238 +       {
239 +        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
240 +        .name = "PCM Playback Volume",
241 +        .index = 0,
242 +        .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
243 +        .private_value = PCM_PLAYBACK_VOLUME,
244 +        .info = snd_bcm2835_ctl_info,
245 +        .get = snd_bcm2835_ctl_get,
246 +        .put = snd_bcm2835_ctl_put,
247 +        .count = 1,
248 +        .tlv = {.p = snd_bcm2835_db_scale}
249 +       },
250 +       {
251 +        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
252 +        .name = "PCM Playback Switch",
253 +        .index = 0,
254 +        .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
255 +        .private_value = PCM_PLAYBACK_MUTE,
256 +        .info = snd_bcm2835_ctl_info,
257 +        .get = snd_bcm2835_ctl_get,
258 +        .put = snd_bcm2835_ctl_put,
259 +        .count = 1,
260 +        },
261 +       {
262 +        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
263 +        .name = "PCM Playback Route",
264 +        .index = 0,
265 +        .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
266 +        .private_value = PCM_PLAYBACK_DEVICE,
267 +        .info = snd_bcm2835_ctl_info,
268 +        .get = snd_bcm2835_ctl_get,
269 +        .put = snd_bcm2835_ctl_put,
270 +        .count = 1,
271 +       },
272 +};
273 +
274 +static int snd_bcm2835_spdif_default_info(struct snd_kcontrol *kcontrol,
275 +                                         struct snd_ctl_elem_info *uinfo)
276 +{
277 +       uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
278 +       uinfo->count = 1;
279 +       return 0;
280 +}
281 +
282 +static int snd_bcm2835_spdif_default_get(struct snd_kcontrol *kcontrol,
283 +                                        struct snd_ctl_elem_value *ucontrol)
284 +{
285 +       struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
286 +       int i;
287 +
288 +       for (i = 0; i < 4; i++)
289 +               ucontrol->value.iec958.status[i] =
290 +                       (chip->spdif_status >> (i * 8)) && 0xff;
291 +
292 +       return 0;
293 +}
294 +
295 +static int snd_bcm2835_spdif_default_put(struct snd_kcontrol *kcontrol,
296 +                                        struct snd_ctl_elem_value *ucontrol)
297 +{
298 +       struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
299 +       unsigned int val = 0;
300 +       int i, change;
301 +
302 +       for (i = 0; i < 4; i++)
303 +               val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
304 +
305 +       change = val != chip->spdif_status;
306 +       chip->spdif_status = val;
307 +
308 +       return change;
309 +}
310 +
311 +static int snd_bcm2835_spdif_mask_info(struct snd_kcontrol *kcontrol,
312 +                                      struct snd_ctl_elem_info *uinfo)
313 +{
314 +       uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
315 +       uinfo->count = 1;
316 +       return 0;
317 +}
318 +
319 +static int snd_bcm2835_spdif_mask_get(struct snd_kcontrol *kcontrol,
320 +                                     struct snd_ctl_elem_value *ucontrol)
321 +{
322 +       /* bcm2835 supports only consumer mode and sets all other format flags
323 +        * automatically. So the only thing left is signalling non-audio
324 +        * content */
325 +       ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO;
326 +       return 0;
327 +}
328 +
329 +static int snd_bcm2835_spdif_stream_info(struct snd_kcontrol *kcontrol,
330 +                                        struct snd_ctl_elem_info *uinfo)
331 +{
332 +       uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
333 +       uinfo->count = 1;
334 +       return 0;
335 +}
336 +
337 +static int snd_bcm2835_spdif_stream_get(struct snd_kcontrol *kcontrol,
338 +                                       struct snd_ctl_elem_value *ucontrol)
339 +{
340 +       struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
341 +       int i;
342 +
343 +       for (i = 0; i < 4; i++)
344 +               ucontrol->value.iec958.status[i] =
345 +                       (chip->spdif_status >> (i * 8)) & 0xff;
346 +       return 0;
347 +}
348 +
349 +static int snd_bcm2835_spdif_stream_put(struct snd_kcontrol *kcontrol,
350 +                                       struct snd_ctl_elem_value *ucontrol)
351 +{
352 +       struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
353 +       unsigned int val = 0;
354 +       int i, change;
355 +
356 +       for (i = 0; i < 4; i++)
357 +               val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
358 +       change = val != chip->spdif_status;
359 +       chip->spdif_status = val;
360 +
361 +       return change;
362 +}
363 +
364 +static struct snd_kcontrol_new snd_bcm2835_spdif[] = {
365 +       {
366 +               .iface = SNDRV_CTL_ELEM_IFACE_PCM,
367 +               .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
368 +               .info = snd_bcm2835_spdif_default_info,
369 +               .get = snd_bcm2835_spdif_default_get,
370 +               .put = snd_bcm2835_spdif_default_put
371 +       },
372 +       {
373 +               .access = SNDRV_CTL_ELEM_ACCESS_READ,
374 +               .iface = SNDRV_CTL_ELEM_IFACE_PCM,
375 +               .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
376 +               .info = snd_bcm2835_spdif_mask_info,
377 +               .get = snd_bcm2835_spdif_mask_get,
378 +       },
379 +       {
380 +               .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
381 +                       SNDRV_CTL_ELEM_ACCESS_INACTIVE,
382 +               .iface = SNDRV_CTL_ELEM_IFACE_PCM,
383 +               .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
384 +               .info = snd_bcm2835_spdif_stream_info,
385 +               .get = snd_bcm2835_spdif_stream_get,
386 +               .put = snd_bcm2835_spdif_stream_put,
387 +       },
388 +};
389 +
390 +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
391 +{
392 +       int err;
393 +       unsigned int idx;
394 +
395 +       strcpy(chip->card->mixername, "Broadcom Mixer");
396 +       for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
397 +               err =
398 +                   snd_ctl_add(chip->card,
399 +                               snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
400 +               if (err < 0)
401 +                       return err;
402 +       }
403 +       for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) {
404 +               err = snd_ctl_add(chip->card,
405 +                               snd_ctl_new1(&snd_bcm2835_spdif[idx], chip));
406 +               if (err < 0)
407 +                       return err;
408 +       }
409 +       return 0;
410 +}
411 --- /dev/null
412 +++ b/sound/arm/bcm2835-pcm.c
413 @@ -0,0 +1,557 @@
414 +/*****************************************************************************
415 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
416 +*
417 +* Unless you and Broadcom execute a separate written software license
418 +* agreement governing use of this software, this software is licensed to you
419 +* under the terms of the GNU General Public License version 2, available at
420 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
421 +*
422 +* Notwithstanding the above, under no circumstances may you combine this
423 +* software in any way with any other Broadcom software provided under a
424 +* license other than the GPL, without Broadcom's express prior written
425 +* consent.
426 +*****************************************************************************/
427 +
428 +#include <linux/interrupt.h>
429 +#include <linux/slab.h>
430 +
431 +#include <sound/asoundef.h>
432 +
433 +#include "bcm2835.h"
434 +
435 +/* hardware definition */
436 +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
437 +       .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
438 +                SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
439 +       .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
440 +       .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
441 +       .rate_min = 8000,
442 +       .rate_max = 48000,
443 +       .channels_min = 1,
444 +       .channels_max = 2,
445 +       .buffer_bytes_max = 128 * 1024,
446 +       .period_bytes_min =   1 * 1024,
447 +       .period_bytes_max = 128 * 1024,
448 +       .periods_min = 1,
449 +       .periods_max = 128,
450 +};
451 +
452 +static struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
453 +       .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
454 +                SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
455 +       .formats = SNDRV_PCM_FMTBIT_S16_LE,
456 +       .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
457 +               SNDRV_PCM_RATE_48000,
458 +       .rate_min = 44100,
459 +       .rate_max = 48000,
460 +       .channels_min = 2,
461 +       .channels_max = 2,
462 +       .buffer_bytes_max = 128 * 1024,
463 +       .period_bytes_min =   1 * 1024,
464 +       .period_bytes_max = 128 * 1024,
465 +       .periods_min = 1,
466 +       .periods_max = 128,
467 +};
468 +
469 +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
470 +{
471 +       audio_info("Freeing up alsa stream here ..\n");
472 +       if (runtime->private_data)
473 +               kfree(runtime->private_data);
474 +       runtime->private_data = NULL;
475 +}
476 +
477 +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
478 +{
479 +       bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
480 +       uint32_t consumed = 0;
481 +       int new_period = 0;
482 +
483 +       audio_info(" .. IN\n");
484 +
485 +       audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
486 +                  alsa_stream ? alsa_stream->substream : 0);
487 +
488 +       if (alsa_stream->open)
489 +               consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
490 +
491 +       /* We get called only if playback was triggered, So, the number of buffers we retrieve in
492 +        * each iteration are the buffers that have been played out already
493 +        */
494 +
495 +       if (alsa_stream->period_size) {
496 +               if ((alsa_stream->pos / alsa_stream->period_size) !=
497 +                   ((alsa_stream->pos + consumed) / alsa_stream->period_size))
498 +                       new_period = 1;
499 +       }
500 +       audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
501 +                     alsa_stream->pos,
502 +                     consumed,
503 +                     alsa_stream->buffer_size,
504 +                         (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
505 +                         frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
506 +                         new_period);
507 +       if (alsa_stream->buffer_size) {
508 +               alsa_stream->pos += consumed &~ (1<<30);
509 +               alsa_stream->pos %= alsa_stream->buffer_size;
510 +       }
511 +
512 +       if (alsa_stream->substream) {
513 +               if (new_period)
514 +                       snd_pcm_period_elapsed(alsa_stream->substream);
515 +       } else {
516 +               audio_warning(" unexpected NULL substream\n");
517 +       }
518 +       audio_info(" .. OUT\n");
519 +
520 +       return IRQ_HANDLED;
521 +}
522 +
523 +/* open callback */
524 +static int snd_bcm2835_playback_open_generic(
525 +               struct snd_pcm_substream *substream, int spdif)
526 +{
527 +       bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
528 +       struct snd_pcm_runtime *runtime = substream->runtime;
529 +       bcm2835_alsa_stream_t *alsa_stream;
530 +       int idx;
531 +       int err;
532 +
533 +       audio_info(" .. IN (%d)\n", substream->number);
534 +
535 +       if(mutex_lock_interruptible(&chip->audio_mutex))
536 +       {
537 +               audio_error("Interrupted whilst waiting for lock\n");
538 +               return -EINTR;
539 +       }
540 +       audio_info("Alsa open (%d)\n", substream->number);
541 +       idx = substream->number;
542 +
543 +       if (spdif && chip->opened != 0) {
544 +               err = -EBUSY;
545 +               goto out;
546 +       }
547 +       else if (!spdif && (chip->opened & (1 << idx))) {
548 +               err = -EBUSY;
549 +               goto out;
550 +       }
551 +       if (idx > MAX_SUBSTREAMS) {
552 +               audio_error
553 +                   ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
554 +                    idx, MAX_SUBSTREAMS);
555 +               err = -ENODEV;
556 +               goto out;
557 +       }
558 +
559 +       /* Check if we are ready */
560 +       if (!(chip->avail_substreams & (1 << idx))) {
561 +               /* We are not ready yet */
562 +               audio_error("substream(%d) device is not ready yet\n", idx);
563 +               err = -EAGAIN;
564 +               goto out;
565 +       }
566 +
567 +       alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
568 +       if (alsa_stream == NULL) {
569 +               err = -ENOMEM;
570 +               goto out;
571 +       }
572 +
573 +       /* Initialise alsa_stream */
574 +       alsa_stream->chip = chip;
575 +       alsa_stream->substream = substream;
576 +       alsa_stream->idx = idx;
577 +
578 +       sema_init(&alsa_stream->buffers_update_sem, 0);
579 +       sema_init(&alsa_stream->control_sem, 0);
580 +       spin_lock_init(&alsa_stream->lock);
581 +
582 +       /* Enabled in start trigger, called on each "fifo irq" after that */
583 +       alsa_stream->enable_fifo_irq = 0;
584 +       alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
585 +
586 +       err = bcm2835_audio_open(alsa_stream);
587 +       if (err != 0) {
588 +               kfree(alsa_stream);
589 +               goto out;
590 +       }
591 +       runtime->private_data = alsa_stream;
592 +       runtime->private_free = snd_bcm2835_playback_free;
593 +       if (spdif) {
594 +               runtime->hw = snd_bcm2835_playback_spdif_hw;
595 +       } else {
596 +               /* clear spdif status, as we are not in spdif mode */
597 +               chip->spdif_status = 0;
598 +               runtime->hw = snd_bcm2835_playback_hw;
599 +       }
600 +       /* minimum 16 bytes alignment (for vchiq bulk transfers) */
601 +       snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
602 +                                  16);
603 +
604 +       chip->alsa_stream[idx] = alsa_stream;
605 +
606 +       chip->opened |= (1 << idx);
607 +       alsa_stream->open = 1;
608 +       alsa_stream->draining = 1;
609 +
610 +out:
611 +       mutex_unlock(&chip->audio_mutex);
612 +
613 +       audio_info(" .. OUT =%d\n", err);
614 +
615 +       return err;
616 +}
617 +
618 +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
619 +{
620 +       return snd_bcm2835_playback_open_generic(substream, 0);
621 +}
622 +
623 +static int snd_bcm2835_playback_spdif_open(struct snd_pcm_substream *substream)
624 +{
625 +       return snd_bcm2835_playback_open_generic(substream, 1);
626 +}
627 +
628 +/* close callback */
629 +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
630 +{
631 +       /* the hardware-specific codes will be here */
632 +
633 +       bcm2835_chip_t *chip;
634 +       struct snd_pcm_runtime *runtime;
635 +       bcm2835_alsa_stream_t *alsa_stream;
636 +
637 +       audio_info(" .. IN\n");
638 +
639 +       chip = snd_pcm_substream_chip(substream);
640 +       if(mutex_lock_interruptible(&chip->audio_mutex))
641 +       {
642 +               audio_error("Interrupted whilst waiting for lock\n");
643 +               return -EINTR;
644 +       }
645 +       runtime = substream->runtime;
646 +       alsa_stream = runtime->private_data;
647 +
648 +       audio_info("Alsa close\n");
649 +
650 +       /*
651 +        * Call stop if it's still running. This happens when app
652 +        * is force killed and we don't get a stop trigger.
653 +        */
654 +       if (alsa_stream->running) {
655 +               int err;
656 +               err = bcm2835_audio_stop(alsa_stream);
657 +               alsa_stream->running = 0;
658 +               if (err != 0)
659 +                       audio_error(" Failed to STOP alsa device\n");
660 +       }
661 +
662 +       alsa_stream->period_size = 0;
663 +       alsa_stream->buffer_size = 0;
664 +
665 +       if (alsa_stream->open) {
666 +               alsa_stream->open = 0;
667 +               bcm2835_audio_close(alsa_stream);
668 +       }
669 +       if (alsa_stream->chip)
670 +               alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
671 +       /*
672 +        * Do not free up alsa_stream here, it will be freed up by
673 +        * runtime->private_free callback we registered in *_open above
674 +        */
675 +
676 +       chip->opened &= ~(1 << substream->number);
677 +
678 +       mutex_unlock(&chip->audio_mutex);
679 +       audio_info(" .. OUT\n");
680 +
681 +       return 0;
682 +}
683 +
684 +/* hw_params callback */
685 +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
686 +                                    struct snd_pcm_hw_params *params)
687 +{
688 +       struct snd_pcm_runtime *runtime = substream->runtime;
689 +       bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
690 +       int err;
691 +
692 +       audio_info(" .. IN\n");
693 +
694 +       err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
695 +       if (err < 0) {
696 +               audio_error
697 +                   (" pcm_lib_malloc failed to allocated pages for buffers\n");
698 +               return err;
699 +       }
700 +
701 +       alsa_stream->channels = params_channels(params);
702 +       alsa_stream->params_rate = params_rate(params);
703 +       alsa_stream->pcm_format_width = snd_pcm_format_width(params_format (params));
704 +       audio_info(" .. OUT\n");
705 +
706 +       return err;
707 +}
708 +
709 +/* hw_free callback */
710 +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
711 +{
712 +       audio_info(" .. IN\n");
713 +       return snd_pcm_lib_free_pages(substream);
714 +}
715 +
716 +/* prepare callback */
717 +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
718 +{
719 +       bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
720 +       struct snd_pcm_runtime *runtime = substream->runtime;
721 +       bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
722 +       int channels;
723 +       int err;
724 +
725 +       audio_info(" .. IN\n");
726 +
727 +       /* notify the vchiq that it should enter spdif passthrough mode by
728 +        * setting channels=0 (see
729 +        * https://github.com/raspberrypi/linux/issues/528) */
730 +       if (chip->spdif_status & IEC958_AES0_NONAUDIO)
731 +               channels = 0;
732 +       else
733 +               channels = alsa_stream->channels;
734 +
735 +       err = bcm2835_audio_set_params(alsa_stream, channels,
736 +                                      alsa_stream->params_rate,
737 +                                      alsa_stream->pcm_format_width);
738 +       if (err < 0) {
739 +               audio_error(" error setting hw params\n");
740 +       }
741 +
742 +       bcm2835_audio_setup(alsa_stream);
743 +
744 +       /* in preparation of the stream, set the controls (volume level) of the stream */
745 +       bcm2835_audio_set_ctls(alsa_stream->chip);
746 +
747 +
748 +       memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
749 +
750 +       alsa_stream->pcm_indirect.hw_buffer_size =
751 +       alsa_stream->pcm_indirect.sw_buffer_size =
752 +               snd_pcm_lib_buffer_bytes(substream);
753 +
754 +       alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
755 +       alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
756 +       alsa_stream->pos = 0;
757 +
758 +       audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
759 +                     alsa_stream->buffer_size, alsa_stream->period_size,
760 +                     alsa_stream->pos, runtime->frame_bits);
761 +
762 +       audio_info(" .. OUT\n");
763 +       return 0;
764 +}
765 +
766 +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
767 +                                   struct snd_pcm_indirect *rec, size_t bytes)
768 +{
769 +       struct snd_pcm_runtime *runtime = substream->runtime;
770 +       bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
771 +       void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
772 +       int err;
773 +
774 +       err = bcm2835_audio_write(alsa_stream, bytes, src);
775 +       if (err)
776 +               audio_error(" Failed to transfer to alsa device (%d)\n", err);
777 +
778 +}
779 +
780 +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
781 +{
782 +       struct snd_pcm_runtime *runtime = substream->runtime;
783 +       bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
784 +       struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
785 +
786 +       pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
787 +       snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
788 +                                          snd_bcm2835_pcm_transfer);
789 +       return 0;
790 +}
791 +
792 +/* trigger callback */
793 +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
794 +{
795 +       struct snd_pcm_runtime *runtime = substream->runtime;
796 +       bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
797 +       int err = 0;
798 +
799 +       audio_info(" .. IN\n");
800 +
801 +       switch (cmd) {
802 +       case SNDRV_PCM_TRIGGER_START:
803 +               audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
804 +                             alsa_stream->running);
805 +               if (!alsa_stream->running) {
806 +                       err = bcm2835_audio_start(alsa_stream);
807 +                       if (err == 0) {
808 +                               alsa_stream->pcm_indirect.hw_io =
809 +                               alsa_stream->pcm_indirect.hw_data =
810 +                                       bytes_to_frames(runtime,
811 +                                                       alsa_stream->pos);
812 +                               substream->ops->ack(substream);
813 +                               alsa_stream->running = 1;
814 +                               alsa_stream->draining = 1;
815 +                       } else {
816 +                               audio_error(" Failed to START alsa device (%d)\n", err);
817 +                       }
818 +               }
819 +               break;
820 +       case SNDRV_PCM_TRIGGER_STOP:
821 +               audio_debug
822 +                   ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
823 +                            alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
824 +               if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
825 +                       audio_info("DRAINING\n");
826 +                       alsa_stream->draining = 1;
827 +               } else {
828 +                       audio_info("DROPPING\n");
829 +                       alsa_stream->draining = 0;
830 +               }
831 +               if (alsa_stream->running) {
832 +                       err = bcm2835_audio_stop(alsa_stream);
833 +                       if (err != 0)
834 +                               audio_error(" Failed to STOP alsa device (%d)\n", err);
835 +                       alsa_stream->running = 0;
836 +               }
837 +               break;
838 +       default:
839 +               err = -EINVAL;
840 +       }
841 +
842 +       audio_info(" .. OUT\n");
843 +       return err;
844 +}
845 +
846 +/* pointer callback */
847 +static snd_pcm_uframes_t
848 +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
849 +{
850 +       struct snd_pcm_runtime *runtime = substream->runtime;
851 +       bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
852 +
853 +       audio_info(" .. IN\n");
854 +
855 +       audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
856 +                     frames_to_bytes(runtime, runtime->status->hw_ptr),
857 +                     frames_to_bytes(runtime, runtime->control->appl_ptr),
858 +                     alsa_stream->pos);
859 +
860 +       audio_info(" .. OUT\n");
861 +       return snd_pcm_indirect_playback_pointer(substream,
862 +                                                &alsa_stream->pcm_indirect,
863 +                                                alsa_stream->pos);
864 +}
865 +
866 +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
867 +                                    unsigned int cmd, void *arg)
868 +{
869 +       int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
870 +       audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
871 +                   cmd, arg, arg ? *(unsigned *)arg : 0, ret);
872 +       return ret;
873 +}
874 +
875 +/* operators */
876 +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
877 +       .open = snd_bcm2835_playback_open,
878 +       .close = snd_bcm2835_playback_close,
879 +       .ioctl = snd_bcm2835_pcm_lib_ioctl,
880 +       .hw_params = snd_bcm2835_pcm_hw_params,
881 +       .hw_free = snd_bcm2835_pcm_hw_free,
882 +       .prepare = snd_bcm2835_pcm_prepare,
883 +       .trigger = snd_bcm2835_pcm_trigger,
884 +       .pointer = snd_bcm2835_pcm_pointer,
885 +       .ack = snd_bcm2835_pcm_ack,
886 +};
887 +
888 +static struct snd_pcm_ops snd_bcm2835_playback_spdif_ops = {
889 +       .open = snd_bcm2835_playback_spdif_open,
890 +       .close = snd_bcm2835_playback_close,
891 +       .ioctl = snd_bcm2835_pcm_lib_ioctl,
892 +       .hw_params = snd_bcm2835_pcm_hw_params,
893 +       .hw_free = snd_bcm2835_pcm_hw_free,
894 +       .prepare = snd_bcm2835_pcm_prepare,
895 +       .trigger = snd_bcm2835_pcm_trigger,
896 +       .pointer = snd_bcm2835_pcm_pointer,
897 +       .ack = snd_bcm2835_pcm_ack,
898 +};
899 +
900 +/* create a pcm device */
901 +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
902 +{
903 +       struct snd_pcm *pcm;
904 +       int err;
905 +
906 +       audio_info(" .. IN\n");
907 +       mutex_init(&chip->audio_mutex);
908 +       if(mutex_lock_interruptible(&chip->audio_mutex))
909 +       {
910 +               audio_error("Interrupted whilst waiting for lock\n");
911 +               return -EINTR;
912 +       }
913 +       err =
914 +           snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
915 +       if (err < 0)
916 +               goto out;
917 +       pcm->private_data = chip;
918 +       strcpy(pcm->name, "bcm2835 ALSA");
919 +       chip->pcm = pcm;
920 +       chip->dest = AUDIO_DEST_AUTO;
921 +       chip->volume = alsa2chip(0);
922 +       chip->mute = CTRL_VOL_UNMUTE;   /*disable mute on startup */
923 +       /* set operators */
924 +       snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
925 +                       &snd_bcm2835_playback_ops);
926 +
927 +       /* pre-allocation of buffers */
928 +       /* NOTE: this may fail */
929 +       snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
930 +                                             snd_dma_continuous_data
931 +                                             (GFP_KERNEL), 64 * 1024,
932 +                                             64 * 1024);
933 +
934 +out:
935 +       mutex_unlock(&chip->audio_mutex);
936 +       audio_info(" .. OUT\n");
937 +
938 +       return 0;
939 +}
940 +
941 +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip)
942 +{
943 +       struct snd_pcm *pcm;
944 +       int err;
945 +
946 +       audio_info(" .. IN\n");
947 +       if(mutex_lock_interruptible(&chip->audio_mutex))
948 +       {
949 +               audio_error("Interrupted whilst waiting for lock\n");
950 +               return -EINTR;
951 +       }
952 +       err = snd_pcm_new(chip->card, "bcm2835 ALSA", 1, 1, 0, &pcm);
953 +       if (err < 0)
954 +               goto out;
955 +
956 +       pcm->private_data = chip;
957 +       strcpy(pcm->name, "bcm2835 IEC958/HDMI");
958 +       chip->pcm_spdif = pcm;
959 +       snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
960 +                       &snd_bcm2835_playback_spdif_ops);
961 +
962 +       snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
963 +                                             snd_dma_continuous_data (GFP_KERNEL),
964 +                                             64 * 1024, 64 * 1024);
965 +out:
966 +       mutex_unlock(&chip->audio_mutex);
967 +       audio_info(" .. OUT\n");
968 +
969 +       return 0;
970 +}
971 --- /dev/null
972 +++ b/sound/arm/bcm2835-vchiq.c
973 @@ -0,0 +1,902 @@
974 +/*****************************************************************************
975 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
976 +*
977 +* Unless you and Broadcom execute a separate written software license
978 +* agreement governing use of this software, this software is licensed to you
979 +* under the terms of the GNU General Public License version 2, available at
980 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
981 +*
982 +* Notwithstanding the above, under no circumstances may you combine this
983 +* software in any way with any other Broadcom software provided under a
984 +* license other than the GPL, without Broadcom's express prior written
985 +* consent.
986 +*****************************************************************************/
987 +
988 +#include <linux/device.h>
989 +#include <sound/core.h>
990 +#include <sound/initval.h>
991 +#include <sound/pcm.h>
992 +#include <linux/io.h>
993 +#include <linux/interrupt.h>
994 +#include <linux/fs.h>
995 +#include <linux/file.h>
996 +#include <linux/mm.h>
997 +#include <linux/syscalls.h>
998 +#include <asm/uaccess.h>
999 +#include <linux/slab.h>
1000 +#include <linux/delay.h>
1001 +#include <linux/atomic.h>
1002 +#include <linux/module.h>
1003 +#include <linux/completion.h>
1004 +
1005 +#include "bcm2835.h"
1006 +
1007 +/* ---- Include Files -------------------------------------------------------- */
1008 +
1009 +#include "interface/vchi/vchi.h"
1010 +#include "vc_vchi_audioserv_defs.h"
1011 +
1012 +/* ---- Private Constants and Types ------------------------------------------ */
1013 +
1014 +#define BCM2835_AUDIO_STOP           0
1015 +#define BCM2835_AUDIO_START          1
1016 +#define BCM2835_AUDIO_WRITE          2
1017 +
1018 +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
1019 +#ifdef AUDIO_DEBUG_ENABLE
1020 +       #define LOG_ERR( fmt, arg... )   pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
1021 +       #define LOG_WARN( fmt, arg... )  pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1022 +       #define LOG_INFO( fmt, arg... )  pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1023 +       #define LOG_DBG( fmt, arg... )   pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
1024 +#else
1025 +       #define LOG_ERR( fmt, arg... )   pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
1026 +       #define LOG_WARN( fmt, arg... )
1027 +       #define LOG_INFO( fmt, arg... )
1028 +       #define LOG_DBG( fmt, arg... )
1029 +#endif
1030 +
1031 +typedef struct opaque_AUDIO_INSTANCE_T {
1032 +       uint32_t num_connections;
1033 +       VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
1034 +       struct completion msg_avail_comp;
1035 +       struct mutex vchi_mutex;
1036 +       bcm2835_alsa_stream_t *alsa_stream;
1037 +       int32_t result;
1038 +       short peer_version;
1039 +} AUDIO_INSTANCE_T;
1040 +
1041 +bool force_bulk = false;
1042 +
1043 +/* ---- Private Variables ---------------------------------------------------- */
1044 +
1045 +/* ---- Private Function Prototypes ------------------------------------------ */
1046 +
1047 +/* ---- Private Functions ---------------------------------------------------- */
1048 +
1049 +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
1050 +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
1051 +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
1052 +                                     uint32_t count, void *src);
1053 +
1054 +typedef struct {
1055 +       struct work_struct my_work;
1056 +       bcm2835_alsa_stream_t *alsa_stream;
1057 +       int cmd;
1058 +       void *src;
1059 +       uint32_t count;
1060 +} my_work_t;
1061 +
1062 +static void my_wq_function(struct work_struct *work)
1063 +{
1064 +       my_work_t *w = (my_work_t *) work;
1065 +       int ret = -9;
1066 +       LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
1067 +       switch (w->cmd) {
1068 +       case BCM2835_AUDIO_START:
1069 +               ret = bcm2835_audio_start_worker(w->alsa_stream);
1070 +               break;
1071 +       case BCM2835_AUDIO_STOP:
1072 +               ret = bcm2835_audio_stop_worker(w->alsa_stream);
1073 +               break;
1074 +       case BCM2835_AUDIO_WRITE:
1075 +               ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
1076 +                                                w->src);
1077 +               break;
1078 +       default:
1079 +               LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
1080 +               break;
1081 +       }
1082 +       kfree((void *)work);
1083 +       LOG_DBG(" .. OUT %d\n", ret);
1084 +}
1085 +
1086 +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
1087 +{
1088 +       int ret = -1;
1089 +       LOG_DBG(" .. IN\n");
1090 +       if (alsa_stream->my_wq) {
1091 +               my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1092 +               /*--- Queue some work (item 1) ---*/
1093 +               if (work) {
1094 +                       INIT_WORK((struct work_struct *)work, my_wq_function);
1095 +                       work->alsa_stream = alsa_stream;
1096 +                       work->cmd = BCM2835_AUDIO_START;
1097 +                       if (queue_work
1098 +                           (alsa_stream->my_wq, (struct work_struct *)work))
1099 +                               ret = 0;
1100 +               } else
1101 +                       LOG_ERR(" .. Error: NULL work kmalloc\n");
1102 +       }
1103 +       LOG_DBG(" .. OUT %d\n", ret);
1104 +       return ret;
1105 +}
1106 +
1107 +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
1108 +{
1109 +       int ret = -1;
1110 +       LOG_DBG(" .. IN\n");
1111 +       if (alsa_stream->my_wq) {
1112 +               my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1113 +                /*--- Queue some work (item 1) ---*/
1114 +               if (work) {
1115 +                       INIT_WORK((struct work_struct *)work, my_wq_function);
1116 +                       work->alsa_stream = alsa_stream;
1117 +                       work->cmd = BCM2835_AUDIO_STOP;
1118 +                       if (queue_work
1119 +                           (alsa_stream->my_wq, (struct work_struct *)work))
1120 +                               ret = 0;
1121 +               } else
1122 +                       LOG_ERR(" .. Error: NULL work kmalloc\n");
1123 +       }
1124 +       LOG_DBG(" .. OUT %d\n", ret);
1125 +       return ret;
1126 +}
1127 +
1128 +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
1129 +                       uint32_t count, void *src)
1130 +{
1131 +       int ret = -1;
1132 +       LOG_DBG(" .. IN\n");
1133 +       if (alsa_stream->my_wq) {
1134 +               my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
1135 +                /*--- Queue some work (item 1) ---*/
1136 +               if (work) {
1137 +                       INIT_WORK((struct work_struct *)work, my_wq_function);
1138 +                       work->alsa_stream = alsa_stream;
1139 +                       work->cmd = BCM2835_AUDIO_WRITE;
1140 +                       work->src = src;
1141 +                       work->count = count;
1142 +                       if (queue_work
1143 +                           (alsa_stream->my_wq, (struct work_struct *)work))
1144 +                               ret = 0;
1145 +               } else
1146 +                       LOG_ERR(" .. Error: NULL work kmalloc\n");
1147 +       }
1148 +       LOG_DBG(" .. OUT %d\n", ret);
1149 +       return ret;
1150 +}
1151 +
1152 +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
1153 +{
1154 +       alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
1155 +       return;
1156 +}
1157 +
1158 +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
1159 +{
1160 +       if (alsa_stream->my_wq) {
1161 +               flush_workqueue(alsa_stream->my_wq);
1162 +               destroy_workqueue(alsa_stream->my_wq);
1163 +               alsa_stream->my_wq = NULL;
1164 +       }
1165 +       return;
1166 +}
1167 +
1168 +static void audio_vchi_callback(void *param,
1169 +                               const VCHI_CALLBACK_REASON_T reason,
1170 +                               void *msg_handle)
1171 +{
1172 +       AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
1173 +       int32_t status;
1174 +       int32_t msg_len;
1175 +       VC_AUDIO_MSG_T m;
1176 +       LOG_DBG(" .. IN instance=%p, handle=%p, alsa=%p, reason=%d, handle=%p\n",
1177 +               instance, instance ? instance->vchi_handle[0] : NULL, instance ? instance->alsa_stream : NULL, reason, msg_handle);
1178 +
1179 +       if (reason != VCHI_CALLBACK_MSG_AVAILABLE) {
1180 +               return;
1181 +       }
1182 +       if (!instance) {
1183 +               LOG_ERR(" .. instance is null\n");
1184 +               BUG();
1185 +               return;
1186 +  }
1187 +  if (!instance->vchi_handle[0]) {
1188 +               LOG_ERR(" .. instance->vchi_handle[0] is null\n");
1189 +               BUG();
1190 +               return;
1191 +  }
1192 +       status = vchi_msg_dequeue(instance->vchi_handle[0],
1193 +                                 &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
1194 +       if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
1195 +               LOG_DBG
1196 +                   (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
1197 +                    instance, m.u.result.success);
1198 +               instance->result = m.u.result.success;
1199 +               complete(&instance->msg_avail_comp);
1200 +       } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
1201 +               bcm2835_alsa_stream_t *alsa_stream = instance->alsa_stream;
1202 +               irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
1203 +               LOG_DBG
1204 +                   (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
1205 +                    instance, m.u.complete.count);
1206 +               if (alsa_stream && callback) {
1207 +                       atomic_add(m.u.complete.count, &alsa_stream->retrieved);
1208 +                       callback(0, alsa_stream);
1209 +               } else {
1210 +                       LOG_ERR(" .. unexpected alsa_stream=%p, callback=%p\n",
1211 +                               alsa_stream, callback);
1212 +               }
1213 +       } else {
1214 +               LOG_ERR(" .. unexpected m.type=%d\n", m.type);
1215 +       }
1216 +       LOG_DBG(" .. OUT\n");
1217 +}
1218 +
1219 +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
1220 +                                           VCHI_CONNECTION_T **
1221 +                                           vchi_connections,
1222 +                                           uint32_t num_connections)
1223 +{
1224 +       uint32_t i;
1225 +       AUDIO_INSTANCE_T *instance;
1226 +       int status;
1227 +
1228 +       LOG_DBG("%s: start", __func__);
1229 +
1230 +       if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
1231 +               LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
1232 +                       __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
1233 +
1234 +               return NULL;
1235 +       }
1236 +       /* Allocate memory for this instance */
1237 +       instance = kmalloc(sizeof(*instance), GFP_KERNEL);
1238 +       if (!instance)
1239 +               return NULL;
1240 +
1241 +       memset(instance, 0, sizeof(*instance));
1242 +       instance->num_connections = num_connections;
1243 +
1244 +       /* Create a lock for exclusive, serialized VCHI connection access */
1245 +       mutex_init(&instance->vchi_mutex);
1246 +       /* Open the VCHI service connections */
1247 +       for (i = 0; i < num_connections; i++) {
1248 +               SERVICE_CREATION_T params = {
1249 +                       VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
1250 +                       VC_AUDIO_SERVER_NAME,   // 4cc service code
1251 +                       vchi_connections[i],    // passed in fn pointers
1252 +                       0,      // rx fifo size (unused)
1253 +                       0,      // tx fifo size (unused)
1254 +                       audio_vchi_callback,    // service callback
1255 +                       instance,       // service callback parameter
1256 +                       1,      //TODO: remove VCOS_FALSE,   // unaligned bulk recieves
1257 +                       1,      //TODO: remove VCOS_FALSE,   // unaligned bulk transmits
1258 +                       0       // want crc check on bulk transfers
1259 +               };
1260 +
1261 +               LOG_DBG("%s: about to open %i\n", __func__, i);
1262 +               status = vchi_service_open(vchi_instance, &params,
1263 +                                          &instance->vchi_handle[i]);
1264 +               LOG_DBG("%s: opened %i: %p=%d\n", __func__, i, instance->vchi_handle[i], status);
1265 +               if (status) {
1266 +                       LOG_ERR
1267 +                           ("%s: failed to open VCHI service connection (status=%d)\n",
1268 +                            __func__, status);
1269 +
1270 +                       goto err_close_services;
1271 +               }
1272 +               /* Finished with the service for now */
1273 +               vchi_service_release(instance->vchi_handle[i]);
1274 +       }
1275 +
1276 +       LOG_DBG("%s: okay\n", __func__);
1277 +       return instance;
1278 +
1279 +err_close_services:
1280 +       for (i = 0; i < instance->num_connections; i++) {
1281 +               LOG_ERR("%s: closing %i: %p\n", __func__, i, instance->vchi_handle[i]);
1282 +               if (instance->vchi_handle[i])
1283 +                       vchi_service_close(instance->vchi_handle[i]);
1284 +       }
1285 +
1286 +       kfree(instance);
1287 +       LOG_ERR("%s: error\n", __func__);
1288 +
1289 +       return NULL;
1290 +}
1291 +
1292 +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
1293 +{
1294 +       uint32_t i;
1295 +
1296 +       LOG_DBG(" .. IN\n");
1297 +
1298 +       if (instance == NULL) {
1299 +               LOG_ERR("%s: invalid handle %p\n", __func__, instance);
1300 +
1301 +               return -1;
1302 +       }
1303 +
1304 +       LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
1305 +       if(mutex_lock_interruptible(&instance->vchi_mutex))
1306 +       {
1307 +               LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1308 +               return -EINTR;
1309 +       }
1310 +
1311 +       /* Close all VCHI service connections */
1312 +       for (i = 0; i < instance->num_connections; i++) {
1313 +               int32_t success;
1314 +               LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
1315 +               vchi_service_use(instance->vchi_handle[i]);
1316 +
1317 +               success = vchi_service_close(instance->vchi_handle[i]);
1318 +               if (success != 0) {
1319 +                       LOG_DBG
1320 +                           ("%s: failed to close VCHI service connection (status=%d)\n",
1321 +                            __func__, success);
1322 +               }
1323 +       }
1324 +
1325 +       mutex_unlock(&instance->vchi_mutex);
1326 +
1327 +       kfree(instance);
1328 +
1329 +       LOG_DBG(" .. OUT\n");
1330 +
1331 +       return 0;
1332 +}
1333 +
1334 +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
1335 +{
1336 +       static VCHI_INSTANCE_T vchi_instance;
1337 +       static VCHI_CONNECTION_T *vchi_connection;
1338 +       static int initted;
1339 +       AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1340 +       int ret;
1341 +       LOG_DBG(" .. IN\n");
1342 +
1343 +       LOG_INFO("%s: start\n", __func__);
1344 +       BUG_ON(instance);
1345 +       if (instance) {
1346 +               LOG_ERR("%s: VCHI instance already open (%p)\n",
1347 +                       __func__, instance);
1348 +               instance->alsa_stream = alsa_stream;
1349 +               alsa_stream->instance = instance;
1350 +               ret = 0;        // xxx todo -1;
1351 +               goto err_free_mem;
1352 +       }
1353 +
1354 +       /* Initialize and create a VCHI connection */
1355 +       if (!initted) {
1356 +         ret = vchi_initialise(&vchi_instance);
1357 +         if (ret != 0) {
1358 +                 LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
1359 +                         __func__, ret);
1360 +
1361 +                 ret = -EIO;
1362 +                 goto err_free_mem;
1363 +         }
1364 +         ret = vchi_connect(NULL, 0, vchi_instance);
1365 +         if (ret != 0) {
1366 +                 LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
1367 +                         __func__, ret);
1368 +
1369 +                 ret = -EIO;
1370 +                 goto err_free_mem;
1371 +         }
1372 +       initted = 1;
1373 +       }
1374 +
1375 +       /* Initialize an instance of the audio service */
1376 +       instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
1377 +
1378 +       if (instance == NULL) {
1379 +               LOG_ERR("%s: failed to initialize audio service\n", __func__);
1380 +
1381 +               ret = -EPERM;
1382 +               goto err_free_mem;
1383 +       }
1384 +
1385 +       instance->alsa_stream = alsa_stream;
1386 +       alsa_stream->instance = instance;
1387 +
1388 +       LOG_DBG(" success !\n");
1389 +err_free_mem:
1390 +       LOG_DBG(" .. OUT\n");
1391 +
1392 +       return ret;
1393 +}
1394 +
1395 +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
1396 +{
1397 +       AUDIO_INSTANCE_T *instance;
1398 +       VC_AUDIO_MSG_T m;
1399 +       int32_t success;
1400 +       int ret;
1401 +       LOG_DBG(" .. IN\n");
1402 +
1403 +       my_workqueue_init(alsa_stream);
1404 +
1405 +       ret = bcm2835_audio_open_connection(alsa_stream);
1406 +       if (ret != 0) {
1407 +               ret = -1;
1408 +               goto exit;
1409 +       }
1410 +       instance = alsa_stream->instance;
1411 +       LOG_DBG(" instance (%p)\n", instance);
1412 +
1413 +       if(mutex_lock_interruptible(&instance->vchi_mutex))
1414 +       {
1415 +               LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1416 +               return -EINTR;
1417 +       }
1418 +       vchi_service_use(instance->vchi_handle[0]);
1419 +
1420 +       m.type = VC_AUDIO_MSG_TYPE_OPEN;
1421 +
1422 +       /* Send the message to the videocore */
1423 +       success = vchi_msg_queue(instance->vchi_handle[0],
1424 +                                &m, sizeof m,
1425 +                                VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1426 +
1427 +       if (success != 0) {
1428 +               LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1429 +                       __func__, success);
1430 +
1431 +               ret = -1;
1432 +               goto unlock;
1433 +       }
1434 +
1435 +       ret = 0;
1436 +
1437 +unlock:
1438 +       vchi_service_release(instance->vchi_handle[0]);
1439 +       mutex_unlock(&instance->vchi_mutex);
1440 +exit:
1441 +       LOG_DBG(" .. OUT\n");
1442 +       return ret;
1443 +}
1444 +
1445 +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
1446 +                                      bcm2835_chip_t * chip)
1447 +{
1448 +       VC_AUDIO_MSG_T m;
1449 +       AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1450 +       int32_t success;
1451 +       int ret;
1452 +       LOG_DBG(" .. IN\n");
1453 +
1454 +       LOG_INFO
1455 +           (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
1456 +
1457 +       if(mutex_lock_interruptible(&instance->vchi_mutex))
1458 +       {
1459 +               LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1460 +               return -EINTR;
1461 +       }
1462 +       vchi_service_use(instance->vchi_handle[0]);
1463 +
1464 +       instance->result = -1;
1465 +
1466 +       m.type = VC_AUDIO_MSG_TYPE_CONTROL;
1467 +       m.u.control.dest = chip->dest;
1468 +       m.u.control.volume = chip->volume;
1469 +
1470 +       /* Create the message available completion */
1471 +       init_completion(&instance->msg_avail_comp);
1472 +
1473 +       /* Send the message to the videocore */
1474 +       success = vchi_msg_queue(instance->vchi_handle[0],
1475 +                                &m, sizeof m,
1476 +                                VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1477 +
1478 +       if (success != 0) {
1479 +               LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1480 +                       __func__, success);
1481 +
1482 +               ret = -1;
1483 +               goto unlock;
1484 +       }
1485 +
1486 +       /* We are expecting a reply from the videocore */
1487 +       ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
1488 +       if (ret) {
1489 +               LOG_DBG("%s: failed on waiting for event (status=%d)\n",
1490 +                       __func__, success);
1491 +               goto unlock;
1492 +       }
1493 +
1494 +       if (instance->result != 0) {
1495 +               LOG_ERR("%s: result=%d\n", __func__, instance->result);
1496 +
1497 +               ret = -1;
1498 +               goto unlock;
1499 +       }
1500 +
1501 +       ret = 0;
1502 +
1503 +unlock:
1504 +       vchi_service_release(instance->vchi_handle[0]);
1505 +       mutex_unlock(&instance->vchi_mutex);
1506 +
1507 +       LOG_DBG(" .. OUT\n");
1508 +       return ret;
1509 +}
1510 +
1511 +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
1512 +{
1513 +       int i;
1514 +       int ret = 0;
1515 +       LOG_DBG(" .. IN\n");
1516 +       LOG_DBG(" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
1517 +
1518 +       /* change ctls for all substreams */
1519 +       for (i = 0; i < MAX_SUBSTREAMS; i++) {
1520 +               if (chip->avail_substreams & (1 << i)) {
1521 +                       if (!chip->alsa_stream[i])
1522 +                       {
1523 +                               LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
1524 +                               ret = 0;
1525 +                       }
1526 +                       else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
1527 +                                (chip->alsa_stream[i], chip) != 0)
1528 +                                {
1529 +                                       LOG_ERR("Couldn't set the controls for stream %d\n", i);
1530 +                                       ret = -1;
1531 +                                }
1532 +                       else LOG_DBG(" Controls set for stream %d\n", i);
1533 +               }
1534 +       }
1535 +       LOG_DBG(" .. OUT ret=%d\n", ret);
1536 +       return ret;
1537 +}
1538 +
1539 +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
1540 +                            uint32_t channels, uint32_t samplerate,
1541 +                            uint32_t bps)
1542 +{
1543 +       VC_AUDIO_MSG_T m;
1544 +       AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1545 +       int32_t success;
1546 +       int ret;
1547 +       LOG_DBG(" .. IN\n");
1548 +
1549 +       LOG_INFO
1550 +           (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
1551 +            channels, samplerate, bps);
1552 +
1553 +       /* resend ctls - alsa_stream may not have been open when first send */
1554 +       ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
1555 +       if (ret != 0) {
1556 +               LOG_ERR(" Alsa controls not supported\n");
1557 +               return -EINVAL;
1558 +       }
1559 +
1560 +       if(mutex_lock_interruptible(&instance->vchi_mutex))
1561 +       {
1562 +               LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1563 +               return -EINTR;
1564 +       }
1565 +       vchi_service_use(instance->vchi_handle[0]);
1566 +
1567 +       instance->result = -1;
1568 +
1569 +       m.type = VC_AUDIO_MSG_TYPE_CONFIG;
1570 +       m.u.config.channels = channels;
1571 +       m.u.config.samplerate = samplerate;
1572 +       m.u.config.bps = bps;
1573 +
1574 +       /* Create the message available completion */
1575 +       init_completion(&instance->msg_avail_comp);
1576 +
1577 +       /* Send the message to the videocore */
1578 +       success = vchi_msg_queue(instance->vchi_handle[0],
1579 +                                &m, sizeof m,
1580 +                                VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1581 +
1582 +       if (success != 0) {
1583 +               LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1584 +                       __func__, success);
1585 +
1586 +               ret = -1;
1587 +               goto unlock;
1588 +       }
1589 +
1590 +       /* We are expecting a reply from the videocore */
1591 +       ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
1592 +       if (ret) {
1593 +               LOG_DBG("%s: failed on waiting for event (status=%d)\n",
1594 +                       __func__, success);
1595 +               goto unlock;
1596 +       }
1597 +
1598 +       if (instance->result != 0) {
1599 +               LOG_ERR("%s: result=%d", __func__, instance->result);
1600 +
1601 +               ret = -1;
1602 +               goto unlock;
1603 +       }
1604 +
1605 +       ret = 0;
1606 +
1607 +unlock:
1608 +       vchi_service_release(instance->vchi_handle[0]);
1609 +       mutex_unlock(&instance->vchi_mutex);
1610 +
1611 +       LOG_DBG(" .. OUT\n");
1612 +       return ret;
1613 +}
1614 +
1615 +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
1616 +{
1617 +       LOG_DBG(" .. IN\n");
1618 +
1619 +       LOG_DBG(" .. OUT\n");
1620 +
1621 +       return 0;
1622 +}
1623 +
1624 +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
1625 +{
1626 +       VC_AUDIO_MSG_T m;
1627 +       AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1628 +       int32_t success;
1629 +       int ret;
1630 +       LOG_DBG(" .. IN\n");
1631 +
1632 +       if(mutex_lock_interruptible(&instance->vchi_mutex))
1633 +       {
1634 +               LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1635 +               return -EINTR;
1636 +       }
1637 +       vchi_service_use(instance->vchi_handle[0]);
1638 +
1639 +       m.type = VC_AUDIO_MSG_TYPE_START;
1640 +
1641 +       /* Send the message to the videocore */
1642 +       success = vchi_msg_queue(instance->vchi_handle[0],
1643 +                                &m, sizeof m,
1644 +                                VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1645 +
1646 +       if (success != 0) {
1647 +               LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1648 +                       __func__, success);
1649 +
1650 +               ret = -1;
1651 +               goto unlock;
1652 +       }
1653 +
1654 +       ret = 0;
1655 +
1656 +unlock:
1657 +       vchi_service_release(instance->vchi_handle[0]);
1658 +       mutex_unlock(&instance->vchi_mutex);
1659 +       LOG_DBG(" .. OUT\n");
1660 +       return ret;
1661 +}
1662 +
1663 +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
1664 +{
1665 +       VC_AUDIO_MSG_T m;
1666 +       AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1667 +       int32_t success;
1668 +       int ret;
1669 +       LOG_DBG(" .. IN\n");
1670 +
1671 +       if(mutex_lock_interruptible(&instance->vchi_mutex))
1672 +       {
1673 +               LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1674 +               return -EINTR;
1675 +       }
1676 +       vchi_service_use(instance->vchi_handle[0]);
1677 +
1678 +       m.type = VC_AUDIO_MSG_TYPE_STOP;
1679 +       m.u.stop.draining = alsa_stream->draining;
1680 +
1681 +       /* Send the message to the videocore */
1682 +       success = vchi_msg_queue(instance->vchi_handle[0],
1683 +                                &m, sizeof m,
1684 +                                VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1685 +
1686 +       if (success != 0) {
1687 +               LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1688 +                       __func__, success);
1689 +
1690 +               ret = -1;
1691 +               goto unlock;
1692 +       }
1693 +
1694 +       ret = 0;
1695 +
1696 +unlock:
1697 +       vchi_service_release(instance->vchi_handle[0]);
1698 +       mutex_unlock(&instance->vchi_mutex);
1699 +       LOG_DBG(" .. OUT\n");
1700 +       return ret;
1701 +}
1702 +
1703 +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
1704 +{
1705 +       VC_AUDIO_MSG_T m;
1706 +       AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1707 +       int32_t success;
1708 +       int ret;
1709 +       LOG_DBG(" .. IN\n");
1710 +
1711 +       my_workqueue_quit(alsa_stream);
1712 +
1713 +       if(mutex_lock_interruptible(&instance->vchi_mutex))
1714 +       {
1715 +               LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1716 +               return -EINTR;
1717 +       }
1718 +       vchi_service_use(instance->vchi_handle[0]);
1719 +
1720 +       m.type = VC_AUDIO_MSG_TYPE_CLOSE;
1721 +
1722 +       /* Create the message available completion */
1723 +       init_completion(&instance->msg_avail_comp);
1724 +
1725 +       /* Send the message to the videocore */
1726 +       success = vchi_msg_queue(instance->vchi_handle[0],
1727 +                                &m, sizeof m,
1728 +                                VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1729 +
1730 +       if (success != 0) {
1731 +               LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1732 +                       __func__, success);
1733 +               ret = -1;
1734 +               goto unlock;
1735 +       }
1736 +
1737 +       ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
1738 +       if (ret) {
1739 +               LOG_DBG("%s: failed on waiting for event (status=%d)\n",
1740 +                       __func__, success);
1741 +               goto unlock;
1742 +       }
1743 +       if (instance->result != 0) {
1744 +               LOG_ERR("%s: failed result (status=%d)\n",
1745 +                       __func__, instance->result);
1746 +
1747 +               ret = -1;
1748 +               goto unlock;
1749 +       }
1750 +
1751 +       ret = 0;
1752 +
1753 +unlock:
1754 +       vchi_service_release(instance->vchi_handle[0]);
1755 +       mutex_unlock(&instance->vchi_mutex);
1756 +
1757 +       /* Stop the audio service */
1758 +       if (instance) {
1759 +               vc_vchi_audio_deinit(instance);
1760 +               alsa_stream->instance = NULL;
1761 +       }
1762 +       LOG_DBG(" .. OUT\n");
1763 +       return ret;
1764 +}
1765 +
1766 +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
1767 +                              uint32_t count, void *src)
1768 +{
1769 +       VC_AUDIO_MSG_T m;
1770 +       AUDIO_INSTANCE_T *instance = alsa_stream->instance;
1771 +       int32_t success;
1772 +       int ret;
1773 +
1774 +       LOG_DBG(" .. IN\n");
1775 +
1776 +       LOG_INFO(" Writing %d bytes from %p\n", count, src);
1777 +
1778 +       if(mutex_lock_interruptible(&instance->vchi_mutex))
1779 +       {
1780 +               LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
1781 +               return -EINTR;
1782 +       }
1783 +       vchi_service_use(instance->vchi_handle[0]);
1784 +
1785 +       if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
1786 +               LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
1787 +       }
1788 +       m.type = VC_AUDIO_MSG_TYPE_WRITE;
1789 +       m.u.write.count = count;
1790 +       // old version uses bulk, new version uses control
1791 +       m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
1792 +       m.u.write.callback = alsa_stream->fifo_irq_handler;
1793 +       m.u.write.cookie = alsa_stream;
1794 +       m.u.write.silence = src == NULL;
1795 +
1796 +       /* Send the message to the videocore */
1797 +       success = vchi_msg_queue(instance->vchi_handle[0],
1798 +                                &m, sizeof m,
1799 +                                VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1800 +
1801 +       if (success != 0) {
1802 +               LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
1803 +                       __func__, success);
1804 +
1805 +               ret = -1;
1806 +               goto unlock;
1807 +       }
1808 +       if (!m.u.write.silence) {
1809 +               if (m.u.write.max_packet == 0) {
1810 +                       /* Send the message to the videocore */
1811 +                       success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
1812 +                                                          src, count,
1813 +                                                          0 *
1814 +                                                          VCHI_FLAGS_BLOCK_UNTIL_QUEUED
1815 +                                                          +
1816 +                                                          1 *
1817 +                                                          VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
1818 +                                                          NULL);
1819 +               } else {
1820 +                       while (count > 0) {
1821 +                               int bytes = min((int)m.u.write.max_packet, (int)count);
1822 +                               success = vchi_msg_queue(instance->vchi_handle[0],
1823 +                                                        src, bytes,
1824 +                                                        VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
1825 +                               src = (char *)src + bytes;
1826 +                               count -= bytes;
1827 +                       }
1828 +               }
1829 +               if (success != 0) {
1830 +                       LOG_ERR
1831 +                           ("%s: failed on vchi_bulk_queue_transmit (status=%d)\n",
1832 +                            __func__, success);
1833 +
1834 +                       ret = -1;
1835 +                       goto unlock;
1836 +               }
1837 +       }
1838 +       ret = 0;
1839 +
1840 +unlock:
1841 +       vchi_service_release(instance->vchi_handle[0]);
1842 +       mutex_unlock(&instance->vchi_mutex);
1843 +       LOG_DBG(" .. OUT\n");
1844 +       return ret;
1845 +}
1846 +
1847 +/**
1848 +  * Returns all buffers from arm->vc
1849 +  */
1850 +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
1851 +{
1852 +       LOG_DBG(" .. IN\n");
1853 +       LOG_DBG(" .. OUT\n");
1854 +       return;
1855 +}
1856 +
1857 +/**
1858 +  * Forces VC to flush(drop) its filled playback buffers and
1859 +  * return them the us. (VC->ARM)
1860 +  */
1861 +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
1862 +{
1863 +       LOG_DBG(" .. IN\n");
1864 +       LOG_DBG(" .. OUT\n");
1865 +}
1866 +
1867 +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
1868 +{
1869 +       uint32_t count = atomic_read(&alsa_stream->retrieved);
1870 +       atomic_sub(count, &alsa_stream->retrieved);
1871 +       return count;
1872 +}
1873 +
1874 +module_param(force_bulk, bool, 0444);
1875 +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
1876 --- /dev/null
1877 +++ b/sound/arm/bcm2835.c
1878 @@ -0,0 +1,511 @@
1879 +/*****************************************************************************
1880 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
1881 +*
1882 +* Unless you and Broadcom execute a separate written software license
1883 +* agreement governing use of this software, this software is licensed to you
1884 +* under the terms of the GNU General Public License version 2, available at
1885 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
1886 +*
1887 +* Notwithstanding the above, under no circumstances may you combine this
1888 +* software in any way with any other Broadcom software provided under a
1889 +* license other than the GPL, without Broadcom's express prior written
1890 +* consent.
1891 +*****************************************************************************/
1892 +
1893 +#include <linux/platform_device.h>
1894 +
1895 +#include <linux/init.h>
1896 +#include <linux/slab.h>
1897 +#include <linux/module.h>
1898 +#include <linux/of.h>
1899 +
1900 +#include "bcm2835.h"
1901 +
1902 +/* module parameters (see "Module Parameters") */
1903 +/* SNDRV_CARDS: maximum number of cards supported by this module */
1904 +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
1905 +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
1906 +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
1907 +
1908 +/* HACKY global pointers needed for successive probes to work : ssp
1909 + * But compared against the changes we will have to do in VC audio_ipc code
1910 + * to export 8 audio_ipc devices as a single IPC device and then monitor all
1911 + * four devices in a thread, this gets things done quickly and should be easier
1912 + * to debug if we run into issues
1913 + */
1914 +
1915 +static struct snd_card *g_card = NULL;
1916 +static bcm2835_chip_t *g_chip = NULL;
1917 +
1918 +static int snd_bcm2835_free(bcm2835_chip_t * chip)
1919 +{
1920 +       kfree(chip);
1921 +       return 0;
1922 +}
1923 +
1924 +/* component-destructor
1925 + * (see "Management of Cards and Components")
1926 + */
1927 +static int snd_bcm2835_dev_free(struct snd_device *device)
1928 +{
1929 +       return snd_bcm2835_free(device->device_data);
1930 +}
1931 +
1932 +/* chip-specific constructor
1933 + * (see "Management of Cards and Components")
1934 + */
1935 +static int snd_bcm2835_create(struct snd_card *card,
1936 +                                       struct platform_device *pdev,
1937 +                                       bcm2835_chip_t ** rchip)
1938 +{
1939 +       bcm2835_chip_t *chip;
1940 +       int err;
1941 +       static struct snd_device_ops ops = {
1942 +               .dev_free = snd_bcm2835_dev_free,
1943 +       };
1944 +
1945 +       *rchip = NULL;
1946 +
1947 +       chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1948 +       if (chip == NULL)
1949 +               return -ENOMEM;
1950 +
1951 +       chip->card = card;
1952 +
1953 +       err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1954 +       if (err < 0) {
1955 +               snd_bcm2835_free(chip);
1956 +               return err;
1957 +       }
1958 +
1959 +       *rchip = chip;
1960 +       return 0;
1961 +}
1962 +
1963 +static int snd_bcm2835_alsa_probe_dt(struct platform_device *pdev)
1964 +{
1965 +       struct device *dev = &pdev->dev;
1966 +       bcm2835_chip_t *chip;
1967 +       struct snd_card *card;
1968 +       u32 numchans;
1969 +       int err, i;
1970 +
1971 +       err = of_property_read_u32(dev->of_node, "brcm,pwm-channels",
1972 +                                  &numchans);
1973 +       if (err) {
1974 +               dev_err(dev, "Failed to get DT property 'brcm,pwm-channels'");
1975 +               return err;
1976 +       }
1977 +
1978 +       if (numchans == 0 || numchans > MAX_SUBSTREAMS) {
1979 +               numchans = MAX_SUBSTREAMS;
1980 +               dev_warn(dev, "Illegal 'brcm,pwm-channels' value, will use %u\n",
1981 +                        numchans);
1982 +       }
1983 +
1984 +       err = snd_card_new(NULL, -1, NULL, THIS_MODULE, 0, &card);
1985 +       if (err) {
1986 +               dev_err(dev, "Failed to create soundcard structure\n");
1987 +               return err;
1988 +       }
1989 +
1990 +       snd_card_set_dev(card, dev);
1991 +       strcpy(card->driver, "bcm2835");
1992 +       strcpy(card->shortname, "bcm2835 ALSA");
1993 +       sprintf(card->longname, "%s", card->shortname);
1994 +
1995 +       err = snd_bcm2835_create(card, pdev, &chip);
1996 +       if (err < 0) {
1997 +               dev_err(dev, "Failed to create bcm2835 chip\n");
1998 +               goto err_free;
1999 +       }
2000 +
2001 +       err = snd_bcm2835_new_pcm(chip);
2002 +       if (err < 0) {
2003 +               dev_err(dev, "Failed to create new bcm2835 pcm device\n");
2004 +               goto err_free;
2005 +       }
2006 +
2007 +       err = snd_bcm2835_new_spdif_pcm(chip);
2008 +       if (err < 0) {
2009 +               dev_err(dev, "Failed to create new bcm2835 spdif pcm device\n");
2010 +               goto err_free;
2011 +       }
2012 +
2013 +       err = snd_bcm2835_new_ctl(chip);
2014 +       if (err < 0) {
2015 +               dev_err(dev, "Failed to create new bcm2835 ctl\n");
2016 +               goto err_free;
2017 +       }
2018 +
2019 +       for (i = 0; i < numchans; i++) {
2020 +               chip->avail_substreams |= (1 << i);
2021 +               chip->pdev[i] = pdev;
2022 +       }
2023 +
2024 +       err = snd_card_register(card);
2025 +       if (err) {
2026 +               dev_err(dev, "Failed to register bcm2835 ALSA card \n");
2027 +               goto err_free;
2028 +       }
2029 +
2030 +       g_card = card;
2031 +       g_chip = chip;
2032 +       platform_set_drvdata(pdev, card);
2033 +       audio_info("bcm2835 ALSA card created with %u channels\n", numchans);
2034 +
2035 +       return 0;
2036 +
2037 +err_free:
2038 +       snd_card_free(card);
2039 +
2040 +       return err;
2041 +}
2042 +
2043 +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
2044 +{
2045 +       static int dev;
2046 +       bcm2835_chip_t *chip;
2047 +       struct snd_card *card;
2048 +       int err;
2049 +
2050 +       if (pdev->dev.of_node)
2051 +               return snd_bcm2835_alsa_probe_dt(pdev);
2052 +
2053 +       if (dev >= MAX_SUBSTREAMS)
2054 +               return -ENODEV;
2055 +
2056 +       if (!enable[dev]) {
2057 +               dev++;
2058 +               return -ENOENT;
2059 +       }
2060 +
2061 +       if (dev > 0)
2062 +               goto add_register_map;
2063 +
2064 +       err = snd_card_new(NULL, index[dev], id[dev], THIS_MODULE, 0, &g_card);
2065 +       if (err < 0)
2066 +               goto out;
2067 +
2068 +       snd_card_set_dev(g_card, &pdev->dev);
2069 +       strcpy(g_card->driver, "bcm2835");
2070 +       strcpy(g_card->shortname, "bcm2835 ALSA");
2071 +       sprintf(g_card->longname, "%s", g_card->shortname);
2072 +
2073 +       err = snd_bcm2835_create(g_card, pdev, &chip);
2074 +       if (err < 0) {
2075 +               dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
2076 +               goto out_bcm2835_create;
2077 +       }
2078 +
2079 +       g_chip = chip;
2080 +       err = snd_bcm2835_new_pcm(chip);
2081 +       if (err < 0) {
2082 +               dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
2083 +               goto out_bcm2835_new_pcm;
2084 +       }
2085 +
2086 +       err = snd_bcm2835_new_spdif_pcm(chip);
2087 +       if (err < 0) {
2088 +               dev_err(&pdev->dev, "Failed to create new BCM2835 spdif pcm device\n");
2089 +               goto out_bcm2835_new_spdif;
2090 +       }
2091 +
2092 +       err = snd_bcm2835_new_ctl(chip);
2093 +       if (err < 0) {
2094 +               dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
2095 +               goto out_bcm2835_new_ctl;
2096 +       }
2097 +
2098 +add_register_map:
2099 +       card = g_card;
2100 +       chip = g_chip;
2101 +
2102 +       BUG_ON(!(card && chip));
2103 +
2104 +       chip->avail_substreams |= (1 << dev);
2105 +       chip->pdev[dev] = pdev;
2106 +
2107 +       if (dev == 0) {
2108 +               err = snd_card_register(card);
2109 +               if (err < 0) {
2110 +                       dev_err(&pdev->dev,
2111 +                               "Failed to register bcm2835 ALSA card \n");
2112 +                       goto out_card_register;
2113 +               }
2114 +               platform_set_drvdata(pdev, card);
2115 +               audio_info("bcm2835 ALSA card created!\n");
2116 +       } else {
2117 +               audio_info("bcm2835 ALSA chip created!\n");
2118 +               platform_set_drvdata(pdev, (void *)dev);
2119 +       }
2120 +
2121 +       dev++;
2122 +
2123 +       return 0;
2124 +
2125 +out_card_register:
2126 +out_bcm2835_new_ctl:
2127 +out_bcm2835_new_spdif:
2128 +out_bcm2835_new_pcm:
2129 +out_bcm2835_create:
2130 +       BUG_ON(!g_card);
2131 +       if (snd_card_free(g_card))
2132 +               dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
2133 +       g_card = NULL;
2134 +out:
2135 +       dev = SNDRV_CARDS;      /* stop more avail_substreams from being probed */
2136 +       dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
2137 +       return err;
2138 +}
2139 +
2140 +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
2141 +{
2142 +       uint32_t idx;
2143 +       void *drv_data;
2144 +
2145 +       drv_data = platform_get_drvdata(pdev);
2146 +
2147 +       if (drv_data == (void *)g_card) {
2148 +               /* This is the card device */
2149 +               snd_card_free((struct snd_card *)drv_data);
2150 +               g_card = NULL;
2151 +               g_chip = NULL;
2152 +       } else {
2153 +               idx = (uint32_t) drv_data;
2154 +               if (g_card != NULL) {
2155 +                       BUG_ON(!g_chip);
2156 +                       /* We pass chip device numbers in audio ipc devices
2157 +                        * other than the one we registered our card with
2158 +                        */
2159 +                       idx = (uint32_t) drv_data;
2160 +                       BUG_ON(!idx || idx > MAX_SUBSTREAMS);
2161 +                       g_chip->avail_substreams &= ~(1 << idx);
2162 +                       /* There should be atleast one substream registered
2163 +                        * after we are done here, as it wil be removed when
2164 +                        * the *remove* is called for the card device
2165 +                        */
2166 +                       BUG_ON(!g_chip->avail_substreams);
2167 +               }
2168 +       }
2169 +
2170 +       platform_set_drvdata(pdev, NULL);
2171 +
2172 +       return 0;
2173 +}
2174 +
2175 +#ifdef CONFIG_PM
2176 +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
2177 +                                   pm_message_t state)
2178 +{
2179 +       return 0;
2180 +}
2181 +
2182 +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
2183 +{
2184 +       return 0;
2185 +}
2186 +
2187 +#endif
2188 +
2189 +static const struct of_device_id snd_bcm2835_of_match_table[] = {
2190 +       { .compatible = "brcm,bcm2835-audio", },
2191 +       {},
2192 +};
2193 +MODULE_DEVICE_TABLE(of, snd_bcm2835_of_match_table);
2194 +
2195 +static struct platform_driver bcm2835_alsa0_driver = {
2196 +       .probe = snd_bcm2835_alsa_probe,
2197 +       .remove = snd_bcm2835_alsa_remove,
2198 +#ifdef CONFIG_PM
2199 +       .suspend = snd_bcm2835_alsa_suspend,
2200 +       .resume = snd_bcm2835_alsa_resume,
2201 +#endif
2202 +       .driver = {
2203 +                  .name = "bcm2835_AUD0",
2204 +                  .owner = THIS_MODULE,
2205 +                  .of_match_table = snd_bcm2835_of_match_table,
2206 +                  },
2207 +};
2208 +
2209 +static struct platform_driver bcm2835_alsa1_driver = {
2210 +       .probe = snd_bcm2835_alsa_probe,
2211 +       .remove = snd_bcm2835_alsa_remove,
2212 +#ifdef CONFIG_PM
2213 +       .suspend = snd_bcm2835_alsa_suspend,
2214 +       .resume = snd_bcm2835_alsa_resume,
2215 +#endif
2216 +       .driver = {
2217 +                  .name = "bcm2835_AUD1",
2218 +                  .owner = THIS_MODULE,
2219 +                  },
2220 +};
2221 +
2222 +static struct platform_driver bcm2835_alsa2_driver = {
2223 +       .probe = snd_bcm2835_alsa_probe,
2224 +       .remove = snd_bcm2835_alsa_remove,
2225 +#ifdef CONFIG_PM
2226 +       .suspend = snd_bcm2835_alsa_suspend,
2227 +       .resume = snd_bcm2835_alsa_resume,
2228 +#endif
2229 +       .driver = {
2230 +                  .name = "bcm2835_AUD2",
2231 +                  .owner = THIS_MODULE,
2232 +                  },
2233 +};
2234 +
2235 +static struct platform_driver bcm2835_alsa3_driver = {
2236 +       .probe = snd_bcm2835_alsa_probe,
2237 +       .remove = snd_bcm2835_alsa_remove,
2238 +#ifdef CONFIG_PM
2239 +       .suspend = snd_bcm2835_alsa_suspend,
2240 +       .resume = snd_bcm2835_alsa_resume,
2241 +#endif
2242 +       .driver = {
2243 +                  .name = "bcm2835_AUD3",
2244 +                  .owner = THIS_MODULE,
2245 +                  },
2246 +};
2247 +
2248 +static struct platform_driver bcm2835_alsa4_driver = {
2249 +       .probe = snd_bcm2835_alsa_probe,
2250 +       .remove = snd_bcm2835_alsa_remove,
2251 +#ifdef CONFIG_PM
2252 +       .suspend = snd_bcm2835_alsa_suspend,
2253 +       .resume = snd_bcm2835_alsa_resume,
2254 +#endif
2255 +       .driver = {
2256 +                  .name = "bcm2835_AUD4",
2257 +                  .owner = THIS_MODULE,
2258 +                  },
2259 +};
2260 +
2261 +static struct platform_driver bcm2835_alsa5_driver = {
2262 +       .probe = snd_bcm2835_alsa_probe,
2263 +       .remove = snd_bcm2835_alsa_remove,
2264 +#ifdef CONFIG_PM
2265 +       .suspend = snd_bcm2835_alsa_suspend,
2266 +       .resume = snd_bcm2835_alsa_resume,
2267 +#endif
2268 +       .driver = {
2269 +                  .name = "bcm2835_AUD5",
2270 +                  .owner = THIS_MODULE,
2271 +                  },
2272 +};
2273 +
2274 +static struct platform_driver bcm2835_alsa6_driver = {
2275 +       .probe = snd_bcm2835_alsa_probe,
2276 +       .remove = snd_bcm2835_alsa_remove,
2277 +#ifdef CONFIG_PM
2278 +       .suspend = snd_bcm2835_alsa_suspend,
2279 +       .resume = snd_bcm2835_alsa_resume,
2280 +#endif
2281 +       .driver = {
2282 +                  .name = "bcm2835_AUD6",
2283 +                  .owner = THIS_MODULE,
2284 +                  },
2285 +};
2286 +
2287 +static struct platform_driver bcm2835_alsa7_driver = {
2288 +       .probe = snd_bcm2835_alsa_probe,
2289 +       .remove = snd_bcm2835_alsa_remove,
2290 +#ifdef CONFIG_PM
2291 +       .suspend = snd_bcm2835_alsa_suspend,
2292 +       .resume = snd_bcm2835_alsa_resume,
2293 +#endif
2294 +       .driver = {
2295 +                  .name = "bcm2835_AUD7",
2296 +                  .owner = THIS_MODULE,
2297 +                  },
2298 +};
2299 +
2300 +static int bcm2835_alsa_device_init(void)
2301 +{
2302 +       int err;
2303 +       err = platform_driver_register(&bcm2835_alsa0_driver);
2304 +       if (err) {
2305 +               pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2306 +               goto out;
2307 +       }
2308 +
2309 +       err = platform_driver_register(&bcm2835_alsa1_driver);
2310 +       if (err) {
2311 +               pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2312 +               goto unregister_0;
2313 +       }
2314 +
2315 +       err = platform_driver_register(&bcm2835_alsa2_driver);
2316 +       if (err) {
2317 +               pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2318 +               goto unregister_1;
2319 +       }
2320 +
2321 +       err = platform_driver_register(&bcm2835_alsa3_driver);
2322 +       if (err) {
2323 +               pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2324 +               goto unregister_2;
2325 +       }
2326 +
2327 +       err = platform_driver_register(&bcm2835_alsa4_driver);
2328 +       if (err) {
2329 +               pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2330 +               goto unregister_3;
2331 +       }
2332 +
2333 +       err = platform_driver_register(&bcm2835_alsa5_driver);
2334 +       if (err) {
2335 +               pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2336 +               goto unregister_4;
2337 +       }
2338 +
2339 +       err = platform_driver_register(&bcm2835_alsa6_driver);
2340 +       if (err) {
2341 +               pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2342 +               goto unregister_5;
2343 +       }
2344 +
2345 +       err = platform_driver_register(&bcm2835_alsa7_driver);
2346 +       if (err) {
2347 +               pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
2348 +               goto unregister_6;
2349 +       }
2350 +
2351 +       return 0;
2352 +
2353 +unregister_6:
2354 +       platform_driver_unregister(&bcm2835_alsa6_driver);
2355 +unregister_5:
2356 +       platform_driver_unregister(&bcm2835_alsa5_driver);
2357 +unregister_4:
2358 +       platform_driver_unregister(&bcm2835_alsa4_driver);
2359 +unregister_3:
2360 +       platform_driver_unregister(&bcm2835_alsa3_driver);
2361 +unregister_2:
2362 +       platform_driver_unregister(&bcm2835_alsa2_driver);
2363 +unregister_1:
2364 +       platform_driver_unregister(&bcm2835_alsa1_driver);
2365 +unregister_0:
2366 +       platform_driver_unregister(&bcm2835_alsa0_driver);
2367 +out:
2368 +       return err;
2369 +}
2370 +
2371 +static void bcm2835_alsa_device_exit(void)
2372 +{
2373 +       platform_driver_unregister(&bcm2835_alsa0_driver);
2374 +       platform_driver_unregister(&bcm2835_alsa1_driver);
2375 +       platform_driver_unregister(&bcm2835_alsa2_driver);
2376 +       platform_driver_unregister(&bcm2835_alsa3_driver);
2377 +       platform_driver_unregister(&bcm2835_alsa4_driver);
2378 +       platform_driver_unregister(&bcm2835_alsa5_driver);
2379 +       platform_driver_unregister(&bcm2835_alsa6_driver);
2380 +       platform_driver_unregister(&bcm2835_alsa7_driver);
2381 +}
2382 +
2383 +late_initcall(bcm2835_alsa_device_init);
2384 +module_exit(bcm2835_alsa_device_exit);
2385 +
2386 +MODULE_AUTHOR("Dom Cobley");
2387 +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
2388 +MODULE_LICENSE("GPL");
2389 +MODULE_ALIAS("platform:bcm2835_alsa");
2390 --- /dev/null
2391 +++ b/sound/arm/bcm2835.h
2392 @@ -0,0 +1,167 @@
2393 +/*****************************************************************************
2394 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
2395 +*
2396 +* Unless you and Broadcom execute a separate written software license
2397 +* agreement governing use of this software, this software is licensed to you
2398 +* under the terms of the GNU General Public License version 2, available at
2399 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
2400 +*
2401 +* Notwithstanding the above, under no circumstances may you combine this
2402 +* software in any way with any other Broadcom software provided under a
2403 +* license other than the GPL, without Broadcom's express prior written
2404 +* consent.
2405 +*****************************************************************************/
2406 +
2407 +#ifndef __SOUND_ARM_BCM2835_H
2408 +#define __SOUND_ARM_BCM2835_H
2409 +
2410 +#include <linux/device.h>
2411 +#include <linux/list.h>
2412 +#include <linux/interrupt.h>
2413 +#include <linux/wait.h>
2414 +#include <sound/core.h>
2415 +#include <sound/initval.h>
2416 +#include <sound/pcm.h>
2417 +#include <sound/pcm_params.h>
2418 +#include <sound/pcm-indirect.h>
2419 +#include <linux/workqueue.h>
2420 +
2421 +/*
2422 +#define AUDIO_DEBUG_ENABLE
2423 +#define AUDIO_VERBOSE_DEBUG_ENABLE
2424 +*/
2425 +
2426 +/* Debug macros */
2427 +
2428 +#ifdef AUDIO_DEBUG_ENABLE
2429 +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
2430 +
2431 +#define audio_debug(fmt, arg...)       \
2432 +       printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
2433 +
2434 +#define audio_info(fmt, arg...)        \
2435 +       printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
2436 +
2437 +#else
2438 +
2439 +#define audio_debug(fmt, arg...)
2440 +
2441 +#define audio_info(fmt, arg...)
2442 +
2443 +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
2444 +
2445 +#else
2446 +
2447 +#define audio_debug(fmt, arg...)
2448 +
2449 +#define audio_info(fmt, arg...)
2450 +
2451 +#endif /* AUDIO_DEBUG_ENABLE */
2452 +
2453 +#define audio_error(fmt, arg...)       \
2454 +       printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
2455 +
2456 +#define audio_warning(fmt, arg...)     \
2457 +       printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
2458 +
2459 +#define audio_alert(fmt, arg...)       \
2460 +       printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
2461 +
2462 +#define MAX_SUBSTREAMS                 (8)
2463 +#define AVAIL_SUBSTREAMS_MASK          (0xff)
2464 +enum {
2465 +       CTRL_VOL_MUTE,
2466 +       CTRL_VOL_UNMUTE
2467 +};
2468 +
2469 +/* macros for alsa2chip and chip2alsa, instead of functions */
2470 +
2471 +#define alsa2chip(vol) (uint)(-((vol << 8) / 100))     /* convert alsa to chip volume (defined as macro rather than function call) */
2472 +#define chip2alsa(vol) -((vol * 100) >> 8)                     /* convert chip to alsa volume */
2473 +
2474 +/* Some constants for values .. */
2475 +typedef enum {
2476 +       AUDIO_DEST_AUTO = 0,
2477 +       AUDIO_DEST_HEADPHONES = 1,
2478 +       AUDIO_DEST_HDMI = 2,
2479 +       AUDIO_DEST_MAX,
2480 +} SND_BCM2835_ROUTE_T;
2481 +
2482 +typedef enum {
2483 +       PCM_PLAYBACK_VOLUME,
2484 +       PCM_PLAYBACK_MUTE,
2485 +       PCM_PLAYBACK_DEVICE,
2486 +} SND_BCM2835_CTRL_T;
2487 +
2488 +/* definition of the chip-specific record */
2489 +typedef struct bcm2835_chip {
2490 +       struct snd_card *card;
2491 +       struct snd_pcm *pcm;
2492 +       struct snd_pcm *pcm_spdif;
2493 +       /* Bitmat for valid reg_base and irq numbers */
2494 +       uint32_t avail_substreams;
2495 +       struct platform_device *pdev[MAX_SUBSTREAMS];
2496 +       struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
2497 +
2498 +       int volume;
2499 +       int old_volume; /* stores the volume value whist muted */
2500 +       int dest;
2501 +       int mute;
2502 +
2503 +       unsigned int opened;
2504 +       unsigned int spdif_status;
2505 +       struct mutex audio_mutex;
2506 +} bcm2835_chip_t;
2507 +
2508 +typedef struct bcm2835_alsa_stream {
2509 +       bcm2835_chip_t *chip;
2510 +       struct snd_pcm_substream *substream;
2511 +       struct snd_pcm_indirect pcm_indirect;
2512 +
2513 +       struct semaphore buffers_update_sem;
2514 +       struct semaphore control_sem;
2515 +       spinlock_t lock;
2516 +       volatile uint32_t control;
2517 +       volatile uint32_t status;
2518 +
2519 +       int open;
2520 +       int running;
2521 +       int draining;
2522 +
2523 +       int channels;
2524 +       int params_rate;
2525 +       int pcm_format_width;
2526 +
2527 +       unsigned int pos;
2528 +       unsigned int buffer_size;
2529 +       unsigned int period_size;
2530 +
2531 +       uint32_t enable_fifo_irq;
2532 +       irq_handler_t fifo_irq_handler;
2533 +
2534 +       atomic_t retrieved;
2535 +       struct opaque_AUDIO_INSTANCE_T *instance;
2536 +       struct workqueue_struct *my_wq;
2537 +       int idx;
2538 +} bcm2835_alsa_stream_t;
2539 +
2540 +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
2541 +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
2542 +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip);
2543 +
2544 +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
2545 +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
2546 +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
2547 +                            uint32_t channels, uint32_t samplerate,
2548 +                            uint32_t bps);
2549 +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
2550 +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
2551 +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
2552 +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
2553 +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
2554 +                       void *src);
2555 +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
2556 +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
2557 +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
2558 +
2559 +#endif /* __SOUND_ARM_BCM2835_H */
2560 --- /dev/null
2561 +++ b/sound/arm/vc_vchi_audioserv_defs.h
2562 @@ -0,0 +1,116 @@
2563 +/*****************************************************************************
2564 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
2565 +*
2566 +* Unless you and Broadcom execute a separate written software license
2567 +* agreement governing use of this software, this software is licensed to you
2568 +* under the terms of the GNU General Public License version 2, available at
2569 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
2570 +*
2571 +* Notwithstanding the above, under no circumstances may you combine this
2572 +* software in any way with any other Broadcom software provided under a
2573 +* license other than the GPL, without Broadcom's express prior written
2574 +* consent.
2575 +*****************************************************************************/
2576 +
2577 +#ifndef _VC_AUDIO_DEFS_H_
2578 +#define _VC_AUDIO_DEFS_H_
2579 +
2580 +#define VC_AUDIOSERV_MIN_VER 1
2581 +#define VC_AUDIOSERV_VER 2
2582 +
2583 +// FourCC code used for VCHI connection
2584 +#define VC_AUDIO_SERVER_NAME  MAKE_FOURCC("AUDS")
2585 +
2586 +// Maximum message length
2587 +#define VC_AUDIO_MAX_MSG_LEN  (sizeof( VC_AUDIO_MSG_T ))
2588 +
2589 +// List of screens that are currently supported
2590 +// All message types supported for HOST->VC direction
2591 +typedef enum {
2592 +       VC_AUDIO_MSG_TYPE_RESULT,       // Generic result
2593 +       VC_AUDIO_MSG_TYPE_COMPLETE,     // Generic result
2594 +       VC_AUDIO_MSG_TYPE_CONFIG,       // Configure audio
2595 +       VC_AUDIO_MSG_TYPE_CONTROL,      // Configure audio
2596 +       VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
2597 +       VC_AUDIO_MSG_TYPE_CLOSE,        // Configure audio
2598 +       VC_AUDIO_MSG_TYPE_START,        // Configure audio
2599 +       VC_AUDIO_MSG_TYPE_STOP, // Configure audio
2600 +       VC_AUDIO_MSG_TYPE_WRITE,        // Configure audio
2601 +       VC_AUDIO_MSG_TYPE_MAX
2602 +} VC_AUDIO_MSG_TYPE;
2603 +
2604 +// configure the audio
2605 +typedef struct {
2606 +       uint32_t channels;
2607 +       uint32_t samplerate;
2608 +       uint32_t bps;
2609 +
2610 +} VC_AUDIO_CONFIG_T;
2611 +
2612 +typedef struct {
2613 +       uint32_t volume;
2614 +       uint32_t dest;
2615 +
2616 +} VC_AUDIO_CONTROL_T;
2617 +
2618 +// audio
2619 +typedef struct {
2620 +       uint32_t dummy;
2621 +
2622 +} VC_AUDIO_OPEN_T;
2623 +
2624 +// audio
2625 +typedef struct {
2626 +       uint32_t dummy;
2627 +
2628 +} VC_AUDIO_CLOSE_T;
2629 +// audio
2630 +typedef struct {
2631 +       uint32_t dummy;
2632 +
2633 +} VC_AUDIO_START_T;
2634 +// audio
2635 +typedef struct {
2636 +       uint32_t draining;
2637 +
2638 +} VC_AUDIO_STOP_T;
2639 +
2640 +// configure the write audio samples
2641 +typedef struct {
2642 +       uint32_t count;         // in bytes
2643 +       void *callback;
2644 +       void *cookie;
2645 +       uint16_t silence;
2646 +       uint16_t max_packet;
2647 +} VC_AUDIO_WRITE_T;
2648 +
2649 +// Generic result for a request (VC->HOST)
2650 +typedef struct {
2651 +       int32_t success;        // Success value
2652 +
2653 +} VC_AUDIO_RESULT_T;
2654 +
2655 +// Generic result for a request (VC->HOST)
2656 +typedef struct {
2657 +       int32_t count;          // Success value
2658 +       void *callback;
2659 +       void *cookie;
2660 +} VC_AUDIO_COMPLETE_T;
2661 +
2662 +// Message header for all messages in HOST->VC direction
2663 +typedef struct {
2664 +       int32_t type;           // Message type (VC_AUDIO_MSG_TYPE)
2665 +       union {
2666 +               VC_AUDIO_CONFIG_T config;
2667 +               VC_AUDIO_CONTROL_T control;
2668 +               VC_AUDIO_OPEN_T open;
2669 +               VC_AUDIO_CLOSE_T close;
2670 +               VC_AUDIO_START_T start;
2671 +               VC_AUDIO_STOP_T stop;
2672 +               VC_AUDIO_WRITE_T write;
2673 +               VC_AUDIO_RESULT_T result;
2674 +               VC_AUDIO_COMPLETE_T complete;
2675 +       } u;
2676 +} VC_AUDIO_MSG_T;
2677 +
2678 +#endif // _VC_AUDIO_DEFS_H_