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