ALSA: hda - Really restrict i915 notifier to HSW+
[deliverable/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>
5a613584 9 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
079d88cc
WF
10 *
11 * Authors:
12 * Wu Fengguang <wfg@linux.intel.com>
13 *
14 * Maintained by:
15 * Wu Fengguang <wfg@linux.intel.com>
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
20 * any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software Foundation,
29 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
84eb01be
TI
32#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/slab.h>
65a77217 35#include <linux/module.h>
84eb01be 36#include <sound/core.h>
07acecc1 37#include <sound/jack.h>
433968da 38#include <sound/asoundef.h>
d45e6889 39#include <sound/tlv.h>
25adc137
DH
40#include <sound/hdaudio.h>
41#include <sound/hda_i915.h>
67b90cb8 42#include <sound/hda_chmap.h>
84eb01be
TI
43#include "hda_codec.h"
44#include "hda_local.h"
1835a0f9 45#include "hda_jack.h"
84eb01be 46
0ebaa24c
TI
47static bool static_hdmi_pcm;
48module_param(static_hdmi_pcm, bool, 0644);
49MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
50
7639a06c
TI
51#define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
52#define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
53#define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
e2656412 54#define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
91815d8a 55#define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
432ac1a2 56#define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
91815d8a
LY
57 || is_skylake(codec) || is_broxton(codec) \
58 || is_kabylake(codec))
75dcbe4d 59
7639a06c
TI
60#define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
61#define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
ca2e7224 62#define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
fb87fa3a 63
384a48d7
SW
64struct hdmi_spec_per_cvt {
65 hda_nid_t cvt_nid;
66 int assigned;
67 unsigned int channels_min;
68 unsigned int channels_max;
69 u32 rates;
70 u64 formats;
71 unsigned int maxbps;
72};
079d88cc 73
4eea3091
TI
74/* max. connections to a widget */
75#define HDA_MAX_CONNECTIONS 32
76
384a48d7
SW
77struct hdmi_spec_per_pin {
78 hda_nid_t pin_nid;
a76056f2
LY
79 /* pin idx, different device entries on the same pin use the same idx */
80 int pin_nid_idx;
384a48d7
SW
81 int num_mux_nids;
82 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
2df6742f 83 int mux_idx;
1df5a06a 84 hda_nid_t cvt_nid;
744626da
WF
85
86 struct hda_codec *codec;
384a48d7 87 struct hdmi_eld sink_eld;
a4e9a38b 88 struct mutex lock;
744626da 89 struct delayed_work work;
2bea241a 90 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
a76056f2 91 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
c6e8453e 92 int repoll_count;
b054087d
TI
93 bool setup; /* the stream has been set up by prepare callback */
94 int channels; /* current number of channels */
1a6003b5 95 bool non_pcm;
d45e6889
TI
96 bool chmap_set; /* channel-map override by ALSA API? */
97 unsigned char chmap[8]; /* ALSA API channel-map */
cd6a6503 98#ifdef CONFIG_SND_PROC_FS
a4e9a38b
TI
99 struct snd_info_entry *proc_entry;
100#endif
384a48d7 101};
079d88cc 102
307229d2
AH
103/* operations used by generic code that can be overridden by patches */
104struct hdmi_ops {
105 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
106 unsigned char *buf, int *eld_size);
107
307229d2
AH
108 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
109 int ca, int active_channels, int conn_type);
110
111 /* enable/disable HBR (HD passthrough) */
112 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
113
114 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
115 hda_nid_t pin_nid, u32 stream_tag, int format);
116
307229d2
AH
117};
118
2bea241a
LY
119struct hdmi_pcm {
120 struct hda_pcm *pcm;
121 struct snd_jack *jack;
fb087eaa 122 struct snd_kcontrol *eld_ctl;
2bea241a
LY
123};
124
384a48d7
SW
125struct hdmi_spec {
126 int num_cvts;
bce0d2a8
TI
127 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
128 hda_nid_t cvt_nids[4]; /* only for haswell fix */
079d88cc 129
384a48d7 130 int num_pins;
bce0d2a8 131 struct snd_array pins; /* struct hdmi_spec_per_pin */
2bea241a 132 struct hdmi_pcm pcm_rec[16];
42b29870 133 struct mutex pcm_lock;
a76056f2
LY
134 /* pcm_bitmap means which pcms have been assigned to pins*/
135 unsigned long pcm_bitmap;
2bf3c85a 136 int pcm_used; /* counter of pcm_rec[] */
ac98379a
LY
137 /* bitmap shows whether the pcm is opened in user space
138 * bit 0 means the first playback PCM (PCM3);
139 * bit 1 means the second playback PCM, and so on.
140 */
141 unsigned long pcm_in_use;
079d88cc 142
4bd038f9 143 struct hdmi_eld temp_eld;
307229d2 144 struct hdmi_ops ops;
75fae117
SW
145
146 bool dyn_pin_out;
6590faab 147 bool dyn_pcm_assign;
079d88cc 148 /*
5a613584 149 * Non-generic VIA/NVIDIA specific
079d88cc
WF
150 */
151 struct hda_multi_out multiout;
d0b1252d 152 struct hda_pcm_stream pcm_playback;
25adc137
DH
153
154 /* i915/powerwell (Haswell+/Valleyview+) specific */
691be973 155 bool use_acomp_notifier; /* use i915 eld_notify callback for hotplug */
25adc137 156 struct i915_audio_component_audio_ops i915_audio_ops;
55913110 157 bool i915_bound; /* was i915 bound in this driver? */
67b90cb8
SP
158
159 struct hdac_chmap chmap;
079d88cc
WF
160};
161
f4e3040b 162#ifdef CONFIG_SND_HDA_I915
691be973
TI
163static inline bool codec_has_acomp(struct hda_codec *codec)
164{
165 struct hdmi_spec *spec = codec->spec;
166 return spec->use_acomp_notifier;
167}
f4e3040b
TI
168#else
169#define codec_has_acomp(codec) false
170#endif
079d88cc
WF
171
172struct hdmi_audio_infoframe {
173 u8 type; /* 0x84 */
174 u8 ver; /* 0x01 */
175 u8 len; /* 0x0a */
176
53d7d69d
WF
177 u8 checksum;
178
079d88cc
WF
179 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
180 u8 SS01_SF24;
181 u8 CXT04;
182 u8 CA;
183 u8 LFEPBL01_LSV36_DM_INH7;
53d7d69d
WF
184};
185
186struct dp_audio_infoframe {
187 u8 type; /* 0x84 */
188 u8 len; /* 0x1b */
189 u8 ver; /* 0x11 << 2 */
190
191 u8 CC02_CT47; /* match with HDMI infoframe from this on */
192 u8 SS01_SF24;
193 u8 CXT04;
194 u8 CA;
195 u8 LFEPBL01_LSV36_DM_INH7;
079d88cc
WF
196};
197
2b203dbb
TI
198union audio_infoframe {
199 struct hdmi_audio_infoframe hdmi;
200 struct dp_audio_infoframe dp;
201 u8 bytes[0];
202};
203
079d88cc
WF
204/*
205 * HDMI routines
206 */
207
bce0d2a8
TI
208#define get_pin(spec, idx) \
209 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
210#define get_cvt(spec, idx) \
211 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
2bea241a
LY
212/* obtain hdmi_pcm object assigned to idx */
213#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
214/* obtain hda_pcm object assigned to idx */
215#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
bce0d2a8 216
4e76a883 217static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
079d88cc 218{
4e76a883 219 struct hdmi_spec *spec = codec->spec;
384a48d7 220 int pin_idx;
079d88cc 221
384a48d7 222 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
bce0d2a8 223 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
384a48d7 224 return pin_idx;
079d88cc 225
4e76a883 226 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
384a48d7
SW
227 return -EINVAL;
228}
229
2bf3c85a
LY
230static int hinfo_to_pcm_index(struct hda_codec *codec,
231 struct hda_pcm_stream *hinfo)
232{
233 struct hdmi_spec *spec = codec->spec;
234 int pcm_idx;
235
236 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
237 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
238 return pcm_idx;
239
240 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
241 return -EINVAL;
242}
243
4e76a883 244static int hinfo_to_pin_index(struct hda_codec *codec,
384a48d7
SW
245 struct hda_pcm_stream *hinfo)
246{
4e76a883 247 struct hdmi_spec *spec = codec->spec;
6590faab 248 struct hdmi_spec_per_pin *per_pin;
384a48d7
SW
249 int pin_idx;
250
6590faab
LY
251 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
252 per_pin = get_pin(spec, pin_idx);
2bea241a
LY
253 if (per_pin->pcm &&
254 per_pin->pcm->pcm->stream == hinfo)
384a48d7 255 return pin_idx;
6590faab 256 }
384a48d7 257
6590faab 258 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
384a48d7
SW
259 return -EINVAL;
260}
261
022f344b
LY
262static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
263 int pcm_idx)
264{
265 int i;
266 struct hdmi_spec_per_pin *per_pin;
267
268 for (i = 0; i < spec->num_pins; i++) {
269 per_pin = get_pin(spec, i);
270 if (per_pin->pcm_idx == pcm_idx)
271 return per_pin;
272 }
273 return NULL;
274}
275
4e76a883 276static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
384a48d7 277{
4e76a883 278 struct hdmi_spec *spec = codec->spec;
384a48d7
SW
279 int cvt_idx;
280
281 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
bce0d2a8 282 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
384a48d7
SW
283 return cvt_idx;
284
4e76a883 285 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
079d88cc
WF
286 return -EINVAL;
287}
288
14bc52b8
PLB
289static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
290 struct snd_ctl_elem_info *uinfo)
291{
292 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
68e03de9 293 struct hdmi_spec *spec = codec->spec;
a4e9a38b 294 struct hdmi_spec_per_pin *per_pin;
68e03de9 295 struct hdmi_eld *eld;
fb087eaa 296 int pcm_idx;
14bc52b8 297
14bc52b8
PLB
298 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
299
fb087eaa
LY
300 pcm_idx = kcontrol->private_value;
301 mutex_lock(&spec->pcm_lock);
302 per_pin = pcm_idx_to_pin(spec, pcm_idx);
303 if (!per_pin) {
304 /* no pin is bound to the pcm */
305 uinfo->count = 0;
306 mutex_unlock(&spec->pcm_lock);
307 return 0;
308 }
a4e9a38b 309 eld = &per_pin->sink_eld;
68e03de9 310 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
fb087eaa 311 mutex_unlock(&spec->pcm_lock);
14bc52b8
PLB
312
313 return 0;
314}
315
316static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
317 struct snd_ctl_elem_value *ucontrol)
318{
319 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
68e03de9 320 struct hdmi_spec *spec = codec->spec;
a4e9a38b 321 struct hdmi_spec_per_pin *per_pin;
68e03de9 322 struct hdmi_eld *eld;
fb087eaa 323 int pcm_idx;
14bc52b8 324
fb087eaa
LY
325 pcm_idx = kcontrol->private_value;
326 mutex_lock(&spec->pcm_lock);
327 per_pin = pcm_idx_to_pin(spec, pcm_idx);
328 if (!per_pin) {
329 /* no pin is bound to the pcm */
330 memset(ucontrol->value.bytes.data, 0,
331 ARRAY_SIZE(ucontrol->value.bytes.data));
332 mutex_unlock(&spec->pcm_lock);
333 return 0;
334 }
a4e9a38b 335 eld = &per_pin->sink_eld;
68e03de9 336
360a8245
DH
337 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
338 eld->eld_size > ELD_MAX_SIZE) {
fb087eaa 339 mutex_unlock(&spec->pcm_lock);
68e03de9
DH
340 snd_BUG();
341 return -EINVAL;
342 }
343
344 memset(ucontrol->value.bytes.data, 0,
345 ARRAY_SIZE(ucontrol->value.bytes.data));
346 if (eld->eld_valid)
347 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
348 eld->eld_size);
fb087eaa 349 mutex_unlock(&spec->pcm_lock);
14bc52b8
PLB
350
351 return 0;
352}
353
354static struct snd_kcontrol_new eld_bytes_ctl = {
355 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
356 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
357 .name = "ELD",
358 .info = hdmi_eld_ctl_info,
359 .get = hdmi_eld_ctl_get,
360};
361
fb087eaa 362static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
14bc52b8
PLB
363 int device)
364{
365 struct snd_kcontrol *kctl;
366 struct hdmi_spec *spec = codec->spec;
367 int err;
368
369 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
370 if (!kctl)
371 return -ENOMEM;
fb087eaa 372 kctl->private_value = pcm_idx;
14bc52b8
PLB
373 kctl->id.device = device;
374
fb087eaa
LY
375 /* no pin nid is associated with the kctl now
376 * tbd: associate pin nid to eld ctl later
377 */
378 err = snd_hda_ctl_add(codec, 0, kctl);
14bc52b8
PLB
379 if (err < 0)
380 return err;
381
fb087eaa 382 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
14bc52b8
PLB
383 return 0;
384}
385
079d88cc
WF
386#ifdef BE_PARANOID
387static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
388 int *packet_index, int *byte_index)
389{
390 int val;
391
392 val = snd_hda_codec_read(codec, pin_nid, 0,
393 AC_VERB_GET_HDMI_DIP_INDEX, 0);
394
395 *packet_index = val >> 5;
396 *byte_index = val & 0x1f;
397}
398#endif
399
400static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
401 int packet_index, int byte_index)
402{
403 int val;
404
405 val = (packet_index << 5) | (byte_index & 0x1f);
406
407 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
408}
409
410static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
411 unsigned char val)
412{
413 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
414}
415
384a48d7 416static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
079d88cc 417{
75fae117
SW
418 struct hdmi_spec *spec = codec->spec;
419 int pin_out;
420
079d88cc
WF
421 /* Unmute */
422 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
423 snd_hda_codec_write(codec, pin_nid, 0,
424 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
75fae117
SW
425
426 if (spec->dyn_pin_out)
427 /* Disable pin out until stream is active */
428 pin_out = 0;
429 else
430 /* Enable pin out: some machines with GM965 gets broken output
431 * when the pin is disabled or changed while using with HDMI
432 */
433 pin_out = PIN_OUT;
434
079d88cc 435 snd_hda_codec_write(codec, pin_nid, 0,
75fae117 436 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
079d88cc
WF
437}
438
a4e9a38b
TI
439/*
440 * ELD proc files
441 */
442
cd6a6503 443#ifdef CONFIG_SND_PROC_FS
a4e9a38b
TI
444static void print_eld_info(struct snd_info_entry *entry,
445 struct snd_info_buffer *buffer)
446{
447 struct hdmi_spec_per_pin *per_pin = entry->private_data;
448
449 mutex_lock(&per_pin->lock);
450 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
451 mutex_unlock(&per_pin->lock);
452}
453
454static void write_eld_info(struct snd_info_entry *entry,
455 struct snd_info_buffer *buffer)
456{
457 struct hdmi_spec_per_pin *per_pin = entry->private_data;
458
459 mutex_lock(&per_pin->lock);
460 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
461 mutex_unlock(&per_pin->lock);
462}
463
464static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
465{
466 char name[32];
467 struct hda_codec *codec = per_pin->codec;
468 struct snd_info_entry *entry;
469 int err;
470
471 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
6efdd851 472 err = snd_card_proc_new(codec->card, name, &entry);
a4e9a38b
TI
473 if (err < 0)
474 return err;
475
476 snd_info_set_text_ops(entry, per_pin, print_eld_info);
477 entry->c.text.write = write_eld_info;
478 entry->mode |= S_IWUSR;
479 per_pin->proc_entry = entry;
480
481 return 0;
482}
483
484static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
485{
1947a114 486 if (!per_pin->codec->bus->shutdown) {
c560a679 487 snd_info_free_entry(per_pin->proc_entry);
a4e9a38b
TI
488 per_pin->proc_entry = NULL;
489 }
490}
491#else
b55447a7
TI
492static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
493 int index)
a4e9a38b
TI
494{
495 return 0;
496}
b55447a7 497static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
a4e9a38b
TI
498{
499}
500#endif
079d88cc 501
079d88cc
WF
502/*
503 * Audio InfoFrame routines
504 */
505
506/*
507 * Enable Audio InfoFrame Transmission
508 */
509static void hdmi_start_infoframe_trans(struct hda_codec *codec,
510 hda_nid_t pin_nid)
511{
512 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
513 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
514 AC_DIPXMIT_BEST);
515}
516
517/*
518 * Disable Audio InfoFrame Transmission
519 */
520static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
521 hda_nid_t pin_nid)
522{
523 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
524 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
525 AC_DIPXMIT_DISABLE);
526}
527
528static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
529{
530#ifdef CONFIG_SND_DEBUG_VERBOSE
531 int i;
532 int size;
533
534 size = snd_hdmi_get_eld_size(codec, pin_nid);
4e76a883 535 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
079d88cc
WF
536
537 for (i = 0; i < 8; i++) {
538 size = snd_hda_codec_read(codec, pin_nid, 0,
539 AC_VERB_GET_HDMI_DIP_SIZE, i);
4e76a883 540 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
079d88cc
WF
541 }
542#endif
543}
544
545static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
546{
547#ifdef BE_PARANOID
548 int i, j;
549 int size;
550 int pi, bi;
551 for (i = 0; i < 8; i++) {
552 size = snd_hda_codec_read(codec, pin_nid, 0,
553 AC_VERB_GET_HDMI_DIP_SIZE, i);
554 if (size == 0)
555 continue;
556
557 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
558 for (j = 1; j < 1000; j++) {
559 hdmi_write_dip_byte(codec, pin_nid, 0x0);
560 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
561 if (pi != i)
4e76a883 562 codec_dbg(codec, "dip index %d: %d != %d\n",
079d88cc
WF
563 bi, pi, i);
564 if (bi == 0) /* byte index wrapped around */
565 break;
566 }
4e76a883 567 codec_dbg(codec,
079d88cc
WF
568 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
569 i, size, j);
570 }
571#endif
572}
573
53d7d69d 574static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
079d88cc 575{
53d7d69d 576 u8 *bytes = (u8 *)hdmi_ai;
079d88cc
WF
577 u8 sum = 0;
578 int i;
579
53d7d69d 580 hdmi_ai->checksum = 0;
079d88cc 581
53d7d69d 582 for (i = 0; i < sizeof(*hdmi_ai); i++)
079d88cc
WF
583 sum += bytes[i];
584
53d7d69d 585 hdmi_ai->checksum = -sum;
079d88cc
WF
586}
587
588static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
589 hda_nid_t pin_nid,
53d7d69d 590 u8 *dip, int size)
079d88cc 591{
079d88cc
WF
592 int i;
593
594 hdmi_debug_dip_size(codec, pin_nid);
595 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
596
079d88cc 597 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
53d7d69d
WF
598 for (i = 0; i < size; i++)
599 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
079d88cc
WF
600}
601
602static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
53d7d69d 603 u8 *dip, int size)
079d88cc 604{
079d88cc
WF
605 u8 val;
606 int i;
607
608 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
609 != AC_DIPXMIT_BEST)
610 return false;
611
612 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
53d7d69d 613 for (i = 0; i < size; i++) {
079d88cc
WF
614 val = snd_hda_codec_read(codec, pin_nid, 0,
615 AC_VERB_GET_HDMI_DIP_DATA, 0);
53d7d69d 616 if (val != dip[i])
079d88cc
WF
617 return false;
618 }
619
620 return true;
621}
622
307229d2
AH
623static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
624 hda_nid_t pin_nid,
625 int ca, int active_channels,
626 int conn_type)
627{
628 union audio_infoframe ai;
629
caaf5ef9 630 memset(&ai, 0, sizeof(ai));
307229d2
AH
631 if (conn_type == 0) { /* HDMI */
632 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
633
634 hdmi_ai->type = 0x84;
635 hdmi_ai->ver = 0x01;
636 hdmi_ai->len = 0x0a;
637 hdmi_ai->CC02_CT47 = active_channels - 1;
638 hdmi_ai->CA = ca;
639 hdmi_checksum_audio_infoframe(hdmi_ai);
640 } else if (conn_type == 1) { /* DisplayPort */
641 struct dp_audio_infoframe *dp_ai = &ai.dp;
642
643 dp_ai->type = 0x84;
644 dp_ai->len = 0x1b;
645 dp_ai->ver = 0x11 << 2;
646 dp_ai->CC02_CT47 = active_channels - 1;
647 dp_ai->CA = ca;
648 } else {
4e76a883 649 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
307229d2
AH
650 pin_nid);
651 return;
652 }
653
654 /*
655 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
656 * sizeof(*dp_ai) to avoid partial match/update problems when
657 * the user switches between HDMI/DP monitors.
658 */
659 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
660 sizeof(ai))) {
4e76a883
TI
661 codec_dbg(codec,
662 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
307229d2
AH
663 pin_nid,
664 active_channels, ca);
665 hdmi_stop_infoframe_trans(codec, pin_nid);
666 hdmi_fill_audio_infoframe(codec, pin_nid,
667 ai.bytes, sizeof(ai));
668 hdmi_start_infoframe_trans(codec, pin_nid);
669 }
670}
671
b054087d
TI
672static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
673 struct hdmi_spec_per_pin *per_pin,
674 bool non_pcm)
079d88cc 675{
307229d2 676 struct hdmi_spec *spec = codec->spec;
739ffee9 677 struct hdac_chmap *chmap = &spec->chmap;
384a48d7 678 hda_nid_t pin_nid = per_pin->pin_nid;
b054087d 679 int channels = per_pin->channels;
1df5a06a 680 int active_channels;
384a48d7 681 struct hdmi_eld *eld;
828cb4ed 682 int ca;
079d88cc 683
b054087d
TI
684 if (!channels)
685 return;
686
75dcbe4d 687 if (is_haswell_plus(codec))
58f7d28d
ML
688 snd_hda_codec_write(codec, pin_nid, 0,
689 AC_VERB_SET_AMP_GAIN_MUTE,
690 AMP_OUT_UNMUTE);
691
bce0d2a8 692 eld = &per_pin->sink_eld;
079d88cc 693
bb63f726 694 ca = snd_hdac_channel_allocation(&codec->core,
828cb4ed
SP
695 eld->info.spk_alloc, channels,
696 per_pin->chmap_set, non_pcm, per_pin->chmap);
384a48d7 697
bb63f726 698 active_channels = snd_hdac_get_active_channels(ca);
1df5a06a 699
739ffee9
SP
700 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
701 active_channels);
1df5a06a 702
39edac70
AH
703 /*
704 * always configure channel mapping, it may have been changed by the
705 * user in the meantime
706 */
bb63f726 707 snd_hdac_setup_channel_mapping(&spec->chmap,
828cb4ed
SP
708 pin_nid, non_pcm, ca, channels,
709 per_pin->chmap, per_pin->chmap_set);
39edac70 710
307229d2
AH
711 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
712 eld->info.conn_type);
433968da 713
1a6003b5 714 per_pin->non_pcm = non_pcm;
079d88cc
WF
715}
716
079d88cc
WF
717/*
718 * Unsolicited events
719 */
720
efe47108 721static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
38faddb1 722
1a4f69d5 723static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
079d88cc
WF
724{
725 struct hdmi_spec *spec = codec->spec;
1a4f69d5
TI
726 int pin_idx = pin_nid_to_pin_index(codec, nid);
727
20ce9029
DH
728 if (pin_idx < 0)
729 return;
20ce9029
DH
730 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
731 snd_hda_jack_report_sync(codec);
732}
733
1a4f69d5
TI
734static void jack_callback(struct hda_codec *codec,
735 struct hda_jack_callback *jack)
736{
2ebab40e 737 check_presence_and_report(codec, jack->nid);
1a4f69d5
TI
738}
739
20ce9029
DH
740static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
741{
3a93897e 742 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
3a93897e 743 struct hda_jack_tbl *jack;
2e59e5ab 744 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
3a93897e
TI
745
746 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
747 if (!jack)
748 return;
3a93897e 749 jack->jack_dirty = 1;
079d88cc 750
4e76a883 751 codec_dbg(codec,
2e59e5ab 752 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
20ce9029 753 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
fae3d88a 754 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
079d88cc 755
1a4f69d5 756 check_presence_and_report(codec, jack->nid);
079d88cc
WF
757}
758
759static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
760{
761 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
762 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
763 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
764 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
765
4e76a883 766 codec_info(codec,
e9ea8e8f 767 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
384a48d7 768 codec->addr,
079d88cc
WF
769 tag,
770 subtag,
771 cp_state,
772 cp_ready);
773
774 /* TODO */
775 if (cp_state)
776 ;
777 if (cp_ready)
778 ;
779}
780
781
782static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
783{
079d88cc
WF
784 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
785 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
786
3a93897e 787 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
4e76a883 788 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
079d88cc
WF
789 return;
790 }
791
792 if (subtag == 0)
793 hdmi_intrinsic_event(codec, res);
794 else
795 hdmi_non_intrinsic_event(codec, res);
796}
797
58f7d28d 798static void haswell_verify_D0(struct hda_codec *codec,
53b434f0 799 hda_nid_t cvt_nid, hda_nid_t nid)
83f26ad2 800{
58f7d28d 801 int pwr;
83f26ad2 802
53b434f0
WX
803 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
804 * thus pins could only choose converter 0 for use. Make sure the
805 * converters are in correct power state */
fd678cac 806 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
53b434f0
WX
807 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
808
fd678cac 809 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
83f26ad2
DH
810 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
811 AC_PWRST_D0);
812 msleep(40);
813 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
814 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
4e76a883 815 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
83f26ad2 816 }
83f26ad2
DH
817}
818
079d88cc
WF
819/*
820 * Callbacks
821 */
822
92f10b3f
TI
823/* HBR should be Non-PCM, 8 channels */
824#define is_hbr_format(format) \
825 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
826
307229d2
AH
827static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
828 bool hbr)
079d88cc 829{
307229d2 830 int pinctl, new_pinctl;
83f26ad2 831
384a48d7
SW
832 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
833 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
ea87d1c4
AH
834 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
835
13122e6e
AH
836 if (pinctl < 0)
837 return hbr ? -EINVAL : 0;
838
ea87d1c4 839 new_pinctl = pinctl & ~AC_PINCTL_EPT;
307229d2 840 if (hbr)
ea87d1c4
AH
841 new_pinctl |= AC_PINCTL_EPT_HBR;
842 else
843 new_pinctl |= AC_PINCTL_EPT_NATIVE;
844
4e76a883
TI
845 codec_dbg(codec,
846 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
384a48d7 847 pin_nid,
ea87d1c4
AH
848 pinctl == new_pinctl ? "" : "new-",
849 new_pinctl);
850
851 if (pinctl != new_pinctl)
384a48d7 852 snd_hda_codec_write(codec, pin_nid, 0,
ea87d1c4
AH
853 AC_VERB_SET_PIN_WIDGET_CONTROL,
854 new_pinctl);
307229d2
AH
855 } else if (hbr)
856 return -EINVAL;
ea87d1c4 857
307229d2
AH
858 return 0;
859}
860
861static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
862 hda_nid_t pin_nid, u32 stream_tag, int format)
863{
864 struct hdmi_spec *spec = codec->spec;
865 int err;
866
75dcbe4d 867 if (is_haswell_plus(codec))
307229d2
AH
868 haswell_verify_D0(codec, cvt_nid, pin_nid);
869
870 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
871
872 if (err) {
4e76a883 873 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
307229d2 874 return err;
ea87d1c4 875 }
079d88cc 876
384a48d7 877 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
ea87d1c4 878 return 0;
079d88cc
WF
879}
880
42b29870
LY
881/* Try to find an available converter
882 * If pin_idx is less then zero, just try to find an available converter.
883 * Otherwise, try to find an available converter and get the cvt mux index
884 * of the pin.
885 */
7ef166b8
WX
886static int hdmi_choose_cvt(struct hda_codec *codec,
887 int pin_idx, int *cvt_id, int *mux_id)
bbbe3390
TI
888{
889 struct hdmi_spec *spec = codec->spec;
384a48d7 890 struct hdmi_spec_per_pin *per_pin;
384a48d7 891 struct hdmi_spec_per_cvt *per_cvt = NULL;
7ef166b8 892 int cvt_idx, mux_idx = 0;
bbbe3390 893
42b29870
LY
894 /* pin_idx < 0 means no pin will be bound to the converter */
895 if (pin_idx < 0)
896 per_pin = NULL;
897 else
898 per_pin = get_pin(spec, pin_idx);
384a48d7
SW
899
900 /* Dynamically assign converter to stream */
901 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
bce0d2a8 902 per_cvt = get_cvt(spec, cvt_idx);
bbbe3390 903
384a48d7
SW
904 /* Must not already be assigned */
905 if (per_cvt->assigned)
906 continue;
42b29870
LY
907 if (per_pin == NULL)
908 break;
384a48d7
SW
909 /* Must be in pin's mux's list of converters */
910 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
911 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
912 break;
913 /* Not in mux list */
914 if (mux_idx == per_pin->num_mux_nids)
915 continue;
916 break;
917 }
7ef166b8 918
384a48d7
SW
919 /* No free converters */
920 if (cvt_idx == spec->num_cvts)
42b29870 921 return -EBUSY;
384a48d7 922
42b29870
LY
923 if (per_pin != NULL)
924 per_pin->mux_idx = mux_idx;
2df6742f 925
7ef166b8
WX
926 if (cvt_id)
927 *cvt_id = cvt_idx;
928 if (mux_id)
929 *mux_id = mux_idx;
930
931 return 0;
932}
933
2df6742f
ML
934/* Assure the pin select the right convetor */
935static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
936 struct hdmi_spec_per_pin *per_pin)
937{
938 hda_nid_t pin_nid = per_pin->pin_nid;
939 int mux_idx, curr;
940
941 mux_idx = per_pin->mux_idx;
942 curr = snd_hda_codec_read(codec, pin_nid, 0,
943 AC_VERB_GET_CONNECT_SEL, 0);
944 if (curr != mux_idx)
945 snd_hda_codec_write_cache(codec, pin_nid, 0,
946 AC_VERB_SET_CONNECT_SEL,
947 mux_idx);
948}
949
42b29870
LY
950/* get the mux index for the converter of the pins
951 * converter's mux index is the same for all pins on Intel platform
952 */
953static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
954 hda_nid_t cvt_nid)
955{
956 int i;
957
958 for (i = 0; i < spec->num_cvts; i++)
959 if (spec->cvt_nids[i] == cvt_nid)
960 return i;
961 return -EINVAL;
962}
963
300016b9
ML
964/* Intel HDMI workaround to fix audio routing issue:
965 * For some Intel display codecs, pins share the same connection list.
966 * So a conveter can be selected by multiple pins and playback on any of these
967 * pins will generate sound on the external display, because audio flows from
968 * the same converter to the display pipeline. Also muting one pin may make
969 * other pins have no sound output.
970 * So this function assures that an assigned converter for a pin is not selected
971 * by any other pins.
972 */
973static void intel_not_share_assigned_cvt(struct hda_codec *codec,
f82d7d16 974 hda_nid_t pin_nid, int mux_idx)
7ef166b8
WX
975{
976 struct hdmi_spec *spec = codec->spec;
7639a06c 977 hda_nid_t nid;
f82d7d16
ML
978 int cvt_idx, curr;
979 struct hdmi_spec_per_cvt *per_cvt;
7ef166b8 980
f82d7d16 981 /* configure all pins, including "no physical connection" ones */
7639a06c 982 for_each_hda_codec_node(nid, codec) {
f82d7d16
ML
983 unsigned int wid_caps = get_wcaps(codec, nid);
984 unsigned int wid_type = get_wcaps_type(wid_caps);
985
986 if (wid_type != AC_WID_PIN)
987 continue;
7ef166b8 988
f82d7d16 989 if (nid == pin_nid)
7ef166b8
WX
990 continue;
991
f82d7d16 992 curr = snd_hda_codec_read(codec, nid, 0,
7ef166b8 993 AC_VERB_GET_CONNECT_SEL, 0);
f82d7d16
ML
994 if (curr != mux_idx)
995 continue;
7ef166b8 996
f82d7d16
ML
997 /* choose an unassigned converter. The conveters in the
998 * connection list are in the same order as in the codec.
999 */
1000 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1001 per_cvt = get_cvt(spec, cvt_idx);
1002 if (!per_cvt->assigned) {
4e76a883
TI
1003 codec_dbg(codec,
1004 "choose cvt %d for pin nid %d\n",
f82d7d16
ML
1005 cvt_idx, nid);
1006 snd_hda_codec_write_cache(codec, nid, 0,
7ef166b8 1007 AC_VERB_SET_CONNECT_SEL,
f82d7d16
ML
1008 cvt_idx);
1009 break;
1010 }
7ef166b8
WX
1011 }
1012 }
1013}
1014
42b29870
LY
1015/* A wrapper of intel_not_share_asigned_cvt() */
1016static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1017 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1018{
1019 int mux_idx;
1020 struct hdmi_spec *spec = codec->spec;
1021
1022 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1023 return;
1024
1025 /* On Intel platform, the mapping of converter nid to
1026 * mux index of the pins are always the same.
1027 * The pin nid may be 0, this means all pins will not
1028 * share the converter.
1029 */
1030 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1031 if (mux_idx >= 0)
1032 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1033}
1034
1035/* called in hdmi_pcm_open when no pin is assigned to the PCM
1036 * in dyn_pcm_assign mode.
1037 */
1038static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1039 struct hda_codec *codec,
1040 struct snd_pcm_substream *substream)
1041{
1042 struct hdmi_spec *spec = codec->spec;
1043 struct snd_pcm_runtime *runtime = substream->runtime;
ac98379a 1044 int cvt_idx, pcm_idx;
42b29870
LY
1045 struct hdmi_spec_per_cvt *per_cvt = NULL;
1046 int err;
1047
ac98379a
LY
1048 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1049 if (pcm_idx < 0)
1050 return -EINVAL;
1051
42b29870
LY
1052 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1053 if (err)
1054 return err;
1055
1056 per_cvt = get_cvt(spec, cvt_idx);
1057 per_cvt->assigned = 1;
1058 hinfo->nid = per_cvt->cvt_nid;
1059
1060 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1061
ac98379a 1062 set_bit(pcm_idx, &spec->pcm_in_use);
42b29870
LY
1063 /* todo: setup spdif ctls assign */
1064
1065 /* Initially set the converter's capabilities */
1066 hinfo->channels_min = per_cvt->channels_min;
1067 hinfo->channels_max = per_cvt->channels_max;
1068 hinfo->rates = per_cvt->rates;
1069 hinfo->formats = per_cvt->formats;
1070 hinfo->maxbps = per_cvt->maxbps;
1071
1072 /* Store the updated parameters */
1073 runtime->hw.channels_min = hinfo->channels_min;
1074 runtime->hw.channels_max = hinfo->channels_max;
1075 runtime->hw.formats = hinfo->formats;
1076 runtime->hw.rates = hinfo->rates;
1077
1078 snd_pcm_hw_constraint_step(substream->runtime, 0,
1079 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1080 return 0;
1081}
1082
7ef166b8
WX
1083/*
1084 * HDA PCM callbacks
1085 */
1086static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1087 struct hda_codec *codec,
1088 struct snd_pcm_substream *substream)
1089{
1090 struct hdmi_spec *spec = codec->spec;
1091 struct snd_pcm_runtime *runtime = substream->runtime;
2bf3c85a 1092 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
7ef166b8
WX
1093 struct hdmi_spec_per_pin *per_pin;
1094 struct hdmi_eld *eld;
1095 struct hdmi_spec_per_cvt *per_cvt = NULL;
1096 int err;
1097
1098 /* Validate hinfo */
2bf3c85a
LY
1099 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1100 if (pcm_idx < 0)
7ef166b8 1101 return -EINVAL;
2bf3c85a 1102
42b29870 1103 mutex_lock(&spec->pcm_lock);
4e76a883 1104 pin_idx = hinfo_to_pin_index(codec, hinfo);
42b29870
LY
1105 if (!spec->dyn_pcm_assign) {
1106 if (snd_BUG_ON(pin_idx < 0)) {
1107 mutex_unlock(&spec->pcm_lock);
1108 return -EINVAL;
1109 }
1110 } else {
1111 /* no pin is assigned to the PCM
1112 * PA need pcm open successfully when probe
1113 */
1114 if (pin_idx < 0) {
1115 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1116 mutex_unlock(&spec->pcm_lock);
1117 return err;
1118 }
1119 }
7ef166b8
WX
1120
1121 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
42b29870
LY
1122 if (err < 0) {
1123 mutex_unlock(&spec->pcm_lock);
7ef166b8 1124 return err;
42b29870 1125 }
7ef166b8
WX
1126
1127 per_cvt = get_cvt(spec, cvt_idx);
384a48d7
SW
1128 /* Claim converter */
1129 per_cvt->assigned = 1;
42b29870 1130
ac98379a 1131 set_bit(pcm_idx, &spec->pcm_in_use);
42b29870 1132 per_pin = get_pin(spec, pin_idx);
1df5a06a 1133 per_pin->cvt_nid = per_cvt->cvt_nid;
384a48d7
SW
1134 hinfo->nid = per_cvt->cvt_nid;
1135
bddee96b 1136 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
384a48d7
SW
1137 AC_VERB_SET_CONNECT_SEL,
1138 mux_idx);
7ef166b8
WX
1139
1140 /* configure unused pins to choose other converters */
ca2e7224 1141 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
300016b9 1142 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
7ef166b8 1143
2bf3c85a 1144 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
bbbe3390 1145
2def8172 1146 /* Initially set the converter's capabilities */
384a48d7
SW
1147 hinfo->channels_min = per_cvt->channels_min;
1148 hinfo->channels_max = per_cvt->channels_max;
1149 hinfo->rates = per_cvt->rates;
1150 hinfo->formats = per_cvt->formats;
1151 hinfo->maxbps = per_cvt->maxbps;
2def8172 1152
42b29870 1153 eld = &per_pin->sink_eld;
384a48d7 1154 /* Restrict capabilities by ELD if this isn't disabled */
c3d52105 1155 if (!static_hdmi_pcm && eld->eld_valid) {
1613d6b4 1156 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
bbbe3390 1157 if (hinfo->channels_min > hinfo->channels_max ||
2ad779b7
TI
1158 !hinfo->rates || !hinfo->formats) {
1159 per_cvt->assigned = 0;
1160 hinfo->nid = 0;
2bf3c85a 1161 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
42b29870 1162 mutex_unlock(&spec->pcm_lock);
bbbe3390 1163 return -ENODEV;
2ad779b7 1164 }
bbbe3390 1165 }
2def8172 1166
42b29870 1167 mutex_unlock(&spec->pcm_lock);
2def8172 1168 /* Store the updated parameters */
639cef0e
TI
1169 runtime->hw.channels_min = hinfo->channels_min;
1170 runtime->hw.channels_max = hinfo->channels_max;
1171 runtime->hw.formats = hinfo->formats;
1172 runtime->hw.rates = hinfo->rates;
4fe2ca14
TI
1173
1174 snd_pcm_hw_constraint_step(substream->runtime, 0,
1175 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
bbbe3390
TI
1176 return 0;
1177}
1178
079d88cc
WF
1179/*
1180 * HDA/HDMI auto parsing
1181 */
384a48d7 1182static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
079d88cc
WF
1183{
1184 struct hdmi_spec *spec = codec->spec;
bce0d2a8 1185 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
384a48d7 1186 hda_nid_t pin_nid = per_pin->pin_nid;
079d88cc
WF
1187
1188 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
4e76a883
TI
1189 codec_warn(codec,
1190 "HDMI: pin %d wcaps %#x does not support connection list\n",
079d88cc
WF
1191 pin_nid, get_wcaps(codec, pin_nid));
1192 return -EINVAL;
1193 }
1194
384a48d7
SW
1195 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1196 per_pin->mux_nids,
1197 HDA_MAX_CONNECTIONS);
079d88cc
WF
1198
1199 return 0;
1200}
1201
a76056f2
LY
1202static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1203 struct hdmi_spec_per_pin *per_pin)
1204{
1205 int i;
1206
1207 /* try the prefer PCM */
1208 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1209 return per_pin->pin_nid_idx;
1210
1211 /* have a second try; check the "reserved area" over num_pins */
1212 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1213 if (!test_bit(i, &spec->pcm_bitmap))
1214 return i;
1215 }
1216
1217 /* the last try; check the empty slots in pins */
1218 for (i = 0; i < spec->num_pins; i++) {
1219 if (!test_bit(i, &spec->pcm_bitmap))
1220 return i;
1221 }
1222 return -EBUSY;
1223}
1224
1225static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1226 struct hdmi_spec_per_pin *per_pin)
1227{
1228 int idx;
1229
1230 /* pcm already be attached to the pin */
1231 if (per_pin->pcm)
1232 return;
1233 idx = hdmi_find_pcm_slot(spec, per_pin);
d10a80de 1234 if (idx == -EBUSY)
a76056f2
LY
1235 return;
1236 per_pin->pcm_idx = idx;
2bea241a 1237 per_pin->pcm = get_hdmi_pcm(spec, idx);
a76056f2
LY
1238 set_bit(idx, &spec->pcm_bitmap);
1239}
1240
1241static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1242 struct hdmi_spec_per_pin *per_pin)
1243{
1244 int idx;
1245
1246 /* pcm already be detached from the pin */
1247 if (!per_pin->pcm)
1248 return;
1249 idx = per_pin->pcm_idx;
1250 per_pin->pcm_idx = -1;
1251 per_pin->pcm = NULL;
1252 if (idx >= 0 && idx < spec->pcm_used)
1253 clear_bit(idx, &spec->pcm_bitmap);
1254}
1255
ac98379a
LY
1256static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1257 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1258{
1259 int mux_idx;
1260
1261 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1262 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1263 break;
1264 return mux_idx;
1265}
1266
1267static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1268
1269static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1270 struct hdmi_spec_per_pin *per_pin)
1271{
1272 struct hda_codec *codec = per_pin->codec;
1273 struct hda_pcm *pcm;
1274 struct hda_pcm_stream *hinfo;
1275 struct snd_pcm_substream *substream;
1276 int mux_idx;
1277 bool non_pcm;
1278
1279 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
2bea241a 1280 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
ac98379a
LY
1281 else
1282 return;
1283 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1284 return;
1285
1286 /* hdmi audio only uses playback and one substream */
1287 hinfo = pcm->stream;
1288 substream = pcm->pcm->streams[0].substream;
1289
1290 per_pin->cvt_nid = hinfo->nid;
1291
1292 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1293 if (mux_idx < per_pin->num_mux_nids)
1294 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1295 AC_VERB_SET_CONNECT_SEL,
1296 mux_idx);
1297 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1298
1299 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1300 if (substream->runtime)
1301 per_pin->channels = substream->runtime->channels;
1302 per_pin->setup = true;
1303 per_pin->mux_idx = mux_idx;
1304
1305 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1306}
1307
1308static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1309 struct hdmi_spec_per_pin *per_pin)
1310{
1311 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1312 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1313
1314 per_pin->chmap_set = false;
1315 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1316
1317 per_pin->setup = false;
1318 per_pin->channels = 0;
1319}
1320
e90247f9
TI
1321/* update per_pin ELD from the given new ELD;
1322 * setup info frame and notification accordingly
1323 */
1324static void update_eld(struct hda_codec *codec,
1325 struct hdmi_spec_per_pin *per_pin,
1326 struct hdmi_eld *eld)
1327{
1328 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
a76056f2 1329 struct hdmi_spec *spec = codec->spec;
e90247f9
TI
1330 bool old_eld_valid = pin_eld->eld_valid;
1331 bool eld_changed;
fb087eaa 1332 int pcm_idx = -1;
e90247f9 1333
fb087eaa
LY
1334 /* for monitor disconnection, save pcm_idx firstly */
1335 pcm_idx = per_pin->pcm_idx;
a76056f2 1336 if (spec->dyn_pcm_assign) {
ac98379a 1337 if (eld->eld_valid) {
a76056f2 1338 hdmi_attach_hda_pcm(spec, per_pin);
ac98379a
LY
1339 hdmi_pcm_setup_pin(spec, per_pin);
1340 } else {
1341 hdmi_pcm_reset_pin(spec, per_pin);
a76056f2 1342 hdmi_detach_hda_pcm(spec, per_pin);
ac98379a 1343 }
a76056f2 1344 }
fb087eaa
LY
1345 /* if pcm_idx == -1, it means this is in monitor connection event
1346 * we can get the correct pcm_idx now.
1347 */
1348 if (pcm_idx == -1)
1349 pcm_idx = per_pin->pcm_idx;
a76056f2 1350
e90247f9
TI
1351 if (eld->eld_valid)
1352 snd_hdmi_show_eld(codec, &eld->info);
1353
1354 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1355 if (eld->eld_valid && pin_eld->eld_valid)
1356 if (pin_eld->eld_size != eld->eld_size ||
1357 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1358 eld->eld_size) != 0)
1359 eld_changed = true;
1360
1361 pin_eld->eld_valid = eld->eld_valid;
1362 pin_eld->eld_size = eld->eld_size;
1363 if (eld->eld_valid)
1364 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1365 pin_eld->info = eld->info;
1366
1367 /*
1368 * Re-setup pin and infoframe. This is needed e.g. when
1369 * - sink is first plugged-in
1370 * - transcoder can change during stream playback on Haswell
1371 * and this can make HW reset converter selection on a pin.
1372 */
1373 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1374 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1375 intel_verify_pin_cvt_connect(codec, per_pin);
1376 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1377 per_pin->mux_idx);
1378 }
1379
1380 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1381 }
1382
fb087eaa 1383 if (eld_changed && pcm_idx >= 0)
e90247f9
TI
1384 snd_ctl_notify(codec->card,
1385 SNDRV_CTL_EVENT_MASK_VALUE |
1386 SNDRV_CTL_EVENT_MASK_INFO,
fb087eaa 1387 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
e90247f9
TI
1388}
1389
788d441a
TI
1390/* update ELD and jack state via HD-audio verbs */
1391static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1392 int repoll)
079d88cc 1393{
464837a7 1394 struct hda_jack_tbl *jack;
744626da 1395 struct hda_codec *codec = per_pin->codec;
4bd038f9
DH
1396 struct hdmi_spec *spec = codec->spec;
1397 struct hdmi_eld *eld = &spec->temp_eld;
1398 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
744626da 1399 hda_nid_t pin_nid = per_pin->pin_nid;
5d44f927
SW
1400 /*
1401 * Always execute a GetPinSense verb here, even when called from
1402 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1403 * response's PD bit is not the real PD value, but indicates that
1404 * the real PD value changed. An older version of the HD-audio
1405 * specification worked this way. Hence, we just ignore the data in
1406 * the unsolicited response to avoid custom WARs.
1407 */
da4a7a39 1408 int present;
efe47108 1409 bool ret;
9a5e5234 1410 bool do_repoll = false;
079d88cc 1411
da4a7a39
DH
1412 present = snd_hda_pin_sense(codec, pin_nid);
1413
a4e9a38b 1414 mutex_lock(&per_pin->lock);
4bd038f9
DH
1415 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1416 if (pin_eld->monitor_present)
1417 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1418 else
1419 eld->eld_valid = false;
079d88cc 1420
4e76a883 1421 codec_dbg(codec,
384a48d7 1422 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
10250911 1423 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
5d44f927 1424
4bd038f9 1425 if (eld->eld_valid) {
307229d2 1426 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1613d6b4 1427 &eld->eld_size) < 0)
4bd038f9 1428 eld->eld_valid = false;
1613d6b4 1429 else {
79514d47 1430 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1613d6b4 1431 eld->eld_size) < 0)
4bd038f9 1432 eld->eld_valid = false;
1613d6b4 1433 }
9a5e5234
TI
1434 if (!eld->eld_valid && repoll)
1435 do_repoll = true;
744626da 1436 }
4bd038f9 1437
9a5e5234 1438 if (do_repoll)
e90247f9
TI
1439 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1440 else
1441 update_eld(codec, per_pin, eld);
92c69e79 1442
aff747eb 1443 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
464837a7
DH
1444
1445 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1446 if (jack)
1447 jack->block_report = !ret;
1448
a4e9a38b 1449 mutex_unlock(&per_pin->lock);
efe47108 1450 return ret;
079d88cc
WF
1451}
1452
3184270e
LY
1453static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1454 struct hdmi_spec_per_pin *per_pin)
1455{
1456 struct hdmi_spec *spec = codec->spec;
1457 struct snd_jack *jack = NULL;
1458 struct hda_jack_tbl *jack_tbl;
1459
1460 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1461 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1462 * NULL even after snd_hda_jack_tbl_clear() is called to
1463 * free snd_jack. This may cause access invalid memory
1464 * when calling snd_jack_report
1465 */
1466 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1467 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1468 else if (!spec->dyn_pcm_assign) {
1469 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1470 if (jack_tbl)
1471 jack = jack_tbl->jack;
1472 }
1473 return jack;
1474}
1475
788d441a
TI
1476/* update ELD and jack state via audio component */
1477static void sync_eld_via_acomp(struct hda_codec *codec,
1478 struct hdmi_spec_per_pin *per_pin)
1479{
788d441a
TI
1480 struct hdmi_spec *spec = codec->spec;
1481 struct hdmi_eld *eld = &spec->temp_eld;
25e4abb3 1482 struct snd_jack *jack = NULL;
788d441a
TI
1483 int size;
1484
e2dc7d7d
TI
1485 mutex_lock(&per_pin->lock);
1486 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1487 &eld->monitor_present, eld->eld_buffer,
1488 ELD_MAX_SIZE);
1489 if (size < 0)
1490 goto unlock;
1491 if (size > 0) {
1492 size = min(size, ELD_MAX_SIZE);
1493 if (snd_hdmi_parse_eld(codec, &eld->info,
1494 eld->eld_buffer, size) < 0)
1495 size = -EINVAL;
1496 }
788d441a 1497
e2dc7d7d
TI
1498 if (size > 0) {
1499 eld->eld_valid = true;
1500 eld->eld_size = size;
1501 } else {
1502 eld->eld_valid = false;
1503 eld->eld_size = 0;
788d441a 1504 }
e2dc7d7d 1505
25e4abb3
LY
1506 /* pcm_idx >=0 before update_eld() means it is in monitor
1507 * disconnected event. Jack must be fetched before update_eld()
1508 */
3184270e 1509 jack = pin_idx_to_jack(codec, per_pin);
e2dc7d7d 1510 update_eld(codec, per_pin, eld);
3184270e
LY
1511 if (jack == NULL)
1512 jack = pin_idx_to_jack(codec, per_pin);
25e4abb3
LY
1513 if (jack == NULL)
1514 goto unlock;
1515 snd_jack_report(jack,
e2dc7d7d
TI
1516 eld->monitor_present ? SND_JACK_AVOUT : 0);
1517 unlock:
1518 mutex_unlock(&per_pin->lock);
788d441a
TI
1519}
1520
1521static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1522{
1523 struct hda_codec *codec = per_pin->codec;
a76056f2
LY
1524 struct hdmi_spec *spec = codec->spec;
1525 int ret;
788d441a 1526
222bde03
TI
1527 /* no temporary power up/down needed for component notifier */
1528 if (!codec_has_acomp(codec))
1529 snd_hda_power_up_pm(codec);
1530
a76056f2 1531 mutex_lock(&spec->pcm_lock);
788d441a
TI
1532 if (codec_has_acomp(codec)) {
1533 sync_eld_via_acomp(codec, per_pin);
a76056f2 1534 ret = false; /* don't call snd_hda_jack_report_sync() */
788d441a 1535 } else {
a76056f2 1536 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
788d441a 1537 }
a76056f2
LY
1538 mutex_unlock(&spec->pcm_lock);
1539
222bde03
TI
1540 if (!codec_has_acomp(codec))
1541 snd_hda_power_down_pm(codec);
1542
a76056f2 1543 return ret;
788d441a
TI
1544}
1545
744626da
WF
1546static void hdmi_repoll_eld(struct work_struct *work)
1547{
1548 struct hdmi_spec_per_pin *per_pin =
1549 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1550
c6e8453e
WF
1551 if (per_pin->repoll_count++ > 6)
1552 per_pin->repoll_count = 0;
1553
efe47108
TI
1554 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1555 snd_hda_jack_report_sync(per_pin->codec);
744626da
WF
1556}
1557
c88d4e84
TI
1558static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1559 hda_nid_t nid);
1560
079d88cc
WF
1561static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1562{
1563 struct hdmi_spec *spec = codec->spec;
384a48d7
SW
1564 unsigned int caps, config;
1565 int pin_idx;
1566 struct hdmi_spec_per_pin *per_pin;
07acecc1 1567 int err;
079d88cc 1568
efc2f8de 1569 caps = snd_hda_query_pin_caps(codec, pin_nid);
384a48d7
SW
1570 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1571 return 0;
1572
efc2f8de 1573 config = snd_hda_codec_get_pincfg(codec, pin_nid);
384a48d7
SW
1574 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1575 return 0;
1576
75dcbe4d 1577 if (is_haswell_plus(codec))
c88d4e84
TI
1578 intel_haswell_fixup_connect_list(codec, pin_nid);
1579
384a48d7 1580 pin_idx = spec->num_pins;
bce0d2a8
TI
1581 per_pin = snd_array_new(&spec->pins);
1582 if (!per_pin)
1583 return -ENOMEM;
384a48d7
SW
1584
1585 per_pin->pin_nid = pin_nid;
1a6003b5 1586 per_pin->non_pcm = false;
a76056f2
LY
1587 if (spec->dyn_pcm_assign)
1588 per_pin->pcm_idx = -1;
2bea241a
LY
1589 else {
1590 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
a76056f2 1591 per_pin->pcm_idx = pin_idx;
2bea241a 1592 }
a76056f2 1593 per_pin->pin_nid_idx = pin_idx;
079d88cc 1594
384a48d7
SW
1595 err = hdmi_read_pin_conn(codec, pin_idx);
1596 if (err < 0)
1597 return err;
079d88cc 1598
079d88cc
WF
1599 spec->num_pins++;
1600
384a48d7 1601 return 0;
079d88cc
WF
1602}
1603
384a48d7 1604static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
079d88cc
WF
1605{
1606 struct hdmi_spec *spec = codec->spec;
384a48d7
SW
1607 struct hdmi_spec_per_cvt *per_cvt;
1608 unsigned int chans;
1609 int err;
079d88cc 1610
384a48d7
SW
1611 chans = get_wcaps(codec, cvt_nid);
1612 chans = get_wcaps_channels(chans);
1613
bce0d2a8
TI
1614 per_cvt = snd_array_new(&spec->cvts);
1615 if (!per_cvt)
1616 return -ENOMEM;
384a48d7
SW
1617
1618 per_cvt->cvt_nid = cvt_nid;
1619 per_cvt->channels_min = 2;
d45e6889 1620 if (chans <= 16) {
384a48d7 1621 per_cvt->channels_max = chans;
67b90cb8
SP
1622 if (chans > spec->chmap.channels_max)
1623 spec->chmap.channels_max = chans;
d45e6889 1624 }
384a48d7
SW
1625
1626 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1627 &per_cvt->rates,
1628 &per_cvt->formats,
1629 &per_cvt->maxbps);
1630 if (err < 0)
1631 return err;
1632
bce0d2a8
TI
1633 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1634 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1635 spec->num_cvts++;
079d88cc
WF
1636
1637 return 0;
1638}
1639
1640static int hdmi_parse_codec(struct hda_codec *codec)
1641{
1642 hda_nid_t nid;
1643 int i, nodes;
1644
7639a06c 1645 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
079d88cc 1646 if (!nid || nodes < 0) {
4e76a883 1647 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
079d88cc
WF
1648 return -EINVAL;
1649 }
1650
1651 for (i = 0; i < nodes; i++, nid++) {
1652 unsigned int caps;
1653 unsigned int type;
1654
efc2f8de 1655 caps = get_wcaps(codec, nid);
079d88cc
WF
1656 type = get_wcaps_type(caps);
1657
1658 if (!(caps & AC_WCAP_DIGITAL))
1659 continue;
1660
1661 switch (type) {
1662 case AC_WID_AUD_OUT:
384a48d7 1663 hdmi_add_cvt(codec, nid);
079d88cc
WF
1664 break;
1665 case AC_WID_PIN:
3eaead57 1666 hdmi_add_pin(codec, nid);
079d88cc
WF
1667 break;
1668 }
1669 }
1670
079d88cc
WF
1671 return 0;
1672}
1673
84eb01be
TI
1674/*
1675 */
1a6003b5
TI
1676static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1677{
1678 struct hda_spdif_out *spdif;
1679 bool non_pcm;
1680
1681 mutex_lock(&codec->spdif_mutex);
1682 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1683 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1684 mutex_unlock(&codec->spdif_mutex);
1685 return non_pcm;
1686}
1687
84eb01be
TI
1688/*
1689 * HDMI callbacks
1690 */
1691
1692static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1693 struct hda_codec *codec,
1694 unsigned int stream_tag,
1695 unsigned int format,
1696 struct snd_pcm_substream *substream)
1697{
384a48d7
SW
1698 hda_nid_t cvt_nid = hinfo->nid;
1699 struct hdmi_spec *spec = codec->spec;
42b29870
LY
1700 int pin_idx;
1701 struct hdmi_spec_per_pin *per_pin;
1702 hda_nid_t pin_nid;
ddd621fb 1703 struct snd_pcm_runtime *runtime = substream->runtime;
1a6003b5 1704 bool non_pcm;
75fae117 1705 int pinctl;
42b29870 1706 int err;
1a6003b5 1707
42b29870
LY
1708 mutex_lock(&spec->pcm_lock);
1709 pin_idx = hinfo_to_pin_index(codec, hinfo);
1710 if (spec->dyn_pcm_assign && pin_idx < 0) {
1711 /* when dyn_pcm_assign and pcm is not bound to a pin
1712 * skip pin setup and return 0 to make audio playback
1713 * be ongoing
1714 */
1715 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
1716 snd_hda_codec_setup_stream(codec, cvt_nid,
1717 stream_tag, 0, format);
1718 mutex_unlock(&spec->pcm_lock);
1719 return 0;
1720 }
1a6003b5 1721
42b29870
LY
1722 if (snd_BUG_ON(pin_idx < 0)) {
1723 mutex_unlock(&spec->pcm_lock);
1724 return -EINVAL;
1725 }
1726 per_pin = get_pin(spec, pin_idx);
1727 pin_nid = per_pin->pin_nid;
ca2e7224 1728 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
2df6742f
ML
1729 /* Verify pin:cvt selections to avoid silent audio after S3.
1730 * After S3, the audio driver restores pin:cvt selections
1731 * but this can happen before gfx is ready and such selection
1732 * is overlooked by HW. Thus multiple pins can share a same
1733 * default convertor and mute control will affect each other,
1734 * which can cause a resumed audio playback become silent
1735 * after S3.
1736 */
1737 intel_verify_pin_cvt_connect(codec, per_pin);
1738 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
1739 }
1740
ddd621fb
LY
1741 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1742 /* Todo: add DP1.2 MST audio support later */
e2dc7d7d 1743 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
ddd621fb 1744
1a6003b5 1745 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
a4e9a38b 1746 mutex_lock(&per_pin->lock);
b054087d
TI
1747 per_pin->channels = substream->runtime->channels;
1748 per_pin->setup = true;
384a48d7 1749
b054087d 1750 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
a4e9a38b 1751 mutex_unlock(&per_pin->lock);
75fae117
SW
1752 if (spec->dyn_pin_out) {
1753 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1754 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1755 snd_hda_codec_write(codec, pin_nid, 0,
1756 AC_VERB_SET_PIN_WIDGET_CONTROL,
1757 pinctl | PIN_OUT);
1758 }
1759
42b29870
LY
1760 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1761 stream_tag, format);
1762 mutex_unlock(&spec->pcm_lock);
1763 return err;
84eb01be
TI
1764}
1765
8dfaa573
TI
1766static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1767 struct hda_codec *codec,
1768 struct snd_pcm_substream *substream)
1769{
1770 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1771 return 0;
1772}
1773
f2ad24fa
TI
1774static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1775 struct hda_codec *codec,
1776 struct snd_pcm_substream *substream)
384a48d7
SW
1777{
1778 struct hdmi_spec *spec = codec->spec;
2bf3c85a 1779 int cvt_idx, pin_idx, pcm_idx;
384a48d7
SW
1780 struct hdmi_spec_per_cvt *per_cvt;
1781 struct hdmi_spec_per_pin *per_pin;
75fae117 1782 int pinctl;
384a48d7 1783
384a48d7 1784 if (hinfo->nid) {
2bf3c85a
LY
1785 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1786 if (snd_BUG_ON(pcm_idx < 0))
1787 return -EINVAL;
4e76a883 1788 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
384a48d7
SW
1789 if (snd_BUG_ON(cvt_idx < 0))
1790 return -EINVAL;
bce0d2a8 1791 per_cvt = get_cvt(spec, cvt_idx);
384a48d7
SW
1792
1793 snd_BUG_ON(!per_cvt->assigned);
1794 per_cvt->assigned = 0;
1795 hinfo->nid = 0;
1796
42b29870 1797 mutex_lock(&spec->pcm_lock);
b09887f8 1798 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
ac98379a 1799 clear_bit(pcm_idx, &spec->pcm_in_use);
4e76a883 1800 pin_idx = hinfo_to_pin_index(codec, hinfo);
42b29870
LY
1801 if (spec->dyn_pcm_assign && pin_idx < 0) {
1802 mutex_unlock(&spec->pcm_lock);
1803 return 0;
1804 }
1805
1806 if (snd_BUG_ON(pin_idx < 0)) {
1807 mutex_unlock(&spec->pcm_lock);
384a48d7 1808 return -EINVAL;
42b29870 1809 }
bce0d2a8 1810 per_pin = get_pin(spec, pin_idx);
384a48d7 1811
75fae117
SW
1812 if (spec->dyn_pin_out) {
1813 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1814 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1815 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1816 AC_VERB_SET_PIN_WIDGET_CONTROL,
1817 pinctl & ~PIN_OUT);
1818 }
1819
a4e9a38b 1820 mutex_lock(&per_pin->lock);
d45e6889
TI
1821 per_pin->chmap_set = false;
1822 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
b054087d
TI
1823
1824 per_pin->setup = false;
1825 per_pin->channels = 0;
a4e9a38b 1826 mutex_unlock(&per_pin->lock);
42b29870 1827 mutex_unlock(&spec->pcm_lock);
384a48d7 1828 }
d45e6889 1829
384a48d7
SW
1830 return 0;
1831}
1832
1833static const struct hda_pcm_ops generic_ops = {
1834 .open = hdmi_pcm_open,
f2ad24fa 1835 .close = hdmi_pcm_close,
384a48d7 1836 .prepare = generic_hdmi_playback_pcm_prepare,
8dfaa573 1837 .cleanup = generic_hdmi_playback_pcm_cleanup,
84eb01be
TI
1838};
1839
9b3dc8aa
SP
1840static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1841 unsigned char *chmap)
1842{
1843 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1844 struct hdmi_spec *spec = codec->spec;
1845 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1846
1847 /* chmap is already set to 0 in caller */
1848 if (!per_pin)
1849 return;
1850
1851 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
1852}
1853
1854static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1855 unsigned char *chmap, int prepared)
1856{
1857 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1858 struct hdmi_spec *spec = codec->spec;
1859 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1860
1861 mutex_lock(&per_pin->lock);
1862 per_pin->chmap_set = true;
1863 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
1864 if (prepared)
1865 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1866 mutex_unlock(&per_pin->lock);
1867}
1868
1869static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1870{
1871 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1872 struct hdmi_spec *spec = codec->spec;
1873 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1874
1875 return per_pin ? true:false;
1876}
1877
84eb01be
TI
1878static int generic_hdmi_build_pcms(struct hda_codec *codec)
1879{
1880 struct hdmi_spec *spec = codec->spec;
384a48d7 1881 int pin_idx;
84eb01be 1882
384a48d7
SW
1883 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1884 struct hda_pcm *info;
84eb01be 1885 struct hda_pcm_stream *pstr;
bce0d2a8 1886
bbbc7e85 1887 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
bce0d2a8
TI
1888 if (!info)
1889 return -ENOMEM;
2bea241a
LY
1890
1891 spec->pcm_rec[pin_idx].pcm = info;
2bf3c85a 1892 spec->pcm_used++;
84eb01be 1893 info->pcm_type = HDA_PCM_TYPE_HDMI;
d45e6889 1894 info->own_chmap = true;
384a48d7 1895
84eb01be 1896 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
384a48d7
SW
1897 pstr->substreams = 1;
1898 pstr->ops = generic_ops;
1899 /* other pstr fields are set in open */
84eb01be
TI
1900 }
1901
1902 return 0;
1903}
1904
25e4abb3 1905static void free_hdmi_jack_priv(struct snd_jack *jack)
788d441a 1906{
25e4abb3 1907 struct hdmi_pcm *pcm = jack->private_data;
788d441a 1908
25e4abb3 1909 pcm->jack = NULL;
788d441a
TI
1910}
1911
25e4abb3
LY
1912static int add_hdmi_jack_kctl(struct hda_codec *codec,
1913 struct hdmi_spec *spec,
1914 int pcm_idx,
788d441a
TI
1915 const char *name)
1916{
1917 struct snd_jack *jack;
1918 int err;
1919
1920 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
1921 true, false);
1922 if (err < 0)
1923 return err;
25e4abb3
LY
1924
1925 spec->pcm_rec[pcm_idx].jack = jack;
1926 jack->private_data = &spec->pcm_rec[pcm_idx];
1927 jack->private_free = free_hdmi_jack_priv;
788d441a
TI
1928 return 0;
1929}
1930
25e4abb3 1931static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
0b6c49b5 1932{
31ef2257 1933 char hdmi_str[32] = "HDMI/DP";
0b6c49b5 1934 struct hdmi_spec *spec = codec->spec;
25e4abb3
LY
1935 struct hdmi_spec_per_pin *per_pin;
1936 struct hda_jack_tbl *jack;
1937 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
909cadc6 1938 bool phantom_jack;
25e4abb3 1939 int ret;
0b6c49b5 1940
31ef2257
TI
1941 if (pcmdev > 0)
1942 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
25e4abb3
LY
1943
1944 if (spec->dyn_pcm_assign)
1945 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
1946
1947 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
1948 /* if !dyn_pcm_assign, it must be non-MST mode.
1949 * This means pcms and pins are statically mapped.
1950 * And pcm_idx is pin_idx.
1951 */
1952 per_pin = get_pin(spec, pcm_idx);
909cadc6
TI
1953 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
1954 if (phantom_jack)
30efd8de
DH
1955 strncat(hdmi_str, " Phantom",
1956 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
25e4abb3
LY
1957 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
1958 phantom_jack);
1959 if (ret < 0)
1960 return ret;
1961 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1962 if (jack == NULL)
1963 return 0;
1964 /* assign jack->jack to pcm_rec[].jack to
1965 * align with dyn_pcm_assign mode
1966 */
1967 spec->pcm_rec[pcm_idx].jack = jack->jack;
1968 return 0;
0b6c49b5
DH
1969}
1970
84eb01be
TI
1971static int generic_hdmi_build_controls(struct hda_codec *codec)
1972{
1973 struct hdmi_spec *spec = codec->spec;
1974 int err;
25e4abb3 1975 int pin_idx, pcm_idx;
84eb01be 1976
0b6c49b5 1977
25e4abb3
LY
1978 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
1979 err = generic_hdmi_build_jack(codec, pcm_idx);
0b6c49b5
DH
1980 if (err < 0)
1981 return err;
1982
b09887f8
LY
1983 /* create the spdif for each pcm
1984 * pin will be bound when monitor is connected
1985 */
1986 if (spec->dyn_pcm_assign)
1987 err = snd_hda_create_dig_out_ctls(codec,
1988 0, spec->cvt_nids[0],
1989 HDA_PCM_TYPE_HDMI);
1990 else {
1991 struct hdmi_spec_per_pin *per_pin =
1992 get_pin(spec, pcm_idx);
1993 err = snd_hda_create_dig_out_ctls(codec,
dcda5806
TI
1994 per_pin->pin_nid,
1995 per_pin->mux_nids[0],
1996 HDA_PCM_TYPE_HDMI);
b09887f8 1997 }
84eb01be
TI
1998 if (err < 0)
1999 return err;
b09887f8 2000 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
14bc52b8
PLB
2001
2002 /* add control for ELD Bytes */
fb087eaa
LY
2003 err = hdmi_create_eld_ctl(codec, pcm_idx,
2004 get_pcm_rec(spec, pcm_idx)->device);
14bc52b8
PLB
2005 if (err < 0)
2006 return err;
fb087eaa
LY
2007 }
2008
2009 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2010 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
31ef2257 2011
82b1d73f 2012 hdmi_present_sense(per_pin, 0);
84eb01be
TI
2013 }
2014
d45e6889 2015 /* add channel maps */
022f344b 2016 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
bbbc7e85 2017 struct hda_pcm *pcm;
2ca320e2 2018
022f344b 2019 pcm = get_pcm_rec(spec, pcm_idx);
bbbc7e85 2020 if (!pcm || !pcm->pcm)
2ca320e2 2021 break;
2f6e8a85 2022 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
d45e6889
TI
2023 if (err < 0)
2024 return err;
d45e6889
TI
2025 }
2026
84eb01be
TI
2027 return 0;
2028}
2029
8b8d654b 2030static int generic_hdmi_init_per_pins(struct hda_codec *codec)
84eb01be
TI
2031{
2032 struct hdmi_spec *spec = codec->spec;
384a48d7
SW
2033 int pin_idx;
2034
2035 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
bce0d2a8 2036 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
84eb01be 2037
744626da 2038 per_pin->codec = codec;
a4e9a38b 2039 mutex_init(&per_pin->lock);
744626da 2040 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
a4e9a38b 2041 eld_proc_new(per_pin, pin_idx);
84eb01be 2042 }
8b8d654b
TI
2043 return 0;
2044}
2045
2046static int generic_hdmi_init(struct hda_codec *codec)
2047{
2048 struct hdmi_spec *spec = codec->spec;
2049 int pin_idx;
2050
2051 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
bce0d2a8 2052 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
8b8d654b
TI
2053 hda_nid_t pin_nid = per_pin->pin_nid;
2054
2055 hdmi_init_pin(codec, pin_nid);
788d441a
TI
2056 if (!codec_has_acomp(codec))
2057 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2058 codec->jackpoll_interval > 0 ?
2059 jack_callback : NULL);
8b8d654b 2060 }
84eb01be
TI
2061 return 0;
2062}
2063
bce0d2a8
TI
2064static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2065{
2066 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2067 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
bce0d2a8
TI
2068}
2069
2070static void hdmi_array_free(struct hdmi_spec *spec)
2071{
2072 snd_array_free(&spec->pins);
2073 snd_array_free(&spec->cvts);
bce0d2a8
TI
2074}
2075
84eb01be
TI
2076static void generic_hdmi_free(struct hda_codec *codec)
2077{
2078 struct hdmi_spec *spec = codec->spec;
25e4abb3 2079 int pin_idx, pcm_idx;
384a48d7 2080
6603249d 2081 if (codec_has_acomp(codec))
25adc137
DH
2082 snd_hdac_i915_register_notifier(NULL);
2083
384a48d7 2084 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
bce0d2a8 2085 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2f35c630 2086 cancel_delayed_work_sync(&per_pin->work);
a4e9a38b 2087 eld_proc_free(per_pin);
25e4abb3
LY
2088 }
2089
2090 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2091 if (spec->pcm_rec[pcm_idx].jack == NULL)
2092 continue;
2093 if (spec->dyn_pcm_assign)
2094 snd_device_free(codec->card,
2095 spec->pcm_rec[pcm_idx].jack);
2096 else
2097 spec->pcm_rec[pcm_idx].jack = NULL;
384a48d7 2098 }
84eb01be 2099
55913110
TI
2100 if (spec->i915_bound)
2101 snd_hdac_i915_exit(&codec->bus->core);
bce0d2a8 2102 hdmi_array_free(spec);
84eb01be
TI
2103 kfree(spec);
2104}
2105
28cb72e5
WX
2106#ifdef CONFIG_PM
2107static int generic_hdmi_resume(struct hda_codec *codec)
2108{
2109 struct hdmi_spec *spec = codec->spec;
2110 int pin_idx;
2111
a2833683 2112 codec->patch_ops.init(codec);
eeecd9d1 2113 regcache_sync(codec->core.regmap);
28cb72e5
WX
2114
2115 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2116 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2117 hdmi_present_sense(per_pin, 1);
2118 }
2119 return 0;
2120}
2121#endif
2122
fb79e1e0 2123static const struct hda_codec_ops generic_hdmi_patch_ops = {
84eb01be
TI
2124 .init = generic_hdmi_init,
2125 .free = generic_hdmi_free,
2126 .build_pcms = generic_hdmi_build_pcms,
2127 .build_controls = generic_hdmi_build_controls,
2128 .unsol_event = hdmi_unsol_event,
28cb72e5
WX
2129#ifdef CONFIG_PM
2130 .resume = generic_hdmi_resume,
2131#endif
84eb01be
TI
2132};
2133
307229d2
AH
2134static const struct hdmi_ops generic_standard_hdmi_ops = {
2135 .pin_get_eld = snd_hdmi_get_eld,
307229d2
AH
2136 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2137 .pin_hbr_setup = hdmi_pin_hbr_setup,
2138 .setup_stream = hdmi_setup_stream,
67b90cb8
SP
2139};
2140
c88d4e84
TI
2141static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2142 hda_nid_t nid)
2143{
2144 struct hdmi_spec *spec = codec->spec;
2145 hda_nid_t conns[4];
2146 int nconns;
6ffe168f 2147
c88d4e84
TI
2148 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2149 if (nconns == spec->num_cvts &&
2150 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
6ffe168f
ML
2151 return;
2152
c88d4e84 2153 /* override pins connection list */
4e76a883 2154 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
c88d4e84 2155 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
6ffe168f
ML
2156}
2157
1611a9c9
ML
2158#define INTEL_VENDOR_NID 0x08
2159#define INTEL_GET_VENDOR_VERB 0xf81
2160#define INTEL_SET_VENDOR_VERB 0x781
2161#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2162#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2163
2164static void intel_haswell_enable_all_pins(struct hda_codec *codec,
17df3f55 2165 bool update_tree)
1611a9c9
ML
2166{
2167 unsigned int vendor_param;
2168
1611a9c9
ML
2169 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2170 INTEL_GET_VENDOR_VERB, 0);
2171 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2172 return;
2173
2174 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2175 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2176 INTEL_SET_VENDOR_VERB, vendor_param);
2177 if (vendor_param == -1)
2178 return;
2179
17df3f55
TI
2180 if (update_tree)
2181 snd_hda_codec_update_widgets(codec);
1611a9c9
ML
2182}
2183
c88d4e84
TI
2184static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2185{
2186 unsigned int vendor_param;
2187
2188 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2189 INTEL_GET_VENDOR_VERB, 0);
2190 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2191 return;
2192
2193 /* enable DP1.2 mode */
2194 vendor_param |= INTEL_EN_DP12;
a551d914 2195 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
c88d4e84
TI
2196 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2197 INTEL_SET_VENDOR_VERB, vendor_param);
2198}
2199
17df3f55
TI
2200/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2201 * Otherwise you may get severe h/w communication errors.
2202 */
2203static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2204 unsigned int power_state)
2205{
2206 if (power_state == AC_PWRST_D0) {
2207 intel_haswell_enable_all_pins(codec, false);
2208 intel_haswell_fixup_enable_dp12(codec);
2209 }
c88d4e84 2210
17df3f55
TI
2211 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2212 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2213}
6ffe168f 2214
f0675d4a 2215static void intel_pin_eld_notify(void *audio_ptr, int port)
25adc137
DH
2216{
2217 struct hda_codec *codec = audio_ptr;
2218 int pin_nid = port + 0x04;
2219
4f8e4f35
TI
2220 /* we assume only from port-B to port-D */
2221 if (port < 1 || port > 3)
2222 return;
2223
8ae743e8
TI
2224 /* skip notification during system suspend (but not in runtime PM);
2225 * the state will be updated at resume
2226 */
2227 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2228 return;
eb399d3c
TI
2229 /* ditto during suspend/resume process itself */
2230 if (atomic_read(&(codec)->core.in_pm))
2231 return;
8ae743e8 2232
25adc137
DH
2233 check_presence_and_report(codec, pin_nid);
2234}
2235
84eb01be
TI
2236static int patch_generic_hdmi(struct hda_codec *codec)
2237{
2238 struct hdmi_spec *spec;
84eb01be
TI
2239
2240 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2241 if (spec == NULL)
2242 return -ENOMEM;
2243
307229d2 2244 spec->ops = generic_standard_hdmi_ops;
42b29870 2245 mutex_init(&spec->pcm_lock);
739ffee9
SP
2246 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2247
739ffee9
SP
2248 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2249 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2250 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2251
84eb01be 2252 codec->spec = spec;
bce0d2a8 2253 hdmi_array_init(spec, 4);
6ffe168f 2254
691be973 2255#ifdef CONFIG_SND_HDA_I915
b62232d4 2256 /* Try to bind with i915 for Intel HSW+ codecs (if not done yet) */
691be973
TI
2257 if ((codec->core.vendor_id >> 16) == 0x8086 &&
2258 is_haswell_plus(codec)) {
2259 if (!codec->bus->core.audio_component)
2260 if (!snd_hdac_i915_init(&codec->bus->core))
2261 spec->i915_bound = true;
2262 /* use i915 audio component notifier for hotplug */
2263 if (codec->bus->core.audio_component)
2264 spec->use_acomp_notifier = true;
2265 }
2266#endif
55913110 2267
75dcbe4d 2268 if (is_haswell_plus(codec)) {
17df3f55 2269 intel_haswell_enable_all_pins(codec, true);
c88d4e84 2270 intel_haswell_fixup_enable_dp12(codec);
17df3f55 2271 }
6ffe168f 2272
2bd1f73f
ML
2273 /* For Valleyview/Cherryview, only the display codec is in the display
2274 * power well and can use link_power ops to request/release the power.
2275 * For Haswell/Broadwell, the controller is also in the power well and
2276 * can cover the codec power request, and so need not set this flag.
2277 * For previous platforms, there is no such power well feature.
2278 */
ff9d8859
LH
2279 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2280 is_broxton(codec))
2bd1f73f
ML
2281 codec->core.link_power_control = 1;
2282
84eb01be 2283 if (hdmi_parse_codec(codec) < 0) {
55913110
TI
2284 if (spec->i915_bound)
2285 snd_hdac_i915_exit(&codec->bus->core);
84eb01be
TI
2286 codec->spec = NULL;
2287 kfree(spec);
2288 return -EINVAL;
2289 }
2290 codec->patch_ops = generic_hdmi_patch_ops;
75dcbe4d 2291 if (is_haswell_plus(codec)) {
17df3f55 2292 codec->patch_ops.set_power_state = haswell_set_power_state;
5dc989bd
ML
2293 codec->dp_mst = true;
2294 }
17df3f55 2295
2377c3c3
LH
2296 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2297 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2298 codec->auto_runtime_pm = 1;
2299
8b8d654b 2300 generic_hdmi_init_per_pins(codec);
84eb01be 2301
84eb01be 2302
790b415c
LY
2303 if (codec_has_acomp(codec)) {
2304 codec->depop_delay = 0;
2305 spec->i915_audio_ops.audio_ptr = codec;
2306 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2307 * will call pin_eld_notify with using audio_ptr pointer
2308 * We need make sure audio_ptr is really setup
2309 */
2310 wmb();
2311 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2312 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2313 }
2314
25e4abb3 2315 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
84eb01be
TI
2316 return 0;
2317}
2318
3aaf8980
SW
2319/*
2320 * Shared non-generic implementations
2321 */
2322
2323static int simple_playback_build_pcms(struct hda_codec *codec)
2324{
2325 struct hdmi_spec *spec = codec->spec;
bce0d2a8 2326 struct hda_pcm *info;
8ceb332d
TI
2327 unsigned int chans;
2328 struct hda_pcm_stream *pstr;
bce0d2a8 2329 struct hdmi_spec_per_cvt *per_cvt;
3aaf8980 2330
bce0d2a8
TI
2331 per_cvt = get_cvt(spec, 0);
2332 chans = get_wcaps(codec, per_cvt->cvt_nid);
8ceb332d 2333 chans = get_wcaps_channels(chans);
3aaf8980 2334
bbbc7e85 2335 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
bce0d2a8
TI
2336 if (!info)
2337 return -ENOMEM;
2bea241a 2338 spec->pcm_rec[0].pcm = info;
8ceb332d
TI
2339 info->pcm_type = HDA_PCM_TYPE_HDMI;
2340 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2341 *pstr = spec->pcm_playback;
bce0d2a8 2342 pstr->nid = per_cvt->cvt_nid;
8ceb332d
TI
2343 if (pstr->channels_max <= 2 && chans && chans <= 16)
2344 pstr->channels_max = chans;
3aaf8980
SW
2345
2346 return 0;
2347}
2348
4b6ace9e
TI
2349/* unsolicited event for jack sensing */
2350static void simple_hdmi_unsol_event(struct hda_codec *codec,
2351 unsigned int res)
2352{
9dd8cf12 2353 snd_hda_jack_set_dirty_all(codec);
4b6ace9e
TI
2354 snd_hda_jack_report_sync(codec);
2355}
2356
2357/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2358 * as long as spec->pins[] is set correctly
2359 */
2360#define simple_hdmi_build_jack generic_hdmi_build_jack
2361
3aaf8980
SW
2362static int simple_playback_build_controls(struct hda_codec *codec)
2363{
2364 struct hdmi_spec *spec = codec->spec;
bce0d2a8 2365 struct hdmi_spec_per_cvt *per_cvt;
3aaf8980 2366 int err;
3aaf8980 2367
bce0d2a8 2368 per_cvt = get_cvt(spec, 0);
c9a6338a
AH
2369 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2370 per_cvt->cvt_nid,
2371 HDA_PCM_TYPE_HDMI);
8ceb332d
TI
2372 if (err < 0)
2373 return err;
2374 return simple_hdmi_build_jack(codec, 0);
3aaf8980
SW
2375}
2376
4f0110ce
TI
2377static int simple_playback_init(struct hda_codec *codec)
2378{
2379 struct hdmi_spec *spec = codec->spec;
bce0d2a8
TI
2380 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2381 hda_nid_t pin = per_pin->pin_nid;
8ceb332d
TI
2382
2383 snd_hda_codec_write(codec, pin, 0,
2384 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2385 /* some codecs require to unmute the pin */
2386 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2387 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2388 AMP_OUT_UNMUTE);
62f949bf 2389 snd_hda_jack_detect_enable(codec, pin);
4f0110ce
TI
2390 return 0;
2391}
2392
3aaf8980
SW
2393static void simple_playback_free(struct hda_codec *codec)
2394{
2395 struct hdmi_spec *spec = codec->spec;
2396
bce0d2a8 2397 hdmi_array_free(spec);
3aaf8980
SW
2398 kfree(spec);
2399}
2400
84eb01be
TI
2401/*
2402 * Nvidia specific implementations
2403 */
2404
2405#define Nv_VERB_SET_Channel_Allocation 0xF79
2406#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2407#define Nv_VERB_SET_Audio_Protection_On 0xF98
2408#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2409
2410#define nvhdmi_master_con_nid_7x 0x04
2411#define nvhdmi_master_pin_nid_7x 0x05
2412
fb79e1e0 2413static const hda_nid_t nvhdmi_con_nids_7x[4] = {
84eb01be
TI
2414 /*front, rear, clfe, rear_surr */
2415 0x6, 0x8, 0xa, 0xc,
2416};
2417
ceaa86ba
TI
2418static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2419 /* set audio protect on */
2420 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2421 /* enable digital output on pin widget */
2422 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2423 {} /* terminator */
2424};
2425
2426static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
84eb01be
TI
2427 /* set audio protect on */
2428 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2429 /* enable digital output on pin widget */
2430 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2431 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2432 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2433 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2434 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2435 {} /* terminator */
2436};
2437
2438#ifdef LIMITED_RATE_FMT_SUPPORT
2439/* support only the safe format and rate */
2440#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2441#define SUPPORTED_MAXBPS 16
2442#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2443#else
2444/* support all rates and formats */
2445#define SUPPORTED_RATES \
2446 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2447 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2448 SNDRV_PCM_RATE_192000)
2449#define SUPPORTED_MAXBPS 24
2450#define SUPPORTED_FORMATS \
2451 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2452#endif
2453
ceaa86ba
TI
2454static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
2455{
2456 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2457 return 0;
2458}
2459
2460static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
84eb01be 2461{
ceaa86ba 2462 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
84eb01be
TI
2463 return 0;
2464}
2465
393004b2
ND
2466static unsigned int channels_2_6_8[] = {
2467 2, 6, 8
2468};
2469
2470static unsigned int channels_2_8[] = {
2471 2, 8
2472};
2473
2474static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2475 .count = ARRAY_SIZE(channels_2_6_8),
2476 .list = channels_2_6_8,
2477 .mask = 0,
2478};
2479
2480static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2481 .count = ARRAY_SIZE(channels_2_8),
2482 .list = channels_2_8,
2483 .mask = 0,
2484};
2485
84eb01be
TI
2486static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2487 struct hda_codec *codec,
2488 struct snd_pcm_substream *substream)
2489{
2490 struct hdmi_spec *spec = codec->spec;
393004b2
ND
2491 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2492
b9a94a9c 2493 switch (codec->preset->vendor_id) {
393004b2
ND
2494 case 0x10de0002:
2495 case 0x10de0003:
2496 case 0x10de0005:
2497 case 0x10de0006:
2498 hw_constraints_channels = &hw_constraints_2_8_channels;
2499 break;
2500 case 0x10de0007:
2501 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2502 break;
2503 default:
2504 break;
2505 }
2506
2507 if (hw_constraints_channels != NULL) {
2508 snd_pcm_hw_constraint_list(substream->runtime, 0,
2509 SNDRV_PCM_HW_PARAM_CHANNELS,
2510 hw_constraints_channels);
ad09fc9d
TI
2511 } else {
2512 snd_pcm_hw_constraint_step(substream->runtime, 0,
2513 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
393004b2
ND
2514 }
2515
84eb01be
TI
2516 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2517}
2518
2519static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2520 struct hda_codec *codec,
2521 struct snd_pcm_substream *substream)
2522{
2523 struct hdmi_spec *spec = codec->spec;
2524 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2525}
2526
2527static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2528 struct hda_codec *codec,
2529 unsigned int stream_tag,
2530 unsigned int format,
2531 struct snd_pcm_substream *substream)
2532{
2533 struct hdmi_spec *spec = codec->spec;
2534 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2535 stream_tag, format, substream);
2536}
2537
d0b1252d
TI
2538static const struct hda_pcm_stream simple_pcm_playback = {
2539 .substreams = 1,
2540 .channels_min = 2,
2541 .channels_max = 2,
2542 .ops = {
2543 .open = simple_playback_pcm_open,
2544 .close = simple_playback_pcm_close,
2545 .prepare = simple_playback_pcm_prepare
2546 },
2547};
2548
2549static const struct hda_codec_ops simple_hdmi_patch_ops = {
2550 .build_controls = simple_playback_build_controls,
2551 .build_pcms = simple_playback_build_pcms,
2552 .init = simple_playback_init,
2553 .free = simple_playback_free,
250e41ac 2554 .unsol_event = simple_hdmi_unsol_event,
d0b1252d
TI
2555};
2556
2557static int patch_simple_hdmi(struct hda_codec *codec,
2558 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2559{
2560 struct hdmi_spec *spec;
bce0d2a8
TI
2561 struct hdmi_spec_per_cvt *per_cvt;
2562 struct hdmi_spec_per_pin *per_pin;
d0b1252d
TI
2563
2564 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2565 if (!spec)
2566 return -ENOMEM;
2567
2568 codec->spec = spec;
bce0d2a8 2569 hdmi_array_init(spec, 1);
d0b1252d
TI
2570
2571 spec->multiout.num_dacs = 0; /* no analog */
2572 spec->multiout.max_channels = 2;
2573 spec->multiout.dig_out_nid = cvt_nid;
2574 spec->num_cvts = 1;
2575 spec->num_pins = 1;
bce0d2a8
TI
2576 per_pin = snd_array_new(&spec->pins);
2577 per_cvt = snd_array_new(&spec->cvts);
2578 if (!per_pin || !per_cvt) {
2579 simple_playback_free(codec);
2580 return -ENOMEM;
2581 }
2582 per_cvt->cvt_nid = cvt_nid;
2583 per_pin->pin_nid = pin_nid;
d0b1252d
TI
2584 spec->pcm_playback = simple_pcm_playback;
2585
2586 codec->patch_ops = simple_hdmi_patch_ops;
2587
2588 return 0;
2589}
2590
1f348522
AP
2591static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2592 int channels)
2593{
2594 unsigned int chanmask;
2595 int chan = channels ? (channels - 1) : 1;
2596
2597 switch (channels) {
2598 default:
2599 case 0:
2600 case 2:
2601 chanmask = 0x00;
2602 break;
2603 case 4:
2604 chanmask = 0x08;
2605 break;
2606 case 6:
2607 chanmask = 0x0b;
2608 break;
2609 case 8:
2610 chanmask = 0x13;
2611 break;
2612 }
2613
2614 /* Set the audio infoframe channel allocation and checksum fields. The
2615 * channel count is computed implicitly by the hardware. */
2616 snd_hda_codec_write(codec, 0x1, 0,
2617 Nv_VERB_SET_Channel_Allocation, chanmask);
2618
2619 snd_hda_codec_write(codec, 0x1, 0,
2620 Nv_VERB_SET_Info_Frame_Checksum,
2621 (0x71 - chan - chanmask));
2622}
2623
84eb01be
TI
2624static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2625 struct hda_codec *codec,
2626 struct snd_pcm_substream *substream)
2627{
2628 struct hdmi_spec *spec = codec->spec;
2629 int i;
2630
2631 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2632 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2633 for (i = 0; i < 4; i++) {
2634 /* set the stream id */
2635 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2636 AC_VERB_SET_CHANNEL_STREAMID, 0);
2637 /* set the stream format */
2638 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2639 AC_VERB_SET_STREAM_FORMAT, 0);
2640 }
2641
1f348522
AP
2642 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2643 * streams are disabled. */
2644 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2645
84eb01be
TI
2646 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2647}
2648
2649static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2650 struct hda_codec *codec,
2651 unsigned int stream_tag,
2652 unsigned int format,
2653 struct snd_pcm_substream *substream)
2654{
2655 int chs;
112daa7a 2656 unsigned int dataDCC2, channel_id;
84eb01be 2657 int i;
7c935976 2658 struct hdmi_spec *spec = codec->spec;
e3245cdd 2659 struct hda_spdif_out *spdif;
bce0d2a8 2660 struct hdmi_spec_per_cvt *per_cvt;
84eb01be
TI
2661
2662 mutex_lock(&codec->spdif_mutex);
bce0d2a8
TI
2663 per_cvt = get_cvt(spec, 0);
2664 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
84eb01be
TI
2665
2666 chs = substream->runtime->channels;
84eb01be 2667
84eb01be
TI
2668 dataDCC2 = 0x2;
2669
84eb01be 2670 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
7c935976 2671 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
84eb01be
TI
2672 snd_hda_codec_write(codec,
2673 nvhdmi_master_con_nid_7x,
2674 0,
2675 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 2676 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
84eb01be
TI
2677
2678 /* set the stream id */
2679 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2680 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2681
2682 /* set the stream format */
2683 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2684 AC_VERB_SET_STREAM_FORMAT, format);
2685
2686 /* turn on again (if needed) */
2687 /* enable and set the channel status audio/data flag */
7c935976 2688 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
84eb01be
TI
2689 snd_hda_codec_write(codec,
2690 nvhdmi_master_con_nid_7x,
2691 0,
2692 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 2693 spdif->ctls & 0xff);
84eb01be
TI
2694 snd_hda_codec_write(codec,
2695 nvhdmi_master_con_nid_7x,
2696 0,
2697 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2698 }
2699
2700 for (i = 0; i < 4; i++) {
2701 if (chs == 2)
2702 channel_id = 0;
2703 else
2704 channel_id = i * 2;
2705
2706 /* turn off SPDIF once;
2707 *otherwise the IEC958 bits won't be updated
2708 */
2709 if (codec->spdif_status_reset &&
7c935976 2710 (spdif->ctls & AC_DIG1_ENABLE))
84eb01be
TI
2711 snd_hda_codec_write(codec,
2712 nvhdmi_con_nids_7x[i],
2713 0,
2714 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 2715 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
84eb01be
TI
2716 /* set the stream id */
2717 snd_hda_codec_write(codec,
2718 nvhdmi_con_nids_7x[i],
2719 0,
2720 AC_VERB_SET_CHANNEL_STREAMID,
2721 (stream_tag << 4) | channel_id);
2722 /* set the stream format */
2723 snd_hda_codec_write(codec,
2724 nvhdmi_con_nids_7x[i],
2725 0,
2726 AC_VERB_SET_STREAM_FORMAT,
2727 format);
2728 /* turn on again (if needed) */
2729 /* enable and set the channel status audio/data flag */
2730 if (codec->spdif_status_reset &&
7c935976 2731 (spdif->ctls & AC_DIG1_ENABLE)) {
84eb01be
TI
2732 snd_hda_codec_write(codec,
2733 nvhdmi_con_nids_7x[i],
2734 0,
2735 AC_VERB_SET_DIGI_CONVERT_1,
7c935976 2736 spdif->ctls & 0xff);
84eb01be
TI
2737 snd_hda_codec_write(codec,
2738 nvhdmi_con_nids_7x[i],
2739 0,
2740 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2741 }
2742 }
2743
1f348522 2744 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
84eb01be
TI
2745
2746 mutex_unlock(&codec->spdif_mutex);
2747 return 0;
2748}
2749
fb79e1e0 2750static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
84eb01be
TI
2751 .substreams = 1,
2752 .channels_min = 2,
2753 .channels_max = 8,
2754 .nid = nvhdmi_master_con_nid_7x,
2755 .rates = SUPPORTED_RATES,
2756 .maxbps = SUPPORTED_MAXBPS,
2757 .formats = SUPPORTED_FORMATS,
2758 .ops = {
2759 .open = simple_playback_pcm_open,
2760 .close = nvhdmi_8ch_7x_pcm_close,
2761 .prepare = nvhdmi_8ch_7x_pcm_prepare
2762 },
2763};
2764
84eb01be
TI
2765static int patch_nvhdmi_2ch(struct hda_codec *codec)
2766{
2767 struct hdmi_spec *spec;
d0b1252d
TI
2768 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2769 nvhdmi_master_pin_nid_7x);
2770 if (err < 0)
2771 return err;
84eb01be 2772
ceaa86ba 2773 codec->patch_ops.init = nvhdmi_7x_init_2ch;
d0b1252d
TI
2774 /* override the PCM rates, etc, as the codec doesn't give full list */
2775 spec = codec->spec;
2776 spec->pcm_playback.rates = SUPPORTED_RATES;
2777 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2778 spec->pcm_playback.formats = SUPPORTED_FORMATS;
84eb01be
TI
2779 return 0;
2780}
2781
53775b0d
TI
2782static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2783{
2784 struct hdmi_spec *spec = codec->spec;
2785 int err = simple_playback_build_pcms(codec);
bce0d2a8
TI
2786 if (!err) {
2787 struct hda_pcm *info = get_pcm_rec(spec, 0);
2788 info->own_chmap = true;
2789 }
53775b0d
TI
2790 return err;
2791}
2792
2793static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2794{
2795 struct hdmi_spec *spec = codec->spec;
bce0d2a8 2796 struct hda_pcm *info;
53775b0d
TI
2797 struct snd_pcm_chmap *chmap;
2798 int err;
2799
2800 err = simple_playback_build_controls(codec);
2801 if (err < 0)
2802 return err;
2803
2804 /* add channel maps */
bce0d2a8
TI
2805 info = get_pcm_rec(spec, 0);
2806 err = snd_pcm_add_chmap_ctls(info->pcm,
53775b0d
TI
2807 SNDRV_PCM_STREAM_PLAYBACK,
2808 snd_pcm_alt_chmaps, 8, 0, &chmap);
2809 if (err < 0)
2810 return err;
b9a94a9c 2811 switch (codec->preset->vendor_id) {
53775b0d
TI
2812 case 0x10de0002:
2813 case 0x10de0003:
2814 case 0x10de0005:
2815 case 0x10de0006:
2816 chmap->channel_mask = (1U << 2) | (1U << 8);
2817 break;
2818 case 0x10de0007:
2819 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2820 }
2821 return 0;
2822}
2823
84eb01be
TI
2824static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2825{
2826 struct hdmi_spec *spec;
2827 int err = patch_nvhdmi_2ch(codec);
84eb01be
TI
2828 if (err < 0)
2829 return err;
2830 spec = codec->spec;
2831 spec->multiout.max_channels = 8;
d0b1252d 2832 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
ceaa86ba 2833 codec->patch_ops.init = nvhdmi_7x_init_8ch;
53775b0d
TI
2834 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2835 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
1f348522
AP
2836
2837 /* Initialize the audio infoframe channel mask and checksum to something
2838 * valid */
2839 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2840
84eb01be
TI
2841 return 0;
2842}
2843
611885bc
AH
2844/*
2845 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
2846 * - 0x10de0015
2847 * - 0x10de0040
2848 */
67b90cb8 2849static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
f302240d 2850 struct hdac_cea_channel_speaker_allocation *cap, int channels)
611885bc
AH
2851{
2852 if (cap->ca_index == 0x00 && channels == 2)
2853 return SNDRV_CTL_TLVT_CHMAP_FIXED;
2854
028cb68e
SP
2855 /* If the speaker allocation matches the channel count, it is OK. */
2856 if (cap->channels != channels)
2857 return -1;
2858
2859 /* all channels are remappable freely */
2860 return SNDRV_CTL_TLVT_CHMAP_VAR;
611885bc
AH
2861}
2862
828cb4ed
SP
2863static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
2864 int ca, int chs, unsigned char *map)
611885bc
AH
2865{
2866 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
2867 return -EINVAL;
2868
2869 return 0;
2870}
2871
2872static int patch_nvhdmi(struct hda_codec *codec)
2873{
2874 struct hdmi_spec *spec;
2875 int err;
2876
2877 err = patch_generic_hdmi(codec);
2878 if (err)
2879 return err;
2880
2881 spec = codec->spec;
75fae117 2882 spec->dyn_pin_out = true;
611885bc 2883
67b90cb8 2884 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
611885bc 2885 nvhdmi_chmap_cea_alloc_validate_get_type;
67b90cb8 2886 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
611885bc
AH
2887
2888 return 0;
2889}
2890
26e9a960
TR
2891/*
2892 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
2893 * accessed using vendor-defined verbs. These registers can be used for
2894 * interoperability between the HDA and HDMI drivers.
2895 */
2896
2897/* Audio Function Group node */
2898#define NVIDIA_AFG_NID 0x01
2899
2900/*
2901 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
2902 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
2903 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
2904 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
2905 * additional bit (at position 30) to signal the validity of the format.
2906 *
2907 * | 31 | 30 | 29 16 | 15 0 |
2908 * +---------+-------+--------+--------+
2909 * | TRIGGER | VALID | UNUSED | FORMAT |
2910 * +-----------------------------------|
2911 *
2912 * Note that for the trigger bit to take effect it needs to change value
2913 * (i.e. it needs to be toggled).
2914 */
2915#define NVIDIA_GET_SCRATCH0 0xfa6
2916#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
2917#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
2918#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
2919#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
2920#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
2921#define NVIDIA_SCRATCH_VALID (1 << 6)
2922
2923#define NVIDIA_GET_SCRATCH1 0xfab
2924#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
2925#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
2926#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
2927#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
2928
2929/*
2930 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
2931 * the format is invalidated so that the HDMI codec can be disabled.
2932 */
2933static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
2934{
2935 unsigned int value;
2936
2937 /* bits [31:30] contain the trigger and valid bits */
2938 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
2939 NVIDIA_GET_SCRATCH0, 0);
2940 value = (value >> 24) & 0xff;
2941
2942 /* bits [15:0] are used to store the HDA format */
2943 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
2944 NVIDIA_SET_SCRATCH0_BYTE0,
2945 (format >> 0) & 0xff);
2946 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
2947 NVIDIA_SET_SCRATCH0_BYTE1,
2948 (format >> 8) & 0xff);
2949
2950 /* bits [16:24] are unused */
2951 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
2952 NVIDIA_SET_SCRATCH0_BYTE2, 0);
2953
2954 /*
2955 * Bit 30 signals that the data is valid and hence that HDMI audio can
2956 * be enabled.
2957 */
2958 if (format == 0)
2959 value &= ~NVIDIA_SCRATCH_VALID;
2960 else
2961 value |= NVIDIA_SCRATCH_VALID;
2962
2963 /*
2964 * Whenever the trigger bit is toggled, an interrupt is raised in the
2965 * HDMI codec. The HDMI driver will use that as trigger to update its
2966 * configuration.
2967 */
2968 value ^= NVIDIA_SCRATCH_TRIGGER;
2969
2970 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
2971 NVIDIA_SET_SCRATCH0_BYTE3, value);
2972}
2973
2974static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
2975 struct hda_codec *codec,
2976 unsigned int stream_tag,
2977 unsigned int format,
2978 struct snd_pcm_substream *substream)
2979{
2980 int err;
2981
2982 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
2983 format, substream);
2984 if (err < 0)
2985 return err;
2986
2987 /* notify the HDMI codec of the format change */
2988 tegra_hdmi_set_format(codec, format);
2989
2990 return 0;
2991}
2992
2993static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
2994 struct hda_codec *codec,
2995 struct snd_pcm_substream *substream)
2996{
2997 /* invalidate the format in the HDMI codec */
2998 tegra_hdmi_set_format(codec, 0);
2999
3000 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3001}
3002
3003static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3004{
3005 struct hdmi_spec *spec = codec->spec;
3006 unsigned int i;
3007
3008 for (i = 0; i < spec->num_pins; i++) {
3009 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3010
3011 if (pcm->pcm_type == type)
3012 return pcm;
3013 }
3014
3015 return NULL;
3016}
3017
3018static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3019{
3020 struct hda_pcm_stream *stream;
3021 struct hda_pcm *pcm;
3022 int err;
3023
3024 err = generic_hdmi_build_pcms(codec);
3025 if (err < 0)
3026 return err;
3027
3028 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3029 if (!pcm)
3030 return -ENODEV;
3031
3032 /*
3033 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3034 * codec about format changes.
3035 */
3036 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3037 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3038 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3039
3040 return 0;
3041}
3042
3043static int patch_tegra_hdmi(struct hda_codec *codec)
3044{
3045 int err;
3046
3047 err = patch_generic_hdmi(codec);
3048 if (err)
3049 return err;
3050
3051 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3052
3053 return 0;
3054}
3055
84eb01be 3056/*
5a613584 3057 * ATI/AMD-specific implementations
84eb01be
TI
3058 */
3059
5a613584 3060#define is_amdhdmi_rev3_or_later(codec) \
7639a06c
TI
3061 ((codec)->core.vendor_id == 0x1002aa01 && \
3062 ((codec)->core.revision_id & 0xff00) >= 0x0300)
5a613584
AH
3063#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3064
3065/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3066#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3067#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3068#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3069#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3070#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3071#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
461cf6b3 3072#define ATI_VERB_SET_HBR_CONTROL 0x77c
5a613584
AH
3073#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3074#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3075#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3076#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3077#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3078#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3079#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3080#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3081#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3082#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3083#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
461cf6b3 3084#define ATI_VERB_GET_HBR_CONTROL 0xf7c
5a613584
AH
3085#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3086#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3087#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3088#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3089#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3090
84d69e79
AH
3091/* AMD specific HDA cvt verbs */
3092#define ATI_VERB_SET_RAMP_RATE 0x770
3093#define ATI_VERB_GET_RAMP_RATE 0xf70
3094
5a613584
AH
3095#define ATI_OUT_ENABLE 0x1
3096
3097#define ATI_MULTICHANNEL_MODE_PAIRED 0
3098#define ATI_MULTICHANNEL_MODE_SINGLE 1
3099
461cf6b3
AH
3100#define ATI_HBR_CAPABLE 0x01
3101#define ATI_HBR_ENABLE 0x10
3102
89250f84
AH
3103static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3104 unsigned char *buf, int *eld_size)
3105{
3106 /* call hda_eld.c ATI/AMD-specific function */
3107 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3108 is_amdhdmi_rev3_or_later(codec));
3109}
3110
5a613584
AH
3111static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3112 int active_channels, int conn_type)
3113{
3114 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3115}
3116
3117static int atihdmi_paired_swap_fc_lfe(int pos)
3118{
3119 /*
3120 * ATI/AMD have automatic FC/LFE swap built-in
3121 * when in pairwise mapping mode.
3122 */
3123
3124 switch (pos) {
3125 /* see channel_allocations[].speakers[] */
3126 case 2: return 3;
3127 case 3: return 2;
3128 default: break;
3129 }
3130
3131 return pos;
3132}
3133
828cb4ed
SP
3134static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3135 int ca, int chs, unsigned char *map)
5a613584 3136{
f302240d 3137 struct hdac_cea_channel_speaker_allocation *cap;
5a613584
AH
3138 int i, j;
3139
3140 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3141
bb63f726 3142 cap = snd_hdac_get_ch_alloc_from_ca(ca);
5a613584 3143 for (i = 0; i < chs; ++i) {
bb63f726 3144 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
5a613584
AH
3145 bool ok = false;
3146 bool companion_ok = false;
3147
3148 if (!mask)
3149 continue;
3150
3151 for (j = 0 + i % 2; j < 8; j += 2) {
3152 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3153 if (cap->speakers[chan_idx] == mask) {
3154 /* channel is in a supported position */
3155 ok = true;
3156
3157 if (i % 2 == 0 && i + 1 < chs) {
3158 /* even channel, check the odd companion */
3159 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
bb63f726 3160 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
5a613584
AH
3161 int comp_mask_act = cap->speakers[comp_chan_idx];
3162
3163 if (comp_mask_req == comp_mask_act)
3164 companion_ok = true;
3165 else
3166 return -EINVAL;
3167 }
3168 break;
3169 }
3170 }
3171
3172 if (!ok)
3173 return -EINVAL;
3174
3175 if (companion_ok)
3176 i++; /* companion channel already checked */
3177 }
3178
3179 return 0;
3180}
3181
739ffee9
SP
3182static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3183 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
5a613584 3184{
739ffee9 3185 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
5a613584
AH
3186 int verb;
3187 int ati_channel_setup = 0;
3188
3189 if (hdmi_slot > 7)
3190 return -EINVAL;
3191
3192 if (!has_amd_full_remap_support(codec)) {
3193 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3194
3195 /* In case this is an odd slot but without stream channel, do not
3196 * disable the slot since the corresponding even slot could have a
3197 * channel. In case neither have a channel, the slot pair will be
3198 * disabled when this function is called for the even slot. */
3199 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3200 return 0;
3201
3202 hdmi_slot -= hdmi_slot % 2;
3203
3204 if (stream_channel != 0xf)
3205 stream_channel -= stream_channel % 2;
3206 }
3207
3208 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3209
3210 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3211
3212 if (stream_channel != 0xf)
3213 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3214
3215 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3216}
3217
739ffee9
SP
3218static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3219 hda_nid_t pin_nid, int asp_slot)
5a613584 3220{
739ffee9 3221 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
5a613584
AH
3222 bool was_odd = false;
3223 int ati_asp_slot = asp_slot;
3224 int verb;
3225 int ati_channel_setup;
3226
3227 if (asp_slot > 7)
3228 return -EINVAL;
3229
3230 if (!has_amd_full_remap_support(codec)) {
3231 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3232 if (ati_asp_slot % 2 != 0) {
3233 ati_asp_slot -= 1;
3234 was_odd = true;
3235 }
3236 }
3237
3238 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3239
3240 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3241
3242 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3243 return 0xf;
3244
3245 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3246}
84eb01be 3247
67b90cb8
SP
3248static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3249 struct hdac_chmap *chmap,
f302240d 3250 struct hdac_cea_channel_speaker_allocation *cap,
67b90cb8 3251 int channels)
5a613584
AH
3252{
3253 int c;
3254
3255 /*
3256 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3257 * we need to take that into account (a single channel may take 2
3258 * channel slots if we need to carry a silent channel next to it).
3259 * On Rev3+ AMD codecs this function is not used.
3260 */
3261 int chanpairs = 0;
3262
3263 /* We only produce even-numbered channel count TLVs */
3264 if ((channels % 2) != 0)
3265 return -1;
3266
3267 for (c = 0; c < 7; c += 2) {
3268 if (cap->speakers[c] || cap->speakers[c+1])
3269 chanpairs++;
3270 }
3271
3272 if (chanpairs * 2 != channels)
3273 return -1;
3274
3275 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3276}
3277
828cb4ed 3278static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
f302240d
SP
3279 struct hdac_cea_channel_speaker_allocation *cap,
3280 unsigned int *chmap, int channels)
5a613584
AH
3281{
3282 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3283 int count = 0;
3284 int c;
3285
3286 for (c = 7; c >= 0; c--) {
3287 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3288 int spk = cap->speakers[chan];
3289 if (!spk) {
3290 /* add N/A channel if the companion channel is occupied */
3291 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3292 chmap[count++] = SNDRV_CHMAP_NA;
3293
3294 continue;
3295 }
3296
bb63f726 3297 chmap[count++] = snd_hdac_spk_to_chmap(spk);
5a613584
AH
3298 }
3299
3300 WARN_ON(count != channels);
3301}
3302
461cf6b3
AH
3303static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3304 bool hbr)
3305{
3306 int hbr_ctl, hbr_ctl_new;
3307
3308 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
13122e6e 3309 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
461cf6b3
AH
3310 if (hbr)
3311 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3312 else
3313 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3314
4e76a883
TI
3315 codec_dbg(codec,
3316 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
461cf6b3
AH
3317 pin_nid,
3318 hbr_ctl == hbr_ctl_new ? "" : "new-",
3319 hbr_ctl_new);
3320
3321 if (hbr_ctl != hbr_ctl_new)
3322 snd_hda_codec_write(codec, pin_nid, 0,
3323 ATI_VERB_SET_HBR_CONTROL,
3324 hbr_ctl_new);
3325
3326 } else if (hbr)
3327 return -EINVAL;
3328
3329 return 0;
3330}
3331
84d69e79
AH
3332static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3333 hda_nid_t pin_nid, u32 stream_tag, int format)
3334{
3335
3336 if (is_amdhdmi_rev3_or_later(codec)) {
3337 int ramp_rate = 180; /* default as per AMD spec */
3338 /* disable ramp-up/down for non-pcm as per AMD spec */
3339 if (format & AC_FMT_TYPE_NON_PCM)
3340 ramp_rate = 0;
3341
3342 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3343 }
3344
3345 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3346}
3347
3348
5a613584 3349static int atihdmi_init(struct hda_codec *codec)
84eb01be
TI
3350{
3351 struct hdmi_spec *spec = codec->spec;
5a613584 3352 int pin_idx, err;
84eb01be 3353
5a613584
AH
3354 err = generic_hdmi_init(codec);
3355
3356 if (err)
84eb01be 3357 return err;
5a613584
AH
3358
3359 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3360 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3361
3362 /* make sure downmix information in infoframe is zero */
3363 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3364
3365 /* enable channel-wise remap mode if supported */
3366 if (has_amd_full_remap_support(codec))
3367 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3368 ATI_VERB_SET_MULTICHANNEL_MODE,
3369 ATI_MULTICHANNEL_MODE_SINGLE);
84eb01be 3370 }
5a613584 3371
84eb01be
TI
3372 return 0;
3373}
3374
84eb01be
TI
3375static int patch_atihdmi(struct hda_codec *codec)
3376{
3377 struct hdmi_spec *spec;
5a613584
AH
3378 struct hdmi_spec_per_cvt *per_cvt;
3379 int err, cvt_idx;
3380
3381 err = patch_generic_hdmi(codec);
3382
3383 if (err)
d0b1252d 3384 return err;
5a613584
AH
3385
3386 codec->patch_ops.init = atihdmi_init;
3387
d0b1252d 3388 spec = codec->spec;
5a613584 3389
89250f84 3390 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
5a613584 3391 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
461cf6b3 3392 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
84d69e79 3393 spec->ops.setup_stream = atihdmi_setup_stream;
5a613584
AH
3394
3395 if (!has_amd_full_remap_support(codec)) {
3396 /* override to ATI/AMD-specific versions with pairwise mapping */
67b90cb8 3397 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
5a613584 3398 atihdmi_paired_chmap_cea_alloc_validate_get_type;
67b90cb8
SP
3399 spec->chmap.ops.cea_alloc_to_tlv_chmap =
3400 atihdmi_paired_cea_alloc_to_tlv_chmap;
3401 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
739ffee9
SP
3402 spec->chmap.ops.pin_get_slot_channel =
3403 atihdmi_pin_get_slot_channel;
3404 spec->chmap.ops.pin_set_slot_channel =
3405 atihdmi_pin_set_slot_channel;
5a613584
AH
3406 }
3407
3408 /* ATI/AMD converters do not advertise all of their capabilities */
3409 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3410 per_cvt = get_cvt(spec, cvt_idx);
3411 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3412 per_cvt->rates |= SUPPORTED_RATES;
3413 per_cvt->formats |= SUPPORTED_FORMATS;
3414 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3415 }
3416
67b90cb8 3417 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
5a613584 3418
84eb01be
TI
3419 return 0;
3420}
3421
3de5ff88
AL
3422/* VIA HDMI Implementation */
3423#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3424#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3425
3de5ff88
AL
3426static int patch_via_hdmi(struct hda_codec *codec)
3427{
250e41ac 3428 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
3de5ff88 3429}
84eb01be
TI
3430
3431/*
3432 * patch entries
3433 */
b9a94a9c
TI
3434static const struct hda_device_id snd_hda_id_hdmi[] = {
3435HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3436HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3437HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3438HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3439HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3440HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3441HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3442HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3443HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3444HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3445HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3446HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3447HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3448HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3449HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3450HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3451HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3452HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3453HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3454HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3455HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3456HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3457HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
c8900a0f 3458/* 17 is known to be absent */
b9a94a9c
TI
3459HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3460HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3461HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3462HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3463HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3464HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3465HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3466HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3467HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3468HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3469HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3470HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3471HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3472HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3473HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3474HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3475HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3476HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3477HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3478HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3479HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
2d369c74 3480HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
3ec622f4 3481HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
b9a94a9c
TI
3482HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3483HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3484HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3485HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3486HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3487HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
3488HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3489HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3490HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3491HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
3492HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
3493HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
3494HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
3495HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
3496HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
3497HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
91815d8a 3498HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
b9a94a9c
TI
3499HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
3500HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
3501HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
3502HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
d8a766a1 3503/* special ID for generic HDMI */
b9a94a9c 3504HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
84eb01be
TI
3505{} /* terminator */
3506};
b9a94a9c 3507MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
84eb01be
TI
3508
3509MODULE_LICENSE("GPL");
3510MODULE_DESCRIPTION("HDMI HD-audio codec");
3511MODULE_ALIAS("snd-hda-codec-intelhdmi");
3512MODULE_ALIAS("snd-hda-codec-nvhdmi");
3513MODULE_ALIAS("snd-hda-codec-atihdmi");
3514
d8a766a1 3515static struct hda_codec_driver hdmi_driver = {
b9a94a9c 3516 .id = snd_hda_id_hdmi,
84eb01be
TI
3517};
3518
d8a766a1 3519module_hda_codec_driver(hdmi_driver);
This page took 0.435915 seconds and 5 git commands to generate.