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