blob: 3564f4e4b74c035d8d88b1193d2a93498789942c [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"
Wu, Fengguang91504872008-11-05 11:16:56 +080035
36#define CVT_NID 0x02 /* audio converter */
37#define PIN_NID 0x03 /* HDMI output pin */
38
39#define INTEL_HDMI_EVENT_TAG 0x08
40
Wu, Fengguang91504872008-11-05 11:16:56 +080041struct intel_hdmi_spec {
42 struct hda_multi_out multiout;
43 struct hda_pcm pcm_rec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +080044 struct hdmi_eld sink_eld;
Wu, Fengguang91504872008-11-05 11:16:56 +080045};
46
47static struct hda_verb pinout_enable_verb[] = {
48 {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
49 {} /* terminator */
50};
51
52static struct hda_verb pinout_disable_verb[] = {
53 {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00},
54 {}
55};
56
57static struct hda_verb unsolicited_response_verb[] = {
58 {PIN_NID, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN |
59 INTEL_HDMI_EVENT_TAG},
60 {}
61};
62
63static struct hda_verb def_chan_map[] = {
64 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x00},
65 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x11},
66 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x22},
67 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x33},
68 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x44},
69 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x55},
70 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x66},
71 {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x77},
72 {}
73};
74
75
76struct hdmi_audio_infoframe {
77 u8 type; /* 0x84 */
78 u8 ver; /* 0x01 */
79 u8 len; /* 0x0a */
80
81 u8 checksum; /* PB0 */
82 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
83 u8 SS01_SF24;
84 u8 CXT04;
85 u8 CA;
86 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang4e19c582008-11-19 15:13:59 +080087 u8 reserved[5]; /* PB6 - PB10 */
Wu, Fengguang91504872008-11-05 11:16:56 +080088};
89
90/*
Wu Fengguang698544d2008-11-19 08:56:17 +080091 * CEA speaker placement:
92 *
93 * FLH FCH FRH
94 * FLW FL FLC FC FRC FR FRW
95 *
96 * LFE
97 * TC
98 *
99 * RL RLC RC RRC RR
100 *
101 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
102 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
103 */
104enum cea_speaker_placement {
105 FL = (1 << 0), /* Front Left */
106 FC = (1 << 1), /* Front Center */
107 FR = (1 << 2), /* Front Right */
108 FLC = (1 << 3), /* Front Left Center */
109 FRC = (1 << 4), /* Front Right Center */
110 RL = (1 << 5), /* Rear Left */
111 RC = (1 << 6), /* Rear Center */
112 RR = (1 << 7), /* Rear Right */
113 RLC = (1 << 8), /* Rear Left Center */
114 RRC = (1 << 9), /* Rear Right Center */
115 LFE = (1 << 10), /* Low Frequency Effect */
116 FLW = (1 << 11), /* Front Left Wide */
117 FRW = (1 << 12), /* Front Right Wide */
118 FLH = (1 << 13), /* Front Left High */
119 FCH = (1 << 14), /* Front Center High */
120 FRH = (1 << 15), /* Front Right High */
121 TC = (1 << 16), /* Top Center */
122};
123
124/*
125 * ELD SA bits in the CEA Speaker Allocation data block
126 */
127static int eld_speaker_allocation_bits[] = {
128 [0] = FL | FR,
129 [1] = LFE,
130 [2] = FC,
131 [3] = RL | RR,
132 [4] = RC,
133 [5] = FLC | FRC,
134 [6] = RLC | RRC,
135 /* the following are not defined in ELD yet */
136 [7] = FLW | FRW,
137 [8] = FLH | FRH,
138 [9] = TC,
139 [10] = FCH,
140};
141
142struct cea_channel_speaker_allocation {
143 int ca_index;
144 int speakers[8];
145
146 /* derived values, just for convenience */
147 int channels;
148 int spk_mask;
149};
150
151/*
152 * This is an ordered list!
153 *
154 * The preceding ones have better chances to be selected by
155 * hdmi_setup_channel_allocation().
156 */
157static struct cea_channel_speaker_allocation channel_allocations[] = {
158/* channel: 8 7 6 5 4 3 2 1 */
159{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
160 /* 2.1 */
161{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
Wu Fengguang4e19c582008-11-19 15:13:59 +0800162 /* Dolby Surround */
Wu Fengguang698544d2008-11-19 08:56:17 +0800163{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
164{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
165{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
166{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
167{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
168{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
169{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
170{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
171{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
172 /* 5.1 */
173{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
174{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
175{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
176{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
177 /* 6.1 */
178{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
179{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
180{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
181{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
182 /* 7.1 */
183{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
184{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
185{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
186{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
187{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
188{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
189{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
190{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
191{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
192{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
193{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
194{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
195{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
196{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
197{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
198{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
199{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
200{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
201{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
202{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
203{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
204{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
205{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
206{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
207{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
208{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
209{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
210{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
211{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
212{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
213{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
214};
215
216/*
Wu, Fengguang91504872008-11-05 11:16:56 +0800217 * HDMI routines
218 */
219
Takashi Iwaibeb0b9c2008-11-05 07:58:25 +0100220#ifdef BE_PARANOID
Wu, Fengguang91504872008-11-05 11:16:56 +0800221static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t nid,
222 int *packet_index, int *byte_index)
223{
224 int val;
225
226 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_INDEX, 0);
227
228 *packet_index = val >> 5;
229 *byte_index = val & 0x1f;
230}
Takashi Iwaibeb0b9c2008-11-05 07:58:25 +0100231#endif
Wu, Fengguang91504872008-11-05 11:16:56 +0800232
233static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t nid,
234 int packet_index, int byte_index)
235{
236 int val;
237
238 val = (packet_index << 5) | (byte_index & 0x1f);
239
240 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
241}
242
243static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t nid,
244 unsigned char val)
245{
246 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
247}
248
249static void hdmi_enable_output(struct hda_codec *codec)
250{
Wu, Fengguang91504872008-11-05 11:16:56 +0800251 /* Enable Audio InfoFrame Transmission */
252 hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
253 snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
254 AC_DIPXMIT_BEST);
Wu Fengguang796359d2008-11-17 16:57:33 +0800255 /* Unmute */
256 if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
257 snd_hda_codec_write(codec, PIN_NID, 0,
258 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
259 /* Enable pin out */
260 snd_hda_sequence_write(codec, pinout_enable_verb);
Wu, Fengguang91504872008-11-05 11:16:56 +0800261}
262
263static void hdmi_disable_output(struct hda_codec *codec)
264{
265 snd_hda_sequence_write(codec, pinout_disable_verb);
266 if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
267 snd_hda_codec_write(codec, PIN_NID, 0,
268 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
269
270 /*
271 * FIXME: noises may arise when playing music after reloading the
272 * kernel module, until the next X restart or monitor repower.
273 */
274}
275
276static int hdmi_get_channel_count(struct hda_codec *codec)
277{
278 return 1 + snd_hda_codec_read(codec, CVT_NID, 0,
279 AC_VERB_GET_CVT_CHAN_COUNT, 0);
280}
281
282static void hdmi_set_channel_count(struct hda_codec *codec, int chs)
283{
284 snd_hda_codec_write(codec, CVT_NID, 0,
285 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
286
287 if (chs != hdmi_get_channel_count(codec))
Wu Fengguang03284c82008-11-22 09:40:53 +0800288 snd_printd(KERN_INFO "HDMI channel count: expect %d, get %d\n",
Wu Fengguang4e19c582008-11-19 15:13:59 +0800289 chs, hdmi_get_channel_count(codec));
Wu, Fengguang91504872008-11-05 11:16:56 +0800290}
291
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800292static void hdmi_debug_channel_mapping(struct hda_codec *codec)
Wu, Fengguang91504872008-11-05 11:16:56 +0800293{
294#ifdef CONFIG_SND_DEBUG_VERBOSE
295 int i;
296 int slot;
297
298 for (i = 0; i < 8; i++) {
299 slot = snd_hda_codec_read(codec, CVT_NID, 0,
300 AC_VERB_GET_HDMI_CHAN_SLOT, i);
Wu Fengguang03284c82008-11-22 09:40:53 +0800301 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
Wu Fengguang4e19c582008-11-19 15:13:59 +0800302 slot >> 4, slot & 0x7);
Wu, Fengguang91504872008-11-05 11:16:56 +0800303 }
304#endif
305}
306
Wu, Fengguang91504872008-11-05 11:16:56 +0800307static void hdmi_parse_eld(struct hda_codec *codec)
308{
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800309 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800310 struct hdmi_eld *eld = &spec->sink_eld;
Wu, Fengguang91504872008-11-05 11:16:56 +0800311
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800312 if (!snd_hdmi_get_eld(eld, codec, PIN_NID))
313 snd_hdmi_show_eld(eld);
Wu, Fengguang91504872008-11-05 11:16:56 +0800314}
315
316
317/*
Wu Fengguang4e19c582008-11-19 15:13:59 +0800318 * Audio InfoFrame routines
Wu, Fengguang91504872008-11-05 11:16:56 +0800319 */
320
321static void hdmi_debug_dip_size(struct hda_codec *codec)
322{
323#ifdef CONFIG_SND_DEBUG_VERBOSE
324 int i;
325 int size;
326
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800327 size = snd_hdmi_get_eld_size(codec, PIN_NID);
Wu Fengguang03284c82008-11-22 09:40:53 +0800328 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
Wu, Fengguang91504872008-11-05 11:16:56 +0800329
330 for (i = 0; i < 8; i++) {
331 size = snd_hda_codec_read(codec, PIN_NID, 0,
332 AC_VERB_GET_HDMI_DIP_SIZE, i);
Wu Fengguang03284c82008-11-22 09:40:53 +0800333 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu, Fengguang91504872008-11-05 11:16:56 +0800334 }
335#endif
336}
337
338static void hdmi_clear_dip_buffers(struct hda_codec *codec)
339{
340#ifdef BE_PARANOID
341 int i, j;
342 int size;
343 int pi, bi;
344 for (i = 0; i < 8; i++) {
345 size = snd_hda_codec_read(codec, PIN_NID, 0,
346 AC_VERB_GET_HDMI_DIP_SIZE, i);
347 if (size == 0)
348 continue;
349
350 hdmi_set_dip_index(codec, PIN_NID, i, 0x0);
351 for (j = 1; j < 1000; j++) {
352 hdmi_write_dip_byte(codec, PIN_NID, 0x0);
353 hdmi_get_dip_index(codec, PIN_NID, &pi, &bi);
354 if (pi != i)
355 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
356 bi, pi, i);
357 if (bi == 0) /* byte index wrapped around */
358 break;
359 }
360 snd_printd(KERN_INFO
Wu Fengguang03284c82008-11-22 09:40:53 +0800361 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
362 i, size, j);
Wu, Fengguang91504872008-11-05 11:16:56 +0800363 }
364#endif
365}
366
Wu Fengguang5457a982008-11-19 08:56:15 +0800367static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
368 struct hdmi_audio_infoframe *ai)
Wu, Fengguang91504872008-11-05 11:16:56 +0800369{
Wu Fengguang5457a982008-11-19 08:56:15 +0800370 u8 *params = (u8 *)ai;
Wu, Fengguang91504872008-11-05 11:16:56 +0800371 int i;
372
373 hdmi_debug_dip_size(codec);
374 hdmi_clear_dip_buffers(codec); /* be paranoid */
375
376 hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
Wu Fengguang5457a982008-11-19 08:56:15 +0800377 for (i = 0; i < sizeof(ai); i++)
Wu, Fengguang91504872008-11-05 11:16:56 +0800378 hdmi_write_dip_byte(codec, PIN_NID, params[i]);
379}
380
Wu Fengguang698544d2008-11-19 08:56:17 +0800381/*
382 * Compute derived values in channel_allocations[].
383 */
384static void init_channel_allocations(void)
385{
386 int i, j;
387 struct cea_channel_speaker_allocation *p;
388
389 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
390 p = channel_allocations + i;
391 p->channels = 0;
392 p->spk_mask = 0;
393 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
394 if (p->speakers[j]) {
395 p->channels++;
396 p->spk_mask |= p->speakers[j];
397 }
398 }
399}
400
401/*
402 * The transformation takes two steps:
403 *
404 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
405 * spk_mask => (channel_allocations[]) => ai->CA
406 *
407 * TODO: it could select the wrong CA from multiple candidates.
408*/
409static int hdmi_setup_channel_allocation(struct hda_codec *codec,
410 struct hdmi_audio_infoframe *ai)
411{
412 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800413 struct hdmi_eld *eld = &spec->sink_eld;
Wu Fengguang698544d2008-11-19 08:56:17 +0800414 int i;
415 int spk_mask = 0;
416 int channels = 1 + (ai->CC02_CT47 & 0x7);
417 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
418
419 /*
420 * CA defaults to 0 for basic stereo audio
421 */
422 if (!eld->eld_ver)
423 return 0;
424 if (!eld->spk_alloc)
425 return 0;
426 if (channels <= 2)
427 return 0;
428
429 /*
430 * expand ELD's speaker allocation mask
431 *
432 * ELD tells the speaker mask in a compact(paired) form,
Wu Fengguangb83923a2008-11-22 09:40:51 +0800433 * expand ELD's notions to match the ones used by Audio InfoFrame.
Wu Fengguang698544d2008-11-19 08:56:17 +0800434 */
435 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
436 if (eld->spk_alloc & (1 << i))
437 spk_mask |= eld_speaker_allocation_bits[i];
438 }
439
440 /* search for the first working match in the CA table */
441 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
442 if (channels == channel_allocations[i].channels &&
443 (spk_mask & channel_allocations[i].spk_mask) ==
444 channel_allocations[i].spk_mask) {
445 ai->CA = channel_allocations[i].ca_index;
Wu Fengguangcc02b832008-11-22 09:40:52 +0800446 break;
Wu Fengguang698544d2008-11-19 08:56:17 +0800447 }
448 }
449
450 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
Wu Fengguangcc02b832008-11-22 09:40:52 +0800451 snd_printdd(KERN_INFO
452 "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
453 ai->CA, channels, buf);
454
455 return ai->CA;
Wu Fengguang698544d2008-11-19 08:56:17 +0800456}
457
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800458static void hdmi_setup_channel_mapping(struct hda_codec *codec,
459 struct hdmi_audio_infoframe *ai)
460{
461 if (!ai->CA)
462 return;
463
464 /*
465 * TODO: adjust channel mapping if necessary
466 * ALSA sequence is front/surr/clfe/side?
467 */
468
469 snd_hda_sequence_write(codec, def_chan_map);
470 hdmi_debug_channel_mapping(codec);
471}
472
473
Wu Fengguang5457a982008-11-19 08:56:15 +0800474static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
475 struct snd_pcm_substream *substream)
476{
477 struct hdmi_audio_infoframe ai = {
478 .type = 0x84,
479 .ver = 0x01,
480 .len = 0x0a,
481 .CC02_CT47 = substream->runtime->channels - 1,
482 };
483
Wu Fengguang698544d2008-11-19 08:56:17 +0800484 hdmi_setup_channel_allocation(codec, &ai);
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800485 hdmi_setup_channel_mapping(codec, &ai);
Wu Fengguang698544d2008-11-19 08:56:17 +0800486
Wu Fengguang5457a982008-11-19 08:56:15 +0800487 hdmi_fill_audio_infoframe(codec, &ai);
488}
489
Wu, Fengguang91504872008-11-05 11:16:56 +0800490
491/*
492 * Unsolicited events
493 */
494
495static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
496{
497 int pind = !!(res & AC_UNSOL_RES_PD);
498 int eldv = !!(res & AC_UNSOL_RES_ELDV);
499
Wu Fengguang03284c82008-11-22 09:40:53 +0800500 printk(KERN_INFO
501 "HDMI hot plug event: Presence_Detect=%d ELD_Valid=%d\n",
502 pind, eldv);
Wu, Fengguang91504872008-11-05 11:16:56 +0800503
504 if (pind && eldv) {
505 hdmi_parse_eld(codec);
506 /* TODO: do real things about ELD */
507 }
508}
509
510static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
511{
512 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
513 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
514 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
515
Wu Fengguang03284c82008-11-22 09:40:53 +0800516 printk(KERN_INFO
517 "HDMI content protection event: SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
518 subtag,
519 cp_state,
520 cp_ready);
Wu, Fengguang91504872008-11-05 11:16:56 +0800521
Wu Fengguang03284c82008-11-22 09:40:53 +0800522 /* TODO */
Wu, Fengguang91504872008-11-05 11:16:56 +0800523 if (cp_state)
524 ;
525 if (cp_ready)
526 ;
527}
528
529
530static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
531{
532 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
533 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
534
535 if (tag != INTEL_HDMI_EVENT_TAG) {
Wu Fengguang03284c82008-11-22 09:40:53 +0800536 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
Wu, Fengguang91504872008-11-05 11:16:56 +0800537 return;
538 }
539
540 if (subtag == 0)
541 hdmi_intrinsic_event(codec, res);
542 else
543 hdmi_non_intrinsic_event(codec, res);
544}
545
546/*
547 * Callbacks
548 */
549
550static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream *hinfo,
Wu Fengguang4e19c582008-11-19 15:13:59 +0800551 struct hda_codec *codec,
552 struct snd_pcm_substream *substream)
Wu, Fengguang91504872008-11-05 11:16:56 +0800553{
554 struct intel_hdmi_spec *spec = codec->spec;
555
556 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
557}
558
559static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream *hinfo,
Wu Fengguang4e19c582008-11-19 15:13:59 +0800560 struct hda_codec *codec,
561 struct snd_pcm_substream *substream)
Wu, Fengguang91504872008-11-05 11:16:56 +0800562{
563 struct intel_hdmi_spec *spec = codec->spec;
564
565 hdmi_disable_output(codec);
566
567 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
568}
569
570static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
571 struct hda_codec *codec,
572 unsigned int stream_tag,
573 unsigned int format,
574 struct snd_pcm_substream *substream)
575{
576 struct intel_hdmi_spec *spec = codec->spec;
577
578 snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
579 format, substream);
580
581 hdmi_set_channel_count(codec, substream->runtime->channels);
582
Wu, Fengguang91504872008-11-05 11:16:56 +0800583 hdmi_setup_audio_infoframe(codec, substream);
584
585 hdmi_enable_output(codec);
586
587 return 0;
588}
589
590static struct hda_pcm_stream intel_hdmi_pcm_playback = {
591 .substreams = 1,
592 .channels_min = 2,
593 .channels_max = 8,
594 .nid = CVT_NID, /* NID to query formats and rates and setup streams */
595 .ops = {
Wu Fengguangb83923a2008-11-22 09:40:51 +0800596 .open = intel_hdmi_playback_pcm_open,
597 .close = intel_hdmi_playback_pcm_close,
Wu, Fengguang91504872008-11-05 11:16:56 +0800598 .prepare = intel_hdmi_playback_pcm_prepare
599 },
600};
601
602static int intel_hdmi_build_pcms(struct hda_codec *codec)
603{
604 struct intel_hdmi_spec *spec = codec->spec;
605 struct hda_pcm *info = &spec->pcm_rec;
606
607 codec->num_pcms = 1;
608 codec->pcm_info = info;
609
610 info->name = "INTEL HDMI";
611 info->pcm_type = HDA_PCM_TYPE_HDMI;
612 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
613
614 return 0;
615}
616
617static int intel_hdmi_build_controls(struct hda_codec *codec)
618{
619 struct intel_hdmi_spec *spec = codec->spec;
620 int err;
621
622 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
623 if (err < 0)
624 return err;
625
626 return 0;
627}
628
629static int intel_hdmi_init(struct hda_codec *codec)
630{
631 /* disable audio output as early as possible */
632 hdmi_disable_output(codec);
633
634 snd_hda_sequence_write(codec, unsolicited_response_verb);
635
636 return 0;
637}
638
639static void intel_hdmi_free(struct hda_codec *codec)
640{
Takashi Iwaif208dba2008-11-21 09:11:50 +0100641 struct intel_hdmi_spec *spec = codec->spec;
642
643 snd_hda_eld_proc_free(codec, &spec->sink_eld);
644 kfree(spec);
Wu, Fengguang91504872008-11-05 11:16:56 +0800645}
646
647static struct hda_codec_ops intel_hdmi_patch_ops = {
648 .init = intel_hdmi_init,
649 .free = intel_hdmi_free,
650 .build_pcms = intel_hdmi_build_pcms,
651 .build_controls = intel_hdmi_build_controls,
652 .unsol_event = intel_hdmi_unsol_event,
653};
654
655static int patch_intel_hdmi(struct hda_codec *codec)
656{
657 struct intel_hdmi_spec *spec;
658
659 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
660 if (spec == NULL)
661 return -ENOMEM;
662
663 spec->multiout.num_dacs = 0; /* no analog */
664 spec->multiout.max_channels = 8;
665 spec->multiout.dig_out_nid = CVT_NID;
666
667 codec->spec = spec;
668 codec->patch_ops = intel_hdmi_patch_ops;
669
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800670 snd_hda_eld_proc_new(codec, &spec->sink_eld);
Wu Fengguang5f1e71b2008-11-18 11:47:53 +0800671
Wu Fengguang698544d2008-11-19 08:56:17 +0800672 init_channel_allocations();
673
Wu, Fengguang91504872008-11-05 11:16:56 +0800674 return 0;
675}
676
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100677static struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
Takashi Iwai74c61132008-12-18 09:11:33 +0100678 { .id = 0x808629fb, .name = "G45 DEVCL", .patch = patch_intel_hdmi },
679 { .id = 0x80862801, .name = "G45 DEVBLC", .patch = patch_intel_hdmi },
680 { .id = 0x80862802, .name = "G45 DEVCTG", .patch = patch_intel_hdmi },
681 { .id = 0x80862803, .name = "G45 DEVELK", .patch = patch_intel_hdmi },
Wu Fengguang3a95cb92008-11-13 10:19:38 +0800682 { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
Wu, Fengguang91504872008-11-05 11:16:56 +0800683 {} /* terminator */
684};
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100685
686MODULE_ALIAS("snd-hda-codec-id:808629fb");
687MODULE_ALIAS("snd-hda-codec-id:80862801");
688MODULE_ALIAS("snd-hda-codec-id:80862802");
689MODULE_ALIAS("snd-hda-codec-id:80862803");
690MODULE_ALIAS("snd-hda-codec-id:10951392");
691
692MODULE_LICENSE("GPL");
693MODULE_DESCRIPTION("Intel HDMI HD-audio codec");
694
695static struct hda_codec_preset_list intel_list = {
696 .preset = snd_hda_preset_intelhdmi,
697 .owner = THIS_MODULE,
698};
699
700static int __init patch_intelhdmi_init(void)
701{
702 return snd_hda_add_codec_preset(&intel_list);
703}
704
705static void __exit patch_intelhdmi_exit(void)
706{
707 snd_hda_delete_codec_preset(&intel_list);
708}
709
710module_init(patch_intelhdmi_init)
711module_exit(patch_intelhdmi_exit)