blob: 76c85f08bea67f85d8b8b15cbe276e3d80f28eb5 [file] [log] [blame]
Wu Fengguang079d88c2010-03-08 10:44:23 +08001/*
2 *
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
4 *
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
Takashi Iwai84eb01b2010-09-07 12:27:25 +02006 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
Anssi Hannula5a6135842013-10-24 21:10:35 +03009 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
Wu Fengguang079d88c2010-03-08 10:44:23 +080010 *
11 * Authors:
12 * Wu Fengguang <wfg@linux.intel.com>
13 *
14 * Maintained by:
15 * Wu Fengguang <wfg@linux.intel.com>
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
20 * any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software Foundation,
29 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
Takashi Iwai84eb01b2010-09-07 12:27:25 +020032#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040035#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020036#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020037#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080038#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020039#include <sound/tlv.h>
David Henningsson25adc132015-08-19 10:48:58 +020040#include <sound/hdaudio.h>
41#include <sound/hda_i915.h>
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +053042#include <sound/hda_chmap.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020043#include "hda_codec.h"
44#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020045#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020046
Takashi Iwai0ebaa242011-01-11 18:11:04 +010047static bool static_hdmi_pcm;
48module_param(static_hdmi_pcm, bool, 0644);
49MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
50
Takashi Iwai7639a062015-03-03 10:07:24 +010051#define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
52#define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
53#define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
Lu, Hane2656412015-11-11 16:54:27 +080054#define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
Libin Yang91815d82016-01-14 14:09:00 +080055#define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
Libin Yang432ac1a2014-12-16 13:17:34 +080056#define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
Libin Yang91815d82016-01-14 14:09:00 +080057 || is_skylake(codec) || is_broxton(codec) \
58 || is_kabylake(codec))
Mengdong Lin75dcbe42014-01-08 15:55:32 -050059
Takashi Iwai7639a062015-03-03 10:07:24 +010060#define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
61#define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
Libin Yangca2e7222014-08-19 16:20:12 +080062#define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
Mengdong Linfb87fa32013-09-04 16:36:57 -040063
Stephen Warren384a48d2011-06-01 11:14:21 -060064struct hdmi_spec_per_cvt {
65 hda_nid_t cvt_nid;
66 int assigned;
67 unsigned int channels_min;
68 unsigned int channels_max;
69 u32 rates;
70 u64 formats;
71 unsigned int maxbps;
72};
73
Takashi Iwai4eea3092013-02-07 18:18:19 +010074/* max. connections to a widget */
75#define HDA_MAX_CONNECTIONS 32
76
Stephen Warren384a48d2011-06-01 11:14:21 -060077struct hdmi_spec_per_pin {
78 hda_nid_t pin_nid;
Libin Yang91520852017-01-12 16:04:53 +080079 int dev_id;
Libin Yanga76056f2015-12-16 16:48:15 +080080 /* pin idx, different device entries on the same pin use the same idx */
81 int pin_nid_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -060082 int num_mux_nids;
83 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Mengdong Lin2df67422014-03-20 13:01:06 +080084 int mux_idx;
Anssi Hannula1df5a062013-10-05 02:25:40 +030085 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080086
87 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060088 struct hdmi_eld sink_eld;
Takashi Iwaia4e9a382013-10-17 18:21:12 +020089 struct mutex lock;
Wu Fengguang744626d2011-11-16 16:29:47 +080090 struct delayed_work work;
Libin Yang2bea2412016-01-12 11:13:26 +080091 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Libin Yanga76056f2015-12-16 16:48:15 +080092 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
Wu Fengguangc6e84532011-11-18 16:59:32 -060093 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020094 bool setup; /* the stream has been set up by prepare callback */
95 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020096 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020097 bool chmap_set; /* channel-map override by ALSA API? */
98 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080099#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200100 struct snd_info_entry *proc_entry;
101#endif
Stephen Warren384a48d2011-06-01 11:14:21 -0600102};
103
Anssi Hannula307229d2013-10-24 21:10:34 +0300104/* operations used by generic code that can be overridden by patches */
105struct hdmi_ops {
106 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
107 unsigned char *buf, int *eld_size);
108
Anssi Hannula307229d2013-10-24 21:10:34 +0300109 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
110 int ca, int active_channels, int conn_type);
111
112 /* enable/disable HBR (HD passthrough) */
113 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
114
115 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
116 hda_nid_t pin_nid, u32 stream_tag, int format);
117
Takashi Iwai4846a672016-03-21 12:56:46 +0100118 void (*pin_cvt_fixup)(struct hda_codec *codec,
119 struct hdmi_spec_per_pin *per_pin,
120 hda_nid_t cvt_nid);
Anssi Hannula307229d2013-10-24 21:10:34 +0300121};
122
Libin Yang2bea2412016-01-12 11:13:26 +0800123struct hdmi_pcm {
124 struct hda_pcm *pcm;
125 struct snd_jack *jack;
Libin Yangfb087ea2016-02-23 16:33:37 +0800126 struct snd_kcontrol *eld_ctl;
Libin Yang2bea2412016-01-12 11:13:26 +0800127};
128
Wu Fengguang079d88c2010-03-08 10:44:23 +0800129struct hdmi_spec {
130 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100131 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
132 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600133
Libin Yang91520852017-01-12 16:04:53 +0800134 /*
135 * num_pins is the number of virtual pins
136 * for example, there are 3 pins, and each pin
137 * has 4 device entries, then the num_pins is 12
138 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800139 int num_pins;
Libin Yang91520852017-01-12 16:04:53 +0800140 /*
141 * num_nids is the number of real pins
142 * In the above example, num_nids is 3
143 */
144 int num_nids;
145 /*
146 * dev_num is the number of device entries
147 * on each pin.
148 * In the above example, dev_num is 4
149 */
150 int dev_num;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100151 struct snd_array pins; /* struct hdmi_spec_per_pin */
Libin Yang2bea2412016-01-12 11:13:26 +0800152 struct hdmi_pcm pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800153 struct mutex pcm_lock;
Libin Yanga76056f2015-12-16 16:48:15 +0800154 /* pcm_bitmap means which pcms have been assigned to pins*/
155 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800156 int pcm_used; /* counter of pcm_rec[] */
Libin Yangac983792015-12-16 16:48:16 +0800157 /* bitmap shows whether the pcm is opened in user space
158 * bit 0 means the first playback PCM (PCM3);
159 * bit 1 means the second playback PCM, and so on.
160 */
161 unsigned long pcm_in_use;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800162
David Henningsson4bd038f2013-02-19 16:11:25 +0100163 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300164 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700165
166 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800167 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800168 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300169 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800170 */
171 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200172 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200173
174 /* i915/powerwell (Haswell+/Valleyview+) specific */
Takashi Iwai691be972016-03-18 15:10:08 +0100175 bool use_acomp_notifier; /* use i915 eld_notify callback for hotplug */
David Henningsson25adc132015-08-19 10:48:58 +0200176 struct i915_audio_component_audio_ops i915_audio_ops;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +0530177
178 struct hdac_chmap chmap;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +0530179 hda_nid_t vendor_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800180};
181
Takashi Iwaif4e30402015-12-10 13:01:28 +0100182#ifdef CONFIG_SND_HDA_I915
Takashi Iwai691be972016-03-18 15:10:08 +0100183static inline bool codec_has_acomp(struct hda_codec *codec)
184{
185 struct hdmi_spec *spec = codec->spec;
186 return spec->use_acomp_notifier;
187}
Takashi Iwaif4e30402015-12-10 13:01:28 +0100188#else
189#define codec_has_acomp(codec) false
190#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800191
192struct hdmi_audio_infoframe {
193 u8 type; /* 0x84 */
194 u8 ver; /* 0x01 */
195 u8 len; /* 0x0a */
196
Wu Fengguang53d7d692010-09-21 14:25:49 +0800197 u8 checksum;
198
Wu Fengguang079d88c2010-03-08 10:44:23 +0800199 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
200 u8 SS01_SF24;
201 u8 CXT04;
202 u8 CA;
203 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800204};
205
206struct dp_audio_infoframe {
207 u8 type; /* 0x84 */
208 u8 len; /* 0x1b */
209 u8 ver; /* 0x11 << 2 */
210
211 u8 CC02_CT47; /* match with HDMI infoframe from this on */
212 u8 SS01_SF24;
213 u8 CXT04;
214 u8 CA;
215 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800216};
217
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100218union audio_infoframe {
219 struct hdmi_audio_infoframe hdmi;
220 struct dp_audio_infoframe dp;
221 u8 bytes[0];
222};
223
Wu Fengguang079d88c2010-03-08 10:44:23 +0800224/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800225 * HDMI routines
226 */
227
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100228#define get_pin(spec, idx) \
229 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
230#define get_cvt(spec, idx) \
231 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Libin Yang2bea2412016-01-12 11:13:26 +0800232/* obtain hdmi_pcm object assigned to idx */
233#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
234/* obtain hda_pcm object assigned to idx */
235#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100236
Libin Yang91520852017-01-12 16:04:53 +0800237static int pin_id_to_pin_index(struct hda_codec *codec,
238 hda_nid_t pin_nid, int dev_id)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800239{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100240 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600241 int pin_idx;
Libin Yang91520852017-01-12 16:04:53 +0800242 struct hdmi_spec_per_pin *per_pin;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800243
Libin Yang91520852017-01-12 16:04:53 +0800244 /*
245 * (dev_id == -1) means it is NON-MST pin
246 * return the first virtual pin on this port
247 */
248 if (dev_id == -1)
249 dev_id = 0;
250
251 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
252 per_pin = get_pin(spec, pin_idx);
253 if ((per_pin->pin_nid == pin_nid) &&
254 (per_pin->dev_id == dev_id))
Stephen Warren384a48d2011-06-01 11:14:21 -0600255 return pin_idx;
Libin Yang91520852017-01-12 16:04:53 +0800256 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800257
Takashi Iwai4e76a882014-02-25 12:21:03 +0100258 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600259 return -EINVAL;
260}
261
Libin Yang2bf3c852015-12-16 13:42:43 +0800262static int hinfo_to_pcm_index(struct hda_codec *codec,
263 struct hda_pcm_stream *hinfo)
264{
265 struct hdmi_spec *spec = codec->spec;
266 int pcm_idx;
267
268 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
269 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
270 return pcm_idx;
271
272 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
273 return -EINVAL;
274}
275
Takashi Iwai4e76a882014-02-25 12:21:03 +0100276static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600277 struct hda_pcm_stream *hinfo)
278{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100279 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800280 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600281 int pin_idx;
282
Libin Yang6590faa2015-12-16 13:42:41 +0800283 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
284 per_pin = get_pin(spec, pin_idx);
Libin Yang2bea2412016-01-12 11:13:26 +0800285 if (per_pin->pcm &&
286 per_pin->pcm->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600287 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800288 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600289
Libin Yang6590faa2015-12-16 13:42:41 +0800290 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600291 return -EINVAL;
292}
293
Libin Yang022f3442016-02-03 10:48:34 +0800294static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
295 int pcm_idx)
296{
297 int i;
298 struct hdmi_spec_per_pin *per_pin;
299
300 for (i = 0; i < spec->num_pins; i++) {
301 per_pin = get_pin(spec, i);
302 if (per_pin->pcm_idx == pcm_idx)
303 return per_pin;
304 }
305 return NULL;
306}
307
Takashi Iwai4e76a882014-02-25 12:21:03 +0100308static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600309{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100310 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600311 int cvt_idx;
312
313 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100314 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600315 return cvt_idx;
316
Takashi Iwai4e76a882014-02-25 12:21:03 +0100317 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800318 return -EINVAL;
319}
320
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500321static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
322 struct snd_ctl_elem_info *uinfo)
323{
324 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100325 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200326 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100327 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800328 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500329
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500330 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
331
Libin Yangfb087ea2016-02-23 16:33:37 +0800332 pcm_idx = kcontrol->private_value;
333 mutex_lock(&spec->pcm_lock);
334 per_pin = pcm_idx_to_pin(spec, pcm_idx);
335 if (!per_pin) {
336 /* no pin is bound to the pcm */
337 uinfo->count = 0;
338 mutex_unlock(&spec->pcm_lock);
339 return 0;
340 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200341 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100342 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Libin Yangfb087ea2016-02-23 16:33:37 +0800343 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500344
345 return 0;
346}
347
348static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
349 struct snd_ctl_elem_value *ucontrol)
350{
351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100352 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200353 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100354 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800355 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500356
Libin Yangfb087ea2016-02-23 16:33:37 +0800357 pcm_idx = kcontrol->private_value;
358 mutex_lock(&spec->pcm_lock);
359 per_pin = pcm_idx_to_pin(spec, pcm_idx);
360 if (!per_pin) {
361 /* no pin is bound to the pcm */
362 memset(ucontrol->value.bytes.data, 0,
363 ARRAY_SIZE(ucontrol->value.bytes.data));
364 mutex_unlock(&spec->pcm_lock);
365 return 0;
366 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200367 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500368
David Henningsson360a8242016-02-05 09:05:41 +0100369 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
370 eld->eld_size > ELD_MAX_SIZE) {
Libin Yangfb087ea2016-02-23 16:33:37 +0800371 mutex_unlock(&spec->pcm_lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100372 snd_BUG();
373 return -EINVAL;
374 }
375
376 memset(ucontrol->value.bytes.data, 0,
377 ARRAY_SIZE(ucontrol->value.bytes.data));
378 if (eld->eld_valid)
379 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
380 eld->eld_size);
Libin Yangfb087ea2016-02-23 16:33:37 +0800381 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500382
383 return 0;
384}
385
Bhumika Goyalf3b827e2017-02-20 00:18:09 +0530386static const struct snd_kcontrol_new eld_bytes_ctl = {
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500387 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
388 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
389 .name = "ELD",
390 .info = hdmi_eld_ctl_info,
391 .get = hdmi_eld_ctl_get,
392};
393
Libin Yangfb087ea2016-02-23 16:33:37 +0800394static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500395 int device)
396{
397 struct snd_kcontrol *kctl;
398 struct hdmi_spec *spec = codec->spec;
399 int err;
400
401 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
402 if (!kctl)
403 return -ENOMEM;
Libin Yangfb087ea2016-02-23 16:33:37 +0800404 kctl->private_value = pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500405 kctl->id.device = device;
406
Libin Yangfb087ea2016-02-23 16:33:37 +0800407 /* no pin nid is associated with the kctl now
408 * tbd: associate pin nid to eld ctl later
409 */
410 err = snd_hda_ctl_add(codec, 0, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500411 if (err < 0)
412 return err;
413
Libin Yangfb087ea2016-02-23 16:33:37 +0800414 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500415 return 0;
416}
417
Wu Fengguang079d88c2010-03-08 10:44:23 +0800418#ifdef BE_PARANOID
419static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
420 int *packet_index, int *byte_index)
421{
422 int val;
423
424 val = snd_hda_codec_read(codec, pin_nid, 0,
425 AC_VERB_GET_HDMI_DIP_INDEX, 0);
426
427 *packet_index = val >> 5;
428 *byte_index = val & 0x1f;
429}
430#endif
431
432static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
433 int packet_index, int byte_index)
434{
435 int val;
436
437 val = (packet_index << 5) | (byte_index & 0x1f);
438
439 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
440}
441
442static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
443 unsigned char val)
444{
445 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
446}
447
Stephen Warren384a48d2011-06-01 11:14:21 -0600448static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800449{
Stephen Warren75fae112014-01-30 11:52:16 -0700450 struct hdmi_spec *spec = codec->spec;
451 int pin_out;
452
Wu Fengguang079d88c2010-03-08 10:44:23 +0800453 /* Unmute */
454 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
455 snd_hda_codec_write(codec, pin_nid, 0,
456 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700457
458 if (spec->dyn_pin_out)
459 /* Disable pin out until stream is active */
460 pin_out = 0;
461 else
462 /* Enable pin out: some machines with GM965 gets broken output
463 * when the pin is disabled or changed while using with HDMI
464 */
465 pin_out = PIN_OUT;
466
Wu Fengguang079d88c2010-03-08 10:44:23 +0800467 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700468 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800469}
470
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200471/*
472 * ELD proc files
473 */
474
Jie Yangcd6a6502015-05-27 19:45:45 +0800475#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200476static void print_eld_info(struct snd_info_entry *entry,
477 struct snd_info_buffer *buffer)
478{
479 struct hdmi_spec_per_pin *per_pin = entry->private_data;
480
481 mutex_lock(&per_pin->lock);
482 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
483 mutex_unlock(&per_pin->lock);
484}
485
486static void write_eld_info(struct snd_info_entry *entry,
487 struct snd_info_buffer *buffer)
488{
489 struct hdmi_spec_per_pin *per_pin = entry->private_data;
490
491 mutex_lock(&per_pin->lock);
492 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
493 mutex_unlock(&per_pin->lock);
494}
495
496static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
497{
498 char name[32];
499 struct hda_codec *codec = per_pin->codec;
500 struct snd_info_entry *entry;
501 int err;
502
503 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100504 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200505 if (err < 0)
506 return err;
507
508 snd_info_set_text_ops(entry, per_pin, print_eld_info);
509 entry->c.text.write = write_eld_info;
510 entry->mode |= S_IWUSR;
511 per_pin->proc_entry = entry;
512
513 return 0;
514}
515
516static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
517{
Markus Elfring1947a112015-06-28 11:15:28 +0200518 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200519 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200520 per_pin->proc_entry = NULL;
521 }
522}
523#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200524static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
525 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200526{
527 return 0;
528}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200529static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200530{
531}
532#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800533
534/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800535 * Audio InfoFrame routines
536 */
537
538/*
539 * Enable Audio InfoFrame Transmission
540 */
541static void hdmi_start_infoframe_trans(struct hda_codec *codec,
542 hda_nid_t pin_nid)
543{
544 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
545 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
546 AC_DIPXMIT_BEST);
547}
548
549/*
550 * Disable Audio InfoFrame Transmission
551 */
552static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
553 hda_nid_t pin_nid)
554{
555 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
556 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
557 AC_DIPXMIT_DISABLE);
558}
559
560static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
561{
562#ifdef CONFIG_SND_DEBUG_VERBOSE
563 int i;
564 int size;
565
566 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100567 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800568
569 for (i = 0; i < 8; i++) {
570 size = snd_hda_codec_read(codec, pin_nid, 0,
571 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100572 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800573 }
574#endif
575}
576
577static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
578{
579#ifdef BE_PARANOID
580 int i, j;
581 int size;
582 int pi, bi;
583 for (i = 0; i < 8; i++) {
584 size = snd_hda_codec_read(codec, pin_nid, 0,
585 AC_VERB_GET_HDMI_DIP_SIZE, i);
586 if (size == 0)
587 continue;
588
589 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
590 for (j = 1; j < 1000; j++) {
591 hdmi_write_dip_byte(codec, pin_nid, 0x0);
592 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
593 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100594 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +0800595 bi, pi, i);
596 if (bi == 0) /* byte index wrapped around */
597 break;
598 }
Takashi Iwai4e76a882014-02-25 12:21:03 +0100599 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800600 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
601 i, size, j);
602 }
603#endif
604}
605
Wu Fengguang53d7d692010-09-21 14:25:49 +0800606static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800607{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800608 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800609 u8 sum = 0;
610 int i;
611
Wu Fengguang53d7d692010-09-21 14:25:49 +0800612 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800613
Wu Fengguang53d7d692010-09-21 14:25:49 +0800614 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800615 sum += bytes[i];
616
Wu Fengguang53d7d692010-09-21 14:25:49 +0800617 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800618}
619
620static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
621 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800622 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800623{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800624 int i;
625
626 hdmi_debug_dip_size(codec, pin_nid);
627 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
628
Wu Fengguang079d88c2010-03-08 10:44:23 +0800629 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800630 for (i = 0; i < size; i++)
631 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800632}
633
634static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800635 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800636{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800637 u8 val;
638 int i;
639
640 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
641 != AC_DIPXMIT_BEST)
642 return false;
643
644 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800645 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800646 val = snd_hda_codec_read(codec, pin_nid, 0,
647 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800648 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800649 return false;
650 }
651
652 return true;
653}
654
Anssi Hannula307229d2013-10-24 21:10:34 +0300655static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
656 hda_nid_t pin_nid,
657 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
686 /*
687 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
688 * sizeof(*dp_ai) to avoid partial match/update problems when
689 * the user switches between HDMI/DP monitors.
690 */
691 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
692 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100693 codec_dbg(codec,
694 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300695 pin_nid,
696 active_channels, ca);
697 hdmi_stop_infoframe_trans(codec, pin_nid);
698 hdmi_fill_audio_infoframe(codec, pin_nid,
699 ai.bytes, sizeof(ai));
700 hdmi_start_infoframe_trans(codec, pin_nid);
701 }
702}
703
Takashi Iwaib0540872013-09-02 12:33:02 +0200704static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
705 struct hdmi_spec_per_pin *per_pin,
706 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800707{
Anssi Hannula307229d2013-10-24 21:10:34 +0300708 struct hdmi_spec *spec = codec->spec;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530709 struct hdac_chmap *chmap = &spec->chmap;
Stephen Warren384a48d2011-06-01 11:14:21 -0600710 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200711 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300712 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600713 struct hdmi_eld *eld;
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530714 int ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800715
Takashi Iwaib0540872013-09-02 12:33:02 +0200716 if (!channels)
717 return;
718
Takashi Iwai44bb6d02016-03-21 12:36:44 +0100719 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
720 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
Mengdong Lin58f7d282013-09-04 16:37:12 -0400721 snd_hda_codec_write(codec, pin_nid, 0,
722 AC_VERB_SET_AMP_GAIN_MUTE,
723 AMP_OUT_UNMUTE);
724
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100725 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800726
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530727 ca = snd_hdac_channel_allocation(&codec->core,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530728 eld->info.spk_alloc, channels,
729 per_pin->chmap_set, non_pcm, per_pin->chmap);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800730
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530731 active_channels = snd_hdac_get_active_channels(ca);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300732
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530733 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
734 active_channels);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300735
Stephen Warren384a48d2011-06-01 11:14:21 -0600736 /*
Anssi Hannula39edac72013-10-07 19:24:52 +0300737 * always configure channel mapping, it may have been changed by the
738 * user in the meantime
739 */
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530740 snd_hdac_setup_channel_mapping(&spec->chmap,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530741 pin_nid, non_pcm, ca, channels,
742 per_pin->chmap, per_pin->chmap_set);
Anssi Hannula39edac72013-10-07 19:24:52 +0300743
Anssi Hannula307229d2013-10-24 21:10:34 +0300744 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
745 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +0800746
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200747 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800748}
749
Wu Fengguang079d88c2010-03-08 10:44:23 +0800750/*
751 * Unsolicited events
752 */
753
Takashi Iwaiefe47102013-11-07 13:38:23 +0100754static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200755
Libin Yang91520852017-01-12 16:04:53 +0800756static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid,
757 int dev_id)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800758{
759 struct hdmi_spec *spec = codec->spec;
Libin Yang91520852017-01-12 16:04:53 +0800760 int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200761
David Henningsson20ce9022013-12-04 10:19:41 +0800762 if (pin_idx < 0)
763 return;
David Henningsson20ce9022013-12-04 10:19:41 +0800764 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
765 snd_hda_jack_report_sync(codec);
766}
767
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200768static void jack_callback(struct hda_codec *codec,
769 struct hda_jack_callback *jack)
770{
Libin Yang91520852017-01-12 16:04:53 +0800771 /* hda_jack don't support DP MST */
772 check_presence_and_report(codec, jack->nid, 0);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200773}
774
David Henningsson20ce9022013-12-04 10:19:41 +0800775static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
776{
Takashi Iwai3a938972011-10-28 01:16:55 +0200777 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200778 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400779 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200780
Libin Yang91520852017-01-12 16:04:53 +0800781 /*
782 * assume DP MST uses dyn_pcm_assign and acomp and
783 * never comes here
784 * if DP MST supports unsol event, below code need
785 * consider dev_entry
786 */
Takashi Iwai3a938972011-10-28 01:16:55 +0200787 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
788 if (!jack)
789 return;
Takashi Iwai3a938972011-10-28 01:16:55 +0200790 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800791
Takashi Iwai4e76a882014-02-25 12:21:03 +0100792 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400793 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
David Henningsson20ce9022013-12-04 10:19:41 +0800794 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800795 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800796
Libin Yang91520852017-01-12 16:04:53 +0800797 /* hda_jack don't support DP MST */
798 check_presence_and_report(codec, jack->nid, 0);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800799}
800
801static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
802{
803 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
804 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
805 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
806 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
807
Takashi Iwai4e76a882014-02-25 12:21:03 +0100808 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200809 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600810 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800811 tag,
812 subtag,
813 cp_state,
814 cp_ready);
815
816 /* TODO */
817 if (cp_state)
818 ;
819 if (cp_ready)
820 ;
821}
822
823
824static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
825{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800826 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
827 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
828
Takashi Iwai3a938972011-10-28 01:16:55 +0200829 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100830 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800831 return;
832 }
833
834 if (subtag == 0)
835 hdmi_intrinsic_event(codec, res);
836 else
837 hdmi_non_intrinsic_event(codec, res);
838}
839
Mengdong Lin58f7d282013-09-04 16:37:12 -0400840static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +0800841 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +0200842{
Mengdong Lin58f7d282013-09-04 16:37:12 -0400843 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +0200844
Wang Xingchao53b434f2013-06-18 10:41:53 +0800845 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
846 * thus pins could only choose converter 0 for use. Make sure the
847 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200848 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +0800849 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
850
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200851 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +0200852 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
853 AC_PWRST_D0);
854 msleep(40);
855 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
856 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100857 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 +0200858 }
David Henningsson83f26ad2013-04-10 12:26:07 +0200859}
860
Wu Fengguang079d88c2010-03-08 10:44:23 +0800861/*
862 * Callbacks
863 */
864
Takashi Iwai92f10b32010-08-03 14:21:00 +0200865/* HBR should be Non-PCM, 8 channels */
866#define is_hbr_format(format) \
867 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
868
Anssi Hannula307229d2013-10-24 21:10:34 +0300869static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
870 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800871{
Anssi Hannula307229d2013-10-24 21:10:34 +0300872 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +0200873
Stephen Warren384a48d2011-06-01 11:14:21 -0600874 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
875 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300876 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
877
Anssi Hannula13122e62013-11-10 20:56:10 +0200878 if (pinctl < 0)
879 return hbr ? -EINVAL : 0;
880
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300881 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +0300882 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300883 new_pinctl |= AC_PINCTL_EPT_HBR;
884 else
885 new_pinctl |= AC_PINCTL_EPT_NATIVE;
886
Takashi Iwai4e76a882014-02-25 12:21:03 +0100887 codec_dbg(codec,
888 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600889 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300890 pinctl == new_pinctl ? "" : "new-",
891 new_pinctl);
892
893 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -0600894 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300895 AC_VERB_SET_PIN_WIDGET_CONTROL,
896 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +0300897 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300898 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +0300899
900 return 0;
901}
902
903static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
904 hda_nid_t pin_nid, u32 stream_tag, int format)
905{
906 struct hdmi_spec *spec = codec->spec;
907 int err;
908
Anssi Hannula307229d2013-10-24 21:10:34 +0300909 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
910
911 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100912 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +0300913 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300914 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800915
Stephen Warren384a48d2011-06-01 11:14:21 -0600916 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300917 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800918}
919
Libin Yang42b29872015-12-16 13:42:42 +0800920/* Try to find an available converter
921 * If pin_idx is less then zero, just try to find an available converter.
922 * Otherwise, try to find an available converter and get the cvt mux index
923 * of the pin.
924 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800925static int hdmi_choose_cvt(struct hda_codec *codec,
Takashi Iwai4846a672016-03-21 12:56:46 +0100926 int pin_idx, int *cvt_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200927{
928 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600929 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600930 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800931 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200932
Libin Yang42b29872015-12-16 13:42:42 +0800933 /* pin_idx < 0 means no pin will be bound to the converter */
934 if (pin_idx < 0)
935 per_pin = NULL;
936 else
937 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200938
Stephen Warren384a48d2011-06-01 11:14:21 -0600939 /* Dynamically assign converter to stream */
940 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100941 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -0600942
943 /* Must not already be assigned */
944 if (per_cvt->assigned)
945 continue;
Libin Yang42b29872015-12-16 13:42:42 +0800946 if (per_pin == NULL)
947 break;
Stephen Warren384a48d2011-06-01 11:14:21 -0600948 /* Must be in pin's mux's list of converters */
949 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
950 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
951 break;
952 /* Not in mux list */
953 if (mux_idx == per_pin->num_mux_nids)
954 continue;
955 break;
956 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800957
Stephen Warren384a48d2011-06-01 11:14:21 -0600958 /* No free converters */
959 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +0800960 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -0600961
Libin Yang42b29872015-12-16 13:42:42 +0800962 if (per_pin != NULL)
963 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +0800964
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800965 if (cvt_id)
966 *cvt_id = cvt_idx;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800967
968 return 0;
969}
970
Mengdong Lin2df67422014-03-20 13:01:06 +0800971/* Assure the pin select the right convetor */
972static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
973 struct hdmi_spec_per_pin *per_pin)
974{
975 hda_nid_t pin_nid = per_pin->pin_nid;
976 int mux_idx, curr;
977
978 mux_idx = per_pin->mux_idx;
979 curr = snd_hda_codec_read(codec, pin_nid, 0,
980 AC_VERB_GET_CONNECT_SEL, 0);
981 if (curr != mux_idx)
982 snd_hda_codec_write_cache(codec, pin_nid, 0,
983 AC_VERB_SET_CONNECT_SEL,
984 mux_idx);
985}
986
Libin Yang42b29872015-12-16 13:42:42 +0800987/* get the mux index for the converter of the pins
988 * converter's mux index is the same for all pins on Intel platform
989 */
990static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
991 hda_nid_t cvt_nid)
992{
993 int i;
994
995 for (i = 0; i < spec->num_cvts; i++)
996 if (spec->cvt_nids[i] == cvt_nid)
997 return i;
998 return -EINVAL;
999}
1000
Mengdong Lin300016b2013-11-04 01:13:13 -05001001/* Intel HDMI workaround to fix audio routing issue:
1002 * For some Intel display codecs, pins share the same connection list.
1003 * So a conveter can be selected by multiple pins and playback on any of these
1004 * pins will generate sound on the external display, because audio flows from
1005 * the same converter to the display pipeline. Also muting one pin may make
1006 * other pins have no sound output.
1007 * So this function assures that an assigned converter for a pin is not selected
1008 * by any other pins.
1009 */
1010static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Libin Yang91520852017-01-12 16:04:53 +08001011 hda_nid_t pin_nid,
1012 int dev_id, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001013{
1014 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001015 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001016 int cvt_idx, curr;
1017 struct hdmi_spec_per_cvt *per_cvt;
Libin Yang91520852017-01-12 16:04:53 +08001018 struct hdmi_spec_per_pin *per_pin;
1019 int pin_idx;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001020
Libin Yang91520852017-01-12 16:04:53 +08001021 /* configure the pins connections */
1022 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1023 int dev_id_saved;
1024 int dev_num;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001025
Libin Yang91520852017-01-12 16:04:53 +08001026 per_pin = get_pin(spec, pin_idx);
1027 /*
1028 * pin not connected to monitor
1029 * no need to operate on it
1030 */
1031 if (!per_pin->pcm)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001032 continue;
1033
Libin Yang91520852017-01-12 16:04:53 +08001034 if ((per_pin->pin_nid == pin_nid) &&
1035 (per_pin->dev_id == dev_id))
Mengdong Linf82d7d12013-09-21 20:34:45 -04001036 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001037
Libin Yang91520852017-01-12 16:04:53 +08001038 /*
1039 * if per_pin->dev_id >= dev_num,
1040 * snd_hda_get_dev_select() will fail,
1041 * and the following operation is unpredictable.
1042 * So skip this situation.
1043 */
1044 dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1;
1045 if (per_pin->dev_id >= dev_num)
1046 continue;
1047
1048 nid = per_pin->pin_nid;
1049
1050 /*
1051 * Calling this function should not impact
1052 * on the device entry selection
1053 * So let's save the dev id for each pin,
1054 * and restore it when return
1055 */
1056 dev_id_saved = snd_hda_get_dev_select(codec, nid);
1057 snd_hda_set_dev_select(codec, nid, per_pin->dev_id);
Mengdong Linf82d7d12013-09-21 20:34:45 -04001058 curr = snd_hda_codec_read(codec, nid, 0,
1059 AC_VERB_GET_CONNECT_SEL, 0);
Libin Yang91520852017-01-12 16:04:53 +08001060 if (curr != mux_idx) {
1061 snd_hda_set_dev_select(codec, nid, dev_id_saved);
Mengdong Linf82d7d12013-09-21 20:34:45 -04001062 continue;
Libin Yang91520852017-01-12 16:04:53 +08001063 }
1064
Mengdong Linf82d7d12013-09-21 20:34:45 -04001065
1066 /* choose an unassigned converter. The conveters in the
1067 * connection list are in the same order as in the codec.
1068 */
1069 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1070 per_cvt = get_cvt(spec, cvt_idx);
1071 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001072 codec_dbg(codec,
1073 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001074 cvt_idx, nid);
1075 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001076 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001077 cvt_idx);
1078 break;
1079 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001080 }
Libin Yang91520852017-01-12 16:04:53 +08001081 snd_hda_set_dev_select(codec, nid, dev_id_saved);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001082 }
1083}
1084
Libin Yang42b29872015-12-16 13:42:42 +08001085/* A wrapper of intel_not_share_asigned_cvt() */
1086static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
Libin Yang91520852017-01-12 16:04:53 +08001087 hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
Libin Yang42b29872015-12-16 13:42:42 +08001088{
1089 int mux_idx;
1090 struct hdmi_spec *spec = codec->spec;
1091
Libin Yang42b29872015-12-16 13:42:42 +08001092 /* On Intel platform, the mapping of converter nid to
1093 * mux index of the pins are always the same.
1094 * The pin nid may be 0, this means all pins will not
1095 * share the converter.
1096 */
1097 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1098 if (mux_idx >= 0)
Libin Yang91520852017-01-12 16:04:53 +08001099 intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001100}
1101
Takashi Iwai4846a672016-03-21 12:56:46 +01001102/* skeleton caller of pin_cvt_fixup ops */
1103static void pin_cvt_fixup(struct hda_codec *codec,
1104 struct hdmi_spec_per_pin *per_pin,
1105 hda_nid_t cvt_nid)
1106{
1107 struct hdmi_spec *spec = codec->spec;
1108
1109 if (spec->ops.pin_cvt_fixup)
1110 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1111}
1112
Libin Yang42b29872015-12-16 13:42:42 +08001113/* called in hdmi_pcm_open when no pin is assigned to the PCM
1114 * in dyn_pcm_assign mode.
1115 */
1116static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1117 struct hda_codec *codec,
1118 struct snd_pcm_substream *substream)
1119{
1120 struct hdmi_spec *spec = codec->spec;
1121 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001122 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001123 struct hdmi_spec_per_cvt *per_cvt = NULL;
1124 int err;
1125
Libin Yangac983792015-12-16 16:48:16 +08001126 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1127 if (pcm_idx < 0)
1128 return -EINVAL;
1129
Takashi Iwai4846a672016-03-21 12:56:46 +01001130 err = hdmi_choose_cvt(codec, -1, &cvt_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001131 if (err)
1132 return err;
1133
1134 per_cvt = get_cvt(spec, cvt_idx);
1135 per_cvt->assigned = 1;
1136 hinfo->nid = per_cvt->cvt_nid;
1137
Takashi Iwai4846a672016-03-21 12:56:46 +01001138 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
Libin Yang42b29872015-12-16 13:42:42 +08001139
Libin Yangac983792015-12-16 16:48:16 +08001140 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001141 /* todo: setup spdif ctls assign */
1142
1143 /* Initially set the converter's capabilities */
1144 hinfo->channels_min = per_cvt->channels_min;
1145 hinfo->channels_max = per_cvt->channels_max;
1146 hinfo->rates = per_cvt->rates;
1147 hinfo->formats = per_cvt->formats;
1148 hinfo->maxbps = per_cvt->maxbps;
1149
1150 /* Store the updated parameters */
1151 runtime->hw.channels_min = hinfo->channels_min;
1152 runtime->hw.channels_max = hinfo->channels_max;
1153 runtime->hw.formats = hinfo->formats;
1154 runtime->hw.rates = hinfo->rates;
1155
1156 snd_pcm_hw_constraint_step(substream->runtime, 0,
1157 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1158 return 0;
1159}
1160
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001161/*
1162 * HDA PCM callbacks
1163 */
1164static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1165 struct hda_codec *codec,
1166 struct snd_pcm_substream *substream)
1167{
1168 struct hdmi_spec *spec = codec->spec;
1169 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai4846a672016-03-21 12:56:46 +01001170 int pin_idx, cvt_idx, pcm_idx;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001171 struct hdmi_spec_per_pin *per_pin;
1172 struct hdmi_eld *eld;
1173 struct hdmi_spec_per_cvt *per_cvt = NULL;
1174 int err;
1175
1176 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001177 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1178 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001179 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001180
Libin Yang42b29872015-12-16 13:42:42 +08001181 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001182 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001183 if (!spec->dyn_pcm_assign) {
1184 if (snd_BUG_ON(pin_idx < 0)) {
1185 mutex_unlock(&spec->pcm_lock);
1186 return -EINVAL;
1187 }
1188 } else {
1189 /* no pin is assigned to the PCM
1190 * PA need pcm open successfully when probe
1191 */
1192 if (pin_idx < 0) {
1193 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1194 mutex_unlock(&spec->pcm_lock);
1195 return err;
1196 }
1197 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001198
Takashi Iwai4846a672016-03-21 12:56:46 +01001199 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001200 if (err < 0) {
1201 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001202 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001203 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001204
1205 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001206 /* Claim converter */
1207 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001208
Libin Yangac983792015-12-16 16:48:16 +08001209 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001210 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001211 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001212 hinfo->nid = per_cvt->cvt_nid;
1213
Libin Yang91520852017-01-12 16:04:53 +08001214 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
Takashi Iwaibddee962013-06-18 16:14:22 +02001215 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001216 AC_VERB_SET_CONNECT_SEL,
Takashi Iwai4846a672016-03-21 12:56:46 +01001217 per_pin->mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001218
1219 /* configure unused pins to choose other converters */
Takashi Iwai4846a672016-03-21 12:56:46 +01001220 pin_cvt_fixup(codec, per_pin, 0);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001221
Libin Yang2bf3c852015-12-16 13:42:43 +08001222 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001223
Stephen Warren2def8172011-06-01 11:14:20 -06001224 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001225 hinfo->channels_min = per_cvt->channels_min;
1226 hinfo->channels_max = per_cvt->channels_max;
1227 hinfo->rates = per_cvt->rates;
1228 hinfo->formats = per_cvt->formats;
1229 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001230
Libin Yang42b29872015-12-16 13:42:42 +08001231 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001232 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001233 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001234 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001235 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001236 !hinfo->rates || !hinfo->formats) {
1237 per_cvt->assigned = 0;
1238 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001239 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001240 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001241 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001242 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001243 }
Stephen Warren2def8172011-06-01 11:14:20 -06001244
Libin Yang42b29872015-12-16 13:42:42 +08001245 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001246 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001247 runtime->hw.channels_min = hinfo->channels_min;
1248 runtime->hw.channels_max = hinfo->channels_max;
1249 runtime->hw.formats = hinfo->formats;
1250 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001251
1252 snd_pcm_hw_constraint_step(substream->runtime, 0,
1253 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001254 return 0;
1255}
1256
1257/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001258 * HDA/HDMI auto parsing
1259 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001260static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001261{
1262 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001263 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001264 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001265
1266 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001267 codec_warn(codec,
1268 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001269 pin_nid, get_wcaps(codec, pin_nid));
1270 return -EINVAL;
1271 }
1272
Libin Yang91520852017-01-12 16:04:53 +08001273 /* all the device entries on the same pin have the same conn list */
Stephen Warren384a48d2011-06-01 11:14:21 -06001274 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1275 per_pin->mux_nids,
1276 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001277
1278 return 0;
1279}
1280
Libin Yanga76056f2015-12-16 16:48:15 +08001281static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1282 struct hdmi_spec_per_pin *per_pin)
1283{
1284 int i;
1285
1286 /* try the prefer PCM */
1287 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1288 return per_pin->pin_nid_idx;
1289
1290 /* have a second try; check the "reserved area" over num_pins */
Libin Yang91520852017-01-12 16:04:53 +08001291 for (i = spec->num_nids; i < spec->pcm_used; i++) {
Libin Yanga76056f2015-12-16 16:48:15 +08001292 if (!test_bit(i, &spec->pcm_bitmap))
1293 return i;
1294 }
1295
1296 /* the last try; check the empty slots in pins */
Libin Yang91520852017-01-12 16:04:53 +08001297 for (i = 0; i < spec->num_nids; i++) {
Libin Yanga76056f2015-12-16 16:48:15 +08001298 if (!test_bit(i, &spec->pcm_bitmap))
1299 return i;
1300 }
1301 return -EBUSY;
1302}
1303
1304static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1305 struct hdmi_spec_per_pin *per_pin)
1306{
1307 int idx;
1308
1309 /* pcm already be attached to the pin */
1310 if (per_pin->pcm)
1311 return;
1312 idx = hdmi_find_pcm_slot(spec, per_pin);
Libin Yangd10a80d2016-03-01 15:18:26 +08001313 if (idx == -EBUSY)
Libin Yanga76056f2015-12-16 16:48:15 +08001314 return;
1315 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001316 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001317 set_bit(idx, &spec->pcm_bitmap);
1318}
1319
1320static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1321 struct hdmi_spec_per_pin *per_pin)
1322{
1323 int idx;
1324
1325 /* pcm already be detached from the pin */
1326 if (!per_pin->pcm)
1327 return;
1328 idx = per_pin->pcm_idx;
1329 per_pin->pcm_idx = -1;
1330 per_pin->pcm = NULL;
1331 if (idx >= 0 && idx < spec->pcm_used)
1332 clear_bit(idx, &spec->pcm_bitmap);
1333}
1334
Libin Yangac983792015-12-16 16:48:16 +08001335static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1336 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1337{
1338 int mux_idx;
1339
1340 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1341 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1342 break;
1343 return mux_idx;
1344}
1345
1346static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1347
1348static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1349 struct hdmi_spec_per_pin *per_pin)
1350{
1351 struct hda_codec *codec = per_pin->codec;
1352 struct hda_pcm *pcm;
1353 struct hda_pcm_stream *hinfo;
1354 struct snd_pcm_substream *substream;
1355 int mux_idx;
1356 bool non_pcm;
1357
1358 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001359 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001360 else
1361 return;
1362 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1363 return;
1364
1365 /* hdmi audio only uses playback and one substream */
1366 hinfo = pcm->stream;
1367 substream = pcm->pcm->streams[0].substream;
1368
1369 per_pin->cvt_nid = hinfo->nid;
1370
1371 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
Libin Yang91520852017-01-12 16:04:53 +08001372 if (mux_idx < per_pin->num_mux_nids) {
1373 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1374 per_pin->dev_id);
Libin Yangac983792015-12-16 16:48:16 +08001375 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1376 AC_VERB_SET_CONNECT_SEL,
1377 mux_idx);
Libin Yang91520852017-01-12 16:04:53 +08001378 }
Libin Yangac983792015-12-16 16:48:16 +08001379 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1380
1381 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1382 if (substream->runtime)
1383 per_pin->channels = substream->runtime->channels;
1384 per_pin->setup = true;
1385 per_pin->mux_idx = mux_idx;
1386
1387 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1388}
1389
1390static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1391 struct hdmi_spec_per_pin *per_pin)
1392{
1393 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1394 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1395
1396 per_pin->chmap_set = false;
1397 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1398
1399 per_pin->setup = false;
1400 per_pin->channels = 0;
1401}
1402
Takashi Iwaie90247f2015-11-13 09:12:12 +01001403/* update per_pin ELD from the given new ELD;
1404 * setup info frame and notification accordingly
1405 */
1406static void update_eld(struct hda_codec *codec,
1407 struct hdmi_spec_per_pin *per_pin,
1408 struct hdmi_eld *eld)
1409{
1410 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001411 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001412 bool old_eld_valid = pin_eld->eld_valid;
1413 bool eld_changed;
Libin Yangfb087ea2016-02-23 16:33:37 +08001414 int pcm_idx = -1;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001415
Libin Yangfb087ea2016-02-23 16:33:37 +08001416 /* for monitor disconnection, save pcm_idx firstly */
1417 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001418 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001419 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001420 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001421 hdmi_pcm_setup_pin(spec, per_pin);
1422 } else {
1423 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001424 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001425 }
Libin Yanga76056f2015-12-16 16:48:15 +08001426 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001427 /* if pcm_idx == -1, it means this is in monitor connection event
1428 * we can get the correct pcm_idx now.
1429 */
1430 if (pcm_idx == -1)
1431 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001432
Takashi Iwaie90247f2015-11-13 09:12:12 +01001433 if (eld->eld_valid)
1434 snd_hdmi_show_eld(codec, &eld->info);
1435
1436 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1437 if (eld->eld_valid && pin_eld->eld_valid)
1438 if (pin_eld->eld_size != eld->eld_size ||
1439 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1440 eld->eld_size) != 0)
1441 eld_changed = true;
1442
Takashi Iwaibd481282016-03-18 18:01:53 +01001443 pin_eld->monitor_present = eld->monitor_present;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001444 pin_eld->eld_valid = eld->eld_valid;
1445 pin_eld->eld_size = eld->eld_size;
1446 if (eld->eld_valid)
1447 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1448 pin_eld->info = eld->info;
1449
1450 /*
1451 * Re-setup pin and infoframe. This is needed e.g. when
1452 * - sink is first plugged-in
1453 * - transcoder can change during stream playback on Haswell
1454 * and this can make HW reset converter selection on a pin.
1455 */
1456 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
Takashi Iwai4846a672016-03-21 12:56:46 +01001457 pin_cvt_fixup(codec, per_pin, 0);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001458 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1459 }
1460
Libin Yangfb087ea2016-02-23 16:33:37 +08001461 if (eld_changed && pcm_idx >= 0)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001462 snd_ctl_notify(codec->card,
1463 SNDRV_CTL_EVENT_MASK_VALUE |
1464 SNDRV_CTL_EVENT_MASK_INFO,
Libin Yangfb087ea2016-02-23 16:33:37 +08001465 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001466}
1467
Takashi Iwai788d4412015-11-12 15:36:13 +01001468/* update ELD and jack state via HD-audio verbs */
1469static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1470 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001471{
David Henningsson464837a2013-11-07 13:38:25 +01001472 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001473 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001474 struct hdmi_spec *spec = codec->spec;
1475 struct hdmi_eld *eld = &spec->temp_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001476 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001477 /*
1478 * Always execute a GetPinSense verb here, even when called from
1479 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1480 * response's PD bit is not the real PD value, but indicates that
1481 * the real PD value changed. An older version of the HD-audio
1482 * specification worked this way. Hence, we just ignore the data in
1483 * the unsolicited response to avoid custom WARs.
1484 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001485 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001486 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001487 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001488
David Henningssonda4a7a32013-12-18 10:46:04 +01001489 present = snd_hda_pin_sense(codec, pin_nid);
1490
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001491 mutex_lock(&per_pin->lock);
Takashi Iwaic44da622016-04-13 09:45:53 +02001492 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1493 if (eld->monitor_present)
David Henningsson4bd038f2013-02-19 16:11:25 +01001494 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1495 else
1496 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001497
Takashi Iwai4e76a882014-02-25 12:21:03 +01001498 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001499 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Takashi Iwaic44da622016-04-13 09:45:53 +02001500 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001501
David Henningsson4bd038f2013-02-19 16:11:25 +01001502 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001503 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001504 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001505 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001506 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001507 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001508 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001509 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001510 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001511 if (!eld->eld_valid && repoll)
1512 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001513 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001514
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001515 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001516 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1517 else
1518 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001519
Takashi Iwaic44da622016-04-13 09:45:53 +02001520 ret = !repoll || !eld->monitor_present || eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001521
1522 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1523 if (jack)
1524 jack->block_report = !ret;
1525
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001526 mutex_unlock(&per_pin->lock);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001527 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001528}
1529
Libin Yang31842702016-02-19 15:42:06 +08001530static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1531 struct hdmi_spec_per_pin *per_pin)
1532{
1533 struct hdmi_spec *spec = codec->spec;
1534 struct snd_jack *jack = NULL;
1535 struct hda_jack_tbl *jack_tbl;
1536
1537 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1538 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1539 * NULL even after snd_hda_jack_tbl_clear() is called to
1540 * free snd_jack. This may cause access invalid memory
1541 * when calling snd_jack_report
1542 */
1543 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1544 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1545 else if (!spec->dyn_pcm_assign) {
Libin Yang91520852017-01-12 16:04:53 +08001546 /*
1547 * jack tbl doesn't support DP MST
1548 * DP MST will use dyn_pcm_assign,
1549 * so DP MST will never come here
1550 */
Libin Yang31842702016-02-19 15:42:06 +08001551 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1552 if (jack_tbl)
1553 jack = jack_tbl->jack;
1554 }
1555 return jack;
1556}
1557
Takashi Iwai788d4412015-11-12 15:36:13 +01001558/* update ELD and jack state via audio component */
1559static void sync_eld_via_acomp(struct hda_codec *codec,
1560 struct hdmi_spec_per_pin *per_pin)
1561{
Takashi Iwai788d4412015-11-12 15:36:13 +01001562 struct hdmi_spec *spec = codec->spec;
1563 struct hdmi_eld *eld = &spec->temp_eld;
Libin Yang25e4abb2016-01-12 11:13:27 +08001564 struct snd_jack *jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001565 int size;
1566
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001567 mutex_lock(&per_pin->lock);
Takashi Iwaic64c1432016-03-21 16:07:30 +01001568 eld->monitor_present = false;
Libin Yang91520852017-01-12 16:04:53 +08001569 size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1570 per_pin->dev_id, &eld->monitor_present,
1571 eld->eld_buffer, ELD_MAX_SIZE);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001572 if (size > 0) {
1573 size = min(size, ELD_MAX_SIZE);
1574 if (snd_hdmi_parse_eld(codec, &eld->info,
1575 eld->eld_buffer, size) < 0)
1576 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001577 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001578
1579 if (size > 0) {
1580 eld->eld_valid = true;
1581 eld->eld_size = size;
1582 } else {
1583 eld->eld_valid = false;
1584 eld->eld_size = 0;
1585 }
1586
Libin Yang25e4abb2016-01-12 11:13:27 +08001587 /* pcm_idx >=0 before update_eld() means it is in monitor
1588 * disconnected event. Jack must be fetched before update_eld()
1589 */
Libin Yang31842702016-02-19 15:42:06 +08001590 jack = pin_idx_to_jack(codec, per_pin);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001591 update_eld(codec, per_pin, eld);
Libin Yang31842702016-02-19 15:42:06 +08001592 if (jack == NULL)
1593 jack = pin_idx_to_jack(codec, per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08001594 if (jack == NULL)
1595 goto unlock;
1596 snd_jack_report(jack,
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001597 eld->monitor_present ? SND_JACK_AVOUT : 0);
1598 unlock:
1599 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001600}
1601
1602static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1603{
1604 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08001605 struct hdmi_spec *spec = codec->spec;
1606 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001607
Takashi Iwai222bde02016-03-17 14:48:13 +01001608 /* no temporary power up/down needed for component notifier */
1609 if (!codec_has_acomp(codec))
1610 snd_hda_power_up_pm(codec);
1611
Libin Yanga76056f2015-12-16 16:48:15 +08001612 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001613 if (codec_has_acomp(codec)) {
1614 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001615 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01001616 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08001617 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01001618 }
Libin Yanga76056f2015-12-16 16:48:15 +08001619 mutex_unlock(&spec->pcm_lock);
1620
Takashi Iwai222bde02016-03-17 14:48:13 +01001621 if (!codec_has_acomp(codec))
1622 snd_hda_power_down_pm(codec);
1623
Libin Yanga76056f2015-12-16 16:48:15 +08001624 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001625}
1626
Wu Fengguang744626d2011-11-16 16:29:47 +08001627static void hdmi_repoll_eld(struct work_struct *work)
1628{
1629 struct hdmi_spec_per_pin *per_pin =
1630 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1631
Wu Fengguangc6e84532011-11-18 16:59:32 -06001632 if (per_pin->repoll_count++ > 6)
1633 per_pin->repoll_count = 0;
1634
Takashi Iwaiefe47102013-11-07 13:38:23 +01001635 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1636 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08001637}
1638
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001639static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1640 hda_nid_t nid);
1641
Wu Fengguang079d88c2010-03-08 10:44:23 +08001642static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1643{
1644 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001645 unsigned int caps, config;
1646 int pin_idx;
1647 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001648 int err;
Libin Yang91520852017-01-12 16:04:53 +08001649 int dev_num, i;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001650
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001651 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001652 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1653 return 0;
1654
Libin Yang91520852017-01-12 16:04:53 +08001655 /*
1656 * For DP MST audio, Configuration Default is the same for
1657 * all device entries on the same pin
1658 */
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001659 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001660 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1661 return 0;
1662
Libin Yang91520852017-01-12 16:04:53 +08001663 /*
1664 * To simplify the implementation, malloc all
1665 * the virtual pins in the initialization statically
1666 */
1667 if (is_haswell_plus(codec)) {
1668 /*
1669 * On Intel platforms, device entries number is
1670 * changed dynamically. If there is a DP MST
1671 * hub connected, the device entries number is 3.
1672 * Otherwise, it is 1.
1673 * Here we manually set dev_num to 3, so that
1674 * we can initialize all the device entries when
1675 * bootup statically.
1676 */
1677 dev_num = 3;
1678 spec->dev_num = 3;
1679 } else if (spec->dyn_pcm_assign && codec->dp_mst) {
1680 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1681 /*
1682 * spec->dev_num is the maxinum number of device entries
1683 * among all the pins
1684 */
1685 spec->dev_num = (spec->dev_num > dev_num) ?
1686 spec->dev_num : dev_num;
1687 } else {
1688 /*
1689 * If the platform doesn't support DP MST,
1690 * manually set dev_num to 1. This means
1691 * the pin has only one device entry.
1692 */
1693 dev_num = 1;
1694 spec->dev_num = 1;
Libin Yang2bea2412016-01-12 11:13:26 +08001695 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001696
Libin Yang91520852017-01-12 16:04:53 +08001697 for (i = 0; i < dev_num; i++) {
1698 pin_idx = spec->num_pins;
1699 per_pin = snd_array_new(&spec->pins);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001700
Libin Yang91520852017-01-12 16:04:53 +08001701 if (!per_pin)
1702 return -ENOMEM;
1703
1704 if (spec->dyn_pcm_assign) {
1705 per_pin->pcm = NULL;
1706 per_pin->pcm_idx = -1;
1707 } else {
1708 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1709 per_pin->pcm_idx = pin_idx;
1710 }
1711 per_pin->pin_nid = pin_nid;
1712 per_pin->pin_nid_idx = spec->num_nids;
1713 per_pin->dev_id = i;
1714 per_pin->non_pcm = false;
1715 snd_hda_set_dev_select(codec, pin_nid, i);
1716 if (is_haswell_plus(codec))
1717 intel_haswell_fixup_connect_list(codec, pin_nid);
1718 err = hdmi_read_pin_conn(codec, pin_idx);
1719 if (err < 0)
1720 return err;
1721 spec->num_pins++;
1722 }
1723 spec->num_nids++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001724
Stephen Warren384a48d2011-06-01 11:14:21 -06001725 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001726}
1727
Stephen Warren384a48d2011-06-01 11:14:21 -06001728static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001729{
1730 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001731 struct hdmi_spec_per_cvt *per_cvt;
1732 unsigned int chans;
1733 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001734
Stephen Warren384a48d2011-06-01 11:14:21 -06001735 chans = get_wcaps(codec, cvt_nid);
1736 chans = get_wcaps_channels(chans);
1737
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001738 per_cvt = snd_array_new(&spec->cvts);
1739 if (!per_cvt)
1740 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001741
1742 per_cvt->cvt_nid = cvt_nid;
1743 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001744 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001745 per_cvt->channels_max = chans;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05301746 if (chans > spec->chmap.channels_max)
1747 spec->chmap.channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001748 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001749
1750 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1751 &per_cvt->rates,
1752 &per_cvt->formats,
1753 &per_cvt->maxbps);
1754 if (err < 0)
1755 return err;
1756
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001757 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1758 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1759 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001760
1761 return 0;
1762}
1763
1764static int hdmi_parse_codec(struct hda_codec *codec)
1765{
1766 hda_nid_t nid;
1767 int i, nodes;
1768
Takashi Iwai7639a062015-03-03 10:07:24 +01001769 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001770 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001771 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08001772 return -EINVAL;
1773 }
1774
1775 for (i = 0; i < nodes; i++, nid++) {
1776 unsigned int caps;
1777 unsigned int type;
1778
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001779 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001780 type = get_wcaps_type(caps);
1781
1782 if (!(caps & AC_WCAP_DIGITAL))
1783 continue;
1784
1785 switch (type) {
1786 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001787 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001788 break;
1789 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001790 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001791 break;
1792 }
1793 }
1794
Wu Fengguang079d88c2010-03-08 10:44:23 +08001795 return 0;
1796}
1797
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001798/*
1799 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001800static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1801{
1802 struct hda_spdif_out *spdif;
1803 bool non_pcm;
1804
1805 mutex_lock(&codec->spdif_mutex);
1806 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
Libin Yang960a5812016-06-16 11:13:25 +08001807 /* Add sanity check to pass klockwork check.
1808 * This should never happen.
1809 */
1810 if (WARN_ON(spdif == NULL))
1811 return true;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001812 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1813 mutex_unlock(&codec->spdif_mutex);
1814 return non_pcm;
1815}
1816
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001817/*
1818 * HDMI callbacks
1819 */
1820
1821static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1822 struct hda_codec *codec,
1823 unsigned int stream_tag,
1824 unsigned int format,
1825 struct snd_pcm_substream *substream)
1826{
Stephen Warren384a48d2011-06-01 11:14:21 -06001827 hda_nid_t cvt_nid = hinfo->nid;
1828 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08001829 int pin_idx;
1830 struct hdmi_spec_per_pin *per_pin;
1831 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08001832 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001833 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07001834 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08001835 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001836
Libin Yang42b29872015-12-16 13:42:42 +08001837 mutex_lock(&spec->pcm_lock);
1838 pin_idx = hinfo_to_pin_index(codec, hinfo);
1839 if (spec->dyn_pcm_assign && pin_idx < 0) {
1840 /* when dyn_pcm_assign and pcm is not bound to a pin
1841 * skip pin setup and return 0 to make audio playback
1842 * be ongoing
1843 */
Takashi Iwai4846a672016-03-21 12:56:46 +01001844 pin_cvt_fixup(codec, NULL, cvt_nid);
Libin Yang42b29872015-12-16 13:42:42 +08001845 snd_hda_codec_setup_stream(codec, cvt_nid,
1846 stream_tag, 0, format);
1847 mutex_unlock(&spec->pcm_lock);
1848 return 0;
1849 }
1850
1851 if (snd_BUG_ON(pin_idx < 0)) {
1852 mutex_unlock(&spec->pcm_lock);
1853 return -EINVAL;
1854 }
1855 per_pin = get_pin(spec, pin_idx);
1856 pin_nid = per_pin->pin_nid;
Takashi Iwai4846a672016-03-21 12:56:46 +01001857
1858 /* Verify pin:cvt selections to avoid silent audio after S3.
1859 * After S3, the audio driver restores pin:cvt selections
1860 * but this can happen before gfx is ready and such selection
1861 * is overlooked by HW. Thus multiple pins can share a same
1862 * default convertor and mute control will affect each other,
1863 * which can cause a resumed audio playback become silent
1864 * after S3.
1865 */
1866 pin_cvt_fixup(codec, per_pin, 0);
Mengdong Lin2df67422014-03-20 13:01:06 +08001867
Libin Yangddd621f2015-09-02 14:11:40 +08001868 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1869 /* Todo: add DP1.2 MST audio support later */
Takashi Iwai93a9ff12016-03-18 19:45:13 +01001870 if (codec_has_acomp(codec))
Libin Yang91520852017-01-12 16:04:53 +08001871 snd_hdac_sync_audio_rate(&codec->core, pin_nid, per_pin->dev_id,
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07001872 runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08001873
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001874 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001875 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02001876 per_pin->channels = substream->runtime->channels;
1877 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001878
Takashi Iwaib0540872013-09-02 12:33:02 +02001879 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001880 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07001881 if (spec->dyn_pin_out) {
1882 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1883 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1884 snd_hda_codec_write(codec, pin_nid, 0,
1885 AC_VERB_SET_PIN_WIDGET_CONTROL,
1886 pinctl | PIN_OUT);
1887 }
1888
Libin Yang91520852017-01-12 16:04:53 +08001889 /* snd_hda_set_dev_select() has been called before */
Libin Yang42b29872015-12-16 13:42:42 +08001890 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1891 stream_tag, format);
1892 mutex_unlock(&spec->pcm_lock);
1893 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001894}
1895
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001896static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1897 struct hda_codec *codec,
1898 struct snd_pcm_substream *substream)
1899{
1900 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1901 return 0;
1902}
1903
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001904static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1905 struct hda_codec *codec,
1906 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001907{
1908 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08001909 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06001910 struct hdmi_spec_per_cvt *per_cvt;
1911 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07001912 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06001913
Stephen Warren384a48d2011-06-01 11:14:21 -06001914 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08001915 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1916 if (snd_BUG_ON(pcm_idx < 0))
1917 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001918 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001919 if (snd_BUG_ON(cvt_idx < 0))
1920 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001921 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001922
1923 snd_BUG_ON(!per_cvt->assigned);
1924 per_cvt->assigned = 0;
1925 hinfo->nid = 0;
1926
Libin Yang42b29872015-12-16 13:42:42 +08001927 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08001928 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001929 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001930 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001931 if (spec->dyn_pcm_assign && pin_idx < 0) {
1932 mutex_unlock(&spec->pcm_lock);
1933 return 0;
1934 }
1935
1936 if (snd_BUG_ON(pin_idx < 0)) {
1937 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06001938 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08001939 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001940 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001941
Stephen Warren75fae112014-01-30 11:52:16 -07001942 if (spec->dyn_pin_out) {
1943 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1944 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1945 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1946 AC_VERB_SET_PIN_WIDGET_CONTROL,
1947 pinctl & ~PIN_OUT);
1948 }
1949
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001950 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001951 per_pin->chmap_set = false;
1952 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001953
1954 per_pin->setup = false;
1955 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001956 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08001957 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06001958 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001959
Stephen Warren384a48d2011-06-01 11:14:21 -06001960 return 0;
1961}
1962
1963static const struct hda_pcm_ops generic_ops = {
1964 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001965 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001966 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001967 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001968};
1969
Subhransu S. Prusty44fde3b2016-04-04 19:23:54 +05301970static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1971{
1972 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1973 struct hdmi_spec *spec = codec->spec;
1974 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1975
1976 if (!per_pin)
1977 return 0;
1978
1979 return per_pin->sink_eld.info.spk_alloc;
1980}
1981
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05301982static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1983 unsigned char *chmap)
1984{
1985 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1986 struct hdmi_spec *spec = codec->spec;
1987 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1988
1989 /* chmap is already set to 0 in caller */
1990 if (!per_pin)
1991 return;
1992
1993 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
1994}
1995
1996static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1997 unsigned char *chmap, int prepared)
1998{
1999 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2000 struct hdmi_spec *spec = codec->spec;
2001 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2002
Libin Yanged0739b2016-04-18 09:16:28 +08002003 if (!per_pin)
2004 return;
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302005 mutex_lock(&per_pin->lock);
2006 per_pin->chmap_set = true;
2007 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2008 if (prepared)
2009 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2010 mutex_unlock(&per_pin->lock);
2011}
2012
2013static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2014{
2015 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2016 struct hdmi_spec *spec = codec->spec;
2017 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2018
2019 return per_pin ? true:false;
2020}
2021
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002022static int generic_hdmi_build_pcms(struct hda_codec *codec)
2023{
2024 struct hdmi_spec *spec = codec->spec;
Libin Yang91520852017-01-12 16:04:53 +08002025 int idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002026
Libin Yang91520852017-01-12 16:04:53 +08002027 /*
2028 * for non-mst mode, pcm number is the same as before
2029 * for DP MST mode, pcm number is (nid number + dev_num - 1)
2030 * dev_num is the device entry number in a pin
2031 *
2032 */
2033 for (idx = 0; idx < spec->num_nids + spec->dev_num - 1; idx++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002034 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002035 struct hda_pcm_stream *pstr;
2036
Libin Yang91520852017-01-12 16:04:53 +08002037 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002038 if (!info)
2039 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002040
Libin Yang91520852017-01-12 16:04:53 +08002041 spec->pcm_rec[idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002042 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002043 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002044 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002045
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002046 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002047 pstr->substreams = 1;
2048 pstr->ops = generic_ops;
Libin Yang91520852017-01-12 16:04:53 +08002049 /* pcm number is less than 16 */
2050 if (spec->pcm_used >= 16)
2051 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06002052 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002053 }
2054
2055 return 0;
2056}
2057
Libin Yang25e4abb2016-01-12 11:13:27 +08002058static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01002059{
Libin Yang25e4abb2016-01-12 11:13:27 +08002060 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01002061
Libin Yang25e4abb2016-01-12 11:13:27 +08002062 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002063}
2064
Libin Yang25e4abb2016-01-12 11:13:27 +08002065static int add_hdmi_jack_kctl(struct hda_codec *codec,
2066 struct hdmi_spec *spec,
2067 int pcm_idx,
Takashi Iwai788d4412015-11-12 15:36:13 +01002068 const char *name)
2069{
2070 struct snd_jack *jack;
2071 int err;
2072
2073 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2074 true, false);
2075 if (err < 0)
2076 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002077
2078 spec->pcm_rec[pcm_idx].jack = jack;
2079 jack->private_data = &spec->pcm_rec[pcm_idx];
2080 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01002081 return 0;
2082}
2083
Libin Yang25e4abb2016-01-12 11:13:27 +08002084static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
David Henningsson0b6c49b2011-08-23 16:56:03 +02002085{
Takashi Iwai31ef2252011-12-01 17:41:36 +01002086 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02002087 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002088 struct hdmi_spec_per_pin *per_pin;
2089 struct hda_jack_tbl *jack;
2090 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01002091 bool phantom_jack;
Libin Yang25e4abb2016-01-12 11:13:27 +08002092 int ret;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002093
Takashi Iwai31ef2252011-12-01 17:41:36 +01002094 if (pcmdev > 0)
2095 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Libin Yang25e4abb2016-01-12 11:13:27 +08002096
2097 if (spec->dyn_pcm_assign)
2098 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2099
2100 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2101 /* if !dyn_pcm_assign, it must be non-MST mode.
2102 * This means pcms and pins are statically mapped.
2103 * And pcm_idx is pin_idx.
2104 */
2105 per_pin = get_pin(spec, pcm_idx);
Takashi Iwai909cadc2015-11-12 11:52:13 +01002106 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2107 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01002108 strncat(hdmi_str, " Phantom",
2109 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
Libin Yang25e4abb2016-01-12 11:13:27 +08002110 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2111 phantom_jack);
2112 if (ret < 0)
2113 return ret;
2114 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2115 if (jack == NULL)
2116 return 0;
2117 /* assign jack->jack to pcm_rec[].jack to
2118 * align with dyn_pcm_assign mode
2119 */
2120 spec->pcm_rec[pcm_idx].jack = jack->jack;
2121 return 0;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002122}
2123
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002124static int generic_hdmi_build_controls(struct hda_codec *codec)
2125{
2126 struct hdmi_spec *spec = codec->spec;
2127 int err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002128 int pin_idx, pcm_idx;
2129
2130
2131 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2132 err = generic_hdmi_build_jack(codec, pcm_idx);
2133 if (err < 0)
2134 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08002135
2136 /* create the spdif for each pcm
2137 * pin will be bound when monitor is connected
2138 */
2139 if (spec->dyn_pcm_assign)
2140 err = snd_hda_create_dig_out_ctls(codec,
2141 0, spec->cvt_nids[0],
2142 HDA_PCM_TYPE_HDMI);
2143 else {
2144 struct hdmi_spec_per_pin *per_pin =
2145 get_pin(spec, pcm_idx);
2146 err = snd_hda_create_dig_out_ctls(codec,
2147 per_pin->pin_nid,
2148 per_pin->mux_nids[0],
2149 HDA_PCM_TYPE_HDMI);
2150 }
2151 if (err < 0)
2152 return err;
2153 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002154
2155 /* add control for ELD Bytes */
2156 err = hdmi_create_eld_ctl(codec, pcm_idx,
2157 get_pcm_rec(spec, pcm_idx)->device);
2158 if (err < 0)
2159 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002160 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002161
Stephen Warren384a48d2011-06-01 11:14:21 -06002162 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002163 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002164
Takashi Iwai82b1d732011-12-20 15:53:07 +01002165 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002166 }
2167
Takashi Iwaid45e6882012-07-31 11:36:00 +02002168 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002169 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002170 struct hda_pcm *pcm;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002171
Libin Yang022f3442016-02-03 10:48:34 +08002172 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002173 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002174 break;
Subhransu S. Prusty2f6e8a82016-03-04 19:59:51 +05302175 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002176 if (err < 0)
2177 return err;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002178 }
2179
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002180 return 0;
2181}
2182
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002183static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2184{
2185 struct hdmi_spec *spec = codec->spec;
2186 int pin_idx;
2187
2188 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002189 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002190
2191 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002192 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002193 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002194 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002195 }
2196 return 0;
2197}
2198
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002199static int generic_hdmi_init(struct hda_codec *codec)
2200{
2201 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002202 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002203
Stephen Warren384a48d2011-06-01 11:14:21 -06002204 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002205 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002206 hda_nid_t pin_nid = per_pin->pin_nid;
Libin Yang91520852017-01-12 16:04:53 +08002207 int dev_id = per_pin->dev_id;
Stephen Warren384a48d2011-06-01 11:14:21 -06002208
Libin Yang91520852017-01-12 16:04:53 +08002209 snd_hda_set_dev_select(codec, pin_nid, dev_id);
Stephen Warren384a48d2011-06-01 11:14:21 -06002210 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002211 if (!codec_has_acomp(codec))
2212 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2213 codec->jackpoll_interval > 0 ?
2214 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002215 }
2216 return 0;
2217}
2218
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002219static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2220{
2221 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2222 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002223}
2224
2225static void hdmi_array_free(struct hdmi_spec *spec)
2226{
2227 snd_array_free(&spec->pins);
2228 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002229}
2230
Takashi Iwaia6866322016-03-21 12:18:33 +01002231static void generic_spec_free(struct hda_codec *codec)
2232{
2233 struct hdmi_spec *spec = codec->spec;
2234
2235 if (spec) {
2236 hdmi_array_free(spec);
2237 kfree(spec);
2238 codec->spec = NULL;
2239 }
2240 codec->dp_mst = false;
2241}
2242
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002243static void generic_hdmi_free(struct hda_codec *codec)
2244{
2245 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002246 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002247
Takashi Iwai66032492015-12-01 16:49:35 +01002248 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002249 snd_hdac_i915_register_notifier(NULL);
2250
Stephen Warren384a48d2011-06-01 11:14:21 -06002251 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002252 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002253 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002254 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002255 }
2256
2257 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2258 if (spec->pcm_rec[pcm_idx].jack == NULL)
2259 continue;
2260 if (spec->dyn_pcm_assign)
2261 snd_device_free(codec->card,
2262 spec->pcm_rec[pcm_idx].jack);
2263 else
2264 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002265 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002266
Takashi Iwaia6866322016-03-21 12:18:33 +01002267 generic_spec_free(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002268}
2269
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002270#ifdef CONFIG_PM
2271static int generic_hdmi_resume(struct hda_codec *codec)
2272{
2273 struct hdmi_spec *spec = codec->spec;
2274 int pin_idx;
2275
Pierre Ossmana2833682014-06-18 21:48:09 +02002276 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002277 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002278
2279 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2280 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2281 hdmi_present_sense(per_pin, 1);
2282 }
2283 return 0;
2284}
2285#endif
2286
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002287static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002288 .init = generic_hdmi_init,
2289 .free = generic_hdmi_free,
2290 .build_pcms = generic_hdmi_build_pcms,
2291 .build_controls = generic_hdmi_build_controls,
2292 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002293#ifdef CONFIG_PM
2294 .resume = generic_hdmi_resume,
2295#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002296};
2297
Anssi Hannula307229d2013-10-24 21:10:34 +03002298static const struct hdmi_ops generic_standard_hdmi_ops = {
2299 .pin_get_eld = snd_hdmi_get_eld,
Anssi Hannula307229d2013-10-24 21:10:34 +03002300 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2301 .pin_hbr_setup = hdmi_pin_hbr_setup,
2302 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302303};
2304
Takashi Iwaia6866322016-03-21 12:18:33 +01002305/* allocate codec->spec and assign/initialize generic parser ops */
2306static int alloc_generic_hdmi(struct hda_codec *codec)
2307{
2308 struct hdmi_spec *spec;
2309
2310 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2311 if (!spec)
2312 return -ENOMEM;
2313
2314 spec->ops = generic_standard_hdmi_ops;
Libin Yang91520852017-01-12 16:04:53 +08002315 spec->dev_num = 1; /* initialize to 1 */
Takashi Iwaia6866322016-03-21 12:18:33 +01002316 mutex_init(&spec->pcm_lock);
2317 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2318
2319 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2320 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2321 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
Subhransu S. Prusty44fde3b2016-04-04 19:23:54 +05302322 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
Takashi Iwaia6866322016-03-21 12:18:33 +01002323
2324 codec->spec = spec;
2325 hdmi_array_init(spec, 4);
2326
2327 codec->patch_ops = generic_hdmi_patch_ops;
2328
2329 return 0;
2330}
2331
2332/* generic HDMI parser */
2333static int patch_generic_hdmi(struct hda_codec *codec)
2334{
2335 int err;
2336
2337 err = alloc_generic_hdmi(codec);
2338 if (err < 0)
2339 return err;
2340
2341 err = hdmi_parse_codec(codec);
2342 if (err < 0) {
2343 generic_spec_free(codec);
2344 return err;
2345 }
2346
2347 generic_hdmi_init_per_pins(codec);
2348 return 0;
2349}
2350
2351/*
2352 * Intel codec parsers and helpers
2353 */
2354
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002355static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2356 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002357{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002358 struct hdmi_spec *spec = codec->spec;
2359 hda_nid_t conns[4];
2360 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002361
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002362 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2363 if (nconns == spec->num_cvts &&
2364 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002365 return;
2366
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002367 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002368 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002369 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002370}
2371
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002372#define INTEL_VENDOR_NID 0x08
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302373#define INTEL_GLK_VENDOR_NID 0x0B
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002374#define INTEL_GET_VENDOR_VERB 0xf81
2375#define INTEL_SET_VENDOR_VERB 0x781
2376#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2377#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2378
2379static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002380 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002381{
2382 unsigned int vendor_param;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302383 struct hdmi_spec *spec = codec->spec;
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002384
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302385 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002386 INTEL_GET_VENDOR_VERB, 0);
2387 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2388 return;
2389
2390 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302391 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002392 INTEL_SET_VENDOR_VERB, vendor_param);
2393 if (vendor_param == -1)
2394 return;
2395
Takashi Iwai17df3f52013-05-08 08:09:34 +02002396 if (update_tree)
2397 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002398}
2399
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002400static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2401{
2402 unsigned int vendor_param;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302403 struct hdmi_spec *spec = codec->spec;
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002404
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302405 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002406 INTEL_GET_VENDOR_VERB, 0);
2407 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2408 return;
2409
2410 /* enable DP1.2 mode */
2411 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002412 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302413 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002414 INTEL_SET_VENDOR_VERB, vendor_param);
2415}
2416
Takashi Iwai17df3f52013-05-08 08:09:34 +02002417/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2418 * Otherwise you may get severe h/w communication errors.
2419 */
2420static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2421 unsigned int power_state)
2422{
2423 if (power_state == AC_PWRST_D0) {
2424 intel_haswell_enable_all_pins(codec, false);
2425 intel_haswell_fixup_enable_dp12(codec);
2426 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002427
Takashi Iwai17df3f52013-05-08 08:09:34 +02002428 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2429 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2430}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002431
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07002432static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
David Henningsson25adc132015-08-19 10:48:58 +02002433{
2434 struct hda_codec *codec = audio_ptr;
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002435 int pin_nid;
Libin Yang91520852017-01-12 16:04:53 +08002436 int dev_id = pipe;
David Henningsson25adc132015-08-19 10:48:58 +02002437
Takashi Iwai4f8e4f32016-03-10 12:02:49 +01002438 /* we assume only from port-B to port-D */
2439 if (port < 1 || port > 3)
2440 return;
2441
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002442 switch (codec->core.vendor_id) {
2443 case 0x80860054: /* ILK */
2444 case 0x80862804: /* ILK */
2445 case 0x80862882: /* VLV */
2446 pin_nid = port + 0x03;
2447 break;
2448 default:
2449 pin_nid = port + 0x04;
2450 break;
2451 }
2452
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002453 /* skip notification during system suspend (but not in runtime PM);
2454 * the state will be updated at resume
2455 */
2456 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2457 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002458 /* ditto during suspend/resume process itself */
2459 if (atomic_read(&(codec)->core.in_pm))
2460 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002461
Takashi Iwaibb03ed22016-04-21 16:39:17 +02002462 snd_hdac_i915_set_bclk(&codec->bus->core);
Libin Yang91520852017-01-12 16:04:53 +08002463 check_presence_and_report(codec, pin_nid, dev_id);
David Henningsson25adc132015-08-19 10:48:58 +02002464}
2465
Takashi Iwaia6866322016-03-21 12:18:33 +01002466/* register i915 component pin_eld_notify callback */
2467static void register_i915_notifier(struct hda_codec *codec)
2468{
2469 struct hdmi_spec *spec = codec->spec;
2470
2471 spec->use_acomp_notifier = true;
2472 spec->i915_audio_ops.audio_ptr = codec;
2473 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2474 * will call pin_eld_notify with using audio_ptr pointer
2475 * We need make sure audio_ptr is really setup
2476 */
2477 wmb();
2478 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2479 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2480}
2481
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002482/* setup_stream ops override for HSW+ */
2483static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
2484 hda_nid_t pin_nid, u32 stream_tag, int format)
2485{
2486 haswell_verify_D0(codec, cvt_nid, pin_nid);
2487 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
2488}
2489
Takashi Iwai4846a672016-03-21 12:56:46 +01002490/* pin_cvt_fixup ops override for HSW+ and VLV+ */
2491static void i915_pin_cvt_fixup(struct hda_codec *codec,
2492 struct hdmi_spec_per_pin *per_pin,
2493 hda_nid_t cvt_nid)
2494{
2495 if (per_pin) {
Libin Yang91520852017-01-12 16:04:53 +08002496 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2497 per_pin->dev_id);
Takashi Iwai4846a672016-03-21 12:56:46 +01002498 intel_verify_pin_cvt_connect(codec, per_pin);
2499 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
Libin Yang91520852017-01-12 16:04:53 +08002500 per_pin->dev_id, per_pin->mux_idx);
Takashi Iwai4846a672016-03-21 12:56:46 +01002501 } else {
Libin Yang91520852017-01-12 16:04:53 +08002502 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
Takashi Iwai4846a672016-03-21 12:56:46 +01002503 }
2504}
2505
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002506/* precondition and allocation for Intel codecs */
2507static int alloc_intel_hdmi(struct hda_codec *codec)
2508{
2509 /* requires i915 binding */
2510 if (!codec->bus->core.audio_component) {
2511 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2512 return -ENODEV;
2513 }
2514
2515 return alloc_generic_hdmi(codec);
2516}
2517
2518/* parse and post-process for Intel codecs */
2519static int parse_intel_hdmi(struct hda_codec *codec)
2520{
2521 int err;
2522
2523 err = hdmi_parse_codec(codec);
2524 if (err < 0) {
2525 generic_spec_free(codec);
2526 return err;
2527 }
2528
2529 generic_hdmi_init_per_pins(codec);
2530 register_i915_notifier(codec);
2531 return 0;
2532}
2533
Takashi Iwaia6866322016-03-21 12:18:33 +01002534/* Intel Haswell and onwards; audio component with eld notifier */
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302535static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002536{
2537 struct hdmi_spec *spec;
Takashi Iwaia6866322016-03-21 12:18:33 +01002538 int err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002539
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002540 err = alloc_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002541 if (err < 0)
2542 return err;
2543 spec = codec->spec;
Libin Yang91520852017-01-12 16:04:53 +08002544 codec->dp_mst = true;
2545 spec->dyn_pcm_assign = true;
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302546 spec->vendor_nid = vendor_nid;
Takashi Iwaia6866322016-03-21 12:18:33 +01002547
2548 intel_haswell_enable_all_pins(codec, true);
2549 intel_haswell_fixup_enable_dp12(codec);
2550
2551 /* For Haswell/Broadwell, the controller is also in the power well and
2552 * can cover the codec power request, and so need not set this flag.
2553 */
2554 if (!is_haswell(codec) && !is_broadwell(codec))
2555 codec->core.link_power_control = 1;
2556
2557 codec->patch_ops.set_power_state = haswell_set_power_state;
Takashi Iwaia6866322016-03-21 12:18:33 +01002558 codec->depop_delay = 0;
2559 codec->auto_runtime_pm = 1;
2560
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002561 spec->ops.setup_stream = i915_hsw_setup_stream;
Takashi Iwai4846a672016-03-21 12:56:46 +01002562 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002563
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002564 return parse_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002565}
2566
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05302567static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2568{
2569 return intel_hsw_common_init(codec, INTEL_VENDOR_NID);
2570}
2571
2572static int patch_i915_glk_hdmi(struct hda_codec *codec)
2573{
2574 return intel_hsw_common_init(codec, INTEL_GLK_VENDOR_NID);
2575}
2576
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002577/* Intel Baytrail and Braswell; with eld notifier */
Takashi Iwaia6866322016-03-21 12:18:33 +01002578static int patch_i915_byt_hdmi(struct hda_codec *codec)
2579{
2580 struct hdmi_spec *spec;
2581 int err;
2582
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002583 err = alloc_intel_hdmi(codec);
Takashi Iwaia6866322016-03-21 12:18:33 +01002584 if (err < 0)
2585 return err;
2586 spec = codec->spec;
2587
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002588 /* For Valleyview/Cherryview, only the display codec is in the display
2589 * power well and can use link_power ops to request/release the power.
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002590 */
Takashi Iwaia6866322016-03-21 12:18:33 +01002591 codec->core.link_power_control = 1;
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002592
Takashi Iwaia6866322016-03-21 12:18:33 +01002593 codec->depop_delay = 0;
2594 codec->auto_runtime_pm = 1;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002595
Takashi Iwai4846a672016-03-21 12:56:46 +01002596 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2597
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002598 return parse_intel_hdmi(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002599}
2600
Takashi Iwai7ff652f2016-03-21 14:50:24 +01002601/* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
Takashi Iwaie85015a32016-03-21 13:56:19 +01002602static int patch_i915_cpt_hdmi(struct hda_codec *codec)
2603{
Takashi Iwaie85015a32016-03-21 13:56:19 +01002604 int err;
2605
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002606 err = alloc_intel_hdmi(codec);
Takashi Iwaie85015a32016-03-21 13:56:19 +01002607 if (err < 0)
2608 return err;
Takashi Iwai43f6c8d2017-06-28 14:18:29 +02002609 return parse_intel_hdmi(codec);
Takashi Iwaie85015a32016-03-21 13:56:19 +01002610}
2611
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002612/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002613 * Shared non-generic implementations
2614 */
2615
2616static int simple_playback_build_pcms(struct hda_codec *codec)
2617{
2618 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002619 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002620 unsigned int chans;
2621 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002622 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002623
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002624 per_cvt = get_cvt(spec, 0);
2625 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002626 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002627
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002628 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002629 if (!info)
2630 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002631 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002632 info->pcm_type = HDA_PCM_TYPE_HDMI;
2633 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2634 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002635 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002636 if (pstr->channels_max <= 2 && chans && chans <= 16)
2637 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002638
2639 return 0;
2640}
2641
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002642/* unsolicited event for jack sensing */
2643static void simple_hdmi_unsol_event(struct hda_codec *codec,
2644 unsigned int res)
2645{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002646 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002647 snd_hda_jack_report_sync(codec);
2648}
2649
2650/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2651 * as long as spec->pins[] is set correctly
2652 */
2653#define simple_hdmi_build_jack generic_hdmi_build_jack
2654
Stephen Warren3aaf8982011-06-01 11:14:19 -06002655static int simple_playback_build_controls(struct hda_codec *codec)
2656{
2657 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002658 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002659 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002660
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002661 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002662 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2663 per_cvt->cvt_nid,
2664 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002665 if (err < 0)
2666 return err;
2667 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002668}
2669
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002670static int simple_playback_init(struct hda_codec *codec)
2671{
2672 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002673 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2674 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002675
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002676 snd_hda_codec_write(codec, pin, 0,
2677 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2678 /* some codecs require to unmute the pin */
2679 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2680 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2681 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002682 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002683 return 0;
2684}
2685
Stephen Warren3aaf8982011-06-01 11:14:19 -06002686static void simple_playback_free(struct hda_codec *codec)
2687{
2688 struct hdmi_spec *spec = codec->spec;
2689
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002690 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002691 kfree(spec);
2692}
2693
2694/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002695 * Nvidia specific implementations
2696 */
2697
2698#define Nv_VERB_SET_Channel_Allocation 0xF79
2699#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2700#define Nv_VERB_SET_Audio_Protection_On 0xF98
2701#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2702
2703#define nvhdmi_master_con_nid_7x 0x04
2704#define nvhdmi_master_pin_nid_7x 0x05
2705
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002706static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002707 /*front, rear, clfe, rear_surr */
2708 0x6, 0x8, 0xa, 0xc,
2709};
2710
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002711static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2712 /* set audio protect on */
2713 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2714 /* enable digital output on pin widget */
2715 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2716 {} /* terminator */
2717};
2718
2719static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002720 /* set audio protect on */
2721 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2722 /* enable digital output on pin widget */
2723 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2724 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2725 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2726 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2727 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2728 {} /* terminator */
2729};
2730
2731#ifdef LIMITED_RATE_FMT_SUPPORT
2732/* support only the safe format and rate */
2733#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2734#define SUPPORTED_MAXBPS 16
2735#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2736#else
2737/* support all rates and formats */
2738#define SUPPORTED_RATES \
2739 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2740 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2741 SNDRV_PCM_RATE_192000)
2742#define SUPPORTED_MAXBPS 24
2743#define SUPPORTED_FORMATS \
2744 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2745#endif
2746
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002747static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002748{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002749 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2750 return 0;
2751}
2752
2753static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2754{
2755 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002756 return 0;
2757}
2758
Takashi Iwai50c697adf2017-06-07 14:20:07 +02002759static const unsigned int channels_2_6_8[] = {
Nitin Daga393004b2011-01-10 21:49:31 +05302760 2, 6, 8
2761};
2762
Takashi Iwai50c697adf2017-06-07 14:20:07 +02002763static const unsigned int channels_2_8[] = {
Nitin Daga393004b2011-01-10 21:49:31 +05302764 2, 8
2765};
2766
Takashi Iwai50c697adf2017-06-07 14:20:07 +02002767static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
Nitin Daga393004b2011-01-10 21:49:31 +05302768 .count = ARRAY_SIZE(channels_2_6_8),
2769 .list = channels_2_6_8,
2770 .mask = 0,
2771};
2772
Takashi Iwai50c697adf2017-06-07 14:20:07 +02002773static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
Nitin Daga393004b2011-01-10 21:49:31 +05302774 .count = ARRAY_SIZE(channels_2_8),
2775 .list = channels_2_8,
2776 .mask = 0,
2777};
2778
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002779static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2780 struct hda_codec *codec,
2781 struct snd_pcm_substream *substream)
2782{
2783 struct hdmi_spec *spec = codec->spec;
Takashi Iwai50c697adf2017-06-07 14:20:07 +02002784 const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
Nitin Daga393004b2011-01-10 21:49:31 +05302785
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002786 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05302787 case 0x10de0002:
2788 case 0x10de0003:
2789 case 0x10de0005:
2790 case 0x10de0006:
2791 hw_constraints_channels = &hw_constraints_2_8_channels;
2792 break;
2793 case 0x10de0007:
2794 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2795 break;
2796 default:
2797 break;
2798 }
2799
2800 if (hw_constraints_channels != NULL) {
2801 snd_pcm_hw_constraint_list(substream->runtime, 0,
2802 SNDRV_PCM_HW_PARAM_CHANNELS,
2803 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002804 } else {
2805 snd_pcm_hw_constraint_step(substream->runtime, 0,
2806 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302807 }
2808
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002809 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2810}
2811
2812static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2813 struct hda_codec *codec,
2814 struct snd_pcm_substream *substream)
2815{
2816 struct hdmi_spec *spec = codec->spec;
2817 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2818}
2819
2820static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2821 struct hda_codec *codec,
2822 unsigned int stream_tag,
2823 unsigned int format,
2824 struct snd_pcm_substream *substream)
2825{
2826 struct hdmi_spec *spec = codec->spec;
2827 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2828 stream_tag, format, substream);
2829}
2830
Takashi Iwaid0b12522012-06-15 14:34:42 +02002831static const struct hda_pcm_stream simple_pcm_playback = {
2832 .substreams = 1,
2833 .channels_min = 2,
2834 .channels_max = 2,
2835 .ops = {
2836 .open = simple_playback_pcm_open,
2837 .close = simple_playback_pcm_close,
2838 .prepare = simple_playback_pcm_prepare
2839 },
2840};
2841
2842static const struct hda_codec_ops simple_hdmi_patch_ops = {
2843 .build_controls = simple_playback_build_controls,
2844 .build_pcms = simple_playback_build_pcms,
2845 .init = simple_playback_init,
2846 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002847 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002848};
2849
2850static int patch_simple_hdmi(struct hda_codec *codec,
2851 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2852{
2853 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002854 struct hdmi_spec_per_cvt *per_cvt;
2855 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002856
2857 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2858 if (!spec)
2859 return -ENOMEM;
2860
2861 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002862 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002863
2864 spec->multiout.num_dacs = 0; /* no analog */
2865 spec->multiout.max_channels = 2;
2866 spec->multiout.dig_out_nid = cvt_nid;
2867 spec->num_cvts = 1;
2868 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002869 per_pin = snd_array_new(&spec->pins);
2870 per_cvt = snd_array_new(&spec->cvts);
2871 if (!per_pin || !per_cvt) {
2872 simple_playback_free(codec);
2873 return -ENOMEM;
2874 }
2875 per_cvt->cvt_nid = cvt_nid;
2876 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002877 spec->pcm_playback = simple_pcm_playback;
2878
2879 codec->patch_ops = simple_hdmi_patch_ops;
2880
2881 return 0;
2882}
2883
Aaron Plattner1f348522011-04-06 17:19:04 -07002884static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2885 int channels)
2886{
2887 unsigned int chanmask;
2888 int chan = channels ? (channels - 1) : 1;
2889
2890 switch (channels) {
2891 default:
2892 case 0:
2893 case 2:
2894 chanmask = 0x00;
2895 break;
2896 case 4:
2897 chanmask = 0x08;
2898 break;
2899 case 6:
2900 chanmask = 0x0b;
2901 break;
2902 case 8:
2903 chanmask = 0x13;
2904 break;
2905 }
2906
2907 /* Set the audio infoframe channel allocation and checksum fields. The
2908 * channel count is computed implicitly by the hardware. */
2909 snd_hda_codec_write(codec, 0x1, 0,
2910 Nv_VERB_SET_Channel_Allocation, chanmask);
2911
2912 snd_hda_codec_write(codec, 0x1, 0,
2913 Nv_VERB_SET_Info_Frame_Checksum,
2914 (0x71 - chan - chanmask));
2915}
2916
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002917static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2918 struct hda_codec *codec,
2919 struct snd_pcm_substream *substream)
2920{
2921 struct hdmi_spec *spec = codec->spec;
2922 int i;
2923
2924 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2925 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2926 for (i = 0; i < 4; i++) {
2927 /* set the stream id */
2928 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2929 AC_VERB_SET_CHANNEL_STREAMID, 0);
2930 /* set the stream format */
2931 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2932 AC_VERB_SET_STREAM_FORMAT, 0);
2933 }
2934
Aaron Plattner1f348522011-04-06 17:19:04 -07002935 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2936 * streams are disabled. */
2937 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2938
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002939 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2940}
2941
2942static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2943 struct hda_codec *codec,
2944 unsigned int stream_tag,
2945 unsigned int format,
2946 struct snd_pcm_substream *substream)
2947{
2948 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002949 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002950 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002951 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002952 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002953 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002954
2955 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002956 per_cvt = get_cvt(spec, 0);
2957 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002958
2959 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002960
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002961 dataDCC2 = 0x2;
2962
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002963 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002964 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002965 snd_hda_codec_write(codec,
2966 nvhdmi_master_con_nid_7x,
2967 0,
2968 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002969 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002970
2971 /* set the stream id */
2972 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2973 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2974
2975 /* set the stream format */
2976 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2977 AC_VERB_SET_STREAM_FORMAT, format);
2978
2979 /* turn on again (if needed) */
2980 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002981 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002982 snd_hda_codec_write(codec,
2983 nvhdmi_master_con_nid_7x,
2984 0,
2985 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002986 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002987 snd_hda_codec_write(codec,
2988 nvhdmi_master_con_nid_7x,
2989 0,
2990 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2991 }
2992
2993 for (i = 0; i < 4; i++) {
2994 if (chs == 2)
2995 channel_id = 0;
2996 else
2997 channel_id = i * 2;
2998
2999 /* turn off SPDIF once;
3000 *otherwise the IEC958 bits won't be updated
3001 */
3002 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003003 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003004 snd_hda_codec_write(codec,
3005 nvhdmi_con_nids_7x[i],
3006 0,
3007 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003008 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003009 /* set the stream id */
3010 snd_hda_codec_write(codec,
3011 nvhdmi_con_nids_7x[i],
3012 0,
3013 AC_VERB_SET_CHANNEL_STREAMID,
3014 (stream_tag << 4) | channel_id);
3015 /* set the stream format */
3016 snd_hda_codec_write(codec,
3017 nvhdmi_con_nids_7x[i],
3018 0,
3019 AC_VERB_SET_STREAM_FORMAT,
3020 format);
3021 /* turn on again (if needed) */
3022 /* enable and set the channel status audio/data flag */
3023 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003024 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003025 snd_hda_codec_write(codec,
3026 nvhdmi_con_nids_7x[i],
3027 0,
3028 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003029 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003030 snd_hda_codec_write(codec,
3031 nvhdmi_con_nids_7x[i],
3032 0,
3033 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3034 }
3035 }
3036
Aaron Plattner1f348522011-04-06 17:19:04 -07003037 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003038
3039 mutex_unlock(&codec->spdif_mutex);
3040 return 0;
3041}
3042
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003043static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003044 .substreams = 1,
3045 .channels_min = 2,
3046 .channels_max = 8,
3047 .nid = nvhdmi_master_con_nid_7x,
3048 .rates = SUPPORTED_RATES,
3049 .maxbps = SUPPORTED_MAXBPS,
3050 .formats = SUPPORTED_FORMATS,
3051 .ops = {
3052 .open = simple_playback_pcm_open,
3053 .close = nvhdmi_8ch_7x_pcm_close,
3054 .prepare = nvhdmi_8ch_7x_pcm_prepare
3055 },
3056};
3057
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003058static int patch_nvhdmi_2ch(struct hda_codec *codec)
3059{
3060 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003061 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3062 nvhdmi_master_pin_nid_7x);
3063 if (err < 0)
3064 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003065
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003066 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003067 /* override the PCM rates, etc, as the codec doesn't give full list */
3068 spec = codec->spec;
3069 spec->pcm_playback.rates = SUPPORTED_RATES;
3070 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3071 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003072 return 0;
3073}
3074
Takashi Iwai53775b02012-08-01 12:17:41 +02003075static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3076{
3077 struct hdmi_spec *spec = codec->spec;
3078 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003079 if (!err) {
3080 struct hda_pcm *info = get_pcm_rec(spec, 0);
3081 info->own_chmap = true;
3082 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003083 return err;
3084}
3085
3086static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3087{
3088 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003089 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003090 struct snd_pcm_chmap *chmap;
3091 int err;
3092
3093 err = simple_playback_build_controls(codec);
3094 if (err < 0)
3095 return err;
3096
3097 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003098 info = get_pcm_rec(spec, 0);
3099 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003100 SNDRV_PCM_STREAM_PLAYBACK,
3101 snd_pcm_alt_chmaps, 8, 0, &chmap);
3102 if (err < 0)
3103 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003104 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003105 case 0x10de0002:
3106 case 0x10de0003:
3107 case 0x10de0005:
3108 case 0x10de0006:
3109 chmap->channel_mask = (1U << 2) | (1U << 8);
3110 break;
3111 case 0x10de0007:
3112 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3113 }
3114 return 0;
3115}
3116
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003117static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3118{
3119 struct hdmi_spec *spec;
3120 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003121 if (err < 0)
3122 return err;
3123 spec = codec->spec;
3124 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003125 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003126 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003127 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3128 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003129
3130 /* Initialize the audio infoframe channel mask and checksum to something
3131 * valid */
3132 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3133
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003134 return 0;
3135}
3136
3137/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003138 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3139 * - 0x10de0015
3140 * - 0x10de0040
3141 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303142static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303143 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02003144{
3145 if (cap->ca_index == 0x00 && channels == 2)
3146 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3147
Subhransu S. Prusty028cb682016-03-14 10:35:06 +05303148 /* If the speaker allocation matches the channel count, it is OK. */
3149 if (cap->channels != channels)
3150 return -1;
3151
3152 /* all channels are remappable freely */
3153 return SNDRV_CTL_TLVT_CHMAP_VAR;
Anssi Hannula611885b2013-11-03 17:15:00 +02003154}
3155
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303156static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3157 int ca, int chs, unsigned char *map)
Anssi Hannula611885b2013-11-03 17:15:00 +02003158{
3159 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3160 return -EINVAL;
3161
3162 return 0;
3163}
3164
3165static int patch_nvhdmi(struct hda_codec *codec)
3166{
3167 struct hdmi_spec *spec;
3168 int err;
3169
3170 err = patch_generic_hdmi(codec);
3171 if (err)
3172 return err;
3173
3174 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003175 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003176
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303177 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02003178 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303179 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02003180
3181 return 0;
3182}
3183
3184/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003185 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3186 * accessed using vendor-defined verbs. These registers can be used for
3187 * interoperability between the HDA and HDMI drivers.
3188 */
3189
3190/* Audio Function Group node */
3191#define NVIDIA_AFG_NID 0x01
3192
3193/*
3194 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3195 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3196 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3197 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3198 * additional bit (at position 30) to signal the validity of the format.
3199 *
3200 * | 31 | 30 | 29 16 | 15 0 |
3201 * +---------+-------+--------+--------+
3202 * | TRIGGER | VALID | UNUSED | FORMAT |
3203 * +-----------------------------------|
3204 *
3205 * Note that for the trigger bit to take effect it needs to change value
3206 * (i.e. it needs to be toggled).
3207 */
3208#define NVIDIA_GET_SCRATCH0 0xfa6
3209#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3210#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3211#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3212#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3213#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3214#define NVIDIA_SCRATCH_VALID (1 << 6)
3215
3216#define NVIDIA_GET_SCRATCH1 0xfab
3217#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3218#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3219#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3220#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3221
3222/*
3223 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3224 * the format is invalidated so that the HDMI codec can be disabled.
3225 */
3226static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3227{
3228 unsigned int value;
3229
3230 /* bits [31:30] contain the trigger and valid bits */
3231 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3232 NVIDIA_GET_SCRATCH0, 0);
3233 value = (value >> 24) & 0xff;
3234
3235 /* bits [15:0] are used to store the HDA format */
3236 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3237 NVIDIA_SET_SCRATCH0_BYTE0,
3238 (format >> 0) & 0xff);
3239 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3240 NVIDIA_SET_SCRATCH0_BYTE1,
3241 (format >> 8) & 0xff);
3242
3243 /* bits [16:24] are unused */
3244 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3245 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3246
3247 /*
3248 * Bit 30 signals that the data is valid and hence that HDMI audio can
3249 * be enabled.
3250 */
3251 if (format == 0)
3252 value &= ~NVIDIA_SCRATCH_VALID;
3253 else
3254 value |= NVIDIA_SCRATCH_VALID;
3255
3256 /*
3257 * Whenever the trigger bit is toggled, an interrupt is raised in the
3258 * HDMI codec. The HDMI driver will use that as trigger to update its
3259 * configuration.
3260 */
3261 value ^= NVIDIA_SCRATCH_TRIGGER;
3262
3263 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3264 NVIDIA_SET_SCRATCH0_BYTE3, value);
3265}
3266
3267static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3268 struct hda_codec *codec,
3269 unsigned int stream_tag,
3270 unsigned int format,
3271 struct snd_pcm_substream *substream)
3272{
3273 int err;
3274
3275 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3276 format, substream);
3277 if (err < 0)
3278 return err;
3279
3280 /* notify the HDMI codec of the format change */
3281 tegra_hdmi_set_format(codec, format);
3282
3283 return 0;
3284}
3285
3286static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3287 struct hda_codec *codec,
3288 struct snd_pcm_substream *substream)
3289{
3290 /* invalidate the format in the HDMI codec */
3291 tegra_hdmi_set_format(codec, 0);
3292
3293 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3294}
3295
3296static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3297{
3298 struct hdmi_spec *spec = codec->spec;
3299 unsigned int i;
3300
3301 for (i = 0; i < spec->num_pins; i++) {
3302 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3303
3304 if (pcm->pcm_type == type)
3305 return pcm;
3306 }
3307
3308 return NULL;
3309}
3310
3311static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3312{
3313 struct hda_pcm_stream *stream;
3314 struct hda_pcm *pcm;
3315 int err;
3316
3317 err = generic_hdmi_build_pcms(codec);
3318 if (err < 0)
3319 return err;
3320
3321 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3322 if (!pcm)
3323 return -ENODEV;
3324
3325 /*
3326 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3327 * codec about format changes.
3328 */
3329 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3330 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3331 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3332
3333 return 0;
3334}
3335
3336static int patch_tegra_hdmi(struct hda_codec *codec)
3337{
3338 int err;
3339
3340 err = patch_generic_hdmi(codec);
3341 if (err)
3342 return err;
3343
3344 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3345
3346 return 0;
3347}
3348
3349/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003350 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003351 */
3352
Anssi Hannula5a6135842013-10-24 21:10:35 +03003353#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003354 ((codec)->core.vendor_id == 0x1002aa01 && \
3355 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003356#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003357
Anssi Hannula5a6135842013-10-24 21:10:35 +03003358/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3359#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3360#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3361#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3362#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3363#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3364#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003365#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003366#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3367#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3368#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3369#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3370#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3371#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3372#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3373#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3374#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3375#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3376#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003377#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003378#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3379#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3380#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3381#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3382#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3383
Anssi Hannula84d69e72013-10-24 21:10:38 +03003384/* AMD specific HDA cvt verbs */
3385#define ATI_VERB_SET_RAMP_RATE 0x770
3386#define ATI_VERB_GET_RAMP_RATE 0xf70
3387
Anssi Hannula5a6135842013-10-24 21:10:35 +03003388#define ATI_OUT_ENABLE 0x1
3389
3390#define ATI_MULTICHANNEL_MODE_PAIRED 0
3391#define ATI_MULTICHANNEL_MODE_SINGLE 1
3392
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003393#define ATI_HBR_CAPABLE 0x01
3394#define ATI_HBR_ENABLE 0x10
3395
Anssi Hannula89250f82013-10-24 21:10:36 +03003396static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3397 unsigned char *buf, int *eld_size)
3398{
3399 /* call hda_eld.c ATI/AMD-specific function */
3400 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3401 is_amdhdmi_rev3_or_later(codec));
3402}
3403
Anssi Hannula5a6135842013-10-24 21:10:35 +03003404static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3405 int active_channels, int conn_type)
3406{
3407 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3408}
3409
3410static int atihdmi_paired_swap_fc_lfe(int pos)
3411{
3412 /*
3413 * ATI/AMD have automatic FC/LFE swap built-in
3414 * when in pairwise mapping mode.
3415 */
3416
3417 switch (pos) {
3418 /* see channel_allocations[].speakers[] */
3419 case 2: return 3;
3420 case 3: return 2;
3421 default: break;
3422 }
3423
3424 return pos;
3425}
3426
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303427static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3428 int ca, int chs, unsigned char *map)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003429{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303430 struct hdac_cea_channel_speaker_allocation *cap;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003431 int i, j;
3432
3433 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3434
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303435 cap = snd_hdac_get_ch_alloc_from_ca(ca);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003436 for (i = 0; i < chs; ++i) {
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303437 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003438 bool ok = false;
3439 bool companion_ok = false;
3440
3441 if (!mask)
3442 continue;
3443
3444 for (j = 0 + i % 2; j < 8; j += 2) {
3445 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3446 if (cap->speakers[chan_idx] == mask) {
3447 /* channel is in a supported position */
3448 ok = true;
3449
3450 if (i % 2 == 0 && i + 1 < chs) {
3451 /* even channel, check the odd companion */
3452 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303453 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003454 int comp_mask_act = cap->speakers[comp_chan_idx];
3455
3456 if (comp_mask_req == comp_mask_act)
3457 companion_ok = true;
3458 else
3459 return -EINVAL;
3460 }
3461 break;
3462 }
3463 }
3464
3465 if (!ok)
3466 return -EINVAL;
3467
3468 if (companion_ok)
3469 i++; /* companion channel already checked */
3470 }
3471
3472 return 0;
3473}
3474
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303475static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3476 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003477{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303478 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003479 int verb;
3480 int ati_channel_setup = 0;
3481
3482 if (hdmi_slot > 7)
3483 return -EINVAL;
3484
3485 if (!has_amd_full_remap_support(codec)) {
3486 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3487
3488 /* In case this is an odd slot but without stream channel, do not
3489 * disable the slot since the corresponding even slot could have a
3490 * channel. In case neither have a channel, the slot pair will be
3491 * disabled when this function is called for the even slot. */
3492 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3493 return 0;
3494
3495 hdmi_slot -= hdmi_slot % 2;
3496
3497 if (stream_channel != 0xf)
3498 stream_channel -= stream_channel % 2;
3499 }
3500
3501 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3502
3503 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3504
3505 if (stream_channel != 0xf)
3506 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3507
3508 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3509}
3510
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303511static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3512 hda_nid_t pin_nid, int asp_slot)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003513{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303514 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003515 bool was_odd = false;
3516 int ati_asp_slot = asp_slot;
3517 int verb;
3518 int ati_channel_setup;
3519
3520 if (asp_slot > 7)
3521 return -EINVAL;
3522
3523 if (!has_amd_full_remap_support(codec)) {
3524 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3525 if (ati_asp_slot % 2 != 0) {
3526 ati_asp_slot -= 1;
3527 was_odd = true;
3528 }
3529 }
3530
3531 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3532
3533 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3534
3535 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3536 return 0xf;
3537
3538 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3539}
3540
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303541static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3542 struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303543 struct hdac_cea_channel_speaker_allocation *cap,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303544 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003545{
3546 int c;
3547
3548 /*
3549 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3550 * we need to take that into account (a single channel may take 2
3551 * channel slots if we need to carry a silent channel next to it).
3552 * On Rev3+ AMD codecs this function is not used.
3553 */
3554 int chanpairs = 0;
3555
3556 /* We only produce even-numbered channel count TLVs */
3557 if ((channels % 2) != 0)
3558 return -1;
3559
3560 for (c = 0; c < 7; c += 2) {
3561 if (cap->speakers[c] || cap->speakers[c+1])
3562 chanpairs++;
3563 }
3564
3565 if (chanpairs * 2 != channels)
3566 return -1;
3567
3568 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3569}
3570
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303571static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303572 struct hdac_cea_channel_speaker_allocation *cap,
3573 unsigned int *chmap, int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003574{
3575 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3576 int count = 0;
3577 int c;
3578
3579 for (c = 7; c >= 0; c--) {
3580 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3581 int spk = cap->speakers[chan];
3582 if (!spk) {
3583 /* add N/A channel if the companion channel is occupied */
3584 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3585 chmap[count++] = SNDRV_CHMAP_NA;
3586
3587 continue;
3588 }
3589
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303590 chmap[count++] = snd_hdac_spk_to_chmap(spk);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003591 }
3592
3593 WARN_ON(count != channels);
3594}
3595
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003596static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3597 bool hbr)
3598{
3599 int hbr_ctl, hbr_ctl_new;
3600
3601 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003602 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003603 if (hbr)
3604 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3605 else
3606 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3607
Takashi Iwai4e76a882014-02-25 12:21:03 +01003608 codec_dbg(codec,
3609 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003610 pin_nid,
3611 hbr_ctl == hbr_ctl_new ? "" : "new-",
3612 hbr_ctl_new);
3613
3614 if (hbr_ctl != hbr_ctl_new)
3615 snd_hda_codec_write(codec, pin_nid, 0,
3616 ATI_VERB_SET_HBR_CONTROL,
3617 hbr_ctl_new);
3618
3619 } else if (hbr)
3620 return -EINVAL;
3621
3622 return 0;
3623}
3624
Anssi Hannula84d69e72013-10-24 21:10:38 +03003625static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3626 hda_nid_t pin_nid, u32 stream_tag, int format)
3627{
3628
3629 if (is_amdhdmi_rev3_or_later(codec)) {
3630 int ramp_rate = 180; /* default as per AMD spec */
3631 /* disable ramp-up/down for non-pcm as per AMD spec */
3632 if (format & AC_FMT_TYPE_NON_PCM)
3633 ramp_rate = 0;
3634
3635 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3636 }
3637
3638 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3639}
3640
3641
Anssi Hannula5a6135842013-10-24 21:10:35 +03003642static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003643{
3644 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003645 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003646
Anssi Hannula5a6135842013-10-24 21:10:35 +03003647 err = generic_hdmi_init(codec);
3648
3649 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003650 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003651
3652 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3653 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3654
3655 /* make sure downmix information in infoframe is zero */
3656 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3657
3658 /* enable channel-wise remap mode if supported */
3659 if (has_amd_full_remap_support(codec))
3660 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3661 ATI_VERB_SET_MULTICHANNEL_MODE,
3662 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003663 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003664
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003665 return 0;
3666}
3667
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003668static int patch_atihdmi(struct hda_codec *codec)
3669{
3670 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003671 struct hdmi_spec_per_cvt *per_cvt;
3672 int err, cvt_idx;
3673
3674 err = patch_generic_hdmi(codec);
3675
3676 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003677 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003678
3679 codec->patch_ops.init = atihdmi_init;
3680
Takashi Iwaid0b12522012-06-15 14:34:42 +02003681 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003682
Anssi Hannula89250f82013-10-24 21:10:36 +03003683 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003684 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003685 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003686 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003687
Takashi Iwai39669222016-05-11 14:56:12 +02003688 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3689 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3690
Anssi Hannula5a6135842013-10-24 21:10:35 +03003691 if (!has_amd_full_remap_support(codec)) {
3692 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303693 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03003694 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303695 spec->chmap.ops.cea_alloc_to_tlv_chmap =
3696 atihdmi_paired_cea_alloc_to_tlv_chmap;
3697 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003698 }
3699
3700 /* ATI/AMD converters do not advertise all of their capabilities */
3701 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3702 per_cvt = get_cvt(spec, cvt_idx);
3703 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3704 per_cvt->rates |= SUPPORTED_RATES;
3705 per_cvt->formats |= SUPPORTED_FORMATS;
3706 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3707 }
3708
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303709 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003710
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003711 return 0;
3712}
3713
Annie Liu3de5ff82012-06-08 19:18:42 +08003714/* VIA HDMI Implementation */
3715#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3716#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3717
Annie Liu3de5ff82012-06-08 19:18:42 +08003718static int patch_via_hdmi(struct hda_codec *codec)
3719{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003720 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003721}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003722
3723/*
3724 * patch entries
3725 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003726static const struct hda_device_id snd_hda_id_hdmi[] = {
3727HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3728HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3729HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3730HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3731HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3732HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3733HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3734HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3735HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3736HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3737HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3738HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3739HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3740HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3741HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3742HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3743HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3744HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3745HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3746HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3747HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3748HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3749HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01003750/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003751HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3752HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3753HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3754HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3755HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3756HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3757HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3758HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3759HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3760HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3761HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3762HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3763HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3764HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3765HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3766HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3767HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3768HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3769HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3770HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3771HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Hui Wangaf677162017-02-09 09:20:54 +08003772HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi),
Aaron Plattner2d369c72016-03-13 13:58:57 -07003773HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08003774HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003775HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3776HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3777HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3778HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3779HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
Takashi Iwai7ff652f2016-03-21 14:50:24 +01003780HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003781HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3782HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3783HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
Takashi Iwai7ff652f2016-03-21 14:50:24 +01003784HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi),
Takashi Iwaie85015a32016-03-21 13:56:19 +01003785HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi),
3786HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01003787HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
3788HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
3789HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
3790HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
3791HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
Ander Conselvan De Oliveiraa87a4d22017-04-13 13:05:35 +05303792HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003793HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01003794HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
3795HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003796HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003797/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003798HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003799{} /* terminator */
3800};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003801MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003802
3803MODULE_LICENSE("GPL");
3804MODULE_DESCRIPTION("HDMI HD-audio codec");
3805MODULE_ALIAS("snd-hda-codec-intelhdmi");
3806MODULE_ALIAS("snd-hda-codec-nvhdmi");
3807MODULE_ALIAS("snd-hda-codec-atihdmi");
3808
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003809static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003810 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003811};
3812
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003813module_hda_codec_driver(hdmi_driver);