source: GPL/trunk/alsa-kernel/pci/hda/patch_hdmi.c

Last change on this file was 777, checked in by David Azarewicz, 7 months ago

Merge from uniaud32-exp branch

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