blob: 028fce996aa245a38c5941fb4413642477b92096 [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;
45 struct sink_eld sink;
46};
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;
88 u8 reserved[5]; /* PB6 - PB10 */
89};
90
91/*
Wu, Fengguang91504872008-11-05 11:16:56 +080092 * HDMI routines
93 */
94
Takashi Iwaibeb0b9c2008-11-05 07:58:25 +010095#ifdef BE_PARANOID
Wu, Fengguang91504872008-11-05 11:16:56 +080096static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t nid,
97 int *packet_index, int *byte_index)
98{
99 int val;
100
101 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_INDEX, 0);
102
103 *packet_index = val >> 5;
104 *byte_index = val & 0x1f;
105}
Takashi Iwaibeb0b9c2008-11-05 07:58:25 +0100106#endif
Wu, Fengguang91504872008-11-05 11:16:56 +0800107
108static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t nid,
109 int packet_index, int byte_index)
110{
111 int val;
112
113 val = (packet_index << 5) | (byte_index & 0x1f);
114
115 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
116}
117
118static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t nid,
119 unsigned char val)
120{
121 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
122}
123
124static void hdmi_enable_output(struct hda_codec *codec)
125{
Wu, Fengguang91504872008-11-05 11:16:56 +0800126 /* Enable Audio InfoFrame Transmission */
127 hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
128 snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
129 AC_DIPXMIT_BEST);
Wu Fengguang796359d2008-11-17 16:57:33 +0800130 /* Unmute */
131 if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
132 snd_hda_codec_write(codec, PIN_NID, 0,
133 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
134 /* Enable pin out */
135 snd_hda_sequence_write(codec, pinout_enable_verb);
Wu, Fengguang91504872008-11-05 11:16:56 +0800136}
137
138static void hdmi_disable_output(struct hda_codec *codec)
139{
140 snd_hda_sequence_write(codec, pinout_disable_verb);
141 if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
142 snd_hda_codec_write(codec, PIN_NID, 0,
143 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
144
145 /*
146 * FIXME: noises may arise when playing music after reloading the
147 * kernel module, until the next X restart or monitor repower.
148 */
149}
150
151static int hdmi_get_channel_count(struct hda_codec *codec)
152{
153 return 1 + snd_hda_codec_read(codec, CVT_NID, 0,
154 AC_VERB_GET_CVT_CHAN_COUNT, 0);
155}
156
157static void hdmi_set_channel_count(struct hda_codec *codec, int chs)
158{
159 snd_hda_codec_write(codec, CVT_NID, 0,
160 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
161
162 if (chs != hdmi_get_channel_count(codec))
163 snd_printd(KERN_INFO "Channel count expect=%d, real=%d\n",
164 chs, hdmi_get_channel_count(codec));
165}
166
167static void hdmi_debug_slot_mapping(struct hda_codec *codec)
168{
169#ifdef CONFIG_SND_DEBUG_VERBOSE
170 int i;
171 int slot;
172
173 for (i = 0; i < 8; i++) {
174 slot = snd_hda_codec_read(codec, CVT_NID, 0,
175 AC_VERB_GET_HDMI_CHAN_SLOT, i);
176 printk(KERN_DEBUG "ASP channel %d => slot %d\n",
177 slot >> 4, slot & 0x7);
178 }
179#endif
180}
181
182static void hdmi_setup_channel_mapping(struct hda_codec *codec)
183{
184 snd_hda_sequence_write(codec, def_chan_map);
185 hdmi_debug_slot_mapping(codec);
186}
187
188
Wu, Fengguang91504872008-11-05 11:16:56 +0800189static void hdmi_parse_eld(struct hda_codec *codec)
190{
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800191 struct intel_hdmi_spec *spec = codec->spec;
192 struct sink_eld *eld = &spec->sink;
Wu, Fengguang91504872008-11-05 11:16:56 +0800193
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800194 if (!snd_hdmi_get_eld(eld, codec, PIN_NID))
195 snd_hdmi_show_eld(eld);
Wu, Fengguang91504872008-11-05 11:16:56 +0800196}
197
198
199/*
200 * Audio Infoframe routines
201 */
202
203static void hdmi_debug_dip_size(struct hda_codec *codec)
204{
205#ifdef CONFIG_SND_DEBUG_VERBOSE
206 int i;
207 int size;
208
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800209 size = snd_hdmi_get_eld_size(codec, PIN_NID);
Wu, Fengguang91504872008-11-05 11:16:56 +0800210 printk(KERN_DEBUG "ELD buf size is %d\n", size);
211
212 for (i = 0; i < 8; i++) {
213 size = snd_hda_codec_read(codec, PIN_NID, 0,
214 AC_VERB_GET_HDMI_DIP_SIZE, i);
215 printk(KERN_DEBUG "DIP GP[%d] buf size is %d\n", i, size);
216 }
217#endif
218}
219
220static void hdmi_clear_dip_buffers(struct hda_codec *codec)
221{
222#ifdef BE_PARANOID
223 int i, j;
224 int size;
225 int pi, bi;
226 for (i = 0; i < 8; i++) {
227 size = snd_hda_codec_read(codec, PIN_NID, 0,
228 AC_VERB_GET_HDMI_DIP_SIZE, i);
229 if (size == 0)
230 continue;
231
232 hdmi_set_dip_index(codec, PIN_NID, i, 0x0);
233 for (j = 1; j < 1000; j++) {
234 hdmi_write_dip_byte(codec, PIN_NID, 0x0);
235 hdmi_get_dip_index(codec, PIN_NID, &pi, &bi);
236 if (pi != i)
237 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
238 bi, pi, i);
239 if (bi == 0) /* byte index wrapped around */
240 break;
241 }
242 snd_printd(KERN_INFO
243 "DIP GP[%d] buf reported size=%d, written=%d\n",
244 i, size, j);
245 }
246#endif
247}
248
Wu Fengguang5457a982008-11-19 08:56:15 +0800249static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
250 struct hdmi_audio_infoframe *ai)
Wu, Fengguang91504872008-11-05 11:16:56 +0800251{
Wu Fengguang5457a982008-11-19 08:56:15 +0800252 u8 *params = (u8 *)ai;
Wu, Fengguang91504872008-11-05 11:16:56 +0800253 int i;
254
255 hdmi_debug_dip_size(codec);
256 hdmi_clear_dip_buffers(codec); /* be paranoid */
257
258 hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
Wu Fengguang5457a982008-11-19 08:56:15 +0800259 for (i = 0; i < sizeof(ai); i++)
Wu, Fengguang91504872008-11-05 11:16:56 +0800260 hdmi_write_dip_byte(codec, PIN_NID, params[i]);
261}
262
Wu Fengguang5457a982008-11-19 08:56:15 +0800263static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
264 struct snd_pcm_substream *substream)
265{
266 struct hdmi_audio_infoframe ai = {
267 .type = 0x84,
268 .ver = 0x01,
269 .len = 0x0a,
270 .CC02_CT47 = substream->runtime->channels - 1,
271 };
272
273 hdmi_fill_audio_infoframe(codec, &ai);
274}
275
Wu, Fengguang91504872008-11-05 11:16:56 +0800276
277/*
278 * Unsolicited events
279 */
280
281static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
282{
283 int pind = !!(res & AC_UNSOL_RES_PD);
284 int eldv = !!(res & AC_UNSOL_RES_ELDV);
285
286 printk(KERN_INFO "HDMI intrinsic event: PD=%d ELDV=%d\n", pind, eldv);
287
288 if (pind && eldv) {
289 hdmi_parse_eld(codec);
290 /* TODO: do real things about ELD */
291 }
292}
293
294static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
295{
296 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
297 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
298 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
299
300 printk(KERN_INFO "HDMI non-intrinsic event: "
301 "SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
302 subtag,
303 cp_state,
304 cp_ready);
305
306 /* who cares? */
307 if (cp_state)
308 ;
309 if (cp_ready)
310 ;
311}
312
313
314static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
315{
316 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
317 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
318
319 if (tag != INTEL_HDMI_EVENT_TAG) {
320 snd_printd(KERN_INFO
321 "Unexpected HDMI unsolicited event tag 0x%x\n",
322 tag);
323 return;
324 }
325
326 if (subtag == 0)
327 hdmi_intrinsic_event(codec, res);
328 else
329 hdmi_non_intrinsic_event(codec, res);
330}
331
332/*
333 * Callbacks
334 */
335
336static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream *hinfo,
337 struct hda_codec *codec,
338 struct snd_pcm_substream *substream)
339{
340 struct intel_hdmi_spec *spec = codec->spec;
341
342 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
343}
344
345static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream *hinfo,
346 struct hda_codec *codec,
347 struct snd_pcm_substream *substream)
348{
349 struct intel_hdmi_spec *spec = codec->spec;
350
351 hdmi_disable_output(codec);
352
353 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
354}
355
356static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
357 struct hda_codec *codec,
358 unsigned int stream_tag,
359 unsigned int format,
360 struct snd_pcm_substream *substream)
361{
362 struct intel_hdmi_spec *spec = codec->spec;
363
364 snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
365 format, substream);
366
367 hdmi_set_channel_count(codec, substream->runtime->channels);
368
369 /* wfg: channel mapping not supported by DEVCTG */
370 hdmi_setup_channel_mapping(codec);
371
372 hdmi_setup_audio_infoframe(codec, substream);
373
374 hdmi_enable_output(codec);
375
376 return 0;
377}
378
379static struct hda_pcm_stream intel_hdmi_pcm_playback = {
380 .substreams = 1,
381 .channels_min = 2,
382 .channels_max = 8,
383 .nid = CVT_NID, /* NID to query formats and rates and setup streams */
384 .ops = {
385 .open = intel_hdmi_playback_pcm_open,
386 .close = intel_hdmi_playback_pcm_close,
387 .prepare = intel_hdmi_playback_pcm_prepare
388 },
389};
390
391static int intel_hdmi_build_pcms(struct hda_codec *codec)
392{
393 struct intel_hdmi_spec *spec = codec->spec;
394 struct hda_pcm *info = &spec->pcm_rec;
395
396 codec->num_pcms = 1;
397 codec->pcm_info = info;
398
399 info->name = "INTEL HDMI";
400 info->pcm_type = HDA_PCM_TYPE_HDMI;
401 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
402
403 return 0;
404}
405
406static int intel_hdmi_build_controls(struct hda_codec *codec)
407{
408 struct intel_hdmi_spec *spec = codec->spec;
409 int err;
410
411 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
412 if (err < 0)
413 return err;
414
415 return 0;
416}
417
418static int intel_hdmi_init(struct hda_codec *codec)
419{
420 /* disable audio output as early as possible */
421 hdmi_disable_output(codec);
422
423 snd_hda_sequence_write(codec, unsolicited_response_verb);
424
425 return 0;
426}
427
428static void intel_hdmi_free(struct hda_codec *codec)
429{
430 kfree(codec->spec);
431}
432
433static struct hda_codec_ops intel_hdmi_patch_ops = {
434 .init = intel_hdmi_init,
435 .free = intel_hdmi_free,
436 .build_pcms = intel_hdmi_build_pcms,
437 .build_controls = intel_hdmi_build_controls,
438 .unsol_event = intel_hdmi_unsol_event,
439};
440
441static int patch_intel_hdmi(struct hda_codec *codec)
442{
443 struct intel_hdmi_spec *spec;
444
445 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
446 if (spec == NULL)
447 return -ENOMEM;
448
449 spec->multiout.num_dacs = 0; /* no analog */
450 spec->multiout.max_channels = 8;
451 spec->multiout.dig_out_nid = CVT_NID;
452
453 codec->spec = spec;
454 codec->patch_ops = intel_hdmi_patch_ops;
455
Wu Fengguang5f1e71b2008-11-18 11:47:53 +0800456 snd_hda_eld_proc_new(codec, &spec->sink);
457
Wu, Fengguang91504872008-11-05 11:16:56 +0800458 return 0;
459}
460
461struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
462 { .id = 0x808629fb, .name = "INTEL G45 DEVCL", .patch = patch_intel_hdmi },
463 { .id = 0x80862801, .name = "INTEL G45 DEVBLC", .patch = patch_intel_hdmi },
464 { .id = 0x80862802, .name = "INTEL G45 DEVCTG", .patch = patch_intel_hdmi },
465 { .id = 0x80862803, .name = "INTEL G45 DEVELK", .patch = patch_intel_hdmi },
Wu Fengguang3a95cb92008-11-13 10:19:38 +0800466 { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
Wu, Fengguang91504872008-11-05 11:16:56 +0800467 {} /* terminator */
468};