blob: 3481b43476dc1780572cdda891c98d2f57154760 [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
Takashi Iwai4846a672016-03-21 12:56:46 +0100117 void (*pin_cvt_fixup)(struct hda_codec *codec,
118 struct hdmi_spec_per_pin *per_pin,
119 hda_nid_t cvt_nid);
Anssi Hannula307229d2013-10-24 21:10:34 +0300120};
121
Libin Yang2bea2412016-01-12 11:13:26 +0800122struct hdmi_pcm {
123 struct hda_pcm *pcm;
124 struct snd_jack *jack;
Libin Yangfb087ea2016-02-23 16:33:37 +0800125 struct snd_kcontrol *eld_ctl;
Libin Yang2bea2412016-01-12 11:13:26 +0800126};
127
Wu Fengguang079d88c2010-03-08 10:44:23 +0800128struct hdmi_spec {
129 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100130 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
131 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600132
Wu Fengguang079d88c2010-03-08 10:44:23 +0800133 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100134 struct snd_array pins; /* struct hdmi_spec_per_pin */
Libin Yang2bea2412016-01-12 11:13:26 +0800135 struct hdmi_pcm pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800136 struct mutex pcm_lock;
Libin Yanga76056f2015-12-16 16:48:15 +0800137 /* pcm_bitmap means which pcms have been assigned to pins*/
138 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800139 int pcm_used; /* counter of pcm_rec[] */
Libin Yangac983792015-12-16 16:48:16 +0800140 /* bitmap shows whether the pcm is opened in user space
141 * bit 0 means the first playback PCM (PCM3);
142 * bit 1 means the second playback PCM, and so on.
143 */
144 unsigned long pcm_in_use;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800145
David Henningsson4bd038f2013-02-19 16:11:25 +0100146 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300147 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700148
149 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800150 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800151 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300152 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800153 */
154 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200155 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200156
157 /* i915/powerwell (Haswell+/Valleyview+) specific */
Takashi Iwai691be972016-03-18 15:10:08 +0100158 bool use_acomp_notifier; /* use i915 eld_notify callback for hotplug */
David Henningsson25adc132015-08-19 10:48:58 +0200159 struct i915_audio_component_audio_ops i915_audio_ops;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +0530160
161 struct hdac_chmap chmap;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800162};
163
Takashi Iwaif4e30402015-12-10 13:01:28 +0100164#ifdef CONFIG_SND_HDA_I915
Takashi Iwai691be972016-03-18 15:10:08 +0100165static inline bool codec_has_acomp(struct hda_codec *codec)
166{
167 struct hdmi_spec *spec = codec->spec;
168 return spec->use_acomp_notifier;
169}
Takashi Iwaif4e30402015-12-10 13:01:28 +0100170#else
171#define codec_has_acomp(codec) false
172#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800173
174struct hdmi_audio_infoframe {
175 u8 type; /* 0x84 */
176 u8 ver; /* 0x01 */
177 u8 len; /* 0x0a */
178
Wu Fengguang53d7d692010-09-21 14:25:49 +0800179 u8 checksum;
180
Wu Fengguang079d88c2010-03-08 10:44:23 +0800181 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
182 u8 SS01_SF24;
183 u8 CXT04;
184 u8 CA;
185 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800186};
187
188struct dp_audio_infoframe {
189 u8 type; /* 0x84 */
190 u8 len; /* 0x1b */
191 u8 ver; /* 0x11 << 2 */
192
193 u8 CC02_CT47; /* match with HDMI infoframe from this on */
194 u8 SS01_SF24;
195 u8 CXT04;
196 u8 CA;
197 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800198};
199
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100200union audio_infoframe {
201 struct hdmi_audio_infoframe hdmi;
202 struct dp_audio_infoframe dp;
203 u8 bytes[0];
204};
205
Wu Fengguang079d88c2010-03-08 10:44:23 +0800206/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800207 * HDMI routines
208 */
209
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100210#define get_pin(spec, idx) \
211 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
212#define get_cvt(spec, idx) \
213 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Libin Yang2bea2412016-01-12 11:13:26 +0800214/* obtain hdmi_pcm object assigned to idx */
215#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
216/* obtain hda_pcm object assigned to idx */
217#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100218
Takashi Iwai4e76a882014-02-25 12:21:03 +0100219static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800220{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100221 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600222 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800223
Stephen Warren384a48d2011-06-01 11:14:21 -0600224 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100225 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600226 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800227
Takashi Iwai4e76a882014-02-25 12:21:03 +0100228 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600229 return -EINVAL;
230}
231
Libin Yang2bf3c852015-12-16 13:42:43 +0800232static int hinfo_to_pcm_index(struct hda_codec *codec,
233 struct hda_pcm_stream *hinfo)
234{
235 struct hdmi_spec *spec = codec->spec;
236 int pcm_idx;
237
238 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
239 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
240 return pcm_idx;
241
242 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
243 return -EINVAL;
244}
245
Takashi Iwai4e76a882014-02-25 12:21:03 +0100246static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600247 struct hda_pcm_stream *hinfo)
248{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100249 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800250 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600251 int pin_idx;
252
Libin Yang6590faa2015-12-16 13:42:41 +0800253 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
254 per_pin = get_pin(spec, pin_idx);
Libin Yang2bea2412016-01-12 11:13:26 +0800255 if (per_pin->pcm &&
256 per_pin->pcm->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600257 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800258 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600259
Libin Yang6590faa2015-12-16 13:42:41 +0800260 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600261 return -EINVAL;
262}
263
Libin Yang022f3442016-02-03 10:48:34 +0800264static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
265 int pcm_idx)
266{
267 int i;
268 struct hdmi_spec_per_pin *per_pin;
269
270 for (i = 0; i < spec->num_pins; i++) {
271 per_pin = get_pin(spec, i);
272 if (per_pin->pcm_idx == pcm_idx)
273 return per_pin;
274 }
275 return NULL;
276}
277
Takashi Iwai4e76a882014-02-25 12:21:03 +0100278static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600279{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100280 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600281 int cvt_idx;
282
283 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100284 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600285 return cvt_idx;
286
Takashi Iwai4e76a882014-02-25 12:21:03 +0100287 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800288 return -EINVAL;
289}
290
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500291static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
292 struct snd_ctl_elem_info *uinfo)
293{
294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100295 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200296 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100297 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800298 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500299
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500300 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
301
Libin Yangfb087ea2016-02-23 16:33:37 +0800302 pcm_idx = kcontrol->private_value;
303 mutex_lock(&spec->pcm_lock);
304 per_pin = pcm_idx_to_pin(spec, pcm_idx);
305 if (!per_pin) {
306 /* no pin is bound to the pcm */
307 uinfo->count = 0;
308 mutex_unlock(&spec->pcm_lock);
309 return 0;
310 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200311 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100312 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Libin Yangfb087ea2016-02-23 16:33:37 +0800313 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500314
315 return 0;
316}
317
318static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
319 struct snd_ctl_elem_value *ucontrol)
320{
321 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100322 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200323 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100324 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800325 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500326
Libin Yangfb087ea2016-02-23 16:33:37 +0800327 pcm_idx = kcontrol->private_value;
328 mutex_lock(&spec->pcm_lock);
329 per_pin = pcm_idx_to_pin(spec, pcm_idx);
330 if (!per_pin) {
331 /* no pin is bound to the pcm */
332 memset(ucontrol->value.bytes.data, 0,
333 ARRAY_SIZE(ucontrol->value.bytes.data));
334 mutex_unlock(&spec->pcm_lock);
335 return 0;
336 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200337 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500338
David Henningsson360a8242016-02-05 09:05:41 +0100339 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
340 eld->eld_size > ELD_MAX_SIZE) {
Libin Yangfb087ea2016-02-23 16:33:37 +0800341 mutex_unlock(&spec->pcm_lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100342 snd_BUG();
343 return -EINVAL;
344 }
345
346 memset(ucontrol->value.bytes.data, 0,
347 ARRAY_SIZE(ucontrol->value.bytes.data));
348 if (eld->eld_valid)
349 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
350 eld->eld_size);
Libin Yangfb087ea2016-02-23 16:33:37 +0800351 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500352
353 return 0;
354}
355
356static struct snd_kcontrol_new eld_bytes_ctl = {
357 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
358 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
359 .name = "ELD",
360 .info = hdmi_eld_ctl_info,
361 .get = hdmi_eld_ctl_get,
362};
363
Libin Yangfb087ea2016-02-23 16:33:37 +0800364static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500365 int device)
366{
367 struct snd_kcontrol *kctl;
368 struct hdmi_spec *spec = codec->spec;
369 int err;
370
371 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
372 if (!kctl)
373 return -ENOMEM;
Libin Yangfb087ea2016-02-23 16:33:37 +0800374 kctl->private_value = pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500375 kctl->id.device = device;
376
Libin Yangfb087ea2016-02-23 16:33:37 +0800377 /* no pin nid is associated with the kctl now
378 * tbd: associate pin nid to eld ctl later
379 */
380 err = snd_hda_ctl_add(codec, 0, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500381 if (err < 0)
382 return err;
383
Libin Yangfb087ea2016-02-23 16:33:37 +0800384 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500385 return 0;
386}
387
Wu Fengguang079d88c2010-03-08 10:44:23 +0800388#ifdef BE_PARANOID
389static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
390 int *packet_index, int *byte_index)
391{
392 int val;
393
394 val = snd_hda_codec_read(codec, pin_nid, 0,
395 AC_VERB_GET_HDMI_DIP_INDEX, 0);
396
397 *packet_index = val >> 5;
398 *byte_index = val & 0x1f;
399}
400#endif
401
402static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
403 int packet_index, int byte_index)
404{
405 int val;
406
407 val = (packet_index << 5) | (byte_index & 0x1f);
408
409 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
410}
411
412static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
413 unsigned char val)
414{
415 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
416}
417
Stephen Warren384a48d2011-06-01 11:14:21 -0600418static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800419{
Stephen Warren75fae112014-01-30 11:52:16 -0700420 struct hdmi_spec *spec = codec->spec;
421 int pin_out;
422
Wu Fengguang079d88c2010-03-08 10:44:23 +0800423 /* Unmute */
424 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
425 snd_hda_codec_write(codec, pin_nid, 0,
426 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700427
428 if (spec->dyn_pin_out)
429 /* Disable pin out until stream is active */
430 pin_out = 0;
431 else
432 /* Enable pin out: some machines with GM965 gets broken output
433 * when the pin is disabled or changed while using with HDMI
434 */
435 pin_out = PIN_OUT;
436
Wu Fengguang079d88c2010-03-08 10:44:23 +0800437 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700438 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800439}
440
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200441/*
442 * ELD proc files
443 */
444
Jie Yangcd6a6502015-05-27 19:45:45 +0800445#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200446static void print_eld_info(struct snd_info_entry *entry,
447 struct snd_info_buffer *buffer)
448{
449 struct hdmi_spec_per_pin *per_pin = entry->private_data;
450
451 mutex_lock(&per_pin->lock);
452 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
453 mutex_unlock(&per_pin->lock);
454}
455
456static void write_eld_info(struct snd_info_entry *entry,
457 struct snd_info_buffer *buffer)
458{
459 struct hdmi_spec_per_pin *per_pin = entry->private_data;
460
461 mutex_lock(&per_pin->lock);
462 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
463 mutex_unlock(&per_pin->lock);
464}
465
466static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
467{
468 char name[32];
469 struct hda_codec *codec = per_pin->codec;
470 struct snd_info_entry *entry;
471 int err;
472
473 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100474 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200475 if (err < 0)
476 return err;
477
478 snd_info_set_text_ops(entry, per_pin, print_eld_info);
479 entry->c.text.write = write_eld_info;
480 entry->mode |= S_IWUSR;
481 per_pin->proc_entry = entry;
482
483 return 0;
484}
485
486static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
487{
Markus Elfring1947a112015-06-28 11:15:28 +0200488 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200489 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200490 per_pin->proc_entry = NULL;
491 }
492}
493#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200494static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
495 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200496{
497 return 0;
498}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200499static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200500{
501}
502#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800503
504/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800505 * Audio InfoFrame routines
506 */
507
508/*
509 * Enable Audio InfoFrame Transmission
510 */
511static void hdmi_start_infoframe_trans(struct hda_codec *codec,
512 hda_nid_t pin_nid)
513{
514 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
515 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
516 AC_DIPXMIT_BEST);
517}
518
519/*
520 * Disable Audio InfoFrame Transmission
521 */
522static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
523 hda_nid_t pin_nid)
524{
525 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
526 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
527 AC_DIPXMIT_DISABLE);
528}
529
530static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
531{
532#ifdef CONFIG_SND_DEBUG_VERBOSE
533 int i;
534 int size;
535
536 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100537 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800538
539 for (i = 0; i < 8; i++) {
540 size = snd_hda_codec_read(codec, pin_nid, 0,
541 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100542 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800543 }
544#endif
545}
546
547static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
548{
549#ifdef BE_PARANOID
550 int i, j;
551 int size;
552 int pi, bi;
553 for (i = 0; i < 8; i++) {
554 size = snd_hda_codec_read(codec, pin_nid, 0,
555 AC_VERB_GET_HDMI_DIP_SIZE, i);
556 if (size == 0)
557 continue;
558
559 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
560 for (j = 1; j < 1000; j++) {
561 hdmi_write_dip_byte(codec, pin_nid, 0x0);
562 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
563 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100564 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +0800565 bi, pi, i);
566 if (bi == 0) /* byte index wrapped around */
567 break;
568 }
Takashi Iwai4e76a882014-02-25 12:21:03 +0100569 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800570 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
571 i, size, j);
572 }
573#endif
574}
575
Wu Fengguang53d7d692010-09-21 14:25:49 +0800576static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800577{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800578 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800579 u8 sum = 0;
580 int i;
581
Wu Fengguang53d7d692010-09-21 14:25:49 +0800582 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800583
Wu Fengguang53d7d692010-09-21 14:25:49 +0800584 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800585 sum += bytes[i];
586
Wu Fengguang53d7d692010-09-21 14:25:49 +0800587 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800588}
589
590static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
591 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800592 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800593{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800594 int i;
595
596 hdmi_debug_dip_size(codec, pin_nid);
597 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
598
Wu Fengguang079d88c2010-03-08 10:44:23 +0800599 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800600 for (i = 0; i < size; i++)
601 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800602}
603
604static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800605 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800606{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800607 u8 val;
608 int i;
609
610 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
611 != AC_DIPXMIT_BEST)
612 return false;
613
614 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800615 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800616 val = snd_hda_codec_read(codec, pin_nid, 0,
617 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800618 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800619 return false;
620 }
621
622 return true;
623}
624
Anssi Hannula307229d2013-10-24 21:10:34 +0300625static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
626 hda_nid_t pin_nid,
627 int ca, int active_channels,
628 int conn_type)
629{
630 union audio_infoframe ai;
631
Mengdong Lincaaf5ef2014-03-11 17:12:52 -0400632 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +0300633 if (conn_type == 0) { /* HDMI */
634 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
635
636 hdmi_ai->type = 0x84;
637 hdmi_ai->ver = 0x01;
638 hdmi_ai->len = 0x0a;
639 hdmi_ai->CC02_CT47 = active_channels - 1;
640 hdmi_ai->CA = ca;
641 hdmi_checksum_audio_infoframe(hdmi_ai);
642 } else if (conn_type == 1) { /* DisplayPort */
643 struct dp_audio_infoframe *dp_ai = &ai.dp;
644
645 dp_ai->type = 0x84;
646 dp_ai->len = 0x1b;
647 dp_ai->ver = 0x11 << 2;
648 dp_ai->CC02_CT47 = active_channels - 1;
649 dp_ai->CA = ca;
650 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100651 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300652 pin_nid);
653 return;
654 }
655
656 /*
657 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
658 * sizeof(*dp_ai) to avoid partial match/update problems when
659 * the user switches between HDMI/DP monitors.
660 */
661 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
662 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100663 codec_dbg(codec,
664 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300665 pin_nid,
666 active_channels, ca);
667 hdmi_stop_infoframe_trans(codec, pin_nid);
668 hdmi_fill_audio_infoframe(codec, pin_nid,
669 ai.bytes, sizeof(ai));
670 hdmi_start_infoframe_trans(codec, pin_nid);
671 }
672}
673
Takashi Iwaib0540872013-09-02 12:33:02 +0200674static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
675 struct hdmi_spec_per_pin *per_pin,
676 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800677{
Anssi Hannula307229d2013-10-24 21:10:34 +0300678 struct hdmi_spec *spec = codec->spec;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530679 struct hdac_chmap *chmap = &spec->chmap;
Stephen Warren384a48d2011-06-01 11:14:21 -0600680 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200681 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300682 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600683 struct hdmi_eld *eld;
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530684 int ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800685
Takashi Iwaib0540872013-09-02 12:33:02 +0200686 if (!channels)
687 return;
688
Takashi Iwai44bb6d02016-03-21 12:36:44 +0100689 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
690 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
Mengdong Lin58f7d282013-09-04 16:37:12 -0400691 snd_hda_codec_write(codec, pin_nid, 0,
692 AC_VERB_SET_AMP_GAIN_MUTE,
693 AMP_OUT_UNMUTE);
694
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100695 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800696
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530697 ca = snd_hdac_channel_allocation(&codec->core,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530698 eld->info.spk_alloc, channels,
699 per_pin->chmap_set, non_pcm, per_pin->chmap);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800700
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530701 active_channels = snd_hdac_get_active_channels(ca);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300702
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530703 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
704 active_channels);
Anssi Hannula1df5a062013-10-05 02:25:40 +0300705
Stephen Warren384a48d2011-06-01 11:14:21 -0600706 /*
Anssi Hannula39edac72013-10-07 19:24:52 +0300707 * always configure channel mapping, it may have been changed by the
708 * user in the meantime
709 */
Subhransu S. Prustybb63f722016-03-04 19:59:52 +0530710 snd_hdac_setup_channel_mapping(&spec->chmap,
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530711 pin_nid, non_pcm, ca, channels,
712 per_pin->chmap, per_pin->chmap_set);
Anssi Hannula39edac72013-10-07 19:24:52 +0300713
Anssi Hannula307229d2013-10-24 21:10:34 +0300714 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
715 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +0800716
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200717 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800718}
719
Wu Fengguang079d88c2010-03-08 10:44:23 +0800720/*
721 * Unsolicited events
722 */
723
Takashi Iwaiefe47102013-11-07 13:38:23 +0100724static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200725
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200726static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800727{
728 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200729 int pin_idx = pin_nid_to_pin_index(codec, nid);
730
David Henningsson20ce9022013-12-04 10:19:41 +0800731 if (pin_idx < 0)
732 return;
David Henningsson20ce9022013-12-04 10:19:41 +0800733 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
734 snd_hda_jack_report_sync(codec);
735}
736
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200737static void jack_callback(struct hda_codec *codec,
738 struct hda_jack_callback *jack)
739{
Takashi Iwai2ebab402016-02-09 10:23:52 +0100740 check_presence_and_report(codec, jack->nid);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200741}
742
David Henningsson20ce9022013-12-04 10:19:41 +0800743static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
744{
Takashi Iwai3a938972011-10-28 01:16:55 +0200745 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200746 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400747 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200748
749 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
750 if (!jack)
751 return;
Takashi Iwai3a938972011-10-28 01:16:55 +0200752 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800753
Takashi Iwai4e76a882014-02-25 12:21:03 +0100754 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400755 "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 +0800756 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800757 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800758
Takashi Iwai1a4f69d2014-09-11 15:22:46 +0200759 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800760}
761
762static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
763{
764 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
765 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
766 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
767 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
768
Takashi Iwai4e76a882014-02-25 12:21:03 +0100769 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200770 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600771 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800772 tag,
773 subtag,
774 cp_state,
775 cp_ready);
776
777 /* TODO */
778 if (cp_state)
779 ;
780 if (cp_ready)
781 ;
782}
783
784
785static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
786{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800787 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
788 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
789
Takashi Iwai3a938972011-10-28 01:16:55 +0200790 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100791 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800792 return;
793 }
794
795 if (subtag == 0)
796 hdmi_intrinsic_event(codec, res);
797 else
798 hdmi_non_intrinsic_event(codec, res);
799}
800
Mengdong Lin58f7d282013-09-04 16:37:12 -0400801static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +0800802 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +0200803{
Mengdong Lin58f7d282013-09-04 16:37:12 -0400804 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +0200805
Wang Xingchao53b434f2013-06-18 10:41:53 +0800806 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
807 * thus pins could only choose converter 0 for use. Make sure the
808 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200809 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +0800810 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
811
Takashi Iwaifd678ca2013-06-18 16:28:36 +0200812 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +0200813 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
814 AC_PWRST_D0);
815 msleep(40);
816 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
817 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100818 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 +0200819 }
David Henningsson83f26ad2013-04-10 12:26:07 +0200820}
821
Wu Fengguang079d88c2010-03-08 10:44:23 +0800822/*
823 * Callbacks
824 */
825
Takashi Iwai92f10b32010-08-03 14:21:00 +0200826/* HBR should be Non-PCM, 8 channels */
827#define is_hbr_format(format) \
828 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
829
Anssi Hannula307229d2013-10-24 21:10:34 +0300830static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
831 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800832{
Anssi Hannula307229d2013-10-24 21:10:34 +0300833 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +0200834
Stephen Warren384a48d2011-06-01 11:14:21 -0600835 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
836 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300837 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
838
Anssi Hannula13122e62013-11-10 20:56:10 +0200839 if (pinctl < 0)
840 return hbr ? -EINVAL : 0;
841
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300842 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +0300843 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300844 new_pinctl |= AC_PINCTL_EPT_HBR;
845 else
846 new_pinctl |= AC_PINCTL_EPT_NATIVE;
847
Takashi Iwai4e76a882014-02-25 12:21:03 +0100848 codec_dbg(codec,
849 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600850 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300851 pinctl == new_pinctl ? "" : "new-",
852 new_pinctl);
853
854 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -0600855 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300856 AC_VERB_SET_PIN_WIDGET_CONTROL,
857 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +0300858 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300859 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +0300860
861 return 0;
862}
863
864static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
865 hda_nid_t pin_nid, u32 stream_tag, int format)
866{
867 struct hdmi_spec *spec = codec->spec;
868 int err;
869
Anssi Hannula307229d2013-10-24 21:10:34 +0300870 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,
Takashi Iwai4846a672016-03-21 12:56:46 +0100887 int pin_idx, int *cvt_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;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800928
929 return 0;
930}
931
Mengdong Lin2df67422014-03-20 13:01:06 +0800932/* Assure the pin select the right convetor */
933static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
934 struct hdmi_spec_per_pin *per_pin)
935{
936 hda_nid_t pin_nid = per_pin->pin_nid;
937 int mux_idx, curr;
938
939 mux_idx = per_pin->mux_idx;
940 curr = snd_hda_codec_read(codec, pin_nid, 0,
941 AC_VERB_GET_CONNECT_SEL, 0);
942 if (curr != mux_idx)
943 snd_hda_codec_write_cache(codec, pin_nid, 0,
944 AC_VERB_SET_CONNECT_SEL,
945 mux_idx);
946}
947
Libin Yang42b29872015-12-16 13:42:42 +0800948/* get the mux index for the converter of the pins
949 * converter's mux index is the same for all pins on Intel platform
950 */
951static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
952 hda_nid_t cvt_nid)
953{
954 int i;
955
956 for (i = 0; i < spec->num_cvts; i++)
957 if (spec->cvt_nids[i] == cvt_nid)
958 return i;
959 return -EINVAL;
960}
961
Mengdong Lin300016b2013-11-04 01:13:13 -0500962/* Intel HDMI workaround to fix audio routing issue:
963 * For some Intel display codecs, pins share the same connection list.
964 * So a conveter can be selected by multiple pins and playback on any of these
965 * pins will generate sound on the external display, because audio flows from
966 * the same converter to the display pipeline. Also muting one pin may make
967 * other pins have no sound output.
968 * So this function assures that an assigned converter for a pin is not selected
969 * by any other pins.
970 */
971static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -0400972 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800973{
974 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +0100975 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -0400976 int cvt_idx, curr;
977 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800978
Mengdong Linf82d7d12013-09-21 20:34:45 -0400979 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +0100980 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -0400981 unsigned int wid_caps = get_wcaps(codec, nid);
982 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800983
Mengdong Linf82d7d12013-09-21 20:34:45 -0400984 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800985 continue;
986
Mengdong Linf82d7d12013-09-21 20:34:45 -0400987 if (nid == pin_nid)
988 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +0800989
Mengdong Linf82d7d12013-09-21 20:34:45 -0400990 curr = snd_hda_codec_read(codec, nid, 0,
991 AC_VERB_GET_CONNECT_SEL, 0);
992 if (curr != mux_idx)
993 continue;
994
995 /* choose an unassigned converter. The conveters in the
996 * connection list are in the same order as in the codec.
997 */
998 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
999 per_cvt = get_cvt(spec, cvt_idx);
1000 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001001 codec_dbg(codec,
1002 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001003 cvt_idx, nid);
1004 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001005 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001006 cvt_idx);
1007 break;
1008 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001009 }
1010 }
1011}
1012
Libin Yang42b29872015-12-16 13:42:42 +08001013/* A wrapper of intel_not_share_asigned_cvt() */
1014static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1015 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1016{
1017 int mux_idx;
1018 struct hdmi_spec *spec = codec->spec;
1019
Libin Yang42b29872015-12-16 13:42:42 +08001020 /* On Intel platform, the mapping of converter nid to
1021 * mux index of the pins are always the same.
1022 * The pin nid may be 0, this means all pins will not
1023 * share the converter.
1024 */
1025 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1026 if (mux_idx >= 0)
1027 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1028}
1029
Takashi Iwai4846a672016-03-21 12:56:46 +01001030/* skeleton caller of pin_cvt_fixup ops */
1031static void pin_cvt_fixup(struct hda_codec *codec,
1032 struct hdmi_spec_per_pin *per_pin,
1033 hda_nid_t cvt_nid)
1034{
1035 struct hdmi_spec *spec = codec->spec;
1036
1037 if (spec->ops.pin_cvt_fixup)
1038 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1039}
1040
Libin Yang42b29872015-12-16 13:42:42 +08001041/* called in hdmi_pcm_open when no pin is assigned to the PCM
1042 * in dyn_pcm_assign mode.
1043 */
1044static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1045 struct hda_codec *codec,
1046 struct snd_pcm_substream *substream)
1047{
1048 struct hdmi_spec *spec = codec->spec;
1049 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001050 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001051 struct hdmi_spec_per_cvt *per_cvt = NULL;
1052 int err;
1053
Libin Yangac983792015-12-16 16:48:16 +08001054 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1055 if (pcm_idx < 0)
1056 return -EINVAL;
1057
Takashi Iwai4846a672016-03-21 12:56:46 +01001058 err = hdmi_choose_cvt(codec, -1, &cvt_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001059 if (err)
1060 return err;
1061
1062 per_cvt = get_cvt(spec, cvt_idx);
1063 per_cvt->assigned = 1;
1064 hinfo->nid = per_cvt->cvt_nid;
1065
Takashi Iwai4846a672016-03-21 12:56:46 +01001066 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
Libin Yang42b29872015-12-16 13:42:42 +08001067
Libin Yangac983792015-12-16 16:48:16 +08001068 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001069 /* todo: setup spdif ctls assign */
1070
1071 /* Initially set the converter's capabilities */
1072 hinfo->channels_min = per_cvt->channels_min;
1073 hinfo->channels_max = per_cvt->channels_max;
1074 hinfo->rates = per_cvt->rates;
1075 hinfo->formats = per_cvt->formats;
1076 hinfo->maxbps = per_cvt->maxbps;
1077
1078 /* Store the updated parameters */
1079 runtime->hw.channels_min = hinfo->channels_min;
1080 runtime->hw.channels_max = hinfo->channels_max;
1081 runtime->hw.formats = hinfo->formats;
1082 runtime->hw.rates = hinfo->rates;
1083
1084 snd_pcm_hw_constraint_step(substream->runtime, 0,
1085 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1086 return 0;
1087}
1088
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001089/*
1090 * HDA PCM callbacks
1091 */
1092static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1093 struct hda_codec *codec,
1094 struct snd_pcm_substream *substream)
1095{
1096 struct hdmi_spec *spec = codec->spec;
1097 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai4846a672016-03-21 12:56:46 +01001098 int pin_idx, cvt_idx, pcm_idx;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001099 struct hdmi_spec_per_pin *per_pin;
1100 struct hdmi_eld *eld;
1101 struct hdmi_spec_per_cvt *per_cvt = NULL;
1102 int err;
1103
1104 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001105 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1106 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001107 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001108
Libin Yang42b29872015-12-16 13:42:42 +08001109 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001110 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001111 if (!spec->dyn_pcm_assign) {
1112 if (snd_BUG_ON(pin_idx < 0)) {
1113 mutex_unlock(&spec->pcm_lock);
1114 return -EINVAL;
1115 }
1116 } else {
1117 /* no pin is assigned to the PCM
1118 * PA need pcm open successfully when probe
1119 */
1120 if (pin_idx < 0) {
1121 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1122 mutex_unlock(&spec->pcm_lock);
1123 return err;
1124 }
1125 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001126
Takashi Iwai4846a672016-03-21 12:56:46 +01001127 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001128 if (err < 0) {
1129 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001130 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001131 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001132
1133 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001134 /* Claim converter */
1135 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001136
Libin Yangac983792015-12-16 16:48:16 +08001137 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001138 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001139 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001140 hinfo->nid = per_cvt->cvt_nid;
1141
Takashi Iwaibddee962013-06-18 16:14:22 +02001142 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001143 AC_VERB_SET_CONNECT_SEL,
Takashi Iwai4846a672016-03-21 12:56:46 +01001144 per_pin->mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001145
1146 /* configure unused pins to choose other converters */
Takashi Iwai4846a672016-03-21 12:56:46 +01001147 pin_cvt_fixup(codec, per_pin, 0);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001148
Libin Yang2bf3c852015-12-16 13:42:43 +08001149 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001150
Stephen Warren2def8172011-06-01 11:14:20 -06001151 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001152 hinfo->channels_min = per_cvt->channels_min;
1153 hinfo->channels_max = per_cvt->channels_max;
1154 hinfo->rates = per_cvt->rates;
1155 hinfo->formats = per_cvt->formats;
1156 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001157
Libin Yang42b29872015-12-16 13:42:42 +08001158 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001159 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001160 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001161 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001162 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001163 !hinfo->rates || !hinfo->formats) {
1164 per_cvt->assigned = 0;
1165 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001166 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001167 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001168 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001169 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001170 }
Stephen Warren2def8172011-06-01 11:14:20 -06001171
Libin Yang42b29872015-12-16 13:42:42 +08001172 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001173 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001174 runtime->hw.channels_min = hinfo->channels_min;
1175 runtime->hw.channels_max = hinfo->channels_max;
1176 runtime->hw.formats = hinfo->formats;
1177 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001178
1179 snd_pcm_hw_constraint_step(substream->runtime, 0,
1180 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001181 return 0;
1182}
1183
1184/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001185 * HDA/HDMI auto parsing
1186 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001187static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001188{
1189 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001190 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001191 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001192
1193 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001194 codec_warn(codec,
1195 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001196 pin_nid, get_wcaps(codec, pin_nid));
1197 return -EINVAL;
1198 }
1199
Stephen Warren384a48d2011-06-01 11:14:21 -06001200 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1201 per_pin->mux_nids,
1202 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001203
1204 return 0;
1205}
1206
Libin Yanga76056f2015-12-16 16:48:15 +08001207static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1208 struct hdmi_spec_per_pin *per_pin)
1209{
1210 int i;
1211
1212 /* try the prefer PCM */
1213 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1214 return per_pin->pin_nid_idx;
1215
1216 /* have a second try; check the "reserved area" over num_pins */
1217 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1218 if (!test_bit(i, &spec->pcm_bitmap))
1219 return i;
1220 }
1221
1222 /* the last try; check the empty slots in pins */
1223 for (i = 0; i < spec->num_pins; i++) {
1224 if (!test_bit(i, &spec->pcm_bitmap))
1225 return i;
1226 }
1227 return -EBUSY;
1228}
1229
1230static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1231 struct hdmi_spec_per_pin *per_pin)
1232{
1233 int idx;
1234
1235 /* pcm already be attached to the pin */
1236 if (per_pin->pcm)
1237 return;
1238 idx = hdmi_find_pcm_slot(spec, per_pin);
Libin Yangd10a80d2016-03-01 15:18:26 +08001239 if (idx == -EBUSY)
Libin Yanga76056f2015-12-16 16:48:15 +08001240 return;
1241 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001242 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001243 set_bit(idx, &spec->pcm_bitmap);
1244}
1245
1246static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1247 struct hdmi_spec_per_pin *per_pin)
1248{
1249 int idx;
1250
1251 /* pcm already be detached from the pin */
1252 if (!per_pin->pcm)
1253 return;
1254 idx = per_pin->pcm_idx;
1255 per_pin->pcm_idx = -1;
1256 per_pin->pcm = NULL;
1257 if (idx >= 0 && idx < spec->pcm_used)
1258 clear_bit(idx, &spec->pcm_bitmap);
1259}
1260
Libin Yangac983792015-12-16 16:48:16 +08001261static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1262 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1263{
1264 int mux_idx;
1265
1266 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1267 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1268 break;
1269 return mux_idx;
1270}
1271
1272static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1273
1274static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1275 struct hdmi_spec_per_pin *per_pin)
1276{
1277 struct hda_codec *codec = per_pin->codec;
1278 struct hda_pcm *pcm;
1279 struct hda_pcm_stream *hinfo;
1280 struct snd_pcm_substream *substream;
1281 int mux_idx;
1282 bool non_pcm;
1283
1284 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001285 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001286 else
1287 return;
1288 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1289 return;
1290
1291 /* hdmi audio only uses playback and one substream */
1292 hinfo = pcm->stream;
1293 substream = pcm->pcm->streams[0].substream;
1294
1295 per_pin->cvt_nid = hinfo->nid;
1296
1297 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1298 if (mux_idx < per_pin->num_mux_nids)
1299 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1300 AC_VERB_SET_CONNECT_SEL,
1301 mux_idx);
1302 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1303
1304 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1305 if (substream->runtime)
1306 per_pin->channels = substream->runtime->channels;
1307 per_pin->setup = true;
1308 per_pin->mux_idx = mux_idx;
1309
1310 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1311}
1312
1313static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1314 struct hdmi_spec_per_pin *per_pin)
1315{
1316 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1317 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1318
1319 per_pin->chmap_set = false;
1320 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1321
1322 per_pin->setup = false;
1323 per_pin->channels = 0;
1324}
1325
Takashi Iwaie90247f2015-11-13 09:12:12 +01001326/* update per_pin ELD from the given new ELD;
1327 * setup info frame and notification accordingly
1328 */
1329static void update_eld(struct hda_codec *codec,
1330 struct hdmi_spec_per_pin *per_pin,
1331 struct hdmi_eld *eld)
1332{
1333 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001334 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001335 bool old_eld_valid = pin_eld->eld_valid;
1336 bool eld_changed;
Libin Yangfb087ea2016-02-23 16:33:37 +08001337 int pcm_idx = -1;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001338
Libin Yangfb087ea2016-02-23 16:33:37 +08001339 /* for monitor disconnection, save pcm_idx firstly */
1340 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001341 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001342 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001343 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001344 hdmi_pcm_setup_pin(spec, per_pin);
1345 } else {
1346 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001347 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001348 }
Libin Yanga76056f2015-12-16 16:48:15 +08001349 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001350 /* if pcm_idx == -1, it means this is in monitor connection event
1351 * we can get the correct pcm_idx now.
1352 */
1353 if (pcm_idx == -1)
1354 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001355
Takashi Iwaie90247f2015-11-13 09:12:12 +01001356 if (eld->eld_valid)
1357 snd_hdmi_show_eld(codec, &eld->info);
1358
1359 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1360 if (eld->eld_valid && pin_eld->eld_valid)
1361 if (pin_eld->eld_size != eld->eld_size ||
1362 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1363 eld->eld_size) != 0)
1364 eld_changed = true;
1365
Takashi Iwaibd481282016-03-18 18:01:53 +01001366 pin_eld->monitor_present = eld->monitor_present;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001367 pin_eld->eld_valid = eld->eld_valid;
1368 pin_eld->eld_size = eld->eld_size;
1369 if (eld->eld_valid)
1370 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1371 pin_eld->info = eld->info;
1372
1373 /*
1374 * Re-setup pin and infoframe. This is needed e.g. when
1375 * - sink is first plugged-in
1376 * - transcoder can change during stream playback on Haswell
1377 * and this can make HW reset converter selection on a pin.
1378 */
1379 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
Takashi Iwai4846a672016-03-21 12:56:46 +01001380 pin_cvt_fixup(codec, per_pin, 0);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001381 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 */
Takashi Iwai4846a672016-03-21 12:56:46 +01001715 pin_cvt_fixup(codec, NULL, cvt_nid);
Libin Yang42b29872015-12-16 13:42:42 +08001716 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;
Takashi Iwai4846a672016-03-21 12:56:46 +01001728
1729 /* 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 pin_cvt_fixup(codec, per_pin, 0);
Mengdong Lin2df67422014-03-20 13:01:06 +08001738
Libin Yangddd621f2015-09-02 14:11:40 +08001739 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1740 /* Todo: add DP1.2 MST audio support later */
Takashi Iwai93a9ff12016-03-18 19:45:13 +01001741 if (codec_has_acomp(codec))
1742 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08001743
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001744 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001745 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02001746 per_pin->channels = substream->runtime->channels;
1747 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001748
Takashi Iwaib0540872013-09-02 12:33:02 +02001749 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001750 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07001751 if (spec->dyn_pin_out) {
1752 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1753 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1754 snd_hda_codec_write(codec, pin_nid, 0,
1755 AC_VERB_SET_PIN_WIDGET_CONTROL,
1756 pinctl | PIN_OUT);
1757 }
1758
Libin Yang42b29872015-12-16 13:42:42 +08001759 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1760 stream_tag, format);
1761 mutex_unlock(&spec->pcm_lock);
1762 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001763}
1764
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001765static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1766 struct hda_codec *codec,
1767 struct snd_pcm_substream *substream)
1768{
1769 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1770 return 0;
1771}
1772
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001773static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1774 struct hda_codec *codec,
1775 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001776{
1777 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08001778 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06001779 struct hdmi_spec_per_cvt *per_cvt;
1780 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07001781 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06001782
Stephen Warren384a48d2011-06-01 11:14:21 -06001783 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08001784 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1785 if (snd_BUG_ON(pcm_idx < 0))
1786 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001787 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001788 if (snd_BUG_ON(cvt_idx < 0))
1789 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001790 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001791
1792 snd_BUG_ON(!per_cvt->assigned);
1793 per_cvt->assigned = 0;
1794 hinfo->nid = 0;
1795
Libin Yang42b29872015-12-16 13:42:42 +08001796 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08001797 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001798 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001799 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001800 if (spec->dyn_pcm_assign && pin_idx < 0) {
1801 mutex_unlock(&spec->pcm_lock);
1802 return 0;
1803 }
1804
1805 if (snd_BUG_ON(pin_idx < 0)) {
1806 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06001807 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08001808 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001809 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001810
Stephen Warren75fae112014-01-30 11:52:16 -07001811 if (spec->dyn_pin_out) {
1812 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1813 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1814 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1815 AC_VERB_SET_PIN_WIDGET_CONTROL,
1816 pinctl & ~PIN_OUT);
1817 }
1818
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001819 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001820 per_pin->chmap_set = false;
1821 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001822
1823 per_pin->setup = false;
1824 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001825 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08001826 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06001827 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001828
Stephen Warren384a48d2011-06-01 11:14:21 -06001829 return 0;
1830}
1831
1832static const struct hda_pcm_ops generic_ops = {
1833 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001834 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001835 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001836 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001837};
1838
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05301839static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1840 unsigned char *chmap)
1841{
1842 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1843 struct hdmi_spec *spec = codec->spec;
1844 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1845
1846 /* chmap is already set to 0 in caller */
1847 if (!per_pin)
1848 return;
1849
1850 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
1851}
1852
1853static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1854 unsigned char *chmap, int prepared)
1855{
1856 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1857 struct hdmi_spec *spec = codec->spec;
1858 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1859
1860 mutex_lock(&per_pin->lock);
1861 per_pin->chmap_set = true;
1862 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
1863 if (prepared)
1864 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1865 mutex_unlock(&per_pin->lock);
1866}
1867
1868static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1869{
1870 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
1871 struct hdmi_spec *spec = codec->spec;
1872 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
1873
1874 return per_pin ? true:false;
1875}
1876
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001877static int generic_hdmi_build_pcms(struct hda_codec *codec)
1878{
1879 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001880 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001881
Stephen Warren384a48d2011-06-01 11:14:21 -06001882 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1883 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001884 struct hda_pcm_stream *pstr;
1885
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001886 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001887 if (!info)
1888 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08001889
1890 spec->pcm_rec[pin_idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08001891 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001892 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001893 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001894
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001895 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001896 pstr->substreams = 1;
1897 pstr->ops = generic_ops;
1898 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001899 }
1900
1901 return 0;
1902}
1903
Libin Yang25e4abb2016-01-12 11:13:27 +08001904static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01001905{
Libin Yang25e4abb2016-01-12 11:13:27 +08001906 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01001907
Libin Yang25e4abb2016-01-12 11:13:27 +08001908 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001909}
1910
Libin Yang25e4abb2016-01-12 11:13:27 +08001911static int add_hdmi_jack_kctl(struct hda_codec *codec,
1912 struct hdmi_spec *spec,
1913 int pcm_idx,
Takashi Iwai788d4412015-11-12 15:36:13 +01001914 const char *name)
1915{
1916 struct snd_jack *jack;
1917 int err;
1918
1919 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
1920 true, false);
1921 if (err < 0)
1922 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08001923
1924 spec->pcm_rec[pcm_idx].jack = jack;
1925 jack->private_data = &spec->pcm_rec[pcm_idx];
1926 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01001927 return 0;
1928}
1929
Libin Yang25e4abb2016-01-12 11:13:27 +08001930static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
David Henningsson0b6c49b2011-08-23 16:56:03 +02001931{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001932 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001933 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08001934 struct hdmi_spec_per_pin *per_pin;
1935 struct hda_jack_tbl *jack;
1936 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01001937 bool phantom_jack;
Libin Yang25e4abb2016-01-12 11:13:27 +08001938 int ret;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001939
Takashi Iwai31ef2252011-12-01 17:41:36 +01001940 if (pcmdev > 0)
1941 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Libin Yang25e4abb2016-01-12 11:13:27 +08001942
1943 if (spec->dyn_pcm_assign)
1944 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
1945
1946 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
1947 /* if !dyn_pcm_assign, it must be non-MST mode.
1948 * This means pcms and pins are statically mapped.
1949 * And pcm_idx is pin_idx.
1950 */
1951 per_pin = get_pin(spec, pcm_idx);
Takashi Iwai909cadc2015-11-12 11:52:13 +01001952 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
1953 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01001954 strncat(hdmi_str, " Phantom",
1955 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
Libin Yang25e4abb2016-01-12 11:13:27 +08001956 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
1957 phantom_jack);
1958 if (ret < 0)
1959 return ret;
1960 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1961 if (jack == NULL)
1962 return 0;
1963 /* assign jack->jack to pcm_rec[].jack to
1964 * align with dyn_pcm_assign mode
1965 */
1966 spec->pcm_rec[pcm_idx].jack = jack->jack;
1967 return 0;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001968}
1969
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001970static int generic_hdmi_build_controls(struct hda_codec *codec)
1971{
1972 struct hdmi_spec *spec = codec->spec;
1973 int err;
Libin Yang25e4abb2016-01-12 11:13:27 +08001974 int pin_idx, pcm_idx;
1975
1976
1977 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
1978 err = generic_hdmi_build_jack(codec, pcm_idx);
1979 if (err < 0)
1980 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08001981
1982 /* create the spdif for each pcm
1983 * pin will be bound when monitor is connected
1984 */
1985 if (spec->dyn_pcm_assign)
1986 err = snd_hda_create_dig_out_ctls(codec,
1987 0, spec->cvt_nids[0],
1988 HDA_PCM_TYPE_HDMI);
1989 else {
1990 struct hdmi_spec_per_pin *per_pin =
1991 get_pin(spec, pcm_idx);
1992 err = snd_hda_create_dig_out_ctls(codec,
1993 per_pin->pin_nid,
1994 per_pin->mux_nids[0],
1995 HDA_PCM_TYPE_HDMI);
1996 }
1997 if (err < 0)
1998 return err;
1999 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002000
2001 /* add control for ELD Bytes */
2002 err = hdmi_create_eld_ctl(codec, pcm_idx,
2003 get_pcm_rec(spec, pcm_idx)->device);
2004 if (err < 0)
2005 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002006 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002007
Stephen Warren384a48d2011-06-01 11:14:21 -06002008 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002009 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002010
Takashi Iwai82b1d732011-12-20 15:53:07 +01002011 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002012 }
2013
Takashi Iwaid45e6882012-07-31 11:36:00 +02002014 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002015 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002016 struct hda_pcm *pcm;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002017
Libin Yang022f3442016-02-03 10:48:34 +08002018 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002019 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002020 break;
Subhransu S. Prusty2f6e8a82016-03-04 19:59:51 +05302021 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002022 if (err < 0)
2023 return err;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002024 }
2025
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002026 return 0;
2027}
2028
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002029static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2030{
2031 struct hdmi_spec *spec = codec->spec;
2032 int pin_idx;
2033
2034 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002035 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002036
2037 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002038 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002039 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002040 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002041 }
2042 return 0;
2043}
2044
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002045static int generic_hdmi_init(struct hda_codec *codec)
2046{
2047 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002048 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002049
Stephen Warren384a48d2011-06-01 11:14:21 -06002050 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002051 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002052 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002053
2054 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002055 if (!codec_has_acomp(codec))
2056 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2057 codec->jackpoll_interval > 0 ?
2058 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002059 }
2060 return 0;
2061}
2062
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002063static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2064{
2065 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2066 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002067}
2068
2069static void hdmi_array_free(struct hdmi_spec *spec)
2070{
2071 snd_array_free(&spec->pins);
2072 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002073}
2074
Takashi Iwaia6866322016-03-21 12:18:33 +01002075static void generic_spec_free(struct hda_codec *codec)
2076{
2077 struct hdmi_spec *spec = codec->spec;
2078
2079 if (spec) {
2080 hdmi_array_free(spec);
2081 kfree(spec);
2082 codec->spec = NULL;
2083 }
2084 codec->dp_mst = false;
2085}
2086
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002087static void generic_hdmi_free(struct hda_codec *codec)
2088{
2089 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002090 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002091
Takashi Iwai66032492015-12-01 16:49:35 +01002092 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002093 snd_hdac_i915_register_notifier(NULL);
2094
Stephen Warren384a48d2011-06-01 11:14:21 -06002095 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002096 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002097 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002098 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002099 }
2100
2101 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2102 if (spec->pcm_rec[pcm_idx].jack == NULL)
2103 continue;
2104 if (spec->dyn_pcm_assign)
2105 snd_device_free(codec->card,
2106 spec->pcm_rec[pcm_idx].jack);
2107 else
2108 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002109 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002110
Takashi Iwaia6866322016-03-21 12:18:33 +01002111 generic_spec_free(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002112}
2113
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002114#ifdef CONFIG_PM
2115static int generic_hdmi_resume(struct hda_codec *codec)
2116{
2117 struct hdmi_spec *spec = codec->spec;
2118 int pin_idx;
2119
Pierre Ossmana2833682014-06-18 21:48:09 +02002120 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002121 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002122
2123 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2124 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2125 hdmi_present_sense(per_pin, 1);
2126 }
2127 return 0;
2128}
2129#endif
2130
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002131static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002132 .init = generic_hdmi_init,
2133 .free = generic_hdmi_free,
2134 .build_pcms = generic_hdmi_build_pcms,
2135 .build_controls = generic_hdmi_build_controls,
2136 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002137#ifdef CONFIG_PM
2138 .resume = generic_hdmi_resume,
2139#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002140};
2141
Anssi Hannula307229d2013-10-24 21:10:34 +03002142static const struct hdmi_ops generic_standard_hdmi_ops = {
2143 .pin_get_eld = snd_hdmi_get_eld,
Anssi Hannula307229d2013-10-24 21:10:34 +03002144 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2145 .pin_hbr_setup = hdmi_pin_hbr_setup,
2146 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302147};
2148
Takashi Iwaia6866322016-03-21 12:18:33 +01002149/* allocate codec->spec and assign/initialize generic parser ops */
2150static int alloc_generic_hdmi(struct hda_codec *codec)
2151{
2152 struct hdmi_spec *spec;
2153
2154 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2155 if (!spec)
2156 return -ENOMEM;
2157
2158 spec->ops = generic_standard_hdmi_ops;
2159 mutex_init(&spec->pcm_lock);
2160 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2161
2162 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2163 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2164 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2165
2166 codec->spec = spec;
2167 hdmi_array_init(spec, 4);
2168
2169 codec->patch_ops = generic_hdmi_patch_ops;
2170
2171 return 0;
2172}
2173
2174/* generic HDMI parser */
2175static int patch_generic_hdmi(struct hda_codec *codec)
2176{
2177 int err;
2178
2179 err = alloc_generic_hdmi(codec);
2180 if (err < 0)
2181 return err;
2182
2183 err = hdmi_parse_codec(codec);
2184 if (err < 0) {
2185 generic_spec_free(codec);
2186 return err;
2187 }
2188
2189 generic_hdmi_init_per_pins(codec);
2190 return 0;
2191}
2192
2193/*
2194 * Intel codec parsers and helpers
2195 */
2196
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002197static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2198 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002199{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002200 struct hdmi_spec *spec = codec->spec;
2201 hda_nid_t conns[4];
2202 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002203
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002204 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2205 if (nconns == spec->num_cvts &&
2206 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002207 return;
2208
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002209 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002210 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002211 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002212}
2213
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002214#define INTEL_VENDOR_NID 0x08
2215#define INTEL_GET_VENDOR_VERB 0xf81
2216#define INTEL_SET_VENDOR_VERB 0x781
2217#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2218#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2219
2220static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002221 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002222{
2223 unsigned int vendor_param;
2224
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002225 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2226 INTEL_GET_VENDOR_VERB, 0);
2227 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2228 return;
2229
2230 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2231 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2232 INTEL_SET_VENDOR_VERB, vendor_param);
2233 if (vendor_param == -1)
2234 return;
2235
Takashi Iwai17df3f52013-05-08 08:09:34 +02002236 if (update_tree)
2237 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002238}
2239
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002240static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2241{
2242 unsigned int vendor_param;
2243
2244 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2245 INTEL_GET_VENDOR_VERB, 0);
2246 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2247 return;
2248
2249 /* enable DP1.2 mode */
2250 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002251 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002252 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2253 INTEL_SET_VENDOR_VERB, vendor_param);
2254}
2255
Takashi Iwai17df3f52013-05-08 08:09:34 +02002256/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2257 * Otherwise you may get severe h/w communication errors.
2258 */
2259static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2260 unsigned int power_state)
2261{
2262 if (power_state == AC_PWRST_D0) {
2263 intel_haswell_enable_all_pins(codec, false);
2264 intel_haswell_fixup_enable_dp12(codec);
2265 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002266
Takashi Iwai17df3f52013-05-08 08:09:34 +02002267 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2268 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2269}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002270
David Henningssonf0675d42015-09-03 11:51:34 +02002271static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002272{
2273 struct hda_codec *codec = audio_ptr;
2274 int pin_nid = port + 0x04;
2275
Takashi Iwai4f8e4f32016-03-10 12:02:49 +01002276 /* we assume only from port-B to port-D */
2277 if (port < 1 || port > 3)
2278 return;
2279
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002280 /* skip notification during system suspend (but not in runtime PM);
2281 * the state will be updated at resume
2282 */
2283 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2284 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002285 /* ditto during suspend/resume process itself */
2286 if (atomic_read(&(codec)->core.in_pm))
2287 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002288
David Henningsson25adc132015-08-19 10:48:58 +02002289 check_presence_and_report(codec, pin_nid);
2290}
2291
Takashi Iwaia6866322016-03-21 12:18:33 +01002292/* register i915 component pin_eld_notify callback */
2293static void register_i915_notifier(struct hda_codec *codec)
2294{
2295 struct hdmi_spec *spec = codec->spec;
2296
2297 spec->use_acomp_notifier = true;
2298 spec->i915_audio_ops.audio_ptr = codec;
2299 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2300 * will call pin_eld_notify with using audio_ptr pointer
2301 * We need make sure audio_ptr is really setup
2302 */
2303 wmb();
2304 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2305 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2306}
2307
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002308/* setup_stream ops override for HSW+ */
2309static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
2310 hda_nid_t pin_nid, u32 stream_tag, int format)
2311{
2312 haswell_verify_D0(codec, cvt_nid, pin_nid);
2313 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
2314}
2315
Takashi Iwai4846a672016-03-21 12:56:46 +01002316/* pin_cvt_fixup ops override for HSW+ and VLV+ */
2317static void i915_pin_cvt_fixup(struct hda_codec *codec,
2318 struct hdmi_spec_per_pin *per_pin,
2319 hda_nid_t cvt_nid)
2320{
2321 if (per_pin) {
2322 intel_verify_pin_cvt_connect(codec, per_pin);
2323 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
2324 per_pin->mux_idx);
2325 } else {
2326 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2327 }
2328}
2329
Takashi Iwaia6866322016-03-21 12:18:33 +01002330/* Intel Haswell and onwards; audio component with eld notifier */
2331static int patch_i915_hsw_hdmi(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002332{
2333 struct hdmi_spec *spec;
Takashi Iwaia6866322016-03-21 12:18:33 +01002334 int err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002335
Takashi Iwaia6866322016-03-21 12:18:33 +01002336 /* HSW+ requires i915 binding */
2337 if (!codec->bus->core.audio_component) {
2338 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2339 return -ENODEV;
Takashi Iwai691be972016-03-18 15:10:08 +01002340 }
Takashi Iwai55913112015-12-10 13:03:29 +01002341
Takashi Iwaia6866322016-03-21 12:18:33 +01002342 err = alloc_generic_hdmi(codec);
2343 if (err < 0)
2344 return err;
2345 spec = codec->spec;
2346
2347 intel_haswell_enable_all_pins(codec, true);
2348 intel_haswell_fixup_enable_dp12(codec);
2349
2350 /* For Haswell/Broadwell, the controller is also in the power well and
2351 * can cover the codec power request, and so need not set this flag.
2352 */
2353 if (!is_haswell(codec) && !is_broadwell(codec))
2354 codec->core.link_power_control = 1;
2355
2356 codec->patch_ops.set_power_state = haswell_set_power_state;
2357 codec->dp_mst = true;
2358 codec->depop_delay = 0;
2359 codec->auto_runtime_pm = 1;
2360
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002361 spec->ops.setup_stream = i915_hsw_setup_stream;
Takashi Iwai4846a672016-03-21 12:56:46 +01002362 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
Takashi Iwai2c1c9b82016-03-21 12:42:06 +01002363
Takashi Iwaia6866322016-03-21 12:18:33 +01002364 err = hdmi_parse_codec(codec);
2365 if (err < 0) {
2366 generic_spec_free(codec);
2367 return err;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002368 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002369
Takashi Iwaia6866322016-03-21 12:18:33 +01002370 generic_hdmi_init_per_pins(codec);
2371 register_i915_notifier(codec);
2372 return 0;
2373}
2374
2375/* Intel Baytrail and Braswell; without get_eld notifier */
2376static int patch_i915_byt_hdmi(struct hda_codec *codec)
2377{
2378 struct hdmi_spec *spec;
2379 int err;
2380
2381 /* requires i915 binding */
2382 if (!codec->bus->core.audio_component) {
2383 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2384 return -ENODEV;
2385 }
2386
2387 err = alloc_generic_hdmi(codec);
2388 if (err < 0)
2389 return err;
2390 spec = codec->spec;
2391
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002392 /* For Valleyview/Cherryview, only the display codec is in the display
2393 * power well and can use link_power ops to request/release the power.
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002394 */
Takashi Iwaia6866322016-03-21 12:18:33 +01002395 codec->core.link_power_control = 1;
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002396
Takashi Iwaia6866322016-03-21 12:18:33 +01002397 codec->depop_delay = 0;
2398 codec->auto_runtime_pm = 1;
Takashi Iwai17df3f52013-05-08 08:09:34 +02002399
Takashi Iwai4846a672016-03-21 12:56:46 +01002400 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2401
Takashi Iwaia6866322016-03-21 12:18:33 +01002402 err = hdmi_parse_codec(codec);
2403 if (err < 0) {
2404 generic_spec_free(codec);
2405 return err;
2406 }
Lu, Han2377c3c2015-06-09 16:50:38 +08002407
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002408 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002409 return 0;
2410}
2411
2412/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002413 * Shared non-generic implementations
2414 */
2415
2416static int simple_playback_build_pcms(struct hda_codec *codec)
2417{
2418 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002419 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002420 unsigned int chans;
2421 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002422 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002423
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002424 per_cvt = get_cvt(spec, 0);
2425 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002426 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002427
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002428 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002429 if (!info)
2430 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002431 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002432 info->pcm_type = HDA_PCM_TYPE_HDMI;
2433 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2434 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002435 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002436 if (pstr->channels_max <= 2 && chans && chans <= 16)
2437 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002438
2439 return 0;
2440}
2441
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002442/* unsolicited event for jack sensing */
2443static void simple_hdmi_unsol_event(struct hda_codec *codec,
2444 unsigned int res)
2445{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002446 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002447 snd_hda_jack_report_sync(codec);
2448}
2449
2450/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2451 * as long as spec->pins[] is set correctly
2452 */
2453#define simple_hdmi_build_jack generic_hdmi_build_jack
2454
Stephen Warren3aaf8982011-06-01 11:14:19 -06002455static int simple_playback_build_controls(struct hda_codec *codec)
2456{
2457 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002458 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002459 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002460
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002461 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002462 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2463 per_cvt->cvt_nid,
2464 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002465 if (err < 0)
2466 return err;
2467 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002468}
2469
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002470static int simple_playback_init(struct hda_codec *codec)
2471{
2472 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002473 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2474 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002475
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002476 snd_hda_codec_write(codec, pin, 0,
2477 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2478 /* some codecs require to unmute the pin */
2479 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2480 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2481 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002482 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002483 return 0;
2484}
2485
Stephen Warren3aaf8982011-06-01 11:14:19 -06002486static void simple_playback_free(struct hda_codec *codec)
2487{
2488 struct hdmi_spec *spec = codec->spec;
2489
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002490 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002491 kfree(spec);
2492}
2493
2494/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002495 * Nvidia specific implementations
2496 */
2497
2498#define Nv_VERB_SET_Channel_Allocation 0xF79
2499#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2500#define Nv_VERB_SET_Audio_Protection_On 0xF98
2501#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2502
2503#define nvhdmi_master_con_nid_7x 0x04
2504#define nvhdmi_master_pin_nid_7x 0x05
2505
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002506static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002507 /*front, rear, clfe, rear_surr */
2508 0x6, 0x8, 0xa, 0xc,
2509};
2510
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002511static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2512 /* set audio protect on */
2513 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2514 /* enable digital output on pin widget */
2515 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2516 {} /* terminator */
2517};
2518
2519static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002520 /* set audio protect on */
2521 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2522 /* enable digital output on pin widget */
2523 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2524 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2525 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2526 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2527 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2528 {} /* terminator */
2529};
2530
2531#ifdef LIMITED_RATE_FMT_SUPPORT
2532/* support only the safe format and rate */
2533#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2534#define SUPPORTED_MAXBPS 16
2535#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2536#else
2537/* support all rates and formats */
2538#define SUPPORTED_RATES \
2539 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2540 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2541 SNDRV_PCM_RATE_192000)
2542#define SUPPORTED_MAXBPS 24
2543#define SUPPORTED_FORMATS \
2544 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2545#endif
2546
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002547static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002548{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002549 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2550 return 0;
2551}
2552
2553static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2554{
2555 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002556 return 0;
2557}
2558
Nitin Daga393004b2011-01-10 21:49:31 +05302559static unsigned int channels_2_6_8[] = {
2560 2, 6, 8
2561};
2562
2563static unsigned int channels_2_8[] = {
2564 2, 8
2565};
2566
2567static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2568 .count = ARRAY_SIZE(channels_2_6_8),
2569 .list = channels_2_6_8,
2570 .mask = 0,
2571};
2572
2573static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2574 .count = ARRAY_SIZE(channels_2_8),
2575 .list = channels_2_8,
2576 .mask = 0,
2577};
2578
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002579static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2580 struct hda_codec *codec,
2581 struct snd_pcm_substream *substream)
2582{
2583 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302584 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2585
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002586 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05302587 case 0x10de0002:
2588 case 0x10de0003:
2589 case 0x10de0005:
2590 case 0x10de0006:
2591 hw_constraints_channels = &hw_constraints_2_8_channels;
2592 break;
2593 case 0x10de0007:
2594 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2595 break;
2596 default:
2597 break;
2598 }
2599
2600 if (hw_constraints_channels != NULL) {
2601 snd_pcm_hw_constraint_list(substream->runtime, 0,
2602 SNDRV_PCM_HW_PARAM_CHANNELS,
2603 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002604 } else {
2605 snd_pcm_hw_constraint_step(substream->runtime, 0,
2606 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302607 }
2608
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002609 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2610}
2611
2612static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2613 struct hda_codec *codec,
2614 struct snd_pcm_substream *substream)
2615{
2616 struct hdmi_spec *spec = codec->spec;
2617 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2618}
2619
2620static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2621 struct hda_codec *codec,
2622 unsigned int stream_tag,
2623 unsigned int format,
2624 struct snd_pcm_substream *substream)
2625{
2626 struct hdmi_spec *spec = codec->spec;
2627 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2628 stream_tag, format, substream);
2629}
2630
Takashi Iwaid0b12522012-06-15 14:34:42 +02002631static const struct hda_pcm_stream simple_pcm_playback = {
2632 .substreams = 1,
2633 .channels_min = 2,
2634 .channels_max = 2,
2635 .ops = {
2636 .open = simple_playback_pcm_open,
2637 .close = simple_playback_pcm_close,
2638 .prepare = simple_playback_pcm_prepare
2639 },
2640};
2641
2642static const struct hda_codec_ops simple_hdmi_patch_ops = {
2643 .build_controls = simple_playback_build_controls,
2644 .build_pcms = simple_playback_build_pcms,
2645 .init = simple_playback_init,
2646 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002647 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002648};
2649
2650static int patch_simple_hdmi(struct hda_codec *codec,
2651 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2652{
2653 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002654 struct hdmi_spec_per_cvt *per_cvt;
2655 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002656
2657 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2658 if (!spec)
2659 return -ENOMEM;
2660
2661 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002662 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002663
2664 spec->multiout.num_dacs = 0; /* no analog */
2665 spec->multiout.max_channels = 2;
2666 spec->multiout.dig_out_nid = cvt_nid;
2667 spec->num_cvts = 1;
2668 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002669 per_pin = snd_array_new(&spec->pins);
2670 per_cvt = snd_array_new(&spec->cvts);
2671 if (!per_pin || !per_cvt) {
2672 simple_playback_free(codec);
2673 return -ENOMEM;
2674 }
2675 per_cvt->cvt_nid = cvt_nid;
2676 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002677 spec->pcm_playback = simple_pcm_playback;
2678
2679 codec->patch_ops = simple_hdmi_patch_ops;
2680
2681 return 0;
2682}
2683
Aaron Plattner1f348522011-04-06 17:19:04 -07002684static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2685 int channels)
2686{
2687 unsigned int chanmask;
2688 int chan = channels ? (channels - 1) : 1;
2689
2690 switch (channels) {
2691 default:
2692 case 0:
2693 case 2:
2694 chanmask = 0x00;
2695 break;
2696 case 4:
2697 chanmask = 0x08;
2698 break;
2699 case 6:
2700 chanmask = 0x0b;
2701 break;
2702 case 8:
2703 chanmask = 0x13;
2704 break;
2705 }
2706
2707 /* Set the audio infoframe channel allocation and checksum fields. The
2708 * channel count is computed implicitly by the hardware. */
2709 snd_hda_codec_write(codec, 0x1, 0,
2710 Nv_VERB_SET_Channel_Allocation, chanmask);
2711
2712 snd_hda_codec_write(codec, 0x1, 0,
2713 Nv_VERB_SET_Info_Frame_Checksum,
2714 (0x71 - chan - chanmask));
2715}
2716
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002717static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2718 struct hda_codec *codec,
2719 struct snd_pcm_substream *substream)
2720{
2721 struct hdmi_spec *spec = codec->spec;
2722 int i;
2723
2724 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2725 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2726 for (i = 0; i < 4; i++) {
2727 /* set the stream id */
2728 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2729 AC_VERB_SET_CHANNEL_STREAMID, 0);
2730 /* set the stream format */
2731 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2732 AC_VERB_SET_STREAM_FORMAT, 0);
2733 }
2734
Aaron Plattner1f348522011-04-06 17:19:04 -07002735 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2736 * streams are disabled. */
2737 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2738
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002739 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2740}
2741
2742static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2743 struct hda_codec *codec,
2744 unsigned int stream_tag,
2745 unsigned int format,
2746 struct snd_pcm_substream *substream)
2747{
2748 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002749 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002750 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002751 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002752 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002753 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002754
2755 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002756 per_cvt = get_cvt(spec, 0);
2757 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002758
2759 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002760
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002761 dataDCC2 = 0x2;
2762
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002763 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002764 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002765 snd_hda_codec_write(codec,
2766 nvhdmi_master_con_nid_7x,
2767 0,
2768 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002769 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002770
2771 /* set the stream id */
2772 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2773 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2774
2775 /* set the stream format */
2776 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2777 AC_VERB_SET_STREAM_FORMAT, format);
2778
2779 /* turn on again (if needed) */
2780 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002781 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002782 snd_hda_codec_write(codec,
2783 nvhdmi_master_con_nid_7x,
2784 0,
2785 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002786 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002787 snd_hda_codec_write(codec,
2788 nvhdmi_master_con_nid_7x,
2789 0,
2790 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2791 }
2792
2793 for (i = 0; i < 4; i++) {
2794 if (chs == 2)
2795 channel_id = 0;
2796 else
2797 channel_id = i * 2;
2798
2799 /* turn off SPDIF once;
2800 *otherwise the IEC958 bits won't be updated
2801 */
2802 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002803 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002804 snd_hda_codec_write(codec,
2805 nvhdmi_con_nids_7x[i],
2806 0,
2807 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002808 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002809 /* set the stream id */
2810 snd_hda_codec_write(codec,
2811 nvhdmi_con_nids_7x[i],
2812 0,
2813 AC_VERB_SET_CHANNEL_STREAMID,
2814 (stream_tag << 4) | channel_id);
2815 /* set the stream format */
2816 snd_hda_codec_write(codec,
2817 nvhdmi_con_nids_7x[i],
2818 0,
2819 AC_VERB_SET_STREAM_FORMAT,
2820 format);
2821 /* turn on again (if needed) */
2822 /* enable and set the channel status audio/data flag */
2823 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002824 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002825 snd_hda_codec_write(codec,
2826 nvhdmi_con_nids_7x[i],
2827 0,
2828 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002829 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002830 snd_hda_codec_write(codec,
2831 nvhdmi_con_nids_7x[i],
2832 0,
2833 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2834 }
2835 }
2836
Aaron Plattner1f348522011-04-06 17:19:04 -07002837 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002838
2839 mutex_unlock(&codec->spdif_mutex);
2840 return 0;
2841}
2842
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002843static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002844 .substreams = 1,
2845 .channels_min = 2,
2846 .channels_max = 8,
2847 .nid = nvhdmi_master_con_nid_7x,
2848 .rates = SUPPORTED_RATES,
2849 .maxbps = SUPPORTED_MAXBPS,
2850 .formats = SUPPORTED_FORMATS,
2851 .ops = {
2852 .open = simple_playback_pcm_open,
2853 .close = nvhdmi_8ch_7x_pcm_close,
2854 .prepare = nvhdmi_8ch_7x_pcm_prepare
2855 },
2856};
2857
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002858static int patch_nvhdmi_2ch(struct hda_codec *codec)
2859{
2860 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002861 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2862 nvhdmi_master_pin_nid_7x);
2863 if (err < 0)
2864 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002865
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002866 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002867 /* override the PCM rates, etc, as the codec doesn't give full list */
2868 spec = codec->spec;
2869 spec->pcm_playback.rates = SUPPORTED_RATES;
2870 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2871 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002872 return 0;
2873}
2874
Takashi Iwai53775b02012-08-01 12:17:41 +02002875static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2876{
2877 struct hdmi_spec *spec = codec->spec;
2878 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002879 if (!err) {
2880 struct hda_pcm *info = get_pcm_rec(spec, 0);
2881 info->own_chmap = true;
2882 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002883 return err;
2884}
2885
2886static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2887{
2888 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002889 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002890 struct snd_pcm_chmap *chmap;
2891 int err;
2892
2893 err = simple_playback_build_controls(codec);
2894 if (err < 0)
2895 return err;
2896
2897 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002898 info = get_pcm_rec(spec, 0);
2899 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002900 SNDRV_PCM_STREAM_PLAYBACK,
2901 snd_pcm_alt_chmaps, 8, 0, &chmap);
2902 if (err < 0)
2903 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002904 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02002905 case 0x10de0002:
2906 case 0x10de0003:
2907 case 0x10de0005:
2908 case 0x10de0006:
2909 chmap->channel_mask = (1U << 2) | (1U << 8);
2910 break;
2911 case 0x10de0007:
2912 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2913 }
2914 return 0;
2915}
2916
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002917static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2918{
2919 struct hdmi_spec *spec;
2920 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002921 if (err < 0)
2922 return err;
2923 spec = codec->spec;
2924 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002925 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002926 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002927 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2928 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002929
2930 /* Initialize the audio infoframe channel mask and checksum to something
2931 * valid */
2932 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2933
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002934 return 0;
2935}
2936
2937/*
Anssi Hannula611885b2013-11-03 17:15:00 +02002938 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
2939 * - 0x10de0015
2940 * - 0x10de0040
2941 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302942static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05302943 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02002944{
2945 if (cap->ca_index == 0x00 && channels == 2)
2946 return SNDRV_CTL_TLVT_CHMAP_FIXED;
2947
Subhransu S. Prusty028cb682016-03-14 10:35:06 +05302948 /* If the speaker allocation matches the channel count, it is OK. */
2949 if (cap->channels != channels)
2950 return -1;
2951
2952 /* all channels are remappable freely */
2953 return SNDRV_CTL_TLVT_CHMAP_VAR;
Anssi Hannula611885b2013-11-03 17:15:00 +02002954}
2955
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05302956static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
2957 int ca, int chs, unsigned char *map)
Anssi Hannula611885b2013-11-03 17:15:00 +02002958{
2959 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
2960 return -EINVAL;
2961
2962 return 0;
2963}
2964
2965static int patch_nvhdmi(struct hda_codec *codec)
2966{
2967 struct hdmi_spec *spec;
2968 int err;
2969
2970 err = patch_generic_hdmi(codec);
2971 if (err)
2972 return err;
2973
2974 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07002975 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02002976
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302977 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02002978 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302979 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02002980
2981 return 0;
2982}
2983
2984/*
Thierry Reding26e9a962015-05-05 14:56:20 +02002985 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
2986 * accessed using vendor-defined verbs. These registers can be used for
2987 * interoperability between the HDA and HDMI drivers.
2988 */
2989
2990/* Audio Function Group node */
2991#define NVIDIA_AFG_NID 0x01
2992
2993/*
2994 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
2995 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
2996 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
2997 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
2998 * additional bit (at position 30) to signal the validity of the format.
2999 *
3000 * | 31 | 30 | 29 16 | 15 0 |
3001 * +---------+-------+--------+--------+
3002 * | TRIGGER | VALID | UNUSED | FORMAT |
3003 * +-----------------------------------|
3004 *
3005 * Note that for the trigger bit to take effect it needs to change value
3006 * (i.e. it needs to be toggled).
3007 */
3008#define NVIDIA_GET_SCRATCH0 0xfa6
3009#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3010#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3011#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3012#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3013#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3014#define NVIDIA_SCRATCH_VALID (1 << 6)
3015
3016#define NVIDIA_GET_SCRATCH1 0xfab
3017#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3018#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3019#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3020#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3021
3022/*
3023 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3024 * the format is invalidated so that the HDMI codec can be disabled.
3025 */
3026static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3027{
3028 unsigned int value;
3029
3030 /* bits [31:30] contain the trigger and valid bits */
3031 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3032 NVIDIA_GET_SCRATCH0, 0);
3033 value = (value >> 24) & 0xff;
3034
3035 /* bits [15:0] are used to store the HDA format */
3036 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3037 NVIDIA_SET_SCRATCH0_BYTE0,
3038 (format >> 0) & 0xff);
3039 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3040 NVIDIA_SET_SCRATCH0_BYTE1,
3041 (format >> 8) & 0xff);
3042
3043 /* bits [16:24] are unused */
3044 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3045 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3046
3047 /*
3048 * Bit 30 signals that the data is valid and hence that HDMI audio can
3049 * be enabled.
3050 */
3051 if (format == 0)
3052 value &= ~NVIDIA_SCRATCH_VALID;
3053 else
3054 value |= NVIDIA_SCRATCH_VALID;
3055
3056 /*
3057 * Whenever the trigger bit is toggled, an interrupt is raised in the
3058 * HDMI codec. The HDMI driver will use that as trigger to update its
3059 * configuration.
3060 */
3061 value ^= NVIDIA_SCRATCH_TRIGGER;
3062
3063 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3064 NVIDIA_SET_SCRATCH0_BYTE3, value);
3065}
3066
3067static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3068 struct hda_codec *codec,
3069 unsigned int stream_tag,
3070 unsigned int format,
3071 struct snd_pcm_substream *substream)
3072{
3073 int err;
3074
3075 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3076 format, substream);
3077 if (err < 0)
3078 return err;
3079
3080 /* notify the HDMI codec of the format change */
3081 tegra_hdmi_set_format(codec, format);
3082
3083 return 0;
3084}
3085
3086static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3087 struct hda_codec *codec,
3088 struct snd_pcm_substream *substream)
3089{
3090 /* invalidate the format in the HDMI codec */
3091 tegra_hdmi_set_format(codec, 0);
3092
3093 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3094}
3095
3096static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3097{
3098 struct hdmi_spec *spec = codec->spec;
3099 unsigned int i;
3100
3101 for (i = 0; i < spec->num_pins; i++) {
3102 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3103
3104 if (pcm->pcm_type == type)
3105 return pcm;
3106 }
3107
3108 return NULL;
3109}
3110
3111static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3112{
3113 struct hda_pcm_stream *stream;
3114 struct hda_pcm *pcm;
3115 int err;
3116
3117 err = generic_hdmi_build_pcms(codec);
3118 if (err < 0)
3119 return err;
3120
3121 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3122 if (!pcm)
3123 return -ENODEV;
3124
3125 /*
3126 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3127 * codec about format changes.
3128 */
3129 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3130 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3131 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3132
3133 return 0;
3134}
3135
3136static int patch_tegra_hdmi(struct hda_codec *codec)
3137{
3138 int err;
3139
3140 err = patch_generic_hdmi(codec);
3141 if (err)
3142 return err;
3143
3144 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3145
3146 return 0;
3147}
3148
3149/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003150 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003151 */
3152
Anssi Hannula5a6135842013-10-24 21:10:35 +03003153#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003154 ((codec)->core.vendor_id == 0x1002aa01 && \
3155 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003156#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003157
Anssi Hannula5a6135842013-10-24 21:10:35 +03003158/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3159#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3160#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3161#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3162#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3163#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3164#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003165#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003166#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3167#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3168#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3169#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3170#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3171#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3172#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3173#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3174#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3175#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3176#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003177#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003178#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3179#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3180#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3181#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3182#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3183
Anssi Hannula84d69e72013-10-24 21:10:38 +03003184/* AMD specific HDA cvt verbs */
3185#define ATI_VERB_SET_RAMP_RATE 0x770
3186#define ATI_VERB_GET_RAMP_RATE 0xf70
3187
Anssi Hannula5a6135842013-10-24 21:10:35 +03003188#define ATI_OUT_ENABLE 0x1
3189
3190#define ATI_MULTICHANNEL_MODE_PAIRED 0
3191#define ATI_MULTICHANNEL_MODE_SINGLE 1
3192
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003193#define ATI_HBR_CAPABLE 0x01
3194#define ATI_HBR_ENABLE 0x10
3195
Anssi Hannula89250f82013-10-24 21:10:36 +03003196static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3197 unsigned char *buf, int *eld_size)
3198{
3199 /* call hda_eld.c ATI/AMD-specific function */
3200 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3201 is_amdhdmi_rev3_or_later(codec));
3202}
3203
Anssi Hannula5a6135842013-10-24 21:10:35 +03003204static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3205 int active_channels, int conn_type)
3206{
3207 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3208}
3209
3210static int atihdmi_paired_swap_fc_lfe(int pos)
3211{
3212 /*
3213 * ATI/AMD have automatic FC/LFE swap built-in
3214 * when in pairwise mapping mode.
3215 */
3216
3217 switch (pos) {
3218 /* see channel_allocations[].speakers[] */
3219 case 2: return 3;
3220 case 3: return 2;
3221 default: break;
3222 }
3223
3224 return pos;
3225}
3226
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303227static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3228 int ca, int chs, unsigned char *map)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003229{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303230 struct hdac_cea_channel_speaker_allocation *cap;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003231 int i, j;
3232
3233 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3234
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303235 cap = snd_hdac_get_ch_alloc_from_ca(ca);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003236 for (i = 0; i < chs; ++i) {
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303237 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003238 bool ok = false;
3239 bool companion_ok = false;
3240
3241 if (!mask)
3242 continue;
3243
3244 for (j = 0 + i % 2; j < 8; j += 2) {
3245 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3246 if (cap->speakers[chan_idx] == mask) {
3247 /* channel is in a supported position */
3248 ok = true;
3249
3250 if (i % 2 == 0 && i + 1 < chs) {
3251 /* even channel, check the odd companion */
3252 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303253 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003254 int comp_mask_act = cap->speakers[comp_chan_idx];
3255
3256 if (comp_mask_req == comp_mask_act)
3257 companion_ok = true;
3258 else
3259 return -EINVAL;
3260 }
3261 break;
3262 }
3263 }
3264
3265 if (!ok)
3266 return -EINVAL;
3267
3268 if (companion_ok)
3269 i++; /* companion channel already checked */
3270 }
3271
3272 return 0;
3273}
3274
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303275static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3276 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003277{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303278 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003279 int verb;
3280 int ati_channel_setup = 0;
3281
3282 if (hdmi_slot > 7)
3283 return -EINVAL;
3284
3285 if (!has_amd_full_remap_support(codec)) {
3286 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3287
3288 /* In case this is an odd slot but without stream channel, do not
3289 * disable the slot since the corresponding even slot could have a
3290 * channel. In case neither have a channel, the slot pair will be
3291 * disabled when this function is called for the even slot. */
3292 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3293 return 0;
3294
3295 hdmi_slot -= hdmi_slot % 2;
3296
3297 if (stream_channel != 0xf)
3298 stream_channel -= stream_channel % 2;
3299 }
3300
3301 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3302
3303 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3304
3305 if (stream_channel != 0xf)
3306 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3307
3308 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3309}
3310
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303311static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3312 hda_nid_t pin_nid, int asp_slot)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003313{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303314 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003315 bool was_odd = false;
3316 int ati_asp_slot = asp_slot;
3317 int verb;
3318 int ati_channel_setup;
3319
3320 if (asp_slot > 7)
3321 return -EINVAL;
3322
3323 if (!has_amd_full_remap_support(codec)) {
3324 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3325 if (ati_asp_slot % 2 != 0) {
3326 ati_asp_slot -= 1;
3327 was_odd = true;
3328 }
3329 }
3330
3331 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3332
3333 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3334
3335 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3336 return 0xf;
3337
3338 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3339}
3340
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303341static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3342 struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303343 struct hdac_cea_channel_speaker_allocation *cap,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303344 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003345{
3346 int c;
3347
3348 /*
3349 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3350 * we need to take that into account (a single channel may take 2
3351 * channel slots if we need to carry a silent channel next to it).
3352 * On Rev3+ AMD codecs this function is not used.
3353 */
3354 int chanpairs = 0;
3355
3356 /* We only produce even-numbered channel count TLVs */
3357 if ((channels % 2) != 0)
3358 return -1;
3359
3360 for (c = 0; c < 7; c += 2) {
3361 if (cap->speakers[c] || cap->speakers[c+1])
3362 chanpairs++;
3363 }
3364
3365 if (chanpairs * 2 != channels)
3366 return -1;
3367
3368 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3369}
3370
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303371static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303372 struct hdac_cea_channel_speaker_allocation *cap,
3373 unsigned int *chmap, int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003374{
3375 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3376 int count = 0;
3377 int c;
3378
3379 for (c = 7; c >= 0; c--) {
3380 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3381 int spk = cap->speakers[chan];
3382 if (!spk) {
3383 /* add N/A channel if the companion channel is occupied */
3384 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3385 chmap[count++] = SNDRV_CHMAP_NA;
3386
3387 continue;
3388 }
3389
Subhransu S. Prustybb63f722016-03-04 19:59:52 +05303390 chmap[count++] = snd_hdac_spk_to_chmap(spk);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003391 }
3392
3393 WARN_ON(count != channels);
3394}
3395
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003396static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3397 bool hbr)
3398{
3399 int hbr_ctl, hbr_ctl_new;
3400
3401 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003402 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003403 if (hbr)
3404 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3405 else
3406 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3407
Takashi Iwai4e76a882014-02-25 12:21:03 +01003408 codec_dbg(codec,
3409 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003410 pin_nid,
3411 hbr_ctl == hbr_ctl_new ? "" : "new-",
3412 hbr_ctl_new);
3413
3414 if (hbr_ctl != hbr_ctl_new)
3415 snd_hda_codec_write(codec, pin_nid, 0,
3416 ATI_VERB_SET_HBR_CONTROL,
3417 hbr_ctl_new);
3418
3419 } else if (hbr)
3420 return -EINVAL;
3421
3422 return 0;
3423}
3424
Anssi Hannula84d69e72013-10-24 21:10:38 +03003425static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3426 hda_nid_t pin_nid, u32 stream_tag, int format)
3427{
3428
3429 if (is_amdhdmi_rev3_or_later(codec)) {
3430 int ramp_rate = 180; /* default as per AMD spec */
3431 /* disable ramp-up/down for non-pcm as per AMD spec */
3432 if (format & AC_FMT_TYPE_NON_PCM)
3433 ramp_rate = 0;
3434
3435 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3436 }
3437
3438 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3439}
3440
3441
Anssi Hannula5a6135842013-10-24 21:10:35 +03003442static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003443{
3444 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003445 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003446
Anssi Hannula5a6135842013-10-24 21:10:35 +03003447 err = generic_hdmi_init(codec);
3448
3449 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003450 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003451
3452 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3453 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3454
3455 /* make sure downmix information in infoframe is zero */
3456 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3457
3458 /* enable channel-wise remap mode if supported */
3459 if (has_amd_full_remap_support(codec))
3460 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3461 ATI_VERB_SET_MULTICHANNEL_MODE,
3462 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003463 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003464
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003465 return 0;
3466}
3467
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003468static int patch_atihdmi(struct hda_codec *codec)
3469{
3470 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003471 struct hdmi_spec_per_cvt *per_cvt;
3472 int err, cvt_idx;
3473
3474 err = patch_generic_hdmi(codec);
3475
3476 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003477 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003478
3479 codec->patch_ops.init = atihdmi_init;
3480
Takashi Iwaid0b12522012-06-15 14:34:42 +02003481 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003482
Anssi Hannula89250f82013-10-24 21:10:36 +03003483 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003484 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003485 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003486 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003487
3488 if (!has_amd_full_remap_support(codec)) {
3489 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303490 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03003491 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303492 spec->chmap.ops.cea_alloc_to_tlv_chmap =
3493 atihdmi_paired_cea_alloc_to_tlv_chmap;
3494 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303495 spec->chmap.ops.pin_get_slot_channel =
3496 atihdmi_pin_get_slot_channel;
3497 spec->chmap.ops.pin_set_slot_channel =
3498 atihdmi_pin_set_slot_channel;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003499 }
3500
3501 /* ATI/AMD converters do not advertise all of their capabilities */
3502 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3503 per_cvt = get_cvt(spec, cvt_idx);
3504 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3505 per_cvt->rates |= SUPPORTED_RATES;
3506 per_cvt->formats |= SUPPORTED_FORMATS;
3507 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3508 }
3509
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303510 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003511
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003512 return 0;
3513}
3514
Annie Liu3de5ff82012-06-08 19:18:42 +08003515/* VIA HDMI Implementation */
3516#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3517#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3518
Annie Liu3de5ff82012-06-08 19:18:42 +08003519static int patch_via_hdmi(struct hda_codec *codec)
3520{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003521 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003522}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003523
3524/*
3525 * patch entries
3526 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003527static const struct hda_device_id snd_hda_id_hdmi[] = {
3528HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3529HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3530HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3531HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3532HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3533HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3534HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3535HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3536HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3537HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3538HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3539HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3540HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3541HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3542HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3543HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3544HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3545HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3546HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3547HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3548HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3549HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3550HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01003551/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003552HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3553HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3554HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3555HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3556HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3557HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3558HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3559HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3560HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3561HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3562HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3563HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3564HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3565HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3566HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3567HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3568HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3569HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3570HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3571HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3572HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Aaron Plattner2d369c72016-03-13 13:58:57 -07003573HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08003574HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003575HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3576HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3577HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3578HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3579HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3580HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
3581HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3582HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3583HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3584HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
3585HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
3586HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01003587HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
3588HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
3589HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
3590HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
3591HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003592HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
Takashi Iwaia6866322016-03-21 12:18:33 +01003593HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
3594HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003595HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003596/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003597HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003598{} /* terminator */
3599};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003600MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003601
3602MODULE_LICENSE("GPL");
3603MODULE_DESCRIPTION("HDMI HD-audio codec");
3604MODULE_ALIAS("snd-hda-codec-intelhdmi");
3605MODULE_ALIAS("snd-hda-codec-nvhdmi");
3606MODULE_ALIAS("snd-hda-codec-atihdmi");
3607
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003608static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003609 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003610};
3611
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003612module_hda_codec_driver(hdmi_driver);