1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 *
4 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
5 *
6 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
7 * Copyright (c) 2006 ATI Technologies Inc.
8 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
9 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
10 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
11 *
12 * Authors:
13 * Wu Fengguang <wfg@linux.intel.com>
14 *
15 * Maintained by:
16 * Wu Fengguang <wfg@linux.intel.com>
17 */
18
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/pci.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/pm_runtime.h>
25 #include <sound/core.h>
26 #include <sound/jack.h>
27 #include <sound/asoundef.h>
28 #include <sound/tlv.h>
29 #include <sound/hdaudio.h>
30 #include <sound/hda_i915.h>
31 #include <sound/hda_chmap.h>
32 #include <sound/hda_codec.h>
33 #include "hda_local.h"
34 #include "hda_jack.h"
35 #include "hda_controller.h"
36
37 static bool static_hdmi_pcm;
38 module_param(static_hdmi_pcm, bool, 0644);
39 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
40
41 #define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
42 #define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
43 #define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
44 #define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
45 #define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
46 #define is_geminilake(codec) (((codec)->core.vendor_id == 0x8086280d) || \
47 ((codec)->core.vendor_id == 0x80862800))
48 #define is_cannonlake(codec) ((codec)->core.vendor_id == 0x8086280c)
49 #define is_icelake(codec) ((codec)->core.vendor_id == 0x8086280f)
50 #define is_tigerlake(codec) ((codec)->core.vendor_id == 0x80862812)
51 #define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
52 || is_skylake(codec) || is_broxton(codec) \
53 || is_kabylake(codec) || is_geminilake(codec) \
54 || is_cannonlake(codec) || is_icelake(codec) \
55 || is_tigerlake(codec))
56 #define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
57 #define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
58 #define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
59
60 static bool enable_acomp = true;
61 module_param(enable_acomp, bool, 0444);
62 MODULE_PARM_DESC(enable_acomp, "Enable audio component binding (default=yes)");
63
64 struct 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
77 struct 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 */
105 struct 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
123 struct hdmi_pcm {
124 struct hda_pcm *pcm;
125 struct snd_jack *jack;
126 struct snd_kcontrol *eld_ctl;
127 };
128
129 struct hdmi_spec {
130 struct hda_codec *codec;
131 int num_cvts;
132 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
133 hda_nid_t cvt_nids[4]; /* only for haswell fix */
134
135 /*
136 * num_pins is the number of virtual pins
137 * for example, there are 3 pins, and each pin
138 * has 4 device entries, then the num_pins is 12
139 */
140 int num_pins;
141 /*
142 * num_nids is the number of real pins
143 * In the above example, num_nids is 3
144 */
145 int num_nids;
146 /*
147 * dev_num is the number of device entries
148 * on each pin.
149 * In the above example, dev_num is 4
150 */
151 int dev_num;
152 struct snd_array pins; /* struct hdmi_spec_per_pin */
153 struct hdmi_pcm pcm_rec[16];
154 struct mutex pcm_lock;
155 struct mutex bind_lock; /* for audio component binding */
156 /* pcm_bitmap means which pcms have been assigned to pins*/
157 unsigned long pcm_bitmap;
158 int pcm_used; /* counter of pcm_rec[] */
159 /* bitmap shows whether the pcm is opened in user space
160 * bit 0 means the first playback PCM (PCM3);
161 * bit 1 means the second playback PCM, and so on.
162 */
163 unsigned long pcm_in_use;
164
165 struct hdmi_eld temp_eld;
166 struct hdmi_ops ops;
167
168 bool dyn_pin_out;
169 bool dyn_pcm_assign;
170 /*
171 * Non-generic VIA/NVIDIA specific
172 */
173 struct hda_multi_out multiout;
174 struct hda_pcm_stream pcm_playback;
175
176 bool use_jack_detect; /* jack detection enabled */
177 bool use_acomp_notifier; /* use eld_notify callback for hotplug */
178 bool acomp_registered; /* audio component registered in this driver */
179 bool force_connect; /* force connectivity */
180 struct drm_audio_component_audio_ops drm_audio_ops;
181 int (*port2pin)(struct hda_codec *, int); /* reverse port/pin mapping */
182
183 struct hdac_chmap chmap;
184 hda_nid_t vendor_nid;
185 const int *port_map;
186 int port_num;
187 };
188
189 #ifdef CONFIG_SND_HDA_COMPONENT
codec_has_acomp(struct hda_codec * codec)190 static 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
199 struct 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
213 struct 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
225 union 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
pin_id_to_pin_index(struct hda_codec * codec,hda_nid_t pin_nid,int dev_id)244 static 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
hinfo_to_pcm_index(struct hda_codec * codec,struct hda_pcm_stream * hinfo)269 static 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
hinfo_to_pin_index(struct hda_codec * codec,struct hda_pcm_stream * hinfo)283 static 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
pcm_idx_to_pin(struct hdmi_spec * spec,int pcm_idx)301 static 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
cvt_nid_to_cvt_index(struct hda_codec * codec,hda_nid_t cvt_nid)315 static 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
hdmi_eld_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)328 static 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
hdmi_eld_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)355 static 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
394 static 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
hdmi_create_eld_ctl(struct hda_codec * codec,int pcm_idx,int device)402 static 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
hdmi_get_dip_index(struct hda_codec * codec,hda_nid_t pin_nid,int * packet_index,int * byte_index)427 static 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
hdmi_set_dip_index(struct hda_codec * codec,hda_nid_t pin_nid,int packet_index,int byte_index)440 static 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
hdmi_write_dip_byte(struct hda_codec * codec,hda_nid_t pin_nid,unsigned char val)450 static 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
hdmi_init_pin(struct hda_codec * codec,hda_nid_t pin_nid)456 static 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
print_eld_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)484 static 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
write_eld_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)494 static 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
eld_proc_new(struct hdmi_spec_per_pin * per_pin,int index)504 static 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
eld_proc_free(struct hdmi_spec_per_pin * per_pin)524 static 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
eld_proc_new(struct hdmi_spec_per_pin * per_pin,int index)532 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
533 int index)
534 {
535 return 0;
536 }
eld_proc_free(struct hdmi_spec_per_pin * per_pin)537 static 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 */
hdmi_start_infoframe_trans(struct hda_codec * codec,hda_nid_t pin_nid)549 static 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 */
hdmi_stop_infoframe_trans(struct hda_codec * codec,hda_nid_t pin_nid)560 static 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
hdmi_debug_dip_size(struct hda_codec * codec,hda_nid_t pin_nid)568 static 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
hdmi_clear_dip_buffers(struct hda_codec * codec,hda_nid_t pin_nid)585 static 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
hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe * hdmi_ai)614 static 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
hdmi_fill_audio_infoframe(struct hda_codec * codec,hda_nid_t pin_nid,u8 * dip,int size)628 static 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
hdmi_infoframe_uptodate(struct hda_codec * codec,hda_nid_t pin_nid,u8 * dip,int size)642 static 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
hdmi_pin_setup_infoframe(struct hda_codec * codec,hda_nid_t pin_nid,int ca,int active_channels,int conn_type)663 static 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
hdmi_setup_audio_infoframe(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin,bool non_pcm)712 static 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
762 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
763
check_presence_and_report(struct hda_codec * codec,hda_nid_t nid,int dev_id)764 static 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
jack_callback(struct hda_codec * codec,struct hda_jack_callback * jack)778 static void jack_callback(struct hda_codec *codec,
779 struct hda_jack_callback *jack)
780 {
781 /* stop polling when notification is enabled */
782 if (codec_has_acomp(codec))
783 return;
784
785 /* hda_jack don't support DP MST */
786 check_presence_and_report(codec, jack->nid, 0);
787 }
788
hdmi_intrinsic_event(struct hda_codec * codec,unsigned int res)789 static 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
hdmi_non_intrinsic_event(struct hda_codec * codec,unsigned int res)815 static 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
hdmi_unsol_event(struct hda_codec * codec,unsigned int res)838 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
839 {
840 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
841 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
842
843 if (codec_has_acomp(codec))
844 return;
845
846 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
847 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
848 return;
849 }
850
851 if (subtag == 0)
852 hdmi_intrinsic_event(codec, res);
853 else
854 hdmi_non_intrinsic_event(codec, res);
855 }
856
haswell_verify_D0(struct hda_codec * codec,hda_nid_t cvt_nid,hda_nid_t nid)857 static 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
hdmi_pin_hbr_setup(struct hda_codec * codec,hda_nid_t pin_nid,bool hbr)886 static 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
hdmi_setup_stream(struct hda_codec * codec,hda_nid_t cvt_nid,hda_nid_t pin_nid,u32 stream_tag,int format)920 static 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 */
hdmi_choose_cvt(struct hda_codec * codec,int pin_idx,int * cvt_id)963 static 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 */
intel_verify_pin_cvt_connect(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin)1010 static 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 */
intel_cvt_id_to_mux_idx(struct hdmi_spec * spec,hda_nid_t cvt_nid)1028 static 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 */
intel_not_share_assigned_cvt(struct hda_codec * codec,hda_nid_t pin_nid,int dev_id,int mux_idx)1048 static 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() */
intel_not_share_assigned_cvt_nid(struct hda_codec * codec,hda_nid_t pin_nid,int dev_id,hda_nid_t cvt_nid)1124 static 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 */
pin_cvt_fixup(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin,hda_nid_t cvt_nid)1141 static 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 */
hdmi_pcm_open_no_pin(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)1154 static 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 */
hdmi_pcm_open(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)1202 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1203 struct hda_codec *codec,
1204 struct snd_pcm_substream *substream)
1205 {
1206 struct hdmi_spec *spec = codec->spec;
1207 struct snd_pcm_runtime *runtime = substream->runtime;
1208 int pin_idx, cvt_idx, pcm_idx;
1209 struct hdmi_spec_per_pin *per_pin;
1210 struct hdmi_eld *eld;
1211 struct hdmi_spec_per_cvt *per_cvt = NULL;
1212 int err;
1213
1214 /* Validate hinfo */
1215 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1216 if (pcm_idx < 0)
1217 return -EINVAL;
1218
1219 mutex_lock(&spec->pcm_lock);
1220 pin_idx = hinfo_to_pin_index(codec, hinfo);
1221 if (!spec->dyn_pcm_assign) {
1222 if (snd_BUG_ON(pin_idx < 0)) {
1223 err = -EINVAL;
1224 goto unlock;
1225 }
1226 } else {
1227 /* no pin is assigned to the PCM
1228 * PA need pcm open successfully when probe
1229 */
1230 if (pin_idx < 0) {
1231 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1232 goto unlock;
1233 }
1234 }
1235
1236 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
1237 if (err < 0)
1238 goto unlock;
1239
1240 per_cvt = get_cvt(spec, cvt_idx);
1241 /* Claim converter */
1242 per_cvt->assigned = 1;
1243
1244 set_bit(pcm_idx, &spec->pcm_in_use);
1245 per_pin = get_pin(spec, pin_idx);
1246 per_pin->cvt_nid = per_cvt->cvt_nid;
1247 hinfo->nid = per_cvt->cvt_nid;
1248
1249 /* flip stripe flag for the assigned stream if supported */
1250 if (get_wcaps(codec, per_cvt->cvt_nid) & AC_WCAP_STRIPE)
1251 azx_stream(get_azx_dev(substream))->stripe = 1;
1252
1253 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1254 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1255 AC_VERB_SET_CONNECT_SEL,
1256 per_pin->mux_idx);
1257
1258 /* configure unused pins to choose other converters */
1259 pin_cvt_fixup(codec, per_pin, 0);
1260
1261 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1262
1263 /* Initially set the converter's capabilities */
1264 hinfo->channels_min = per_cvt->channels_min;
1265 hinfo->channels_max = per_cvt->channels_max;
1266 hinfo->rates = per_cvt->rates;
1267 hinfo->formats = per_cvt->formats;
1268 hinfo->maxbps = per_cvt->maxbps;
1269
1270 eld = &per_pin->sink_eld;
1271 /* Restrict capabilities by ELD if this isn't disabled */
1272 if (!static_hdmi_pcm && eld->eld_valid) {
1273 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1274 if (hinfo->channels_min > hinfo->channels_max ||
1275 !hinfo->rates || !hinfo->formats) {
1276 per_cvt->assigned = 0;
1277 hinfo->nid = 0;
1278 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1279 err = -ENODEV;
1280 goto unlock;
1281 }
1282 }
1283
1284 /* Store the updated parameters */
1285 runtime->hw.channels_min = hinfo->channels_min;
1286 runtime->hw.channels_max = hinfo->channels_max;
1287 runtime->hw.formats = hinfo->formats;
1288 runtime->hw.rates = hinfo->rates;
1289
1290 snd_pcm_hw_constraint_step(substream->runtime, 0,
1291 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1292 unlock:
1293 mutex_unlock(&spec->pcm_lock);
1294 return err;
1295 }
1296
1297 /*
1298 * HDA/HDMI auto parsing
1299 */
hdmi_read_pin_conn(struct hda_codec * codec,int pin_idx)1300 static 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
hdmi_find_pcm_slot(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1321 static 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
hdmi_attach_hda_pcm(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1344 static 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
hdmi_detach_hda_pcm(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1360 static 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
hdmi_get_pin_cvt_mux(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin,hda_nid_t cvt_nid)1375 static 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
1386 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1387
hdmi_pcm_setup_pin(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1388 static 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
hdmi_pcm_reset_pin(struct hdmi_spec * spec,struct hdmi_spec_per_pin * per_pin)1432 static 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 */
update_eld(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin,struct hdmi_eld * eld)1448 static bool update_eld(struct hda_codec *codec,
1449 struct hdmi_spec_per_pin *per_pin,
1450 struct hdmi_eld *eld)
1451 {
1452 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1453 struct hdmi_spec *spec = codec->spec;
1454 bool old_eld_valid = pin_eld->eld_valid;
1455 bool eld_changed;
1456 int pcm_idx;
1457
1458 /* for monitor disconnection, save pcm_idx firstly */
1459 pcm_idx = per_pin->pcm_idx;
1460 if (spec->dyn_pcm_assign) {
1461 if (eld->eld_valid) {
1462 hdmi_attach_hda_pcm(spec, per_pin);
1463 hdmi_pcm_setup_pin(spec, per_pin);
1464 } else {
1465 hdmi_pcm_reset_pin(spec, per_pin);
1466 hdmi_detach_hda_pcm(spec, per_pin);
1467 }
1468 }
1469 /* if pcm_idx == -1, it means this is in monitor connection event
1470 * we can get the correct pcm_idx now.
1471 */
1472 if (pcm_idx == -1)
1473 pcm_idx = per_pin->pcm_idx;
1474
1475 if (eld->eld_valid)
1476 snd_hdmi_show_eld(codec, &eld->info);
1477
1478 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1479 eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
1480 if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
1481 if (pin_eld->eld_size != eld->eld_size ||
1482 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1483 eld->eld_size) != 0)
1484 eld_changed = true;
1485
1486 if (eld_changed) {
1487 pin_eld->monitor_present = eld->monitor_present;
1488 pin_eld->eld_valid = eld->eld_valid;
1489 pin_eld->eld_size = eld->eld_size;
1490 if (eld->eld_valid)
1491 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1492 eld->eld_size);
1493 pin_eld->info = eld->info;
1494 }
1495
1496 /*
1497 * Re-setup pin and infoframe. This is needed e.g. when
1498 * - sink is first plugged-in
1499 * - transcoder can change during stream playback on Haswell
1500 * and this can make HW reset converter selection on a pin.
1501 */
1502 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1503 pin_cvt_fixup(codec, per_pin, 0);
1504 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1505 }
1506
1507 if (eld_changed && pcm_idx >= 0)
1508 snd_ctl_notify(codec->card,
1509 SNDRV_CTL_EVENT_MASK_VALUE |
1510 SNDRV_CTL_EVENT_MASK_INFO,
1511 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1512 return eld_changed;
1513 }
1514
1515 /* update ELD and jack state via HD-audio verbs */
hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin * per_pin,int repoll)1516 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1517 int repoll)
1518 {
1519 struct hda_jack_tbl *jack;
1520 struct hda_codec *codec = per_pin->codec;
1521 struct hdmi_spec *spec = codec->spec;
1522 struct hdmi_eld *eld = &spec->temp_eld;
1523 hda_nid_t pin_nid = per_pin->pin_nid;
1524 /*
1525 * Always execute a GetPinSense verb here, even when called from
1526 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1527 * response's PD bit is not the real PD value, but indicates that
1528 * the real PD value changed. An older version of the HD-audio
1529 * specification worked this way. Hence, we just ignore the data in
1530 * the unsolicited response to avoid custom WARs.
1531 */
1532 int present;
1533 bool ret;
1534 bool do_repoll = false;
1535
1536 present = snd_hda_pin_sense(codec, pin_nid);
1537
1538 mutex_lock(&per_pin->lock);
1539 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1540 if (eld->monitor_present)
1541 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1542 else
1543 eld->eld_valid = false;
1544
1545 codec_dbg(codec,
1546 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1547 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
1548
1549 if (eld->eld_valid) {
1550 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1551 &eld->eld_size) < 0)
1552 eld->eld_valid = false;
1553 else {
1554 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1555 eld->eld_size) < 0)
1556 eld->eld_valid = false;
1557 }
1558 if (!eld->eld_valid && repoll)
1559 do_repoll = true;
1560 }
1561
1562 if (do_repoll)
1563 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1564 else
1565 update_eld(codec, per_pin, eld);
1566
1567 ret = !repoll || !eld->monitor_present || eld->eld_valid;
1568
1569 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1570 if (jack) {
1571 jack->block_report = !ret;
1572 jack->pin_sense = (eld->monitor_present && eld->eld_valid) ?
1573 AC_PINSENSE_PRESENCE : 0;
1574 }
1575 mutex_unlock(&per_pin->lock);
1576 return ret;
1577 }
1578
pin_idx_to_jack(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin)1579 static 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 */
sync_eld_via_acomp(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin)1608 static void sync_eld_via_acomp(struct hda_codec *codec,
1609 struct hdmi_spec_per_pin *per_pin)
1610 {
1611 struct hdmi_spec *spec = codec->spec;
1612 struct hdmi_eld *eld = &spec->temp_eld;
1613 struct snd_jack *jack = NULL;
1614 bool changed;
1615 int size;
1616
1617 mutex_lock(&per_pin->lock);
1618 eld->monitor_present = false;
1619 size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1620 per_pin->dev_id, &eld->monitor_present,
1621 eld->eld_buffer, ELD_MAX_SIZE);
1622 if (size > 0) {
1623 size = min(size, ELD_MAX_SIZE);
1624 if (snd_hdmi_parse_eld(codec, &eld->info,
1625 eld->eld_buffer, size) < 0)
1626 size = -EINVAL;
1627 }
1628
1629 if (size > 0) {
1630 eld->eld_valid = true;
1631 eld->eld_size = size;
1632 } else {
1633 eld->eld_valid = false;
1634 eld->eld_size = 0;
1635 }
1636
1637 /* pcm_idx >=0 before update_eld() means it is in monitor
1638 * disconnected event. Jack must be fetched before update_eld()
1639 */
1640 jack = pin_idx_to_jack(codec, per_pin);
1641 changed = update_eld(codec, per_pin, eld);
1642 if (jack == NULL)
1643 jack = pin_idx_to_jack(codec, per_pin);
1644 if (changed && jack)
1645 snd_jack_report(jack,
1646 (eld->monitor_present && eld->eld_valid) ?
1647 SND_JACK_AVOUT : 0);
1648 mutex_unlock(&per_pin->lock);
1649 }
1650
hdmi_present_sense(struct hdmi_spec_per_pin * per_pin,int repoll)1651 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1652 {
1653 struct hda_codec *codec = per_pin->codec;
1654 int ret;
1655
1656 /* no temporary power up/down needed for component notifier */
1657 if (!codec_has_acomp(codec)) {
1658 ret = snd_hda_power_up_pm(codec);
1659 if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec))) {
1660 snd_hda_power_down_pm(codec);
1661 return false;
1662 }
1663 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
1664 snd_hda_power_down_pm(codec);
1665 } else {
1666 sync_eld_via_acomp(codec, per_pin);
1667 ret = false; /* don't call snd_hda_jack_report_sync() */
1668 }
1669
1670 return ret;
1671 }
1672
hdmi_repoll_eld(struct work_struct * work)1673 static void hdmi_repoll_eld(struct work_struct *work)
1674 {
1675 struct hdmi_spec_per_pin *per_pin =
1676 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1677 struct hda_codec *codec = per_pin->codec;
1678 struct hdmi_spec *spec = codec->spec;
1679 struct hda_jack_tbl *jack;
1680
1681 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1682 if (jack)
1683 jack->jack_dirty = 1;
1684
1685 if (per_pin->repoll_count++ > 6)
1686 per_pin->repoll_count = 0;
1687
1688 mutex_lock(&spec->pcm_lock);
1689 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1690 snd_hda_jack_report_sync(per_pin->codec);
1691 mutex_unlock(&spec->pcm_lock);
1692 }
1693
1694 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1695 hda_nid_t nid);
1696
hdmi_add_pin(struct hda_codec * codec,hda_nid_t pin_nid)1697 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1698 {
1699 struct hdmi_spec *spec = codec->spec;
1700 unsigned int caps, config;
1701 int pin_idx;
1702 struct hdmi_spec_per_pin *per_pin;
1703 int err;
1704 int dev_num, i;
1705
1706 caps = snd_hda_query_pin_caps(codec, pin_nid);
1707 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1708 return 0;
1709
1710 /*
1711 * For DP MST audio, Configuration Default is the same for
1712 * all device entries on the same pin
1713 */
1714 config = snd_hda_codec_get_pincfg(codec, pin_nid);
1715 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE &&
1716 !spec->force_connect)
1717 return 0;
1718
1719 /*
1720 * To simplify the implementation, malloc all
1721 * the virtual pins in the initialization statically
1722 */
1723 if (is_haswell_plus(codec)) {
1724 /*
1725 * On Intel platforms, device entries number is
1726 * changed dynamically. If there is a DP MST
1727 * hub connected, the device entries number is 3.
1728 * Otherwise, it is 1.
1729 * Here we manually set dev_num to 3, so that
1730 * we can initialize all the device entries when
1731 * bootup statically.
1732 */
1733 dev_num = 3;
1734 spec->dev_num = 3;
1735 } else if (spec->dyn_pcm_assign && codec->dp_mst) {
1736 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1737 /*
1738 * spec->dev_num is the maxinum number of device entries
1739 * among all the pins
1740 */
1741 spec->dev_num = (spec->dev_num > dev_num) ?
1742 spec->dev_num : dev_num;
1743 } else {
1744 /*
1745 * If the platform doesn't support DP MST,
1746 * manually set dev_num to 1. This means
1747 * the pin has only one device entry.
1748 */
1749 dev_num = 1;
1750 spec->dev_num = 1;
1751 }
1752
1753 for (i = 0; i < dev_num; i++) {
1754 pin_idx = spec->num_pins;
1755 per_pin = snd_array_new(&spec->pins);
1756
1757 if (!per_pin)
1758 return -ENOMEM;
1759
1760 if (spec->dyn_pcm_assign) {
1761 per_pin->pcm = NULL;
1762 per_pin->pcm_idx = -1;
1763 } else {
1764 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1765 per_pin->pcm_idx = pin_idx;
1766 }
1767 per_pin->pin_nid = pin_nid;
1768 per_pin->pin_nid_idx = spec->num_nids;
1769 per_pin->dev_id = i;
1770 per_pin->non_pcm = false;
1771 snd_hda_set_dev_select(codec, pin_nid, i);
1772 if (is_haswell_plus(codec))
1773 intel_haswell_fixup_connect_list(codec, pin_nid);
1774 err = hdmi_read_pin_conn(codec, pin_idx);
1775 if (err < 0)
1776 return err;
1777 spec->num_pins++;
1778 }
1779 spec->num_nids++;
1780
1781 return 0;
1782 }
1783
hdmi_add_cvt(struct hda_codec * codec,hda_nid_t cvt_nid)1784 static 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
1820 static 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(0x103c, 0x8711, "HP", 1),
1824 SND_PCI_QUIRK(0x103c, 0x8715, "HP", 1),
1825 SND_PCI_QUIRK(0x1043, 0x86ae, "ASUS", 1), /* Z170 PRO */
1826 SND_PCI_QUIRK(0x1043, 0x86c7, "ASUS", 1), /* Z170M PLUS */
1827 SND_PCI_QUIRK(0x1462, 0xec94, "MS-7C94", 1),
1828 SND_PCI_QUIRK(0x8086, 0x2060, "Intel NUC5CPYB", 1),
1829 SND_PCI_QUIRK(0x8086, 0x2081, "Intel NUC 10", 1),
1830 {}
1831 };
1832
hdmi_parse_codec(struct hda_codec * codec)1833 static int hdmi_parse_codec(struct hda_codec *codec)
1834 {
1835 struct hdmi_spec *spec = codec->spec;
1836 hda_nid_t start_nid;
1837 unsigned int caps;
1838 int i, nodes;
1839 const struct snd_pci_quirk *q;
1840
1841 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &start_nid);
1842 if (!start_nid || nodes < 0) {
1843 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1844 return -EINVAL;
1845 }
1846
1847 q = snd_pci_quirk_lookup(codec->bus->pci, force_connect_list);
1848
1849 if (q && q->value)
1850 spec->force_connect = true;
1851
1852 /*
1853 * hdmi_add_pin() assumes total amount of converters to
1854 * be known, so first discover all converters
1855 */
1856 for (i = 0; i < nodes; i++) {
1857 hda_nid_t nid = start_nid + i;
1858
1859 caps = get_wcaps(codec, nid);
1860
1861 if (!(caps & AC_WCAP_DIGITAL))
1862 continue;
1863
1864 if (get_wcaps_type(caps) == AC_WID_AUD_OUT)
1865 hdmi_add_cvt(codec, nid);
1866 }
1867
1868 /* discover audio pins */
1869 for (i = 0; i < nodes; i++) {
1870 hda_nid_t nid = start_nid + i;
1871
1872 caps = get_wcaps(codec, nid);
1873
1874 if (!(caps & AC_WCAP_DIGITAL))
1875 continue;
1876
1877 if (get_wcaps_type(caps) == AC_WID_PIN)
1878 hdmi_add_pin(codec, nid);
1879 }
1880
1881 return 0;
1882 }
1883
1884 /*
1885 */
check_non_pcm_per_cvt(struct hda_codec * codec,hda_nid_t cvt_nid)1886 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1887 {
1888 struct hda_spdif_out *spdif;
1889 bool non_pcm;
1890
1891 mutex_lock(&codec->spdif_mutex);
1892 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1893 /* Add sanity check to pass klockwork check.
1894 * This should never happen.
1895 */
1896 if (WARN_ON(spdif == NULL)) {
1897 mutex_unlock(&codec->spdif_mutex);
1898 return true;
1899 }
1900 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1901 mutex_unlock(&codec->spdif_mutex);
1902 return non_pcm;
1903 }
1904
1905 /*
1906 * HDMI callbacks
1907 */
1908
generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream * hinfo,struct hda_codec * codec,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)1909 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1910 struct hda_codec *codec,
1911 unsigned int stream_tag,
1912 unsigned int format,
1913 struct snd_pcm_substream *substream)
1914 {
1915 hda_nid_t cvt_nid = hinfo->nid;
1916 struct hdmi_spec *spec = codec->spec;
1917 int pin_idx;
1918 struct hdmi_spec_per_pin *per_pin;
1919 hda_nid_t pin_nid;
1920 struct snd_pcm_runtime *runtime = substream->runtime;
1921 bool non_pcm;
1922 int pinctl, stripe;
1923 int err = 0;
1924
1925 mutex_lock(&spec->pcm_lock);
1926 pin_idx = hinfo_to_pin_index(codec, hinfo);
1927 if (spec->dyn_pcm_assign && pin_idx < 0) {
1928 /* when dyn_pcm_assign and pcm is not bound to a pin
1929 * skip pin setup and return 0 to make audio playback
1930 * be ongoing
1931 */
1932 pin_cvt_fixup(codec, NULL, cvt_nid);
1933 snd_hda_codec_setup_stream(codec, cvt_nid,
1934 stream_tag, 0, format);
1935 goto unlock;
1936 }
1937
1938 if (snd_BUG_ON(pin_idx < 0)) {
1939 err = -EINVAL;
1940 goto unlock;
1941 }
1942 per_pin = get_pin(spec, pin_idx);
1943 pin_nid = per_pin->pin_nid;
1944
1945 /* Verify pin:cvt selections to avoid silent audio after S3.
1946 * After S3, the audio driver restores pin:cvt selections
1947 * but this can happen before gfx is ready and such selection
1948 * is overlooked by HW. Thus multiple pins can share a same
1949 * default convertor and mute control will affect each other,
1950 * which can cause a resumed audio playback become silent
1951 * after S3.
1952 */
1953 pin_cvt_fixup(codec, per_pin, 0);
1954
1955 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1956 /* Todo: add DP1.2 MST audio support later */
1957 if (codec_has_acomp(codec))
1958 snd_hdac_sync_audio_rate(&codec->core, pin_nid, per_pin->dev_id,
1959 runtime->rate);
1960
1961 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1962 mutex_lock(&per_pin->lock);
1963 per_pin->channels = substream->runtime->channels;
1964 per_pin->setup = true;
1965
1966 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1967 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1968 substream);
1969 snd_hda_codec_write(codec, cvt_nid, 0,
1970 AC_VERB_SET_STRIPE_CONTROL,
1971 stripe);
1972 }
1973
1974 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1975 mutex_unlock(&per_pin->lock);
1976 if (spec->dyn_pin_out) {
1977 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1978 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1979 snd_hda_codec_write(codec, pin_nid, 0,
1980 AC_VERB_SET_PIN_WIDGET_CONTROL,
1981 pinctl | PIN_OUT);
1982 }
1983
1984 /* snd_hda_set_dev_select() has been called before */
1985 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1986 stream_tag, format);
1987 unlock:
1988 mutex_unlock(&spec->pcm_lock);
1989 return err;
1990 }
1991
generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)1992 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1993 struct hda_codec *codec,
1994 struct snd_pcm_substream *substream)
1995 {
1996 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1997 return 0;
1998 }
1999
hdmi_pcm_close(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)2000 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2001 struct hda_codec *codec,
2002 struct snd_pcm_substream *substream)
2003 {
2004 struct hdmi_spec *spec = codec->spec;
2005 int cvt_idx, pin_idx, pcm_idx;
2006 struct hdmi_spec_per_cvt *per_cvt;
2007 struct hdmi_spec_per_pin *per_pin;
2008 int pinctl;
2009 int err = 0;
2010
2011 mutex_lock(&spec->pcm_lock);
2012 if (hinfo->nid) {
2013 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2014 if (snd_BUG_ON(pcm_idx < 0)) {
2015 err = -EINVAL;
2016 goto unlock;
2017 }
2018 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
2019 if (snd_BUG_ON(cvt_idx < 0)) {
2020 err = -EINVAL;
2021 goto unlock;
2022 }
2023 per_cvt = get_cvt(spec, cvt_idx);
2024 snd_BUG_ON(!per_cvt->assigned);
2025 per_cvt->assigned = 0;
2026 hinfo->nid = 0;
2027
2028 azx_stream(get_azx_dev(substream))->stripe = 0;
2029
2030 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2031 clear_bit(pcm_idx, &spec->pcm_in_use);
2032 pin_idx = hinfo_to_pin_index(codec, hinfo);
2033 if (spec->dyn_pcm_assign && pin_idx < 0)
2034 goto unlock;
2035
2036 if (snd_BUG_ON(pin_idx < 0)) {
2037 err = -EINVAL;
2038 goto unlock;
2039 }
2040 per_pin = get_pin(spec, pin_idx);
2041
2042 if (spec->dyn_pin_out) {
2043 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2044 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2045 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2046 AC_VERB_SET_PIN_WIDGET_CONTROL,
2047 pinctl & ~PIN_OUT);
2048 }
2049
2050 mutex_lock(&per_pin->lock);
2051 per_pin->chmap_set = false;
2052 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2053
2054 per_pin->setup = false;
2055 per_pin->channels = 0;
2056 mutex_unlock(&per_pin->lock);
2057 }
2058
2059 unlock:
2060 mutex_unlock(&spec->pcm_lock);
2061
2062 return err;
2063 }
2064
2065 static const struct hda_pcm_ops generic_ops = {
2066 .open = hdmi_pcm_open,
2067 .close = hdmi_pcm_close,
2068 .prepare = generic_hdmi_playback_pcm_prepare,
2069 .cleanup = generic_hdmi_playback_pcm_cleanup,
2070 };
2071
hdmi_get_spk_alloc(struct hdac_device * hdac,int pcm_idx)2072 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2073 {
2074 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2075 struct hdmi_spec *spec = codec->spec;
2076 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2077
2078 if (!per_pin)
2079 return 0;
2080
2081 return per_pin->sink_eld.info.spk_alloc;
2082 }
2083
hdmi_get_chmap(struct hdac_device * hdac,int pcm_idx,unsigned char * chmap)2084 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2085 unsigned char *chmap)
2086 {
2087 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2088 struct hdmi_spec *spec = codec->spec;
2089 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2090
2091 /* chmap is already set to 0 in caller */
2092 if (!per_pin)
2093 return;
2094
2095 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2096 }
2097
hdmi_set_chmap(struct hdac_device * hdac,int pcm_idx,unsigned char * chmap,int prepared)2098 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2099 unsigned char *chmap, int prepared)
2100 {
2101 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2102 struct hdmi_spec *spec = codec->spec;
2103 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2104
2105 if (!per_pin)
2106 return;
2107 mutex_lock(&per_pin->lock);
2108 per_pin->chmap_set = true;
2109 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2110 if (prepared)
2111 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2112 mutex_unlock(&per_pin->lock);
2113 }
2114
is_hdmi_pcm_attached(struct hdac_device * hdac,int pcm_idx)2115 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2116 {
2117 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2118 struct hdmi_spec *spec = codec->spec;
2119 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2120
2121 return per_pin ? true:false;
2122 }
2123
generic_hdmi_build_pcms(struct hda_codec * codec)2124 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2125 {
2126 struct hdmi_spec *spec = codec->spec;
2127 int idx;
2128
2129 /*
2130 * for non-mst mode, pcm number is the same as before
2131 * for DP MST mode, pcm number is (nid number + dev_num - 1)
2132 * dev_num is the device entry number in a pin
2133 *
2134 */
2135 for (idx = 0; idx < spec->num_nids + spec->dev_num - 1; idx++) {
2136 struct hda_pcm *info;
2137 struct hda_pcm_stream *pstr;
2138
2139 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
2140 if (!info)
2141 return -ENOMEM;
2142
2143 spec->pcm_rec[idx].pcm = info;
2144 spec->pcm_used++;
2145 info->pcm_type = HDA_PCM_TYPE_HDMI;
2146 info->own_chmap = true;
2147
2148 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2149 pstr->substreams = 1;
2150 pstr->ops = generic_ops;
2151 /* pcm number is less than 16 */
2152 if (spec->pcm_used >= 16)
2153 break;
2154 /* other pstr fields are set in open */
2155 }
2156
2157 return 0;
2158 }
2159
free_hdmi_jack_priv(struct snd_jack * jack)2160 static void free_hdmi_jack_priv(struct snd_jack *jack)
2161 {
2162 struct hdmi_pcm *pcm = jack->private_data;
2163
2164 pcm->jack = NULL;
2165 }
2166
add_hdmi_jack_kctl(struct hda_codec * codec,struct hdmi_spec * spec,int pcm_idx,const char * name)2167 static int add_hdmi_jack_kctl(struct hda_codec *codec,
2168 struct hdmi_spec *spec,
2169 int pcm_idx,
2170 const char *name)
2171 {
2172 struct snd_jack *jack;
2173 int err;
2174
2175 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2176 true, false);
2177 if (err < 0)
2178 return err;
2179
2180 spec->pcm_rec[pcm_idx].jack = jack;
2181 jack->private_data = &spec->pcm_rec[pcm_idx];
2182 jack->private_free = free_hdmi_jack_priv;
2183 return 0;
2184 }
2185
generic_hdmi_build_jack(struct hda_codec * codec,int pcm_idx)2186 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2187 {
2188 char hdmi_str[32] = "HDMI/DP";
2189 struct hdmi_spec *spec = codec->spec;
2190 struct hdmi_spec_per_pin *per_pin;
2191 struct hda_jack_tbl *jack;
2192 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2193 bool phantom_jack;
2194 int ret;
2195
2196 if (pcmdev > 0)
2197 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2198
2199 if (spec->dyn_pcm_assign)
2200 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2201
2202 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2203 /* if !dyn_pcm_assign, it must be non-MST mode.
2204 * This means pcms and pins are statically mapped.
2205 * And pcm_idx is pin_idx.
2206 */
2207 per_pin = get_pin(spec, pcm_idx);
2208 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2209 if (phantom_jack)
2210 strncat(hdmi_str, " Phantom",
2211 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2212 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2213 phantom_jack, 0, NULL);
2214 if (ret < 0)
2215 return ret;
2216 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2217 if (jack == NULL)
2218 return 0;
2219 /* assign jack->jack to pcm_rec[].jack to
2220 * align with dyn_pcm_assign mode
2221 */
2222 spec->pcm_rec[pcm_idx].jack = jack->jack;
2223 return 0;
2224 }
2225
generic_hdmi_build_controls(struct hda_codec * codec)2226 static int generic_hdmi_build_controls(struct hda_codec *codec)
2227 {
2228 struct hdmi_spec *spec = codec->spec;
2229 int dev, err;
2230 int pin_idx, pcm_idx;
2231
2232 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2233 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2234 /* no PCM: mark this for skipping permanently */
2235 set_bit(pcm_idx, &spec->pcm_bitmap);
2236 continue;
2237 }
2238
2239 err = generic_hdmi_build_jack(codec, pcm_idx);
2240 if (err < 0)
2241 return err;
2242
2243 /* create the spdif for each pcm
2244 * pin will be bound when monitor is connected
2245 */
2246 if (spec->dyn_pcm_assign)
2247 err = snd_hda_create_dig_out_ctls(codec,
2248 0, spec->cvt_nids[0],
2249 HDA_PCM_TYPE_HDMI);
2250 else {
2251 struct hdmi_spec_per_pin *per_pin =
2252 get_pin(spec, pcm_idx);
2253 err = snd_hda_create_dig_out_ctls(codec,
2254 per_pin->pin_nid,
2255 per_pin->mux_nids[0],
2256 HDA_PCM_TYPE_HDMI);
2257 }
2258 if (err < 0)
2259 return err;
2260 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2261
2262 dev = get_pcm_rec(spec, pcm_idx)->device;
2263 if (dev != SNDRV_PCM_INVALID_DEVICE) {
2264 /* add control for ELD Bytes */
2265 err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2266 if (err < 0)
2267 return err;
2268 }
2269 }
2270
2271 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2272 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2273 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
2274
2275 pin_eld->eld_valid = false;
2276 hdmi_present_sense(per_pin, 0);
2277 }
2278
2279 /* add channel maps */
2280 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2281 struct hda_pcm *pcm;
2282
2283 pcm = get_pcm_rec(spec, pcm_idx);
2284 if (!pcm || !pcm->pcm)
2285 break;
2286 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
2287 if (err < 0)
2288 return err;
2289 }
2290
2291 return 0;
2292 }
2293
generic_hdmi_init_per_pins(struct hda_codec * codec)2294 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2295 {
2296 struct hdmi_spec *spec = codec->spec;
2297 int pin_idx;
2298
2299 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2300 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2301
2302 per_pin->codec = codec;
2303 mutex_init(&per_pin->lock);
2304 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2305 eld_proc_new(per_pin, pin_idx);
2306 }
2307 return 0;
2308 }
2309
generic_hdmi_init(struct hda_codec * codec)2310 static int generic_hdmi_init(struct hda_codec *codec)
2311 {
2312 struct hdmi_spec *spec = codec->spec;
2313 int pin_idx;
2314
2315 mutex_lock(&spec->bind_lock);
2316 spec->use_jack_detect = !codec->jackpoll_interval;
2317 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2318 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2319 hda_nid_t pin_nid = per_pin->pin_nid;
2320 int dev_id = per_pin->dev_id;
2321
2322 snd_hda_set_dev_select(codec, pin_nid, dev_id);
2323 hdmi_init_pin(codec, pin_nid);
2324 if (codec_has_acomp(codec))
2325 continue;
2326 if (spec->use_jack_detect)
2327 snd_hda_jack_detect_enable(codec, pin_nid);
2328 else
2329 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2330 jack_callback);
2331 }
2332 mutex_unlock(&spec->bind_lock);
2333 return 0;
2334 }
2335
hdmi_array_init(struct hdmi_spec * spec,int nums)2336 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2337 {
2338 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2339 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2340 }
2341
hdmi_array_free(struct hdmi_spec * spec)2342 static void hdmi_array_free(struct hdmi_spec *spec)
2343 {
2344 snd_array_free(&spec->pins);
2345 snd_array_free(&spec->cvts);
2346 }
2347
generic_spec_free(struct hda_codec * codec)2348 static void generic_spec_free(struct hda_codec *codec)
2349 {
2350 struct hdmi_spec *spec = codec->spec;
2351
2352 if (spec) {
2353 hdmi_array_free(spec);
2354 kfree(spec);
2355 codec->spec = NULL;
2356 }
2357 codec->dp_mst = false;
2358 }
2359
generic_hdmi_free(struct hda_codec * codec)2360 static void generic_hdmi_free(struct hda_codec *codec)
2361 {
2362 struct hdmi_spec *spec = codec->spec;
2363 int pin_idx, pcm_idx;
2364
2365 if (spec->acomp_registered) {
2366 snd_hdac_acomp_exit(&codec->bus->core);
2367 } else if (codec_has_acomp(codec)) {
2368 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2369 codec->relaxed_resume = 0;
2370 }
2371
2372 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2373 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2374 cancel_delayed_work_sync(&per_pin->work);
2375 eld_proc_free(per_pin);
2376 }
2377
2378 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2379 if (spec->pcm_rec[pcm_idx].jack == NULL)
2380 continue;
2381 if (spec->dyn_pcm_assign)
2382 snd_device_free(codec->card,
2383 spec->pcm_rec[pcm_idx].jack);
2384 else
2385 spec->pcm_rec[pcm_idx].jack = NULL;
2386 }
2387
2388 generic_spec_free(codec);
2389 }
2390
2391 #ifdef CONFIG_PM
generic_hdmi_suspend(struct hda_codec * codec)2392 static int generic_hdmi_suspend(struct hda_codec *codec)
2393 {
2394 struct hdmi_spec *spec = codec->spec;
2395 int pin_idx;
2396
2397 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2398 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2399 cancel_delayed_work_sync(&per_pin->work);
2400 }
2401 return 0;
2402 }
2403
generic_hdmi_resume(struct hda_codec * codec)2404 static int generic_hdmi_resume(struct hda_codec *codec)
2405 {
2406 struct hdmi_spec *spec = codec->spec;
2407 int pin_idx;
2408
2409 codec->patch_ops.init(codec);
2410 snd_hda_regmap_sync(codec);
2411
2412 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2413 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2414 hdmi_present_sense(per_pin, 1);
2415 }
2416 return 0;
2417 }
2418 #endif
2419
2420 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2421 .init = generic_hdmi_init,
2422 .free = generic_hdmi_free,
2423 .build_pcms = generic_hdmi_build_pcms,
2424 .build_controls = generic_hdmi_build_controls,
2425 .unsol_event = hdmi_unsol_event,
2426 #ifdef CONFIG_PM
2427 .suspend = generic_hdmi_suspend,
2428 .resume = generic_hdmi_resume,
2429 #endif
2430 };
2431
2432 static const struct hdmi_ops generic_standard_hdmi_ops = {
2433 .pin_get_eld = snd_hdmi_get_eld,
2434 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2435 .pin_hbr_setup = hdmi_pin_hbr_setup,
2436 .setup_stream = hdmi_setup_stream,
2437 };
2438
2439 /* allocate codec->spec and assign/initialize generic parser ops */
alloc_generic_hdmi(struct hda_codec * codec)2440 static int alloc_generic_hdmi(struct hda_codec *codec)
2441 {
2442 struct hdmi_spec *spec;
2443
2444 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2445 if (!spec)
2446 return -ENOMEM;
2447
2448 spec->codec = codec;
2449 spec->ops = generic_standard_hdmi_ops;
2450 spec->dev_num = 1; /* initialize to 1 */
2451 mutex_init(&spec->pcm_lock);
2452 mutex_init(&spec->bind_lock);
2453 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2454
2455 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2456 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2457 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2458 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
2459
2460 codec->spec = spec;
2461 hdmi_array_init(spec, 4);
2462
2463 codec->patch_ops = generic_hdmi_patch_ops;
2464
2465 return 0;
2466 }
2467
2468 /* generic HDMI parser */
patch_generic_hdmi(struct hda_codec * codec)2469 static int patch_generic_hdmi(struct hda_codec *codec)
2470 {
2471 int err;
2472
2473 err = alloc_generic_hdmi(codec);
2474 if (err < 0)
2475 return err;
2476
2477 err = hdmi_parse_codec(codec);
2478 if (err < 0) {
2479 generic_spec_free(codec);
2480 return err;
2481 }
2482
2483 generic_hdmi_init_per_pins(codec);
2484 return 0;
2485 }
2486
2487 /*
2488 * generic audio component binding
2489 */
2490
2491 /* turn on / off the unsol event jack detection dynamically */
reprogram_jack_detect(struct hda_codec * codec,hda_nid_t nid,bool use_acomp)2492 static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
2493 bool use_acomp)
2494 {
2495 struct hda_jack_tbl *tbl;
2496
2497 tbl = snd_hda_jack_tbl_get(codec, nid);
2498 if (tbl) {
2499 /* clear unsol even if component notifier is used, or re-enable
2500 * if notifier is cleared
2501 */
2502 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2503 snd_hda_codec_write_cache(codec, nid, 0,
2504 AC_VERB_SET_UNSOLICITED_ENABLE, val);
2505 } else {
2506 /* if no jack entry was defined beforehand, create a new one
2507 * at need (i.e. only when notifier is cleared)
2508 */
2509 if (!use_acomp)
2510 snd_hda_jack_detect_enable(codec, nid);
2511 }
2512 }
2513
2514 /* set up / clear component notifier dynamically */
generic_acomp_notifier_set(struct drm_audio_component * acomp,bool use_acomp)2515 static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2516 bool use_acomp)
2517 {
2518 struct hdmi_spec *spec;
2519 int i;
2520
2521 spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
2522 mutex_lock(&spec->bind_lock);
2523 spec->use_acomp_notifier = use_acomp;
2524 spec->codec->relaxed_resume = use_acomp;
2525 spec->codec->bus->keep_power = 0;
2526 /* reprogram each jack detection logic depending on the notifier */
2527 if (spec->use_jack_detect) {
2528 for (i = 0; i < spec->num_pins; i++)
2529 reprogram_jack_detect(spec->codec,
2530 get_pin(spec, i)->pin_nid,
2531 use_acomp);
2532 }
2533 mutex_unlock(&spec->bind_lock);
2534 }
2535
2536 /* enable / disable the notifier via master bind / unbind */
generic_acomp_master_bind(struct device * dev,struct drm_audio_component * acomp)2537 static int generic_acomp_master_bind(struct device *dev,
2538 struct drm_audio_component *acomp)
2539 {
2540 generic_acomp_notifier_set(acomp, true);
2541 return 0;
2542 }
2543
generic_acomp_master_unbind(struct device * dev,struct drm_audio_component * acomp)2544 static void generic_acomp_master_unbind(struct device *dev,
2545 struct drm_audio_component *acomp)
2546 {
2547 generic_acomp_notifier_set(acomp, false);
2548 }
2549
2550 /* check whether both HD-audio and DRM PCI devices belong to the same bus */
match_bound_vga(struct device * dev,int subtype,void * data)2551 static int match_bound_vga(struct device *dev, int subtype, void *data)
2552 {
2553 struct hdac_bus *bus = data;
2554 struct pci_dev *pci, *master;
2555
2556 if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2557 return 0;
2558 master = to_pci_dev(bus->dev);
2559 pci = to_pci_dev(dev);
2560 return master->bus == pci->bus;
2561 }
2562
2563 /* audio component notifier for AMD/Nvidia HDMI codecs */
generic_acomp_pin_eld_notify(void * audio_ptr,int port,int dev_id)2564 static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2565 {
2566 struct hda_codec *codec = audio_ptr;
2567 struct hdmi_spec *spec = codec->spec;
2568 hda_nid_t pin_nid = spec->port2pin(codec, port);
2569
2570 if (!pin_nid)
2571 return;
2572 if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2573 return;
2574 /* skip notification during system suspend (but not in runtime PM);
2575 * the state will be updated at resume
2576 */
2577 if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
2578 return;
2579
2580 check_presence_and_report(codec, pin_nid, dev_id);
2581 }
2582
2583 /* set up the private drm_audio_ops from the template */
setup_drm_audio_ops(struct hda_codec * codec,const struct drm_audio_component_audio_ops * ops)2584 static void setup_drm_audio_ops(struct hda_codec *codec,
2585 const struct drm_audio_component_audio_ops *ops)
2586 {
2587 struct hdmi_spec *spec = codec->spec;
2588
2589 spec->drm_audio_ops.audio_ptr = codec;
2590 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2591 * will call pin_eld_notify with using audio_ptr pointer
2592 * We need make sure audio_ptr is really setup
2593 */
2594 wmb();
2595 spec->drm_audio_ops.pin2port = ops->pin2port;
2596 spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2597 spec->drm_audio_ops.master_bind = ops->master_bind;
2598 spec->drm_audio_ops.master_unbind = ops->master_unbind;
2599 }
2600
2601 /* initialize the generic HDMI audio component */
generic_acomp_init(struct hda_codec * codec,const struct drm_audio_component_audio_ops * ops,int (* port2pin)(struct hda_codec *,int))2602 static void generic_acomp_init(struct hda_codec *codec,
2603 const struct drm_audio_component_audio_ops *ops,
2604 int (*port2pin)(struct hda_codec *, int))
2605 {
2606 struct hdmi_spec *spec = codec->spec;
2607
2608 if (!enable_acomp) {
2609 codec_info(codec, "audio component disabled by module option\n");
2610 return;
2611 }
2612
2613 spec->port2pin = port2pin;
2614 setup_drm_audio_ops(codec, ops);
2615 if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
2616 match_bound_vga, 0)) {
2617 spec->acomp_registered = true;
2618 }
2619 }
2620
2621 /*
2622 * Intel codec parsers and helpers
2623 */
2624
intel_haswell_fixup_connect_list(struct hda_codec * codec,hda_nid_t nid)2625 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2626 hda_nid_t nid)
2627 {
2628 struct hdmi_spec *spec = codec->spec;
2629 hda_nid_t conns[4];
2630 int nconns;
2631
2632 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2633 if (nconns == spec->num_cvts &&
2634 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2635 return;
2636
2637 /* override pins connection list */
2638 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2639 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2640 }
2641
2642 #define INTEL_GET_VENDOR_VERB 0xf81
2643 #define INTEL_SET_VENDOR_VERB 0x781
2644 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2645 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2646
intel_haswell_enable_all_pins(struct hda_codec * codec,bool update_tree)2647 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2648 bool update_tree)
2649 {
2650 unsigned int vendor_param;
2651 struct hdmi_spec *spec = codec->spec;
2652
2653 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2654 INTEL_GET_VENDOR_VERB, 0);
2655 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2656 return;
2657
2658 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2659 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2660 INTEL_SET_VENDOR_VERB, vendor_param);
2661 if (vendor_param == -1)
2662 return;
2663
2664 if (update_tree)
2665 snd_hda_codec_update_widgets(codec);
2666 }
2667
intel_haswell_fixup_enable_dp12(struct hda_codec * codec)2668 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2669 {
2670 unsigned int vendor_param;
2671 struct hdmi_spec *spec = codec->spec;
2672
2673 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2674 INTEL_GET_VENDOR_VERB, 0);
2675 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2676 return;
2677
2678 /* enable DP1.2 mode */
2679 vendor_param |= INTEL_EN_DP12;
2680 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2681 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
2682 INTEL_SET_VENDOR_VERB, vendor_param);
2683 }
2684
2685 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2686 * Otherwise you may get severe h/w communication errors.
2687 */
haswell_set_power_state(struct hda_codec * codec,hda_nid_t fg,unsigned int power_state)2688 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2689 unsigned int power_state)
2690 {
2691 if (power_state == AC_PWRST_D0) {
2692 intel_haswell_enable_all_pins(codec, false);
2693 intel_haswell_fixup_enable_dp12(codec);
2694 }
2695
2696 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2697 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2698 }
2699
2700 /* There is a fixed mapping between audio pin node and display port.
2701 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2702 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2703 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2704 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2705 *
2706 * on VLV, ILK:
2707 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2708 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2709 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2710 */
intel_base_nid(struct hda_codec * codec)2711 static int intel_base_nid(struct hda_codec *codec)
2712 {
2713 switch (codec->core.vendor_id) {
2714 case 0x80860054: /* ILK */
2715 case 0x80862804: /* ILK */
2716 case 0x80862882: /* VLV */
2717 return 4;
2718 default:
2719 return 5;
2720 }
2721 }
2722
intel_pin2port(void * audio_ptr,int pin_nid)2723 static int intel_pin2port(void *audio_ptr, int pin_nid)
2724 {
2725 struct hda_codec *codec = audio_ptr;
2726 struct hdmi_spec *spec = codec->spec;
2727 int base_nid, i;
2728
2729 if (!spec->port_num) {
2730 base_nid = intel_base_nid(codec);
2731 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2732 return -1;
2733 return pin_nid - base_nid + 1; /* intel port is 1-based */
2734 }
2735
2736 /*
2737 * looking for the pin number in the mapping table and return
2738 * the index which indicate the port number
2739 */
2740 for (i = 0; i < spec->port_num; i++) {
2741 if (pin_nid == spec->port_map[i])
2742 return i + 1;
2743 }
2744
2745 /* return -1 if pin number exceeds our expectation */
2746 codec_info(codec, "Can't find the HDMI/DP port for pin %d\n", pin_nid);
2747 return -1;
2748 }
2749
intel_port2pin(struct hda_codec * codec,int port)2750 static int intel_port2pin(struct hda_codec *codec, int port)
2751 {
2752 struct hdmi_spec *spec = codec->spec;
2753
2754 if (!spec->port_num) {
2755 /* we assume only from port-B to port-D */
2756 if (port < 1 || port > 3)
2757 return 0;
2758 /* intel port is 1-based */
2759 return port + intel_base_nid(codec) - 1;
2760 }
2761
2762 if (port < 1 || port > spec->port_num)
2763 return 0;
2764 return spec->port_map[port - 1];
2765 }
2766
intel_pin_eld_notify(void * audio_ptr,int port,int pipe)2767 static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
2768 {
2769 struct hda_codec *codec = audio_ptr;
2770 int pin_nid;
2771 int dev_id = pipe;
2772
2773 pin_nid = intel_port2pin(codec, port);
2774 if (!pin_nid)
2775 return;
2776 /* skip notification during system suspend (but not in runtime PM);
2777 * the state will be updated at resume
2778 */
2779 if (codec->core.dev.power.power_state.event == PM_EVENT_SUSPEND)
2780 return;
2781
2782 snd_hdac_i915_set_bclk(&codec->bus->core);
2783 check_presence_and_report(codec, pin_nid, dev_id);
2784 }
2785
2786 static const struct drm_audio_component_audio_ops intel_audio_ops = {
2787 .pin2port = intel_pin2port,
2788 .pin_eld_notify = intel_pin_eld_notify,
2789 };
2790
2791 /* register i915 component pin_eld_notify callback */
register_i915_notifier(struct hda_codec * codec)2792 static void register_i915_notifier(struct hda_codec *codec)
2793 {
2794 struct hdmi_spec *spec = codec->spec;
2795
2796 spec->use_acomp_notifier = true;
2797 spec->port2pin = intel_port2pin;
2798 setup_drm_audio_ops(codec, &intel_audio_ops);
2799 snd_hdac_acomp_register_notifier(&codec->bus->core,
2800 &spec->drm_audio_ops);
2801 /* no need for forcible resume for jack check thanks to notifier */
2802 codec->relaxed_resume = 1;
2803 }
2804
2805 /* setup_stream ops override for HSW+ */
i915_hsw_setup_stream(struct hda_codec * codec,hda_nid_t cvt_nid,hda_nid_t pin_nid,u32 stream_tag,int format)2806 static 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+ */
i915_pin_cvt_fixup(struct hda_codec * codec,struct hdmi_spec_per_pin * per_pin,hda_nid_t cvt_nid)2814 static 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) {
2819 haswell_verify_D0(codec, per_pin->cvt_nid, per_pin->pin_nid);
2820 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 */
alloc_intel_hdmi(struct hda_codec * codec)2831 static int alloc_intel_hdmi(struct hda_codec *codec)
2832 {
2833 int err;
2834
2835 /* 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
2843 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;
2849 }
2850
2851 /* parse and post-process for Intel codecs */
parse_intel_hdmi(struct hda_codec * codec)2852 static int parse_intel_hdmi(struct hda_codec *codec)
2853 {
2854 int err, retries = 3;
2855
2856 do {
2857 err = hdmi_parse_codec(codec);
2858 } while (err < 0 && retries--);
2859
2860 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 */
intel_hsw_common_init(struct hda_codec * codec,hda_nid_t vendor_nid,const int * port_map,int port_num)2871 static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2872 const int *port_map, int port_num)
2873 {
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;
2884 spec->port_map = port_map;
2885 spec->port_num = port_num;
2886
2887 intel_haswell_enable_all_pins(codec, true);
2888 intel_haswell_fixup_enable_dp12(codec);
2889
2890 codec->display_power_control = 1;
2891
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
patch_i915_hsw_hdmi(struct hda_codec * codec)2902 static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2903 {
2904 return intel_hsw_common_init(codec, 0x08, NULL, 0);
2905 }
2906
patch_i915_glk_hdmi(struct hda_codec * codec)2907 static int patch_i915_glk_hdmi(struct hda_codec *codec)
2908 {
2909 return intel_hsw_common_init(codec, 0x0b, NULL, 0);
2910 }
2911
patch_i915_icl_hdmi(struct hda_codec * codec)2912 static 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
patch_i915_tgl_hdmi(struct hda_codec * codec)2923 static 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
2935 /* Intel Baytrail and Braswell; with eld notifier */
patch_i915_byt_hdmi(struct hda_codec * codec)2936 static 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 */
2949 codec->display_power_control = 1;
2950
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 */
patch_i915_cpt_hdmi(struct hda_codec * codec)2960 static 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
simple_playback_build_pcms(struct hda_codec * codec)2974 static 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 */
simple_hdmi_unsol_event(struct hda_codec * codec,unsigned int res)3001 static 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
simple_playback_build_controls(struct hda_codec * codec)3013 static 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
simple_playback_init(struct hda_codec * codec)3028 static 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
simple_playback_free(struct hda_codec * codec)3044 static 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
3064 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
3065 /*front, rear, clfe, rear_surr */
3066 0x6, 0x8, 0xa, 0xc,
3067 };
3068
3069 static 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
3077 static 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
nvhdmi_7x_init_2ch(struct hda_codec * codec)3105 static 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
nvhdmi_7x_init_8ch(struct hda_codec * codec)3111 static 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
3117 static const unsigned int channels_2_6_8[] = {
3118 2, 6, 8
3119 };
3120
3121 static const unsigned int channels_2_8[] = {
3122 2, 8
3123 };
3124
3125 static 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
3131 static 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
simple_playback_pcm_open(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)3137 static 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
simple_playback_pcm_close(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)3170 static 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
simple_playback_pcm_prepare(struct hda_pcm_stream * hinfo,struct hda_codec * codec,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)3178 static 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
3189 static 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
3200 static 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
patch_simple_hdmi(struct hda_codec * codec,hda_nid_t cvt_nid,hda_nid_t pin_nid)3208 static 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
3219 spec->codec = codec;
3220 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
nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec * codec,int channels)3243 static 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
nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)3276 static 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
nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream * hinfo,struct hda_codec * codec,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)3301 static 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
3402 static 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
patch_nvhdmi_2ch(struct hda_codec * codec)3417 static 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
nvhdmi_7x_8ch_build_pcms(struct hda_codec * codec)3434 static 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
nvhdmi_7x_8ch_build_controls(struct hda_codec * codec)3445 static 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
patch_nvhdmi_8ch_7x(struct hda_codec * codec)3476 static 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 */
nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap * chmap,struct hdac_cea_channel_speaker_allocation * cap,int channels)3501 static 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
nvhdmi_chmap_validate(struct hdac_chmap * chmap,int ca,int chs,unsigned char * map)3515 static 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
patch_nvhdmi(struct hda_codec * codec)3524 static 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
3540 codec->link_down_at_suspend = 1;
3541
3542 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 */
tegra_hdmi_set_format(struct hda_codec * codec,unsigned int format)3587 static 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
tegra_hdmi_pcm_prepare(struct hda_pcm_stream * hinfo,struct hda_codec * codec,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)3628 static 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
tegra_hdmi_pcm_cleanup(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)3647 static 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
hda_find_pcm_by_type(struct hda_codec * codec,int type)3657 static 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
tegra_hdmi_build_pcms(struct hda_codec * codec)3672 static 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
patch_tegra_hdmi(struct hda_codec * codec)3697 static int patch_tegra_hdmi(struct hda_codec *codec)
3698 {
3699 struct hdmi_spec *spec;
3700 int err;
3701
3702 err = patch_generic_hdmi(codec);
3703 if (err)
3704 return err;
3705
3706 codec->depop_delay = 10;
3707 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3708 spec = codec->spec;
3709 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3710 nvhdmi_chmap_cea_alloc_validate_get_type;
3711 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3712
3713 return 0;
3714 }
3715
3716 /*
3717 * ATI/AMD-specific implementations
3718 */
3719
3720 #define is_amdhdmi_rev3_or_later(codec) \
3721 ((codec)->core.vendor_id == 0x1002aa01 && \
3722 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3723 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3724
3725 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3726 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3727 #define ATI_VERB_SET_DOWNMIX_INFO 0x772
3728 #define ATI_VERB_SET_MULTICHANNEL_01 0x777
3729 #define ATI_VERB_SET_MULTICHANNEL_23 0x778
3730 #define ATI_VERB_SET_MULTICHANNEL_45 0x779
3731 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3732 #define ATI_VERB_SET_HBR_CONTROL 0x77c
3733 #define ATI_VERB_SET_MULTICHANNEL_1 0x785
3734 #define ATI_VERB_SET_MULTICHANNEL_3 0x786
3735 #define ATI_VERB_SET_MULTICHANNEL_5 0x787
3736 #define ATI_VERB_SET_MULTICHANNEL_7 0x788
3737 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3738 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3739 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3740 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3741 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3742 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3743 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3744 #define ATI_VERB_GET_HBR_CONTROL 0xf7c
3745 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3746 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3747 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3748 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3749 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3750
3751 /* AMD specific HDA cvt verbs */
3752 #define ATI_VERB_SET_RAMP_RATE 0x770
3753 #define ATI_VERB_GET_RAMP_RATE 0xf70
3754
3755 #define ATI_OUT_ENABLE 0x1
3756
3757 #define ATI_MULTICHANNEL_MODE_PAIRED 0
3758 #define ATI_MULTICHANNEL_MODE_SINGLE 1
3759
3760 #define ATI_HBR_CAPABLE 0x01
3761 #define ATI_HBR_ENABLE 0x10
3762
atihdmi_pin_get_eld(struct hda_codec * codec,hda_nid_t nid,unsigned char * buf,int * eld_size)3763 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3764 unsigned char *buf, int *eld_size)
3765 {
3766 /* call hda_eld.c ATI/AMD-specific function */
3767 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3768 is_amdhdmi_rev3_or_later(codec));
3769 }
3770
atihdmi_pin_setup_infoframe(struct hda_codec * codec,hda_nid_t pin_nid,int ca,int active_channels,int conn_type)3771 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3772 int active_channels, int conn_type)
3773 {
3774 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3775 }
3776
atihdmi_paired_swap_fc_lfe(int pos)3777 static int atihdmi_paired_swap_fc_lfe(int pos)
3778 {
3779 /*
3780 * ATI/AMD have automatic FC/LFE swap built-in
3781 * when in pairwise mapping mode.
3782 */
3783
3784 switch (pos) {
3785 /* see channel_allocations[].speakers[] */
3786 case 2: return 3;
3787 case 3: return 2;
3788 default: break;
3789 }
3790
3791 return pos;
3792 }
3793
atihdmi_paired_chmap_validate(struct hdac_chmap * chmap,int ca,int chs,unsigned char * map)3794 static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3795 int ca, int chs, unsigned char *map)
3796 {
3797 struct hdac_cea_channel_speaker_allocation *cap;
3798 int i, j;
3799
3800 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3801
3802 cap = snd_hdac_get_ch_alloc_from_ca(ca);
3803 for (i = 0; i < chs; ++i) {
3804 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
3805 bool ok = false;
3806 bool companion_ok = false;
3807
3808 if (!mask)
3809 continue;
3810
3811 for (j = 0 + i % 2; j < 8; j += 2) {
3812 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3813 if (cap->speakers[chan_idx] == mask) {
3814 /* channel is in a supported position */
3815 ok = true;
3816
3817 if (i % 2 == 0 && i + 1 < chs) {
3818 /* even channel, check the odd companion */
3819 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3820 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
3821 int comp_mask_act = cap->speakers[comp_chan_idx];
3822
3823 if (comp_mask_req == comp_mask_act)
3824 companion_ok = true;
3825 else
3826 return -EINVAL;
3827 }
3828 break;
3829 }
3830 }
3831
3832 if (!ok)
3833 return -EINVAL;
3834
3835 if (companion_ok)
3836 i++; /* companion channel already checked */
3837 }
3838
3839 return 0;
3840 }
3841
atihdmi_pin_set_slot_channel(struct hdac_device * hdac,hda_nid_t pin_nid,int hdmi_slot,int stream_channel)3842 static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3843 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
3844 {
3845 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3846 int verb;
3847 int ati_channel_setup = 0;
3848
3849 if (hdmi_slot > 7)
3850 return -EINVAL;
3851
3852 if (!has_amd_full_remap_support(codec)) {
3853 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3854
3855 /* In case this is an odd slot but without stream channel, do not
3856 * disable the slot since the corresponding even slot could have a
3857 * channel. In case neither have a channel, the slot pair will be
3858 * disabled when this function is called for the even slot. */
3859 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3860 return 0;
3861
3862 hdmi_slot -= hdmi_slot % 2;
3863
3864 if (stream_channel != 0xf)
3865 stream_channel -= stream_channel % 2;
3866 }
3867
3868 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3869
3870 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3871
3872 if (stream_channel != 0xf)
3873 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3874
3875 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3876 }
3877
atihdmi_pin_get_slot_channel(struct hdac_device * hdac,hda_nid_t pin_nid,int asp_slot)3878 static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3879 hda_nid_t pin_nid, int asp_slot)
3880 {
3881 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3882 bool was_odd = false;
3883 int ati_asp_slot = asp_slot;
3884 int verb;
3885 int ati_channel_setup;
3886
3887 if (asp_slot > 7)
3888 return -EINVAL;
3889
3890 if (!has_amd_full_remap_support(codec)) {
3891 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3892 if (ati_asp_slot % 2 != 0) {
3893 ati_asp_slot -= 1;
3894 was_odd = true;
3895 }
3896 }
3897
3898 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3899
3900 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3901
3902 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3903 return 0xf;
3904
3905 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3906 }
3907
atihdmi_paired_chmap_cea_alloc_validate_get_type(struct hdac_chmap * chmap,struct hdac_cea_channel_speaker_allocation * cap,int channels)3908 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3909 struct hdac_chmap *chmap,
3910 struct hdac_cea_channel_speaker_allocation *cap,
3911 int channels)
3912 {
3913 int c;
3914
3915 /*
3916 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3917 * we need to take that into account (a single channel may take 2
3918 * channel slots if we need to carry a silent channel next to it).
3919 * On Rev3+ AMD codecs this function is not used.
3920 */
3921 int chanpairs = 0;
3922
3923 /* We only produce even-numbered channel count TLVs */
3924 if ((channels % 2) != 0)
3925 return -1;
3926
3927 for (c = 0; c < 7; c += 2) {
3928 if (cap->speakers[c] || cap->speakers[c+1])
3929 chanpairs++;
3930 }
3931
3932 if (chanpairs * 2 != channels)
3933 return -1;
3934
3935 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3936 }
3937
atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap * hchmap,struct hdac_cea_channel_speaker_allocation * cap,unsigned int * chmap,int channels)3938 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
3939 struct hdac_cea_channel_speaker_allocation *cap,
3940 unsigned int *chmap, int channels)
3941 {
3942 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3943 int count = 0;
3944 int c;
3945
3946 for (c = 7; c >= 0; c--) {
3947 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3948 int spk = cap->speakers[chan];
3949 if (!spk) {
3950 /* add N/A channel if the companion channel is occupied */
3951 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3952 chmap[count++] = SNDRV_CHMAP_NA;
3953
3954 continue;
3955 }
3956
3957 chmap[count++] = snd_hdac_spk_to_chmap(spk);
3958 }
3959
3960 WARN_ON(count != channels);
3961 }
3962
atihdmi_pin_hbr_setup(struct hda_codec * codec,hda_nid_t pin_nid,bool hbr)3963 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3964 bool hbr)
3965 {
3966 int hbr_ctl, hbr_ctl_new;
3967
3968 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3969 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3970 if (hbr)
3971 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3972 else
3973 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3974
3975 codec_dbg(codec,
3976 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3977 pin_nid,
3978 hbr_ctl == hbr_ctl_new ? "" : "new-",
3979 hbr_ctl_new);
3980
3981 if (hbr_ctl != hbr_ctl_new)
3982 snd_hda_codec_write(codec, pin_nid, 0,
3983 ATI_VERB_SET_HBR_CONTROL,
3984 hbr_ctl_new);
3985
3986 } else if (hbr)
3987 return -EINVAL;
3988
3989 return 0;
3990 }
3991
atihdmi_setup_stream(struct hda_codec * codec,hda_nid_t cvt_nid,hda_nid_t pin_nid,u32 stream_tag,int format)3992 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3993 hda_nid_t pin_nid, u32 stream_tag, int format)
3994 {
3995
3996 if (is_amdhdmi_rev3_or_later(codec)) {
3997 int ramp_rate = 180; /* default as per AMD spec */
3998 /* disable ramp-up/down for non-pcm as per AMD spec */
3999 if (format & AC_FMT_TYPE_NON_PCM)
4000 ramp_rate = 0;
4001
4002 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
4003 }
4004
4005 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
4006 }
4007
4008
atihdmi_init(struct hda_codec * codec)4009 static int atihdmi_init(struct hda_codec *codec)
4010 {
4011 struct hdmi_spec *spec = codec->spec;
4012 int pin_idx, err;
4013
4014 err = generic_hdmi_init(codec);
4015
4016 if (err)
4017 return err;
4018
4019 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
4020 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
4021
4022 /* make sure downmix information in infoframe is zero */
4023 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
4024
4025 /* enable channel-wise remap mode if supported */
4026 if (has_amd_full_remap_support(codec))
4027 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
4028 ATI_VERB_SET_MULTICHANNEL_MODE,
4029 ATI_MULTICHANNEL_MODE_SINGLE);
4030 }
4031
4032 return 0;
4033 }
4034
4035 /* map from pin NID to port; port is 0-based */
4036 /* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */
atihdmi_pin2port(void * audio_ptr,int pin_nid)4037 static int atihdmi_pin2port(void *audio_ptr, int pin_nid)
4038 {
4039 return pin_nid / 2 - 1;
4040 }
4041
4042 /* reverse-map from port to pin NID: see above */
atihdmi_port2pin(struct hda_codec * codec,int port)4043 static int atihdmi_port2pin(struct hda_codec *codec, int port)
4044 {
4045 return port * 2 + 3;
4046 }
4047
4048 static const struct drm_audio_component_audio_ops atihdmi_audio_ops = {
4049 .pin2port = atihdmi_pin2port,
4050 .pin_eld_notify = generic_acomp_pin_eld_notify,
4051 .master_bind = generic_acomp_master_bind,
4052 .master_unbind = generic_acomp_master_unbind,
4053 };
4054
patch_atihdmi(struct hda_codec * codec)4055 static int patch_atihdmi(struct hda_codec *codec)
4056 {
4057 struct hdmi_spec *spec;
4058 struct hdmi_spec_per_cvt *per_cvt;
4059 int err, cvt_idx;
4060
4061 err = patch_generic_hdmi(codec);
4062
4063 if (err)
4064 return err;
4065
4066 codec->patch_ops.init = atihdmi_init;
4067
4068 spec = codec->spec;
4069
4070 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
4071 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
4072 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
4073 spec->ops.setup_stream = atihdmi_setup_stream;
4074
4075 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4076 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4077
4078 if (!has_amd_full_remap_support(codec)) {
4079 /* override to ATI/AMD-specific versions with pairwise mapping */
4080 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
4081 atihdmi_paired_chmap_cea_alloc_validate_get_type;
4082 spec->chmap.ops.cea_alloc_to_tlv_chmap =
4083 atihdmi_paired_cea_alloc_to_tlv_chmap;
4084 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
4085 }
4086
4087 /* ATI/AMD converters do not advertise all of their capabilities */
4088 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4089 per_cvt = get_cvt(spec, cvt_idx);
4090 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4091 per_cvt->rates |= SUPPORTED_RATES;
4092 per_cvt->formats |= SUPPORTED_FORMATS;
4093 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4094 }
4095
4096 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
4097
4098 /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
4099 * the link-down as is. Tell the core to allow it.
4100 */
4101 codec->link_down_at_suspend = 1;
4102
4103 generic_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin);
4104
4105 return 0;
4106 }
4107
4108 /* VIA HDMI Implementation */
4109 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4110 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4111
patch_via_hdmi(struct hda_codec * codec)4112 static int patch_via_hdmi(struct hda_codec *codec)
4113 {
4114 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
4115 }
4116
4117 /*
4118 * patch entries
4119 */
4120 static const struct hda_device_id snd_hda_id_hdmi[] = {
4121 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4122 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4123 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4124 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4125 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4126 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4127 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4128 HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI", patch_nvhdmi_2ch),
4129 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4130 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4131 HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI", patch_nvhdmi_8ch_7x),
4132 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4133 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4134 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4135 HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP", patch_nvhdmi),
4136 HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP", patch_nvhdmi),
4137 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4138 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4139 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4140 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4141 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4142 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4143 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4144 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4145 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4146 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4147 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
4148 /* 17 is known to be absent */
4149 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4150 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4151 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4152 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4153 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4154 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4155 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4156 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4157 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4158 HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi),
4159 HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi),
4160 HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi),
4161 HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi),
4162 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4163 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4164 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4165 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4166 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4167 HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP", patch_nvhdmi),
4168 HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP", patch_nvhdmi),
4169 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4170 HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP", patch_nvhdmi),
4171 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4172 HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP", patch_nvhdmi),
4173 HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP", patch_nvhdmi),
4174 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4175 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4176 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4177 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4178 HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP", patch_nvhdmi),
4179 HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP", patch_nvhdmi),
4180 HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP", patch_nvhdmi),
4181 HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP", patch_nvhdmi),
4182 HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP", patch_nvhdmi),
4183 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
4184 HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP", patch_nvhdmi),
4185 HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi),
4186 HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP", patch_nvhdmi),
4187 HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
4188 HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
4189 HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP", patch_nvhdmi),
4190 HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP", patch_nvhdmi),
4191 HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP", patch_nvhdmi),
4192 HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP", patch_nvhdmi),
4193 HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP", patch_nvhdmi),
4194 HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP", patch_nvhdmi),
4195 HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP", patch_nvhdmi),
4196 HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP", patch_nvhdmi),
4197 HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP", patch_nvhdmi),
4198 HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP", patch_nvhdmi),
4199 HDA_CODEC_ENTRY(0x10de009a, "GPU 9a HDMI/DP", patch_nvhdmi),
4200 HDA_CODEC_ENTRY(0x10de009d, "GPU 9d HDMI/DP", patch_nvhdmi),
4201 HDA_CODEC_ENTRY(0x10de009e, "GPU 9e HDMI/DP", patch_nvhdmi),
4202 HDA_CODEC_ENTRY(0x10de009f, "GPU 9f HDMI/DP", patch_nvhdmi),
4203 HDA_CODEC_ENTRY(0x10de00a0, "GPU a0 HDMI/DP", patch_nvhdmi),
4204 HDA_CODEC_ENTRY(0x10de00a3, "GPU a3 HDMI/DP", patch_nvhdmi),
4205 HDA_CODEC_ENTRY(0x10de00a4, "GPU a4 HDMI/DP", patch_nvhdmi),
4206 HDA_CODEC_ENTRY(0x10de00a5, "GPU a5 HDMI/DP", patch_nvhdmi),
4207 HDA_CODEC_ENTRY(0x10de00a6, "GPU a6 HDMI/DP", patch_nvhdmi),
4208 HDA_CODEC_ENTRY(0x10de00a7, "GPU a7 HDMI/DP", patch_nvhdmi),
4209 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4210 HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI", patch_nvhdmi_2ch),
4211 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4212 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4213 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4214 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4215 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi),
4216 HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI", patch_i915_glk_hdmi),
4217 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4218 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4219 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4220 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi),
4221 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi),
4222 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
4223 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
4224 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
4225 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
4226 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
4227 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
4228 HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI", patch_i915_glk_hdmi),
4229 HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi),
4230 HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI", patch_i915_icl_hdmi),
4231 HDA_CODEC_ENTRY(0x80862812, "Tigerlake HDMI", patch_i915_tgl_hdmi),
4232 HDA_CODEC_ENTRY(0x80862814, "DG1 HDMI", patch_i915_tgl_hdmi),
4233 HDA_CODEC_ENTRY(0x80862815, "Alderlake HDMI", patch_i915_tgl_hdmi),
4234 HDA_CODEC_ENTRY(0x80862816, "Rocketlake HDMI", patch_i915_tgl_hdmi),
4235 HDA_CODEC_ENTRY(0x80862819, "DG2 HDMI", patch_i915_tgl_hdmi),
4236 HDA_CODEC_ENTRY(0x8086281a, "Jasperlake HDMI", patch_i915_icl_hdmi),
4237 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4238 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
4239 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
4240 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
4241 /* special ID for generic HDMI */
4242 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4243 {} /* terminator */
4244 };
4245 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4246
4247 MODULE_LICENSE("GPL");
4248 MODULE_DESCRIPTION("HDMI HD-audio codec");
4249 MODULE_ALIAS("snd-hda-codec-intelhdmi");
4250 MODULE_ALIAS("snd-hda-codec-nvhdmi");
4251 MODULE_ALIAS("snd-hda-codec-atihdmi");
4252
4253 static struct hda_codec_driver hdmi_driver = {
4254 .id = snd_hda_id_hdmi,
4255 };
4256
4257 module_hda_codec_driver(hdmi_driver);
4258