blob: 31ba21b805041458fe600f0b5ff3096c609ef12b [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *
4 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
5 *
6 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
7 * Copyright (c) 2006 ATI Technologies Inc.
8 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
9 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
10 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
11 *
12 * Authors:
13 * Wu Fengguang <wfg@linux.intel.com>
14 *
15 * Maintained by:
16 * Wu Fengguang <wfg@linux.intel.com>
17 */
18
19#include <linux/init.h>
20#include <linux/delay.h>
21#include <linux/pci.h>
22#include <linux/slab.h>
23#include <linux/module.h>
24#include <linux/pm_runtime.h>
25#include <sound/core.h>
26#include <sound/jack.h>
27#include <sound/asoundef.h>
28#include <sound/tlv.h>
29#include <sound/hdaudio.h>
30#include <sound/hda_i915.h>
31#include <sound/hda_chmap.h>
32#include <sound/hda_codec.h>
33#include "hda_local.h"
34#include "hda_jack.h"
35#include "hda_controller.h"
36
37static bool static_hdmi_pcm;
38module_param(static_hdmi_pcm, bool, 0644);
39MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
40
41#define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
42#define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
43#define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
44#define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
45#define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
46#define is_geminilake(codec) (((codec)->core.vendor_id == 0x8086280d) || \
47 ((codec)->core.vendor_id == 0x80862800))
48#define is_cannonlake(codec) ((codec)->core.vendor_id == 0x8086280c)
49#define is_icelake(codec) ((codec)->core.vendor_id == 0x8086280f)
50#define is_tigerlake(codec) ((codec)->core.vendor_id == 0x80862812)
51#define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
52 || is_skylake(codec) || is_broxton(codec) \
53 || is_kabylake(codec) || is_geminilake(codec) \
54 || is_cannonlake(codec) || is_icelake(codec) \
55 || is_tigerlake(codec))
56#define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
57#define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
58#define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
59
60static bool enable_acomp = true;
61module_param(enable_acomp, bool, 0444);
62MODULE_PARM_DESC(enable_acomp, "Enable audio component binding (default=yes)");
63
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};
73
74/* max. connections to a widget */
75#define HDA_MAX_CONNECTIONS 32
76
77struct hdmi_spec_per_pin {
78 hda_nid_t pin_nid;
79 int dev_id;
80 /* pin idx, different device entries on the same pin use the same idx */
81 int pin_nid_idx;
82 int num_mux_nids;
83 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
84 int mux_idx;
85 hda_nid_t cvt_nid;
86
87 struct hda_codec *codec;
88 struct hdmi_eld sink_eld;
89 struct mutex lock;
90 struct delayed_work work;
91 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
92 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
93 int repoll_count;
94 bool setup; /* the stream has been set up by prepare callback */
95 int channels; /* current number of channels */
96 bool non_pcm;
97 bool chmap_set; /* channel-map override by ALSA API? */
98 unsigned char chmap[8]; /* ALSA API channel-map */
99#ifdef CONFIG_SND_PROC_FS
100 struct snd_info_entry *proc_entry;
101#endif
102};
103
104/* operations used by generic code that can be overridden by patches */
105struct hdmi_ops {
106 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
107 unsigned char *buf, int *eld_size);
108
109 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
110 int ca, int active_channels, int conn_type);
111
112 /* enable/disable HBR (HD passthrough) */
113 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
114
115 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
116 hda_nid_t pin_nid, u32 stream_tag, int format);
117
118 void (*pin_cvt_fixup)(struct hda_codec *codec,
119 struct hdmi_spec_per_pin *per_pin,
120 hda_nid_t cvt_nid);
121};
122
123struct hdmi_pcm {
124 struct hda_pcm *pcm;
125 struct snd_jack *jack;
126 struct snd_kcontrol *eld_ctl;
127};
128
129struct hdmi_spec {
130 struct hda_codec *codec;
131 int num_cvts;
132 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
133 hda_nid_t cvt_nids[4]; /* only for haswell fix */
134
135 /*
136 * num_pins is the number of virtual pins
137 * for example, there are 3 pins, and each pin
138 * has 4 device entries, then the num_pins is 12
139 */
140 int num_pins;
141 /*
142 * num_nids is the number of real pins
143 * In the above example, num_nids is 3
144 */
145 int num_nids;
146 /*
147 * dev_num is the number of device entries
148 * on each pin.
149 * In the above example, dev_num is 4
150 */
151 int dev_num;
152 struct snd_array pins; /* struct hdmi_spec_per_pin */
153 struct hdmi_pcm pcm_rec[16];
154 struct mutex pcm_lock;
155 struct mutex bind_lock; /* for audio component binding */
156 /* pcm_bitmap means which pcms have been assigned to pins*/
157 unsigned long pcm_bitmap;
158 int pcm_used; /* counter of pcm_rec[] */
159 /* bitmap shows whether the pcm is opened in user space
160 * bit 0 means the first playback PCM (PCM3);
161 * bit 1 means the second playback PCM, and so on.
162 */
163 unsigned long pcm_in_use;
164
165 struct hdmi_eld temp_eld;
166 struct hdmi_ops ops;
167
168 bool dyn_pin_out;
169 bool dyn_pcm_assign;
170 /*
171 * Non-generic VIA/NVIDIA specific
172 */
173 struct hda_multi_out multiout;
174 struct hda_pcm_stream pcm_playback;
175
176 bool use_jack_detect; /* jack detection enabled */
177 bool use_acomp_notifier; /* use eld_notify callback for hotplug */
178 bool acomp_registered; /* audio component registered in this driver */
179 bool force_connect; /* force connectivity */
180 struct drm_audio_component_audio_ops drm_audio_ops;
181 int (*port2pin)(struct hda_codec *, int); /* reverse port/pin mapping */
182
183 struct hdac_chmap chmap;
184 hda_nid_t vendor_nid;
185 const int *port_map;
186 int port_num;
187};
188
189#ifdef CONFIG_SND_HDA_COMPONENT
190static inline bool codec_has_acomp(struct hda_codec *codec)
191{
192 struct hdmi_spec *spec = codec->spec;
193 return spec->use_acomp_notifier;
194}
195#else
196#define codec_has_acomp(codec) false
197#endif
198
199struct hdmi_audio_infoframe {
200 u8 type; /* 0x84 */
201 u8 ver; /* 0x01 */
202 u8 len; /* 0x0a */
203
204 u8 checksum;
205
206 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
207 u8 SS01_SF24;
208 u8 CXT04;
209 u8 CA;
210 u8 LFEPBL01_LSV36_DM_INH7;
211};
212
213struct dp_audio_infoframe {
214 u8 type; /* 0x84 */
215 u8 len; /* 0x1b */
216 u8 ver; /* 0x11 << 2 */
217
218 u8 CC02_CT47; /* match with HDMI infoframe from this on */
219 u8 SS01_SF24;
220 u8 CXT04;
221 u8 CA;
222 u8 LFEPBL01_LSV36_DM_INH7;
223};
224
225union audio_infoframe {
226 struct hdmi_audio_infoframe hdmi;
227 struct dp_audio_infoframe dp;
228 u8 bytes[0];
229};
230
231/*
232 * HDMI routines
233 */
234
235#define get_pin(spec, idx) \
236 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
237#define get_cvt(spec, idx) \
238 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
239/* obtain hdmi_pcm object assigned to idx */
240#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
241/* obtain hda_pcm object assigned to idx */
242#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
243
244static int pin_id_to_pin_index(struct hda_codec *codec,
245 hda_nid_t pin_nid, int dev_id)
246{
247 struct hdmi_spec *spec = codec->spec;
248 int pin_idx;
249 struct hdmi_spec_per_pin *per_pin;
250
251 /*
252 * (dev_id == -1) means it is NON-MST pin
253 * return the first virtual pin on this port
254 */
255 if (dev_id == -1)
256 dev_id = 0;
257
258 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
259 per_pin = get_pin(spec, pin_idx);
260 if ((per_pin->pin_nid == pin_nid) &&
261 (per_pin->dev_id == dev_id))
262 return pin_idx;
263 }
264
265 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
266 return -EINVAL;
267}
268
269static int hinfo_to_pcm_index(struct hda_codec *codec,
270 struct hda_pcm_stream *hinfo)
271{
272 struct hdmi_spec *spec = codec->spec;
273 int pcm_idx;
274
275 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
276 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
277 return pcm_idx;
278
279 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
280 return -EINVAL;
281}
282
283static int hinfo_to_pin_index(struct hda_codec *codec,
284 struct hda_pcm_stream *hinfo)
285{
286 struct hdmi_spec *spec = codec->spec;
287 struct hdmi_spec_per_pin *per_pin;
288 int pin_idx;
289
290 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
291 per_pin = get_pin(spec, pin_idx);
292 if (per_pin->pcm &&
293 per_pin->pcm->pcm->stream == hinfo)
294 return pin_idx;
295 }
296
297 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
298 return -EINVAL;
299}
300
301static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
302 int pcm_idx)
303{
304 int i;
305 struct hdmi_spec_per_pin *per_pin;
306
307 for (i = 0; i < spec->num_pins; i++) {
308 per_pin = get_pin(spec, i);
309 if (per_pin->pcm_idx == pcm_idx)
310 return per_pin;
311 }
312 return NULL;
313}
314
315static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
316{
317 struct hdmi_spec *spec = codec->spec;
318 int cvt_idx;
319
320 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
321 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
322 return cvt_idx;
323
324 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
325 return -EINVAL;
326}
327
328static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
329 struct snd_ctl_elem_info *uinfo)
330{
331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
332 struct hdmi_spec *spec = codec->spec;
333 struct hdmi_spec_per_pin *per_pin;
334 struct hdmi_eld *eld;
335 int pcm_idx;
336
337 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
338
339 pcm_idx = kcontrol->private_value;
340 mutex_lock(&spec->pcm_lock);
341 per_pin = pcm_idx_to_pin(spec, pcm_idx);
342 if (!per_pin) {
343 /* no pin is bound to the pcm */
344 uinfo->count = 0;
345 goto unlock;
346 }
347 eld = &per_pin->sink_eld;
348 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
349
350 unlock:
351 mutex_unlock(&spec->pcm_lock);
352 return 0;
353}
354
355static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
356 struct snd_ctl_elem_value *ucontrol)
357{
358 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
359 struct hdmi_spec *spec = codec->spec;
360 struct hdmi_spec_per_pin *per_pin;
361 struct hdmi_eld *eld;
362 int pcm_idx;
363 int err = 0;
364
365 pcm_idx = kcontrol->private_value;
366 mutex_lock(&spec->pcm_lock);
367 per_pin = pcm_idx_to_pin(spec, pcm_idx);
368 if (!per_pin) {
369 /* no pin is bound to the pcm */
370 memset(ucontrol->value.bytes.data, 0,
371 ARRAY_SIZE(ucontrol->value.bytes.data));
372 goto unlock;
373 }
374
375 eld = &per_pin->sink_eld;
376 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
377 eld->eld_size > ELD_MAX_SIZE) {
378 snd_BUG();
379 err = -EINVAL;
380 goto unlock;
381 }
382
383 memset(ucontrol->value.bytes.data, 0,
384 ARRAY_SIZE(ucontrol->value.bytes.data));
385 if (eld->eld_valid)
386 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
387 eld->eld_size);
388
389 unlock:
390 mutex_unlock(&spec->pcm_lock);
391 return err;
392}
393
394static const struct snd_kcontrol_new eld_bytes_ctl = {
395 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
396 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
397 .name = "ELD",
398 .info = hdmi_eld_ctl_info,
399 .get = hdmi_eld_ctl_get,
400};
401
402static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
403 int device)
404{
405 struct snd_kcontrol *kctl;
406 struct hdmi_spec *spec = codec->spec;
407 int err;
408
409 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
410 if (!kctl)
411 return -ENOMEM;
412 kctl->private_value = pcm_idx;
413 kctl->id.device = device;
414
415 /* no pin nid is associated with the kctl now
416 * tbd: associate pin nid to eld ctl later
417 */
418 err = snd_hda_ctl_add(codec, 0, kctl);
419 if (err < 0)
420 return err;
421
422 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
423 return 0;
424}
425
426#ifdef BE_PARANOID
427static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
428 int *packet_index, int *byte_index)
429{
430 int val;
431
432 val = snd_hda_codec_read(codec, pin_nid, 0,
433 AC_VERB_GET_HDMI_DIP_INDEX, 0);
434
435 *packet_index = val >> 5;
436 *byte_index = val & 0x1f;
437}
438#endif
439
440static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
441 int packet_index, int byte_index)
442{
443 int val;
444
445 val = (packet_index << 5) | (byte_index & 0x1f);
446
447 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
448}
449
450static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
451 unsigned char val)
452{
453 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
454}
455
456static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
457{
458 struct hdmi_spec *spec = codec->spec;
459 int pin_out;
460
461 /* Unmute */
462 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
463 snd_hda_codec_write(codec, pin_nid, 0,
464 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
465
466 if (spec->dyn_pin_out)
467 /* Disable pin out until stream is active */
468 pin_out = 0;
469 else
470 /* Enable pin out: some machines with GM965 gets broken output
471 * when the pin is disabled or changed while using with HDMI
472 */
473 pin_out = PIN_OUT;
474
475 snd_hda_codec_write(codec, pin_nid, 0,
476 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
477}
478
479/*
480 * ELD proc files
481 */
482
483#ifdef CONFIG_SND_PROC_FS
484static void print_eld_info(struct snd_info_entry *entry,
485 struct snd_info_buffer *buffer)
486{
487 struct hdmi_spec_per_pin *per_pin = entry->private_data;
488
489 mutex_lock(&per_pin->lock);
490 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
491 mutex_unlock(&per_pin->lock);
492}
493
494static void write_eld_info(struct snd_info_entry *entry,
495 struct snd_info_buffer *buffer)
496{
497 struct hdmi_spec_per_pin *per_pin = entry->private_data;
498
499 mutex_lock(&per_pin->lock);
500 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
501 mutex_unlock(&per_pin->lock);
502}
503
504static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
505{
506 char name[32];
507 struct hda_codec *codec = per_pin->codec;
508 struct snd_info_entry *entry;
509 int err;
510
511 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
512 err = snd_card_proc_new(codec->card, name, &entry);
513 if (err < 0)
514 return err;
515
516 snd_info_set_text_ops(entry, per_pin, print_eld_info);
517 entry->c.text.write = write_eld_info;
518 entry->mode |= 0200;
519 per_pin->proc_entry = entry;
520
521 return 0;
522}
523
524static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
525{
526 if (!per_pin->codec->bus->shutdown) {
527 snd_info_free_entry(per_pin->proc_entry);
528 per_pin->proc_entry = NULL;
529 }
530}
531#else
532static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
533 int index)
534{
535 return 0;
536}
537static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
538{
539}
540#endif
541
542/*
543 * Audio InfoFrame routines
544 */
545
546/*
547 * Enable Audio InfoFrame Transmission
548 */
549static void hdmi_start_infoframe_trans(struct hda_codec *codec,
550 hda_nid_t pin_nid)
551{
552 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
553 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
554 AC_DIPXMIT_BEST);
555}
556
557/*
558 * Disable Audio InfoFrame Transmission
559 */
560static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
561 hda_nid_t pin_nid)
562{
563 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
564 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
565 AC_DIPXMIT_DISABLE);
566}
567
568static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
569{
570#ifdef CONFIG_SND_DEBUG_VERBOSE
571 int i;
572 int size;
573
574 size = snd_hdmi_get_eld_size(codec, pin_nid);
575 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
576
577 for (i = 0; i < 8; i++) {
578 size = snd_hda_codec_read(codec, pin_nid, 0,
579 AC_VERB_GET_HDMI_DIP_SIZE, i);
580 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
581 }
582#endif
583}
584
585static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
586{
587#ifdef BE_PARANOID
588 int i, j;
589 int size;
590 int pi, bi;
591 for (i = 0; i < 8; i++) {
592 size = snd_hda_codec_read(codec, pin_nid, 0,
593 AC_VERB_GET_HDMI_DIP_SIZE, i);
594 if (size == 0)
595 continue;
596
597 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
598 for (j = 1; j < 1000; j++) {
599 hdmi_write_dip_byte(codec, pin_nid, 0x0);
600 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
601 if (pi != i)
602 codec_dbg(codec, "dip index %d: %d != %d\n",
603 bi, pi, i);
604 if (bi == 0) /* byte index wrapped around */
605 break;
606 }
607 codec_dbg(codec,
608 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
609 i, size, j);
610 }
611#endif
612}
613
614static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
615{
616 u8 *bytes = (u8 *)hdmi_ai;
617 u8 sum = 0;
618 int i;
619
620 hdmi_ai->checksum = 0;
621
622 for (i = 0; i < sizeof(*hdmi_ai); i++)
623 sum += bytes[i];
624
625 hdmi_ai->checksum = -sum;
626}
627
628static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
629 hda_nid_t pin_nid,
630 u8 *dip, int size)
631{
632 int i;
633
634 hdmi_debug_dip_size(codec, pin_nid);
635 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
636
637 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
638 for (i = 0; i < size; i++)
639 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
640}
641
642static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
643 u8 *dip, int size)
644{
645 u8 val;
646 int i;
647
648 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
649 != AC_DIPXMIT_BEST)
650 return false;
651
652 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
653 for (i = 0; i < size; i++) {
654 val = snd_hda_codec_read(codec, pin_nid, 0,
655 AC_VERB_GET_HDMI_DIP_DATA, 0);
656 if (val != dip[i])
657 return false;
658 }
659
660 return true;
661}
662
663static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
664 hda_nid_t pin_nid,
665 int ca, int active_channels,
666 int conn_type)
667{
668 union audio_infoframe ai;
669
670 memset(&ai, 0, sizeof(ai));
671 if (conn_type == 0) { /* HDMI */
672 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
673
674 hdmi_ai->type = 0x84;
675 hdmi_ai->ver = 0x01;
676 hdmi_ai->len = 0x0a;
677 hdmi_ai->CC02_CT47 = active_channels - 1;
678 hdmi_ai->CA = ca;
679 hdmi_checksum_audio_infoframe(hdmi_ai);
680 } else if (conn_type == 1) { /* DisplayPort */
681 struct dp_audio_infoframe *dp_ai = &ai.dp;
682
683 dp_ai->type = 0x84;
684 dp_ai->len = 0x1b;
685 dp_ai->ver = 0x11 << 2;
686 dp_ai->CC02_CT47 = active_channels - 1;
687 dp_ai->CA = ca;
688 } else {
689 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
690 pin_nid);
691 return;
692 }
693
694 /*
695 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
696 * sizeof(*dp_ai) to avoid partial match/update problems when
697 * the user switches between HDMI/DP monitors.
698 */
699 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
700 sizeof(ai))) {
701 codec_dbg(codec,
702 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
703 pin_nid,
704 active_channels, ca);
705 hdmi_stop_infoframe_trans(codec, pin_nid);
706 hdmi_fill_audio_infoframe(codec, pin_nid,
707 ai.bytes, sizeof(ai));
708 hdmi_start_infoframe_trans(codec, pin_nid);
709 }
710}
711
712static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
713 struct hdmi_spec_per_pin *per_pin,
714 bool non_pcm)
715{
716 struct hdmi_spec *spec = codec->spec;
717 struct hdac_chmap *chmap = &spec->chmap;
718 hda_nid_t pin_nid = per_pin->pin_nid;
719 int channels = per_pin->channels;
720 int active_channels;
721 struct hdmi_eld *eld;
722 int ca;
723
724 if (!channels)
725 return;
726
727 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
728 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
729 snd_hda_codec_write(codec, pin_nid, 0,
730 AC_VERB_SET_AMP_GAIN_MUTE,
731 AMP_OUT_UNMUTE);
732
733 eld = &per_pin->sink_eld;
734
735 ca = snd_hdac_channel_allocation(&codec->core,
736 eld->info.spk_alloc, channels,
737 per_pin->chmap_set, non_pcm, per_pin->chmap);
738
739 active_channels = snd_hdac_get_active_channels(ca);
740
741 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
742 active_channels);
743
744 /*
745 * always configure channel mapping, it may have been changed by the
746 * user in the meantime
747 */
748 snd_hdac_setup_channel_mapping(&spec->chmap,
749 pin_nid, non_pcm, ca, channels,
750 per_pin->chmap, per_pin->chmap_set);
751
752 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
753 eld->info.conn_type);
754
755 per_pin->non_pcm = non_pcm;
756}
757
758/*
759 * Unsolicited events
760 */
761
762static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
763
764static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid,
765 int dev_id)
766{
767 struct hdmi_spec *spec = codec->spec;
768 int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
769
770 if (pin_idx < 0)
771 return;
772 mutex_lock(&spec->pcm_lock);
773 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
774 snd_hda_jack_report_sync(codec);
775 mutex_unlock(&spec->pcm_lock);
776}
777
778static void jack_callback(struct hda_codec *codec,
779 struct hda_jack_callback *jack)
780{
781 /* stop polling when notification is enabled */
782 if (codec_has_acomp(codec))
783 return;
784
785 /* hda_jack don't support DP MST */
786 check_presence_and_report(codec, jack->nid, 0);
787}
788
789static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
790{
791 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
792 struct hda_jack_tbl *jack;
793 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
794
795 /*
796 * assume DP MST uses dyn_pcm_assign and acomp and
797 * never comes here
798 * if DP MST supports unsol event, below code need
799 * consider dev_entry
800 */
801 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
802 if (!jack)
803 return;
804 jack->jack_dirty = 1;
805
806 codec_dbg(codec,
807 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
808 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
809 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
810
811 /* hda_jack don't support DP MST */
812 check_presence_and_report(codec, jack->nid, 0);
813}
814
815static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
816{
817 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
818 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
819 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
820 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
821
822 codec_info(codec,
823 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
824 codec->addr,
825 tag,
826 subtag,
827 cp_state,
828 cp_ready);
829
830 /* TODO */
831 if (cp_state)
832 ;
833 if (cp_ready)
834 ;
835}
836
837
838static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
839{
840 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
841 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
842
843 if (codec_has_acomp(codec))
844 return;
845
846 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
847 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
848 return;
849 }
850
851 if (subtag == 0)
852 hdmi_intrinsic_event(codec, res);
853 else
854 hdmi_non_intrinsic_event(codec, res);
855}
856
857static void haswell_verify_D0(struct hda_codec *codec,
858 hda_nid_t cvt_nid, hda_nid_t nid)
859{
860 int pwr;
861
862 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
863 * thus pins could only choose converter 0 for use. Make sure the
864 * converters are in correct power state */
865 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
866 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
867
868 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
869 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
870 AC_PWRST_D0);
871 msleep(40);
872 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
873 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
874 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
875 }
876}
877
878/*
879 * Callbacks
880 */
881
882/* HBR should be Non-PCM, 8 channels */
883#define is_hbr_format(format) \
884 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
885
886static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
887 bool hbr)
888{
889 int pinctl, new_pinctl;
890
891 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
892 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
893 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
894
895 if (pinctl < 0)
896 return hbr ? -EINVAL : 0;
897
898 new_pinctl = pinctl & ~AC_PINCTL_EPT;
899 if (hbr)
900 new_pinctl |= AC_PINCTL_EPT_HBR;
901 else
902 new_pinctl |= AC_PINCTL_EPT_NATIVE;
903
904 codec_dbg(codec,
905 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
906 pin_nid,
907 pinctl == new_pinctl ? "" : "new-",
908 new_pinctl);
909
910 if (pinctl != new_pinctl)
911 snd_hda_codec_write(codec, pin_nid, 0,
912 AC_VERB_SET_PIN_WIDGET_CONTROL,
913 new_pinctl);
914 } else if (hbr)
915 return -EINVAL;
916
917 return 0;
918}
919
920static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
921 hda_nid_t pin_nid, u32 stream_tag, int format)
922{
923 struct hdmi_spec *spec = codec->spec;
924 unsigned int param;
925 int err;
926
927 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
928
929 if (err) {
930 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
931 return err;
932 }
933
934 if (is_haswell_plus(codec)) {
935
936 /*
937 * on recent platforms IEC Coding Type is required for HBR
938 * support, read current Digital Converter settings and set
939 * ICT bitfield if needed.
940 */
941 param = snd_hda_codec_read(codec, cvt_nid, 0,
942 AC_VERB_GET_DIGI_CONVERT_1, 0);
943
944 param = (param >> 16) & ~(AC_DIG3_ICT);
945
946 /* on recent platforms ICT mode is required for HBR support */
947 if (is_hbr_format(format))
948 param |= 0x1;
949
950 snd_hda_codec_write(codec, cvt_nid, 0,
951 AC_VERB_SET_DIGI_CONVERT_3, param);
952 }
953
954 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
955 return 0;
956}
957
958/* Try to find an available converter
959 * If pin_idx is less then zero, just try to find an available converter.
960 * Otherwise, try to find an available converter and get the cvt mux index
961 * of the pin.
962 */
963static int hdmi_choose_cvt(struct hda_codec *codec,
964 int pin_idx, int *cvt_id)
965{
966 struct hdmi_spec *spec = codec->spec;
967 struct hdmi_spec_per_pin *per_pin;
968 struct hdmi_spec_per_cvt *per_cvt = NULL;
969 int cvt_idx, mux_idx = 0;
970
971 /* pin_idx < 0 means no pin will be bound to the converter */
972 if (pin_idx < 0)
973 per_pin = NULL;
974 else
975 per_pin = get_pin(spec, pin_idx);
976
977 /* Dynamically assign converter to stream */
978 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
979 per_cvt = get_cvt(spec, cvt_idx);
980
981 /* Must not already be assigned */
982 if (per_cvt->assigned)
983 continue;
984 if (per_pin == NULL)
985 break;
986 /* Must be in pin's mux's list of converters */
987 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
988 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
989 break;
990 /* Not in mux list */
991 if (mux_idx == per_pin->num_mux_nids)
992 continue;
993 break;
994 }
995
996 /* No free converters */
997 if (cvt_idx == spec->num_cvts)
998 return -EBUSY;
999
1000 if (per_pin != NULL)
1001 per_pin->mux_idx = mux_idx;
1002
1003 if (cvt_id)
1004 *cvt_id = cvt_idx;
1005
1006 return 0;
1007}
1008
1009/* Assure the pin select the right convetor */
1010static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1011 struct hdmi_spec_per_pin *per_pin)
1012{
1013 hda_nid_t pin_nid = per_pin->pin_nid;
1014 int mux_idx, curr;
1015
1016 mux_idx = per_pin->mux_idx;
1017 curr = snd_hda_codec_read(codec, pin_nid, 0,
1018 AC_VERB_GET_CONNECT_SEL, 0);
1019 if (curr != mux_idx)
1020 snd_hda_codec_write_cache(codec, pin_nid, 0,
1021 AC_VERB_SET_CONNECT_SEL,
1022 mux_idx);
1023}
1024
1025/* get the mux index for the converter of the pins
1026 * converter's mux index is the same for all pins on Intel platform
1027 */
1028static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1029 hda_nid_t cvt_nid)
1030{
1031 int i;
1032
1033 for (i = 0; i < spec->num_cvts; i++)
1034 if (spec->cvt_nids[i] == cvt_nid)
1035 return i;
1036 return -EINVAL;
1037}
1038
1039/* Intel HDMI workaround to fix audio routing issue:
1040 * For some Intel display codecs, pins share the same connection list.
1041 * So a conveter can be selected by multiple pins and playback on any of these
1042 * pins will generate sound on the external display, because audio flows from
1043 * the same converter to the display pipeline. Also muting one pin may make
1044 * other pins have no sound output.
1045 * So this function assures that an assigned converter for a pin is not selected
1046 * by any other pins.
1047 */
1048static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1049 hda_nid_t pin_nid,
1050 int dev_id, int mux_idx)
1051{
1052 struct hdmi_spec *spec = codec->spec;
1053 hda_nid_t nid;
1054 int cvt_idx, curr;
1055 struct hdmi_spec_per_cvt *per_cvt;
1056 struct hdmi_spec_per_pin *per_pin;
1057 int pin_idx;
1058
1059 /* configure the pins connections */
1060 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1061 int dev_id_saved;
1062 int dev_num;
1063
1064 per_pin = get_pin(spec, pin_idx);
1065 /*
1066 * pin not connected to monitor
1067 * no need to operate on it
1068 */
1069 if (!per_pin->pcm)
1070 continue;
1071
1072 if ((per_pin->pin_nid == pin_nid) &&
1073 (per_pin->dev_id == dev_id))
1074 continue;
1075
1076 /*
1077 * if per_pin->dev_id >= dev_num,
1078 * snd_hda_get_dev_select() will fail,
1079 * and the following operation is unpredictable.
1080 * So skip this situation.
1081 */
1082 dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1;
1083 if (per_pin->dev_id >= dev_num)
1084 continue;
1085
1086 nid = per_pin->pin_nid;
1087
1088 /*
1089 * Calling this function should not impact
1090 * on the device entry selection
1091 * So let's save the dev id for each pin,
1092 * and restore it when return
1093 */
1094 dev_id_saved = snd_hda_get_dev_select(codec, nid);
1095 snd_hda_set_dev_select(codec, nid, per_pin->dev_id);
1096 curr = snd_hda_codec_read(codec, nid, 0,
1097 AC_VERB_GET_CONNECT_SEL, 0);
1098 if (curr != mux_idx) {
1099 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1100 continue;
1101 }
1102
1103
1104 /* choose an unassigned converter. The conveters in the
1105 * connection list are in the same order as in the codec.
1106 */
1107 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1108 per_cvt = get_cvt(spec, cvt_idx);
1109 if (!per_cvt->assigned) {
1110 codec_dbg(codec,
1111 "choose cvt %d for pin nid %d\n",
1112 cvt_idx, nid);
1113 snd_hda_codec_write_cache(codec, nid, 0,
1114 AC_VERB_SET_CONNECT_SEL,
1115 cvt_idx);
1116 break;
1117 }
1118 }
1119 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1120 }
1121}
1122
1123/* A wrapper of intel_not_share_asigned_cvt() */
1124static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1125 hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
1126{
1127 int mux_idx;
1128 struct hdmi_spec *spec = codec->spec;
1129
1130 /* On Intel platform, the mapping of converter nid to
1131 * mux index of the pins are always the same.
1132 * The pin nid may be 0, this means all pins will not
1133 * share the converter.
1134 */
1135 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1136 if (mux_idx >= 0)
1137 intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx);
1138}
1139
1140/* skeleton caller of pin_cvt_fixup ops */
1141static void pin_cvt_fixup(struct hda_codec *codec,
1142 struct hdmi_spec_per_pin *per_pin,
1143 hda_nid_t cvt_nid)
1144{
1145 struct hdmi_spec *spec = codec->spec;
1146
1147 if (spec->ops.pin_cvt_fixup)
1148 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1149}
1150
1151/* called in hdmi_pcm_open when no pin is assigned to the PCM
1152 * in dyn_pcm_assign mode.
1153 */
1154static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1155 struct hda_codec *codec,
1156 struct snd_pcm_substream *substream)
1157{
1158 struct hdmi_spec *spec = codec->spec;
1159 struct snd_pcm_runtime *runtime = substream->runtime;
1160 int cvt_idx, pcm_idx;
1161 struct hdmi_spec_per_cvt *per_cvt = NULL;
1162 int err;
1163
1164 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1165 if (pcm_idx < 0)
1166 return -EINVAL;
1167
1168 err = hdmi_choose_cvt(codec, -1, &cvt_idx);
1169 if (err)
1170 return err;
1171
1172 per_cvt = get_cvt(spec, cvt_idx);
1173 per_cvt->assigned = 1;
1174 hinfo->nid = per_cvt->cvt_nid;
1175
1176 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
1177
1178 set_bit(pcm_idx, &spec->pcm_in_use);
1179 /* todo: setup spdif ctls assign */
1180
1181 /* Initially set the converter's capabilities */
1182 hinfo->channels_min = per_cvt->channels_min;
1183 hinfo->channels_max = per_cvt->channels_max;
1184 hinfo->rates = per_cvt->rates;
1185 hinfo->formats = per_cvt->formats;
1186 hinfo->maxbps = per_cvt->maxbps;
1187
1188 /* Store the updated parameters */
1189 runtime->hw.channels_min = hinfo->channels_min;
1190 runtime->hw.channels_max = hinfo->channels_max;
1191 runtime->hw.formats = hinfo->formats;
1192 runtime->hw.rates = hinfo->rates;
1193
1194 snd_pcm_hw_constraint_step(substream->runtime, 0,
1195 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1196 return 0;
1197}
1198
1199/*
1200 * HDA PCM callbacks
1201 */
1202static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1203 struct hda_codec *codec,
1204 struct snd_pcm_substream *substream)
1205{
1206 struct hdmi_spec *spec = codec->spec;
1207 struct snd_pcm_runtime *runtime = substream->runtime;
1208 int pin_idx, cvt_idx, pcm_idx;
1209 struct hdmi_spec_per_pin *per_pin;
1210 struct hdmi_eld *eld;
1211 struct hdmi_spec_per_cvt *per_cvt = NULL;
1212 int err;
1213
1214 /* Validate hinfo */
1215 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1216 if (pcm_idx < 0)
1217 return -EINVAL;
1218
1219 mutex_lock(&spec->pcm_lock);
1220 pin_idx = hinfo_to_pin_index(codec, hinfo);
1221 if (!spec->dyn_pcm_assign) {
1222 if (snd_BUG_ON(pin_idx < 0)) {
1223 err = -EINVAL;
1224 goto unlock;
1225 }
1226 } else {
1227 /* no pin is assigned to the PCM
1228 * PA need pcm open successfully when probe
1229 */
1230 if (pin_idx < 0) {
1231 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1232 goto unlock;
1233 }
1234 }
1235
1236 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
1237 if (err < 0)
1238 goto unlock;
1239
1240 per_cvt = get_cvt(spec, cvt_idx);
1241 /* Claim converter */
1242 per_cvt->assigned = 1;
1243
1244 set_bit(pcm_idx, &spec->pcm_in_use);
1245 per_pin = get_pin(spec, pin_idx);
1246 per_pin->cvt_nid = per_cvt->cvt_nid;
1247 hinfo->nid = per_cvt->cvt_nid;
1248
1249 /* flip stripe flag for the assigned stream if supported */
1250 if (get_wcaps(codec, per_cvt->cvt_nid) & AC_WCAP_STRIPE)
1251 azx_stream(get_azx_dev(substream))->stripe = 1;
1252
1253 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1254 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1255 AC_VERB_SET_CONNECT_SEL,
1256 per_pin->mux_idx);
1257
1258 /* configure unused pins to choose other converters */
1259 pin_cvt_fixup(codec, per_pin, 0);
1260
1261 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1262
1263 /* Initially set the converter's capabilities */
1264 hinfo->channels_min = per_cvt->channels_min;
1265 hinfo->channels_max = per_cvt->channels_max;
1266 hinfo->rates = per_cvt->rates;
1267 hinfo->formats = per_cvt->formats;
1268 hinfo->maxbps = per_cvt->maxbps;
1269
1270 eld = &per_pin->sink_eld;
1271 /* Restrict capabilities by ELD if this isn't disabled */
1272 if (!static_hdmi_pcm && eld->eld_valid) {
1273 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1274 if (hinfo->channels_min > hinfo->channels_max ||
1275 !hinfo->rates || !hinfo->formats) {
1276 per_cvt->assigned = 0;
1277 hinfo->nid = 0;
1278 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1279 err = -ENODEV;
1280 goto unlock;
1281 }
1282 }
1283
1284 /* Store the updated parameters */
1285 runtime->hw.channels_min = hinfo->channels_min;
1286 runtime->hw.channels_max = hinfo->channels_max;
1287 runtime->hw.formats = hinfo->formats;
1288 runtime->hw.rates = hinfo->rates;
1289
1290 snd_pcm_hw_constraint_step(substream->runtime, 0,
1291 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1292 unlock:
1293 mutex_unlock(&spec->pcm_lock);
1294 return err;
1295}
1296
1297/*
1298 * HDA/HDMI auto parsing
1299 */
1300static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1301{
1302 struct hdmi_spec *spec = codec->spec;
1303 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1304 hda_nid_t pin_nid = per_pin->pin_nid;
1305
1306 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1307 codec_warn(codec,
1308 "HDMI: pin %d wcaps %#x does not support connection list\n",
1309 pin_nid, get_wcaps(codec, pin_nid));
1310 return -EINVAL;
1311 }
1312
1313 /* all the device entries on the same pin have the same conn list */
1314 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1315 per_pin->mux_nids,
1316 HDA_MAX_CONNECTIONS);
1317
1318 return 0;
1319}
1320
1321static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1322 struct hdmi_spec_per_pin *per_pin)
1323{
1324 int i;
1325
1326 /* try the prefer PCM */
1327 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1328 return per_pin->pin_nid_idx;
1329
1330 /* have a second try; check the "reserved area" over num_pins */
1331 for (i = spec->num_nids; i < spec->pcm_used; i++) {
1332 if (!test_bit(i, &spec->pcm_bitmap))
1333 return i;
1334 }
1335
1336 /* the last try; check the empty slots in pins */
1337 for (i = 0; i < spec->num_nids; i++) {
1338 if (!test_bit(i, &spec->pcm_bitmap))
1339 return i;
1340 }
1341 return -EBUSY;
1342}
1343
1344static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1345 struct hdmi_spec_per_pin *per_pin)
1346{
1347 int idx;
1348
1349 /* pcm already be attached to the pin */
1350 if (per_pin->pcm)
1351 return;
1352 idx = hdmi_find_pcm_slot(spec, per_pin);
1353 if (idx == -EBUSY)
1354 return;
1355 per_pin->pcm_idx = idx;
1356 per_pin->pcm = get_hdmi_pcm(spec, idx);
1357 set_bit(idx, &spec->pcm_bitmap);
1358}
1359
1360static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1361 struct hdmi_spec_per_pin *per_pin)
1362{
1363 int idx;
1364
1365 /* pcm already be detached from the pin */
1366 if (!per_pin->pcm)
1367 return;
1368 idx = per_pin->pcm_idx;
1369 per_pin->pcm_idx = -1;
1370 per_pin->pcm = NULL;
1371 if (idx >= 0 && idx < spec->pcm_used)
1372 clear_bit(idx, &spec->pcm_bitmap);
1373}
1374
1375static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1376 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1377{
1378 int mux_idx;
1379
1380 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1381 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1382 break;
1383 return mux_idx;
1384}
1385
1386static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1387
1388static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1389 struct hdmi_spec_per_pin *per_pin)
1390{
1391 struct hda_codec *codec = per_pin->codec;
1392 struct hda_pcm *pcm;
1393 struct hda_pcm_stream *hinfo;
1394 struct snd_pcm_substream *substream;
1395 int mux_idx;
1396 bool non_pcm;
1397
1398 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1399 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1400 else
1401 return;
1402 if (!pcm->pcm)
1403 return;
1404 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1405 return;
1406
1407 /* hdmi audio only uses playback and one substream */
1408 hinfo = pcm->stream;
1409 substream = pcm->pcm->streams[0].substream;
1410
1411 per_pin->cvt_nid = hinfo->nid;
1412
1413 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1414 if (mux_idx < per_pin->num_mux_nids) {
1415 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1416 per_pin->dev_id);
1417 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1418 AC_VERB_SET_CONNECT_SEL,
1419 mux_idx);
1420 }
1421 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1422
1423 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1424 if (substream->runtime)
1425 per_pin->channels = substream->runtime->channels;
1426 per_pin->setup = true;
1427 per_pin->mux_idx = mux_idx;
1428
1429 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1430}
1431
1432static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1433 struct hdmi_spec_per_pin *per_pin)
1434{
1435 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1436 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1437
1438 per_pin->chmap_set = false;
1439 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1440
1441 per_pin->setup = false;
1442 per_pin->channels = 0;
1443}
1444
1445/* update per_pin ELD from the given new ELD;
1446 * setup info frame and notification accordingly
1447 */
1448static bool update_eld(struct hda_codec *codec,
1449 struct hdmi_spec_per_pin *per_pin,
1450 struct hdmi_eld *eld)
1451{
1452 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1453 struct hdmi_spec *spec = codec->spec;
1454 bool old_eld_valid = pin_eld->eld_valid;
1455 bool eld_changed;
1456 int pcm_idx;
1457
1458 /* for monitor disconnection, save pcm_idx firstly */
1459 pcm_idx = per_pin->pcm_idx;
1460 if (spec->dyn_pcm_assign) {
1461 if (eld->eld_valid) {
1462 hdmi_attach_hda_pcm(spec, per_pin);
1463 hdmi_pcm_setup_pin(spec, per_pin);
1464 } else {
1465 hdmi_pcm_reset_pin(spec, per_pin);
1466 hdmi_detach_hda_pcm(spec, per_pin);
1467 }
1468 }
1469 /* if pcm_idx == -1, it means this is in monitor connection event
1470 * we can get the correct pcm_idx now.
1471 */
1472 if (pcm_idx == -1)
1473 pcm_idx = per_pin->pcm_idx;
1474
1475 if (eld->eld_valid)
1476 snd_hdmi_show_eld(codec, &eld->info);
1477
1478 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1479 eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
1480 if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
1481 if (pin_eld->eld_size != eld->eld_size ||
1482 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1483 eld->eld_size) != 0)
1484 eld_changed = true;
1485
1486 if (eld_changed) {
1487 pin_eld->monitor_present = eld->monitor_present;
1488 pin_eld->eld_valid = eld->eld_valid;
1489 pin_eld->eld_size = eld->eld_size;
1490 if (eld->eld_valid)
1491 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1492 eld->eld_size);
1493 pin_eld->info = eld->info;
1494 }
1495
1496 /*
1497 * Re-setup pin and infoframe. This is needed e.g. when
1498 * - sink is first plugged-in
1499 * - transcoder can change during stream playback on Haswell
1500 * and this can make HW reset converter selection on a pin.
1501 */
1502 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1503 pin_cvt_fixup(codec, per_pin, 0);
1504 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1505 }
1506
1507 if (eld_changed && pcm_idx >= 0)
1508 snd_ctl_notify(codec->card,
1509 SNDRV_CTL_EVENT_MASK_VALUE |
1510 SNDRV_CTL_EVENT_MASK_INFO,
1511 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1512 return eld_changed;
1513}
1514
1515/* update ELD and jack state via HD-audio verbs */
1516static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1517 int repoll)
1518{
1519 struct hda_jack_tbl *jack;
1520 struct hda_codec *codec = per_pin->codec;
1521 struct hdmi_spec *spec = codec->spec;
1522 struct hdmi_eld *eld = &spec->temp_eld;
1523 hda_nid_t pin_nid = per_pin->pin_nid;
1524 /*
1525 * Always execute a GetPinSense verb here, even when called from
1526 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1527 * response's PD bit is not the real PD value, but indicates that
1528 * the real PD value changed. An older version of the HD-audio
1529 * specification worked this way. Hence, we just ignore the data in
1530 * the unsolicited response to avoid custom WARs.
1531 */
1532 int present;
1533 bool ret;
1534 bool do_repoll = false;
1535
1536 present = snd_hda_pin_sense(codec, pin_nid);
1537
1538 mutex_lock(&per_pin->lock);
1539 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1540 if (eld->monitor_present)
1541 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1542 else
1543 eld->eld_valid = false;
1544
1545 codec_dbg(codec,
1546 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1547 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
1548
1549 if (eld->eld_valid) {
1550 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1551 &eld->eld_size) < 0)
1552 eld->eld_valid = false;
1553 else {
1554 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1555 eld->eld_size) < 0)
1556 eld->eld_valid = false;
1557 }
1558 if (!eld->eld_valid && repoll)
1559 do_repoll = true;
1560 }
1561
1562 if (do_repoll)
1563 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1564 else
1565 update_eld(codec, per_pin, eld);
1566
1567 ret = !repoll || !eld->monitor_present || eld->eld_valid;
1568
1569 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1570 if (jack) {
1571 jack->block_report = !ret;
1572 jack->pin_sense = (eld->monitor_present && eld->eld_valid) ?
1573 AC_PINSENSE_PRESENCE : 0;
1574 }
1575 mutex_unlock(&per_pin->lock);
1576 return ret;
1577}
1578
1579static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1580 struct hdmi_spec_per_pin *per_pin)
1581{
1582 struct hdmi_spec *spec = codec->spec;
1583 struct snd_jack *jack = NULL;
1584 struct hda_jack_tbl *jack_tbl;
1585
1586 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1587 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1588 * NULL even after snd_hda_jack_tbl_clear() is called to
1589 * free snd_jack. This may cause access invalid memory
1590 * when calling snd_jack_report
1591 */
1592 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1593 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1594 else if (!spec->dyn_pcm_assign) {
1595 /*
1596 * jack tbl doesn't support DP MST
1597 * DP MST will use dyn_pcm_assign,
1598 * so DP MST will never come here
1599 */
1600 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1601 if (jack_tbl)
1602 jack = jack_tbl->jack;
1603 }
1604 return jack;
1605}
1606
1607/* update ELD and jack state via audio component */
1608static void sync_eld_via_acomp(struct hda_codec *codec,
1609 struct hdmi_spec_per_pin *per_pin)
1610{
1611 struct hdmi_spec *spec = codec->spec;
1612 struct hdmi_eld *eld = &spec->temp_eld;
1613 struct snd_jack *jack = NULL;
1614 bool changed;
1615 int size;
1616
1617 mutex_lock(&per_pin->lock);
1618 eld->monitor_present = false;
1619 size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1620 per_pin->dev_id, &eld->monitor_present,
1621 eld->eld_buffer, ELD_MAX_SIZE);
1622 if (size > 0) {
1623 size = min(size, ELD_MAX_SIZE);
1624 if (snd_hdmi_parse_eld(codec, &eld->info,
1625 eld->eld_buffer, size) < 0)
1626 size = -EINVAL;
1627 }
1628
1629 if (size > 0) {
1630 eld->eld_valid = true;
1631 eld->eld_size = size;
1632 } else {
1633 eld->eld_valid = false;
1634 eld->eld_size = 0;
1635 }
1636
1637 /* pcm_idx >=0 before update_eld() means it is in monitor
1638 * disconnected event. Jack must be fetched before update_eld()
1639 */
1640 jack = pin_idx_to_jack(codec, per_pin);
1641 changed = update_eld(codec, per_pin, eld);
1642 if (jack == NULL)
1643 jack = pin_idx_to_jack(codec, per_pin);
1644 if (changed && jack)
1645 snd_jack_report(jack,
1646 (eld->monitor_present && eld->eld_valid) ?
1647 SND_JACK_AVOUT : 0);
1648 mutex_unlock(&per_pin->lock);
1649}
1650
1651static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1652{
1653 struct hda_codec *codec = per_pin->codec;
1654 int ret;
1655
1656 /* no temporary power up/down needed for component notifier */
1657 if (!codec_has_acomp(codec)) {
1658 ret = snd_hda_power_up_pm(codec);
1659 if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec))) {
1660 snd_hda_power_down_pm(codec);
1661 return false;
1662 }
1663 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
1664 snd_hda_power_down_pm(codec);
1665 } else {
1666 sync_eld_via_acomp(codec, per_pin);
1667 ret = false; /* don't call snd_hda_jack_report_sync() */
1668 }
1669
1670 return ret;
1671}
1672
1673static void hdmi_repoll_eld(struct work_struct *work)
1674{
1675 struct hdmi_spec_per_pin *per_pin =
1676 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1677 struct hda_codec *codec = per_pin->codec;
1678 struct hdmi_spec *spec = codec->spec;
1679 struct hda_jack_tbl *jack;
1680
1681 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1682 if (jack)
1683 jack->jack_dirty = 1;
1684
1685 if (per_pin->repoll_count++ > 6)
1686 per_pin->repoll_count = 0;
1687
1688 mutex_lock(&spec->pcm_lock);
1689 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1690 snd_hda_jack_report_sync(per_pin->codec);
1691 mutex_unlock(&spec->pcm_lock);
1692}
1693
1694static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1695 hda_nid_t nid);
1696
1697static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1698{
1699 struct hdmi_spec *spec = codec->spec;
1700 unsigned int caps, config;
1701 int pin_idx;
1702 struct hdmi_spec_per_pin *per_pin;
1703 int err;
1704 int dev_num, i;
1705
1706 caps = snd_hda_query_pin_caps(codec, pin_nid);
1707 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1708 return 0;
1709
1710 /*
1711 * For DP MST audio, Configuration Default is the same for
1712 * all device entries on the same pin
1713 */
1714 config = snd_hda_codec_get_pincfg(codec, pin_nid);
1715 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE &&
1716 !spec->force_connect)
1717 return 0;
1718
1719 /*
1720 * To simplify the implementation, malloc all
1721 * the virtual pins in the initialization statically
1722 */
1723 if (is_haswell_plus(codec)) {
1724 /*
1725 * On Intel platforms, device entries number is
1726 * changed dynamically. If there is a DP MST
1727 * hub connected, the device entries number is 3.
1728 * Otherwise, it is 1.
1729 * Here we manually set dev_num to 3, so that
1730 * we can initialize all the device entries when
1731 * bootup statically.
1732 */
1733 dev_num = 3;
1734 spec->dev_num = 3;
1735 } else if (spec->dyn_pcm_assign && codec->dp_mst) {
1736 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1737 /*
1738 * spec->dev_num is the maxinum number of device entries
1739 * among all the pins
1740 */
1741 spec->dev_num = (spec->dev_num > dev_num) ?
1742 spec->dev_num : dev_num;
1743 } else {
1744 /*
1745 * If the platform doesn't support DP MST,
1746 * manually set dev_num to 1. This means
1747 * the pin has only one device entry.
1748 */
1749 dev_num = 1;
1750 spec->dev_num = 1;
1751 }
1752
1753 for (i = 0; i < dev_num; i++) {
1754 pin_idx = spec->num_pins;
1755 per_pin = snd_array_new(&spec->pins);
1756
1757 if (!per_pin)
1758 return -ENOMEM;
1759
1760 if (spec->dyn_pcm_assign) {
1761 per_pin->pcm = NULL;
1762 per_pin->pcm_idx = -1;
1763 } else {
1764 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1765 per_pin->pcm_idx = pin_idx;
1766 }
1767 per_pin->pin_nid = pin_nid;
1768 per_pin->pin_nid_idx = spec->num_nids;
1769 per_pin->dev_id = i;
1770 per_pin->non_pcm = false;
1771 snd_hda_set_dev_select(codec, pin_nid, i);
1772 if (is_haswell_plus(codec))
1773 intel_haswell_fixup_connect_list(codec, pin_nid);
1774 err = hdmi_read_pin_conn(codec, pin_idx);
1775 if (err < 0)
1776 return err;
1777 spec->num_pins++;
1778 }
1779 spec->num_nids++;
1780
1781 return 0;
1782}
1783
1784static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1785{
1786 struct hdmi_spec *spec = codec->spec;
1787 struct hdmi_spec_per_cvt *per_cvt;
1788 unsigned int chans;
1789 int err;
1790
1791 chans = get_wcaps(codec, cvt_nid);
1792 chans = get_wcaps_channels(chans);
1793
1794 per_cvt = snd_array_new(&spec->cvts);
1795 if (!per_cvt)
1796 return -ENOMEM;
1797
1798 per_cvt->cvt_nid = cvt_nid;
1799 per_cvt->channels_min = 2;
1800 if (chans <= 16) {
1801 per_cvt->channels_max = chans;
1802 if (chans > spec->chmap.channels_max)
1803 spec->chmap.channels_max = chans;
1804 }
1805
1806 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1807 &per_cvt->rates,
1808 &per_cvt->formats,
1809 &per_cvt->maxbps);
1810 if (err < 0)
1811 return err;
1812
1813 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1814 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1815 spec->num_cvts++;
1816
1817 return 0;
1818}
1819
1820static const struct snd_pci_quirk force_connect_list[] = {
1821 SND_PCI_QUIRK(0x103c, 0x83e2, "HP EliteDesk 800 G4", 1),
1822 SND_PCI_QUIRK(0x103c, 0x83ef, "HP MP9 G4 Retail System AMS", 1),
1823 SND_PCI_QUIRK(0x103c, 0x870f, "HP", 1),
1824 SND_PCI_QUIRK(0x103c, 0x871a, "HP", 1),
1825 SND_PCI_QUIRK(0x103c, 0x8711, "HP", 1),
1826 SND_PCI_QUIRK(0x103c, 0x8715, "HP", 1),
1827 SND_PCI_QUIRK(0x1043, 0x86ae, "ASUS", 1), /* Z170 PRO */
1828 SND_PCI_QUIRK(0x1043, 0x86c7, "ASUS", 1), /* Z170M PLUS */
1829 SND_PCI_QUIRK(0x1462, 0xec94, "MS-7C94", 1),
1830 SND_PCI_QUIRK(0x8086, 0x2060, "Intel NUC5CPYB", 1),
1831 SND_PCI_QUIRK(0x8086, 0x2081, "Intel NUC 10", 1),
1832 {}
1833};
1834
1835static int hdmi_parse_codec(struct hda_codec *codec)
1836{
1837 struct hdmi_spec *spec = codec->spec;
1838 hda_nid_t start_nid;
1839 unsigned int caps;
1840 int i, nodes;
1841 const struct snd_pci_quirk *q;
1842
1843 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &start_nid);
1844 if (!start_nid || nodes < 0) {
1845 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1846 return -EINVAL;
1847 }
1848
1849 q = snd_pci_quirk_lookup(codec->bus->pci, force_connect_list);
1850
1851 if (q && q->value)
1852 spec->force_connect = true;
1853
1854 /*
1855 * hdmi_add_pin() assumes total amount of converters to
1856 * be known, so first discover all converters
1857 */
1858 for (i = 0; i < nodes; i++) {
1859 hda_nid_t nid = start_nid + i;
1860
1861 caps = get_wcaps(codec, nid);
1862
1863 if (!(caps & AC_WCAP_DIGITAL))
1864 continue;
1865
1866 if (get_wcaps_type(caps) == AC_WID_AUD_OUT)
1867 hdmi_add_cvt(codec, nid);
1868 }
1869
1870 /* discover audio pins */
1871 for (i = 0; i < nodes; i++) {
1872 hda_nid_t nid = start_nid + i;
1873
1874 caps = get_wcaps(codec, nid);
1875
1876 if (!(caps & AC_WCAP_DIGITAL))
1877 continue;
1878
1879 if (get_wcaps_type(caps) == AC_WID_PIN)
1880 hdmi_add_pin(codec, nid);
1881 }
1882
1883 return 0;
1884}
1885
1886/*
1887 */
1888static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1889{
1890 struct hda_spdif_out *spdif;
1891 bool non_pcm;
1892
1893 mutex_lock(&codec->spdif_mutex);
1894 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1895 /* Add sanity check to pass klockwork check.
1896 * This should never happen.
1897 */
1898 if (WARN_ON(spdif == NULL)) {
1899 mutex_unlock(&codec->spdif_mutex);
1900 return true;
1901 }
1902 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1903 mutex_unlock(&codec->spdif_mutex);
1904 return non_pcm;
1905}
1906
1907/*
1908 * HDMI callbacks
1909 */
1910
1911static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1912 struct hda_codec *codec,
1913 unsigned int stream_tag,
1914 unsigned int format,
1915 struct snd_pcm_substream *substream)
1916{
1917 hda_nid_t cvt_nid = hinfo->nid;
1918 struct hdmi_spec *spec = codec->spec;
1919 int pin_idx;
1920 struct hdmi_spec_per_pin *per_pin;
1921 hda_nid_t pin_nid;
1922 struct snd_pcm_runtime *runtime = substream->runtime;
1923 bool non_pcm;
1924 int pinctl, stripe;
1925 int err = 0;
1926
1927 mutex_lock(&spec->pcm_lock);
1928 pin_idx = hinfo_to_pin_index(codec, hinfo);
1929 if (spec->dyn_pcm_assign && pin_idx < 0) {
1930 /* when dyn_pcm_assign and pcm is not bound to a pin
1931 * skip pin setup and return 0 to make audio playback
1932 * be ongoing
1933 */
1934 pin_cvt_fixup(codec, NULL, cvt_nid);
1935 snd_hda_codec_setup_stream(codec, cvt_nid,
1936 stream_tag, 0, format);
1937 goto unlock;
1938 }
1939
1940 if (snd_BUG_ON(pin_idx < 0)) {
1941 err = -EINVAL;
1942 goto unlock;
1943 }
1944 per_pin = get_pin(spec, pin_idx);
1945 pin_nid = per_pin->pin_nid;
1946
1947 /* Verify pin:cvt selections to avoid silent audio after S3.
1948 * After S3, the audio driver restores pin:cvt selections
1949 * but this can happen before gfx is ready and such selection
1950 * is overlooked by HW. Thus multiple pins can share a same
1951 * default convertor and mute control will affect each other,
1952 * which can cause a resumed audio playback become silent
1953 * after S3.
1954 */
1955 pin_cvt_fixup(codec, per_pin, 0);
1956
1957 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1958 /* Todo: add DP1.2 MST audio support later */
1959 if (codec_has_acomp(codec))
1960 snd_hdac_sync_audio_rate(&codec->core, pin_nid, per_pin->dev_id,
1961 runtime->rate);
1962
1963 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1964 mutex_lock(&per_pin->lock);
1965 per_pin->channels = substream->runtime->channels;
1966 per_pin->setup = true;
1967
1968 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1969 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1970 substream);
1971 snd_hda_codec_write(codec, cvt_nid, 0,
1972 AC_VERB_SET_STRIPE_CONTROL,
1973 stripe);
1974 }
1975
1976 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1977 mutex_unlock(&per_pin->lock);
1978 if (spec->dyn_pin_out) {
1979 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1980 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1981 snd_hda_codec_write(codec, pin_nid, 0,
1982 AC_VERB_SET_PIN_WIDGET_CONTROL,
1983 pinctl | PIN_OUT);
1984 }
1985
1986 /* snd_hda_set_dev_select() has been called before */
1987 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1988 stream_tag, format);
1989 unlock:
1990 mutex_unlock(&spec->pcm_lock);
1991 return err;
1992}
1993
1994static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1995 struct hda_codec *codec,
1996 struct snd_pcm_substream *substream)
1997{
1998 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1999 return 0;
2000}
2001
2002static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2003 struct hda_codec *codec,
2004 struct snd_pcm_substream *substream)
2005{
2006 struct hdmi_spec *spec = codec->spec;
2007 int cvt_idx, pin_idx, pcm_idx;
2008 struct hdmi_spec_per_cvt *per_cvt;
2009 struct hdmi_spec_per_pin *per_pin;
2010 int pinctl;
2011 int err = 0;
2012
2013 mutex_lock(&spec->pcm_lock);
2014 if (hinfo->nid) {
2015 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2016 if (snd_BUG_ON(pcm_idx < 0)) {
2017 err = -EINVAL;
2018 goto unlock;
2019 }
2020 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
2021 if (snd_BUG_ON(cvt_idx < 0)) {
2022 err = -EINVAL;
2023 goto unlock;
2024 }
2025 per_cvt = get_cvt(spec, cvt_idx);
2026 snd_BUG_ON(!per_cvt->assigned);
2027 per_cvt->assigned = 0;
2028 hinfo->nid = 0;
2029
2030 azx_stream(get_azx_dev(substream))->stripe = 0;
2031
2032 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2033 clear_bit(pcm_idx, &spec->pcm_in_use);
2034 pin_idx = hinfo_to_pin_index(codec, hinfo);
2035 if (spec->dyn_pcm_assign && pin_idx < 0)
2036 goto unlock;
2037
2038 if (snd_BUG_ON(pin_idx < 0)) {
2039 err = -EINVAL;
2040 goto unlock;
2041 }
2042 per_pin = get_pin(spec, pin_idx);
2043
2044 if (spec->dyn_pin_out) {
2045 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2046 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2047 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2048 AC_VERB_SET_PIN_WIDGET_CONTROL,
2049 pinctl & ~PIN_OUT);
2050 }
2051
2052 mutex_lock(&per_pin->lock);
2053 per_pin->chmap_set = false;
2054 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2055
2056 per_pin->setup = false;
2057 per_pin->channels = 0;
2058 mutex_unlock(&per_pin->lock);
2059 }
2060
2061unlock:
2062 mutex_unlock(&spec->pcm_lock);
2063
2064 return err;
2065}
2066
2067static const struct hda_pcm_ops generic_ops = {
2068 .open = hdmi_pcm_open,
2069 .close = hdmi_pcm_close,
2070 .prepare = generic_hdmi_playback_pcm_prepare,
2071 .cleanup = generic_hdmi_playback_pcm_cleanup,
2072};
2073
2074static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2075{
2076 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2077 struct hdmi_spec *spec = codec->spec;
2078 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2079
2080 if (!per_pin)
2081 return 0;
2082
2083 return per_pin->sink_eld.info.spk_alloc;
2084}
2085
2086static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2087 unsigned char *chmap)
2088{
2089 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2090 struct hdmi_spec *spec = codec->spec;
2091 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2092
2093 /* chmap is already set to 0 in caller */
2094 if (!per_pin)
2095 return;
2096
2097 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2098}
2099
2100static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2101 unsigned char *chmap, int prepared)
2102{
2103 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2104 struct hdmi_spec *spec = codec->spec;
2105 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2106
2107 if (!per_pin)
2108 return;
2109 mutex_lock(&per_pin->lock);
2110 per_pin->chmap_set = true;
2111 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2112 if (prepared)
2113 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2114 mutex_unlock(&per_pin->lock);
2115}
2116
2117static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2118{
2119 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2120 struct hdmi_spec *spec = codec->spec;
2121 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2122
2123 return per_pin ? true:false;
2124}
2125
2126static int generic_hdmi_build_pcms(struct hda_codec *codec)
2127{
2128 struct hdmi_spec *spec = codec->spec;
2129 int idx;
2130
2131 /*
2132 * for non-mst mode, pcm number is the same as before
2133 * for DP MST mode, pcm number is (nid number + dev_num - 1)
2134 * dev_num is the device entry number in a pin
2135 *
2136 */
2137 for (idx = 0; idx < spec->num_nids + spec->dev_num - 1; idx++) {
2138 struct hda_pcm *info;
2139 struct hda_pcm_stream *pstr;
2140
2141 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
2142 if (!info)
2143 return -ENOMEM;
2144
2145 spec->pcm_rec[idx].pcm = info;
2146 spec->pcm_used++;
2147 info->pcm_type = HDA_PCM_TYPE_HDMI;
2148 info->own_chmap = true;
2149
2150 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2151 pstr->substreams = 1;
2152 pstr->ops = generic_ops;
2153 /* pcm number is less than 16 */
2154 if (spec->pcm_used >= 16)
2155 break;
2156 /* other pstr fields are set in open */
2157 }
2158
2159 return 0;
2160}
2161
2162static void free_hdmi_jack_priv(struct snd_jack *jack)
2163{
2164 struct hdmi_pcm *pcm = jack->private_data;
2165
2166 pcm->jack = NULL;
2167}
2168
2169static int add_hdmi_jack_kctl(struct hda_codec *codec,
2170 struct hdmi_spec *spec,
2171 int pcm_idx,
2172 const char *name)
2173{
2174 struct snd_jack *jack;
2175 int err;
2176
2177 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2178 true, false);
2179 if (err < 0)
2180 return err;
2181
2182 spec->pcm_rec[pcm_idx].jack = jack;
2183 jack->private_data = &spec->pcm_rec[pcm_idx];
2184 jack->private_free = free_hdmi_jack_priv;
2185 return 0;
2186}
2187
2188static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2189{
2190 char hdmi_str[32] = "HDMI/DP";
2191 struct hdmi_spec *spec = codec->spec;
2192 struct hdmi_spec_per_pin *per_pin;
2193 struct hda_jack_tbl *jack;
2194 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2195 bool phantom_jack;
2196 int ret;
2197
2198 if (pcmdev > 0)
2199 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2200
2201 if (spec->dyn_pcm_assign)
2202 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2203
2204 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2205 /* if !dyn_pcm_assign, it must be non-MST mode.
2206 * This means pcms and pins are statically mapped.
2207 * And pcm_idx is pin_idx.
2208 */
2209 per_pin = get_pin(spec, pcm_idx);
2210 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2211 if (phantom_jack)
2212 strncat(hdmi_str, " Phantom",
2213 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2214 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2215 phantom_jack, 0, NULL);
2216 if (ret < 0)
2217 return ret;
2218 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2219 if (jack == NULL)
2220 return 0;
2221 /* assign jack->jack to pcm_rec[].jack to
2222 * align with dyn_pcm_assign mode
2223 */
2224 spec->pcm_rec[pcm_idx].jack = jack->jack;
2225 return 0;
2226}
2227
2228static int generic_hdmi_build_controls(struct hda_codec *codec)
2229{
2230 struct hdmi_spec *spec = codec->spec;
2231 int dev, err;
2232 int pin_idx, pcm_idx;
2233
2234 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2235 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2236 /* no PCM: mark this for skipping permanently */
2237 set_bit(pcm_idx, &spec->pcm_bitmap);
2238 continue;
2239 }
2240
2241 err = generic_hdmi_build_jack(codec, pcm_idx);
2242 if (err < 0)
2243 return err;
2244
2245 /* create the spdif for each pcm
2246 * pin will be bound when monitor is connected
2247 */
2248 if (spec->dyn_pcm_assign)
2249 err = snd_hda_create_dig_out_ctls(codec,
2250 0, spec->cvt_nids[0],
2251 HDA_PCM_TYPE_HDMI);
2252 else {
2253 struct hdmi_spec_per_pin *per_pin =
2254 get_pin(spec, pcm_idx);
2255 err = snd_hda_create_dig_out_ctls(codec,
2256 per_pin->pin_nid,
2257 per_pin->mux_nids[0],
2258 HDA_PCM_TYPE_HDMI);
2259 }
2260 if (err < 0)
2261 return err;
2262 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2263
2264 dev = get_pcm_rec(spec, pcm_idx)->device;
2265 if (dev != SNDRV_PCM_INVALID_DEVICE) {
2266 /* add control for ELD Bytes */
2267 err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2268 if (err < 0)
2269 return err;
2270 }
2271 }
2272
2273 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2274 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2275 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
2276
2277 pin_eld->eld_valid = false;
2278 hdmi_present_sense(per_pin, 0);
2279 }
2280
2281 /* add channel maps */
2282 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2283 struct hda_pcm *pcm;
2284
2285 pcm = get_pcm_rec(spec, pcm_idx);
2286 if (!pcm || !pcm->pcm)
2287 break;
2288 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
2289 if (err < 0)
2290 return err;
2291 }
2292
2293 return 0;
2294}
2295
2296static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2297{
2298 struct hdmi_spec *spec = codec->spec;
2299 int pin_idx;
2300
2301 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2302 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2303
2304 per_pin->codec = codec;
2305 mutex_init(&per_pin->lock);
2306 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2307 eld_proc_new(per_pin, pin_idx);
2308 }
2309 return 0;
2310}
2311
2312static int generic_hdmi_init(struct hda_codec *codec)
2313{
2314 struct hdmi_spec *spec = codec->spec;
2315 int pin_idx;
2316
2317 mutex_lock(&spec->bind_lock);
2318 spec->use_jack_detect = !codec->jackpoll_interval;
2319 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2320 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2321 hda_nid_t pin_nid = per_pin->pin_nid;
2322 int dev_id = per_pin->dev_id;
2323
2324 snd_hda_set_dev_select(codec, pin_nid, dev_id);
2325 hdmi_init_pin(codec, pin_nid);
2326 if (codec_has_acomp(codec))
2327 continue;
2328 if (spec->use_jack_detect)
2329 snd_hda_jack_detect_enable(codec, pin_nid);
2330 else
2331 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2332 jack_callback);
2333 }
2334 mutex_unlock(&spec->bind_lock);
2335 return 0;
2336}
2337
2338static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2339{
2340 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2341 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2342}
2343
2344static void hdmi_array_free(struct hdmi_spec *spec)
2345{
2346 snd_array_free(&spec->pins);
2347 snd_array_free(&spec->cvts);
2348}
2349
2350static void generic_spec_free(struct hda_codec *codec)
2351{
2352 struct hdmi_spec *spec = codec->spec;
2353
2354 if (spec) {
2355 hdmi_array_free(spec);
2356 kfree(spec);
2357 codec->spec = NULL;
2358 }
2359 codec->dp_mst = false;
2360}
2361
2362static void generic_hdmi_free(struct hda_codec *codec)
2363{
2364 struct hdmi_spec *spec = codec->spec;
2365 int pin_idx, pcm_idx;
2366
2367 if (spec->acomp_registered) {
2368 snd_hdac_acomp_exit(&codec->bus->core);
2369 } else if (codec_has_acomp(codec)) {
2370 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2371 codec->relaxed_resume = 0;
2372 }
2373
2374 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2375 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2376 cancel_delayed_work_sync(&per_pin->work);
2377 eld_proc_free(per_pin);
2378 }
2379
2380 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2381 if (spec->pcm_rec[pcm_idx].jack == NULL)
2382 continue;
2383 if (spec->dyn_pcm_assign)
2384 snd_device_free(codec->card,
2385 spec->pcm_rec[pcm_idx].jack);
2386 else
2387 spec->pcm_rec[pcm_idx].jack = NULL;
2388 }
2389
2390 generic_spec_free(codec);
2391}
2392
2393#ifdef CONFIG_PM
2394static int generic_hdmi_suspend(struct hda_codec *codec)
2395{
2396 struct hdmi_spec *spec = codec->spec;
2397 int pin_idx;
2398
2399 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2400 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2401 cancel_delayed_work_sync(&per_pin->work);
2402 }
2403 return 0;
2404}
2405
2406static int generic_hdmi_resume(struct hda_codec *codec)
2407{
2408 struct hdmi_spec *spec = codec->spec;
2409 int pin_idx;
2410
2411 codec->patch_ops.init(codec);
2412 snd_hda_regmap_sync(codec);
2413
2414 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2415 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2416 hdmi_present_sense(per_pin, 1);
2417 }
2418 return 0;
2419}
2420#endif
2421
2422static const struct hda_codec_ops generic_hdmi_patch_ops = {
2423 .init = generic_hdmi_init,
2424 .free = generic_hdmi_free,
2425 .build_pcms = generic_hdmi_build_pcms,
2426 .build_controls = generic_hdmi_build_controls,
2427 .unsol_event = hdmi_unsol_event,
2428#ifdef CONFIG_PM
2429 .suspend = generic_hdmi_suspend,
2430 .resume = generic_hdmi_resume,
2431#endif
2432};
2433
2434static const struct hdmi_ops generic_standard_hdmi_ops = {
2435 .pin_get_eld = snd_hdmi_get_eld,
2436 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2437 .pin_hbr_setup = hdmi_pin_hbr_setup,
2438 .setup_stream = hdmi_setup_stream,
2439};
2440
2441/* allocate codec->spec and assign/initialize generic parser ops */
2442static int alloc_generic_hdmi(struct hda_codec *codec)
2443{
2444 struct hdmi_spec *spec;
2445
2446 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2447 if (!spec)
2448 return -ENOMEM;
2449
2450 spec->codec = codec;
2451 spec->ops = generic_standard_hdmi_ops;
2452 spec->dev_num = 1; /* initialize to 1 */
2453 mutex_init(&spec->pcm_lock);
2454 mutex_init(&spec->bind_lock);
2455 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2456
2457 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2458 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2459 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2460 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
2461
2462 codec->spec = spec;
2463 hdmi_array_init(spec, 4);
2464
2465 codec->patch_ops = generic_hdmi_patch_ops;
2466
2467 return 0;
2468}
2469
2470/* generic HDMI parser */
2471static int patch_generic_hdmi(struct hda_codec *codec)
2472{
2473 int err;
2474
2475 err = alloc_generic_hdmi(codec);
2476 if (err < 0)
2477 return err;
2478
2479 err = hdmi_parse_codec(codec);
2480 if (err < 0) {
2481 generic_spec_free(codec);
2482 return err;
2483 }
2484
2485 generic_hdmi_init_per_pins(codec);
2486 return 0;
2487}
2488
2489/*
2490 * generic audio component binding
2491 */
2492
2493/* turn on / off the unsol event jack detection dynamically */
2494static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
2495 bool use_acomp)
2496{
2497 struct hda_jack_tbl *tbl;
2498
2499 tbl = snd_hda_jack_tbl_get(codec, nid);
2500 if (tbl) {
2501 /* clear unsol even if component notifier is used, or re-enable
2502 * if notifier is cleared
2503 */
2504 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2505 snd_hda_codec_write_cache(codec, nid, 0,
2506 AC_VERB_SET_UNSOLICITED_ENABLE, val);
2507 } else {
2508 /* if no jack entry was defined beforehand, create a new one
2509 * at need (i.e. only when notifier is cleared)
2510 */
2511 if (!use_acomp)
2512 snd_hda_jack_detect_enable(codec, nid);
2513 }
2514}
2515
2516/* set up / clear component notifier dynamically */
2517static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2518 bool use_acomp)
2519{
2520 struct hdmi_spec *spec;
2521 int i;
2522
2523 spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
2524 mutex_lock(&spec->bind_lock);
2525 spec->use_acomp_notifier = use_acomp;
2526 spec->codec->relaxed_resume = use_acomp;
2527 spec->codec->bus->keep_power = 0;
2528 /* reprogram each jack detection logic depending on the notifier */
2529 if (spec->use_jack_detect) {
2530 for (i = 0; i < spec->num_pins; i++)
2531 reprogram_jack_detect(spec->codec,
2532 get_pin(spec, i)->pin_nid,
2533 use_acomp);
2534 }
2535 mutex_unlock(&spec->bind_lock);
2536}
2537
2538/* enable / disable the notifier via master bind / unbind */
2539static int generic_acomp_master_bind(struct device *dev,
2540 struct drm_audio_component *acomp)
2541{
2542 generic_acomp_notifier_set(acomp, true);
2543 return 0;
2544}
2545
2546static void generic_acomp_master_unbind(struct device *dev,
2547 struct drm_audio_component *acomp)
2548{
2549 generic_acomp_notifier_set(acomp, false);
2550}
2551
2552/* check whether both HD-audio and DRM PCI devices belong to the same bus */
2553static int match_bound_vga(struct device *dev, int subtype, void *data)
2554{
2555 struct hdac_bus *bus = data;
2556 struct pci_dev *pci, *master;
2557
2558 if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2559 return 0;
2560 master = to_pci_dev(bus->dev);
2561 pci = to_pci_dev(dev);
2562 return master->bus == pci->bus;
2563}
2564
2565/* audio component notifier for AMD/Nvidia HDMI codecs */
2566static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2567{
2568 struct hda_codec *codec = audio_ptr;
2569 struct hdmi_spec *spec = codec->spec;
2570 hda_nid_t pin_nid = spec->port2pin(codec, port);
2571
2572 if (!pin_nid)
2573 return;
2574 if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2575 return;
2576 /* skip notification during system suspend (but not in runtime PM);
2577 * the state will be updated at resume
2578 */
2579 if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
2580 return;
2581
2582 check_presence_and_report(codec, pin_nid, dev_id);
2583}
2584
2585/* set up the private drm_audio_ops from the template */
2586static void setup_drm_audio_ops(struct hda_codec *codec,
2587 const struct drm_audio_component_audio_ops *ops)
2588{
2589 struct hdmi_spec *spec = codec->spec;
2590
2591 spec->drm_audio_ops.audio_ptr = codec;
2592 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2593 * will call pin_eld_notify with using audio_ptr pointer
2594 * We need make sure audio_ptr is really setup
2595 */
2596 wmb();
2597 spec->drm_audio_ops.pin2port = ops->pin2port;
2598 spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2599 spec->drm_audio_ops.master_bind = ops->master_bind;
2600 spec->drm_audio_ops.master_unbind = ops->master_unbind;
2601}
2602
2603/* initialize the generic HDMI audio component */
2604static void generic_acomp_init(struct hda_codec *codec,
2605 const struct drm_audio_component_audio_ops *ops,
2606 int (*port2pin)(struct hda_codec *, int))
2607{
2608 struct hdmi_spec *spec = codec->spec;
2609
2610 if (!enable_acomp) {
2611 codec_info(codec, "audio component disabled by module option\n");
2612 return;
2613 }
2614
2615 spec->port2pin = port2pin;
2616 setup_drm_audio_ops(codec, ops);
2617 if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
2618 match_bound_vga, 0)) {
2619 spec->acomp_registered = true;
2620 }
2621}
2622
2623/*
2624 * Intel codec parsers and helpers
2625 */
2626
2627static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2628 hda_nid_t nid)
2629{
2630 struct hdmi_spec *spec = codec->spec;
2631 hda_nid_t conns[4];
2632 int nconns;
2633
2634 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2635 if (nconns == spec->num_cvts &&
2636 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2637 return;
2638
2639 /* override pins connection list */
2640 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2641 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2642}
2643
2644#define INTEL_GET_VENDOR_VERB 0xf81
2645#define INTEL_SET_VENDOR_VERB 0x781
2646#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2647#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2648
2649static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2650 bool update_tree)
2651{
2652 unsigned int vendor_param;
2653 struct hdmi_spec *spec = codec->spec;
2654
2655 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2656 INTEL_GET_VENDOR_VERB, 0);
2657 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2658 return;
2659
2660 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2661 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2662 INTEL_SET_VENDOR_VERB, vendor_param);
2663 if (vendor_param == -1)
2664 return;
2665
2666 if (update_tree)
2667 snd_hda_codec_update_widgets(codec);
2668}
2669
2670static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2671{
2672 unsigned int vendor_param;
2673 struct hdmi_spec *spec = codec->spec;
2674
2675 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2676 INTEL_GET_VENDOR_VERB, 0);
2677 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2678 return;
2679
2680 /* enable DP1.2 mode */
2681 vendor_param |= INTEL_EN_DP12;
2682 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2683 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
2684 INTEL_SET_VENDOR_VERB, vendor_param);
2685}
2686
2687/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2688 * Otherwise you may get severe h/w communication errors.
2689 */
2690static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2691 unsigned int power_state)
2692{
2693 if (power_state == AC_PWRST_D0) {
2694 intel_haswell_enable_all_pins(codec, false);
2695 intel_haswell_fixup_enable_dp12(codec);
2696 }
2697
2698 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2699 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2700}
2701
2702/* There is a fixed mapping between audio pin node and display port.
2703 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2704 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2705 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2706 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2707 *
2708 * on VLV, ILK:
2709 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2710 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2711 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2712 */
2713static int intel_base_nid(struct hda_codec *codec)
2714{
2715 switch (codec->core.vendor_id) {
2716 case 0x80860054: /* ILK */
2717 case 0x80862804: /* ILK */
2718 case 0x80862882: /* VLV */
2719 return 4;
2720 default:
2721 return 5;
2722 }
2723}
2724
2725static int intel_pin2port(void *audio_ptr, int pin_nid)
2726{
2727 struct hda_codec *codec = audio_ptr;
2728 struct hdmi_spec *spec = codec->spec;
2729 int base_nid, i;
2730
2731 if (!spec->port_num) {
2732 base_nid = intel_base_nid(codec);
2733 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2734 return -1;
2735 return pin_nid - base_nid + 1; /* intel port is 1-based */
2736 }
2737
2738 /*
2739 * looking for the pin number in the mapping table and return
2740 * the index which indicate the port number
2741 */
2742 for (i = 0; i < spec->port_num; i++) {
2743 if (pin_nid == spec->port_map[i])
2744 return i + 1;
2745 }
2746
2747 /* return -1 if pin number exceeds our expectation */
2748 codec_info(codec, "Can't find the HDMI/DP port for pin %d\n", pin_nid);
2749 return -1;
2750}
2751
2752static int intel_port2pin(struct hda_codec *codec, int port)
2753{
2754 struct hdmi_spec *spec = codec->spec;
2755
2756 if (!spec->port_num) {
2757 /* we assume only from port-B to port-D */
2758 if (port < 1 || port > 3)
2759 return 0;
2760 /* intel port is 1-based */
2761 return port + intel_base_nid(codec) - 1;
2762 }
2763
2764 if (port < 1 || port > spec->port_num)
2765 return 0;
2766 return spec->port_map[port - 1];
2767}
2768
2769static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
2770{
2771 struct hda_codec *codec = audio_ptr;
2772 int pin_nid;
2773 int dev_id = pipe;
2774
2775 pin_nid = intel_port2pin(codec, port);
2776 if (!pin_nid)
2777 return;
2778 /* skip notification during system suspend (but not in runtime PM);
2779 * the state will be updated at resume
2780 */
2781 if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
2782 return;
2783
2784 snd_hdac_i915_set_bclk(&codec->bus->core);
2785 check_presence_and_report(codec, pin_nid, dev_id);
2786}
2787
2788static const struct drm_audio_component_audio_ops intel_audio_ops = {
2789 .pin2port = intel_pin2port,
2790 .pin_eld_notify = intel_pin_eld_notify,
2791};
2792
2793/* register i915 component pin_eld_notify callback */
2794static void register_i915_notifier(struct hda_codec *codec)
2795{
2796 struct hdmi_spec *spec = codec->spec;
2797
2798 spec->use_acomp_notifier = true;
2799 spec->port2pin = intel_port2pin;
2800 setup_drm_audio_ops(codec, &intel_audio_ops);
2801 snd_hdac_acomp_register_notifier(&codec->bus->core,
2802 &spec->drm_audio_ops);
2803 /* no need for forcible resume for jack check thanks to notifier */
2804 codec->relaxed_resume = 1;
2805}
2806
2807/* setup_stream ops override for HSW+ */
2808static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
2809 hda_nid_t pin_nid, u32 stream_tag, int format)
2810{
2811 haswell_verify_D0(codec, cvt_nid, pin_nid);
2812 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
2813}
2814
2815/* pin_cvt_fixup ops override for HSW+ and VLV+ */
2816static void i915_pin_cvt_fixup(struct hda_codec *codec,
2817 struct hdmi_spec_per_pin *per_pin,
2818 hda_nid_t cvt_nid)
2819{
2820 if (per_pin) {
2821 haswell_verify_D0(codec, per_pin->cvt_nid, per_pin->pin_nid);
2822 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2823 per_pin->dev_id);
2824 intel_verify_pin_cvt_connect(codec, per_pin);
2825 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
2826 per_pin->dev_id, per_pin->mux_idx);
2827 } else {
2828 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
2829 }
2830}
2831
2832/* precondition and allocation for Intel codecs */
2833static int alloc_intel_hdmi(struct hda_codec *codec)
2834{
2835 int err;
2836
2837 /* requires i915 binding */
2838 if (!codec->bus->core.audio_component) {
2839 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2840 /* set probe_id here to prevent generic fallback binding */
2841 codec->probe_id = HDA_CODEC_ID_SKIP_PROBE;
2842 return -ENODEV;
2843 }
2844
2845 err = alloc_generic_hdmi(codec);
2846 if (err < 0)
2847 return err;
2848 /* no need to handle unsol events */
2849 codec->patch_ops.unsol_event = NULL;
2850 return 0;
2851}
2852
2853/* parse and post-process for Intel codecs */
2854static int parse_intel_hdmi(struct hda_codec *codec)
2855{
2856 int err, retries = 3;
2857
2858 do {
2859 err = hdmi_parse_codec(codec);
2860 } while (err < 0 && retries--);
2861
2862 if (err < 0) {
2863 generic_spec_free(codec);
2864 return err;
2865 }
2866
2867 generic_hdmi_init_per_pins(codec);
2868 register_i915_notifier(codec);
2869 return 0;
2870}
2871
2872/* Intel Haswell and onwards; audio component with eld notifier */
2873static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2874 const int *port_map, int port_num)
2875{
2876 struct hdmi_spec *spec;
2877 int err;
2878
2879 err = alloc_intel_hdmi(codec);
2880 if (err < 0)
2881 return err;
2882 spec = codec->spec;
2883 codec->dp_mst = true;
2884 spec->dyn_pcm_assign = true;
2885 spec->vendor_nid = vendor_nid;
2886 spec->port_map = port_map;
2887 spec->port_num = port_num;
2888
2889 intel_haswell_enable_all_pins(codec, true);
2890 intel_haswell_fixup_enable_dp12(codec);
2891
2892 codec->display_power_control = 1;
2893
2894 codec->patch_ops.set_power_state = haswell_set_power_state;
2895 codec->depop_delay = 0;
2896 codec->auto_runtime_pm = 1;
2897
2898 spec->ops.setup_stream = i915_hsw_setup_stream;
2899 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2900
2901 return parse_intel_hdmi(codec);
2902}
2903
2904static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2905{
2906 return intel_hsw_common_init(codec, 0x08, NULL, 0);
2907}
2908
2909static int patch_i915_glk_hdmi(struct hda_codec *codec)
2910{
2911 return intel_hsw_common_init(codec, 0x0b, NULL, 0);
2912}
2913
2914static int patch_i915_icl_hdmi(struct hda_codec *codec)
2915{
2916 /*
2917 * pin to port mapping table where the value indicate the pin number and
2918 * the index indicate the port number with 1 base.
2919 */
2920 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
2921
2922 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2923}
2924
2925static int patch_i915_tgl_hdmi(struct hda_codec *codec)
2926{
2927 /*
2928 * pin to port mapping table where the value indicate the pin number and
2929 * the index indicate the port number with 1 base.
2930 */
2931 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
2932
2933 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2934}
2935
2936
2937/* Intel Baytrail and Braswell; with eld notifier */
2938static int patch_i915_byt_hdmi(struct hda_codec *codec)
2939{
2940 struct hdmi_spec *spec;
2941 int err;
2942
2943 err = alloc_intel_hdmi(codec);
2944 if (err < 0)
2945 return err;
2946 spec = codec->spec;
2947
2948 /* For Valleyview/Cherryview, only the display codec is in the display
2949 * power well and can use link_power ops to request/release the power.
2950 */
2951 codec->display_power_control = 1;
2952
2953 codec->depop_delay = 0;
2954 codec->auto_runtime_pm = 1;
2955
2956 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2957
2958 return parse_intel_hdmi(codec);
2959}
2960
2961/* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
2962static int patch_i915_cpt_hdmi(struct hda_codec *codec)
2963{
2964 int err;
2965
2966 err = alloc_intel_hdmi(codec);
2967 if (err < 0)
2968 return err;
2969 return parse_intel_hdmi(codec);
2970}
2971
2972/*
2973 * Shared non-generic implementations
2974 */
2975
2976static int simple_playback_build_pcms(struct hda_codec *codec)
2977{
2978 struct hdmi_spec *spec = codec->spec;
2979 struct hda_pcm *info;
2980 unsigned int chans;
2981 struct hda_pcm_stream *pstr;
2982 struct hdmi_spec_per_cvt *per_cvt;
2983
2984 per_cvt = get_cvt(spec, 0);
2985 chans = get_wcaps(codec, per_cvt->cvt_nid);
2986 chans = get_wcaps_channels(chans);
2987
2988 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2989 if (!info)
2990 return -ENOMEM;
2991 spec->pcm_rec[0].pcm = info;
2992 info->pcm_type = HDA_PCM_TYPE_HDMI;
2993 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2994 *pstr = spec->pcm_playback;
2995 pstr->nid = per_cvt->cvt_nid;
2996 if (pstr->channels_max <= 2 && chans && chans <= 16)
2997 pstr->channels_max = chans;
2998
2999 return 0;
3000}
3001
3002/* unsolicited event for jack sensing */
3003static void simple_hdmi_unsol_event(struct hda_codec *codec,
3004 unsigned int res)
3005{
3006 snd_hda_jack_set_dirty_all(codec);
3007 snd_hda_jack_report_sync(codec);
3008}
3009
3010/* generic_hdmi_build_jack can be used for simple_hdmi, too,
3011 * as long as spec->pins[] is set correctly
3012 */
3013#define simple_hdmi_build_jack generic_hdmi_build_jack
3014
3015static int simple_playback_build_controls(struct hda_codec *codec)
3016{
3017 struct hdmi_spec *spec = codec->spec;
3018 struct hdmi_spec_per_cvt *per_cvt;
3019 int err;
3020
3021 per_cvt = get_cvt(spec, 0);
3022 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
3023 per_cvt->cvt_nid,
3024 HDA_PCM_TYPE_HDMI);
3025 if (err < 0)
3026 return err;
3027 return simple_hdmi_build_jack(codec, 0);
3028}
3029
3030static int simple_playback_init(struct hda_codec *codec)
3031{
3032 struct hdmi_spec *spec = codec->spec;
3033 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
3034 hda_nid_t pin = per_pin->pin_nid;
3035
3036 snd_hda_codec_write(codec, pin, 0,
3037 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3038 /* some codecs require to unmute the pin */
3039 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3040 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3041 AMP_OUT_UNMUTE);
3042 snd_hda_jack_detect_enable(codec, pin);
3043 return 0;
3044}
3045
3046static void simple_playback_free(struct hda_codec *codec)
3047{
3048 struct hdmi_spec *spec = codec->spec;
3049
3050 hdmi_array_free(spec);
3051 kfree(spec);
3052}
3053
3054/*
3055 * Nvidia specific implementations
3056 */
3057
3058#define Nv_VERB_SET_Channel_Allocation 0xF79
3059#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
3060#define Nv_VERB_SET_Audio_Protection_On 0xF98
3061#define Nv_VERB_SET_Audio_Protection_Off 0xF99
3062
3063#define nvhdmi_master_con_nid_7x 0x04
3064#define nvhdmi_master_pin_nid_7x 0x05
3065
3066static const hda_nid_t nvhdmi_con_nids_7x[4] = {
3067 /*front, rear, clfe, rear_surr */
3068 0x6, 0x8, 0xa, 0xc,
3069};
3070
3071static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3072 /* set audio protect on */
3073 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3074 /* enable digital output on pin widget */
3075 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3076 {} /* terminator */
3077};
3078
3079static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
3080 /* set audio protect on */
3081 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3082 /* enable digital output on pin widget */
3083 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3084 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3085 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3086 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3087 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3088 {} /* terminator */
3089};
3090
3091#ifdef LIMITED_RATE_FMT_SUPPORT
3092/* support only the safe format and rate */
3093#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3094#define SUPPORTED_MAXBPS 16
3095#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3096#else
3097/* support all rates and formats */
3098#define SUPPORTED_RATES \
3099 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3100 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3101 SNDRV_PCM_RATE_192000)
3102#define SUPPORTED_MAXBPS 24
3103#define SUPPORTED_FORMATS \
3104 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3105#endif
3106
3107static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
3108{
3109 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3110 return 0;
3111}
3112
3113static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3114{
3115 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
3116 return 0;
3117}
3118
3119static const unsigned int channels_2_6_8[] = {
3120 2, 6, 8
3121};
3122
3123static const unsigned int channels_2_8[] = {
3124 2, 8
3125};
3126
3127static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3128 .count = ARRAY_SIZE(channels_2_6_8),
3129 .list = channels_2_6_8,
3130 .mask = 0,
3131};
3132
3133static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3134 .count = ARRAY_SIZE(channels_2_8),
3135 .list = channels_2_8,
3136 .mask = 0,
3137};
3138
3139static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3140 struct hda_codec *codec,
3141 struct snd_pcm_substream *substream)
3142{
3143 struct hdmi_spec *spec = codec->spec;
3144 const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3145
3146 switch (codec->preset->vendor_id) {
3147 case 0x10de0002:
3148 case 0x10de0003:
3149 case 0x10de0005:
3150 case 0x10de0006:
3151 hw_constraints_channels = &hw_constraints_2_8_channels;
3152 break;
3153 case 0x10de0007:
3154 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3155 break;
3156 default:
3157 break;
3158 }
3159
3160 if (hw_constraints_channels != NULL) {
3161 snd_pcm_hw_constraint_list(substream->runtime, 0,
3162 SNDRV_PCM_HW_PARAM_CHANNELS,
3163 hw_constraints_channels);
3164 } else {
3165 snd_pcm_hw_constraint_step(substream->runtime, 0,
3166 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3167 }
3168
3169 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3170}
3171
3172static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3173 struct hda_codec *codec,
3174 struct snd_pcm_substream *substream)
3175{
3176 struct hdmi_spec *spec = codec->spec;
3177 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3178}
3179
3180static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3181 struct hda_codec *codec,
3182 unsigned int stream_tag,
3183 unsigned int format,
3184 struct snd_pcm_substream *substream)
3185{
3186 struct hdmi_spec *spec = codec->spec;
3187 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3188 stream_tag, format, substream);
3189}
3190
3191static const struct hda_pcm_stream simple_pcm_playback = {
3192 .substreams = 1,
3193 .channels_min = 2,
3194 .channels_max = 2,
3195 .ops = {
3196 .open = simple_playback_pcm_open,
3197 .close = simple_playback_pcm_close,
3198 .prepare = simple_playback_pcm_prepare
3199 },
3200};
3201
3202static const struct hda_codec_ops simple_hdmi_patch_ops = {
3203 .build_controls = simple_playback_build_controls,
3204 .build_pcms = simple_playback_build_pcms,
3205 .init = simple_playback_init,
3206 .free = simple_playback_free,
3207 .unsol_event = simple_hdmi_unsol_event,
3208};
3209
3210static int patch_simple_hdmi(struct hda_codec *codec,
3211 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3212{
3213 struct hdmi_spec *spec;
3214 struct hdmi_spec_per_cvt *per_cvt;
3215 struct hdmi_spec_per_pin *per_pin;
3216
3217 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3218 if (!spec)
3219 return -ENOMEM;
3220
3221 spec->codec = codec;
3222 codec->spec = spec;
3223 hdmi_array_init(spec, 1);
3224
3225 spec->multiout.num_dacs = 0; /* no analog */
3226 spec->multiout.max_channels = 2;
3227 spec->multiout.dig_out_nid = cvt_nid;
3228 spec->num_cvts = 1;
3229 spec->num_pins = 1;
3230 per_pin = snd_array_new(&spec->pins);
3231 per_cvt = snd_array_new(&spec->cvts);
3232 if (!per_pin || !per_cvt) {
3233 simple_playback_free(codec);
3234 return -ENOMEM;
3235 }
3236 per_cvt->cvt_nid = cvt_nid;
3237 per_pin->pin_nid = pin_nid;
3238 spec->pcm_playback = simple_pcm_playback;
3239
3240 codec->patch_ops = simple_hdmi_patch_ops;
3241
3242 return 0;
3243}
3244
3245static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3246 int channels)
3247{
3248 unsigned int chanmask;
3249 int chan = channels ? (channels - 1) : 1;
3250
3251 switch (channels) {
3252 default:
3253 case 0:
3254 case 2:
3255 chanmask = 0x00;
3256 break;
3257 case 4:
3258 chanmask = 0x08;
3259 break;
3260 case 6:
3261 chanmask = 0x0b;
3262 break;
3263 case 8:
3264 chanmask = 0x13;
3265 break;
3266 }
3267
3268 /* Set the audio infoframe channel allocation and checksum fields. The
3269 * channel count is computed implicitly by the hardware. */
3270 snd_hda_codec_write(codec, 0x1, 0,
3271 Nv_VERB_SET_Channel_Allocation, chanmask);
3272
3273 snd_hda_codec_write(codec, 0x1, 0,
3274 Nv_VERB_SET_Info_Frame_Checksum,
3275 (0x71 - chan - chanmask));
3276}
3277
3278static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3279 struct hda_codec *codec,
3280 struct snd_pcm_substream *substream)
3281{
3282 struct hdmi_spec *spec = codec->spec;
3283 int i;
3284
3285 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3286 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3287 for (i = 0; i < 4; i++) {
3288 /* set the stream id */
3289 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3290 AC_VERB_SET_CHANNEL_STREAMID, 0);
3291 /* set the stream format */
3292 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3293 AC_VERB_SET_STREAM_FORMAT, 0);
3294 }
3295
3296 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3297 * streams are disabled. */
3298 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3299
3300 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3301}
3302
3303static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3304 struct hda_codec *codec,
3305 unsigned int stream_tag,
3306 unsigned int format,
3307 struct snd_pcm_substream *substream)
3308{
3309 int chs;
3310 unsigned int dataDCC2, channel_id;
3311 int i;
3312 struct hdmi_spec *spec = codec->spec;
3313 struct hda_spdif_out *spdif;
3314 struct hdmi_spec_per_cvt *per_cvt;
3315
3316 mutex_lock(&codec->spdif_mutex);
3317 per_cvt = get_cvt(spec, 0);
3318 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3319
3320 chs = substream->runtime->channels;
3321
3322 dataDCC2 = 0x2;
3323
3324 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3325 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3326 snd_hda_codec_write(codec,
3327 nvhdmi_master_con_nid_7x,
3328 0,
3329 AC_VERB_SET_DIGI_CONVERT_1,
3330 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3331
3332 /* set the stream id */
3333 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3334 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3335
3336 /* set the stream format */
3337 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3338 AC_VERB_SET_STREAM_FORMAT, format);
3339
3340 /* turn on again (if needed) */
3341 /* enable and set the channel status audio/data flag */
3342 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3343 snd_hda_codec_write(codec,
3344 nvhdmi_master_con_nid_7x,
3345 0,
3346 AC_VERB_SET_DIGI_CONVERT_1,
3347 spdif->ctls & 0xff);
3348 snd_hda_codec_write(codec,
3349 nvhdmi_master_con_nid_7x,
3350 0,
3351 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3352 }
3353
3354 for (i = 0; i < 4; i++) {
3355 if (chs == 2)
3356 channel_id = 0;
3357 else
3358 channel_id = i * 2;
3359
3360 /* turn off SPDIF once;
3361 *otherwise the IEC958 bits won't be updated
3362 */
3363 if (codec->spdif_status_reset &&
3364 (spdif->ctls & AC_DIG1_ENABLE))
3365 snd_hda_codec_write(codec,
3366 nvhdmi_con_nids_7x[i],
3367 0,
3368 AC_VERB_SET_DIGI_CONVERT_1,
3369 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3370 /* set the stream id */
3371 snd_hda_codec_write(codec,
3372 nvhdmi_con_nids_7x[i],
3373 0,
3374 AC_VERB_SET_CHANNEL_STREAMID,
3375 (stream_tag << 4) | channel_id);
3376 /* set the stream format */
3377 snd_hda_codec_write(codec,
3378 nvhdmi_con_nids_7x[i],
3379 0,
3380 AC_VERB_SET_STREAM_FORMAT,
3381 format);
3382 /* turn on again (if needed) */
3383 /* enable and set the channel status audio/data flag */
3384 if (codec->spdif_status_reset &&
3385 (spdif->ctls & AC_DIG1_ENABLE)) {
3386 snd_hda_codec_write(codec,
3387 nvhdmi_con_nids_7x[i],
3388 0,
3389 AC_VERB_SET_DIGI_CONVERT_1,
3390 spdif->ctls & 0xff);
3391 snd_hda_codec_write(codec,
3392 nvhdmi_con_nids_7x[i],
3393 0,
3394 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3395 }
3396 }
3397
3398 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3399
3400 mutex_unlock(&codec->spdif_mutex);
3401 return 0;
3402}
3403
3404static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3405 .substreams = 1,
3406 .channels_min = 2,
3407 .channels_max = 8,
3408 .nid = nvhdmi_master_con_nid_7x,
3409 .rates = SUPPORTED_RATES,
3410 .maxbps = SUPPORTED_MAXBPS,
3411 .formats = SUPPORTED_FORMATS,
3412 .ops = {
3413 .open = simple_playback_pcm_open,
3414 .close = nvhdmi_8ch_7x_pcm_close,
3415 .prepare = nvhdmi_8ch_7x_pcm_prepare
3416 },
3417};
3418
3419static int patch_nvhdmi_2ch(struct hda_codec *codec)
3420{
3421 struct hdmi_spec *spec;
3422 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3423 nvhdmi_master_pin_nid_7x);
3424 if (err < 0)
3425 return err;
3426
3427 codec->patch_ops.init = nvhdmi_7x_init_2ch;
3428 /* override the PCM rates, etc, as the codec doesn't give full list */
3429 spec = codec->spec;
3430 spec->pcm_playback.rates = SUPPORTED_RATES;
3431 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3432 spec->pcm_playback.formats = SUPPORTED_FORMATS;
3433 return 0;
3434}
3435
3436static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3437{
3438 struct hdmi_spec *spec = codec->spec;
3439 int err = simple_playback_build_pcms(codec);
3440 if (!err) {
3441 struct hda_pcm *info = get_pcm_rec(spec, 0);
3442 info->own_chmap = true;
3443 }
3444 return err;
3445}
3446
3447static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3448{
3449 struct hdmi_spec *spec = codec->spec;
3450 struct hda_pcm *info;
3451 struct snd_pcm_chmap *chmap;
3452 int err;
3453
3454 err = simple_playback_build_controls(codec);
3455 if (err < 0)
3456 return err;
3457
3458 /* add channel maps */
3459 info = get_pcm_rec(spec, 0);
3460 err = snd_pcm_add_chmap_ctls(info->pcm,
3461 SNDRV_PCM_STREAM_PLAYBACK,
3462 snd_pcm_alt_chmaps, 8, 0, &chmap);
3463 if (err < 0)
3464 return err;
3465 switch (codec->preset->vendor_id) {
3466 case 0x10de0002:
3467 case 0x10de0003:
3468 case 0x10de0005:
3469 case 0x10de0006:
3470 chmap->channel_mask = (1U << 2) | (1U << 8);
3471 break;
3472 case 0x10de0007:
3473 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3474 }
3475 return 0;
3476}
3477
3478static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3479{
3480 struct hdmi_spec *spec;
3481 int err = patch_nvhdmi_2ch(codec);
3482 if (err < 0)
3483 return err;
3484 spec = codec->spec;
3485 spec->multiout.max_channels = 8;
3486 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3487 codec->patch_ops.init = nvhdmi_7x_init_8ch;
3488 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3489 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3490
3491 /* Initialize the audio infoframe channel mask and checksum to something
3492 * valid */
3493 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3494
3495 return 0;
3496}
3497
3498/*
3499 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3500 * - 0x10de0015
3501 * - 0x10de0040
3502 */
3503static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
3504 struct hdac_cea_channel_speaker_allocation *cap, int channels)
3505{
3506 if (cap->ca_index == 0x00 && channels == 2)
3507 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3508
3509 /* If the speaker allocation matches the channel count, it is OK. */
3510 if (cap->channels != channels)
3511 return -1;
3512
3513 /* all channels are remappable freely */
3514 return SNDRV_CTL_TLVT_CHMAP_VAR;
3515}
3516
3517static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3518 int ca, int chs, unsigned char *map)
3519{
3520 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3521 return -EINVAL;
3522
3523 return 0;
3524}
3525
3526static int patch_nvhdmi(struct hda_codec *codec)
3527{
3528 struct hdmi_spec *spec;
3529 int err;
3530
3531 err = patch_generic_hdmi(codec);
3532 if (err)
3533 return err;
3534
3535 spec = codec->spec;
3536 spec->dyn_pin_out = true;
3537
3538 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3539 nvhdmi_chmap_cea_alloc_validate_get_type;
3540 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3541
3542 codec->link_down_at_suspend = 1;
3543
3544 return 0;
3545}
3546
3547/*
3548 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3549 * accessed using vendor-defined verbs. These registers can be used for
3550 * interoperability between the HDA and HDMI drivers.
3551 */
3552
3553/* Audio Function Group node */
3554#define NVIDIA_AFG_NID 0x01
3555
3556/*
3557 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3558 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3559 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3560 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3561 * additional bit (at position 30) to signal the validity of the format.
3562 *
3563 * | 31 | 30 | 29 16 | 15 0 |
3564 * +---------+-------+--------+--------+
3565 * | TRIGGER | VALID | UNUSED | FORMAT |
3566 * +-----------------------------------|
3567 *
3568 * Note that for the trigger bit to take effect it needs to change value
3569 * (i.e. it needs to be toggled).
3570 */
3571#define NVIDIA_GET_SCRATCH0 0xfa6
3572#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3573#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3574#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3575#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3576#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3577#define NVIDIA_SCRATCH_VALID (1 << 6)
3578
3579#define NVIDIA_GET_SCRATCH1 0xfab
3580#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3581#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3582#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3583#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3584
3585/*
3586 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3587 * the format is invalidated so that the HDMI codec can be disabled.
3588 */
3589static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3590{
3591 unsigned int value;
3592
3593 /* bits [31:30] contain the trigger and valid bits */
3594 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3595 NVIDIA_GET_SCRATCH0, 0);
3596 value = (value >> 24) & 0xff;
3597
3598 /* bits [15:0] are used to store the HDA format */
3599 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3600 NVIDIA_SET_SCRATCH0_BYTE0,
3601 (format >> 0) & 0xff);
3602 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3603 NVIDIA_SET_SCRATCH0_BYTE1,
3604 (format >> 8) & 0xff);
3605
3606 /* bits [16:24] are unused */
3607 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3608 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3609
3610 /*
3611 * Bit 30 signals that the data is valid and hence that HDMI audio can
3612 * be enabled.
3613 */
3614 if (format == 0)
3615 value &= ~NVIDIA_SCRATCH_VALID;
3616 else
3617 value |= NVIDIA_SCRATCH_VALID;
3618
3619 /*
3620 * Whenever the trigger bit is toggled, an interrupt is raised in the
3621 * HDMI codec. The HDMI driver will use that as trigger to update its
3622 * configuration.
3623 */
3624 value ^= NVIDIA_SCRATCH_TRIGGER;
3625
3626 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3627 NVIDIA_SET_SCRATCH0_BYTE3, value);
3628}
3629
3630static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3631 struct hda_codec *codec,
3632 unsigned int stream_tag,
3633 unsigned int format,
3634 struct snd_pcm_substream *substream)
3635{
3636 int err;
3637
3638 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3639 format, substream);
3640 if (err < 0)
3641 return err;
3642
3643 /* notify the HDMI codec of the format change */
3644 tegra_hdmi_set_format(codec, format);
3645
3646 return 0;
3647}
3648
3649static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3650 struct hda_codec *codec,
3651 struct snd_pcm_substream *substream)
3652{
3653 /* invalidate the format in the HDMI codec */
3654 tegra_hdmi_set_format(codec, 0);
3655
3656 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3657}
3658
3659static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3660{
3661 struct hdmi_spec *spec = codec->spec;
3662 unsigned int i;
3663
3664 for (i = 0; i < spec->num_pins; i++) {
3665 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3666
3667 if (pcm->pcm_type == type)
3668 return pcm;
3669 }
3670
3671 return NULL;
3672}
3673
3674static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3675{
3676 struct hda_pcm_stream *stream;
3677 struct hda_pcm *pcm;
3678 int err;
3679
3680 err = generic_hdmi_build_pcms(codec);
3681 if (err < 0)
3682 return err;
3683
3684 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3685 if (!pcm)
3686 return -ENODEV;
3687
3688 /*
3689 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3690 * codec about format changes.
3691 */
3692 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3693 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3694 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3695
3696 return 0;
3697}
3698
3699static int patch_tegra_hdmi(struct hda_codec *codec)
3700{
3701 struct hdmi_spec *spec;
3702 int err;
3703
3704 err = patch_generic_hdmi(codec);
3705 if (err)
3706 return err;
3707
3708 codec->depop_delay = 10;
3709 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3710 spec = codec->spec;
3711 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3712 nvhdmi_chmap_cea_alloc_validate_get_type;
3713 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3714
3715 return 0;
3716}
3717
3718/*
3719 * ATI/AMD-specific implementations
3720 */
3721
3722#define is_amdhdmi_rev3_or_later(codec) \
3723 ((codec)->core.vendor_id == 0x1002aa01 && \
3724 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3725#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3726
3727/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3728#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3729#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3730#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3731#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3732#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3733#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3734#define ATI_VERB_SET_HBR_CONTROL 0x77c
3735#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3736#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3737#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3738#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3739#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3740#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3741#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3742#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3743#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3744#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3745#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3746#define ATI_VERB_GET_HBR_CONTROL 0xf7c
3747#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3748#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3749#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3750#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3751#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3752
3753/* AMD specific HDA cvt verbs */
3754#define ATI_VERB_SET_RAMP_RATE 0x770
3755#define ATI_VERB_GET_RAMP_RATE 0xf70
3756
3757#define ATI_OUT_ENABLE 0x1
3758
3759#define ATI_MULTICHANNEL_MODE_PAIRED 0
3760#define ATI_MULTICHANNEL_MODE_SINGLE 1
3761
3762#define ATI_HBR_CAPABLE 0x01
3763#define ATI_HBR_ENABLE 0x10
3764
3765static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3766 unsigned char *buf, int *eld_size)
3767{
3768 /* call hda_eld.c ATI/AMD-specific function */
3769 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3770 is_amdhdmi_rev3_or_later(codec));
3771}
3772
3773static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3774 int active_channels, int conn_type)
3775{
3776 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3777}
3778
3779static int atihdmi_paired_swap_fc_lfe(int pos)
3780{
3781 /*
3782 * ATI/AMD have automatic FC/LFE swap built-in
3783 * when in pairwise mapping mode.
3784 */
3785
3786 switch (pos) {
3787 /* see channel_allocations[].speakers[] */
3788 case 2: return 3;
3789 case 3: return 2;
3790 default: break;
3791 }
3792
3793 return pos;
3794}
3795
3796static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3797 int ca, int chs, unsigned char *map)
3798{
3799 struct hdac_cea_channel_speaker_allocation *cap;
3800 int i, j;
3801
3802 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3803
3804 cap = snd_hdac_get_ch_alloc_from_ca(ca);
3805 for (i = 0; i < chs; ++i) {
3806 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
3807 bool ok = false;
3808 bool companion_ok = false;
3809
3810 if (!mask)
3811 continue;
3812
3813 for (j = 0 + i % 2; j < 8; j += 2) {
3814 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3815 if (cap->speakers[chan_idx] == mask) {
3816 /* channel is in a supported position */
3817 ok = true;
3818
3819 if (i % 2 == 0 && i + 1 < chs) {
3820 /* even channel, check the odd companion */
3821 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3822 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
3823 int comp_mask_act = cap->speakers[comp_chan_idx];
3824
3825 if (comp_mask_req == comp_mask_act)
3826 companion_ok = true;
3827 else
3828 return -EINVAL;
3829 }
3830 break;
3831 }
3832 }
3833
3834 if (!ok)
3835 return -EINVAL;
3836
3837 if (companion_ok)
3838 i++; /* companion channel already checked */
3839 }
3840
3841 return 0;
3842}
3843
3844static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3845 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
3846{
3847 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3848 int verb;
3849 int ati_channel_setup = 0;
3850
3851 if (hdmi_slot > 7)
3852 return -EINVAL;
3853
3854 if (!has_amd_full_remap_support(codec)) {
3855 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3856
3857 /* In case this is an odd slot but without stream channel, do not
3858 * disable the slot since the corresponding even slot could have a
3859 * channel. In case neither have a channel, the slot pair will be
3860 * disabled when this function is called for the even slot. */
3861 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3862 return 0;
3863
3864 hdmi_slot -= hdmi_slot % 2;
3865
3866 if (stream_channel != 0xf)
3867 stream_channel -= stream_channel % 2;
3868 }
3869
3870 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3871
3872 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3873
3874 if (stream_channel != 0xf)
3875 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3876
3877 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3878}
3879
3880static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3881 hda_nid_t pin_nid, int asp_slot)
3882{
3883 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3884 bool was_odd = false;
3885 int ati_asp_slot = asp_slot;
3886 int verb;
3887 int ati_channel_setup;
3888
3889 if (asp_slot > 7)
3890 return -EINVAL;
3891
3892 if (!has_amd_full_remap_support(codec)) {
3893 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3894 if (ati_asp_slot % 2 != 0) {
3895 ati_asp_slot -= 1;
3896 was_odd = true;
3897 }
3898 }
3899
3900 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3901
3902 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3903
3904 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3905 return 0xf;
3906
3907 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3908}
3909
3910static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3911 struct hdac_chmap *chmap,
3912 struct hdac_cea_channel_speaker_allocation *cap,
3913 int channels)
3914{
3915 int c;
3916
3917 /*
3918 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3919 * we need to take that into account (a single channel may take 2
3920 * channel slots if we need to carry a silent channel next to it).
3921 * On Rev3+ AMD codecs this function is not used.
3922 */
3923 int chanpairs = 0;
3924
3925 /* We only produce even-numbered channel count TLVs */
3926 if ((channels % 2) != 0)
3927 return -1;
3928
3929 for (c = 0; c < 7; c += 2) {
3930 if (cap->speakers[c] || cap->speakers[c+1])
3931 chanpairs++;
3932 }
3933
3934 if (chanpairs * 2 != channels)
3935 return -1;
3936
3937 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3938}
3939
3940static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
3941 struct hdac_cea_channel_speaker_allocation *cap,
3942 unsigned int *chmap, int channels)
3943{
3944 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3945 int count = 0;
3946 int c;
3947
3948 for (c = 7; c >= 0; c--) {
3949 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3950 int spk = cap->speakers[chan];
3951 if (!spk) {
3952 /* add N/A channel if the companion channel is occupied */
3953 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3954 chmap[count++] = SNDRV_CHMAP_NA;
3955
3956 continue;
3957 }
3958
3959 chmap[count++] = snd_hdac_spk_to_chmap(spk);
3960 }
3961
3962 WARN_ON(count != channels);
3963}
3964
3965static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3966 bool hbr)
3967{
3968 int hbr_ctl, hbr_ctl_new;
3969
3970 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3971 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3972 if (hbr)
3973 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3974 else
3975 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3976
3977 codec_dbg(codec,
3978 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3979 pin_nid,
3980 hbr_ctl == hbr_ctl_new ? "" : "new-",
3981 hbr_ctl_new);
3982
3983 if (hbr_ctl != hbr_ctl_new)
3984 snd_hda_codec_write(codec, pin_nid, 0,
3985 ATI_VERB_SET_HBR_CONTROL,
3986 hbr_ctl_new);
3987
3988 } else if (hbr)
3989 return -EINVAL;
3990
3991 return 0;
3992}
3993
3994static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3995 hda_nid_t pin_nid, u32 stream_tag, int format)
3996{
3997
3998 if (is_amdhdmi_rev3_or_later(codec)) {
3999 int ramp_rate = 180; /* default as per AMD spec */
4000 /* disable ramp-up/down for non-pcm as per AMD spec */
4001 if (format & AC_FMT_TYPE_NON_PCM)
4002 ramp_rate = 0;
4003
4004 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
4005 }
4006
4007 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
4008}
4009
4010
4011static int atihdmi_init(struct hda_codec *codec)
4012{
4013 struct hdmi_spec *spec = codec->spec;
4014 int pin_idx, err;
4015
4016 err = generic_hdmi_init(codec);
4017
4018 if (err)
4019 return err;
4020
4021 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
4022 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
4023
4024 /* make sure downmix information in infoframe is zero */
4025 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
4026
4027 /* enable channel-wise remap mode if supported */
4028 if (has_amd_full_remap_support(codec))
4029 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
4030 ATI_VERB_SET_MULTICHANNEL_MODE,
4031 ATI_MULTICHANNEL_MODE_SINGLE);
4032 }
4033
4034 return 0;
4035}
4036
4037/* map from pin NID to port; port is 0-based */
4038/* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */
4039static int atihdmi_pin2port(void *audio_ptr, int pin_nid)
4040{
4041 return pin_nid / 2 - 1;
4042}
4043
4044/* reverse-map from port to pin NID: see above */
4045static int atihdmi_port2pin(struct hda_codec *codec, int port)
4046{
4047 return port * 2 + 3;
4048}
4049
4050static const struct drm_audio_component_audio_ops atihdmi_audio_ops = {
4051 .pin2port = atihdmi_pin2port,
4052 .pin_eld_notify = generic_acomp_pin_eld_notify,
4053 .master_bind = generic_acomp_master_bind,
4054 .master_unbind = generic_acomp_master_unbind,
4055};
4056
4057static int patch_atihdmi(struct hda_codec *codec)
4058{
4059 struct hdmi_spec *spec;
4060 struct hdmi_spec_per_cvt *per_cvt;
4061 int err, cvt_idx;
4062
4063 err = patch_generic_hdmi(codec);
4064
4065 if (err)
4066 return err;
4067
4068 codec->patch_ops.init = atihdmi_init;
4069
4070 spec = codec->spec;
4071
4072 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
4073 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
4074 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
4075 spec->ops.setup_stream = atihdmi_setup_stream;
4076
4077 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4078 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4079
4080 if (!has_amd_full_remap_support(codec)) {
4081 /* override to ATI/AMD-specific versions with pairwise mapping */
4082 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
4083 atihdmi_paired_chmap_cea_alloc_validate_get_type;
4084 spec->chmap.ops.cea_alloc_to_tlv_chmap =
4085 atihdmi_paired_cea_alloc_to_tlv_chmap;
4086 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
4087 }
4088
4089 /* ATI/AMD converters do not advertise all of their capabilities */
4090 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4091 per_cvt = get_cvt(spec, cvt_idx);
4092 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4093 per_cvt->rates |= SUPPORTED_RATES;
4094 per_cvt->formats |= SUPPORTED_FORMATS;
4095 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4096 }
4097
4098 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
4099
4100 /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
4101 * the link-down as is. Tell the core to allow it.
4102 */
4103 codec->link_down_at_suspend = 1;
4104
4105 generic_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin);
4106
4107 return 0;
4108}
4109
4110/* VIA HDMI Implementation */
4111#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4112#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4113
4114static int patch_via_hdmi(struct hda_codec *codec)
4115{
4116 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
4117}
4118
4119/*
4120 * patch entries
4121 */
4122static const struct hda_device_id snd_hda_id_hdmi[] = {
4123HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4124HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4125HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4126HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4127HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4128HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4129HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4130HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI", patch_nvhdmi_2ch),
4131HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4132HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4133HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI", patch_nvhdmi_8ch_7x),
4134HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4135HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4136HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4137HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP", patch_nvhdmi),
4138HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP", patch_nvhdmi),
4139HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4140HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4141HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4142HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4143HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4144HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4145HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4146HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4147HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4148HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4149HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
4150/* 17 is known to be absent */
4151HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4152HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4153HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4154HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4155HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4156HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4157HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4158HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4159HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4160HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi),
4161HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi),
4162HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi),
4163HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi),
4164HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4165HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4166HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4167HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4168HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4169HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP", patch_nvhdmi),
4170HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP", patch_nvhdmi),
4171HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4172HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP", patch_nvhdmi),
4173HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4174HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP", patch_nvhdmi),
4175HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP", patch_nvhdmi),
4176HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4177HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4178HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4179HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4180HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP", patch_nvhdmi),
4181HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP", patch_nvhdmi),
4182HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP", patch_nvhdmi),
4183HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP", patch_nvhdmi),
4184HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP", patch_nvhdmi),
4185HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
4186HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP", patch_nvhdmi),
4187HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi),
4188HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP", patch_nvhdmi),
4189HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
4190HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
4191HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP", patch_nvhdmi),
4192HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP", patch_nvhdmi),
4193HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP", patch_nvhdmi),
4194HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP", patch_nvhdmi),
4195HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP", patch_nvhdmi),
4196HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP", patch_nvhdmi),
4197HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP", patch_nvhdmi),
4198HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP", patch_nvhdmi),
4199HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP", patch_nvhdmi),
4200HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP", patch_nvhdmi),
4201HDA_CODEC_ENTRY(0x10de009a, "GPU 9a HDMI/DP", patch_nvhdmi),
4202HDA_CODEC_ENTRY(0x10de009d, "GPU 9d HDMI/DP", patch_nvhdmi),
4203HDA_CODEC_ENTRY(0x10de009e, "GPU 9e HDMI/DP", patch_nvhdmi),
4204HDA_CODEC_ENTRY(0x10de009f, "GPU 9f HDMI/DP", patch_nvhdmi),
4205HDA_CODEC_ENTRY(0x10de00a0, "GPU a0 HDMI/DP", patch_nvhdmi),
4206HDA_CODEC_ENTRY(0x10de00a3, "GPU a3 HDMI/DP", patch_nvhdmi),
4207HDA_CODEC_ENTRY(0x10de00a4, "GPU a4 HDMI/DP", patch_nvhdmi),
4208HDA_CODEC_ENTRY(0x10de00a5, "GPU a5 HDMI/DP", patch_nvhdmi),
4209HDA_CODEC_ENTRY(0x10de00a6, "GPU a6 HDMI/DP", patch_nvhdmi),
4210HDA_CODEC_ENTRY(0x10de00a7, "GPU a7 HDMI/DP", patch_nvhdmi),
4211HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4212HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI", patch_nvhdmi_2ch),
4213HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4214HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4215HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4216HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4217HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi),
4218HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI", patch_i915_glk_hdmi),
4219HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4220HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4221HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4222HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi),
4223HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi),
4224HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
4225HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
4226HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
4227HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
4228HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
4229HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
4230HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI", patch_i915_glk_hdmi),
4231HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi),
4232HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI", patch_i915_icl_hdmi),
4233HDA_CODEC_ENTRY(0x80862812, "Tigerlake HDMI", patch_i915_tgl_hdmi),
4234HDA_CODEC_ENTRY(0x80862814, "DG1 HDMI", patch_i915_tgl_hdmi),
4235HDA_CODEC_ENTRY(0x80862815, "Alderlake HDMI", patch_i915_tgl_hdmi),
4236HDA_CODEC_ENTRY(0x80862816, "Rocketlake HDMI", patch_i915_tgl_hdmi),
4237HDA_CODEC_ENTRY(0x80862819, "DG2 HDMI", patch_i915_tgl_hdmi),
4238HDA_CODEC_ENTRY(0x8086281a, "Jasperlake HDMI", patch_i915_icl_hdmi),
4239HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4240HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
4241HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
4242HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
4243/* special ID for generic HDMI */
4244HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4245{} /* terminator */
4246};
4247MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4248
4249MODULE_LICENSE("GPL");
4250MODULE_DESCRIPTION("HDMI HD-audio codec");
4251MODULE_ALIAS("snd-hda-codec-intelhdmi");
4252MODULE_ALIAS("snd-hda-codec-nvhdmi");
4253MODULE_ALIAS("snd-hda-codec-atihdmi");
4254
4255static struct hda_codec_driver hdmi_driver = {
4256 .id = snd_hda_id_hdmi,
4257};
4258
4259module_hda_codec_driver(hdmi_driver);