032850eba369f3469be88734e218dec040c41863
[pandora-kernel.git] / sound / pci / hda / patch_intelhdmi.c
1 /*
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>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35
36 #define CVT_NID         0x02    /* audio converter */
37 #define PIN_NID         0x03    /* HDMI output pin */
38
39 #define INTEL_HDMI_EVENT_TAG            0x08
40
41 struct intel_hdmi_spec {
42         struct hda_multi_out multiout;
43         struct hda_pcm pcm_rec;
44         struct hdmi_eld sink_eld;
45 };
46
47 static struct hda_verb pinout_enable_verb[] = {
48         {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
49         {} /* terminator */
50 };
51
52 static struct hda_verb unsolicited_response_verb[] = {
53         {PIN_NID, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN |
54                                                   INTEL_HDMI_EVENT_TAG},
55         {}
56 };
57
58 static struct hda_verb def_chan_map[] = {
59         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x00},
60         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x11},
61         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x22},
62         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x33},
63         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x44},
64         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x55},
65         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x66},
66         {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x77},
67         {}
68 };
69
70
71 struct hdmi_audio_infoframe {
72         u8 type; /* 0x84 */
73         u8 ver;  /* 0x01 */
74         u8 len;  /* 0x0a */
75
76         u8 checksum;    /* PB0 */
77         u8 CC02_CT47;   /* CC in bits 0:2, CT in 4:7 */
78         u8 SS01_SF24;
79         u8 CXT04;
80         u8 CA;
81         u8 LFEPBL01_LSV36_DM_INH7;
82         u8 reserved[5]; /* PB6 - PB10 */
83 };
84
85 /*
86  * CEA speaker placement:
87  *
88  *        FLH       FCH        FRH
89  *  FLW    FL  FLC   FC   FRC   FR   FRW
90  *
91  *                                  LFE
92  *                     TC
93  *
94  *          RL  RLC   RC   RRC   RR
95  *
96  * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
97  * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
98  */
99 enum cea_speaker_placement {
100         FL  = (1 <<  0),        /* Front Left           */
101         FC  = (1 <<  1),        /* Front Center         */
102         FR  = (1 <<  2),        /* Front Right          */
103         FLC = (1 <<  3),        /* Front Left Center    */
104         FRC = (1 <<  4),        /* Front Right Center   */
105         RL  = (1 <<  5),        /* Rear Left            */
106         RC  = (1 <<  6),        /* Rear Center          */
107         RR  = (1 <<  7),        /* Rear Right           */
108         RLC = (1 <<  8),        /* Rear Left Center     */
109         RRC = (1 <<  9),        /* Rear Right Center    */
110         LFE = (1 << 10),        /* Low Frequency Effect */
111         FLW = (1 << 11),        /* Front Left Wide      */
112         FRW = (1 << 12),        /* Front Right Wide     */
113         FLH = (1 << 13),        /* Front Left High      */
114         FCH = (1 << 14),        /* Front Center High    */
115         FRH = (1 << 15),        /* Front Right High     */
116         TC  = (1 << 16),        /* Top Center           */
117 };
118
119 /*
120  * ELD SA bits in the CEA Speaker Allocation data block
121  */
122 static int eld_speaker_allocation_bits[] = {
123         [0] = FL | FR,
124         [1] = LFE,
125         [2] = FC,
126         [3] = RL | RR,
127         [4] = RC,
128         [5] = FLC | FRC,
129         [6] = RLC | RRC,
130         /* the following are not defined in ELD yet */
131         [7] = FLW | FRW,
132         [8] = FLH | FRH,
133         [9] = TC,
134         [10] = FCH,
135 };
136
137 struct cea_channel_speaker_allocation {
138         int ca_index;
139         int speakers[8];
140
141         /* derived values, just for convenience */
142         int channels;
143         int spk_mask;
144 };
145
146 /*
147  * This is an ordered list!
148  *
149  * The preceding ones have better chances to be selected by
150  * hdmi_setup_channel_allocation().
151  */
152 static struct cea_channel_speaker_allocation channel_allocations[] = {
153 /*                        channel:   8     7    6    5    4     3    2    1  */
154 { .ca_index = 0x00,  .speakers = {   0,    0,   0,   0,   0,    0,  FR,  FL } },
155                                  /* 2.1 */
156 { .ca_index = 0x01,  .speakers = {   0,    0,   0,   0,   0,  LFE,  FR,  FL } },
157                                  /* Dolby Surround */
158 { .ca_index = 0x02,  .speakers = {   0,    0,   0,   0,  FC,    0,  FR,  FL } },
159 { .ca_index = 0x03,  .speakers = {   0,    0,   0,   0,  FC,  LFE,  FR,  FL } },
160 { .ca_index = 0x04,  .speakers = {   0,    0,   0,  RC,   0,    0,  FR,  FL } },
161 { .ca_index = 0x05,  .speakers = {   0,    0,   0,  RC,   0,  LFE,  FR,  FL } },
162 { .ca_index = 0x06,  .speakers = {   0,    0,   0,  RC,  FC,    0,  FR,  FL } },
163 { .ca_index = 0x07,  .speakers = {   0,    0,   0,  RC,  FC,  LFE,  FR,  FL } },
164 { .ca_index = 0x08,  .speakers = {   0,    0,  RR,  RL,   0,    0,  FR,  FL } },
165 { .ca_index = 0x09,  .speakers = {   0,    0,  RR,  RL,   0,  LFE,  FR,  FL } },
166 { .ca_index = 0x0a,  .speakers = {   0,    0,  RR,  RL,  FC,    0,  FR,  FL } },
167                                  /* 5.1 */
168 { .ca_index = 0x0b,  .speakers = {   0,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
169 { .ca_index = 0x0c,  .speakers = {   0,   RC,  RR,  RL,   0,    0,  FR,  FL } },
170 { .ca_index = 0x0d,  .speakers = {   0,   RC,  RR,  RL,   0,  LFE,  FR,  FL } },
171 { .ca_index = 0x0e,  .speakers = {   0,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
172                                  /* 6.1 */
173 { .ca_index = 0x0f,  .speakers = {   0,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
174 { .ca_index = 0x10,  .speakers = { RRC,  RLC,  RR,  RL,   0,    0,  FR,  FL } },
175 { .ca_index = 0x11,  .speakers = { RRC,  RLC,  RR,  RL,   0,  LFE,  FR,  FL } },
176 { .ca_index = 0x12,  .speakers = { RRC,  RLC,  RR,  RL,  FC,    0,  FR,  FL } },
177                                  /* 7.1 */
178 { .ca_index = 0x13,  .speakers = { RRC,  RLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
179 { .ca_index = 0x14,  .speakers = { FRC,  FLC,   0,   0,   0,    0,  FR,  FL } },
180 { .ca_index = 0x15,  .speakers = { FRC,  FLC,   0,   0,   0,  LFE,  FR,  FL } },
181 { .ca_index = 0x16,  .speakers = { FRC,  FLC,   0,   0,  FC,    0,  FR,  FL } },
182 { .ca_index = 0x17,  .speakers = { FRC,  FLC,   0,   0,  FC,  LFE,  FR,  FL } },
183 { .ca_index = 0x18,  .speakers = { FRC,  FLC,   0,  RC,   0,    0,  FR,  FL } },
184 { .ca_index = 0x19,  .speakers = { FRC,  FLC,   0,  RC,   0,  LFE,  FR,  FL } },
185 { .ca_index = 0x1a,  .speakers = { FRC,  FLC,   0,  RC,  FC,    0,  FR,  FL } },
186 { .ca_index = 0x1b,  .speakers = { FRC,  FLC,   0,  RC,  FC,  LFE,  FR,  FL } },
187 { .ca_index = 0x1c,  .speakers = { FRC,  FLC,  RR,  RL,   0,    0,  FR,  FL } },
188 { .ca_index = 0x1d,  .speakers = { FRC,  FLC,  RR,  RL,   0,  LFE,  FR,  FL } },
189 { .ca_index = 0x1e,  .speakers = { FRC,  FLC,  RR,  RL,  FC,    0,  FR,  FL } },
190 { .ca_index = 0x1f,  .speakers = { FRC,  FLC,  RR,  RL,  FC,  LFE,  FR,  FL } },
191 { .ca_index = 0x20,  .speakers = {   0,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
192 { .ca_index = 0x21,  .speakers = {   0,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
193 { .ca_index = 0x22,  .speakers = {  TC,    0,  RR,  RL,  FC,    0,  FR,  FL } },
194 { .ca_index = 0x23,  .speakers = {  TC,    0,  RR,  RL,  FC,  LFE,  FR,  FL } },
195 { .ca_index = 0x24,  .speakers = { FRH,  FLH,  RR,  RL,   0,    0,  FR,  FL } },
196 { .ca_index = 0x25,  .speakers = { FRH,  FLH,  RR,  RL,   0,  LFE,  FR,  FL } },
197 { .ca_index = 0x26,  .speakers = { FRW,  FLW,  RR,  RL,   0,    0,  FR,  FL } },
198 { .ca_index = 0x27,  .speakers = { FRW,  FLW,  RR,  RL,   0,  LFE,  FR,  FL } },
199 { .ca_index = 0x28,  .speakers = {  TC,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
200 { .ca_index = 0x29,  .speakers = {  TC,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
201 { .ca_index = 0x2a,  .speakers = { FCH,   RC,  RR,  RL,  FC,    0,  FR,  FL } },
202 { .ca_index = 0x2b,  .speakers = { FCH,   RC,  RR,  RL,  FC,  LFE,  FR,  FL } },
203 { .ca_index = 0x2c,  .speakers = {  TC,  FCH,  RR,  RL,  FC,    0,  FR,  FL } },
204 { .ca_index = 0x2d,  .speakers = {  TC,  FCH,  RR,  RL,  FC,  LFE,  FR,  FL } },
205 { .ca_index = 0x2e,  .speakers = { FRH,  FLH,  RR,  RL,  FC,    0,  FR,  FL } },
206 { .ca_index = 0x2f,  .speakers = { FRH,  FLH,  RR,  RL,  FC,  LFE,  FR,  FL } },
207 { .ca_index = 0x30,  .speakers = { FRW,  FLW,  RR,  RL,  FC,    0,  FR,  FL } },
208 { .ca_index = 0x31,  .speakers = { FRW,  FLW,  RR,  RL,  FC,  LFE,  FR,  FL } },
209 };
210
211 /*
212  * HDMI routines
213  */
214
215 #ifdef BE_PARANOID
216 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t nid,
217                                 int *packet_index, int *byte_index)
218 {
219         int val;
220
221         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_INDEX, 0);
222
223         *packet_index = val >> 5;
224         *byte_index = val & 0x1f;
225 }
226 #endif
227
228 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t nid,
229                                 int packet_index, int byte_index)
230 {
231         int val;
232
233         val = (packet_index << 5) | (byte_index & 0x1f);
234
235         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
236 }
237
238 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t nid,
239                                 unsigned char val)
240 {
241         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
242 }
243
244 static void hdmi_enable_output(struct hda_codec *codec)
245 {
246         /* Unmute */
247         if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
248                 snd_hda_codec_write(codec, PIN_NID, 0,
249                                 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
250         /* Enable pin out */
251         snd_hda_sequence_write(codec, pinout_enable_verb);
252 }
253
254 /*
255  * Enable Audio InfoFrame Transmission
256  */
257 static void hdmi_start_infoframe_trans(struct hda_codec *codec)
258 {
259         hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
260         snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
261                                                 AC_DIPXMIT_BEST);
262 }
263
264 /*
265  * Disable Audio InfoFrame Transmission
266  */
267 static void hdmi_stop_infoframe_trans(struct hda_codec *codec)
268 {
269         hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
270         snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
271                                                 AC_DIPXMIT_DISABLE);
272 }
273
274 static int hdmi_get_channel_count(struct hda_codec *codec)
275 {
276         return 1 + snd_hda_codec_read(codec, CVT_NID, 0,
277                                         AC_VERB_GET_CVT_CHAN_COUNT, 0);
278 }
279
280 static void hdmi_set_channel_count(struct hda_codec *codec, int chs)
281 {
282         snd_hda_codec_write(codec, CVT_NID, 0,
283                                         AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
284
285         if (chs != hdmi_get_channel_count(codec))
286                 snd_printd(KERN_INFO "HDMI channel count: expect %d, get %d\n",
287                                         chs, hdmi_get_channel_count(codec));
288 }
289
290 static void hdmi_debug_channel_mapping(struct hda_codec *codec)
291 {
292 #ifdef CONFIG_SND_DEBUG_VERBOSE
293         int i;
294         int slot;
295
296         for (i = 0; i < 8; i++) {
297                 slot = snd_hda_codec_read(codec, CVT_NID, 0,
298                                                 AC_VERB_GET_HDMI_CHAN_SLOT, i);
299                 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
300                                                 slot >> 4, slot & 0x7);
301         }
302 #endif
303 }
304
305 static void hdmi_parse_eld(struct hda_codec *codec)
306 {
307         struct intel_hdmi_spec *spec = codec->spec;
308         struct hdmi_eld *eld = &spec->sink_eld;
309
310         if (!snd_hdmi_get_eld(eld, codec, PIN_NID))
311                 snd_hdmi_show_eld(eld);
312 }
313
314
315 /*
316  * Audio InfoFrame routines
317  */
318
319 static void hdmi_debug_dip_size(struct hda_codec *codec)
320 {
321 #ifdef CONFIG_SND_DEBUG_VERBOSE
322         int i;
323         int size;
324
325         size = snd_hdmi_get_eld_size(codec, PIN_NID);
326         printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
327
328         for (i = 0; i < 8; i++) {
329                 size = snd_hda_codec_read(codec, PIN_NID, 0,
330                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
331                 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
332         }
333 #endif
334 }
335
336 static void hdmi_clear_dip_buffers(struct hda_codec *codec)
337 {
338 #ifdef BE_PARANOID
339         int i, j;
340         int size;
341         int pi, bi;
342         for (i = 0; i < 8; i++) {
343                 size = snd_hda_codec_read(codec, PIN_NID, 0,
344                                                 AC_VERB_GET_HDMI_DIP_SIZE, i);
345                 if (size == 0)
346                         continue;
347
348                 hdmi_set_dip_index(codec, PIN_NID, i, 0x0);
349                 for (j = 1; j < 1000; j++) {
350                         hdmi_write_dip_byte(codec, PIN_NID, 0x0);
351                         hdmi_get_dip_index(codec, PIN_NID, &pi, &bi);
352                         if (pi != i)
353                                 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
354                                                 bi, pi, i);
355                         if (bi == 0) /* byte index wrapped around */
356                                 break;
357                 }
358                 snd_printd(KERN_INFO
359                         "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
360                         i, size, j);
361         }
362 #endif
363 }
364
365 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
366                                         struct hdmi_audio_infoframe *ai)
367 {
368         u8 *params = (u8 *)ai;
369         u8 sum = 0;
370         int i;
371
372         hdmi_debug_dip_size(codec);
373         hdmi_clear_dip_buffers(codec); /* be paranoid */
374
375         for (i = 0; i < sizeof(ai); i++)
376                 sum += params[i];
377         ai->checksum = - sum;
378
379         hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
380         for (i = 0; i < sizeof(ai); i++)
381                 hdmi_write_dip_byte(codec, PIN_NID, params[i]);
382 }
383
384 /*
385  * Compute derived values in channel_allocations[].
386  */
387 static void init_channel_allocations(void)
388 {
389         int i, j;
390         struct cea_channel_speaker_allocation *p;
391
392         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
393                 p = channel_allocations + i;
394                 p->channels = 0;
395                 p->spk_mask = 0;
396                 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
397                         if (p->speakers[j]) {
398                                 p->channels++;
399                                 p->spk_mask |= p->speakers[j];
400                         }
401         }
402 }
403
404 /*
405  * The transformation takes two steps:
406  *
407  *      eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
408  *            spk_mask => (channel_allocations[])         => ai->CA
409  *
410  * TODO: it could select the wrong CA from multiple candidates.
411 */
412 static int hdmi_setup_channel_allocation(struct hda_codec *codec,
413                                          struct hdmi_audio_infoframe *ai)
414 {
415         struct intel_hdmi_spec *spec = codec->spec;
416         struct hdmi_eld *eld = &spec->sink_eld;
417         int i;
418         int spk_mask = 0;
419         int channels = 1 + (ai->CC02_CT47 & 0x7);
420         char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
421
422         /*
423          * CA defaults to 0 for basic stereo audio
424          */
425         if (channels <= 2)
426                 return 0;
427
428         /*
429          * HDMI sink's ELD info cannot always be retrieved for now, e.g.
430          * in console or for audio devices. Assume the highest speakers
431          * configuration, to _not_ prohibit multi-channel audio playback.
432          */
433         if (!eld->spk_alloc)
434                 eld->spk_alloc = 0xffff;
435
436         /*
437          * expand ELD's speaker allocation mask
438          *
439          * ELD tells the speaker mask in a compact(paired) form,
440          * expand ELD's notions to match the ones used by Audio InfoFrame.
441          */
442         for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
443                 if (eld->spk_alloc & (1 << i))
444                         spk_mask |= eld_speaker_allocation_bits[i];
445         }
446
447         /* search for the first working match in the CA table */
448         for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
449                 if (channels == channel_allocations[i].channels &&
450                     (spk_mask & channel_allocations[i].spk_mask) ==
451                                 channel_allocations[i].spk_mask) {
452                         ai->CA = channel_allocations[i].ca_index;
453                         break;
454                 }
455         }
456
457         snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
458         snd_printdd(KERN_INFO
459                         "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
460                         ai->CA, channels, buf);
461
462         return ai->CA;
463 }
464
465 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
466                                         struct hdmi_audio_infoframe *ai)
467 {
468         if (!ai->CA)
469                 return;
470
471         /*
472          * TODO: adjust channel mapping if necessary
473          * ALSA sequence is front/surr/clfe/side?
474          */
475
476         snd_hda_sequence_write(codec, def_chan_map);
477         hdmi_debug_channel_mapping(codec);
478 }
479
480
481 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
482                                         struct snd_pcm_substream *substream)
483 {
484         struct hdmi_audio_infoframe ai = {
485                 .type           = 0x84,
486                 .ver            = 0x01,
487                 .len            = 0x0a,
488                 .CC02_CT47      = substream->runtime->channels - 1,
489         };
490
491         hdmi_setup_channel_allocation(codec, &ai);
492         hdmi_setup_channel_mapping(codec, &ai);
493
494         hdmi_fill_audio_infoframe(codec, &ai);
495         hdmi_start_infoframe_trans(codec);
496 }
497
498
499 /*
500  * Unsolicited events
501  */
502
503 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
504 {
505         int pind = !!(res & AC_UNSOL_RES_PD);
506         int eldv = !!(res & AC_UNSOL_RES_ELDV);
507
508         printk(KERN_INFO
509                 "HDMI hot plug event: Presence_Detect=%d ELD_Valid=%d\n",
510                 pind, eldv);
511
512         if (pind && eldv) {
513                 hdmi_parse_eld(codec);
514                 /* TODO: do real things about ELD */
515         }
516 }
517
518 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
519 {
520         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
521         int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
522         int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
523
524         printk(KERN_INFO
525                 "HDMI content protection event: SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
526                 subtag,
527                 cp_state,
528                 cp_ready);
529
530         /* TODO */
531         if (cp_state)
532                 ;
533         if (cp_ready)
534                 ;
535 }
536
537
538 static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
539 {
540         int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
541         int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
542
543         if (tag != INTEL_HDMI_EVENT_TAG) {
544                 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
545                 return;
546         }
547
548         if (subtag == 0)
549                 hdmi_intrinsic_event(codec, res);
550         else
551                 hdmi_non_intrinsic_event(codec, res);
552 }
553
554 /*
555  * Callbacks
556  */
557
558 static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream *hinfo,
559                                         struct hda_codec *codec,
560                                         struct snd_pcm_substream *substream)
561 {
562         struct intel_hdmi_spec *spec = codec->spec;
563
564         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
565 }
566
567 static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream *hinfo,
568                                          struct hda_codec *codec,
569                                          struct snd_pcm_substream *substream)
570 {
571         struct intel_hdmi_spec *spec = codec->spec;
572
573         hdmi_stop_infoframe_trans(codec);
574
575         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
576 }
577
578 static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
579                                            struct hda_codec *codec,
580                                            unsigned int stream_tag,
581                                            unsigned int format,
582                                            struct snd_pcm_substream *substream)
583 {
584         struct intel_hdmi_spec *spec = codec->spec;
585
586         snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
587                                              format, substream);
588
589         hdmi_set_channel_count(codec, substream->runtime->channels);
590
591         hdmi_setup_audio_infoframe(codec, substream);
592
593         return 0;
594 }
595
596 static struct hda_pcm_stream intel_hdmi_pcm_playback = {
597         .substreams = 1,
598         .channels_min = 2,
599         .channels_max = 8,
600         .nid = CVT_NID, /* NID to query formats and rates and setup streams */
601         .ops = {
602                 .open    = intel_hdmi_playback_pcm_open,
603                 .close   = intel_hdmi_playback_pcm_close,
604                 .prepare = intel_hdmi_playback_pcm_prepare
605         },
606 };
607
608 static int intel_hdmi_build_pcms(struct hda_codec *codec)
609 {
610         struct intel_hdmi_spec *spec = codec->spec;
611         struct hda_pcm *info = &spec->pcm_rec;
612
613         codec->num_pcms = 1;
614         codec->pcm_info = info;
615
616         info->name = "INTEL HDMI";
617         info->pcm_type = HDA_PCM_TYPE_HDMI;
618         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
619
620         return 0;
621 }
622
623 static int intel_hdmi_build_controls(struct hda_codec *codec)
624 {
625         struct intel_hdmi_spec *spec = codec->spec;
626         int err;
627
628         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
629         if (err < 0)
630                 return err;
631
632         return 0;
633 }
634
635 static int intel_hdmi_init(struct hda_codec *codec)
636 {
637         hdmi_enable_output(codec);
638
639         snd_hda_sequence_write(codec, unsolicited_response_verb);
640
641         return 0;
642 }
643
644 static void intel_hdmi_free(struct hda_codec *codec)
645 {
646         struct intel_hdmi_spec *spec = codec->spec;
647
648         snd_hda_eld_proc_free(codec, &spec->sink_eld);
649         kfree(spec);
650 }
651
652 static struct hda_codec_ops intel_hdmi_patch_ops = {
653         .init                   = intel_hdmi_init,
654         .free                   = intel_hdmi_free,
655         .build_pcms             = intel_hdmi_build_pcms,
656         .build_controls         = intel_hdmi_build_controls,
657         .unsol_event            = intel_hdmi_unsol_event,
658 };
659
660 static int patch_intel_hdmi(struct hda_codec *codec)
661 {
662         struct intel_hdmi_spec *spec;
663
664         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
665         if (spec == NULL)
666                 return -ENOMEM;
667
668         spec->multiout.num_dacs = 0;      /* no analog */
669         spec->multiout.max_channels = 8;
670         spec->multiout.dig_out_nid = CVT_NID;
671
672         codec->spec = spec;
673         codec->patch_ops = intel_hdmi_patch_ops;
674
675         snd_hda_eld_proc_new(codec, &spec->sink_eld);
676
677         init_channel_allocations();
678
679         return 0;
680 }
681
682 static struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
683         { .id = 0x808629fb, .name = "G45 DEVCL",  .patch = patch_intel_hdmi },
684         { .id = 0x80862801, .name = "G45 DEVBLC", .patch = patch_intel_hdmi },
685         { .id = 0x80862802, .name = "G45 DEVCTG", .patch = patch_intel_hdmi },
686         { .id = 0x80862803, .name = "G45 DEVELK", .patch = patch_intel_hdmi },
687         { .id = 0x80862804, .name = "G45 DEVIBX", .patch = patch_intel_hdmi },
688         { .id = 0x80860054, .name = "G45 DEVIBX", .patch = patch_intel_hdmi },
689         { .id = 0x10951392, .name = "SiI1392 HDMI",     .patch = patch_intel_hdmi },
690         {} /* terminator */
691 };
692
693 MODULE_ALIAS("snd-hda-codec-id:808629fb");
694 MODULE_ALIAS("snd-hda-codec-id:80862801");
695 MODULE_ALIAS("snd-hda-codec-id:80862802");
696 MODULE_ALIAS("snd-hda-codec-id:80862803");
697 MODULE_ALIAS("snd-hda-codec-id:80862804");
698 MODULE_ALIAS("snd-hda-codec-id:80860054");
699 MODULE_ALIAS("snd-hda-codec-id:10951392");
700
701 MODULE_LICENSE("GPL");
702 MODULE_DESCRIPTION("Intel HDMI HD-audio codec");
703
704 static struct hda_codec_preset_list intel_list = {
705         .preset = snd_hda_preset_intelhdmi,
706         .owner = THIS_MODULE,
707 };
708
709 static int __init patch_intelhdmi_init(void)
710 {
711         return snd_hda_add_codec_preset(&intel_list);
712 }
713
714 static void __exit patch_intelhdmi_exit(void)
715 {
716         snd_hda_delete_codec_preset(&intel_list);
717 }
718
719 module_init(patch_intelhdmi_init)
720 module_exit(patch_intelhdmi_exit)