blob: fcd207d3ce7d3ec690361ae073be2d77e1bed607 [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
Takashi Iwai44bb6d02016-03-21 12:36:44 +0100686 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
687 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
Mengdong Lin58f7d282013-09-04 16:37:12 -0400688 snd_hda_codec_write(codec, pin_nid, 0,
689 AC_VERB_SET_AMP_GAIN_MUTE,
690 AMP_OUT_UNMUTE);
691
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100692 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800693
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530694 ca = snd_hdac_channel_allocation(&codec->core,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530695 eld->info.spk_alloc, channels,
696 per_pin->chmap_set, non_pcm, per_pin->chmap);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800697
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530698 active_channels = snd_hdac_get_active_channels(ca);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300699
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530700 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
701 active_channels);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300702
Stephen Warren384a48d2011-06-01 11:14:21 -0600703 /*
Anssi Hannula39edac72013-10-07 19:24:52 +0300704 * always configure channel mapping, it may have been changed by the
705 * user in the meantime
706 */
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530707 snd_hdac_setup_channel_mapping(&spec->chmap,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530708 pin_nid, non_pcm, ca, channels,
709 per_pin->chmap, per_pin->chmap_set);
Anssi Hannula39edac72013-10-07 19:24:52 +0300710
Anssi Hannula307229d2013-10-24 21:10:34 +0300711 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
712 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +0800713
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200714 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800715}
716
Wu Fengguang079d88c2010-03-08 10:44:23 +0800717/*
718 * Unsolicited events
719 */
720
Takashi Iwaiefe47102013-11-07 13:38:23 +0100721static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200722
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200723static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800724{
725 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200726 int pin_idx = pin_nid_to_pin_index(codec, nid);
727
David Henningsson20ce9022013-12-04 10:19:41 +0800728 if (pin_idx < 0)
729 return;
David Henningsson20ce9022013-12-04 10:19:41 +0800730 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
731 snd_hda_jack_report_sync(codec);
732}
733
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200734static void jack_callback(struct hda_codec *codec,
735 struct hda_jack_callback *jack)
736{
Takashi Iwai2ebab402016-02-09 10:23:52 +0100737 check_presence_and_report(codec, jack->nid);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200738}
739
David Henningsson20ce9022013-12-04 10:19:41 +0800740static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
741{
Takashi Iwai3a938972011-10-28 01:16:55 +0200742 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200743 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400744 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200745
746 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
747 if (!jack)
748 return;
Takashi Iwai3a938972011-10-28 01:16:55 +0200749 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800750
Takashi Iwai4e76a882014-02-25 12:21:03 +0100751 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400752 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
David Henningsson20ce9022013-12-04 10:19:41 +0800753 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800754 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800755
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200756 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800757}
758
759static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
760{
761 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
762 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
763 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
764 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
765
Takashi Iwai4e76a882014-02-25 12:21:03 +0100766 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200767 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600768 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800769 tag,
770 subtag,
771 cp_state,
772 cp_ready);
773
774 /* TODO */
775 if (cp_state)
776 ;
777 if (cp_ready)
778 ;
779}
780
781
782static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
783{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800784 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
785 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
786
Takashi Iwai3a938972011-10-28 01:16:55 +0200787 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100788 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800789 return;
790 }
791
792 if (subtag == 0)
793 hdmi_intrinsic_event(codec, res);
794 else
795 hdmi_non_intrinsic_event(codec, res);
796}
797
Mengdong Lin58f7d282013-09-04 16:37:12 -0400798static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +0800799 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +0200800{
Mengdong Lin58f7d282013-09-04 16:37:12 -0400801 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +0200802
Wang Xingchao53b434f2013-06-18 10:41:53 +0800803 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
804 * thus pins could only choose converter 0 for use. Make sure the
805 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200806 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +0800807 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
808
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200809 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +0200810 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
811 AC_PWRST_D0);
812 msleep(40);
813 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
814 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100815 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
David Henningsson83f26ad2013-04-10 12:26:07 +0200816 }
David Henningsson83f26ad2013-04-10 12:26:07 +0200817}
818
Wu Fengguang079d88c2010-03-08 10:44:23 +0800819/*
820 * Callbacks
821 */
822
Takashi Iwai92f10b32010-08-03 14:21:00 +0200823/* HBR should be Non-PCM, 8 channels */
824#define is_hbr_format(format) \
825 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
826
Anssi Hannula307229d2013-10-24 21:10:34 +0300827static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
828 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800829{
Anssi Hannula307229d2013-10-24 21:10:34 +0300830 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +0200831
Stephen Warren384a48d2011-06-01 11:14:21 -0600832 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
833 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300834 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
835
Anssi Hannula13122e62013-11-10 20:56:10 +0200836 if (pinctl < 0)
837 return hbr ? -EINVAL : 0;
838
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300839 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +0300840 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300841 new_pinctl |= AC_PINCTL_EPT_HBR;
842 else
843 new_pinctl |= AC_PINCTL_EPT_NATIVE;
844
Takashi Iwai4e76a882014-02-25 12:21:03 +0100845 codec_dbg(codec,
846 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600847 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300848 pinctl == new_pinctl ? "" : "new-",
849 new_pinctl);
850
851 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -0600852 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300853 AC_VERB_SET_PIN_WIDGET_CONTROL,
854 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +0300855 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300856 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +0300857
858 return 0;
859}
860
861static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
862 hda_nid_t pin_nid, u32 stream_tag, int format)
863{
864 struct hdmi_spec *spec = codec->spec;
865 int err;
866
Mengdong Lin75dcbe42014-01-08 15:55:32 -0500867 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +0300868 haswell_verify_D0(codec, cvt_nid, pin_nid);
869
870 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
871
872 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100873 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +0300874 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300875 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800876
Stephen Warren384a48d2011-06-01 11:14:21 -0600877 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300878 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800879}
880
Libin Yang42b29872015-12-16 13:42:42 +0800881/* Try to find an available converter
882 * If pin_idx is less then zero, just try to find an available converter.
883 * Otherwise, try to find an available converter and get the cvt mux index
884 * of the pin.
885 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800886static int hdmi_choose_cvt(struct hda_codec *codec,
887 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200888{
889 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600890 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600891 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800892 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200893
Libin Yang42b29872015-12-16 13:42:42 +0800894 /* pin_idx < 0 means no pin will be bound to the converter */
895 if (pin_idx < 0)
896 per_pin = NULL;
897 else
898 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200899
Stephen Warren384a48d2011-06-01 11:14:21 -0600900 /* Dynamically assign converter to stream */
901 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100902 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -0600903
904 /* Must not already be assigned */
905 if (per_cvt->assigned)
906 continue;
Libin Yang42b29872015-12-16 13:42:42 +0800907 if (per_pin == NULL)
908 break;
Stephen Warren384a48d2011-06-01 11:14:21 -0600909 /* Must be in pin's mux's list of converters */
910 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
911 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
912 break;
913 /* Not in mux list */
914 if (mux_idx == per_pin->num_mux_nids)
915 continue;
916 break;
917 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800918
Stephen Warren384a48d2011-06-01 11:14:21 -0600919 /* No free converters */
920 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +0800921 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -0600922
Libin Yang42b29872015-12-16 13:42:42 +0800923 if (per_pin != NULL)
924 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +0800925
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800926 if (cvt_id)
927 *cvt_id = cvt_idx;
928 if (mux_id)
929 *mux_id = mux_idx;
930
931 return 0;
932}
933
Mengdong Lin2df67422014-03-20 13:01:06 +0800934/* Assure the pin select the right convetor */
935static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
936 struct hdmi_spec_per_pin *per_pin)
937{
938 hda_nid_t pin_nid = per_pin->pin_nid;
939 int mux_idx, curr;
940
941 mux_idx = per_pin->mux_idx;
942 curr = snd_hda_codec_read(codec, pin_nid, 0,
943 AC_VERB_GET_CONNECT_SEL, 0);
944 if (curr != mux_idx)
945 snd_hda_codec_write_cache(codec, pin_nid, 0,
946 AC_VERB_SET_CONNECT_SEL,
947 mux_idx);
948}
949
Libin Yang42b29872015-12-16 13:42:42 +0800950/* get the mux index for the converter of the pins
951 * converter's mux index is the same for all pins on Intel platform
952 */
953static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
954 hda_nid_t cvt_nid)
955{
956 int i;
957
958 for (i = 0; i < spec->num_cvts; i++)
959 if (spec->cvt_nids[i] == cvt_nid)
960 return i;
961 return -EINVAL;
962}
963
Mengdong Lin300016b2013-11-04 01:13:13 -0500964/* Intel HDMI workaround to fix audio routing issue:
965 * For some Intel display codecs, pins share the same connection list.
966 * So a conveter can be selected by multiple pins and playback on any of these
967 * pins will generate sound on the external display, because audio flows from
968 * the same converter to the display pipeline. Also muting one pin may make
969 * other pins have no sound output.
970 * So this function assures that an assigned converter for a pin is not selected
971 * by any other pins.
972 */
973static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -0400974 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800975{
976 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +0100977 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -0400978 int cvt_idx, curr;
979 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800980
Mengdong Linf82d7d12013-09-21 20:34:45 -0400981 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +0100982 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -0400983 unsigned int wid_caps = get_wcaps(codec, nid);
984 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800985
Mengdong Linf82d7d12013-09-21 20:34:45 -0400986 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800987 continue;
988
Mengdong Linf82d7d12013-09-21 20:34:45 -0400989 if (nid == pin_nid)
990 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800991
Mengdong Linf82d7d12013-09-21 20:34:45 -0400992 curr = snd_hda_codec_read(codec, nid, 0,
993 AC_VERB_GET_CONNECT_SEL, 0);
994 if (curr != mux_idx)
995 continue;
996
997 /* choose an unassigned converter. The conveters in the
998 * connection list are in the same order as in the codec.
999 */
1000 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1001 per_cvt = get_cvt(spec, cvt_idx);
1002 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001003 codec_dbg(codec,
1004 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001005 cvt_idx, nid);
1006 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001007 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001008 cvt_idx);
1009 break;
1010 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001011 }
1012 }
1013}
1014
Libin Yang42b29872015-12-16 13:42:42 +08001015/* A wrapper of intel_not_share_asigned_cvt() */
1016static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1017 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1018{
1019 int mux_idx;
1020 struct hdmi_spec *spec = codec->spec;
1021
1022 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1023 return;
1024
1025 /* On Intel platform, the mapping of converter nid to
1026 * mux index of the pins are always the same.
1027 * The pin nid may be 0, this means all pins will not
1028 * share the converter.
1029 */
1030 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1031 if (mux_idx >= 0)
1032 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1033}
1034
1035/* called in hdmi_pcm_open when no pin is assigned to the PCM
1036 * in dyn_pcm_assign mode.
1037 */
1038static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1039 struct hda_codec *codec,
1040 struct snd_pcm_substream *substream)
1041{
1042 struct hdmi_spec *spec = codec->spec;
1043 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001044 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001045 struct hdmi_spec_per_cvt *per_cvt = NULL;
1046 int err;
1047
Libin Yangac983792015-12-16 16:48:16 +08001048 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1049 if (pcm_idx < 0)
1050 return -EINVAL;
1051
Libin Yang42b29872015-12-16 13:42:42 +08001052 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1053 if (err)
1054 return err;
1055
1056 per_cvt = get_cvt(spec, cvt_idx);
1057 per_cvt->assigned = 1;
1058 hinfo->nid = per_cvt->cvt_nid;
1059
1060 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1061
Libin Yangac983792015-12-16 16:48:16 +08001062 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001063 /* todo: setup spdif ctls assign */
1064
1065 /* Initially set the converter's capabilities */
1066 hinfo->channels_min = per_cvt->channels_min;
1067 hinfo->channels_max = per_cvt->channels_max;
1068 hinfo->rates = per_cvt->rates;
1069 hinfo->formats = per_cvt->formats;
1070 hinfo->maxbps = per_cvt->maxbps;
1071
1072 /* Store the updated parameters */
1073 runtime->hw.channels_min = hinfo->channels_min;
1074 runtime->hw.channels_max = hinfo->channels_max;
1075 runtime->hw.formats = hinfo->formats;
1076 runtime->hw.rates = hinfo->rates;
1077
1078 snd_pcm_hw_constraint_step(substream->runtime, 0,
1079 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1080 return 0;
1081}
1082
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001083/*
1084 * HDA PCM callbacks
1085 */
1086static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1087 struct hda_codec *codec,
1088 struct snd_pcm_substream *substream)
1089{
1090 struct hdmi_spec *spec = codec->spec;
1091 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001092 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001093 struct hdmi_spec_per_pin *per_pin;
1094 struct hdmi_eld *eld;
1095 struct hdmi_spec_per_cvt *per_cvt = NULL;
1096 int err;
1097
1098 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001099 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1100 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001101 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001102
Libin Yang42b29872015-12-16 13:42:42 +08001103 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001104 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001105 if (!spec->dyn_pcm_assign) {
1106 if (snd_BUG_ON(pin_idx < 0)) {
1107 mutex_unlock(&spec->pcm_lock);
1108 return -EINVAL;
1109 }
1110 } else {
1111 /* no pin is assigned to the PCM
1112 * PA need pcm open successfully when probe
1113 */
1114 if (pin_idx < 0) {
1115 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1116 mutex_unlock(&spec->pcm_lock);
1117 return err;
1118 }
1119 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001120
1121 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001122 if (err < 0) {
1123 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001124 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001125 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001126
1127 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001128 /* Claim converter */
1129 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001130
Libin Yangac983792015-12-16 16:48:16 +08001131 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001132 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001133 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001134 hinfo->nid = per_cvt->cvt_nid;
1135
Takashi Iwaibddee962013-06-18 16:14:22 +02001136 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001137 AC_VERB_SET_CONNECT_SEL,
1138 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001139
1140 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001141 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001142 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001143
Libin Yang2bf3c852015-12-16 13:42:43 +08001144 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001145
Stephen Warren2def8172011-06-01 11:14:20 -06001146 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001147 hinfo->channels_min = per_cvt->channels_min;
1148 hinfo->channels_max = per_cvt->channels_max;
1149 hinfo->rates = per_cvt->rates;
1150 hinfo->formats = per_cvt->formats;
1151 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001152
Libin Yang42b29872015-12-16 13:42:42 +08001153 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001154 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001155 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001156 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001157 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001158 !hinfo->rates || !hinfo->formats) {
1159 per_cvt->assigned = 0;
1160 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001161 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001162 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001163 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001164 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001165 }
Stephen Warren2def8172011-06-01 11:14:20 -06001166
Libin Yang42b29872015-12-16 13:42:42 +08001167 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001168 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001169 runtime->hw.channels_min = hinfo->channels_min;
1170 runtime->hw.channels_max = hinfo->channels_max;
1171 runtime->hw.formats = hinfo->formats;
1172 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001173
1174 snd_pcm_hw_constraint_step(substream->runtime, 0,
1175 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001176 return 0;
1177}
1178
1179/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001180 * HDA/HDMI auto parsing
1181 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001182static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001183{
1184 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001185 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001186 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001187
1188 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001189 codec_warn(codec,
1190 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001191 pin_nid, get_wcaps(codec, pin_nid));
1192 return -EINVAL;
1193 }
1194
Stephen Warren384a48d2011-06-01 11:14:21 -06001195 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1196 per_pin->mux_nids,
1197 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001198
1199 return 0;
1200}
1201
Libin Yanga76056f2015-12-16 16:48:15 +08001202static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1203 struct hdmi_spec_per_pin *per_pin)
1204{
1205 int i;
1206
1207 /* try the prefer PCM */
1208 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1209 return per_pin->pin_nid_idx;
1210
1211 /* have a second try; check the "reserved area" over num_pins */
1212 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1213 if (!test_bit(i, &spec->pcm_bitmap))
1214 return i;
1215 }
1216
1217 /* the last try; check the empty slots in pins */
1218 for (i = 0; i < spec->num_pins; i++) {
1219 if (!test_bit(i, &spec->pcm_bitmap))
1220 return i;
1221 }
1222 return -EBUSY;
1223}
1224
1225static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1226 struct hdmi_spec_per_pin *per_pin)
1227{
1228 int idx;
1229
1230 /* pcm already be attached to the pin */
1231 if (per_pin->pcm)
1232 return;
1233 idx = hdmi_find_pcm_slot(spec, per_pin);
Libin Yangd10a80d2016-03-01 15:18:26 +08001234 if (idx == -EBUSY)
Libin Yanga76056f2015-12-16 16:48:15 +08001235 return;
1236 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001237 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001238 set_bit(idx, &spec->pcm_bitmap);
1239}
1240
1241static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1242 struct hdmi_spec_per_pin *per_pin)
1243{
1244 int idx;
1245
1246 /* pcm already be detached from the pin */
1247 if (!per_pin->pcm)
1248 return;
1249 idx = per_pin->pcm_idx;
1250 per_pin->pcm_idx = -1;
1251 per_pin->pcm = NULL;
1252 if (idx >= 0 && idx < spec->pcm_used)
1253 clear_bit(idx, &spec->pcm_bitmap);
1254}
1255
Libin Yangac983792015-12-16 16:48:16 +08001256static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1257 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1258{
1259 int mux_idx;
1260
1261 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1262 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1263 break;
1264 return mux_idx;
1265}
1266
1267static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1268
1269static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1270 struct hdmi_spec_per_pin *per_pin)
1271{
1272 struct hda_codec *codec = per_pin->codec;
1273 struct hda_pcm *pcm;
1274 struct hda_pcm_stream *hinfo;
1275 struct snd_pcm_substream *substream;
1276 int mux_idx;
1277 bool non_pcm;
1278
1279 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001280 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001281 else
1282 return;
1283 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1284 return;
1285
1286 /* hdmi audio only uses playback and one substream */
1287 hinfo = pcm->stream;
1288 substream = pcm->pcm->streams[0].substream;
1289
1290 per_pin->cvt_nid = hinfo->nid;
1291
1292 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1293 if (mux_idx < per_pin->num_mux_nids)
1294 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1295 AC_VERB_SET_CONNECT_SEL,
1296 mux_idx);
1297 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1298
1299 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1300 if (substream->runtime)
1301 per_pin->channels = substream->runtime->channels;
1302 per_pin->setup = true;
1303 per_pin->mux_idx = mux_idx;
1304
1305 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1306}
1307
1308static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1309 struct hdmi_spec_per_pin *per_pin)
1310{
1311 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1312 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1313
1314 per_pin->chmap_set = false;
1315 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1316
1317 per_pin->setup = false;
1318 per_pin->channels = 0;
1319}
1320
Takashi Iwaie90247f2015-11-13 09:12:12 +01001321/* update per_pin ELD from the given new ELD;
1322 * setup info frame and notification accordingly
1323 */
1324static void update_eld(struct hda_codec *codec,
1325 struct hdmi_spec_per_pin *per_pin,
1326 struct hdmi_eld *eld)
1327{
1328 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001329 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001330 bool old_eld_valid = pin_eld->eld_valid;
1331 bool eld_changed;
Libin Yangfb087ea2016-02-23 16:33:37 +08001332 int pcm_idx = -1;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001333
Libin Yangfb087ea2016-02-23 16:33:37 +08001334 /* for monitor disconnection, save pcm_idx firstly */
1335 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001336 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001337 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001338 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001339 hdmi_pcm_setup_pin(spec, per_pin);
1340 } else {
1341 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001342 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001343 }
Libin Yanga76056f2015-12-16 16:48:15 +08001344 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001345 /* if pcm_idx == -1, it means this is in monitor connection event
1346 * we can get the correct pcm_idx now.
1347 */
1348 if (pcm_idx == -1)
1349 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001350
Takashi Iwaie90247f2015-11-13 09:12:12 +01001351 if (eld->eld_valid)
1352 snd_hdmi_show_eld(codec, &eld->info);
1353
1354 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1355 if (eld->eld_valid && pin_eld->eld_valid)
1356 if (pin_eld->eld_size != eld->eld_size ||
1357 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1358 eld->eld_size) != 0)
1359 eld_changed = true;
1360
Takashi Iwaibd481282016-03-18 18:01:53 +01001361 pin_eld->monitor_present = eld->monitor_present;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001362 pin_eld->eld_valid = eld->eld_valid;
1363 pin_eld->eld_size = eld->eld_size;
1364 if (eld->eld_valid)
1365 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1366 pin_eld->info = eld->info;
1367
1368 /*
1369 * Re-setup pin and infoframe. This is needed e.g. when
1370 * - sink is first plugged-in
1371 * - transcoder can change during stream playback on Haswell
1372 * and this can make HW reset converter selection on a pin.
1373 */
1374 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1375 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1376 intel_verify_pin_cvt_connect(codec, per_pin);
1377 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1378 per_pin->mux_idx);
1379 }
1380
1381 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1382 }
1383
Libin Yangfb087ea2016-02-23 16:33:37 +08001384 if (eld_changed && pcm_idx >= 0)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001385 snd_ctl_notify(codec->card,
1386 SNDRV_CTL_EVENT_MASK_VALUE |
1387 SNDRV_CTL_EVENT_MASK_INFO,
Libin Yangfb087ea2016-02-23 16:33:37 +08001388 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001389}
1390
Takashi Iwai788d4412015-11-12 15:36:13 +01001391/* update ELD and jack state via HD-audio verbs */
1392static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1393 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001394{
David Henningsson464837a2013-11-07 13:38:25 +01001395 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001396 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001397 struct hdmi_spec *spec = codec->spec;
1398 struct hdmi_eld *eld = &spec->temp_eld;
1399 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001400 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001401 /*
1402 * Always execute a GetPinSense verb here, even when called from
1403 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1404 * response's PD bit is not the real PD value, but indicates that
1405 * the real PD value changed. An older version of the HD-audio
1406 * specification worked this way. Hence, we just ignore the data in
1407 * the unsolicited response to avoid custom WARs.
1408 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001409 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001410 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001411 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001412
David Henningssonda4a7a32013-12-18 10:46:04 +01001413 present = snd_hda_pin_sense(codec, pin_nid);
1414
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001415 mutex_lock(&per_pin->lock);
David Henningsson4bd038f2013-02-19 16:11:25 +01001416 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1417 if (pin_eld->monitor_present)
1418 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1419 else
1420 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001421
Takashi Iwai4e76a882014-02-25 12:21:03 +01001422 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001423 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001424 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001425
David Henningsson4bd038f2013-02-19 16:11:25 +01001426 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001427 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001428 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001429 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001430 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001431 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001432 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001433 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001434 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001435 if (!eld->eld_valid && repoll)
1436 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001437 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001438
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001439 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001440 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1441 else
1442 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001443
Takashi Iwaiaff747eb2013-11-07 16:39:37 +01001444 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001445
1446 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1447 if (jack)
1448 jack->block_report = !ret;
1449
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001450 mutex_unlock(&per_pin->lock);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001451 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001452}
1453
Libin Yang31842702016-02-19 15:42:06 +08001454static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1455 struct hdmi_spec_per_pin *per_pin)
1456{
1457 struct hdmi_spec *spec = codec->spec;
1458 struct snd_jack *jack = NULL;
1459 struct hda_jack_tbl *jack_tbl;
1460
1461 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1462 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1463 * NULL even after snd_hda_jack_tbl_clear() is called to
1464 * free snd_jack. This may cause access invalid memory
1465 * when calling snd_jack_report
1466 */
1467 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1468 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1469 else if (!spec->dyn_pcm_assign) {
1470 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1471 if (jack_tbl)
1472 jack = jack_tbl->jack;
1473 }
1474 return jack;
1475}
1476
Takashi Iwai788d4412015-11-12 15:36:13 +01001477/* update ELD and jack state via audio component */
1478static void sync_eld_via_acomp(struct hda_codec *codec,
1479 struct hdmi_spec_per_pin *per_pin)
1480{
Takashi Iwai788d4412015-11-12 15:36:13 +01001481 struct hdmi_spec *spec = codec->spec;
1482 struct hdmi_eld *eld = &spec->temp_eld;
Libin Yang25e4abb2016-01-12 11:13:27 +08001483 struct snd_jack *jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001484 int size;
1485
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001486 mutex_lock(&per_pin->lock);
Takashi Iwaic64c1432016-03-21 16:07:30 +01001487 eld->monitor_present = false;
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001488 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1489 &eld->monitor_present, eld->eld_buffer,
1490 ELD_MAX_SIZE);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001491 if (size > 0) {
1492 size = min(size, ELD_MAX_SIZE);
1493 if (snd_hdmi_parse_eld(codec, &eld->info,
1494 eld->eld_buffer, size) < 0)
1495 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001496 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001497
1498 if (size > 0) {
1499 eld->eld_valid = true;
1500 eld->eld_size = size;
1501 } else {
1502 eld->eld_valid = false;
1503 eld->eld_size = 0;
1504 }
1505
Libin Yang25e4abb2016-01-12 11:13:27 +08001506 /* pcm_idx >=0 before update_eld() means it is in monitor
1507 * disconnected event. Jack must be fetched before update_eld()
1508 */
Libin Yang31842702016-02-19 15:42:06 +08001509 jack = pin_idx_to_jack(codec, per_pin);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001510 update_eld(codec, per_pin, eld);
Libin Yang31842702016-02-19 15:42:06 +08001511 if (jack == NULL)
1512 jack = pin_idx_to_jack(codec, per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08001513 if (jack == NULL)
1514 goto unlock;
1515 snd_jack_report(jack,
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001516 eld->monitor_present ? SND_JACK_AVOUT : 0);
1517 unlock:
1518 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001519}
1520
1521static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1522{
1523 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08001524 struct hdmi_spec *spec = codec->spec;
1525 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001526
Takashi Iwai222bde02016-03-17 14:48:13 +01001527 /* no temporary power up/down needed for component notifier */
1528 if (!codec_has_acomp(codec))
1529 snd_hda_power_up_pm(codec);
1530
Libin Yanga76056f2015-12-16 16:48:15 +08001531 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001532 if (codec_has_acomp(codec)) {
1533 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001534 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01001535 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08001536 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01001537 }
Libin Yanga76056f2015-12-16 16:48:15 +08001538 mutex_unlock(&spec->pcm_lock);
1539
Takashi Iwai222bde02016-03-17 14:48:13 +01001540 if (!codec_has_acomp(codec))
1541 snd_hda_power_down_pm(codec);
1542
Libin Yanga76056f2015-12-16 16:48:15 +08001543 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001544}
1545
Wu Fengguang744626d2011-11-16 16:29:47 +08001546static void hdmi_repoll_eld(struct work_struct *work)
1547{
1548 struct hdmi_spec_per_pin *per_pin =
1549 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1550
Wu Fengguangc6e84532011-11-18 16:59:32 -06001551 if (per_pin->repoll_count++ > 6)
1552 per_pin->repoll_count = 0;
1553
Takashi Iwaiefe47102013-11-07 13:38:23 +01001554 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1555 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08001556}
1557
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001558static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1559 hda_nid_t nid);
1560
Wu Fengguang079d88c2010-03-08 10:44:23 +08001561static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1562{
1563 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001564 unsigned int caps, config;
1565 int pin_idx;
1566 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001567 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001568
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001569 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001570 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1571 return 0;
1572
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001573 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001574 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1575 return 0;
1576
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001577 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001578 intel_haswell_fixup_connect_list(codec, pin_nid);
1579
Stephen Warren384a48d2011-06-01 11:14:21 -06001580 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001581 per_pin = snd_array_new(&spec->pins);
1582 if (!per_pin)
1583 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001584
1585 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001586 per_pin->non_pcm = false;
Libin Yanga76056f2015-12-16 16:48:15 +08001587 if (spec->dyn_pcm_assign)
1588 per_pin->pcm_idx = -1;
Libin Yang2bea2412016-01-12 11:13:26 +08001589 else {
1590 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001591 per_pin->pcm_idx = pin_idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001592 }
Libin Yanga76056f2015-12-16 16:48:15 +08001593 per_pin->pin_nid_idx = pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001594
Stephen Warren384a48d2011-06-01 11:14:21 -06001595 err = hdmi_read_pin_conn(codec, pin_idx);
1596 if (err < 0)
1597 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001598
Wu Fengguang079d88c2010-03-08 10:44:23 +08001599 spec->num_pins++;
1600
Stephen Warren384a48d2011-06-01 11:14:21 -06001601 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001602}
1603
Stephen Warren384a48d2011-06-01 11:14:21 -06001604static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001605{
1606 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001607 struct hdmi_spec_per_cvt *per_cvt;
1608 unsigned int chans;
1609 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001610
Stephen Warren384a48d2011-06-01 11:14:21 -06001611 chans = get_wcaps(codec, cvt_nid);
1612 chans = get_wcaps_channels(chans);
1613
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001614 per_cvt = snd_array_new(&spec->cvts);
1615 if (!per_cvt)
1616 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001617
1618 per_cvt->cvt_nid = cvt_nid;
1619 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001620 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001621 per_cvt->channels_max = chans;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05301622 if (chans > spec->chmap.channels_max)
1623 spec->chmap.channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001624 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001625
1626 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1627 &per_cvt->rates,
1628 &per_cvt->formats,
1629 &per_cvt->maxbps);
1630 if (err < 0)
1631 return err;
1632
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001633 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1634 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1635 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001636
1637 return 0;
1638}
1639
1640static int hdmi_parse_codec(struct hda_codec *codec)
1641{
1642 hda_nid_t nid;
1643 int i, nodes;
1644
Takashi Iwai7639a062015-03-03 10:07:24 +01001645 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001646 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001647 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08001648 return -EINVAL;
1649 }
1650
1651 for (i = 0; i < nodes; i++, nid++) {
1652 unsigned int caps;
1653 unsigned int type;
1654
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001655 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001656 type = get_wcaps_type(caps);
1657
1658 if (!(caps & AC_WCAP_DIGITAL))
1659 continue;
1660
1661 switch (type) {
1662 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001663 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001664 break;
1665 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001666 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001667 break;
1668 }
1669 }
1670
Wu Fengguang079d88c2010-03-08 10:44:23 +08001671 return 0;
1672}
1673
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001674/*
1675 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001676static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1677{
1678 struct hda_spdif_out *spdif;
1679 bool non_pcm;
1680
1681 mutex_lock(&codec->spdif_mutex);
1682 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1683 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1684 mutex_unlock(&codec->spdif_mutex);
1685 return non_pcm;
1686}
1687
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001688/*
1689 * HDMI callbacks
1690 */
1691
1692static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1693 struct hda_codec *codec,
1694 unsigned int stream_tag,
1695 unsigned int format,
1696 struct snd_pcm_substream *substream)
1697{
Stephen Warren384a48d2011-06-01 11:14:21 -06001698 hda_nid_t cvt_nid = hinfo->nid;
1699 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08001700 int pin_idx;
1701 struct hdmi_spec_per_pin *per_pin;
1702 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08001703 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001704 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07001705 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08001706 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001707
Libin Yang42b29872015-12-16 13:42:42 +08001708 mutex_lock(&spec->pcm_lock);
1709 pin_idx = hinfo_to_pin_index(codec, hinfo);
1710 if (spec->dyn_pcm_assign && pin_idx < 0) {
1711 /* when dyn_pcm_assign and pcm is not bound to a pin
1712 * skip pin setup and return 0 to make audio playback
1713 * be ongoing
1714 */
1715 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
1716 snd_hda_codec_setup_stream(codec, cvt_nid,
1717 stream_tag, 0, format);
1718 mutex_unlock(&spec->pcm_lock);
1719 return 0;
1720 }
1721
1722 if (snd_BUG_ON(pin_idx < 0)) {
1723 mutex_unlock(&spec->pcm_lock);
1724 return -EINVAL;
1725 }
1726 per_pin = get_pin(spec, pin_idx);
1727 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08001728 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08001729 /* Verify pin:cvt selections to avoid silent audio after S3.
1730 * After S3, the audio driver restores pin:cvt selections
1731 * but this can happen before gfx is ready and such selection
1732 * is overlooked by HW. Thus multiple pins can share a same
1733 * default convertor and mute control will affect each other,
1734 * which can cause a resumed audio playback become silent
1735 * after S3.
1736 */
1737 intel_verify_pin_cvt_connect(codec, per_pin);
1738 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
1739 }
1740
Libin Yangddd621f2015-09-02 14:11:40 +08001741 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1742 /* Todo: add DP1.2 MST audio support later */
Takashi Iwai93a9ff12016-03-18 19:45:13 +01001743 if (codec_has_acomp(codec))
1744 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08001745
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001746 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001747 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02001748 per_pin->channels = substream->runtime->channels;
1749 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001750
Takashi Iwaib0540872013-09-02 12:33:02 +02001751 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001752 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07001753 if (spec->dyn_pin_out) {
1754 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1755 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1756 snd_hda_codec_write(codec, pin_nid, 0,
1757 AC_VERB_SET_PIN_WIDGET_CONTROL,
1758 pinctl | PIN_OUT);
1759 }
1760
Libin Yang42b29872015-12-16 13:42:42 +08001761 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1762 stream_tag, format);
1763 mutex_unlock(&spec->pcm_lock);
1764 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001765}
1766
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001767static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1768 struct hda_codec *codec,
1769 struct snd_pcm_substream *substream)
1770{
1771 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1772 return 0;
1773}
1774
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001775static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1776 struct hda_codec *codec,
1777 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001778{
1779 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08001780 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06001781 struct hdmi_spec_per_cvt *per_cvt;
1782 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07001783 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06001784
Stephen Warren384a48d2011-06-01 11:14:21 -06001785 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08001786 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1787 if (snd_BUG_ON(pcm_idx < 0))
1788 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001789 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001790 if (snd_BUG_ON(cvt_idx < 0))
1791 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001792 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001793
1794 snd_BUG_ON(!per_cvt->assigned);
1795 per_cvt->assigned = 0;
1796 hinfo->nid = 0;
1797
Libin Yang42b29872015-12-16 13:42:42 +08001798 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08001799 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001800 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001801 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001802 if (spec->dyn_pcm_assign && pin_idx < 0) {
1803 mutex_unlock(&spec->pcm_lock);
1804 return 0;
1805 }
1806
1807 if (snd_BUG_ON(pin_idx < 0)) {
1808 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06001809 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08001810 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001811 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001812
Stephen Warren75fae112014-01-30 11:52:16 -07001813 if (spec->dyn_pin_out) {
1814 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1815 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1816 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1817 AC_VERB_SET_PIN_WIDGET_CONTROL,
1818 pinctl & ~PIN_OUT);
1819 }
1820
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001821 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001822 per_pin->chmap_set = false;
1823 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001824
1825 per_pin->setup = false;
1826 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001827 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08001828 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06001829 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001830
Stephen Warren384a48d2011-06-01 11:14:21 -06001831 return 0;
1832}
1833
1834static const struct hda_pcm_ops generic_ops = {
1835 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001836 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001837 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001838 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001839};
1840
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05301841static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1842 unsigned char *chmap)
1843{
1844 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1845 struct hdmi_spec *spec = codec->spec;
1846 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1847
1848 /* chmap is already set to 0 in caller */
1849 if (!per_pin)
1850 return;
1851
1852 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
1853}
1854
1855static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1856 unsigned char *chmap, int prepared)
1857{
1858 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1859 struct hdmi_spec *spec = codec->spec;
1860 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1861
1862 mutex_lock(&per_pin->lock);
1863 per_pin->chmap_set = true;
1864 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
1865 if (prepared)
1866 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1867 mutex_unlock(&per_pin->lock);
1868}
1869
1870static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1871{
1872 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1873 struct hdmi_spec *spec = codec->spec;
1874 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1875
1876 return per_pin ? true:false;
1877}
1878
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001879static int generic_hdmi_build_pcms(struct hda_codec *codec)
1880{
1881 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001882 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001883
Stephen Warren384a48d2011-06-01 11:14:21 -06001884 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1885 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001886 struct hda_pcm_stream *pstr;
1887
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001888 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001889 if (!info)
1890 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08001891
1892 spec->pcm_rec[pin_idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08001893 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001894 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001895 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001896
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001897 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001898 pstr->substreams = 1;
1899 pstr->ops = generic_ops;
1900 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001901 }
1902
1903 return 0;
1904}
1905
Libin Yang25e4abb2016-01-12 11:13:27 +08001906static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01001907{
Libin Yang25e4abb2016-01-12 11:13:27 +08001908 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01001909
Libin Yang25e4abb2016-01-12 11:13:27 +08001910 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001911}
1912
Libin Yang25e4abb2016-01-12 11:13:27 +08001913static int add_hdmi_jack_kctl(struct hda_codec *codec,
1914 struct hdmi_spec *spec,
1915 int pcm_idx,
Takashi Iwai788d4412015-11-12 15:36:13 +01001916 const char *name)
1917{
1918 struct snd_jack *jack;
1919 int err;
1920
1921 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
1922 true, false);
1923 if (err < 0)
1924 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08001925
1926 spec->pcm_rec[pcm_idx].jack = jack;
1927 jack->private_data = &spec->pcm_rec[pcm_idx];
1928 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01001929 return 0;
1930}
1931
Libin Yang25e4abb2016-01-12 11:13:27 +08001932static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
David Henningsson0b6c49b2011-08-23 16:56:03 +02001933{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001934 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001935 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08001936 struct hdmi_spec_per_pin *per_pin;
1937 struct hda_jack_tbl *jack;
1938 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01001939 bool phantom_jack;
Libin Yang25e4abb2016-01-12 11:13:27 +08001940 int ret;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001941
Takashi Iwai31ef2252011-12-01 17:41:36 +01001942 if (pcmdev > 0)
1943 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Libin Yang25e4abb2016-01-12 11:13:27 +08001944
1945 if (spec->dyn_pcm_assign)
1946 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
1947
1948 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
1949 /* if !dyn_pcm_assign, it must be non-MST mode.
1950 * This means pcms and pins are statically mapped.
1951 * And pcm_idx is pin_idx.
1952 */
1953 per_pin = get_pin(spec, pcm_idx);
Takashi Iwai909cadc2015-11-12 11:52:13 +01001954 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
1955 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01001956 strncat(hdmi_str, " Phantom",
1957 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
Libin Yang25e4abb2016-01-12 11:13:27 +08001958 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
1959 phantom_jack);
1960 if (ret < 0)
1961 return ret;
1962 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1963 if (jack == NULL)
1964 return 0;
1965 /* assign jack->jack to pcm_rec[].jack to
1966 * align with dyn_pcm_assign mode
1967 */
1968 spec->pcm_rec[pcm_idx].jack = jack->jack;
1969 return 0;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001970}
1971
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001972static int generic_hdmi_build_controls(struct hda_codec *codec)
1973{
1974 struct hdmi_spec *spec = codec->spec;
1975 int err;
Libin Yang25e4abb2016-01-12 11:13:27 +08001976 int pin_idx, pcm_idx;
1977
1978
1979 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
1980 err = generic_hdmi_build_jack(codec, pcm_idx);
1981 if (err < 0)
1982 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08001983
1984 /* create the spdif for each pcm
1985 * pin will be bound when monitor is connected
1986 */
1987 if (spec->dyn_pcm_assign)
1988 err = snd_hda_create_dig_out_ctls(codec,
1989 0, spec->cvt_nids[0],
1990 HDA_PCM_TYPE_HDMI);
1991 else {
1992 struct hdmi_spec_per_pin *per_pin =
1993 get_pin(spec, pcm_idx);
1994 err = snd_hda_create_dig_out_ctls(codec,
1995 per_pin->pin_nid,
1996 per_pin->mux_nids[0],
1997 HDA_PCM_TYPE_HDMI);
1998 }
1999 if (err < 0)
2000 return err;
2001 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002002
2003 /* add control for ELD Bytes */
2004 err = hdmi_create_eld_ctl(codec, pcm_idx,
2005 get_pcm_rec(spec, pcm_idx)->device);
2006 if (err < 0)
2007 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002008 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002009
Stephen Warren384a48d2011-06-01 11:14:21 -06002010 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002011 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002012
Takashi Iwai82b1d732011-12-20 15:53:07 +01002013 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002014 }
2015
Takashi Iwaid45e6882012-07-31 11:36:00 +02002016 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002017 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002018 struct hda_pcm *pcm;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002019
Libin Yang022f3442016-02-03 10:48:34 +08002020 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002021 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002022 break;
Subhransu S. Prusty2f6e8a82016-03-04 19:59:51 +05302023 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002024 if (err < 0)
2025 return err;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002026 }
2027
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002028 return 0;
2029}
2030
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002031static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2032{
2033 struct hdmi_spec *spec = codec->spec;
2034 int pin_idx;
2035
2036 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002037 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002038
2039 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002040 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002041 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002042 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002043 }
2044 return 0;
2045}
2046
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002047static int generic_hdmi_init(struct hda_codec *codec)
2048{
2049 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002050 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002051
Stephen Warren384a48d2011-06-01 11:14:21 -06002052 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002053 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002054 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002055
2056 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002057 if (!codec_has_acomp(codec))
2058 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2059 codec->jackpoll_interval > 0 ?
2060 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002061 }
2062 return 0;
2063}
2064
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002065static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2066{
2067 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2068 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002069}
2070
2071static void hdmi_array_free(struct hdmi_spec *spec)
2072{
2073 snd_array_free(&spec->pins);
2074 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002075}
2076
Takashi Iwaia6866322016-03-21 12:18:33 +01002077static void generic_spec_free(struct hda_codec *codec)
2078{
2079 struct hdmi_spec *spec = codec->spec;
2080
2081 if (spec) {
2082 hdmi_array_free(spec);
2083 kfree(spec);
2084 codec->spec = NULL;
2085 }
2086 codec->dp_mst = false;
2087}
2088
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002089static void generic_hdmi_free(struct hda_codec *codec)
2090{
2091 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002092 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002093
Takashi Iwai66032492015-12-01 16:49:35 +01002094 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002095 snd_hdac_i915_register_notifier(NULL);
2096
Stephen Warren384a48d2011-06-01 11:14:21 -06002097 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002098 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002099 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002100 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002101 }
2102
2103 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2104 if (spec->pcm_rec[pcm_idx].jack == NULL)
2105 continue;
2106 if (spec->dyn_pcm_assign)
2107 snd_device_free(codec->card,
2108 spec->pcm_rec[pcm_idx].jack);
2109 else
2110 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002111 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002112
Takashi Iwaia6866322016-03-21 12:18:33 +01002113 generic_spec_free(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002114}
2115
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002116#ifdef CONFIG_PM
2117static int generic_hdmi_resume(struct hda_codec *codec)
2118{
2119 struct hdmi_spec *spec = codec->spec;
2120 int pin_idx;
2121
Pierre Ossmana2833682014-06-18 21:48:09 +02002122 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002123 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002124
2125 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2126 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2127 hdmi_present_sense(per_pin, 1);
2128 }
2129 return 0;
2130}
2131#endif
2132
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002133static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002134 .init = generic_hdmi_init,
2135 .free = generic_hdmi_free,
2136 .build_pcms = generic_hdmi_build_pcms,
2137 .build_controls = generic_hdmi_build_controls,
2138 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002139#ifdef CONFIG_PM
2140 .resume = generic_hdmi_resume,
2141#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002142};
2143
Anssi Hannula307229d2013-10-24 21:10:34 +03002144static const struct hdmi_ops generic_standard_hdmi_ops = {
2145 .pin_get_eld = snd_hdmi_get_eld,
Anssi Hannula307229d2013-10-24 21:10:34 +03002146 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2147 .pin_hbr_setup = hdmi_pin_hbr_setup,
2148 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302149};
2150
Takashi Iwaia6866322016-03-21 12:18:33 +01002151/* allocate codec->spec and assign/initialize generic parser ops */
2152static int alloc_generic_hdmi(struct hda_codec *codec)
2153{
2154 struct hdmi_spec *spec;
2155
2156 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2157 if (!spec)
2158 return -ENOMEM;
2159
2160 spec->ops = generic_standard_hdmi_ops;
2161 mutex_init(&spec->pcm_lock);
2162 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2163
2164 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2165 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2166 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2167
2168 codec->spec = spec;
2169 hdmi_array_init(spec, 4);
2170
2171 codec->patch_ops = generic_hdmi_patch_ops;
2172
2173 return 0;
2174}
2175
2176/* generic HDMI parser */
2177static int patch_generic_hdmi(struct hda_codec *codec)
2178{
2179 int err;
2180
2181 err = alloc_generic_hdmi(codec);
2182 if (err < 0)
2183 return err;
2184
2185 err = hdmi_parse_codec(codec);
2186 if (err < 0) {
2187 generic_spec_free(codec);
2188 return err;
2189 }
2190
2191 generic_hdmi_init_per_pins(codec);
2192 return 0;
2193}
2194
2195/*
2196 * Intel codec parsers and helpers
2197 */
2198
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002199static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2200 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002201{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002202 struct hdmi_spec *spec = codec->spec;
2203 hda_nid_t conns[4];
2204 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002205
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002206 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2207 if (nconns == spec->num_cvts &&
2208 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002209 return;
2210
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002211 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002212 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002213 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002214}
2215
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002216#define INTEL_VENDOR_NID 0x08
2217#define INTEL_GET_VENDOR_VERB 0xf81
2218#define INTEL_SET_VENDOR_VERB 0x781
2219#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2220#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2221
2222static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002223 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002224{
2225 unsigned int vendor_param;
2226
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002227 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2228 INTEL_GET_VENDOR_VERB, 0);
2229 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2230 return;
2231
2232 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2233 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2234 INTEL_SET_VENDOR_VERB, vendor_param);
2235 if (vendor_param == -1)
2236 return;
2237
Takashi Iwai17df3f52013-05-08 08:09:34 +02002238 if (update_tree)
2239 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002240}
2241
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002242static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2243{
2244 unsigned int vendor_param;
2245
2246 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2247 INTEL_GET_VENDOR_VERB, 0);
2248 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2249 return;
2250
2251 /* enable DP1.2 mode */
2252 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002253 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002254 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2255 INTEL_SET_VENDOR_VERB, vendor_param);
2256}
2257
Takashi Iwai17df3f52013-05-08 08:09:34 +02002258/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2259 * Otherwise you may get severe h/w communication errors.
2260 */
2261static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2262 unsigned int power_state)
2263{
2264 if (power_state == AC_PWRST_D0) {
2265 intel_haswell_enable_all_pins(codec, false);
2266 intel_haswell_fixup_enable_dp12(codec);
2267 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002268
Takashi Iwai17df3f52013-05-08 08:09:34 +02002269 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2270 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2271}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002272
David Henningssonf0675d42015-09-03 11:51:34 +02002273static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002274{
2275 struct hda_codec *codec = audio_ptr;
2276 int pin_nid = port + 0x04;
2277
Takashi Iwai4f8e4f32016-03-10 12:02:49 +01002278 /* we assume only from port-B to port-D */
2279 if (port < 1 || port > 3)
2280 return;
2281
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002282 /* skip notification during system suspend (but not in runtime PM);
2283 * the state will be updated at resume
2284 */
2285 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2286 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002287 /* ditto during suspend/resume process itself */
2288 if (atomic_read(&(codec)->core.in_pm))
2289 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002290
David Henningsson25adc132015-08-19 10:48:58 +02002291 check_presence_and_report(codec, pin_nid);
2292}
2293
Takashi Iwaia6866322016-03-21 12:18:33 +01002294/* register i915 component pin_eld_notify callback */
2295static void register_i915_notifier(struct hda_codec *codec)
2296{
2297 struct hdmi_spec *spec = codec->spec;
2298
2299 spec->use_acomp_notifier = true;
2300 spec->i915_audio_ops.audio_ptr = codec;
2301 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2302 * will call pin_eld_notify with using audio_ptr pointer
2303 * We need make sure audio_ptr is really setup
2304 */
2305 wmb();
2306 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2307 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2308}
2309
2310/* Intel Haswell and onwards; audio component with eld notifier */
2311static int patch_i915_hsw_hdmi(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002312{
2313 struct hdmi_spec *spec;
Takashi Iwaia6866322016-03-21 12:18:33 +01002314 int err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002315
Takashi Iwaia6866322016-03-21 12:18:33 +01002316 /* HSW+ requires i915 binding */
2317 if (!codec->bus->core.audio_component) {
2318 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2319 return -ENODEV;
Takashi Iwai691be972016-03-18 15:10:08 +01002320 }
Takashi Iwai55913112015-12-10 13:03:29 +01002321
Takashi Iwaia6866322016-03-21 12:18:33 +01002322 err = alloc_generic_hdmi(codec);
2323 if (err < 0)
2324 return err;
2325 spec = codec->spec;
2326
2327 intel_haswell_enable_all_pins(codec, true);
2328 intel_haswell_fixup_enable_dp12(codec);
2329
2330 /* For Haswell/Broadwell, the controller is also in the power well and
2331 * can cover the codec power request, and so need not set this flag.
2332 */
2333 if (!is_haswell(codec) && !is_broadwell(codec))
2334 codec->core.link_power_control = 1;
2335
2336 codec->patch_ops.set_power_state = haswell_set_power_state;
2337 codec->dp_mst = true;
2338 codec->depop_delay = 0;
2339 codec->auto_runtime_pm = 1;
2340
2341 err = hdmi_parse_codec(codec);
2342 if (err < 0) {
2343 generic_spec_free(codec);
2344 return err;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002345 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002346
Takashi Iwaia6866322016-03-21 12:18:33 +01002347 generic_hdmi_init_per_pins(codec);
2348 register_i915_notifier(codec);
2349 return 0;
2350}
2351
2352/* Intel Baytrail and Braswell; without get_eld notifier */
2353static int patch_i915_byt_hdmi(struct hda_codec *codec)
2354{
2355 struct hdmi_spec *spec;
2356 int err;
2357
2358 /* requires i915 binding */
2359 if (!codec->bus->core.audio_component) {
2360 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2361 return -ENODEV;
2362 }
2363
2364 err = alloc_generic_hdmi(codec);
2365 if (err < 0)
2366 return err;
2367 spec = codec->spec;
2368
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002369 /* For Valleyview/Cherryview, only the display codec is in the display
2370 * power well and can use link_power ops to request/release the power.
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002371 */
Takashi Iwaia6866322016-03-21 12:18:33 +01002372 codec->core.link_power_control = 1;
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002373
Takashi Iwaia6866322016-03-21 12:18:33 +01002374 codec->depop_delay = 0;
2375 codec->auto_runtime_pm = 1;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002376
Takashi Iwaia6866322016-03-21 12:18:33 +01002377 err = hdmi_parse_codec(codec);
2378 if (err < 0) {
2379 generic_spec_free(codec);
2380 return err;
2381 }
Lu, Han2377c3c2015-06-09 16:50:38 +08002382
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002383 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002384 return 0;
2385}
2386
2387/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002388 * Shared non-generic implementations
2389 */
2390
2391static int simple_playback_build_pcms(struct hda_codec *codec)
2392{
2393 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002394 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002395 unsigned int chans;
2396 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002397 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002398
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002399 per_cvt = get_cvt(spec, 0);
2400 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002401 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002402
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002403 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002404 if (!info)
2405 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002406 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002407 info->pcm_type = HDA_PCM_TYPE_HDMI;
2408 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2409 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002410 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002411 if (pstr->channels_max <= 2 && chans && chans <= 16)
2412 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002413
2414 return 0;
2415}
2416
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002417/* unsolicited event for jack sensing */
2418static void simple_hdmi_unsol_event(struct hda_codec *codec,
2419 unsigned int res)
2420{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002421 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002422 snd_hda_jack_report_sync(codec);
2423}
2424
2425/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2426 * as long as spec->pins[] is set correctly
2427 */
2428#define simple_hdmi_build_jack generic_hdmi_build_jack
2429
Stephen Warren3aaf8982011-06-01 11:14:19 -06002430static int simple_playback_build_controls(struct hda_codec *codec)
2431{
2432 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002433 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002434 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002435
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002436 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002437 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2438 per_cvt->cvt_nid,
2439 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002440 if (err < 0)
2441 return err;
2442 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002443}
2444
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002445static int simple_playback_init(struct hda_codec *codec)
2446{
2447 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002448 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2449 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002450
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002451 snd_hda_codec_write(codec, pin, 0,
2452 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2453 /* some codecs require to unmute the pin */
2454 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2455 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2456 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002457 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002458 return 0;
2459}
2460
Stephen Warren3aaf8982011-06-01 11:14:19 -06002461static void simple_playback_free(struct hda_codec *codec)
2462{
2463 struct hdmi_spec *spec = codec->spec;
2464
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002465 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002466 kfree(spec);
2467}
2468
2469/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002470 * Nvidia specific implementations
2471 */
2472
2473#define Nv_VERB_SET_Channel_Allocation 0xF79
2474#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2475#define Nv_VERB_SET_Audio_Protection_On 0xF98
2476#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2477
2478#define nvhdmi_master_con_nid_7x 0x04
2479#define nvhdmi_master_pin_nid_7x 0x05
2480
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002481static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002482 /*front, rear, clfe, rear_surr */
2483 0x6, 0x8, 0xa, 0xc,
2484};
2485
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002486static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2487 /* set audio protect on */
2488 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2489 /* enable digital output on pin widget */
2490 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2491 {} /* terminator */
2492};
2493
2494static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002495 /* set audio protect on */
2496 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2497 /* enable digital output on pin widget */
2498 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2499 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2500 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2501 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2502 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2503 {} /* terminator */
2504};
2505
2506#ifdef LIMITED_RATE_FMT_SUPPORT
2507/* support only the safe format and rate */
2508#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2509#define SUPPORTED_MAXBPS 16
2510#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2511#else
2512/* support all rates and formats */
2513#define SUPPORTED_RATES \
2514 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2515 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2516 SNDRV_PCM_RATE_192000)
2517#define SUPPORTED_MAXBPS 24
2518#define SUPPORTED_FORMATS \
2519 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2520#endif
2521
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002522static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002523{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002524 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2525 return 0;
2526}
2527
2528static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2529{
2530 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002531 return 0;
2532}
2533
Nitin Daga393004b2011-01-10 21:49:31 +05302534static unsigned int channels_2_6_8[] = {
2535 2, 6, 8
2536};
2537
2538static unsigned int channels_2_8[] = {
2539 2, 8
2540};
2541
2542static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2543 .count = ARRAY_SIZE(channels_2_6_8),
2544 .list = channels_2_6_8,
2545 .mask = 0,
2546};
2547
2548static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2549 .count = ARRAY_SIZE(channels_2_8),
2550 .list = channels_2_8,
2551 .mask = 0,
2552};
2553
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002554static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2555 struct hda_codec *codec,
2556 struct snd_pcm_substream *substream)
2557{
2558 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302559 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2560
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002561 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05302562 case 0x10de0002:
2563 case 0x10de0003:
2564 case 0x10de0005:
2565 case 0x10de0006:
2566 hw_constraints_channels = &hw_constraints_2_8_channels;
2567 break;
2568 case 0x10de0007:
2569 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2570 break;
2571 default:
2572 break;
2573 }
2574
2575 if (hw_constraints_channels != NULL) {
2576 snd_pcm_hw_constraint_list(substream->runtime, 0,
2577 SNDRV_PCM_HW_PARAM_CHANNELS,
2578 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002579 } else {
2580 snd_pcm_hw_constraint_step(substream->runtime, 0,
2581 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302582 }
2583
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002584 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2585}
2586
2587static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2588 struct hda_codec *codec,
2589 struct snd_pcm_substream *substream)
2590{
2591 struct hdmi_spec *spec = codec->spec;
2592 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2593}
2594
2595static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2596 struct hda_codec *codec,
2597 unsigned int stream_tag,
2598 unsigned int format,
2599 struct snd_pcm_substream *substream)
2600{
2601 struct hdmi_spec *spec = codec->spec;
2602 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2603 stream_tag, format, substream);
2604}
2605
Takashi Iwaid0b12522012-06-15 14:34:42 +02002606static const struct hda_pcm_stream simple_pcm_playback = {
2607 .substreams = 1,
2608 .channels_min = 2,
2609 .channels_max = 2,
2610 .ops = {
2611 .open = simple_playback_pcm_open,
2612 .close = simple_playback_pcm_close,
2613 .prepare = simple_playback_pcm_prepare
2614 },
2615};
2616
2617static const struct hda_codec_ops simple_hdmi_patch_ops = {
2618 .build_controls = simple_playback_build_controls,
2619 .build_pcms = simple_playback_build_pcms,
2620 .init = simple_playback_init,
2621 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002622 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002623};
2624
2625static int patch_simple_hdmi(struct hda_codec *codec,
2626 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2627{
2628 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002629 struct hdmi_spec_per_cvt *per_cvt;
2630 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002631
2632 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2633 if (!spec)
2634 return -ENOMEM;
2635
2636 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002637 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002638
2639 spec->multiout.num_dacs = 0; /* no analog */
2640 spec->multiout.max_channels = 2;
2641 spec->multiout.dig_out_nid = cvt_nid;
2642 spec->num_cvts = 1;
2643 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002644 per_pin = snd_array_new(&spec->pins);
2645 per_cvt = snd_array_new(&spec->cvts);
2646 if (!per_pin || !per_cvt) {
2647 simple_playback_free(codec);
2648 return -ENOMEM;
2649 }
2650 per_cvt->cvt_nid = cvt_nid;
2651 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002652 spec->pcm_playback = simple_pcm_playback;
2653
2654 codec->patch_ops = simple_hdmi_patch_ops;
2655
2656 return 0;
2657}
2658
Aaron Plattner1f348522011-04-06 17:19:04 -07002659static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2660 int channels)
2661{
2662 unsigned int chanmask;
2663 int chan = channels ? (channels - 1) : 1;
2664
2665 switch (channels) {
2666 default:
2667 case 0:
2668 case 2:
2669 chanmask = 0x00;
2670 break;
2671 case 4:
2672 chanmask = 0x08;
2673 break;
2674 case 6:
2675 chanmask = 0x0b;
2676 break;
2677 case 8:
2678 chanmask = 0x13;
2679 break;
2680 }
2681
2682 /* Set the audio infoframe channel allocation and checksum fields. The
2683 * channel count is computed implicitly by the hardware. */
2684 snd_hda_codec_write(codec, 0x1, 0,
2685 Nv_VERB_SET_Channel_Allocation, chanmask);
2686
2687 snd_hda_codec_write(codec, 0x1, 0,
2688 Nv_VERB_SET_Info_Frame_Checksum,
2689 (0x71 - chan - chanmask));
2690}
2691
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002692static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2693 struct hda_codec *codec,
2694 struct snd_pcm_substream *substream)
2695{
2696 struct hdmi_spec *spec = codec->spec;
2697 int i;
2698
2699 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2700 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2701 for (i = 0; i < 4; i++) {
2702 /* set the stream id */
2703 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2704 AC_VERB_SET_CHANNEL_STREAMID, 0);
2705 /* set the stream format */
2706 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2707 AC_VERB_SET_STREAM_FORMAT, 0);
2708 }
2709
Aaron Plattner1f348522011-04-06 17:19:04 -07002710 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2711 * streams are disabled. */
2712 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2713
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002714 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2715}
2716
2717static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2718 struct hda_codec *codec,
2719 unsigned int stream_tag,
2720 unsigned int format,
2721 struct snd_pcm_substream *substream)
2722{
2723 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002724 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002725 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002726 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002727 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002728 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002729
2730 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002731 per_cvt = get_cvt(spec, 0);
2732 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002733
2734 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002735
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002736 dataDCC2 = 0x2;
2737
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002738 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002739 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002740 snd_hda_codec_write(codec,
2741 nvhdmi_master_con_nid_7x,
2742 0,
2743 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002744 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002745
2746 /* set the stream id */
2747 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2748 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2749
2750 /* set the stream format */
2751 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2752 AC_VERB_SET_STREAM_FORMAT, format);
2753
2754 /* turn on again (if needed) */
2755 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002756 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002757 snd_hda_codec_write(codec,
2758 nvhdmi_master_con_nid_7x,
2759 0,
2760 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002761 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002762 snd_hda_codec_write(codec,
2763 nvhdmi_master_con_nid_7x,
2764 0,
2765 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2766 }
2767
2768 for (i = 0; i < 4; i++) {
2769 if (chs == 2)
2770 channel_id = 0;
2771 else
2772 channel_id = i * 2;
2773
2774 /* turn off SPDIF once;
2775 *otherwise the IEC958 bits won't be updated
2776 */
2777 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002778 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002779 snd_hda_codec_write(codec,
2780 nvhdmi_con_nids_7x[i],
2781 0,
2782 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002783 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002784 /* set the stream id */
2785 snd_hda_codec_write(codec,
2786 nvhdmi_con_nids_7x[i],
2787 0,
2788 AC_VERB_SET_CHANNEL_STREAMID,
2789 (stream_tag << 4) | channel_id);
2790 /* set the stream format */
2791 snd_hda_codec_write(codec,
2792 nvhdmi_con_nids_7x[i],
2793 0,
2794 AC_VERB_SET_STREAM_FORMAT,
2795 format);
2796 /* turn on again (if needed) */
2797 /* enable and set the channel status audio/data flag */
2798 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002799 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002800 snd_hda_codec_write(codec,
2801 nvhdmi_con_nids_7x[i],
2802 0,
2803 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002804 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002805 snd_hda_codec_write(codec,
2806 nvhdmi_con_nids_7x[i],
2807 0,
2808 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2809 }
2810 }
2811
Aaron Plattner1f348522011-04-06 17:19:04 -07002812 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002813
2814 mutex_unlock(&codec->spdif_mutex);
2815 return 0;
2816}
2817
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002818static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002819 .substreams = 1,
2820 .channels_min = 2,
2821 .channels_max = 8,
2822 .nid = nvhdmi_master_con_nid_7x,
2823 .rates = SUPPORTED_RATES,
2824 .maxbps = SUPPORTED_MAXBPS,
2825 .formats = SUPPORTED_FORMATS,
2826 .ops = {
2827 .open = simple_playback_pcm_open,
2828 .close = nvhdmi_8ch_7x_pcm_close,
2829 .prepare = nvhdmi_8ch_7x_pcm_prepare
2830 },
2831};
2832
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002833static int patch_nvhdmi_2ch(struct hda_codec *codec)
2834{
2835 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002836 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2837 nvhdmi_master_pin_nid_7x);
2838 if (err < 0)
2839 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002840
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002841 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002842 /* override the PCM rates, etc, as the codec doesn't give full list */
2843 spec = codec->spec;
2844 spec->pcm_playback.rates = SUPPORTED_RATES;
2845 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2846 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002847 return 0;
2848}
2849
Takashi Iwai53775b02012-08-01 12:17:41 +02002850static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2851{
2852 struct hdmi_spec *spec = codec->spec;
2853 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002854 if (!err) {
2855 struct hda_pcm *info = get_pcm_rec(spec, 0);
2856 info->own_chmap = true;
2857 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002858 return err;
2859}
2860
2861static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2862{
2863 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002864 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002865 struct snd_pcm_chmap *chmap;
2866 int err;
2867
2868 err = simple_playback_build_controls(codec);
2869 if (err < 0)
2870 return err;
2871
2872 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002873 info = get_pcm_rec(spec, 0);
2874 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002875 SNDRV_PCM_STREAM_PLAYBACK,
2876 snd_pcm_alt_chmaps, 8, 0, &chmap);
2877 if (err < 0)
2878 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002879 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02002880 case 0x10de0002:
2881 case 0x10de0003:
2882 case 0x10de0005:
2883 case 0x10de0006:
2884 chmap->channel_mask = (1U << 2) | (1U << 8);
2885 break;
2886 case 0x10de0007:
2887 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2888 }
2889 return 0;
2890}
2891
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002892static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2893{
2894 struct hdmi_spec *spec;
2895 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002896 if (err < 0)
2897 return err;
2898 spec = codec->spec;
2899 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002900 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002901 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002902 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2903 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002904
2905 /* Initialize the audio infoframe channel mask and checksum to something
2906 * valid */
2907 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2908
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002909 return 0;
2910}
2911
2912/*
Anssi Hannula611885b2013-11-03 17:15:00 +02002913 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
2914 * - 0x10de0015
2915 * - 0x10de0040
2916 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302917static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05302918 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02002919{
2920 if (cap->ca_index == 0x00 && channels == 2)
2921 return SNDRV_CTL_TLVT_CHMAP_FIXED;
2922
Subhransu S. Prusty028cb682016-03-14 10:35:06 +05302923 /* If the speaker allocation matches the channel count, it is OK. */
2924 if (cap->channels != channels)
2925 return -1;
2926
2927 /* all channels are remappable freely */
2928 return SNDRV_CTL_TLVT_CHMAP_VAR;
Anssi Hannula611885b2013-11-03 17:15:00 +02002929}
2930
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05302931static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
2932 int ca, int chs, unsigned char *map)
Anssi Hannula611885b2013-11-03 17:15:00 +02002933{
2934 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
2935 return -EINVAL;
2936
2937 return 0;
2938}
2939
2940static int patch_nvhdmi(struct hda_codec *codec)
2941{
2942 struct hdmi_spec *spec;
2943 int err;
2944
2945 err = patch_generic_hdmi(codec);
2946 if (err)
2947 return err;
2948
2949 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07002950 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02002951
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302952 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02002953 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302954 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02002955
2956 return 0;
2957}
2958
2959/*
Thierry Reding26e9a962015-05-05 14:56:20 +02002960 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
2961 * accessed using vendor-defined verbs. These registers can be used for
2962 * interoperability between the HDA and HDMI drivers.
2963 */
2964
2965/* Audio Function Group node */
2966#define NVIDIA_AFG_NID 0x01
2967
2968/*
2969 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
2970 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
2971 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
2972 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
2973 * additional bit (at position 30) to signal the validity of the format.
2974 *
2975 * | 31 | 30 | 29 16 | 15 0 |
2976 * +---------+-------+--------+--------+
2977 * | TRIGGER | VALID | UNUSED | FORMAT |
2978 * +-----------------------------------|
2979 *
2980 * Note that for the trigger bit to take effect it needs to change value
2981 * (i.e. it needs to be toggled).
2982 */
2983#define NVIDIA_GET_SCRATCH0 0xfa6
2984#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
2985#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
2986#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
2987#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
2988#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
2989#define NVIDIA_SCRATCH_VALID (1 << 6)
2990
2991#define NVIDIA_GET_SCRATCH1 0xfab
2992#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
2993#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
2994#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
2995#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
2996
2997/*
2998 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
2999 * the format is invalidated so that the HDMI codec can be disabled.
3000 */
3001static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3002{
3003 unsigned int value;
3004
3005 /* bits [31:30] contain the trigger and valid bits */
3006 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3007 NVIDIA_GET_SCRATCH0, 0);
3008 value = (value >> 24) & 0xff;
3009
3010 /* bits [15:0] are used to store the HDA format */
3011 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3012 NVIDIA_SET_SCRATCH0_BYTE0,
3013 (format >> 0) & 0xff);
3014 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3015 NVIDIA_SET_SCRATCH0_BYTE1,
3016 (format >> 8) & 0xff);
3017
3018 /* bits [16:24] are unused */
3019 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3020 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3021
3022 /*
3023 * Bit 30 signals that the data is valid and hence that HDMI audio can
3024 * be enabled.
3025 */
3026 if (format == 0)
3027 value &= ~NVIDIA_SCRATCH_VALID;
3028 else
3029 value |= NVIDIA_SCRATCH_VALID;
3030
3031 /*
3032 * Whenever the trigger bit is toggled, an interrupt is raised in the
3033 * HDMI codec. The HDMI driver will use that as trigger to update its
3034 * configuration.
3035 */
3036 value ^= NVIDIA_SCRATCH_TRIGGER;
3037
3038 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3039 NVIDIA_SET_SCRATCH0_BYTE3, value);
3040}
3041
3042static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3043 struct hda_codec *codec,
3044 unsigned int stream_tag,
3045 unsigned int format,
3046 struct snd_pcm_substream *substream)
3047{
3048 int err;
3049
3050 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3051 format, substream);
3052 if (err < 0)
3053 return err;
3054
3055 /* notify the HDMI codec of the format change */
3056 tegra_hdmi_set_format(codec, format);
3057
3058 return 0;
3059}
3060
3061static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3062 struct hda_codec *codec,
3063 struct snd_pcm_substream *substream)
3064{
3065 /* invalidate the format in the HDMI codec */
3066 tegra_hdmi_set_format(codec, 0);
3067
3068 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3069}
3070
3071static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3072{
3073 struct hdmi_spec *spec = codec->spec;
3074 unsigned int i;
3075
3076 for (i = 0; i < spec->num_pins; i++) {
3077 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3078
3079 if (pcm->pcm_type == type)
3080 return pcm;
3081 }
3082
3083 return NULL;
3084}
3085
3086static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3087{
3088 struct hda_pcm_stream *stream;
3089 struct hda_pcm *pcm;
3090 int err;
3091
3092 err = generic_hdmi_build_pcms(codec);
3093 if (err < 0)
3094 return err;
3095
3096 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3097 if (!pcm)
3098 return -ENODEV;
3099
3100 /*
3101 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3102 * codec about format changes.
3103 */
3104 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3105 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3106 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3107
3108 return 0;
3109}
3110
3111static int patch_tegra_hdmi(struct hda_codec *codec)
3112{
3113 int err;
3114
3115 err = patch_generic_hdmi(codec);
3116 if (err)
3117 return err;
3118
3119 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3120
3121 return 0;
3122}
3123
3124/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003125 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003126 */
3127
Anssi Hannula5a6135842013-10-24 21:10:35 +03003128#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003129 ((codec)->core.vendor_id == 0x1002aa01 && \
3130 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003131#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003132
Anssi Hannula5a6135842013-10-24 21:10:35 +03003133/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3134#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3135#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3136#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3137#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3138#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3139#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003140#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003141#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3142#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3143#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3144#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3145#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3146#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3147#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3148#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3149#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3150#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3151#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003152#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003153#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3154#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3155#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3156#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3157#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3158
Anssi Hannula84d69e72013-10-24 21:10:38 +03003159/* AMD specific HDA cvt verbs */
3160#define ATI_VERB_SET_RAMP_RATE 0x770
3161#define ATI_VERB_GET_RAMP_RATE 0xf70
3162
Anssi Hannula5a6135842013-10-24 21:10:35 +03003163#define ATI_OUT_ENABLE 0x1
3164
3165#define ATI_MULTICHANNEL_MODE_PAIRED 0
3166#define ATI_MULTICHANNEL_MODE_SINGLE 1
3167
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003168#define ATI_HBR_CAPABLE 0x01
3169#define ATI_HBR_ENABLE 0x10
3170
Anssi Hannula89250f82013-10-24 21:10:36 +03003171static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3172 unsigned char *buf, int *eld_size)
3173{
3174 /* call hda_eld.c ATI/AMD-specific function */
3175 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3176 is_amdhdmi_rev3_or_later(codec));
3177}
3178
Anssi Hannula5a6135842013-10-24 21:10:35 +03003179static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3180 int active_channels, int conn_type)
3181{
3182 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3183}
3184
3185static int atihdmi_paired_swap_fc_lfe(int pos)
3186{
3187 /*
3188 * ATI/AMD have automatic FC/LFE swap built-in
3189 * when in pairwise mapping mode.
3190 */
3191
3192 switch (pos) {
3193 /* see channel_allocations[].speakers[] */
3194 case 2: return 3;
3195 case 3: return 2;
3196 default: break;
3197 }
3198
3199 return pos;
3200}
3201
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303202static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3203 int ca, int chs, unsigned char *map)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003204{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303205 struct hdac_cea_channel_speaker_allocation *cap;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003206 int i, j;
3207
3208 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3209
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303210 cap = snd_hdac_get_ch_alloc_from_ca(ca);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003211 for (i = 0; i < chs; ++i) {
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303212 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003213 bool ok = false;
3214 bool companion_ok = false;
3215
3216 if (!mask)
3217 continue;
3218
3219 for (j = 0 + i % 2; j < 8; j += 2) {
3220 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3221 if (cap->speakers[chan_idx] == mask) {
3222 /* channel is in a supported position */
3223 ok = true;
3224
3225 if (i % 2 == 0 && i + 1 < chs) {
3226 /* even channel, check the odd companion */
3227 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303228 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003229 int comp_mask_act = cap->speakers[comp_chan_idx];
3230
3231 if (comp_mask_req == comp_mask_act)
3232 companion_ok = true;
3233 else
3234 return -EINVAL;
3235 }
3236 break;
3237 }
3238 }
3239
3240 if (!ok)
3241 return -EINVAL;
3242
3243 if (companion_ok)
3244 i++; /* companion channel already checked */
3245 }
3246
3247 return 0;
3248}
3249
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303250static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3251 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003252{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303253 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003254 int verb;
3255 int ati_channel_setup = 0;
3256
3257 if (hdmi_slot > 7)
3258 return -EINVAL;
3259
3260 if (!has_amd_full_remap_support(codec)) {
3261 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3262
3263 /* In case this is an odd slot but without stream channel, do not
3264 * disable the slot since the corresponding even slot could have a
3265 * channel. In case neither have a channel, the slot pair will be
3266 * disabled when this function is called for the even slot. */
3267 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3268 return 0;
3269
3270 hdmi_slot -= hdmi_slot % 2;
3271
3272 if (stream_channel != 0xf)
3273 stream_channel -= stream_channel % 2;
3274 }
3275
3276 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3277
3278 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3279
3280 if (stream_channel != 0xf)
3281 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3282
3283 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3284}
3285
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303286static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3287 hda_nid_t pin_nid, int asp_slot)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003288{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303289 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003290 bool was_odd = false;
3291 int ati_asp_slot = asp_slot;
3292 int verb;
3293 int ati_channel_setup;
3294
3295 if (asp_slot > 7)
3296 return -EINVAL;
3297
3298 if (!has_amd_full_remap_support(codec)) {
3299 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3300 if (ati_asp_slot % 2 != 0) {
3301 ati_asp_slot -= 1;
3302 was_odd = true;
3303 }
3304 }
3305
3306 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3307
3308 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3309
3310 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3311 return 0xf;
3312
3313 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3314}
3315
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303316static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3317 struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303318 struct hdac_cea_channel_speaker_allocation *cap,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303319 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003320{
3321 int c;
3322
3323 /*
3324 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3325 * we need to take that into account (a single channel may take 2
3326 * channel slots if we need to carry a silent channel next to it).
3327 * On Rev3+ AMD codecs this function is not used.
3328 */
3329 int chanpairs = 0;
3330
3331 /* We only produce even-numbered channel count TLVs */
3332 if ((channels % 2) != 0)
3333 return -1;
3334
3335 for (c = 0; c < 7; c += 2) {
3336 if (cap->speakers[c] || cap->speakers[c+1])
3337 chanpairs++;
3338 }
3339
3340 if (chanpairs * 2 != channels)
3341 return -1;
3342
3343 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3344}
3345
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303346static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303347 struct hdac_cea_channel_speaker_allocation *cap,
3348 unsigned int *chmap, int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003349{
3350 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3351 int count = 0;
3352 int c;
3353
3354 for (c = 7; c >= 0; c--) {
3355 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3356 int spk = cap->speakers[chan];
3357 if (!spk) {
3358 /* add N/A channel if the companion channel is occupied */
3359 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3360 chmap[count++] = SNDRV_CHMAP_NA;
3361
3362 continue;
3363 }
3364
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303365 chmap[count++] = snd_hdac_spk_to_chmap(spk);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003366 }
3367
3368 WARN_ON(count != channels);
3369}
3370
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003371static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3372 bool hbr)
3373{
3374 int hbr_ctl, hbr_ctl_new;
3375
3376 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003377 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003378 if (hbr)
3379 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3380 else
3381 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3382
Takashi Iwai4e76a882014-02-25 12:21:03 +01003383 codec_dbg(codec,
3384 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003385 pin_nid,
3386 hbr_ctl == hbr_ctl_new ? "" : "new-",
3387 hbr_ctl_new);
3388
3389 if (hbr_ctl != hbr_ctl_new)
3390 snd_hda_codec_write(codec, pin_nid, 0,
3391 ATI_VERB_SET_HBR_CONTROL,
3392 hbr_ctl_new);
3393
3394 } else if (hbr)
3395 return -EINVAL;
3396
3397 return 0;
3398}
3399
Anssi Hannula84d69e72013-10-24 21:10:38 +03003400static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3401 hda_nid_t pin_nid, u32 stream_tag, int format)
3402{
3403
3404 if (is_amdhdmi_rev3_or_later(codec)) {
3405 int ramp_rate = 180; /* default as per AMD spec */
3406 /* disable ramp-up/down for non-pcm as per AMD spec */
3407 if (format & AC_FMT_TYPE_NON_PCM)
3408 ramp_rate = 0;
3409
3410 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3411 }
3412
3413 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3414}
3415
3416
Anssi Hannula5a6135842013-10-24 21:10:35 +03003417static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003418{
3419 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003420 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003421
Anssi Hannula5a6135842013-10-24 21:10:35 +03003422 err = generic_hdmi_init(codec);
3423
3424 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003425 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003426
3427 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3428 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3429
3430 /* make sure downmix information in infoframe is zero */
3431 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3432
3433 /* enable channel-wise remap mode if supported */
3434 if (has_amd_full_remap_support(codec))
3435 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3436 ATI_VERB_SET_MULTICHANNEL_MODE,
3437 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003438 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003439
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003440 return 0;
3441}
3442
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003443static int patch_atihdmi(struct hda_codec *codec)
3444{
3445 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003446 struct hdmi_spec_per_cvt *per_cvt;
3447 int err, cvt_idx;
3448
3449 err = patch_generic_hdmi(codec);
3450
3451 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003452 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003453
3454 codec->patch_ops.init = atihdmi_init;
3455
Takashi Iwaid0b12522012-06-15 14:34:42 +02003456 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003457
Anssi Hannula89250f82013-10-24 21:10:36 +03003458 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003459 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003460 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003461 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003462
3463 if (!has_amd_full_remap_support(codec)) {
3464 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303465 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03003466 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303467 spec->chmap.ops.cea_alloc_to_tlv_chmap =
3468 atihdmi_paired_cea_alloc_to_tlv_chmap;
3469 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303470 spec->chmap.ops.pin_get_slot_channel =
3471 atihdmi_pin_get_slot_channel;
3472 spec->chmap.ops.pin_set_slot_channel =
3473 atihdmi_pin_set_slot_channel;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003474 }
3475
3476 /* ATI/AMD converters do not advertise all of their capabilities */
3477 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3478 per_cvt = get_cvt(spec, cvt_idx);
3479 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3480 per_cvt->rates |= SUPPORTED_RATES;
3481 per_cvt->formats |= SUPPORTED_FORMATS;
3482 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3483 }
3484
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303485 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003486
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003487 return 0;
3488}
3489
Annie Liu3de5ff82012-06-08 19:18:42 +08003490/* VIA HDMI Implementation */
3491#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3492#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3493
Annie Liu3de5ff82012-06-08 19:18:42 +08003494static int patch_via_hdmi(struct hda_codec *codec)
3495{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003496 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003497}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003498
3499/*
3500 * patch entries
3501 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003502static const struct hda_device_id snd_hda_id_hdmi[] = {
3503HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3504HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3505HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3506HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3507HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3508HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3509HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3510HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3511HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3512HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3513HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3514HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3515HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3516HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3517HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3518HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3519HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3520HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3521HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3522HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3523HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3524HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3525HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01003526/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003527HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3528HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3529HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3530HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3531HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3532HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3533HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3534HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3535HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3536HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3537HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3538HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3539HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3540HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3541HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3542HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3543HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3544HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3545HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3546HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3547HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Aaron Plattner2d369c72016-03-13 13:58:57 -07003548HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08003549HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003550HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3551HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3552HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3553HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3554HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3555HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
3556HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3557HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3558HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3559HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
3560HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
3561HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01003562HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
3563HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
3564HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
3565HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
3566HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003567HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01003568HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
3569HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003570HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003571/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003572HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003573{} /* terminator */
3574};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003575MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003576
3577MODULE_LICENSE("GPL");
3578MODULE_DESCRIPTION("HDMI HD-audio codec");
3579MODULE_ALIAS("snd-hda-codec-intelhdmi");
3580MODULE_ALIAS("snd-hda-codec-nvhdmi");
3581MODULE_ALIAS("snd-hda-codec-atihdmi");
3582
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003583static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003584 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003585};
3586
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003587module_hda_codec_driver(hdmi_driver);