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