blob: 1e381918eb826a89abf8192ac1b903830394b76b [file] [log] [blame]
Wu Fengguang079d88c2010-03-08 10:44:23 +08001/*
2 *
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
4 *
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
Takashi Iwai84eb01b2010-09-07 12:27:25 +02006 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
Wu Fengguang079d88c2010-03-08 10:44:23 +08009 *
10 * Authors:
11 * Wu Fengguang <wfg@linux.intel.com>
12 *
13 * Maintained by:
14 * Wu Fengguang <wfg@linux.intel.com>
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
Takashi Iwai84eb01b2010-09-07 12:27:25 +020031#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040034#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020035#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020036#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080037#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020038#include <sound/tlv.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020039#include "hda_codec.h"
40#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020041#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020042
Takashi Iwai0ebaa242011-01-11 18:11:04 +010043static bool static_hdmi_pcm;
44module_param(static_hdmi_pcm, bool, 0644);
45MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
46
Takashi Iwai84eb01b2010-09-07 12:27:25 +020047/*
48 * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
Stephen Warren384a48d2011-06-01 11:14:21 -060049 * could support N independent pipes, each of them can be connected to one or
Takashi Iwai84eb01b2010-09-07 12:27:25 +020050 * more ports (DVI, HDMI or DisplayPort).
51 *
52 * The HDA correspondence of pipes/ports are converter/pin nodes.
53 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +010054#define MAX_HDMI_CVTS 8
55#define MAX_HDMI_PINS 8
Wu Fengguang079d88c2010-03-08 10:44:23 +080056
Stephen Warren384a48d2011-06-01 11:14:21 -060057struct hdmi_spec_per_cvt {
58 hda_nid_t cvt_nid;
59 int assigned;
60 unsigned int channels_min;
61 unsigned int channels_max;
62 u32 rates;
63 u64 formats;
64 unsigned int maxbps;
65};
66
Takashi Iwai4eea3092013-02-07 18:18:19 +010067/* max. connections to a widget */
68#define HDA_MAX_CONNECTIONS 32
69
Stephen Warren384a48d2011-06-01 11:14:21 -060070struct hdmi_spec_per_pin {
71 hda_nid_t pin_nid;
72 int num_mux_nids;
73 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Wu Fengguang744626d2011-11-16 16:29:47 +080074
75 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060076 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080077 struct delayed_work work;
Wu Fengguangc6e84532011-11-18 16:59:32 -060078 int repoll_count;
Takashi Iwai1a6003b2012-09-06 17:42:08 +020079 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020080 bool chmap_set; /* channel-map override by ALSA API? */
81 unsigned char chmap[8]; /* ALSA API channel-map */
Stephen Warren384a48d2011-06-01 11:14:21 -060082};
83
Wu Fengguang079d88c2010-03-08 10:44:23 +080084struct hdmi_spec {
85 int num_cvts;
Stephen Warren384a48d2011-06-01 11:14:21 -060086 struct hdmi_spec_per_cvt cvts[MAX_HDMI_CVTS];
Takashi Iwaic88d4e82013-02-08 17:10:04 -050087 hda_nid_t cvt_nids[MAX_HDMI_CVTS];
Stephen Warren384a48d2011-06-01 11:14:21 -060088
Wu Fengguang079d88c2010-03-08 10:44:23 +080089 int num_pins;
Stephen Warren384a48d2011-06-01 11:14:21 -060090 struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
91 struct hda_pcm pcm_rec[MAX_HDMI_PINS];
Takashi Iwaid45e6882012-07-31 11:36:00 +020092 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080093
94 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060095 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080096 */
97 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020098 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080099};
100
101
102struct hdmi_audio_infoframe {
103 u8 type; /* 0x84 */
104 u8 ver; /* 0x01 */
105 u8 len; /* 0x0a */
106
Wu Fengguang53d7d692010-09-21 14:25:49 +0800107 u8 checksum;
108
Wu Fengguang079d88c2010-03-08 10:44:23 +0800109 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
110 u8 SS01_SF24;
111 u8 CXT04;
112 u8 CA;
113 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800114};
115
116struct dp_audio_infoframe {
117 u8 type; /* 0x84 */
118 u8 len; /* 0x1b */
119 u8 ver; /* 0x11 << 2 */
120
121 u8 CC02_CT47; /* match with HDMI infoframe from this on */
122 u8 SS01_SF24;
123 u8 CXT04;
124 u8 CA;
125 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800126};
127
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100128union audio_infoframe {
129 struct hdmi_audio_infoframe hdmi;
130 struct dp_audio_infoframe dp;
131 u8 bytes[0];
132};
133
Wu Fengguang079d88c2010-03-08 10:44:23 +0800134/*
135 * CEA speaker placement:
136 *
137 * FLH FCH FRH
138 * FLW FL FLC FC FRC FR FRW
139 *
140 * LFE
141 * TC
142 *
143 * RL RLC RC RRC RR
144 *
145 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
146 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
147 */
148enum cea_speaker_placement {
149 FL = (1 << 0), /* Front Left */
150 FC = (1 << 1), /* Front Center */
151 FR = (1 << 2), /* Front Right */
152 FLC = (1 << 3), /* Front Left Center */
153 FRC = (1 << 4), /* Front Right Center */
154 RL = (1 << 5), /* Rear Left */
155 RC = (1 << 6), /* Rear Center */
156 RR = (1 << 7), /* Rear Right */
157 RLC = (1 << 8), /* Rear Left Center */
158 RRC = (1 << 9), /* Rear Right Center */
159 LFE = (1 << 10), /* Low Frequency Effect */
160 FLW = (1 << 11), /* Front Left Wide */
161 FRW = (1 << 12), /* Front Right Wide */
162 FLH = (1 << 13), /* Front Left High */
163 FCH = (1 << 14), /* Front Center High */
164 FRH = (1 << 15), /* Front Right High */
165 TC = (1 << 16), /* Top Center */
166};
167
168/*
169 * ELD SA bits in the CEA Speaker Allocation data block
170 */
171static int eld_speaker_allocation_bits[] = {
172 [0] = FL | FR,
173 [1] = LFE,
174 [2] = FC,
175 [3] = RL | RR,
176 [4] = RC,
177 [5] = FLC | FRC,
178 [6] = RLC | RRC,
179 /* the following are not defined in ELD yet */
180 [7] = FLW | FRW,
181 [8] = FLH | FRH,
182 [9] = TC,
183 [10] = FCH,
184};
185
186struct cea_channel_speaker_allocation {
187 int ca_index;
188 int speakers[8];
189
190 /* derived values, just for convenience */
191 int channels;
192 int spk_mask;
193};
194
195/*
196 * ALSA sequence is:
197 *
198 * surround40 surround41 surround50 surround51 surround71
199 * ch0 front left = = = =
200 * ch1 front right = = = =
201 * ch2 rear left = = = =
202 * ch3 rear right = = = =
203 * ch4 LFE center center center
204 * ch5 LFE LFE
205 * ch6 side left
206 * ch7 side right
207 *
208 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
209 */
210static int hdmi_channel_mapping[0x32][8] = {
211 /* stereo */
212 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
213 /* 2.1 */
214 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
215 /* Dolby Surround */
216 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
217 /* surround40 */
218 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
219 /* 4ch */
220 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
221 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800222 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800223 /* surround50 */
224 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
225 /* surround51 */
226 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
227 /* 7.1 */
228 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
229};
230
231/*
232 * This is an ordered list!
233 *
234 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800235 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800236 */
237static struct cea_channel_speaker_allocation channel_allocations[] = {
238/* channel: 7 6 5 4 3 2 1 0 */
239{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
240 /* 2.1 */
241{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
242 /* Dolby Surround */
243{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
244 /* surround40 */
245{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
246 /* surround41 */
247{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
248 /* surround50 */
249{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
250 /* surround51 */
251{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
252 /* 6.1 */
253{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
254 /* surround71 */
255{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
256
257{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
258{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
259{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
260{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
261{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
262{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
263{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
264{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
265{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
266{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
267{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
268{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
269{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
270{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
271{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
272{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
273{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
274{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
275{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
276{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
277{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
278{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
279{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
280{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
281{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
282{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
283{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
284{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
285{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
286{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
287{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
288{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
289{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
290{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
291{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
292{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
293{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
294{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
295{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
296{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
297{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
298};
299
300
301/*
302 * HDMI routines
303 */
304
Stephen Warren384a48d2011-06-01 11:14:21 -0600305static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800306{
Stephen Warren384a48d2011-06-01 11:14:21 -0600307 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800308
Stephen Warren384a48d2011-06-01 11:14:21 -0600309 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
310 if (spec->pins[pin_idx].pin_nid == pin_nid)
311 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800312
Stephen Warren384a48d2011-06-01 11:14:21 -0600313 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
314 return -EINVAL;
315}
316
317static int hinfo_to_pin_index(struct hdmi_spec *spec,
318 struct hda_pcm_stream *hinfo)
319{
320 int pin_idx;
321
322 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
323 if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
324 return pin_idx;
325
326 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
327 return -EINVAL;
328}
329
330static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
331{
332 int cvt_idx;
333
334 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
335 if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
336 return cvt_idx;
337
338 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800339 return -EINVAL;
340}
341
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500342static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
343 struct snd_ctl_elem_info *uinfo)
344{
345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100346 struct hdmi_spec *spec = codec->spec;
347 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500348 int pin_idx;
349
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500350 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
351
352 pin_idx = kcontrol->private_value;
David Henningsson68e03de2013-02-19 16:11:23 +0100353 eld = &spec->pins[pin_idx].sink_eld;
354
355 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500356
357 return 0;
358}
359
360static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
361 struct snd_ctl_elem_value *ucontrol)
362{
363 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100364 struct hdmi_spec *spec = codec->spec;
365 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500366 int pin_idx;
367
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500368 pin_idx = kcontrol->private_value;
David Henningsson68e03de2013-02-19 16:11:23 +0100369 eld = &spec->pins[pin_idx].sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500370
David Henningsson68e03de2013-02-19 16:11:23 +0100371 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
372 snd_BUG();
373 return -EINVAL;
374 }
375
376 memset(ucontrol->value.bytes.data, 0,
377 ARRAY_SIZE(ucontrol->value.bytes.data));
378 if (eld->eld_valid)
379 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
380 eld->eld_size);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500381
382 return 0;
383}
384
385static struct snd_kcontrol_new eld_bytes_ctl = {
386 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
387 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
388 .name = "ELD",
389 .info = hdmi_eld_ctl_info,
390 .get = hdmi_eld_ctl_get,
391};
392
393static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
394 int device)
395{
396 struct snd_kcontrol *kctl;
397 struct hdmi_spec *spec = codec->spec;
398 int err;
399
400 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
401 if (!kctl)
402 return -ENOMEM;
403 kctl->private_value = pin_idx;
404 kctl->id.device = device;
405
406 err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
407 if (err < 0)
408 return err;
409
410 return 0;
411}
412
Wu Fengguang079d88c2010-03-08 10:44:23 +0800413#ifdef BE_PARANOID
414static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
415 int *packet_index, int *byte_index)
416{
417 int val;
418
419 val = snd_hda_codec_read(codec, pin_nid, 0,
420 AC_VERB_GET_HDMI_DIP_INDEX, 0);
421
422 *packet_index = val >> 5;
423 *byte_index = val & 0x1f;
424}
425#endif
426
427static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
428 int packet_index, int byte_index)
429{
430 int val;
431
432 val = (packet_index << 5) | (byte_index & 0x1f);
433
434 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
435}
436
437static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
438 unsigned char val)
439{
440 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
441}
442
Stephen Warren384a48d2011-06-01 11:14:21 -0600443static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800444{
445 /* Unmute */
446 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
447 snd_hda_codec_write(codec, pin_nid, 0,
448 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100449 /* Enable pin out: some machines with GM965 gets broken output when
450 * the pin is disabled or changed while using with HDMI
451 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800452 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100453 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800454}
455
Stephen Warren384a48d2011-06-01 11:14:21 -0600456static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800457{
Stephen Warren384a48d2011-06-01 11:14:21 -0600458 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800459 AC_VERB_GET_CVT_CHAN_COUNT, 0);
460}
461
462static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600463 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800464{
Stephen Warren384a48d2011-06-01 11:14:21 -0600465 if (chs != hdmi_get_channel_count(codec, cvt_nid))
466 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800467 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
468}
469
470
471/*
472 * Channel mapping routines
473 */
474
475/*
476 * Compute derived values in channel_allocations[].
477 */
478static void init_channel_allocations(void)
479{
480 int i, j;
481 struct cea_channel_speaker_allocation *p;
482
483 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
484 p = channel_allocations + i;
485 p->channels = 0;
486 p->spk_mask = 0;
487 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
488 if (p->speakers[j]) {
489 p->channels++;
490 p->spk_mask |= p->speakers[j];
491 }
492 }
493}
494
Wang Xingchao72357c72012-09-06 10:02:36 +0800495static int get_channel_allocation_order(int ca)
496{
497 int i;
498
499 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
500 if (channel_allocations[i].ca_index == ca)
501 break;
502 }
503 return i;
504}
505
Wu Fengguang079d88c2010-03-08 10:44:23 +0800506/*
507 * The transformation takes two steps:
508 *
509 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
510 * spk_mask => (channel_allocations[]) => ai->CA
511 *
512 * TODO: it could select the wrong CA from multiple candidates.
513*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600514static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800515{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800516 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800517 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800518 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800519 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
520
521 /*
522 * CA defaults to 0 for basic stereo audio
523 */
524 if (channels <= 2)
525 return 0;
526
Wu Fengguang079d88c2010-03-08 10:44:23 +0800527 /*
528 * expand ELD's speaker allocation mask
529 *
530 * ELD tells the speaker mask in a compact(paired) form,
531 * expand ELD's notions to match the ones used by Audio InfoFrame.
532 */
533 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100534 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800535 spk_mask |= eld_speaker_allocation_bits[i];
536 }
537
538 /* search for the first working match in the CA table */
539 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
540 if (channels == channel_allocations[i].channels &&
541 (spk_mask & channel_allocations[i].spk_mask) ==
542 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800543 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800544 break;
545 }
546 }
547
David Henningsson1613d6b2013-02-19 16:11:24 +0100548 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800549 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800550 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800551
Wu Fengguang53d7d692010-09-21 14:25:49 +0800552 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800553}
554
555static void hdmi_debug_channel_mapping(struct hda_codec *codec,
556 hda_nid_t pin_nid)
557{
558#ifdef CONFIG_SND_DEBUG_VERBOSE
559 int i;
560 int slot;
561
562 for (i = 0; i < 8; i++) {
563 slot = snd_hda_codec_read(codec, pin_nid, 0,
564 AC_VERB_GET_HDMI_CHAN_SLOT, i);
565 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
566 slot >> 4, slot & 0xf);
567 }
568#endif
569}
570
571
Takashi Iwaid45e6882012-07-31 11:36:00 +0200572static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800573 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800574 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800575 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800576{
577 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800578 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800579 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800580 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800581
Wang Xingchao72357c72012-09-06 10:02:36 +0800582 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800583
Wu Fengguang079d88c2010-03-08 10:44:23 +0800584 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800585 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800586 hdmi_channel_mapping[ca][i] = i | (i << 4);
587 for (; i < 8; i++)
588 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
589 }
590
Wang Xingchao433968d2012-09-06 10:02:37 +0800591 if (non_pcm) {
592 for (i = 0; i < channel_allocations[order].channels; i++)
593 non_pcm_mapping[i] = i | (i << 4);
594 for (; i < 8; i++)
595 non_pcm_mapping[i] = 0xf | (i << 4);
596 }
597
Wu Fengguang079d88c2010-03-08 10:44:23 +0800598 for (i = 0; i < 8; i++) {
599 err = snd_hda_codec_write(codec, pin_nid, 0,
600 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800601 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800602 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800603 snd_printdd(KERN_NOTICE
604 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800605 break;
606 }
607 }
608
609 hdmi_debug_channel_mapping(codec, pin_nid);
610}
611
Takashi Iwaid45e6882012-07-31 11:36:00 +0200612struct channel_map_table {
613 unsigned char map; /* ALSA API channel map position */
614 unsigned char cea_slot; /* CEA slot value */
615 int spk_mask; /* speaker position bit mask */
616};
617
618static struct channel_map_table map_tables[] = {
619 { SNDRV_CHMAP_FL, 0x00, FL },
620 { SNDRV_CHMAP_FR, 0x01, FR },
621 { SNDRV_CHMAP_RL, 0x04, RL },
622 { SNDRV_CHMAP_RR, 0x05, RR },
623 { SNDRV_CHMAP_LFE, 0x02, LFE },
624 { SNDRV_CHMAP_FC, 0x03, FC },
625 { SNDRV_CHMAP_RLC, 0x06, RLC },
626 { SNDRV_CHMAP_RRC, 0x07, RRC },
627 {} /* terminator */
628};
629
630/* from ALSA API channel position to speaker bit mask */
631static int to_spk_mask(unsigned char c)
632{
633 struct channel_map_table *t = map_tables;
634 for (; t->map; t++) {
635 if (t->map == c)
636 return t->spk_mask;
637 }
638 return 0;
639}
640
641/* from ALSA API channel position to CEA slot */
642static int to_cea_slot(unsigned char c)
643{
644 struct channel_map_table *t = map_tables;
645 for (; t->map; t++) {
646 if (t->map == c)
647 return t->cea_slot;
648 }
649 return 0x0f;
650}
651
652/* from CEA slot to ALSA API channel position */
653static int from_cea_slot(unsigned char c)
654{
655 struct channel_map_table *t = map_tables;
656 for (; t->map; t++) {
657 if (t->cea_slot == c)
658 return t->map;
659 }
660 return 0;
661}
662
663/* from speaker bit mask to ALSA API channel position */
664static int spk_to_chmap(int spk)
665{
666 struct channel_map_table *t = map_tables;
667 for (; t->map; t++) {
668 if (t->spk_mask == spk)
669 return t->map;
670 }
671 return 0;
672}
673
674/* get the CA index corresponding to the given ALSA API channel map */
675static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
676{
677 int i, spks = 0, spk_mask = 0;
678
679 for (i = 0; i < chs; i++) {
680 int mask = to_spk_mask(map[i]);
681 if (mask) {
682 spk_mask |= mask;
683 spks++;
684 }
685 }
686
687 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
688 if ((chs == channel_allocations[i].channels ||
689 spks == channel_allocations[i].channels) &&
690 (spk_mask & channel_allocations[i].spk_mask) ==
691 channel_allocations[i].spk_mask)
692 return channel_allocations[i].ca_index;
693 }
694 return -1;
695}
696
697/* set up the channel slots for the given ALSA API channel map */
698static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
699 hda_nid_t pin_nid,
700 int chs, unsigned char *map)
701{
702 int i;
703 for (i = 0; i < 8; i++) {
704 int val, err;
705 if (i < chs)
706 val = to_cea_slot(map[i]);
707 else
708 val = 0xf;
709 val |= (i << 4);
710 err = snd_hda_codec_write(codec, pin_nid, 0,
711 AC_VERB_SET_HDMI_CHAN_SLOT, val);
712 if (err)
713 return -EINVAL;
714 }
715 return 0;
716}
717
718/* store ALSA API channel map from the current default map */
719static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
720{
721 int i;
722 for (i = 0; i < 8; i++) {
723 if (i < channel_allocations[ca].channels)
724 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
725 else
726 map[i] = 0;
727 }
728}
729
730static void hdmi_setup_channel_mapping(struct hda_codec *codec,
731 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200732 int channels, unsigned char *map,
733 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200734{
Anssi Hannula20608732013-02-03 17:55:45 +0200735 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200736 hdmi_manual_setup_channel_mapping(codec, pin_nid,
737 channels, map);
738 } else {
739 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
740 hdmi_setup_fake_chmap(map, ca);
741 }
742}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800743
744/*
745 * Audio InfoFrame routines
746 */
747
748/*
749 * Enable Audio InfoFrame Transmission
750 */
751static void hdmi_start_infoframe_trans(struct hda_codec *codec,
752 hda_nid_t pin_nid)
753{
754 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
755 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
756 AC_DIPXMIT_BEST);
757}
758
759/*
760 * Disable Audio InfoFrame Transmission
761 */
762static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
763 hda_nid_t pin_nid)
764{
765 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
766 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
767 AC_DIPXMIT_DISABLE);
768}
769
770static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
771{
772#ifdef CONFIG_SND_DEBUG_VERBOSE
773 int i;
774 int size;
775
776 size = snd_hdmi_get_eld_size(codec, pin_nid);
777 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
778
779 for (i = 0; i < 8; i++) {
780 size = snd_hda_codec_read(codec, pin_nid, 0,
781 AC_VERB_GET_HDMI_DIP_SIZE, i);
782 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
783 }
784#endif
785}
786
787static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
788{
789#ifdef BE_PARANOID
790 int i, j;
791 int size;
792 int pi, bi;
793 for (i = 0; i < 8; i++) {
794 size = snd_hda_codec_read(codec, pin_nid, 0,
795 AC_VERB_GET_HDMI_DIP_SIZE, i);
796 if (size == 0)
797 continue;
798
799 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
800 for (j = 1; j < 1000; j++) {
801 hdmi_write_dip_byte(codec, pin_nid, 0x0);
802 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
803 if (pi != i)
804 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
805 bi, pi, i);
806 if (bi == 0) /* byte index wrapped around */
807 break;
808 }
809 snd_printd(KERN_INFO
810 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
811 i, size, j);
812 }
813#endif
814}
815
Wu Fengguang53d7d692010-09-21 14:25:49 +0800816static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800817{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800818 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800819 u8 sum = 0;
820 int i;
821
Wu Fengguang53d7d692010-09-21 14:25:49 +0800822 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800823
Wu Fengguang53d7d692010-09-21 14:25:49 +0800824 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800825 sum += bytes[i];
826
Wu Fengguang53d7d692010-09-21 14:25:49 +0800827 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800828}
829
830static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
831 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800832 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800833{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800834 int i;
835
836 hdmi_debug_dip_size(codec, pin_nid);
837 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
838
Wu Fengguang079d88c2010-03-08 10:44:23 +0800839 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800840 for (i = 0; i < size; i++)
841 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800842}
843
844static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800845 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800846{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800847 u8 val;
848 int i;
849
850 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
851 != AC_DIPXMIT_BEST)
852 return false;
853
854 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800855 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800856 val = snd_hda_codec_read(codec, pin_nid, 0,
857 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800858 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800859 return false;
860 }
861
862 return true;
863}
864
Stephen Warren384a48d2011-06-01 11:14:21 -0600865static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200866 bool non_pcm,
867 struct snd_pcm_substream *substream)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800868{
869 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600870 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
871 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800872 int channels = substream->runtime->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600873 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800874 int ca;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100875 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800876
Stephen Warren384a48d2011-06-01 11:14:21 -0600877 eld = &spec->pins[pin_idx].sink_eld;
878 if (!eld->monitor_present)
879 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800880
Takashi Iwaid45e6882012-07-31 11:36:00 +0200881 if (!non_pcm && per_pin->chmap_set)
882 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
883 else
884 ca = hdmi_channel_allocation(eld, channels);
885 if (ca < 0)
886 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800887
Stephen Warren384a48d2011-06-01 11:14:21 -0600888 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b2013-02-19 16:11:24 +0100889 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600890 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800891
Stephen Warren384a48d2011-06-01 11:14:21 -0600892 hdmi_ai->type = 0x84;
893 hdmi_ai->ver = 0x01;
894 hdmi_ai->len = 0x0a;
895 hdmi_ai->CC02_CT47 = channels - 1;
896 hdmi_ai->CA = ca;
897 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b2013-02-19 16:11:24 +0100898 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600899 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800900
Stephen Warren384a48d2011-06-01 11:14:21 -0600901 dp_ai->type = 0x84;
902 dp_ai->len = 0x1b;
903 dp_ai->ver = 0x11 << 2;
904 dp_ai->CC02_CT47 = channels - 1;
905 dp_ai->CA = ca;
906 } else {
907 snd_printd("HDMI: unknown connection type at pin %d\n",
908 pin_nid);
909 return;
910 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800911
Stephen Warren384a48d2011-06-01 11:14:21 -0600912 /*
913 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
914 * sizeof(*dp_ai) to avoid partial match/update problems when
915 * the user switches between HDMI/DP monitors.
916 */
917 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
918 sizeof(ai))) {
919 snd_printdd("hdmi_setup_audio_infoframe: "
920 "pin=%d channels=%d\n",
921 pin_nid,
922 channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200923 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200924 channels, per_pin->chmap,
925 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600926 hdmi_stop_infoframe_trans(codec, pin_nid);
927 hdmi_fill_audio_infoframe(codec, pin_nid,
928 ai.bytes, sizeof(ai));
929 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800930 } else {
931 /* For non-pcm audio switch, setup new channel mapping
932 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200933 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200934 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200935 channels, per_pin->chmap,
936 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800937 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800938
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200939 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800940}
941
942
943/*
944 * Unsolicited events
945 */
946
Wu Fengguangc6e84532011-11-18 16:59:32 -0600947static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200948
Wu Fengguang079d88c2010-03-08 10:44:23 +0800949static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
950{
951 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200952 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
953 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600954 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200955 struct hda_jack_tbl *jack;
956
957 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
958 if (!jack)
959 return;
960 pin_nid = jack->nid;
961 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800962
Fengguang Wufae3d882012-04-10 17:00:35 +0800963 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -0600964 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Fengguang Wufae3d882012-04-10 17:00:35 +0800965 codec->addr, pin_nid,
966 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800967
Stephen Warren384a48d2011-06-01 11:14:21 -0600968 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
969 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800970 return;
971
Wu Fengguangc6e84532011-11-18 16:59:32 -0600972 hdmi_present_sense(&spec->pins[pin_idx], 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200973 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800974}
975
976static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
977{
978 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
979 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
980 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
981 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
982
983 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200984 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600985 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800986 tag,
987 subtag,
988 cp_state,
989 cp_ready);
990
991 /* TODO */
992 if (cp_state)
993 ;
994 if (cp_ready)
995 ;
996}
997
998
999static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1000{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001001 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1002 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1003
Takashi Iwai3a938972011-10-28 01:16:55 +02001004 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001005 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1006 return;
1007 }
1008
1009 if (subtag == 0)
1010 hdmi_intrinsic_event(codec, res);
1011 else
1012 hdmi_non_intrinsic_event(codec, res);
1013}
1014
1015/*
1016 * Callbacks
1017 */
1018
Takashi Iwai92f10b32010-08-03 14:21:00 +02001019/* HBR should be Non-PCM, 8 channels */
1020#define is_hbr_format(format) \
1021 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1022
Stephen Warren384a48d2011-06-01 11:14:21 -06001023static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1024 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001025{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001026 int pinctl;
1027 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001028
Stephen Warren384a48d2011-06-01 11:14:21 -06001029 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1030 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001031 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1032
1033 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001034 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001035 new_pinctl |= AC_PINCTL_EPT_HBR;
1036 else
1037 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1038
1039 snd_printdd("hdmi_setup_stream: "
1040 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001041 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001042 pinctl == new_pinctl ? "" : "new-",
1043 new_pinctl);
1044
1045 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001046 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001047 AC_VERB_SET_PIN_WIDGET_CONTROL,
1048 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001049
Stephen Warren384a48d2011-06-01 11:14:21 -06001050 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001051 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001052 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1053 return -EINVAL;
1054 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001055
Stephen Warren384a48d2011-06-01 11:14:21 -06001056 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001057 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001058}
1059
1060/*
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001061 * HDA PCM callbacks
1062 */
1063static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1064 struct hda_codec *codec,
1065 struct snd_pcm_substream *substream)
1066{
1067 struct hdmi_spec *spec = codec->spec;
Takashi Iwai639cef02011-01-14 10:30:46 +01001068 struct snd_pcm_runtime *runtime = substream->runtime;
Stephen Warren384a48d2011-06-01 11:14:21 -06001069 int pin_idx, cvt_idx, mux_idx = 0;
1070 struct hdmi_spec_per_pin *per_pin;
1071 struct hdmi_eld *eld;
1072 struct hdmi_spec_per_cvt *per_cvt = NULL;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001073
Stephen Warren384a48d2011-06-01 11:14:21 -06001074 /* Validate hinfo */
1075 pin_idx = hinfo_to_pin_index(spec, hinfo);
1076 if (snd_BUG_ON(pin_idx < 0))
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001077 return -EINVAL;
Stephen Warren384a48d2011-06-01 11:14:21 -06001078 per_pin = &spec->pins[pin_idx];
1079 eld = &per_pin->sink_eld;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001080
Stephen Warren384a48d2011-06-01 11:14:21 -06001081 /* Dynamically assign converter to stream */
1082 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1083 per_cvt = &spec->cvts[cvt_idx];
1084
1085 /* Must not already be assigned */
1086 if (per_cvt->assigned)
1087 continue;
1088 /* Must be in pin's mux's list of converters */
1089 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1090 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1091 break;
1092 /* Not in mux list */
1093 if (mux_idx == per_pin->num_mux_nids)
1094 continue;
1095 break;
1096 }
1097 /* No free converters */
1098 if (cvt_idx == spec->num_cvts)
1099 return -ENODEV;
1100
1101 /* Claim converter */
1102 per_cvt->assigned = 1;
1103 hinfo->nid = per_cvt->cvt_nid;
1104
1105 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1106 AC_VERB_SET_CONNECT_SEL,
1107 mux_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001108 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001109
Stephen Warren2def8172011-06-01 11:14:20 -06001110 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001111 hinfo->channels_min = per_cvt->channels_min;
1112 hinfo->channels_max = per_cvt->channels_max;
1113 hinfo->rates = per_cvt->rates;
1114 hinfo->formats = per_cvt->formats;
1115 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001116
Stephen Warren384a48d2011-06-01 11:14:21 -06001117 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001118 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001119 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001120 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001121 !hinfo->rates || !hinfo->formats) {
1122 per_cvt->assigned = 0;
1123 hinfo->nid = 0;
1124 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001125 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001126 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001127 }
Stephen Warren2def8172011-06-01 11:14:20 -06001128
1129 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001130 runtime->hw.channels_min = hinfo->channels_min;
1131 runtime->hw.channels_max = hinfo->channels_max;
1132 runtime->hw.formats = hinfo->formats;
1133 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001134
1135 snd_pcm_hw_constraint_step(substream->runtime, 0,
1136 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001137 return 0;
1138}
1139
1140/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001141 * HDA/HDMI auto parsing
1142 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001143static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001144{
1145 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001146 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1147 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001148
1149 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1150 snd_printk(KERN_WARNING
1151 "HDMI: pin %d wcaps %#x "
1152 "does not support connection list\n",
1153 pin_nid, get_wcaps(codec, pin_nid));
1154 return -EINVAL;
1155 }
1156
Stephen Warren384a48d2011-06-01 11:14:21 -06001157 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1158 per_pin->mux_nids,
1159 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001160
1161 return 0;
1162}
1163
Wu Fengguangc6e84532011-11-18 16:59:32 -06001164static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001165{
Wu Fengguang744626d2011-11-16 16:29:47 +08001166 struct hda_codec *codec = per_pin->codec;
1167 struct hdmi_eld *eld = &per_pin->sink_eld;
1168 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001169 /*
1170 * Always execute a GetPinSense verb here, even when called from
1171 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1172 * response's PD bit is not the real PD value, but indicates that
1173 * the real PD value changed. An older version of the HD-audio
1174 * specification worked this way. Hence, we just ignore the data in
1175 * the unsolicited response to avoid custom WARs.
1176 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001177 int present = snd_hda_pin_sense(codec, pin_nid);
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001178 bool eld_valid = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001179
Stephen Warren5d44f922011-05-24 17:11:17 -06001180 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1181 if (eld->monitor_present)
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001182 eld_valid = !!(present & AC_PINSENSE_ELDV);
Stephen Warren5d44f922011-05-24 17:11:17 -06001183
Fengguang Wufae3d882012-04-10 17:00:35 +08001184 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001185 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001186 codec->addr, pin_nid, eld->monitor_present, eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001187
David Henningssonbbfd8a12013-02-19 16:11:22 +01001188 eld->eld_valid = false;
Wu Fengguang744626d2011-11-16 16:29:47 +08001189 if (eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001190 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1191 &eld->eld_size) < 0)
1192 eld_valid = false;
1193 else {
1194 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1195 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1196 eld->eld_size) < 0)
1197 eld_valid = false;
1198 }
1199
1200 if (eld_valid) {
1201 snd_hdmi_show_eld(&eld->info);
1202 eld->eld_valid = true;
1203 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001204 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001205 queue_delayed_work(codec->bus->workq,
1206 &per_pin->work,
1207 msecs_to_jiffies(300));
1208 }
1209 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001210}
1211
Wu Fengguang744626d2011-11-16 16:29:47 +08001212static void hdmi_repoll_eld(struct work_struct *work)
1213{
1214 struct hdmi_spec_per_pin *per_pin =
1215 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1216
Wu Fengguangc6e84532011-11-18 16:59:32 -06001217 if (per_pin->repoll_count++ > 6)
1218 per_pin->repoll_count = 0;
1219
1220 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001221}
1222
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001223static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1224 hda_nid_t nid);
1225
Wu Fengguang079d88c2010-03-08 10:44:23 +08001226static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1227{
1228 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001229 unsigned int caps, config;
1230 int pin_idx;
1231 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001232 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001233
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001234 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001235 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1236 return 0;
1237
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001238 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001239 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1240 return 0;
1241
1242 if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
Wu Fengguang3eaead52010-05-14 16:36:15 +08001243 return -E2BIG;
Stephen Warren384a48d2011-06-01 11:14:21 -06001244
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001245 if (codec->vendor_id == 0x80862807)
1246 intel_haswell_fixup_connect_list(codec, pin_nid);
1247
Stephen Warren384a48d2011-06-01 11:14:21 -06001248 pin_idx = spec->num_pins;
1249 per_pin = &spec->pins[pin_idx];
Stephen Warren384a48d2011-06-01 11:14:21 -06001250
1251 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001252 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001253
Stephen Warren384a48d2011-06-01 11:14:21 -06001254 err = hdmi_read_pin_conn(codec, pin_idx);
1255 if (err < 0)
1256 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001257
Wu Fengguang079d88c2010-03-08 10:44:23 +08001258 spec->num_pins++;
1259
Stephen Warren384a48d2011-06-01 11:14:21 -06001260 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001261}
1262
Stephen Warren384a48d2011-06-01 11:14:21 -06001263static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001264{
1265 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001266 int cvt_idx;
1267 struct hdmi_spec_per_cvt *per_cvt;
1268 unsigned int chans;
1269 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001270
David Henningsson116dcde2010-11-23 10:23:40 +01001271 if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
1272 return -E2BIG;
1273
Stephen Warren384a48d2011-06-01 11:14:21 -06001274 chans = get_wcaps(codec, cvt_nid);
1275 chans = get_wcaps_channels(chans);
1276
1277 cvt_idx = spec->num_cvts;
1278 per_cvt = &spec->cvts[cvt_idx];
1279
1280 per_cvt->cvt_nid = cvt_nid;
1281 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001282 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001283 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001284 if (chans > spec->channels_max)
1285 spec->channels_max = chans;
1286 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001287
1288 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1289 &per_cvt->rates,
1290 &per_cvt->formats,
1291 &per_cvt->maxbps);
1292 if (err < 0)
1293 return err;
1294
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001295 spec->cvt_nids[spec->num_cvts++] = cvt_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001296
1297 return 0;
1298}
1299
1300static int hdmi_parse_codec(struct hda_codec *codec)
1301{
1302 hda_nid_t nid;
1303 int i, nodes;
1304
1305 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1306 if (!nid || nodes < 0) {
1307 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1308 return -EINVAL;
1309 }
1310
1311 for (i = 0; i < nodes; i++, nid++) {
1312 unsigned int caps;
1313 unsigned int type;
1314
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001315 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001316 type = get_wcaps_type(caps);
1317
1318 if (!(caps & AC_WCAP_DIGITAL))
1319 continue;
1320
1321 switch (type) {
1322 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001323 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001324 break;
1325 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001326 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001327 break;
1328 }
1329 }
1330
David Henningssonc9adeef2012-11-07 09:22:33 +01001331#ifdef CONFIG_PM
1332 /* We're seeing some problems with unsolicited hot plug events on
1333 * PantherPoint after S3, if this is not enabled */
1334 if (codec->vendor_id == 0x80862806)
1335 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001336 /*
1337 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1338 * can be lost and presence sense verb will become inaccurate if the
1339 * HDA link is powered off at hot plug or hw initialization time.
1340 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001341 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001342 AC_PWRST_EPSS))
1343 codec->bus->power_keep_link_on = 1;
1344#endif
1345
1346 return 0;
1347}
1348
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001349/*
1350 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001351static char *get_hdmi_pcm_name(int idx)
1352{
1353 static char names[MAX_HDMI_PINS][8];
1354 sprintf(&names[idx][0], "HDMI %d", idx);
1355 return &names[idx][0];
1356}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001357
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001358static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1359{
1360 struct hda_spdif_out *spdif;
1361 bool non_pcm;
1362
1363 mutex_lock(&codec->spdif_mutex);
1364 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1365 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1366 mutex_unlock(&codec->spdif_mutex);
1367 return non_pcm;
1368}
1369
1370
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001371/*
1372 * HDMI callbacks
1373 */
1374
1375static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1376 struct hda_codec *codec,
1377 unsigned int stream_tag,
1378 unsigned int format,
1379 struct snd_pcm_substream *substream)
1380{
Stephen Warren384a48d2011-06-01 11:14:21 -06001381 hda_nid_t cvt_nid = hinfo->nid;
1382 struct hdmi_spec *spec = codec->spec;
1383 int pin_idx = hinfo_to_pin_index(spec, hinfo);
1384 hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001385 bool non_pcm;
1386
1387 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001388
Stephen Warren384a48d2011-06-01 11:14:21 -06001389 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001390
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001391 hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
Stephen Warren384a48d2011-06-01 11:14:21 -06001392
1393 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001394}
1395
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001396static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1397 struct hda_codec *codec,
1398 struct snd_pcm_substream *substream)
1399{
1400 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1401 return 0;
1402}
1403
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001404static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1405 struct hda_codec *codec,
1406 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001407{
1408 struct hdmi_spec *spec = codec->spec;
1409 int cvt_idx, pin_idx;
1410 struct hdmi_spec_per_cvt *per_cvt;
1411 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001412
Stephen Warren384a48d2011-06-01 11:14:21 -06001413 if (hinfo->nid) {
1414 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1415 if (snd_BUG_ON(cvt_idx < 0))
1416 return -EINVAL;
1417 per_cvt = &spec->cvts[cvt_idx];
1418
1419 snd_BUG_ON(!per_cvt->assigned);
1420 per_cvt->assigned = 0;
1421 hinfo->nid = 0;
1422
1423 pin_idx = hinfo_to_pin_index(spec, hinfo);
1424 if (snd_BUG_ON(pin_idx < 0))
1425 return -EINVAL;
1426 per_pin = &spec->pins[pin_idx];
1427
Stephen Warren384a48d2011-06-01 11:14:21 -06001428 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001429 per_pin->chmap_set = false;
1430 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Stephen Warren384a48d2011-06-01 11:14:21 -06001431 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001432
Stephen Warren384a48d2011-06-01 11:14:21 -06001433 return 0;
1434}
1435
1436static const struct hda_pcm_ops generic_ops = {
1437 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001438 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001439 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001440 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001441};
1442
Takashi Iwaid45e6882012-07-31 11:36:00 +02001443/*
1444 * ALSA API channel-map control callbacks
1445 */
1446static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1447 struct snd_ctl_elem_info *uinfo)
1448{
1449 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1450 struct hda_codec *codec = info->private_data;
1451 struct hdmi_spec *spec = codec->spec;
1452 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1453 uinfo->count = spec->channels_max;
1454 uinfo->value.integer.min = 0;
1455 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1456 return 0;
1457}
1458
1459static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1460 unsigned int size, unsigned int __user *tlv)
1461{
1462 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1463 struct hda_codec *codec = info->private_data;
1464 struct hdmi_spec *spec = codec->spec;
1465 const unsigned int valid_mask =
1466 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1467 unsigned int __user *dst;
1468 int chs, count = 0;
1469
1470 if (size < 8)
1471 return -ENOMEM;
1472 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1473 return -EFAULT;
1474 size -= 8;
1475 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001476 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001477 int i, c;
1478 struct cea_channel_speaker_allocation *cap;
1479 cap = channel_allocations;
1480 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1481 int chs_bytes = chs * 4;
1482 if (cap->channels != chs)
1483 continue;
1484 if (cap->spk_mask & ~valid_mask)
1485 continue;
1486 if (size < 8)
1487 return -ENOMEM;
1488 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1489 put_user(chs_bytes, dst + 1))
1490 return -EFAULT;
1491 dst += 2;
1492 size -= 8;
1493 count += 8;
1494 if (size < chs_bytes)
1495 return -ENOMEM;
1496 size -= chs_bytes;
1497 count += chs_bytes;
1498 for (c = 7; c >= 0; c--) {
1499 int spk = cap->speakers[c];
1500 if (!spk)
1501 continue;
1502 if (put_user(spk_to_chmap(spk), dst))
1503 return -EFAULT;
1504 dst++;
1505 }
1506 }
1507 }
1508 if (put_user(count, tlv + 1))
1509 return -EFAULT;
1510 return 0;
1511}
1512
1513static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1514 struct snd_ctl_elem_value *ucontrol)
1515{
1516 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1517 struct hda_codec *codec = info->private_data;
1518 struct hdmi_spec *spec = codec->spec;
1519 int pin_idx = kcontrol->private_value;
1520 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1521 int i;
1522
1523 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1524 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1525 return 0;
1526}
1527
1528static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1529 struct snd_ctl_elem_value *ucontrol)
1530{
1531 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1532 struct hda_codec *codec = info->private_data;
1533 struct hdmi_spec *spec = codec->spec;
1534 int pin_idx = kcontrol->private_value;
1535 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1536 unsigned int ctl_idx;
1537 struct snd_pcm_substream *substream;
1538 unsigned char chmap[8];
1539 int i, ca, prepared = 0;
1540
1541 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1542 substream = snd_pcm_chmap_substream(info, ctl_idx);
1543 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001544 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001545 switch (substream->runtime->status->state) {
1546 case SNDRV_PCM_STATE_OPEN:
1547 case SNDRV_PCM_STATE_SETUP:
1548 break;
1549 case SNDRV_PCM_STATE_PREPARED:
1550 prepared = 1;
1551 break;
1552 default:
1553 return -EBUSY;
1554 }
1555 memset(chmap, 0, sizeof(chmap));
1556 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1557 chmap[i] = ucontrol->value.integer.value[i];
1558 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1559 return 0;
1560 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1561 if (ca < 0)
1562 return -EINVAL;
1563 per_pin->chmap_set = true;
1564 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1565 if (prepared)
1566 hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
1567 substream);
1568
1569 return 0;
1570}
1571
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001572static int generic_hdmi_build_pcms(struct hda_codec *codec)
1573{
1574 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001575 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001576
Stephen Warren384a48d2011-06-01 11:14:21 -06001577 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1578 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001579 struct hda_pcm_stream *pstr;
1580
Stephen Warren384a48d2011-06-01 11:14:21 -06001581 info = &spec->pcm_rec[pin_idx];
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001582 info->name = get_hdmi_pcm_name(pin_idx);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001583 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001584 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001585
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001586 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001587 pstr->substreams = 1;
1588 pstr->ops = generic_ops;
1589 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001590 }
1591
Stephen Warren384a48d2011-06-01 11:14:21 -06001592 codec->num_pcms = spec->num_pins;
1593 codec->pcm_info = spec->pcm_rec;
1594
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001595 return 0;
1596}
1597
David Henningsson0b6c49b2011-08-23 16:56:03 +02001598static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1599{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001600 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001601 struct hdmi_spec *spec = codec->spec;
1602 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1603 int pcmdev = spec->pcm_rec[pin_idx].device;
1604
Takashi Iwai31ef2252011-12-01 17:41:36 +01001605 if (pcmdev > 0)
1606 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001607
Takashi Iwai31ef2252011-12-01 17:41:36 +01001608 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001609}
1610
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001611static int generic_hdmi_build_controls(struct hda_codec *codec)
1612{
1613 struct hdmi_spec *spec = codec->spec;
1614 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001615 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001616
Stephen Warren384a48d2011-06-01 11:14:21 -06001617 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1618 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
David Henningsson0b6c49b2011-08-23 16:56:03 +02001619
1620 err = generic_hdmi_build_jack(codec, pin_idx);
1621 if (err < 0)
1622 return err;
1623
Takashi Iwaidcda5802012-10-12 17:24:51 +02001624 err = snd_hda_create_dig_out_ctls(codec,
1625 per_pin->pin_nid,
1626 per_pin->mux_nids[0],
1627 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001628 if (err < 0)
1629 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001630 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001631
1632 /* add control for ELD Bytes */
1633 err = hdmi_create_eld_ctl(codec,
1634 pin_idx,
1635 spec->pcm_rec[pin_idx].device);
1636
1637 if (err < 0)
1638 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001639
Takashi Iwai82b1d732011-12-20 15:53:07 +01001640 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001641 }
1642
Takashi Iwaid45e6882012-07-31 11:36:00 +02001643 /* add channel maps */
1644 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1645 struct snd_pcm_chmap *chmap;
1646 struct snd_kcontrol *kctl;
1647 int i;
1648 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1649 SNDRV_PCM_STREAM_PLAYBACK,
1650 NULL, 0, pin_idx, &chmap);
1651 if (err < 0)
1652 return err;
1653 /* override handlers */
1654 chmap->private_data = codec;
1655 kctl = chmap->kctl;
1656 for (i = 0; i < kctl->count; i++)
1657 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1658 kctl->info = hdmi_chmap_ctl_info;
1659 kctl->get = hdmi_chmap_ctl_get;
1660 kctl->put = hdmi_chmap_ctl_put;
1661 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1662 }
1663
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001664 return 0;
1665}
1666
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001667static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1668{
1669 struct hdmi_spec *spec = codec->spec;
1670 int pin_idx;
1671
1672 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1673 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1674 struct hdmi_eld *eld = &per_pin->sink_eld;
1675
1676 per_pin->codec = codec;
1677 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1678 snd_hda_eld_proc_new(codec, eld, pin_idx);
1679 }
1680 return 0;
1681}
1682
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001683static int generic_hdmi_init(struct hda_codec *codec)
1684{
1685 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001686 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001687
Stephen Warren384a48d2011-06-01 11:14:21 -06001688 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1689 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1690 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001691
1692 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001693 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001694 }
1695 return 0;
1696}
1697
1698static void generic_hdmi_free(struct hda_codec *codec)
1699{
1700 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001701 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001702
Stephen Warren384a48d2011-06-01 11:14:21 -06001703 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1704 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1705 struct hdmi_eld *eld = &per_pin->sink_eld;
1706
Wu Fengguang744626d2011-11-16 16:29:47 +08001707 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001708 snd_hda_eld_proc_free(codec, eld);
1709 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001710
Wu Fengguang744626d2011-11-16 16:29:47 +08001711 flush_workqueue(codec->bus->workq);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001712 kfree(spec);
1713}
1714
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001715static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001716 .init = generic_hdmi_init,
1717 .free = generic_hdmi_free,
1718 .build_pcms = generic_hdmi_build_pcms,
1719 .build_controls = generic_hdmi_build_controls,
1720 .unsol_event = hdmi_unsol_event,
1721};
1722
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001723
1724static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1725 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001726{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001727 struct hdmi_spec *spec = codec->spec;
1728 hda_nid_t conns[4];
1729 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001730
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001731 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1732 if (nconns == spec->num_cvts &&
1733 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001734 return;
1735
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001736 /* override pins connection list */
1737 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1738 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001739}
1740
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001741#define INTEL_VENDOR_NID 0x08
1742#define INTEL_GET_VENDOR_VERB 0xf81
1743#define INTEL_SET_VENDOR_VERB 0x781
1744#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1745#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1746
1747static void intel_haswell_enable_all_pins(struct hda_codec *codec,
1748 const struct hda_fixup *fix, int action)
1749{
1750 unsigned int vendor_param;
1751
1752 if (action != HDA_FIXUP_ACT_PRE_PROBE)
1753 return;
1754 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1755 INTEL_GET_VENDOR_VERB, 0);
1756 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1757 return;
1758
1759 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1760 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1761 INTEL_SET_VENDOR_VERB, vendor_param);
1762 if (vendor_param == -1)
1763 return;
1764
1765 snd_hda_codec_update_widgets(codec);
1766 return;
1767}
1768
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001769static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1770{
1771 unsigned int vendor_param;
1772
1773 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1774 INTEL_GET_VENDOR_VERB, 0);
1775 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1776 return;
1777
1778 /* enable DP1.2 mode */
1779 vendor_param |= INTEL_EN_DP12;
1780 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1781 INTEL_SET_VENDOR_VERB, vendor_param);
1782}
1783
1784
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001785
1786/* available models for fixup */
1787enum {
1788 INTEL_HASWELL,
1789};
1790
1791static const struct hda_model_fixup hdmi_models[] = {
1792 {.id = INTEL_HASWELL, .name = "Haswell"},
1793 {}
1794};
1795
1796static const struct snd_pci_quirk hdmi_fixup_tbl[] = {
1797 SND_PCI_QUIRK(0x8086, 0x2010, "Haswell", INTEL_HASWELL),
1798 {} /* terminator */
1799};
1800
1801static const struct hda_fixup hdmi_fixups[] = {
1802 [INTEL_HASWELL] = {
1803 .type = HDA_FIXUP_FUNC,
1804 .v.func = intel_haswell_enable_all_pins,
1805 },
1806};
1807
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001808
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001809static int patch_generic_hdmi(struct hda_codec *codec)
1810{
1811 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001812
1813 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1814 if (spec == NULL)
1815 return -ENOMEM;
1816
1817 codec->spec = spec;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001818
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001819 snd_hda_pick_fixup(codec, hdmi_models, hdmi_fixup_tbl, hdmi_fixups);
1820 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1821
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001822 if (codec->vendor_id == 0x80862807)
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001823 intel_haswell_fixup_enable_dp12(codec);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001824
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001825 if (hdmi_parse_codec(codec) < 0) {
1826 codec->spec = NULL;
1827 kfree(spec);
1828 return -EINVAL;
1829 }
1830 codec->patch_ops = generic_hdmi_patch_ops;
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001831 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001832
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001833 init_channel_allocations();
1834
1835 return 0;
1836}
1837
1838/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06001839 * Shared non-generic implementations
1840 */
1841
1842static int simple_playback_build_pcms(struct hda_codec *codec)
1843{
1844 struct hdmi_spec *spec = codec->spec;
1845 struct hda_pcm *info = spec->pcm_rec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001846 unsigned int chans;
1847 struct hda_pcm_stream *pstr;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001848
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001849 codec->num_pcms = 1;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001850 codec->pcm_info = info;
1851
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001852 chans = get_wcaps(codec, spec->cvts[0].cvt_nid);
1853 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001854
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001855 info->name = get_hdmi_pcm_name(0);
1856 info->pcm_type = HDA_PCM_TYPE_HDMI;
1857 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1858 *pstr = spec->pcm_playback;
1859 pstr->nid = spec->cvts[0].cvt_nid;
1860 if (pstr->channels_max <= 2 && chans && chans <= 16)
1861 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001862
1863 return 0;
1864}
1865
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001866/* unsolicited event for jack sensing */
1867static void simple_hdmi_unsol_event(struct hda_codec *codec,
1868 unsigned int res)
1869{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02001870 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001871 snd_hda_jack_report_sync(codec);
1872}
1873
1874/* generic_hdmi_build_jack can be used for simple_hdmi, too,
1875 * as long as spec->pins[] is set correctly
1876 */
1877#define simple_hdmi_build_jack generic_hdmi_build_jack
1878
Stephen Warren3aaf8982011-06-01 11:14:19 -06001879static int simple_playback_build_controls(struct hda_codec *codec)
1880{
1881 struct hdmi_spec *spec = codec->spec;
1882 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001883
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001884 err = snd_hda_create_spdif_out_ctls(codec,
1885 spec->cvts[0].cvt_nid,
1886 spec->cvts[0].cvt_nid);
1887 if (err < 0)
1888 return err;
1889 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001890}
1891
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001892static int simple_playback_init(struct hda_codec *codec)
1893{
1894 struct hdmi_spec *spec = codec->spec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001895 hda_nid_t pin = spec->pins[0].pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001896
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001897 snd_hda_codec_write(codec, pin, 0,
1898 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1899 /* some codecs require to unmute the pin */
1900 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
1901 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1902 AMP_OUT_UNMUTE);
1903 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001904 return 0;
1905}
1906
Stephen Warren3aaf8982011-06-01 11:14:19 -06001907static void simple_playback_free(struct hda_codec *codec)
1908{
1909 struct hdmi_spec *spec = codec->spec;
1910
1911 kfree(spec);
1912}
1913
1914/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001915 * Nvidia specific implementations
1916 */
1917
1918#define Nv_VERB_SET_Channel_Allocation 0xF79
1919#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
1920#define Nv_VERB_SET_Audio_Protection_On 0xF98
1921#define Nv_VERB_SET_Audio_Protection_Off 0xF99
1922
1923#define nvhdmi_master_con_nid_7x 0x04
1924#define nvhdmi_master_pin_nid_7x 0x05
1925
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001926static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001927 /*front, rear, clfe, rear_surr */
1928 0x6, 0x8, 0xa, 0xc,
1929};
1930
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001931static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
1932 /* set audio protect on */
1933 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1934 /* enable digital output on pin widget */
1935 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1936 {} /* terminator */
1937};
1938
1939static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001940 /* set audio protect on */
1941 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1942 /* enable digital output on pin widget */
1943 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1944 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1945 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1946 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1947 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1948 {} /* terminator */
1949};
1950
1951#ifdef LIMITED_RATE_FMT_SUPPORT
1952/* support only the safe format and rate */
1953#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
1954#define SUPPORTED_MAXBPS 16
1955#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1956#else
1957/* support all rates and formats */
1958#define SUPPORTED_RATES \
1959 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1960 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
1961 SNDRV_PCM_RATE_192000)
1962#define SUPPORTED_MAXBPS 24
1963#define SUPPORTED_FORMATS \
1964 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1965#endif
1966
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001967static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001968{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001969 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
1970 return 0;
1971}
1972
1973static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
1974{
1975 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001976 return 0;
1977}
1978
Nitin Daga393004b2011-01-10 21:49:31 +05301979static unsigned int channels_2_6_8[] = {
1980 2, 6, 8
1981};
1982
1983static unsigned int channels_2_8[] = {
1984 2, 8
1985};
1986
1987static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
1988 .count = ARRAY_SIZE(channels_2_6_8),
1989 .list = channels_2_6_8,
1990 .mask = 0,
1991};
1992
1993static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
1994 .count = ARRAY_SIZE(channels_2_8),
1995 .list = channels_2_8,
1996 .mask = 0,
1997};
1998
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001999static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2000 struct hda_codec *codec,
2001 struct snd_pcm_substream *substream)
2002{
2003 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302004 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2005
2006 switch (codec->preset->id) {
2007 case 0x10de0002:
2008 case 0x10de0003:
2009 case 0x10de0005:
2010 case 0x10de0006:
2011 hw_constraints_channels = &hw_constraints_2_8_channels;
2012 break;
2013 case 0x10de0007:
2014 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2015 break;
2016 default:
2017 break;
2018 }
2019
2020 if (hw_constraints_channels != NULL) {
2021 snd_pcm_hw_constraint_list(substream->runtime, 0,
2022 SNDRV_PCM_HW_PARAM_CHANNELS,
2023 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002024 } else {
2025 snd_pcm_hw_constraint_step(substream->runtime, 0,
2026 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302027 }
2028
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002029 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2030}
2031
2032static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2033 struct hda_codec *codec,
2034 struct snd_pcm_substream *substream)
2035{
2036 struct hdmi_spec *spec = codec->spec;
2037 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2038}
2039
2040static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2041 struct hda_codec *codec,
2042 unsigned int stream_tag,
2043 unsigned int format,
2044 struct snd_pcm_substream *substream)
2045{
2046 struct hdmi_spec *spec = codec->spec;
2047 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2048 stream_tag, format, substream);
2049}
2050
Takashi Iwaid0b12522012-06-15 14:34:42 +02002051static const struct hda_pcm_stream simple_pcm_playback = {
2052 .substreams = 1,
2053 .channels_min = 2,
2054 .channels_max = 2,
2055 .ops = {
2056 .open = simple_playback_pcm_open,
2057 .close = simple_playback_pcm_close,
2058 .prepare = simple_playback_pcm_prepare
2059 },
2060};
2061
2062static const struct hda_codec_ops simple_hdmi_patch_ops = {
2063 .build_controls = simple_playback_build_controls,
2064 .build_pcms = simple_playback_build_pcms,
2065 .init = simple_playback_init,
2066 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002067 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002068};
2069
2070static int patch_simple_hdmi(struct hda_codec *codec,
2071 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2072{
2073 struct hdmi_spec *spec;
2074
2075 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2076 if (!spec)
2077 return -ENOMEM;
2078
2079 codec->spec = spec;
2080
2081 spec->multiout.num_dacs = 0; /* no analog */
2082 spec->multiout.max_channels = 2;
2083 spec->multiout.dig_out_nid = cvt_nid;
2084 spec->num_cvts = 1;
2085 spec->num_pins = 1;
2086 spec->cvts[0].cvt_nid = cvt_nid;
Takashi Iwai21cd6832012-06-20 09:19:32 +02002087 spec->pins[0].pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002088 spec->pcm_playback = simple_pcm_playback;
2089
2090 codec->patch_ops = simple_hdmi_patch_ops;
2091
2092 return 0;
2093}
2094
Aaron Plattner1f348522011-04-06 17:19:04 -07002095static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2096 int channels)
2097{
2098 unsigned int chanmask;
2099 int chan = channels ? (channels - 1) : 1;
2100
2101 switch (channels) {
2102 default:
2103 case 0:
2104 case 2:
2105 chanmask = 0x00;
2106 break;
2107 case 4:
2108 chanmask = 0x08;
2109 break;
2110 case 6:
2111 chanmask = 0x0b;
2112 break;
2113 case 8:
2114 chanmask = 0x13;
2115 break;
2116 }
2117
2118 /* Set the audio infoframe channel allocation and checksum fields. The
2119 * channel count is computed implicitly by the hardware. */
2120 snd_hda_codec_write(codec, 0x1, 0,
2121 Nv_VERB_SET_Channel_Allocation, chanmask);
2122
2123 snd_hda_codec_write(codec, 0x1, 0,
2124 Nv_VERB_SET_Info_Frame_Checksum,
2125 (0x71 - chan - chanmask));
2126}
2127
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002128static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2129 struct hda_codec *codec,
2130 struct snd_pcm_substream *substream)
2131{
2132 struct hdmi_spec *spec = codec->spec;
2133 int i;
2134
2135 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2136 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2137 for (i = 0; i < 4; i++) {
2138 /* set the stream id */
2139 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2140 AC_VERB_SET_CHANNEL_STREAMID, 0);
2141 /* set the stream format */
2142 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2143 AC_VERB_SET_STREAM_FORMAT, 0);
2144 }
2145
Aaron Plattner1f348522011-04-06 17:19:04 -07002146 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2147 * streams are disabled. */
2148 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2149
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002150 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2151}
2152
2153static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2154 struct hda_codec *codec,
2155 unsigned int stream_tag,
2156 unsigned int format,
2157 struct snd_pcm_substream *substream)
2158{
2159 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002160 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002161 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002162 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002163 struct hda_spdif_out *spdif;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002164
2165 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002166 spdif = snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002167
2168 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002169
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002170 dataDCC2 = 0x2;
2171
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002172 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002173 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002174 snd_hda_codec_write(codec,
2175 nvhdmi_master_con_nid_7x,
2176 0,
2177 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002178 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002179
2180 /* set the stream id */
2181 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2182 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2183
2184 /* set the stream format */
2185 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2186 AC_VERB_SET_STREAM_FORMAT, format);
2187
2188 /* turn on again (if needed) */
2189 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002190 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002191 snd_hda_codec_write(codec,
2192 nvhdmi_master_con_nid_7x,
2193 0,
2194 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002195 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002196 snd_hda_codec_write(codec,
2197 nvhdmi_master_con_nid_7x,
2198 0,
2199 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2200 }
2201
2202 for (i = 0; i < 4; i++) {
2203 if (chs == 2)
2204 channel_id = 0;
2205 else
2206 channel_id = i * 2;
2207
2208 /* turn off SPDIF once;
2209 *otherwise the IEC958 bits won't be updated
2210 */
2211 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002212 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002213 snd_hda_codec_write(codec,
2214 nvhdmi_con_nids_7x[i],
2215 0,
2216 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002217 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002218 /* set the stream id */
2219 snd_hda_codec_write(codec,
2220 nvhdmi_con_nids_7x[i],
2221 0,
2222 AC_VERB_SET_CHANNEL_STREAMID,
2223 (stream_tag << 4) | channel_id);
2224 /* set the stream format */
2225 snd_hda_codec_write(codec,
2226 nvhdmi_con_nids_7x[i],
2227 0,
2228 AC_VERB_SET_STREAM_FORMAT,
2229 format);
2230 /* turn on again (if needed) */
2231 /* enable and set the channel status audio/data flag */
2232 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002233 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002234 snd_hda_codec_write(codec,
2235 nvhdmi_con_nids_7x[i],
2236 0,
2237 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002238 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002239 snd_hda_codec_write(codec,
2240 nvhdmi_con_nids_7x[i],
2241 0,
2242 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2243 }
2244 }
2245
Aaron Plattner1f348522011-04-06 17:19:04 -07002246 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002247
2248 mutex_unlock(&codec->spdif_mutex);
2249 return 0;
2250}
2251
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002252static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002253 .substreams = 1,
2254 .channels_min = 2,
2255 .channels_max = 8,
2256 .nid = nvhdmi_master_con_nid_7x,
2257 .rates = SUPPORTED_RATES,
2258 .maxbps = SUPPORTED_MAXBPS,
2259 .formats = SUPPORTED_FORMATS,
2260 .ops = {
2261 .open = simple_playback_pcm_open,
2262 .close = nvhdmi_8ch_7x_pcm_close,
2263 .prepare = nvhdmi_8ch_7x_pcm_prepare
2264 },
2265};
2266
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002267static int patch_nvhdmi_2ch(struct hda_codec *codec)
2268{
2269 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002270 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2271 nvhdmi_master_pin_nid_7x);
2272 if (err < 0)
2273 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002274
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002275 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002276 /* override the PCM rates, etc, as the codec doesn't give full list */
2277 spec = codec->spec;
2278 spec->pcm_playback.rates = SUPPORTED_RATES;
2279 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2280 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002281 return 0;
2282}
2283
Takashi Iwai53775b02012-08-01 12:17:41 +02002284static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2285{
2286 struct hdmi_spec *spec = codec->spec;
2287 int err = simple_playback_build_pcms(codec);
2288 spec->pcm_rec[0].own_chmap = true;
2289 return err;
2290}
2291
2292static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2293{
2294 struct hdmi_spec *spec = codec->spec;
2295 struct snd_pcm_chmap *chmap;
2296 int err;
2297
2298 err = simple_playback_build_controls(codec);
2299 if (err < 0)
2300 return err;
2301
2302 /* add channel maps */
2303 err = snd_pcm_add_chmap_ctls(spec->pcm_rec[0].pcm,
2304 SNDRV_PCM_STREAM_PLAYBACK,
2305 snd_pcm_alt_chmaps, 8, 0, &chmap);
2306 if (err < 0)
2307 return err;
2308 switch (codec->preset->id) {
2309 case 0x10de0002:
2310 case 0x10de0003:
2311 case 0x10de0005:
2312 case 0x10de0006:
2313 chmap->channel_mask = (1U << 2) | (1U << 8);
2314 break;
2315 case 0x10de0007:
2316 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2317 }
2318 return 0;
2319}
2320
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002321static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2322{
2323 struct hdmi_spec *spec;
2324 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002325 if (err < 0)
2326 return err;
2327 spec = codec->spec;
2328 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002329 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002330 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002331 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2332 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002333
2334 /* Initialize the audio infoframe channel mask and checksum to something
2335 * valid */
2336 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2337
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002338 return 0;
2339}
2340
2341/*
2342 * ATI-specific implementations
2343 *
2344 * FIXME: we may omit the whole this and use the generic code once after
2345 * it's confirmed to work.
2346 */
2347
2348#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2349#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2350
2351static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2352 struct hda_codec *codec,
2353 unsigned int stream_tag,
2354 unsigned int format,
2355 struct snd_pcm_substream *substream)
2356{
2357 struct hdmi_spec *spec = codec->spec;
2358 int chans = substream->runtime->channels;
2359 int i, err;
2360
2361 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2362 substream);
2363 if (err < 0)
2364 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002365 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
2366 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002367 /* FIXME: XXX */
2368 for (i = 0; i < chans; i++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002369 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002370 AC_VERB_SET_HDMI_CHAN_SLOT,
2371 (i << 4) | i);
2372 }
2373 return 0;
2374}
2375
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002376static int patch_atihdmi(struct hda_codec *codec)
2377{
2378 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002379 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2380 if (err < 0)
2381 return err;
2382 spec = codec->spec;
2383 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002384 return 0;
2385}
2386
Annie Liu3de5ff82012-06-08 19:18:42 +08002387/* VIA HDMI Implementation */
2388#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2389#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2390
Annie Liu3de5ff82012-06-08 19:18:42 +08002391static int patch_via_hdmi(struct hda_codec *codec)
2392{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002393 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002394}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002395
2396/*
2397 * patch entries
2398 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002399static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002400{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2401{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2402{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002403{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002404{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2405{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2406{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2407{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2408{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2409{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2410{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2411{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002412{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2413{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2414{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2415{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2416{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2417{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2418{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2419{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2420{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2421{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2422{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002423/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002424{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2425{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2426{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2427{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2428{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2429{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2430{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2431{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2432{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2433{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002434{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002435{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2436{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002437{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2438{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2439{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2440{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002441{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2442{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2443{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2444{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2445{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2446{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002447{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002448{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002449{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002450{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2451{} /* terminator */
2452};
2453
2454MODULE_ALIAS("snd-hda-codec-id:1002793c");
2455MODULE_ALIAS("snd-hda-codec-id:10027919");
2456MODULE_ALIAS("snd-hda-codec-id:1002791a");
2457MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2458MODULE_ALIAS("snd-hda-codec-id:10951390");
2459MODULE_ALIAS("snd-hda-codec-id:10951392");
2460MODULE_ALIAS("snd-hda-codec-id:10de0002");
2461MODULE_ALIAS("snd-hda-codec-id:10de0003");
2462MODULE_ALIAS("snd-hda-codec-id:10de0005");
2463MODULE_ALIAS("snd-hda-codec-id:10de0006");
2464MODULE_ALIAS("snd-hda-codec-id:10de0007");
2465MODULE_ALIAS("snd-hda-codec-id:10de000a");
2466MODULE_ALIAS("snd-hda-codec-id:10de000b");
2467MODULE_ALIAS("snd-hda-codec-id:10de000c");
2468MODULE_ALIAS("snd-hda-codec-id:10de000d");
2469MODULE_ALIAS("snd-hda-codec-id:10de0010");
2470MODULE_ALIAS("snd-hda-codec-id:10de0011");
2471MODULE_ALIAS("snd-hda-codec-id:10de0012");
2472MODULE_ALIAS("snd-hda-codec-id:10de0013");
2473MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002474MODULE_ALIAS("snd-hda-codec-id:10de0015");
2475MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002476MODULE_ALIAS("snd-hda-codec-id:10de0018");
2477MODULE_ALIAS("snd-hda-codec-id:10de0019");
2478MODULE_ALIAS("snd-hda-codec-id:10de001a");
2479MODULE_ALIAS("snd-hda-codec-id:10de001b");
2480MODULE_ALIAS("snd-hda-codec-id:10de001c");
2481MODULE_ALIAS("snd-hda-codec-id:10de0040");
2482MODULE_ALIAS("snd-hda-codec-id:10de0041");
2483MODULE_ALIAS("snd-hda-codec-id:10de0042");
2484MODULE_ALIAS("snd-hda-codec-id:10de0043");
2485MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002486MODULE_ALIAS("snd-hda-codec-id:10de0051");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002487MODULE_ALIAS("snd-hda-codec-id:10de0067");
2488MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002489MODULE_ALIAS("snd-hda-codec-id:11069f80");
2490MODULE_ALIAS("snd-hda-codec-id:11069f81");
2491MODULE_ALIAS("snd-hda-codec-id:11069f84");
2492MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002493MODULE_ALIAS("snd-hda-codec-id:17e80047");
2494MODULE_ALIAS("snd-hda-codec-id:80860054");
2495MODULE_ALIAS("snd-hda-codec-id:80862801");
2496MODULE_ALIAS("snd-hda-codec-id:80862802");
2497MODULE_ALIAS("snd-hda-codec-id:80862803");
2498MODULE_ALIAS("snd-hda-codec-id:80862804");
2499MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002500MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002501MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002502MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002503MODULE_ALIAS("snd-hda-codec-id:808629fb");
2504
2505MODULE_LICENSE("GPL");
2506MODULE_DESCRIPTION("HDMI HD-audio codec");
2507MODULE_ALIAS("snd-hda-codec-intelhdmi");
2508MODULE_ALIAS("snd-hda-codec-nvhdmi");
2509MODULE_ALIAS("snd-hda-codec-atihdmi");
2510
2511static struct hda_codec_preset_list intel_list = {
2512 .preset = snd_hda_preset_hdmi,
2513 .owner = THIS_MODULE,
2514};
2515
2516static int __init patch_hdmi_init(void)
2517{
2518 return snd_hda_add_codec_preset(&intel_list);
2519}
2520
2521static void __exit patch_hdmi_exit(void)
2522{
2523 snd_hda_delete_codec_preset(&intel_list);
2524}
2525
2526module_init(patch_hdmi_init)
2527module_exit(patch_hdmi_exit)