1 From 55349969a1914b515956c14f3fafd25df5c827a9 Mon Sep 17 00:00:00 2001
2 From: wm4 <wm4@nowhere>
3 Date: Wed, 13 Jan 2016 19:43:12 +0100
4 Subject: [PATCH] bcm2835: implement channel map API
6 Report all layouts supported by the HDMI protocol to userspace.
7 Make the videocore set the correct layout according to the
10 Some code taken from patch_hdmi.c. In particular, the HDMI channel
11 layout table was copied without changes - with the idea in mind that
12 hopefully it can be shared one day. Or at least updating it will be
15 In my tests, everything appears to work, except when outputting
16 FL FR RL RR. Then my receiver outputs RL on both the RL and RR
17 speakers, while RR is never heard.
19 sound/arm/bcm2835-ctl.c | 276 ++++++++++++++++++++++++++++++++++++++++++++++
20 sound/arm/bcm2835-pcm.c | 22 +++-
21 sound/arm/bcm2835-vchiq.c | 13 +++
22 sound/arm/bcm2835.h | 4 +
23 4 files changed, 311 insertions(+), 4 deletions(-)
25 --- a/sound/arm/bcm2835-ctl.c
26 +++ b/sound/arm/bcm2835-ctl.c
27 @@ -300,6 +300,281 @@ static struct snd_kcontrol_new snd_bcm28
31 +struct cea_channel_speaker_allocation {
36 +#define FL SNDRV_CHMAP_FL
37 +#define FR SNDRV_CHMAP_FR
38 +#define RL SNDRV_CHMAP_RL
39 +#define RR SNDRV_CHMAP_RR
40 +#define LFE SNDRV_CHMAP_LFE
41 +#define FC SNDRV_CHMAP_FC
42 +#define RLC SNDRV_CHMAP_RLC
43 +#define RRC SNDRV_CHMAP_RRC
44 +#define RC SNDRV_CHMAP_RC
45 +#define FLC SNDRV_CHMAP_FLC
46 +#define FRC SNDRV_CHMAP_FRC
47 +#define FLH SNDRV_CHMAP_TFL
48 +#define FRH SNDRV_CHMAP_TFR
49 +#define FLW SNDRV_CHMAP_FLW
50 +#define FRW SNDRV_CHMAP_FRW
51 +#define TC SNDRV_CHMAP_TC
52 +#define FCH SNDRV_CHMAP_TFC
55 + * CEA-861 channel maps
57 + * Stolen from sound/pci/hda/patch_hdmi.c
58 + * (unlike the source, this uses SNDRV_* constants directly, as by the
59 + * map_tables array in patch_hdmi.c)
60 + * Unknown entries use 0, which unfortunately is SNDRV_CHMAP_UNKNOWN instead
61 + * of SNDRV_CHMAP_NA.
63 +static struct cea_channel_speaker_allocation channel_allocations[] = {
64 +/* channel: 7 6 5 4 3 2 1 0 */
65 +{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
67 +{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
68 + /* Dolby Surround */
69 +{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
71 +{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
73 +{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
75 +{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
77 +{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
79 +{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
81 +{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
83 +{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
84 +{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
85 +{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
86 +{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
87 +{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
88 +{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
89 +{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
90 +{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
91 +{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
92 +{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
93 +{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
94 +{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
95 +{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
96 +{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
97 +{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
98 +{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
99 +{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
100 +{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
101 +{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
102 +{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
103 +{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
104 +{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
105 +{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
106 +{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
107 +{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
108 +{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
109 +{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
110 +{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
111 +{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
112 +{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
113 +{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
114 +{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
115 +{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
116 +{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
117 +{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
118 +{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
119 +{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
120 +{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
121 +{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
122 +{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
123 +{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
126 +static int snd_bcm2835_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
127 + unsigned int size, unsigned int __user *tlv)
129 + unsigned int __user *dst;
135 + if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
139 + for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
140 + struct cea_channel_speaker_allocation *ch = &channel_allocations[i];
145 + for (c = 0; c < 8; c++) {
146 + if (ch->speakers[c])
150 + chs_bytes = num_chs * 4;
153 + if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
154 + put_user(chs_bytes, dst + 1))
159 + if (size < chs_bytes)
162 + count += chs_bytes;
163 + for (c = 0; c < 8; c++) {
164 + int sp = ch->speakers[7 - c];
166 + if (put_user(sp, dst))
172 + if (put_user(count, tlv + 1))
177 +static int snd_bcm2835_chmap_ctl_get(struct snd_kcontrol *kcontrol,
178 + struct snd_ctl_elem_value *ucontrol)
180 + struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
181 + bcm2835_chip_t *chip = info->private_data;
182 + unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
183 + struct snd_pcm_substream *substream = snd_pcm_chmap_substream(info, idx);
184 + struct cea_channel_speaker_allocation *ch = NULL;
188 + if (!substream || !substream->runtime)
191 + for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
192 + if (channel_allocations[i].ca_index == chip->cea_chmap)
193 + ch = &channel_allocations[i];
196 + /* If no layout was set yet, return a dummy. Apparently the userspace
197 + * API will be confused if we don't. */
199 + ch = &channel_allocations[0];
201 + for (i = 0; i < 8; i++) {
202 + if (ch->speakers[7 - i])
203 + ucontrol->value.integer.value[cur++] = ch->speakers[7 - i];
206 + ucontrol->value.integer.value[cur++] = SNDRV_CHMAP_NA;
210 +static int snd_bcm2835_chmap_ctl_put(struct snd_kcontrol *kcontrol,
211 + struct snd_ctl_elem_value *ucontrol)
213 + struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
214 + bcm2835_chip_t *chip = info->private_data;
215 + unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
216 + struct snd_pcm_substream *substream = snd_pcm_chmap_substream(info, idx);
217 + int i, prepared = 0, cea_chmap = -1;
220 + if (!substream || !substream->runtime)
223 + switch (substream->runtime->status->state) {
224 + case SNDRV_PCM_STATE_OPEN:
225 + case SNDRV_PCM_STATE_SETUP:
227 + case SNDRV_PCM_STATE_PREPARED:
234 + for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
235 + struct cea_channel_speaker_allocation *ch = &channel_allocations[i];
239 + memset(remap, 0, sizeof(remap));
240 + for (x = 0; x < substream->runtime->channels; x++) {
241 + int sp = ucontrol->value.integer.value[x];
242 + while (cur < 8 && !ch->speakers[7 - cur])
245 + /* user has more channels than ch */
249 + if (ch->speakers[7 - cur] != sp) {
256 + for (x = cur; x < 8; x++) {
257 + if (ch->speakers[7 - x]) {
258 + /* ch has more channels than user */
264 + cea_chmap = ch->ca_index;
272 + /* don't change the layout if another substream is active */
273 + if (chip->opened != (1 << substream->number) && chip->cea_chmap != cea_chmap)
274 + return -EBUSY; /* unsure whether this is a good error code */
276 + chip->cea_chmap = cea_chmap;
277 + for (i = 0; i < 8; i++)
278 + chip->map_channels[i] = remap[i];
280 + snd_bcm2835_pcm_prepare_again(substream);
284 +static int snd_bcm2835_add_chmap_ctl(bcm2835_chip_t * chip)
286 + struct snd_pcm_chmap *chmap;
287 + struct snd_kcontrol *kctl;
290 + err = snd_pcm_add_chmap_ctls(chip->pcm,
291 + SNDRV_PCM_STREAM_PLAYBACK,
292 + NULL, 8, 0, &chmap);
295 + /* override handlers */
296 + chmap->private_data = chip;
297 + kctl = chmap->kctl;
298 + for (i = 0; i < kctl->count; i++)
299 + kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
300 + kctl->get = snd_bcm2835_chmap_ctl_get;
301 + kctl->put = snd_bcm2835_chmap_ctl_put;
302 + kctl->tlv.c = snd_bcm2835_chmap_ctl_tlv;
306 int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
309 @@ -313,6 +588,7 @@ int snd_bcm2835_new_ctl(bcm2835_chip_t *
313 + snd_bcm2835_add_chmap_ctl(chip);
314 for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) {
315 err = snd_ctl_add(chip->card,
316 snd_ctl_new1(&snd_bcm2835_spdif[idx], chip));
317 --- a/sound/arm/bcm2835-pcm.c
318 +++ b/sound/arm/bcm2835-pcm.c
319 @@ -231,6 +231,9 @@ static int snd_bcm2835_playback_open_gen
321 chip->alsa_stream[idx] = alsa_stream;
324 + chip->cea_chmap = -1;
326 chip->opened |= (1 << idx);
327 alsa_stream->open = 1;
328 alsa_stream->draining = 1;
329 @@ -341,8 +344,7 @@ static int snd_bcm2835_pcm_hw_free(struc
330 return snd_pcm_lib_free_pages(substream);
333 -/* prepare callback */
334 -static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
335 +int snd_bcm2835_pcm_prepare_again(struct snd_pcm_substream *substream)
337 bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
338 struct snd_pcm_runtime *runtime = substream->runtime;
339 @@ -350,8 +352,6 @@ static int snd_bcm2835_pcm_prepare(struc
343 - audio_info(" .. IN\n");
345 /* notify the vchiq that it should enter spdif passthrough mode by
346 * setting channels=0 (see
347 * https://github.com/raspberrypi/linux/issues/528) */
348 @@ -367,6 +367,20 @@ static int snd_bcm2835_pcm_prepare(struc
349 audio_error(" error setting hw params\n");
355 +/* prepare callback */
356 +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
358 + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
359 + struct snd_pcm_runtime *runtime = substream->runtime;
360 + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
362 + audio_info(" .. IN\n");
364 + snd_bcm2835_pcm_prepare_again(substream);
366 bcm2835_audio_setup(alsa_stream);
368 /* in preparation of the stream, set the controls (volume level) of the stream */
369 --- a/sound/arm/bcm2835-vchiq.c
370 +++ b/sound/arm/bcm2835-vchiq.c
371 @@ -570,6 +570,8 @@ int bcm2835_audio_set_params(bcm2835_als
373 AUDIO_INSTANCE_T *instance = alsa_stream->instance;
375 + uint32_t chmap_value;
380 @@ -593,10 +595,21 @@ int bcm2835_audio_set_params(bcm2835_als
382 instance->result = -1;
384 + if (alsa_stream->chip->cea_chmap >= 0) {
385 + chmap_value = (unsigned)alsa_stream->chip->cea_chmap << 24;
387 + chmap_value = 0; /* force stereo */
388 + for (i = 0; i < 8; i++)
389 + alsa_stream->chip->map_channels[i] = i;
391 + for (i = 0; i < 8; i++)
392 + chmap_value |= alsa_stream->chip->map_channels[i] << (i * 3);
394 m.type = VC_AUDIO_MSG_TYPE_CONFIG;
395 m.u.config.channels = channels;
396 m.u.config.samplerate = samplerate;
397 m.u.config.bps = bps;
398 + m.u.config.channelmap = chmap_value;
400 /* Create the message available completion */
401 init_completion(&instance->msg_avail_comp);
402 --- a/sound/arm/bcm2835.h
403 +++ b/sound/arm/bcm2835.h
404 @@ -107,6 +107,8 @@ typedef struct bcm2835_chip {
405 int old_volume; /* stores the volume value whist muted */
408 + int cea_chmap; /* currently requested Audio InfoFrame Data Byte 4 */
409 + int map_channels[8];
412 unsigned int spdif_status;
413 @@ -149,6 +151,8 @@ int snd_bcm2835_new_ctl(bcm2835_chip_t *
414 int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
415 int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip);
417 +int snd_bcm2835_pcm_prepare_again(struct snd_pcm_substream *substream);
419 int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
420 int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
421 int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,