blob: 295cc5a989d5c02a9ebc78a24d0566980e765e2c [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Wu Fengguang079d88c2010-03-08 10:44:23 +08002/*
3 *
4 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
5 *
6 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
Takashi Iwai84eb01b2010-09-07 12:27:25 +02007 * Copyright (c) 2006 ATI Technologies Inc.
8 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
9 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
Anssi Hannula5a6135842013-10-24 21:10:35 +030010 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
Wu Fengguang079d88c2010-03-08 10:44:23 +080011 *
12 * Authors:
13 * Wu Fengguang <wfg@linux.intel.com>
14 *
15 * Maintained by:
16 * Wu Fengguang <wfg@linux.intel.com>
Wu Fengguang079d88c2010-03-08 10:44:23 +080017 */
18
Takashi Iwai84eb01b2010-09-07 12:27:25 +020019#include <linux/init.h>
20#include <linux/delay.h>
Takashi Iwaiade49db2018-07-11 18:05:52 +020021#include <linux/pci.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020022#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040023#include <linux/module.h>
Chris Wilsonaaa23f82018-06-27 07:25:32 +010024#include <linux/pm_runtime.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020025#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020026#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080027#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020028#include <sound/tlv.h>
David Henningsson25adc132015-08-19 10:48:58 +020029#include <sound/hdaudio.h>
30#include <sound/hda_i915.h>
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +053031#include <sound/hda_chmap.h>
Pierre-Louis Bossartbe57bff2018-08-22 15:24:57 -050032#include <sound/hda_codec.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020033#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020034#include "hda_jack.h"
Takashi Iwaie38e4862019-12-02 08:49:47 +010035#include "hda_controller.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020036
Takashi Iwai0ebaa242011-01-11 18:11:04 +010037static bool static_hdmi_pcm;
38module_param(static_hdmi_pcm, bool, 0644);
39MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
40
Takashi Iwaib3923502020-04-15 18:25:23 +020041static bool enable_acomp = true;
42module_param(enable_acomp, bool, 0444);
43MODULE_PARM_DESC(enable_acomp, "Enable audio component binding (default=yes)");
44
Stephen Warren384a48d2011-06-01 11:14:21 -060045struct hdmi_spec_per_cvt {
46 hda_nid_t cvt_nid;
47 int assigned;
48 unsigned int channels_min;
49 unsigned int channels_max;
50 u32 rates;
51 u64 formats;
52 unsigned int maxbps;
53};
54
Takashi Iwai4eea3092013-02-07 18:18:19 +010055/* max. connections to a widget */
56#define HDA_MAX_CONNECTIONS 32
57
Stephen Warren384a48d2011-06-01 11:14:21 -060058struct hdmi_spec_per_pin {
59 hda_nid_t pin_nid;
Libin Yang91520852017-01-12 16:04:53 +080060 int dev_id;
Libin Yanga76056f2015-12-16 16:48:15 +080061 /* pin idx, different device entries on the same pin use the same idx */
62 int pin_nid_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -060063 int num_mux_nids;
64 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Mengdong Lin2df67422014-03-20 13:01:06 +080065 int mux_idx;
Anssi Hannula1df5a062013-10-05 02:25:40 +030066 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080067
68 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060069 struct hdmi_eld sink_eld;
Takashi Iwaia4e9a382013-10-17 18:21:12 +020070 struct mutex lock;
Wu Fengguang744626d2011-11-16 16:29:47 +080071 struct delayed_work work;
Libin Yang2bea2412016-01-12 11:13:26 +080072 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Libin Yanga76056f2015-12-16 16:48:15 +080073 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
Wu Fengguangc6e84532011-11-18 16:59:32 -060074 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020075 bool setup; /* the stream has been set up by prepare callback */
76 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020077 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020078 bool chmap_set; /* channel-map override by ALSA API? */
79 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080080#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +020081 struct snd_info_entry *proc_entry;
82#endif
Stephen Warren384a48d2011-06-01 11:14:21 -060083};
84
Anssi Hannula307229d2013-10-24 21:10:34 +030085/* operations used by generic code that can be overridden by patches */
86struct hdmi_ops {
87 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +053088 int dev_id, unsigned char *buf, int *eld_size);
Anssi Hannula307229d2013-10-24 21:10:34 +030089
Anssi Hannula307229d2013-10-24 21:10:34 +030090 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +053091 int dev_id,
Anssi Hannula307229d2013-10-24 21:10:34 +030092 int ca, int active_channels, int conn_type);
93
94 /* enable/disable HBR (HD passthrough) */
Nikhil Mahale9c32fea2019-11-19 14:17:09 +053095 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid,
96 int dev_id, bool hbr);
Anssi Hannula307229d2013-10-24 21:10:34 +030097
98 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +053099 hda_nid_t pin_nid, int dev_id, u32 stream_tag,
100 int format);
Anssi Hannula307229d2013-10-24 21:10:34 +0300101
Takashi Iwai4846a672016-03-21 12:56:46 +0100102 void (*pin_cvt_fixup)(struct hda_codec *codec,
103 struct hdmi_spec_per_pin *per_pin,
104 hda_nid_t cvt_nid);
Anssi Hannula307229d2013-10-24 21:10:34 +0300105};
106
Libin Yang2bea2412016-01-12 11:13:26 +0800107struct hdmi_pcm {
108 struct hda_pcm *pcm;
109 struct snd_jack *jack;
Libin Yangfb087ea2016-02-23 16:33:37 +0800110 struct snd_kcontrol *eld_ctl;
Libin Yang2bea2412016-01-12 11:13:26 +0800111};
112
Wu Fengguang079d88c2010-03-08 10:44:23 +0800113struct hdmi_spec {
Takashi Iwaiade49db2018-07-11 18:05:52 +0200114 struct hda_codec *codec;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800115 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100116 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
117 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600118
Libin Yang91520852017-01-12 16:04:53 +0800119 /*
120 * num_pins is the number of virtual pins
121 * for example, there are 3 pins, and each pin
122 * has 4 device entries, then the num_pins is 12
123 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800124 int num_pins;
Libin Yang91520852017-01-12 16:04:53 +0800125 /*
126 * num_nids is the number of real pins
127 * In the above example, num_nids is 3
128 */
129 int num_nids;
130 /*
131 * dev_num is the number of device entries
132 * on each pin.
133 * In the above example, dev_num is 4
134 */
135 int dev_num;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100136 struct snd_array pins; /* struct hdmi_spec_per_pin */
Libin Yang2bea2412016-01-12 11:13:26 +0800137 struct hdmi_pcm pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800138 struct mutex pcm_lock;
Takashi Iwai302d5a82019-10-29 21:41:20 +0100139 struct mutex bind_lock; /* for audio component binding */
Libin Yanga76056f2015-12-16 16:48:15 +0800140 /* pcm_bitmap means which pcms have been assigned to pins*/
141 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800142 int pcm_used; /* counter of pcm_rec[] */
Libin Yangac983792015-12-16 16:48:16 +0800143 /* bitmap shows whether the pcm is opened in user space
144 * bit 0 means the first playback PCM (PCM3);
145 * bit 1 means the second playback PCM, and so on.
146 */
147 unsigned long pcm_in_use;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800148
David Henningsson4bd038f2013-02-19 16:11:25 +0100149 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300150 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700151
152 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800153 bool dyn_pcm_assign;
Takashi Iwaicb457222019-11-11 20:09:37 +0100154 bool intel_hsw_fixup; /* apply Intel platform-specific fixups */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800155 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300156 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800157 */
158 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200159 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200160
Takashi Iwaiade49db2018-07-11 18:05:52 +0200161 bool use_acomp_notifier; /* use eld_notify callback for hotplug */
162 bool acomp_registered; /* audio component registered in this driver */
Takashi Iwaiae891ab2018-07-11 15:17:22 +0200163 struct drm_audio_component_audio_ops drm_audio_ops;
Takashi Iwaiade49db2018-07-11 18:05:52 +0200164 int (*port2pin)(struct hda_codec *, int); /* reverse port/pin mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +0530165
166 struct hdac_chmap chmap;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +0530167 hda_nid_t vendor_nid;
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +0100168 const int *port_map;
169 int port_num;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800170};
171
Takashi Iwaia57942b2018-07-11 16:23:16 +0200172#ifdef CONFIG_SND_HDA_COMPONENT
Takashi Iwai691be972016-03-18 15:10:08 +0100173static inline bool codec_has_acomp(struct hda_codec *codec)
174{
175 struct hdmi_spec *spec = codec->spec;
176 return spec->use_acomp_notifier;
177}
Takashi Iwaif4e30402015-12-10 13:01:28 +0100178#else
179#define codec_has_acomp(codec) false
180#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800181
182struct hdmi_audio_infoframe {
183 u8 type; /* 0x84 */
184 u8 ver; /* 0x01 */
185 u8 len; /* 0x0a */
186
Wu Fengguang53d7d692010-09-21 14:25:49 +0800187 u8 checksum;
188
Wu Fengguang079d88c2010-03-08 10:44:23 +0800189 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
190 u8 SS01_SF24;
191 u8 CXT04;
192 u8 CA;
193 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800194};
195
196struct dp_audio_infoframe {
197 u8 type; /* 0x84 */
198 u8 len; /* 0x1b */
199 u8 ver; /* 0x11 << 2 */
200
201 u8 CC02_CT47; /* match with HDMI infoframe from this on */
202 u8 SS01_SF24;
203 u8 CXT04;
204 u8 CA;
205 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800206};
207
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100208union audio_infoframe {
209 struct hdmi_audio_infoframe hdmi;
210 struct dp_audio_infoframe dp;
211 u8 bytes[0];
212};
213
Wu Fengguang079d88c2010-03-08 10:44:23 +0800214/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800215 * HDMI routines
216 */
217
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100218#define get_pin(spec, idx) \
219 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
220#define get_cvt(spec, idx) \
221 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Libin Yang2bea2412016-01-12 11:13:26 +0800222/* obtain hdmi_pcm object assigned to idx */
223#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
224/* obtain hda_pcm object assigned to idx */
225#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100226
Libin Yang91520852017-01-12 16:04:53 +0800227static int pin_id_to_pin_index(struct hda_codec *codec,
228 hda_nid_t pin_nid, int dev_id)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800229{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100230 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600231 int pin_idx;
Libin Yang91520852017-01-12 16:04:53 +0800232 struct hdmi_spec_per_pin *per_pin;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800233
Libin Yang91520852017-01-12 16:04:53 +0800234 /*
235 * (dev_id == -1) means it is NON-MST pin
236 * return the first virtual pin on this port
237 */
238 if (dev_id == -1)
239 dev_id = 0;
240
241 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
242 per_pin = get_pin(spec, pin_idx);
243 if ((per_pin->pin_nid == pin_nid) &&
244 (per_pin->dev_id == dev_id))
Stephen Warren384a48d2011-06-01 11:14:21 -0600245 return pin_idx;
Libin Yang91520852017-01-12 16:04:53 +0800246 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800247
Takashi Iwai4e76a882014-02-25 12:21:03 +0100248 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600249 return -EINVAL;
250}
251
Libin Yang2bf3c852015-12-16 13:42:43 +0800252static int hinfo_to_pcm_index(struct hda_codec *codec,
253 struct hda_pcm_stream *hinfo)
254{
255 struct hdmi_spec *spec = codec->spec;
256 int pcm_idx;
257
258 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
259 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
260 return pcm_idx;
261
262 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
263 return -EINVAL;
264}
265
Takashi Iwai4e76a882014-02-25 12:21:03 +0100266static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600267 struct hda_pcm_stream *hinfo)
268{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100269 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800270 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600271 int pin_idx;
272
Libin Yang6590faa2015-12-16 13:42:41 +0800273 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
274 per_pin = get_pin(spec, pin_idx);
Libin Yang2bea2412016-01-12 11:13:26 +0800275 if (per_pin->pcm &&
276 per_pin->pcm->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600277 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800278 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600279
Libin Yang6590faa2015-12-16 13:42:41 +0800280 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600281 return -EINVAL;
282}
283
Libin Yang022f3442016-02-03 10:48:34 +0800284static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
285 int pcm_idx)
286{
287 int i;
288 struct hdmi_spec_per_pin *per_pin;
289
290 for (i = 0; i < spec->num_pins; i++) {
291 per_pin = get_pin(spec, i);
292 if (per_pin->pcm_idx == pcm_idx)
293 return per_pin;
294 }
295 return NULL;
296}
297
Takashi Iwai4e76a882014-02-25 12:21:03 +0100298static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600299{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100300 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600301 int cvt_idx;
302
303 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100304 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600305 return cvt_idx;
306
Takashi Iwai4e76a882014-02-25 12:21:03 +0100307 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800308 return -EINVAL;
309}
310
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500311static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
312 struct snd_ctl_elem_info *uinfo)
313{
314 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100315 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200316 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100317 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800318 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500319
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500320 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
321
Libin Yangfb087ea2016-02-23 16:33:37 +0800322 pcm_idx = kcontrol->private_value;
323 mutex_lock(&spec->pcm_lock);
324 per_pin = pcm_idx_to_pin(spec, pcm_idx);
325 if (!per_pin) {
326 /* no pin is bound to the pcm */
327 uinfo->count = 0;
Takashi Iwaif69548f2018-07-12 23:06:51 +0200328 goto unlock;
Libin Yangfb087ea2016-02-23 16:33:37 +0800329 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200330 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100331 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500332
Takashi Iwaif69548f2018-07-12 23:06:51 +0200333 unlock:
334 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500335 return 0;
336}
337
338static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_value *ucontrol)
340{
341 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100342 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200343 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100344 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800345 int pcm_idx;
Takashi Iwaif69548f2018-07-12 23:06:51 +0200346 int err = 0;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500347
Libin Yangfb087ea2016-02-23 16:33:37 +0800348 pcm_idx = kcontrol->private_value;
349 mutex_lock(&spec->pcm_lock);
350 per_pin = pcm_idx_to_pin(spec, pcm_idx);
351 if (!per_pin) {
352 /* no pin is bound to the pcm */
353 memset(ucontrol->value.bytes.data, 0,
354 ARRAY_SIZE(ucontrol->value.bytes.data));
Takashi Iwaif69548f2018-07-12 23:06:51 +0200355 goto unlock;
Libin Yangfb087ea2016-02-23 16:33:37 +0800356 }
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500357
Takashi Iwaif69548f2018-07-12 23:06:51 +0200358 eld = &per_pin->sink_eld;
David Henningsson360a8242016-02-05 09:05:41 +0100359 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
360 eld->eld_size > ELD_MAX_SIZE) {
David Henningsson68e03de2013-02-19 16:11:23 +0100361 snd_BUG();
Takashi Iwaif69548f2018-07-12 23:06:51 +0200362 err = -EINVAL;
363 goto unlock;
David Henningsson68e03de2013-02-19 16:11:23 +0100364 }
365
366 memset(ucontrol->value.bytes.data, 0,
367 ARRAY_SIZE(ucontrol->value.bytes.data));
368 if (eld->eld_valid)
369 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
370 eld->eld_size);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500371
Takashi Iwaif69548f2018-07-12 23:06:51 +0200372 unlock:
373 mutex_unlock(&spec->pcm_lock);
374 return err;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500375}
376
Bhumika Goyalf3b827e2017-02-20 00:18:09 +0530377static const struct snd_kcontrol_new eld_bytes_ctl = {
Takashi Iwaifbd3eb72020-01-04 09:35:56 +0100378 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE |
379 SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500380 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
381 .name = "ELD",
382 .info = hdmi_eld_ctl_info,
383 .get = hdmi_eld_ctl_get,
384};
385
Libin Yangfb087ea2016-02-23 16:33:37 +0800386static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500387 int device)
388{
389 struct snd_kcontrol *kctl;
390 struct hdmi_spec *spec = codec->spec;
391 int err;
392
393 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
394 if (!kctl)
395 return -ENOMEM;
Libin Yangfb087ea2016-02-23 16:33:37 +0800396 kctl->private_value = pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500397 kctl->id.device = device;
398
Libin Yangfb087ea2016-02-23 16:33:37 +0800399 /* no pin nid is associated with the kctl now
400 * tbd: associate pin nid to eld ctl later
401 */
402 err = snd_hda_ctl_add(codec, 0, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500403 if (err < 0)
404 return err;
405
Libin Yangfb087ea2016-02-23 16:33:37 +0800406 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500407 return 0;
408}
409
Wu Fengguang079d88c2010-03-08 10:44:23 +0800410#ifdef BE_PARANOID
411static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
412 int *packet_index, int *byte_index)
413{
414 int val;
415
416 val = snd_hda_codec_read(codec, pin_nid, 0,
417 AC_VERB_GET_HDMI_DIP_INDEX, 0);
418
419 *packet_index = val >> 5;
420 *byte_index = val & 0x1f;
421}
422#endif
423
424static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
425 int packet_index, int byte_index)
426{
427 int val;
428
429 val = (packet_index << 5) | (byte_index & 0x1f);
430
431 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
432}
433
434static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
435 unsigned char val)
436{
437 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
438}
439
Stephen Warren384a48d2011-06-01 11:14:21 -0600440static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800441{
Stephen Warren75fae112014-01-30 11:52:16 -0700442 struct hdmi_spec *spec = codec->spec;
443 int pin_out;
444
Wu Fengguang079d88c2010-03-08 10:44:23 +0800445 /* Unmute */
446 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
447 snd_hda_codec_write(codec, pin_nid, 0,
448 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700449
450 if (spec->dyn_pin_out)
451 /* Disable pin out until stream is active */
452 pin_out = 0;
453 else
454 /* Enable pin out: some machines with GM965 gets broken output
455 * when the pin is disabled or changed while using with HDMI
456 */
457 pin_out = PIN_OUT;
458
Wu Fengguang079d88c2010-03-08 10:44:23 +0800459 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700460 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800461}
462
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200463/*
464 * ELD proc files
465 */
466
Jie Yangcd6a6502015-05-27 19:45:45 +0800467#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200468static void print_eld_info(struct snd_info_entry *entry,
469 struct snd_info_buffer *buffer)
470{
471 struct hdmi_spec_per_pin *per_pin = entry->private_data;
472
473 mutex_lock(&per_pin->lock);
474 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
475 mutex_unlock(&per_pin->lock);
476}
477
478static void write_eld_info(struct snd_info_entry *entry,
479 struct snd_info_buffer *buffer)
480{
481 struct hdmi_spec_per_pin *per_pin = entry->private_data;
482
483 mutex_lock(&per_pin->lock);
484 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
485 mutex_unlock(&per_pin->lock);
486}
487
488static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
489{
490 char name[32];
491 struct hda_codec *codec = per_pin->codec;
492 struct snd_info_entry *entry;
493 int err;
494
495 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100496 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200497 if (err < 0)
498 return err;
499
500 snd_info_set_text_ops(entry, per_pin, print_eld_info);
501 entry->c.text.write = write_eld_info;
Joe Perches6a73cf42018-05-23 12:20:59 -0700502 entry->mode |= 0200;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200503 per_pin->proc_entry = entry;
504
505 return 0;
506}
507
508static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
509{
Markus Elfring1947a112015-06-28 11:15:28 +0200510 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200511 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200512 per_pin->proc_entry = NULL;
513 }
514}
515#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200516static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
517 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200518{
519 return 0;
520}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200521static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200522{
523}
524#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800525
526/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800527 * Audio InfoFrame routines
528 */
529
530/*
531 * Enable Audio InfoFrame Transmission
532 */
533static void hdmi_start_infoframe_trans(struct hda_codec *codec,
534 hda_nid_t pin_nid)
535{
536 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
537 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
538 AC_DIPXMIT_BEST);
539}
540
541/*
542 * Disable Audio InfoFrame Transmission
543 */
544static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
545 hda_nid_t pin_nid)
546{
547 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
548 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
549 AC_DIPXMIT_DISABLE);
550}
551
552static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
553{
554#ifdef CONFIG_SND_DEBUG_VERBOSE
555 int i;
556 int size;
557
558 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100559 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800560
561 for (i = 0; i < 8; i++) {
562 size = snd_hda_codec_read(codec, pin_nid, 0,
563 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100564 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800565 }
566#endif
567}
568
569static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
570{
571#ifdef BE_PARANOID
572 int i, j;
573 int size;
574 int pi, bi;
575 for (i = 0; i < 8; i++) {
576 size = snd_hda_codec_read(codec, pin_nid, 0,
577 AC_VERB_GET_HDMI_DIP_SIZE, i);
578 if (size == 0)
579 continue;
580
581 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
582 for (j = 1; j < 1000; j++) {
583 hdmi_write_dip_byte(codec, pin_nid, 0x0);
584 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
585 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100586 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +0800587 bi, pi, i);
588 if (bi == 0) /* byte index wrapped around */
589 break;
590 }
Takashi Iwai4e76a882014-02-25 12:21:03 +0100591 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800592 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
593 i, size, j);
594 }
595#endif
596}
597
Wu Fengguang53d7d692010-09-21 14:25:49 +0800598static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800599{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800600 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800601 u8 sum = 0;
602 int i;
603
Wu Fengguang53d7d692010-09-21 14:25:49 +0800604 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800605
Wu Fengguang53d7d692010-09-21 14:25:49 +0800606 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800607 sum += bytes[i];
608
Wu Fengguang53d7d692010-09-21 14:25:49 +0800609 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800610}
611
612static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
613 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800614 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800615{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800616 int i;
617
618 hdmi_debug_dip_size(codec, pin_nid);
619 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
620
Wu Fengguang079d88c2010-03-08 10:44:23 +0800621 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800622 for (i = 0; i < size; i++)
623 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800624}
625
626static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800627 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800628{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800629 u8 val;
630 int i;
631
632 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
633 != AC_DIPXMIT_BEST)
634 return false;
635
636 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800637 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800638 val = snd_hda_codec_read(codec, pin_nid, 0,
639 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800640 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800641 return false;
642 }
643
644 return true;
645}
646
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530647static int hdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
648 int dev_id, unsigned char *buf, int *eld_size)
649{
650 snd_hda_set_dev_select(codec, nid, dev_id);
651
652 return snd_hdmi_get_eld(codec, nid, buf, eld_size);
653}
654
Anssi Hannula307229d2013-10-24 21:10:34 +0300655static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530656 hda_nid_t pin_nid, int dev_id,
Anssi Hannula307229d2013-10-24 21:10:34 +0300657 int ca, int active_channels,
658 int conn_type)
659{
660 union audio_infoframe ai;
661
Mengdong Lincaaf5ef2014-03-11 17:12:52 -0400662 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +0300663 if (conn_type == 0) { /* HDMI */
664 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
665
666 hdmi_ai->type = 0x84;
667 hdmi_ai->ver = 0x01;
668 hdmi_ai->len = 0x0a;
669 hdmi_ai->CC02_CT47 = active_channels - 1;
670 hdmi_ai->CA = ca;
671 hdmi_checksum_audio_infoframe(hdmi_ai);
672 } else if (conn_type == 1) { /* DisplayPort */
673 struct dp_audio_infoframe *dp_ai = &ai.dp;
674
675 dp_ai->type = 0x84;
676 dp_ai->len = 0x1b;
677 dp_ai->ver = 0x11 << 2;
678 dp_ai->CC02_CT47 = active_channels - 1;
679 dp_ai->CA = ca;
680 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100681 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300682 pin_nid);
683 return;
684 }
685
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530686 snd_hda_set_dev_select(codec, pin_nid, dev_id);
687
Anssi Hannula307229d2013-10-24 21:10:34 +0300688 /*
689 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
690 * sizeof(*dp_ai) to avoid partial match/update problems when
691 * the user switches between HDMI/DP monitors.
692 */
693 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
694 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100695 codec_dbg(codec,
696 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300697 pin_nid,
698 active_channels, ca);
699 hdmi_stop_infoframe_trans(codec, pin_nid);
700 hdmi_fill_audio_infoframe(codec, pin_nid,
701 ai.bytes, sizeof(ai));
702 hdmi_start_infoframe_trans(codec, pin_nid);
703 }
704}
705
Takashi Iwaib0540872013-09-02 12:33:02 +0200706static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
707 struct hdmi_spec_per_pin *per_pin,
708 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800709{
Anssi Hannula307229d2013-10-24 21:10:34 +0300710 struct hdmi_spec *spec = codec->spec;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530711 struct hdac_chmap *chmap = &spec->chmap;
Stephen Warren384a48d2011-06-01 11:14:21 -0600712 hda_nid_t pin_nid = per_pin->pin_nid;
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530713 int dev_id = per_pin->dev_id;
Takashi Iwaib0540872013-09-02 12:33:02 +0200714 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300715 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600716 struct hdmi_eld *eld;
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530717 int ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800718
Takashi Iwaib0540872013-09-02 12:33:02 +0200719 if (!channels)
720 return;
721
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530722 snd_hda_set_dev_select(codec, pin_nid, dev_id);
723
Takashi Iwai44bb6d02016-03-21 12:36:44 +0100724 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
725 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
Mengdong Lin58f7d282013-09-04 16:37:12 -0400726 snd_hda_codec_write(codec, pin_nid, 0,
727 AC_VERB_SET_AMP_GAIN_MUTE,
728 AMP_OUT_UNMUTE);
729
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100730 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800731
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530732 ca = snd_hdac_channel_allocation(&codec->core,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530733 eld->info.spk_alloc, channels,
734 per_pin->chmap_set, non_pcm, per_pin->chmap);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800735
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530736 active_channels = snd_hdac_get_active_channels(ca);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300737
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530738 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
739 active_channels);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300740
Stephen Warren384a48d2011-06-01 11:14:21 -0600741 /*
Anssi Hannula39edac72013-10-07 19:24:52 +0300742 * always configure channel mapping, it may have been changed by the
743 * user in the meantime
744 */
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530745 snd_hdac_setup_channel_mapping(&spec->chmap,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530746 pin_nid, non_pcm, ca, channels,
747 per_pin->chmap, per_pin->chmap_set);
Anssi Hannula39edac72013-10-07 19:24:52 +0300748
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530749 spec->ops.pin_setup_infoframe(codec, pin_nid, dev_id,
750 ca, active_channels, eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +0800751
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200752 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800753}
754
Wu Fengguang079d88c2010-03-08 10:44:23 +0800755/*
756 * Unsolicited events
757 */
758
Takashi Iwaidb845402020-02-06 17:28:02 +0100759static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200760
Libin Yang91520852017-01-12 16:04:53 +0800761static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid,
762 int dev_id)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800763{
764 struct hdmi_spec *spec = codec->spec;
Libin Yang91520852017-01-12 16:04:53 +0800765 int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200766
David Henningsson20ce9022013-12-04 10:19:41 +0800767 if (pin_idx < 0)
768 return;
Chris Wilsonaaa23f82018-06-27 07:25:32 +0100769 mutex_lock(&spec->pcm_lock);
Takashi Iwaidb845402020-02-06 17:28:02 +0100770 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Chris Wilsonaaa23f82018-06-27 07:25:32 +0100771 mutex_unlock(&spec->pcm_lock);
David Henningsson20ce9022013-12-04 10:19:41 +0800772}
773
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200774static void jack_callback(struct hda_codec *codec,
775 struct hda_jack_callback *jack)
776{
Takashi Iwaiade49db2018-07-11 18:05:52 +0200777 /* stop polling when notification is enabled */
778 if (codec_has_acomp(codec))
779 return;
780
Nikhil Mahale5204a052019-11-19 14:17:08 +0530781 check_presence_and_report(codec, jack->nid, jack->dev_id);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200782}
783
Takashi Iwai165c0942020-02-06 17:28:01 +0100784static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res,
785 struct hda_jack_tbl *jack)
David Henningsson20ce9022013-12-04 10:19:41 +0800786{
Takashi Iwai3a938972011-10-28 01:16:55 +0200787 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800788
Takashi Iwai4e76a882014-02-25 12:21:03 +0100789 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400790 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
Nikhil Mahale5204a052019-11-19 14:17:08 +0530791 codec->addr, jack->nid, jack->dev_id, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800792 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800793
Nikhil Mahale5204a052019-11-19 14:17:08 +0530794 check_presence_and_report(codec, jack->nid, jack->dev_id);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800795}
796
797static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
798{
799 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
800 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
801 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
802 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
803
Takashi Iwai4e76a882014-02-25 12:21:03 +0100804 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200805 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600806 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800807 tag,
808 subtag,
809 cp_state,
810 cp_ready);
811
812 /* TODO */
Pierre-Louis Bossart75663c02020-01-13 15:14:05 -0600813 if (cp_state) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800814 ;
Pierre-Louis Bossart75663c02020-01-13 15:14:05 -0600815 }
816 if (cp_ready) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800817 ;
Pierre-Louis Bossart75663c02020-01-13 15:14:05 -0600818 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800819}
820
821
822static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
823{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800824 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
825 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
Nikhil Mahale5204a052019-11-19 14:17:08 +0530826 struct hda_jack_tbl *jack;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800827
Takashi Iwaiade49db2018-07-11 18:05:52 +0200828 if (codec_has_acomp(codec))
829 return;
830
Nikhil Mahale5204a052019-11-19 14:17:08 +0530831 if (codec->dp_mst) {
832 int dev_entry =
833 (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
834
835 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
836 } else {
837 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
838 }
839
840 if (!jack) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100841 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800842 return;
843 }
844
845 if (subtag == 0)
Takashi Iwai165c0942020-02-06 17:28:01 +0100846 hdmi_intrinsic_event(codec, res, jack);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800847 else
848 hdmi_non_intrinsic_event(codec, res);
849}
850
Mengdong Lin58f7d282013-09-04 16:37:12 -0400851static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +0800852 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +0200853{
Mengdong Lin58f7d282013-09-04 16:37:12 -0400854 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +0200855
Wang Xingchao53b434f2013-06-18 10:41:53 +0800856 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
857 * thus pins could only choose converter 0 for use. Make sure the
858 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200859 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +0800860 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
861
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200862 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +0200863 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
864 AC_PWRST_D0);
865 msleep(40);
866 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
867 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100868 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
David Henningsson83f26ad2013-04-10 12:26:07 +0200869 }
David Henningsson83f26ad2013-04-10 12:26:07 +0200870}
871
Wu Fengguang079d88c2010-03-08 10:44:23 +0800872/*
873 * Callbacks
874 */
875
Takashi Iwai92f10b32010-08-03 14:21:00 +0200876/* HBR should be Non-PCM, 8 channels */
877#define is_hbr_format(format) \
878 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
879
Anssi Hannula307229d2013-10-24 21:10:34 +0300880static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530881 int dev_id, bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800882{
Anssi Hannula307229d2013-10-24 21:10:34 +0300883 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +0200884
Stephen Warren384a48d2011-06-01 11:14:21 -0600885 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530886 snd_hda_set_dev_select(codec, pin_nid, dev_id);
Stephen Warren384a48d2011-06-01 11:14:21 -0600887 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300888 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
889
Anssi Hannula13122e62013-11-10 20:56:10 +0200890 if (pinctl < 0)
891 return hbr ? -EINVAL : 0;
892
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300893 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +0300894 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300895 new_pinctl |= AC_PINCTL_EPT_HBR;
896 else
897 new_pinctl |= AC_PINCTL_EPT_NATIVE;
898
Takashi Iwai4e76a882014-02-25 12:21:03 +0100899 codec_dbg(codec,
900 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600901 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300902 pinctl == new_pinctl ? "" : "new-",
903 new_pinctl);
904
905 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -0600906 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300907 AC_VERB_SET_PIN_WIDGET_CONTROL,
908 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +0300909 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300910 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +0300911
912 return 0;
913}
914
915static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530916 hda_nid_t pin_nid, int dev_id,
917 u32 stream_tag, int format)
Anssi Hannula307229d2013-10-24 21:10:34 +0300918{
919 struct hdmi_spec *spec = codec->spec;
Sriram Periyasamy5a5d7182017-09-19 17:25:05 -0500920 unsigned int param;
Anssi Hannula307229d2013-10-24 21:10:34 +0300921 int err;
922
Nikhil Mahale9c32fea2019-11-19 14:17:09 +0530923 err = spec->ops.pin_hbr_setup(codec, pin_nid, dev_id,
924 is_hbr_format(format));
Anssi Hannula307229d2013-10-24 21:10:34 +0300925
926 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100927 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +0300928 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300929 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800930
Takashi Iwaicb457222019-11-11 20:09:37 +0100931 if (spec->intel_hsw_fixup) {
Sriram Periyasamy5a5d7182017-09-19 17:25:05 -0500932
933 /*
934 * on recent platforms IEC Coding Type is required for HBR
935 * support, read current Digital Converter settings and set
936 * ICT bitfield if needed.
937 */
938 param = snd_hda_codec_read(codec, cvt_nid, 0,
939 AC_VERB_GET_DIGI_CONVERT_1, 0);
940
941 param = (param >> 16) & ~(AC_DIG3_ICT);
942
943 /* on recent platforms ICT mode is required for HBR support */
944 if (is_hbr_format(format))
945 param |= 0x1;
946
947 snd_hda_codec_write(codec, cvt_nid, 0,
948 AC_VERB_SET_DIGI_CONVERT_3, param);
949 }
950
Stephen Warren384a48d2011-06-01 11:14:21 -0600951 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300952 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800953}
954
Libin Yang42b29872015-12-16 13:42:42 +0800955/* Try to find an available converter
956 * If pin_idx is less then zero, just try to find an available converter.
957 * Otherwise, try to find an available converter and get the cvt mux index
958 * of the pin.
959 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800960static int hdmi_choose_cvt(struct hda_codec *codec,
Takashi Iwai4846a672016-03-21 12:56:46 +0100961 int pin_idx, int *cvt_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200962{
963 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600964 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600965 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800966 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200967
Libin Yang42b29872015-12-16 13:42:42 +0800968 /* pin_idx < 0 means no pin will be bound to the converter */
969 if (pin_idx < 0)
970 per_pin = NULL;
971 else
972 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200973
Stephen Warren384a48d2011-06-01 11:14:21 -0600974 /* Dynamically assign converter to stream */
975 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100976 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -0600977
978 /* Must not already be assigned */
979 if (per_cvt->assigned)
980 continue;
Libin Yang42b29872015-12-16 13:42:42 +0800981 if (per_pin == NULL)
982 break;
Stephen Warren384a48d2011-06-01 11:14:21 -0600983 /* Must be in pin's mux's list of converters */
984 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
985 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
986 break;
987 /* Not in mux list */
988 if (mux_idx == per_pin->num_mux_nids)
989 continue;
990 break;
991 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800992
Stephen Warren384a48d2011-06-01 11:14:21 -0600993 /* No free converters */
994 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +0800995 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -0600996
Libin Yang42b29872015-12-16 13:42:42 +0800997 if (per_pin != NULL)
998 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +0800999
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001000 if (cvt_id)
1001 *cvt_id = cvt_idx;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001002
1003 return 0;
1004}
1005
Mengdong Lin2df67422014-03-20 13:01:06 +08001006/* Assure the pin select the right convetor */
1007static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1008 struct hdmi_spec_per_pin *per_pin)
1009{
1010 hda_nid_t pin_nid = per_pin->pin_nid;
1011 int mux_idx, curr;
1012
1013 mux_idx = per_pin->mux_idx;
1014 curr = snd_hda_codec_read(codec, pin_nid, 0,
1015 AC_VERB_GET_CONNECT_SEL, 0);
1016 if (curr != mux_idx)
1017 snd_hda_codec_write_cache(codec, pin_nid, 0,
1018 AC_VERB_SET_CONNECT_SEL,
1019 mux_idx);
1020}
1021
Libin Yang42b29872015-12-16 13:42:42 +08001022/* get the mux index for the converter of the pins
1023 * converter's mux index is the same for all pins on Intel platform
1024 */
1025static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1026 hda_nid_t cvt_nid)
1027{
1028 int i;
1029
1030 for (i = 0; i < spec->num_cvts; i++)
1031 if (spec->cvt_nids[i] == cvt_nid)
1032 return i;
1033 return -EINVAL;
1034}
1035
Mengdong Lin300016b2013-11-04 01:13:13 -05001036/* Intel HDMI workaround to fix audio routing issue:
1037 * For some Intel display codecs, pins share the same connection list.
1038 * So a conveter can be selected by multiple pins and playback on any of these
1039 * pins will generate sound on the external display, because audio flows from
1040 * the same converter to the display pipeline. Also muting one pin may make
1041 * other pins have no sound output.
1042 * So this function assures that an assigned converter for a pin is not selected
1043 * by any other pins.
1044 */
1045static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Libin Yang91520852017-01-12 16:04:53 +08001046 hda_nid_t pin_nid,
1047 int dev_id, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001048{
1049 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001050 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001051 int cvt_idx, curr;
1052 struct hdmi_spec_per_cvt *per_cvt;
Libin Yang91520852017-01-12 16:04:53 +08001053 struct hdmi_spec_per_pin *per_pin;
1054 int pin_idx;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001055
Libin Yang91520852017-01-12 16:04:53 +08001056 /* configure the pins connections */
1057 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1058 int dev_id_saved;
1059 int dev_num;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001060
Libin Yang91520852017-01-12 16:04:53 +08001061 per_pin = get_pin(spec, pin_idx);
1062 /*
1063 * pin not connected to monitor
1064 * no need to operate on it
1065 */
1066 if (!per_pin->pcm)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001067 continue;
1068
Libin Yang91520852017-01-12 16:04:53 +08001069 if ((per_pin->pin_nid == pin_nid) &&
1070 (per_pin->dev_id == dev_id))
Mengdong Linf82d7d12013-09-21 20:34:45 -04001071 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001072
Libin Yang91520852017-01-12 16:04:53 +08001073 /*
1074 * if per_pin->dev_id >= dev_num,
1075 * snd_hda_get_dev_select() will fail,
1076 * and the following operation is unpredictable.
1077 * So skip this situation.
1078 */
1079 dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1;
1080 if (per_pin->dev_id >= dev_num)
1081 continue;
1082
1083 nid = per_pin->pin_nid;
1084
1085 /*
1086 * Calling this function should not impact
1087 * on the device entry selection
1088 * So let's save the dev id for each pin,
1089 * and restore it when return
1090 */
1091 dev_id_saved = snd_hda_get_dev_select(codec, nid);
1092 snd_hda_set_dev_select(codec, nid, per_pin->dev_id);
Mengdong Linf82d7d12013-09-21 20:34:45 -04001093 curr = snd_hda_codec_read(codec, nid, 0,
1094 AC_VERB_GET_CONNECT_SEL, 0);
Libin Yang91520852017-01-12 16:04:53 +08001095 if (curr != mux_idx) {
1096 snd_hda_set_dev_select(codec, nid, dev_id_saved);
Mengdong Linf82d7d12013-09-21 20:34:45 -04001097 continue;
Libin Yang91520852017-01-12 16:04:53 +08001098 }
1099
Mengdong Linf82d7d12013-09-21 20:34:45 -04001100
1101 /* choose an unassigned converter. The conveters in the
1102 * connection list are in the same order as in the codec.
1103 */
1104 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1105 per_cvt = get_cvt(spec, cvt_idx);
1106 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001107 codec_dbg(codec,
1108 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001109 cvt_idx, nid);
1110 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001111 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001112 cvt_idx);
1113 break;
1114 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001115 }
Libin Yang91520852017-01-12 16:04:53 +08001116 snd_hda_set_dev_select(codec, nid, dev_id_saved);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001117 }
1118}
1119
Libin Yang42b29872015-12-16 13:42:42 +08001120/* A wrapper of intel_not_share_asigned_cvt() */
1121static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
Libin Yang91520852017-01-12 16:04:53 +08001122 hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
Libin Yang42b29872015-12-16 13:42:42 +08001123{
1124 int mux_idx;
1125 struct hdmi_spec *spec = codec->spec;
1126
Libin Yang42b29872015-12-16 13:42:42 +08001127 /* On Intel platform, the mapping of converter nid to
1128 * mux index of the pins are always the same.
1129 * The pin nid may be 0, this means all pins will not
1130 * share the converter.
1131 */
1132 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1133 if (mux_idx >= 0)
Libin Yang91520852017-01-12 16:04:53 +08001134 intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001135}
1136
Takashi Iwai4846a672016-03-21 12:56:46 +01001137/* skeleton caller of pin_cvt_fixup ops */
1138static void pin_cvt_fixup(struct hda_codec *codec,
1139 struct hdmi_spec_per_pin *per_pin,
1140 hda_nid_t cvt_nid)
1141{
1142 struct hdmi_spec *spec = codec->spec;
1143
1144 if (spec->ops.pin_cvt_fixup)
1145 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1146}
1147
Libin Yang42b29872015-12-16 13:42:42 +08001148/* called in hdmi_pcm_open when no pin is assigned to the PCM
1149 * in dyn_pcm_assign mode.
1150 */
1151static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1152 struct hda_codec *codec,
1153 struct snd_pcm_substream *substream)
1154{
1155 struct hdmi_spec *spec = codec->spec;
1156 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001157 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001158 struct hdmi_spec_per_cvt *per_cvt = NULL;
1159 int err;
1160
Libin Yangac983792015-12-16 16:48:16 +08001161 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1162 if (pcm_idx < 0)
1163 return -EINVAL;
1164
Takashi Iwai4846a672016-03-21 12:56:46 +01001165 err = hdmi_choose_cvt(codec, -1, &cvt_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001166 if (err)
1167 return err;
1168
1169 per_cvt = get_cvt(spec, cvt_idx);
1170 per_cvt->assigned = 1;
1171 hinfo->nid = per_cvt->cvt_nid;
1172
Takashi Iwai4846a672016-03-21 12:56:46 +01001173 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
Libin Yang42b29872015-12-16 13:42:42 +08001174
Libin Yangac983792015-12-16 16:48:16 +08001175 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001176 /* todo: setup spdif ctls assign */
1177
1178 /* Initially set the converter's capabilities */
1179 hinfo->channels_min = per_cvt->channels_min;
1180 hinfo->channels_max = per_cvt->channels_max;
1181 hinfo->rates = per_cvt->rates;
1182 hinfo->formats = per_cvt->formats;
1183 hinfo->maxbps = per_cvt->maxbps;
1184
1185 /* Store the updated parameters */
1186 runtime->hw.channels_min = hinfo->channels_min;
1187 runtime->hw.channels_max = hinfo->channels_max;
1188 runtime->hw.formats = hinfo->formats;
1189 runtime->hw.rates = hinfo->rates;
1190
1191 snd_pcm_hw_constraint_step(substream->runtime, 0,
1192 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1193 return 0;
1194}
1195
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001196/*
1197 * HDA PCM callbacks
1198 */
1199static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1200 struct hda_codec *codec,
1201 struct snd_pcm_substream *substream)
1202{
1203 struct hdmi_spec *spec = codec->spec;
1204 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai4846a672016-03-21 12:56:46 +01001205 int pin_idx, cvt_idx, pcm_idx;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001206 struct hdmi_spec_per_pin *per_pin;
1207 struct hdmi_eld *eld;
1208 struct hdmi_spec_per_cvt *per_cvt = NULL;
1209 int err;
1210
1211 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001212 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1213 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001214 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001215
Libin Yang42b29872015-12-16 13:42:42 +08001216 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001217 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001218 if (!spec->dyn_pcm_assign) {
1219 if (snd_BUG_ON(pin_idx < 0)) {
Takashi Iwaif69548f2018-07-12 23:06:51 +02001220 err = -EINVAL;
1221 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001222 }
1223 } else {
1224 /* no pin is assigned to the PCM
1225 * PA need pcm open successfully when probe
1226 */
1227 if (pin_idx < 0) {
1228 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001229 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001230 }
1231 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001232
Takashi Iwai4846a672016-03-21 12:56:46 +01001233 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001234 if (err < 0)
1235 goto unlock;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001236
1237 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001238 /* Claim converter */
1239 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001240
Libin Yangac983792015-12-16 16:48:16 +08001241 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001242 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001243 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001244 hinfo->nid = per_cvt->cvt_nid;
1245
Takashi Iwaie38e4862019-12-02 08:49:47 +01001246 /* flip stripe flag for the assigned stream if supported */
1247 if (get_wcaps(codec, per_cvt->cvt_nid) & AC_WCAP_STRIPE)
1248 azx_stream(get_azx_dev(substream))->stripe = 1;
1249
Libin Yang91520852017-01-12 16:04:53 +08001250 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
Takashi Iwaibddee962013-06-18 16:14:22 +02001251 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001252 AC_VERB_SET_CONNECT_SEL,
Takashi Iwai4846a672016-03-21 12:56:46 +01001253 per_pin->mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001254
1255 /* configure unused pins to choose other converters */
Takashi Iwai4846a672016-03-21 12:56:46 +01001256 pin_cvt_fixup(codec, per_pin, 0);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001257
Libin Yang2bf3c852015-12-16 13:42:43 +08001258 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001259
Stephen Warren2def8172011-06-01 11:14:20 -06001260 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001261 hinfo->channels_min = per_cvt->channels_min;
1262 hinfo->channels_max = per_cvt->channels_max;
1263 hinfo->rates = per_cvt->rates;
1264 hinfo->formats = per_cvt->formats;
1265 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001266
Libin Yang42b29872015-12-16 13:42:42 +08001267 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001268 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001269 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001270 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001271 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001272 !hinfo->rates || !hinfo->formats) {
1273 per_cvt->assigned = 0;
1274 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001275 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001276 err = -ENODEV;
1277 goto unlock;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001278 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001279 }
Stephen Warren2def8172011-06-01 11:14:20 -06001280
1281 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001282 runtime->hw.channels_min = hinfo->channels_min;
1283 runtime->hw.channels_max = hinfo->channels_max;
1284 runtime->hw.formats = hinfo->formats;
1285 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001286
1287 snd_pcm_hw_constraint_step(substream->runtime, 0,
1288 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001289 unlock:
1290 mutex_unlock(&spec->pcm_lock);
1291 return err;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001292}
1293
1294/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001295 * HDA/HDMI auto parsing
1296 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001297static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001298{
1299 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001300 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001301 hda_nid_t pin_nid = per_pin->pin_nid;
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301302 int dev_id = per_pin->dev_id;
Kai Vehmanenae254882019-11-27 18:12:40 +02001303 int conns;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001304
1305 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001306 codec_warn(codec,
1307 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001308 pin_nid, get_wcaps(codec, pin_nid));
1309 return -EINVAL;
1310 }
1311
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301312 snd_hda_set_dev_select(codec, pin_nid, dev_id);
1313
Kai Vehmanenae254882019-11-27 18:12:40 +02001314 if (spec->intel_hsw_fixup) {
1315 conns = spec->num_cvts;
1316 memcpy(per_pin->mux_nids, spec->cvt_nids,
1317 sizeof(hda_nid_t) * conns);
1318 } else {
1319 conns = snd_hda_get_raw_connections(codec, pin_nid,
1320 per_pin->mux_nids,
1321 HDA_MAX_CONNECTIONS);
1322 }
1323
Libin Yang91520852017-01-12 16:04:53 +08001324 /* all the device entries on the same pin have the same conn list */
Kai Vehmanenae254882019-11-27 18:12:40 +02001325 per_pin->num_mux_nids = conns;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001326
1327 return 0;
1328}
1329
Libin Yanga76056f2015-12-16 16:48:15 +08001330static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
Nikhil Mahale5398e942019-11-19 14:17:10 +05301331 struct hdmi_spec_per_pin *per_pin)
Libin Yanga76056f2015-12-16 16:48:15 +08001332{
1333 int i;
1334
Nikhil Mahale5398e942019-11-19 14:17:10 +05301335 /*
Kai Vehmanen0c0fe9e2019-11-29 16:37:55 +02001336 * generic_hdmi_build_pcms() may allocate extra PCMs on some
1337 * platforms (with maximum of 'num_nids + dev_num - 1')
Nikhil Mahale5398e942019-11-19 14:17:10 +05301338 *
1339 * The per_pin of pin_nid_idx=n and dev_id=m prefers to get pcm-n
1340 * if m==0. This guarantees that dynamic pcm assignments are compatible
Kai Vehmanen0c0fe9e2019-11-29 16:37:55 +02001341 * with the legacy static per_pin-pcm assignment that existed in the
Nikhil Mahale5398e942019-11-19 14:17:10 +05301342 * days before DP-MST.
1343 *
Takashi Iwai643a2cc2019-12-03 16:41:05 +01001344 * Intel DP-MST prefers this legacy behavior for compatibility, too.
1345 *
Nikhil Mahale5398e942019-11-19 14:17:10 +05301346 * per_pin of m!=0 prefers to get pcm=(num_nids + (m - 1)).
1347 */
Libin Yanga76056f2015-12-16 16:48:15 +08001348
Takashi Iwai643a2cc2019-12-03 16:41:05 +01001349 if (per_pin->dev_id == 0 || spec->intel_hsw_fixup) {
Kai Vehmanen0c0fe9e2019-11-29 16:37:55 +02001350 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1351 return per_pin->pin_nid_idx;
1352 } else {
1353 i = spec->num_nids + (per_pin->dev_id - 1);
1354 if (i < spec->pcm_used && !(test_bit(i, &spec->pcm_bitmap)))
1355 return i;
Nikhil Mahale5398e942019-11-19 14:17:10 +05301356 }
1357
1358 /* have a second try; check the area over num_nids */
Libin Yang91520852017-01-12 16:04:53 +08001359 for (i = spec->num_nids; i < spec->pcm_used; i++) {
Libin Yanga76056f2015-12-16 16:48:15 +08001360 if (!test_bit(i, &spec->pcm_bitmap))
1361 return i;
1362 }
1363
1364 /* the last try; check the empty slots in pins */
Libin Yang91520852017-01-12 16:04:53 +08001365 for (i = 0; i < spec->num_nids; i++) {
Libin Yanga76056f2015-12-16 16:48:15 +08001366 if (!test_bit(i, &spec->pcm_bitmap))
1367 return i;
1368 }
1369 return -EBUSY;
1370}
1371
1372static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1373 struct hdmi_spec_per_pin *per_pin)
1374{
1375 int idx;
1376
1377 /* pcm already be attached to the pin */
1378 if (per_pin->pcm)
1379 return;
1380 idx = hdmi_find_pcm_slot(spec, per_pin);
Libin Yangd10a80d2016-03-01 15:18:26 +08001381 if (idx == -EBUSY)
Libin Yanga76056f2015-12-16 16:48:15 +08001382 return;
1383 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001384 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001385 set_bit(idx, &spec->pcm_bitmap);
1386}
1387
1388static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1389 struct hdmi_spec_per_pin *per_pin)
1390{
1391 int idx;
1392
1393 /* pcm already be detached from the pin */
1394 if (!per_pin->pcm)
1395 return;
1396 idx = per_pin->pcm_idx;
1397 per_pin->pcm_idx = -1;
1398 per_pin->pcm = NULL;
1399 if (idx >= 0 && idx < spec->pcm_used)
1400 clear_bit(idx, &spec->pcm_bitmap);
1401}
1402
Libin Yangac983792015-12-16 16:48:16 +08001403static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1404 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1405{
1406 int mux_idx;
1407
1408 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1409 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1410 break;
1411 return mux_idx;
1412}
1413
1414static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1415
1416static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1417 struct hdmi_spec_per_pin *per_pin)
1418{
1419 struct hda_codec *codec = per_pin->codec;
1420 struct hda_pcm *pcm;
1421 struct hda_pcm_stream *hinfo;
1422 struct snd_pcm_substream *substream;
1423 int mux_idx;
1424 bool non_pcm;
1425
1426 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001427 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001428 else
1429 return;
Takashi Iwai8a7d6002018-04-25 16:19:13 +02001430 if (!pcm->pcm)
1431 return;
Libin Yangac983792015-12-16 16:48:16 +08001432 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1433 return;
1434
1435 /* hdmi audio only uses playback and one substream */
1436 hinfo = pcm->stream;
1437 substream = pcm->pcm->streams[0].substream;
1438
1439 per_pin->cvt_nid = hinfo->nid;
1440
1441 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
Libin Yang91520852017-01-12 16:04:53 +08001442 if (mux_idx < per_pin->num_mux_nids) {
1443 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1444 per_pin->dev_id);
Libin Yangac983792015-12-16 16:48:16 +08001445 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1446 AC_VERB_SET_CONNECT_SEL,
1447 mux_idx);
Libin Yang91520852017-01-12 16:04:53 +08001448 }
Libin Yangac983792015-12-16 16:48:16 +08001449 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1450
1451 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1452 if (substream->runtime)
1453 per_pin->channels = substream->runtime->channels;
1454 per_pin->setup = true;
1455 per_pin->mux_idx = mux_idx;
1456
1457 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1458}
1459
1460static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1461 struct hdmi_spec_per_pin *per_pin)
1462{
1463 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1464 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1465
1466 per_pin->chmap_set = false;
1467 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1468
1469 per_pin->setup = false;
1470 per_pin->channels = 0;
1471}
1472
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001473static struct snd_jack *pin_idx_to_pcm_jack(struct hda_codec *codec,
1474 struct hdmi_spec_per_pin *per_pin)
1475{
1476 struct hdmi_spec *spec = codec->spec;
1477
1478 if (per_pin->pcm_idx >= 0)
1479 return spec->pcm_rec[per_pin->pcm_idx].jack;
1480 else
1481 return NULL;
1482}
1483
Takashi Iwaie90247f2015-11-13 09:12:12 +01001484/* update per_pin ELD from the given new ELD;
1485 * setup info frame and notification accordingly
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001486 * also notify ELD kctl and report jack status changes
Takashi Iwaie90247f2015-11-13 09:12:12 +01001487 */
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001488static void update_eld(struct hda_codec *codec,
Takashi Iwaie90247f2015-11-13 09:12:12 +01001489 struct hdmi_spec_per_pin *per_pin,
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001490 struct hdmi_eld *eld,
1491 int repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001492{
1493 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001494 struct hdmi_spec *spec = codec->spec;
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001495 struct snd_jack *pcm_jack;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001496 bool old_eld_valid = pin_eld->eld_valid;
1497 bool eld_changed;
Colin Ian Kingf953e722019-09-05 16:48:26 +01001498 int pcm_idx;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001499
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001500 if (eld->eld_valid) {
1501 if (eld->eld_size <= 0 ||
1502 snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1503 eld->eld_size) < 0) {
1504 eld->eld_valid = false;
1505 if (repoll) {
1506 schedule_delayed_work(&per_pin->work,
1507 msecs_to_jiffies(300));
1508 return;
1509 }
1510 }
1511 }
1512
1513 if (!eld->eld_valid || eld->eld_size <= 0) {
1514 eld->eld_valid = false;
1515 eld->eld_size = 0;
1516 }
1517
Libin Yangfb087ea2016-02-23 16:33:37 +08001518 /* for monitor disconnection, save pcm_idx firstly */
1519 pcm_idx = per_pin->pcm_idx;
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001520
1521 /*
1522 * pcm_idx >=0 before update_eld() means it is in monitor
1523 * disconnected event. Jack must be fetched before update_eld().
1524 */
1525 pcm_jack = pin_idx_to_pcm_jack(codec, per_pin);
1526
Libin Yanga76056f2015-12-16 16:48:15 +08001527 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001528 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001529 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001530 hdmi_pcm_setup_pin(spec, per_pin);
1531 } else {
1532 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001533 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001534 }
Libin Yanga76056f2015-12-16 16:48:15 +08001535 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001536 /* if pcm_idx == -1, it means this is in monitor connection event
1537 * we can get the correct pcm_idx now.
1538 */
1539 if (pcm_idx == -1)
1540 pcm_idx = per_pin->pcm_idx;
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001541 if (!pcm_jack)
1542 pcm_jack = pin_idx_to_pcm_jack(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001543
Takashi Iwaie90247f2015-11-13 09:12:12 +01001544 if (eld->eld_valid)
1545 snd_hdmi_show_eld(codec, &eld->info);
1546
1547 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
Takashi Iwai551626e2019-07-17 14:30:23 +02001548 eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
1549 if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001550 if (pin_eld->eld_size != eld->eld_size ||
1551 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1552 eld->eld_size) != 0)
1553 eld_changed = true;
1554
Takashi Iwai551626e2019-07-17 14:30:23 +02001555 if (eld_changed) {
1556 pin_eld->monitor_present = eld->monitor_present;
1557 pin_eld->eld_valid = eld->eld_valid;
1558 pin_eld->eld_size = eld->eld_size;
1559 if (eld->eld_valid)
1560 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1561 eld->eld_size);
1562 pin_eld->info = eld->info;
1563 }
Takashi Iwaie90247f2015-11-13 09:12:12 +01001564
1565 /*
1566 * Re-setup pin and infoframe. This is needed e.g. when
1567 * - sink is first plugged-in
1568 * - transcoder can change during stream playback on Haswell
1569 * and this can make HW reset converter selection on a pin.
1570 */
1571 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
Takashi Iwai4846a672016-03-21 12:56:46 +01001572 pin_cvt_fixup(codec, per_pin, 0);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001573 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1574 }
1575
Libin Yangfb087ea2016-02-23 16:33:37 +08001576 if (eld_changed && pcm_idx >= 0)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001577 snd_ctl_notify(codec->card,
1578 SNDRV_CTL_EVENT_MASK_VALUE |
1579 SNDRV_CTL_EVENT_MASK_INFO,
Libin Yangfb087ea2016-02-23 16:33:37 +08001580 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001581
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001582 if (eld_changed && pcm_jack)
Takashi Iwaidb845402020-02-06 17:28:02 +01001583 snd_jack_report(pcm_jack,
1584 (eld->monitor_present && eld->eld_valid) ?
1585 SND_JACK_AVOUT : 0);
1586}
1587
Takashi Iwai788d4412015-11-12 15:36:13 +01001588/* update ELD and jack state via HD-audio verbs */
Takashi Iwaidb845402020-02-06 17:28:02 +01001589static void hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
Takashi Iwai788d4412015-11-12 15:36:13 +01001590 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001591{
Wu Fengguang744626d2011-11-16 16:29:47 +08001592 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001593 struct hdmi_spec *spec = codec->spec;
1594 struct hdmi_eld *eld = &spec->temp_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001595 hda_nid_t pin_nid = per_pin->pin_nid;
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301596 int dev_id = per_pin->dev_id;
Stephen Warren5d44f922011-05-24 17:11:17 -06001597 /*
1598 * Always execute a GetPinSense verb here, even when called from
1599 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1600 * response's PD bit is not the real PD value, but indicates that
1601 * the real PD value changed. An older version of the HD-audio
1602 * specification worked this way. Hence, we just ignore the data in
1603 * the unsolicited response to avoid custom WARs.
1604 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001605 int present;
Takashi Iwaiae47e2e2020-02-06 17:28:03 +01001606 int ret;
1607
1608 ret = snd_hda_power_up_pm(codec);
1609 if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec)))
1610 goto out;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001611
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301612 present = snd_hda_jack_pin_sense(codec, pin_nid, dev_id);
David Henningssonda4a7a32013-12-18 10:46:04 +01001613
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001614 mutex_lock(&per_pin->lock);
Takashi Iwaic44da622016-04-13 09:45:53 +02001615 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1616 if (eld->monitor_present)
David Henningsson4bd038f2013-02-19 16:11:25 +01001617 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1618 else
1619 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001620
Takashi Iwai4e76a882014-02-25 12:21:03 +01001621 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001622 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Takashi Iwaic44da622016-04-13 09:45:53 +02001623 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001624
David Henningsson4bd038f2013-02-19 16:11:25 +01001625 if (eld->eld_valid) {
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301626 if (spec->ops.pin_get_eld(codec, pin_nid, dev_id,
1627 eld->eld_buffer, &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001628 eld->eld_valid = false;
Wu Fengguang744626d2011-11-16 16:29:47 +08001629 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001630
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001631 update_eld(codec, per_pin, eld, repoll);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001632 mutex_unlock(&per_pin->lock);
Takashi Iwaiae47e2e2020-02-06 17:28:03 +01001633 out:
1634 snd_hda_power_down_pm(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001635}
1636
Takashi Iwai788d4412015-11-12 15:36:13 +01001637/* update ELD and jack state via audio component */
1638static void sync_eld_via_acomp(struct hda_codec *codec,
1639 struct hdmi_spec_per_pin *per_pin)
1640{
Takashi Iwai788d4412015-11-12 15:36:13 +01001641 struct hdmi_spec *spec = codec->spec;
1642 struct hdmi_eld *eld = &spec->temp_eld;
Takashi Iwai788d4412015-11-12 15:36:13 +01001643
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001644 mutex_lock(&per_pin->lock);
Takashi Iwaic64c1432016-03-21 16:07:30 +01001645 eld->monitor_present = false;
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001646 eld->eld_size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
Libin Yang91520852017-01-12 16:04:53 +08001647 per_pin->dev_id, &eld->monitor_present,
1648 eld->eld_buffer, ELD_MAX_SIZE);
Takashi Iwaiadf615a2020-02-06 17:28:04 +01001649 eld->eld_valid = (eld->eld_size > 0);
1650 update_eld(codec, per_pin, eld, 0);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001651 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001652}
1653
Takashi Iwaidb845402020-02-06 17:28:02 +01001654static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Takashi Iwai788d4412015-11-12 15:36:13 +01001655{
1656 struct hda_codec *codec = per_pin->codec;
1657
Takashi Iwaiae47e2e2020-02-06 17:28:03 +01001658 if (!codec_has_acomp(codec))
Takashi Iwaidb845402020-02-06 17:28:02 +01001659 hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwaiae47e2e2020-02-06 17:28:03 +01001660 else
Takashi Iwai788d4412015-11-12 15:36:13 +01001661 sync_eld_via_acomp(codec, per_pin);
Takashi Iwai788d4412015-11-12 15:36:13 +01001662}
1663
Wu Fengguang744626d2011-11-16 16:29:47 +08001664static void hdmi_repoll_eld(struct work_struct *work)
1665{
1666 struct hdmi_spec_per_pin *per_pin =
1667 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
Chris Wilsonaaa23f82018-06-27 07:25:32 +01001668 struct hda_codec *codec = per_pin->codec;
1669 struct hdmi_spec *spec = codec->spec;
Hui Wang8c2e6722019-05-06 22:09:31 +08001670 struct hda_jack_tbl *jack;
1671
Nikhil Mahale5204a052019-11-19 14:17:08 +05301672 jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
1673 per_pin->dev_id);
Hui Wang8c2e6722019-05-06 22:09:31 +08001674 if (jack)
1675 jack->jack_dirty = 1;
Wu Fengguang744626d2011-11-16 16:29:47 +08001676
Wu Fengguangc6e84532011-11-18 16:59:32 -06001677 if (per_pin->repoll_count++ > 6)
1678 per_pin->repoll_count = 0;
1679
Chris Wilsonaaa23f82018-06-27 07:25:32 +01001680 mutex_lock(&spec->pcm_lock);
Takashi Iwaidb845402020-02-06 17:28:02 +01001681 hdmi_present_sense(per_pin, per_pin->repoll_count);
Chris Wilsonaaa23f82018-06-27 07:25:32 +01001682 mutex_unlock(&spec->pcm_lock);
Wu Fengguang744626d2011-11-16 16:29:47 +08001683}
1684
Wu Fengguang079d88c2010-03-08 10:44:23 +08001685static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1686{
1687 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001688 unsigned int caps, config;
1689 int pin_idx;
1690 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001691 int err;
Libin Yang91520852017-01-12 16:04:53 +08001692 int dev_num, i;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001693
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001694 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001695 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1696 return 0;
1697
Libin Yang91520852017-01-12 16:04:53 +08001698 /*
1699 * For DP MST audio, Configuration Default is the same for
1700 * all device entries on the same pin
1701 */
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001702 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001703 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1704 return 0;
1705
Libin Yang91520852017-01-12 16:04:53 +08001706 /*
1707 * To simplify the implementation, malloc all
1708 * the virtual pins in the initialization statically
1709 */
Takashi Iwaicb457222019-11-11 20:09:37 +01001710 if (spec->intel_hsw_fixup) {
Libin Yang91520852017-01-12 16:04:53 +08001711 /*
1712 * On Intel platforms, device entries number is
1713 * changed dynamically. If there is a DP MST
1714 * hub connected, the device entries number is 3.
1715 * Otherwise, it is 1.
1716 * Here we manually set dev_num to 3, so that
1717 * we can initialize all the device entries when
1718 * bootup statically.
1719 */
1720 dev_num = 3;
1721 spec->dev_num = 3;
1722 } else if (spec->dyn_pcm_assign && codec->dp_mst) {
1723 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1724 /*
1725 * spec->dev_num is the maxinum number of device entries
1726 * among all the pins
1727 */
1728 spec->dev_num = (spec->dev_num > dev_num) ?
1729 spec->dev_num : dev_num;
1730 } else {
1731 /*
1732 * If the platform doesn't support DP MST,
1733 * manually set dev_num to 1. This means
1734 * the pin has only one device entry.
1735 */
1736 dev_num = 1;
1737 spec->dev_num = 1;
Libin Yang2bea2412016-01-12 11:13:26 +08001738 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001739
Libin Yang91520852017-01-12 16:04:53 +08001740 for (i = 0; i < dev_num; i++) {
1741 pin_idx = spec->num_pins;
1742 per_pin = snd_array_new(&spec->pins);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001743
Libin Yang91520852017-01-12 16:04:53 +08001744 if (!per_pin)
1745 return -ENOMEM;
1746
1747 if (spec->dyn_pcm_assign) {
1748 per_pin->pcm = NULL;
1749 per_pin->pcm_idx = -1;
1750 } else {
1751 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1752 per_pin->pcm_idx = pin_idx;
1753 }
1754 per_pin->pin_nid = pin_nid;
1755 per_pin->pin_nid_idx = spec->num_nids;
1756 per_pin->dev_id = i;
1757 per_pin->non_pcm = false;
1758 snd_hda_set_dev_select(codec, pin_nid, i);
Libin Yang91520852017-01-12 16:04:53 +08001759 err = hdmi_read_pin_conn(codec, pin_idx);
1760 if (err < 0)
1761 return err;
1762 spec->num_pins++;
1763 }
1764 spec->num_nids++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001765
Stephen Warren384a48d2011-06-01 11:14:21 -06001766 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001767}
1768
Stephen Warren384a48d2011-06-01 11:14:21 -06001769static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001770{
1771 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001772 struct hdmi_spec_per_cvt *per_cvt;
1773 unsigned int chans;
1774 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001775
Stephen Warren384a48d2011-06-01 11:14:21 -06001776 chans = get_wcaps(codec, cvt_nid);
1777 chans = get_wcaps_channels(chans);
1778
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001779 per_cvt = snd_array_new(&spec->cvts);
1780 if (!per_cvt)
1781 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001782
1783 per_cvt->cvt_nid = cvt_nid;
1784 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001785 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001786 per_cvt->channels_max = chans;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05301787 if (chans > spec->chmap.channels_max)
1788 spec->chmap.channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001789 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001790
1791 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1792 &per_cvt->rates,
1793 &per_cvt->formats,
1794 &per_cvt->maxbps);
1795 if (err < 0)
1796 return err;
1797
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001798 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1799 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1800 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001801
1802 return 0;
1803}
1804
1805static int hdmi_parse_codec(struct hda_codec *codec)
1806{
Kai Vehmanen56275032020-07-03 18:38:17 +03001807 hda_nid_t start_nid;
1808 unsigned int caps;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001809 int i, nodes;
1810
Kai Vehmanen56275032020-07-03 18:38:17 +03001811 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &start_nid);
1812 if (!start_nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001813 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08001814 return -EINVAL;
1815 }
1816
Kai Vehmanen56275032020-07-03 18:38:17 +03001817 /*
1818 * hdmi_add_pin() assumes total amount of converters to
1819 * be known, so first discover all converters
1820 */
1821 for (i = 0; i < nodes; i++) {
1822 hda_nid_t nid = start_nid + i;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001823
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001824 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001825
1826 if (!(caps & AC_WCAP_DIGITAL))
1827 continue;
1828
Kai Vehmanen56275032020-07-03 18:38:17 +03001829 if (get_wcaps_type(caps) == AC_WID_AUD_OUT)
Stephen Warren384a48d2011-06-01 11:14:21 -06001830 hdmi_add_cvt(codec, nid);
Kai Vehmanen56275032020-07-03 18:38:17 +03001831 }
1832
1833 /* discover audio pins */
1834 for (i = 0; i < nodes; i++) {
1835 hda_nid_t nid = start_nid + i;
1836
1837 caps = get_wcaps(codec, nid);
1838
1839 if (!(caps & AC_WCAP_DIGITAL))
1840 continue;
1841
1842 if (get_wcaps_type(caps) == AC_WID_PIN)
Wu Fengguang3eaead52010-05-14 16:36:15 +08001843 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001844 }
1845
Wu Fengguang079d88c2010-03-08 10:44:23 +08001846 return 0;
1847}
1848
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001849/*
1850 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001851static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1852{
1853 struct hda_spdif_out *spdif;
1854 bool non_pcm;
1855
1856 mutex_lock(&codec->spdif_mutex);
1857 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
Libin Yang960a5812016-06-16 11:13:25 +08001858 /* Add sanity check to pass klockwork check.
1859 * This should never happen.
1860 */
Wu Boa2f647242020-04-26 21:17:22 +08001861 if (WARN_ON(spdif == NULL)) {
1862 mutex_unlock(&codec->spdif_mutex);
Libin Yang960a5812016-06-16 11:13:25 +08001863 return true;
Wu Boa2f647242020-04-26 21:17:22 +08001864 }
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001865 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1866 mutex_unlock(&codec->spdif_mutex);
1867 return non_pcm;
1868}
1869
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001870/*
1871 * HDMI callbacks
1872 */
1873
1874static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1875 struct hda_codec *codec,
1876 unsigned int stream_tag,
1877 unsigned int format,
1878 struct snd_pcm_substream *substream)
1879{
Stephen Warren384a48d2011-06-01 11:14:21 -06001880 hda_nid_t cvt_nid = hinfo->nid;
1881 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08001882 int pin_idx;
1883 struct hdmi_spec_per_pin *per_pin;
Libin Yangddd621f2015-09-02 14:11:40 +08001884 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001885 bool non_pcm;
Sameer Pujar053b0552019-01-14 23:51:12 +05301886 int pinctl, stripe;
Takashi Iwaif69548f2018-07-12 23:06:51 +02001887 int err = 0;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001888
Libin Yang42b29872015-12-16 13:42:42 +08001889 mutex_lock(&spec->pcm_lock);
1890 pin_idx = hinfo_to_pin_index(codec, hinfo);
1891 if (spec->dyn_pcm_assign && pin_idx < 0) {
1892 /* when dyn_pcm_assign and pcm is not bound to a pin
1893 * skip pin setup and return 0 to make audio playback
1894 * be ongoing
1895 */
Takashi Iwai4846a672016-03-21 12:56:46 +01001896 pin_cvt_fixup(codec, NULL, cvt_nid);
Libin Yang42b29872015-12-16 13:42:42 +08001897 snd_hda_codec_setup_stream(codec, cvt_nid,
1898 stream_tag, 0, format);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001899 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001900 }
1901
1902 if (snd_BUG_ON(pin_idx < 0)) {
Takashi Iwaif69548f2018-07-12 23:06:51 +02001903 err = -EINVAL;
1904 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001905 }
1906 per_pin = get_pin(spec, pin_idx);
Takashi Iwai4846a672016-03-21 12:56:46 +01001907
1908 /* Verify pin:cvt selections to avoid silent audio after S3.
1909 * After S3, the audio driver restores pin:cvt selections
1910 * but this can happen before gfx is ready and such selection
1911 * is overlooked by HW. Thus multiple pins can share a same
1912 * default convertor and mute control will affect each other,
1913 * which can cause a resumed audio playback become silent
1914 * after S3.
1915 */
1916 pin_cvt_fixup(codec, per_pin, 0);
Mengdong Lin2df67422014-03-20 13:01:06 +08001917
Libin Yangddd621f2015-09-02 14:11:40 +08001918 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1919 /* Todo: add DP1.2 MST audio support later */
Takashi Iwai93a9ff12016-03-18 19:45:13 +01001920 if (codec_has_acomp(codec))
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301921 snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
1922 per_pin->dev_id, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08001923
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001924 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001925 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02001926 per_pin->channels = substream->runtime->channels;
1927 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001928
Sameer Pujar053b0552019-01-14 23:51:12 +05301929 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1930 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1931 substream);
1932 snd_hda_codec_write(codec, cvt_nid, 0,
1933 AC_VERB_SET_STRIPE_CONTROL,
1934 stripe);
1935 }
1936
Takashi Iwaib0540872013-09-02 12:33:02 +02001937 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001938 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07001939 if (spec->dyn_pin_out) {
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301940 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1941 per_pin->dev_id);
1942 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -07001943 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301944 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -07001945 AC_VERB_SET_PIN_WIDGET_CONTROL,
1946 pinctl | PIN_OUT);
1947 }
1948
Libin Yang91520852017-01-12 16:04:53 +08001949 /* snd_hda_set_dev_select() has been called before */
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301950 err = spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid,
1951 per_pin->dev_id, stream_tag, format);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001952 unlock:
Libin Yang42b29872015-12-16 13:42:42 +08001953 mutex_unlock(&spec->pcm_lock);
1954 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001955}
1956
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001957static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1958 struct hda_codec *codec,
1959 struct snd_pcm_substream *substream)
1960{
1961 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1962 return 0;
1963}
1964
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001965static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1966 struct hda_codec *codec,
1967 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001968{
1969 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08001970 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06001971 struct hdmi_spec_per_cvt *per_cvt;
1972 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07001973 int pinctl;
Takashi Iwaif69548f2018-07-12 23:06:51 +02001974 int err = 0;
Stephen Warren384a48d2011-06-01 11:14:21 -06001975
Stephen Warren384a48d2011-06-01 11:14:21 -06001976 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08001977 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1978 if (snd_BUG_ON(pcm_idx < 0))
1979 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001980 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001981 if (snd_BUG_ON(cvt_idx < 0))
1982 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001983 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001984
1985 snd_BUG_ON(!per_cvt->assigned);
1986 per_cvt->assigned = 0;
1987 hinfo->nid = 0;
1988
Takashi Iwai6fd739c2019-12-14 18:52:17 +01001989 azx_stream(get_azx_dev(substream))->stripe = 0;
1990
Libin Yang42b29872015-12-16 13:42:42 +08001991 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08001992 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001993 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001994 pin_idx = hinfo_to_pin_index(codec, hinfo);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001995 if (spec->dyn_pcm_assign && pin_idx < 0)
1996 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001997
1998 if (snd_BUG_ON(pin_idx < 0)) {
Takashi Iwaif69548f2018-07-12 23:06:51 +02001999 err = -EINVAL;
2000 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08002001 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002002 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002003
Stephen Warren75fae112014-01-30 11:52:16 -07002004 if (spec->dyn_pin_out) {
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05302005 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2006 per_pin->dev_id);
Stephen Warren75fae112014-01-30 11:52:16 -07002007 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2008 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2009 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2010 AC_VERB_SET_PIN_WIDGET_CONTROL,
2011 pinctl & ~PIN_OUT);
2012 }
2013
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002014 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002015 per_pin->chmap_set = false;
2016 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002017
2018 per_pin->setup = false;
2019 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002020 mutex_unlock(&per_pin->lock);
Takashi Iwaif69548f2018-07-12 23:06:51 +02002021 unlock:
Libin Yang42b29872015-12-16 13:42:42 +08002022 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002023 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002024
Takashi Iwaif69548f2018-07-12 23:06:51 +02002025 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002026}
2027
2028static const struct hda_pcm_ops generic_ops = {
2029 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002030 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002031 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002032 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002033};
2034
Subhransu S. Prusty44fde3b2016-04-04 19:23:54 +05302035static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2036{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08002037 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Subhransu S. Prusty44fde3b2016-04-04 19:23:54 +05302038 struct hdmi_spec *spec = codec->spec;
2039 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2040
2041 if (!per_pin)
2042 return 0;
2043
2044 return per_pin->sink_eld.info.spk_alloc;
2045}
2046
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302047static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2048 unsigned char *chmap)
2049{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08002050 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302051 struct hdmi_spec *spec = codec->spec;
2052 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2053
2054 /* chmap is already set to 0 in caller */
2055 if (!per_pin)
2056 return;
2057
2058 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2059}
2060
2061static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2062 unsigned char *chmap, int prepared)
2063{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08002064 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302065 struct hdmi_spec *spec = codec->spec;
2066 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2067
Libin Yanged0739b2016-04-18 09:16:28 +08002068 if (!per_pin)
2069 return;
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302070 mutex_lock(&per_pin->lock);
2071 per_pin->chmap_set = true;
2072 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2073 if (prepared)
2074 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2075 mutex_unlock(&per_pin->lock);
2076}
2077
2078static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2079{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08002080 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302081 struct hdmi_spec *spec = codec->spec;
2082 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2083
2084 return per_pin ? true:false;
2085}
2086
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002087static int generic_hdmi_build_pcms(struct hda_codec *codec)
2088{
2089 struct hdmi_spec *spec = codec->spec;
Kai Vehmanen2a2edfb2019-10-29 15:40:09 +02002090 int idx, pcm_num;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002091
Libin Yang91520852017-01-12 16:04:53 +08002092 /*
2093 * for non-mst mode, pcm number is the same as before
Kai Vehmanen2a2edfb2019-10-29 15:40:09 +02002094 * for DP MST mode without extra PCM, pcm number is same
2095 * for DP MST mode with extra PCMs, pcm number is
2096 * (nid number + dev_num - 1)
2097 * dev_num is the device entry number in a pin
Libin Yang91520852017-01-12 16:04:53 +08002098 */
Kai Vehmanen2a2edfb2019-10-29 15:40:09 +02002099
2100 if (codec->mst_no_extra_pcms)
2101 pcm_num = spec->num_nids;
2102 else
2103 pcm_num = spec->num_nids + spec->dev_num - 1;
2104
2105 codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num);
2106
2107 for (idx = 0; idx < pcm_num; idx++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002108 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002109 struct hda_pcm_stream *pstr;
2110
Libin Yang91520852017-01-12 16:04:53 +08002111 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002112 if (!info)
2113 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002114
Libin Yang91520852017-01-12 16:04:53 +08002115 spec->pcm_rec[idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002116 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002117 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002118 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002119
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002120 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002121 pstr->substreams = 1;
2122 pstr->ops = generic_ops;
Libin Yang91520852017-01-12 16:04:53 +08002123 /* pcm number is less than 16 */
2124 if (spec->pcm_used >= 16)
2125 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06002126 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002127 }
2128
2129 return 0;
2130}
2131
Libin Yang25e4abb2016-01-12 11:13:27 +08002132static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01002133{
Libin Yang25e4abb2016-01-12 11:13:27 +08002134 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01002135
Libin Yang25e4abb2016-01-12 11:13:27 +08002136 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002137}
2138
Takashi Iwaidb845402020-02-06 17:28:02 +01002139static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
Takashi Iwai788d4412015-11-12 15:36:13 +01002140{
Takashi Iwaidb845402020-02-06 17:28:02 +01002141 char hdmi_str[32] = "HDMI/DP";
2142 struct hdmi_spec *spec = codec->spec;
2143 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pcm_idx);
Takashi Iwai788d4412015-11-12 15:36:13 +01002144 struct snd_jack *jack;
Takashi Iwaidb845402020-02-06 17:28:02 +01002145 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai788d4412015-11-12 15:36:13 +01002146 int err;
2147
Takashi Iwaidb845402020-02-06 17:28:02 +01002148 if (pcmdev > 0)
2149 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2150 if (!spec->dyn_pcm_assign &&
2151 !is_jack_detectable(codec, per_pin->pin_nid))
2152 strncat(hdmi_str, " Phantom",
2153 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2154
2155 err = snd_jack_new(codec->card, hdmi_str, SND_JACK_AVOUT, &jack,
Takashi Iwai788d4412015-11-12 15:36:13 +01002156 true, false);
2157 if (err < 0)
2158 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002159
2160 spec->pcm_rec[pcm_idx].jack = jack;
2161 jack->private_data = &spec->pcm_rec[pcm_idx];
2162 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01002163 return 0;
2164}
2165
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002166static int generic_hdmi_build_controls(struct hda_codec *codec)
2167{
2168 struct hdmi_spec *spec = codec->spec;
Wang YanQing1f7f51a2017-09-03 21:18:49 +08002169 int dev, err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002170 int pin_idx, pcm_idx;
2171
Libin Yang25e4abb2016-01-12 11:13:27 +08002172 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwai8a7d6002018-04-25 16:19:13 +02002173 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2174 /* no PCM: mark this for skipping permanently */
2175 set_bit(pcm_idx, &spec->pcm_bitmap);
2176 continue;
2177 }
2178
Libin Yang25e4abb2016-01-12 11:13:27 +08002179 err = generic_hdmi_build_jack(codec, pcm_idx);
2180 if (err < 0)
2181 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08002182
2183 /* create the spdif for each pcm
2184 * pin will be bound when monitor is connected
2185 */
2186 if (spec->dyn_pcm_assign)
2187 err = snd_hda_create_dig_out_ctls(codec,
2188 0, spec->cvt_nids[0],
2189 HDA_PCM_TYPE_HDMI);
2190 else {
2191 struct hdmi_spec_per_pin *per_pin =
2192 get_pin(spec, pcm_idx);
2193 err = snd_hda_create_dig_out_ctls(codec,
2194 per_pin->pin_nid,
2195 per_pin->mux_nids[0],
2196 HDA_PCM_TYPE_HDMI);
2197 }
2198 if (err < 0)
2199 return err;
2200 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002201
Wang YanQing1f7f51a2017-09-03 21:18:49 +08002202 dev = get_pcm_rec(spec, pcm_idx)->device;
2203 if (dev != SNDRV_PCM_INVALID_DEVICE) {
2204 /* add control for ELD Bytes */
2205 err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2206 if (err < 0)
2207 return err;
2208 }
Libin Yang25e4abb2016-01-12 11:13:27 +08002209 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002210
Stephen Warren384a48d2011-06-01 11:14:21 -06002211 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002212 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Kai Vehmanenca762822020-04-28 15:38:36 +03002213 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002214
Kai Vehmanenca762822020-04-28 15:38:36 +03002215 pin_eld->eld_valid = false;
Takashi Iwai82b1d732011-12-20 15:53:07 +01002216 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002217 }
2218
Takashi Iwaid45e6882012-07-31 11:36:00 +02002219 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002220 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002221 struct hda_pcm *pcm;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002222
Libin Yang022f3442016-02-03 10:48:34 +08002223 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002224 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002225 break;
Subhransu S. Prusty2f6e8a82016-03-04 19:59:51 +05302226 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002227 if (err < 0)
2228 return err;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002229 }
2230
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002231 return 0;
2232}
2233
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002234static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2235{
2236 struct hdmi_spec *spec = codec->spec;
2237 int pin_idx;
2238
2239 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002240 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002241
2242 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002243 mutex_init(&per_pin->lock);
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002244 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002245 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002246 }
2247 return 0;
2248}
2249
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002250static int generic_hdmi_init(struct hda_codec *codec)
2251{
2252 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002253 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002254
Takashi Iwai302d5a82019-10-29 21:41:20 +01002255 mutex_lock(&spec->bind_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002256 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002257 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002258 hda_nid_t pin_nid = per_pin->pin_nid;
Libin Yang91520852017-01-12 16:04:53 +08002259 int dev_id = per_pin->dev_id;
Stephen Warren384a48d2011-06-01 11:14:21 -06002260
Libin Yang91520852017-01-12 16:04:53 +08002261 snd_hda_set_dev_select(codec, pin_nid, dev_id);
Stephen Warren384a48d2011-06-01 11:14:21 -06002262 hdmi_init_pin(codec, pin_nid);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002263 if (codec_has_acomp(codec))
2264 continue;
Takashi Iwaidb845402020-02-06 17:28:02 +01002265 snd_hda_jack_detect_enable_callback_mst(codec, pin_nid, dev_id,
2266 jack_callback);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002267 }
Takashi Iwai302d5a82019-10-29 21:41:20 +01002268 mutex_unlock(&spec->bind_lock);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002269 return 0;
2270}
2271
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002272static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2273{
2274 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2275 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002276}
2277
2278static void hdmi_array_free(struct hdmi_spec *spec)
2279{
2280 snd_array_free(&spec->pins);
2281 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002282}
2283
Takashi Iwaia6866322016-03-21 12:18:33 +01002284static void generic_spec_free(struct hda_codec *codec)
2285{
2286 struct hdmi_spec *spec = codec->spec;
2287
2288 if (spec) {
2289 hdmi_array_free(spec);
2290 kfree(spec);
2291 codec->spec = NULL;
2292 }
2293 codec->dp_mst = false;
2294}
2295
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002296static void generic_hdmi_free(struct hda_codec *codec)
2297{
2298 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002299 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002300
Takashi Iwaiade49db2018-07-11 18:05:52 +02002301 if (spec->acomp_registered) {
2302 snd_hdac_acomp_exit(&codec->bus->core);
2303 } else if (codec_has_acomp(codec)) {
Takashi Iwaia57942b2018-07-11 16:23:16 +02002304 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
Takashi Iwai4914da22019-07-16 08:56:51 +02002305 }
Takashi Iwai10a95942019-11-22 14:26:24 +01002306 codec->relaxed_resume = 0;
David Henningsson25adc132015-08-19 10:48:58 +02002307
Stephen Warren384a48d2011-06-01 11:14:21 -06002308 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002309 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002310 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002311 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002312 }
2313
2314 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2315 if (spec->pcm_rec[pcm_idx].jack == NULL)
2316 continue;
2317 if (spec->dyn_pcm_assign)
2318 snd_device_free(codec->card,
2319 spec->pcm_rec[pcm_idx].jack);
2320 else
2321 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002322 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002323
Takashi Iwaia6866322016-03-21 12:18:33 +01002324 generic_spec_free(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002325}
2326
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002327#ifdef CONFIG_PM
2328static int generic_hdmi_resume(struct hda_codec *codec)
2329{
2330 struct hdmi_spec *spec = codec->spec;
2331 int pin_idx;
2332
Pierre Ossmana2833682014-06-18 21:48:09 +02002333 codec->patch_ops.init(codec);
Takashi Iwai1a462be2020-01-09 10:01:04 +01002334 snd_hda_regmap_sync(codec);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002335
2336 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2337 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2338 hdmi_present_sense(per_pin, 1);
2339 }
2340 return 0;
2341}
2342#endif
2343
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002344static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002345 .init = generic_hdmi_init,
2346 .free = generic_hdmi_free,
2347 .build_pcms = generic_hdmi_build_pcms,
2348 .build_controls = generic_hdmi_build_controls,
2349 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002350#ifdef CONFIG_PM
2351 .resume = generic_hdmi_resume,
2352#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002353};
2354
Anssi Hannula307229d2013-10-24 21:10:34 +03002355static const struct hdmi_ops generic_standard_hdmi_ops = {
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05302356 .pin_get_eld = hdmi_pin_get_eld,
Anssi Hannula307229d2013-10-24 21:10:34 +03002357 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2358 .pin_hbr_setup = hdmi_pin_hbr_setup,
2359 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302360};
2361
Takashi Iwaia6866322016-03-21 12:18:33 +01002362/* allocate codec->spec and assign/initialize generic parser ops */
2363static int alloc_generic_hdmi(struct hda_codec *codec)
2364{
2365 struct hdmi_spec *spec;
2366
2367 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2368 if (!spec)
2369 return -ENOMEM;
2370
Takashi Iwaiade49db2018-07-11 18:05:52 +02002371 spec->codec = codec;
Takashi Iwaia6866322016-03-21 12:18:33 +01002372 spec->ops = generic_standard_hdmi_ops;
Libin Yang91520852017-01-12 16:04:53 +08002373 spec->dev_num = 1; /* initialize to 1 */
Takashi Iwaia6866322016-03-21 12:18:33 +01002374 mutex_init(&spec->pcm_lock);
Takashi Iwai302d5a82019-10-29 21:41:20 +01002375 mutex_init(&spec->bind_lock);
Takashi Iwaia6866322016-03-21 12:18:33 +01002376 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2377
2378 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2379 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2380 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
Subhransu S. Prusty44fde3b2016-04-04 19:23:54 +05302381 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
Takashi Iwaia6866322016-03-21 12:18:33 +01002382
2383 codec->spec = spec;
2384 hdmi_array_init(spec, 4);
2385
2386 codec->patch_ops = generic_hdmi_patch_ops;
2387
2388 return 0;
2389}
2390
2391/* generic HDMI parser */
2392static int patch_generic_hdmi(struct hda_codec *codec)
2393{
2394 int err;
2395
2396 err = alloc_generic_hdmi(codec);
2397 if (err < 0)
2398 return err;
2399
2400 err = hdmi_parse_codec(codec);
2401 if (err < 0) {
2402 generic_spec_free(codec);
2403 return err;
2404 }
2405
2406 generic_hdmi_init_per_pins(codec);
2407 return 0;
2408}
2409
2410/*
Takashi Iwaiade49db2018-07-11 18:05:52 +02002411 * generic audio component binding
2412 */
2413
2414/* turn on / off the unsol event jack detection dynamically */
2415static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
Nikhil Mahale5204a052019-11-19 14:17:08 +05302416 int dev_id, bool use_acomp)
Takashi Iwaiade49db2018-07-11 18:05:52 +02002417{
2418 struct hda_jack_tbl *tbl;
2419
Nikhil Mahale5204a052019-11-19 14:17:08 +05302420 tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002421 if (tbl) {
2422 /* clear unsol even if component notifier is used, or re-enable
2423 * if notifier is cleared
2424 */
2425 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2426 snd_hda_codec_write_cache(codec, nid, 0,
2427 AC_VERB_SET_UNSOLICITED_ENABLE, val);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002428 }
2429}
2430
2431/* set up / clear component notifier dynamically */
2432static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2433 bool use_acomp)
2434{
2435 struct hdmi_spec *spec;
2436 int i;
2437
2438 spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
Takashi Iwai302d5a82019-10-29 21:41:20 +01002439 mutex_lock(&spec->bind_lock);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002440 spec->use_acomp_notifier = use_acomp;
2441 spec->codec->relaxed_resume = use_acomp;
2442 /* reprogram each jack detection logic depending on the notifier */
Takashi Iwaidb845402020-02-06 17:28:02 +01002443 for (i = 0; i < spec->num_pins; i++)
2444 reprogram_jack_detect(spec->codec,
2445 get_pin(spec, i)->pin_nid,
2446 get_pin(spec, i)->dev_id,
2447 use_acomp);
Takashi Iwai302d5a82019-10-29 21:41:20 +01002448 mutex_unlock(&spec->bind_lock);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002449}
2450
2451/* enable / disable the notifier via master bind / unbind */
2452static int generic_acomp_master_bind(struct device *dev,
2453 struct drm_audio_component *acomp)
2454{
2455 generic_acomp_notifier_set(acomp, true);
2456 return 0;
2457}
2458
2459static void generic_acomp_master_unbind(struct device *dev,
2460 struct drm_audio_component *acomp)
2461{
2462 generic_acomp_notifier_set(acomp, false);
2463}
2464
2465/* check whether both HD-audio and DRM PCI devices belong to the same bus */
2466static int match_bound_vga(struct device *dev, int subtype, void *data)
2467{
2468 struct hdac_bus *bus = data;
2469 struct pci_dev *pci, *master;
2470
2471 if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2472 return 0;
2473 master = to_pci_dev(bus->dev);
2474 pci = to_pci_dev(dev);
2475 return master->bus == pci->bus;
2476}
2477
2478/* audio component notifier for AMD/Nvidia HDMI codecs */
2479static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2480{
2481 struct hda_codec *codec = audio_ptr;
2482 struct hdmi_spec *spec = codec->spec;
2483 hda_nid_t pin_nid = spec->port2pin(codec, port);
2484
2485 if (!pin_nid)
2486 return;
2487 if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2488 return;
2489 /* skip notification during system suspend (but not in runtime PM);
2490 * the state will be updated at resume
2491 */
2492 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2493 return;
2494 /* ditto during suspend/resume process itself */
2495 if (snd_hdac_is_in_pm(&codec->core))
2496 return;
2497
2498 check_presence_and_report(codec, pin_nid, dev_id);
2499}
2500
2501/* set up the private drm_audio_ops from the template */
2502static void setup_drm_audio_ops(struct hda_codec *codec,
2503 const struct drm_audio_component_audio_ops *ops)
2504{
2505 struct hdmi_spec *spec = codec->spec;
2506
2507 spec->drm_audio_ops.audio_ptr = codec;
2508 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2509 * will call pin_eld_notify with using audio_ptr pointer
2510 * We need make sure audio_ptr is really setup
2511 */
2512 wmb();
2513 spec->drm_audio_ops.pin2port = ops->pin2port;
2514 spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2515 spec->drm_audio_ops.master_bind = ops->master_bind;
2516 spec->drm_audio_ops.master_unbind = ops->master_unbind;
2517}
2518
2519/* initialize the generic HDMI audio component */
2520static void generic_acomp_init(struct hda_codec *codec,
2521 const struct drm_audio_component_audio_ops *ops,
2522 int (*port2pin)(struct hda_codec *, int))
2523{
2524 struct hdmi_spec *spec = codec->spec;
2525
Takashi Iwaib3923502020-04-15 18:25:23 +02002526 if (!enable_acomp) {
2527 codec_info(codec, "audio component disabled by module option\n");
2528 return;
2529 }
2530
Takashi Iwaiade49db2018-07-11 18:05:52 +02002531 spec->port2pin = port2pin;
2532 setup_drm_audio_ops(codec, ops);
2533 if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
Takashi Iwaidd23e1d2019-08-27 16:37:50 +02002534 match_bound_vga, 0)) {
Takashi Iwaiade49db2018-07-11 18:05:52 +02002535 spec->acomp_registered = true;
Takashi Iwaidd23e1d2019-08-27 16:37:50 +02002536 codec->bus->keep_power = 0;
2537 }
Takashi Iwaiade49db2018-07-11 18:05:52 +02002538}
2539
2540/*
Takashi Iwaia6866322016-03-21 12:18:33 +01002541 * Intel codec parsers and helpers
2542 */
2543
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002544#define INTEL_GET_VENDOR_VERB 0xf81
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002545#define INTEL_SET_VENDOR_VERB 0x781
2546#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2547#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002548
2549static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002550 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002551{
2552 unsigned int vendor_param;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302553 struct hdmi_spec *spec = codec->spec;
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002554
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302555 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002556 INTEL_GET_VENDOR_VERB, 0);
2557 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2558 return;
2559
2560 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302561 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002562 INTEL_SET_VENDOR_VERB, vendor_param);
2563 if (vendor_param == -1)
2564 return;
2565
Takashi Iwai17df3f52013-05-08 08:09:34 +02002566 if (update_tree)
2567 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002568}
2569
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002570static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2571{
2572 unsigned int vendor_param;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302573 struct hdmi_spec *spec = codec->spec;
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002574
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302575 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002576 INTEL_GET_VENDOR_VERB, 0);
2577 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2578 return;
2579
2580 /* enable DP1.2 mode */
2581 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002582 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302583 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002584 INTEL_SET_VENDOR_VERB, vendor_param);
2585}
2586
Takashi Iwai17df3f52013-05-08 08:09:34 +02002587/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2588 * Otherwise you may get severe h/w communication errors.
2589 */
2590static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2591 unsigned int power_state)
2592{
2593 if (power_state == AC_PWRST_D0) {
2594 intel_haswell_enable_all_pins(codec, false);
2595 intel_haswell_fixup_enable_dp12(codec);
2596 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002597
Takashi Iwai17df3f52013-05-08 08:09:34 +02002598 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2599 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2600}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002601
Takashi Iwaia57942b2018-07-11 16:23:16 +02002602/* There is a fixed mapping between audio pin node and display port.
2603 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2604 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2605 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2606 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2607 *
2608 * on VLV, ILK:
2609 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2610 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2611 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2612 */
2613static int intel_base_nid(struct hda_codec *codec)
2614{
2615 switch (codec->core.vendor_id) {
2616 case 0x80860054: /* ILK */
2617 case 0x80862804: /* ILK */
2618 case 0x80862882: /* VLV */
2619 return 4;
2620 default:
2621 return 5;
2622 }
2623}
2624
2625static int intel_pin2port(void *audio_ptr, int pin_nid)
2626{
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002627 struct hda_codec *codec = audio_ptr;
2628 struct hdmi_spec *spec = codec->spec;
2629 int base_nid, i;
Takashi Iwaia57942b2018-07-11 16:23:16 +02002630
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002631 if (!spec->port_num) {
2632 base_nid = intel_base_nid(codec);
2633 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2634 return -1;
Kai Vehmanenc8e3eb92019-11-15 14:44:48 +02002635 return pin_nid - base_nid + 1;
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002636 }
2637
2638 /*
2639 * looking for the pin number in the mapping table and return
2640 * the index which indicate the port number
2641 */
2642 for (i = 0; i < spec->port_num; i++) {
2643 if (pin_nid == spec->port_map[i])
Kai Vehmanend577cf72019-11-15 14:44:47 +02002644 return i;
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002645 }
2646
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002647 codec_info(codec, "Can't find the HDMI/DP port for pin %d\n", pin_nid);
2648 return -1;
Takashi Iwaia57942b2018-07-11 16:23:16 +02002649}
2650
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002651static int intel_port2pin(struct hda_codec *codec, int port)
2652{
2653 struct hdmi_spec *spec = codec->spec;
2654
2655 if (!spec->port_num) {
2656 /* we assume only from port-B to port-D */
2657 if (port < 1 || port > 3)
2658 return 0;
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002659 return port + intel_base_nid(codec) - 1;
2660 }
2661
Kai Vehmanend577cf72019-11-15 14:44:47 +02002662 if (port < 0 || port >= spec->port_num)
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002663 return 0;
Kai Vehmanend577cf72019-11-15 14:44:47 +02002664 return spec->port_map[port];
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002665}
2666
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07002667static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
David Henningsson25adc132015-08-19 10:48:58 +02002668{
2669 struct hda_codec *codec = audio_ptr;
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002670 int pin_nid;
Libin Yang91520852017-01-12 16:04:53 +08002671 int dev_id = pipe;
David Henningsson25adc132015-08-19 10:48:58 +02002672
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002673 pin_nid = intel_port2pin(codec, port);
2674 if (!pin_nid)
Takashi Iwai4f8e4f32016-03-10 12:02:49 +01002675 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002676 /* skip notification during system suspend (but not in runtime PM);
2677 * the state will be updated at resume
2678 */
2679 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2680 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002681 /* ditto during suspend/resume process itself */
Takashi Iwaifeb20fa2018-06-27 09:03:51 +02002682 if (snd_hdac_is_in_pm(&codec->core))
Takashi Iwaieb399d32015-11-27 14:53:35 +01002683 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002684
Takashi Iwaibb03ed22016-04-21 16:39:17 +02002685 snd_hdac_i915_set_bclk(&codec->bus->core);
Libin Yang91520852017-01-12 16:04:53 +08002686 check_presence_and_report(codec, pin_nid, dev_id);
David Henningsson25adc132015-08-19 10:48:58 +02002687}
2688
Takashi Iwaiade49db2018-07-11 18:05:52 +02002689static const struct drm_audio_component_audio_ops intel_audio_ops = {
2690 .pin2port = intel_pin2port,
2691 .pin_eld_notify = intel_pin_eld_notify,
2692};
2693
Takashi Iwaia6866322016-03-21 12:18:33 +01002694/* register i915 component pin_eld_notify callback */
2695static void register_i915_notifier(struct hda_codec *codec)
2696{
2697 struct hdmi_spec *spec = codec->spec;
2698
2699 spec->use_acomp_notifier = true;
Takashi Iwaiade49db2018-07-11 18:05:52 +02002700 spec->port2pin = intel_port2pin;
2701 setup_drm_audio_ops(codec, &intel_audio_ops);
Takashi Iwaia57942b2018-07-11 16:23:16 +02002702 snd_hdac_acomp_register_notifier(&codec->bus->core,
Takashi Iwai82887c02018-07-11 15:48:18 +02002703 &spec->drm_audio_ops);
Takashi Iwai4914da22019-07-16 08:56:51 +02002704 /* no need for forcible resume for jack check thanks to notifier */
2705 codec->relaxed_resume = 1;
Takashi Iwaia6866322016-03-21 12:18:33 +01002706}
2707
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002708/* setup_stream ops override for HSW+ */
2709static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05302710 hda_nid_t pin_nid, int dev_id, u32 stream_tag,
2711 int format)
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002712{
2713 haswell_verify_D0(codec, cvt_nid, pin_nid);
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05302714 return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
2715 stream_tag, format);
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002716}
2717
Takashi Iwai4846a672016-03-21 12:56:46 +01002718/* pin_cvt_fixup ops override for HSW+ and VLV+ */
2719static void i915_pin_cvt_fixup(struct hda_codec *codec,
2720 struct hdmi_spec_per_pin *per_pin,
2721 hda_nid_t cvt_nid)
2722{
2723 if (per_pin) {
Libin Yang91520852017-01-12 16:04:53 +08002724 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2725 per_pin->dev_id);
Takashi Iwai4846a672016-03-21 12:56:46 +01002726 intel_verify_pin_cvt_connect(codec, per_pin);
2727 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
Libin Yang91520852017-01-12 16:04:53 +08002728 per_pin->dev_id, per_pin->mux_idx);
Takashi Iwai4846a672016-03-21 12:56:46 +01002729 } else {
Libin Yang91520852017-01-12 16:04:53 +08002730 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
Takashi Iwai4846a672016-03-21 12:56:46 +01002731 }
2732}
2733
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002734/* precondition and allocation for Intel codecs */
2735static int alloc_intel_hdmi(struct hda_codec *codec)
2736{
Takashi Iwaif2dbe872019-08-13 17:11:28 +02002737 int err;
2738
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002739 /* requires i915 binding */
2740 if (!codec->bus->core.audio_component) {
2741 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
Takashi Iwaifdd49c52018-06-27 09:54:46 +02002742 /* set probe_id here to prevent generic fallback binding */
2743 codec->probe_id = HDA_CODEC_ID_SKIP_PROBE;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002744 return -ENODEV;
2745 }
2746
Takashi Iwaif2dbe872019-08-13 17:11:28 +02002747 err = alloc_generic_hdmi(codec);
2748 if (err < 0)
2749 return err;
2750 /* no need to handle unsol events */
2751 codec->patch_ops.unsol_event = NULL;
2752 return 0;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002753}
2754
2755/* parse and post-process for Intel codecs */
2756static int parse_intel_hdmi(struct hda_codec *codec)
2757{
Kai Vehmanen2928fa02020-01-20 18:01:17 +02002758 int err, retries = 3;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002759
Kai Vehmanen2928fa02020-01-20 18:01:17 +02002760 do {
2761 err = hdmi_parse_codec(codec);
2762 } while (err < 0 && retries--);
2763
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002764 if (err < 0) {
2765 generic_spec_free(codec);
2766 return err;
2767 }
2768
2769 generic_hdmi_init_per_pins(codec);
2770 register_i915_notifier(codec);
2771 return 0;
2772}
2773
Takashi Iwaia6866322016-03-21 12:18:33 +01002774/* Intel Haswell and onwards; audio component with eld notifier */
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002775static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2776 const int *port_map, int port_num)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002777{
2778 struct hdmi_spec *spec;
Takashi Iwaia6866322016-03-21 12:18:33 +01002779 int err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002780
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002781 err = alloc_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002782 if (err < 0)
2783 return err;
2784 spec = codec->spec;
Libin Yang91520852017-01-12 16:04:53 +08002785 codec->dp_mst = true;
2786 spec->dyn_pcm_assign = true;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302787 spec->vendor_nid = vendor_nid;
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002788 spec->port_map = port_map;
2789 spec->port_num = port_num;
Takashi Iwaicb457222019-11-11 20:09:37 +01002790 spec->intel_hsw_fixup = true;
Takashi Iwaia6866322016-03-21 12:18:33 +01002791
2792 intel_haswell_enable_all_pins(codec, true);
2793 intel_haswell_fixup_enable_dp12(codec);
2794
Takashi Iwai46594d32018-12-09 10:10:19 +01002795 codec->display_power_control = 1;
Takashi Iwaia6866322016-03-21 12:18:33 +01002796
2797 codec->patch_ops.set_power_state = haswell_set_power_state;
Takashi Iwaia6866322016-03-21 12:18:33 +01002798 codec->depop_delay = 0;
2799 codec->auto_runtime_pm = 1;
2800
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002801 spec->ops.setup_stream = i915_hsw_setup_stream;
Takashi Iwai4846a672016-03-21 12:56:46 +01002802 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002803
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002804 return parse_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002805}
2806
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302807static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2808{
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002809 return intel_hsw_common_init(codec, 0x08, NULL, 0);
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302810}
2811
2812static int patch_i915_glk_hdmi(struct hda_codec *codec)
2813{
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002814 return intel_hsw_common_init(codec, 0x0b, NULL, 0);
2815}
2816
2817static int patch_i915_icl_hdmi(struct hda_codec *codec)
2818{
2819 /*
2820 * pin to port mapping table where the value indicate the pin number and
Kai Vehmanend577cf72019-11-15 14:44:47 +02002821 * the index indicate the port number.
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002822 */
Kai Vehmanend577cf72019-11-15 14:44:47 +02002823 static const int map[] = {0x0, 0x4, 0x6, 0x8, 0xa, 0xb};
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002824
2825 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302826}
2827
Kai Vehmanen9a11ba72019-11-05 18:10:53 +02002828static int patch_i915_tgl_hdmi(struct hda_codec *codec)
2829{
2830 /*
2831 * pin to port mapping table where the value indicate the pin number and
Kai Vehmanend577cf72019-11-15 14:44:47 +02002832 * the index indicate the port number.
Kai Vehmanen9a11ba72019-11-05 18:10:53 +02002833 */
2834 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
2835
2836 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2837}
2838
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002839/* Intel Baytrail and Braswell; with eld notifier */
Takashi Iwaia6866322016-03-21 12:18:33 +01002840static int patch_i915_byt_hdmi(struct hda_codec *codec)
2841{
2842 struct hdmi_spec *spec;
2843 int err;
2844
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002845 err = alloc_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002846 if (err < 0)
2847 return err;
2848 spec = codec->spec;
2849
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002850 /* For Valleyview/Cherryview, only the display codec is in the display
2851 * power well and can use link_power ops to request/release the power.
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002852 */
Takashi Iwai029d92c2018-12-08 17:31:49 +01002853 codec->display_power_control = 1;
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002854
Takashi Iwaia6866322016-03-21 12:18:33 +01002855 codec->depop_delay = 0;
2856 codec->auto_runtime_pm = 1;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002857
Takashi Iwai4846a672016-03-21 12:56:46 +01002858 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2859
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002860 return parse_intel_hdmi(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002861}
2862
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002863/* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
Takashi Iwaie85015a32016-03-21 13:56:19 +01002864static int patch_i915_cpt_hdmi(struct hda_codec *codec)
2865{
Takashi Iwaie85015a32016-03-21 13:56:19 +01002866 int err;
2867
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002868 err = alloc_intel_hdmi(codec);
Takashi Iwaie85015a32016-03-21 13:56:19 +01002869 if (err < 0)
2870 return err;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002871 return parse_intel_hdmi(codec);
Takashi Iwaie85015a32016-03-21 13:56:19 +01002872}
2873
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002874/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002875 * Shared non-generic implementations
2876 */
2877
2878static int simple_playback_build_pcms(struct hda_codec *codec)
2879{
2880 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002881 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002882 unsigned int chans;
2883 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002884 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002885
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002886 per_cvt = get_cvt(spec, 0);
2887 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002888 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002889
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002890 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002891 if (!info)
2892 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002893 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002894 info->pcm_type = HDA_PCM_TYPE_HDMI;
2895 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2896 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002897 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002898 if (pstr->channels_max <= 2 && chans && chans <= 16)
2899 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002900
2901 return 0;
2902}
2903
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002904/* unsolicited event for jack sensing */
2905static void simple_hdmi_unsol_event(struct hda_codec *codec,
2906 unsigned int res)
2907{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002908 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002909 snd_hda_jack_report_sync(codec);
2910}
2911
2912/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2913 * as long as spec->pins[] is set correctly
2914 */
2915#define simple_hdmi_build_jack generic_hdmi_build_jack
2916
Stephen Warren3aaf8982011-06-01 11:14:19 -06002917static int simple_playback_build_controls(struct hda_codec *codec)
2918{
2919 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002920 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002921 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002922
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002923 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002924 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2925 per_cvt->cvt_nid,
2926 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002927 if (err < 0)
2928 return err;
2929 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002930}
2931
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002932static int simple_playback_init(struct hda_codec *codec)
2933{
2934 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002935 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2936 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002937
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002938 snd_hda_codec_write(codec, pin, 0,
2939 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2940 /* some codecs require to unmute the pin */
2941 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2942 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2943 AMP_OUT_UNMUTE);
Nikhil Mahale5204a052019-11-19 14:17:08 +05302944 snd_hda_jack_detect_enable(codec, pin, per_pin->dev_id);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002945 return 0;
2946}
2947
Stephen Warren3aaf8982011-06-01 11:14:19 -06002948static void simple_playback_free(struct hda_codec *codec)
2949{
2950 struct hdmi_spec *spec = codec->spec;
2951
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002952 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002953 kfree(spec);
2954}
2955
2956/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002957 * Nvidia specific implementations
2958 */
2959
2960#define Nv_VERB_SET_Channel_Allocation 0xF79
2961#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2962#define Nv_VERB_SET_Audio_Protection_On 0xF98
2963#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2964
2965#define nvhdmi_master_con_nid_7x 0x04
2966#define nvhdmi_master_pin_nid_7x 0x05
2967
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002968static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002969 /*front, rear, clfe, rear_surr */
2970 0x6, 0x8, 0xa, 0xc,
2971};
2972
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002973static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2974 /* set audio protect on */
2975 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2976 /* enable digital output on pin widget */
2977 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2978 {} /* terminator */
2979};
2980
2981static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002982 /* set audio protect on */
2983 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2984 /* enable digital output on pin widget */
2985 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2986 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2987 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2988 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2989 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2990 {} /* terminator */
2991};
2992
2993#ifdef LIMITED_RATE_FMT_SUPPORT
2994/* support only the safe format and rate */
2995#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2996#define SUPPORTED_MAXBPS 16
2997#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2998#else
2999/* support all rates and formats */
3000#define SUPPORTED_RATES \
3001 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3002 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3003 SNDRV_PCM_RATE_192000)
3004#define SUPPORTED_MAXBPS 24
3005#define SUPPORTED_FORMATS \
3006 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3007#endif
3008
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003009static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003010{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003011 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3012 return 0;
3013}
3014
3015static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3016{
3017 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003018 return 0;
3019}
3020
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003021static const unsigned int channels_2_6_8[] = {
Nitin Daga393004b2011-01-10 21:49:31 +05303022 2, 6, 8
3023};
3024
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003025static const unsigned int channels_2_8[] = {
Nitin Daga393004b2011-01-10 21:49:31 +05303026 2, 8
3027};
3028
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003029static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
Nitin Daga393004b2011-01-10 21:49:31 +05303030 .count = ARRAY_SIZE(channels_2_6_8),
3031 .list = channels_2_6_8,
3032 .mask = 0,
3033};
3034
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003035static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
Nitin Daga393004b2011-01-10 21:49:31 +05303036 .count = ARRAY_SIZE(channels_2_8),
3037 .list = channels_2_8,
3038 .mask = 0,
3039};
3040
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003041static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3042 struct hda_codec *codec,
3043 struct snd_pcm_substream *substream)
3044{
3045 struct hdmi_spec *spec = codec->spec;
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003046 const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
Nitin Daga393004b2011-01-10 21:49:31 +05303047
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003048 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05303049 case 0x10de0002:
3050 case 0x10de0003:
3051 case 0x10de0005:
3052 case 0x10de0006:
3053 hw_constraints_channels = &hw_constraints_2_8_channels;
3054 break;
3055 case 0x10de0007:
3056 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3057 break;
3058 default:
3059 break;
3060 }
3061
3062 if (hw_constraints_channels != NULL) {
3063 snd_pcm_hw_constraint_list(substream->runtime, 0,
3064 SNDRV_PCM_HW_PARAM_CHANNELS,
3065 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01003066 } else {
3067 snd_pcm_hw_constraint_step(substream->runtime, 0,
3068 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05303069 }
3070
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003071 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3072}
3073
3074static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3075 struct hda_codec *codec,
3076 struct snd_pcm_substream *substream)
3077{
3078 struct hdmi_spec *spec = codec->spec;
3079 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3080}
3081
3082static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3083 struct hda_codec *codec,
3084 unsigned int stream_tag,
3085 unsigned int format,
3086 struct snd_pcm_substream *substream)
3087{
3088 struct hdmi_spec *spec = codec->spec;
3089 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3090 stream_tag, format, substream);
3091}
3092
Takashi Iwaid0b12522012-06-15 14:34:42 +02003093static const struct hda_pcm_stream simple_pcm_playback = {
3094 .substreams = 1,
3095 .channels_min = 2,
3096 .channels_max = 2,
3097 .ops = {
3098 .open = simple_playback_pcm_open,
3099 .close = simple_playback_pcm_close,
3100 .prepare = simple_playback_pcm_prepare
3101 },
3102};
3103
3104static const struct hda_codec_ops simple_hdmi_patch_ops = {
3105 .build_controls = simple_playback_build_controls,
3106 .build_pcms = simple_playback_build_pcms,
3107 .init = simple_playback_init,
3108 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02003109 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02003110};
3111
3112static int patch_simple_hdmi(struct hda_codec *codec,
3113 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3114{
3115 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003116 struct hdmi_spec_per_cvt *per_cvt;
3117 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003118
3119 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3120 if (!spec)
3121 return -ENOMEM;
3122
Takashi Iwaiade49db2018-07-11 18:05:52 +02003123 spec->codec = codec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003124 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003125 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02003126
3127 spec->multiout.num_dacs = 0; /* no analog */
3128 spec->multiout.max_channels = 2;
3129 spec->multiout.dig_out_nid = cvt_nid;
3130 spec->num_cvts = 1;
3131 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003132 per_pin = snd_array_new(&spec->pins);
3133 per_cvt = snd_array_new(&spec->cvts);
3134 if (!per_pin || !per_cvt) {
3135 simple_playback_free(codec);
3136 return -ENOMEM;
3137 }
3138 per_cvt->cvt_nid = cvt_nid;
3139 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003140 spec->pcm_playback = simple_pcm_playback;
3141
3142 codec->patch_ops = simple_hdmi_patch_ops;
3143
3144 return 0;
3145}
3146
Aaron Plattner1f348522011-04-06 17:19:04 -07003147static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3148 int channels)
3149{
3150 unsigned int chanmask;
3151 int chan = channels ? (channels - 1) : 1;
3152
3153 switch (channels) {
3154 default:
3155 case 0:
3156 case 2:
3157 chanmask = 0x00;
3158 break;
3159 case 4:
3160 chanmask = 0x08;
3161 break;
3162 case 6:
3163 chanmask = 0x0b;
3164 break;
3165 case 8:
3166 chanmask = 0x13;
3167 break;
3168 }
3169
3170 /* Set the audio infoframe channel allocation and checksum fields. The
3171 * channel count is computed implicitly by the hardware. */
3172 snd_hda_codec_write(codec, 0x1, 0,
3173 Nv_VERB_SET_Channel_Allocation, chanmask);
3174
3175 snd_hda_codec_write(codec, 0x1, 0,
3176 Nv_VERB_SET_Info_Frame_Checksum,
3177 (0x71 - chan - chanmask));
3178}
3179
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003180static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3181 struct hda_codec *codec,
3182 struct snd_pcm_substream *substream)
3183{
3184 struct hdmi_spec *spec = codec->spec;
3185 int i;
3186
3187 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3188 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3189 for (i = 0; i < 4; i++) {
3190 /* set the stream id */
3191 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3192 AC_VERB_SET_CHANNEL_STREAMID, 0);
3193 /* set the stream format */
3194 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3195 AC_VERB_SET_STREAM_FORMAT, 0);
3196 }
3197
Aaron Plattner1f348522011-04-06 17:19:04 -07003198 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3199 * streams are disabled. */
3200 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3201
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003202 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3203}
3204
3205static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3206 struct hda_codec *codec,
3207 unsigned int stream_tag,
3208 unsigned int format,
3209 struct snd_pcm_substream *substream)
3210{
3211 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003212 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003213 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003214 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003215 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003216 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003217
3218 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003219 per_cvt = get_cvt(spec, 0);
3220 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003221
3222 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003223
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003224 dataDCC2 = 0x2;
3225
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003226 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003227 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003228 snd_hda_codec_write(codec,
3229 nvhdmi_master_con_nid_7x,
3230 0,
3231 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003232 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003233
3234 /* set the stream id */
3235 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3236 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3237
3238 /* set the stream format */
3239 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3240 AC_VERB_SET_STREAM_FORMAT, format);
3241
3242 /* turn on again (if needed) */
3243 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003244 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003245 snd_hda_codec_write(codec,
3246 nvhdmi_master_con_nid_7x,
3247 0,
3248 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003249 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003250 snd_hda_codec_write(codec,
3251 nvhdmi_master_con_nid_7x,
3252 0,
3253 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3254 }
3255
3256 for (i = 0; i < 4; i++) {
3257 if (chs == 2)
3258 channel_id = 0;
3259 else
3260 channel_id = i * 2;
3261
3262 /* turn off SPDIF once;
3263 *otherwise the IEC958 bits won't be updated
3264 */
3265 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003266 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003267 snd_hda_codec_write(codec,
3268 nvhdmi_con_nids_7x[i],
3269 0,
3270 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003271 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003272 /* set the stream id */
3273 snd_hda_codec_write(codec,
3274 nvhdmi_con_nids_7x[i],
3275 0,
3276 AC_VERB_SET_CHANNEL_STREAMID,
3277 (stream_tag << 4) | channel_id);
3278 /* set the stream format */
3279 snd_hda_codec_write(codec,
3280 nvhdmi_con_nids_7x[i],
3281 0,
3282 AC_VERB_SET_STREAM_FORMAT,
3283 format);
3284 /* turn on again (if needed) */
3285 /* enable and set the channel status audio/data flag */
3286 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003287 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003288 snd_hda_codec_write(codec,
3289 nvhdmi_con_nids_7x[i],
3290 0,
3291 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003292 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003293 snd_hda_codec_write(codec,
3294 nvhdmi_con_nids_7x[i],
3295 0,
3296 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3297 }
3298 }
3299
Aaron Plattner1f348522011-04-06 17:19:04 -07003300 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003301
3302 mutex_unlock(&codec->spdif_mutex);
3303 return 0;
3304}
3305
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003306static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003307 .substreams = 1,
3308 .channels_min = 2,
3309 .channels_max = 8,
3310 .nid = nvhdmi_master_con_nid_7x,
3311 .rates = SUPPORTED_RATES,
3312 .maxbps = SUPPORTED_MAXBPS,
3313 .formats = SUPPORTED_FORMATS,
3314 .ops = {
3315 .open = simple_playback_pcm_open,
3316 .close = nvhdmi_8ch_7x_pcm_close,
3317 .prepare = nvhdmi_8ch_7x_pcm_prepare
3318 },
3319};
3320
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003321static int patch_nvhdmi_2ch(struct hda_codec *codec)
3322{
3323 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003324 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3325 nvhdmi_master_pin_nid_7x);
3326 if (err < 0)
3327 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003328
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003329 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003330 /* override the PCM rates, etc, as the codec doesn't give full list */
3331 spec = codec->spec;
3332 spec->pcm_playback.rates = SUPPORTED_RATES;
3333 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3334 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003335 return 0;
3336}
3337
Takashi Iwai53775b02012-08-01 12:17:41 +02003338static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3339{
3340 struct hdmi_spec *spec = codec->spec;
3341 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003342 if (!err) {
3343 struct hda_pcm *info = get_pcm_rec(spec, 0);
3344 info->own_chmap = true;
3345 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003346 return err;
3347}
3348
3349static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3350{
3351 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003352 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003353 struct snd_pcm_chmap *chmap;
3354 int err;
3355
3356 err = simple_playback_build_controls(codec);
3357 if (err < 0)
3358 return err;
3359
3360 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003361 info = get_pcm_rec(spec, 0);
3362 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003363 SNDRV_PCM_STREAM_PLAYBACK,
3364 snd_pcm_alt_chmaps, 8, 0, &chmap);
3365 if (err < 0)
3366 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003367 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003368 case 0x10de0002:
3369 case 0x10de0003:
3370 case 0x10de0005:
3371 case 0x10de0006:
3372 chmap->channel_mask = (1U << 2) | (1U << 8);
3373 break;
3374 case 0x10de0007:
3375 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3376 }
3377 return 0;
3378}
3379
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003380static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3381{
3382 struct hdmi_spec *spec;
3383 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003384 if (err < 0)
3385 return err;
3386 spec = codec->spec;
3387 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003388 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003389 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003390 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3391 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003392
3393 /* Initialize the audio infoframe channel mask and checksum to something
3394 * valid */
3395 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3396
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003397 return 0;
3398}
3399
3400/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003401 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3402 * - 0x10de0015
3403 * - 0x10de0040
3404 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303405static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303406 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02003407{
3408 if (cap->ca_index == 0x00 && channels == 2)
3409 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3410
Subhransu S. Prusty028cb682016-03-14 10:35:06 +05303411 /* If the speaker allocation matches the channel count, it is OK. */
3412 if (cap->channels != channels)
3413 return -1;
3414
3415 /* all channels are remappable freely */
3416 return SNDRV_CTL_TLVT_CHMAP_VAR;
Anssi Hannula611885b2013-11-03 17:15:00 +02003417}
3418
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303419static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3420 int ca, int chs, unsigned char *map)
Anssi Hannula611885b2013-11-03 17:15:00 +02003421{
3422 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3423 return -EINVAL;
3424
3425 return 0;
3426}
3427
Takashi Iwaiade49db2018-07-11 18:05:52 +02003428/* map from pin NID to port; port is 0-based */
3429/* for Nvidia: assume widget NID starting from 4, with step 1 (4, 5, 6, ...) */
3430static int nvhdmi_pin2port(void *audio_ptr, int pin_nid)
3431{
3432 return pin_nid - 4;
3433}
3434
3435/* reverse-map from port to pin NID: see above */
3436static int nvhdmi_port2pin(struct hda_codec *codec, int port)
3437{
3438 return port + 4;
3439}
3440
3441static const struct drm_audio_component_audio_ops nvhdmi_audio_ops = {
3442 .pin2port = nvhdmi_pin2port,
3443 .pin_eld_notify = generic_acomp_pin_eld_notify,
3444 .master_bind = generic_acomp_master_bind,
3445 .master_unbind = generic_acomp_master_unbind,
3446};
3447
Anssi Hannula611885b2013-11-03 17:15:00 +02003448static int patch_nvhdmi(struct hda_codec *codec)
3449{
3450 struct hdmi_spec *spec;
3451 int err;
3452
Nikhil Mahale5398e942019-11-19 14:17:10 +05303453 err = alloc_generic_hdmi(codec);
3454 if (err < 0)
3455 return err;
3456 codec->dp_mst = true;
3457
3458 spec = codec->spec;
3459 spec->dyn_pcm_assign = true;
3460
3461 err = hdmi_parse_codec(codec);
3462 if (err < 0) {
3463 generic_spec_free(codec);
3464 return err;
3465 }
3466
3467 generic_hdmi_init_per_pins(codec);
3468
3469 spec->dyn_pin_out = true;
3470
3471 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3472 nvhdmi_chmap_cea_alloc_validate_get_type;
3473 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3474
3475 codec->link_down_at_suspend = 1;
3476
3477 generic_acomp_init(codec, &nvhdmi_audio_ops, nvhdmi_port2pin);
3478
3479 return 0;
3480}
3481
3482static int patch_nvhdmi_legacy(struct hda_codec *codec)
3483{
3484 struct hdmi_spec *spec;
3485 int err;
3486
Anssi Hannula611885b2013-11-03 17:15:00 +02003487 err = patch_generic_hdmi(codec);
3488 if (err)
3489 return err;
3490
3491 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003492 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003493
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303494 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02003495 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303496 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02003497
Lukas Wunner94989e32019-10-17 17:04:11 +02003498 codec->link_down_at_suspend = 1;
3499
Anssi Hannula611885b2013-11-03 17:15:00 +02003500 return 0;
3501}
3502
3503/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003504 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3505 * accessed using vendor-defined verbs. These registers can be used for
3506 * interoperability between the HDA and HDMI drivers.
3507 */
3508
3509/* Audio Function Group node */
3510#define NVIDIA_AFG_NID 0x01
3511
3512/*
3513 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3514 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3515 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3516 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3517 * additional bit (at position 30) to signal the validity of the format.
3518 *
3519 * | 31 | 30 | 29 16 | 15 0 |
3520 * +---------+-------+--------+--------+
3521 * | TRIGGER | VALID | UNUSED | FORMAT |
3522 * +-----------------------------------|
3523 *
3524 * Note that for the trigger bit to take effect it needs to change value
3525 * (i.e. it needs to be toggled).
3526 */
3527#define NVIDIA_GET_SCRATCH0 0xfa6
3528#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3529#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3530#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3531#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3532#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3533#define NVIDIA_SCRATCH_VALID (1 << 6)
3534
3535#define NVIDIA_GET_SCRATCH1 0xfab
3536#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3537#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3538#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3539#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3540
3541/*
3542 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3543 * the format is invalidated so that the HDMI codec can be disabled.
3544 */
3545static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3546{
3547 unsigned int value;
3548
3549 /* bits [31:30] contain the trigger and valid bits */
3550 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3551 NVIDIA_GET_SCRATCH0, 0);
3552 value = (value >> 24) & 0xff;
3553
3554 /* bits [15:0] are used to store the HDA format */
3555 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3556 NVIDIA_SET_SCRATCH0_BYTE0,
3557 (format >> 0) & 0xff);
3558 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3559 NVIDIA_SET_SCRATCH0_BYTE1,
3560 (format >> 8) & 0xff);
3561
3562 /* bits [16:24] are unused */
3563 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3564 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3565
3566 /*
3567 * Bit 30 signals that the data is valid and hence that HDMI audio can
3568 * be enabled.
3569 */
3570 if (format == 0)
3571 value &= ~NVIDIA_SCRATCH_VALID;
3572 else
3573 value |= NVIDIA_SCRATCH_VALID;
3574
3575 /*
3576 * Whenever the trigger bit is toggled, an interrupt is raised in the
3577 * HDMI codec. The HDMI driver will use that as trigger to update its
3578 * configuration.
3579 */
3580 value ^= NVIDIA_SCRATCH_TRIGGER;
3581
3582 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3583 NVIDIA_SET_SCRATCH0_BYTE3, value);
3584}
3585
3586static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3587 struct hda_codec *codec,
3588 unsigned int stream_tag,
3589 unsigned int format,
3590 struct snd_pcm_substream *substream)
3591{
3592 int err;
3593
3594 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3595 format, substream);
3596 if (err < 0)
3597 return err;
3598
3599 /* notify the HDMI codec of the format change */
3600 tegra_hdmi_set_format(codec, format);
3601
3602 return 0;
3603}
3604
3605static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3606 struct hda_codec *codec,
3607 struct snd_pcm_substream *substream)
3608{
3609 /* invalidate the format in the HDMI codec */
3610 tegra_hdmi_set_format(codec, 0);
3611
3612 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3613}
3614
3615static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3616{
3617 struct hdmi_spec *spec = codec->spec;
3618 unsigned int i;
3619
3620 for (i = 0; i < spec->num_pins; i++) {
3621 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3622
3623 if (pcm->pcm_type == type)
3624 return pcm;
3625 }
3626
3627 return NULL;
3628}
3629
3630static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3631{
3632 struct hda_pcm_stream *stream;
3633 struct hda_pcm *pcm;
3634 int err;
3635
3636 err = generic_hdmi_build_pcms(codec);
3637 if (err < 0)
3638 return err;
3639
3640 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3641 if (!pcm)
3642 return -ENODEV;
3643
3644 /*
3645 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3646 * codec about format changes.
3647 */
3648 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3649 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3650 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3651
3652 return 0;
3653}
3654
3655static int patch_tegra_hdmi(struct hda_codec *codec)
3656{
3657 int err;
3658
3659 err = patch_generic_hdmi(codec);
3660 if (err)
3661 return err;
3662
3663 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3664
3665 return 0;
3666}
3667
3668/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003669 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003670 */
3671
Anssi Hannula5a6135842013-10-24 21:10:35 +03003672#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003673 ((codec)->core.vendor_id == 0x1002aa01 && \
3674 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003675#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003676
Anssi Hannula5a6135842013-10-24 21:10:35 +03003677/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3678#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3679#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3680#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3681#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3682#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3683#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003684#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003685#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3686#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3687#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3688#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3689#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3690#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3691#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3692#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3693#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3694#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3695#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003696#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003697#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3698#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3699#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3700#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3701#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3702
Anssi Hannula84d69e72013-10-24 21:10:38 +03003703/* AMD specific HDA cvt verbs */
3704#define ATI_VERB_SET_RAMP_RATE 0x770
3705#define ATI_VERB_GET_RAMP_RATE 0xf70
3706
Anssi Hannula5a6135842013-10-24 21:10:35 +03003707#define ATI_OUT_ENABLE 0x1
3708
3709#define ATI_MULTICHANNEL_MODE_PAIRED 0
3710#define ATI_MULTICHANNEL_MODE_SINGLE 1
3711
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003712#define ATI_HBR_CAPABLE 0x01
3713#define ATI_HBR_ENABLE 0x10
3714
Anssi Hannula89250f82013-10-24 21:10:36 +03003715static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303716 int dev_id, unsigned char *buf, int *eld_size)
Anssi Hannula89250f82013-10-24 21:10:36 +03003717{
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303718 WARN_ON(dev_id != 0);
Anssi Hannula89250f82013-10-24 21:10:36 +03003719 /* call hda_eld.c ATI/AMD-specific function */
3720 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3721 is_amdhdmi_rev3_or_later(codec));
3722}
3723
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303724static void atihdmi_pin_setup_infoframe(struct hda_codec *codec,
3725 hda_nid_t pin_nid, int dev_id, int ca,
Anssi Hannula5a6135842013-10-24 21:10:35 +03003726 int active_channels, int conn_type)
3727{
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303728 WARN_ON(dev_id != 0);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003729 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3730}
3731
3732static int atihdmi_paired_swap_fc_lfe(int pos)
3733{
3734 /*
3735 * ATI/AMD have automatic FC/LFE swap built-in
3736 * when in pairwise mapping mode.
3737 */
3738
3739 switch (pos) {
3740 /* see channel_allocations[].speakers[] */
3741 case 2: return 3;
3742 case 3: return 2;
3743 default: break;
3744 }
3745
3746 return pos;
3747}
3748
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303749static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3750 int ca, int chs, unsigned char *map)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003751{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303752 struct hdac_cea_channel_speaker_allocation *cap;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003753 int i, j;
3754
3755 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3756
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303757 cap = snd_hdac_get_ch_alloc_from_ca(ca);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003758 for (i = 0; i < chs; ++i) {
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303759 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003760 bool ok = false;
3761 bool companion_ok = false;
3762
3763 if (!mask)
3764 continue;
3765
3766 for (j = 0 + i % 2; j < 8; j += 2) {
3767 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3768 if (cap->speakers[chan_idx] == mask) {
3769 /* channel is in a supported position */
3770 ok = true;
3771
3772 if (i % 2 == 0 && i + 1 < chs) {
3773 /* even channel, check the odd companion */
3774 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303775 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003776 int comp_mask_act = cap->speakers[comp_chan_idx];
3777
3778 if (comp_mask_req == comp_mask_act)
3779 companion_ok = true;
3780 else
3781 return -EINVAL;
3782 }
3783 break;
3784 }
3785 }
3786
3787 if (!ok)
3788 return -EINVAL;
3789
3790 if (companion_ok)
3791 i++; /* companion channel already checked */
3792 }
3793
3794 return 0;
3795}
3796
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303797static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3798 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003799{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08003800 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003801 int verb;
3802 int ati_channel_setup = 0;
3803
3804 if (hdmi_slot > 7)
3805 return -EINVAL;
3806
3807 if (!has_amd_full_remap_support(codec)) {
3808 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3809
3810 /* In case this is an odd slot but without stream channel, do not
3811 * disable the slot since the corresponding even slot could have a
3812 * channel. In case neither have a channel, the slot pair will be
3813 * disabled when this function is called for the even slot. */
3814 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3815 return 0;
3816
3817 hdmi_slot -= hdmi_slot % 2;
3818
3819 if (stream_channel != 0xf)
3820 stream_channel -= stream_channel % 2;
3821 }
3822
3823 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3824
3825 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3826
3827 if (stream_channel != 0xf)
3828 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3829
3830 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3831}
3832
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303833static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3834 hda_nid_t pin_nid, int asp_slot)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003835{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08003836 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003837 bool was_odd = false;
3838 int ati_asp_slot = asp_slot;
3839 int verb;
3840 int ati_channel_setup;
3841
3842 if (asp_slot > 7)
3843 return -EINVAL;
3844
3845 if (!has_amd_full_remap_support(codec)) {
3846 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3847 if (ati_asp_slot % 2 != 0) {
3848 ati_asp_slot -= 1;
3849 was_odd = true;
3850 }
3851 }
3852
3853 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3854
3855 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3856
3857 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3858 return 0xf;
3859
3860 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3861}
3862
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303863static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3864 struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303865 struct hdac_cea_channel_speaker_allocation *cap,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303866 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003867{
3868 int c;
3869
3870 /*
3871 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3872 * we need to take that into account (a single channel may take 2
3873 * channel slots if we need to carry a silent channel next to it).
3874 * On Rev3+ AMD codecs this function is not used.
3875 */
3876 int chanpairs = 0;
3877
3878 /* We only produce even-numbered channel count TLVs */
3879 if ((channels % 2) != 0)
3880 return -1;
3881
3882 for (c = 0; c < 7; c += 2) {
3883 if (cap->speakers[c] || cap->speakers[c+1])
3884 chanpairs++;
3885 }
3886
3887 if (chanpairs * 2 != channels)
3888 return -1;
3889
3890 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3891}
3892
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303893static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303894 struct hdac_cea_channel_speaker_allocation *cap,
3895 unsigned int *chmap, int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003896{
3897 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3898 int count = 0;
3899 int c;
3900
3901 for (c = 7; c >= 0; c--) {
3902 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3903 int spk = cap->speakers[chan];
3904 if (!spk) {
3905 /* add N/A channel if the companion channel is occupied */
3906 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3907 chmap[count++] = SNDRV_CHMAP_NA;
3908
3909 continue;
3910 }
3911
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303912 chmap[count++] = snd_hdac_spk_to_chmap(spk);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003913 }
3914
3915 WARN_ON(count != channels);
3916}
3917
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003918static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303919 int dev_id, bool hbr)
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003920{
3921 int hbr_ctl, hbr_ctl_new;
3922
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303923 WARN_ON(dev_id != 0);
3924
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003925 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003926 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003927 if (hbr)
3928 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3929 else
3930 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3931
Takashi Iwai4e76a882014-02-25 12:21:03 +01003932 codec_dbg(codec,
3933 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003934 pin_nid,
3935 hbr_ctl == hbr_ctl_new ? "" : "new-",
3936 hbr_ctl_new);
3937
3938 if (hbr_ctl != hbr_ctl_new)
3939 snd_hda_codec_write(codec, pin_nid, 0,
3940 ATI_VERB_SET_HBR_CONTROL,
3941 hbr_ctl_new);
3942
3943 } else if (hbr)
3944 return -EINVAL;
3945
3946 return 0;
3947}
3948
Anssi Hannula84d69e72013-10-24 21:10:38 +03003949static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303950 hda_nid_t pin_nid, int dev_id,
3951 u32 stream_tag, int format)
Anssi Hannula84d69e72013-10-24 21:10:38 +03003952{
Anssi Hannula84d69e72013-10-24 21:10:38 +03003953 if (is_amdhdmi_rev3_or_later(codec)) {
3954 int ramp_rate = 180; /* default as per AMD spec */
3955 /* disable ramp-up/down for non-pcm as per AMD spec */
3956 if (format & AC_FMT_TYPE_NON_PCM)
3957 ramp_rate = 0;
3958
3959 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3960 }
3961
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303962 return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
3963 stream_tag, format);
Anssi Hannula84d69e72013-10-24 21:10:38 +03003964}
3965
3966
Anssi Hannula5a6135842013-10-24 21:10:35 +03003967static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003968{
3969 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003970 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003971
Anssi Hannula5a6135842013-10-24 21:10:35 +03003972 err = generic_hdmi_init(codec);
3973
3974 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003975 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003976
3977 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3978 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3979
3980 /* make sure downmix information in infoframe is zero */
3981 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3982
3983 /* enable channel-wise remap mode if supported */
3984 if (has_amd_full_remap_support(codec))
3985 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3986 ATI_VERB_SET_MULTICHANNEL_MODE,
3987 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003988 }
Alex Deucher8218df92019-11-22 16:43:53 -05003989 codec->auto_runtime_pm = 1;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003990
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003991 return 0;
3992}
3993
Takashi Iwaiade49db2018-07-11 18:05:52 +02003994/* map from pin NID to port; port is 0-based */
3995/* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */
3996static int atihdmi_pin2port(void *audio_ptr, int pin_nid)
3997{
3998 return pin_nid / 2 - 1;
3999}
4000
4001/* reverse-map from port to pin NID: see above */
4002static int atihdmi_port2pin(struct hda_codec *codec, int port)
4003{
4004 return port * 2 + 3;
4005}
4006
4007static const struct drm_audio_component_audio_ops atihdmi_audio_ops = {
4008 .pin2port = atihdmi_pin2port,
4009 .pin_eld_notify = generic_acomp_pin_eld_notify,
4010 .master_bind = generic_acomp_master_bind,
4011 .master_unbind = generic_acomp_master_unbind,
4012};
4013
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004014static int patch_atihdmi(struct hda_codec *codec)
4015{
4016 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004017 struct hdmi_spec_per_cvt *per_cvt;
4018 int err, cvt_idx;
4019
4020 err = patch_generic_hdmi(codec);
4021
4022 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02004023 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004024
4025 codec->patch_ops.init = atihdmi_init;
4026
Takashi Iwaid0b12522012-06-15 14:34:42 +02004027 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004028
Anssi Hannula89250f82013-10-24 21:10:36 +03004029 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004030 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03004031 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03004032 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004033
Takashi Iwai39669222016-05-11 14:56:12 +02004034 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4035 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4036
Anssi Hannula5a6135842013-10-24 21:10:35 +03004037 if (!has_amd_full_remap_support(codec)) {
4038 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304039 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03004040 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304041 spec->chmap.ops.cea_alloc_to_tlv_chmap =
4042 atihdmi_paired_cea_alloc_to_tlv_chmap;
4043 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004044 }
4045
4046 /* ATI/AMD converters do not advertise all of their capabilities */
4047 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4048 per_cvt = get_cvt(spec, cvt_idx);
4049 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4050 per_cvt->rates |= SUPPORTED_RATES;
4051 per_cvt->formats |= SUPPORTED_FORMATS;
4052 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4053 }
4054
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304055 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03004056
Takashi Iwai57cb54e2018-06-21 13:33:53 +02004057 /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
4058 * the link-down as is. Tell the core to allow it.
4059 */
4060 codec->link_down_at_suspend = 1;
4061
Takashi Iwaiade49db2018-07-11 18:05:52 +02004062 generic_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin);
4063
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004064 return 0;
4065}
4066
Annie Liu3de5ff82012-06-08 19:18:42 +08004067/* VIA HDMI Implementation */
4068#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4069#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4070
Annie Liu3de5ff82012-06-08 19:18:42 +08004071static int patch_via_hdmi(struct hda_codec *codec)
4072{
Takashi Iwai250e41a2012-06-15 14:40:21 +02004073 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08004074}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004075
4076/*
4077 * patch entries
4078 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004079static const struct hda_device_id snd_hda_id_hdmi[] = {
4080HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4081HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4082HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4083HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4084HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4085HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4086HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004087HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI", patch_nvhdmi_2ch),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004088HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4089HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004090HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI", patch_nvhdmi_8ch_7x),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004091HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4092HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4093HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
Nikhil Mahale5398e942019-11-19 14:17:10 +05304094HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP", patch_nvhdmi_legacy),
4095HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP", patch_nvhdmi_legacy),
4096HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi_legacy),
4097HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi_legacy),
4098HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi_legacy),
4099HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi_legacy),
4100HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi_legacy),
4101HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi_legacy),
4102HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi_legacy),
4103HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi_legacy),
4104HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi_legacy),
4105HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi_legacy),
4106HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi_legacy),
Richard Samsonc8900a02011-03-03 12:46:13 +01004107/* 17 is known to be absent */
Nikhil Mahale5398e942019-11-19 14:17:10 +05304108HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi_legacy),
4109HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi_legacy),
4110HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi_legacy),
4111HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi_legacy),
4112HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi_legacy),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004113HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4114HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4115HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4116HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
Thierry Reding917bb902018-12-03 16:53:17 +01004117HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi),
4118HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi),
4119HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi),
4120HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004121HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4122HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4123HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4124HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4125HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004126HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP", patch_nvhdmi),
4127HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004128HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004129HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004130HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004131HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP", patch_nvhdmi),
4132HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004133HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4134HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4135HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4136HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004137HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP", patch_nvhdmi),
4138HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP", patch_nvhdmi),
4139HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP", patch_nvhdmi),
4140HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP", patch_nvhdmi),
4141HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004142HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004143HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP", patch_nvhdmi),
Hui Wangaf677162017-02-09 09:20:54 +08004144HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004145HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP", patch_nvhdmi),
Aaron Plattner2d369c72016-03-13 13:58:57 -07004146HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08004147HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004148HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP", patch_nvhdmi),
4149HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP", patch_nvhdmi),
4150HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP", patch_nvhdmi),
4151HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP", patch_nvhdmi),
4152HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP", patch_nvhdmi),
4153HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP", patch_nvhdmi),
4154HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP", patch_nvhdmi),
4155HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP", patch_nvhdmi),
4156HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP", patch_nvhdmi),
4157HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP", patch_nvhdmi),
Aaron Plattneradb36a822020-06-11 11:08:45 -07004158HDA_CODEC_ENTRY(0x10de009a, "GPU 9a HDMI/DP", patch_nvhdmi),
4159HDA_CODEC_ENTRY(0x10de009d, "GPU 9d HDMI/DP", patch_nvhdmi),
4160HDA_CODEC_ENTRY(0x10de009e, "GPU 9e HDMI/DP", patch_nvhdmi),
4161HDA_CODEC_ENTRY(0x10de009f, "GPU 9f HDMI/DP", patch_nvhdmi),
4162HDA_CODEC_ENTRY(0x10de00a0, "GPU a0 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004163HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004164HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI", patch_nvhdmi_2ch),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004165HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4166HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4167HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4168HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
Takashi Iwai7ff652f2016-03-21 14:50:24 +01004169HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi),
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01004170HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI", patch_i915_glk_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004171HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4172HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4173HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
Takashi Iwai7ff652f2016-03-21 14:50:24 +01004174HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi),
Takashi Iwaie85015a32016-03-21 13:56:19 +01004175HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi),
4176HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01004177HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
4178HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
4179HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
4180HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
4181HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
Guneshwor Singh2b4584d2017-12-07 18:06:20 +05304182HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI", patch_i915_glk_hdmi),
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05304183HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi),
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01004184HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI", patch_i915_icl_hdmi),
Kai Vehmanen9a11ba72019-11-05 18:10:53 +02004185HDA_CODEC_ENTRY(0x80862812, "Tigerlake HDMI", patch_i915_tgl_hdmi),
Yong Zhi78be2222020-01-31 14:40:03 -06004186HDA_CODEC_ENTRY(0x8086281a, "Jasperlake HDMI", patch_i915_icl_hdmi),
Libin Yangd233c492020-04-09 13:58:17 -05004187HDA_CODEC_ENTRY(0x8086281b, "Elkhartlake HDMI", patch_i915_icl_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004188HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01004189HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
4190HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004191HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004192/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004193HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004194{} /* terminator */
4195};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004196MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004197
4198MODULE_LICENSE("GPL");
4199MODULE_DESCRIPTION("HDMI HD-audio codec");
4200MODULE_ALIAS("snd-hda-codec-intelhdmi");
4201MODULE_ALIAS("snd-hda-codec-nvhdmi");
4202MODULE_ALIAS("snd-hda-codec-atihdmi");
4203
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004204static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004205 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004206};
4207
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004208module_hda_codec_driver(hdmi_driver);