xref: /linux/sound/hda/core/hdmi_chmap.c (revision 2aa0a3691711257b888d6024a492eb9fd1e8f52a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HDMI Channel map support helpers
4  */
5 
6 #include <linux/module.h>
7 #include <sound/control.h>
8 #include <sound/tlv.h>
9 #include <sound/hda_chmap.h>
10 
11 /*
12  * CEA speaker placement:
13  *
14  *        FLH       FCH        FRH
15  *  FLW    FL  FLC   FC   FRC   FR   FRW
16  *
17  *                                  LFE
18  *                     TC
19  *
20  *          RL  RLC   RC   RRC   RR
21  *
22  * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
23  * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
24  */
25 enum cea_speaker_placement {
26 	FL  = (1 <<  0),	/* Front Left           */
27 	FC  = (1 <<  1),	/* Front Center         */
28 	FR  = (1 <<  2),	/* Front Right          */
29 	FLC = (1 <<  3),	/* Front Left Center    */
30 	FRC = (1 <<  4),	/* Front Right Center   */
31 	RL  = (1 <<  5),	/* Rear Left            */
32 	RC  = (1 <<  6),	/* Rear Center          */
33 	RR  = (1 <<  7),	/* Rear Right           */
34 	RLC = (1 <<  8),	/* Rear Left Center     */
35 	RRC = (1 <<  9),	/* Rear Right Center    */
36 	LFE = (1 << 10),	/* Low Frequency Effect */
37 	FLW = (1 << 11),	/* Front Left Wide      */
38 	FRW = (1 << 12),	/* Front Right Wide     */
39 	FLH = (1 << 13),	/* Front Left High      */
40 	FCH = (1 << 14),	/* Front Center High    */
41 	FRH = (1 << 15),	/* Front Right High     */
42 	TC  = (1 << 16),	/* Top Center           */
43 };
44 
45 static const char * const cea_speaker_allocation_names[] = {
46 	/*  0 */ "FL/FR",
47 	/*  1 */ "LFE",
48 	/*  2 */ "FC",
49 	/*  3 */ "RL/RR",
50 	/*  4 */ "RC",
51 	/*  5 */ "FLC/FRC",
52 	/*  6 */ "RLC/RRC",
53 	/*  7 */ "FLW/FRW",
54 	/*  8 */ "FLH/FRH",
55 	/*  9 */ "TC",
56 	/* 10 */ "FCH",
57 };
58 
59 /*
60  * ELD SA bits in the CEA Speaker Allocation data block
61  */
62 static const int eld_speaker_allocation_bits[] = {
63 	[0] = FL | FR,
64 	[1] = LFE,
65 	[2] = FC,
66 	[3] = RL | RR,
67 	[4] = RC,
68 	[5] = FLC | FRC,
69 	[6] = RLC | RRC,
70 	/* the following are not defined in ELD yet */
71 	[7] = FLW | FRW,
72 	[8] = FLH | FRH,
73 	[9] = TC,
74 	[10] = FCH,
75 };
76 
77 /*
78  * ALSA sequence is:
79  *
80  *       surround40   surround41   surround50   surround51   surround71
81  * ch0   front left   =            =            =            =
82  * ch1   front right  =            =            =            =
83  * ch2   rear left    =            =            =            =
84  * ch3   rear right   =            =            =            =
85  * ch4                LFE          center       center       center
86  * ch5                                          LFE          LFE
87  * ch6                                                       side left
88  * ch7                                                       side right
89  *
90  * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
91  */
92 static int hdmi_channel_mapping[0x32][8] = {
93 	/* stereo */
94 	[0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
95 	/* 2.1 */
96 	[0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
97 	/* Dolby Surround */
98 	[0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
99 	/* surround40 */
100 	[0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
101 	/* 4ch */
102 	[0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
103 	/* surround41 */
104 	[0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
105 	/* surround50 */
106 	[0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
107 	/* surround51 */
108 	[0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
109 	/* 7.1 */
110 	[0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
111 };
112 
113 /*
114  * This is an ordered list!
115  *
116  * The preceding ones have better chances to be selected by
117  * hdmi_channel_allocation().
118  */
119 static struct hdac_cea_channel_speaker_allocation channel_allocations[] = {
120 /*			  channel:   7     6    5    4    3     2    1    0  */
121 { .ca_index = 0x00,  .speakers = {   0,    0,   0,   0,   0,    0,  FR,  FL } },
122 				 /* 2.1 */
123 { .ca_index = 0x01,  .speakers = {   0,    0,   0,   0,   0,  LFE,  FR,  FL } },
124 				 /* Dolby Surround */
125 { .ca_index = 0x02,  .speakers = {   0,    0,   0,   0,  FC,    0,  FR,  FL } },
126 				 /* surround40 */
127 { .ca_index = 0x08,  .speakers = {   0,    0,  RR,  RL,   0,    0,  FR,  FL } },
128 				 /* surround41 */
129 { .ca_index = 0x09,  .speakers = {   0,    0,  RR,  RL,   0,  LFE,  FR,  FL } },
130 				 /* surround50 */
131 { .ca_index = 0x0a,  .speakers = {   0,    0,  RR,  RL,  FC,    0,  FR,  FL } },
132 				 /* surround51 */
133 { .ca_index = 0x0b,  .speakers = {   0,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
134 				 /* 6.1 */
135 { .ca_index = 0x0f,  .speakers = {   0,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
136 				 /* surround71 */
137 { .ca_index = 0x13,  .speakers = { RRC,  RLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
138 
139 { .ca_index = 0x03,  .speakers = {   0,    0,   0,   0,  FC,  LFE,  FR,  FL } },
140 { .ca_index = 0x04,  .speakers = {   0,    0,   0,  RC,   0,    0,  FR,  FL } },
141 { .ca_index = 0x05,  .speakers = {   0,    0,   0,  RC,   0,  LFE,  FR,  FL } },
142 { .ca_index = 0x06,  .speakers = {   0,    0,   0,  RC,  FC,    0,  FR,  FL } },
143 { .ca_index = 0x07,  .speakers = {   0,    0,   0,  RC,  FC,  LFE,  FR,  FL } },
144 { .ca_index = 0x0c,  .speakers = {   0,   RC,  RR,  RL,   0,    0,  FR,  FL } },
145 { .ca_index = 0x0d,  .speakers = {   0,   RC,  RR,  RL,   0,  LFE,  FR,  FL } },
146 { .ca_index = 0x0e,  .speakers = {   0,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
147 { .ca_index = 0x10,  .speakers = { RRC,  RLC,  RR,  RL,   0,    0,  FR,  FL } },
148 { .ca_index = 0x11,  .speakers = { RRC,  RLC,  RR,  RL,   0,  LFE,  FR,  FL } },
149 { .ca_index = 0x12,  .speakers = { RRC,  RLC,  RR,  RL,  FC,    0,  FR,  FL } },
150 { .ca_index = 0x14,  .speakers = { FRC,  FLC,   0,   0,   0,    0,  FR,  FL } },
151 { .ca_index = 0x15,  .speakers = { FRC,  FLC,   0,   0,   0,  LFE,  FR,  FL } },
152 { .ca_index = 0x16,  .speakers = { FRC,  FLC,   0,   0,  FC,    0,  FR,  FL } },
153 { .ca_index = 0x17,  .speakers = { FRC,  FLC,   0,   0,  FC,  LFE,  FR,  FL } },
154 { .ca_index = 0x18,  .speakers = { FRC,  FLC,   0,  RC,   0,    0,  FR,  FL } },
155 { .ca_index = 0x19,  .speakers = { FRC,  FLC,   0,  RC,   0,  LFE,  FR,  FL } },
156 { .ca_index = 0x1a,  .speakers = { FRC,  FLC,   0,  RC,  FC,    0,  FR,  FL } },
157 { .ca_index = 0x1b,  .speakers = { FRC,  FLC,   0,  RC,  FC,  LFE,  FR,  FL } },
158 { .ca_index = 0x1c,  .speakers = { FRC,  FLC,  RR,  RL,   0,    0,  FR,  FL } },
159 { .ca_index = 0x1d,  .speakers = { FRC,  FLC,  RR,  RL,   0,  LFE,  FR,  FL } },
160 { .ca_index = 0x1e,  .speakers = { FRC,  FLC,  RR,  RL,  FC,    0,  FR,  FL } },
161 { .ca_index = 0x1f,  .speakers = { FRC,  FLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
162 { .ca_index = 0x20,  .speakers = {   0,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
163 { .ca_index = 0x21,  .speakers = {   0,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
164 { .ca_index = 0x22,  .speakers = {  TC,    0,  RR,  RL,  FC,    0,  FR,  FL } },
165 { .ca_index = 0x23,  .speakers = {  TC,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
166 { .ca_index = 0x24,  .speakers = { FRH,  FLH,  RR,  RL,   0,    0,  FR,  FL } },
167 { .ca_index = 0x25,  .speakers = { FRH,  FLH,  RR,  RL,   0,  LFE,  FR,  FL } },
168 { .ca_index = 0x26,  .speakers = { FRW,  FLW,  RR,  RL,   0,    0,  FR,  FL } },
169 { .ca_index = 0x27,  .speakers = { FRW,  FLW,  RR,  RL,   0,  LFE,  FR,  FL } },
170 { .ca_index = 0x28,  .speakers = {  TC,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
171 { .ca_index = 0x29,  .speakers = {  TC,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
172 { .ca_index = 0x2a,  .speakers = { FCH,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
173 { .ca_index = 0x2b,  .speakers = { FCH,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
174 { .ca_index = 0x2c,  .speakers = {  TC,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
175 { .ca_index = 0x2d,  .speakers = {  TC,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
176 { .ca_index = 0x2e,  .speakers = { FRH,  FLH,  RR,  RL,  FC,    0,  FR,  FL } },
177 { .ca_index = 0x2f,  .speakers = { FRH,  FLH,  RR,  RL,  FC,  LFE,  FR,  FL } },
178 { .ca_index = 0x30,  .speakers = { FRW,  FLW,  RR,  RL,  FC,    0,  FR,  FL } },
179 { .ca_index = 0x31,  .speakers = { FRW,  FLW,  RR,  RL,  FC,  LFE,  FR,  FL } },
180 };
181 
182 static int hdmi_pin_set_slot_channel(struct hdac_device *codec,
183 		hda_nid_t pin_nid, int asp_slot, int channel)
184 {
185 	return snd_hdac_codec_write(codec, pin_nid, 0,
186 				AC_VERB_SET_HDMI_CHAN_SLOT,
187 				(channel << 4) | asp_slot);
188 }
189 
190 static int hdmi_pin_get_slot_channel(struct hdac_device *codec,
191 			hda_nid_t pin_nid, int asp_slot)
192 {
193 	return (snd_hdac_codec_read(codec, pin_nid, 0,
194 				   AC_VERB_GET_HDMI_CHAN_SLOT,
195 				   asp_slot) & 0xf0) >> 4;
196 }
197 
198 static int hdmi_get_channel_count(struct hdac_device *codec, hda_nid_t cvt_nid)
199 {
200 	return 1 + snd_hdac_codec_read(codec, cvt_nid, 0,
201 					AC_VERB_GET_CVT_CHAN_COUNT, 0);
202 }
203 
204 static void hdmi_set_channel_count(struct hdac_device *codec,
205 				   hda_nid_t cvt_nid, int chs)
206 {
207 	if (chs != hdmi_get_channel_count(codec, cvt_nid))
208 		snd_hdac_codec_write(codec, cvt_nid, 0,
209 				    AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
210 }
211 
212 /*
213  * Channel mapping routines
214  */
215 
216 /*
217  * Compute derived values in channel_allocations[].
218  */
219 static void init_channel_allocations(void)
220 {
221 	int i, j;
222 	struct hdac_cea_channel_speaker_allocation *p;
223 
224 	for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
225 		p = channel_allocations + i;
226 		p->channels = 0;
227 		p->spk_mask = 0;
228 		for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
229 			if (p->speakers[j]) {
230 				p->channels++;
231 				p->spk_mask |= p->speakers[j];
232 			}
233 	}
234 }
235 
236 static int get_channel_allocation_order(int ca)
237 {
238 	int i;
239 
240 	for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
241 		if (channel_allocations[i].ca_index == ca)
242 			break;
243 	}
244 	return i;
245 }
246 
247 void snd_hdac_print_channel_allocation(int spk_alloc, char *buf, int buflen)
248 {
249 	int i, j;
250 
251 	for (i = 0, j = 0; i < ARRAY_SIZE(cea_speaker_allocation_names); i++) {
252 		if (spk_alloc & (1 << i))
253 			j += scnprintf(buf + j, buflen - j,  " %s",
254 					cea_speaker_allocation_names[i]);
255 	}
256 	buf[j] = '\0';	/* necessary when j == 0 */
257 }
258 EXPORT_SYMBOL_GPL(snd_hdac_print_channel_allocation);
259 
260 /*
261  * The transformation takes two steps:
262  *
263  *	eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
264  *	      spk_mask => (channel_allocations[])         => ai->CA
265  *
266  * TODO: it could select the wrong CA from multiple candidates.
267 */
268 static int hdmi_channel_allocation_spk_alloc_blk(struct hdac_device *codec,
269 				   int spk_alloc, int channels)
270 {
271 	int i;
272 	int ca = 0;
273 	int spk_mask = 0;
274 	char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
275 
276 	/*
277 	 * CA defaults to 0 for basic stereo audio
278 	 */
279 	if (channels <= 2)
280 		return 0;
281 
282 	/*
283 	 * expand ELD's speaker allocation mask
284 	 *
285 	 * ELD tells the speaker mask in a compact(paired) form,
286 	 * expand ELD's notions to match the ones used by Audio InfoFrame.
287 	 */
288 	for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
289 		if (spk_alloc & (1 << i))
290 			spk_mask |= eld_speaker_allocation_bits[i];
291 	}
292 
293 	/* search for the first working match in the CA table */
294 	for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
295 		if (channels == channel_allocations[i].channels &&
296 		    (spk_mask & channel_allocations[i].spk_mask) ==
297 				channel_allocations[i].spk_mask) {
298 			ca = channel_allocations[i].ca_index;
299 			break;
300 		}
301 	}
302 
303 	if (!ca) {
304 		/*
305 		 * if there was no match, select the regular ALSA channel
306 		 * allocation with the matching number of channels
307 		 */
308 		for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
309 			if (channels == channel_allocations[i].channels) {
310 				ca = channel_allocations[i].ca_index;
311 				break;
312 			}
313 		}
314 	}
315 
316 	snd_hdac_print_channel_allocation(spk_alloc, buf, sizeof(buf));
317 	dev_dbg(&codec->dev, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
318 		    ca, channels, buf);
319 
320 	return ca;
321 }
322 
323 static void hdmi_debug_channel_mapping(struct hdac_chmap *chmap,
324 				       hda_nid_t pin_nid)
325 {
326 #ifdef CONFIG_SND_DEBUG_VERBOSE
327 	int i;
328 	int channel;
329 
330 	for (i = 0; i < 8; i++) {
331 		channel = chmap->ops.pin_get_slot_channel(
332 				chmap->hdac, pin_nid, i);
333 		dev_dbg(&chmap->hdac->dev, "HDMI: ASP channel %d => slot %d\n",
334 						channel, i);
335 	}
336 #endif
337 }
338 
339 static void hdmi_std_setup_channel_mapping(struct hdac_chmap *chmap,
340 				       hda_nid_t pin_nid,
341 				       bool non_pcm,
342 				       int ca)
343 {
344 	struct hdac_cea_channel_speaker_allocation *ch_alloc;
345 	int i;
346 	int err;
347 	int order;
348 	int non_pcm_mapping[8];
349 
350 	order = get_channel_allocation_order(ca);
351 	ch_alloc = &channel_allocations[order];
352 
353 	if (hdmi_channel_mapping[ca][1] == 0) {
354 		int hdmi_slot = 0;
355 		/* fill actual channel mappings in ALSA channel (i) order */
356 		for (i = 0; i < ch_alloc->channels && hdmi_slot < 8; i++) {
357 			while (!ch_alloc->speakers[7 - hdmi_slot]) {
358 				/* skip zero slots */
359 				if (++hdmi_slot >= 8)
360 					goto out;
361 			}
362 
363 			hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
364 		}
365 	out:
366 		/* fill the rest of the slots with ALSA channel 0xf */
367 		for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
368 			if (!ch_alloc->speakers[7 - hdmi_slot])
369 				hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
370 	}
371 
372 	if (non_pcm) {
373 		for (i = 0; i < ch_alloc->channels; i++)
374 			non_pcm_mapping[i] = (i << 4) | i;
375 		for (; i < 8; i++)
376 			non_pcm_mapping[i] = (0xf << 4) | i;
377 	}
378 
379 	for (i = 0; i < 8; i++) {
380 		int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
381 		int hdmi_slot = slotsetup & 0x0f;
382 		int channel = (slotsetup & 0xf0) >> 4;
383 
384 		err = chmap->ops.pin_set_slot_channel(chmap->hdac,
385 				pin_nid, hdmi_slot, channel);
386 		if (err) {
387 			dev_dbg(&chmap->hdac->dev, "HDMI: channel mapping failed\n");
388 			break;
389 		}
390 	}
391 }
392 
393 struct channel_map_table {
394 	unsigned char map;		/* ALSA API channel map position */
395 	int spk_mask;			/* speaker position bit mask */
396 };
397 
398 static struct channel_map_table map_tables[] = {
399 	{ SNDRV_CHMAP_FL,	FL },
400 	{ SNDRV_CHMAP_FR,	FR },
401 	{ SNDRV_CHMAP_RL,	RL },
402 	{ SNDRV_CHMAP_RR,	RR },
403 	{ SNDRV_CHMAP_LFE,	LFE },
404 	{ SNDRV_CHMAP_FC,	FC },
405 	{ SNDRV_CHMAP_RLC,	RLC },
406 	{ SNDRV_CHMAP_RRC,	RRC },
407 	{ SNDRV_CHMAP_RC,	RC },
408 	{ SNDRV_CHMAP_FLC,	FLC },
409 	{ SNDRV_CHMAP_FRC,	FRC },
410 	{ SNDRV_CHMAP_TFL,	FLH },
411 	{ SNDRV_CHMAP_TFR,	FRH },
412 	{ SNDRV_CHMAP_FLW,	FLW },
413 	{ SNDRV_CHMAP_FRW,	FRW },
414 	{ SNDRV_CHMAP_TC,	TC },
415 	{ SNDRV_CHMAP_TFC,	FCH },
416 	{} /* terminator */
417 };
418 
419 /* from ALSA API channel position to speaker bit mask */
420 int snd_hdac_chmap_to_spk_mask(unsigned char c)
421 {
422 	struct channel_map_table *t = map_tables;
423 
424 	for (; t->map; t++) {
425 		if (t->map == c)
426 			return t->spk_mask;
427 	}
428 	return 0;
429 }
430 EXPORT_SYMBOL_GPL(snd_hdac_chmap_to_spk_mask);
431 
432 /* from ALSA API channel position to CEA slot */
433 static int to_cea_slot(int ordered_ca, unsigned char pos)
434 {
435 	int mask = snd_hdac_chmap_to_spk_mask(pos);
436 	int i;
437 
438 	/* Add sanity check to pass klockwork check.
439 	 * This should never happen.
440 	 */
441 	if (ordered_ca >= ARRAY_SIZE(channel_allocations))
442 		return -1;
443 
444 	if (mask) {
445 		for (i = 0; i < 8; i++) {
446 			if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
447 				return i;
448 		}
449 	}
450 
451 	return -1;
452 }
453 
454 /* from speaker bit mask to ALSA API channel position */
455 int snd_hdac_spk_to_chmap(int spk)
456 {
457 	struct channel_map_table *t = map_tables;
458 
459 	for (; t->map; t++) {
460 		if (t->spk_mask == spk)
461 			return t->map;
462 	}
463 	return 0;
464 }
465 EXPORT_SYMBOL_GPL(snd_hdac_spk_to_chmap);
466 
467 /* from CEA slot to ALSA API channel position */
468 static int from_cea_slot(int ordered_ca, unsigned char slot)
469 {
470 	int mask;
471 
472 	/* Add sanity check to pass klockwork check.
473 	 * This should never happen.
474 	 */
475 	if (slot >= 8)
476 		return 0;
477 
478 	mask = channel_allocations[ordered_ca].speakers[7 - slot];
479 
480 	return snd_hdac_spk_to_chmap(mask);
481 }
482 
483 /* get the CA index corresponding to the given ALSA API channel map */
484 static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
485 {
486 	int i, spks = 0, spk_mask = 0;
487 
488 	for (i = 0; i < chs; i++) {
489 		int mask = snd_hdac_chmap_to_spk_mask(map[i]);
490 
491 		if (mask) {
492 			spk_mask |= mask;
493 			spks++;
494 		}
495 	}
496 
497 	for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
498 		if ((chs == channel_allocations[i].channels ||
499 		     spks == channel_allocations[i].channels) &&
500 		    (spk_mask & channel_allocations[i].spk_mask) ==
501 				channel_allocations[i].spk_mask)
502 			return channel_allocations[i].ca_index;
503 	}
504 	return -1;
505 }
506 
507 /* set up the channel slots for the given ALSA API channel map */
508 static int hdmi_manual_setup_channel_mapping(struct hdac_chmap *chmap,
509 					     hda_nid_t pin_nid,
510 					     int chs, unsigned char *map,
511 					     int ca)
512 {
513 	int ordered_ca = get_channel_allocation_order(ca);
514 	int alsa_pos, hdmi_slot;
515 	int assignments[8] = {[0 ... 7] = 0xf};
516 
517 	for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
518 
519 		hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
520 
521 		if (hdmi_slot < 0)
522 			continue; /* unassigned channel */
523 
524 		assignments[hdmi_slot] = alsa_pos;
525 	}
526 
527 	for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
528 		int err;
529 
530 		err = chmap->ops.pin_set_slot_channel(chmap->hdac,
531 				pin_nid, hdmi_slot, assignments[hdmi_slot]);
532 		if (err)
533 			return -EINVAL;
534 	}
535 	return 0;
536 }
537 
538 /* store ALSA API channel map from the current default map */
539 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
540 {
541 	int i;
542 	int ordered_ca = get_channel_allocation_order(ca);
543 
544 	for (i = 0; i < 8; i++) {
545 		if (ordered_ca < ARRAY_SIZE(channel_allocations) &&
546 		    i < channel_allocations[ordered_ca].channels)
547 			map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
548 		else
549 			map[i] = 0;
550 	}
551 }
552 
553 void snd_hdac_setup_channel_mapping(struct hdac_chmap *chmap,
554 				       hda_nid_t pin_nid, bool non_pcm, int ca,
555 				       int channels, unsigned char *map,
556 				       bool chmap_set)
557 {
558 	if (!non_pcm && chmap_set) {
559 		hdmi_manual_setup_channel_mapping(chmap, pin_nid,
560 						  channels, map, ca);
561 	} else {
562 		hdmi_std_setup_channel_mapping(chmap, pin_nid, non_pcm, ca);
563 		hdmi_setup_fake_chmap(map, ca);
564 	}
565 
566 	hdmi_debug_channel_mapping(chmap, pin_nid);
567 }
568 EXPORT_SYMBOL_GPL(snd_hdac_setup_channel_mapping);
569 
570 int snd_hdac_get_active_channels(int ca)
571 {
572 	int ordered_ca = get_channel_allocation_order(ca);
573 
574 	/* Add sanity check to pass klockwork check.
575 	 * This should never happen.
576 	 */
577 	if (ordered_ca >= ARRAY_SIZE(channel_allocations))
578 		ordered_ca = 0;
579 
580 	return channel_allocations[ordered_ca].channels;
581 }
582 EXPORT_SYMBOL_GPL(snd_hdac_get_active_channels);
583 
584 struct hdac_cea_channel_speaker_allocation *snd_hdac_get_ch_alloc_from_ca(int ca)
585 {
586 	return &channel_allocations[get_channel_allocation_order(ca)];
587 }
588 EXPORT_SYMBOL_GPL(snd_hdac_get_ch_alloc_from_ca);
589 
590 int snd_hdac_channel_allocation(struct hdac_device *hdac, int spk_alloc,
591 		int channels, bool chmap_set, bool non_pcm, unsigned char *map)
592 {
593 	int ca;
594 
595 	if (!non_pcm && chmap_set)
596 		ca = hdmi_manual_channel_allocation(channels, map);
597 	else
598 		ca = hdmi_channel_allocation_spk_alloc_blk(hdac,
599 					spk_alloc, channels);
600 
601 	if (ca < 0)
602 		ca = 0;
603 
604 	return ca;
605 }
606 EXPORT_SYMBOL_GPL(snd_hdac_channel_allocation);
607 
608 /*
609  * ALSA API channel-map control callbacks
610  */
611 static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
612 			       struct snd_ctl_elem_info *uinfo)
613 {
614 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
615 	struct hdac_chmap *chmap = info->private_data;
616 
617 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
618 	uinfo->count = chmap->channels_max;
619 	uinfo->value.integer.min = 0;
620 	uinfo->value.integer.max = SNDRV_CHMAP_LAST;
621 	return 0;
622 }
623 
624 static int hdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
625 		struct hdac_cea_channel_speaker_allocation *cap, int channels)
626 {
627 	/* If the speaker allocation matches the channel count, it is OK.*/
628 	if (cap->channels != channels)
629 		return -1;
630 
631 	/* all channels are remappable freely */
632 	return SNDRV_CTL_TLVT_CHMAP_VAR;
633 }
634 
635 static void hdmi_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
636 		struct hdac_cea_channel_speaker_allocation *cap,
637 		unsigned int *chmap, int channels)
638 {
639 	int count = 0;
640 	int c;
641 
642 	for (c = 7; c >= 0; c--) {
643 		int spk = cap->speakers[c];
644 
645 		if (!spk)
646 			continue;
647 
648 		chmap[count++] = snd_hdac_spk_to_chmap(spk);
649 	}
650 
651 	WARN_ON(count != channels);
652 }
653 
654 static int spk_mask_from_spk_alloc(int spk_alloc)
655 {
656 	int i;
657 	int spk_mask = eld_speaker_allocation_bits[0];
658 
659 	for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
660 		if (spk_alloc & (1 << i))
661 			spk_mask |= eld_speaker_allocation_bits[i];
662 	}
663 
664 	return spk_mask;
665 }
666 
667 static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
668 			      unsigned int size, unsigned int __user *tlv)
669 {
670 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
671 	struct hdac_chmap *chmap = info->private_data;
672 	int pcm_idx = kcontrol->private_value;
673 	unsigned int __user *dst;
674 	int chs, count = 0;
675 	unsigned long max_chs;
676 	int type;
677 	int spk_alloc, spk_mask;
678 
679 	if (size < 8)
680 		return -ENOMEM;
681 	if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
682 		return -EFAULT;
683 	size -= 8;
684 	dst = tlv + 2;
685 
686 	spk_alloc = chmap->ops.get_spk_alloc(chmap->hdac, pcm_idx);
687 	spk_mask = spk_mask_from_spk_alloc(spk_alloc);
688 
689 	max_chs = hweight_long(spk_mask);
690 
691 	for (chs = 2; chs <= max_chs; chs++) {
692 		int i;
693 		struct hdac_cea_channel_speaker_allocation *cap;
694 
695 		cap = channel_allocations;
696 		for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
697 			int chs_bytes = chs * 4;
698 			unsigned int tlv_chmap[8];
699 
700 			if (cap->channels != chs)
701 				continue;
702 
703 			if (!(cap->spk_mask == (spk_mask & cap->spk_mask)))
704 				continue;
705 
706 			type = chmap->ops.chmap_cea_alloc_validate_get_type(
707 							chmap, cap, chs);
708 			if (type < 0)
709 				return -ENODEV;
710 			if (size < 8)
711 				return -ENOMEM;
712 
713 			if (put_user(type, dst) ||
714 			    put_user(chs_bytes, dst + 1))
715 				return -EFAULT;
716 
717 			dst += 2;
718 			size -= 8;
719 			count += 8;
720 
721 			if (size < chs_bytes)
722 				return -ENOMEM;
723 
724 			size -= chs_bytes;
725 			count += chs_bytes;
726 			chmap->ops.cea_alloc_to_tlv_chmap(chmap, cap,
727 						tlv_chmap, chs);
728 
729 			if (copy_to_user(dst, tlv_chmap, chs_bytes))
730 				return -EFAULT;
731 			dst += chs;
732 		}
733 	}
734 
735 	if (put_user(count, tlv + 1))
736 		return -EFAULT;
737 
738 	return 0;
739 }
740 
741 static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
742 			      struct snd_ctl_elem_value *ucontrol)
743 {
744 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
745 	struct hdac_chmap *chmap = info->private_data;
746 	int pcm_idx = kcontrol->private_value;
747 	unsigned char pcm_chmap[8];
748 	int i;
749 
750 	memset(pcm_chmap, 0, sizeof(pcm_chmap));
751 	chmap->ops.get_chmap(chmap->hdac, pcm_idx, pcm_chmap);
752 
753 	for (i = 0; i < ARRAY_SIZE(pcm_chmap); i++)
754 		ucontrol->value.integer.value[i] = pcm_chmap[i];
755 
756 	return 0;
757 }
758 
759 /* a simple sanity check for input values to chmap kcontrol */
760 static int chmap_value_check(struct hdac_chmap *hchmap,
761 			     const struct snd_ctl_elem_value *ucontrol)
762 {
763 	int i;
764 
765 	for (i = 0; i < hchmap->channels_max; i++) {
766 		if (ucontrol->value.integer.value[i] < 0 ||
767 		    ucontrol->value.integer.value[i] > SNDRV_CHMAP_LAST)
768 			return -EINVAL;
769 	}
770 	return 0;
771 }
772 
773 static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
774 			      struct snd_ctl_elem_value *ucontrol)
775 {
776 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
777 	struct hdac_chmap *hchmap = info->private_data;
778 	int pcm_idx = kcontrol->private_value;
779 	unsigned int ctl_idx;
780 	struct snd_pcm_substream *substream;
781 	unsigned char chmap[8], per_pin_chmap[8];
782 	int i, err, ca, prepared = 0;
783 
784 	err = chmap_value_check(hchmap, ucontrol);
785 	if (err < 0)
786 		return err;
787 
788 	/* No monitor is connected in dyn_pcm_assign.
789 	 * It's invalid to setup the chmap
790 	 */
791 	if (!hchmap->ops.is_pcm_attached(hchmap->hdac, pcm_idx))
792 		return 0;
793 
794 	ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
795 	substream = snd_pcm_chmap_substream(info, ctl_idx);
796 	if (!substream || !substream->runtime)
797 		return 0; /* just for avoiding error from alsactl restore */
798 	switch (substream->runtime->state) {
799 	case SNDRV_PCM_STATE_OPEN:
800 	case SNDRV_PCM_STATE_SETUP:
801 		break;
802 	case SNDRV_PCM_STATE_PREPARED:
803 		prepared = 1;
804 		break;
805 	default:
806 		return -EBUSY;
807 	}
808 	memset(chmap, 0, sizeof(chmap));
809 	for (i = 0; i < ARRAY_SIZE(chmap); i++)
810 		chmap[i] = ucontrol->value.integer.value[i];
811 
812 	hchmap->ops.get_chmap(hchmap->hdac, pcm_idx, per_pin_chmap);
813 	if (!memcmp(chmap, per_pin_chmap, sizeof(chmap)))
814 		return 0;
815 	ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
816 	if (ca < 0)
817 		return -EINVAL;
818 	if (hchmap->ops.chmap_validate) {
819 		err = hchmap->ops.chmap_validate(hchmap, ca,
820 				ARRAY_SIZE(chmap), chmap);
821 		if (err)
822 			return err;
823 	}
824 
825 	hchmap->ops.set_chmap(hchmap->hdac, pcm_idx, chmap, prepared);
826 
827 	return 0;
828 }
829 
830 static const struct hdac_chmap_ops chmap_ops = {
831 	.chmap_cea_alloc_validate_get_type	= hdmi_chmap_cea_alloc_validate_get_type,
832 	.cea_alloc_to_tlv_chmap			= hdmi_cea_alloc_to_tlv_chmap,
833 	.pin_get_slot_channel			= hdmi_pin_get_slot_channel,
834 	.pin_set_slot_channel			= hdmi_pin_set_slot_channel,
835 	.set_channel_count			= hdmi_set_channel_count,
836 };
837 
838 void snd_hdac_register_chmap_ops(struct hdac_device *hdac,
839 				struct hdac_chmap *chmap)
840 {
841 	chmap->ops = chmap_ops;
842 	chmap->hdac = hdac;
843 	init_channel_allocations();
844 }
845 EXPORT_SYMBOL_GPL(snd_hdac_register_chmap_ops);
846 
847 int snd_hdac_add_chmap_ctls(struct snd_pcm *pcm, int pcm_idx,
848 				struct hdac_chmap *hchmap)
849 {
850 	struct snd_pcm_chmap *chmap;
851 	struct snd_kcontrol *kctl;
852 	int err, i;
853 
854 	err = snd_pcm_add_chmap_ctls(pcm,
855 				     SNDRV_PCM_STREAM_PLAYBACK,
856 				     NULL, 0, pcm_idx, &chmap);
857 	if (err < 0)
858 		return err;
859 	/* override handlers */
860 	chmap->private_data = hchmap;
861 	kctl = chmap->kctl;
862 	for (i = 0; i < kctl->count; i++)
863 		kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
864 	kctl->info = hdmi_chmap_ctl_info;
865 	kctl->get = hdmi_chmap_ctl_get;
866 	kctl->put = hdmi_chmap_ctl_put;
867 	kctl->tlv.c = hdmi_chmap_ctl_tlv;
868 
869 	return 0;
870 }
871 EXPORT_SYMBOL_GPL(snd_hdac_add_chmap_ctls);
872