blob: 48c63fea7018b64d1eb73ae473dbd6fbcf7b4dd8 [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;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +0530157
158 struct hdac_chmap chmap;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800159};
160
Takashi Iwaif4e30402015-12-10 13:01:28 +0100161#ifdef CONFIG_SND_HDA_I915
Takashi Iwai691be972016-03-18 15:10:08 +0100162static inline bool codec_has_acomp(struct hda_codec *codec)
163{
164 struct hdmi_spec *spec = codec->spec;
165 return spec->use_acomp_notifier;
166}
Takashi Iwaif4e30402015-12-10 13:01:28 +0100167#else
168#define codec_has_acomp(codec) false
169#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800170
171struct hdmi_audio_infoframe {
172 u8 type; /* 0x84 */
173 u8 ver; /* 0x01 */
174 u8 len; /* 0x0a */
175
Wu Fengguang53d7d692010-09-21 14:25:49 +0800176 u8 checksum;
177
Wu Fengguang079d88c2010-03-08 10:44:23 +0800178 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
179 u8 SS01_SF24;
180 u8 CXT04;
181 u8 CA;
182 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800183};
184
185struct dp_audio_infoframe {
186 u8 type; /* 0x84 */
187 u8 len; /* 0x1b */
188 u8 ver; /* 0x11 << 2 */
189
190 u8 CC02_CT47; /* match with HDMI infoframe from this on */
191 u8 SS01_SF24;
192 u8 CXT04;
193 u8 CA;
194 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800195};
196
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100197union audio_infoframe {
198 struct hdmi_audio_infoframe hdmi;
199 struct dp_audio_infoframe dp;
200 u8 bytes[0];
201};
202
Wu Fengguang079d88c2010-03-08 10:44:23 +0800203/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800204 * HDMI routines
205 */
206
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100207#define get_pin(spec, idx) \
208 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
209#define get_cvt(spec, idx) \
210 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Libin Yang2bea2412016-01-12 11:13:26 +0800211/* obtain hdmi_pcm object assigned to idx */
212#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
213/* obtain hda_pcm object assigned to idx */
214#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100215
Takashi Iwai4e76a882014-02-25 12:21:03 +0100216static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800217{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100218 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600219 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800220
Stephen Warren384a48d2011-06-01 11:14:21 -0600221 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100222 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600223 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800224
Takashi Iwai4e76a882014-02-25 12:21:03 +0100225 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600226 return -EINVAL;
227}
228
Libin Yang2bf3c852015-12-16 13:42:43 +0800229static int hinfo_to_pcm_index(struct hda_codec *codec,
230 struct hda_pcm_stream *hinfo)
231{
232 struct hdmi_spec *spec = codec->spec;
233 int pcm_idx;
234
235 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
236 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
237 return pcm_idx;
238
239 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
240 return -EINVAL;
241}
242
Takashi Iwai4e76a882014-02-25 12:21:03 +0100243static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600244 struct hda_pcm_stream *hinfo)
245{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100246 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800247 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600248 int pin_idx;
249
Libin Yang6590faa2015-12-16 13:42:41 +0800250 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
251 per_pin = get_pin(spec, pin_idx);
Libin Yang2bea2412016-01-12 11:13:26 +0800252 if (per_pin->pcm &&
253 per_pin->pcm->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600254 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800255 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600256
Libin Yang6590faa2015-12-16 13:42:41 +0800257 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600258 return -EINVAL;
259}
260
Libin Yang022f3442016-02-03 10:48:34 +0800261static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
262 int pcm_idx)
263{
264 int i;
265 struct hdmi_spec_per_pin *per_pin;
266
267 for (i = 0; i < spec->num_pins; i++) {
268 per_pin = get_pin(spec, i);
269 if (per_pin->pcm_idx == pcm_idx)
270 return per_pin;
271 }
272 return NULL;
273}
274
Takashi Iwai4e76a882014-02-25 12:21:03 +0100275static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600276{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100277 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600278 int cvt_idx;
279
280 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100281 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600282 return cvt_idx;
283
Takashi Iwai4e76a882014-02-25 12:21:03 +0100284 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800285 return -EINVAL;
286}
287
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500288static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
289 struct snd_ctl_elem_info *uinfo)
290{
291 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100292 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200293 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100294 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800295 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500296
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500297 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
298
Libin Yangfb087ea2016-02-23 16:33:37 +0800299 pcm_idx = kcontrol->private_value;
300 mutex_lock(&spec->pcm_lock);
301 per_pin = pcm_idx_to_pin(spec, pcm_idx);
302 if (!per_pin) {
303 /* no pin is bound to the pcm */
304 uinfo->count = 0;
305 mutex_unlock(&spec->pcm_lock);
306 return 0;
307 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200308 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100309 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Libin Yangfb087ea2016-02-23 16:33:37 +0800310 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500311
312 return 0;
313}
314
315static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
316 struct snd_ctl_elem_value *ucontrol)
317{
318 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100319 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200320 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100321 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800322 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500323
Libin Yangfb087ea2016-02-23 16:33:37 +0800324 pcm_idx = kcontrol->private_value;
325 mutex_lock(&spec->pcm_lock);
326 per_pin = pcm_idx_to_pin(spec, pcm_idx);
327 if (!per_pin) {
328 /* no pin is bound to the pcm */
329 memset(ucontrol->value.bytes.data, 0,
330 ARRAY_SIZE(ucontrol->value.bytes.data));
331 mutex_unlock(&spec->pcm_lock);
332 return 0;
333 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200334 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500335
David Henningsson360a8242016-02-05 09:05:41 +0100336 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
337 eld->eld_size > ELD_MAX_SIZE) {
Libin Yangfb087ea2016-02-23 16:33:37 +0800338 mutex_unlock(&spec->pcm_lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100339 snd_BUG();
340 return -EINVAL;
341 }
342
343 memset(ucontrol->value.bytes.data, 0,
344 ARRAY_SIZE(ucontrol->value.bytes.data));
345 if (eld->eld_valid)
346 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
347 eld->eld_size);
Libin Yangfb087ea2016-02-23 16:33:37 +0800348 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500349
350 return 0;
351}
352
353static struct snd_kcontrol_new eld_bytes_ctl = {
354 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
355 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
356 .name = "ELD",
357 .info = hdmi_eld_ctl_info,
358 .get = hdmi_eld_ctl_get,
359};
360
Libin Yangfb087ea2016-02-23 16:33:37 +0800361static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500362 int device)
363{
364 struct snd_kcontrol *kctl;
365 struct hdmi_spec *spec = codec->spec;
366 int err;
367
368 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
369 if (!kctl)
370 return -ENOMEM;
Libin Yangfb087ea2016-02-23 16:33:37 +0800371 kctl->private_value = pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500372 kctl->id.device = device;
373
Libin Yangfb087ea2016-02-23 16:33:37 +0800374 /* no pin nid is associated with the kctl now
375 * tbd: associate pin nid to eld ctl later
376 */
377 err = snd_hda_ctl_add(codec, 0, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500378 if (err < 0)
379 return err;
380
Libin Yangfb087ea2016-02-23 16:33:37 +0800381 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500382 return 0;
383}
384
Wu Fengguang079d88c2010-03-08 10:44:23 +0800385#ifdef BE_PARANOID
386static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
387 int *packet_index, int *byte_index)
388{
389 int val;
390
391 val = snd_hda_codec_read(codec, pin_nid, 0,
392 AC_VERB_GET_HDMI_DIP_INDEX, 0);
393
394 *packet_index = val >> 5;
395 *byte_index = val & 0x1f;
396}
397#endif
398
399static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
400 int packet_index, int byte_index)
401{
402 int val;
403
404 val = (packet_index << 5) | (byte_index & 0x1f);
405
406 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
407}
408
409static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
410 unsigned char val)
411{
412 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
413}
414
Stephen Warren384a48d2011-06-01 11:14:21 -0600415static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800416{
Stephen Warren75fae112014-01-30 11:52:16 -0700417 struct hdmi_spec *spec = codec->spec;
418 int pin_out;
419
Wu Fengguang079d88c2010-03-08 10:44:23 +0800420 /* Unmute */
421 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
422 snd_hda_codec_write(codec, pin_nid, 0,
423 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700424
425 if (spec->dyn_pin_out)
426 /* Disable pin out until stream is active */
427 pin_out = 0;
428 else
429 /* Enable pin out: some machines with GM965 gets broken output
430 * when the pin is disabled or changed while using with HDMI
431 */
432 pin_out = PIN_OUT;
433
Wu Fengguang079d88c2010-03-08 10:44:23 +0800434 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700435 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800436}
437
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200438/*
439 * ELD proc files
440 */
441
Jie Yangcd6a6502015-05-27 19:45:45 +0800442#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200443static void print_eld_info(struct snd_info_entry *entry,
444 struct snd_info_buffer *buffer)
445{
446 struct hdmi_spec_per_pin *per_pin = entry->private_data;
447
448 mutex_lock(&per_pin->lock);
449 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
450 mutex_unlock(&per_pin->lock);
451}
452
453static void write_eld_info(struct snd_info_entry *entry,
454 struct snd_info_buffer *buffer)
455{
456 struct hdmi_spec_per_pin *per_pin = entry->private_data;
457
458 mutex_lock(&per_pin->lock);
459 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
460 mutex_unlock(&per_pin->lock);
461}
462
463static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
464{
465 char name[32];
466 struct hda_codec *codec = per_pin->codec;
467 struct snd_info_entry *entry;
468 int err;
469
470 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100471 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200472 if (err < 0)
473 return err;
474
475 snd_info_set_text_ops(entry, per_pin, print_eld_info);
476 entry->c.text.write = write_eld_info;
477 entry->mode |= S_IWUSR;
478 per_pin->proc_entry = entry;
479
480 return 0;
481}
482
483static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
484{
Markus Elfring1947a112015-06-28 11:15:28 +0200485 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200486 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200487 per_pin->proc_entry = NULL;
488 }
489}
490#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200491static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
492 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200493{
494 return 0;
495}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200496static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200497{
498}
499#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800500
501/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800502 * Audio InfoFrame routines
503 */
504
505/*
506 * Enable Audio InfoFrame Transmission
507 */
508static void hdmi_start_infoframe_trans(struct hda_codec *codec,
509 hda_nid_t pin_nid)
510{
511 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
512 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
513 AC_DIPXMIT_BEST);
514}
515
516/*
517 * Disable Audio InfoFrame Transmission
518 */
519static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
520 hda_nid_t pin_nid)
521{
522 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
523 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
524 AC_DIPXMIT_DISABLE);
525}
526
527static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
528{
529#ifdef CONFIG_SND_DEBUG_VERBOSE
530 int i;
531 int size;
532
533 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100534 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800535
536 for (i = 0; i < 8; i++) {
537 size = snd_hda_codec_read(codec, pin_nid, 0,
538 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100539 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800540 }
541#endif
542}
543
544static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
545{
546#ifdef BE_PARANOID
547 int i, j;
548 int size;
549 int pi, bi;
550 for (i = 0; i < 8; i++) {
551 size = snd_hda_codec_read(codec, pin_nid, 0,
552 AC_VERB_GET_HDMI_DIP_SIZE, i);
553 if (size == 0)
554 continue;
555
556 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
557 for (j = 1; j < 1000; j++) {
558 hdmi_write_dip_byte(codec, pin_nid, 0x0);
559 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
560 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100561 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +0800562 bi, pi, i);
563 if (bi == 0) /* byte index wrapped around */
564 break;
565 }
Takashi Iwai4e76a882014-02-25 12:21:03 +0100566 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800567 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
568 i, size, j);
569 }
570#endif
571}
572
Wu Fengguang53d7d692010-09-21 14:25:49 +0800573static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800574{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800575 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800576 u8 sum = 0;
577 int i;
578
Wu Fengguang53d7d692010-09-21 14:25:49 +0800579 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800580
Wu Fengguang53d7d692010-09-21 14:25:49 +0800581 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800582 sum += bytes[i];
583
Wu Fengguang53d7d692010-09-21 14:25:49 +0800584 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800585}
586
587static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
588 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800589 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800590{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800591 int i;
592
593 hdmi_debug_dip_size(codec, pin_nid);
594 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
595
Wu Fengguang079d88c2010-03-08 10:44:23 +0800596 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800597 for (i = 0; i < size; i++)
598 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800599}
600
601static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800602 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800603{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800604 u8 val;
605 int i;
606
607 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
608 != AC_DIPXMIT_BEST)
609 return false;
610
611 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800612 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800613 val = snd_hda_codec_read(codec, pin_nid, 0,
614 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800615 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800616 return false;
617 }
618
619 return true;
620}
621
Anssi Hannula307229d2013-10-24 21:10:34 +0300622static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
623 hda_nid_t pin_nid,
624 int ca, int active_channels,
625 int conn_type)
626{
627 union audio_infoframe ai;
628
Mengdong Lincaaf5ef2014-03-11 17:12:52 -0400629 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +0300630 if (conn_type == 0) { /* HDMI */
631 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
632
633 hdmi_ai->type = 0x84;
634 hdmi_ai->ver = 0x01;
635 hdmi_ai->len = 0x0a;
636 hdmi_ai->CC02_CT47 = active_channels - 1;
637 hdmi_ai->CA = ca;
638 hdmi_checksum_audio_infoframe(hdmi_ai);
639 } else if (conn_type == 1) { /* DisplayPort */
640 struct dp_audio_infoframe *dp_ai = &ai.dp;
641
642 dp_ai->type = 0x84;
643 dp_ai->len = 0x1b;
644 dp_ai->ver = 0x11 << 2;
645 dp_ai->CC02_CT47 = active_channels - 1;
646 dp_ai->CA = ca;
647 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100648 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300649 pin_nid);
650 return;
651 }
652
653 /*
654 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
655 * sizeof(*dp_ai) to avoid partial match/update problems when
656 * the user switches between HDMI/DP monitors.
657 */
658 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
659 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100660 codec_dbg(codec,
661 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300662 pin_nid,
663 active_channels, ca);
664 hdmi_stop_infoframe_trans(codec, pin_nid);
665 hdmi_fill_audio_infoframe(codec, pin_nid,
666 ai.bytes, sizeof(ai));
667 hdmi_start_infoframe_trans(codec, pin_nid);
668 }
669}
670
Takashi Iwaib0540872013-09-02 12:33:02 +0200671static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
672 struct hdmi_spec_per_pin *per_pin,
673 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800674{
Anssi Hannula307229d2013-10-24 21:10:34 +0300675 struct hdmi_spec *spec = codec->spec;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530676 struct hdac_chmap *chmap = &spec->chmap;
Stephen Warren384a48d2011-06-01 11:14:21 -0600677 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200678 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300679 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600680 struct hdmi_eld *eld;
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530681 int ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800682
Takashi Iwaib0540872013-09-02 12:33:02 +0200683 if (!channels)
684 return;
685
Mengdong Lin75dcbe42014-01-08 15:55:32 -0500686 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -0400687 snd_hda_codec_write(codec, pin_nid, 0,
688 AC_VERB_SET_AMP_GAIN_MUTE,
689 AMP_OUT_UNMUTE);
690
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100691 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800692
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530693 ca = snd_hdac_channel_allocation(&codec->core,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530694 eld->info.spk_alloc, channels,
695 per_pin->chmap_set, non_pcm, per_pin->chmap);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800696
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530697 active_channels = snd_hdac_get_active_channels(ca);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300698
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530699 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
700 active_channels);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300701
Stephen Warren384a48d2011-06-01 11:14:21 -0600702 /*
Anssi Hannula39edac72013-10-07 19:24:52 +0300703 * always configure channel mapping, it may have been changed by the
704 * user in the meantime
705 */
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530706 snd_hdac_setup_channel_mapping(&spec->chmap,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530707 pin_nid, non_pcm, ca, channels,
708 per_pin->chmap, per_pin->chmap_set);
Anssi Hannula39edac72013-10-07 19:24:52 +0300709
Anssi Hannula307229d2013-10-24 21:10:34 +0300710 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
711 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +0800712
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200713 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800714}
715
Wu Fengguang079d88c2010-03-08 10:44:23 +0800716/*
717 * Unsolicited events
718 */
719
Takashi Iwaiefe47102013-11-07 13:38:23 +0100720static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200721
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200722static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800723{
724 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200725 int pin_idx = pin_nid_to_pin_index(codec, nid);
726
David Henningsson20ce9022013-12-04 10:19:41 +0800727 if (pin_idx < 0)
728 return;
David Henningsson20ce9022013-12-04 10:19:41 +0800729 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
730 snd_hda_jack_report_sync(codec);
731}
732
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200733static void jack_callback(struct hda_codec *codec,
734 struct hda_jack_callback *jack)
735{
Takashi Iwai2ebab402016-02-09 10:23:52 +0100736 check_presence_and_report(codec, jack->nid);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200737}
738
David Henningsson20ce9022013-12-04 10:19:41 +0800739static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
740{
Takashi Iwai3a938972011-10-28 01:16:55 +0200741 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200742 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400743 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200744
745 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
746 if (!jack)
747 return;
Takashi Iwai3a938972011-10-28 01:16:55 +0200748 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800749
Takashi Iwai4e76a882014-02-25 12:21:03 +0100750 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400751 "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 +0800752 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800753 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800754
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200755 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800756}
757
758static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
759{
760 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
761 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
762 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
763 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
764
Takashi Iwai4e76a882014-02-25 12:21:03 +0100765 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200766 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600767 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800768 tag,
769 subtag,
770 cp_state,
771 cp_ready);
772
773 /* TODO */
774 if (cp_state)
775 ;
776 if (cp_ready)
777 ;
778}
779
780
781static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
782{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800783 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
784 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
785
Takashi Iwai3a938972011-10-28 01:16:55 +0200786 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100787 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800788 return;
789 }
790
791 if (subtag == 0)
792 hdmi_intrinsic_event(codec, res);
793 else
794 hdmi_non_intrinsic_event(codec, res);
795}
796
Mengdong Lin58f7d282013-09-04 16:37:12 -0400797static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +0800798 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +0200799{
Mengdong Lin58f7d282013-09-04 16:37:12 -0400800 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +0200801
Wang Xingchao53b434f2013-06-18 10:41:53 +0800802 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
803 * thus pins could only choose converter 0 for use. Make sure the
804 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200805 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +0800806 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
807
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200808 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +0200809 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
810 AC_PWRST_D0);
811 msleep(40);
812 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
813 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100814 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 +0200815 }
David Henningsson83f26ad2013-04-10 12:26:07 +0200816}
817
Wu Fengguang079d88c2010-03-08 10:44:23 +0800818/*
819 * Callbacks
820 */
821
Takashi Iwai92f10b32010-08-03 14:21:00 +0200822/* HBR should be Non-PCM, 8 channels */
823#define is_hbr_format(format) \
824 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
825
Anssi Hannula307229d2013-10-24 21:10:34 +0300826static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
827 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800828{
Anssi Hannula307229d2013-10-24 21:10:34 +0300829 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +0200830
Stephen Warren384a48d2011-06-01 11:14:21 -0600831 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
832 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300833 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
834
Anssi Hannula13122e62013-11-10 20:56:10 +0200835 if (pinctl < 0)
836 return hbr ? -EINVAL : 0;
837
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300838 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +0300839 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300840 new_pinctl |= AC_PINCTL_EPT_HBR;
841 else
842 new_pinctl |= AC_PINCTL_EPT_NATIVE;
843
Takashi Iwai4e76a882014-02-25 12:21:03 +0100844 codec_dbg(codec,
845 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600846 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300847 pinctl == new_pinctl ? "" : "new-",
848 new_pinctl);
849
850 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -0600851 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300852 AC_VERB_SET_PIN_WIDGET_CONTROL,
853 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +0300854 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300855 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +0300856
857 return 0;
858}
859
860static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
861 hda_nid_t pin_nid, u32 stream_tag, int format)
862{
863 struct hdmi_spec *spec = codec->spec;
864 int err;
865
Mengdong Lin75dcbe42014-01-08 15:55:32 -0500866 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +0300867 haswell_verify_D0(codec, cvt_nid, pin_nid);
868
869 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
870
871 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100872 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +0300873 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300874 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800875
Stephen Warren384a48d2011-06-01 11:14:21 -0600876 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300877 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800878}
879
Libin Yang42b29872015-12-16 13:42:42 +0800880/* Try to find an available converter
881 * If pin_idx is less then zero, just try to find an available converter.
882 * Otherwise, try to find an available converter and get the cvt mux index
883 * of the pin.
884 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800885static int hdmi_choose_cvt(struct hda_codec *codec,
886 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200887{
888 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600889 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600890 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800891 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200892
Libin Yang42b29872015-12-16 13:42:42 +0800893 /* pin_idx < 0 means no pin will be bound to the converter */
894 if (pin_idx < 0)
895 per_pin = NULL;
896 else
897 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200898
Stephen Warren384a48d2011-06-01 11:14:21 -0600899 /* Dynamically assign converter to stream */
900 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100901 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -0600902
903 /* Must not already be assigned */
904 if (per_cvt->assigned)
905 continue;
Libin Yang42b29872015-12-16 13:42:42 +0800906 if (per_pin == NULL)
907 break;
Stephen Warren384a48d2011-06-01 11:14:21 -0600908 /* Must be in pin's mux's list of converters */
909 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
910 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
911 break;
912 /* Not in mux list */
913 if (mux_idx == per_pin->num_mux_nids)
914 continue;
915 break;
916 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800917
Stephen Warren384a48d2011-06-01 11:14:21 -0600918 /* No free converters */
919 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +0800920 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -0600921
Libin Yang42b29872015-12-16 13:42:42 +0800922 if (per_pin != NULL)
923 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +0800924
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800925 if (cvt_id)
926 *cvt_id = cvt_idx;
927 if (mux_id)
928 *mux_id = mux_idx;
929
930 return 0;
931}
932
Mengdong Lin2df67422014-03-20 13:01:06 +0800933/* Assure the pin select the right convetor */
934static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
935 struct hdmi_spec_per_pin *per_pin)
936{
937 hda_nid_t pin_nid = per_pin->pin_nid;
938 int mux_idx, curr;
939
940 mux_idx = per_pin->mux_idx;
941 curr = snd_hda_codec_read(codec, pin_nid, 0,
942 AC_VERB_GET_CONNECT_SEL, 0);
943 if (curr != mux_idx)
944 snd_hda_codec_write_cache(codec, pin_nid, 0,
945 AC_VERB_SET_CONNECT_SEL,
946 mux_idx);
947}
948
Libin Yang42b29872015-12-16 13:42:42 +0800949/* get the mux index for the converter of the pins
950 * converter's mux index is the same for all pins on Intel platform
951 */
952static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
953 hda_nid_t cvt_nid)
954{
955 int i;
956
957 for (i = 0; i < spec->num_cvts; i++)
958 if (spec->cvt_nids[i] == cvt_nid)
959 return i;
960 return -EINVAL;
961}
962
Mengdong Lin300016b2013-11-04 01:13:13 -0500963/* Intel HDMI workaround to fix audio routing issue:
964 * For some Intel display codecs, pins share the same connection list.
965 * So a conveter can be selected by multiple pins and playback on any of these
966 * pins will generate sound on the external display, because audio flows from
967 * the same converter to the display pipeline. Also muting one pin may make
968 * other pins have no sound output.
969 * So this function assures that an assigned converter for a pin is not selected
970 * by any other pins.
971 */
972static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -0400973 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800974{
975 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +0100976 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -0400977 int cvt_idx, curr;
978 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800979
Mengdong Linf82d7d12013-09-21 20:34:45 -0400980 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +0100981 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -0400982 unsigned int wid_caps = get_wcaps(codec, nid);
983 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800984
Mengdong Linf82d7d12013-09-21 20:34:45 -0400985 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800986 continue;
987
Mengdong Linf82d7d12013-09-21 20:34:45 -0400988 if (nid == pin_nid)
989 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800990
Mengdong Linf82d7d12013-09-21 20:34:45 -0400991 curr = snd_hda_codec_read(codec, nid, 0,
992 AC_VERB_GET_CONNECT_SEL, 0);
993 if (curr != mux_idx)
994 continue;
995
996 /* choose an unassigned converter. The conveters in the
997 * connection list are in the same order as in the codec.
998 */
999 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1000 per_cvt = get_cvt(spec, cvt_idx);
1001 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001002 codec_dbg(codec,
1003 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001004 cvt_idx, nid);
1005 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001006 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001007 cvt_idx);
1008 break;
1009 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001010 }
1011 }
1012}
1013
Libin Yang42b29872015-12-16 13:42:42 +08001014/* A wrapper of intel_not_share_asigned_cvt() */
1015static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1016 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1017{
1018 int mux_idx;
1019 struct hdmi_spec *spec = codec->spec;
1020
1021 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1022 return;
1023
1024 /* On Intel platform, the mapping of converter nid to
1025 * mux index of the pins are always the same.
1026 * The pin nid may be 0, this means all pins will not
1027 * share the converter.
1028 */
1029 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1030 if (mux_idx >= 0)
1031 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1032}
1033
1034/* called in hdmi_pcm_open when no pin is assigned to the PCM
1035 * in dyn_pcm_assign mode.
1036 */
1037static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1038 struct hda_codec *codec,
1039 struct snd_pcm_substream *substream)
1040{
1041 struct hdmi_spec *spec = codec->spec;
1042 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001043 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001044 struct hdmi_spec_per_cvt *per_cvt = NULL;
1045 int err;
1046
Libin Yangac983792015-12-16 16:48:16 +08001047 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1048 if (pcm_idx < 0)
1049 return -EINVAL;
1050
Libin Yang42b29872015-12-16 13:42:42 +08001051 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1052 if (err)
1053 return err;
1054
1055 per_cvt = get_cvt(spec, cvt_idx);
1056 per_cvt->assigned = 1;
1057 hinfo->nid = per_cvt->cvt_nid;
1058
1059 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1060
Libin Yangac983792015-12-16 16:48:16 +08001061 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001062 /* todo: setup spdif ctls assign */
1063
1064 /* Initially set the converter's capabilities */
1065 hinfo->channels_min = per_cvt->channels_min;
1066 hinfo->channels_max = per_cvt->channels_max;
1067 hinfo->rates = per_cvt->rates;
1068 hinfo->formats = per_cvt->formats;
1069 hinfo->maxbps = per_cvt->maxbps;
1070
1071 /* Store the updated parameters */
1072 runtime->hw.channels_min = hinfo->channels_min;
1073 runtime->hw.channels_max = hinfo->channels_max;
1074 runtime->hw.formats = hinfo->formats;
1075 runtime->hw.rates = hinfo->rates;
1076
1077 snd_pcm_hw_constraint_step(substream->runtime, 0,
1078 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1079 return 0;
1080}
1081
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001082/*
1083 * HDA PCM callbacks
1084 */
1085static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1086 struct hda_codec *codec,
1087 struct snd_pcm_substream *substream)
1088{
1089 struct hdmi_spec *spec = codec->spec;
1090 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001091 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001092 struct hdmi_spec_per_pin *per_pin;
1093 struct hdmi_eld *eld;
1094 struct hdmi_spec_per_cvt *per_cvt = NULL;
1095 int err;
1096
1097 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001098 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1099 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001100 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001101
Libin Yang42b29872015-12-16 13:42:42 +08001102 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001103 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001104 if (!spec->dyn_pcm_assign) {
1105 if (snd_BUG_ON(pin_idx < 0)) {
1106 mutex_unlock(&spec->pcm_lock);
1107 return -EINVAL;
1108 }
1109 } else {
1110 /* no pin is assigned to the PCM
1111 * PA need pcm open successfully when probe
1112 */
1113 if (pin_idx < 0) {
1114 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1115 mutex_unlock(&spec->pcm_lock);
1116 return err;
1117 }
1118 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001119
1120 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001121 if (err < 0) {
1122 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001123 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001124 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001125
1126 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001127 /* Claim converter */
1128 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001129
Libin Yangac983792015-12-16 16:48:16 +08001130 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001131 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001132 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001133 hinfo->nid = per_cvt->cvt_nid;
1134
Takashi Iwaibddee962013-06-18 16:14:22 +02001135 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001136 AC_VERB_SET_CONNECT_SEL,
1137 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001138
1139 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001140 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001141 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001142
Libin Yang2bf3c852015-12-16 13:42:43 +08001143 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001144
Stephen Warren2def8172011-06-01 11:14:20 -06001145 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001146 hinfo->channels_min = per_cvt->channels_min;
1147 hinfo->channels_max = per_cvt->channels_max;
1148 hinfo->rates = per_cvt->rates;
1149 hinfo->formats = per_cvt->formats;
1150 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001151
Libin Yang42b29872015-12-16 13:42:42 +08001152 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001153 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001154 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001155 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001156 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001157 !hinfo->rates || !hinfo->formats) {
1158 per_cvt->assigned = 0;
1159 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001160 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001161 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001162 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001163 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001164 }
Stephen Warren2def8172011-06-01 11:14:20 -06001165
Libin Yang42b29872015-12-16 13:42:42 +08001166 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001167 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001168 runtime->hw.channels_min = hinfo->channels_min;
1169 runtime->hw.channels_max = hinfo->channels_max;
1170 runtime->hw.formats = hinfo->formats;
1171 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001172
1173 snd_pcm_hw_constraint_step(substream->runtime, 0,
1174 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001175 return 0;
1176}
1177
1178/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001179 * HDA/HDMI auto parsing
1180 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001181static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001182{
1183 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001184 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001185 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001186
1187 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001188 codec_warn(codec,
1189 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001190 pin_nid, get_wcaps(codec, pin_nid));
1191 return -EINVAL;
1192 }
1193
Stephen Warren384a48d2011-06-01 11:14:21 -06001194 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1195 per_pin->mux_nids,
1196 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001197
1198 return 0;
1199}
1200
Libin Yanga76056f2015-12-16 16:48:15 +08001201static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1202 struct hdmi_spec_per_pin *per_pin)
1203{
1204 int i;
1205
1206 /* try the prefer PCM */
1207 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1208 return per_pin->pin_nid_idx;
1209
1210 /* have a second try; check the "reserved area" over num_pins */
1211 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1212 if (!test_bit(i, &spec->pcm_bitmap))
1213 return i;
1214 }
1215
1216 /* the last try; check the empty slots in pins */
1217 for (i = 0; i < spec->num_pins; i++) {
1218 if (!test_bit(i, &spec->pcm_bitmap))
1219 return i;
1220 }
1221 return -EBUSY;
1222}
1223
1224static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1225 struct hdmi_spec_per_pin *per_pin)
1226{
1227 int idx;
1228
1229 /* pcm already be attached to the pin */
1230 if (per_pin->pcm)
1231 return;
1232 idx = hdmi_find_pcm_slot(spec, per_pin);
Libin Yangd10a80d2016-03-01 15:18:26 +08001233 if (idx == -EBUSY)
Libin Yanga76056f2015-12-16 16:48:15 +08001234 return;
1235 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001236 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001237 set_bit(idx, &spec->pcm_bitmap);
1238}
1239
1240static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1241 struct hdmi_spec_per_pin *per_pin)
1242{
1243 int idx;
1244
1245 /* pcm already be detached from the pin */
1246 if (!per_pin->pcm)
1247 return;
1248 idx = per_pin->pcm_idx;
1249 per_pin->pcm_idx = -1;
1250 per_pin->pcm = NULL;
1251 if (idx >= 0 && idx < spec->pcm_used)
1252 clear_bit(idx, &spec->pcm_bitmap);
1253}
1254
Libin Yangac983792015-12-16 16:48:16 +08001255static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1256 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1257{
1258 int mux_idx;
1259
1260 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1261 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1262 break;
1263 return mux_idx;
1264}
1265
1266static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1267
1268static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1269 struct hdmi_spec_per_pin *per_pin)
1270{
1271 struct hda_codec *codec = per_pin->codec;
1272 struct hda_pcm *pcm;
1273 struct hda_pcm_stream *hinfo;
1274 struct snd_pcm_substream *substream;
1275 int mux_idx;
1276 bool non_pcm;
1277
1278 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001279 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001280 else
1281 return;
1282 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1283 return;
1284
1285 /* hdmi audio only uses playback and one substream */
1286 hinfo = pcm->stream;
1287 substream = pcm->pcm->streams[0].substream;
1288
1289 per_pin->cvt_nid = hinfo->nid;
1290
1291 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1292 if (mux_idx < per_pin->num_mux_nids)
1293 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1294 AC_VERB_SET_CONNECT_SEL,
1295 mux_idx);
1296 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1297
1298 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1299 if (substream->runtime)
1300 per_pin->channels = substream->runtime->channels;
1301 per_pin->setup = true;
1302 per_pin->mux_idx = mux_idx;
1303
1304 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1305}
1306
1307static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1308 struct hdmi_spec_per_pin *per_pin)
1309{
1310 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1311 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1312
1313 per_pin->chmap_set = false;
1314 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1315
1316 per_pin->setup = false;
1317 per_pin->channels = 0;
1318}
1319
Takashi Iwaie90247f2015-11-13 09:12:12 +01001320/* update per_pin ELD from the given new ELD;
1321 * setup info frame and notification accordingly
1322 */
1323static void update_eld(struct hda_codec *codec,
1324 struct hdmi_spec_per_pin *per_pin,
1325 struct hdmi_eld *eld)
1326{
1327 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001328 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001329 bool old_eld_valid = pin_eld->eld_valid;
1330 bool eld_changed;
Libin Yangfb087ea2016-02-23 16:33:37 +08001331 int pcm_idx = -1;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001332
Libin Yangfb087ea2016-02-23 16:33:37 +08001333 /* for monitor disconnection, save pcm_idx firstly */
1334 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001335 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001336 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001337 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001338 hdmi_pcm_setup_pin(spec, per_pin);
1339 } else {
1340 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001341 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001342 }
Libin Yanga76056f2015-12-16 16:48:15 +08001343 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001344 /* if pcm_idx == -1, it means this is in monitor connection event
1345 * we can get the correct pcm_idx now.
1346 */
1347 if (pcm_idx == -1)
1348 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001349
Takashi Iwaie90247f2015-11-13 09:12:12 +01001350 if (eld->eld_valid)
1351 snd_hdmi_show_eld(codec, &eld->info);
1352
1353 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1354 if (eld->eld_valid && pin_eld->eld_valid)
1355 if (pin_eld->eld_size != eld->eld_size ||
1356 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1357 eld->eld_size) != 0)
1358 eld_changed = true;
1359
Takashi Iwaibd481282016-03-18 18:01:53 +01001360 pin_eld->monitor_present = eld->monitor_present;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001361 pin_eld->eld_valid = eld->eld_valid;
1362 pin_eld->eld_size = eld->eld_size;
1363 if (eld->eld_valid)
1364 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1365 pin_eld->info = eld->info;
1366
1367 /*
1368 * Re-setup pin and infoframe. This is needed e.g. when
1369 * - sink is first plugged-in
1370 * - transcoder can change during stream playback on Haswell
1371 * and this can make HW reset converter selection on a pin.
1372 */
1373 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1374 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1375 intel_verify_pin_cvt_connect(codec, per_pin);
1376 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1377 per_pin->mux_idx);
1378 }
1379
1380 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1381 }
1382
Libin Yangfb087ea2016-02-23 16:33:37 +08001383 if (eld_changed && pcm_idx >= 0)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001384 snd_ctl_notify(codec->card,
1385 SNDRV_CTL_EVENT_MASK_VALUE |
1386 SNDRV_CTL_EVENT_MASK_INFO,
Libin Yangfb087ea2016-02-23 16:33:37 +08001387 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001388}
1389
Takashi Iwai788d4412015-11-12 15:36:13 +01001390/* update ELD and jack state via HD-audio verbs */
1391static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1392 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001393{
David Henningsson464837a2013-11-07 13:38:25 +01001394 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001395 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001396 struct hdmi_spec *spec = codec->spec;
1397 struct hdmi_eld *eld = &spec->temp_eld;
1398 struct hdmi_eld *pin_eld = &per_pin->sink_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);
David Henningsson4bd038f2013-02-19 16:11:25 +01001415 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1416 if (pin_eld->monitor_present)
1417 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",
Mengdong Lin10250912013-03-28 05:21:28 -04001423 codec->addr, pin_nid, pin_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 Iwaiaff747eb2013-11-07 16:39:37 +01001443 ret = !repoll || !pin_eld->monitor_present || pin_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 Iwaia6866322016-03-21 12:18:33 +01002076static void generic_spec_free(struct hda_codec *codec)
2077{
2078 struct hdmi_spec *spec = codec->spec;
2079
2080 if (spec) {
2081 hdmi_array_free(spec);
2082 kfree(spec);
2083 codec->spec = NULL;
2084 }
2085 codec->dp_mst = false;
2086}
2087
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002088static void generic_hdmi_free(struct hda_codec *codec)
2089{
2090 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002091 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002092
Takashi Iwai66032492015-12-01 16:49:35 +01002093 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002094 snd_hdac_i915_register_notifier(NULL);
2095
Stephen Warren384a48d2011-06-01 11:14:21 -06002096 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002097 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002098 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002099 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002100 }
2101
2102 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2103 if (spec->pcm_rec[pcm_idx].jack == NULL)
2104 continue;
2105 if (spec->dyn_pcm_assign)
2106 snd_device_free(codec->card,
2107 spec->pcm_rec[pcm_idx].jack);
2108 else
2109 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002110 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002111
Takashi Iwaia6866322016-03-21 12:18:33 +01002112 generic_spec_free(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002113}
2114
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002115#ifdef CONFIG_PM
2116static int generic_hdmi_resume(struct hda_codec *codec)
2117{
2118 struct hdmi_spec *spec = codec->spec;
2119 int pin_idx;
2120
Pierre Ossmana2833682014-06-18 21:48:09 +02002121 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002122 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002123
2124 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2125 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2126 hdmi_present_sense(per_pin, 1);
2127 }
2128 return 0;
2129}
2130#endif
2131
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002132static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002133 .init = generic_hdmi_init,
2134 .free = generic_hdmi_free,
2135 .build_pcms = generic_hdmi_build_pcms,
2136 .build_controls = generic_hdmi_build_controls,
2137 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002138#ifdef CONFIG_PM
2139 .resume = generic_hdmi_resume,
2140#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002141};
2142
Anssi Hannula307229d2013-10-24 21:10:34 +03002143static const struct hdmi_ops generic_standard_hdmi_ops = {
2144 .pin_get_eld = snd_hdmi_get_eld,
Anssi Hannula307229d2013-10-24 21:10:34 +03002145 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2146 .pin_hbr_setup = hdmi_pin_hbr_setup,
2147 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302148};
2149
Takashi Iwaia6866322016-03-21 12:18:33 +01002150/* allocate codec->spec and assign/initialize generic parser ops */
2151static int alloc_generic_hdmi(struct hda_codec *codec)
2152{
2153 struct hdmi_spec *spec;
2154
2155 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2156 if (!spec)
2157 return -ENOMEM;
2158
2159 spec->ops = generic_standard_hdmi_ops;
2160 mutex_init(&spec->pcm_lock);
2161 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2162
2163 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2164 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2165 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2166
2167 codec->spec = spec;
2168 hdmi_array_init(spec, 4);
2169
2170 codec->patch_ops = generic_hdmi_patch_ops;
2171
2172 return 0;
2173}
2174
2175/* generic HDMI parser */
2176static int patch_generic_hdmi(struct hda_codec *codec)
2177{
2178 int err;
2179
2180 err = alloc_generic_hdmi(codec);
2181 if (err < 0)
2182 return err;
2183
2184 err = hdmi_parse_codec(codec);
2185 if (err < 0) {
2186 generic_spec_free(codec);
2187 return err;
2188 }
2189
2190 generic_hdmi_init_per_pins(codec);
2191 return 0;
2192}
2193
2194/*
2195 * Intel codec parsers and helpers
2196 */
2197
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002198static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2199 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002200{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002201 struct hdmi_spec *spec = codec->spec;
2202 hda_nid_t conns[4];
2203 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002204
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002205 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2206 if (nconns == spec->num_cvts &&
2207 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002208 return;
2209
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002210 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002211 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002212 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002213}
2214
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002215#define INTEL_VENDOR_NID 0x08
2216#define INTEL_GET_VENDOR_VERB 0xf81
2217#define INTEL_SET_VENDOR_VERB 0x781
2218#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2219#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2220
2221static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002222 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002223{
2224 unsigned int vendor_param;
2225
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002226 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2227 INTEL_GET_VENDOR_VERB, 0);
2228 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2229 return;
2230
2231 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2232 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2233 INTEL_SET_VENDOR_VERB, vendor_param);
2234 if (vendor_param == -1)
2235 return;
2236
Takashi Iwai17df3f52013-05-08 08:09:34 +02002237 if (update_tree)
2238 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002239}
2240
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002241static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2242{
2243 unsigned int vendor_param;
2244
2245 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2246 INTEL_GET_VENDOR_VERB, 0);
2247 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2248 return;
2249
2250 /* enable DP1.2 mode */
2251 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002252 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002253 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2254 INTEL_SET_VENDOR_VERB, vendor_param);
2255}
2256
Takashi Iwai17df3f52013-05-08 08:09:34 +02002257/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2258 * Otherwise you may get severe h/w communication errors.
2259 */
2260static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2261 unsigned int power_state)
2262{
2263 if (power_state == AC_PWRST_D0) {
2264 intel_haswell_enable_all_pins(codec, false);
2265 intel_haswell_fixup_enable_dp12(codec);
2266 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002267
Takashi Iwai17df3f52013-05-08 08:09:34 +02002268 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2269 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2270}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002271
David Henningssonf0675d42015-09-03 11:51:34 +02002272static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002273{
2274 struct hda_codec *codec = audio_ptr;
2275 int pin_nid = port + 0x04;
2276
Takashi Iwai4f8e4f32016-03-10 12:02:49 +01002277 /* we assume only from port-B to port-D */
2278 if (port < 1 || port > 3)
2279 return;
2280
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002281 /* skip notification during system suspend (but not in runtime PM);
2282 * the state will be updated at resume
2283 */
2284 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2285 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002286 /* ditto during suspend/resume process itself */
2287 if (atomic_read(&(codec)->core.in_pm))
2288 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002289
David Henningsson25adc132015-08-19 10:48:58 +02002290 check_presence_and_report(codec, pin_nid);
2291}
2292
Takashi Iwaia6866322016-03-21 12:18:33 +01002293/* register i915 component pin_eld_notify callback */
2294static void register_i915_notifier(struct hda_codec *codec)
2295{
2296 struct hdmi_spec *spec = codec->spec;
2297
2298 spec->use_acomp_notifier = true;
2299 spec->i915_audio_ops.audio_ptr = codec;
2300 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2301 * will call pin_eld_notify with using audio_ptr pointer
2302 * We need make sure audio_ptr is really setup
2303 */
2304 wmb();
2305 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2306 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2307}
2308
2309/* Intel Haswell and onwards; audio component with eld notifier */
2310static int patch_i915_hsw_hdmi(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002311{
2312 struct hdmi_spec *spec;
Takashi Iwaia6866322016-03-21 12:18:33 +01002313 int err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002314
Takashi Iwaia6866322016-03-21 12:18:33 +01002315 /* HSW+ requires i915 binding */
2316 if (!codec->bus->core.audio_component) {
2317 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2318 return -ENODEV;
Takashi Iwai691be972016-03-18 15:10:08 +01002319 }
Takashi Iwai55913112015-12-10 13:03:29 +01002320
Takashi Iwaia6866322016-03-21 12:18:33 +01002321 err = alloc_generic_hdmi(codec);
2322 if (err < 0)
2323 return err;
2324 spec = codec->spec;
2325
2326 intel_haswell_enable_all_pins(codec, true);
2327 intel_haswell_fixup_enable_dp12(codec);
2328
2329 /* For Haswell/Broadwell, the controller is also in the power well and
2330 * can cover the codec power request, and so need not set this flag.
2331 */
2332 if (!is_haswell(codec) && !is_broadwell(codec))
2333 codec->core.link_power_control = 1;
2334
2335 codec->patch_ops.set_power_state = haswell_set_power_state;
2336 codec->dp_mst = true;
2337 codec->depop_delay = 0;
2338 codec->auto_runtime_pm = 1;
2339
2340 err = hdmi_parse_codec(codec);
2341 if (err < 0) {
2342 generic_spec_free(codec);
2343 return err;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002344 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002345
Takashi Iwaia6866322016-03-21 12:18:33 +01002346 generic_hdmi_init_per_pins(codec);
2347 register_i915_notifier(codec);
2348 return 0;
2349}
2350
2351/* Intel Baytrail and Braswell; without get_eld notifier */
2352static int patch_i915_byt_hdmi(struct hda_codec *codec)
2353{
2354 struct hdmi_spec *spec;
2355 int err;
2356
2357 /* requires i915 binding */
2358 if (!codec->bus->core.audio_component) {
2359 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2360 return -ENODEV;
2361 }
2362
2363 err = alloc_generic_hdmi(codec);
2364 if (err < 0)
2365 return err;
2366 spec = codec->spec;
2367
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002368 /* For Valleyview/Cherryview, only the display codec is in the display
2369 * power well and can use link_power ops to request/release the power.
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002370 */
Takashi Iwaia6866322016-03-21 12:18:33 +01002371 codec->core.link_power_control = 1;
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002372
Takashi Iwaia6866322016-03-21 12:18:33 +01002373 codec->depop_delay = 0;
2374 codec->auto_runtime_pm = 1;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002375
Takashi Iwaia6866322016-03-21 12:18:33 +01002376 err = hdmi_parse_codec(codec);
2377 if (err < 0) {
2378 generic_spec_free(codec);
2379 return err;
2380 }
Lu, Han2377c3c2015-06-09 16:50:38 +08002381
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002382 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002383 return 0;
2384}
2385
2386/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002387 * Shared non-generic implementations
2388 */
2389
2390static int simple_playback_build_pcms(struct hda_codec *codec)
2391{
2392 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002393 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002394 unsigned int chans;
2395 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002396 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002397
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002398 per_cvt = get_cvt(spec, 0);
2399 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002400 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002401
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002402 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002403 if (!info)
2404 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002405 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002406 info->pcm_type = HDA_PCM_TYPE_HDMI;
2407 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2408 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002409 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002410 if (pstr->channels_max <= 2 && chans && chans <= 16)
2411 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002412
2413 return 0;
2414}
2415
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002416/* unsolicited event for jack sensing */
2417static void simple_hdmi_unsol_event(struct hda_codec *codec,
2418 unsigned int res)
2419{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002420 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002421 snd_hda_jack_report_sync(codec);
2422}
2423
2424/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2425 * as long as spec->pins[] is set correctly
2426 */
2427#define simple_hdmi_build_jack generic_hdmi_build_jack
2428
Stephen Warren3aaf8982011-06-01 11:14:19 -06002429static int simple_playback_build_controls(struct hda_codec *codec)
2430{
2431 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002432 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002433 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002434
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002435 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002436 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2437 per_cvt->cvt_nid,
2438 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002439 if (err < 0)
2440 return err;
2441 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002442}
2443
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002444static int simple_playback_init(struct hda_codec *codec)
2445{
2446 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002447 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2448 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002449
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002450 snd_hda_codec_write(codec, pin, 0,
2451 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2452 /* some codecs require to unmute the pin */
2453 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2454 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2455 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002456 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002457 return 0;
2458}
2459
Stephen Warren3aaf8982011-06-01 11:14:19 -06002460static void simple_playback_free(struct hda_codec *codec)
2461{
2462 struct hdmi_spec *spec = codec->spec;
2463
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002464 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002465 kfree(spec);
2466}
2467
2468/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002469 * Nvidia specific implementations
2470 */
2471
2472#define Nv_VERB_SET_Channel_Allocation 0xF79
2473#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2474#define Nv_VERB_SET_Audio_Protection_On 0xF98
2475#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2476
2477#define nvhdmi_master_con_nid_7x 0x04
2478#define nvhdmi_master_pin_nid_7x 0x05
2479
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002480static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002481 /*front, rear, clfe, rear_surr */
2482 0x6, 0x8, 0xa, 0xc,
2483};
2484
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002485static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2486 /* set audio protect on */
2487 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2488 /* enable digital output on pin widget */
2489 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2490 {} /* terminator */
2491};
2492
2493static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002494 /* set audio protect on */
2495 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2496 /* enable digital output on pin widget */
2497 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2498 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2499 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2500 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2501 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2502 {} /* terminator */
2503};
2504
2505#ifdef LIMITED_RATE_FMT_SUPPORT
2506/* support only the safe format and rate */
2507#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2508#define SUPPORTED_MAXBPS 16
2509#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2510#else
2511/* support all rates and formats */
2512#define SUPPORTED_RATES \
2513 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2514 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2515 SNDRV_PCM_RATE_192000)
2516#define SUPPORTED_MAXBPS 24
2517#define SUPPORTED_FORMATS \
2518 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2519#endif
2520
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002521static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002522{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002523 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2524 return 0;
2525}
2526
2527static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2528{
2529 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002530 return 0;
2531}
2532
Nitin Daga393004b2011-01-10 21:49:31 +05302533static unsigned int channels_2_6_8[] = {
2534 2, 6, 8
2535};
2536
2537static unsigned int channels_2_8[] = {
2538 2, 8
2539};
2540
2541static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2542 .count = ARRAY_SIZE(channels_2_6_8),
2543 .list = channels_2_6_8,
2544 .mask = 0,
2545};
2546
2547static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2548 .count = ARRAY_SIZE(channels_2_8),
2549 .list = channels_2_8,
2550 .mask = 0,
2551};
2552
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002553static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2554 struct hda_codec *codec,
2555 struct snd_pcm_substream *substream)
2556{
2557 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302558 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2559
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002560 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05302561 case 0x10de0002:
2562 case 0x10de0003:
2563 case 0x10de0005:
2564 case 0x10de0006:
2565 hw_constraints_channels = &hw_constraints_2_8_channels;
2566 break;
2567 case 0x10de0007:
2568 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2569 break;
2570 default:
2571 break;
2572 }
2573
2574 if (hw_constraints_channels != NULL) {
2575 snd_pcm_hw_constraint_list(substream->runtime, 0,
2576 SNDRV_PCM_HW_PARAM_CHANNELS,
2577 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002578 } else {
2579 snd_pcm_hw_constraint_step(substream->runtime, 0,
2580 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302581 }
2582
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002583 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2584}
2585
2586static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2587 struct hda_codec *codec,
2588 struct snd_pcm_substream *substream)
2589{
2590 struct hdmi_spec *spec = codec->spec;
2591 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2592}
2593
2594static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2595 struct hda_codec *codec,
2596 unsigned int stream_tag,
2597 unsigned int format,
2598 struct snd_pcm_substream *substream)
2599{
2600 struct hdmi_spec *spec = codec->spec;
2601 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2602 stream_tag, format, substream);
2603}
2604
Takashi Iwaid0b12522012-06-15 14:34:42 +02002605static const struct hda_pcm_stream simple_pcm_playback = {
2606 .substreams = 1,
2607 .channels_min = 2,
2608 .channels_max = 2,
2609 .ops = {
2610 .open = simple_playback_pcm_open,
2611 .close = simple_playback_pcm_close,
2612 .prepare = simple_playback_pcm_prepare
2613 },
2614};
2615
2616static const struct hda_codec_ops simple_hdmi_patch_ops = {
2617 .build_controls = simple_playback_build_controls,
2618 .build_pcms = simple_playback_build_pcms,
2619 .init = simple_playback_init,
2620 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002621 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002622};
2623
2624static int patch_simple_hdmi(struct hda_codec *codec,
2625 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2626{
2627 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002628 struct hdmi_spec_per_cvt *per_cvt;
2629 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002630
2631 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2632 if (!spec)
2633 return -ENOMEM;
2634
2635 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002636 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002637
2638 spec->multiout.num_dacs = 0; /* no analog */
2639 spec->multiout.max_channels = 2;
2640 spec->multiout.dig_out_nid = cvt_nid;
2641 spec->num_cvts = 1;
2642 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002643 per_pin = snd_array_new(&spec->pins);
2644 per_cvt = snd_array_new(&spec->cvts);
2645 if (!per_pin || !per_cvt) {
2646 simple_playback_free(codec);
2647 return -ENOMEM;
2648 }
2649 per_cvt->cvt_nid = cvt_nid;
2650 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002651 spec->pcm_playback = simple_pcm_playback;
2652
2653 codec->patch_ops = simple_hdmi_patch_ops;
2654
2655 return 0;
2656}
2657
Aaron Plattner1f348522011-04-06 17:19:04 -07002658static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2659 int channels)
2660{
2661 unsigned int chanmask;
2662 int chan = channels ? (channels - 1) : 1;
2663
2664 switch (channels) {
2665 default:
2666 case 0:
2667 case 2:
2668 chanmask = 0x00;
2669 break;
2670 case 4:
2671 chanmask = 0x08;
2672 break;
2673 case 6:
2674 chanmask = 0x0b;
2675 break;
2676 case 8:
2677 chanmask = 0x13;
2678 break;
2679 }
2680
2681 /* Set the audio infoframe channel allocation and checksum fields. The
2682 * channel count is computed implicitly by the hardware. */
2683 snd_hda_codec_write(codec, 0x1, 0,
2684 Nv_VERB_SET_Channel_Allocation, chanmask);
2685
2686 snd_hda_codec_write(codec, 0x1, 0,
2687 Nv_VERB_SET_Info_Frame_Checksum,
2688 (0x71 - chan - chanmask));
2689}
2690
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002691static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2692 struct hda_codec *codec,
2693 struct snd_pcm_substream *substream)
2694{
2695 struct hdmi_spec *spec = codec->spec;
2696 int i;
2697
2698 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2699 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2700 for (i = 0; i < 4; i++) {
2701 /* set the stream id */
2702 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2703 AC_VERB_SET_CHANNEL_STREAMID, 0);
2704 /* set the stream format */
2705 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2706 AC_VERB_SET_STREAM_FORMAT, 0);
2707 }
2708
Aaron Plattner1f348522011-04-06 17:19:04 -07002709 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2710 * streams are disabled. */
2711 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2712
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002713 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2714}
2715
2716static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2717 struct hda_codec *codec,
2718 unsigned int stream_tag,
2719 unsigned int format,
2720 struct snd_pcm_substream *substream)
2721{
2722 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002723 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002724 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002725 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002726 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002727 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002728
2729 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002730 per_cvt = get_cvt(spec, 0);
2731 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002732
2733 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002734
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002735 dataDCC2 = 0x2;
2736
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002737 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002738 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002739 snd_hda_codec_write(codec,
2740 nvhdmi_master_con_nid_7x,
2741 0,
2742 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002743 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002744
2745 /* set the stream id */
2746 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2747 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2748
2749 /* set the stream format */
2750 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2751 AC_VERB_SET_STREAM_FORMAT, format);
2752
2753 /* turn on again (if needed) */
2754 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002755 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002756 snd_hda_codec_write(codec,
2757 nvhdmi_master_con_nid_7x,
2758 0,
2759 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002760 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002761 snd_hda_codec_write(codec,
2762 nvhdmi_master_con_nid_7x,
2763 0,
2764 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2765 }
2766
2767 for (i = 0; i < 4; i++) {
2768 if (chs == 2)
2769 channel_id = 0;
2770 else
2771 channel_id = i * 2;
2772
2773 /* turn off SPDIF once;
2774 *otherwise the IEC958 bits won't be updated
2775 */
2776 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002777 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002778 snd_hda_codec_write(codec,
2779 nvhdmi_con_nids_7x[i],
2780 0,
2781 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002782 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002783 /* set the stream id */
2784 snd_hda_codec_write(codec,
2785 nvhdmi_con_nids_7x[i],
2786 0,
2787 AC_VERB_SET_CHANNEL_STREAMID,
2788 (stream_tag << 4) | channel_id);
2789 /* set the stream format */
2790 snd_hda_codec_write(codec,
2791 nvhdmi_con_nids_7x[i],
2792 0,
2793 AC_VERB_SET_STREAM_FORMAT,
2794 format);
2795 /* turn on again (if needed) */
2796 /* enable and set the channel status audio/data flag */
2797 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002798 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002799 snd_hda_codec_write(codec,
2800 nvhdmi_con_nids_7x[i],
2801 0,
2802 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002803 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002804 snd_hda_codec_write(codec,
2805 nvhdmi_con_nids_7x[i],
2806 0,
2807 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2808 }
2809 }
2810
Aaron Plattner1f348522011-04-06 17:19:04 -07002811 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002812
2813 mutex_unlock(&codec->spdif_mutex);
2814 return 0;
2815}
2816
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002817static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002818 .substreams = 1,
2819 .channels_min = 2,
2820 .channels_max = 8,
2821 .nid = nvhdmi_master_con_nid_7x,
2822 .rates = SUPPORTED_RATES,
2823 .maxbps = SUPPORTED_MAXBPS,
2824 .formats = SUPPORTED_FORMATS,
2825 .ops = {
2826 .open = simple_playback_pcm_open,
2827 .close = nvhdmi_8ch_7x_pcm_close,
2828 .prepare = nvhdmi_8ch_7x_pcm_prepare
2829 },
2830};
2831
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002832static int patch_nvhdmi_2ch(struct hda_codec *codec)
2833{
2834 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002835 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2836 nvhdmi_master_pin_nid_7x);
2837 if (err < 0)
2838 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002839
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002840 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002841 /* override the PCM rates, etc, as the codec doesn't give full list */
2842 spec = codec->spec;
2843 spec->pcm_playback.rates = SUPPORTED_RATES;
2844 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2845 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002846 return 0;
2847}
2848
Takashi Iwai53775b02012-08-01 12:17:41 +02002849static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2850{
2851 struct hdmi_spec *spec = codec->spec;
2852 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002853 if (!err) {
2854 struct hda_pcm *info = get_pcm_rec(spec, 0);
2855 info->own_chmap = true;
2856 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002857 return err;
2858}
2859
2860static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2861{
2862 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002863 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002864 struct snd_pcm_chmap *chmap;
2865 int err;
2866
2867 err = simple_playback_build_controls(codec);
2868 if (err < 0)
2869 return err;
2870
2871 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002872 info = get_pcm_rec(spec, 0);
2873 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002874 SNDRV_PCM_STREAM_PLAYBACK,
2875 snd_pcm_alt_chmaps, 8, 0, &chmap);
2876 if (err < 0)
2877 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002878 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02002879 case 0x10de0002:
2880 case 0x10de0003:
2881 case 0x10de0005:
2882 case 0x10de0006:
2883 chmap->channel_mask = (1U << 2) | (1U << 8);
2884 break;
2885 case 0x10de0007:
2886 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2887 }
2888 return 0;
2889}
2890
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002891static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2892{
2893 struct hdmi_spec *spec;
2894 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002895 if (err < 0)
2896 return err;
2897 spec = codec->spec;
2898 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002899 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002900 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002901 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2902 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002903
2904 /* Initialize the audio infoframe channel mask and checksum to something
2905 * valid */
2906 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2907
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002908 return 0;
2909}
2910
2911/*
Anssi Hannula611885b2013-11-03 17:15:00 +02002912 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
2913 * - 0x10de0015
2914 * - 0x10de0040
2915 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302916static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05302917 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02002918{
2919 if (cap->ca_index == 0x00 && channels == 2)
2920 return SNDRV_CTL_TLVT_CHMAP_FIXED;
2921
Subhransu S. Prusty028cb682016-03-14 10:35:06 +05302922 /* If the speaker allocation matches the channel count, it is OK. */
2923 if (cap->channels != channels)
2924 return -1;
2925
2926 /* all channels are remappable freely */
2927 return SNDRV_CTL_TLVT_CHMAP_VAR;
Anssi Hannula611885b2013-11-03 17:15:00 +02002928}
2929
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05302930static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
2931 int ca, int chs, unsigned char *map)
Anssi Hannula611885b2013-11-03 17:15:00 +02002932{
2933 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
2934 return -EINVAL;
2935
2936 return 0;
2937}
2938
2939static int patch_nvhdmi(struct hda_codec *codec)
2940{
2941 struct hdmi_spec *spec;
2942 int err;
2943
2944 err = patch_generic_hdmi(codec);
2945 if (err)
2946 return err;
2947
2948 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07002949 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02002950
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302951 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02002952 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302953 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02002954
2955 return 0;
2956}
2957
2958/*
Thierry Reding26e9a962015-05-05 14:56:20 +02002959 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
2960 * accessed using vendor-defined verbs. These registers can be used for
2961 * interoperability between the HDA and HDMI drivers.
2962 */
2963
2964/* Audio Function Group node */
2965#define NVIDIA_AFG_NID 0x01
2966
2967/*
2968 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
2969 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
2970 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
2971 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
2972 * additional bit (at position 30) to signal the validity of the format.
2973 *
2974 * | 31 | 30 | 29 16 | 15 0 |
2975 * +---------+-------+--------+--------+
2976 * | TRIGGER | VALID | UNUSED | FORMAT |
2977 * +-----------------------------------|
2978 *
2979 * Note that for the trigger bit to take effect it needs to change value
2980 * (i.e. it needs to be toggled).
2981 */
2982#define NVIDIA_GET_SCRATCH0 0xfa6
2983#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
2984#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
2985#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
2986#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
2987#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
2988#define NVIDIA_SCRATCH_VALID (1 << 6)
2989
2990#define NVIDIA_GET_SCRATCH1 0xfab
2991#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
2992#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
2993#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
2994#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
2995
2996/*
2997 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
2998 * the format is invalidated so that the HDMI codec can be disabled.
2999 */
3000static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3001{
3002 unsigned int value;
3003
3004 /* bits [31:30] contain the trigger and valid bits */
3005 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3006 NVIDIA_GET_SCRATCH0, 0);
3007 value = (value >> 24) & 0xff;
3008
3009 /* bits [15:0] are used to store the HDA format */
3010 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3011 NVIDIA_SET_SCRATCH0_BYTE0,
3012 (format >> 0) & 0xff);
3013 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3014 NVIDIA_SET_SCRATCH0_BYTE1,
3015 (format >> 8) & 0xff);
3016
3017 /* bits [16:24] are unused */
3018 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3019 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3020
3021 /*
3022 * Bit 30 signals that the data is valid and hence that HDMI audio can
3023 * be enabled.
3024 */
3025 if (format == 0)
3026 value &= ~NVIDIA_SCRATCH_VALID;
3027 else
3028 value |= NVIDIA_SCRATCH_VALID;
3029
3030 /*
3031 * Whenever the trigger bit is toggled, an interrupt is raised in the
3032 * HDMI codec. The HDMI driver will use that as trigger to update its
3033 * configuration.
3034 */
3035 value ^= NVIDIA_SCRATCH_TRIGGER;
3036
3037 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3038 NVIDIA_SET_SCRATCH0_BYTE3, value);
3039}
3040
3041static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3042 struct hda_codec *codec,
3043 unsigned int stream_tag,
3044 unsigned int format,
3045 struct snd_pcm_substream *substream)
3046{
3047 int err;
3048
3049 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3050 format, substream);
3051 if (err < 0)
3052 return err;
3053
3054 /* notify the HDMI codec of the format change */
3055 tegra_hdmi_set_format(codec, format);
3056
3057 return 0;
3058}
3059
3060static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3061 struct hda_codec *codec,
3062 struct snd_pcm_substream *substream)
3063{
3064 /* invalidate the format in the HDMI codec */
3065 tegra_hdmi_set_format(codec, 0);
3066
3067 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3068}
3069
3070static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3071{
3072 struct hdmi_spec *spec = codec->spec;
3073 unsigned int i;
3074
3075 for (i = 0; i < spec->num_pins; i++) {
3076 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3077
3078 if (pcm->pcm_type == type)
3079 return pcm;
3080 }
3081
3082 return NULL;
3083}
3084
3085static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3086{
3087 struct hda_pcm_stream *stream;
3088 struct hda_pcm *pcm;
3089 int err;
3090
3091 err = generic_hdmi_build_pcms(codec);
3092 if (err < 0)
3093 return err;
3094
3095 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3096 if (!pcm)
3097 return -ENODEV;
3098
3099 /*
3100 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3101 * codec about format changes.
3102 */
3103 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3104 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3105 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3106
3107 return 0;
3108}
3109
3110static int patch_tegra_hdmi(struct hda_codec *codec)
3111{
3112 int err;
3113
3114 err = patch_generic_hdmi(codec);
3115 if (err)
3116 return err;
3117
3118 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3119
3120 return 0;
3121}
3122
3123/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003124 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003125 */
3126
Anssi Hannula5a6135842013-10-24 21:10:35 +03003127#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003128 ((codec)->core.vendor_id == 0x1002aa01 && \
3129 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003130#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003131
Anssi Hannula5a6135842013-10-24 21:10:35 +03003132/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3133#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3134#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3135#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3136#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3137#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3138#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003139#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003140#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3141#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3142#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3143#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3144#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3145#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3146#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3147#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3148#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3149#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3150#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003151#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003152#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3153#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3154#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3155#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3156#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3157
Anssi Hannula84d69e72013-10-24 21:10:38 +03003158/* AMD specific HDA cvt verbs */
3159#define ATI_VERB_SET_RAMP_RATE 0x770
3160#define ATI_VERB_GET_RAMP_RATE 0xf70
3161
Anssi Hannula5a6135842013-10-24 21:10:35 +03003162#define ATI_OUT_ENABLE 0x1
3163
3164#define ATI_MULTICHANNEL_MODE_PAIRED 0
3165#define ATI_MULTICHANNEL_MODE_SINGLE 1
3166
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003167#define ATI_HBR_CAPABLE 0x01
3168#define ATI_HBR_ENABLE 0x10
3169
Anssi Hannula89250f82013-10-24 21:10:36 +03003170static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3171 unsigned char *buf, int *eld_size)
3172{
3173 /* call hda_eld.c ATI/AMD-specific function */
3174 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3175 is_amdhdmi_rev3_or_later(codec));
3176}
3177
Anssi Hannula5a6135842013-10-24 21:10:35 +03003178static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3179 int active_channels, int conn_type)
3180{
3181 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3182}
3183
3184static int atihdmi_paired_swap_fc_lfe(int pos)
3185{
3186 /*
3187 * ATI/AMD have automatic FC/LFE swap built-in
3188 * when in pairwise mapping mode.
3189 */
3190
3191 switch (pos) {
3192 /* see channel_allocations[].speakers[] */
3193 case 2: return 3;
3194 case 3: return 2;
3195 default: break;
3196 }
3197
3198 return pos;
3199}
3200
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303201static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3202 int ca, int chs, unsigned char *map)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003203{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303204 struct hdac_cea_channel_speaker_allocation *cap;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003205 int i, j;
3206
3207 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3208
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303209 cap = snd_hdac_get_ch_alloc_from_ca(ca);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003210 for (i = 0; i < chs; ++i) {
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303211 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003212 bool ok = false;
3213 bool companion_ok = false;
3214
3215 if (!mask)
3216 continue;
3217
3218 for (j = 0 + i % 2; j < 8; j += 2) {
3219 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3220 if (cap->speakers[chan_idx] == mask) {
3221 /* channel is in a supported position */
3222 ok = true;
3223
3224 if (i % 2 == 0 && i + 1 < chs) {
3225 /* even channel, check the odd companion */
3226 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303227 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003228 int comp_mask_act = cap->speakers[comp_chan_idx];
3229
3230 if (comp_mask_req == comp_mask_act)
3231 companion_ok = true;
3232 else
3233 return -EINVAL;
3234 }
3235 break;
3236 }
3237 }
3238
3239 if (!ok)
3240 return -EINVAL;
3241
3242 if (companion_ok)
3243 i++; /* companion channel already checked */
3244 }
3245
3246 return 0;
3247}
3248
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303249static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3250 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003251{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303252 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003253 int verb;
3254 int ati_channel_setup = 0;
3255
3256 if (hdmi_slot > 7)
3257 return -EINVAL;
3258
3259 if (!has_amd_full_remap_support(codec)) {
3260 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3261
3262 /* In case this is an odd slot but without stream channel, do not
3263 * disable the slot since the corresponding even slot could have a
3264 * channel. In case neither have a channel, the slot pair will be
3265 * disabled when this function is called for the even slot. */
3266 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3267 return 0;
3268
3269 hdmi_slot -= hdmi_slot % 2;
3270
3271 if (stream_channel != 0xf)
3272 stream_channel -= stream_channel % 2;
3273 }
3274
3275 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3276
3277 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3278
3279 if (stream_channel != 0xf)
3280 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3281
3282 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3283}
3284
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303285static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3286 hda_nid_t pin_nid, int asp_slot)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003287{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303288 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003289 bool was_odd = false;
3290 int ati_asp_slot = asp_slot;
3291 int verb;
3292 int ati_channel_setup;
3293
3294 if (asp_slot > 7)
3295 return -EINVAL;
3296
3297 if (!has_amd_full_remap_support(codec)) {
3298 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3299 if (ati_asp_slot % 2 != 0) {
3300 ati_asp_slot -= 1;
3301 was_odd = true;
3302 }
3303 }
3304
3305 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3306
3307 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3308
3309 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3310 return 0xf;
3311
3312 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3313}
3314
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303315static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3316 struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303317 struct hdac_cea_channel_speaker_allocation *cap,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303318 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003319{
3320 int c;
3321
3322 /*
3323 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3324 * we need to take that into account (a single channel may take 2
3325 * channel slots if we need to carry a silent channel next to it).
3326 * On Rev3+ AMD codecs this function is not used.
3327 */
3328 int chanpairs = 0;
3329
3330 /* We only produce even-numbered channel count TLVs */
3331 if ((channels % 2) != 0)
3332 return -1;
3333
3334 for (c = 0; c < 7; c += 2) {
3335 if (cap->speakers[c] || cap->speakers[c+1])
3336 chanpairs++;
3337 }
3338
3339 if (chanpairs * 2 != channels)
3340 return -1;
3341
3342 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3343}
3344
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303345static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303346 struct hdac_cea_channel_speaker_allocation *cap,
3347 unsigned int *chmap, int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003348{
3349 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3350 int count = 0;
3351 int c;
3352
3353 for (c = 7; c >= 0; c--) {
3354 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3355 int spk = cap->speakers[chan];
3356 if (!spk) {
3357 /* add N/A channel if the companion channel is occupied */
3358 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3359 chmap[count++] = SNDRV_CHMAP_NA;
3360
3361 continue;
3362 }
3363
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303364 chmap[count++] = snd_hdac_spk_to_chmap(spk);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003365 }
3366
3367 WARN_ON(count != channels);
3368}
3369
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003370static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3371 bool hbr)
3372{
3373 int hbr_ctl, hbr_ctl_new;
3374
3375 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003376 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003377 if (hbr)
3378 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3379 else
3380 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3381
Takashi Iwai4e76a882014-02-25 12:21:03 +01003382 codec_dbg(codec,
3383 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003384 pin_nid,
3385 hbr_ctl == hbr_ctl_new ? "" : "new-",
3386 hbr_ctl_new);
3387
3388 if (hbr_ctl != hbr_ctl_new)
3389 snd_hda_codec_write(codec, pin_nid, 0,
3390 ATI_VERB_SET_HBR_CONTROL,
3391 hbr_ctl_new);
3392
3393 } else if (hbr)
3394 return -EINVAL;
3395
3396 return 0;
3397}
3398
Anssi Hannula84d69e72013-10-24 21:10:38 +03003399static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3400 hda_nid_t pin_nid, u32 stream_tag, int format)
3401{
3402
3403 if (is_amdhdmi_rev3_or_later(codec)) {
3404 int ramp_rate = 180; /* default as per AMD spec */
3405 /* disable ramp-up/down for non-pcm as per AMD spec */
3406 if (format & AC_FMT_TYPE_NON_PCM)
3407 ramp_rate = 0;
3408
3409 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3410 }
3411
3412 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3413}
3414
3415
Anssi Hannula5a6135842013-10-24 21:10:35 +03003416static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003417{
3418 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003419 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003420
Anssi Hannula5a6135842013-10-24 21:10:35 +03003421 err = generic_hdmi_init(codec);
3422
3423 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003424 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003425
3426 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3427 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3428
3429 /* make sure downmix information in infoframe is zero */
3430 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3431
3432 /* enable channel-wise remap mode if supported */
3433 if (has_amd_full_remap_support(codec))
3434 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3435 ATI_VERB_SET_MULTICHANNEL_MODE,
3436 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003437 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003438
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003439 return 0;
3440}
3441
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003442static int patch_atihdmi(struct hda_codec *codec)
3443{
3444 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003445 struct hdmi_spec_per_cvt *per_cvt;
3446 int err, cvt_idx;
3447
3448 err = patch_generic_hdmi(codec);
3449
3450 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003451 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003452
3453 codec->patch_ops.init = atihdmi_init;
3454
Takashi Iwaid0b12522012-06-15 14:34:42 +02003455 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003456
Anssi Hannula89250f82013-10-24 21:10:36 +03003457 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003458 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003459 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003460 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003461
3462 if (!has_amd_full_remap_support(codec)) {
3463 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303464 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03003465 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303466 spec->chmap.ops.cea_alloc_to_tlv_chmap =
3467 atihdmi_paired_cea_alloc_to_tlv_chmap;
3468 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303469 spec->chmap.ops.pin_get_slot_channel =
3470 atihdmi_pin_get_slot_channel;
3471 spec->chmap.ops.pin_set_slot_channel =
3472 atihdmi_pin_set_slot_channel;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003473 }
3474
3475 /* ATI/AMD converters do not advertise all of their capabilities */
3476 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3477 per_cvt = get_cvt(spec, cvt_idx);
3478 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3479 per_cvt->rates |= SUPPORTED_RATES;
3480 per_cvt->formats |= SUPPORTED_FORMATS;
3481 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3482 }
3483
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303484 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003485
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003486 return 0;
3487}
3488
Annie Liu3de5ff82012-06-08 19:18:42 +08003489/* VIA HDMI Implementation */
3490#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3491#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3492
Annie Liu3de5ff82012-06-08 19:18:42 +08003493static int patch_via_hdmi(struct hda_codec *codec)
3494{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003495 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003496}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003497
3498/*
3499 * patch entries
3500 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003501static const struct hda_device_id snd_hda_id_hdmi[] = {
3502HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3503HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3504HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3505HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3506HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3507HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3508HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3509HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3510HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3511HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3512HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3513HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3514HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3515HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3516HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3517HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3518HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3519HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3520HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3521HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3522HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3523HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3524HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01003525/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003526HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3527HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3528HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3529HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3530HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3531HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3532HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3533HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3534HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3535HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3536HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3537HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3538HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3539HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3540HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3541HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3542HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3543HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3544HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3545HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3546HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Aaron Plattner2d369c72016-03-13 13:58:57 -07003547HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08003548HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003549HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3550HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3551HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3552HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3553HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3554HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
3555HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3556HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3557HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3558HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
3559HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
3560HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01003561HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
3562HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
3563HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
3564HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
3565HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003566HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01003567HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
3568HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003569HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003570/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003571HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003572{} /* terminator */
3573};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003574MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003575
3576MODULE_LICENSE("GPL");
3577MODULE_DESCRIPTION("HDMI HD-audio codec");
3578MODULE_ALIAS("snd-hda-codec-intelhdmi");
3579MODULE_ALIAS("snd-hda-codec-nvhdmi");
3580MODULE_ALIAS("snd-hda-codec-atihdmi");
3581
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003582static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003583 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003584};
3585
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003586module_hda_codec_driver(hdmi_driver);