]> git.ipfire.org Git - people/ms/linux.git/blame - sound/pci/hda/patch_hdmi.c
ALSA: hda - Use the primary DAC for all aamix outputs
[people/ms/linux.git] / sound / pci / hda / patch_hdmi.c
CommitLineData
079d88cc
WF
1/*
2 *
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
4 *
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
84eb01be
TI
6 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
079d88cc
WF
9 *
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
84eb01be
TI
31#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
65a77217 34#include <linux/module.h>
84eb01be 35#include <sound/core.h>
07acecc1 36#include <sound/jack.h>
433968da 37#include <sound/asoundef.h>
d45e6889 38#include <sound/tlv.h>
84eb01be
TI
39#include "hda_codec.h"
40#include "hda_local.h"
1835a0f9 41#include "hda_jack.h"
84eb01be 42
0ebaa24c
TI
43static 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
384a48d7
SW
47struct hdmi_spec_per_cvt {
48 hda_nid_t cvt_nid;
49 int assigned;
50 unsigned int channels_min;
51 unsigned int channels_max;
52 u32 rates;
53 u64 formats;
54 unsigned int maxbps;
55};
079d88cc 56
4eea3091
TI
57/* max. connections to a widget */
58#define HDA_MAX_CONNECTIONS 32
59
384a48d7
SW
60struct hdmi_spec_per_pin {
61 hda_nid_t pin_nid;
62 int num_mux_nids;
63 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
744626da
WF
64
65 struct hda_codec *codec;
384a48d7 66 struct hdmi_eld sink_eld;
744626da 67 struct delayed_work work;
92c69e79 68 struct snd_kcontrol *eld_ctl;
c6e8453e 69 int repoll_count;
1a6003b5 70 bool non_pcm;
d45e6889
TI
71 bool chmap_set; /* channel-map override by ALSA API? */
72 unsigned char chmap[8]; /* ALSA API channel-map */
bce0d2a8 73 char pcm_name[8]; /* filled in build_pcm callbacks */
384a48d7 74};
079d88cc 75
384a48d7
SW
76struct hdmi_spec {
77 int num_cvts;
bce0d2a8
TI
78 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
79 hda_nid_t cvt_nids[4]; /* only for haswell fix */
079d88cc 80
384a48d7 81 int num_pins;
bce0d2a8
TI
82 struct snd_array pins; /* struct hdmi_spec_per_pin */
83 struct snd_array pcm_rec; /* struct hda_pcm */
d45e6889 84 unsigned int channels_max; /* max over all cvts */
079d88cc 85
4bd038f9 86 struct hdmi_eld temp_eld;
079d88cc 87 /*
384a48d7 88 * Non-generic ATI/NVIDIA specific
079d88cc
WF
89 */
90 struct hda_multi_out multiout;
d0b1252d 91 struct hda_pcm_stream pcm_playback;
079d88cc
WF
92};
93
94
95struct hdmi_audio_infoframe {
96 u8 type; /* 0x84 */
97 u8 ver; /* 0x01 */
98 u8 len; /* 0x0a */
99
53d7d69d
WF
100 u8 checksum;
101
079d88cc
WF
102 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
103 u8 SS01_SF24;
104 u8 CXT04;
105 u8 CA;
106 u8 LFEPBL01_LSV36_DM_INH7;
53d7d69d
WF
107};
108
109struct dp_audio_infoframe {
110 u8 type; /* 0x84 */
111 u8 len; /* 0x1b */
112 u8 ver; /* 0x11 << 2 */
113
114 u8 CC02_CT47; /* match with HDMI infoframe from this on */
115 u8 SS01_SF24;
116 u8 CXT04;
117 u8 CA;
118 u8 LFEPBL01_LSV36_DM_INH7;
079d88cc
WF
119};
120
2b203dbb
TI
121union audio_infoframe {
122 struct hdmi_audio_infoframe hdmi;
123 struct dp_audio_infoframe dp;
124 u8 bytes[0];
125};
126
079d88cc
WF
127/*
128 * CEA speaker placement:
129 *
130 * FLH FCH FRH
131 * FLW FL FLC FC FRC FR FRW
132 *
133 * LFE
134 * TC
135 *
136 * RL RLC RC RRC RR
137 *
138 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
139 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
140 */
141enum cea_speaker_placement {
142 FL = (1 << 0), /* Front Left */
143 FC = (1 << 1), /* Front Center */
144 FR = (1 << 2), /* Front Right */
145 FLC = (1 << 3), /* Front Left Center */
146 FRC = (1 << 4), /* Front Right Center */
147 RL = (1 << 5), /* Rear Left */
148 RC = (1 << 6), /* Rear Center */
149 RR = (1 << 7), /* Rear Right */
150 RLC = (1 << 8), /* Rear Left Center */
151 RRC = (1 << 9), /* Rear Right Center */
152 LFE = (1 << 10), /* Low Frequency Effect */
153 FLW = (1 << 11), /* Front Left Wide */
154 FRW = (1 << 12), /* Front Right Wide */
155 FLH = (1 << 13), /* Front Left High */
156 FCH = (1 << 14), /* Front Center High */
157 FRH = (1 << 15), /* Front Right High */
158 TC = (1 << 16), /* Top Center */
159};
160
161/*
162 * ELD SA bits in the CEA Speaker Allocation data block
163 */
164static int eld_speaker_allocation_bits[] = {
165 [0] = FL | FR,
166 [1] = LFE,
167 [2] = FC,
168 [3] = RL | RR,
169 [4] = RC,
170 [5] = FLC | FRC,
171 [6] = RLC | RRC,
172 /* the following are not defined in ELD yet */
173 [7] = FLW | FRW,
174 [8] = FLH | FRH,
175 [9] = TC,
176 [10] = FCH,
177};
178
179struct cea_channel_speaker_allocation {
180 int ca_index;
181 int speakers[8];
182
183 /* derived values, just for convenience */
184 int channels;
185 int spk_mask;
186};
187
188/*
189 * ALSA sequence is:
190 *
191 * surround40 surround41 surround50 surround51 surround71
192 * ch0 front left = = = =
193 * ch1 front right = = = =
194 * ch2 rear left = = = =
195 * ch3 rear right = = = =
196 * ch4 LFE center center center
197 * ch5 LFE LFE
198 * ch6 side left
199 * ch7 side right
200 *
201 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
202 */
203static int hdmi_channel_mapping[0x32][8] = {
204 /* stereo */
205 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
206 /* 2.1 */
207 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
208 /* Dolby Surround */
209 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
210 /* surround40 */
211 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
212 /* 4ch */
213 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
214 /* surround41 */
9396d317 215 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
079d88cc
WF
216 /* surround50 */
217 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
218 /* surround51 */
219 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
220 /* 7.1 */
221 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
222};
223
224/*
225 * This is an ordered list!
226 *
227 * The preceding ones have better chances to be selected by
53d7d69d 228 * hdmi_channel_allocation().
079d88cc
WF
229 */
230static struct cea_channel_speaker_allocation channel_allocations[] = {
231/* channel: 7 6 5 4 3 2 1 0 */
232{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
233 /* 2.1 */
234{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
235 /* Dolby Surround */
236{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
237 /* surround40 */
238{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
239 /* surround41 */
240{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
241 /* surround50 */
242{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
243 /* surround51 */
244{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
245 /* 6.1 */
246{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
247 /* surround71 */
248{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
249
250{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
251{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
252{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
253{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
254{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
255{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
256{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
257{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
258{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
259{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
260{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
261{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
262{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
263{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
264{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
265{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
266{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
267{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
268{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
269{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
270{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
271{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
272{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
273{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
274{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
275{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
276{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
277{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
278{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
279{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
280{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
281{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
282{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
283{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
284{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
285{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
286{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
287{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
288{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
289{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
290{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
291};
292
293
294/*
295 * HDMI routines
296 */
297
bce0d2a8
TI
298#define get_pin(spec, idx) \
299 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
300#define get_cvt(spec, idx) \
301 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
302#define get_pcm_rec(spec, idx) \
303 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
304
384a48d7 305static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
079d88cc 306{
384a48d7 307 int pin_idx;
079d88cc 308
384a48d7 309 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
bce0d2a8 310 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
384a48d7 311 return pin_idx;
079d88cc 312
384a48d7
SW
313 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++)
bce0d2a8 323 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
384a48d7
SW
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++)
bce0d2a8 335 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
384a48d7
SW
336 return cvt_idx;
337
338 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
079d88cc
WF
339 return -EINVAL;
340}
341
14bc52b8
PLB
342static 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);
68e03de9
DH
346 struct hdmi_spec *spec = codec->spec;
347 struct hdmi_eld *eld;
14bc52b8
PLB
348 int pin_idx;
349
14bc52b8
PLB
350 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
351
352 pin_idx = kcontrol->private_value;
bce0d2a8 353 eld = &get_pin(spec, pin_idx)->sink_eld;
68e03de9 354
4bd038f9 355 mutex_lock(&eld->lock);
68e03de9 356 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
4bd038f9 357 mutex_unlock(&eld->lock);
14bc52b8
PLB
358
359 return 0;
360}
361
362static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
363 struct snd_ctl_elem_value *ucontrol)
364{
365 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
68e03de9
DH
366 struct hdmi_spec *spec = codec->spec;
367 struct hdmi_eld *eld;
14bc52b8
PLB
368 int pin_idx;
369
14bc52b8 370 pin_idx = kcontrol->private_value;
bce0d2a8 371 eld = &get_pin(spec, pin_idx)->sink_eld;
68e03de9 372
4bd038f9 373 mutex_lock(&eld->lock);
68e03de9 374 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
4bd038f9 375 mutex_unlock(&eld->lock);
68e03de9
DH
376 snd_BUG();
377 return -EINVAL;
378 }
379
380 memset(ucontrol->value.bytes.data, 0,
381 ARRAY_SIZE(ucontrol->value.bytes.data));
382 if (eld->eld_valid)
383 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
384 eld->eld_size);
4bd038f9 385 mutex_unlock(&eld->lock);
14bc52b8
PLB
386
387 return 0;
388}
389
390static struct snd_kcontrol_new eld_bytes_ctl = {
391 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
392 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
393 .name = "ELD",
394 .info = hdmi_eld_ctl_info,
395 .get = hdmi_eld_ctl_get,
396};
397
398static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
399 int device)
400{
401 struct snd_kcontrol *kctl;
402 struct hdmi_spec *spec = codec->spec;
403 int err;
404
405 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
406 if (!kctl)
407 return -ENOMEM;
408 kctl->private_value = pin_idx;
409 kctl->id.device = device;
410
bce0d2a8 411 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
14bc52b8
PLB
412 if (err < 0)
413 return err;
414
bce0d2a8 415 get_pin(spec, pin_idx)->eld_ctl = kctl;
14bc52b8
PLB
416 return 0;
417}
418
079d88cc
WF
419#ifdef BE_PARANOID
420static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
421 int *packet_index, int *byte_index)
422{
423 int val;
424
425 val = snd_hda_codec_read(codec, pin_nid, 0,
426 AC_VERB_GET_HDMI_DIP_INDEX, 0);
427
428 *packet_index = val >> 5;
429 *byte_index = val & 0x1f;
430}
431#endif
432
433static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
434 int packet_index, int byte_index)
435{
436 int val;
437
438 val = (packet_index << 5) | (byte_index & 0x1f);
439
440 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
441}
442
443static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
444 unsigned char val)
445{
446 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
447}
448
384a48d7 449static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
079d88cc
WF
450{
451 /* Unmute */
452 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
453 snd_hda_codec_write(codec, pin_nid, 0,
454 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
6169b673
TI
455 /* Enable pin out: some machines with GM965 gets broken output when
456 * the pin is disabled or changed while using with HDMI
457 */
079d88cc 458 snd_hda_codec_write(codec, pin_nid, 0,
6169b673 459 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
079d88cc
WF
460}
461
384a48d7 462static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
079d88cc 463{
384a48d7 464 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
079d88cc
WF
465 AC_VERB_GET_CVT_CHAN_COUNT, 0);
466}
467
468static void hdmi_set_channel_count(struct hda_codec *codec,
384a48d7 469 hda_nid_t cvt_nid, int chs)
079d88cc 470{
384a48d7
SW
471 if (chs != hdmi_get_channel_count(codec, cvt_nid))
472 snd_hda_codec_write(codec, cvt_nid, 0,
079d88cc
WF
473 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
474}
475
476
477/*
478 * Channel mapping routines
479 */
480
481/*
482 * Compute derived values in channel_allocations[].
483 */
484static void init_channel_allocations(void)
485{
486 int i, j;
487 struct cea_channel_speaker_allocation *p;
488
489 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
490 p = channel_allocations + i;
491 p->channels = 0;
492 p->spk_mask = 0;
493 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
494 if (p->speakers[j]) {
495 p->channels++;
496 p->spk_mask |= p->speakers[j];
497 }
498 }
499}
500
72357c78
WX
501static int get_channel_allocation_order(int ca)
502{
503 int i;
504
505 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
506 if (channel_allocations[i].ca_index == ca)
507 break;
508 }
509 return i;
510}
511
079d88cc
WF
512/*
513 * The transformation takes two steps:
514 *
515 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
516 * spk_mask => (channel_allocations[]) => ai->CA
517 *
518 * TODO: it could select the wrong CA from multiple candidates.
519*/
384a48d7 520static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
079d88cc 521{
079d88cc 522 int i;
53d7d69d 523 int ca = 0;
079d88cc 524 int spk_mask = 0;
079d88cc
WF
525 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
526
527 /*
528 * CA defaults to 0 for basic stereo audio
529 */
530 if (channels <= 2)
531 return 0;
532
079d88cc
WF
533 /*
534 * expand ELD's speaker allocation mask
535 *
536 * ELD tells the speaker mask in a compact(paired) form,
537 * expand ELD's notions to match the ones used by Audio InfoFrame.
538 */
539 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
1613d6b4 540 if (eld->info.spk_alloc & (1 << i))
079d88cc
WF
541 spk_mask |= eld_speaker_allocation_bits[i];
542 }
543
544 /* search for the first working match in the CA table */
545 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
546 if (channels == channel_allocations[i].channels &&
547 (spk_mask & channel_allocations[i].spk_mask) ==
548 channel_allocations[i].spk_mask) {
53d7d69d 549 ca = channel_allocations[i].ca_index;
079d88cc
WF
550 break;
551 }
552 }
553
1613d6b4 554 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
2abbf439 555 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
53d7d69d 556 ca, channels, buf);
079d88cc 557
53d7d69d 558 return ca;
079d88cc
WF
559}
560
561static void hdmi_debug_channel_mapping(struct hda_codec *codec,
562 hda_nid_t pin_nid)
563{
564#ifdef CONFIG_SND_DEBUG_VERBOSE
565 int i;
566 int slot;
567
568 for (i = 0; i < 8; i++) {
569 slot = snd_hda_codec_read(codec, pin_nid, 0,
570 AC_VERB_GET_HDMI_CHAN_SLOT, i);
571 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
572 slot >> 4, slot & 0xf);
573 }
574#endif
575}
576
577
d45e6889 578static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
079d88cc 579 hda_nid_t pin_nid,
433968da 580 bool non_pcm,
53d7d69d 581 int ca)
079d88cc
WF
582{
583 int i;
079d88cc 584 int err;
72357c78 585 int order;
433968da 586 int non_pcm_mapping[8];
079d88cc 587
72357c78 588 order = get_channel_allocation_order(ca);
433968da 589
079d88cc 590 if (hdmi_channel_mapping[ca][1] == 0) {
72357c78 591 for (i = 0; i < channel_allocations[order].channels; i++)
079d88cc
WF
592 hdmi_channel_mapping[ca][i] = i | (i << 4);
593 for (; i < 8; i++)
594 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
595 }
596
433968da
WX
597 if (non_pcm) {
598 for (i = 0; i < channel_allocations[order].channels; i++)
599 non_pcm_mapping[i] = i | (i << 4);
600 for (; i < 8; i++)
601 non_pcm_mapping[i] = 0xf | (i << 4);
602 }
603
079d88cc
WF
604 for (i = 0; i < 8; i++) {
605 err = snd_hda_codec_write(codec, pin_nid, 0,
606 AC_VERB_SET_HDMI_CHAN_SLOT,
433968da 607 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
079d88cc 608 if (err) {
2abbf439
WF
609 snd_printdd(KERN_NOTICE
610 "HDMI: channel mapping failed\n");
079d88cc
WF
611 break;
612 }
613 }
614
615 hdmi_debug_channel_mapping(codec, pin_nid);
616}
617
d45e6889
TI
618struct channel_map_table {
619 unsigned char map; /* ALSA API channel map position */
620 unsigned char cea_slot; /* CEA slot value */
621 int spk_mask; /* speaker position bit mask */
622};
623
624static struct channel_map_table map_tables[] = {
625 { SNDRV_CHMAP_FL, 0x00, FL },
626 { SNDRV_CHMAP_FR, 0x01, FR },
627 { SNDRV_CHMAP_RL, 0x04, RL },
628 { SNDRV_CHMAP_RR, 0x05, RR },
629 { SNDRV_CHMAP_LFE, 0x02, LFE },
630 { SNDRV_CHMAP_FC, 0x03, FC },
631 { SNDRV_CHMAP_RLC, 0x06, RLC },
632 { SNDRV_CHMAP_RRC, 0x07, RRC },
633 {} /* terminator */
634};
635
636/* from ALSA API channel position to speaker bit mask */
637static int to_spk_mask(unsigned char c)
638{
639 struct channel_map_table *t = map_tables;
640 for (; t->map; t++) {
641 if (t->map == c)
642 return t->spk_mask;
643 }
644 return 0;
645}
646
647/* from ALSA API channel position to CEA slot */
648static int to_cea_slot(unsigned char c)
649{
650 struct channel_map_table *t = map_tables;
651 for (; t->map; t++) {
652 if (t->map == c)
653 return t->cea_slot;
654 }
655 return 0x0f;
656}
657
658/* from CEA slot to ALSA API channel position */
659static int from_cea_slot(unsigned char c)
660{
661 struct channel_map_table *t = map_tables;
662 for (; t->map; t++) {
663 if (t->cea_slot == c)
664 return t->map;
665 }
666 return 0;
667}
668
669/* from speaker bit mask to ALSA API channel position */
670static int spk_to_chmap(int spk)
671{
672 struct channel_map_table *t = map_tables;
673 for (; t->map; t++) {
674 if (t->spk_mask == spk)
675 return t->map;
676 }
677 return 0;
678}
679
680/* get the CA index corresponding to the given ALSA API channel map */
681static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
682{
683 int i, spks = 0, spk_mask = 0;
684
685 for (i = 0; i < chs; i++) {
686 int mask = to_spk_mask(map[i]);
687 if (mask) {
688 spk_mask |= mask;
689 spks++;
690 }
691 }
692
693 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
694 if ((chs == channel_allocations[i].channels ||
695 spks == channel_allocations[i].channels) &&
696 (spk_mask & channel_allocations[i].spk_mask) ==
697 channel_allocations[i].spk_mask)
698 return channel_allocations[i].ca_index;
699 }
700 return -1;
701}
702
703/* set up the channel slots for the given ALSA API channel map */
704static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
705 hda_nid_t pin_nid,
706 int chs, unsigned char *map)
707{
708 int i;
709 for (i = 0; i < 8; i++) {
710 int val, err;
711 if (i < chs)
712 val = to_cea_slot(map[i]);
713 else
714 val = 0xf;
715 val |= (i << 4);
716 err = snd_hda_codec_write(codec, pin_nid, 0,
717 AC_VERB_SET_HDMI_CHAN_SLOT, val);
718 if (err)
719 return -EINVAL;
720 }
721 return 0;
722}
723
724/* store ALSA API channel map from the current default map */
725static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
726{
727 int i;
728 for (i = 0; i < 8; i++) {
729 if (i < channel_allocations[ca].channels)
730 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
731 else
732 map[i] = 0;
733 }
734}
735
736static void hdmi_setup_channel_mapping(struct hda_codec *codec,
737 hda_nid_t pin_nid, bool non_pcm, int ca,
20608731
AH
738 int channels, unsigned char *map,
739 bool chmap_set)
d45e6889 740{
20608731 741 if (!non_pcm && chmap_set) {
d45e6889
TI
742 hdmi_manual_setup_channel_mapping(codec, pin_nid,
743 channels, map);
744 } else {
745 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
746 hdmi_setup_fake_chmap(map, ca);
747 }
748}
079d88cc
WF
749
750/*
751 * Audio InfoFrame routines
752 */
753
754/*
755 * Enable Audio InfoFrame Transmission
756 */
757static void hdmi_start_infoframe_trans(struct hda_codec *codec,
758 hda_nid_t pin_nid)
759{
760 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
761 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
762 AC_DIPXMIT_BEST);
763}
764
765/*
766 * Disable Audio InfoFrame Transmission
767 */
768static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
769 hda_nid_t pin_nid)
770{
771 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
772 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
773 AC_DIPXMIT_DISABLE);
774}
775
776static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
777{
778#ifdef CONFIG_SND_DEBUG_VERBOSE
779 int i;
780 int size;
781
782 size = snd_hdmi_get_eld_size(codec, pin_nid);
783 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
784
785 for (i = 0; i < 8; i++) {
786 size = snd_hda_codec_read(codec, pin_nid, 0,
787 AC_VERB_GET_HDMI_DIP_SIZE, i);
788 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
789 }
790#endif
791}
792
793static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
794{
795#ifdef BE_PARANOID
796 int i, j;
797 int size;
798 int pi, bi;
799 for (i = 0; i < 8; i++) {
800 size = snd_hda_codec_read(codec, pin_nid, 0,
801 AC_VERB_GET_HDMI_DIP_SIZE, i);
802 if (size == 0)
803 continue;
804
805 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
806 for (j = 1; j < 1000; j++) {
807 hdmi_write_dip_byte(codec, pin_nid, 0x0);
808 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
809 if (pi != i)
810 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
811 bi, pi, i);
812 if (bi == 0) /* byte index wrapped around */
813 break;
814 }
815 snd_printd(KERN_INFO
816 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
817 i, size, j);
818 }
819#endif
820}
821
53d7d69d 822static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
079d88cc 823{
53d7d69d 824 u8 *bytes = (u8 *)hdmi_ai;
079d88cc
WF
825 u8 sum = 0;
826 int i;
827
53d7d69d 828 hdmi_ai->checksum = 0;
079d88cc 829
53d7d69d 830 for (i = 0; i < sizeof(*hdmi_ai); i++)
079d88cc
WF
831 sum += bytes[i];
832
53d7d69d 833 hdmi_ai->checksum = -sum;
079d88cc
WF
834}
835
836static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
837 hda_nid_t pin_nid,
53d7d69d 838 u8 *dip, int size)
079d88cc 839{
079d88cc
WF
840 int i;
841
842 hdmi_debug_dip_size(codec, pin_nid);
843 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
844
079d88cc 845 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
53d7d69d
WF
846 for (i = 0; i < size; i++)
847 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
079d88cc
WF
848}
849
850static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
53d7d69d 851 u8 *dip, int size)
079d88cc 852{
079d88cc
WF
853 u8 val;
854 int i;
855
856 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
857 != AC_DIPXMIT_BEST)
858 return false;
859
860 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
53d7d69d 861 for (i = 0; i < size; i++) {
079d88cc
WF
862 val = snd_hda_codec_read(codec, pin_nid, 0,
863 AC_VERB_GET_HDMI_DIP_DATA, 0);
53d7d69d 864 if (val != dip[i])
079d88cc
WF
865 return false;
866 }
867
868 return true;
869}
870
384a48d7 871static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
1a6003b5
TI
872 bool non_pcm,
873 struct snd_pcm_substream *substream)
079d88cc
WF
874{
875 struct hdmi_spec *spec = codec->spec;
bce0d2a8 876 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
384a48d7 877 hda_nid_t pin_nid = per_pin->pin_nid;
53d7d69d 878 int channels = substream->runtime->channels;
384a48d7 879 struct hdmi_eld *eld;
53d7d69d 880 int ca;
2b203dbb 881 union audio_infoframe ai;
079d88cc 882
bce0d2a8 883 eld = &per_pin->sink_eld;
384a48d7
SW
884 if (!eld->monitor_present)
885 return;
079d88cc 886
d45e6889
TI
887 if (!non_pcm && per_pin->chmap_set)
888 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
889 else
890 ca = hdmi_channel_allocation(eld, channels);
891 if (ca < 0)
892 ca = 0;
384a48d7
SW
893
894 memset(&ai, 0, sizeof(ai));
1613d6b4 895 if (eld->info.conn_type == 0) { /* HDMI */
384a48d7
SW
896 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
897
898 hdmi_ai->type = 0x84;
899 hdmi_ai->ver = 0x01;
900 hdmi_ai->len = 0x0a;
901 hdmi_ai->CC02_CT47 = channels - 1;
902 hdmi_ai->CA = ca;
903 hdmi_checksum_audio_infoframe(hdmi_ai);
1613d6b4 904 } else if (eld->info.conn_type == 1) { /* DisplayPort */
384a48d7
SW
905 struct dp_audio_infoframe *dp_ai = &ai.dp;
906
907 dp_ai->type = 0x84;
908 dp_ai->len = 0x1b;
909 dp_ai->ver = 0x11 << 2;
910 dp_ai->CC02_CT47 = channels - 1;
911 dp_ai->CA = ca;
912 } else {
913 snd_printd("HDMI: unknown connection type at pin %d\n",
914 pin_nid);
915 return;
916 }
53d7d69d 917
384a48d7
SW
918 /*
919 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
920 * sizeof(*dp_ai) to avoid partial match/update problems when
921 * the user switches between HDMI/DP monitors.
922 */
923 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
924 sizeof(ai))) {
925 snd_printdd("hdmi_setup_audio_infoframe: "
926 "pin=%d channels=%d\n",
927 pin_nid,
928 channels);
d45e6889 929 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
20608731
AH
930 channels, per_pin->chmap,
931 per_pin->chmap_set);
384a48d7
SW
932 hdmi_stop_infoframe_trans(codec, pin_nid);
933 hdmi_fill_audio_infoframe(codec, pin_nid,
934 ai.bytes, sizeof(ai));
935 hdmi_start_infoframe_trans(codec, pin_nid);
2d7e887c
WX
936 } else {
937 /* For non-pcm audio switch, setup new channel mapping
938 * accordingly */
1a6003b5 939 if (per_pin->non_pcm != non_pcm)
d45e6889 940 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
20608731
AH
941 channels, per_pin->chmap,
942 per_pin->chmap_set);
079d88cc 943 }
433968da 944
1a6003b5 945 per_pin->non_pcm = non_pcm;
079d88cc
WF
946}
947
948
949/*
950 * Unsolicited events
951 */
952
c6e8453e 953static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
38faddb1 954
079d88cc
WF
955static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
956{
957 struct hdmi_spec *spec = codec->spec;
3a93897e
TI
958 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
959 int pin_nid;
384a48d7 960 int pin_idx;
3a93897e
TI
961 struct hda_jack_tbl *jack;
962
963 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
964 if (!jack)
965 return;
966 pin_nid = jack->nid;
967 jack->jack_dirty = 1;
079d88cc 968
fae3d88a 969 _snd_printd(SND_PR_VERBOSE,
384a48d7 970 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
fae3d88a
FW
971 codec->addr, pin_nid,
972 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
079d88cc 973
384a48d7
SW
974 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
975 if (pin_idx < 0)
079d88cc
WF
976 return;
977
bce0d2a8 978 hdmi_present_sense(get_pin(spec, pin_idx), 1);
01a61e12 979 snd_hda_jack_report_sync(codec);
079d88cc
WF
980}
981
982static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
983{
984 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
985 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
986 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
987 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
988
989 printk(KERN_INFO
e9ea8e8f 990 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
384a48d7 991 codec->addr,
079d88cc
WF
992 tag,
993 subtag,
994 cp_state,
995 cp_ready);
996
997 /* TODO */
998 if (cp_state)
999 ;
1000 if (cp_ready)
1001 ;
1002}
1003
1004
1005static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1006{
079d88cc
WF
1007 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1008 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1009
3a93897e 1010 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
079d88cc
WF
1011 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1012 return;
1013 }
1014
1015 if (subtag == 0)
1016 hdmi_intrinsic_event(codec, res);
1017 else
1018 hdmi_non_intrinsic_event(codec, res);
1019}
1020
1021/*
1022 * Callbacks
1023 */
1024
92f10b3f
TI
1025/* HBR should be Non-PCM, 8 channels */
1026#define is_hbr_format(format) \
1027 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1028
384a48d7
SW
1029static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1030 hda_nid_t pin_nid, u32 stream_tag, int format)
079d88cc 1031{
ea87d1c4
AH
1032 int pinctl;
1033 int new_pinctl = 0;
ea87d1c4 1034
384a48d7
SW
1035 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1036 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
ea87d1c4
AH
1037 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1038
1039 new_pinctl = pinctl & ~AC_PINCTL_EPT;
92f10b3f 1040 if (is_hbr_format(format))
ea87d1c4
AH
1041 new_pinctl |= AC_PINCTL_EPT_HBR;
1042 else
1043 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1044
1045 snd_printdd("hdmi_setup_stream: "
1046 "NID=0x%x, %spinctl=0x%x\n",
384a48d7 1047 pin_nid,
ea87d1c4
AH
1048 pinctl == new_pinctl ? "" : "new-",
1049 new_pinctl);
1050
1051 if (pinctl != new_pinctl)
384a48d7 1052 snd_hda_codec_write(codec, pin_nid, 0,
ea87d1c4
AH
1053 AC_VERB_SET_PIN_WIDGET_CONTROL,
1054 new_pinctl);
ea87d1c4 1055
384a48d7 1056 }
92f10b3f 1057 if (is_hbr_format(format) && !new_pinctl) {
ea87d1c4
AH
1058 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1059 return -EINVAL;
1060 }
079d88cc 1061
384a48d7 1062 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
ea87d1c4 1063 return 0;
079d88cc
WF
1064}
1065
bbbe3390
TI
1066/*
1067 * HDA PCM callbacks
1068 */
1069static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1070 struct hda_codec *codec,
1071 struct snd_pcm_substream *substream)
1072{
1073 struct hdmi_spec *spec = codec->spec;
639cef0e 1074 struct snd_pcm_runtime *runtime = substream->runtime;
384a48d7
SW
1075 int pin_idx, cvt_idx, mux_idx = 0;
1076 struct hdmi_spec_per_pin *per_pin;
1077 struct hdmi_eld *eld;
1078 struct hdmi_spec_per_cvt *per_cvt = NULL;
bbbe3390 1079
384a48d7
SW
1080 /* Validate hinfo */
1081 pin_idx = hinfo_to_pin_index(spec, hinfo);
1082 if (snd_BUG_ON(pin_idx < 0))
bbbe3390 1083 return -EINVAL;
bce0d2a8 1084 per_pin = get_pin(spec, pin_idx);
384a48d7
SW
1085 eld = &per_pin->sink_eld;
1086
1087 /* Dynamically assign converter to stream */
1088 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
bce0d2a8 1089 per_cvt = get_cvt(spec, cvt_idx);
bbbe3390 1090
384a48d7
SW
1091 /* Must not already be assigned */
1092 if (per_cvt->assigned)
1093 continue;
1094 /* Must be in pin's mux's list of converters */
1095 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1096 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1097 break;
1098 /* Not in mux list */
1099 if (mux_idx == per_pin->num_mux_nids)
1100 continue;
1101 break;
1102 }
1103 /* No free converters */
1104 if (cvt_idx == spec->num_cvts)
1105 return -ENODEV;
1106
1107 /* Claim converter */
1108 per_cvt->assigned = 1;
1109 hinfo->nid = per_cvt->cvt_nid;
1110
1111 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1112 AC_VERB_SET_CONNECT_SEL,
1113 mux_idx);
384a48d7 1114 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
bbbe3390 1115
2def8172 1116 /* Initially set the converter's capabilities */
384a48d7
SW
1117 hinfo->channels_min = per_cvt->channels_min;
1118 hinfo->channels_max = per_cvt->channels_max;
1119 hinfo->rates = per_cvt->rates;
1120 hinfo->formats = per_cvt->formats;
1121 hinfo->maxbps = per_cvt->maxbps;
2def8172 1122
384a48d7 1123 /* Restrict capabilities by ELD if this isn't disabled */
c3d52105 1124 if (!static_hdmi_pcm && eld->eld_valid) {
1613d6b4 1125 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
bbbe3390 1126 if (hinfo->channels_min > hinfo->channels_max ||
2ad779b7
TI
1127 !hinfo->rates || !hinfo->formats) {
1128 per_cvt->assigned = 0;
1129 hinfo->nid = 0;
1130 snd_hda_spdif_ctls_unassign(codec, pin_idx);
bbbe3390 1131 return -ENODEV;
2ad779b7 1132 }
bbbe3390 1133 }
2def8172
SW
1134
1135 /* Store the updated parameters */
639cef0e
TI
1136 runtime->hw.channels_min = hinfo->channels_min;
1137 runtime->hw.channels_max = hinfo->channels_max;
1138 runtime->hw.formats = hinfo->formats;
1139 runtime->hw.rates = hinfo->rates;
4fe2ca14
TI
1140
1141 snd_pcm_hw_constraint_step(substream->runtime, 0,
1142 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
bbbe3390
TI
1143 return 0;
1144}
1145
079d88cc
WF
1146/*
1147 * HDA/HDMI auto parsing
1148 */
384a48d7 1149static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
079d88cc
WF
1150{
1151 struct hdmi_spec *spec = codec->spec;
bce0d2a8 1152 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
384a48d7 1153 hda_nid_t pin_nid = per_pin->pin_nid;
079d88cc
WF
1154
1155 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1156 snd_printk(KERN_WARNING
1157 "HDMI: pin %d wcaps %#x "
1158 "does not support connection list\n",
1159 pin_nid, get_wcaps(codec, pin_nid));
1160 return -EINVAL;
1161 }
1162
384a48d7
SW
1163 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1164 per_pin->mux_nids,
1165 HDA_MAX_CONNECTIONS);
079d88cc
WF
1166
1167 return 0;
1168}
1169
c6e8453e 1170static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
079d88cc 1171{
744626da 1172 struct hda_codec *codec = per_pin->codec;
4bd038f9
DH
1173 struct hdmi_spec *spec = codec->spec;
1174 struct hdmi_eld *eld = &spec->temp_eld;
1175 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
744626da 1176 hda_nid_t pin_nid = per_pin->pin_nid;
5d44f927
SW
1177 /*
1178 * Always execute a GetPinSense verb here, even when called from
1179 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1180 * response's PD bit is not the real PD value, but indicates that
1181 * the real PD value changed. An older version of the HD-audio
1182 * specification worked this way. Hence, we just ignore the data in
1183 * the unsolicited response to avoid custom WARs.
1184 */
079d88cc 1185 int present = snd_hda_pin_sense(codec, pin_nid);
4bd038f9
DH
1186 bool update_eld = false;
1187 bool eld_changed = false;
079d88cc 1188
4bd038f9
DH
1189 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1190 if (pin_eld->monitor_present)
1191 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1192 else
1193 eld->eld_valid = false;
079d88cc 1194
fae3d88a 1195 _snd_printd(SND_PR_VERBOSE,
384a48d7 1196 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
10250911 1197 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
5d44f927 1198
4bd038f9 1199 if (eld->eld_valid) {
1613d6b4
DH
1200 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1201 &eld->eld_size) < 0)
4bd038f9 1202 eld->eld_valid = false;
1613d6b4
DH
1203 else {
1204 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1205 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1206 eld->eld_size) < 0)
4bd038f9 1207 eld->eld_valid = false;
1613d6b4
DH
1208 }
1209
4bd038f9 1210 if (eld->eld_valid) {
1613d6b4 1211 snd_hdmi_show_eld(&eld->info);
4bd038f9 1212 update_eld = true;
1613d6b4 1213 }
c6e8453e 1214 else if (repoll) {
744626da
WF
1215 queue_delayed_work(codec->bus->workq,
1216 &per_pin->work,
1217 msecs_to_jiffies(300));
4bd038f9 1218 return;
744626da
WF
1219 }
1220 }
4bd038f9
DH
1221
1222 mutex_lock(&pin_eld->lock);
92c69e79 1223 if (pin_eld->eld_valid && !eld->eld_valid) {
4bd038f9 1224 update_eld = true;
92c69e79
DH
1225 eld_changed = true;
1226 }
4bd038f9
DH
1227 if (update_eld) {
1228 pin_eld->eld_valid = eld->eld_valid;
92c69e79
DH
1229 eld_changed = pin_eld->eld_size != eld->eld_size ||
1230 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
4bd038f9
DH
1231 eld->eld_size) != 0;
1232 if (eld_changed)
1233 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1234 eld->eld_size);
1235 pin_eld->eld_size = eld->eld_size;
1236 pin_eld->info = eld->info;
1237 }
1238 mutex_unlock(&pin_eld->lock);
92c69e79
DH
1239
1240 if (eld_changed)
1241 snd_ctl_notify(codec->bus->card,
1242 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1243 &per_pin->eld_ctl->id);
079d88cc
WF
1244}
1245
744626da
WF
1246static void hdmi_repoll_eld(struct work_struct *work)
1247{
1248 struct hdmi_spec_per_pin *per_pin =
1249 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1250
c6e8453e
WF
1251 if (per_pin->repoll_count++ > 6)
1252 per_pin->repoll_count = 0;
1253
1254 hdmi_present_sense(per_pin, per_pin->repoll_count);
744626da
WF
1255}
1256
c88d4e84
TI
1257static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1258 hda_nid_t nid);
1259
079d88cc
WF
1260static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1261{
1262 struct hdmi_spec *spec = codec->spec;
384a48d7
SW
1263 unsigned int caps, config;
1264 int pin_idx;
1265 struct hdmi_spec_per_pin *per_pin;
07acecc1 1266 int err;
079d88cc 1267
efc2f8de 1268 caps = snd_hda_query_pin_caps(codec, pin_nid);
384a48d7
SW
1269 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1270 return 0;
1271
efc2f8de 1272 config = snd_hda_codec_get_pincfg(codec, pin_nid);
384a48d7
SW
1273 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1274 return 0;
1275
c88d4e84
TI
1276 if (codec->vendor_id == 0x80862807)
1277 intel_haswell_fixup_connect_list(codec, pin_nid);
1278
384a48d7 1279 pin_idx = spec->num_pins;
bce0d2a8
TI
1280 per_pin = snd_array_new(&spec->pins);
1281 if (!per_pin)
1282 return -ENOMEM;
384a48d7
SW
1283
1284 per_pin->pin_nid = pin_nid;
1a6003b5 1285 per_pin->non_pcm = false;
079d88cc 1286
384a48d7
SW
1287 err = hdmi_read_pin_conn(codec, pin_idx);
1288 if (err < 0)
1289 return err;
079d88cc 1290
079d88cc
WF
1291 spec->num_pins++;
1292
384a48d7 1293 return 0;
079d88cc
WF
1294}
1295
384a48d7 1296static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
079d88cc
WF
1297{
1298 struct hdmi_spec *spec = codec->spec;
384a48d7
SW
1299 struct hdmi_spec_per_cvt *per_cvt;
1300 unsigned int chans;
1301 int err;
079d88cc 1302
384a48d7
SW
1303 chans = get_wcaps(codec, cvt_nid);
1304 chans = get_wcaps_channels(chans);
1305
bce0d2a8
TI
1306 per_cvt = snd_array_new(&spec->cvts);
1307 if (!per_cvt)
1308 return -ENOMEM;
384a48d7
SW
1309
1310 per_cvt->cvt_nid = cvt_nid;
1311 per_cvt->channels_min = 2;
d45e6889 1312 if (chans <= 16) {
384a48d7 1313 per_cvt->channels_max = chans;
d45e6889
TI
1314 if (chans > spec->channels_max)
1315 spec->channels_max = chans;
1316 }
384a48d7
SW
1317
1318 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1319 &per_cvt->rates,
1320 &per_cvt->formats,
1321 &per_cvt->maxbps);
1322 if (err < 0)
1323 return err;
1324
bce0d2a8
TI
1325 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1326 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1327 spec->num_cvts++;
079d88cc
WF
1328
1329 return 0;
1330}
1331
1332static int hdmi_parse_codec(struct hda_codec *codec)
1333{
1334 hda_nid_t nid;
1335 int i, nodes;
1336
1337 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1338 if (!nid || nodes < 0) {
1339 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1340 return -EINVAL;
1341 }
1342
1343 for (i = 0; i < nodes; i++, nid++) {
1344 unsigned int caps;
1345 unsigned int type;
1346
efc2f8de 1347 caps = get_wcaps(codec, nid);
079d88cc
WF
1348 type = get_wcaps_type(caps);
1349
1350 if (!(caps & AC_WCAP_DIGITAL))
1351 continue;
1352
1353 switch (type) {
1354 case AC_WID_AUD_OUT:
384a48d7 1355 hdmi_add_cvt(codec, nid);
079d88cc
WF
1356 break;
1357 case AC_WID_PIN:
3eaead57 1358 hdmi_add_pin(codec, nid);
079d88cc
WF
1359 break;
1360 }
1361 }
1362
c9adeefd
DH
1363#ifdef CONFIG_PM
1364 /* We're seeing some problems with unsolicited hot plug events on
1365 * PantherPoint after S3, if this is not enabled */
1366 if (codec->vendor_id == 0x80862806)
1367 codec->bus->power_keep_link_on = 1;
079d88cc
WF
1368 /*
1369 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1370 * can be lost and presence sense verb will become inaccurate if the
1371 * HDA link is powered off at hot plug or hw initialization time.
1372 */
c9adeefd 1373 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
079d88cc
WF
1374 AC_PWRST_EPSS))
1375 codec->bus->power_keep_link_on = 1;
1376#endif
1377
1378 return 0;
1379}
1380
84eb01be
TI
1381/*
1382 */
1a6003b5
TI
1383static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1384{
1385 struct hda_spdif_out *spdif;
1386 bool non_pcm;
1387
1388 mutex_lock(&codec->spdif_mutex);
1389 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1390 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1391 mutex_unlock(&codec->spdif_mutex);
1392 return non_pcm;
1393}
1394
1395
84eb01be
TI
1396/*
1397 * HDMI callbacks
1398 */
1399
1400static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1401 struct hda_codec *codec,
1402 unsigned int stream_tag,
1403 unsigned int format,
1404 struct snd_pcm_substream *substream)
1405{
384a48d7
SW
1406 hda_nid_t cvt_nid = hinfo->nid;
1407 struct hdmi_spec *spec = codec->spec;
1408 int pin_idx = hinfo_to_pin_index(spec, hinfo);
bce0d2a8 1409 hda_nid_t pin_nid = get_pin(spec, pin_idx)->pin_nid;
1a6003b5
TI
1410 bool non_pcm;
1411
1412 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
384a48d7
SW
1413
1414 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
84eb01be 1415
1a6003b5 1416 hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
84eb01be 1417
384a48d7 1418 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
84eb01be
TI
1419}
1420
8dfaa573
TI
1421static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1422 struct hda_codec *codec,
1423 struct snd_pcm_substream *substream)
1424{
1425 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1426 return 0;
1427}
1428
f2ad24fa
TI
1429static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1430 struct hda_codec *codec,
1431 struct snd_pcm_substream *substream)
384a48d7
SW
1432{
1433 struct hdmi_spec *spec = codec->spec;
1434 int cvt_idx, pin_idx;
1435 struct hdmi_spec_per_cvt *per_cvt;
1436 struct hdmi_spec_per_pin *per_pin;
384a48d7 1437
384a48d7
SW
1438 if (hinfo->nid) {
1439 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1440 if (snd_BUG_ON(cvt_idx < 0))
1441 return -EINVAL;
bce0d2a8 1442 per_cvt = get_cvt(spec, cvt_idx);
384a48d7
SW
1443
1444 snd_BUG_ON(!per_cvt->assigned);
1445 per_cvt->assigned = 0;
1446 hinfo->nid = 0;
1447
1448 pin_idx = hinfo_to_pin_index(spec, hinfo);
1449 if (snd_BUG_ON(pin_idx < 0))
1450 return -EINVAL;
bce0d2a8 1451 per_pin = get_pin(spec, pin_idx);
384a48d7 1452
384a48d7 1453 snd_hda_spdif_ctls_unassign(codec, pin_idx);
d45e6889
TI
1454 per_pin->chmap_set = false;
1455 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
384a48d7 1456 }
d45e6889 1457
384a48d7
SW
1458 return 0;
1459}
1460
1461static const struct hda_pcm_ops generic_ops = {
1462 .open = hdmi_pcm_open,
f2ad24fa 1463 .close = hdmi_pcm_close,
384a48d7 1464 .prepare = generic_hdmi_playback_pcm_prepare,
8dfaa573 1465 .cleanup = generic_hdmi_playback_pcm_cleanup,
84eb01be
TI
1466};
1467
d45e6889
TI
1468/*
1469 * ALSA API channel-map control callbacks
1470 */
1471static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1472 struct snd_ctl_elem_info *uinfo)
1473{
1474 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1475 struct hda_codec *codec = info->private_data;
1476 struct hdmi_spec *spec = codec->spec;
1477 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1478 uinfo->count = spec->channels_max;
1479 uinfo->value.integer.min = 0;
1480 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1481 return 0;
1482}
1483
1484static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1485 unsigned int size, unsigned int __user *tlv)
1486{
1487 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1488 struct hda_codec *codec = info->private_data;
1489 struct hdmi_spec *spec = codec->spec;
1490 const unsigned int valid_mask =
1491 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1492 unsigned int __user *dst;
1493 int chs, count = 0;
1494
1495 if (size < 8)
1496 return -ENOMEM;
1497 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1498 return -EFAULT;
1499 size -= 8;
1500 dst = tlv + 2;
498dab3a 1501 for (chs = 2; chs <= spec->channels_max; chs++) {
d45e6889
TI
1502 int i, c;
1503 struct cea_channel_speaker_allocation *cap;
1504 cap = channel_allocations;
1505 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1506 int chs_bytes = chs * 4;
1507 if (cap->channels != chs)
1508 continue;
1509 if (cap->spk_mask & ~valid_mask)
1510 continue;
1511 if (size < 8)
1512 return -ENOMEM;
1513 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1514 put_user(chs_bytes, dst + 1))
1515 return -EFAULT;
1516 dst += 2;
1517 size -= 8;
1518 count += 8;
1519 if (size < chs_bytes)
1520 return -ENOMEM;
1521 size -= chs_bytes;
1522 count += chs_bytes;
1523 for (c = 7; c >= 0; c--) {
1524 int spk = cap->speakers[c];
1525 if (!spk)
1526 continue;
1527 if (put_user(spk_to_chmap(spk), dst))
1528 return -EFAULT;
1529 dst++;
1530 }
1531 }
1532 }
1533 if (put_user(count, tlv + 1))
1534 return -EFAULT;
1535 return 0;
1536}
1537
1538static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1539 struct snd_ctl_elem_value *ucontrol)
1540{
1541 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1542 struct hda_codec *codec = info->private_data;
1543 struct hdmi_spec *spec = codec->spec;
1544 int pin_idx = kcontrol->private_value;
bce0d2a8 1545 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
d45e6889
TI
1546 int i;
1547
1548 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1549 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1550 return 0;
1551}
1552
1553static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1554 struct snd_ctl_elem_value *ucontrol)
1555{
1556 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1557 struct hda_codec *codec = info->private_data;
1558 struct hdmi_spec *spec = codec->spec;
1559 int pin_idx = kcontrol->private_value;
bce0d2a8 1560 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
d45e6889
TI
1561 unsigned int ctl_idx;
1562 struct snd_pcm_substream *substream;
1563 unsigned char chmap[8];
1564 int i, ca, prepared = 0;
1565
1566 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1567 substream = snd_pcm_chmap_substream(info, ctl_idx);
1568 if (!substream || !substream->runtime)
6f54c361 1569 return 0; /* just for avoiding error from alsactl restore */
d45e6889
TI
1570 switch (substream->runtime->status->state) {
1571 case SNDRV_PCM_STATE_OPEN:
1572 case SNDRV_PCM_STATE_SETUP:
1573 break;
1574 case SNDRV_PCM_STATE_PREPARED:
1575 prepared = 1;
1576 break;
1577 default:
1578 return -EBUSY;
1579 }
1580 memset(chmap, 0, sizeof(chmap));
1581 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1582 chmap[i] = ucontrol->value.integer.value[i];
1583 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1584 return 0;
1585 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1586 if (ca < 0)
1587 return -EINVAL;
1588 per_pin->chmap_set = true;
1589 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1590 if (prepared)
1591 hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
1592 substream);
1593
1594 return 0;
1595}
1596
84eb01be
TI
1597static int generic_hdmi_build_pcms(struct hda_codec *codec)
1598{
1599 struct hdmi_spec *spec = codec->spec;
384a48d7 1600 int pin_idx;
84eb01be 1601
384a48d7
SW
1602 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1603 struct hda_pcm *info;
84eb01be 1604 struct hda_pcm_stream *pstr;
bce0d2a8
TI
1605 struct hdmi_spec_per_pin *per_pin;
1606
1607 per_pin = get_pin(spec, pin_idx);
1608 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1609 info = snd_array_new(&spec->pcm_rec);
1610 if (!info)
1611 return -ENOMEM;
1612 info->name = per_pin->pcm_name;
84eb01be 1613 info->pcm_type = HDA_PCM_TYPE_HDMI;
d45e6889 1614 info->own_chmap = true;
384a48d7 1615
84eb01be 1616 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
384a48d7
SW
1617 pstr->substreams = 1;
1618 pstr->ops = generic_ops;
1619 /* other pstr fields are set in open */
84eb01be
TI
1620 }
1621
384a48d7 1622 codec->num_pcms = spec->num_pins;
bce0d2a8 1623 codec->pcm_info = spec->pcm_rec.list;
384a48d7 1624
84eb01be
TI
1625 return 0;
1626}
1627
0b6c49b5
DH
1628static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1629{
31ef2257 1630 char hdmi_str[32] = "HDMI/DP";
0b6c49b5 1631 struct hdmi_spec *spec = codec->spec;
bce0d2a8
TI
1632 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1633 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
0b6c49b5 1634
31ef2257
TI
1635 if (pcmdev > 0)
1636 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
30efd8de
DH
1637 if (!is_jack_detectable(codec, per_pin->pin_nid))
1638 strncat(hdmi_str, " Phantom",
1639 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
0b6c49b5 1640
31ef2257 1641 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
0b6c49b5
DH
1642}
1643
84eb01be
TI
1644static int generic_hdmi_build_controls(struct hda_codec *codec)
1645{
1646 struct hdmi_spec *spec = codec->spec;
1647 int err;
384a48d7 1648 int pin_idx;
84eb01be 1649
384a48d7 1650 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
bce0d2a8 1651 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
0b6c49b5
DH
1652
1653 err = generic_hdmi_build_jack(codec, pin_idx);
1654 if (err < 0)
1655 return err;
1656
dcda5806
TI
1657 err = snd_hda_create_dig_out_ctls(codec,
1658 per_pin->pin_nid,
1659 per_pin->mux_nids[0],
1660 HDA_PCM_TYPE_HDMI);
84eb01be
TI
1661 if (err < 0)
1662 return err;
384a48d7 1663 snd_hda_spdif_ctls_unassign(codec, pin_idx);
14bc52b8
PLB
1664
1665 /* add control for ELD Bytes */
bce0d2a8
TI
1666 err = hdmi_create_eld_ctl(codec, pin_idx,
1667 get_pcm_rec(spec, pin_idx)->device);
14bc52b8
PLB
1668
1669 if (err < 0)
1670 return err;
31ef2257 1671
82b1d73f 1672 hdmi_present_sense(per_pin, 0);
84eb01be
TI
1673 }
1674
d45e6889
TI
1675 /* add channel maps */
1676 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1677 struct snd_pcm_chmap *chmap;
1678 struct snd_kcontrol *kctl;
1679 int i;
1680 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1681 SNDRV_PCM_STREAM_PLAYBACK,
1682 NULL, 0, pin_idx, &chmap);
1683 if (err < 0)
1684 return err;
1685 /* override handlers */
1686 chmap->private_data = codec;
1687 kctl = chmap->kctl;
1688 for (i = 0; i < kctl->count; i++)
1689 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1690 kctl->info = hdmi_chmap_ctl_info;
1691 kctl->get = hdmi_chmap_ctl_get;
1692 kctl->put = hdmi_chmap_ctl_put;
1693 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1694 }
1695
84eb01be
TI
1696 return 0;
1697}
1698
8b8d654b 1699static int generic_hdmi_init_per_pins(struct hda_codec *codec)
84eb01be
TI
1700{
1701 struct hdmi_spec *spec = codec->spec;
384a48d7
SW
1702 int pin_idx;
1703
1704 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
bce0d2a8 1705 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
384a48d7 1706 struct hdmi_eld *eld = &per_pin->sink_eld;
84eb01be 1707
744626da 1708 per_pin->codec = codec;
4bd038f9 1709 mutex_init(&eld->lock);
744626da 1710 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
384a48d7 1711 snd_hda_eld_proc_new(codec, eld, pin_idx);
84eb01be 1712 }
8b8d654b
TI
1713 return 0;
1714}
1715
1716static int generic_hdmi_init(struct hda_codec *codec)
1717{
1718 struct hdmi_spec *spec = codec->spec;
1719 int pin_idx;
1720
1721 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
bce0d2a8 1722 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
8b8d654b
TI
1723 hda_nid_t pin_nid = per_pin->pin_nid;
1724
1725 hdmi_init_pin(codec, pin_nid);
1726 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
1727 }
84eb01be
TI
1728 return 0;
1729}
1730
bce0d2a8
TI
1731static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1732{
1733 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1734 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1735 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1736}
1737
1738static void hdmi_array_free(struct hdmi_spec *spec)
1739{
1740 snd_array_free(&spec->pins);
1741 snd_array_free(&spec->cvts);
1742 snd_array_free(&spec->pcm_rec);
1743}
1744
84eb01be
TI
1745static void generic_hdmi_free(struct hda_codec *codec)
1746{
1747 struct hdmi_spec *spec = codec->spec;
384a48d7
SW
1748 int pin_idx;
1749
1750 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
bce0d2a8 1751 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
384a48d7 1752 struct hdmi_eld *eld = &per_pin->sink_eld;
84eb01be 1753
744626da 1754 cancel_delayed_work(&per_pin->work);
384a48d7
SW
1755 snd_hda_eld_proc_free(codec, eld);
1756 }
84eb01be 1757
744626da 1758 flush_workqueue(codec->bus->workq);
bce0d2a8 1759 hdmi_array_free(spec);
84eb01be
TI
1760 kfree(spec);
1761}
1762
fb79e1e0 1763static const struct hda_codec_ops generic_hdmi_patch_ops = {
84eb01be
TI
1764 .init = generic_hdmi_init,
1765 .free = generic_hdmi_free,
1766 .build_pcms = generic_hdmi_build_pcms,
1767 .build_controls = generic_hdmi_build_controls,
1768 .unsol_event = hdmi_unsol_event,
1769};
1770
6ffe168f 1771
c88d4e84
TI
1772static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1773 hda_nid_t nid)
1774{
1775 struct hdmi_spec *spec = codec->spec;
1776 hda_nid_t conns[4];
1777 int nconns;
6ffe168f 1778
c88d4e84
TI
1779 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1780 if (nconns == spec->num_cvts &&
1781 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
6ffe168f
ML
1782 return;
1783
c88d4e84
TI
1784 /* override pins connection list */
1785 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
bce0d2a8 1786 nconns = max(spec->num_cvts, 4);
c88d4e84 1787 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
6ffe168f
ML
1788}
1789
1611a9c9
ML
1790#define INTEL_VENDOR_NID 0x08
1791#define INTEL_GET_VENDOR_VERB 0xf81
1792#define INTEL_SET_VENDOR_VERB 0x781
1793#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1794#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1795
1796static void intel_haswell_enable_all_pins(struct hda_codec *codec,
1797 const struct hda_fixup *fix, int action)
1798{
1799 unsigned int vendor_param;
1800
1801 if (action != HDA_FIXUP_ACT_PRE_PROBE)
1802 return;
1803 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1804 INTEL_GET_VENDOR_VERB, 0);
1805 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1806 return;
1807
1808 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1809 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1810 INTEL_SET_VENDOR_VERB, vendor_param);
1811 if (vendor_param == -1)
1812 return;
1813
1814 snd_hda_codec_update_widgets(codec);
1815 return;
1816}
1817
c88d4e84
TI
1818static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1819{
1820 unsigned int vendor_param;
1821
1822 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1823 INTEL_GET_VENDOR_VERB, 0);
1824 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1825 return;
1826
1827 /* enable DP1.2 mode */
1828 vendor_param |= INTEL_EN_DP12;
1829 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1830 INTEL_SET_VENDOR_VERB, vendor_param);
1831}
1832
1833
1611a9c9
ML
1834
1835/* available models for fixup */
1836enum {
1837 INTEL_HASWELL,
1838};
1839
1840static const struct hda_model_fixup hdmi_models[] = {
1841 {.id = INTEL_HASWELL, .name = "Haswell"},
1842 {}
1843};
1844
1845static const struct snd_pci_quirk hdmi_fixup_tbl[] = {
1846 SND_PCI_QUIRK(0x8086, 0x2010, "Haswell", INTEL_HASWELL),
1847 {} /* terminator */
1848};
1849
1850static const struct hda_fixup hdmi_fixups[] = {
1851 [INTEL_HASWELL] = {
1852 .type = HDA_FIXUP_FUNC,
1853 .v.func = intel_haswell_enable_all_pins,
1854 },
1855};
1856
6ffe168f 1857
84eb01be
TI
1858static int patch_generic_hdmi(struct hda_codec *codec)
1859{
1860 struct hdmi_spec *spec;
84eb01be
TI
1861
1862 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1863 if (spec == NULL)
1864 return -ENOMEM;
1865
1866 codec->spec = spec;
bce0d2a8 1867 hdmi_array_init(spec, 4);
6ffe168f 1868
1611a9c9
ML
1869 snd_hda_pick_fixup(codec, hdmi_models, hdmi_fixup_tbl, hdmi_fixups);
1870 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1871
6ffe168f 1872 if (codec->vendor_id == 0x80862807)
c88d4e84 1873 intel_haswell_fixup_enable_dp12(codec);
6ffe168f 1874
84eb01be
TI
1875 if (hdmi_parse_codec(codec) < 0) {
1876 codec->spec = NULL;
1877 kfree(spec);
1878 return -EINVAL;
1879 }
1880 codec->patch_ops = generic_hdmi_patch_ops;
8b8d654b 1881 generic_hdmi_init_per_pins(codec);
84eb01be 1882
84eb01be
TI
1883 init_channel_allocations();
1884
1885 return 0;
1886}
1887
3aaf8980
SW
1888/*
1889 * Shared non-generic implementations
1890 */
1891
1892static int simple_playback_build_pcms(struct hda_codec *codec)
1893{
1894 struct hdmi_spec *spec = codec->spec;
bce0d2a8 1895 struct hda_pcm *info;
8ceb332d
TI
1896 unsigned int chans;
1897 struct hda_pcm_stream *pstr;
bce0d2a8 1898 struct hdmi_spec_per_cvt *per_cvt;
3aaf8980 1899
bce0d2a8
TI
1900 per_cvt = get_cvt(spec, 0);
1901 chans = get_wcaps(codec, per_cvt->cvt_nid);
8ceb332d 1902 chans = get_wcaps_channels(chans);
3aaf8980 1903
bce0d2a8
TI
1904 info = snd_array_new(&spec->pcm_rec);
1905 if (!info)
1906 return -ENOMEM;
1907 info->name = get_pin(spec, 0)->pcm_name;
1908 sprintf(info->name, "HDMI 0");
8ceb332d
TI
1909 info->pcm_type = HDA_PCM_TYPE_HDMI;
1910 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1911 *pstr = spec->pcm_playback;
bce0d2a8 1912 pstr->nid = per_cvt->cvt_nid;
8ceb332d
TI
1913 if (pstr->channels_max <= 2 && chans && chans <= 16)
1914 pstr->channels_max = chans;
3aaf8980 1915
bce0d2a8
TI
1916 codec->num_pcms = 1;
1917 codec->pcm_info = info;
1918
3aaf8980
SW
1919 return 0;
1920}
1921
4b6ace9e
TI
1922/* unsolicited event for jack sensing */
1923static void simple_hdmi_unsol_event(struct hda_codec *codec,
1924 unsigned int res)
1925{
9dd8cf12 1926 snd_hda_jack_set_dirty_all(codec);
4b6ace9e
TI
1927 snd_hda_jack_report_sync(codec);
1928}
1929
1930/* generic_hdmi_build_jack can be used for simple_hdmi, too,
1931 * as long as spec->pins[] is set correctly
1932 */
1933#define simple_hdmi_build_jack generic_hdmi_build_jack
1934
3aaf8980
SW
1935static int simple_playback_build_controls(struct hda_codec *codec)
1936{
1937 struct hdmi_spec *spec = codec->spec;
bce0d2a8 1938 struct hdmi_spec_per_cvt *per_cvt;
3aaf8980 1939 int err;
3aaf8980 1940
bce0d2a8
TI
1941 per_cvt = get_cvt(spec, 0);
1942 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
1943 per_cvt->cvt_nid);
8ceb332d
TI
1944 if (err < 0)
1945 return err;
1946 return simple_hdmi_build_jack(codec, 0);
3aaf8980
SW
1947}
1948
4f0110ce
TI
1949static int simple_playback_init(struct hda_codec *codec)
1950{
1951 struct hdmi_spec *spec = codec->spec;
bce0d2a8
TI
1952 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
1953 hda_nid_t pin = per_pin->pin_nid;
8ceb332d
TI
1954
1955 snd_hda_codec_write(codec, pin, 0,
1956 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1957 /* some codecs require to unmute the pin */
1958 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
1959 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1960 AMP_OUT_UNMUTE);
1961 snd_hda_jack_detect_enable(codec, pin, pin);
4f0110ce
TI
1962 return 0;
1963}
1964
3aaf8980
SW
1965static void simple_playback_free(struct hda_codec *codec)
1966{
1967 struct hdmi_spec *spec = codec->spec;
1968
bce0d2a8 1969 hdmi_array_free(spec);
3aaf8980
SW
1970 kfree(spec);
1971}
1972
84eb01be
TI
1973/*
1974 * Nvidia specific implementations
1975 */
1976
1977#define Nv_VERB_SET_Channel_Allocation 0xF79
1978#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
1979#define Nv_VERB_SET_Audio_Protection_On 0xF98
1980#define Nv_VERB_SET_Audio_Protection_Off 0xF99
1981
1982#define nvhdmi_master_con_nid_7x 0x04
1983#define nvhdmi_master_pin_nid_7x 0x05
1984
fb79e1e0 1985static const hda_nid_t nvhdmi_con_nids_7x[4] = {
84eb01be
TI
1986 /*front, rear, clfe, rear_surr */
1987 0x6, 0x8, 0xa, 0xc,
1988};
1989
ceaa86ba
TI
1990static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
1991 /* set audio protect on */
1992 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1993 /* enable digital output on pin widget */
1994 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1995 {} /* terminator */
1996};
1997
1998static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
84eb01be
TI
1999 /* set audio protect on */
2000 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2001 /* enable digital output on pin widget */
2002 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2003 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2004 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2005 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2006 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2007 {} /* terminator */
2008};
2009
2010#ifdef LIMITED_RATE_FMT_SUPPORT
2011/* support only the safe format and rate */
2012#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2013#define SUPPORTED_MAXBPS 16
2014#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2015#else
2016/* support all rates and formats */
2017#define SUPPORTED_RATES \
2018 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2019 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2020 SNDRV_PCM_RATE_192000)
2021#define SUPPORTED_MAXBPS 24
2022#define SUPPORTED_FORMATS \
2023 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2024#endif
2025
ceaa86ba
TI
2026static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
2027{
2028 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2029 return 0;
2030}
2031
2032static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
84eb01be 2033{
ceaa86ba 2034 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
84eb01be
TI
2035 return 0;
2036}
2037
393004b2
ND
2038static unsigned int channels_2_6_8[] = {
2039 2, 6, 8
2040};
2041
2042static unsigned int channels_2_8[] = {
2043 2, 8
2044};
2045
2046static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2047 .count = ARRAY_SIZE(channels_2_6_8),
2048 .list = channels_2_6_8,
2049 .mask = 0,
2050};
2051
2052static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2053 .count = ARRAY_SIZE(channels_2_8),
2054 .list = channels_2_8,
2055 .mask = 0,
2056};
2057
84eb01be
TI
2058static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2059 struct hda_codec *codec,
2060 struct snd_pcm_substream *substream)
2061{
2062 struct hdmi_spec *spec = codec->spec;
393004b2
ND
2063 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2064
2065 switch (codec->preset->id) {
2066 case 0x10de0002:
2067 case 0x10de0003:
2068 case 0x10de0005:
2069 case 0x10de0006:
2070 hw_constraints_channels = &hw_constraints_2_8_channels;
2071 break;
2072 case 0x10de0007:
2073 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2074 break;
2075 default:
2076 break;
2077 }
2078
2079 if (hw_constraints_channels != NULL) {
2080 snd_pcm_hw_constraint_list(substream->runtime, 0,
2081 SNDRV_PCM_HW_PARAM_CHANNELS,
2082 hw_constraints_channels);
ad09fc9d
TI
2083 } else {
2084 snd_pcm_hw_constraint_step(substream->runtime, 0,
2085 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
393004b2
ND
2086 }
2087
84eb01be
TI
2088 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2089}
2090
2091static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2092 struct hda_codec *codec,
2093 struct snd_pcm_substream *substream)
2094{
2095 struct hdmi_spec *spec = codec->spec;
2096 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2097}
2098
2099static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2100 struct hda_codec *codec,
2101 unsigned int stream_tag,
2102 unsigned int format,
2103 struct snd_pcm_substream *substream)
2104{
2105 struct hdmi_spec *spec = codec->spec;
2106 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2107 stream_tag, format, substream);
2108}
2109
d0b1252d
TI
2110static const struct hda_pcm_stream simple_pcm_playback = {
2111 .substreams = 1,
2112 .channels_min = 2,
2113 .channels_max = 2,
2114 .ops = {
2115 .open = simple_playback_pcm_open,
2116 .close = simple_playback_pcm_close,
2117 .prepare = simple_playback_pcm_prepare
2118 },
2119};
2120
2121static const struct hda_codec_ops simple_hdmi_patch_ops = {
2122 .build_controls = simple_playback_build_controls,
2123 .build_pcms = simple_playback_build_pcms,
2124 .init = simple_playback_init,
2125 .free = simple_playback_free,
250e41ac 2126 .unsol_event = simple_hdmi_unsol_event,
d0b1252d
TI
2127};
2128
2129static int patch_simple_hdmi(struct hda_codec *codec,
2130 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2131{
2132 struct hdmi_spec *spec;
bce0d2a8
TI
2133 struct hdmi_spec_per_cvt *per_cvt;
2134 struct hdmi_spec_per_pin *per_pin;
d0b1252d
TI
2135
2136 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2137 if (!spec)
2138 return -ENOMEM;
2139
2140 codec->spec = spec;
bce0d2a8 2141 hdmi_array_init(spec, 1);
d0b1252d
TI
2142
2143 spec->multiout.num_dacs = 0; /* no analog */
2144 spec->multiout.max_channels = 2;
2145 spec->multiout.dig_out_nid = cvt_nid;
2146 spec->num_cvts = 1;
2147 spec->num_pins = 1;
bce0d2a8
TI
2148 per_pin = snd_array_new(&spec->pins);
2149 per_cvt = snd_array_new(&spec->cvts);
2150 if (!per_pin || !per_cvt) {
2151 simple_playback_free(codec);
2152 return -ENOMEM;
2153 }
2154 per_cvt->cvt_nid = cvt_nid;
2155 per_pin->pin_nid = pin_nid;
d0b1252d
TI
2156 spec->pcm_playback = simple_pcm_playback;
2157
2158 codec->patch_ops = simple_hdmi_patch_ops;
2159
2160 return 0;
2161}
2162
1f348522
AP
2163static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2164 int channels)
2165{
2166 unsigned int chanmask;
2167 int chan = channels ? (channels - 1) : 1;
2168
2169 switch (channels) {
2170 default:
2171 case 0:
2172 case 2:
2173 chanmask = 0x00;
2174 break;
2175 case 4:
2176 chanmask = 0x08;
2177 break;
2178 case 6:
2179 chanmask = 0x0b;
2180 break;
2181 case 8:
2182 chanmask = 0x13;
2183 break;
2184 }
2185
2186 /* Set the audio infoframe channel allocation and checksum fields. The
2187 * channel count is computed implicitly by the hardware. */
2188 snd_hda_codec_write(codec, 0x1, 0,
2189 Nv_VERB_SET_Channel_Allocation, chanmask);
2190
2191 snd_hda_codec_write(codec, 0x1, 0,
2192 Nv_VERB_SET_Info_Frame_Checksum,
2193 (0x71 - chan - chanmask));
2194}
2195
84eb01be
TI
2196static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2197 struct hda_codec *codec,
2198 struct snd_pcm_substream *substream)
2199{
2200 struct hdmi_spec *spec = codec->spec;
2201 int i;
2202
2203 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2204 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2205 for (i = 0; i < 4; i++) {
2206 /* set the stream id */
2207 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2208 AC_VERB_SET_CHANNEL_STREAMID, 0);
2209 /* set the stream format */
2210 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2211 AC_VERB_SET_STREAM_FORMAT, 0);
2212 }
2213
1f348522
AP
2214 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2215 * streams are disabled. */
2216 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2217
84eb01be
TI
2218 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2219}
2220
2221static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2222 struct hda_codec *codec,
2223 unsigned int stream_tag,
2224 unsigned int format,
2225 struct snd_pcm_substream *substream)
2226{
2227 int chs;
112daa7a 2228 unsigned int dataDCC2, channel_id;
84eb01be 2229 int i;
7c935976 2230 struct hdmi_spec *spec = codec->spec;
e3245cdd 2231 struct hda_spdif_out *spdif;
bce0d2a8 2232 struct hdmi_spec_per_cvt *per_cvt;
84eb01be
TI
2233
2234 mutex_lock(&codec->spdif_mutex);
bce0d2a8
TI
2235 per_cvt = get_cvt(spec, 0);
2236 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
84eb01be
TI
2237
2238 chs = substream->runtime->channels;
84eb01be 2239
84eb01be
TI
2240 dataDCC2 = 0x2;
2241
84eb01be 2242 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
7c935976 2243 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
84eb01be
TI
2244 snd_hda_codec_write(codec,
2245 nvhdmi_master_con_nid_7x,
2246 0,
2247 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 2248 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
84eb01be
TI
2249
2250 /* set the stream id */
2251 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2252 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2253
2254 /* set the stream format */
2255 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2256 AC_VERB_SET_STREAM_FORMAT, format);
2257
2258 /* turn on again (if needed) */
2259 /* enable and set the channel status audio/data flag */
7c935976 2260 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
84eb01be
TI
2261 snd_hda_codec_write(codec,
2262 nvhdmi_master_con_nid_7x,
2263 0,
2264 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 2265 spdif->ctls & 0xff);
84eb01be
TI
2266 snd_hda_codec_write(codec,
2267 nvhdmi_master_con_nid_7x,
2268 0,
2269 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2270 }
2271
2272 for (i = 0; i < 4; i++) {
2273 if (chs == 2)
2274 channel_id = 0;
2275 else
2276 channel_id = i * 2;
2277
2278 /* turn off SPDIF once;
2279 *otherwise the IEC958 bits won't be updated
2280 */
2281 if (codec->spdif_status_reset &&
7c935976 2282 (spdif->ctls & AC_DIG1_ENABLE))
84eb01be
TI
2283 snd_hda_codec_write(codec,
2284 nvhdmi_con_nids_7x[i],
2285 0,
2286 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 2287 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
84eb01be
TI
2288 /* set the stream id */
2289 snd_hda_codec_write(codec,
2290 nvhdmi_con_nids_7x[i],
2291 0,
2292 AC_VERB_SET_CHANNEL_STREAMID,
2293 (stream_tag << 4) | channel_id);
2294 /* set the stream format */
2295 snd_hda_codec_write(codec,
2296 nvhdmi_con_nids_7x[i],
2297 0,
2298 AC_VERB_SET_STREAM_FORMAT,
2299 format);
2300 /* turn on again (if needed) */
2301 /* enable and set the channel status audio/data flag */
2302 if (codec->spdif_status_reset &&
7c935976 2303 (spdif->ctls & AC_DIG1_ENABLE)) {
84eb01be
TI
2304 snd_hda_codec_write(codec,
2305 nvhdmi_con_nids_7x[i],
2306 0,
2307 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 2308 spdif->ctls & 0xff);
84eb01be
TI
2309 snd_hda_codec_write(codec,
2310 nvhdmi_con_nids_7x[i],
2311 0,
2312 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2313 }
2314 }
2315
1f348522 2316 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
84eb01be
TI
2317
2318 mutex_unlock(&codec->spdif_mutex);
2319 return 0;
2320}
2321
fb79e1e0 2322static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
84eb01be
TI
2323 .substreams = 1,
2324 .channels_min = 2,
2325 .channels_max = 8,
2326 .nid = nvhdmi_master_con_nid_7x,
2327 .rates = SUPPORTED_RATES,
2328 .maxbps = SUPPORTED_MAXBPS,
2329 .formats = SUPPORTED_FORMATS,
2330 .ops = {
2331 .open = simple_playback_pcm_open,
2332 .close = nvhdmi_8ch_7x_pcm_close,
2333 .prepare = nvhdmi_8ch_7x_pcm_prepare
2334 },
2335};
2336
84eb01be
TI
2337static int patch_nvhdmi_2ch(struct hda_codec *codec)
2338{
2339 struct hdmi_spec *spec;
d0b1252d
TI
2340 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2341 nvhdmi_master_pin_nid_7x);
2342 if (err < 0)
2343 return err;
84eb01be 2344
ceaa86ba 2345 codec->patch_ops.init = nvhdmi_7x_init_2ch;
d0b1252d
TI
2346 /* override the PCM rates, etc, as the codec doesn't give full list */
2347 spec = codec->spec;
2348 spec->pcm_playback.rates = SUPPORTED_RATES;
2349 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2350 spec->pcm_playback.formats = SUPPORTED_FORMATS;
84eb01be
TI
2351 return 0;
2352}
2353
53775b0d
TI
2354static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2355{
2356 struct hdmi_spec *spec = codec->spec;
2357 int err = simple_playback_build_pcms(codec);
bce0d2a8
TI
2358 if (!err) {
2359 struct hda_pcm *info = get_pcm_rec(spec, 0);
2360 info->own_chmap = true;
2361 }
53775b0d
TI
2362 return err;
2363}
2364
2365static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2366{
2367 struct hdmi_spec *spec = codec->spec;
bce0d2a8 2368 struct hda_pcm *info;
53775b0d
TI
2369 struct snd_pcm_chmap *chmap;
2370 int err;
2371
2372 err = simple_playback_build_controls(codec);
2373 if (err < 0)
2374 return err;
2375
2376 /* add channel maps */
bce0d2a8
TI
2377 info = get_pcm_rec(spec, 0);
2378 err = snd_pcm_add_chmap_ctls(info->pcm,
53775b0d
TI
2379 SNDRV_PCM_STREAM_PLAYBACK,
2380 snd_pcm_alt_chmaps, 8, 0, &chmap);
2381 if (err < 0)
2382 return err;
2383 switch (codec->preset->id) {
2384 case 0x10de0002:
2385 case 0x10de0003:
2386 case 0x10de0005:
2387 case 0x10de0006:
2388 chmap->channel_mask = (1U << 2) | (1U << 8);
2389 break;
2390 case 0x10de0007:
2391 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2392 }
2393 return 0;
2394}
2395
84eb01be
TI
2396static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2397{
2398 struct hdmi_spec *spec;
2399 int err = patch_nvhdmi_2ch(codec);
84eb01be
TI
2400 if (err < 0)
2401 return err;
2402 spec = codec->spec;
2403 spec->multiout.max_channels = 8;
d0b1252d 2404 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
ceaa86ba 2405 codec->patch_ops.init = nvhdmi_7x_init_8ch;
53775b0d
TI
2406 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2407 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
1f348522
AP
2408
2409 /* Initialize the audio infoframe channel mask and checksum to something
2410 * valid */
2411 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2412
84eb01be
TI
2413 return 0;
2414}
2415
2416/*
2417 * ATI-specific implementations
2418 *
2419 * FIXME: we may omit the whole this and use the generic code once after
2420 * it's confirmed to work.
2421 */
2422
2423#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2424#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2425
2426static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2427 struct hda_codec *codec,
2428 unsigned int stream_tag,
2429 unsigned int format,
2430 struct snd_pcm_substream *substream)
2431{
2432 struct hdmi_spec *spec = codec->spec;
bce0d2a8 2433 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
84eb01be
TI
2434 int chans = substream->runtime->channels;
2435 int i, err;
2436
2437 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2438 substream);
2439 if (err < 0)
2440 return err;
bce0d2a8 2441 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
384a48d7 2442 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
84eb01be
TI
2443 /* FIXME: XXX */
2444 for (i = 0; i < chans; i++) {
bce0d2a8 2445 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
84eb01be
TI
2446 AC_VERB_SET_HDMI_CHAN_SLOT,
2447 (i << 4) | i);
2448 }
2449 return 0;
2450}
2451
84eb01be
TI
2452static int patch_atihdmi(struct hda_codec *codec)
2453{
2454 struct hdmi_spec *spec;
d0b1252d
TI
2455 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2456 if (err < 0)
2457 return err;
2458 spec = codec->spec;
2459 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
84eb01be
TI
2460 return 0;
2461}
2462
3de5ff88
AL
2463/* VIA HDMI Implementation */
2464#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2465#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2466
3de5ff88
AL
2467static int patch_via_hdmi(struct hda_codec *codec)
2468{
250e41ac 2469 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
3de5ff88 2470}
84eb01be
TI
2471
2472/*
2473 * patch entries
2474 */
fb79e1e0 2475static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
84eb01be
TI
2476{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2477{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2478{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
36e9c135 2479{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
84eb01be
TI
2480{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2481{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2482{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2483{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2484{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2485{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2486{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2487{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
5d44f927
SW
2488{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2489{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2490{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2491{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2492{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2493{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2494{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2495{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2496{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2497{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2498{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
c8900a0f 2499/* 17 is known to be absent */
5d44f927
SW
2500{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2501{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2502{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2503{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2504{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2505{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2506{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2507{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2508{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2509{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
7ae48b56 2510{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
84eb01be
TI
2511{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2512{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
3de5ff88
AL
2513{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2514{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2515{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2516{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
84eb01be
TI
2517{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2518{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2519{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2520{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2521{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2522{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
591e610d 2523{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
1c76684d 2524{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
6edc59e6 2525{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
84eb01be
TI
2526{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2527{} /* terminator */
2528};
2529
2530MODULE_ALIAS("snd-hda-codec-id:1002793c");
2531MODULE_ALIAS("snd-hda-codec-id:10027919");
2532MODULE_ALIAS("snd-hda-codec-id:1002791a");
2533MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2534MODULE_ALIAS("snd-hda-codec-id:10951390");
2535MODULE_ALIAS("snd-hda-codec-id:10951392");
2536MODULE_ALIAS("snd-hda-codec-id:10de0002");
2537MODULE_ALIAS("snd-hda-codec-id:10de0003");
2538MODULE_ALIAS("snd-hda-codec-id:10de0005");
2539MODULE_ALIAS("snd-hda-codec-id:10de0006");
2540MODULE_ALIAS("snd-hda-codec-id:10de0007");
2541MODULE_ALIAS("snd-hda-codec-id:10de000a");
2542MODULE_ALIAS("snd-hda-codec-id:10de000b");
2543MODULE_ALIAS("snd-hda-codec-id:10de000c");
2544MODULE_ALIAS("snd-hda-codec-id:10de000d");
2545MODULE_ALIAS("snd-hda-codec-id:10de0010");
2546MODULE_ALIAS("snd-hda-codec-id:10de0011");
2547MODULE_ALIAS("snd-hda-codec-id:10de0012");
2548MODULE_ALIAS("snd-hda-codec-id:10de0013");
2549MODULE_ALIAS("snd-hda-codec-id:10de0014");
c8900a0f
RS
2550MODULE_ALIAS("snd-hda-codec-id:10de0015");
2551MODULE_ALIAS("snd-hda-codec-id:10de0016");
84eb01be
TI
2552MODULE_ALIAS("snd-hda-codec-id:10de0018");
2553MODULE_ALIAS("snd-hda-codec-id:10de0019");
2554MODULE_ALIAS("snd-hda-codec-id:10de001a");
2555MODULE_ALIAS("snd-hda-codec-id:10de001b");
2556MODULE_ALIAS("snd-hda-codec-id:10de001c");
2557MODULE_ALIAS("snd-hda-codec-id:10de0040");
2558MODULE_ALIAS("snd-hda-codec-id:10de0041");
2559MODULE_ALIAS("snd-hda-codec-id:10de0042");
2560MODULE_ALIAS("snd-hda-codec-id:10de0043");
2561MODULE_ALIAS("snd-hda-codec-id:10de0044");
7ae48b56 2562MODULE_ALIAS("snd-hda-codec-id:10de0051");
84eb01be
TI
2563MODULE_ALIAS("snd-hda-codec-id:10de0067");
2564MODULE_ALIAS("snd-hda-codec-id:10de8001");
3de5ff88
AL
2565MODULE_ALIAS("snd-hda-codec-id:11069f80");
2566MODULE_ALIAS("snd-hda-codec-id:11069f81");
2567MODULE_ALIAS("snd-hda-codec-id:11069f84");
2568MODULE_ALIAS("snd-hda-codec-id:11069f85");
84eb01be
TI
2569MODULE_ALIAS("snd-hda-codec-id:17e80047");
2570MODULE_ALIAS("snd-hda-codec-id:80860054");
2571MODULE_ALIAS("snd-hda-codec-id:80862801");
2572MODULE_ALIAS("snd-hda-codec-id:80862802");
2573MODULE_ALIAS("snd-hda-codec-id:80862803");
2574MODULE_ALIAS("snd-hda-codec-id:80862804");
2575MODULE_ALIAS("snd-hda-codec-id:80862805");
591e610d 2576MODULE_ALIAS("snd-hda-codec-id:80862806");
1c76684d 2577MODULE_ALIAS("snd-hda-codec-id:80862807");
6edc59e6 2578MODULE_ALIAS("snd-hda-codec-id:80862880");
84eb01be
TI
2579MODULE_ALIAS("snd-hda-codec-id:808629fb");
2580
2581MODULE_LICENSE("GPL");
2582MODULE_DESCRIPTION("HDMI HD-audio codec");
2583MODULE_ALIAS("snd-hda-codec-intelhdmi");
2584MODULE_ALIAS("snd-hda-codec-nvhdmi");
2585MODULE_ALIAS("snd-hda-codec-atihdmi");
2586
2587static struct hda_codec_preset_list intel_list = {
2588 .preset = snd_hda_preset_hdmi,
2589 .owner = THIS_MODULE,
2590};
2591
2592static int __init patch_hdmi_init(void)
2593{
2594 return snd_hda_add_codec_preset(&intel_list);
2595}
2596
2597static void __exit patch_hdmi_exit(void)
2598{
2599 snd_hda_delete_codec_preset(&intel_list);
2600}
2601
2602module_init(patch_hdmi_init)
2603module_exit(patch_hdmi_exit)