blob: 3990182777ee7a223a5f45c7c6b33fd8bba7f4d5 [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
Wu Fengguang54a25f82009-10-30 11:44:26 +010036/*
37 * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
38 * could support two independent pipes, each of them can be connected to one or
39 * more ports (DVI, HDMI or DisplayPort).
40 *
41 * The HDA correspondence of pipes/ports are converter/pin nodes.
42 */
43#define INTEL_HDMI_CVTS 2
44#define INTEL_HDMI_PINS 3
Wu, Fengguang91504872008-11-05 11:16:56 +080045
Wu Fengguang54a25f82009-10-30 11:44:26 +010046static char *intel_hdmi_pcm_names[INTEL_HDMI_CVTS] = {
47 "INTEL HDMI 0",
48 "INTEL HDMI 1",
49};
Wu, Fengguang91504872008-11-05 11:16:56 +080050
Wu, Fengguang91504872008-11-05 11:16:56 +080051struct intel_hdmi_spec {
Wu Fengguang54a25f82009-10-30 11:44:26 +010052 int num_cvts;
53 int num_pins;
54 hda_nid_t cvt[INTEL_HDMI_CVTS+1]; /* audio sources */
55 hda_nid_t pin[INTEL_HDMI_PINS+1]; /* audio sinks */
56
57 /*
58 * source connection for each pin
59 */
60 hda_nid_t pin_cvt[INTEL_HDMI_PINS+1];
61
62 /*
63 * HDMI sink attached to each pin
64 */
Wu Fengguang54a25f82009-10-30 11:44:26 +010065 struct hdmi_eld sink_eld[INTEL_HDMI_PINS];
66
67 /*
68 * export one pcm per pipe
69 */
70 struct hda_pcm pcm_rec[INTEL_HDMI_CVTS];
Wu, Fengguang91504872008-11-05 11:16:56 +080071};
72
Wu, Fengguang91504872008-11-05 11:16:56 +080073struct hdmi_audio_infoframe {
74 u8 type; /* 0x84 */
75 u8 ver; /* 0x01 */
76 u8 len; /* 0x0a */
77
78 u8 checksum; /* PB0 */
79 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
80 u8 SS01_SF24;
81 u8 CXT04;
82 u8 CA;
83 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang4e19c582008-11-19 15:13:59 +080084 u8 reserved[5]; /* PB6 - PB10 */
Wu, Fengguang91504872008-11-05 11:16:56 +080085};
86
87/*
Wu Fengguang698544d2008-11-19 08:56:17 +080088 * CEA speaker placement:
89 *
90 * FLH FCH FRH
91 * FLW FL FLC FC FRC FR FRW
92 *
93 * LFE
94 * TC
95 *
96 * RL RLC RC RRC RR
97 *
98 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
99 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
100 */
101enum cea_speaker_placement {
102 FL = (1 << 0), /* Front Left */
103 FC = (1 << 1), /* Front Center */
104 FR = (1 << 2), /* Front Right */
105 FLC = (1 << 3), /* Front Left Center */
106 FRC = (1 << 4), /* Front Right Center */
107 RL = (1 << 5), /* Rear Left */
108 RC = (1 << 6), /* Rear Center */
109 RR = (1 << 7), /* Rear Right */
110 RLC = (1 << 8), /* Rear Left Center */
111 RRC = (1 << 9), /* Rear Right Center */
112 LFE = (1 << 10), /* Low Frequency Effect */
113 FLW = (1 << 11), /* Front Left Wide */
114 FRW = (1 << 12), /* Front Right Wide */
115 FLH = (1 << 13), /* Front Left High */
116 FCH = (1 << 14), /* Front Center High */
117 FRH = (1 << 15), /* Front Right High */
118 TC = (1 << 16), /* Top Center */
119};
120
121/*
122 * ELD SA bits in the CEA Speaker Allocation data block
123 */
124static int eld_speaker_allocation_bits[] = {
125 [0] = FL | FR,
126 [1] = LFE,
127 [2] = FC,
128 [3] = RL | RR,
129 [4] = RC,
130 [5] = FLC | FRC,
131 [6] = RLC | RRC,
132 /* the following are not defined in ELD yet */
133 [7] = FLW | FRW,
134 [8] = FLH | FRH,
135 [9] = TC,
136 [10] = FCH,
137};
138
139struct cea_channel_speaker_allocation {
140 int ca_index;
141 int speakers[8];
142
143 /* derived values, just for convenience */
144 int channels;
145 int spk_mask;
146};
147
148/*
Wu Fengguangb14224b2009-12-11 12:28:36 +0800149 * ALSA sequence is:
150 *
151 * surround40 surround41 surround50 surround51 surround71
152 * ch0 front left = = = =
153 * ch1 front right = = = =
154 * ch2 rear left = = = =
155 * ch3 rear right = = = =
156 * ch4 LFE center center center
157 * ch5 LFE LFE
158 * ch6 side left
159 * ch7 side right
160 *
161 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
162 */
163static int hdmi_channel_mapping[0x32][8] = {
164 /* stereo */
165 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
166 /* 2.1 */
167 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
168 /* Dolby Surround */
169 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
170 /* surround40 */
171 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
172 /* 4ch */
173 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
174 /* surround41 */
175 [0x09] = { 0x00, 0x11, 0x24, 0x34, 0x43, 0xf2, 0xf6, 0xf7 },
176 /* surround50 */
177 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
178 /* surround51 */
179 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
180 /* 7.1 */
181 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
182};
183
184/*
Wu Fengguang698544d2008-11-19 08:56:17 +0800185 * This is an ordered list!
186 *
187 * The preceding ones have better chances to be selected by
188 * hdmi_setup_channel_allocation().
189 */
190static struct cea_channel_speaker_allocation channel_allocations[] = {
Wu Fengguangb14224b2009-12-11 12:28:36 +0800191/* channel: 7 6 5 4 3 2 1 0 */
Wu Fengguang698544d2008-11-19 08:56:17 +0800192{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
193 /* 2.1 */
194{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
Wu Fengguang4e19c582008-11-19 15:13:59 +0800195 /* Dolby Surround */
Wu Fengguang698544d2008-11-19 08:56:17 +0800196{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
Wu Fengguangb14224b2009-12-11 12:28:36 +0800197 /* surround40 */
198{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
199 /* surround41 */
200{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
201 /* surround50 */
202{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
203 /* surround51 */
204{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
205 /* 6.1 */
206{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
207 /* surround71 */
208{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
209
Wu Fengguang698544d2008-11-19 08:56:17 +0800210{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
211{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
212{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
213{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
214{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
Wu Fengguang698544d2008-11-19 08:56:17 +0800215{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
216{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
217{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
Wu Fengguang698544d2008-11-19 08:56:17 +0800218{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
219{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
220{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
Wu Fengguang698544d2008-11-19 08:56:17 +0800221{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
222{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
223{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
224{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
225{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
226{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
227{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
228{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
229{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
230{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
231{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
232{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
233{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
234{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
235{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
236{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
237{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
238{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
239{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
240{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
241{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
242{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
243{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
244{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
245{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
246{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
247{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
248{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
249{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
250{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
251};
252
Wu Fengguangf4243672009-10-30 11:45:35 +0100253/*
254 * HDA/HDMI auto parsing
255 */
256
Wu Fengguang54a25f82009-10-30 11:44:26 +0100257static int hda_node_index(hda_nid_t *nids, hda_nid_t nid)
258{
259 int i;
260
261 for (i = 0; nids[i]; i++)
262 if (nids[i] == nid)
263 return i;
264
265 snd_printk(KERN_WARNING "HDMI: nid %d not registered\n", nid);
266 return -EINVAL;
267}
268
Wu Fengguangf4243672009-10-30 11:45:35 +0100269static int intel_hdmi_read_pin_conn(struct hda_codec *codec, hda_nid_t pin_nid)
270{
271 struct intel_hdmi_spec *spec = codec->spec;
272 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
273 int conn_len, curr;
274 int index;
275
276 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
277 snd_printk(KERN_WARNING
278 "HDMI: pin %d wcaps %#x "
279 "does not support connection list\n",
280 pin_nid, get_wcaps(codec, pin_nid));
281 return -EINVAL;
282 }
283
284 conn_len = snd_hda_get_connections(codec, pin_nid, conn_list,
285 HDA_MAX_CONNECTIONS);
286 if (conn_len > 1)
287 curr = snd_hda_codec_read(codec, pin_nid, 0,
288 AC_VERB_GET_CONNECT_SEL, 0);
289 else
290 curr = 0;
291
292 index = hda_node_index(spec->pin, pin_nid);
293 if (index < 0)
294 return -EINVAL;
295
296 spec->pin_cvt[index] = conn_list[curr];
297
298 return 0;
299}
300
Wu Fengguang3f54aa52009-11-18 12:38:03 +0800301static void hdmi_get_show_eld(struct hda_codec *codec, hda_nid_t pin_nid,
302 struct hdmi_eld *eld)
303{
304 if (!snd_hdmi_get_eld(eld, codec, pin_nid))
305 snd_hdmi_show_eld(eld);
306}
307
308static void hdmi_present_sense(struct hda_codec *codec, hda_nid_t pin_nid,
309 struct hdmi_eld *eld)
310{
311 int present = snd_hda_pin_sense(codec, pin_nid);
312
313 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
314 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
315
316 if (present & AC_PINSENSE_ELDV)
317 hdmi_get_show_eld(codec, pin_nid, eld);
318}
319
Wu Fengguangf4243672009-10-30 11:45:35 +0100320static int intel_hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
321{
322 struct intel_hdmi_spec *spec = codec->spec;
323
324 if (spec->num_pins >= INTEL_HDMI_PINS) {
325 snd_printk(KERN_WARNING
326 "HDMI: no space for pin %d \n", pin_nid);
327 return -EINVAL;
328 }
329
Wu Fengguang3f54aa52009-11-18 12:38:03 +0800330 hdmi_present_sense(codec, pin_nid, &spec->sink_eld[spec->num_pins]);
331
Wu Fengguangf4243672009-10-30 11:45:35 +0100332 spec->pin[spec->num_pins] = pin_nid;
333 spec->num_pins++;
334
335 /*
336 * It is assumed that converter nodes come first in the node list and
337 * hence have been registered and usable now.
338 */
339 return intel_hdmi_read_pin_conn(codec, pin_nid);
340}
341
342static int intel_hdmi_add_cvt(struct hda_codec *codec, hda_nid_t nid)
343{
344 struct intel_hdmi_spec *spec = codec->spec;
345
346 if (spec->num_cvts >= INTEL_HDMI_CVTS) {
347 snd_printk(KERN_WARNING
348 "HDMI: no space for converter %d \n", nid);
349 return -EINVAL;
350 }
351
352 spec->cvt[spec->num_cvts] = nid;
353 spec->num_cvts++;
354
355 return 0;
356}
357
358static int intel_hdmi_parse_codec(struct hda_codec *codec)
359{
360 hda_nid_t nid;
361 int i, nodes;
362
363 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
364 if (!nid || nodes < 0) {
365 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
366 return -EINVAL;
367 }
368
369 for (i = 0; i < nodes; i++, nid++) {
370 unsigned int caps;
371 unsigned int type;
372
373 caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
374 type = get_wcaps_type(caps);
375
376 if (!(caps & AC_WCAP_DIGITAL))
377 continue;
378
379 switch (type) {
380 case AC_WID_AUD_OUT:
381 if (intel_hdmi_add_cvt(codec, nid) < 0)
382 return -EINVAL;
383 break;
384 case AC_WID_PIN:
385 caps = snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
Wu Fengguang728765b2009-12-11 12:28:34 +0800386 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
Wu Fengguangf4243672009-10-30 11:45:35 +0100387 continue;
388 if (intel_hdmi_add_pin(codec, nid) < 0)
389 return -EINVAL;
390 break;
391 }
392 }
393
394 return 0;
395}
396
Wu Fengguang698544d2008-11-19 08:56:17 +0800397/*
Wu, Fengguang91504872008-11-05 11:16:56 +0800398 * HDMI routines
399 */
400
Takashi Iwaibeb0b9cf2008-11-05 07:58:25 +0100401#ifdef BE_PARANOID
Wu Fengguang6797cf22009-10-30 11:40:40 +0100402static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
Wu, Fengguang91504872008-11-05 11:16:56 +0800403 int *packet_index, int *byte_index)
404{
405 int val;
406
Wu Fengguang6797cf22009-10-30 11:40:40 +0100407 val = snd_hda_codec_read(codec, pin_nid, 0,
408 AC_VERB_GET_HDMI_DIP_INDEX, 0);
Wu, Fengguang91504872008-11-05 11:16:56 +0800409
410 *packet_index = val >> 5;
411 *byte_index = val & 0x1f;
412}
Takashi Iwaibeb0b9cf2008-11-05 07:58:25 +0100413#endif
Wu, Fengguang91504872008-11-05 11:16:56 +0800414
Wu Fengguang6797cf22009-10-30 11:40:40 +0100415static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
Wu, Fengguang91504872008-11-05 11:16:56 +0800416 int packet_index, int byte_index)
417{
418 int val;
419
420 val = (packet_index << 5) | (byte_index & 0x1f);
421
Wu Fengguang6797cf22009-10-30 11:40:40 +0100422 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
Wu, Fengguang91504872008-11-05 11:16:56 +0800423}
424
Wu Fengguang6797cf22009-10-30 11:40:40 +0100425static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
Wu, Fengguang91504872008-11-05 11:16:56 +0800426 unsigned char val)
427{
Wu Fengguang6797cf22009-10-30 11:40:40 +0100428 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
Wu, Fengguang91504872008-11-05 11:16:56 +0800429}
430
Wu Fengguang6797cf22009-10-30 11:40:40 +0100431static void hdmi_enable_output(struct hda_codec *codec, hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800432{
Wu Fengguang796359d2008-11-17 16:57:33 +0800433 /* Unmute */
Wu Fengguang559059b2009-08-02 16:48:55 +0800434 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
435 snd_hda_codec_write(codec, pin_nid, 0,
Wu Fengguang796359d2008-11-17 16:57:33 +0800436 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
437 /* Enable pin out */
Wu Fengguang559059b2009-08-02 16:48:55 +0800438 snd_hda_codec_write(codec, pin_nid, 0,
439 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu, Fengguang91504872008-11-05 11:16:56 +0800440}
441
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800442/*
443 * Enable Audio InfoFrame Transmission
444 */
Wu Fengguang6797cf22009-10-30 11:40:40 +0100445static void hdmi_start_infoframe_trans(struct hda_codec *codec,
446 hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800447{
Wu Fengguang559059b2009-08-02 16:48:55 +0800448 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
449 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800450 AC_DIPXMIT_BEST);
451}
Wu, Fengguang91504872008-11-05 11:16:56 +0800452
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800453/*
454 * Disable Audio InfoFrame Transmission
455 */
Wu Fengguang6797cf22009-10-30 11:40:40 +0100456static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
457 hda_nid_t pin_nid)
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800458{
Wu Fengguang559059b2009-08-02 16:48:55 +0800459 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
460 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800461 AC_DIPXMIT_DISABLE);
Wu, Fengguang91504872008-11-05 11:16:56 +0800462}
463
Wu Fengguang6797cf22009-10-30 11:40:40 +0100464static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800465{
Wu Fengguang6797cf22009-10-30 11:40:40 +0100466 return 1 + snd_hda_codec_read(codec, nid, 0,
Wu, Fengguang91504872008-11-05 11:16:56 +0800467 AC_VERB_GET_CVT_CHAN_COUNT, 0);
468}
469
Wu Fengguang6797cf22009-10-30 11:40:40 +0100470static void hdmi_set_channel_count(struct hda_codec *codec,
471 hda_nid_t nid, int chs)
Wu, Fengguang91504872008-11-05 11:16:56 +0800472{
Wu Fengguang6797cf22009-10-30 11:40:40 +0100473 if (chs != hdmi_get_channel_count(codec, nid))
Wu Fengguang81bf31e2009-11-18 12:38:07 +0800474 snd_hda_codec_write(codec, nid, 0,
475 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
Wu, Fengguang91504872008-11-05 11:16:56 +0800476}
477
Wu Fengguang1ffc69a2009-12-11 12:28:35 +0800478static void hdmi_debug_channel_mapping(struct hda_codec *codec,
479 hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800480{
481#ifdef CONFIG_SND_DEBUG_VERBOSE
482 int i;
483 int slot;
484
485 for (i = 0; i < 8; i++) {
Wu Fengguang1ffc69a2009-12-11 12:28:35 +0800486 slot = snd_hda_codec_read(codec, pin_nid, 0,
Wu, Fengguang91504872008-11-05 11:16:56 +0800487 AC_VERB_GET_HDMI_CHAN_SLOT, i);
Wu Fengguang03284c82008-11-22 09:40:53 +0800488 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
Wu Fengguang1e7c10f2009-11-18 12:38:00 +0800489 slot >> 4, slot & 0xf);
Wu, Fengguang91504872008-11-05 11:16:56 +0800490 }
491#endif
492}
493
Wu, Fengguang91504872008-11-05 11:16:56 +0800494
495/*
Wu Fengguang4e19c582008-11-19 15:13:59 +0800496 * Audio InfoFrame routines
Wu, Fengguang91504872008-11-05 11:16:56 +0800497 */
498
Wu Fengguang6797cf22009-10-30 11:40:40 +0100499static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800500{
501#ifdef CONFIG_SND_DEBUG_VERBOSE
502 int i;
503 int size;
504
Wu Fengguang559059b2009-08-02 16:48:55 +0800505 size = snd_hdmi_get_eld_size(codec, pin_nid);
Wu Fengguang03284c82008-11-22 09:40:53 +0800506 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
Wu, Fengguang91504872008-11-05 11:16:56 +0800507
508 for (i = 0; i < 8; i++) {
Wu Fengguang559059b2009-08-02 16:48:55 +0800509 size = snd_hda_codec_read(codec, pin_nid, 0,
Wu, Fengguang91504872008-11-05 11:16:56 +0800510 AC_VERB_GET_HDMI_DIP_SIZE, i);
Wu Fengguang03284c82008-11-22 09:40:53 +0800511 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu, Fengguang91504872008-11-05 11:16:56 +0800512 }
513#endif
514}
515
Wu Fengguang6797cf22009-10-30 11:40:40 +0100516static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800517{
518#ifdef BE_PARANOID
519 int i, j;
520 int size;
521 int pi, bi;
522 for (i = 0; i < 8; i++) {
Wu Fengguang559059b2009-08-02 16:48:55 +0800523 size = snd_hda_codec_read(codec, pin_nid, 0,
Wu, Fengguang91504872008-11-05 11:16:56 +0800524 AC_VERB_GET_HDMI_DIP_SIZE, i);
525 if (size == 0)
526 continue;
527
Wu Fengguang559059b2009-08-02 16:48:55 +0800528 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
Wu, Fengguang91504872008-11-05 11:16:56 +0800529 for (j = 1; j < 1000; j++) {
Wu Fengguang559059b2009-08-02 16:48:55 +0800530 hdmi_write_dip_byte(codec, pin_nid, 0x0);
531 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
Wu, Fengguang91504872008-11-05 11:16:56 +0800532 if (pi != i)
533 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
534 bi, pi, i);
535 if (bi == 0) /* byte index wrapped around */
536 break;
537 }
538 snd_printd(KERN_INFO
Wu Fengguang03284c82008-11-22 09:40:53 +0800539 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
540 i, size, j);
Wu, Fengguang91504872008-11-05 11:16:56 +0800541 }
542#endif
543}
544
Wu Fengguang978be6d2009-11-18 12:38:04 +0800545static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *ai)
546{
547 u8 *bytes = (u8 *)ai;
548 u8 sum = 0;
549 int i;
550
551 ai->checksum = 0;
552
553 for (i = 0; i < sizeof(*ai); i++)
554 sum += bytes[i];
555
556 ai->checksum = - sum;
557}
558
Wu Fengguang5457a982008-11-19 08:56:15 +0800559static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
Wu Fengguang6797cf22009-10-30 11:40:40 +0100560 hda_nid_t pin_nid,
561 struct hdmi_audio_infoframe *ai)
Wu, Fengguang91504872008-11-05 11:16:56 +0800562{
Wu Fengguang978be6d2009-11-18 12:38:04 +0800563 u8 *bytes = (u8 *)ai;
Wu, Fengguang91504872008-11-05 11:16:56 +0800564 int i;
565
Wu Fengguang6797cf22009-10-30 11:40:40 +0100566 hdmi_debug_dip_size(codec, pin_nid);
567 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
Wu, Fengguang91504872008-11-05 11:16:56 +0800568
Wu Fengguang978be6d2009-11-18 12:38:04 +0800569 hdmi_checksum_audio_infoframe(ai);
Wu Fengguang9a957a22009-02-11 15:22:30 +0800570
Wu Fengguang559059b2009-08-02 16:48:55 +0800571 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang6f539a92009-11-18 12:37:59 +0800572 for (i = 0; i < sizeof(*ai); i++)
Wu Fengguang978be6d2009-11-18 12:38:04 +0800573 hdmi_write_dip_byte(codec, pin_nid, bytes[i]);
Wu, Fengguang91504872008-11-05 11:16:56 +0800574}
575
Wu Fengguang698544d2008-11-19 08:56:17 +0800576/*
577 * Compute derived values in channel_allocations[].
578 */
579static void init_channel_allocations(void)
580{
581 int i, j;
582 struct cea_channel_speaker_allocation *p;
583
584 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
585 p = channel_allocations + i;
586 p->channels = 0;
587 p->spk_mask = 0;
588 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
589 if (p->speakers[j]) {
590 p->channels++;
591 p->spk_mask |= p->speakers[j];
592 }
593 }
594}
595
596/*
597 * The transformation takes two steps:
598 *
599 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
600 * spk_mask => (channel_allocations[]) => ai->CA
601 *
602 * TODO: it could select the wrong CA from multiple candidates.
603*/
Wu Fengguang6797cf22009-10-30 11:40:40 +0100604static int hdmi_setup_channel_allocation(struct hda_codec *codec, hda_nid_t nid,
Wu Fengguang698544d2008-11-19 08:56:17 +0800605 struct hdmi_audio_infoframe *ai)
606{
607 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang54a25f82009-10-30 11:44:26 +0100608 struct hdmi_eld *eld;
Wu Fengguang698544d2008-11-19 08:56:17 +0800609 int i;
610 int spk_mask = 0;
611 int channels = 1 + (ai->CC02_CT47 & 0x7);
612 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
613
614 /*
615 * CA defaults to 0 for basic stereo audio
616 */
Wu Fengguang698544d2008-11-19 08:56:17 +0800617 if (channels <= 2)
618 return 0;
619
Wu Fengguang54a25f82009-10-30 11:44:26 +0100620 i = hda_node_index(spec->pin_cvt, nid);
621 if (i < 0)
622 return 0;
623 eld = &spec->sink_eld[i];
624
Wu Fengguang698544d2008-11-19 08:56:17 +0800625 /*
Wu Fengguanga1667e42009-02-11 15:22:28 +0800626 * HDMI sink's ELD info cannot always be retrieved for now, e.g.
627 * in console or for audio devices. Assume the highest speakers
628 * configuration, to _not_ prohibit multi-channel audio playback.
629 */
630 if (!eld->spk_alloc)
631 eld->spk_alloc = 0xffff;
632
633 /*
Wu Fengguang698544d2008-11-19 08:56:17 +0800634 * expand ELD's speaker allocation mask
635 *
636 * ELD tells the speaker mask in a compact(paired) form,
Wu Fengguangb83923a2008-11-22 09:40:51 +0800637 * expand ELD's notions to match the ones used by Audio InfoFrame.
Wu Fengguang698544d2008-11-19 08:56:17 +0800638 */
639 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
640 if (eld->spk_alloc & (1 << i))
641 spk_mask |= eld_speaker_allocation_bits[i];
642 }
643
644 /* search for the first working match in the CA table */
645 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
646 if (channels == channel_allocations[i].channels &&
647 (spk_mask & channel_allocations[i].spk_mask) ==
648 channel_allocations[i].spk_mask) {
649 ai->CA = channel_allocations[i].ca_index;
Wu Fengguangcc02b832008-11-22 09:40:52 +0800650 break;
Wu Fengguang698544d2008-11-19 08:56:17 +0800651 }
652 }
653
654 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
Wu Fengguangcc02b832008-11-22 09:40:52 +0800655 snd_printdd(KERN_INFO
656 "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
657 ai->CA, channels, buf);
658
659 return ai->CA;
Wu Fengguang698544d2008-11-19 08:56:17 +0800660}
661
Wu Fengguang1ffc69a2009-12-11 12:28:35 +0800662static void hdmi_setup_channel_mapping(struct hda_codec *codec,
663 hda_nid_t pin_nid,
Wu Fengguang6797cf22009-10-30 11:40:40 +0100664 struct hdmi_audio_infoframe *ai)
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800665{
Wu Fengguang559059b2009-08-02 16:48:55 +0800666 int i;
Wu Fengguangb14224b2009-12-11 12:28:36 +0800667 int ca = ai->CA;
668 int err;
Wu Fengguang559059b2009-08-02 16:48:55 +0800669
Wu Fengguangb14224b2009-12-11 12:28:36 +0800670 if (hdmi_channel_mapping[ca][1] == 0) {
671 for (i = 0; i < channel_allocations[ca].channels; i++)
672 hdmi_channel_mapping[ca][i] = i | (i << 4);
673 for (; i < 8; i++)
674 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
675 }
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800676
Wu Fengguangb14224b2009-12-11 12:28:36 +0800677 for (i = 0; i < 8; i++) {
678 err = snd_hda_codec_write(codec, pin_nid, 0,
679 AC_VERB_SET_HDMI_CHAN_SLOT,
680 hdmi_channel_mapping[ca][i]);
681 if (err) {
682 snd_printdd(KERN_INFO "HDMI: channel mapping failed\n");
683 break;
684 }
685 }
Wu Fengguang559059b2009-08-02 16:48:55 +0800686
Wu Fengguang1ffc69a2009-12-11 12:28:35 +0800687 hdmi_debug_channel_mapping(codec, pin_nid);
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800688}
689
Wu Fengguang848de592009-11-18 12:38:05 +0800690static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
691 struct hdmi_audio_infoframe *ai)
692{
693 u8 *bytes = (u8 *)ai;
694 u8 val;
695 int i;
696
697 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
698 != AC_DIPXMIT_BEST)
699 return false;
700
701 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
702 for (i = 0; i < sizeof(*ai); i++) {
703 val = snd_hda_codec_read(codec, pin_nid, 0,
704 AC_VERB_GET_HDMI_DIP_DATA, 0);
705 if (val != bytes[i])
706 return false;
707 }
708
709 return true;
710}
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800711
Wu Fengguang6797cf22009-10-30 11:40:40 +0100712static void hdmi_setup_audio_infoframe(struct hda_codec *codec, hda_nid_t nid,
Wu Fengguang5457a982008-11-19 08:56:15 +0800713 struct snd_pcm_substream *substream)
714{
Wu Fengguang54a25f82009-10-30 11:44:26 +0100715 struct intel_hdmi_spec *spec = codec->spec;
716 hda_nid_t pin_nid;
717 int i;
Wu Fengguang5457a982008-11-19 08:56:15 +0800718 struct hdmi_audio_infoframe ai = {
719 .type = 0x84,
720 .ver = 0x01,
721 .len = 0x0a,
722 .CC02_CT47 = substream->runtime->channels - 1,
723 };
724
Wu Fengguang6797cf22009-10-30 11:40:40 +0100725 hdmi_setup_channel_allocation(codec, nid, &ai);
Wu Fengguang698544d2008-11-19 08:56:17 +0800726
Wu Fengguang54a25f82009-10-30 11:44:26 +0100727 for (i = 0; i < spec->num_pins; i++) {
728 if (spec->pin_cvt[i] != nid)
729 continue;
Wu Fengguang23ccc2b2009-11-18 12:38:01 +0800730 if (!spec->sink_eld[i].monitor_present)
Wu Fengguang54a25f82009-10-30 11:44:26 +0100731 continue;
732
733 pin_nid = spec->pin[i];
Wu Fengguang848de592009-11-18 12:38:05 +0800734 if (!hdmi_infoframe_uptodate(codec, pin_nid, &ai)) {
Wu Fengguang1ffc69a2009-12-11 12:28:35 +0800735 hdmi_setup_channel_mapping(codec, pin_nid, &ai);
Wu Fengguang848de592009-11-18 12:38:05 +0800736 hdmi_stop_infoframe_trans(codec, pin_nid);
737 hdmi_fill_audio_infoframe(codec, pin_nid, &ai);
738 hdmi_start_infoframe_trans(codec, pin_nid);
739 }
Wu Fengguang54a25f82009-10-30 11:44:26 +0100740 }
Wu Fengguang5457a982008-11-19 08:56:15 +0800741}
742
Wu, Fengguang91504872008-11-05 11:16:56 +0800743
744/*
745 * Unsolicited events
746 */
747
748static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
749{
Wu Fengguang54a25f82009-10-30 11:44:26 +0100750 struct intel_hdmi_spec *spec = codec->spec;
751 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Wu, Fengguang91504872008-11-05 11:16:56 +0800752 int pind = !!(res & AC_UNSOL_RES_PD);
753 int eldv = !!(res & AC_UNSOL_RES_ELDV);
Wu Fengguang54a25f82009-10-30 11:44:26 +0100754 int index;
Wu, Fengguang91504872008-11-05 11:16:56 +0800755
Wu Fengguang03284c82008-11-22 09:40:53 +0800756 printk(KERN_INFO
Wu Fengguang54a25f82009-10-30 11:44:26 +0100757 "HDMI hot plug event: Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
758 tag, pind, eldv);
759
760 index = hda_node_index(spec->pin, tag);
761 if (index < 0)
762 return;
763
Wu Fengguang23ccc2b2009-11-18 12:38:01 +0800764 spec->sink_eld[index].monitor_present = pind;
765 spec->sink_eld[index].eld_valid = eldv;
Wu, Fengguang91504872008-11-05 11:16:56 +0800766
767 if (pind && eldv) {
Wu Fengguang3f54aa52009-11-18 12:38:03 +0800768 hdmi_get_show_eld(codec, spec->pin[index], &spec->sink_eld[index]);
Wu, Fengguang91504872008-11-05 11:16:56 +0800769 /* TODO: do real things about ELD */
770 }
771}
772
773static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
774{
Wu Fengguang54a25f82009-10-30 11:44:26 +0100775 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Wu, Fengguang91504872008-11-05 11:16:56 +0800776 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
777 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
778 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
779
Wu Fengguang03284c82008-11-22 09:40:53 +0800780 printk(KERN_INFO
Wu Fengguang54a25f82009-10-30 11:44:26 +0100781 "HDMI CP event: PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
782 tag,
Wu Fengguang03284c82008-11-22 09:40:53 +0800783 subtag,
784 cp_state,
785 cp_ready);
Wu, Fengguang91504872008-11-05 11:16:56 +0800786
Wu Fengguang03284c82008-11-22 09:40:53 +0800787 /* TODO */
Wu, Fengguang91504872008-11-05 11:16:56 +0800788 if (cp_state)
789 ;
790 if (cp_ready)
791 ;
792}
793
794
795static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
796{
Wu Fengguang54a25f82009-10-30 11:44:26 +0100797 struct intel_hdmi_spec *spec = codec->spec;
Wu, Fengguang91504872008-11-05 11:16:56 +0800798 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
799 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
800
Wu Fengguang54a25f82009-10-30 11:44:26 +0100801 if (hda_node_index(spec->pin, tag) < 0) {
Wu Fengguang03284c82008-11-22 09:40:53 +0800802 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
Wu, Fengguang91504872008-11-05 11:16:56 +0800803 return;
804 }
805
806 if (subtag == 0)
807 hdmi_intrinsic_event(codec, res);
808 else
809 hdmi_non_intrinsic_event(codec, res);
810}
811
812/*
813 * Callbacks
814 */
815
Wu Fengguang57791912009-11-18 12:38:06 +0800816static void hdmi_setup_stream(struct hda_codec *codec, hda_nid_t nid,
817 u32 stream_tag, int format)
818{
819 int tag;
820 int fmt;
821
822 tag = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0) >> 4;
823 fmt = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_STREAM_FORMAT, 0);
824
825 snd_printdd("hdmi_setup_stream: "
826 "NID=0x%x, %sstream=0x%x, %sformat=0x%x\n",
827 nid,
828 tag == stream_tag ? "" : "new-",
829 stream_tag,
830 fmt == format ? "" : "new-",
831 format);
832
833 if (tag != stream_tag)
834 snd_hda_codec_write(codec, nid, 0,
835 AC_VERB_SET_CHANNEL_STREAMID, stream_tag << 4);
836 if (fmt != format)
837 snd_hda_codec_write(codec, nid, 0,
838 AC_VERB_SET_STREAM_FORMAT, format);
839}
840
Wu, Fengguang91504872008-11-05 11:16:56 +0800841static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
842 struct hda_codec *codec,
843 unsigned int stream_tag,
844 unsigned int format,
845 struct snd_pcm_substream *substream)
846{
Wu Fengguang54a25f82009-10-30 11:44:26 +0100847 hdmi_set_channel_count(codec, hinfo->nid,
Wu Fengguang7bedb012009-10-30 11:41:44 +0100848 substream->runtime->channels);
Wu, Fengguang91504872008-11-05 11:16:56 +0800849
Wu Fengguang54a25f82009-10-30 11:44:26 +0100850 hdmi_setup_audio_infoframe(codec, hinfo->nid, substream);
Wu, Fengguang91504872008-11-05 11:16:56 +0800851
Wu Fengguang57791912009-11-18 12:38:06 +0800852 hdmi_setup_stream(codec, hinfo->nid, stream_tag, format);
Wu, Fengguang91504872008-11-05 11:16:56 +0800853 return 0;
854}
855
Wu Fengguangddb81522009-10-30 11:43:03 +0100856static int intel_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
857 struct hda_codec *codec,
858 struct snd_pcm_substream *substream)
859{
Wu Fengguangddb81522009-10-30 11:43:03 +0100860 return 0;
861}
862
Wu, Fengguang91504872008-11-05 11:16:56 +0800863static struct hda_pcm_stream intel_hdmi_pcm_playback = {
864 .substreams = 1,
865 .channels_min = 2,
Wu, Fengguang91504872008-11-05 11:16:56 +0800866 .ops = {
Wu Fengguang70ca35f2009-10-30 11:42:18 +0100867 .prepare = intel_hdmi_playback_pcm_prepare,
868 .cleanup = intel_hdmi_playback_pcm_cleanup,
Wu, Fengguang91504872008-11-05 11:16:56 +0800869 },
870};
871
872static int intel_hdmi_build_pcms(struct hda_codec *codec)
873{
874 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang54a25f82009-10-30 11:44:26 +0100875 struct hda_pcm *info = spec->pcm_rec;
876 int i;
Wu, Fengguang91504872008-11-05 11:16:56 +0800877
Wu Fengguang54a25f82009-10-30 11:44:26 +0100878 codec->num_pcms = spec->num_cvts;
Wu, Fengguang91504872008-11-05 11:16:56 +0800879 codec->pcm_info = info;
880
Wu Fengguang54a25f82009-10-30 11:44:26 +0100881 for (i = 0; i < codec->num_pcms; i++, info++) {
Wu Fengguang69fb3462009-10-30 11:45:04 +0100882 unsigned int chans;
883
884 chans = get_wcaps(codec, spec->cvt[i]);
885 chans = get_wcaps_channels(chans);
886
Wu Fengguang54a25f82009-10-30 11:44:26 +0100887 info->name = intel_hdmi_pcm_names[i];
888 info->pcm_type = HDA_PCM_TYPE_HDMI;
889 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
890 intel_hdmi_pcm_playback;
891 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->cvt[i];
Wu Fengguang69fb3462009-10-30 11:45:04 +0100892 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = chans;
Wu Fengguang54a25f82009-10-30 11:44:26 +0100893 }
Wu, Fengguang91504872008-11-05 11:16:56 +0800894
895 return 0;
896}
897
898static int intel_hdmi_build_controls(struct hda_codec *codec)
899{
900 struct intel_hdmi_spec *spec = codec->spec;
901 int err;
Wu Fengguang54a25f82009-10-30 11:44:26 +0100902 int i;
Wu, Fengguang91504872008-11-05 11:16:56 +0800903
Wu Fengguang54a25f82009-10-30 11:44:26 +0100904 for (i = 0; i < codec->num_pcms; i++) {
905 err = snd_hda_create_spdif_out_ctls(codec, spec->cvt[i]);
906 if (err < 0)
907 return err;
908 }
Wu, Fengguang91504872008-11-05 11:16:56 +0800909
910 return 0;
911}
912
913static int intel_hdmi_init(struct hda_codec *codec)
914{
Wu Fengguang54a25f82009-10-30 11:44:26 +0100915 struct intel_hdmi_spec *spec = codec->spec;
916 int i;
Wu, Fengguang91504872008-11-05 11:16:56 +0800917
Wu Fengguang54a25f82009-10-30 11:44:26 +0100918 for (i = 0; spec->pin[i]; i++) {
919 hdmi_enable_output(codec, spec->pin[i]);
920 snd_hda_codec_write(codec, spec->pin[i], 0,
921 AC_VERB_SET_UNSOLICITED_ENABLE,
922 AC_USRSP_EN | spec->pin[i]);
923 }
Wu, Fengguang91504872008-11-05 11:16:56 +0800924 return 0;
925}
926
927static void intel_hdmi_free(struct hda_codec *codec)
928{
Takashi Iwaif208dba2008-11-21 09:11:50 +0100929 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang54a25f82009-10-30 11:44:26 +0100930 int i;
Takashi Iwaif208dba2008-11-21 09:11:50 +0100931
Wu Fengguang54a25f82009-10-30 11:44:26 +0100932 for (i = 0; i < spec->num_pins; i++)
933 snd_hda_eld_proc_free(codec, &spec->sink_eld[i]);
934
Takashi Iwaif208dba2008-11-21 09:11:50 +0100935 kfree(spec);
Wu, Fengguang91504872008-11-05 11:16:56 +0800936}
937
938static struct hda_codec_ops intel_hdmi_patch_ops = {
939 .init = intel_hdmi_init,
940 .free = intel_hdmi_free,
941 .build_pcms = intel_hdmi_build_pcms,
942 .build_controls = intel_hdmi_build_controls,
943 .unsol_event = intel_hdmi_unsol_event,
944};
945
Wu Fengguangfd080b22009-10-30 11:46:22 +0100946static int patch_intel_hdmi(struct hda_codec *codec)
Wu, Fengguang91504872008-11-05 11:16:56 +0800947{
948 struct intel_hdmi_spec *spec;
Wu Fengguang54a25f82009-10-30 11:44:26 +0100949 int i;
Wu, Fengguang91504872008-11-05 11:16:56 +0800950
951 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
952 if (spec == NULL)
953 return -ENOMEM;
954
Wu, Fengguang91504872008-11-05 11:16:56 +0800955 codec->spec = spec;
Wu Fengguangf4243672009-10-30 11:45:35 +0100956 if (intel_hdmi_parse_codec(codec) < 0) {
957 codec->spec = NULL;
958 kfree(spec);
959 return -EINVAL;
960 }
Wu, Fengguang91504872008-11-05 11:16:56 +0800961 codec->patch_ops = intel_hdmi_patch_ops;
962
Wu Fengguang54a25f82009-10-30 11:44:26 +0100963 for (i = 0; i < spec->num_pins; i++)
964 snd_hda_eld_proc_new(codec, &spec->sink_eld[i], i);
Wu Fengguang5f1e71b2008-11-18 11:47:53 +0800965
Wu Fengguang698544d2008-11-19 08:56:17 +0800966 init_channel_allocations();
967
Wu, Fengguang91504872008-11-05 11:16:56 +0800968 return 0;
969}
970
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100971static struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
Takashi Iwai74c61132008-12-18 09:11:33 +0100972 { .id = 0x808629fb, .name = "G45 DEVCL", .patch = patch_intel_hdmi },
973 { .id = 0x80862801, .name = "G45 DEVBLC", .patch = patch_intel_hdmi },
974 { .id = 0x80862802, .name = "G45 DEVCTG", .patch = patch_intel_hdmi },
975 { .id = 0x80862803, .name = "G45 DEVELK", .patch = patch_intel_hdmi },
Wu Fengguangfd080b22009-10-30 11:46:22 +0100976 { .id = 0x80862804, .name = "G45 DEVIBX", .patch = patch_intel_hdmi },
977 { .id = 0x80860054, .name = "Q57 DEVIBX", .patch = patch_intel_hdmi },
Wu Fengguang3a95cb92008-11-13 10:19:38 +0800978 { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
Wu, Fengguang91504872008-11-05 11:16:56 +0800979 {} /* terminator */
980};
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100981
982MODULE_ALIAS("snd-hda-codec-id:808629fb");
983MODULE_ALIAS("snd-hda-codec-id:80862801");
984MODULE_ALIAS("snd-hda-codec-id:80862802");
985MODULE_ALIAS("snd-hda-codec-id:80862803");
Wu Fengguanga57c0eb2009-02-11 15:22:31 +0800986MODULE_ALIAS("snd-hda-codec-id:80862804");
Jaroslav Kysela87a8c372009-07-23 10:58:29 +0200987MODULE_ALIAS("snd-hda-codec-id:80860054");
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100988MODULE_ALIAS("snd-hda-codec-id:10951392");
989
990MODULE_LICENSE("GPL");
991MODULE_DESCRIPTION("Intel HDMI HD-audio codec");
992
993static struct hda_codec_preset_list intel_list = {
994 .preset = snd_hda_preset_intelhdmi,
995 .owner = THIS_MODULE,
996};
997
998static int __init patch_intelhdmi_init(void)
999{
1000 return snd_hda_add_codec_preset(&intel_list);
1001}
1002
1003static void __exit patch_intelhdmi_exit(void)
1004{
1005 snd_hda_delete_codec_preset(&intel_list);
1006}
1007
1008module_init(patch_intelhdmi_init)
1009module_exit(patch_intelhdmi_exit)