code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / sound / pci / hda / patch_hdmi.c
blob51387bec154aca2b498fed8954dfd33a7c8e6bd0
1 /*
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
7 * Authors:
8 * Wu Fengguang <wfg@linux.intel.com>
10 * Maintained by:
11 * Wu Fengguang <wfg@linux.intel.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 struct hdmi_spec {
30 int num_cvts;
31 int num_pins;
32 hda_nid_t cvt[MAX_HDMI_CVTS+1]; /* audio sources */
33 hda_nid_t pin[MAX_HDMI_PINS+1]; /* audio sinks */
36 * source connection for each pin
38 hda_nid_t pin_cvt[MAX_HDMI_PINS+1];
41 * HDMI sink attached to each pin
43 struct hdmi_eld sink_eld[MAX_HDMI_PINS];
46 * export one pcm per pipe
48 struct hda_pcm pcm_rec[MAX_HDMI_CVTS];
51 * nvhdmi specific
53 struct hda_multi_out multiout;
54 unsigned int codec_type;
56 /* misc flags */
57 /* PD bit indicates only the update, not the current state */
58 unsigned int old_pin_detect:1;
62 struct hdmi_audio_infoframe {
63 u8 type; /* 0x84 */
64 u8 ver; /* 0x01 */
65 u8 len; /* 0x0a */
67 u8 checksum; /* PB0 */
68 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
69 u8 SS01_SF24;
70 u8 CXT04;
71 u8 CA;
72 u8 LFEPBL01_LSV36_DM_INH7;
73 u8 reserved[5]; /* PB6 - PB10 */
77 * CEA speaker placement:
79 * FLH FCH FRH
80 * FLW FL FLC FC FRC FR FRW
82 * LFE
83 * TC
85 * RL RLC RC RRC RR
87 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
88 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
90 enum cea_speaker_placement {
91 FL = (1 << 0), /* Front Left */
92 FC = (1 << 1), /* Front Center */
93 FR = (1 << 2), /* Front Right */
94 FLC = (1 << 3), /* Front Left Center */
95 FRC = (1 << 4), /* Front Right Center */
96 RL = (1 << 5), /* Rear Left */
97 RC = (1 << 6), /* Rear Center */
98 RR = (1 << 7), /* Rear Right */
99 RLC = (1 << 8), /* Rear Left Center */
100 RRC = (1 << 9), /* Rear Right Center */
101 LFE = (1 << 10), /* Low Frequency Effect */
102 FLW = (1 << 11), /* Front Left Wide */
103 FRW = (1 << 12), /* Front Right Wide */
104 FLH = (1 << 13), /* Front Left High */
105 FCH = (1 << 14), /* Front Center High */
106 FRH = (1 << 15), /* Front Right High */
107 TC = (1 << 16), /* Top Center */
111 * ELD SA bits in the CEA Speaker Allocation data block
113 static int eld_speaker_allocation_bits[] = {
114 [0] = FL | FR,
115 [1] = LFE,
116 [2] = FC,
117 [3] = RL | RR,
118 [4] = RC,
119 [5] = FLC | FRC,
120 [6] = RLC | RRC,
121 /* the following are not defined in ELD yet */
122 [7] = FLW | FRW,
123 [8] = FLH | FRH,
124 [9] = TC,
125 [10] = FCH,
128 struct cea_channel_speaker_allocation {
129 int ca_index;
130 int speakers[8];
132 /* derived values, just for convenience */
133 int channels;
134 int spk_mask;
138 * ALSA sequence is:
140 * surround40 surround41 surround50 surround51 surround71
141 * ch0 front left = = = =
142 * ch1 front right = = = =
143 * ch2 rear left = = = =
144 * ch3 rear right = = = =
145 * ch4 LFE center center center
146 * ch5 LFE LFE
147 * ch6 side left
148 * ch7 side right
150 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
152 static int hdmi_channel_mapping[0x32][8] = {
153 /* stereo */
154 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
155 /* 2.1 */
156 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
157 /* Dolby Surround */
158 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
159 /* surround40 */
160 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
161 /* 4ch */
162 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
163 /* surround41 */
164 [0x09] = { 0x00, 0x11, 0x24, 0x34, 0x43, 0xf2, 0xf6, 0xf7 },
165 /* surround50 */
166 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
167 /* surround51 */
168 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
169 /* 7.1 */
170 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
174 * This is an ordered list!
176 * The preceding ones have better chances to be selected by
177 * hdmi_setup_channel_allocation().
179 static struct cea_channel_speaker_allocation channel_allocations[] = {
180 /* channel: 7 6 5 4 3 2 1 0 */
181 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
182 /* 2.1 */
183 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
184 /* Dolby Surround */
185 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
186 /* surround40 */
187 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
188 /* surround41 */
189 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
190 /* surround50 */
191 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
192 /* surround51 */
193 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
194 /* 6.1 */
195 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
196 /* surround71 */
197 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
199 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
200 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
201 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
202 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
203 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
204 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
205 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
206 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
207 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
208 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
209 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
210 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
211 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
212 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
213 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
214 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
215 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
216 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
217 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
218 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
219 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
220 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
221 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
222 { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
223 { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
224 { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
225 { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
226 { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
227 { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
228 { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
229 { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
230 { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
231 { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
232 { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
233 { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
234 { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
235 { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
236 { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
237 { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
238 { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
239 { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
244 * HDMI routines
247 static int hda_node_index(hda_nid_t *nids, hda_nid_t nid)
249 int i;
251 for (i = 0; nids[i]; i++)
252 if (nids[i] == nid)
253 return i;
255 snd_printk(KERN_WARNING "HDMI: nid %d not registered\n", nid);
256 return -EINVAL;
259 static void hdmi_get_show_eld(struct hda_codec *codec, hda_nid_t pin_nid,
260 struct hdmi_eld *eld)
262 if (!snd_hdmi_get_eld(eld, codec, pin_nid))
263 snd_hdmi_show_eld(eld);
266 #ifdef BE_PARANOID
267 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
268 int *packet_index, int *byte_index)
270 int val;
272 val = snd_hda_codec_read(codec, pin_nid, 0,
273 AC_VERB_GET_HDMI_DIP_INDEX, 0);
275 *packet_index = val >> 5;
276 *byte_index = val & 0x1f;
278 #endif
280 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
281 int packet_index, int byte_index)
283 int val;
285 val = (packet_index << 5) | (byte_index & 0x1f);
287 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
290 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
291 unsigned char val)
293 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
296 static void hdmi_enable_output(struct hda_codec *codec, hda_nid_t pin_nid)
298 /* Unmute */
299 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
300 snd_hda_codec_write(codec, pin_nid, 0,
301 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
302 /* Enable pin out */
303 snd_hda_codec_write(codec, pin_nid, 0,
304 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
307 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t nid)
309 return 1 + snd_hda_codec_read(codec, nid, 0,
310 AC_VERB_GET_CVT_CHAN_COUNT, 0);
313 static void hdmi_set_channel_count(struct hda_codec *codec,
314 hda_nid_t nid, int chs)
316 if (chs != hdmi_get_channel_count(codec, nid))
317 snd_hda_codec_write(codec, nid, 0,
318 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
323 * Channel mapping routines
327 * Compute derived values in channel_allocations[].
329 static void init_channel_allocations(void)
331 int i, j;
332 struct cea_channel_speaker_allocation *p;
334 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
335 p = channel_allocations + i;
336 p->channels = 0;
337 p->spk_mask = 0;
338 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
339 if (p->speakers[j]) {
340 p->channels++;
341 p->spk_mask |= p->speakers[j];
347 * The transformation takes two steps:
349 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
350 * spk_mask => (channel_allocations[]) => ai->CA
352 * TODO: it could select the wrong CA from multiple candidates.
354 static int hdmi_setup_channel_allocation(struct hda_codec *codec, hda_nid_t nid,
355 struct hdmi_audio_infoframe *ai)
357 struct hdmi_spec *spec = codec->spec;
358 struct hdmi_eld *eld;
359 int i;
360 int spk_mask = 0;
361 int channels = 1 + (ai->CC02_CT47 & 0x7);
362 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
365 * CA defaults to 0 for basic stereo audio
367 if (channels <= 2)
368 return 0;
370 i = hda_node_index(spec->pin_cvt, nid);
371 if (i < 0)
372 return 0;
373 eld = &spec->sink_eld[i];
376 * HDMI sink's ELD info cannot always be retrieved for now, e.g.
377 * in console or for audio devices. Assume the highest speakers
378 * configuration, to _not_ prohibit multi-channel audio playback.
380 if (!eld->spk_alloc)
381 eld->spk_alloc = 0xffff;
384 * expand ELD's speaker allocation mask
386 * ELD tells the speaker mask in a compact(paired) form,
387 * expand ELD's notions to match the ones used by Audio InfoFrame.
389 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
390 if (eld->spk_alloc & (1 << i))
391 spk_mask |= eld_speaker_allocation_bits[i];
394 /* search for the first working match in the CA table */
395 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
396 if (channels == channel_allocations[i].channels &&
397 (spk_mask & channel_allocations[i].spk_mask) ==
398 channel_allocations[i].spk_mask) {
399 ai->CA = channel_allocations[i].ca_index;
400 break;
404 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
405 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
406 ai->CA, channels, buf);
408 return ai->CA;
411 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
412 hda_nid_t pin_nid)
414 #ifdef CONFIG_SND_DEBUG_VERBOSE
415 int i;
416 int slot;
418 for (i = 0; i < 8; i++) {
419 slot = snd_hda_codec_read(codec, pin_nid, 0,
420 AC_VERB_GET_HDMI_CHAN_SLOT, i);
421 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
422 slot >> 4, slot & 0xf);
424 #endif
428 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
429 hda_nid_t pin_nid,
430 struct hdmi_audio_infoframe *ai)
432 int i;
433 int ca = ai->CA;
434 int err;
436 if (hdmi_channel_mapping[ca][1] == 0) {
437 for (i = 0; i < channel_allocations[ca].channels; i++)
438 hdmi_channel_mapping[ca][i] = i | (i << 4);
439 for (; i < 8; i++)
440 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
443 for (i = 0; i < 8; i++) {
444 err = snd_hda_codec_write(codec, pin_nid, 0,
445 AC_VERB_SET_HDMI_CHAN_SLOT,
446 hdmi_channel_mapping[ca][i]);
447 if (err) {
448 snd_printdd(KERN_NOTICE
449 "HDMI: channel mapping failed\n");
450 break;
454 hdmi_debug_channel_mapping(codec, pin_nid);
459 * Audio InfoFrame routines
463 * Enable Audio InfoFrame Transmission
465 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
466 hda_nid_t pin_nid)
468 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
469 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
470 AC_DIPXMIT_BEST);
474 * Disable Audio InfoFrame Transmission
476 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
477 hda_nid_t pin_nid)
479 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
480 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
481 AC_DIPXMIT_DISABLE);
484 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
486 #ifdef CONFIG_SND_DEBUG_VERBOSE
487 int i;
488 int size;
490 size = snd_hdmi_get_eld_size(codec, pin_nid);
491 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
493 for (i = 0; i < 8; i++) {
494 size = snd_hda_codec_read(codec, pin_nid, 0,
495 AC_VERB_GET_HDMI_DIP_SIZE, i);
496 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
498 #endif
501 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
503 #ifdef BE_PARANOID
504 int i, j;
505 int size;
506 int pi, bi;
507 for (i = 0; i < 8; i++) {
508 size = snd_hda_codec_read(codec, pin_nid, 0,
509 AC_VERB_GET_HDMI_DIP_SIZE, i);
510 if (size == 0)
511 continue;
513 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
514 for (j = 1; j < 1000; j++) {
515 hdmi_write_dip_byte(codec, pin_nid, 0x0);
516 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
517 if (pi != i)
518 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
519 bi, pi, i);
520 if (bi == 0) /* byte index wrapped around */
521 break;
523 snd_printd(KERN_INFO
524 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
525 i, size, j);
527 #endif
530 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *ai)
532 u8 *bytes = (u8 *)ai;
533 u8 sum = 0;
534 int i;
536 ai->checksum = 0;
538 for (i = 0; i < sizeof(*ai); i++)
539 sum += bytes[i];
541 ai->checksum = -sum;
544 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
545 hda_nid_t pin_nid,
546 struct hdmi_audio_infoframe *ai)
548 u8 *bytes = (u8 *)ai;
549 int i;
551 hdmi_debug_dip_size(codec, pin_nid);
552 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
554 hdmi_checksum_audio_infoframe(ai);
556 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
557 for (i = 0; i < sizeof(*ai); i++)
558 hdmi_write_dip_byte(codec, pin_nid, bytes[i]);
561 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
562 struct hdmi_audio_infoframe *ai)
564 u8 *bytes = (u8 *)ai;
565 u8 val;
566 int i;
568 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
569 != AC_DIPXMIT_BEST)
570 return false;
572 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
573 for (i = 0; i < sizeof(*ai); i++) {
574 val = snd_hda_codec_read(codec, pin_nid, 0,
575 AC_VERB_GET_HDMI_DIP_DATA, 0);
576 if (val != bytes[i])
577 return false;
580 return true;
583 static void hdmi_setup_audio_infoframe(struct hda_codec *codec, hda_nid_t nid,
584 struct snd_pcm_substream *substream)
586 struct hdmi_spec *spec = codec->spec;
587 hda_nid_t pin_nid;
588 int i;
589 struct hdmi_audio_infoframe ai = {
590 .type = 0x84,
591 .ver = 0x01,
592 .len = 0x0a,
593 .CC02_CT47 = substream->runtime->channels - 1,
596 hdmi_setup_channel_allocation(codec, nid, &ai);
598 for (i = 0; i < spec->num_pins; i++) {
599 if (spec->pin_cvt[i] != nid)
600 continue;
601 if (!spec->sink_eld[i].monitor_present)
602 continue;
604 pin_nid = spec->pin[i];
605 if (!hdmi_infoframe_uptodate(codec, pin_nid, &ai)) {
606 snd_printdd("hdmi_setup_audio_infoframe: "
607 "cvt=%d pin=%d channels=%d\n",
608 nid, pin_nid,
609 substream->runtime->channels);
610 hdmi_setup_channel_mapping(codec, pin_nid, &ai);
611 hdmi_stop_infoframe_trans(codec, pin_nid);
612 hdmi_fill_audio_infoframe(codec, pin_nid, &ai);
613 hdmi_start_infoframe_trans(codec, pin_nid);
620 * Unsolicited events
623 static void hdmi_present_sense(struct hda_codec *codec, hda_nid_t pin_nid,
624 struct hdmi_eld *eld);
626 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
628 struct hdmi_spec *spec = codec->spec;
629 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
630 int pind = !!(res & AC_UNSOL_RES_PD);
631 int eldv = !!(res & AC_UNSOL_RES_ELDV);
632 int index;
634 printk(KERN_INFO
635 "HDMI hot plug event: Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
636 tag, pind, eldv);
638 index = hda_node_index(spec->pin, tag);
639 if (index < 0)
640 return;
642 if (spec->old_pin_detect) {
643 if (pind)
644 hdmi_present_sense(codec, tag, &spec->sink_eld[index]);
645 pind = spec->sink_eld[index].monitor_present;
648 spec->sink_eld[index].monitor_present = pind;
649 spec->sink_eld[index].eld_valid = eldv;
651 if (pind && eldv) {
652 hdmi_get_show_eld(codec, spec->pin[index],
653 &spec->sink_eld[index]);
654 /* TODO: do real things about ELD */
658 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
660 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
661 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
662 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
663 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
665 printk(KERN_INFO
666 "HDMI CP event: PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
667 tag,
668 subtag,
669 cp_state,
670 cp_ready);
672 /* TODO */
673 if (cp_state)
675 if (cp_ready)
680 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
682 struct hdmi_spec *spec = codec->spec;
683 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
684 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
686 if (hda_node_index(spec->pin, tag) < 0) {
687 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
688 return;
691 if (subtag == 0)
692 hdmi_intrinsic_event(codec, res);
693 else
694 hdmi_non_intrinsic_event(codec, res);
698 * Callbacks
701 static void hdmi_setup_stream(struct hda_codec *codec, hda_nid_t nid,
702 u32 stream_tag, int format)
704 int tag;
705 int fmt;
707 tag = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0) >> 4;
708 fmt = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_STREAM_FORMAT, 0);
710 snd_printdd("hdmi_setup_stream: "
711 "NID=0x%x, %sstream=0x%x, %sformat=0x%x\n",
712 nid,
713 tag == stream_tag ? "" : "new-",
714 stream_tag,
715 fmt == format ? "" : "new-",
716 format);
718 if (tag != stream_tag)
719 snd_hda_codec_write(codec, nid, 0,
720 AC_VERB_SET_CHANNEL_STREAMID,
721 stream_tag << 4);
722 if (fmt != format)
723 snd_hda_codec_write(codec, nid, 0,
724 AC_VERB_SET_STREAM_FORMAT, format);
728 * HDA/HDMI auto parsing
731 static int hdmi_read_pin_conn(struct hda_codec *codec, hda_nid_t pin_nid)
733 struct hdmi_spec *spec = codec->spec;
734 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
735 int conn_len, curr;
736 int index;
738 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
739 snd_printk(KERN_WARNING
740 "HDMI: pin %d wcaps %#x "
741 "does not support connection list\n",
742 pin_nid, get_wcaps(codec, pin_nid));
743 return -EINVAL;
746 conn_len = snd_hda_get_connections(codec, pin_nid, conn_list,
747 HDA_MAX_CONNECTIONS);
748 if (conn_len > 1)
749 curr = snd_hda_codec_read(codec, pin_nid, 0,
750 AC_VERB_GET_CONNECT_SEL, 0);
751 else
752 curr = 0;
754 index = hda_node_index(spec->pin, pin_nid);
755 if (index < 0)
756 return -EINVAL;
758 spec->pin_cvt[index] = conn_list[curr];
760 return 0;
763 static void hdmi_present_sense(struct hda_codec *codec, hda_nid_t pin_nid,
764 struct hdmi_eld *eld)
766 int present = snd_hda_pin_sense(codec, pin_nid);
768 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
769 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
771 if (present & AC_PINSENSE_ELDV)
772 hdmi_get_show_eld(codec, pin_nid, eld);
775 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
777 struct hdmi_spec *spec = codec->spec;
779 if (spec->num_pins >= MAX_HDMI_PINS) {
780 snd_printk(KERN_WARNING
781 "HDMI: no space for pin %d\n", pin_nid);
782 return -EINVAL;
785 hdmi_present_sense(codec, pin_nid, &spec->sink_eld[spec->num_pins]);
787 spec->pin[spec->num_pins] = pin_nid;
788 spec->num_pins++;
791 * It is assumed that converter nodes come first in the node list and
792 * hence have been registered and usable now.
794 return hdmi_read_pin_conn(codec, pin_nid);
797 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t nid)
799 struct hdmi_spec *spec = codec->spec;
801 if (spec->num_cvts >= MAX_HDMI_CVTS) {
802 snd_printk(KERN_WARNING
803 "HDMI: no space for converter %d\n", nid);
804 return -EINVAL;
807 spec->cvt[spec->num_cvts] = nid;
808 spec->num_cvts++;
810 return 0;
813 static int hdmi_parse_codec(struct hda_codec *codec)
815 hda_nid_t nid;
816 int i, nodes;
818 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
819 if (!nid || nodes < 0) {
820 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
821 return -EINVAL;
824 for (i = 0; i < nodes; i++, nid++) {
825 unsigned int caps;
826 unsigned int type;
828 caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
829 type = get_wcaps_type(caps);
831 if (!(caps & AC_WCAP_DIGITAL))
832 continue;
834 switch (type) {
835 case AC_WID_AUD_OUT:
836 if (hdmi_add_cvt(codec, nid) < 0)
837 return -EINVAL;
838 break;
839 case AC_WID_PIN:
840 caps = snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
841 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
842 continue;
843 if (hdmi_add_pin(codec, nid) < 0)
844 return -EINVAL;
845 break;
850 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
851 * can be lost and presence sense verb will become inaccurate if the
852 * HDA link is powered off at hot plug or hw initialization time.
854 #ifdef CONFIG_SND_HDA_POWER_SAVE
855 if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
856 AC_PWRST_EPSS))
857 codec->bus->power_keep_link_on = 1;
858 #endif
860 return 0;