blob: e2b21ef5d7d12994be0ca64d332d0d11c24486a0 [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{
1807 hda_nid_t nid;
1808 int i, nodes;
1809
Takashi Iwai7639a062015-03-03 10:07:24 +01001810 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001811 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001812 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08001813 return -EINVAL;
1814 }
1815
1816 for (i = 0; i < nodes; i++, nid++) {
1817 unsigned int caps;
1818 unsigned int type;
1819
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001820 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001821 type = get_wcaps_type(caps);
1822
1823 if (!(caps & AC_WCAP_DIGITAL))
1824 continue;
1825
1826 switch (type) {
1827 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001828 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001829 break;
1830 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001831 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001832 break;
1833 }
1834 }
1835
Wu Fengguang079d88c2010-03-08 10:44:23 +08001836 return 0;
1837}
1838
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001839/*
1840 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001841static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1842{
1843 struct hda_spdif_out *spdif;
1844 bool non_pcm;
1845
1846 mutex_lock(&codec->spdif_mutex);
1847 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
Libin Yang960a5812016-06-16 11:13:25 +08001848 /* Add sanity check to pass klockwork check.
1849 * This should never happen.
1850 */
Wu Boa2f647242020-04-26 21:17:22 +08001851 if (WARN_ON(spdif == NULL)) {
1852 mutex_unlock(&codec->spdif_mutex);
Libin Yang960a5812016-06-16 11:13:25 +08001853 return true;
Wu Boa2f647242020-04-26 21:17:22 +08001854 }
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001855 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1856 mutex_unlock(&codec->spdif_mutex);
1857 return non_pcm;
1858}
1859
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001860/*
1861 * HDMI callbacks
1862 */
1863
1864static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1865 struct hda_codec *codec,
1866 unsigned int stream_tag,
1867 unsigned int format,
1868 struct snd_pcm_substream *substream)
1869{
Stephen Warren384a48d2011-06-01 11:14:21 -06001870 hda_nid_t cvt_nid = hinfo->nid;
1871 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08001872 int pin_idx;
1873 struct hdmi_spec_per_pin *per_pin;
Libin Yangddd621f2015-09-02 14:11:40 +08001874 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001875 bool non_pcm;
Sameer Pujar053b0552019-01-14 23:51:12 +05301876 int pinctl, stripe;
Takashi Iwaif69548f2018-07-12 23:06:51 +02001877 int err = 0;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001878
Libin Yang42b29872015-12-16 13:42:42 +08001879 mutex_lock(&spec->pcm_lock);
1880 pin_idx = hinfo_to_pin_index(codec, hinfo);
1881 if (spec->dyn_pcm_assign && pin_idx < 0) {
1882 /* when dyn_pcm_assign and pcm is not bound to a pin
1883 * skip pin setup and return 0 to make audio playback
1884 * be ongoing
1885 */
Takashi Iwai4846a672016-03-21 12:56:46 +01001886 pin_cvt_fixup(codec, NULL, cvt_nid);
Libin Yang42b29872015-12-16 13:42:42 +08001887 snd_hda_codec_setup_stream(codec, cvt_nid,
1888 stream_tag, 0, format);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001889 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001890 }
1891
1892 if (snd_BUG_ON(pin_idx < 0)) {
Takashi Iwaif69548f2018-07-12 23:06:51 +02001893 err = -EINVAL;
1894 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001895 }
1896 per_pin = get_pin(spec, pin_idx);
Takashi Iwai4846a672016-03-21 12:56:46 +01001897
1898 /* Verify pin:cvt selections to avoid silent audio after S3.
1899 * After S3, the audio driver restores pin:cvt selections
1900 * but this can happen before gfx is ready and such selection
1901 * is overlooked by HW. Thus multiple pins can share a same
1902 * default convertor and mute control will affect each other,
1903 * which can cause a resumed audio playback become silent
1904 * after S3.
1905 */
1906 pin_cvt_fixup(codec, per_pin, 0);
Mengdong Lin2df67422014-03-20 13:01:06 +08001907
Libin Yangddd621f2015-09-02 14:11:40 +08001908 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1909 /* Todo: add DP1.2 MST audio support later */
Takashi Iwai93a9ff12016-03-18 19:45:13 +01001910 if (codec_has_acomp(codec))
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301911 snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
1912 per_pin->dev_id, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08001913
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001914 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001915 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02001916 per_pin->channels = substream->runtime->channels;
1917 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001918
Sameer Pujar053b0552019-01-14 23:51:12 +05301919 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1920 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1921 substream);
1922 snd_hda_codec_write(codec, cvt_nid, 0,
1923 AC_VERB_SET_STRIPE_CONTROL,
1924 stripe);
1925 }
1926
Takashi Iwaib0540872013-09-02 12:33:02 +02001927 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001928 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07001929 if (spec->dyn_pin_out) {
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301930 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1931 per_pin->dev_id);
1932 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -07001933 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301934 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -07001935 AC_VERB_SET_PIN_WIDGET_CONTROL,
1936 pinctl | PIN_OUT);
1937 }
1938
Libin Yang91520852017-01-12 16:04:53 +08001939 /* snd_hda_set_dev_select() has been called before */
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301940 err = spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid,
1941 per_pin->dev_id, stream_tag, format);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001942 unlock:
Libin Yang42b29872015-12-16 13:42:42 +08001943 mutex_unlock(&spec->pcm_lock);
1944 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001945}
1946
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001947static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1948 struct hda_codec *codec,
1949 struct snd_pcm_substream *substream)
1950{
1951 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1952 return 0;
1953}
1954
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001955static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1956 struct hda_codec *codec,
1957 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001958{
1959 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08001960 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06001961 struct hdmi_spec_per_cvt *per_cvt;
1962 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07001963 int pinctl;
Takashi Iwaif69548f2018-07-12 23:06:51 +02001964 int err = 0;
Stephen Warren384a48d2011-06-01 11:14:21 -06001965
Stephen Warren384a48d2011-06-01 11:14:21 -06001966 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08001967 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1968 if (snd_BUG_ON(pcm_idx < 0))
1969 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001970 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001971 if (snd_BUG_ON(cvt_idx < 0))
1972 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001973 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001974
1975 snd_BUG_ON(!per_cvt->assigned);
1976 per_cvt->assigned = 0;
1977 hinfo->nid = 0;
1978
Takashi Iwai6fd739c2019-12-14 18:52:17 +01001979 azx_stream(get_azx_dev(substream))->stripe = 0;
1980
Libin Yang42b29872015-12-16 13:42:42 +08001981 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08001982 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001983 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001984 pin_idx = hinfo_to_pin_index(codec, hinfo);
Takashi Iwaif69548f2018-07-12 23:06:51 +02001985 if (spec->dyn_pcm_assign && pin_idx < 0)
1986 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001987
1988 if (snd_BUG_ON(pin_idx < 0)) {
Takashi Iwaif69548f2018-07-12 23:06:51 +02001989 err = -EINVAL;
1990 goto unlock;
Libin Yang42b29872015-12-16 13:42:42 +08001991 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001992 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001993
Stephen Warren75fae112014-01-30 11:52:16 -07001994 if (spec->dyn_pin_out) {
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05301995 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1996 per_pin->dev_id);
Stephen Warren75fae112014-01-30 11:52:16 -07001997 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1998 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1999 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2000 AC_VERB_SET_PIN_WIDGET_CONTROL,
2001 pinctl & ~PIN_OUT);
2002 }
2003
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002004 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002005 per_pin->chmap_set = false;
2006 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002007
2008 per_pin->setup = false;
2009 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002010 mutex_unlock(&per_pin->lock);
Takashi Iwaif69548f2018-07-12 23:06:51 +02002011 unlock:
Libin Yang42b29872015-12-16 13:42:42 +08002012 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002013 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002014
Takashi Iwaif69548f2018-07-12 23:06:51 +02002015 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002016}
2017
2018static const struct hda_pcm_ops generic_ops = {
2019 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002020 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002021 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002022 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002023};
2024
Subhransu S. Prusty44fde3b2016-04-04 19:23:54 +05302025static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2026{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08002027 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Subhransu S. Prusty44fde3b2016-04-04 19:23:54 +05302028 struct hdmi_spec *spec = codec->spec;
2029 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2030
2031 if (!per_pin)
2032 return 0;
2033
2034 return per_pin->sink_eld.info.spk_alloc;
2035}
2036
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302037static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2038 unsigned char *chmap)
2039{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08002040 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302041 struct hdmi_spec *spec = codec->spec;
2042 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2043
2044 /* chmap is already set to 0 in caller */
2045 if (!per_pin)
2046 return;
2047
2048 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2049}
2050
2051static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2052 unsigned char *chmap, int prepared)
2053{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08002054 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302055 struct hdmi_spec *spec = codec->spec;
2056 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2057
Libin Yanged0739b2016-04-18 09:16:28 +08002058 if (!per_pin)
2059 return;
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302060 mutex_lock(&per_pin->lock);
2061 per_pin->chmap_set = true;
2062 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2063 if (prepared)
2064 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2065 mutex_unlock(&per_pin->lock);
2066}
2067
2068static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2069{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08002070 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302071 struct hdmi_spec *spec = codec->spec;
2072 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2073
2074 return per_pin ? true:false;
2075}
2076
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002077static int generic_hdmi_build_pcms(struct hda_codec *codec)
2078{
2079 struct hdmi_spec *spec = codec->spec;
Kai Vehmanen2a2edfb2019-10-29 15:40:09 +02002080 int idx, pcm_num;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002081
Libin Yang91520852017-01-12 16:04:53 +08002082 /*
2083 * for non-mst mode, pcm number is the same as before
Kai Vehmanen2a2edfb2019-10-29 15:40:09 +02002084 * for DP MST mode without extra PCM, pcm number is same
2085 * for DP MST mode with extra PCMs, pcm number is
2086 * (nid number + dev_num - 1)
2087 * dev_num is the device entry number in a pin
Libin Yang91520852017-01-12 16:04:53 +08002088 */
Kai Vehmanen2a2edfb2019-10-29 15:40:09 +02002089
2090 if (codec->mst_no_extra_pcms)
2091 pcm_num = spec->num_nids;
2092 else
2093 pcm_num = spec->num_nids + spec->dev_num - 1;
2094
2095 codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num);
2096
2097 for (idx = 0; idx < pcm_num; idx++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002098 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002099 struct hda_pcm_stream *pstr;
2100
Libin Yang91520852017-01-12 16:04:53 +08002101 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002102 if (!info)
2103 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002104
Libin Yang91520852017-01-12 16:04:53 +08002105 spec->pcm_rec[idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002106 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002107 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002108 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002109
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002110 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002111 pstr->substreams = 1;
2112 pstr->ops = generic_ops;
Libin Yang91520852017-01-12 16:04:53 +08002113 /* pcm number is less than 16 */
2114 if (spec->pcm_used >= 16)
2115 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06002116 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002117 }
2118
2119 return 0;
2120}
2121
Libin Yang25e4abb2016-01-12 11:13:27 +08002122static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01002123{
Libin Yang25e4abb2016-01-12 11:13:27 +08002124 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01002125
Libin Yang25e4abb2016-01-12 11:13:27 +08002126 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002127}
2128
Takashi Iwaidb845402020-02-06 17:28:02 +01002129static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
Takashi Iwai788d4412015-11-12 15:36:13 +01002130{
Takashi Iwaidb845402020-02-06 17:28:02 +01002131 char hdmi_str[32] = "HDMI/DP";
2132 struct hdmi_spec *spec = codec->spec;
2133 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pcm_idx);
Takashi Iwai788d4412015-11-12 15:36:13 +01002134 struct snd_jack *jack;
Takashi Iwaidb845402020-02-06 17:28:02 +01002135 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai788d4412015-11-12 15:36:13 +01002136 int err;
2137
Takashi Iwaidb845402020-02-06 17:28:02 +01002138 if (pcmdev > 0)
2139 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2140 if (!spec->dyn_pcm_assign &&
2141 !is_jack_detectable(codec, per_pin->pin_nid))
2142 strncat(hdmi_str, " Phantom",
2143 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2144
2145 err = snd_jack_new(codec->card, hdmi_str, SND_JACK_AVOUT, &jack,
Takashi Iwai788d4412015-11-12 15:36:13 +01002146 true, false);
2147 if (err < 0)
2148 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002149
2150 spec->pcm_rec[pcm_idx].jack = jack;
2151 jack->private_data = &spec->pcm_rec[pcm_idx];
2152 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01002153 return 0;
2154}
2155
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002156static int generic_hdmi_build_controls(struct hda_codec *codec)
2157{
2158 struct hdmi_spec *spec = codec->spec;
Wang YanQing1f7f51a2017-09-03 21:18:49 +08002159 int dev, err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002160 int pin_idx, pcm_idx;
2161
Libin Yang25e4abb2016-01-12 11:13:27 +08002162 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwai8a7d6002018-04-25 16:19:13 +02002163 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2164 /* no PCM: mark this for skipping permanently */
2165 set_bit(pcm_idx, &spec->pcm_bitmap);
2166 continue;
2167 }
2168
Libin Yang25e4abb2016-01-12 11:13:27 +08002169 err = generic_hdmi_build_jack(codec, pcm_idx);
2170 if (err < 0)
2171 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08002172
2173 /* create the spdif for each pcm
2174 * pin will be bound when monitor is connected
2175 */
2176 if (spec->dyn_pcm_assign)
2177 err = snd_hda_create_dig_out_ctls(codec,
2178 0, spec->cvt_nids[0],
2179 HDA_PCM_TYPE_HDMI);
2180 else {
2181 struct hdmi_spec_per_pin *per_pin =
2182 get_pin(spec, pcm_idx);
2183 err = snd_hda_create_dig_out_ctls(codec,
2184 per_pin->pin_nid,
2185 per_pin->mux_nids[0],
2186 HDA_PCM_TYPE_HDMI);
2187 }
2188 if (err < 0)
2189 return err;
2190 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002191
Wang YanQing1f7f51a2017-09-03 21:18:49 +08002192 dev = get_pcm_rec(spec, pcm_idx)->device;
2193 if (dev != SNDRV_PCM_INVALID_DEVICE) {
2194 /* add control for ELD Bytes */
2195 err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2196 if (err < 0)
2197 return err;
2198 }
Libin Yang25e4abb2016-01-12 11:13:27 +08002199 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002200
Stephen Warren384a48d2011-06-01 11:14:21 -06002201 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002202 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Kai Vehmanenca762822020-04-28 15:38:36 +03002203 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002204
Kai Vehmanenca762822020-04-28 15:38:36 +03002205 pin_eld->eld_valid = false;
Takashi Iwai82b1d732011-12-20 15:53:07 +01002206 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002207 }
2208
Takashi Iwaid45e6882012-07-31 11:36:00 +02002209 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002210 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002211 struct hda_pcm *pcm;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002212
Libin Yang022f3442016-02-03 10:48:34 +08002213 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002214 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002215 break;
Subhransu S. Prusty2f6e8a82016-03-04 19:59:51 +05302216 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002217 if (err < 0)
2218 return err;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002219 }
2220
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002221 return 0;
2222}
2223
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002224static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2225{
2226 struct hdmi_spec *spec = codec->spec;
2227 int pin_idx;
2228
2229 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002230 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002231
2232 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002233 mutex_init(&per_pin->lock);
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002234 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002235 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002236 }
2237 return 0;
2238}
2239
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002240static int generic_hdmi_init(struct hda_codec *codec)
2241{
2242 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002243 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002244
Takashi Iwai302d5a82019-10-29 21:41:20 +01002245 mutex_lock(&spec->bind_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002246 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002247 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002248 hda_nid_t pin_nid = per_pin->pin_nid;
Libin Yang91520852017-01-12 16:04:53 +08002249 int dev_id = per_pin->dev_id;
Stephen Warren384a48d2011-06-01 11:14:21 -06002250
Libin Yang91520852017-01-12 16:04:53 +08002251 snd_hda_set_dev_select(codec, pin_nid, dev_id);
Stephen Warren384a48d2011-06-01 11:14:21 -06002252 hdmi_init_pin(codec, pin_nid);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002253 if (codec_has_acomp(codec))
2254 continue;
Takashi Iwaidb845402020-02-06 17:28:02 +01002255 snd_hda_jack_detect_enable_callback_mst(codec, pin_nid, dev_id,
2256 jack_callback);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002257 }
Takashi Iwai302d5a82019-10-29 21:41:20 +01002258 mutex_unlock(&spec->bind_lock);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002259 return 0;
2260}
2261
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002262static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2263{
2264 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2265 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002266}
2267
2268static void hdmi_array_free(struct hdmi_spec *spec)
2269{
2270 snd_array_free(&spec->pins);
2271 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002272}
2273
Takashi Iwaia6866322016-03-21 12:18:33 +01002274static void generic_spec_free(struct hda_codec *codec)
2275{
2276 struct hdmi_spec *spec = codec->spec;
2277
2278 if (spec) {
2279 hdmi_array_free(spec);
2280 kfree(spec);
2281 codec->spec = NULL;
2282 }
2283 codec->dp_mst = false;
2284}
2285
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002286static void generic_hdmi_free(struct hda_codec *codec)
2287{
2288 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002289 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002290
Takashi Iwaiade49db2018-07-11 18:05:52 +02002291 if (spec->acomp_registered) {
2292 snd_hdac_acomp_exit(&codec->bus->core);
2293 } else if (codec_has_acomp(codec)) {
Takashi Iwaia57942b2018-07-11 16:23:16 +02002294 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
Takashi Iwai4914da22019-07-16 08:56:51 +02002295 }
Takashi Iwai10a95942019-11-22 14:26:24 +01002296 codec->relaxed_resume = 0;
David Henningsson25adc132015-08-19 10:48:58 +02002297
Stephen Warren384a48d2011-06-01 11:14:21 -06002298 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002299 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002300 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002301 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002302 }
2303
2304 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2305 if (spec->pcm_rec[pcm_idx].jack == NULL)
2306 continue;
2307 if (spec->dyn_pcm_assign)
2308 snd_device_free(codec->card,
2309 spec->pcm_rec[pcm_idx].jack);
2310 else
2311 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002312 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002313
Takashi Iwaia6866322016-03-21 12:18:33 +01002314 generic_spec_free(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002315}
2316
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002317#ifdef CONFIG_PM
2318static int generic_hdmi_resume(struct hda_codec *codec)
2319{
2320 struct hdmi_spec *spec = codec->spec;
2321 int pin_idx;
2322
Pierre Ossmana2833682014-06-18 21:48:09 +02002323 codec->patch_ops.init(codec);
Takashi Iwai1a462be2020-01-09 10:01:04 +01002324 snd_hda_regmap_sync(codec);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002325
2326 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2327 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2328 hdmi_present_sense(per_pin, 1);
2329 }
2330 return 0;
2331}
2332#endif
2333
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002334static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002335 .init = generic_hdmi_init,
2336 .free = generic_hdmi_free,
2337 .build_pcms = generic_hdmi_build_pcms,
2338 .build_controls = generic_hdmi_build_controls,
2339 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002340#ifdef CONFIG_PM
2341 .resume = generic_hdmi_resume,
2342#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002343};
2344
Anssi Hannula307229d2013-10-24 21:10:34 +03002345static const struct hdmi_ops generic_standard_hdmi_ops = {
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05302346 .pin_get_eld = hdmi_pin_get_eld,
Anssi Hannula307229d2013-10-24 21:10:34 +03002347 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2348 .pin_hbr_setup = hdmi_pin_hbr_setup,
2349 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302350};
2351
Takashi Iwaia6866322016-03-21 12:18:33 +01002352/* allocate codec->spec and assign/initialize generic parser ops */
2353static int alloc_generic_hdmi(struct hda_codec *codec)
2354{
2355 struct hdmi_spec *spec;
2356
2357 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2358 if (!spec)
2359 return -ENOMEM;
2360
Takashi Iwaiade49db2018-07-11 18:05:52 +02002361 spec->codec = codec;
Takashi Iwaia6866322016-03-21 12:18:33 +01002362 spec->ops = generic_standard_hdmi_ops;
Libin Yang91520852017-01-12 16:04:53 +08002363 spec->dev_num = 1; /* initialize to 1 */
Takashi Iwaia6866322016-03-21 12:18:33 +01002364 mutex_init(&spec->pcm_lock);
Takashi Iwai302d5a82019-10-29 21:41:20 +01002365 mutex_init(&spec->bind_lock);
Takashi Iwaia6866322016-03-21 12:18:33 +01002366 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2367
2368 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2369 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2370 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
Subhransu S. Prusty44fde3b2016-04-04 19:23:54 +05302371 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
Takashi Iwaia6866322016-03-21 12:18:33 +01002372
2373 codec->spec = spec;
2374 hdmi_array_init(spec, 4);
2375
2376 codec->patch_ops = generic_hdmi_patch_ops;
2377
2378 return 0;
2379}
2380
2381/* generic HDMI parser */
2382static int patch_generic_hdmi(struct hda_codec *codec)
2383{
2384 int err;
2385
2386 err = alloc_generic_hdmi(codec);
2387 if (err < 0)
2388 return err;
2389
2390 err = hdmi_parse_codec(codec);
2391 if (err < 0) {
2392 generic_spec_free(codec);
2393 return err;
2394 }
2395
2396 generic_hdmi_init_per_pins(codec);
2397 return 0;
2398}
2399
2400/*
Takashi Iwaiade49db2018-07-11 18:05:52 +02002401 * generic audio component binding
2402 */
2403
2404/* turn on / off the unsol event jack detection dynamically */
2405static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
Nikhil Mahale5204a052019-11-19 14:17:08 +05302406 int dev_id, bool use_acomp)
Takashi Iwaiade49db2018-07-11 18:05:52 +02002407{
2408 struct hda_jack_tbl *tbl;
2409
Nikhil Mahale5204a052019-11-19 14:17:08 +05302410 tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002411 if (tbl) {
2412 /* clear unsol even if component notifier is used, or re-enable
2413 * if notifier is cleared
2414 */
2415 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2416 snd_hda_codec_write_cache(codec, nid, 0,
2417 AC_VERB_SET_UNSOLICITED_ENABLE, val);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002418 }
2419}
2420
2421/* set up / clear component notifier dynamically */
2422static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2423 bool use_acomp)
2424{
2425 struct hdmi_spec *spec;
2426 int i;
2427
2428 spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
Takashi Iwai302d5a82019-10-29 21:41:20 +01002429 mutex_lock(&spec->bind_lock);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002430 spec->use_acomp_notifier = use_acomp;
2431 spec->codec->relaxed_resume = use_acomp;
2432 /* reprogram each jack detection logic depending on the notifier */
Takashi Iwaidb845402020-02-06 17:28:02 +01002433 for (i = 0; i < spec->num_pins; i++)
2434 reprogram_jack_detect(spec->codec,
2435 get_pin(spec, i)->pin_nid,
2436 get_pin(spec, i)->dev_id,
2437 use_acomp);
Takashi Iwai302d5a82019-10-29 21:41:20 +01002438 mutex_unlock(&spec->bind_lock);
Takashi Iwaiade49db2018-07-11 18:05:52 +02002439}
2440
2441/* enable / disable the notifier via master bind / unbind */
2442static int generic_acomp_master_bind(struct device *dev,
2443 struct drm_audio_component *acomp)
2444{
2445 generic_acomp_notifier_set(acomp, true);
2446 return 0;
2447}
2448
2449static void generic_acomp_master_unbind(struct device *dev,
2450 struct drm_audio_component *acomp)
2451{
2452 generic_acomp_notifier_set(acomp, false);
2453}
2454
2455/* check whether both HD-audio and DRM PCI devices belong to the same bus */
2456static int match_bound_vga(struct device *dev, int subtype, void *data)
2457{
2458 struct hdac_bus *bus = data;
2459 struct pci_dev *pci, *master;
2460
2461 if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2462 return 0;
2463 master = to_pci_dev(bus->dev);
2464 pci = to_pci_dev(dev);
2465 return master->bus == pci->bus;
2466}
2467
2468/* audio component notifier for AMD/Nvidia HDMI codecs */
2469static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2470{
2471 struct hda_codec *codec = audio_ptr;
2472 struct hdmi_spec *spec = codec->spec;
2473 hda_nid_t pin_nid = spec->port2pin(codec, port);
2474
2475 if (!pin_nid)
2476 return;
2477 if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2478 return;
2479 /* skip notification during system suspend (but not in runtime PM);
2480 * the state will be updated at resume
2481 */
2482 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2483 return;
2484 /* ditto during suspend/resume process itself */
2485 if (snd_hdac_is_in_pm(&codec->core))
2486 return;
2487
2488 check_presence_and_report(codec, pin_nid, dev_id);
2489}
2490
2491/* set up the private drm_audio_ops from the template */
2492static void setup_drm_audio_ops(struct hda_codec *codec,
2493 const struct drm_audio_component_audio_ops *ops)
2494{
2495 struct hdmi_spec *spec = codec->spec;
2496
2497 spec->drm_audio_ops.audio_ptr = codec;
2498 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2499 * will call pin_eld_notify with using audio_ptr pointer
2500 * We need make sure audio_ptr is really setup
2501 */
2502 wmb();
2503 spec->drm_audio_ops.pin2port = ops->pin2port;
2504 spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2505 spec->drm_audio_ops.master_bind = ops->master_bind;
2506 spec->drm_audio_ops.master_unbind = ops->master_unbind;
2507}
2508
2509/* initialize the generic HDMI audio component */
2510static void generic_acomp_init(struct hda_codec *codec,
2511 const struct drm_audio_component_audio_ops *ops,
2512 int (*port2pin)(struct hda_codec *, int))
2513{
2514 struct hdmi_spec *spec = codec->spec;
2515
Takashi Iwaib3923502020-04-15 18:25:23 +02002516 if (!enable_acomp) {
2517 codec_info(codec, "audio component disabled by module option\n");
2518 return;
2519 }
2520
Takashi Iwaiade49db2018-07-11 18:05:52 +02002521 spec->port2pin = port2pin;
2522 setup_drm_audio_ops(codec, ops);
2523 if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
Takashi Iwaidd23e1d2019-08-27 16:37:50 +02002524 match_bound_vga, 0)) {
Takashi Iwaiade49db2018-07-11 18:05:52 +02002525 spec->acomp_registered = true;
Takashi Iwaidd23e1d2019-08-27 16:37:50 +02002526 codec->bus->keep_power = 0;
2527 }
Takashi Iwaiade49db2018-07-11 18:05:52 +02002528}
2529
2530/*
Takashi Iwaia6866322016-03-21 12:18:33 +01002531 * Intel codec parsers and helpers
2532 */
2533
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002534#define INTEL_GET_VENDOR_VERB 0xf81
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002535#define INTEL_SET_VENDOR_VERB 0x781
2536#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2537#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002538
2539static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002540 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002541{
2542 unsigned int vendor_param;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302543 struct hdmi_spec *spec = codec->spec;
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002544
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302545 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002546 INTEL_GET_VENDOR_VERB, 0);
2547 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2548 return;
2549
2550 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302551 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002552 INTEL_SET_VENDOR_VERB, vendor_param);
2553 if (vendor_param == -1)
2554 return;
2555
Takashi Iwai17df3f52013-05-08 08:09:34 +02002556 if (update_tree)
2557 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002558}
2559
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002560static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2561{
2562 unsigned int vendor_param;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302563 struct hdmi_spec *spec = codec->spec;
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002564
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302565 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002566 INTEL_GET_VENDOR_VERB, 0);
2567 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2568 return;
2569
2570 /* enable DP1.2 mode */
2571 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002572 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302573 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002574 INTEL_SET_VENDOR_VERB, vendor_param);
2575}
2576
Takashi Iwai17df3f52013-05-08 08:09:34 +02002577/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2578 * Otherwise you may get severe h/w communication errors.
2579 */
2580static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2581 unsigned int power_state)
2582{
2583 if (power_state == AC_PWRST_D0) {
2584 intel_haswell_enable_all_pins(codec, false);
2585 intel_haswell_fixup_enable_dp12(codec);
2586 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002587
Takashi Iwai17df3f52013-05-08 08:09:34 +02002588 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2589 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2590}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002591
Takashi Iwaia57942b2018-07-11 16:23:16 +02002592/* There is a fixed mapping between audio pin node and display port.
2593 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2594 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2595 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2596 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2597 *
2598 * on VLV, ILK:
2599 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2600 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2601 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2602 */
2603static int intel_base_nid(struct hda_codec *codec)
2604{
2605 switch (codec->core.vendor_id) {
2606 case 0x80860054: /* ILK */
2607 case 0x80862804: /* ILK */
2608 case 0x80862882: /* VLV */
2609 return 4;
2610 default:
2611 return 5;
2612 }
2613}
2614
2615static int intel_pin2port(void *audio_ptr, int pin_nid)
2616{
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002617 struct hda_codec *codec = audio_ptr;
2618 struct hdmi_spec *spec = codec->spec;
2619 int base_nid, i;
Takashi Iwaia57942b2018-07-11 16:23:16 +02002620
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002621 if (!spec->port_num) {
2622 base_nid = intel_base_nid(codec);
2623 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2624 return -1;
Kai Vehmanenc8e3eb92019-11-15 14:44:48 +02002625 return pin_nid - base_nid + 1;
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002626 }
2627
2628 /*
2629 * looking for the pin number in the mapping table and return
2630 * the index which indicate the port number
2631 */
2632 for (i = 0; i < spec->port_num; i++) {
2633 if (pin_nid == spec->port_map[i])
Kai Vehmanend577cf72019-11-15 14:44:47 +02002634 return i;
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002635 }
2636
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002637 codec_info(codec, "Can't find the HDMI/DP port for pin %d\n", pin_nid);
2638 return -1;
Takashi Iwaia57942b2018-07-11 16:23:16 +02002639}
2640
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002641static int intel_port2pin(struct hda_codec *codec, int port)
2642{
2643 struct hdmi_spec *spec = codec->spec;
2644
2645 if (!spec->port_num) {
2646 /* we assume only from port-B to port-D */
2647 if (port < 1 || port > 3)
2648 return 0;
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002649 return port + intel_base_nid(codec) - 1;
2650 }
2651
Kai Vehmanend577cf72019-11-15 14:44:47 +02002652 if (port < 0 || port >= spec->port_num)
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002653 return 0;
Kai Vehmanend577cf72019-11-15 14:44:47 +02002654 return spec->port_map[port];
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002655}
2656
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07002657static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
David Henningsson25adc132015-08-19 10:48:58 +02002658{
2659 struct hda_codec *codec = audio_ptr;
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002660 int pin_nid;
Libin Yang91520852017-01-12 16:04:53 +08002661 int dev_id = pipe;
David Henningsson25adc132015-08-19 10:48:58 +02002662
Takashi Iwai3140aaf2019-07-15 23:14:53 +02002663 pin_nid = intel_port2pin(codec, port);
2664 if (!pin_nid)
Takashi Iwai4f8e4f32016-03-10 12:02:49 +01002665 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002666 /* skip notification during system suspend (but not in runtime PM);
2667 * the state will be updated at resume
2668 */
2669 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2670 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002671 /* ditto during suspend/resume process itself */
Takashi Iwaifeb20fa2018-06-27 09:03:51 +02002672 if (snd_hdac_is_in_pm(&codec->core))
Takashi Iwaieb399d32015-11-27 14:53:35 +01002673 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002674
Takashi Iwaibb03ed22016-04-21 16:39:17 +02002675 snd_hdac_i915_set_bclk(&codec->bus->core);
Libin Yang91520852017-01-12 16:04:53 +08002676 check_presence_and_report(codec, pin_nid, dev_id);
David Henningsson25adc132015-08-19 10:48:58 +02002677}
2678
Takashi Iwaiade49db2018-07-11 18:05:52 +02002679static const struct drm_audio_component_audio_ops intel_audio_ops = {
2680 .pin2port = intel_pin2port,
2681 .pin_eld_notify = intel_pin_eld_notify,
2682};
2683
Takashi Iwaia6866322016-03-21 12:18:33 +01002684/* register i915 component pin_eld_notify callback */
2685static void register_i915_notifier(struct hda_codec *codec)
2686{
2687 struct hdmi_spec *spec = codec->spec;
2688
2689 spec->use_acomp_notifier = true;
Takashi Iwaiade49db2018-07-11 18:05:52 +02002690 spec->port2pin = intel_port2pin;
2691 setup_drm_audio_ops(codec, &intel_audio_ops);
Takashi Iwaia57942b2018-07-11 16:23:16 +02002692 snd_hdac_acomp_register_notifier(&codec->bus->core,
Takashi Iwai82887c02018-07-11 15:48:18 +02002693 &spec->drm_audio_ops);
Takashi Iwai4914da22019-07-16 08:56:51 +02002694 /* no need for forcible resume for jack check thanks to notifier */
2695 codec->relaxed_resume = 1;
Takashi Iwaia6866322016-03-21 12:18:33 +01002696}
2697
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002698/* setup_stream ops override for HSW+ */
2699static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05302700 hda_nid_t pin_nid, int dev_id, u32 stream_tag,
2701 int format)
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002702{
2703 haswell_verify_D0(codec, cvt_nid, pin_nid);
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05302704 return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
2705 stream_tag, format);
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002706}
2707
Takashi Iwai4846a672016-03-21 12:56:46 +01002708/* pin_cvt_fixup ops override for HSW+ and VLV+ */
2709static void i915_pin_cvt_fixup(struct hda_codec *codec,
2710 struct hdmi_spec_per_pin *per_pin,
2711 hda_nid_t cvt_nid)
2712{
2713 if (per_pin) {
Libin Yang91520852017-01-12 16:04:53 +08002714 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2715 per_pin->dev_id);
Takashi Iwai4846a672016-03-21 12:56:46 +01002716 intel_verify_pin_cvt_connect(codec, per_pin);
2717 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
Libin Yang91520852017-01-12 16:04:53 +08002718 per_pin->dev_id, per_pin->mux_idx);
Takashi Iwai4846a672016-03-21 12:56:46 +01002719 } else {
Libin Yang91520852017-01-12 16:04:53 +08002720 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
Takashi Iwai4846a672016-03-21 12:56:46 +01002721 }
2722}
2723
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002724/* precondition and allocation for Intel codecs */
2725static int alloc_intel_hdmi(struct hda_codec *codec)
2726{
Takashi Iwaif2dbe872019-08-13 17:11:28 +02002727 int err;
2728
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002729 /* requires i915 binding */
2730 if (!codec->bus->core.audio_component) {
2731 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
Takashi Iwaifdd49c52018-06-27 09:54:46 +02002732 /* set probe_id here to prevent generic fallback binding */
2733 codec->probe_id = HDA_CODEC_ID_SKIP_PROBE;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002734 return -ENODEV;
2735 }
2736
Takashi Iwaif2dbe872019-08-13 17:11:28 +02002737 err = alloc_generic_hdmi(codec);
2738 if (err < 0)
2739 return err;
2740 /* no need to handle unsol events */
2741 codec->patch_ops.unsol_event = NULL;
2742 return 0;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002743}
2744
2745/* parse and post-process for Intel codecs */
2746static int parse_intel_hdmi(struct hda_codec *codec)
2747{
Kai Vehmanen2928fa02020-01-20 18:01:17 +02002748 int err, retries = 3;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002749
Kai Vehmanen2928fa02020-01-20 18:01:17 +02002750 do {
2751 err = hdmi_parse_codec(codec);
2752 } while (err < 0 && retries--);
2753
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002754 if (err < 0) {
2755 generic_spec_free(codec);
2756 return err;
2757 }
2758
2759 generic_hdmi_init_per_pins(codec);
2760 register_i915_notifier(codec);
2761 return 0;
2762}
2763
Takashi Iwaia6866322016-03-21 12:18:33 +01002764/* Intel Haswell and onwards; audio component with eld notifier */
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002765static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2766 const int *port_map, int port_num)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002767{
2768 struct hdmi_spec *spec;
Takashi Iwaia6866322016-03-21 12:18:33 +01002769 int err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002770
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002771 err = alloc_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002772 if (err < 0)
2773 return err;
2774 spec = codec->spec;
Libin Yang91520852017-01-12 16:04:53 +08002775 codec->dp_mst = true;
2776 spec->dyn_pcm_assign = true;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302777 spec->vendor_nid = vendor_nid;
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002778 spec->port_map = port_map;
2779 spec->port_num = port_num;
Takashi Iwaicb457222019-11-11 20:09:37 +01002780 spec->intel_hsw_fixup = true;
Takashi Iwaia6866322016-03-21 12:18:33 +01002781
2782 intel_haswell_enable_all_pins(codec, true);
2783 intel_haswell_fixup_enable_dp12(codec);
2784
Takashi Iwai46594d32018-12-09 10:10:19 +01002785 codec->display_power_control = 1;
Takashi Iwaia6866322016-03-21 12:18:33 +01002786
2787 codec->patch_ops.set_power_state = haswell_set_power_state;
Takashi Iwaia6866322016-03-21 12:18:33 +01002788 codec->depop_delay = 0;
2789 codec->auto_runtime_pm = 1;
2790
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002791 spec->ops.setup_stream = i915_hsw_setup_stream;
Takashi Iwai4846a672016-03-21 12:56:46 +01002792 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002793
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002794 return parse_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002795}
2796
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302797static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2798{
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002799 return intel_hsw_common_init(codec, 0x08, NULL, 0);
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302800}
2801
2802static int patch_i915_glk_hdmi(struct hda_codec *codec)
2803{
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002804 return intel_hsw_common_init(codec, 0x0b, NULL, 0);
2805}
2806
2807static int patch_i915_icl_hdmi(struct hda_codec *codec)
2808{
2809 /*
2810 * pin to port mapping table where the value indicate the pin number and
Kai Vehmanend577cf72019-11-15 14:44:47 +02002811 * the index indicate the port number.
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002812 */
Kai Vehmanend577cf72019-11-15 14:44:47 +02002813 static const int map[] = {0x0, 0x4, 0x6, 0x8, 0xa, 0xb};
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01002814
2815 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302816}
2817
Kai Vehmanen9a11ba72019-11-05 18:10:53 +02002818static int patch_i915_tgl_hdmi(struct hda_codec *codec)
2819{
2820 /*
2821 * pin to port mapping table where the value indicate the pin number and
Kai Vehmanend577cf72019-11-15 14:44:47 +02002822 * the index indicate the port number.
Kai Vehmanen9a11ba72019-11-05 18:10:53 +02002823 */
2824 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
2825
2826 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2827}
2828
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002829/* Intel Baytrail and Braswell; with eld notifier */
Takashi Iwaia6866322016-03-21 12:18:33 +01002830static int patch_i915_byt_hdmi(struct hda_codec *codec)
2831{
2832 struct hdmi_spec *spec;
2833 int err;
2834
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002835 err = alloc_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002836 if (err < 0)
2837 return err;
2838 spec = codec->spec;
2839
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002840 /* For Valleyview/Cherryview, only the display codec is in the display
2841 * power well and can use link_power ops to request/release the power.
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002842 */
Takashi Iwai029d92c2018-12-08 17:31:49 +01002843 codec->display_power_control = 1;
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002844
Takashi Iwaia6866322016-03-21 12:18:33 +01002845 codec->depop_delay = 0;
2846 codec->auto_runtime_pm = 1;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002847
Takashi Iwai4846a672016-03-21 12:56:46 +01002848 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2849
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002850 return parse_intel_hdmi(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002851}
2852
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002853/* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
Takashi Iwaie85015a32016-03-21 13:56:19 +01002854static int patch_i915_cpt_hdmi(struct hda_codec *codec)
2855{
Takashi Iwaie85015a32016-03-21 13:56:19 +01002856 int err;
2857
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002858 err = alloc_intel_hdmi(codec);
Takashi Iwaie85015a32016-03-21 13:56:19 +01002859 if (err < 0)
2860 return err;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002861 return parse_intel_hdmi(codec);
Takashi Iwaie85015a32016-03-21 13:56:19 +01002862}
2863
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002864/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002865 * Shared non-generic implementations
2866 */
2867
2868static int simple_playback_build_pcms(struct hda_codec *codec)
2869{
2870 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002871 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002872 unsigned int chans;
2873 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002874 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002875
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002876 per_cvt = get_cvt(spec, 0);
2877 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002878 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002879
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002880 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002881 if (!info)
2882 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002883 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002884 info->pcm_type = HDA_PCM_TYPE_HDMI;
2885 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2886 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002887 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002888 if (pstr->channels_max <= 2 && chans && chans <= 16)
2889 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002890
2891 return 0;
2892}
2893
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002894/* unsolicited event for jack sensing */
2895static void simple_hdmi_unsol_event(struct hda_codec *codec,
2896 unsigned int res)
2897{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002898 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002899 snd_hda_jack_report_sync(codec);
2900}
2901
2902/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2903 * as long as spec->pins[] is set correctly
2904 */
2905#define simple_hdmi_build_jack generic_hdmi_build_jack
2906
Stephen Warren3aaf8982011-06-01 11:14:19 -06002907static int simple_playback_build_controls(struct hda_codec *codec)
2908{
2909 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002910 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002911 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002912
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002913 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002914 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2915 per_cvt->cvt_nid,
2916 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002917 if (err < 0)
2918 return err;
2919 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002920}
2921
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002922static int simple_playback_init(struct hda_codec *codec)
2923{
2924 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002925 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2926 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002927
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002928 snd_hda_codec_write(codec, pin, 0,
2929 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2930 /* some codecs require to unmute the pin */
2931 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2932 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2933 AMP_OUT_UNMUTE);
Nikhil Mahale5204a052019-11-19 14:17:08 +05302934 snd_hda_jack_detect_enable(codec, pin, per_pin->dev_id);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002935 return 0;
2936}
2937
Stephen Warren3aaf8982011-06-01 11:14:19 -06002938static void simple_playback_free(struct hda_codec *codec)
2939{
2940 struct hdmi_spec *spec = codec->spec;
2941
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002942 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002943 kfree(spec);
2944}
2945
2946/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002947 * Nvidia specific implementations
2948 */
2949
2950#define Nv_VERB_SET_Channel_Allocation 0xF79
2951#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2952#define Nv_VERB_SET_Audio_Protection_On 0xF98
2953#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2954
2955#define nvhdmi_master_con_nid_7x 0x04
2956#define nvhdmi_master_pin_nid_7x 0x05
2957
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002958static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002959 /*front, rear, clfe, rear_surr */
2960 0x6, 0x8, 0xa, 0xc,
2961};
2962
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002963static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2964 /* set audio protect on */
2965 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2966 /* enable digital output on pin widget */
2967 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2968 {} /* terminator */
2969};
2970
2971static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002972 /* set audio protect on */
2973 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2974 /* enable digital output on pin widget */
2975 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2976 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2977 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2978 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2979 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2980 {} /* terminator */
2981};
2982
2983#ifdef LIMITED_RATE_FMT_SUPPORT
2984/* support only the safe format and rate */
2985#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2986#define SUPPORTED_MAXBPS 16
2987#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2988#else
2989/* support all rates and formats */
2990#define SUPPORTED_RATES \
2991 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2992 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2993 SNDRV_PCM_RATE_192000)
2994#define SUPPORTED_MAXBPS 24
2995#define SUPPORTED_FORMATS \
2996 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2997#endif
2998
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002999static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003000{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003001 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3002 return 0;
3003}
3004
3005static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3006{
3007 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003008 return 0;
3009}
3010
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003011static const unsigned int channels_2_6_8[] = {
Nitin Daga393004b2011-01-10 21:49:31 +05303012 2, 6, 8
3013};
3014
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003015static const unsigned int channels_2_8[] = {
Nitin Daga393004b2011-01-10 21:49:31 +05303016 2, 8
3017};
3018
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003019static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
Nitin Daga393004b2011-01-10 21:49:31 +05303020 .count = ARRAY_SIZE(channels_2_6_8),
3021 .list = channels_2_6_8,
3022 .mask = 0,
3023};
3024
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003025static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
Nitin Daga393004b2011-01-10 21:49:31 +05303026 .count = ARRAY_SIZE(channels_2_8),
3027 .list = channels_2_8,
3028 .mask = 0,
3029};
3030
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003031static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3032 struct hda_codec *codec,
3033 struct snd_pcm_substream *substream)
3034{
3035 struct hdmi_spec *spec = codec->spec;
Takashi Iwai50c697adf2017-06-07 14:20:07 +02003036 const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
Nitin Daga393004b2011-01-10 21:49:31 +05303037
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003038 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05303039 case 0x10de0002:
3040 case 0x10de0003:
3041 case 0x10de0005:
3042 case 0x10de0006:
3043 hw_constraints_channels = &hw_constraints_2_8_channels;
3044 break;
3045 case 0x10de0007:
3046 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3047 break;
3048 default:
3049 break;
3050 }
3051
3052 if (hw_constraints_channels != NULL) {
3053 snd_pcm_hw_constraint_list(substream->runtime, 0,
3054 SNDRV_PCM_HW_PARAM_CHANNELS,
3055 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01003056 } else {
3057 snd_pcm_hw_constraint_step(substream->runtime, 0,
3058 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05303059 }
3060
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003061 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3062}
3063
3064static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3065 struct hda_codec *codec,
3066 struct snd_pcm_substream *substream)
3067{
3068 struct hdmi_spec *spec = codec->spec;
3069 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3070}
3071
3072static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3073 struct hda_codec *codec,
3074 unsigned int stream_tag,
3075 unsigned int format,
3076 struct snd_pcm_substream *substream)
3077{
3078 struct hdmi_spec *spec = codec->spec;
3079 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3080 stream_tag, format, substream);
3081}
3082
Takashi Iwaid0b12522012-06-15 14:34:42 +02003083static const struct hda_pcm_stream simple_pcm_playback = {
3084 .substreams = 1,
3085 .channels_min = 2,
3086 .channels_max = 2,
3087 .ops = {
3088 .open = simple_playback_pcm_open,
3089 .close = simple_playback_pcm_close,
3090 .prepare = simple_playback_pcm_prepare
3091 },
3092};
3093
3094static const struct hda_codec_ops simple_hdmi_patch_ops = {
3095 .build_controls = simple_playback_build_controls,
3096 .build_pcms = simple_playback_build_pcms,
3097 .init = simple_playback_init,
3098 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02003099 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02003100};
3101
3102static int patch_simple_hdmi(struct hda_codec *codec,
3103 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3104{
3105 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003106 struct hdmi_spec_per_cvt *per_cvt;
3107 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003108
3109 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3110 if (!spec)
3111 return -ENOMEM;
3112
Takashi Iwaiade49db2018-07-11 18:05:52 +02003113 spec->codec = codec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003114 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003115 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02003116
3117 spec->multiout.num_dacs = 0; /* no analog */
3118 spec->multiout.max_channels = 2;
3119 spec->multiout.dig_out_nid = cvt_nid;
3120 spec->num_cvts = 1;
3121 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003122 per_pin = snd_array_new(&spec->pins);
3123 per_cvt = snd_array_new(&spec->cvts);
3124 if (!per_pin || !per_cvt) {
3125 simple_playback_free(codec);
3126 return -ENOMEM;
3127 }
3128 per_cvt->cvt_nid = cvt_nid;
3129 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003130 spec->pcm_playback = simple_pcm_playback;
3131
3132 codec->patch_ops = simple_hdmi_patch_ops;
3133
3134 return 0;
3135}
3136
Aaron Plattner1f348522011-04-06 17:19:04 -07003137static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3138 int channels)
3139{
3140 unsigned int chanmask;
3141 int chan = channels ? (channels - 1) : 1;
3142
3143 switch (channels) {
3144 default:
3145 case 0:
3146 case 2:
3147 chanmask = 0x00;
3148 break;
3149 case 4:
3150 chanmask = 0x08;
3151 break;
3152 case 6:
3153 chanmask = 0x0b;
3154 break;
3155 case 8:
3156 chanmask = 0x13;
3157 break;
3158 }
3159
3160 /* Set the audio infoframe channel allocation and checksum fields. The
3161 * channel count is computed implicitly by the hardware. */
3162 snd_hda_codec_write(codec, 0x1, 0,
3163 Nv_VERB_SET_Channel_Allocation, chanmask);
3164
3165 snd_hda_codec_write(codec, 0x1, 0,
3166 Nv_VERB_SET_Info_Frame_Checksum,
3167 (0x71 - chan - chanmask));
3168}
3169
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003170static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3171 struct hda_codec *codec,
3172 struct snd_pcm_substream *substream)
3173{
3174 struct hdmi_spec *spec = codec->spec;
3175 int i;
3176
3177 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3178 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3179 for (i = 0; i < 4; i++) {
3180 /* set the stream id */
3181 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3182 AC_VERB_SET_CHANNEL_STREAMID, 0);
3183 /* set the stream format */
3184 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3185 AC_VERB_SET_STREAM_FORMAT, 0);
3186 }
3187
Aaron Plattner1f348522011-04-06 17:19:04 -07003188 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3189 * streams are disabled. */
3190 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3191
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003192 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3193}
3194
3195static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3196 struct hda_codec *codec,
3197 unsigned int stream_tag,
3198 unsigned int format,
3199 struct snd_pcm_substream *substream)
3200{
3201 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003202 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003203 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003204 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003205 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003206 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003207
3208 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003209 per_cvt = get_cvt(spec, 0);
3210 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003211
3212 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003213
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003214 dataDCC2 = 0x2;
3215
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003216 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003217 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003218 snd_hda_codec_write(codec,
3219 nvhdmi_master_con_nid_7x,
3220 0,
3221 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003222 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003223
3224 /* set the stream id */
3225 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3226 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3227
3228 /* set the stream format */
3229 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3230 AC_VERB_SET_STREAM_FORMAT, format);
3231
3232 /* turn on again (if needed) */
3233 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003234 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003235 snd_hda_codec_write(codec,
3236 nvhdmi_master_con_nid_7x,
3237 0,
3238 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003239 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003240 snd_hda_codec_write(codec,
3241 nvhdmi_master_con_nid_7x,
3242 0,
3243 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3244 }
3245
3246 for (i = 0; i < 4; i++) {
3247 if (chs == 2)
3248 channel_id = 0;
3249 else
3250 channel_id = i * 2;
3251
3252 /* turn off SPDIF once;
3253 *otherwise the IEC958 bits won't be updated
3254 */
3255 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003256 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003257 snd_hda_codec_write(codec,
3258 nvhdmi_con_nids_7x[i],
3259 0,
3260 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003261 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003262 /* set the stream id */
3263 snd_hda_codec_write(codec,
3264 nvhdmi_con_nids_7x[i],
3265 0,
3266 AC_VERB_SET_CHANNEL_STREAMID,
3267 (stream_tag << 4) | channel_id);
3268 /* set the stream format */
3269 snd_hda_codec_write(codec,
3270 nvhdmi_con_nids_7x[i],
3271 0,
3272 AC_VERB_SET_STREAM_FORMAT,
3273 format);
3274 /* turn on again (if needed) */
3275 /* enable and set the channel status audio/data flag */
3276 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003277 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003278 snd_hda_codec_write(codec,
3279 nvhdmi_con_nids_7x[i],
3280 0,
3281 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003282 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003283 snd_hda_codec_write(codec,
3284 nvhdmi_con_nids_7x[i],
3285 0,
3286 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3287 }
3288 }
3289
Aaron Plattner1f348522011-04-06 17:19:04 -07003290 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003291
3292 mutex_unlock(&codec->spdif_mutex);
3293 return 0;
3294}
3295
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003296static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003297 .substreams = 1,
3298 .channels_min = 2,
3299 .channels_max = 8,
3300 .nid = nvhdmi_master_con_nid_7x,
3301 .rates = SUPPORTED_RATES,
3302 .maxbps = SUPPORTED_MAXBPS,
3303 .formats = SUPPORTED_FORMATS,
3304 .ops = {
3305 .open = simple_playback_pcm_open,
3306 .close = nvhdmi_8ch_7x_pcm_close,
3307 .prepare = nvhdmi_8ch_7x_pcm_prepare
3308 },
3309};
3310
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003311static int patch_nvhdmi_2ch(struct hda_codec *codec)
3312{
3313 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003314 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3315 nvhdmi_master_pin_nid_7x);
3316 if (err < 0)
3317 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003318
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003319 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003320 /* override the PCM rates, etc, as the codec doesn't give full list */
3321 spec = codec->spec;
3322 spec->pcm_playback.rates = SUPPORTED_RATES;
3323 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3324 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003325 return 0;
3326}
3327
Takashi Iwai53775b02012-08-01 12:17:41 +02003328static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3329{
3330 struct hdmi_spec *spec = codec->spec;
3331 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003332 if (!err) {
3333 struct hda_pcm *info = get_pcm_rec(spec, 0);
3334 info->own_chmap = true;
3335 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003336 return err;
3337}
3338
3339static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3340{
3341 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003342 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003343 struct snd_pcm_chmap *chmap;
3344 int err;
3345
3346 err = simple_playback_build_controls(codec);
3347 if (err < 0)
3348 return err;
3349
3350 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003351 info = get_pcm_rec(spec, 0);
3352 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003353 SNDRV_PCM_STREAM_PLAYBACK,
3354 snd_pcm_alt_chmaps, 8, 0, &chmap);
3355 if (err < 0)
3356 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003357 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003358 case 0x10de0002:
3359 case 0x10de0003:
3360 case 0x10de0005:
3361 case 0x10de0006:
3362 chmap->channel_mask = (1U << 2) | (1U << 8);
3363 break;
3364 case 0x10de0007:
3365 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3366 }
3367 return 0;
3368}
3369
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003370static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3371{
3372 struct hdmi_spec *spec;
3373 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003374 if (err < 0)
3375 return err;
3376 spec = codec->spec;
3377 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003378 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003379 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003380 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3381 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003382
3383 /* Initialize the audio infoframe channel mask and checksum to something
3384 * valid */
3385 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3386
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003387 return 0;
3388}
3389
3390/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003391 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3392 * - 0x10de0015
3393 * - 0x10de0040
3394 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303395static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303396 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02003397{
3398 if (cap->ca_index == 0x00 && channels == 2)
3399 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3400
Subhransu S. Prusty028cb682016-03-14 10:35:06 +05303401 /* If the speaker allocation matches the channel count, it is OK. */
3402 if (cap->channels != channels)
3403 return -1;
3404
3405 /* all channels are remappable freely */
3406 return SNDRV_CTL_TLVT_CHMAP_VAR;
Anssi Hannula611885b2013-11-03 17:15:00 +02003407}
3408
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303409static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3410 int ca, int chs, unsigned char *map)
Anssi Hannula611885b2013-11-03 17:15:00 +02003411{
3412 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3413 return -EINVAL;
3414
3415 return 0;
3416}
3417
Takashi Iwaiade49db2018-07-11 18:05:52 +02003418/* map from pin NID to port; port is 0-based */
3419/* for Nvidia: assume widget NID starting from 4, with step 1 (4, 5, 6, ...) */
3420static int nvhdmi_pin2port(void *audio_ptr, int pin_nid)
3421{
3422 return pin_nid - 4;
3423}
3424
3425/* reverse-map from port to pin NID: see above */
3426static int nvhdmi_port2pin(struct hda_codec *codec, int port)
3427{
3428 return port + 4;
3429}
3430
3431static const struct drm_audio_component_audio_ops nvhdmi_audio_ops = {
3432 .pin2port = nvhdmi_pin2port,
3433 .pin_eld_notify = generic_acomp_pin_eld_notify,
3434 .master_bind = generic_acomp_master_bind,
3435 .master_unbind = generic_acomp_master_unbind,
3436};
3437
Anssi Hannula611885b2013-11-03 17:15:00 +02003438static int patch_nvhdmi(struct hda_codec *codec)
3439{
3440 struct hdmi_spec *spec;
3441 int err;
3442
Nikhil Mahale5398e942019-11-19 14:17:10 +05303443 err = alloc_generic_hdmi(codec);
3444 if (err < 0)
3445 return err;
3446 codec->dp_mst = true;
3447
3448 spec = codec->spec;
3449 spec->dyn_pcm_assign = true;
3450
3451 err = hdmi_parse_codec(codec);
3452 if (err < 0) {
3453 generic_spec_free(codec);
3454 return err;
3455 }
3456
3457 generic_hdmi_init_per_pins(codec);
3458
3459 spec->dyn_pin_out = true;
3460
3461 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3462 nvhdmi_chmap_cea_alloc_validate_get_type;
3463 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3464
3465 codec->link_down_at_suspend = 1;
3466
3467 generic_acomp_init(codec, &nvhdmi_audio_ops, nvhdmi_port2pin);
3468
3469 return 0;
3470}
3471
3472static int patch_nvhdmi_legacy(struct hda_codec *codec)
3473{
3474 struct hdmi_spec *spec;
3475 int err;
3476
Anssi Hannula611885b2013-11-03 17:15:00 +02003477 err = patch_generic_hdmi(codec);
3478 if (err)
3479 return err;
3480
3481 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003482 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003483
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303484 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02003485 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303486 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02003487
Lukas Wunner94989e32019-10-17 17:04:11 +02003488 codec->link_down_at_suspend = 1;
3489
Anssi Hannula611885b2013-11-03 17:15:00 +02003490 return 0;
3491}
3492
3493/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003494 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3495 * accessed using vendor-defined verbs. These registers can be used for
3496 * interoperability between the HDA and HDMI drivers.
3497 */
3498
3499/* Audio Function Group node */
3500#define NVIDIA_AFG_NID 0x01
3501
3502/*
3503 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3504 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3505 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3506 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3507 * additional bit (at position 30) to signal the validity of the format.
3508 *
3509 * | 31 | 30 | 29 16 | 15 0 |
3510 * +---------+-------+--------+--------+
3511 * | TRIGGER | VALID | UNUSED | FORMAT |
3512 * +-----------------------------------|
3513 *
3514 * Note that for the trigger bit to take effect it needs to change value
3515 * (i.e. it needs to be toggled).
3516 */
3517#define NVIDIA_GET_SCRATCH0 0xfa6
3518#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3519#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3520#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3521#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3522#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3523#define NVIDIA_SCRATCH_VALID (1 << 6)
3524
3525#define NVIDIA_GET_SCRATCH1 0xfab
3526#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3527#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3528#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3529#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3530
3531/*
3532 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3533 * the format is invalidated so that the HDMI codec can be disabled.
3534 */
3535static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3536{
3537 unsigned int value;
3538
3539 /* bits [31:30] contain the trigger and valid bits */
3540 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3541 NVIDIA_GET_SCRATCH0, 0);
3542 value = (value >> 24) & 0xff;
3543
3544 /* bits [15:0] are used to store the HDA format */
3545 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3546 NVIDIA_SET_SCRATCH0_BYTE0,
3547 (format >> 0) & 0xff);
3548 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3549 NVIDIA_SET_SCRATCH0_BYTE1,
3550 (format >> 8) & 0xff);
3551
3552 /* bits [16:24] are unused */
3553 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3554 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3555
3556 /*
3557 * Bit 30 signals that the data is valid and hence that HDMI audio can
3558 * be enabled.
3559 */
3560 if (format == 0)
3561 value &= ~NVIDIA_SCRATCH_VALID;
3562 else
3563 value |= NVIDIA_SCRATCH_VALID;
3564
3565 /*
3566 * Whenever the trigger bit is toggled, an interrupt is raised in the
3567 * HDMI codec. The HDMI driver will use that as trigger to update its
3568 * configuration.
3569 */
3570 value ^= NVIDIA_SCRATCH_TRIGGER;
3571
3572 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3573 NVIDIA_SET_SCRATCH0_BYTE3, value);
3574}
3575
3576static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3577 struct hda_codec *codec,
3578 unsigned int stream_tag,
3579 unsigned int format,
3580 struct snd_pcm_substream *substream)
3581{
3582 int err;
3583
3584 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3585 format, substream);
3586 if (err < 0)
3587 return err;
3588
3589 /* notify the HDMI codec of the format change */
3590 tegra_hdmi_set_format(codec, format);
3591
3592 return 0;
3593}
3594
3595static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3596 struct hda_codec *codec,
3597 struct snd_pcm_substream *substream)
3598{
3599 /* invalidate the format in the HDMI codec */
3600 tegra_hdmi_set_format(codec, 0);
3601
3602 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3603}
3604
3605static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3606{
3607 struct hdmi_spec *spec = codec->spec;
3608 unsigned int i;
3609
3610 for (i = 0; i < spec->num_pins; i++) {
3611 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3612
3613 if (pcm->pcm_type == type)
3614 return pcm;
3615 }
3616
3617 return NULL;
3618}
3619
3620static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3621{
3622 struct hda_pcm_stream *stream;
3623 struct hda_pcm *pcm;
3624 int err;
3625
3626 err = generic_hdmi_build_pcms(codec);
3627 if (err < 0)
3628 return err;
3629
3630 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3631 if (!pcm)
3632 return -ENODEV;
3633
3634 /*
3635 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3636 * codec about format changes.
3637 */
3638 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3639 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3640 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3641
3642 return 0;
3643}
3644
3645static int patch_tegra_hdmi(struct hda_codec *codec)
3646{
3647 int err;
3648
3649 err = patch_generic_hdmi(codec);
3650 if (err)
3651 return err;
3652
3653 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3654
3655 return 0;
3656}
3657
3658/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003659 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003660 */
3661
Anssi Hannula5a6135842013-10-24 21:10:35 +03003662#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003663 ((codec)->core.vendor_id == 0x1002aa01 && \
3664 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003665#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003666
Anssi Hannula5a6135842013-10-24 21:10:35 +03003667/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3668#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3669#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3670#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3671#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3672#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3673#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003674#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003675#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3676#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3677#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3678#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3679#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3680#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3681#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3682#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3683#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3684#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3685#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003686#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003687#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3688#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3689#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3690#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3691#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3692
Anssi Hannula84d69e72013-10-24 21:10:38 +03003693/* AMD specific HDA cvt verbs */
3694#define ATI_VERB_SET_RAMP_RATE 0x770
3695#define ATI_VERB_GET_RAMP_RATE 0xf70
3696
Anssi Hannula5a6135842013-10-24 21:10:35 +03003697#define ATI_OUT_ENABLE 0x1
3698
3699#define ATI_MULTICHANNEL_MODE_PAIRED 0
3700#define ATI_MULTICHANNEL_MODE_SINGLE 1
3701
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003702#define ATI_HBR_CAPABLE 0x01
3703#define ATI_HBR_ENABLE 0x10
3704
Anssi Hannula89250f82013-10-24 21:10:36 +03003705static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303706 int dev_id, unsigned char *buf, int *eld_size)
Anssi Hannula89250f82013-10-24 21:10:36 +03003707{
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303708 WARN_ON(dev_id != 0);
Anssi Hannula89250f82013-10-24 21:10:36 +03003709 /* call hda_eld.c ATI/AMD-specific function */
3710 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3711 is_amdhdmi_rev3_or_later(codec));
3712}
3713
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303714static void atihdmi_pin_setup_infoframe(struct hda_codec *codec,
3715 hda_nid_t pin_nid, int dev_id, int ca,
Anssi Hannula5a6135842013-10-24 21:10:35 +03003716 int active_channels, int conn_type)
3717{
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303718 WARN_ON(dev_id != 0);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003719 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3720}
3721
3722static int atihdmi_paired_swap_fc_lfe(int pos)
3723{
3724 /*
3725 * ATI/AMD have automatic FC/LFE swap built-in
3726 * when in pairwise mapping mode.
3727 */
3728
3729 switch (pos) {
3730 /* see channel_allocations[].speakers[] */
3731 case 2: return 3;
3732 case 3: return 2;
3733 default: break;
3734 }
3735
3736 return pos;
3737}
3738
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303739static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3740 int ca, int chs, unsigned char *map)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003741{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303742 struct hdac_cea_channel_speaker_allocation *cap;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003743 int i, j;
3744
3745 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3746
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303747 cap = snd_hdac_get_ch_alloc_from_ca(ca);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003748 for (i = 0; i < chs; ++i) {
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303749 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003750 bool ok = false;
3751 bool companion_ok = false;
3752
3753 if (!mask)
3754 continue;
3755
3756 for (j = 0 + i % 2; j < 8; j += 2) {
3757 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3758 if (cap->speakers[chan_idx] == mask) {
3759 /* channel is in a supported position */
3760 ok = true;
3761
3762 if (i % 2 == 0 && i + 1 < chs) {
3763 /* even channel, check the odd companion */
3764 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303765 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003766 int comp_mask_act = cap->speakers[comp_chan_idx];
3767
3768 if (comp_mask_req == comp_mask_act)
3769 companion_ok = true;
3770 else
3771 return -EINVAL;
3772 }
3773 break;
3774 }
3775 }
3776
3777 if (!ok)
3778 return -EINVAL;
3779
3780 if (companion_ok)
3781 i++; /* companion channel already checked */
3782 }
3783
3784 return 0;
3785}
3786
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303787static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3788 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003789{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08003790 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003791 int verb;
3792 int ati_channel_setup = 0;
3793
3794 if (hdmi_slot > 7)
3795 return -EINVAL;
3796
3797 if (!has_amd_full_remap_support(codec)) {
3798 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3799
3800 /* In case this is an odd slot but without stream channel, do not
3801 * disable the slot since the corresponding even slot could have a
3802 * channel. In case neither have a channel, the slot pair will be
3803 * disabled when this function is called for the even slot. */
3804 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3805 return 0;
3806
3807 hdmi_slot -= hdmi_slot % 2;
3808
3809 if (stream_channel != 0xf)
3810 stream_channel -= stream_channel % 2;
3811 }
3812
3813 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3814
3815 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3816
3817 if (stream_channel != 0xf)
3818 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3819
3820 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3821}
3822
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303823static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3824 hda_nid_t pin_nid, int asp_slot)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003825{
Kai-Heng Feng8c118272020-05-05 11:03:53 +08003826 struct hda_codec *codec = hdac_to_hda_codec(hdac);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003827 bool was_odd = false;
3828 int ati_asp_slot = asp_slot;
3829 int verb;
3830 int ati_channel_setup;
3831
3832 if (asp_slot > 7)
3833 return -EINVAL;
3834
3835 if (!has_amd_full_remap_support(codec)) {
3836 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3837 if (ati_asp_slot % 2 != 0) {
3838 ati_asp_slot -= 1;
3839 was_odd = true;
3840 }
3841 }
3842
3843 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3844
3845 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3846
3847 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3848 return 0xf;
3849
3850 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3851}
3852
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303853static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3854 struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303855 struct hdac_cea_channel_speaker_allocation *cap,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303856 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003857{
3858 int c;
3859
3860 /*
3861 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3862 * we need to take that into account (a single channel may take 2
3863 * channel slots if we need to carry a silent channel next to it).
3864 * On Rev3+ AMD codecs this function is not used.
3865 */
3866 int chanpairs = 0;
3867
3868 /* We only produce even-numbered channel count TLVs */
3869 if ((channels % 2) != 0)
3870 return -1;
3871
3872 for (c = 0; c < 7; c += 2) {
3873 if (cap->speakers[c] || cap->speakers[c+1])
3874 chanpairs++;
3875 }
3876
3877 if (chanpairs * 2 != channels)
3878 return -1;
3879
3880 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3881}
3882
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303883static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303884 struct hdac_cea_channel_speaker_allocation *cap,
3885 unsigned int *chmap, int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003886{
3887 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3888 int count = 0;
3889 int c;
3890
3891 for (c = 7; c >= 0; c--) {
3892 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3893 int spk = cap->speakers[chan];
3894 if (!spk) {
3895 /* add N/A channel if the companion channel is occupied */
3896 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3897 chmap[count++] = SNDRV_CHMAP_NA;
3898
3899 continue;
3900 }
3901
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303902 chmap[count++] = snd_hdac_spk_to_chmap(spk);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003903 }
3904
3905 WARN_ON(count != channels);
3906}
3907
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003908static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303909 int dev_id, bool hbr)
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003910{
3911 int hbr_ctl, hbr_ctl_new;
3912
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303913 WARN_ON(dev_id != 0);
3914
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003915 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003916 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003917 if (hbr)
3918 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3919 else
3920 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3921
Takashi Iwai4e76a882014-02-25 12:21:03 +01003922 codec_dbg(codec,
3923 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003924 pin_nid,
3925 hbr_ctl == hbr_ctl_new ? "" : "new-",
3926 hbr_ctl_new);
3927
3928 if (hbr_ctl != hbr_ctl_new)
3929 snd_hda_codec_write(codec, pin_nid, 0,
3930 ATI_VERB_SET_HBR_CONTROL,
3931 hbr_ctl_new);
3932
3933 } else if (hbr)
3934 return -EINVAL;
3935
3936 return 0;
3937}
3938
Anssi Hannula84d69e72013-10-24 21:10:38 +03003939static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303940 hda_nid_t pin_nid, int dev_id,
3941 u32 stream_tag, int format)
Anssi Hannula84d69e72013-10-24 21:10:38 +03003942{
Anssi Hannula84d69e72013-10-24 21:10:38 +03003943 if (is_amdhdmi_rev3_or_later(codec)) {
3944 int ramp_rate = 180; /* default as per AMD spec */
3945 /* disable ramp-up/down for non-pcm as per AMD spec */
3946 if (format & AC_FMT_TYPE_NON_PCM)
3947 ramp_rate = 0;
3948
3949 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3950 }
3951
Nikhil Mahale9c32fea2019-11-19 14:17:09 +05303952 return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
3953 stream_tag, format);
Anssi Hannula84d69e72013-10-24 21:10:38 +03003954}
3955
3956
Anssi Hannula5a6135842013-10-24 21:10:35 +03003957static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003958{
3959 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003960 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003961
Anssi Hannula5a6135842013-10-24 21:10:35 +03003962 err = generic_hdmi_init(codec);
3963
3964 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003965 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003966
3967 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3968 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3969
3970 /* make sure downmix information in infoframe is zero */
3971 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3972
3973 /* enable channel-wise remap mode if supported */
3974 if (has_amd_full_remap_support(codec))
3975 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3976 ATI_VERB_SET_MULTICHANNEL_MODE,
3977 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003978 }
Alex Deucher8218df92019-11-22 16:43:53 -05003979 codec->auto_runtime_pm = 1;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003980
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003981 return 0;
3982}
3983
Takashi Iwaiade49db2018-07-11 18:05:52 +02003984/* map from pin NID to port; port is 0-based */
3985/* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */
3986static int atihdmi_pin2port(void *audio_ptr, int pin_nid)
3987{
3988 return pin_nid / 2 - 1;
3989}
3990
3991/* reverse-map from port to pin NID: see above */
3992static int atihdmi_port2pin(struct hda_codec *codec, int port)
3993{
3994 return port * 2 + 3;
3995}
3996
3997static const struct drm_audio_component_audio_ops atihdmi_audio_ops = {
3998 .pin2port = atihdmi_pin2port,
3999 .pin_eld_notify = generic_acomp_pin_eld_notify,
4000 .master_bind = generic_acomp_master_bind,
4001 .master_unbind = generic_acomp_master_unbind,
4002};
4003
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004004static int patch_atihdmi(struct hda_codec *codec)
4005{
4006 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004007 struct hdmi_spec_per_cvt *per_cvt;
4008 int err, cvt_idx;
4009
4010 err = patch_generic_hdmi(codec);
4011
4012 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02004013 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004014
4015 codec->patch_ops.init = atihdmi_init;
4016
Takashi Iwaid0b12522012-06-15 14:34:42 +02004017 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004018
Anssi Hannula89250f82013-10-24 21:10:36 +03004019 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004020 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03004021 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03004022 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004023
Takashi Iwai39669222016-05-11 14:56:12 +02004024 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4025 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4026
Anssi Hannula5a6135842013-10-24 21:10:35 +03004027 if (!has_amd_full_remap_support(codec)) {
4028 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304029 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03004030 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304031 spec->chmap.ops.cea_alloc_to_tlv_chmap =
4032 atihdmi_paired_cea_alloc_to_tlv_chmap;
4033 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004034 }
4035
4036 /* ATI/AMD converters do not advertise all of their capabilities */
4037 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4038 per_cvt = get_cvt(spec, cvt_idx);
4039 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4040 per_cvt->rates |= SUPPORTED_RATES;
4041 per_cvt->formats |= SUPPORTED_FORMATS;
4042 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4043 }
4044
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304045 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03004046
Takashi Iwai57cb54e2018-06-21 13:33:53 +02004047 /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
4048 * the link-down as is. Tell the core to allow it.
4049 */
4050 codec->link_down_at_suspend = 1;
4051
Takashi Iwaiade49db2018-07-11 18:05:52 +02004052 generic_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin);
4053
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004054 return 0;
4055}
4056
Annie Liu3de5ff82012-06-08 19:18:42 +08004057/* VIA HDMI Implementation */
4058#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4059#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4060
Annie Liu3de5ff82012-06-08 19:18:42 +08004061static int patch_via_hdmi(struct hda_codec *codec)
4062{
Takashi Iwai250e41a2012-06-15 14:40:21 +02004063 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08004064}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004065
4066/*
4067 * patch entries
4068 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004069static const struct hda_device_id snd_hda_id_hdmi[] = {
4070HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4071HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4072HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4073HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4074HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4075HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4076HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004077HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI", patch_nvhdmi_2ch),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004078HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4079HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004080HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI", patch_nvhdmi_8ch_7x),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004081HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4082HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4083HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
Nikhil Mahale5398e942019-11-19 14:17:10 +05304084HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP", patch_nvhdmi_legacy),
4085HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP", patch_nvhdmi_legacy),
4086HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi_legacy),
4087HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi_legacy),
4088HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi_legacy),
4089HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi_legacy),
4090HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi_legacy),
4091HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi_legacy),
4092HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi_legacy),
4093HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi_legacy),
4094HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi_legacy),
4095HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi_legacy),
4096HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi_legacy),
Richard Samsonc8900a02011-03-03 12:46:13 +01004097/* 17 is known to be absent */
Nikhil Mahale5398e942019-11-19 14:17:10 +05304098HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi_legacy),
4099HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi_legacy),
4100HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi_legacy),
4101HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi_legacy),
4102HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi_legacy),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004103HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4104HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4105HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4106HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
Thierry Reding917bb902018-12-03 16:53:17 +01004107HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi),
4108HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi),
4109HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi),
4110HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004111HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4112HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4113HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4114HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4115HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004116HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP", patch_nvhdmi),
4117HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004118HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004119HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004120HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004121HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP", patch_nvhdmi),
4122HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004123HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4124HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4125HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4126HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004127HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP", patch_nvhdmi),
4128HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP", patch_nvhdmi),
4129HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP", patch_nvhdmi),
4130HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP", patch_nvhdmi),
4131HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004132HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004133HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP", patch_nvhdmi),
Hui Wangaf677162017-02-09 09:20:54 +08004134HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004135HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP", patch_nvhdmi),
Aaron Plattner2d369c72016-03-13 13:58:57 -07004136HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08004137HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004138HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP", patch_nvhdmi),
4139HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP", patch_nvhdmi),
4140HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP", patch_nvhdmi),
4141HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP", patch_nvhdmi),
4142HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP", patch_nvhdmi),
4143HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP", patch_nvhdmi),
4144HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP", patch_nvhdmi),
4145HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP", patch_nvhdmi),
4146HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP", patch_nvhdmi),
4147HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP", patch_nvhdmi),
Aaron Plattneradb36a822020-06-11 11:08:45 -07004148HDA_CODEC_ENTRY(0x10de009a, "GPU 9a HDMI/DP", patch_nvhdmi),
4149HDA_CODEC_ENTRY(0x10de009d, "GPU 9d HDMI/DP", patch_nvhdmi),
4150HDA_CODEC_ENTRY(0x10de009e, "GPU 9e HDMI/DP", patch_nvhdmi),
4151HDA_CODEC_ENTRY(0x10de009f, "GPU 9f HDMI/DP", patch_nvhdmi),
4152HDA_CODEC_ENTRY(0x10de00a0, "GPU a0 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004153HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
Daniel Dadap74ec1182017-07-13 19:27:39 -05004154HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI", patch_nvhdmi_2ch),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004155HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4156HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4157HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4158HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
Takashi Iwai7ff652f2016-03-21 14:50:24 +01004159HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi),
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01004160HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI", patch_i915_glk_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004161HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4162HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4163HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
Takashi Iwai7ff652f2016-03-21 14:50:24 +01004164HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi),
Takashi Iwaie85015a32016-03-21 13:56:19 +01004165HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi),
4166HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01004167HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
4168HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
4169HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
4170HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
4171HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
Guneshwor Singh2b4584d2017-12-07 18:06:20 +05304172HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI", patch_i915_glk_hdmi),
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05304173HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi),
Jaroslav Kyselab0d8bc52019-03-13 17:09:23 +01004174HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI", patch_i915_icl_hdmi),
Kai Vehmanen9a11ba72019-11-05 18:10:53 +02004175HDA_CODEC_ENTRY(0x80862812, "Tigerlake HDMI", patch_i915_tgl_hdmi),
Yong Zhi78be2222020-01-31 14:40:03 -06004176HDA_CODEC_ENTRY(0x8086281a, "Jasperlake HDMI", patch_i915_icl_hdmi),
Libin Yangd233c492020-04-09 13:58:17 -05004177HDA_CODEC_ENTRY(0x8086281b, "Elkhartlake HDMI", patch_i915_icl_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004178HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01004179HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
4180HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004181HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004182/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004183HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004184{} /* terminator */
4185};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004186MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004187
4188MODULE_LICENSE("GPL");
4189MODULE_DESCRIPTION("HDMI HD-audio codec");
4190MODULE_ALIAS("snd-hda-codec-intelhdmi");
4191MODULE_ALIAS("snd-hda-codec-nvhdmi");
4192MODULE_ALIAS("snd-hda-codec-atihdmi");
4193
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004194static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004195 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004196};
4197
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004198module_hda_codec_driver(hdmi_driver);