3 * patch_intelhdmi.c - Patch for Intel HDMI codecs
5 * Copyright(c) 2008 Intel Corporation. All rights reserved.
8 * Jiang Zhe <zhe.jiang@intel.com>
9 * Wu Fengguang <wfg@linux.intel.com>
12 * Wu Fengguang <wfg@linux.intel.com>
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)
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
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.
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/slab.h>
32 #include <sound/core.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35 #include "hda_patch.h"
37 #define CVT_NID 0x02 /* audio converter */
38 #define PIN_NID 0x03 /* HDMI output pin */
40 #define INTEL_HDMI_EVENT_TAG 0x08
42 struct intel_hdmi_spec
{
43 struct hda_multi_out multiout
;
44 struct hda_pcm pcm_rec
;
48 static struct hda_verb pinout_enable_verb
[] = {
49 {PIN_NID
, AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_OUT
},
53 static struct hda_verb pinout_disable_verb
[] = {
54 {PIN_NID
, AC_VERB_SET_PIN_WIDGET_CONTROL
, 0x00},
58 static struct hda_verb unsolicited_response_verb
[] = {
59 {PIN_NID
, AC_VERB_SET_UNSOLICITED_ENABLE
, AC_USRSP_EN
|
60 INTEL_HDMI_EVENT_TAG
},
64 static 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},
77 struct hdmi_audio_infoframe
{
82 u8 checksum
; /* PB0 */
83 u8 CC02_CT47
; /* CC in bits 0:2, CT in 4:7 */
87 u8 LFEPBL01_LSV36_DM_INH7
;
88 u8 reserved
[5]; /* PB6 - PB10 */
96 static void hdmi_get_dip_index(struct hda_codec
*codec
, hda_nid_t nid
,
97 int *packet_index
, int *byte_index
)
101 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_HDMI_DIP_INDEX
, 0);
103 *packet_index
= val
>> 5;
104 *byte_index
= val
& 0x1f;
108 static void hdmi_set_dip_index(struct hda_codec
*codec
, hda_nid_t nid
,
109 int packet_index
, int byte_index
)
113 val
= (packet_index
<< 5) | (byte_index
& 0x1f);
115 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_HDMI_DIP_INDEX
, val
);
118 static void hdmi_write_dip_byte(struct hda_codec
*codec
, hda_nid_t nid
,
121 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_HDMI_DIP_DATA
, val
);
124 static void hdmi_enable_output(struct hda_codec
*codec
)
126 /* 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
,
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
);
135 snd_hda_sequence_write(codec
, pinout_enable_verb
);
138 static void hdmi_disable_output(struct hda_codec
*codec
)
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
);
146 * FIXME: noises may arise when playing music after reloading the
147 * kernel module, until the next X restart or monitor repower.
151 static int hdmi_get_channel_count(struct hda_codec
*codec
)
153 return 1 + snd_hda_codec_read(codec
, CVT_NID
, 0,
154 AC_VERB_GET_CVT_CHAN_COUNT
, 0);
157 static void hdmi_set_channel_count(struct hda_codec
*codec
, int chs
)
159 snd_hda_codec_write(codec
, CVT_NID
, 0,
160 AC_VERB_SET_CVT_CHAN_COUNT
, chs
- 1);
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
));
167 static void hdmi_debug_slot_mapping(struct hda_codec
*codec
)
169 #ifdef CONFIG_SND_DEBUG_VERBOSE
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);
182 static void hdmi_setup_channel_mapping(struct hda_codec
*codec
)
184 snd_hda_sequence_write(codec
, def_chan_map
);
185 hdmi_debug_slot_mapping(codec
);
189 static void hdmi_parse_eld(struct hda_codec
*codec
)
191 struct intel_hdmi_spec
*spec
= codec
->spec
;
192 struct sink_eld
*eld
= &spec
->sink
;
194 if (!snd_hdmi_get_eld(eld
, codec
, PIN_NID
))
195 snd_hdmi_show_eld(eld
);
200 * Audio Infoframe routines
203 static void hdmi_debug_dip_size(struct hda_codec
*codec
)
205 #ifdef CONFIG_SND_DEBUG_VERBOSE
209 size
= snd_hdmi_get_eld_size(codec
, PIN_NID
);
210 printk(KERN_DEBUG
"ELD buf size is %d\n", size
);
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
);
220 static void hdmi_clear_dip_buffers(struct hda_codec
*codec
)
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
);
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
);
237 snd_printd(KERN_INFO
"dip index %d: %d != %d\n",
239 if (bi
== 0) /* byte index wrapped around */
243 "DIP GP[%d] buf reported size=%d, written=%d\n",
249 static void hdmi_setup_audio_infoframe(struct hda_codec
*codec
,
250 struct snd_pcm_substream
*substream
)
252 struct hdmi_audio_infoframe audio_infoframe
= {
256 .CC02_CT47
= substream
->runtime
->channels
- 1,
258 u8
*params
= (u8
*)&audio_infoframe
;
261 hdmi_debug_dip_size(codec
);
262 hdmi_clear_dip_buffers(codec
); /* be paranoid */
264 hdmi_set_dip_index(codec
, PIN_NID
, 0x0, 0x0);
265 for (i
= 0; i
< sizeof(audio_infoframe
); i
++)
266 hdmi_write_dip_byte(codec
, PIN_NID
, params
[i
]);
274 static void hdmi_intrinsic_event(struct hda_codec
*codec
, unsigned int res
)
276 int pind
= !!(res
& AC_UNSOL_RES_PD
);
277 int eldv
= !!(res
& AC_UNSOL_RES_ELDV
);
279 printk(KERN_INFO
"HDMI intrinsic event: PD=%d ELDV=%d\n", pind
, eldv
);
282 hdmi_parse_eld(codec
);
283 /* TODO: do real things about ELD */
287 static void hdmi_non_intrinsic_event(struct hda_codec
*codec
, unsigned int res
)
289 int subtag
= (res
& AC_UNSOL_RES_SUBTAG
) >> AC_UNSOL_RES_SUBTAG_SHIFT
;
290 int cp_state
= !!(res
& AC_UNSOL_RES_CP_STATE
);
291 int cp_ready
= !!(res
& AC_UNSOL_RES_CP_READY
);
293 printk(KERN_INFO
"HDMI non-intrinsic event: "
294 "SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
307 static void intel_hdmi_unsol_event(struct hda_codec
*codec
, unsigned int res
)
309 int tag
= res
>> AC_UNSOL_RES_TAG_SHIFT
;
310 int subtag
= (res
& AC_UNSOL_RES_SUBTAG
) >> AC_UNSOL_RES_SUBTAG_SHIFT
;
312 if (tag
!= INTEL_HDMI_EVENT_TAG
) {
314 "Unexpected HDMI unsolicited event tag 0x%x\n",
320 hdmi_intrinsic_event(codec
, res
);
322 hdmi_non_intrinsic_event(codec
, res
);
329 static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
330 struct hda_codec
*codec
,
331 struct snd_pcm_substream
*substream
)
333 struct intel_hdmi_spec
*spec
= codec
->spec
;
335 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
338 static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
339 struct hda_codec
*codec
,
340 struct snd_pcm_substream
*substream
)
342 struct intel_hdmi_spec
*spec
= codec
->spec
;
344 hdmi_disable_output(codec
);
346 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
349 static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
350 struct hda_codec
*codec
,
351 unsigned int stream_tag
,
353 struct snd_pcm_substream
*substream
)
355 struct intel_hdmi_spec
*spec
= codec
->spec
;
357 snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
, stream_tag
,
360 hdmi_set_channel_count(codec
, substream
->runtime
->channels
);
362 /* wfg: channel mapping not supported by DEVCTG */
363 hdmi_setup_channel_mapping(codec
);
365 hdmi_setup_audio_infoframe(codec
, substream
);
367 hdmi_enable_output(codec
);
372 static struct hda_pcm_stream intel_hdmi_pcm_playback
= {
376 .nid
= CVT_NID
, /* NID to query formats and rates and setup streams */
378 .open
= intel_hdmi_playback_pcm_open
,
379 .close
= intel_hdmi_playback_pcm_close
,
380 .prepare
= intel_hdmi_playback_pcm_prepare
384 static int intel_hdmi_build_pcms(struct hda_codec
*codec
)
386 struct intel_hdmi_spec
*spec
= codec
->spec
;
387 struct hda_pcm
*info
= &spec
->pcm_rec
;
390 codec
->pcm_info
= info
;
392 info
->name
= "INTEL HDMI";
393 info
->pcm_type
= HDA_PCM_TYPE_HDMI
;
394 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = intel_hdmi_pcm_playback
;
399 static int intel_hdmi_build_controls(struct hda_codec
*codec
)
401 struct intel_hdmi_spec
*spec
= codec
->spec
;
404 err
= snd_hda_create_spdif_out_ctls(codec
, spec
->multiout
.dig_out_nid
);
411 static int intel_hdmi_init(struct hda_codec
*codec
)
413 /* disable audio output as early as possible */
414 hdmi_disable_output(codec
);
416 snd_hda_sequence_write(codec
, unsolicited_response_verb
);
421 static void intel_hdmi_free(struct hda_codec
*codec
)
426 static struct hda_codec_ops intel_hdmi_patch_ops
= {
427 .init
= intel_hdmi_init
,
428 .free
= intel_hdmi_free
,
429 .build_pcms
= intel_hdmi_build_pcms
,
430 .build_controls
= intel_hdmi_build_controls
,
431 .unsol_event
= intel_hdmi_unsol_event
,
434 static int patch_intel_hdmi(struct hda_codec
*codec
)
436 struct intel_hdmi_spec
*spec
;
438 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
442 spec
->multiout
.num_dacs
= 0; /* no analog */
443 spec
->multiout
.max_channels
= 8;
444 spec
->multiout
.dig_out_nid
= CVT_NID
;
447 codec
->patch_ops
= intel_hdmi_patch_ops
;
449 snd_hda_eld_proc_new(codec
, &spec
->sink
);
454 struct hda_codec_preset snd_hda_preset_intelhdmi
[] = {
455 { .id
= 0x808629fb, .name
= "INTEL G45 DEVCL", .patch
= patch_intel_hdmi
},
456 { .id
= 0x80862801, .name
= "INTEL G45 DEVBLC", .patch
= patch_intel_hdmi
},
457 { .id
= 0x80862802, .name
= "INTEL G45 DEVCTG", .patch
= patch_intel_hdmi
},
458 { .id
= 0x80862803, .name
= "INTEL G45 DEVELK", .patch
= patch_intel_hdmi
},
459 { .id
= 0x10951392, .name
= "SiI1392 HDMI", .patch
= patch_intel_hdmi
},