blob: fe08bef897cd00f05d05d0ec06c43b07962432b5 [file] [log] [blame]
Wu, Fengguang91504872008-11-05 11:16:56 +08001/*
2 *
3 * patch_intelhdmi.c - Patch for Intel HDMI codecs
4 *
5 * Copyright(c) 2008 Intel Corporation. All rights reserved.
6 *
7 * Authors:
8 * Jiang Zhe <zhe.jiang@intel.com>
9 * Wu Fengguang <wfg@linux.intel.com>
10 *
11 * Maintained by:
12 * Wu Fengguang <wfg@linux.intel.com>
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29#include <linux/init.h>
30#include <linux/delay.h>
31#include <linux/slab.h>
32#include <sound/core.h>
Wu, Fengguang91504872008-11-05 11:16:56 +080033#include "hda_codec.h"
34#include "hda_local.h"
35#include "hda_patch.h"
36
37#define CVT_NID 0x02 /* audio converter */
38#define PIN_NID 0x03 /* HDMI output pin */
39
40#define INTEL_HDMI_EVENT_TAG 0x08
41
Wu, Fengguang91504872008-11-05 11:16:56 +080042struct intel_hdmi_spec {
43 struct hda_multi_out multiout;
44 struct hda_pcm pcm_rec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +080045 struct hdmi_eld sink_eld;
Wu, Fengguang91504872008-11-05 11:16:56 +080046};
47
48static struct hda_verb pinout_enable_verb[] = {
49 {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
50 {} /* terminator */
51};
52
53static struct hda_verb pinout_disable_verb[] = {
54 {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00},
55 {}
56};
57
58static struct hda_verb unsolicited_response_verb[] = {
59 {PIN_NID, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN |
60 INTEL_HDMI_EVENT_TAG},
61 {}
62};
63
64static struct hda_verb def_chan_map[] = {
65 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x00},
66 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x11},
67 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x22},
68 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x33},
69 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x44},
70 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x55},
71 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x66},
72 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x77},
73 {}
74};
75
76
77struct hdmi_audio_infoframe {
78 u8 type; /* 0x84 */
79 u8 ver; /* 0x01 */
80 u8 len; /* 0x0a */
81
82 u8 checksum; /* PB0 */
83 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
84 u8 SS01_SF24;
85 u8 CXT04;
86 u8 CA;
87 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang4e19c582008-11-19 15:13:59 +080088 u8 reserved[5]; /* PB6 - PB10 */
Wu, Fengguang91504872008-11-05 11:16:56 +080089};
90
91/*
Wu Fengguang698544d2008-11-19 08:56:17 +080092 * CEA speaker placement:
93 *
94 * FLH FCH FRH
95 * FLW FL FLC FC FRC FR FRW
96 *
97 * LFE
98 * TC
99 *
100 * RL RLC RC RRC RR
101 *
102 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
103 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
104 */
105enum cea_speaker_placement {
106 FL = (1 << 0), /* Front Left */
107 FC = (1 << 1), /* Front Center */
108 FR = (1 << 2), /* Front Right */
109 FLC = (1 << 3), /* Front Left Center */
110 FRC = (1 << 4), /* Front Right Center */
111 RL = (1 << 5), /* Rear Left */
112 RC = (1 << 6), /* Rear Center */
113 RR = (1 << 7), /* Rear Right */
114 RLC = (1 << 8), /* Rear Left Center */
115 RRC = (1 << 9), /* Rear Right Center */
116 LFE = (1 << 10), /* Low Frequency Effect */
117 FLW = (1 << 11), /* Front Left Wide */
118 FRW = (1 << 12), /* Front Right Wide */
119 FLH = (1 << 13), /* Front Left High */
120 FCH = (1 << 14), /* Front Center High */
121 FRH = (1 << 15), /* Front Right High */
122 TC = (1 << 16), /* Top Center */
123};
124
125/*
126 * ELD SA bits in the CEA Speaker Allocation data block
127 */
128static int eld_speaker_allocation_bits[] = {
129 [0] = FL | FR,
130 [1] = LFE,
131 [2] = FC,
132 [3] = RL | RR,
133 [4] = RC,
134 [5] = FLC | FRC,
135 [6] = RLC | RRC,
136 /* the following are not defined in ELD yet */
137 [7] = FLW | FRW,
138 [8] = FLH | FRH,
139 [9] = TC,
140 [10] = FCH,
141};
142
143struct cea_channel_speaker_allocation {
144 int ca_index;
145 int speakers[8];
146
147 /* derived values, just for convenience */
148 int channels;
149 int spk_mask;
150};
151
152/*
153 * This is an ordered list!
154 *
155 * The preceding ones have better chances to be selected by
156 * hdmi_setup_channel_allocation().
157 */
158static struct cea_channel_speaker_allocation channel_allocations[] = {
159/* channel: 8 7 6 5 4 3 2 1 */
160{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
161 /* 2.1 */
162{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
Wu Fengguang4e19c582008-11-19 15:13:59 +0800163 /* Dolby Surround */
Wu Fengguang698544d2008-11-19 08:56:17 +0800164{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
165{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
166{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
167{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
168{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
169{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
170{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
171{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
172{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
173 /* 5.1 */
174{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
175{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
176{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
177{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
178 /* 6.1 */
179{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
180{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
181{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
182{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
183 /* 7.1 */
184{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
185{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
186{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
187{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
188{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
189{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
190{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
191{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
192{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
193{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
194{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
195{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
196{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
197{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
198{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
199{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
200{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
201{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
202{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
203{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
204{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
205{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
206{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
207{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
208{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
209{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
210{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
211{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
212{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
213{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
214{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
215};
216
217/*
Wu, Fengguang91504872008-11-05 11:16:56 +0800218 * HDMI routines
219 */
220
Takashi Iwaibeb0b9cf2008-11-05 07:58:25 +0100221#ifdef BE_PARANOID
Wu, Fengguang91504872008-11-05 11:16:56 +0800222static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t nid,
223 int *packet_index, int *byte_index)
224{
225 int val;
226
227 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_INDEX, 0);
228
229 *packet_index = val >> 5;
230 *byte_index = val & 0x1f;
231}
Takashi Iwaibeb0b9cf2008-11-05 07:58:25 +0100232#endif
Wu, Fengguang91504872008-11-05 11:16:56 +0800233
234static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t nid,
235 int packet_index, int byte_index)
236{
237 int val;
238
239 val = (packet_index << 5) | (byte_index & 0x1f);
240
241 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
242}
243
244static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t nid,
245 unsigned char val)
246{
247 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
248}
249
250static void hdmi_enable_output(struct hda_codec *codec)
251{
Wu, Fengguang91504872008-11-05 11:16:56 +0800252 /* Enable Audio InfoFrame Transmission */
253 hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
254 snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
255 AC_DIPXMIT_BEST);
Wu Fengguang796359d2008-11-17 16:57:33 +0800256 /* Unmute */
257 if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
258 snd_hda_codec_write(codec, PIN_NID, 0,
259 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
260 /* Enable pin out */
261 snd_hda_sequence_write(codec, pinout_enable_verb);
Wu, Fengguang91504872008-11-05 11:16:56 +0800262}
263
264static void hdmi_disable_output(struct hda_codec *codec)
265{
266 snd_hda_sequence_write(codec, pinout_disable_verb);
267 if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
268 snd_hda_codec_write(codec, PIN_NID, 0,
269 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
270
271 /*
272 * FIXME: noises may arise when playing music after reloading the
273 * kernel module, until the next X restart or monitor repower.
274 */
275}
276
277static int hdmi_get_channel_count(struct hda_codec *codec)
278{
279 return 1 + snd_hda_codec_read(codec, CVT_NID, 0,
280 AC_VERB_GET_CVT_CHAN_COUNT, 0);
281}
282
283static void hdmi_set_channel_count(struct hda_codec *codec, int chs)
284{
285 snd_hda_codec_write(codec, CVT_NID, 0,
286 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
287
288 if (chs != hdmi_get_channel_count(codec))
Wu Fengguang03284c82008-11-22 09:40:53 +0800289 snd_printd(KERN_INFO "HDMI channel count: expect %d, get %d\n",
Wu Fengguang4e19c582008-11-19 15:13:59 +0800290 chs, hdmi_get_channel_count(codec));
Wu, Fengguang91504872008-11-05 11:16:56 +0800291}
292
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800293static void hdmi_debug_channel_mapping(struct hda_codec *codec)
Wu, Fengguang91504872008-11-05 11:16:56 +0800294{
295#ifdef CONFIG_SND_DEBUG_VERBOSE
296 int i;
297 int slot;
298
299 for (i = 0; i < 8; i++) {
300 slot = snd_hda_codec_read(codec, CVT_NID, 0,
301 AC_VERB_GET_HDMI_CHAN_SLOT, i);
Wu Fengguang03284c82008-11-22 09:40:53 +0800302 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
Wu Fengguang4e19c582008-11-19 15:13:59 +0800303 slot >> 4, slot & 0x7);
Wu, Fengguang91504872008-11-05 11:16:56 +0800304 }
305#endif
306}
307
Wu, Fengguang91504872008-11-05 11:16:56 +0800308static void hdmi_parse_eld(struct hda_codec *codec)
309{
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800310 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800311 struct hdmi_eld *eld = &spec->sink_eld;
Wu, Fengguang91504872008-11-05 11:16:56 +0800312
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800313 if (!snd_hdmi_get_eld(eld, codec, PIN_NID))
314 snd_hdmi_show_eld(eld);
Wu, Fengguang91504872008-11-05 11:16:56 +0800315}
316
317
318/*
Wu Fengguang4e19c582008-11-19 15:13:59 +0800319 * Audio InfoFrame routines
Wu, Fengguang91504872008-11-05 11:16:56 +0800320 */
321
322static void hdmi_debug_dip_size(struct hda_codec *codec)
323{
324#ifdef CONFIG_SND_DEBUG_VERBOSE
325 int i;
326 int size;
327
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800328 size = snd_hdmi_get_eld_size(codec, PIN_NID);
Wu Fengguang03284c82008-11-22 09:40:53 +0800329 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
Wu, Fengguang91504872008-11-05 11:16:56 +0800330
331 for (i = 0; i < 8; i++) {
332 size = snd_hda_codec_read(codec, PIN_NID, 0,
333 AC_VERB_GET_HDMI_DIP_SIZE, i);
Wu Fengguang03284c82008-11-22 09:40:53 +0800334 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu, Fengguang91504872008-11-05 11:16:56 +0800335 }
336#endif
337}
338
339static void hdmi_clear_dip_buffers(struct hda_codec *codec)
340{
341#ifdef BE_PARANOID
342 int i, j;
343 int size;
344 int pi, bi;
345 for (i = 0; i < 8; i++) {
346 size = snd_hda_codec_read(codec, PIN_NID, 0,
347 AC_VERB_GET_HDMI_DIP_SIZE, i);
348 if (size == 0)
349 continue;
350
351 hdmi_set_dip_index(codec, PIN_NID, i, 0x0);
352 for (j = 1; j < 1000; j++) {
353 hdmi_write_dip_byte(codec, PIN_NID, 0x0);
354 hdmi_get_dip_index(codec, PIN_NID, &pi, &bi);
355 if (pi != i)
356 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
357 bi, pi, i);
358 if (bi == 0) /* byte index wrapped around */
359 break;
360 }
361 snd_printd(KERN_INFO
Wu Fengguang03284c82008-11-22 09:40:53 +0800362 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
363 i, size, j);
Wu, Fengguang91504872008-11-05 11:16:56 +0800364 }
365#endif
366}
367
Wu Fengguang5457a982008-11-19 08:56:15 +0800368static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
369 struct hdmi_audio_infoframe *ai)
Wu, Fengguang91504872008-11-05 11:16:56 +0800370{
Wu Fengguang5457a982008-11-19 08:56:15 +0800371 u8 *params = (u8 *)ai;
Wu, Fengguang91504872008-11-05 11:16:56 +0800372 int i;
373
374 hdmi_debug_dip_size(codec);
375 hdmi_clear_dip_buffers(codec); /* be paranoid */
376
377 hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
Wu Fengguang5457a982008-11-19 08:56:15 +0800378 for (i = 0; i < sizeof(ai); i++)
Wu, Fengguang91504872008-11-05 11:16:56 +0800379 hdmi_write_dip_byte(codec, PIN_NID, params[i]);
380}
381
Wu Fengguang698544d2008-11-19 08:56:17 +0800382/*
383 * Compute derived values in channel_allocations[].
384 */
385static void init_channel_allocations(void)
386{
387 int i, j;
388 struct cea_channel_speaker_allocation *p;
389
390 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
391 p = channel_allocations + i;
392 p->channels = 0;
393 p->spk_mask = 0;
394 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
395 if (p->speakers[j]) {
396 p->channels++;
397 p->spk_mask |= p->speakers[j];
398 }
399 }
400}
401
402/*
403 * The transformation takes two steps:
404 *
405 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
406 * spk_mask => (channel_allocations[]) => ai->CA
407 *
408 * TODO: it could select the wrong CA from multiple candidates.
409*/
410static int hdmi_setup_channel_allocation(struct hda_codec *codec,
411 struct hdmi_audio_infoframe *ai)
412{
413 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800414 struct hdmi_eld *eld = &spec->sink_eld;
Wu Fengguang698544d2008-11-19 08:56:17 +0800415 int i;
416 int spk_mask = 0;
417 int channels = 1 + (ai->CC02_CT47 & 0x7);
418 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
419
420 /*
421 * CA defaults to 0 for basic stereo audio
422 */
423 if (!eld->eld_ver)
424 return 0;
425 if (!eld->spk_alloc)
426 return 0;
427 if (channels <= 2)
428 return 0;
429
430 /*
431 * expand ELD's speaker allocation mask
432 *
433 * ELD tells the speaker mask in a compact(paired) form,
Wu Fengguangb83923a2008-11-22 09:40:51 +0800434 * expand ELD's notions to match the ones used by Audio InfoFrame.
Wu Fengguang698544d2008-11-19 08:56:17 +0800435 */
436 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
437 if (eld->spk_alloc & (1 << i))
438 spk_mask |= eld_speaker_allocation_bits[i];
439 }
440
441 /* search for the first working match in the CA table */
442 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
443 if (channels == channel_allocations[i].channels &&
444 (spk_mask & channel_allocations[i].spk_mask) ==
445 channel_allocations[i].spk_mask) {
446 ai->CA = channel_allocations[i].ca_index;
Wu Fengguangcc02b832008-11-22 09:40:52 +0800447 break;
Wu Fengguang698544d2008-11-19 08:56:17 +0800448 }
449 }
450
451 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
Wu Fengguangcc02b832008-11-22 09:40:52 +0800452 snd_printdd(KERN_INFO
453 "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
454 ai->CA, channels, buf);
455
456 return ai->CA;
Wu Fengguang698544d2008-11-19 08:56:17 +0800457}
458
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800459static void hdmi_setup_channel_mapping(struct hda_codec *codec,
460 struct hdmi_audio_infoframe *ai)
461{
462 if (!ai->CA)
463 return;
464
465 /*
466 * TODO: adjust channel mapping if necessary
467 * ALSA sequence is front/surr/clfe/side?
468 */
469
470 snd_hda_sequence_write(codec, def_chan_map);
471 hdmi_debug_channel_mapping(codec);
472}
473
474
Wu Fengguang5457a982008-11-19 08:56:15 +0800475static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
476 struct snd_pcm_substream *substream)
477{
478 struct hdmi_audio_infoframe ai = {
479 .type = 0x84,
480 .ver = 0x01,
481 .len = 0x0a,
482 .CC02_CT47 = substream->runtime->channels - 1,
483 };
484
Wu Fengguang698544d2008-11-19 08:56:17 +0800485 hdmi_setup_channel_allocation(codec, &ai);
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800486 hdmi_setup_channel_mapping(codec, &ai);
Wu Fengguang698544d2008-11-19 08:56:17 +0800487
Wu Fengguang5457a982008-11-19 08:56:15 +0800488 hdmi_fill_audio_infoframe(codec, &ai);
489}
490
Wu, Fengguang91504872008-11-05 11:16:56 +0800491
492/*
493 * Unsolicited events
494 */
495
496static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
497{
498 int pind = !!(res & AC_UNSOL_RES_PD);
499 int eldv = !!(res & AC_UNSOL_RES_ELDV);
500
Wu Fengguang03284c82008-11-22 09:40:53 +0800501 printk(KERN_INFO
502 "HDMI hot plug event: Presence_Detect=%d ELD_Valid=%d\n",
503 pind, eldv);
Wu, Fengguang91504872008-11-05 11:16:56 +0800504
505 if (pind && eldv) {
506 hdmi_parse_eld(codec);
507 /* TODO: do real things about ELD */
508 }
509}
510
511static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
512{
513 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
514 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
515 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
516
Wu Fengguang03284c82008-11-22 09:40:53 +0800517 printk(KERN_INFO
518 "HDMI content protection event: SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
519 subtag,
520 cp_state,
521 cp_ready);
Wu, Fengguang91504872008-11-05 11:16:56 +0800522
Wu Fengguang03284c82008-11-22 09:40:53 +0800523 /* TODO */
Wu, Fengguang91504872008-11-05 11:16:56 +0800524 if (cp_state)
525 ;
526 if (cp_ready)
527 ;
528}
529
530
531static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
532{
533 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
534 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
535
536 if (tag != INTEL_HDMI_EVENT_TAG) {
Wu Fengguang03284c82008-11-22 09:40:53 +0800537 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
Wu, Fengguang91504872008-11-05 11:16:56 +0800538 return;
539 }
540
541 if (subtag == 0)
542 hdmi_intrinsic_event(codec, res);
543 else
544 hdmi_non_intrinsic_event(codec, res);
545}
546
547/*
548 * Callbacks
549 */
550
551static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream *hinfo,
Wu Fengguang4e19c582008-11-19 15:13:59 +0800552 struct hda_codec *codec,
553 struct snd_pcm_substream *substream)
Wu, Fengguang91504872008-11-05 11:16:56 +0800554{
555 struct intel_hdmi_spec *spec = codec->spec;
556
557 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
558}
559
560static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream *hinfo,
Wu Fengguang4e19c582008-11-19 15:13:59 +0800561 struct hda_codec *codec,
562 struct snd_pcm_substream *substream)
Wu, Fengguang91504872008-11-05 11:16:56 +0800563{
564 struct intel_hdmi_spec *spec = codec->spec;
565
566 hdmi_disable_output(codec);
567
568 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
569}
570
571static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
572 struct hda_codec *codec,
573 unsigned int stream_tag,
574 unsigned int format,
575 struct snd_pcm_substream *substream)
576{
577 struct intel_hdmi_spec *spec = codec->spec;
578
579 snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
580 format, substream);
581
582 hdmi_set_channel_count(codec, substream->runtime->channels);
583
Wu, Fengguang91504872008-11-05 11:16:56 +0800584 hdmi_setup_audio_infoframe(codec, substream);
585
586 hdmi_enable_output(codec);
587
588 return 0;
589}
590
591static struct hda_pcm_stream intel_hdmi_pcm_playback = {
592 .substreams = 1,
593 .channels_min = 2,
594 .channels_max = 8,
595 .nid = CVT_NID, /* NID to query formats and rates and setup streams */
596 .ops = {
Wu Fengguangb83923a2008-11-22 09:40:51 +0800597 .open = intel_hdmi_playback_pcm_open,
598 .close = intel_hdmi_playback_pcm_close,
Wu, Fengguang91504872008-11-05 11:16:56 +0800599 .prepare = intel_hdmi_playback_pcm_prepare
600 },
601};
602
603static int intel_hdmi_build_pcms(struct hda_codec *codec)
604{
605 struct intel_hdmi_spec *spec = codec->spec;
606 struct hda_pcm *info = &spec->pcm_rec;
607
608 codec->num_pcms = 1;
609 codec->pcm_info = info;
610
611 info->name = "INTEL HDMI";
612 info->pcm_type = HDA_PCM_TYPE_HDMI;
613 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
614
615 return 0;
616}
617
618static int intel_hdmi_build_controls(struct hda_codec *codec)
619{
620 struct intel_hdmi_spec *spec = codec->spec;
621 int err;
622
623 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
624 if (err < 0)
625 return err;
626
627 return 0;
628}
629
630static int intel_hdmi_init(struct hda_codec *codec)
631{
632 /* disable audio output as early as possible */
633 hdmi_disable_output(codec);
634
635 snd_hda_sequence_write(codec, unsolicited_response_verb);
636
637 return 0;
638}
639
640static void intel_hdmi_free(struct hda_codec *codec)
641{
Takashi Iwaif208dba2008-11-21 09:11:50 +0100642 struct intel_hdmi_spec *spec = codec->spec;
643
644 snd_hda_eld_proc_free(codec, &spec->sink_eld);
645 kfree(spec);
Wu, Fengguang91504872008-11-05 11:16:56 +0800646}
647
648static struct hda_codec_ops intel_hdmi_patch_ops = {
649 .init = intel_hdmi_init,
650 .free = intel_hdmi_free,
651 .build_pcms = intel_hdmi_build_pcms,
652 .build_controls = intel_hdmi_build_controls,
653 .unsol_event = intel_hdmi_unsol_event,
654};
655
656static int patch_intel_hdmi(struct hda_codec *codec)
657{
658 struct intel_hdmi_spec *spec;
659
660 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
661 if (spec == NULL)
662 return -ENOMEM;
663
664 spec->multiout.num_dacs = 0; /* no analog */
665 spec->multiout.max_channels = 8;
666 spec->multiout.dig_out_nid = CVT_NID;
667
668 codec->spec = spec;
669 codec->patch_ops = intel_hdmi_patch_ops;
670
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800671 snd_hda_eld_proc_new(codec, &spec->sink_eld);
Wu Fengguang5f1e71b2008-11-18 11:47:53 +0800672
Wu Fengguang698544d2008-11-19 08:56:17 +0800673 init_channel_allocations();
674
Wu, Fengguang91504872008-11-05 11:16:56 +0800675 return 0;
676}
677
678struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
679 { .id = 0x808629fb, .name = "INTEL G45 DEVCL", .patch = patch_intel_hdmi },
680 { .id = 0x80862801, .name = "INTEL G45 DEVBLC", .patch = patch_intel_hdmi },
681 { .id = 0x80862802, .name = "INTEL G45 DEVCTG", .patch = patch_intel_hdmi },
682 { .id = 0x80862803, .name = "INTEL G45 DEVELK", .patch = patch_intel_hdmi },
Wu Fengguang3a95cb92008-11-13 10:19:38 +0800683 { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
Wu, Fengguang91504872008-11-05 11:16:56 +0800684 {} /* terminator */
685};