blob: c83c1a8d9742b8f2da261f416561111cb64bd662 [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 Yanga76056f2015-12-16 16:48:15 +080079 /* pin idx, different device entries on the same pin use the same idx */
80 int pin_nid_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -060081 int num_mux_nids;
82 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Mengdong Lin2df67422014-03-20 13:01:06 +080083 int mux_idx;
Anssi Hannula1df5a062013-10-05 02:25:40 +030084 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080085
86 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060087 struct hdmi_eld sink_eld;
Takashi Iwaia4e9a382013-10-17 18:21:12 +020088 struct mutex lock;
Wu Fengguang744626d2011-11-16 16:29:47 +080089 struct delayed_work work;
Libin Yang2bea2412016-01-12 11:13:26 +080090 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Libin Yanga76056f2015-12-16 16:48:15 +080091 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
Wu Fengguangc6e84532011-11-18 16:59:32 -060092 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020093 bool setup; /* the stream has been set up by prepare callback */
94 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020095 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020096 bool chmap_set; /* channel-map override by ALSA API? */
97 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080098#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +020099 struct snd_info_entry *proc_entry;
100#endif
Stephen Warren384a48d2011-06-01 11:14:21 -0600101};
102
Anssi Hannula307229d2013-10-24 21:10:34 +0300103/* operations used by generic code that can be overridden by patches */
104struct hdmi_ops {
105 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
106 unsigned char *buf, int *eld_size);
107
Anssi Hannula307229d2013-10-24 21:10:34 +0300108 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
109 int ca, int active_channels, int conn_type);
110
111 /* enable/disable HBR (HD passthrough) */
112 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
113
114 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
115 hda_nid_t pin_nid, u32 stream_tag, int format);
116
Anssi Hannula307229d2013-10-24 21:10:34 +0300117};
118
Libin Yang2bea2412016-01-12 11:13:26 +0800119struct hdmi_pcm {
120 struct hda_pcm *pcm;
121 struct snd_jack *jack;
Libin Yangfb087ea2016-02-23 16:33:37 +0800122 struct snd_kcontrol *eld_ctl;
Libin Yang2bea2412016-01-12 11:13:26 +0800123};
124
Wu Fengguang079d88c2010-03-08 10:44:23 +0800125struct hdmi_spec {
126 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100127 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
128 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600129
Wu Fengguang079d88c2010-03-08 10:44:23 +0800130 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100131 struct snd_array pins; /* struct hdmi_spec_per_pin */
Libin Yang2bea2412016-01-12 11:13:26 +0800132 struct hdmi_pcm pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800133 struct mutex pcm_lock;
Libin Yanga76056f2015-12-16 16:48:15 +0800134 /* pcm_bitmap means which pcms have been assigned to pins*/
135 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800136 int pcm_used; /* counter of pcm_rec[] */
Libin Yangac983792015-12-16 16:48:16 +0800137 /* bitmap shows whether the pcm is opened in user space
138 * bit 0 means the first playback PCM (PCM3);
139 * bit 1 means the second playback PCM, and so on.
140 */
141 unsigned long pcm_in_use;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800142
David Henningsson4bd038f2013-02-19 16:11:25 +0100143 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300144 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700145
146 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800147 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800148 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300149 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800150 */
151 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200152 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200153
154 /* i915/powerwell (Haswell+/Valleyview+) specific */
Takashi Iwai691be972016-03-18 15:10:08 +0100155 bool use_acomp_notifier; /* use i915 eld_notify callback for hotplug */
David Henningsson25adc132015-08-19 10:48:58 +0200156 struct i915_audio_component_audio_ops i915_audio_ops;
Takashi Iwai55913112015-12-10 13:03:29 +0100157 bool i915_bound; /* was i915 bound in this driver? */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +0530158
159 struct hdac_chmap chmap;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800160};
161
Takashi Iwaif4e30402015-12-10 13:01:28 +0100162#ifdef CONFIG_SND_HDA_I915
Takashi Iwai691be972016-03-18 15:10:08 +0100163static inline bool codec_has_acomp(struct hda_codec *codec)
164{
165 struct hdmi_spec *spec = codec->spec;
166 return spec->use_acomp_notifier;
167}
Takashi Iwaif4e30402015-12-10 13:01:28 +0100168#else
169#define codec_has_acomp(codec) false
170#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800171
172struct hdmi_audio_infoframe {
173 u8 type; /* 0x84 */
174 u8 ver; /* 0x01 */
175 u8 len; /* 0x0a */
176
Wu Fengguang53d7d692010-09-21 14:25:49 +0800177 u8 checksum;
178
Wu Fengguang079d88c2010-03-08 10:44:23 +0800179 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
180 u8 SS01_SF24;
181 u8 CXT04;
182 u8 CA;
183 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800184};
185
186struct dp_audio_infoframe {
187 u8 type; /* 0x84 */
188 u8 len; /* 0x1b */
189 u8 ver; /* 0x11 << 2 */
190
191 u8 CC02_CT47; /* match with HDMI infoframe from this on */
192 u8 SS01_SF24;
193 u8 CXT04;
194 u8 CA;
195 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800196};
197
Takashi Iwai2b203db2011-02-11 12:17:30 +0100198union audio_infoframe {
199 struct hdmi_audio_infoframe hdmi;
200 struct dp_audio_infoframe dp;
201 u8 bytes[0];
202};
203
Wu Fengguang079d88c2010-03-08 10:44:23 +0800204/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800205 * HDMI routines
206 */
207
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100208#define get_pin(spec, idx) \
209 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
210#define get_cvt(spec, idx) \
211 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Libin Yang2bea2412016-01-12 11:13:26 +0800212/* obtain hdmi_pcm object assigned to idx */
213#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
214/* obtain hda_pcm object assigned to idx */
215#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100216
Takashi Iwai4e76a882014-02-25 12:21:03 +0100217static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800218{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100219 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600220 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800221
Stephen Warren384a48d2011-06-01 11:14:21 -0600222 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100223 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600224 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800225
Takashi Iwai4e76a882014-02-25 12:21:03 +0100226 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600227 return -EINVAL;
228}
229
Libin Yang2bf3c852015-12-16 13:42:43 +0800230static int hinfo_to_pcm_index(struct hda_codec *codec,
231 struct hda_pcm_stream *hinfo)
232{
233 struct hdmi_spec *spec = codec->spec;
234 int pcm_idx;
235
236 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
237 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
238 return pcm_idx;
239
240 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
241 return -EINVAL;
242}
243
Takashi Iwai4e76a882014-02-25 12:21:03 +0100244static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600245 struct hda_pcm_stream *hinfo)
246{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100247 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800248 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600249 int pin_idx;
250
Libin Yang6590faa2015-12-16 13:42:41 +0800251 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
252 per_pin = get_pin(spec, pin_idx);
Libin Yang2bea2412016-01-12 11:13:26 +0800253 if (per_pin->pcm &&
254 per_pin->pcm->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600255 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800256 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600257
Libin Yang6590faa2015-12-16 13:42:41 +0800258 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600259 return -EINVAL;
260}
261
Libin Yang022f3442016-02-03 10:48:34 +0800262static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
263 int pcm_idx)
264{
265 int i;
266 struct hdmi_spec_per_pin *per_pin;
267
268 for (i = 0; i < spec->num_pins; i++) {
269 per_pin = get_pin(spec, i);
270 if (per_pin->pcm_idx == pcm_idx)
271 return per_pin;
272 }
273 return NULL;
274}
275
Takashi Iwai4e76a882014-02-25 12:21:03 +0100276static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600277{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100278 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600279 int cvt_idx;
280
281 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100282 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600283 return cvt_idx;
284
Takashi Iwai4e76a882014-02-25 12:21:03 +0100285 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800286 return -EINVAL;
287}
288
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500289static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
290 struct snd_ctl_elem_info *uinfo)
291{
292 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100293 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200294 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100295 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800296 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500297
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500298 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
299
Libin Yangfb087ea2016-02-23 16:33:37 +0800300 pcm_idx = kcontrol->private_value;
301 mutex_lock(&spec->pcm_lock);
302 per_pin = pcm_idx_to_pin(spec, pcm_idx);
303 if (!per_pin) {
304 /* no pin is bound to the pcm */
305 uinfo->count = 0;
306 mutex_unlock(&spec->pcm_lock);
307 return 0;
308 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200309 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100310 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Libin Yangfb087ea2016-02-23 16:33:37 +0800311 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500312
313 return 0;
314}
315
316static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
317 struct snd_ctl_elem_value *ucontrol)
318{
319 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100320 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200321 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100322 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800323 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500324
Libin Yangfb087ea2016-02-23 16:33:37 +0800325 pcm_idx = kcontrol->private_value;
326 mutex_lock(&spec->pcm_lock);
327 per_pin = pcm_idx_to_pin(spec, pcm_idx);
328 if (!per_pin) {
329 /* no pin is bound to the pcm */
330 memset(ucontrol->value.bytes.data, 0,
331 ARRAY_SIZE(ucontrol->value.bytes.data));
332 mutex_unlock(&spec->pcm_lock);
333 return 0;
334 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200335 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500336
David Henningsson360a8242016-02-05 09:05:41 +0100337 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
338 eld->eld_size > ELD_MAX_SIZE) {
Libin Yangfb087ea2016-02-23 16:33:37 +0800339 mutex_unlock(&spec->pcm_lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100340 snd_BUG();
341 return -EINVAL;
342 }
343
344 memset(ucontrol->value.bytes.data, 0,
345 ARRAY_SIZE(ucontrol->value.bytes.data));
346 if (eld->eld_valid)
347 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
348 eld->eld_size);
Libin Yangfb087ea2016-02-23 16:33:37 +0800349 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500350
351 return 0;
352}
353
354static struct snd_kcontrol_new eld_bytes_ctl = {
355 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
356 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
357 .name = "ELD",
358 .info = hdmi_eld_ctl_info,
359 .get = hdmi_eld_ctl_get,
360};
361
Libin Yangfb087ea2016-02-23 16:33:37 +0800362static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500363 int device)
364{
365 struct snd_kcontrol *kctl;
366 struct hdmi_spec *spec = codec->spec;
367 int err;
368
369 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
370 if (!kctl)
371 return -ENOMEM;
Libin Yangfb087ea2016-02-23 16:33:37 +0800372 kctl->private_value = pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500373 kctl->id.device = device;
374
Libin Yangfb087ea2016-02-23 16:33:37 +0800375 /* no pin nid is associated with the kctl now
376 * tbd: associate pin nid to eld ctl later
377 */
378 err = snd_hda_ctl_add(codec, 0, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500379 if (err < 0)
380 return err;
381
Libin Yangfb087ea2016-02-23 16:33:37 +0800382 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500383 return 0;
384}
385
Wu Fengguang079d88c2010-03-08 10:44:23 +0800386#ifdef BE_PARANOID
387static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
388 int *packet_index, int *byte_index)
389{
390 int val;
391
392 val = snd_hda_codec_read(codec, pin_nid, 0,
393 AC_VERB_GET_HDMI_DIP_INDEX, 0);
394
395 *packet_index = val >> 5;
396 *byte_index = val & 0x1f;
397}
398#endif
399
400static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
401 int packet_index, int byte_index)
402{
403 int val;
404
405 val = (packet_index << 5) | (byte_index & 0x1f);
406
407 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
408}
409
410static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
411 unsigned char val)
412{
413 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
414}
415
Stephen Warren384a48d2011-06-01 11:14:21 -0600416static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800417{
Stephen Warren75fae112014-01-30 11:52:16 -0700418 struct hdmi_spec *spec = codec->spec;
419 int pin_out;
420
Wu Fengguang079d88c2010-03-08 10:44:23 +0800421 /* Unmute */
422 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
423 snd_hda_codec_write(codec, pin_nid, 0,
424 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700425
426 if (spec->dyn_pin_out)
427 /* Disable pin out until stream is active */
428 pin_out = 0;
429 else
430 /* Enable pin out: some machines with GM965 gets broken output
431 * when the pin is disabled or changed while using with HDMI
432 */
433 pin_out = PIN_OUT;
434
Wu Fengguang079d88c2010-03-08 10:44:23 +0800435 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700436 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800437}
438
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200439/*
440 * ELD proc files
441 */
442
Jie Yangcd6a6502015-05-27 19:45:45 +0800443#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200444static void print_eld_info(struct snd_info_entry *entry,
445 struct snd_info_buffer *buffer)
446{
447 struct hdmi_spec_per_pin *per_pin = entry->private_data;
448
449 mutex_lock(&per_pin->lock);
450 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
451 mutex_unlock(&per_pin->lock);
452}
453
454static void write_eld_info(struct snd_info_entry *entry,
455 struct snd_info_buffer *buffer)
456{
457 struct hdmi_spec_per_pin *per_pin = entry->private_data;
458
459 mutex_lock(&per_pin->lock);
460 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
461 mutex_unlock(&per_pin->lock);
462}
463
464static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
465{
466 char name[32];
467 struct hda_codec *codec = per_pin->codec;
468 struct snd_info_entry *entry;
469 int err;
470
471 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100472 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200473 if (err < 0)
474 return err;
475
476 snd_info_set_text_ops(entry, per_pin, print_eld_info);
477 entry->c.text.write = write_eld_info;
478 entry->mode |= S_IWUSR;
479 per_pin->proc_entry = entry;
480
481 return 0;
482}
483
484static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
485{
Markus Elfring1947a112015-06-28 11:15:28 +0200486 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200487 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200488 per_pin->proc_entry = NULL;
489 }
490}
491#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200492static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
493 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200494{
495 return 0;
496}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200497static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200498{
499}
500#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800501
502/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800503 * Audio InfoFrame routines
504 */
505
506/*
507 * Enable Audio InfoFrame Transmission
508 */
509static void hdmi_start_infoframe_trans(struct hda_codec *codec,
510 hda_nid_t pin_nid)
511{
512 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
513 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
514 AC_DIPXMIT_BEST);
515}
516
517/*
518 * Disable Audio InfoFrame Transmission
519 */
520static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
521 hda_nid_t pin_nid)
522{
523 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
524 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
525 AC_DIPXMIT_DISABLE);
526}
527
528static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
529{
530#ifdef CONFIG_SND_DEBUG_VERBOSE
531 int i;
532 int size;
533
534 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100535 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800536
537 for (i = 0; i < 8; i++) {
538 size = snd_hda_codec_read(codec, pin_nid, 0,
539 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100540 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800541 }
542#endif
543}
544
545static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
546{
547#ifdef BE_PARANOID
548 int i, j;
549 int size;
550 int pi, bi;
551 for (i = 0; i < 8; i++) {
552 size = snd_hda_codec_read(codec, pin_nid, 0,
553 AC_VERB_GET_HDMI_DIP_SIZE, i);
554 if (size == 0)
555 continue;
556
557 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
558 for (j = 1; j < 1000; j++) {
559 hdmi_write_dip_byte(codec, pin_nid, 0x0);
560 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
561 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100562 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +0800563 bi, pi, i);
564 if (bi == 0) /* byte index wrapped around */
565 break;
566 }
Takashi Iwai4e76a882014-02-25 12:21:03 +0100567 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800568 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
569 i, size, j);
570 }
571#endif
572}
573
Wu Fengguang53d7d692010-09-21 14:25:49 +0800574static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800575{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800576 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800577 u8 sum = 0;
578 int i;
579
Wu Fengguang53d7d692010-09-21 14:25:49 +0800580 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800581
Wu Fengguang53d7d692010-09-21 14:25:49 +0800582 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800583 sum += bytes[i];
584
Wu Fengguang53d7d692010-09-21 14:25:49 +0800585 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800586}
587
588static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
589 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800590 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800591{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800592 int i;
593
594 hdmi_debug_dip_size(codec, pin_nid);
595 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
596
Wu Fengguang079d88c2010-03-08 10:44:23 +0800597 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800598 for (i = 0; i < size; i++)
599 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800600}
601
602static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800603 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800604{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800605 u8 val;
606 int i;
607
608 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
609 != AC_DIPXMIT_BEST)
610 return false;
611
612 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800613 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800614 val = snd_hda_codec_read(codec, pin_nid, 0,
615 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800616 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800617 return false;
618 }
619
620 return true;
621}
622
Anssi Hannula307229d2013-10-24 21:10:34 +0300623static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
624 hda_nid_t pin_nid,
625 int ca, int active_channels,
626 int conn_type)
627{
628 union audio_infoframe ai;
629
Mengdong Lincaaf5ef2014-03-11 17:12:52 -0400630 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +0300631 if (conn_type == 0) { /* HDMI */
632 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
633
634 hdmi_ai->type = 0x84;
635 hdmi_ai->ver = 0x01;
636 hdmi_ai->len = 0x0a;
637 hdmi_ai->CC02_CT47 = active_channels - 1;
638 hdmi_ai->CA = ca;
639 hdmi_checksum_audio_infoframe(hdmi_ai);
640 } else if (conn_type == 1) { /* DisplayPort */
641 struct dp_audio_infoframe *dp_ai = &ai.dp;
642
643 dp_ai->type = 0x84;
644 dp_ai->len = 0x1b;
645 dp_ai->ver = 0x11 << 2;
646 dp_ai->CC02_CT47 = active_channels - 1;
647 dp_ai->CA = ca;
648 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100649 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300650 pin_nid);
651 return;
652 }
653
654 /*
655 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
656 * sizeof(*dp_ai) to avoid partial match/update problems when
657 * the user switches between HDMI/DP monitors.
658 */
659 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
660 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100661 codec_dbg(codec,
662 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300663 pin_nid,
664 active_channels, ca);
665 hdmi_stop_infoframe_trans(codec, pin_nid);
666 hdmi_fill_audio_infoframe(codec, pin_nid,
667 ai.bytes, sizeof(ai));
668 hdmi_start_infoframe_trans(codec, pin_nid);
669 }
670}
671
Takashi Iwaib0540872013-09-02 12:33:02 +0200672static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
673 struct hdmi_spec_per_pin *per_pin,
674 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800675{
Anssi Hannula307229d2013-10-24 21:10:34 +0300676 struct hdmi_spec *spec = codec->spec;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530677 struct hdac_chmap *chmap = &spec->chmap;
Stephen Warren384a48d2011-06-01 11:14:21 -0600678 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200679 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300680 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600681 struct hdmi_eld *eld;
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530682 int ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800683
Takashi Iwaib0540872013-09-02 12:33:02 +0200684 if (!channels)
685 return;
686
Mengdong Lin75dcbe42014-01-08 15:55:32 -0500687 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -0400688 snd_hda_codec_write(codec, pin_nid, 0,
689 AC_VERB_SET_AMP_GAIN_MUTE,
690 AMP_OUT_UNMUTE);
691
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100692 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800693
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530694 ca = snd_hdac_channel_allocation(&codec->core,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530695 eld->info.spk_alloc, channels,
696 per_pin->chmap_set, non_pcm, per_pin->chmap);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800697
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530698 active_channels = snd_hdac_get_active_channels(ca);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300699
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530700 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
701 active_channels);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300702
Stephen Warren384a48d2011-06-01 11:14:21 -0600703 /*
Anssi Hannula39edac72013-10-07 19:24:52 +0300704 * always configure channel mapping, it may have been changed by the
705 * user in the meantime
706 */
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530707 snd_hdac_setup_channel_mapping(&spec->chmap,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530708 pin_nid, non_pcm, ca, channels,
709 per_pin->chmap, per_pin->chmap_set);
Anssi Hannula39edac72013-10-07 19:24:52 +0300710
Anssi Hannula307229d2013-10-24 21:10:34 +0300711 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
712 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +0800713
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200714 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800715}
716
Wu Fengguang079d88c2010-03-08 10:44:23 +0800717/*
718 * Unsolicited events
719 */
720
Takashi Iwaiefe47102013-11-07 13:38:23 +0100721static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200722
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200723static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800724{
725 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200726 int pin_idx = pin_nid_to_pin_index(codec, nid);
727
David Henningsson20ce9022013-12-04 10:19:41 +0800728 if (pin_idx < 0)
729 return;
David Henningsson20ce9022013-12-04 10:19:41 +0800730 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
731 snd_hda_jack_report_sync(codec);
732}
733
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200734static void jack_callback(struct hda_codec *codec,
735 struct hda_jack_callback *jack)
736{
Takashi Iwai2ebab402016-02-09 10:23:52 +0100737 check_presence_and_report(codec, jack->nid);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200738}
739
David Henningsson20ce9022013-12-04 10:19:41 +0800740static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
741{
Takashi Iwai3a938972011-10-28 01:16:55 +0200742 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200743 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400744 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200745
746 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
747 if (!jack)
748 return;
Takashi Iwai3a938972011-10-28 01:16:55 +0200749 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800750
Takashi Iwai4e76a882014-02-25 12:21:03 +0100751 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400752 "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 +0800753 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800754 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800755
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200756 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800757}
758
759static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
760{
761 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
762 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
763 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
764 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
765
Takashi Iwai4e76a882014-02-25 12:21:03 +0100766 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200767 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600768 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800769 tag,
770 subtag,
771 cp_state,
772 cp_ready);
773
774 /* TODO */
775 if (cp_state)
776 ;
777 if (cp_ready)
778 ;
779}
780
781
782static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
783{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800784 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
785 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
786
Takashi Iwai3a938972011-10-28 01:16:55 +0200787 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100788 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800789 return;
790 }
791
792 if (subtag == 0)
793 hdmi_intrinsic_event(codec, res);
794 else
795 hdmi_non_intrinsic_event(codec, res);
796}
797
Mengdong Lin58f7d282013-09-04 16:37:12 -0400798static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +0800799 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +0200800{
Mengdong Lin58f7d282013-09-04 16:37:12 -0400801 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +0200802
Wang Xingchao53b434f2013-06-18 10:41:53 +0800803 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
804 * thus pins could only choose converter 0 for use. Make sure the
805 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200806 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +0800807 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
808
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200809 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +0200810 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
811 AC_PWRST_D0);
812 msleep(40);
813 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
814 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100815 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 +0200816 }
David Henningsson83f26ad2013-04-10 12:26:07 +0200817}
818
Wu Fengguang079d88c2010-03-08 10:44:23 +0800819/*
820 * Callbacks
821 */
822
Takashi Iwai92f10b32010-08-03 14:21:00 +0200823/* HBR should be Non-PCM, 8 channels */
824#define is_hbr_format(format) \
825 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
826
Anssi Hannula307229d2013-10-24 21:10:34 +0300827static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
828 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800829{
Anssi Hannula307229d2013-10-24 21:10:34 +0300830 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +0200831
Stephen Warren384a48d2011-06-01 11:14:21 -0600832 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
833 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300834 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
835
Anssi Hannula13122e62013-11-10 20:56:10 +0200836 if (pinctl < 0)
837 return hbr ? -EINVAL : 0;
838
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300839 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +0300840 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300841 new_pinctl |= AC_PINCTL_EPT_HBR;
842 else
843 new_pinctl |= AC_PINCTL_EPT_NATIVE;
844
Takashi Iwai4e76a882014-02-25 12:21:03 +0100845 codec_dbg(codec,
846 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600847 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300848 pinctl == new_pinctl ? "" : "new-",
849 new_pinctl);
850
851 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -0600852 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300853 AC_VERB_SET_PIN_WIDGET_CONTROL,
854 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +0300855 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300856 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +0300857
858 return 0;
859}
860
861static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
862 hda_nid_t pin_nid, u32 stream_tag, int format)
863{
864 struct hdmi_spec *spec = codec->spec;
865 int err;
866
Mengdong Lin75dcbe42014-01-08 15:55:32 -0500867 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +0300868 haswell_verify_D0(codec, cvt_nid, pin_nid);
869
870 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
871
872 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100873 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +0300874 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300875 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800876
Stephen Warren384a48d2011-06-01 11:14:21 -0600877 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300878 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800879}
880
Libin Yang42b29872015-12-16 13:42:42 +0800881/* Try to find an available converter
882 * If pin_idx is less then zero, just try to find an available converter.
883 * Otherwise, try to find an available converter and get the cvt mux index
884 * of the pin.
885 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800886static int hdmi_choose_cvt(struct hda_codec *codec,
887 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200888{
889 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600890 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600891 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800892 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200893
Libin Yang42b29872015-12-16 13:42:42 +0800894 /* pin_idx < 0 means no pin will be bound to the converter */
895 if (pin_idx < 0)
896 per_pin = NULL;
897 else
898 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200899
Stephen Warren384a48d2011-06-01 11:14:21 -0600900 /* Dynamically assign converter to stream */
901 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100902 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -0600903
904 /* Must not already be assigned */
905 if (per_cvt->assigned)
906 continue;
Libin Yang42b29872015-12-16 13:42:42 +0800907 if (per_pin == NULL)
908 break;
Stephen Warren384a48d2011-06-01 11:14:21 -0600909 /* Must be in pin's mux's list of converters */
910 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
911 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
912 break;
913 /* Not in mux list */
914 if (mux_idx == per_pin->num_mux_nids)
915 continue;
916 break;
917 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800918
Stephen Warren384a48d2011-06-01 11:14:21 -0600919 /* No free converters */
920 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +0800921 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -0600922
Libin Yang42b29872015-12-16 13:42:42 +0800923 if (per_pin != NULL)
924 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +0800925
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800926 if (cvt_id)
927 *cvt_id = cvt_idx;
928 if (mux_id)
929 *mux_id = mux_idx;
930
931 return 0;
932}
933
Mengdong Lin2df67422014-03-20 13:01:06 +0800934/* Assure the pin select the right convetor */
935static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
936 struct hdmi_spec_per_pin *per_pin)
937{
938 hda_nid_t pin_nid = per_pin->pin_nid;
939 int mux_idx, curr;
940
941 mux_idx = per_pin->mux_idx;
942 curr = snd_hda_codec_read(codec, pin_nid, 0,
943 AC_VERB_GET_CONNECT_SEL, 0);
944 if (curr != mux_idx)
945 snd_hda_codec_write_cache(codec, pin_nid, 0,
946 AC_VERB_SET_CONNECT_SEL,
947 mux_idx);
948}
949
Libin Yang42b29872015-12-16 13:42:42 +0800950/* get the mux index for the converter of the pins
951 * converter's mux index is the same for all pins on Intel platform
952 */
953static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
954 hda_nid_t cvt_nid)
955{
956 int i;
957
958 for (i = 0; i < spec->num_cvts; i++)
959 if (spec->cvt_nids[i] == cvt_nid)
960 return i;
961 return -EINVAL;
962}
963
Mengdong Lin300016b2013-11-04 01:13:13 -0500964/* Intel HDMI workaround to fix audio routing issue:
965 * For some Intel display codecs, pins share the same connection list.
966 * So a conveter can be selected by multiple pins and playback on any of these
967 * pins will generate sound on the external display, because audio flows from
968 * the same converter to the display pipeline. Also muting one pin may make
969 * other pins have no sound output.
970 * So this function assures that an assigned converter for a pin is not selected
971 * by any other pins.
972 */
973static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -0400974 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800975{
976 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +0100977 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -0400978 int cvt_idx, curr;
979 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800980
Mengdong Linf82d7d12013-09-21 20:34:45 -0400981 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +0100982 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -0400983 unsigned int wid_caps = get_wcaps(codec, nid);
984 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800985
Mengdong Linf82d7d12013-09-21 20:34:45 -0400986 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800987 continue;
988
Mengdong Linf82d7d12013-09-21 20:34:45 -0400989 if (nid == pin_nid)
990 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800991
Mengdong Linf82d7d12013-09-21 20:34:45 -0400992 curr = snd_hda_codec_read(codec, nid, 0,
993 AC_VERB_GET_CONNECT_SEL, 0);
994 if (curr != mux_idx)
995 continue;
996
997 /* choose an unassigned converter. The conveters in the
998 * connection list are in the same order as in the codec.
999 */
1000 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1001 per_cvt = get_cvt(spec, cvt_idx);
1002 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001003 codec_dbg(codec,
1004 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001005 cvt_idx, nid);
1006 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001007 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001008 cvt_idx);
1009 break;
1010 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001011 }
1012 }
1013}
1014
Libin Yang42b29872015-12-16 13:42:42 +08001015/* A wrapper of intel_not_share_asigned_cvt() */
1016static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1017 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1018{
1019 int mux_idx;
1020 struct hdmi_spec *spec = codec->spec;
1021
1022 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1023 return;
1024
1025 /* On Intel platform, the mapping of converter nid to
1026 * mux index of the pins are always the same.
1027 * The pin nid may be 0, this means all pins will not
1028 * share the converter.
1029 */
1030 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1031 if (mux_idx >= 0)
1032 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1033}
1034
1035/* called in hdmi_pcm_open when no pin is assigned to the PCM
1036 * in dyn_pcm_assign mode.
1037 */
1038static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1039 struct hda_codec *codec,
1040 struct snd_pcm_substream *substream)
1041{
1042 struct hdmi_spec *spec = codec->spec;
1043 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001044 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001045 struct hdmi_spec_per_cvt *per_cvt = NULL;
1046 int err;
1047
Libin Yangac983792015-12-16 16:48:16 +08001048 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1049 if (pcm_idx < 0)
1050 return -EINVAL;
1051
Libin Yang42b29872015-12-16 13:42:42 +08001052 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1053 if (err)
1054 return err;
1055
1056 per_cvt = get_cvt(spec, cvt_idx);
1057 per_cvt->assigned = 1;
1058 hinfo->nid = per_cvt->cvt_nid;
1059
1060 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1061
Libin Yangac983792015-12-16 16:48:16 +08001062 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001063 /* todo: setup spdif ctls assign */
1064
1065 /* Initially set the converter's capabilities */
1066 hinfo->channels_min = per_cvt->channels_min;
1067 hinfo->channels_max = per_cvt->channels_max;
1068 hinfo->rates = per_cvt->rates;
1069 hinfo->formats = per_cvt->formats;
1070 hinfo->maxbps = per_cvt->maxbps;
1071
1072 /* Store the updated parameters */
1073 runtime->hw.channels_min = hinfo->channels_min;
1074 runtime->hw.channels_max = hinfo->channels_max;
1075 runtime->hw.formats = hinfo->formats;
1076 runtime->hw.rates = hinfo->rates;
1077
1078 snd_pcm_hw_constraint_step(substream->runtime, 0,
1079 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1080 return 0;
1081}
1082
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001083/*
1084 * HDA PCM callbacks
1085 */
1086static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1087 struct hda_codec *codec,
1088 struct snd_pcm_substream *substream)
1089{
1090 struct hdmi_spec *spec = codec->spec;
1091 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001092 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001093 struct hdmi_spec_per_pin *per_pin;
1094 struct hdmi_eld *eld;
1095 struct hdmi_spec_per_cvt *per_cvt = NULL;
1096 int err;
1097
1098 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001099 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1100 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001101 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001102
Libin Yang42b29872015-12-16 13:42:42 +08001103 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001104 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001105 if (!spec->dyn_pcm_assign) {
1106 if (snd_BUG_ON(pin_idx < 0)) {
1107 mutex_unlock(&spec->pcm_lock);
1108 return -EINVAL;
1109 }
1110 } else {
1111 /* no pin is assigned to the PCM
1112 * PA need pcm open successfully when probe
1113 */
1114 if (pin_idx < 0) {
1115 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1116 mutex_unlock(&spec->pcm_lock);
1117 return err;
1118 }
1119 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001120
1121 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001122 if (err < 0) {
1123 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001124 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001125 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001126
1127 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001128 /* Claim converter */
1129 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001130
Libin Yangac983792015-12-16 16:48:16 +08001131 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001132 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001133 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001134 hinfo->nid = per_cvt->cvt_nid;
1135
Takashi Iwaibddee962013-06-18 16:14:22 +02001136 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001137 AC_VERB_SET_CONNECT_SEL,
1138 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001139
1140 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001141 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001142 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001143
Libin Yang2bf3c852015-12-16 13:42:43 +08001144 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001145
Stephen Warren2def8172011-06-01 11:14:20 -06001146 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001147 hinfo->channels_min = per_cvt->channels_min;
1148 hinfo->channels_max = per_cvt->channels_max;
1149 hinfo->rates = per_cvt->rates;
1150 hinfo->formats = per_cvt->formats;
1151 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001152
Libin Yang42b29872015-12-16 13:42:42 +08001153 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001154 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001155 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001156 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001157 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001158 !hinfo->rates || !hinfo->formats) {
1159 per_cvt->assigned = 0;
1160 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001161 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001162 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001163 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001164 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001165 }
Stephen Warren2def8172011-06-01 11:14:20 -06001166
Libin Yang42b29872015-12-16 13:42:42 +08001167 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001168 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001169 runtime->hw.channels_min = hinfo->channels_min;
1170 runtime->hw.channels_max = hinfo->channels_max;
1171 runtime->hw.formats = hinfo->formats;
1172 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001173
1174 snd_pcm_hw_constraint_step(substream->runtime, 0,
1175 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001176 return 0;
1177}
1178
1179/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001180 * HDA/HDMI auto parsing
1181 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001182static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001183{
1184 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001185 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001186 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001187
1188 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001189 codec_warn(codec,
1190 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001191 pin_nid, get_wcaps(codec, pin_nid));
1192 return -EINVAL;
1193 }
1194
Stephen Warren384a48d2011-06-01 11:14:21 -06001195 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1196 per_pin->mux_nids,
1197 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001198
1199 return 0;
1200}
1201
Libin Yanga76056f2015-12-16 16:48:15 +08001202static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1203 struct hdmi_spec_per_pin *per_pin)
1204{
1205 int i;
1206
1207 /* try the prefer PCM */
1208 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1209 return per_pin->pin_nid_idx;
1210
1211 /* have a second try; check the "reserved area" over num_pins */
1212 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1213 if (!test_bit(i, &spec->pcm_bitmap))
1214 return i;
1215 }
1216
1217 /* the last try; check the empty slots in pins */
1218 for (i = 0; i < spec->num_pins; i++) {
1219 if (!test_bit(i, &spec->pcm_bitmap))
1220 return i;
1221 }
1222 return -EBUSY;
1223}
1224
1225static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1226 struct hdmi_spec_per_pin *per_pin)
1227{
1228 int idx;
1229
1230 /* pcm already be attached to the pin */
1231 if (per_pin->pcm)
1232 return;
1233 idx = hdmi_find_pcm_slot(spec, per_pin);
Libin Yangd10a80d2016-03-01 15:18:26 +08001234 if (idx == -EBUSY)
Libin Yanga76056f2015-12-16 16:48:15 +08001235 return;
1236 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001237 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001238 set_bit(idx, &spec->pcm_bitmap);
1239}
1240
1241static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1242 struct hdmi_spec_per_pin *per_pin)
1243{
1244 int idx;
1245
1246 /* pcm already be detached from the pin */
1247 if (!per_pin->pcm)
1248 return;
1249 idx = per_pin->pcm_idx;
1250 per_pin->pcm_idx = -1;
1251 per_pin->pcm = NULL;
1252 if (idx >= 0 && idx < spec->pcm_used)
1253 clear_bit(idx, &spec->pcm_bitmap);
1254}
1255
Libin Yangac983792015-12-16 16:48:16 +08001256static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1257 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1258{
1259 int mux_idx;
1260
1261 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1262 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1263 break;
1264 return mux_idx;
1265}
1266
1267static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1268
1269static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1270 struct hdmi_spec_per_pin *per_pin)
1271{
1272 struct hda_codec *codec = per_pin->codec;
1273 struct hda_pcm *pcm;
1274 struct hda_pcm_stream *hinfo;
1275 struct snd_pcm_substream *substream;
1276 int mux_idx;
1277 bool non_pcm;
1278
1279 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001280 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001281 else
1282 return;
1283 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1284 return;
1285
1286 /* hdmi audio only uses playback and one substream */
1287 hinfo = pcm->stream;
1288 substream = pcm->pcm->streams[0].substream;
1289
1290 per_pin->cvt_nid = hinfo->nid;
1291
1292 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1293 if (mux_idx < per_pin->num_mux_nids)
1294 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1295 AC_VERB_SET_CONNECT_SEL,
1296 mux_idx);
1297 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1298
1299 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1300 if (substream->runtime)
1301 per_pin->channels = substream->runtime->channels;
1302 per_pin->setup = true;
1303 per_pin->mux_idx = mux_idx;
1304
1305 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1306}
1307
1308static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1309 struct hdmi_spec_per_pin *per_pin)
1310{
1311 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1312 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1313
1314 per_pin->chmap_set = false;
1315 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1316
1317 per_pin->setup = false;
1318 per_pin->channels = 0;
1319}
1320
Takashi Iwaie90247f2015-11-13 09:12:12 +01001321/* update per_pin ELD from the given new ELD;
1322 * setup info frame and notification accordingly
1323 */
1324static void update_eld(struct hda_codec *codec,
1325 struct hdmi_spec_per_pin *per_pin,
1326 struct hdmi_eld *eld)
1327{
1328 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001329 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001330 bool old_eld_valid = pin_eld->eld_valid;
1331 bool eld_changed;
Libin Yangfb087ea2016-02-23 16:33:37 +08001332 int pcm_idx = -1;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001333
Libin Yangfb087ea2016-02-23 16:33:37 +08001334 /* for monitor disconnection, save pcm_idx firstly */
1335 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001336 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001337 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001338 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001339 hdmi_pcm_setup_pin(spec, per_pin);
1340 } else {
1341 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001342 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001343 }
Libin Yanga76056f2015-12-16 16:48:15 +08001344 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001345 /* if pcm_idx == -1, it means this is in monitor connection event
1346 * we can get the correct pcm_idx now.
1347 */
1348 if (pcm_idx == -1)
1349 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001350
Takashi Iwaie90247f2015-11-13 09:12:12 +01001351 if (eld->eld_valid)
1352 snd_hdmi_show_eld(codec, &eld->info);
1353
1354 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1355 if (eld->eld_valid && pin_eld->eld_valid)
1356 if (pin_eld->eld_size != eld->eld_size ||
1357 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1358 eld->eld_size) != 0)
1359 eld_changed = true;
1360
Takashi Iwaibd481282016-03-18 18:01:53 +01001361 pin_eld->monitor_present = eld->monitor_present;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001362 pin_eld->eld_valid = eld->eld_valid;
1363 pin_eld->eld_size = eld->eld_size;
1364 if (eld->eld_valid)
1365 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1366 pin_eld->info = eld->info;
1367
1368 /*
1369 * Re-setup pin and infoframe. This is needed e.g. when
1370 * - sink is first plugged-in
1371 * - transcoder can change during stream playback on Haswell
1372 * and this can make HW reset converter selection on a pin.
1373 */
1374 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1375 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1376 intel_verify_pin_cvt_connect(codec, per_pin);
1377 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1378 per_pin->mux_idx);
1379 }
1380
1381 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1382 }
1383
Libin Yangfb087ea2016-02-23 16:33:37 +08001384 if (eld_changed && pcm_idx >= 0)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001385 snd_ctl_notify(codec->card,
1386 SNDRV_CTL_EVENT_MASK_VALUE |
1387 SNDRV_CTL_EVENT_MASK_INFO,
Libin Yangfb087ea2016-02-23 16:33:37 +08001388 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001389}
1390
Takashi Iwai788d4412015-11-12 15:36:13 +01001391/* update ELD and jack state via HD-audio verbs */
1392static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1393 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001394{
David Henningsson464837a2013-11-07 13:38:25 +01001395 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001396 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001397 struct hdmi_spec *spec = codec->spec;
1398 struct hdmi_eld *eld = &spec->temp_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001399 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001400 /*
1401 * Always execute a GetPinSense verb here, even when called from
1402 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1403 * response's PD bit is not the real PD value, but indicates that
1404 * the real PD value changed. An older version of the HD-audio
1405 * specification worked this way. Hence, we just ignore the data in
1406 * the unsolicited response to avoid custom WARs.
1407 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001408 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001409 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001410 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001411
David Henningssonda4a7a32013-12-18 10:46:04 +01001412 present = snd_hda_pin_sense(codec, pin_nid);
1413
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001414 mutex_lock(&per_pin->lock);
Takashi Iwaic44da622016-04-13 09:45:53 +02001415 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1416 if (eld->monitor_present)
David Henningsson4bd038f2013-02-19 16:11:25 +01001417 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1418 else
1419 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001420
Takashi Iwai4e76a882014-02-25 12:21:03 +01001421 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001422 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Takashi Iwaic44da622016-04-13 09:45:53 +02001423 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001424
David Henningsson4bd038f2013-02-19 16:11:25 +01001425 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001426 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001427 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001428 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001429 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001430 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001431 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001432 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001433 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001434 if (!eld->eld_valid && repoll)
1435 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001436 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001437
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001438 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001439 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1440 else
1441 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001442
Takashi Iwaic44da622016-04-13 09:45:53 +02001443 ret = !repoll || !eld->monitor_present || eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001444
1445 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1446 if (jack)
1447 jack->block_report = !ret;
1448
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001449 mutex_unlock(&per_pin->lock);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001450 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001451}
1452
Libin Yang31842702016-02-19 15:42:06 +08001453static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1454 struct hdmi_spec_per_pin *per_pin)
1455{
1456 struct hdmi_spec *spec = codec->spec;
1457 struct snd_jack *jack = NULL;
1458 struct hda_jack_tbl *jack_tbl;
1459
1460 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1461 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1462 * NULL even after snd_hda_jack_tbl_clear() is called to
1463 * free snd_jack. This may cause access invalid memory
1464 * when calling snd_jack_report
1465 */
1466 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1467 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1468 else if (!spec->dyn_pcm_assign) {
1469 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1470 if (jack_tbl)
1471 jack = jack_tbl->jack;
1472 }
1473 return jack;
1474}
1475
Takashi Iwai788d4412015-11-12 15:36:13 +01001476/* update ELD and jack state via audio component */
1477static void sync_eld_via_acomp(struct hda_codec *codec,
1478 struct hdmi_spec_per_pin *per_pin)
1479{
Takashi Iwai788d4412015-11-12 15:36:13 +01001480 struct hdmi_spec *spec = codec->spec;
1481 struct hdmi_eld *eld = &spec->temp_eld;
Libin Yang25e4abb2016-01-12 11:13:27 +08001482 struct snd_jack *jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001483 int size;
1484
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001485 mutex_lock(&per_pin->lock);
Takashi Iwaic64c1432016-03-21 16:07:30 +01001486 eld->monitor_present = false;
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001487 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1488 &eld->monitor_present, eld->eld_buffer,
1489 ELD_MAX_SIZE);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001490 if (size > 0) {
1491 size = min(size, ELD_MAX_SIZE);
1492 if (snd_hdmi_parse_eld(codec, &eld->info,
1493 eld->eld_buffer, size) < 0)
1494 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001495 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001496
1497 if (size > 0) {
1498 eld->eld_valid = true;
1499 eld->eld_size = size;
1500 } else {
1501 eld->eld_valid = false;
1502 eld->eld_size = 0;
1503 }
1504
Libin Yang25e4abb2016-01-12 11:13:27 +08001505 /* pcm_idx >=0 before update_eld() means it is in monitor
1506 * disconnected event. Jack must be fetched before update_eld()
1507 */
Libin Yang31842702016-02-19 15:42:06 +08001508 jack = pin_idx_to_jack(codec, per_pin);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001509 update_eld(codec, per_pin, eld);
Libin Yang31842702016-02-19 15:42:06 +08001510 if (jack == NULL)
1511 jack = pin_idx_to_jack(codec, per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08001512 if (jack == NULL)
1513 goto unlock;
1514 snd_jack_report(jack,
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001515 eld->monitor_present ? SND_JACK_AVOUT : 0);
1516 unlock:
1517 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001518}
1519
1520static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1521{
1522 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08001523 struct hdmi_spec *spec = codec->spec;
1524 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001525
Takashi Iwai222bde02016-03-17 14:48:13 +01001526 /* no temporary power up/down needed for component notifier */
1527 if (!codec_has_acomp(codec))
1528 snd_hda_power_up_pm(codec);
1529
Libin Yanga76056f2015-12-16 16:48:15 +08001530 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001531 if (codec_has_acomp(codec)) {
1532 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001533 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01001534 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08001535 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01001536 }
Libin Yanga76056f2015-12-16 16:48:15 +08001537 mutex_unlock(&spec->pcm_lock);
1538
Takashi Iwai222bde02016-03-17 14:48:13 +01001539 if (!codec_has_acomp(codec))
1540 snd_hda_power_down_pm(codec);
1541
Libin Yanga76056f2015-12-16 16:48:15 +08001542 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001543}
1544
Wu Fengguang744626d2011-11-16 16:29:47 +08001545static void hdmi_repoll_eld(struct work_struct *work)
1546{
1547 struct hdmi_spec_per_pin *per_pin =
1548 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1549
Wu Fengguangc6e84532011-11-18 16:59:32 -06001550 if (per_pin->repoll_count++ > 6)
1551 per_pin->repoll_count = 0;
1552
Takashi Iwaiefe47102013-11-07 13:38:23 +01001553 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1554 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08001555}
1556
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001557static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1558 hda_nid_t nid);
1559
Wu Fengguang079d88c2010-03-08 10:44:23 +08001560static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1561{
1562 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001563 unsigned int caps, config;
1564 int pin_idx;
1565 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001566 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001567
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001568 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001569 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1570 return 0;
1571
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001572 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001573 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1574 return 0;
1575
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001576 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001577 intel_haswell_fixup_connect_list(codec, pin_nid);
1578
Stephen Warren384a48d2011-06-01 11:14:21 -06001579 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001580 per_pin = snd_array_new(&spec->pins);
1581 if (!per_pin)
1582 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001583
1584 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001585 per_pin->non_pcm = false;
Libin Yanga76056f2015-12-16 16:48:15 +08001586 if (spec->dyn_pcm_assign)
1587 per_pin->pcm_idx = -1;
Libin Yang2bea2412016-01-12 11:13:26 +08001588 else {
1589 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001590 per_pin->pcm_idx = pin_idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001591 }
Libin Yanga76056f2015-12-16 16:48:15 +08001592 per_pin->pin_nid_idx = pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001593
Stephen Warren384a48d2011-06-01 11:14:21 -06001594 err = hdmi_read_pin_conn(codec, pin_idx);
1595 if (err < 0)
1596 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001597
Wu Fengguang079d88c2010-03-08 10:44:23 +08001598 spec->num_pins++;
1599
Stephen Warren384a48d2011-06-01 11:14:21 -06001600 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001601}
1602
Stephen Warren384a48d2011-06-01 11:14:21 -06001603static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001604{
1605 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001606 struct hdmi_spec_per_cvt *per_cvt;
1607 unsigned int chans;
1608 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001609
Stephen Warren384a48d2011-06-01 11:14:21 -06001610 chans = get_wcaps(codec, cvt_nid);
1611 chans = get_wcaps_channels(chans);
1612
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001613 per_cvt = snd_array_new(&spec->cvts);
1614 if (!per_cvt)
1615 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001616
1617 per_cvt->cvt_nid = cvt_nid;
1618 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001619 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001620 per_cvt->channels_max = chans;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05301621 if (chans > spec->chmap.channels_max)
1622 spec->chmap.channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001623 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001624
1625 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1626 &per_cvt->rates,
1627 &per_cvt->formats,
1628 &per_cvt->maxbps);
1629 if (err < 0)
1630 return err;
1631
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001632 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1633 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1634 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001635
1636 return 0;
1637}
1638
1639static int hdmi_parse_codec(struct hda_codec *codec)
1640{
1641 hda_nid_t nid;
1642 int i, nodes;
1643
Takashi Iwai7639a062015-03-03 10:07:24 +01001644 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001645 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001646 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08001647 return -EINVAL;
1648 }
1649
1650 for (i = 0; i < nodes; i++, nid++) {
1651 unsigned int caps;
1652 unsigned int type;
1653
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001654 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001655 type = get_wcaps_type(caps);
1656
1657 if (!(caps & AC_WCAP_DIGITAL))
1658 continue;
1659
1660 switch (type) {
1661 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001662 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001663 break;
1664 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001665 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001666 break;
1667 }
1668 }
1669
Wu Fengguang079d88c2010-03-08 10:44:23 +08001670 return 0;
1671}
1672
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001673/*
1674 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001675static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1676{
1677 struct hda_spdif_out *spdif;
1678 bool non_pcm;
1679
1680 mutex_lock(&codec->spdif_mutex);
1681 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1682 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1683 mutex_unlock(&codec->spdif_mutex);
1684 return non_pcm;
1685}
1686
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001687/*
1688 * HDMI callbacks
1689 */
1690
1691static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1692 struct hda_codec *codec,
1693 unsigned int stream_tag,
1694 unsigned int format,
1695 struct snd_pcm_substream *substream)
1696{
Stephen Warren384a48d2011-06-01 11:14:21 -06001697 hda_nid_t cvt_nid = hinfo->nid;
1698 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08001699 int pin_idx;
1700 struct hdmi_spec_per_pin *per_pin;
1701 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08001702 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001703 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07001704 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08001705 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001706
Libin Yang42b29872015-12-16 13:42:42 +08001707 mutex_lock(&spec->pcm_lock);
1708 pin_idx = hinfo_to_pin_index(codec, hinfo);
1709 if (spec->dyn_pcm_assign && pin_idx < 0) {
1710 /* when dyn_pcm_assign and pcm is not bound to a pin
1711 * skip pin setup and return 0 to make audio playback
1712 * be ongoing
1713 */
1714 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
1715 snd_hda_codec_setup_stream(codec, cvt_nid,
1716 stream_tag, 0, format);
1717 mutex_unlock(&spec->pcm_lock);
1718 return 0;
1719 }
1720
1721 if (snd_BUG_ON(pin_idx < 0)) {
1722 mutex_unlock(&spec->pcm_lock);
1723 return -EINVAL;
1724 }
1725 per_pin = get_pin(spec, pin_idx);
1726 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08001727 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08001728 /* Verify pin:cvt selections to avoid silent audio after S3.
1729 * After S3, the audio driver restores pin:cvt selections
1730 * but this can happen before gfx is ready and such selection
1731 * is overlooked by HW. Thus multiple pins can share a same
1732 * default convertor and mute control will affect each other,
1733 * which can cause a resumed audio playback become silent
1734 * after S3.
1735 */
1736 intel_verify_pin_cvt_connect(codec, per_pin);
1737 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
1738 }
1739
Libin Yangddd621f2015-09-02 14:11:40 +08001740 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1741 /* Todo: add DP1.2 MST audio support later */
Takashi Iwai93a9ff12016-03-18 19:45:13 +01001742 if (codec_has_acomp(codec))
1743 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08001744
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001745 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001746 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02001747 per_pin->channels = substream->runtime->channels;
1748 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001749
Takashi Iwaib0540872013-09-02 12:33:02 +02001750 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001751 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07001752 if (spec->dyn_pin_out) {
1753 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1754 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1755 snd_hda_codec_write(codec, pin_nid, 0,
1756 AC_VERB_SET_PIN_WIDGET_CONTROL,
1757 pinctl | PIN_OUT);
1758 }
1759
Libin Yang42b29872015-12-16 13:42:42 +08001760 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1761 stream_tag, format);
1762 mutex_unlock(&spec->pcm_lock);
1763 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001764}
1765
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001766static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1767 struct hda_codec *codec,
1768 struct snd_pcm_substream *substream)
1769{
1770 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1771 return 0;
1772}
1773
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001774static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1775 struct hda_codec *codec,
1776 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001777{
1778 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08001779 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06001780 struct hdmi_spec_per_cvt *per_cvt;
1781 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07001782 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06001783
Stephen Warren384a48d2011-06-01 11:14:21 -06001784 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08001785 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1786 if (snd_BUG_ON(pcm_idx < 0))
1787 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001788 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001789 if (snd_BUG_ON(cvt_idx < 0))
1790 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001791 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001792
1793 snd_BUG_ON(!per_cvt->assigned);
1794 per_cvt->assigned = 0;
1795 hinfo->nid = 0;
1796
Libin Yang42b29872015-12-16 13:42:42 +08001797 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08001798 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001799 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001800 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001801 if (spec->dyn_pcm_assign && pin_idx < 0) {
1802 mutex_unlock(&spec->pcm_lock);
1803 return 0;
1804 }
1805
1806 if (snd_BUG_ON(pin_idx < 0)) {
1807 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06001808 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08001809 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001810 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001811
Stephen Warren75fae112014-01-30 11:52:16 -07001812 if (spec->dyn_pin_out) {
1813 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1814 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1815 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1816 AC_VERB_SET_PIN_WIDGET_CONTROL,
1817 pinctl & ~PIN_OUT);
1818 }
1819
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001820 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001821 per_pin->chmap_set = false;
1822 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001823
1824 per_pin->setup = false;
1825 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001826 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08001827 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06001828 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001829
Stephen Warren384a48d2011-06-01 11:14:21 -06001830 return 0;
1831}
1832
1833static const struct hda_pcm_ops generic_ops = {
1834 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001835 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001836 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001837 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001838};
1839
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05301840static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1841 unsigned char *chmap)
1842{
1843 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1844 struct hdmi_spec *spec = codec->spec;
1845 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1846
1847 /* chmap is already set to 0 in caller */
1848 if (!per_pin)
1849 return;
1850
1851 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
1852}
1853
1854static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1855 unsigned char *chmap, int prepared)
1856{
1857 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1858 struct hdmi_spec *spec = codec->spec;
1859 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1860
1861 mutex_lock(&per_pin->lock);
1862 per_pin->chmap_set = true;
1863 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
1864 if (prepared)
1865 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1866 mutex_unlock(&per_pin->lock);
1867}
1868
1869static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1870{
1871 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1872 struct hdmi_spec *spec = codec->spec;
1873 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1874
1875 return per_pin ? true:false;
1876}
1877
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001878static int generic_hdmi_build_pcms(struct hda_codec *codec)
1879{
1880 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001881 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001882
Stephen Warren384a48d2011-06-01 11:14:21 -06001883 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1884 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001885 struct hda_pcm_stream *pstr;
1886
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001887 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001888 if (!info)
1889 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08001890
1891 spec->pcm_rec[pin_idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08001892 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001893 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001894 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001895
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001896 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001897 pstr->substreams = 1;
1898 pstr->ops = generic_ops;
1899 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001900 }
1901
1902 return 0;
1903}
1904
Libin Yang25e4abb2016-01-12 11:13:27 +08001905static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01001906{
Libin Yang25e4abb2016-01-12 11:13:27 +08001907 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01001908
Libin Yang25e4abb2016-01-12 11:13:27 +08001909 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001910}
1911
Libin Yang25e4abb2016-01-12 11:13:27 +08001912static int add_hdmi_jack_kctl(struct hda_codec *codec,
1913 struct hdmi_spec *spec,
1914 int pcm_idx,
Takashi Iwai788d4412015-11-12 15:36:13 +01001915 const char *name)
1916{
1917 struct snd_jack *jack;
1918 int err;
1919
1920 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
1921 true, false);
1922 if (err < 0)
1923 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08001924
1925 spec->pcm_rec[pcm_idx].jack = jack;
1926 jack->private_data = &spec->pcm_rec[pcm_idx];
1927 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01001928 return 0;
1929}
1930
Libin Yang25e4abb2016-01-12 11:13:27 +08001931static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
David Henningsson0b6c49b2011-08-23 16:56:03 +02001932{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001933 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001934 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08001935 struct hdmi_spec_per_pin *per_pin;
1936 struct hda_jack_tbl *jack;
1937 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01001938 bool phantom_jack;
Libin Yang25e4abb2016-01-12 11:13:27 +08001939 int ret;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001940
Takashi Iwai31ef2252011-12-01 17:41:36 +01001941 if (pcmdev > 0)
1942 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Libin Yang25e4abb2016-01-12 11:13:27 +08001943
1944 if (spec->dyn_pcm_assign)
1945 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
1946
1947 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
1948 /* if !dyn_pcm_assign, it must be non-MST mode.
1949 * This means pcms and pins are statically mapped.
1950 * And pcm_idx is pin_idx.
1951 */
1952 per_pin = get_pin(spec, pcm_idx);
Takashi Iwai909cadc2015-11-12 11:52:13 +01001953 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
1954 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01001955 strncat(hdmi_str, " Phantom",
1956 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
Libin Yang25e4abb2016-01-12 11:13:27 +08001957 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
1958 phantom_jack);
1959 if (ret < 0)
1960 return ret;
1961 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1962 if (jack == NULL)
1963 return 0;
1964 /* assign jack->jack to pcm_rec[].jack to
1965 * align with dyn_pcm_assign mode
1966 */
1967 spec->pcm_rec[pcm_idx].jack = jack->jack;
1968 return 0;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001969}
1970
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001971static int generic_hdmi_build_controls(struct hda_codec *codec)
1972{
1973 struct hdmi_spec *spec = codec->spec;
1974 int err;
Libin Yang25e4abb2016-01-12 11:13:27 +08001975 int pin_idx, pcm_idx;
1976
1977
1978 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
1979 err = generic_hdmi_build_jack(codec, pcm_idx);
1980 if (err < 0)
1981 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08001982
1983 /* create the spdif for each pcm
1984 * pin will be bound when monitor is connected
1985 */
1986 if (spec->dyn_pcm_assign)
1987 err = snd_hda_create_dig_out_ctls(codec,
1988 0, spec->cvt_nids[0],
1989 HDA_PCM_TYPE_HDMI);
1990 else {
1991 struct hdmi_spec_per_pin *per_pin =
1992 get_pin(spec, pcm_idx);
1993 err = snd_hda_create_dig_out_ctls(codec,
1994 per_pin->pin_nid,
1995 per_pin->mux_nids[0],
1996 HDA_PCM_TYPE_HDMI);
1997 }
1998 if (err < 0)
1999 return err;
2000 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002001
2002 /* add control for ELD Bytes */
2003 err = hdmi_create_eld_ctl(codec, pcm_idx,
2004 get_pcm_rec(spec, pcm_idx)->device);
2005 if (err < 0)
2006 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002007 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002008
Stephen Warren384a48d2011-06-01 11:14:21 -06002009 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002010 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002011
Takashi Iwai82b1d732011-12-20 15:53:07 +01002012 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002013 }
2014
Takashi Iwaid45e6882012-07-31 11:36:00 +02002015 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002016 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002017 struct hda_pcm *pcm;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002018
Libin Yang022f3442016-02-03 10:48:34 +08002019 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002020 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002021 break;
Subhransu S. Prusty2f6e8a82016-03-04 19:59:51 +05302022 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002023 if (err < 0)
2024 return err;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002025 }
2026
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002027 return 0;
2028}
2029
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002030static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2031{
2032 struct hdmi_spec *spec = codec->spec;
2033 int pin_idx;
2034
2035 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002036 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002037
2038 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002039 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002040 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002041 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002042 }
2043 return 0;
2044}
2045
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002046static int generic_hdmi_init(struct hda_codec *codec)
2047{
2048 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002049 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002050
Stephen Warren384a48d2011-06-01 11:14:21 -06002051 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002052 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002053 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002054
2055 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002056 if (!codec_has_acomp(codec))
2057 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2058 codec->jackpoll_interval > 0 ?
2059 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002060 }
2061 return 0;
2062}
2063
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002064static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2065{
2066 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2067 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002068}
2069
2070static void hdmi_array_free(struct hdmi_spec *spec)
2071{
2072 snd_array_free(&spec->pins);
2073 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002074}
2075
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002076static void generic_hdmi_free(struct hda_codec *codec)
2077{
2078 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002079 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002080
Takashi Iwai66032492015-12-01 16:49:35 +01002081 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002082 snd_hdac_i915_register_notifier(NULL);
2083
Stephen Warren384a48d2011-06-01 11:14:21 -06002084 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002085 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002086 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002087 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002088 }
2089
2090 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2091 if (spec->pcm_rec[pcm_idx].jack == NULL)
2092 continue;
2093 if (spec->dyn_pcm_assign)
2094 snd_device_free(codec->card,
2095 spec->pcm_rec[pcm_idx].jack);
2096 else
2097 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002098 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002099
Takashi Iwai55913112015-12-10 13:03:29 +01002100 if (spec->i915_bound)
2101 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002102 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002103 kfree(spec);
2104}
2105
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002106#ifdef CONFIG_PM
2107static int generic_hdmi_resume(struct hda_codec *codec)
2108{
2109 struct hdmi_spec *spec = codec->spec;
2110 int pin_idx;
2111
Pierre Ossmana2833682014-06-18 21:48:09 +02002112 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002113 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002114
2115 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2116 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2117 hdmi_present_sense(per_pin, 1);
2118 }
2119 return 0;
2120}
2121#endif
2122
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002123static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002124 .init = generic_hdmi_init,
2125 .free = generic_hdmi_free,
2126 .build_pcms = generic_hdmi_build_pcms,
2127 .build_controls = generic_hdmi_build_controls,
2128 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002129#ifdef CONFIG_PM
2130 .resume = generic_hdmi_resume,
2131#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002132};
2133
Anssi Hannula307229d2013-10-24 21:10:34 +03002134static const struct hdmi_ops generic_standard_hdmi_ops = {
2135 .pin_get_eld = snd_hdmi_get_eld,
Anssi Hannula307229d2013-10-24 21:10:34 +03002136 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2137 .pin_hbr_setup = hdmi_pin_hbr_setup,
2138 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302139};
2140
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002141static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2142 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002143{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002144 struct hdmi_spec *spec = codec->spec;
2145 hda_nid_t conns[4];
2146 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002147
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002148 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2149 if (nconns == spec->num_cvts &&
2150 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002151 return;
2152
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002153 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002154 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002155 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002156}
2157
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002158#define INTEL_VENDOR_NID 0x08
2159#define INTEL_GET_VENDOR_VERB 0xf81
2160#define INTEL_SET_VENDOR_VERB 0x781
2161#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2162#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2163
2164static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002165 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002166{
2167 unsigned int vendor_param;
2168
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002169 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2170 INTEL_GET_VENDOR_VERB, 0);
2171 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2172 return;
2173
2174 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2175 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2176 INTEL_SET_VENDOR_VERB, vendor_param);
2177 if (vendor_param == -1)
2178 return;
2179
Takashi Iwai17df3f52013-05-08 08:09:34 +02002180 if (update_tree)
2181 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002182}
2183
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002184static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2185{
2186 unsigned int vendor_param;
2187
2188 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2189 INTEL_GET_VENDOR_VERB, 0);
2190 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2191 return;
2192
2193 /* enable DP1.2 mode */
2194 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002195 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002196 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2197 INTEL_SET_VENDOR_VERB, vendor_param);
2198}
2199
Takashi Iwai17df3f52013-05-08 08:09:34 +02002200/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2201 * Otherwise you may get severe h/w communication errors.
2202 */
2203static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2204 unsigned int power_state)
2205{
2206 if (power_state == AC_PWRST_D0) {
2207 intel_haswell_enable_all_pins(codec, false);
2208 intel_haswell_fixup_enable_dp12(codec);
2209 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002210
Takashi Iwai17df3f52013-05-08 08:09:34 +02002211 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2212 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2213}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002214
David Henningssonf0675d42015-09-03 11:51:34 +02002215static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002216{
2217 struct hda_codec *codec = audio_ptr;
2218 int pin_nid = port + 0x04;
2219
Takashi Iwai4f8e4f32016-03-10 12:02:49 +01002220 /* we assume only from port-B to port-D */
2221 if (port < 1 || port > 3)
2222 return;
2223
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002224 /* skip notification during system suspend (but not in runtime PM);
2225 * the state will be updated at resume
2226 */
2227 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2228 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002229 /* ditto during suspend/resume process itself */
2230 if (atomic_read(&(codec)->core.in_pm))
2231 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002232
David Henningsson25adc132015-08-19 10:48:58 +02002233 check_presence_and_report(codec, pin_nid);
2234}
2235
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002236static int patch_generic_hdmi(struct hda_codec *codec)
2237{
2238 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002239
2240 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2241 if (spec == NULL)
2242 return -ENOMEM;
2243
Anssi Hannula307229d2013-10-24 21:10:34 +03002244 spec->ops = generic_standard_hdmi_ops;
Libin Yang42b29872015-12-16 13:42:42 +08002245 mutex_init(&spec->pcm_lock);
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05302246 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2247
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05302248 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2249 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2250 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2251
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002252 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002253 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002254
Takashi Iwai691be972016-03-18 15:10:08 +01002255#ifdef CONFIG_SND_HDA_I915
Takashi Iwaib62232d2016-03-15 18:15:26 +01002256 /* Try to bind with i915 for Intel HSW+ codecs (if not done yet) */
Takashi Iwai691be972016-03-18 15:10:08 +01002257 if ((codec->core.vendor_id >> 16) == 0x8086 &&
2258 is_haswell_plus(codec)) {
Takashi Iwai71697012016-03-19 10:40:21 +01002259#if 0
2260 /* on-demand binding leads to an unbalanced refcount when
2261 * both i915 and hda drivers are probed concurrently;
2262 * disabled temporarily for now
2263 */
Takashi Iwai691be972016-03-18 15:10:08 +01002264 if (!codec->bus->core.audio_component)
2265 if (!snd_hdac_i915_init(&codec->bus->core))
2266 spec->i915_bound = true;
Takashi Iwai71697012016-03-19 10:40:21 +01002267#endif
Takashi Iwai691be972016-03-18 15:10:08 +01002268 /* use i915 audio component notifier for hotplug */
2269 if (codec->bus->core.audio_component)
2270 spec->use_acomp_notifier = true;
2271 }
2272#endif
Takashi Iwai55913112015-12-10 13:03:29 +01002273
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002274 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002275 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002276 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002277 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002278
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002279 /* For Valleyview/Cherryview, only the display codec is in the display
2280 * power well and can use link_power ops to request/release the power.
2281 * For Haswell/Broadwell, the controller is also in the power well and
2282 * can cover the codec power request, and so need not set this flag.
2283 * For previous platforms, there is no such power well feature.
2284 */
Lu, Hanff9d8852015-11-19 23:25:13 +08002285 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2286 is_broxton(codec))
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002287 codec->core.link_power_control = 1;
2288
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002289 if (hdmi_parse_codec(codec) < 0) {
Takashi Iwai55913112015-12-10 13:03:29 +01002290 if (spec->i915_bound)
2291 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002292 codec->spec = NULL;
2293 kfree(spec);
2294 return -EINVAL;
2295 }
2296 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002297 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002298 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002299 codec->dp_mst = true;
2300 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002301
Lu, Han2377c3c2015-06-09 16:50:38 +08002302 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2303 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2304 codec->auto_runtime_pm = 1;
2305
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002306 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002307
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002308
Libin Yang790b4152016-03-04 14:33:43 +08002309 if (codec_has_acomp(codec)) {
2310 codec->depop_delay = 0;
2311 spec->i915_audio_ops.audio_ptr = codec;
2312 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2313 * will call pin_eld_notify with using audio_ptr pointer
2314 * We need make sure audio_ptr is really setup
2315 */
2316 wmb();
2317 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2318 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2319 }
2320
Libin Yang25e4abb2016-01-12 11:13:27 +08002321 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002322 return 0;
2323}
2324
2325/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002326 * Shared non-generic implementations
2327 */
2328
2329static int simple_playback_build_pcms(struct hda_codec *codec)
2330{
2331 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002332 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002333 unsigned int chans;
2334 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002335 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002336
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002337 per_cvt = get_cvt(spec, 0);
2338 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002339 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002340
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002341 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002342 if (!info)
2343 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002344 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002345 info->pcm_type = HDA_PCM_TYPE_HDMI;
2346 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2347 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002348 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002349 if (pstr->channels_max <= 2 && chans && chans <= 16)
2350 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002351
2352 return 0;
2353}
2354
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002355/* unsolicited event for jack sensing */
2356static void simple_hdmi_unsol_event(struct hda_codec *codec,
2357 unsigned int res)
2358{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002359 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002360 snd_hda_jack_report_sync(codec);
2361}
2362
2363/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2364 * as long as spec->pins[] is set correctly
2365 */
2366#define simple_hdmi_build_jack generic_hdmi_build_jack
2367
Stephen Warren3aaf8982011-06-01 11:14:19 -06002368static int simple_playback_build_controls(struct hda_codec *codec)
2369{
2370 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002371 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002372 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002373
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002374 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002375 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2376 per_cvt->cvt_nid,
2377 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002378 if (err < 0)
2379 return err;
2380 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002381}
2382
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002383static int simple_playback_init(struct hda_codec *codec)
2384{
2385 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002386 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2387 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002388
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002389 snd_hda_codec_write(codec, pin, 0,
2390 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2391 /* some codecs require to unmute the pin */
2392 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2393 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2394 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002395 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002396 return 0;
2397}
2398
Stephen Warren3aaf8982011-06-01 11:14:19 -06002399static void simple_playback_free(struct hda_codec *codec)
2400{
2401 struct hdmi_spec *spec = codec->spec;
2402
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002403 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002404 kfree(spec);
2405}
2406
2407/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002408 * Nvidia specific implementations
2409 */
2410
2411#define Nv_VERB_SET_Channel_Allocation 0xF79
2412#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2413#define Nv_VERB_SET_Audio_Protection_On 0xF98
2414#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2415
2416#define nvhdmi_master_con_nid_7x 0x04
2417#define nvhdmi_master_pin_nid_7x 0x05
2418
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002419static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002420 /*front, rear, clfe, rear_surr */
2421 0x6, 0x8, 0xa, 0xc,
2422};
2423
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002424static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2425 /* set audio protect on */
2426 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2427 /* enable digital output on pin widget */
2428 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2429 {} /* terminator */
2430};
2431
2432static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002433 /* set audio protect on */
2434 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2435 /* enable digital output on pin widget */
2436 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2437 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2438 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2439 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2440 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2441 {} /* terminator */
2442};
2443
2444#ifdef LIMITED_RATE_FMT_SUPPORT
2445/* support only the safe format and rate */
2446#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2447#define SUPPORTED_MAXBPS 16
2448#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2449#else
2450/* support all rates and formats */
2451#define SUPPORTED_RATES \
2452 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2453 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2454 SNDRV_PCM_RATE_192000)
2455#define SUPPORTED_MAXBPS 24
2456#define SUPPORTED_FORMATS \
2457 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2458#endif
2459
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002460static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002461{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002462 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2463 return 0;
2464}
2465
2466static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2467{
2468 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002469 return 0;
2470}
2471
Nitin Daga393004b2011-01-10 21:49:31 +05302472static unsigned int channels_2_6_8[] = {
2473 2, 6, 8
2474};
2475
2476static unsigned int channels_2_8[] = {
2477 2, 8
2478};
2479
2480static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2481 .count = ARRAY_SIZE(channels_2_6_8),
2482 .list = channels_2_6_8,
2483 .mask = 0,
2484};
2485
2486static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2487 .count = ARRAY_SIZE(channels_2_8),
2488 .list = channels_2_8,
2489 .mask = 0,
2490};
2491
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002492static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2493 struct hda_codec *codec,
2494 struct snd_pcm_substream *substream)
2495{
2496 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302497 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2498
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002499 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05302500 case 0x10de0002:
2501 case 0x10de0003:
2502 case 0x10de0005:
2503 case 0x10de0006:
2504 hw_constraints_channels = &hw_constraints_2_8_channels;
2505 break;
2506 case 0x10de0007:
2507 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2508 break;
2509 default:
2510 break;
2511 }
2512
2513 if (hw_constraints_channels != NULL) {
2514 snd_pcm_hw_constraint_list(substream->runtime, 0,
2515 SNDRV_PCM_HW_PARAM_CHANNELS,
2516 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002517 } else {
2518 snd_pcm_hw_constraint_step(substream->runtime, 0,
2519 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302520 }
2521
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002522 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2523}
2524
2525static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2526 struct hda_codec *codec,
2527 struct snd_pcm_substream *substream)
2528{
2529 struct hdmi_spec *spec = codec->spec;
2530 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2531}
2532
2533static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2534 struct hda_codec *codec,
2535 unsigned int stream_tag,
2536 unsigned int format,
2537 struct snd_pcm_substream *substream)
2538{
2539 struct hdmi_spec *spec = codec->spec;
2540 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2541 stream_tag, format, substream);
2542}
2543
Takashi Iwaid0b12522012-06-15 14:34:42 +02002544static const struct hda_pcm_stream simple_pcm_playback = {
2545 .substreams = 1,
2546 .channels_min = 2,
2547 .channels_max = 2,
2548 .ops = {
2549 .open = simple_playback_pcm_open,
2550 .close = simple_playback_pcm_close,
2551 .prepare = simple_playback_pcm_prepare
2552 },
2553};
2554
2555static const struct hda_codec_ops simple_hdmi_patch_ops = {
2556 .build_controls = simple_playback_build_controls,
2557 .build_pcms = simple_playback_build_pcms,
2558 .init = simple_playback_init,
2559 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002560 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002561};
2562
2563static int patch_simple_hdmi(struct hda_codec *codec,
2564 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2565{
2566 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002567 struct hdmi_spec_per_cvt *per_cvt;
2568 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002569
2570 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2571 if (!spec)
2572 return -ENOMEM;
2573
2574 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002575 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002576
2577 spec->multiout.num_dacs = 0; /* no analog */
2578 spec->multiout.max_channels = 2;
2579 spec->multiout.dig_out_nid = cvt_nid;
2580 spec->num_cvts = 1;
2581 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002582 per_pin = snd_array_new(&spec->pins);
2583 per_cvt = snd_array_new(&spec->cvts);
2584 if (!per_pin || !per_cvt) {
2585 simple_playback_free(codec);
2586 return -ENOMEM;
2587 }
2588 per_cvt->cvt_nid = cvt_nid;
2589 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002590 spec->pcm_playback = simple_pcm_playback;
2591
2592 codec->patch_ops = simple_hdmi_patch_ops;
2593
2594 return 0;
2595}
2596
Aaron Plattner1f348522011-04-06 17:19:04 -07002597static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2598 int channels)
2599{
2600 unsigned int chanmask;
2601 int chan = channels ? (channels - 1) : 1;
2602
2603 switch (channels) {
2604 default:
2605 case 0:
2606 case 2:
2607 chanmask = 0x00;
2608 break;
2609 case 4:
2610 chanmask = 0x08;
2611 break;
2612 case 6:
2613 chanmask = 0x0b;
2614 break;
2615 case 8:
2616 chanmask = 0x13;
2617 break;
2618 }
2619
2620 /* Set the audio infoframe channel allocation and checksum fields. The
2621 * channel count is computed implicitly by the hardware. */
2622 snd_hda_codec_write(codec, 0x1, 0,
2623 Nv_VERB_SET_Channel_Allocation, chanmask);
2624
2625 snd_hda_codec_write(codec, 0x1, 0,
2626 Nv_VERB_SET_Info_Frame_Checksum,
2627 (0x71 - chan - chanmask));
2628}
2629
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002630static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2631 struct hda_codec *codec,
2632 struct snd_pcm_substream *substream)
2633{
2634 struct hdmi_spec *spec = codec->spec;
2635 int i;
2636
2637 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2638 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2639 for (i = 0; i < 4; i++) {
2640 /* set the stream id */
2641 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2642 AC_VERB_SET_CHANNEL_STREAMID, 0);
2643 /* set the stream format */
2644 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2645 AC_VERB_SET_STREAM_FORMAT, 0);
2646 }
2647
Aaron Plattner1f348522011-04-06 17:19:04 -07002648 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2649 * streams are disabled. */
2650 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2651
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002652 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2653}
2654
2655static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2656 struct hda_codec *codec,
2657 unsigned int stream_tag,
2658 unsigned int format,
2659 struct snd_pcm_substream *substream)
2660{
2661 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002662 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002663 int i;
Stephen Warren7c9359762011-06-01 11:14:17 -06002664 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002665 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002666 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002667
2668 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002669 per_cvt = get_cvt(spec, 0);
2670 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002671
2672 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002673
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002674 dataDCC2 = 0x2;
2675
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002676 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06002677 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002678 snd_hda_codec_write(codec,
2679 nvhdmi_master_con_nid_7x,
2680 0,
2681 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c9359762011-06-01 11:14:17 -06002682 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002683
2684 /* set the stream id */
2685 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2686 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2687
2688 /* set the stream format */
2689 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2690 AC_VERB_SET_STREAM_FORMAT, format);
2691
2692 /* turn on again (if needed) */
2693 /* enable and set the channel status audio/data flag */
Stephen Warren7c9359762011-06-01 11:14:17 -06002694 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002695 snd_hda_codec_write(codec,
2696 nvhdmi_master_con_nid_7x,
2697 0,
2698 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c9359762011-06-01 11:14:17 -06002699 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002700 snd_hda_codec_write(codec,
2701 nvhdmi_master_con_nid_7x,
2702 0,
2703 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2704 }
2705
2706 for (i = 0; i < 4; i++) {
2707 if (chs == 2)
2708 channel_id = 0;
2709 else
2710 channel_id = i * 2;
2711
2712 /* turn off SPDIF once;
2713 *otherwise the IEC958 bits won't be updated
2714 */
2715 if (codec->spdif_status_reset &&
Stephen Warren7c9359762011-06-01 11:14:17 -06002716 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002717 snd_hda_codec_write(codec,
2718 nvhdmi_con_nids_7x[i],
2719 0,
2720 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c9359762011-06-01 11:14:17 -06002721 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002722 /* set the stream id */
2723 snd_hda_codec_write(codec,
2724 nvhdmi_con_nids_7x[i],
2725 0,
2726 AC_VERB_SET_CHANNEL_STREAMID,
2727 (stream_tag << 4) | channel_id);
2728 /* set the stream format */
2729 snd_hda_codec_write(codec,
2730 nvhdmi_con_nids_7x[i],
2731 0,
2732 AC_VERB_SET_STREAM_FORMAT,
2733 format);
2734 /* turn on again (if needed) */
2735 /* enable and set the channel status audio/data flag */
2736 if (codec->spdif_status_reset &&
Stephen Warren7c9359762011-06-01 11:14:17 -06002737 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002738 snd_hda_codec_write(codec,
2739 nvhdmi_con_nids_7x[i],
2740 0,
2741 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c9359762011-06-01 11:14:17 -06002742 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002743 snd_hda_codec_write(codec,
2744 nvhdmi_con_nids_7x[i],
2745 0,
2746 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2747 }
2748 }
2749
Aaron Plattner1f348522011-04-06 17:19:04 -07002750 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002751
2752 mutex_unlock(&codec->spdif_mutex);
2753 return 0;
2754}
2755
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002756static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002757 .substreams = 1,
2758 .channels_min = 2,
2759 .channels_max = 8,
2760 .nid = nvhdmi_master_con_nid_7x,
2761 .rates = SUPPORTED_RATES,
2762 .maxbps = SUPPORTED_MAXBPS,
2763 .formats = SUPPORTED_FORMATS,
2764 .ops = {
2765 .open = simple_playback_pcm_open,
2766 .close = nvhdmi_8ch_7x_pcm_close,
2767 .prepare = nvhdmi_8ch_7x_pcm_prepare
2768 },
2769};
2770
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002771static int patch_nvhdmi_2ch(struct hda_codec *codec)
2772{
2773 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002774 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2775 nvhdmi_master_pin_nid_7x);
2776 if (err < 0)
2777 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002778
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002779 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002780 /* override the PCM rates, etc, as the codec doesn't give full list */
2781 spec = codec->spec;
2782 spec->pcm_playback.rates = SUPPORTED_RATES;
2783 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2784 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002785 return 0;
2786}
2787
Takashi Iwai53775b02012-08-01 12:17:41 +02002788static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2789{
2790 struct hdmi_spec *spec = codec->spec;
2791 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002792 if (!err) {
2793 struct hda_pcm *info = get_pcm_rec(spec, 0);
2794 info->own_chmap = true;
2795 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002796 return err;
2797}
2798
2799static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2800{
2801 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002802 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002803 struct snd_pcm_chmap *chmap;
2804 int err;
2805
2806 err = simple_playback_build_controls(codec);
2807 if (err < 0)
2808 return err;
2809
2810 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002811 info = get_pcm_rec(spec, 0);
2812 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002813 SNDRV_PCM_STREAM_PLAYBACK,
2814 snd_pcm_alt_chmaps, 8, 0, &chmap);
2815 if (err < 0)
2816 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002817 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02002818 case 0x10de0002:
2819 case 0x10de0003:
2820 case 0x10de0005:
2821 case 0x10de0006:
2822 chmap->channel_mask = (1U << 2) | (1U << 8);
2823 break;
2824 case 0x10de0007:
2825 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2826 }
2827 return 0;
2828}
2829
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002830static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2831{
2832 struct hdmi_spec *spec;
2833 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002834 if (err < 0)
2835 return err;
2836 spec = codec->spec;
2837 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002838 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002839 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002840 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2841 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002842
2843 /* Initialize the audio infoframe channel mask and checksum to something
2844 * valid */
2845 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2846
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002847 return 0;
2848}
2849
2850/*
Anssi Hannula611885b2013-11-03 17:15:00 +02002851 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
2852 * - 0x10de0015
2853 * - 0x10de0040
2854 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302855static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05302856 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02002857{
2858 if (cap->ca_index == 0x00 && channels == 2)
2859 return SNDRV_CTL_TLVT_CHMAP_FIXED;
2860
Subhransu S. Prusty028cb682016-03-14 10:35:06 +05302861 /* If the speaker allocation matches the channel count, it is OK. */
2862 if (cap->channels != channels)
2863 return -1;
2864
2865 /* all channels are remappable freely */
2866 return SNDRV_CTL_TLVT_CHMAP_VAR;
Anssi Hannula611885b2013-11-03 17:15:00 +02002867}
2868
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05302869static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
2870 int ca, int chs, unsigned char *map)
Anssi Hannula611885b2013-11-03 17:15:00 +02002871{
2872 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
2873 return -EINVAL;
2874
2875 return 0;
2876}
2877
2878static int patch_nvhdmi(struct hda_codec *codec)
2879{
2880 struct hdmi_spec *spec;
2881 int err;
2882
2883 err = patch_generic_hdmi(codec);
2884 if (err)
2885 return err;
2886
2887 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07002888 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02002889
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302890 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02002891 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302892 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02002893
2894 return 0;
2895}
2896
2897/*
Thierry Reding26e9a962015-05-05 14:56:20 +02002898 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
2899 * accessed using vendor-defined verbs. These registers can be used for
2900 * interoperability between the HDA and HDMI drivers.
2901 */
2902
2903/* Audio Function Group node */
2904#define NVIDIA_AFG_NID 0x01
2905
2906/*
2907 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
2908 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
2909 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
2910 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
2911 * additional bit (at position 30) to signal the validity of the format.
2912 *
2913 * | 31 | 30 | 29 16 | 15 0 |
2914 * +---------+-------+--------+--------+
2915 * | TRIGGER | VALID | UNUSED | FORMAT |
2916 * +-----------------------------------|
2917 *
2918 * Note that for the trigger bit to take effect it needs to change value
2919 * (i.e. it needs to be toggled).
2920 */
2921#define NVIDIA_GET_SCRATCH0 0xfa6
2922#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
2923#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
2924#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
2925#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
2926#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
2927#define NVIDIA_SCRATCH_VALID (1 << 6)
2928
2929#define NVIDIA_GET_SCRATCH1 0xfab
2930#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
2931#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
2932#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
2933#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
2934
2935/*
2936 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
2937 * the format is invalidated so that the HDMI codec can be disabled.
2938 */
2939static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
2940{
2941 unsigned int value;
2942
2943 /* bits [31:30] contain the trigger and valid bits */
2944 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
2945 NVIDIA_GET_SCRATCH0, 0);
2946 value = (value >> 24) & 0xff;
2947
2948 /* bits [15:0] are used to store the HDA format */
2949 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
2950 NVIDIA_SET_SCRATCH0_BYTE0,
2951 (format >> 0) & 0xff);
2952 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
2953 NVIDIA_SET_SCRATCH0_BYTE1,
2954 (format >> 8) & 0xff);
2955
2956 /* bits [16:24] are unused */
2957 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
2958 NVIDIA_SET_SCRATCH0_BYTE2, 0);
2959
2960 /*
2961 * Bit 30 signals that the data is valid and hence that HDMI audio can
2962 * be enabled.
2963 */
2964 if (format == 0)
2965 value &= ~NVIDIA_SCRATCH_VALID;
2966 else
2967 value |= NVIDIA_SCRATCH_VALID;
2968
2969 /*
2970 * Whenever the trigger bit is toggled, an interrupt is raised in the
2971 * HDMI codec. The HDMI driver will use that as trigger to update its
2972 * configuration.
2973 */
2974 value ^= NVIDIA_SCRATCH_TRIGGER;
2975
2976 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
2977 NVIDIA_SET_SCRATCH0_BYTE3, value);
2978}
2979
2980static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
2981 struct hda_codec *codec,
2982 unsigned int stream_tag,
2983 unsigned int format,
2984 struct snd_pcm_substream *substream)
2985{
2986 int err;
2987
2988 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
2989 format, substream);
2990 if (err < 0)
2991 return err;
2992
2993 /* notify the HDMI codec of the format change */
2994 tegra_hdmi_set_format(codec, format);
2995
2996 return 0;
2997}
2998
2999static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3000 struct hda_codec *codec,
3001 struct snd_pcm_substream *substream)
3002{
3003 /* invalidate the format in the HDMI codec */
3004 tegra_hdmi_set_format(codec, 0);
3005
3006 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3007}
3008
3009static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3010{
3011 struct hdmi_spec *spec = codec->spec;
3012 unsigned int i;
3013
3014 for (i = 0; i < spec->num_pins; i++) {
3015 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3016
3017 if (pcm->pcm_type == type)
3018 return pcm;
3019 }
3020
3021 return NULL;
3022}
3023
3024static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3025{
3026 struct hda_pcm_stream *stream;
3027 struct hda_pcm *pcm;
3028 int err;
3029
3030 err = generic_hdmi_build_pcms(codec);
3031 if (err < 0)
3032 return err;
3033
3034 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3035 if (!pcm)
3036 return -ENODEV;
3037
3038 /*
3039 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3040 * codec about format changes.
3041 */
3042 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3043 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3044 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3045
3046 return 0;
3047}
3048
3049static int patch_tegra_hdmi(struct hda_codec *codec)
3050{
3051 int err;
3052
3053 err = patch_generic_hdmi(codec);
3054 if (err)
3055 return err;
3056
3057 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3058
3059 return 0;
3060}
3061
3062/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003063 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003064 */
3065
Anssi Hannula5a6135842013-10-24 21:10:35 +03003066#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003067 ((codec)->core.vendor_id == 0x1002aa01 && \
3068 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003069#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003070
Anssi Hannula5a6135842013-10-24 21:10:35 +03003071/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3072#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3073#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3074#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3075#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3076#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3077#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003078#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003079#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3080#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3081#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3082#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3083#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3084#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3085#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3086#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3087#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3088#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3089#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003090#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003091#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3092#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3093#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3094#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3095#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3096
Anssi Hannula84d69e72013-10-24 21:10:38 +03003097/* AMD specific HDA cvt verbs */
3098#define ATI_VERB_SET_RAMP_RATE 0x770
3099#define ATI_VERB_GET_RAMP_RATE 0xf70
3100
Anssi Hannula5a6135842013-10-24 21:10:35 +03003101#define ATI_OUT_ENABLE 0x1
3102
3103#define ATI_MULTICHANNEL_MODE_PAIRED 0
3104#define ATI_MULTICHANNEL_MODE_SINGLE 1
3105
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003106#define ATI_HBR_CAPABLE 0x01
3107#define ATI_HBR_ENABLE 0x10
3108
Anssi Hannula89250f82013-10-24 21:10:36 +03003109static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3110 unsigned char *buf, int *eld_size)
3111{
3112 /* call hda_eld.c ATI/AMD-specific function */
3113 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3114 is_amdhdmi_rev3_or_later(codec));
3115}
3116
Anssi Hannula5a6135842013-10-24 21:10:35 +03003117static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3118 int active_channels, int conn_type)
3119{
3120 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3121}
3122
3123static int atihdmi_paired_swap_fc_lfe(int pos)
3124{
3125 /*
3126 * ATI/AMD have automatic FC/LFE swap built-in
3127 * when in pairwise mapping mode.
3128 */
3129
3130 switch (pos) {
3131 /* see channel_allocations[].speakers[] */
3132 case 2: return 3;
3133 case 3: return 2;
3134 default: break;
3135 }
3136
3137 return pos;
3138}
3139
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303140static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3141 int ca, int chs, unsigned char *map)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003142{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303143 struct hdac_cea_channel_speaker_allocation *cap;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003144 int i, j;
3145
3146 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3147
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303148 cap = snd_hdac_get_ch_alloc_from_ca(ca);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003149 for (i = 0; i < chs; ++i) {
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303150 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003151 bool ok = false;
3152 bool companion_ok = false;
3153
3154 if (!mask)
3155 continue;
3156
3157 for (j = 0 + i % 2; j < 8; j += 2) {
3158 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3159 if (cap->speakers[chan_idx] == mask) {
3160 /* channel is in a supported position */
3161 ok = true;
3162
3163 if (i % 2 == 0 && i + 1 < chs) {
3164 /* even channel, check the odd companion */
3165 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303166 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003167 int comp_mask_act = cap->speakers[comp_chan_idx];
3168
3169 if (comp_mask_req == comp_mask_act)
3170 companion_ok = true;
3171 else
3172 return -EINVAL;
3173 }
3174 break;
3175 }
3176 }
3177
3178 if (!ok)
3179 return -EINVAL;
3180
3181 if (companion_ok)
3182 i++; /* companion channel already checked */
3183 }
3184
3185 return 0;
3186}
3187
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303188static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3189 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003190{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303191 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003192 int verb;
3193 int ati_channel_setup = 0;
3194
3195 if (hdmi_slot > 7)
3196 return -EINVAL;
3197
3198 if (!has_amd_full_remap_support(codec)) {
3199 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3200
3201 /* In case this is an odd slot but without stream channel, do not
3202 * disable the slot since the corresponding even slot could have a
3203 * channel. In case neither have a channel, the slot pair will be
3204 * disabled when this function is called for the even slot. */
3205 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3206 return 0;
3207
3208 hdmi_slot -= hdmi_slot % 2;
3209
3210 if (stream_channel != 0xf)
3211 stream_channel -= stream_channel % 2;
3212 }
3213
3214 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3215
3216 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3217
3218 if (stream_channel != 0xf)
3219 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3220
3221 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3222}
3223
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303224static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3225 hda_nid_t pin_nid, int asp_slot)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003226{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303227 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003228 bool was_odd = false;
3229 int ati_asp_slot = asp_slot;
3230 int verb;
3231 int ati_channel_setup;
3232
3233 if (asp_slot > 7)
3234 return -EINVAL;
3235
3236 if (!has_amd_full_remap_support(codec)) {
3237 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3238 if (ati_asp_slot % 2 != 0) {
3239 ati_asp_slot -= 1;
3240 was_odd = true;
3241 }
3242 }
3243
3244 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3245
3246 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3247
3248 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3249 return 0xf;
3250
3251 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3252}
3253
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303254static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3255 struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303256 struct hdac_cea_channel_speaker_allocation *cap,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303257 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003258{
3259 int c;
3260
3261 /*
3262 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3263 * we need to take that into account (a single channel may take 2
3264 * channel slots if we need to carry a silent channel next to it).
3265 * On Rev3+ AMD codecs this function is not used.
3266 */
3267 int chanpairs = 0;
3268
3269 /* We only produce even-numbered channel count TLVs */
3270 if ((channels % 2) != 0)
3271 return -1;
3272
3273 for (c = 0; c < 7; c += 2) {
3274 if (cap->speakers[c] || cap->speakers[c+1])
3275 chanpairs++;
3276 }
3277
3278 if (chanpairs * 2 != channels)
3279 return -1;
3280
3281 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3282}
3283
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303284static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303285 struct hdac_cea_channel_speaker_allocation *cap,
3286 unsigned int *chmap, int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003287{
3288 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3289 int count = 0;
3290 int c;
3291
3292 for (c = 7; c >= 0; c--) {
3293 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3294 int spk = cap->speakers[chan];
3295 if (!spk) {
3296 /* add N/A channel if the companion channel is occupied */
3297 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3298 chmap[count++] = SNDRV_CHMAP_NA;
3299
3300 continue;
3301 }
3302
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303303 chmap[count++] = snd_hdac_spk_to_chmap(spk);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003304 }
3305
3306 WARN_ON(count != channels);
3307}
3308
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003309static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3310 bool hbr)
3311{
3312 int hbr_ctl, hbr_ctl_new;
3313
3314 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003315 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003316 if (hbr)
3317 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3318 else
3319 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3320
Takashi Iwai4e76a882014-02-25 12:21:03 +01003321 codec_dbg(codec,
3322 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003323 pin_nid,
3324 hbr_ctl == hbr_ctl_new ? "" : "new-",
3325 hbr_ctl_new);
3326
3327 if (hbr_ctl != hbr_ctl_new)
3328 snd_hda_codec_write(codec, pin_nid, 0,
3329 ATI_VERB_SET_HBR_CONTROL,
3330 hbr_ctl_new);
3331
3332 } else if (hbr)
3333 return -EINVAL;
3334
3335 return 0;
3336}
3337
Anssi Hannula84d69e72013-10-24 21:10:38 +03003338static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3339 hda_nid_t pin_nid, u32 stream_tag, int format)
3340{
3341
3342 if (is_amdhdmi_rev3_or_later(codec)) {
3343 int ramp_rate = 180; /* default as per AMD spec */
3344 /* disable ramp-up/down for non-pcm as per AMD spec */
3345 if (format & AC_FMT_TYPE_NON_PCM)
3346 ramp_rate = 0;
3347
3348 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3349 }
3350
3351 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3352}
3353
3354
Anssi Hannula5a6135842013-10-24 21:10:35 +03003355static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003356{
3357 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003358 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003359
Anssi Hannula5a6135842013-10-24 21:10:35 +03003360 err = generic_hdmi_init(codec);
3361
3362 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003363 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003364
3365 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3366 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3367
3368 /* make sure downmix information in infoframe is zero */
3369 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3370
3371 /* enable channel-wise remap mode if supported */
3372 if (has_amd_full_remap_support(codec))
3373 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3374 ATI_VERB_SET_MULTICHANNEL_MODE,
3375 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003376 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003377
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003378 return 0;
3379}
3380
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003381static int patch_atihdmi(struct hda_codec *codec)
3382{
3383 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003384 struct hdmi_spec_per_cvt *per_cvt;
3385 int err, cvt_idx;
3386
3387 err = patch_generic_hdmi(codec);
3388
3389 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003390 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003391
3392 codec->patch_ops.init = atihdmi_init;
3393
Takashi Iwaid0b12522012-06-15 14:34:42 +02003394 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003395
Anssi Hannula89250f82013-10-24 21:10:36 +03003396 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003397 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003398 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003399 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003400
3401 if (!has_amd_full_remap_support(codec)) {
3402 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303403 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03003404 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303405 spec->chmap.ops.cea_alloc_to_tlv_chmap =
3406 atihdmi_paired_cea_alloc_to_tlv_chmap;
3407 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303408 spec->chmap.ops.pin_get_slot_channel =
3409 atihdmi_pin_get_slot_channel;
3410 spec->chmap.ops.pin_set_slot_channel =
3411 atihdmi_pin_set_slot_channel;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003412 }
3413
3414 /* ATI/AMD converters do not advertise all of their capabilities */
3415 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3416 per_cvt = get_cvt(spec, cvt_idx);
3417 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3418 per_cvt->rates |= SUPPORTED_RATES;
3419 per_cvt->formats |= SUPPORTED_FORMATS;
3420 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3421 }
3422
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303423 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003424
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003425 return 0;
3426}
3427
Annie Liu3de5ff82012-06-08 19:18:42 +08003428/* VIA HDMI Implementation */
3429#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3430#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3431
Annie Liu3de5ff82012-06-08 19:18:42 +08003432static int patch_via_hdmi(struct hda_codec *codec)
3433{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003434 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003435}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003436
3437/*
3438 * patch entries
3439 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003440static const struct hda_device_id snd_hda_id_hdmi[] = {
3441HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3442HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3443HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3444HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3445HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3446HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3447HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3448HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3449HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3450HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3451HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3452HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3453HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3454HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3455HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3456HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3457HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3458HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3459HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3460HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3461HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3462HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3463HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01003464/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003465HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3466HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3467HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3468HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3469HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3470HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3471HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3472HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3473HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3474HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3475HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3476HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3477HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3478HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3479HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3480HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3481HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3482HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3483HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3484HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3485HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Aaron Plattner2d369c72016-03-13 13:58:57 -07003486HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08003487HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003488HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3489HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3490HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3491HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3492HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3493HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
3494HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3495HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3496HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3497HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
3498HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
3499HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
3500HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
3501HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
3502HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
3503HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
Libin Yang91815d82016-01-14 14:09:00 +08003504HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003505HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
3506HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
3507HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
3508HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003509/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003510HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003511{} /* terminator */
3512};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003513MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003514
3515MODULE_LICENSE("GPL");
3516MODULE_DESCRIPTION("HDMI HD-audio codec");
3517MODULE_ALIAS("snd-hda-codec-intelhdmi");
3518MODULE_ALIAS("snd-hda-codec-nvhdmi");
3519MODULE_ALIAS("snd-hda-codec-atihdmi");
3520
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003521static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003522 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003523};
3524
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003525module_hda_codec_driver(hdmi_driver);