blob: 6be5ca44a83b56559c9970d2b910e917b1c51002 [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 Fengguang559059b2009-08-02 16:48:55 +080036static hda_nid_t cvt_nid; /* audio converter */
37static hda_nid_t pin_nid; /* HDMI output pin */
Wu, Fengguang91504872008-11-05 11:16:56 +080038
39#define INTEL_HDMI_EVENT_TAG 0x08
40
Wu, Fengguang91504872008-11-05 11:16:56 +080041struct intel_hdmi_spec {
Wu, Fengguang91504872008-11-05 11:16:56 +080042 struct hda_pcm pcm_rec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +080043 struct hdmi_eld sink_eld;
Wu, Fengguang91504872008-11-05 11:16:56 +080044};
45
Wu, Fengguang91504872008-11-05 11:16:56 +080046struct hdmi_audio_infoframe {
47 u8 type; /* 0x84 */
48 u8 ver; /* 0x01 */
49 u8 len; /* 0x0a */
50
51 u8 checksum; /* PB0 */
52 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
53 u8 SS01_SF24;
54 u8 CXT04;
55 u8 CA;
56 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang4e19c582008-11-19 15:13:59 +080057 u8 reserved[5]; /* PB6 - PB10 */
Wu, Fengguang91504872008-11-05 11:16:56 +080058};
59
60/*
Wu Fengguang698544d2008-11-19 08:56:17 +080061 * CEA speaker placement:
62 *
63 * FLH FCH FRH
64 * FLW FL FLC FC FRC FR FRW
65 *
66 * LFE
67 * TC
68 *
69 * RL RLC RC RRC RR
70 *
71 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
72 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
73 */
74enum cea_speaker_placement {
75 FL = (1 << 0), /* Front Left */
76 FC = (1 << 1), /* Front Center */
77 FR = (1 << 2), /* Front Right */
78 FLC = (1 << 3), /* Front Left Center */
79 FRC = (1 << 4), /* Front Right Center */
80 RL = (1 << 5), /* Rear Left */
81 RC = (1 << 6), /* Rear Center */
82 RR = (1 << 7), /* Rear Right */
83 RLC = (1 << 8), /* Rear Left Center */
84 RRC = (1 << 9), /* Rear Right Center */
85 LFE = (1 << 10), /* Low Frequency Effect */
86 FLW = (1 << 11), /* Front Left Wide */
87 FRW = (1 << 12), /* Front Right Wide */
88 FLH = (1 << 13), /* Front Left High */
89 FCH = (1 << 14), /* Front Center High */
90 FRH = (1 << 15), /* Front Right High */
91 TC = (1 << 16), /* Top Center */
92};
93
94/*
95 * ELD SA bits in the CEA Speaker Allocation data block
96 */
97static int eld_speaker_allocation_bits[] = {
98 [0] = FL | FR,
99 [1] = LFE,
100 [2] = FC,
101 [3] = RL | RR,
102 [4] = RC,
103 [5] = FLC | FRC,
104 [6] = RLC | RRC,
105 /* the following are not defined in ELD yet */
106 [7] = FLW | FRW,
107 [8] = FLH | FRH,
108 [9] = TC,
109 [10] = FCH,
110};
111
112struct cea_channel_speaker_allocation {
113 int ca_index;
114 int speakers[8];
115
116 /* derived values, just for convenience */
117 int channels;
118 int spk_mask;
119};
120
121/*
122 * This is an ordered list!
123 *
124 * The preceding ones have better chances to be selected by
125 * hdmi_setup_channel_allocation().
126 */
127static struct cea_channel_speaker_allocation channel_allocations[] = {
128/* channel: 8 7 6 5 4 3 2 1 */
129{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
130 /* 2.1 */
131{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
Wu Fengguang4e19c582008-11-19 15:13:59 +0800132 /* Dolby Surround */
Wu Fengguang698544d2008-11-19 08:56:17 +0800133{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
134{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
135{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
136{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
137{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
138{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
139{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
140{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
141{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
142 /* 5.1 */
143{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
144{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
145{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
146{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
147 /* 6.1 */
148{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
149{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
150{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
151{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
152 /* 7.1 */
153{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
154{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
155{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
156{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
157{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
158{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
159{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
160{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
161{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
162{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
163{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
164{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
165{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
166{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
167{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
168{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
169{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
170{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
171{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
172{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
173{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
174{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
175{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
176{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
177{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
178{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
179{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
180{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
181{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
182{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
183{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
184};
185
186/*
Wu, Fengguang91504872008-11-05 11:16:56 +0800187 * HDMI routines
188 */
189
Takashi Iwaibeb0b9cf2008-11-05 07:58:25 +0100190#ifdef BE_PARANOID
Wu Fengguang6797cf22009-10-30 11:40:40 +0100191static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
Wu, Fengguang91504872008-11-05 11:16:56 +0800192 int *packet_index, int *byte_index)
193{
194 int val;
195
Wu Fengguang6797cf22009-10-30 11:40:40 +0100196 val = snd_hda_codec_read(codec, pin_nid, 0,
197 AC_VERB_GET_HDMI_DIP_INDEX, 0);
Wu, Fengguang91504872008-11-05 11:16:56 +0800198
199 *packet_index = val >> 5;
200 *byte_index = val & 0x1f;
201}
Takashi Iwaibeb0b9cf2008-11-05 07:58:25 +0100202#endif
Wu, Fengguang91504872008-11-05 11:16:56 +0800203
Wu Fengguang6797cf22009-10-30 11:40:40 +0100204static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
Wu, Fengguang91504872008-11-05 11:16:56 +0800205 int packet_index, int byte_index)
206{
207 int val;
208
209 val = (packet_index << 5) | (byte_index & 0x1f);
210
Wu Fengguang6797cf22009-10-30 11:40:40 +0100211 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
Wu, Fengguang91504872008-11-05 11:16:56 +0800212}
213
Wu Fengguang6797cf22009-10-30 11:40:40 +0100214static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
Wu, Fengguang91504872008-11-05 11:16:56 +0800215 unsigned char val)
216{
Wu Fengguang6797cf22009-10-30 11:40:40 +0100217 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
Wu, Fengguang91504872008-11-05 11:16:56 +0800218}
219
Wu Fengguang6797cf22009-10-30 11:40:40 +0100220static void hdmi_enable_output(struct hda_codec *codec, hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800221{
Wu Fengguang796359d2008-11-17 16:57:33 +0800222 /* Unmute */
Wu Fengguang559059b2009-08-02 16:48:55 +0800223 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
224 snd_hda_codec_write(codec, pin_nid, 0,
Wu Fengguang796359d2008-11-17 16:57:33 +0800225 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
226 /* Enable pin out */
Wu Fengguang559059b2009-08-02 16:48:55 +0800227 snd_hda_codec_write(codec, pin_nid, 0,
228 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu, Fengguang91504872008-11-05 11:16:56 +0800229}
230
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800231/*
232 * Enable Audio InfoFrame Transmission
233 */
Wu Fengguang6797cf22009-10-30 11:40:40 +0100234static void hdmi_start_infoframe_trans(struct hda_codec *codec,
235 hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800236{
Wu Fengguang559059b2009-08-02 16:48:55 +0800237 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
238 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800239 AC_DIPXMIT_BEST);
240}
Wu, Fengguang91504872008-11-05 11:16:56 +0800241
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800242/*
243 * Disable Audio InfoFrame Transmission
244 */
Wu Fengguang6797cf22009-10-30 11:40:40 +0100245static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
246 hda_nid_t pin_nid)
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800247{
Wu Fengguang559059b2009-08-02 16:48:55 +0800248 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
249 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
Wu Fengguang606c0ce2009-02-11 15:22:29 +0800250 AC_DIPXMIT_DISABLE);
Wu, Fengguang91504872008-11-05 11:16:56 +0800251}
252
Wu Fengguang6797cf22009-10-30 11:40:40 +0100253static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800254{
Wu Fengguang6797cf22009-10-30 11:40:40 +0100255 return 1 + snd_hda_codec_read(codec, nid, 0,
Wu, Fengguang91504872008-11-05 11:16:56 +0800256 AC_VERB_GET_CVT_CHAN_COUNT, 0);
257}
258
Wu Fengguang6797cf22009-10-30 11:40:40 +0100259static void hdmi_set_channel_count(struct hda_codec *codec,
260 hda_nid_t nid, int chs)
Wu, Fengguang91504872008-11-05 11:16:56 +0800261{
Wu Fengguang6797cf22009-10-30 11:40:40 +0100262 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
Wu, Fengguang91504872008-11-05 11:16:56 +0800263
Wu Fengguang6797cf22009-10-30 11:40:40 +0100264#ifdef CONFIG_SND_DEBUG_VERBOSE
265 if (chs != hdmi_get_channel_count(codec, nid))
Wu Fengguang03284c82008-11-22 09:40:53 +0800266 snd_printd(KERN_INFO "HDMI channel count: expect %d, get %d\n",
Wu Fengguang6797cf22009-10-30 11:40:40 +0100267 chs, hdmi_get_channel_count(codec, nid));
268#endif
Wu, Fengguang91504872008-11-05 11:16:56 +0800269}
270
Wu Fengguang6797cf22009-10-30 11:40:40 +0100271static void hdmi_debug_channel_mapping(struct hda_codec *codec, hda_nid_t nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800272{
273#ifdef CONFIG_SND_DEBUG_VERBOSE
274 int i;
275 int slot;
276
277 for (i = 0; i < 8; i++) {
Wu Fengguang6797cf22009-10-30 11:40:40 +0100278 slot = snd_hda_codec_read(codec, nid, 0,
Wu, Fengguang91504872008-11-05 11:16:56 +0800279 AC_VERB_GET_HDMI_CHAN_SLOT, i);
Wu Fengguang03284c82008-11-22 09:40:53 +0800280 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
Wu Fengguang4e19c582008-11-19 15:13:59 +0800281 slot >> 4, slot & 0x7);
Wu, Fengguang91504872008-11-05 11:16:56 +0800282 }
283#endif
284}
285
Wu, Fengguang91504872008-11-05 11:16:56 +0800286static void hdmi_parse_eld(struct hda_codec *codec)
287{
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800288 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800289 struct hdmi_eld *eld = &spec->sink_eld;
Wu, Fengguang91504872008-11-05 11:16:56 +0800290
Wu Fengguang559059b2009-08-02 16:48:55 +0800291 if (!snd_hdmi_get_eld(eld, codec, pin_nid))
Wu Fengguang7f4a9f42008-11-18 11:47:52 +0800292 snd_hdmi_show_eld(eld);
Wu, Fengguang91504872008-11-05 11:16:56 +0800293}
294
295
296/*
Wu Fengguang4e19c582008-11-19 15:13:59 +0800297 * Audio InfoFrame routines
Wu, Fengguang91504872008-11-05 11:16:56 +0800298 */
299
Wu Fengguang6797cf22009-10-30 11:40:40 +0100300static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800301{
302#ifdef CONFIG_SND_DEBUG_VERBOSE
303 int i;
304 int size;
305
Wu Fengguang559059b2009-08-02 16:48:55 +0800306 size = snd_hdmi_get_eld_size(codec, pin_nid);
Wu Fengguang03284c82008-11-22 09:40:53 +0800307 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
Wu, Fengguang91504872008-11-05 11:16:56 +0800308
309 for (i = 0; i < 8; i++) {
Wu Fengguang559059b2009-08-02 16:48:55 +0800310 size = snd_hda_codec_read(codec, pin_nid, 0,
Wu, Fengguang91504872008-11-05 11:16:56 +0800311 AC_VERB_GET_HDMI_DIP_SIZE, i);
Wu Fengguang03284c82008-11-22 09:40:53 +0800312 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu, Fengguang91504872008-11-05 11:16:56 +0800313 }
314#endif
315}
316
Wu Fengguang6797cf22009-10-30 11:40:40 +0100317static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
Wu, Fengguang91504872008-11-05 11:16:56 +0800318{
319#ifdef BE_PARANOID
320 int i, j;
321 int size;
322 int pi, bi;
323 for (i = 0; i < 8; i++) {
Wu Fengguang559059b2009-08-02 16:48:55 +0800324 size = snd_hda_codec_read(codec, pin_nid, 0,
Wu, Fengguang91504872008-11-05 11:16:56 +0800325 AC_VERB_GET_HDMI_DIP_SIZE, i);
326 if (size == 0)
327 continue;
328
Wu Fengguang559059b2009-08-02 16:48:55 +0800329 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
Wu, Fengguang91504872008-11-05 11:16:56 +0800330 for (j = 1; j < 1000; j++) {
Wu Fengguang559059b2009-08-02 16:48:55 +0800331 hdmi_write_dip_byte(codec, pin_nid, 0x0);
332 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
Wu, Fengguang91504872008-11-05 11:16:56 +0800333 if (pi != i)
334 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
335 bi, pi, i);
336 if (bi == 0) /* byte index wrapped around */
337 break;
338 }
339 snd_printd(KERN_INFO
Wu Fengguang03284c82008-11-22 09:40:53 +0800340 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
341 i, size, j);
Wu, Fengguang91504872008-11-05 11:16:56 +0800342 }
343#endif
344}
345
Wu Fengguang5457a982008-11-19 08:56:15 +0800346static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
Wu Fengguang6797cf22009-10-30 11:40:40 +0100347 hda_nid_t pin_nid,
348 struct hdmi_audio_infoframe *ai)
Wu, Fengguang91504872008-11-05 11:16:56 +0800349{
Wu Fengguang5457a982008-11-19 08:56:15 +0800350 u8 *params = (u8 *)ai;
Wu Fengguang9a957a22009-02-11 15:22:30 +0800351 u8 sum = 0;
Wu, Fengguang91504872008-11-05 11:16:56 +0800352 int i;
353
Wu Fengguang6797cf22009-10-30 11:40:40 +0100354 hdmi_debug_dip_size(codec, pin_nid);
355 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
Wu, Fengguang91504872008-11-05 11:16:56 +0800356
Wu Fengguang9a957a22009-02-11 15:22:30 +0800357 for (i = 0; i < sizeof(ai); i++)
358 sum += params[i];
359 ai->checksum = - sum;
360
Wu Fengguang559059b2009-08-02 16:48:55 +0800361 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang5457a982008-11-19 08:56:15 +0800362 for (i = 0; i < sizeof(ai); i++)
Wu Fengguang559059b2009-08-02 16:48:55 +0800363 hdmi_write_dip_byte(codec, pin_nid, params[i]);
Wu, Fengguang91504872008-11-05 11:16:56 +0800364}
365
Wu Fengguang698544d2008-11-19 08:56:17 +0800366/*
367 * Compute derived values in channel_allocations[].
368 */
369static void init_channel_allocations(void)
370{
371 int i, j;
372 struct cea_channel_speaker_allocation *p;
373
374 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
375 p = channel_allocations + i;
376 p->channels = 0;
377 p->spk_mask = 0;
378 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
379 if (p->speakers[j]) {
380 p->channels++;
381 p->spk_mask |= p->speakers[j];
382 }
383 }
384}
385
386/*
387 * The transformation takes two steps:
388 *
389 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
390 * spk_mask => (channel_allocations[]) => ai->CA
391 *
392 * TODO: it could select the wrong CA from multiple candidates.
393*/
Wu Fengguang6797cf22009-10-30 11:40:40 +0100394static int hdmi_setup_channel_allocation(struct hda_codec *codec, hda_nid_t nid,
Wu Fengguang698544d2008-11-19 08:56:17 +0800395 struct hdmi_audio_infoframe *ai)
396{
397 struct intel_hdmi_spec *spec = codec->spec;
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800398 struct hdmi_eld *eld = &spec->sink_eld;
Wu Fengguang698544d2008-11-19 08:56:17 +0800399 int i;
400 int spk_mask = 0;
401 int channels = 1 + (ai->CC02_CT47 & 0x7);
402 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
403
404 /*
405 * CA defaults to 0 for basic stereo audio
406 */
Wu Fengguang698544d2008-11-19 08:56:17 +0800407 if (channels <= 2)
408 return 0;
409
410 /*
Wu Fengguanga1667e42009-02-11 15:22:28 +0800411 * HDMI sink's ELD info cannot always be retrieved for now, e.g.
412 * in console or for audio devices. Assume the highest speakers
413 * configuration, to _not_ prohibit multi-channel audio playback.
414 */
415 if (!eld->spk_alloc)
416 eld->spk_alloc = 0xffff;
417
418 /*
Wu Fengguang698544d2008-11-19 08:56:17 +0800419 * expand ELD's speaker allocation mask
420 *
421 * ELD tells the speaker mask in a compact(paired) form,
Wu Fengguangb83923a2008-11-22 09:40:51 +0800422 * expand ELD's notions to match the ones used by Audio InfoFrame.
Wu Fengguang698544d2008-11-19 08:56:17 +0800423 */
424 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
425 if (eld->spk_alloc & (1 << i))
426 spk_mask |= eld_speaker_allocation_bits[i];
427 }
428
429 /* search for the first working match in the CA table */
430 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
431 if (channels == channel_allocations[i].channels &&
432 (spk_mask & channel_allocations[i].spk_mask) ==
433 channel_allocations[i].spk_mask) {
434 ai->CA = channel_allocations[i].ca_index;
Wu Fengguangcc02b832008-11-22 09:40:52 +0800435 break;
Wu Fengguang698544d2008-11-19 08:56:17 +0800436 }
437 }
438
439 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
Wu Fengguangcc02b832008-11-22 09:40:52 +0800440 snd_printdd(KERN_INFO
441 "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
442 ai->CA, channels, buf);
443
444 return ai->CA;
Wu Fengguang698544d2008-11-19 08:56:17 +0800445}
446
Wu Fengguang6797cf22009-10-30 11:40:40 +0100447static void hdmi_setup_channel_mapping(struct hda_codec *codec, hda_nid_t nid,
448 struct hdmi_audio_infoframe *ai)
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800449{
Wu Fengguang559059b2009-08-02 16:48:55 +0800450 int i;
451
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800452 if (!ai->CA)
453 return;
454
455 /*
456 * TODO: adjust channel mapping if necessary
457 * ALSA sequence is front/surr/clfe/side?
458 */
459
Wu Fengguang559059b2009-08-02 16:48:55 +0800460 for (i = 0; i < 8; i++)
Wu Fengguang6797cf22009-10-30 11:40:40 +0100461 snd_hda_codec_write(codec, nid, 0,
Wu Fengguang559059b2009-08-02 16:48:55 +0800462 AC_VERB_SET_HDMI_CHAN_SLOT,
463 (i << 4) | i);
464
Wu Fengguang6797cf22009-10-30 11:40:40 +0100465 hdmi_debug_channel_mapping(codec, nid);
Wu Fengguang9c8641e2008-11-19 08:56:18 +0800466}
467
468
Wu Fengguang6797cf22009-10-30 11:40:40 +0100469static void hdmi_setup_audio_infoframe(struct hda_codec *codec, hda_nid_t nid,
Wu Fengguang5457a982008-11-19 08:56:15 +0800470 struct snd_pcm_substream *substream)
471{
472 struct hdmi_audio_infoframe ai = {
473 .type = 0x84,
474 .ver = 0x01,
475 .len = 0x0a,
476 .CC02_CT47 = substream->runtime->channels - 1,
477 };
478
Wu Fengguang6797cf22009-10-30 11:40:40 +0100479 hdmi_setup_channel_allocation(codec, nid, &ai);
480 hdmi_setup_channel_mapping(codec, nid, &ai);
Wu Fengguang698544d2008-11-19 08:56:17 +0800481
Wu Fengguang6797cf22009-10-30 11:40:40 +0100482 hdmi_fill_audio_infoframe(codec, pin_nid, &ai);
483 hdmi_start_infoframe_trans(codec, pin_nid);
Wu Fengguang5457a982008-11-19 08:56:15 +0800484}
485
Wu, Fengguang91504872008-11-05 11:16:56 +0800486
487/*
488 * Unsolicited events
489 */
490
491static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
492{
493 int pind = !!(res & AC_UNSOL_RES_PD);
494 int eldv = !!(res & AC_UNSOL_RES_ELDV);
495
Wu Fengguang03284c82008-11-22 09:40:53 +0800496 printk(KERN_INFO
497 "HDMI hot plug event: Presence_Detect=%d ELD_Valid=%d\n",
498 pind, eldv);
Wu, Fengguang91504872008-11-05 11:16:56 +0800499
500 if (pind && eldv) {
501 hdmi_parse_eld(codec);
502 /* TODO: do real things about ELD */
503 }
504}
505
506static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
507{
508 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
509 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
510 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
511
Wu Fengguang03284c82008-11-22 09:40:53 +0800512 printk(KERN_INFO
513 "HDMI content protection event: SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
514 subtag,
515 cp_state,
516 cp_ready);
Wu, Fengguang91504872008-11-05 11:16:56 +0800517
Wu Fengguang03284c82008-11-22 09:40:53 +0800518 /* TODO */
Wu, Fengguang91504872008-11-05 11:16:56 +0800519 if (cp_state)
520 ;
521 if (cp_ready)
522 ;
523}
524
525
526static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
527{
528 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
529 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
530
531 if (tag != INTEL_HDMI_EVENT_TAG) {
Wu Fengguang03284c82008-11-22 09:40:53 +0800532 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
Wu, Fengguang91504872008-11-05 11:16:56 +0800533 return;
534 }
535
536 if (subtag == 0)
537 hdmi_intrinsic_event(codec, res);
538 else
539 hdmi_non_intrinsic_event(codec, res);
540}
541
542/*
543 * Callbacks
544 */
545
Wu, Fengguang91504872008-11-05 11:16:56 +0800546static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
547 struct hda_codec *codec,
548 unsigned int stream_tag,
549 unsigned int format,
550 struct snd_pcm_substream *substream)
551{
Wu Fengguang7bedb012009-10-30 11:41:44 +0100552 hdmi_set_channel_count(codec, cvt_nid,
553 substream->runtime->channels);
Wu, Fengguang91504872008-11-05 11:16:56 +0800554
Wu Fengguang6797cf22009-10-30 11:40:40 +0100555 hdmi_setup_audio_infoframe(codec, cvt_nid, substream);
Wu, Fengguang91504872008-11-05 11:16:56 +0800556
Wu Fengguang7bedb012009-10-30 11:41:44 +0100557 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
Wu, Fengguang91504872008-11-05 11:16:56 +0800558 return 0;
559}
560
Wu Fengguangddb81522009-10-30 11:43:03 +0100561static int intel_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
562 struct hda_codec *codec,
563 struct snd_pcm_substream *substream)
564{
565 struct intel_hdmi_spec *spec = codec->spec;
566
567 hdmi_stop_infoframe_trans(codec, pin_nid);
568
569 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
570 return 0;
571}
572
Wu, Fengguang91504872008-11-05 11:16:56 +0800573static struct hda_pcm_stream intel_hdmi_pcm_playback = {
574 .substreams = 1,
575 .channels_min = 2,
576 .channels_max = 8,
Wu, Fengguang91504872008-11-05 11:16:56 +0800577 .ops = {
Wu Fengguang70ca35f2009-10-30 11:42:18 +0100578 .prepare = intel_hdmi_playback_pcm_prepare,
579 .cleanup = intel_hdmi_playback_pcm_cleanup,
Wu, Fengguang91504872008-11-05 11:16:56 +0800580 },
581};
582
583static int intel_hdmi_build_pcms(struct hda_codec *codec)
584{
585 struct intel_hdmi_spec *spec = codec->spec;
586 struct hda_pcm *info = &spec->pcm_rec;
587
588 codec->num_pcms = 1;
589 codec->pcm_info = info;
590
Wu Fengguang559059b2009-08-02 16:48:55 +0800591 /* NID to query formats and rates and setup streams */
592 intel_hdmi_pcm_playback.nid = cvt_nid;
593
Wu, Fengguang91504872008-11-05 11:16:56 +0800594 info->name = "INTEL HDMI";
595 info->pcm_type = HDA_PCM_TYPE_HDMI;
596 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
597
598 return 0;
599}
600
601static int intel_hdmi_build_controls(struct hda_codec *codec)
602{
603 struct intel_hdmi_spec *spec = codec->spec;
604 int err;
605
Wu Fengguang7bedb012009-10-30 11:41:44 +0100606 err = snd_hda_create_spdif_out_ctls(codec, cvt_nid);
Wu, Fengguang91504872008-11-05 11:16:56 +0800607 if (err < 0)
608 return err;
609
610 return 0;
611}
612
613static int intel_hdmi_init(struct hda_codec *codec)
614{
Wu Fengguang6797cf22009-10-30 11:40:40 +0100615 hdmi_enable_output(codec, pin_nid);
Wu, Fengguang91504872008-11-05 11:16:56 +0800616
Wu Fengguang559059b2009-08-02 16:48:55 +0800617 snd_hda_codec_write(codec, pin_nid, 0,
618 AC_VERB_SET_UNSOLICITED_ENABLE,
619 AC_USRSP_EN | INTEL_HDMI_EVENT_TAG);
Wu, Fengguang91504872008-11-05 11:16:56 +0800620 return 0;
621}
622
623static void intel_hdmi_free(struct hda_codec *codec)
624{
Takashi Iwaif208dba2008-11-21 09:11:50 +0100625 struct intel_hdmi_spec *spec = codec->spec;
626
627 snd_hda_eld_proc_free(codec, &spec->sink_eld);
628 kfree(spec);
Wu, Fengguang91504872008-11-05 11:16:56 +0800629}
630
631static struct hda_codec_ops intel_hdmi_patch_ops = {
632 .init = intel_hdmi_init,
633 .free = intel_hdmi_free,
634 .build_pcms = intel_hdmi_build_pcms,
635 .build_controls = intel_hdmi_build_controls,
636 .unsol_event = intel_hdmi_unsol_event,
637};
638
Wu Fengguang559059b2009-08-02 16:48:55 +0800639static int do_patch_intel_hdmi(struct hda_codec *codec)
Wu, Fengguang91504872008-11-05 11:16:56 +0800640{
641 struct intel_hdmi_spec *spec;
642
643 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
644 if (spec == NULL)
645 return -ENOMEM;
646
Wu, Fengguang91504872008-11-05 11:16:56 +0800647 codec->spec = spec;
648 codec->patch_ops = intel_hdmi_patch_ops;
649
Wu Fengguang5b87ebb2008-11-19 15:14:00 +0800650 snd_hda_eld_proc_new(codec, &spec->sink_eld);
Wu Fengguang5f1e71b2008-11-18 11:47:53 +0800651
Wu Fengguang698544d2008-11-19 08:56:17 +0800652 init_channel_allocations();
653
Wu, Fengguang91504872008-11-05 11:16:56 +0800654 return 0;
655}
656
Wu Fengguang559059b2009-08-02 16:48:55 +0800657static int patch_intel_hdmi(struct hda_codec *codec)
658{
659 cvt_nid = 0x02;
660 pin_nid = 0x03;
661 return do_patch_intel_hdmi(codec);
662}
663
664static int patch_intel_hdmi_ibexpeak(struct hda_codec *codec)
665{
666 cvt_nid = 0x02;
667 pin_nid = 0x04;
668 return do_patch_intel_hdmi(codec);
669}
670
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100671static struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
Takashi Iwai74c61132008-12-18 09:11:33 +0100672 { .id = 0x808629fb, .name = "G45 DEVCL", .patch = patch_intel_hdmi },
673 { .id = 0x80862801, .name = "G45 DEVBLC", .patch = patch_intel_hdmi },
674 { .id = 0x80862802, .name = "G45 DEVCTG", .patch = patch_intel_hdmi },
675 { .id = 0x80862803, .name = "G45 DEVELK", .patch = patch_intel_hdmi },
Wu Fengguang739b47f2009-10-30 11:34:19 +0100676 { .id = 0x80862804, .name = "G45 DEVIBX", .patch = patch_intel_hdmi_ibexpeak },
Wu Fengguang559059b2009-08-02 16:48:55 +0800677 { .id = 0x80860054, .name = "Q57 DEVIBX", .patch = patch_intel_hdmi_ibexpeak },
Wu Fengguang3a95cb92008-11-13 10:19:38 +0800678 { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
Wu, Fengguang91504872008-11-05 11:16:56 +0800679 {} /* terminator */
680};
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100681
682MODULE_ALIAS("snd-hda-codec-id:808629fb");
683MODULE_ALIAS("snd-hda-codec-id:80862801");
684MODULE_ALIAS("snd-hda-codec-id:80862802");
685MODULE_ALIAS("snd-hda-codec-id:80862803");
Wu Fengguanga57c0eb2009-02-11 15:22:31 +0800686MODULE_ALIAS("snd-hda-codec-id:80862804");
Jaroslav Kysela87a8c372009-07-23 10:58:29 +0200687MODULE_ALIAS("snd-hda-codec-id:80860054");
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100688MODULE_ALIAS("snd-hda-codec-id:10951392");
689
690MODULE_LICENSE("GPL");
691MODULE_DESCRIPTION("Intel HDMI HD-audio codec");
692
693static struct hda_codec_preset_list intel_list = {
694 .preset = snd_hda_preset_intelhdmi,
695 .owner = THIS_MODULE,
696};
697
698static int __init patch_intelhdmi_init(void)
699{
700 return snd_hda_add_codec_preset(&intel_list);
701}
702
703static void __exit patch_intelhdmi_exit(void)
704{
705 snd_hda_delete_codec_preset(&intel_list);
706}
707
708module_init(patch_intelhdmi_init)
709module_exit(patch_intelhdmi_exit)