blob: 3718b41f4b60129a9bc851cba3327c586ee2ef82 [file] [log] [blame]
Wu Fengguang079d88c2010-03-08 10:44:23 +08001/*
2 *
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
4 *
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
Takashi Iwai84eb01b2010-09-07 12:27:25 +02006 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
Anssi Hannula5a6135842013-10-24 21:10:35 +03009 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
Wu Fengguang079d88c2010-03-08 10:44:23 +080010 *
11 * Authors:
12 * Wu Fengguang <wfg@linux.intel.com>
13 *
14 * Maintained by:
15 * Wu Fengguang <wfg@linux.intel.com>
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
20 * any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software Foundation,
29 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
Takashi Iwai84eb01b2010-09-07 12:27:25 +020032#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040035#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020036#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020037#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080038#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020039#include <sound/tlv.h>
David Henningsson25adc132015-08-19 10:48:58 +020040#include <sound/hdaudio.h>
41#include <sound/hda_i915.h>
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +053042#include <sound/hda_chmap.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020043#include "hda_codec.h"
44#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020045#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020046
Takashi Iwai0ebaa242011-01-11 18:11:04 +010047static bool static_hdmi_pcm;
48module_param(static_hdmi_pcm, bool, 0644);
49MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
50
Takashi Iwai7639a062015-03-03 10:07:24 +010051#define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
52#define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
53#define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
Lu, Hane2656412015-11-11 16:54:27 +080054#define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
Libin Yang91815d82016-01-14 14:09:00 +080055#define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
Libin Yang432ac1a2014-12-16 13:17:34 +080056#define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
Libin Yang91815d82016-01-14 14:09:00 +080057 || is_skylake(codec) || is_broxton(codec) \
58 || is_kabylake(codec))
Mengdong Lin75dcbe42014-01-08 15:55:32 -050059
Takashi Iwai7639a062015-03-03 10:07:24 +010060#define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
61#define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
Libin Yangca2e7222014-08-19 16:20:12 +080062#define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
Mengdong Linfb87fa32013-09-04 16:36:57 -040063
Stephen Warren384a48d2011-06-01 11:14:21 -060064struct hdmi_spec_per_cvt {
65 hda_nid_t cvt_nid;
66 int assigned;
67 unsigned int channels_min;
68 unsigned int channels_max;
69 u32 rates;
70 u64 formats;
71 unsigned int maxbps;
72};
73
Takashi Iwai4eea3092013-02-07 18:18:19 +010074/* max. connections to a widget */
75#define HDA_MAX_CONNECTIONS 32
76
Stephen Warren384a48d2011-06-01 11:14:21 -060077struct hdmi_spec_per_pin {
78 hda_nid_t pin_nid;
Libin Yanga76056f2015-12-16 16:48:15 +080079 /* pin idx, different device entries on the same pin use the same idx */
80 int pin_nid_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -060081 int num_mux_nids;
82 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Mengdong Lin2df67422014-03-20 13:01:06 +080083 int mux_idx;
Anssi Hannula1df5a062013-10-05 02:25:40 +030084 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080085
86 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060087 struct hdmi_eld sink_eld;
Takashi Iwaia4e9a382013-10-17 18:21:12 +020088 struct mutex lock;
Wu Fengguang744626d2011-11-16 16:29:47 +080089 struct delayed_work work;
Libin Yang2bea2412016-01-12 11:13:26 +080090 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Libin Yanga76056f2015-12-16 16:48:15 +080091 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
Wu Fengguangc6e84532011-11-18 16:59:32 -060092 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020093 bool setup; /* the stream has been set up by prepare callback */
94 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020095 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020096 bool chmap_set; /* channel-map override by ALSA API? */
97 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080098#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +020099 struct snd_info_entry *proc_entry;
100#endif
Stephen Warren384a48d2011-06-01 11:14:21 -0600101};
102
Anssi Hannula307229d2013-10-24 21:10:34 +0300103/* operations used by generic code that can be overridden by patches */
104struct hdmi_ops {
105 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
106 unsigned char *buf, int *eld_size);
107
Anssi Hannula307229d2013-10-24 21:10:34 +0300108 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
109 int ca, int active_channels, int conn_type);
110
111 /* enable/disable HBR (HD passthrough) */
112 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
113
114 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
115 hda_nid_t pin_nid, u32 stream_tag, int format);
116
Anssi Hannula307229d2013-10-24 21:10:34 +0300117};
118
Libin Yang2bea2412016-01-12 11:13:26 +0800119struct hdmi_pcm {
120 struct hda_pcm *pcm;
121 struct snd_jack *jack;
Libin Yangfb087ea2016-02-23 16:33:37 +0800122 struct snd_kcontrol *eld_ctl;
Libin Yang2bea2412016-01-12 11:13:26 +0800123};
124
Wu Fengguang079d88c2010-03-08 10:44:23 +0800125struct hdmi_spec {
126 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100127 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
128 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600129
Wu Fengguang079d88c2010-03-08 10:44:23 +0800130 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100131 struct snd_array pins; /* struct hdmi_spec_per_pin */
Libin Yang2bea2412016-01-12 11:13:26 +0800132 struct hdmi_pcm pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800133 struct mutex pcm_lock;
Libin Yanga76056f2015-12-16 16:48:15 +0800134 /* pcm_bitmap means which pcms have been assigned to pins*/
135 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800136 int pcm_used; /* counter of pcm_rec[] */
Libin Yangac983792015-12-16 16:48:16 +0800137 /* bitmap shows whether the pcm is opened in user space
138 * bit 0 means the first playback PCM (PCM3);
139 * bit 1 means the second playback PCM, and so on.
140 */
141 unsigned long pcm_in_use;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800142
David Henningsson4bd038f2013-02-19 16:11:25 +0100143 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300144 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700145
146 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800147 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800148 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300149 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800150 */
151 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200152 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200153
154 /* i915/powerwell (Haswell+/Valleyview+) specific */
155 struct i915_audio_component_audio_ops i915_audio_ops;
Takashi Iwai55913112015-12-10 13:03:29 +0100156 bool i915_bound; /* was i915 bound in this driver? */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +0530157
158 struct hdac_chmap chmap;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800159};
160
Takashi Iwaif4e30402015-12-10 13:01:28 +0100161#ifdef CONFIG_SND_HDA_I915
Takashi Iwai66032492015-12-01 16:49:35 +0100162#define codec_has_acomp(codec) \
163 ((codec)->bus->core.audio_component != NULL)
Takashi Iwaif4e30402015-12-10 13:01:28 +0100164#else
165#define codec_has_acomp(codec) false
166#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800167
168struct hdmi_audio_infoframe {
169 u8 type; /* 0x84 */
170 u8 ver; /* 0x01 */
171 u8 len; /* 0x0a */
172
Wu Fengguang53d7d692010-09-21 14:25:49 +0800173 u8 checksum;
174
Wu Fengguang079d88c2010-03-08 10:44:23 +0800175 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
176 u8 SS01_SF24;
177 u8 CXT04;
178 u8 CA;
179 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800180};
181
182struct dp_audio_infoframe {
183 u8 type; /* 0x84 */
184 u8 len; /* 0x1b */
185 u8 ver; /* 0x11 << 2 */
186
187 u8 CC02_CT47; /* match with HDMI infoframe from this on */
188 u8 SS01_SF24;
189 u8 CXT04;
190 u8 CA;
191 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800192};
193
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100194union audio_infoframe {
195 struct hdmi_audio_infoframe hdmi;
196 struct dp_audio_infoframe dp;
197 u8 bytes[0];
198};
199
Wu Fengguang079d88c2010-03-08 10:44:23 +0800200/*
201 * CEA speaker placement:
202 *
203 * FLH FCH FRH
204 * FLW FL FLC FC FRC FR FRW
205 *
206 * LFE
207 * TC
208 *
209 * RL RLC RC RRC RR
210 *
211 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
212 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
213 */
214enum cea_speaker_placement {
215 FL = (1 << 0), /* Front Left */
216 FC = (1 << 1), /* Front Center */
217 FR = (1 << 2), /* Front Right */
218 FLC = (1 << 3), /* Front Left Center */
219 FRC = (1 << 4), /* Front Right Center */
220 RL = (1 << 5), /* Rear Left */
221 RC = (1 << 6), /* Rear Center */
222 RR = (1 << 7), /* Rear Right */
223 RLC = (1 << 8), /* Rear Left Center */
224 RRC = (1 << 9), /* Rear Right Center */
225 LFE = (1 << 10), /* Low Frequency Effect */
226 FLW = (1 << 11), /* Front Left Wide */
227 FRW = (1 << 12), /* Front Right Wide */
228 FLH = (1 << 13), /* Front Left High */
229 FCH = (1 << 14), /* Front Center High */
230 FRH = (1 << 15), /* Front Right High */
231 TC = (1 << 16), /* Top Center */
232};
233
234/*
235 * ELD SA bits in the CEA Speaker Allocation data block
236 */
237static int eld_speaker_allocation_bits[] = {
238 [0] = FL | FR,
239 [1] = LFE,
240 [2] = FC,
241 [3] = RL | RR,
242 [4] = RC,
243 [5] = FLC | FRC,
244 [6] = RLC | RRC,
245 /* the following are not defined in ELD yet */
246 [7] = FLW | FRW,
247 [8] = FLH | FRH,
248 [9] = TC,
249 [10] = FCH,
250};
251
Wu Fengguang079d88c2010-03-08 10:44:23 +0800252/*
253 * ALSA sequence is:
254 *
255 * surround40 surround41 surround50 surround51 surround71
256 * ch0 front left = = = =
257 * ch1 front right = = = =
258 * ch2 rear left = = = =
259 * ch3 rear right = = = =
260 * ch4 LFE center center center
261 * ch5 LFE LFE
262 * ch6 side left
263 * ch7 side right
264 *
265 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
266 */
267static int hdmi_channel_mapping[0x32][8] = {
268 /* stereo */
269 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
270 /* 2.1 */
271 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
272 /* Dolby Surround */
273 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
274 /* surround40 */
275 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
276 /* 4ch */
277 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
278 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800279 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800280 /* surround50 */
281 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
282 /* surround51 */
283 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
284 /* 7.1 */
285 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
286};
287
288/*
289 * This is an ordered list!
290 *
291 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800292 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800293 */
Subhransu S. Prustyf3022402016-03-04 19:59:48 +0530294static struct hdac_cea_channel_speaker_allocation channel_allocations[] = {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800295/* channel: 7 6 5 4 3 2 1 0 */
296{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
297 /* 2.1 */
298{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
299 /* Dolby Surround */
300{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
301 /* surround40 */
302{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
303 /* surround41 */
304{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
305 /* surround50 */
306{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
307 /* surround51 */
308{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
309 /* 6.1 */
310{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
311 /* surround71 */
312{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
313
314{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
315{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
316{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
317{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
318{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
319{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
320{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
321{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
322{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
323{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
324{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
325{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
326{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
327{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
328{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
329{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
330{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
331{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
332{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
333{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
334{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
335{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
336{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
337{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
338{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
339{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
340{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
341{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
342{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
343{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
344{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
345{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
346{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
347{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
348{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
349{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
350{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
351{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
352{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
353{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
354{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
355};
356
357
358/*
359 * HDMI routines
360 */
361
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100362#define get_pin(spec, idx) \
363 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
364#define get_cvt(spec, idx) \
365 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Libin Yang2bea2412016-01-12 11:13:26 +0800366/* obtain hdmi_pcm object assigned to idx */
367#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
368/* obtain hda_pcm object assigned to idx */
369#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100370
Takashi Iwai4e76a882014-02-25 12:21:03 +0100371static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800372{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100373 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600374 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800375
Stephen Warren384a48d2011-06-01 11:14:21 -0600376 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100377 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600378 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800379
Takashi Iwai4e76a882014-02-25 12:21:03 +0100380 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600381 return -EINVAL;
382}
383
Libin Yang2bf3c852015-12-16 13:42:43 +0800384static int hinfo_to_pcm_index(struct hda_codec *codec,
385 struct hda_pcm_stream *hinfo)
386{
387 struct hdmi_spec *spec = codec->spec;
388 int pcm_idx;
389
390 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
391 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
392 return pcm_idx;
393
394 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
395 return -EINVAL;
396}
397
Takashi Iwai4e76a882014-02-25 12:21:03 +0100398static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600399 struct hda_pcm_stream *hinfo)
400{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100401 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800402 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600403 int pin_idx;
404
Libin Yang6590faa2015-12-16 13:42:41 +0800405 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
406 per_pin = get_pin(spec, pin_idx);
Libin Yang2bea2412016-01-12 11:13:26 +0800407 if (per_pin->pcm &&
408 per_pin->pcm->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600409 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800410 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600411
Libin Yang6590faa2015-12-16 13:42:41 +0800412 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600413 return -EINVAL;
414}
415
Libin Yang022f3442016-02-03 10:48:34 +0800416static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
417 int pcm_idx)
418{
419 int i;
420 struct hdmi_spec_per_pin *per_pin;
421
422 for (i = 0; i < spec->num_pins; i++) {
423 per_pin = get_pin(spec, i);
424 if (per_pin->pcm_idx == pcm_idx)
425 return per_pin;
426 }
427 return NULL;
428}
429
Takashi Iwai4e76a882014-02-25 12:21:03 +0100430static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600431{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100432 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600433 int cvt_idx;
434
435 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100436 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600437 return cvt_idx;
438
Takashi Iwai4e76a882014-02-25 12:21:03 +0100439 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800440 return -EINVAL;
441}
442
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500443static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
444 struct snd_ctl_elem_info *uinfo)
445{
446 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100447 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200448 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100449 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800450 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500451
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500452 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
453
Libin Yangfb087ea2016-02-23 16:33:37 +0800454 pcm_idx = kcontrol->private_value;
455 mutex_lock(&spec->pcm_lock);
456 per_pin = pcm_idx_to_pin(spec, pcm_idx);
457 if (!per_pin) {
458 /* no pin is bound to the pcm */
459 uinfo->count = 0;
460 mutex_unlock(&spec->pcm_lock);
461 return 0;
462 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200463 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100464 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Libin Yangfb087ea2016-02-23 16:33:37 +0800465 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500466
467 return 0;
468}
469
470static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
471 struct snd_ctl_elem_value *ucontrol)
472{
473 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100474 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200475 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100476 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800477 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500478
Libin Yangfb087ea2016-02-23 16:33:37 +0800479 pcm_idx = kcontrol->private_value;
480 mutex_lock(&spec->pcm_lock);
481 per_pin = pcm_idx_to_pin(spec, pcm_idx);
482 if (!per_pin) {
483 /* no pin is bound to the pcm */
484 memset(ucontrol->value.bytes.data, 0,
485 ARRAY_SIZE(ucontrol->value.bytes.data));
486 mutex_unlock(&spec->pcm_lock);
487 return 0;
488 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200489 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500490
David Henningsson360a8242016-02-05 09:05:41 +0100491 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
492 eld->eld_size > ELD_MAX_SIZE) {
Libin Yangfb087ea2016-02-23 16:33:37 +0800493 mutex_unlock(&spec->pcm_lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100494 snd_BUG();
495 return -EINVAL;
496 }
497
498 memset(ucontrol->value.bytes.data, 0,
499 ARRAY_SIZE(ucontrol->value.bytes.data));
500 if (eld->eld_valid)
501 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
502 eld->eld_size);
Libin Yangfb087ea2016-02-23 16:33:37 +0800503 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500504
505 return 0;
506}
507
508static struct snd_kcontrol_new eld_bytes_ctl = {
509 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
510 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
511 .name = "ELD",
512 .info = hdmi_eld_ctl_info,
513 .get = hdmi_eld_ctl_get,
514};
515
Libin Yangfb087ea2016-02-23 16:33:37 +0800516static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500517 int device)
518{
519 struct snd_kcontrol *kctl;
520 struct hdmi_spec *spec = codec->spec;
521 int err;
522
523 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
524 if (!kctl)
525 return -ENOMEM;
Libin Yangfb087ea2016-02-23 16:33:37 +0800526 kctl->private_value = pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500527 kctl->id.device = device;
528
Libin Yangfb087ea2016-02-23 16:33:37 +0800529 /* no pin nid is associated with the kctl now
530 * tbd: associate pin nid to eld ctl later
531 */
532 err = snd_hda_ctl_add(codec, 0, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500533 if (err < 0)
534 return err;
535
Libin Yangfb087ea2016-02-23 16:33:37 +0800536 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500537 return 0;
538}
539
Wu Fengguang079d88c2010-03-08 10:44:23 +0800540#ifdef BE_PARANOID
541static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
542 int *packet_index, int *byte_index)
543{
544 int val;
545
546 val = snd_hda_codec_read(codec, pin_nid, 0,
547 AC_VERB_GET_HDMI_DIP_INDEX, 0);
548
549 *packet_index = val >> 5;
550 *byte_index = val & 0x1f;
551}
552#endif
553
554static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
555 int packet_index, int byte_index)
556{
557 int val;
558
559 val = (packet_index << 5) | (byte_index & 0x1f);
560
561 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
562}
563
564static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
565 unsigned char val)
566{
567 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
568}
569
Stephen Warren384a48d2011-06-01 11:14:21 -0600570static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800571{
Stephen Warren75fae112014-01-30 11:52:16 -0700572 struct hdmi_spec *spec = codec->spec;
573 int pin_out;
574
Wu Fengguang079d88c2010-03-08 10:44:23 +0800575 /* Unmute */
576 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
577 snd_hda_codec_write(codec, pin_nid, 0,
578 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700579
580 if (spec->dyn_pin_out)
581 /* Disable pin out until stream is active */
582 pin_out = 0;
583 else
584 /* Enable pin out: some machines with GM965 gets broken output
585 * when the pin is disabled or changed while using with HDMI
586 */
587 pin_out = PIN_OUT;
588
Wu Fengguang079d88c2010-03-08 10:44:23 +0800589 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700590 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800591}
592
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200593/*
594 * ELD proc files
595 */
596
Jie Yangcd6a6502015-05-27 19:45:45 +0800597#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200598static void print_eld_info(struct snd_info_entry *entry,
599 struct snd_info_buffer *buffer)
600{
601 struct hdmi_spec_per_pin *per_pin = entry->private_data;
602
603 mutex_lock(&per_pin->lock);
604 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
605 mutex_unlock(&per_pin->lock);
606}
607
608static void write_eld_info(struct snd_info_entry *entry,
609 struct snd_info_buffer *buffer)
610{
611 struct hdmi_spec_per_pin *per_pin = entry->private_data;
612
613 mutex_lock(&per_pin->lock);
614 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
615 mutex_unlock(&per_pin->lock);
616}
617
618static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
619{
620 char name[32];
621 struct hda_codec *codec = per_pin->codec;
622 struct snd_info_entry *entry;
623 int err;
624
625 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100626 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200627 if (err < 0)
628 return err;
629
630 snd_info_set_text_ops(entry, per_pin, print_eld_info);
631 entry->c.text.write = write_eld_info;
632 entry->mode |= S_IWUSR;
633 per_pin->proc_entry = entry;
634
635 return 0;
636}
637
638static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
639{
Markus Elfring1947a112015-06-28 11:15:28 +0200640 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200641 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200642 per_pin->proc_entry = NULL;
643 }
644}
645#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200646static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
647 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200648{
649 return 0;
650}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200651static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200652{
653}
654#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800655
656/*
657 * Channel mapping routines
658 */
659
660/*
661 * Compute derived values in channel_allocations[].
662 */
663static void init_channel_allocations(void)
664{
665 int i, j;
Subhransu S. Prustyf3022402016-03-04 19:59:48 +0530666 struct hdac_cea_channel_speaker_allocation *p;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800667
668 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
669 p = channel_allocations + i;
670 p->channels = 0;
671 p->spk_mask = 0;
672 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
673 if (p->speakers[j]) {
674 p->channels++;
675 p->spk_mask |= p->speakers[j];
676 }
677 }
678}
679
Wang Xingchao72357c72012-09-06 10:02:36 +0800680static int get_channel_allocation_order(int ca)
681{
682 int i;
683
684 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
685 if (channel_allocations[i].ca_index == ca)
686 break;
687 }
688 return i;
689}
690
Wu Fengguang079d88c2010-03-08 10:44:23 +0800691/*
692 * The transformation takes two steps:
693 *
694 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
695 * spk_mask => (channel_allocations[]) => ai->CA
696 *
697 * TODO: it could select the wrong CA from multiple candidates.
698*/
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530699static int hdmi_channel_allocation_spk_alloc_blk(struct hdac_device *codec,
700 int spk_alloc, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800701{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800702 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800703 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800704 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800705 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
706
707 /*
708 * CA defaults to 0 for basic stereo audio
709 */
710 if (channels <= 2)
711 return 0;
712
Wu Fengguang079d88c2010-03-08 10:44:23 +0800713 /*
714 * expand ELD's speaker allocation mask
715 *
716 * ELD tells the speaker mask in a compact(paired) form,
717 * expand ELD's notions to match the ones used by Audio InfoFrame.
718 */
719 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530720 if (spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800721 spk_mask |= eld_speaker_allocation_bits[i];
722 }
723
724 /* search for the first working match in the CA table */
725 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
726 if (channels == channel_allocations[i].channels &&
727 (spk_mask & channel_allocations[i].spk_mask) ==
728 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800729 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800730 break;
731 }
732 }
733
Anssi Hannula18e39182013-09-01 14:36:47 +0300734 if (!ca) {
735 /* if there was no match, select the regular ALSA channel
736 * allocation with the matching number of channels */
737 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
738 if (channels == channel_allocations[i].channels) {
739 ca = channel_allocations[i].ca_index;
740 break;
741 }
742 }
743 }
744
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530745 snd_print_channel_allocation(spk_alloc, buf, sizeof(buf));
746 dev_dbg(&codec->dev, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800747 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800748
Wu Fengguang53d7d692010-09-21 14:25:49 +0800749 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800750}
751
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530752static void hdmi_debug_channel_mapping(struct hdac_chmap *chmap,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800753 hda_nid_t pin_nid)
754{
755#ifdef CONFIG_SND_DEBUG_VERBOSE
756 int i;
Anssi Hannula307229d2013-10-24 21:10:34 +0300757 int channel;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800758
759 for (i = 0; i < 8; i++) {
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530760 channel = chmap->ops.pin_get_slot_channel(
761 chmap->hdac, pin_nid, i);
762 dev_dbg(&chmap->hdac->dev, "HDMI: ASP channel %d => slot %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300763 channel, i);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800764 }
765#endif
766}
767
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530768static void hdmi_std_setup_channel_mapping(struct hdac_chmap *chmap,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800769 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800770 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800771 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800772{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +0530773 struct hdac_cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800774 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800775 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800776 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800777 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800778
Wang Xingchao72357c72012-09-06 10:02:36 +0800779 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300780 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800781
Wu Fengguang079d88c2010-03-08 10:44:23 +0800782 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300783 int hdmi_slot = 0;
784 /* fill actual channel mappings in ALSA channel (i) order */
785 for (i = 0; i < ch_alloc->channels; i++) {
786 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
787 hdmi_slot++; /* skip zero slots */
788
789 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
790 }
791 /* fill the rest of the slots with ALSA channel 0xf */
792 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
793 if (!ch_alloc->speakers[7 - hdmi_slot])
794 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800795 }
796
Wang Xingchao433968d2012-09-06 10:02:37 +0800797 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300798 for (i = 0; i < ch_alloc->channels; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300799 non_pcm_mapping[i] = (i << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800800 for (; i < 8; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300801 non_pcm_mapping[i] = (0xf << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800802 }
803
Wu Fengguang079d88c2010-03-08 10:44:23 +0800804 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300805 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
806 int hdmi_slot = slotsetup & 0x0f;
807 int channel = (slotsetup & 0xf0) >> 4;
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530808 err = chmap->ops.pin_set_slot_channel(chmap->hdac,
809 pin_nid, hdmi_slot, channel);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800810 if (err) {
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530811 dev_dbg(&chmap->hdac->dev, "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800812 break;
813 }
814 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800815}
816
Takashi Iwaid45e6882012-07-31 11:36:00 +0200817struct channel_map_table {
818 unsigned char map; /* ALSA API channel map position */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200819 int spk_mask; /* speaker position bit mask */
820};
821
822static struct channel_map_table map_tables[] = {
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300823 { SNDRV_CHMAP_FL, FL },
824 { SNDRV_CHMAP_FR, FR },
825 { SNDRV_CHMAP_RL, RL },
826 { SNDRV_CHMAP_RR, RR },
827 { SNDRV_CHMAP_LFE, LFE },
828 { SNDRV_CHMAP_FC, FC },
829 { SNDRV_CHMAP_RLC, RLC },
830 { SNDRV_CHMAP_RRC, RRC },
831 { SNDRV_CHMAP_RC, RC },
832 { SNDRV_CHMAP_FLC, FLC },
833 { SNDRV_CHMAP_FRC, FRC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200834 { SNDRV_CHMAP_TFL, FLH },
835 { SNDRV_CHMAP_TFR, FRH },
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300836 { SNDRV_CHMAP_FLW, FLW },
837 { SNDRV_CHMAP_FRW, FRW },
838 { SNDRV_CHMAP_TC, TC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200839 { SNDRV_CHMAP_TFC, FCH },
Takashi Iwaid45e6882012-07-31 11:36:00 +0200840 {} /* terminator */
841};
842
843/* from ALSA API channel position to speaker bit mask */
844static int to_spk_mask(unsigned char c)
845{
846 struct channel_map_table *t = map_tables;
847 for (; t->map; t++) {
848 if (t->map == c)
849 return t->spk_mask;
850 }
851 return 0;
852}
853
854/* from ALSA API channel position to CEA slot */
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300855static int to_cea_slot(int ordered_ca, unsigned char pos)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200856{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300857 int mask = to_spk_mask(pos);
858 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200859
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300860 if (mask) {
861 for (i = 0; i < 8; i++) {
862 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
863 return i;
864 }
Takashi Iwaid45e6882012-07-31 11:36:00 +0200865 }
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300866
867 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200868}
869
870/* from speaker bit mask to ALSA API channel position */
871static int spk_to_chmap(int spk)
872{
873 struct channel_map_table *t = map_tables;
874 for (; t->map; t++) {
875 if (t->spk_mask == spk)
876 return t->map;
877 }
878 return 0;
879}
880
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300881/* from CEA slot to ALSA API channel position */
882static int from_cea_slot(int ordered_ca, unsigned char slot)
883{
884 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
885
886 return spk_to_chmap(mask);
887}
888
Takashi Iwaid45e6882012-07-31 11:36:00 +0200889/* get the CA index corresponding to the given ALSA API channel map */
890static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
891{
892 int i, spks = 0, spk_mask = 0;
893
894 for (i = 0; i < chs; i++) {
895 int mask = to_spk_mask(map[i]);
896 if (mask) {
897 spk_mask |= mask;
898 spks++;
899 }
900 }
901
902 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
903 if ((chs == channel_allocations[i].channels ||
904 spks == channel_allocations[i].channels) &&
905 (spk_mask & channel_allocations[i].spk_mask) ==
906 channel_allocations[i].spk_mask)
907 return channel_allocations[i].ca_index;
908 }
909 return -1;
910}
911
912/* set up the channel slots for the given ALSA API channel map */
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530913static int hdmi_manual_setup_channel_mapping(struct hdac_chmap *chmap,
Takashi Iwaid45e6882012-07-31 11:36:00 +0200914 hda_nid_t pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300915 int chs, unsigned char *map,
916 int ca)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200917{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300918 int ordered_ca = get_channel_allocation_order(ca);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300919 int alsa_pos, hdmi_slot;
920 int assignments[8] = {[0 ... 7] = 0xf};
921
922 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
923
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300924 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300925
926 if (hdmi_slot < 0)
927 continue; /* unassigned channel */
928
929 assignments[hdmi_slot] = alsa_pos;
930 }
931
932 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300933 int err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300934
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530935 err = chmap->ops.pin_set_slot_channel(chmap->hdac,
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +0530936 pin_nid, hdmi_slot, assignments[hdmi_slot]);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200937 if (err)
938 return -EINVAL;
939 }
940 return 0;
941}
942
943/* store ALSA API channel map from the current default map */
944static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
945{
946 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300947 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200948 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300949 if (i < channel_allocations[ordered_ca].channels)
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300950 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200951 else
952 map[i] = 0;
953 }
954}
955
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530956static void hdmi_setup_channel_mapping(struct hdac_chmap *chmap,
Takashi Iwaid45e6882012-07-31 11:36:00 +0200957 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200958 int channels, unsigned char *map,
959 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200960{
Anssi Hannula20608732013-02-03 17:55:45 +0200961 if (!non_pcm && chmap_set) {
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530962 hdmi_manual_setup_channel_mapping(chmap, pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300963 channels, map, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200964 } else {
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530965 hdmi_std_setup_channel_mapping(chmap, pin_nid, non_pcm, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200966 hdmi_setup_fake_chmap(map, ca);
967 }
Anssi Hannula980b2492013-10-05 02:25:44 +0300968
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +0530969 hdmi_debug_channel_mapping(chmap, pin_nid);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200970}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800971
972/*
973 * Audio InfoFrame routines
974 */
975
976/*
977 * Enable Audio InfoFrame Transmission
978 */
979static void hdmi_start_infoframe_trans(struct hda_codec *codec,
980 hda_nid_t pin_nid)
981{
982 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
983 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
984 AC_DIPXMIT_BEST);
985}
986
987/*
988 * Disable Audio InfoFrame Transmission
989 */
990static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
991 hda_nid_t pin_nid)
992{
993 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
994 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
995 AC_DIPXMIT_DISABLE);
996}
997
998static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
999{
1000#ifdef CONFIG_SND_DEBUG_VERBOSE
1001 int i;
1002 int size;
1003
1004 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001005 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001006
1007 for (i = 0; i < 8; i++) {
1008 size = snd_hda_codec_read(codec, pin_nid, 0,
1009 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001010 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001011 }
1012#endif
1013}
1014
1015static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1016{
1017#ifdef BE_PARANOID
1018 int i, j;
1019 int size;
1020 int pi, bi;
1021 for (i = 0; i < 8; i++) {
1022 size = snd_hda_codec_read(codec, pin_nid, 0,
1023 AC_VERB_GET_HDMI_DIP_SIZE, i);
1024 if (size == 0)
1025 continue;
1026
1027 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1028 for (j = 1; j < 1000; j++) {
1029 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1030 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1031 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001032 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001033 bi, pi, i);
1034 if (bi == 0) /* byte index wrapped around */
1035 break;
1036 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001037 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001038 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1039 i, size, j);
1040 }
1041#endif
1042}
1043
Wu Fengguang53d7d692010-09-21 14:25:49 +08001044static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001045{
Wu Fengguang53d7d692010-09-21 14:25:49 +08001046 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001047 u8 sum = 0;
1048 int i;
1049
Wu Fengguang53d7d692010-09-21 14:25:49 +08001050 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001051
Wu Fengguang53d7d692010-09-21 14:25:49 +08001052 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001053 sum += bytes[i];
1054
Wu Fengguang53d7d692010-09-21 14:25:49 +08001055 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001056}
1057
1058static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1059 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001060 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001061{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001062 int i;
1063
1064 hdmi_debug_dip_size(codec, pin_nid);
1065 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1066
Wu Fengguang079d88c2010-03-08 10:44:23 +08001067 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001068 for (i = 0; i < size; i++)
1069 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001070}
1071
1072static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001073 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001074{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001075 u8 val;
1076 int i;
1077
1078 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1079 != AC_DIPXMIT_BEST)
1080 return false;
1081
1082 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001083 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001084 val = snd_hda_codec_read(codec, pin_nid, 0,
1085 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001086 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +08001087 return false;
1088 }
1089
1090 return true;
1091}
1092
Anssi Hannula307229d2013-10-24 21:10:34 +03001093static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1094 hda_nid_t pin_nid,
1095 int ca, int active_channels,
1096 int conn_type)
1097{
1098 union audio_infoframe ai;
1099
Mengdong Lincaaf5ef2014-03-11 17:12:52 -04001100 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +03001101 if (conn_type == 0) { /* HDMI */
1102 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1103
1104 hdmi_ai->type = 0x84;
1105 hdmi_ai->ver = 0x01;
1106 hdmi_ai->len = 0x0a;
1107 hdmi_ai->CC02_CT47 = active_channels - 1;
1108 hdmi_ai->CA = ca;
1109 hdmi_checksum_audio_infoframe(hdmi_ai);
1110 } else if (conn_type == 1) { /* DisplayPort */
1111 struct dp_audio_infoframe *dp_ai = &ai.dp;
1112
1113 dp_ai->type = 0x84;
1114 dp_ai->len = 0x1b;
1115 dp_ai->ver = 0x11 << 2;
1116 dp_ai->CC02_CT47 = active_channels - 1;
1117 dp_ai->CA = ca;
1118 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001119 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001120 pin_nid);
1121 return;
1122 }
1123
1124 /*
1125 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1126 * sizeof(*dp_ai) to avoid partial match/update problems when
1127 * the user switches between HDMI/DP monitors.
1128 */
1129 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1130 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001131 codec_dbg(codec,
1132 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001133 pin_nid,
1134 active_channels, ca);
1135 hdmi_stop_infoframe_trans(codec, pin_nid);
1136 hdmi_fill_audio_infoframe(codec, pin_nid,
1137 ai.bytes, sizeof(ai));
1138 hdmi_start_infoframe_trans(codec, pin_nid);
1139 }
1140}
1141
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05301142static int hdmi_get_active_channels(int ca)
1143{
1144 int ordered_ca = get_channel_allocation_order(ca);
1145
1146 return channel_allocations[ordered_ca].channels;
1147}
1148
1149static struct hdac_cea_channel_speaker_allocation *hdmi_get_ch_alloc_from_ca(int ca)
1150{
1151 return &channel_allocations[get_channel_allocation_order(ca)];
1152}
1153
1154static int hdmi_channel_allocation(struct hdac_device *hdac, int spk_alloc,
1155 int channels, bool chmap_set, bool non_pcm, unsigned char *map)
1156{
1157 int ca;
1158
1159 if (!non_pcm && chmap_set)
1160 ca = hdmi_manual_channel_allocation(channels, map);
1161 else
1162 ca = hdmi_channel_allocation_spk_alloc_blk(hdac,
1163 spk_alloc, channels);
1164
1165 if (ca < 0)
1166 ca = 0;
1167
1168 return ca;
1169}
1170
Takashi Iwaib0540872013-09-02 12:33:02 +02001171static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1172 struct hdmi_spec_per_pin *per_pin,
1173 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001174{
Anssi Hannula307229d2013-10-24 21:10:34 +03001175 struct hdmi_spec *spec = codec->spec;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05301176 struct hdac_chmap *chmap = &spec->chmap;
Stephen Warren384a48d2011-06-01 11:14:21 -06001177 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +02001178 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001179 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -06001180 struct hdmi_eld *eld;
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05301181 int ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001182
Takashi Iwaib0540872013-09-02 12:33:02 +02001183 if (!channels)
1184 return;
1185
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001186 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001187 snd_hda_codec_write(codec, pin_nid, 0,
1188 AC_VERB_SET_AMP_GAIN_MUTE,
1189 AMP_OUT_UNMUTE);
1190
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001191 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001192
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05301193 ca = hdmi_channel_allocation(&codec->core,
1194 eld->info.spk_alloc, channels,
1195 per_pin->chmap_set, non_pcm, per_pin->chmap);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001196
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05301197 active_channels = hdmi_get_active_channels(ca);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001198
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05301199 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
1200 active_channels);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001201
Stephen Warren384a48d2011-06-01 11:14:21 -06001202 /*
Anssi Hannula39edac72013-10-07 19:24:52 +03001203 * always configure channel mapping, it may have been changed by the
1204 * user in the meantime
1205 */
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05301206 hdmi_setup_channel_mapping(&spec->chmap,
1207 pin_nid, non_pcm, ca, channels,
1208 per_pin->chmap, per_pin->chmap_set);
Anssi Hannula39edac72013-10-07 19:24:52 +03001209
Anssi Hannula307229d2013-10-24 21:10:34 +03001210 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1211 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +08001212
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001213 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001214}
1215
Wu Fengguang079d88c2010-03-08 10:44:23 +08001216/*
1217 * Unsolicited events
1218 */
1219
Takashi Iwaiefe47102013-11-07 13:38:23 +01001220static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001221
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001222static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001223{
1224 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001225 int pin_idx = pin_nid_to_pin_index(codec, nid);
1226
David Henningsson20ce9022013-12-04 10:19:41 +08001227 if (pin_idx < 0)
1228 return;
David Henningsson20ce9022013-12-04 10:19:41 +08001229 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1230 snd_hda_jack_report_sync(codec);
1231}
1232
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001233static void jack_callback(struct hda_codec *codec,
1234 struct hda_jack_callback *jack)
1235{
Takashi Iwai2ebab402016-02-09 10:23:52 +01001236 check_presence_and_report(codec, jack->nid);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001237}
1238
David Henningsson20ce9022013-12-04 10:19:41 +08001239static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1240{
Takashi Iwai3a938972011-10-28 01:16:55 +02001241 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001242 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001243 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001244
1245 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1246 if (!jack)
1247 return;
Takashi Iwai3a938972011-10-28 01:16:55 +02001248 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001249
Takashi Iwai4e76a882014-02-25 12:21:03 +01001250 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001251 "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 +08001252 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001253 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001254
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001255 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001256}
1257
1258static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1259{
1260 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1261 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1262 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1263 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1264
Takashi Iwai4e76a882014-02-25 12:21:03 +01001265 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001266 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001267 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001268 tag,
1269 subtag,
1270 cp_state,
1271 cp_ready);
1272
1273 /* TODO */
1274 if (cp_state)
1275 ;
1276 if (cp_ready)
1277 ;
1278}
1279
1280
1281static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1282{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001283 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1284 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1285
Takashi Iwai3a938972011-10-28 01:16:55 +02001286 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001287 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001288 return;
1289 }
1290
1291 if (subtag == 0)
1292 hdmi_intrinsic_event(codec, res);
1293 else
1294 hdmi_non_intrinsic_event(codec, res);
1295}
1296
Mengdong Lin58f7d282013-09-04 16:37:12 -04001297static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001298 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001299{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001300 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001301
Wang Xingchao53b434f2013-06-18 10:41:53 +08001302 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1303 * thus pins could only choose converter 0 for use. Make sure the
1304 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001305 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001306 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1307
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001308 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001309 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1310 AC_PWRST_D0);
1311 msleep(40);
1312 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1313 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001314 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 +02001315 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001316}
1317
Wu Fengguang079d88c2010-03-08 10:44:23 +08001318/*
1319 * Callbacks
1320 */
1321
Takashi Iwai92f10b32010-08-03 14:21:00 +02001322/* HBR should be Non-PCM, 8 channels */
1323#define is_hbr_format(format) \
1324 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1325
Anssi Hannula307229d2013-10-24 21:10:34 +03001326static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1327 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001328{
Anssi Hannula307229d2013-10-24 21:10:34 +03001329 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +02001330
Stephen Warren384a48d2011-06-01 11:14:21 -06001331 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1332 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001333 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1334
Anssi Hannula13122e62013-11-10 20:56:10 +02001335 if (pinctl < 0)
1336 return hbr ? -EINVAL : 0;
1337
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001338 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +03001339 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001340 new_pinctl |= AC_PINCTL_EPT_HBR;
1341 else
1342 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1343
Takashi Iwai4e76a882014-02-25 12:21:03 +01001344 codec_dbg(codec,
1345 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001346 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001347 pinctl == new_pinctl ? "" : "new-",
1348 new_pinctl);
1349
1350 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001351 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001352 AC_VERB_SET_PIN_WIDGET_CONTROL,
1353 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +03001354 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001355 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03001356
1357 return 0;
1358}
1359
1360static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1361 hda_nid_t pin_nid, u32 stream_tag, int format)
1362{
1363 struct hdmi_spec *spec = codec->spec;
1364 int err;
1365
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001366 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +03001367 haswell_verify_D0(codec, cvt_nid, pin_nid);
1368
1369 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1370
1371 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001372 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +03001373 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001374 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001375
Stephen Warren384a48d2011-06-01 11:14:21 -06001376 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001377 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001378}
1379
Libin Yang42b29872015-12-16 13:42:42 +08001380/* Try to find an available converter
1381 * If pin_idx is less then zero, just try to find an available converter.
1382 * Otherwise, try to find an available converter and get the cvt mux index
1383 * of the pin.
1384 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001385static int hdmi_choose_cvt(struct hda_codec *codec,
1386 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001387{
1388 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001389 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001390 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001391 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001392
Libin Yang42b29872015-12-16 13:42:42 +08001393 /* pin_idx < 0 means no pin will be bound to the converter */
1394 if (pin_idx < 0)
1395 per_pin = NULL;
1396 else
1397 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001398
Stephen Warren384a48d2011-06-01 11:14:21 -06001399 /* Dynamically assign converter to stream */
1400 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001401 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001402
1403 /* Must not already be assigned */
1404 if (per_cvt->assigned)
1405 continue;
Libin Yang42b29872015-12-16 13:42:42 +08001406 if (per_pin == NULL)
1407 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06001408 /* Must be in pin's mux's list of converters */
1409 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1410 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1411 break;
1412 /* Not in mux list */
1413 if (mux_idx == per_pin->num_mux_nids)
1414 continue;
1415 break;
1416 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001417
Stephen Warren384a48d2011-06-01 11:14:21 -06001418 /* No free converters */
1419 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +08001420 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -06001421
Libin Yang42b29872015-12-16 13:42:42 +08001422 if (per_pin != NULL)
1423 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +08001424
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001425 if (cvt_id)
1426 *cvt_id = cvt_idx;
1427 if (mux_id)
1428 *mux_id = mux_idx;
1429
1430 return 0;
1431}
1432
Mengdong Lin2df67422014-03-20 13:01:06 +08001433/* Assure the pin select the right convetor */
1434static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1435 struct hdmi_spec_per_pin *per_pin)
1436{
1437 hda_nid_t pin_nid = per_pin->pin_nid;
1438 int mux_idx, curr;
1439
1440 mux_idx = per_pin->mux_idx;
1441 curr = snd_hda_codec_read(codec, pin_nid, 0,
1442 AC_VERB_GET_CONNECT_SEL, 0);
1443 if (curr != mux_idx)
1444 snd_hda_codec_write_cache(codec, pin_nid, 0,
1445 AC_VERB_SET_CONNECT_SEL,
1446 mux_idx);
1447}
1448
Libin Yang42b29872015-12-16 13:42:42 +08001449/* get the mux index for the converter of the pins
1450 * converter's mux index is the same for all pins on Intel platform
1451 */
1452static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1453 hda_nid_t cvt_nid)
1454{
1455 int i;
1456
1457 for (i = 0; i < spec->num_cvts; i++)
1458 if (spec->cvt_nids[i] == cvt_nid)
1459 return i;
1460 return -EINVAL;
1461}
1462
Mengdong Lin300016b2013-11-04 01:13:13 -05001463/* Intel HDMI workaround to fix audio routing issue:
1464 * For some Intel display codecs, pins share the same connection list.
1465 * So a conveter can be selected by multiple pins and playback on any of these
1466 * pins will generate sound on the external display, because audio flows from
1467 * the same converter to the display pipeline. Also muting one pin may make
1468 * other pins have no sound output.
1469 * So this function assures that an assigned converter for a pin is not selected
1470 * by any other pins.
1471 */
1472static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001473 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001474{
1475 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001476 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001477 int cvt_idx, curr;
1478 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001479
Mengdong Linf82d7d12013-09-21 20:34:45 -04001480 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +01001481 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -04001482 unsigned int wid_caps = get_wcaps(codec, nid);
1483 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001484
Mengdong Linf82d7d12013-09-21 20:34:45 -04001485 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001486 continue;
1487
Mengdong Linf82d7d12013-09-21 20:34:45 -04001488 if (nid == pin_nid)
1489 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001490
Mengdong Linf82d7d12013-09-21 20:34:45 -04001491 curr = snd_hda_codec_read(codec, nid, 0,
1492 AC_VERB_GET_CONNECT_SEL, 0);
1493 if (curr != mux_idx)
1494 continue;
1495
1496 /* choose an unassigned converter. The conveters in the
1497 * connection list are in the same order as in the codec.
1498 */
1499 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1500 per_cvt = get_cvt(spec, cvt_idx);
1501 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001502 codec_dbg(codec,
1503 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001504 cvt_idx, nid);
1505 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001506 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001507 cvt_idx);
1508 break;
1509 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001510 }
1511 }
1512}
1513
Libin Yang42b29872015-12-16 13:42:42 +08001514/* A wrapper of intel_not_share_asigned_cvt() */
1515static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1516 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1517{
1518 int mux_idx;
1519 struct hdmi_spec *spec = codec->spec;
1520
1521 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1522 return;
1523
1524 /* On Intel platform, the mapping of converter nid to
1525 * mux index of the pins are always the same.
1526 * The pin nid may be 0, this means all pins will not
1527 * share the converter.
1528 */
1529 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1530 if (mux_idx >= 0)
1531 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1532}
1533
1534/* called in hdmi_pcm_open when no pin is assigned to the PCM
1535 * in dyn_pcm_assign mode.
1536 */
1537static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1538 struct hda_codec *codec,
1539 struct snd_pcm_substream *substream)
1540{
1541 struct hdmi_spec *spec = codec->spec;
1542 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001543 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001544 struct hdmi_spec_per_cvt *per_cvt = NULL;
1545 int err;
1546
Libin Yangac983792015-12-16 16:48:16 +08001547 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1548 if (pcm_idx < 0)
1549 return -EINVAL;
1550
Libin Yang42b29872015-12-16 13:42:42 +08001551 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1552 if (err)
1553 return err;
1554
1555 per_cvt = get_cvt(spec, cvt_idx);
1556 per_cvt->assigned = 1;
1557 hinfo->nid = per_cvt->cvt_nid;
1558
1559 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1560
Libin Yangac983792015-12-16 16:48:16 +08001561 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001562 /* todo: setup spdif ctls assign */
1563
1564 /* Initially set the converter's capabilities */
1565 hinfo->channels_min = per_cvt->channels_min;
1566 hinfo->channels_max = per_cvt->channels_max;
1567 hinfo->rates = per_cvt->rates;
1568 hinfo->formats = per_cvt->formats;
1569 hinfo->maxbps = per_cvt->maxbps;
1570
1571 /* Store the updated parameters */
1572 runtime->hw.channels_min = hinfo->channels_min;
1573 runtime->hw.channels_max = hinfo->channels_max;
1574 runtime->hw.formats = hinfo->formats;
1575 runtime->hw.rates = hinfo->rates;
1576
1577 snd_pcm_hw_constraint_step(substream->runtime, 0,
1578 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1579 return 0;
1580}
1581
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001582/*
1583 * HDA PCM callbacks
1584 */
1585static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1586 struct hda_codec *codec,
1587 struct snd_pcm_substream *substream)
1588{
1589 struct hdmi_spec *spec = codec->spec;
1590 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001591 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001592 struct hdmi_spec_per_pin *per_pin;
1593 struct hdmi_eld *eld;
1594 struct hdmi_spec_per_cvt *per_cvt = NULL;
1595 int err;
1596
1597 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001598 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1599 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001600 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001601
Libin Yang42b29872015-12-16 13:42:42 +08001602 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001603 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001604 if (!spec->dyn_pcm_assign) {
1605 if (snd_BUG_ON(pin_idx < 0)) {
1606 mutex_unlock(&spec->pcm_lock);
1607 return -EINVAL;
1608 }
1609 } else {
1610 /* no pin is assigned to the PCM
1611 * PA need pcm open successfully when probe
1612 */
1613 if (pin_idx < 0) {
1614 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1615 mutex_unlock(&spec->pcm_lock);
1616 return err;
1617 }
1618 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001619
1620 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001621 if (err < 0) {
1622 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001623 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001624 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001625
1626 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001627 /* Claim converter */
1628 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001629
Libin Yangac983792015-12-16 16:48:16 +08001630 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001631 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001632 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001633 hinfo->nid = per_cvt->cvt_nid;
1634
Takashi Iwaibddee962013-06-18 16:14:22 +02001635 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001636 AC_VERB_SET_CONNECT_SEL,
1637 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001638
1639 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001640 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001641 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001642
Libin Yang2bf3c852015-12-16 13:42:43 +08001643 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001644
Stephen Warren2def8172011-06-01 11:14:20 -06001645 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001646 hinfo->channels_min = per_cvt->channels_min;
1647 hinfo->channels_max = per_cvt->channels_max;
1648 hinfo->rates = per_cvt->rates;
1649 hinfo->formats = per_cvt->formats;
1650 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001651
Libin Yang42b29872015-12-16 13:42:42 +08001652 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001653 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001654 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001655 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001656 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001657 !hinfo->rates || !hinfo->formats) {
1658 per_cvt->assigned = 0;
1659 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001660 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001661 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001662 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001663 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001664 }
Stephen Warren2def8172011-06-01 11:14:20 -06001665
Libin Yang42b29872015-12-16 13:42:42 +08001666 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001667 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001668 runtime->hw.channels_min = hinfo->channels_min;
1669 runtime->hw.channels_max = hinfo->channels_max;
1670 runtime->hw.formats = hinfo->formats;
1671 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001672
1673 snd_pcm_hw_constraint_step(substream->runtime, 0,
1674 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001675 return 0;
1676}
1677
1678/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001679 * HDA/HDMI auto parsing
1680 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001681static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001682{
1683 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001684 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001685 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001686
1687 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001688 codec_warn(codec,
1689 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001690 pin_nid, get_wcaps(codec, pin_nid));
1691 return -EINVAL;
1692 }
1693
Stephen Warren384a48d2011-06-01 11:14:21 -06001694 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1695 per_pin->mux_nids,
1696 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001697
1698 return 0;
1699}
1700
Libin Yanga76056f2015-12-16 16:48:15 +08001701static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1702 struct hdmi_spec_per_pin *per_pin)
1703{
1704 int i;
1705
1706 /* try the prefer PCM */
1707 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1708 return per_pin->pin_nid_idx;
1709
1710 /* have a second try; check the "reserved area" over num_pins */
1711 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1712 if (!test_bit(i, &spec->pcm_bitmap))
1713 return i;
1714 }
1715
1716 /* the last try; check the empty slots in pins */
1717 for (i = 0; i < spec->num_pins; i++) {
1718 if (!test_bit(i, &spec->pcm_bitmap))
1719 return i;
1720 }
1721 return -EBUSY;
1722}
1723
1724static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1725 struct hdmi_spec_per_pin *per_pin)
1726{
1727 int idx;
1728
1729 /* pcm already be attached to the pin */
1730 if (per_pin->pcm)
1731 return;
1732 idx = hdmi_find_pcm_slot(spec, per_pin);
Libin Yangd10a80d2016-03-01 15:18:26 +08001733 if (idx == -EBUSY)
Libin Yanga76056f2015-12-16 16:48:15 +08001734 return;
1735 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001736 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001737 set_bit(idx, &spec->pcm_bitmap);
1738}
1739
1740static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1741 struct hdmi_spec_per_pin *per_pin)
1742{
1743 int idx;
1744
1745 /* pcm already be detached from the pin */
1746 if (!per_pin->pcm)
1747 return;
1748 idx = per_pin->pcm_idx;
1749 per_pin->pcm_idx = -1;
1750 per_pin->pcm = NULL;
1751 if (idx >= 0 && idx < spec->pcm_used)
1752 clear_bit(idx, &spec->pcm_bitmap);
1753}
1754
Libin Yangac983792015-12-16 16:48:16 +08001755static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1756 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1757{
1758 int mux_idx;
1759
1760 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1761 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1762 break;
1763 return mux_idx;
1764}
1765
1766static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1767
1768static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1769 struct hdmi_spec_per_pin *per_pin)
1770{
1771 struct hda_codec *codec = per_pin->codec;
1772 struct hda_pcm *pcm;
1773 struct hda_pcm_stream *hinfo;
1774 struct snd_pcm_substream *substream;
1775 int mux_idx;
1776 bool non_pcm;
1777
1778 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001779 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001780 else
1781 return;
1782 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1783 return;
1784
1785 /* hdmi audio only uses playback and one substream */
1786 hinfo = pcm->stream;
1787 substream = pcm->pcm->streams[0].substream;
1788
1789 per_pin->cvt_nid = hinfo->nid;
1790
1791 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1792 if (mux_idx < per_pin->num_mux_nids)
1793 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1794 AC_VERB_SET_CONNECT_SEL,
1795 mux_idx);
1796 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1797
1798 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1799 if (substream->runtime)
1800 per_pin->channels = substream->runtime->channels;
1801 per_pin->setup = true;
1802 per_pin->mux_idx = mux_idx;
1803
1804 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1805}
1806
1807static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1808 struct hdmi_spec_per_pin *per_pin)
1809{
1810 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1811 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1812
1813 per_pin->chmap_set = false;
1814 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1815
1816 per_pin->setup = false;
1817 per_pin->channels = 0;
1818}
1819
Takashi Iwaie90247f2015-11-13 09:12:12 +01001820/* update per_pin ELD from the given new ELD;
1821 * setup info frame and notification accordingly
1822 */
1823static void update_eld(struct hda_codec *codec,
1824 struct hdmi_spec_per_pin *per_pin,
1825 struct hdmi_eld *eld)
1826{
1827 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001828 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001829 bool old_eld_valid = pin_eld->eld_valid;
1830 bool eld_changed;
Libin Yangfb087ea2016-02-23 16:33:37 +08001831 int pcm_idx = -1;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001832
Libin Yangfb087ea2016-02-23 16:33:37 +08001833 /* for monitor disconnection, save pcm_idx firstly */
1834 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001835 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001836 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001837 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001838 hdmi_pcm_setup_pin(spec, per_pin);
1839 } else {
1840 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001841 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001842 }
Libin Yanga76056f2015-12-16 16:48:15 +08001843 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001844 /* if pcm_idx == -1, it means this is in monitor connection event
1845 * we can get the correct pcm_idx now.
1846 */
1847 if (pcm_idx == -1)
1848 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001849
Takashi Iwaie90247f2015-11-13 09:12:12 +01001850 if (eld->eld_valid)
1851 snd_hdmi_show_eld(codec, &eld->info);
1852
1853 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1854 if (eld->eld_valid && pin_eld->eld_valid)
1855 if (pin_eld->eld_size != eld->eld_size ||
1856 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1857 eld->eld_size) != 0)
1858 eld_changed = true;
1859
1860 pin_eld->eld_valid = eld->eld_valid;
1861 pin_eld->eld_size = eld->eld_size;
1862 if (eld->eld_valid)
1863 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1864 pin_eld->info = eld->info;
1865
1866 /*
1867 * Re-setup pin and infoframe. This is needed e.g. when
1868 * - sink is first plugged-in
1869 * - transcoder can change during stream playback on Haswell
1870 * and this can make HW reset converter selection on a pin.
1871 */
1872 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1873 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1874 intel_verify_pin_cvt_connect(codec, per_pin);
1875 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1876 per_pin->mux_idx);
1877 }
1878
1879 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1880 }
1881
Libin Yangfb087ea2016-02-23 16:33:37 +08001882 if (eld_changed && pcm_idx >= 0)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001883 snd_ctl_notify(codec->card,
1884 SNDRV_CTL_EVENT_MASK_VALUE |
1885 SNDRV_CTL_EVENT_MASK_INFO,
Libin Yangfb087ea2016-02-23 16:33:37 +08001886 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001887}
1888
Takashi Iwai788d4412015-11-12 15:36:13 +01001889/* update ELD and jack state via HD-audio verbs */
1890static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1891 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001892{
David Henningsson464837a2013-11-07 13:38:25 +01001893 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001894 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001895 struct hdmi_spec *spec = codec->spec;
1896 struct hdmi_eld *eld = &spec->temp_eld;
1897 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001898 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001899 /*
1900 * Always execute a GetPinSense verb here, even when called from
1901 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1902 * response's PD bit is not the real PD value, but indicates that
1903 * the real PD value changed. An older version of the HD-audio
1904 * specification worked this way. Hence, we just ignore the data in
1905 * the unsolicited response to avoid custom WARs.
1906 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001907 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001908 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001909 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001910
Takashi Iwai664c7152015-04-08 11:43:14 +02001911 snd_hda_power_up_pm(codec);
David Henningssonda4a7a32013-12-18 10:46:04 +01001912 present = snd_hda_pin_sense(codec, pin_nid);
1913
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001914 mutex_lock(&per_pin->lock);
David Henningsson4bd038f2013-02-19 16:11:25 +01001915 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1916 if (pin_eld->monitor_present)
1917 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1918 else
1919 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001920
Takashi Iwai4e76a882014-02-25 12:21:03 +01001921 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001922 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001923 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001924
David Henningsson4bd038f2013-02-19 16:11:25 +01001925 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001926 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001927 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001928 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001929 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001930 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001931 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001932 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001933 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001934 if (!eld->eld_valid && repoll)
1935 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001936 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001937
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001938 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001939 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1940 else
1941 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001942
Takashi Iwaiaff747eb2013-11-07 16:39:37 +01001943 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001944
1945 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1946 if (jack)
1947 jack->block_report = !ret;
1948
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001949 mutex_unlock(&per_pin->lock);
Takashi Iwai664c7152015-04-08 11:43:14 +02001950 snd_hda_power_down_pm(codec);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001951 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001952}
1953
Libin Yang31842702016-02-19 15:42:06 +08001954static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1955 struct hdmi_spec_per_pin *per_pin)
1956{
1957 struct hdmi_spec *spec = codec->spec;
1958 struct snd_jack *jack = NULL;
1959 struct hda_jack_tbl *jack_tbl;
1960
1961 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1962 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1963 * NULL even after snd_hda_jack_tbl_clear() is called to
1964 * free snd_jack. This may cause access invalid memory
1965 * when calling snd_jack_report
1966 */
1967 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1968 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1969 else if (!spec->dyn_pcm_assign) {
1970 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1971 if (jack_tbl)
1972 jack = jack_tbl->jack;
1973 }
1974 return jack;
1975}
1976
Takashi Iwai788d4412015-11-12 15:36:13 +01001977/* update ELD and jack state via audio component */
1978static void sync_eld_via_acomp(struct hda_codec *codec,
1979 struct hdmi_spec_per_pin *per_pin)
1980{
Takashi Iwai788d4412015-11-12 15:36:13 +01001981 struct hdmi_spec *spec = codec->spec;
1982 struct hdmi_eld *eld = &spec->temp_eld;
Libin Yang25e4abb2016-01-12 11:13:27 +08001983 struct snd_jack *jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001984 int size;
1985
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001986 mutex_lock(&per_pin->lock);
1987 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1988 &eld->monitor_present, eld->eld_buffer,
1989 ELD_MAX_SIZE);
1990 if (size < 0)
1991 goto unlock;
1992 if (size > 0) {
1993 size = min(size, ELD_MAX_SIZE);
1994 if (snd_hdmi_parse_eld(codec, &eld->info,
1995 eld->eld_buffer, size) < 0)
1996 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001997 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001998
1999 if (size > 0) {
2000 eld->eld_valid = true;
2001 eld->eld_size = size;
2002 } else {
2003 eld->eld_valid = false;
2004 eld->eld_size = 0;
2005 }
2006
Libin Yang25e4abb2016-01-12 11:13:27 +08002007 /* pcm_idx >=0 before update_eld() means it is in monitor
2008 * disconnected event. Jack must be fetched before update_eld()
2009 */
Libin Yang31842702016-02-19 15:42:06 +08002010 jack = pin_idx_to_jack(codec, per_pin);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002011 update_eld(codec, per_pin, eld);
Libin Yang31842702016-02-19 15:42:06 +08002012 if (jack == NULL)
2013 jack = pin_idx_to_jack(codec, per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002014 if (jack == NULL)
2015 goto unlock;
2016 snd_jack_report(jack,
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002017 eld->monitor_present ? SND_JACK_AVOUT : 0);
2018 unlock:
2019 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01002020}
2021
2022static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
2023{
2024 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08002025 struct hdmi_spec *spec = codec->spec;
2026 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01002027
Libin Yanga76056f2015-12-16 16:48:15 +08002028 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01002029 if (codec_has_acomp(codec)) {
2030 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08002031 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01002032 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08002033 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01002034 }
Libin Yanga76056f2015-12-16 16:48:15 +08002035 mutex_unlock(&spec->pcm_lock);
2036
2037 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01002038}
2039
Wu Fengguang744626d2011-11-16 16:29:47 +08002040static void hdmi_repoll_eld(struct work_struct *work)
2041{
2042 struct hdmi_spec_per_pin *per_pin =
2043 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
2044
Wu Fengguangc6e84532011-11-18 16:59:32 -06002045 if (per_pin->repoll_count++ > 6)
2046 per_pin->repoll_count = 0;
2047
Takashi Iwaiefe47102013-11-07 13:38:23 +01002048 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
2049 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08002050}
2051
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002052static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2053 hda_nid_t nid);
2054
Wu Fengguang079d88c2010-03-08 10:44:23 +08002055static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2056{
2057 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002058 unsigned int caps, config;
2059 int pin_idx;
2060 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02002061 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002062
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002063 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002064 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2065 return 0;
2066
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002067 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002068 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2069 return 0;
2070
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002071 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002072 intel_haswell_fixup_connect_list(codec, pin_nid);
2073
Stephen Warren384a48d2011-06-01 11:14:21 -06002074 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002075 per_pin = snd_array_new(&spec->pins);
2076 if (!per_pin)
2077 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002078
2079 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002080 per_pin->non_pcm = false;
Libin Yanga76056f2015-12-16 16:48:15 +08002081 if (spec->dyn_pcm_assign)
2082 per_pin->pcm_idx = -1;
Libin Yang2bea2412016-01-12 11:13:26 +08002083 else {
2084 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
Libin Yanga76056f2015-12-16 16:48:15 +08002085 per_pin->pcm_idx = pin_idx;
Libin Yang2bea2412016-01-12 11:13:26 +08002086 }
Libin Yanga76056f2015-12-16 16:48:15 +08002087 per_pin->pin_nid_idx = pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002088
Stephen Warren384a48d2011-06-01 11:14:21 -06002089 err = hdmi_read_pin_conn(codec, pin_idx);
2090 if (err < 0)
2091 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002092
Wu Fengguang079d88c2010-03-08 10:44:23 +08002093 spec->num_pins++;
2094
Stephen Warren384a48d2011-06-01 11:14:21 -06002095 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002096}
2097
Stephen Warren384a48d2011-06-01 11:14:21 -06002098static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08002099{
2100 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002101 struct hdmi_spec_per_cvt *per_cvt;
2102 unsigned int chans;
2103 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002104
Stephen Warren384a48d2011-06-01 11:14:21 -06002105 chans = get_wcaps(codec, cvt_nid);
2106 chans = get_wcaps_channels(chans);
2107
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002108 per_cvt = snd_array_new(&spec->cvts);
2109 if (!per_cvt)
2110 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002111
2112 per_cvt->cvt_nid = cvt_nid;
2113 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002114 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002115 per_cvt->channels_max = chans;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302116 if (chans > spec->chmap.channels_max)
2117 spec->chmap.channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002118 }
Stephen Warren384a48d2011-06-01 11:14:21 -06002119
2120 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2121 &per_cvt->rates,
2122 &per_cvt->formats,
2123 &per_cvt->maxbps);
2124 if (err < 0)
2125 return err;
2126
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002127 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2128 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2129 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002130
2131 return 0;
2132}
2133
2134static int hdmi_parse_codec(struct hda_codec *codec)
2135{
2136 hda_nid_t nid;
2137 int i, nodes;
2138
Takashi Iwai7639a062015-03-03 10:07:24 +01002139 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002140 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002141 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08002142 return -EINVAL;
2143 }
2144
2145 for (i = 0; i < nodes; i++, nid++) {
2146 unsigned int caps;
2147 unsigned int type;
2148
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002149 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002150 type = get_wcaps_type(caps);
2151
2152 if (!(caps & AC_WCAP_DIGITAL))
2153 continue;
2154
2155 switch (type) {
2156 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06002157 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002158 break;
2159 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08002160 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002161 break;
2162 }
2163 }
2164
Wu Fengguang079d88c2010-03-08 10:44:23 +08002165 return 0;
2166}
2167
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002168/*
2169 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002170static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2171{
2172 struct hda_spdif_out *spdif;
2173 bool non_pcm;
2174
2175 mutex_lock(&codec->spdif_mutex);
2176 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2177 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2178 mutex_unlock(&codec->spdif_mutex);
2179 return non_pcm;
2180}
2181
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002182/*
2183 * HDMI callbacks
2184 */
2185
2186static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2187 struct hda_codec *codec,
2188 unsigned int stream_tag,
2189 unsigned int format,
2190 struct snd_pcm_substream *substream)
2191{
Stephen Warren384a48d2011-06-01 11:14:21 -06002192 hda_nid_t cvt_nid = hinfo->nid;
2193 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08002194 int pin_idx;
2195 struct hdmi_spec_per_pin *per_pin;
2196 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08002197 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002198 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07002199 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08002200 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002201
Libin Yang42b29872015-12-16 13:42:42 +08002202 mutex_lock(&spec->pcm_lock);
2203 pin_idx = hinfo_to_pin_index(codec, hinfo);
2204 if (spec->dyn_pcm_assign && pin_idx < 0) {
2205 /* when dyn_pcm_assign and pcm is not bound to a pin
2206 * skip pin setup and return 0 to make audio playback
2207 * be ongoing
2208 */
2209 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2210 snd_hda_codec_setup_stream(codec, cvt_nid,
2211 stream_tag, 0, format);
2212 mutex_unlock(&spec->pcm_lock);
2213 return 0;
2214 }
2215
2216 if (snd_BUG_ON(pin_idx < 0)) {
2217 mutex_unlock(&spec->pcm_lock);
2218 return -EINVAL;
2219 }
2220 per_pin = get_pin(spec, pin_idx);
2221 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08002222 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08002223 /* Verify pin:cvt selections to avoid silent audio after S3.
2224 * After S3, the audio driver restores pin:cvt selections
2225 * but this can happen before gfx is ready and such selection
2226 * is overlooked by HW. Thus multiple pins can share a same
2227 * default convertor and mute control will affect each other,
2228 * which can cause a resumed audio playback become silent
2229 * after S3.
2230 */
2231 intel_verify_pin_cvt_connect(codec, per_pin);
2232 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2233 }
2234
Libin Yangddd621f2015-09-02 14:11:40 +08002235 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2236 /* Todo: add DP1.2 MST audio support later */
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002237 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08002238
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002239 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002240 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02002241 per_pin->channels = substream->runtime->channels;
2242 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002243
Takashi Iwaib0540872013-09-02 12:33:02 +02002244 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002245 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07002246 if (spec->dyn_pin_out) {
2247 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2248 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2249 snd_hda_codec_write(codec, pin_nid, 0,
2250 AC_VERB_SET_PIN_WIDGET_CONTROL,
2251 pinctl | PIN_OUT);
2252 }
2253
Libin Yang42b29872015-12-16 13:42:42 +08002254 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2255 stream_tag, format);
2256 mutex_unlock(&spec->pcm_lock);
2257 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002258}
2259
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002260static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2261 struct hda_codec *codec,
2262 struct snd_pcm_substream *substream)
2263{
2264 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2265 return 0;
2266}
2267
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002268static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2269 struct hda_codec *codec,
2270 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06002271{
2272 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08002273 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06002274 struct hdmi_spec_per_cvt *per_cvt;
2275 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07002276 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06002277
Stephen Warren384a48d2011-06-01 11:14:21 -06002278 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08002279 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2280 if (snd_BUG_ON(pcm_idx < 0))
2281 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002282 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002283 if (snd_BUG_ON(cvt_idx < 0))
2284 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002285 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002286
2287 snd_BUG_ON(!per_cvt->assigned);
2288 per_cvt->assigned = 0;
2289 hinfo->nid = 0;
2290
Libin Yang42b29872015-12-16 13:42:42 +08002291 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08002292 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08002293 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002294 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08002295 if (spec->dyn_pcm_assign && pin_idx < 0) {
2296 mutex_unlock(&spec->pcm_lock);
2297 return 0;
2298 }
2299
2300 if (snd_BUG_ON(pin_idx < 0)) {
2301 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002302 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08002303 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002304 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002305
Stephen Warren75fae112014-01-30 11:52:16 -07002306 if (spec->dyn_pin_out) {
2307 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2308 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2309 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2310 AC_VERB_SET_PIN_WIDGET_CONTROL,
2311 pinctl & ~PIN_OUT);
2312 }
2313
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002314 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002315 per_pin->chmap_set = false;
2316 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002317
2318 per_pin->setup = false;
2319 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002320 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08002321 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002322 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002323
Stephen Warren384a48d2011-06-01 11:14:21 -06002324 return 0;
2325}
2326
2327static const struct hda_pcm_ops generic_ops = {
2328 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002329 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002330 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002331 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002332};
2333
Takashi Iwaid45e6882012-07-31 11:36:00 +02002334/*
2335 * ALSA API channel-map control callbacks
2336 */
2337static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2338 struct snd_ctl_elem_info *uinfo)
2339{
2340 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302341 struct hdac_chmap *chmap = info->private_data;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302342
Takashi Iwaid45e6882012-07-31 11:36:00 +02002343 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302344 uinfo->count = chmap->channels_max;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002345 uinfo->value.integer.min = 0;
2346 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2347 return 0;
2348}
2349
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302350static int hdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05302351 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula307229d2013-10-24 21:10:34 +03002352{
2353 /* If the speaker allocation matches the channel count, it is OK.*/
2354 if (cap->channels != channels)
2355 return -1;
2356
2357 /* all channels are remappable freely */
2358 return SNDRV_CTL_TLVT_CHMAP_VAR;
2359}
2360
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05302361static void hdmi_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05302362 struct hdac_cea_channel_speaker_allocation *cap,
2363 unsigned int *chmap, int channels)
Anssi Hannula307229d2013-10-24 21:10:34 +03002364{
2365 int count = 0;
2366 int c;
2367
2368 for (c = 7; c >= 0; c--) {
2369 int spk = cap->speakers[c];
2370 if (!spk)
2371 continue;
2372
2373 chmap[count++] = spk_to_chmap(spk);
2374 }
2375
2376 WARN_ON(count != channels);
2377}
2378
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302379static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2380 unsigned char *chmap)
2381{
2382 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2383 struct hdmi_spec *spec = codec->spec;
2384 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2385
2386 /* chmap is already set to 0 in caller */
2387 if (!per_pin)
2388 return;
2389
2390 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2391}
2392
2393static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2394 unsigned char *chmap, int prepared)
2395{
2396 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2397 struct hdmi_spec *spec = codec->spec;
2398 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2399
2400 mutex_lock(&per_pin->lock);
2401 per_pin->chmap_set = true;
2402 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2403 if (prepared)
2404 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2405 mutex_unlock(&per_pin->lock);
2406}
2407
2408static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2409{
2410 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2411 struct hdmi_spec *spec = codec->spec;
2412 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2413
2414 return per_pin ? true:false;
2415}
2416
Takashi Iwaid45e6882012-07-31 11:36:00 +02002417static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2418 unsigned int size, unsigned int __user *tlv)
2419{
2420 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302421 struct hdac_chmap *chmap = info->private_data;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002422 unsigned int __user *dst;
2423 int chs, count = 0;
2424
2425 if (size < 8)
2426 return -ENOMEM;
2427 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2428 return -EFAULT;
2429 size -= 8;
2430 dst = tlv + 2;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302431 for (chs = 2; chs <= chmap->channels_max; chs++) {
Anssi Hannula307229d2013-10-24 21:10:34 +03002432 int i;
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05302433 struct hdac_cea_channel_speaker_allocation *cap;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002434 cap = channel_allocations;
2435 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2436 int chs_bytes = chs * 4;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302437 int type = chmap->ops.chmap_cea_alloc_validate_get_type(
2438 chmap, cap, chs);
Anssi Hannula307229d2013-10-24 21:10:34 +03002439 unsigned int tlv_chmap[8];
2440
2441 if (type < 0)
Takashi Iwaid45e6882012-07-31 11:36:00 +02002442 continue;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002443 if (size < 8)
2444 return -ENOMEM;
Anssi Hannula307229d2013-10-24 21:10:34 +03002445 if (put_user(type, dst) ||
Takashi Iwaid45e6882012-07-31 11:36:00 +02002446 put_user(chs_bytes, dst + 1))
2447 return -EFAULT;
2448 dst += 2;
2449 size -= 8;
2450 count += 8;
2451 if (size < chs_bytes)
2452 return -ENOMEM;
2453 size -= chs_bytes;
2454 count += chs_bytes;
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05302455 chmap->ops.cea_alloc_to_tlv_chmap(chmap, cap,
2456 tlv_chmap, chs);
Anssi Hannula307229d2013-10-24 21:10:34 +03002457 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2458 return -EFAULT;
2459 dst += chs;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002460 }
2461 }
2462 if (put_user(count, tlv + 1))
2463 return -EFAULT;
2464 return 0;
2465}
2466
2467static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2468 struct snd_ctl_elem_value *ucontrol)
2469{
2470 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302471 struct hdac_chmap *chmap = info->private_data;
Libin Yang022f3442016-02-03 10:48:34 +08002472 int pcm_idx = kcontrol->private_value;
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302473 unsigned char pcm_chmap[8];
Takashi Iwaid45e6882012-07-31 11:36:00 +02002474 int i;
2475
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302476 memset(pcm_chmap, 0, sizeof(pcm_chmap));
2477 chmap->ops.get_chmap(chmap->hdac, pcm_idx, pcm_chmap);
Libin Yang022f3442016-02-03 10:48:34 +08002478
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302479 for (i = 0; i < sizeof(chmap); i++)
2480 ucontrol->value.integer.value[i] = pcm_chmap[i];
2481
Takashi Iwaid45e6882012-07-31 11:36:00 +02002482 return 0;
2483}
2484
2485static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_value *ucontrol)
2487{
2488 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302489 struct hdac_chmap *hchmap = info->private_data;
Libin Yang022f3442016-02-03 10:48:34 +08002490 int pcm_idx = kcontrol->private_value;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002491 unsigned int ctl_idx;
2492 struct snd_pcm_substream *substream;
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302493 unsigned char chmap[8], per_pin_chmap[8];
Anssi Hannula307229d2013-10-24 21:10:34 +03002494 int i, err, ca, prepared = 0;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002495
Libin Yang022f3442016-02-03 10:48:34 +08002496 /* No monitor is connected in dyn_pcm_assign.
2497 * It's invalid to setup the chmap
2498 */
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302499 if (!hchmap->ops.is_pcm_attached(hchmap->hdac, pcm_idx))
Libin Yang022f3442016-02-03 10:48:34 +08002500 return 0;
2501
Takashi Iwaid45e6882012-07-31 11:36:00 +02002502 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2503 substream = snd_pcm_chmap_substream(info, ctl_idx);
2504 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01002505 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02002506 switch (substream->runtime->status->state) {
2507 case SNDRV_PCM_STATE_OPEN:
2508 case SNDRV_PCM_STATE_SETUP:
2509 break;
2510 case SNDRV_PCM_STATE_PREPARED:
2511 prepared = 1;
2512 break;
2513 default:
2514 return -EBUSY;
2515 }
2516 memset(chmap, 0, sizeof(chmap));
2517 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2518 chmap[i] = ucontrol->value.integer.value[i];
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302519
2520 hchmap->ops.get_chmap(hchmap->hdac, pcm_idx, per_pin_chmap);
2521 if (!memcmp(chmap, per_pin_chmap, sizeof(chmap)))
Takashi Iwaid45e6882012-07-31 11:36:00 +02002522 return 0;
2523 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2524 if (ca < 0)
2525 return -EINVAL;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302526 if (hchmap->ops.chmap_validate) {
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05302527 err = hchmap->ops.chmap_validate(hchmap, ca,
2528 ARRAY_SIZE(chmap), chmap);
Anssi Hannula307229d2013-10-24 21:10:34 +03002529 if (err)
2530 return err;
2531 }
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302532
2533 hchmap->ops.set_chmap(hchmap->hdac, pcm_idx, chmap, prepared);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002534
2535 return 0;
2536}
2537
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002538static int generic_hdmi_build_pcms(struct hda_codec *codec)
2539{
2540 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002541 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002542
Stephen Warren384a48d2011-06-01 11:14:21 -06002543 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2544 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002545 struct hda_pcm_stream *pstr;
2546
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002547 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002548 if (!info)
2549 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002550
2551 spec->pcm_rec[pin_idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002552 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002553 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002554 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002555
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002556 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002557 pstr->substreams = 1;
2558 pstr->ops = generic_ops;
2559 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002560 }
2561
2562 return 0;
2563}
2564
Libin Yang25e4abb2016-01-12 11:13:27 +08002565static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01002566{
Libin Yang25e4abb2016-01-12 11:13:27 +08002567 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01002568
Libin Yang25e4abb2016-01-12 11:13:27 +08002569 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002570}
2571
Libin Yang25e4abb2016-01-12 11:13:27 +08002572static int add_hdmi_jack_kctl(struct hda_codec *codec,
2573 struct hdmi_spec *spec,
2574 int pcm_idx,
Takashi Iwai788d4412015-11-12 15:36:13 +01002575 const char *name)
2576{
2577 struct snd_jack *jack;
2578 int err;
2579
2580 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2581 true, false);
2582 if (err < 0)
2583 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002584
2585 spec->pcm_rec[pcm_idx].jack = jack;
2586 jack->private_data = &spec->pcm_rec[pcm_idx];
2587 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01002588 return 0;
2589}
2590
Libin Yang25e4abb2016-01-12 11:13:27 +08002591static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
David Henningsson0b6c49b2011-08-23 16:56:03 +02002592{
Takashi Iwai31ef2252011-12-01 17:41:36 +01002593 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02002594 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002595 struct hdmi_spec_per_pin *per_pin;
2596 struct hda_jack_tbl *jack;
2597 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01002598 bool phantom_jack;
Libin Yang25e4abb2016-01-12 11:13:27 +08002599 int ret;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002600
Takashi Iwai31ef2252011-12-01 17:41:36 +01002601 if (pcmdev > 0)
2602 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Libin Yang25e4abb2016-01-12 11:13:27 +08002603
2604 if (spec->dyn_pcm_assign)
2605 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2606
2607 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2608 /* if !dyn_pcm_assign, it must be non-MST mode.
2609 * This means pcms and pins are statically mapped.
2610 * And pcm_idx is pin_idx.
2611 */
2612 per_pin = get_pin(spec, pcm_idx);
Takashi Iwai909cadc2015-11-12 11:52:13 +01002613 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2614 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01002615 strncat(hdmi_str, " Phantom",
2616 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
Libin Yang25e4abb2016-01-12 11:13:27 +08002617 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2618 phantom_jack);
2619 if (ret < 0)
2620 return ret;
2621 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2622 if (jack == NULL)
2623 return 0;
2624 /* assign jack->jack to pcm_rec[].jack to
2625 * align with dyn_pcm_assign mode
2626 */
2627 spec->pcm_rec[pcm_idx].jack = jack->jack;
2628 return 0;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002629}
2630
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002631static int generic_hdmi_build_controls(struct hda_codec *codec)
2632{
2633 struct hdmi_spec *spec = codec->spec;
2634 int err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002635 int pin_idx, pcm_idx;
2636
2637
2638 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2639 err = generic_hdmi_build_jack(codec, pcm_idx);
2640 if (err < 0)
2641 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08002642
2643 /* create the spdif for each pcm
2644 * pin will be bound when monitor is connected
2645 */
2646 if (spec->dyn_pcm_assign)
2647 err = snd_hda_create_dig_out_ctls(codec,
2648 0, spec->cvt_nids[0],
2649 HDA_PCM_TYPE_HDMI);
2650 else {
2651 struct hdmi_spec_per_pin *per_pin =
2652 get_pin(spec, pcm_idx);
2653 err = snd_hda_create_dig_out_ctls(codec,
2654 per_pin->pin_nid,
2655 per_pin->mux_nids[0],
2656 HDA_PCM_TYPE_HDMI);
2657 }
2658 if (err < 0)
2659 return err;
2660 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002661
2662 /* add control for ELD Bytes */
2663 err = hdmi_create_eld_ctl(codec, pcm_idx,
2664 get_pcm_rec(spec, pcm_idx)->device);
2665 if (err < 0)
2666 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002667 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002668
Stephen Warren384a48d2011-06-01 11:14:21 -06002669 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002670 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002671
Takashi Iwai82b1d732011-12-20 15:53:07 +01002672 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002673 }
2674
Takashi Iwaid45e6882012-07-31 11:36:00 +02002675 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002676 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002677 struct hda_pcm *pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002678 struct snd_pcm_chmap *chmap;
2679 struct snd_kcontrol *kctl;
2680 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002681
Libin Yang022f3442016-02-03 10:48:34 +08002682 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002683 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002684 break;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002685 err = snd_pcm_add_chmap_ctls(pcm->pcm,
Takashi Iwaid45e6882012-07-31 11:36:00 +02002686 SNDRV_PCM_STREAM_PLAYBACK,
Libin Yang022f3442016-02-03 10:48:34 +08002687 NULL, 0, pcm_idx, &chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002688 if (err < 0)
2689 return err;
2690 /* override handlers */
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302691 chmap->private_data = &spec->chmap;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002692 kctl = chmap->kctl;
2693 for (i = 0; i < kctl->count; i++)
2694 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2695 kctl->info = hdmi_chmap_ctl_info;
2696 kctl->get = hdmi_chmap_ctl_get;
2697 kctl->put = hdmi_chmap_ctl_put;
2698 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2699 }
2700
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002701 return 0;
2702}
2703
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002704static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2705{
2706 struct hdmi_spec *spec = codec->spec;
2707 int pin_idx;
2708
2709 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002710 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002711
2712 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002713 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002714 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002715 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002716 }
2717 return 0;
2718}
2719
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002720static int generic_hdmi_init(struct hda_codec *codec)
2721{
2722 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002723 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002724
Stephen Warren384a48d2011-06-01 11:14:21 -06002725 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002726 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002727 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002728
2729 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002730 if (!codec_has_acomp(codec))
2731 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2732 codec->jackpoll_interval > 0 ?
2733 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002734 }
2735 return 0;
2736}
2737
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002738static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2739{
2740 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2741 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002742}
2743
2744static void hdmi_array_free(struct hdmi_spec *spec)
2745{
2746 snd_array_free(&spec->pins);
2747 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002748}
2749
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002750static void generic_hdmi_free(struct hda_codec *codec)
2751{
2752 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002753 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002754
Takashi Iwai66032492015-12-01 16:49:35 +01002755 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002756 snd_hdac_i915_register_notifier(NULL);
2757
Stephen Warren384a48d2011-06-01 11:14:21 -06002758 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002759 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002760 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002761 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002762 }
2763
2764 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2765 if (spec->pcm_rec[pcm_idx].jack == NULL)
2766 continue;
2767 if (spec->dyn_pcm_assign)
2768 snd_device_free(codec->card,
2769 spec->pcm_rec[pcm_idx].jack);
2770 else
2771 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002772 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002773
Takashi Iwai55913112015-12-10 13:03:29 +01002774 if (spec->i915_bound)
2775 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002776 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002777 kfree(spec);
2778}
2779
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002780#ifdef CONFIG_PM
2781static int generic_hdmi_resume(struct hda_codec *codec)
2782{
2783 struct hdmi_spec *spec = codec->spec;
2784 int pin_idx;
2785
Pierre Ossmana2833682014-06-18 21:48:09 +02002786 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002787 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002788
2789 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2790 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2791 hdmi_present_sense(per_pin, 1);
2792 }
2793 return 0;
2794}
2795#endif
2796
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002797static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002798 .init = generic_hdmi_init,
2799 .free = generic_hdmi_free,
2800 .build_pcms = generic_hdmi_build_pcms,
2801 .build_controls = generic_hdmi_build_controls,
2802 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002803#ifdef CONFIG_PM
2804 .resume = generic_hdmi_resume,
2805#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002806};
2807
Anssi Hannula307229d2013-10-24 21:10:34 +03002808static const struct hdmi_ops generic_standard_hdmi_ops = {
2809 .pin_get_eld = snd_hdmi_get_eld,
Anssi Hannula307229d2013-10-24 21:10:34 +03002810 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2811 .pin_hbr_setup = hdmi_pin_hbr_setup,
2812 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302813};
2814
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002815static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2816 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002817{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002818 struct hdmi_spec *spec = codec->spec;
2819 hda_nid_t conns[4];
2820 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002821
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002822 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2823 if (nconns == spec->num_cvts &&
2824 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002825 return;
2826
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002827 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002828 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002829 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002830}
2831
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002832#define INTEL_VENDOR_NID 0x08
2833#define INTEL_GET_VENDOR_VERB 0xf81
2834#define INTEL_SET_VENDOR_VERB 0x781
2835#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2836#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2837
2838static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002839 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002840{
2841 unsigned int vendor_param;
2842
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002843 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2844 INTEL_GET_VENDOR_VERB, 0);
2845 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2846 return;
2847
2848 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2849 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2850 INTEL_SET_VENDOR_VERB, vendor_param);
2851 if (vendor_param == -1)
2852 return;
2853
Takashi Iwai17df3f52013-05-08 08:09:34 +02002854 if (update_tree)
2855 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002856}
2857
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002858static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2859{
2860 unsigned int vendor_param;
2861
2862 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2863 INTEL_GET_VENDOR_VERB, 0);
2864 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2865 return;
2866
2867 /* enable DP1.2 mode */
2868 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002869 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002870 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2871 INTEL_SET_VENDOR_VERB, vendor_param);
2872}
2873
Takashi Iwai17df3f52013-05-08 08:09:34 +02002874/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2875 * Otherwise you may get severe h/w communication errors.
2876 */
2877static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2878 unsigned int power_state)
2879{
2880 if (power_state == AC_PWRST_D0) {
2881 intel_haswell_enable_all_pins(codec, false);
2882 intel_haswell_fixup_enable_dp12(codec);
2883 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002884
Takashi Iwai17df3f52013-05-08 08:09:34 +02002885 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2886 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2887}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002888
David Henningssonf0675d42015-09-03 11:51:34 +02002889static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002890{
2891 struct hda_codec *codec = audio_ptr;
2892 int pin_nid = port + 0x04;
2893
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002894 /* skip notification during system suspend (but not in runtime PM);
2895 * the state will be updated at resume
2896 */
2897 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2898 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002899 /* ditto during suspend/resume process itself */
2900 if (atomic_read(&(codec)->core.in_pm))
2901 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002902
David Henningsson25adc132015-08-19 10:48:58 +02002903 check_presence_and_report(codec, pin_nid);
2904}
2905
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002906static int patch_generic_hdmi(struct hda_codec *codec)
2907{
2908 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002909
2910 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2911 if (spec == NULL)
2912 return -ENOMEM;
2913
Anssi Hannula307229d2013-10-24 21:10:34 +03002914 spec->ops = generic_standard_hdmi_ops;
Libin Yang42b29872015-12-16 13:42:42 +08002915 mutex_init(&spec->pcm_lock);
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05302916 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2917
2918 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
2919 hdmi_chmap_cea_alloc_validate_get_type;
2920 spec->chmap.ops.cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap;
2921 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2922 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2923 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2924
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002925 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002926 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002927
Takashi Iwai55913112015-12-10 13:03:29 +01002928 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2929 if (!codec_has_acomp(codec) &&
2930 (codec->core.vendor_id >> 16) == 0x8086)
2931 if (!snd_hdac_i915_init(&codec->bus->core))
2932 spec->i915_bound = true;
2933
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002934 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002935 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002936 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002937 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002938
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002939 /* For Valleyview/Cherryview, only the display codec is in the display
2940 * power well and can use link_power ops to request/release the power.
2941 * For Haswell/Broadwell, the controller is also in the power well and
2942 * can cover the codec power request, and so need not set this flag.
2943 * For previous platforms, there is no such power well feature.
2944 */
Lu, Hanff9d8852015-11-19 23:25:13 +08002945 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2946 is_broxton(codec))
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002947 codec->core.link_power_control = 1;
2948
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002949 if (hdmi_parse_codec(codec) < 0) {
Takashi Iwai55913112015-12-10 13:03:29 +01002950 if (spec->i915_bound)
2951 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002952 codec->spec = NULL;
2953 kfree(spec);
2954 return -EINVAL;
2955 }
2956 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002957 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002958 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002959 codec->dp_mst = true;
2960 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002961
Lu, Han2377c3c2015-06-09 16:50:38 +08002962 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2963 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2964 codec->auto_runtime_pm = 1;
2965
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002966 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002967
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002968 init_channel_allocations();
2969
Libin Yang790b4152016-03-04 14:33:43 +08002970 if (codec_has_acomp(codec)) {
2971 codec->depop_delay = 0;
2972 spec->i915_audio_ops.audio_ptr = codec;
2973 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2974 * will call pin_eld_notify with using audio_ptr pointer
2975 * We need make sure audio_ptr is really setup
2976 */
2977 wmb();
2978 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2979 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2980 }
2981
Libin Yang25e4abb2016-01-12 11:13:27 +08002982 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002983 return 0;
2984}
2985
2986/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002987 * Shared non-generic implementations
2988 */
2989
2990static int simple_playback_build_pcms(struct hda_codec *codec)
2991{
2992 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002993 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002994 unsigned int chans;
2995 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002996 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002997
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002998 per_cvt = get_cvt(spec, 0);
2999 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003000 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06003001
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003002 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003003 if (!info)
3004 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08003005 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003006 info->pcm_type = HDA_PCM_TYPE_HDMI;
3007 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
3008 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003009 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003010 if (pstr->channels_max <= 2 && chans && chans <= 16)
3011 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003012
3013 return 0;
3014}
3015
Takashi Iwai4b6ace92012-06-15 11:53:32 +02003016/* unsolicited event for jack sensing */
3017static void simple_hdmi_unsol_event(struct hda_codec *codec,
3018 unsigned int res)
3019{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02003020 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02003021 snd_hda_jack_report_sync(codec);
3022}
3023
3024/* generic_hdmi_build_jack can be used for simple_hdmi, too,
3025 * as long as spec->pins[] is set correctly
3026 */
3027#define simple_hdmi_build_jack generic_hdmi_build_jack
3028
Stephen Warren3aaf8982011-06-01 11:14:19 -06003029static int simple_playback_build_controls(struct hda_codec *codec)
3030{
3031 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003032 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003033 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003034
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003035 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02003036 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
3037 per_cvt->cvt_nid,
3038 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003039 if (err < 0)
3040 return err;
3041 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06003042}
3043
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003044static int simple_playback_init(struct hda_codec *codec)
3045{
3046 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003047 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
3048 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003049
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003050 snd_hda_codec_write(codec, pin, 0,
3051 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3052 /* some codecs require to unmute the pin */
3053 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3054 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3055 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02003056 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003057 return 0;
3058}
3059
Stephen Warren3aaf8982011-06-01 11:14:19 -06003060static void simple_playback_free(struct hda_codec *codec)
3061{
3062 struct hdmi_spec *spec = codec->spec;
3063
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003064 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06003065 kfree(spec);
3066}
3067
3068/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003069 * Nvidia specific implementations
3070 */
3071
3072#define Nv_VERB_SET_Channel_Allocation 0xF79
3073#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
3074#define Nv_VERB_SET_Audio_Protection_On 0xF98
3075#define Nv_VERB_SET_Audio_Protection_Off 0xF99
3076
3077#define nvhdmi_master_con_nid_7x 0x04
3078#define nvhdmi_master_pin_nid_7x 0x05
3079
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003080static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003081 /*front, rear, clfe, rear_surr */
3082 0x6, 0x8, 0xa, 0xc,
3083};
3084
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003085static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3086 /* set audio protect on */
3087 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3088 /* enable digital output on pin widget */
3089 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3090 {} /* terminator */
3091};
3092
3093static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003094 /* set audio protect on */
3095 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3096 /* enable digital output on pin widget */
3097 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3098 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3099 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3100 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3101 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3102 {} /* terminator */
3103};
3104
3105#ifdef LIMITED_RATE_FMT_SUPPORT
3106/* support only the safe format and rate */
3107#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3108#define SUPPORTED_MAXBPS 16
3109#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3110#else
3111/* support all rates and formats */
3112#define SUPPORTED_RATES \
3113 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3114 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3115 SNDRV_PCM_RATE_192000)
3116#define SUPPORTED_MAXBPS 24
3117#define SUPPORTED_FORMATS \
3118 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3119#endif
3120
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003121static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003122{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003123 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3124 return 0;
3125}
3126
3127static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3128{
3129 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003130 return 0;
3131}
3132
Nitin Daga393004b2011-01-10 21:49:31 +05303133static unsigned int channels_2_6_8[] = {
3134 2, 6, 8
3135};
3136
3137static unsigned int channels_2_8[] = {
3138 2, 8
3139};
3140
3141static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3142 .count = ARRAY_SIZE(channels_2_6_8),
3143 .list = channels_2_6_8,
3144 .mask = 0,
3145};
3146
3147static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3148 .count = ARRAY_SIZE(channels_2_8),
3149 .list = channels_2_8,
3150 .mask = 0,
3151};
3152
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003153static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3154 struct hda_codec *codec,
3155 struct snd_pcm_substream *substream)
3156{
3157 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05303158 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3159
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003160 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05303161 case 0x10de0002:
3162 case 0x10de0003:
3163 case 0x10de0005:
3164 case 0x10de0006:
3165 hw_constraints_channels = &hw_constraints_2_8_channels;
3166 break;
3167 case 0x10de0007:
3168 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3169 break;
3170 default:
3171 break;
3172 }
3173
3174 if (hw_constraints_channels != NULL) {
3175 snd_pcm_hw_constraint_list(substream->runtime, 0,
3176 SNDRV_PCM_HW_PARAM_CHANNELS,
3177 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01003178 } else {
3179 snd_pcm_hw_constraint_step(substream->runtime, 0,
3180 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05303181 }
3182
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003183 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3184}
3185
3186static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3187 struct hda_codec *codec,
3188 struct snd_pcm_substream *substream)
3189{
3190 struct hdmi_spec *spec = codec->spec;
3191 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3192}
3193
3194static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3195 struct hda_codec *codec,
3196 unsigned int stream_tag,
3197 unsigned int format,
3198 struct snd_pcm_substream *substream)
3199{
3200 struct hdmi_spec *spec = codec->spec;
3201 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3202 stream_tag, format, substream);
3203}
3204
Takashi Iwaid0b12522012-06-15 14:34:42 +02003205static const struct hda_pcm_stream simple_pcm_playback = {
3206 .substreams = 1,
3207 .channels_min = 2,
3208 .channels_max = 2,
3209 .ops = {
3210 .open = simple_playback_pcm_open,
3211 .close = simple_playback_pcm_close,
3212 .prepare = simple_playback_pcm_prepare
3213 },
3214};
3215
3216static const struct hda_codec_ops simple_hdmi_patch_ops = {
3217 .build_controls = simple_playback_build_controls,
3218 .build_pcms = simple_playback_build_pcms,
3219 .init = simple_playback_init,
3220 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02003221 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02003222};
3223
3224static int patch_simple_hdmi(struct hda_codec *codec,
3225 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3226{
3227 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003228 struct hdmi_spec_per_cvt *per_cvt;
3229 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003230
3231 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3232 if (!spec)
3233 return -ENOMEM;
3234
3235 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003236 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02003237
3238 spec->multiout.num_dacs = 0; /* no analog */
3239 spec->multiout.max_channels = 2;
3240 spec->multiout.dig_out_nid = cvt_nid;
3241 spec->num_cvts = 1;
3242 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003243 per_pin = snd_array_new(&spec->pins);
3244 per_cvt = snd_array_new(&spec->cvts);
3245 if (!per_pin || !per_cvt) {
3246 simple_playback_free(codec);
3247 return -ENOMEM;
3248 }
3249 per_cvt->cvt_nid = cvt_nid;
3250 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003251 spec->pcm_playback = simple_pcm_playback;
3252
3253 codec->patch_ops = simple_hdmi_patch_ops;
3254
3255 return 0;
3256}
3257
Aaron Plattner1f348522011-04-06 17:19:04 -07003258static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3259 int channels)
3260{
3261 unsigned int chanmask;
3262 int chan = channels ? (channels - 1) : 1;
3263
3264 switch (channels) {
3265 default:
3266 case 0:
3267 case 2:
3268 chanmask = 0x00;
3269 break;
3270 case 4:
3271 chanmask = 0x08;
3272 break;
3273 case 6:
3274 chanmask = 0x0b;
3275 break;
3276 case 8:
3277 chanmask = 0x13;
3278 break;
3279 }
3280
3281 /* Set the audio infoframe channel allocation and checksum fields. The
3282 * channel count is computed implicitly by the hardware. */
3283 snd_hda_codec_write(codec, 0x1, 0,
3284 Nv_VERB_SET_Channel_Allocation, chanmask);
3285
3286 snd_hda_codec_write(codec, 0x1, 0,
3287 Nv_VERB_SET_Info_Frame_Checksum,
3288 (0x71 - chan - chanmask));
3289}
3290
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003291static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3292 struct hda_codec *codec,
3293 struct snd_pcm_substream *substream)
3294{
3295 struct hdmi_spec *spec = codec->spec;
3296 int i;
3297
3298 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3299 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3300 for (i = 0; i < 4; i++) {
3301 /* set the stream id */
3302 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3303 AC_VERB_SET_CHANNEL_STREAMID, 0);
3304 /* set the stream format */
3305 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3306 AC_VERB_SET_STREAM_FORMAT, 0);
3307 }
3308
Aaron Plattner1f348522011-04-06 17:19:04 -07003309 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3310 * streams are disabled. */
3311 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3312
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003313 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3314}
3315
3316static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3317 struct hda_codec *codec,
3318 unsigned int stream_tag,
3319 unsigned int format,
3320 struct snd_pcm_substream *substream)
3321{
3322 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003323 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003324 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003325 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003326 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003327 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003328
3329 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003330 per_cvt = get_cvt(spec, 0);
3331 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003332
3333 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003334
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003335 dataDCC2 = 0x2;
3336
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003337 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003338 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003339 snd_hda_codec_write(codec,
3340 nvhdmi_master_con_nid_7x,
3341 0,
3342 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003343 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003344
3345 /* set the stream id */
3346 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3347 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3348
3349 /* set the stream format */
3350 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3351 AC_VERB_SET_STREAM_FORMAT, format);
3352
3353 /* turn on again (if needed) */
3354 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003355 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003356 snd_hda_codec_write(codec,
3357 nvhdmi_master_con_nid_7x,
3358 0,
3359 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003360 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003361 snd_hda_codec_write(codec,
3362 nvhdmi_master_con_nid_7x,
3363 0,
3364 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3365 }
3366
3367 for (i = 0; i < 4; i++) {
3368 if (chs == 2)
3369 channel_id = 0;
3370 else
3371 channel_id = i * 2;
3372
3373 /* turn off SPDIF once;
3374 *otherwise the IEC958 bits won't be updated
3375 */
3376 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003377 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003378 snd_hda_codec_write(codec,
3379 nvhdmi_con_nids_7x[i],
3380 0,
3381 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003382 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003383 /* set the stream id */
3384 snd_hda_codec_write(codec,
3385 nvhdmi_con_nids_7x[i],
3386 0,
3387 AC_VERB_SET_CHANNEL_STREAMID,
3388 (stream_tag << 4) | channel_id);
3389 /* set the stream format */
3390 snd_hda_codec_write(codec,
3391 nvhdmi_con_nids_7x[i],
3392 0,
3393 AC_VERB_SET_STREAM_FORMAT,
3394 format);
3395 /* turn on again (if needed) */
3396 /* enable and set the channel status audio/data flag */
3397 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003398 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003399 snd_hda_codec_write(codec,
3400 nvhdmi_con_nids_7x[i],
3401 0,
3402 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003403 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003404 snd_hda_codec_write(codec,
3405 nvhdmi_con_nids_7x[i],
3406 0,
3407 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3408 }
3409 }
3410
Aaron Plattner1f348522011-04-06 17:19:04 -07003411 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003412
3413 mutex_unlock(&codec->spdif_mutex);
3414 return 0;
3415}
3416
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003417static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003418 .substreams = 1,
3419 .channels_min = 2,
3420 .channels_max = 8,
3421 .nid = nvhdmi_master_con_nid_7x,
3422 .rates = SUPPORTED_RATES,
3423 .maxbps = SUPPORTED_MAXBPS,
3424 .formats = SUPPORTED_FORMATS,
3425 .ops = {
3426 .open = simple_playback_pcm_open,
3427 .close = nvhdmi_8ch_7x_pcm_close,
3428 .prepare = nvhdmi_8ch_7x_pcm_prepare
3429 },
3430};
3431
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003432static int patch_nvhdmi_2ch(struct hda_codec *codec)
3433{
3434 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003435 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3436 nvhdmi_master_pin_nid_7x);
3437 if (err < 0)
3438 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003439
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003440 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003441 /* override the PCM rates, etc, as the codec doesn't give full list */
3442 spec = codec->spec;
3443 spec->pcm_playback.rates = SUPPORTED_RATES;
3444 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3445 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003446 return 0;
3447}
3448
Takashi Iwai53775b02012-08-01 12:17:41 +02003449static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3450{
3451 struct hdmi_spec *spec = codec->spec;
3452 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003453 if (!err) {
3454 struct hda_pcm *info = get_pcm_rec(spec, 0);
3455 info->own_chmap = true;
3456 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003457 return err;
3458}
3459
3460static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3461{
3462 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003463 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003464 struct snd_pcm_chmap *chmap;
3465 int err;
3466
3467 err = simple_playback_build_controls(codec);
3468 if (err < 0)
3469 return err;
3470
3471 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003472 info = get_pcm_rec(spec, 0);
3473 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003474 SNDRV_PCM_STREAM_PLAYBACK,
3475 snd_pcm_alt_chmaps, 8, 0, &chmap);
3476 if (err < 0)
3477 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003478 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003479 case 0x10de0002:
3480 case 0x10de0003:
3481 case 0x10de0005:
3482 case 0x10de0006:
3483 chmap->channel_mask = (1U << 2) | (1U << 8);
3484 break;
3485 case 0x10de0007:
3486 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3487 }
3488 return 0;
3489}
3490
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003491static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3492{
3493 struct hdmi_spec *spec;
3494 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003495 if (err < 0)
3496 return err;
3497 spec = codec->spec;
3498 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003499 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003500 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003501 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3502 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003503
3504 /* Initialize the audio infoframe channel mask and checksum to something
3505 * valid */
3506 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3507
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003508 return 0;
3509}
3510
3511/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003512 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3513 * - 0x10de0015
3514 * - 0x10de0040
3515 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303516static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303517 struct hdac_cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02003518{
3519 if (cap->ca_index == 0x00 && channels == 2)
3520 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3521
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303522 return chmap->ops.chmap_cea_alloc_validate_get_type(
3523 chmap, cap, channels);
Anssi Hannula611885b2013-11-03 17:15:00 +02003524}
3525
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303526static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3527 int ca, int chs, unsigned char *map)
Anssi Hannula611885b2013-11-03 17:15:00 +02003528{
3529 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3530 return -EINVAL;
3531
3532 return 0;
3533}
3534
3535static int patch_nvhdmi(struct hda_codec *codec)
3536{
3537 struct hdmi_spec *spec;
3538 int err;
3539
3540 err = patch_generic_hdmi(codec);
3541 if (err)
3542 return err;
3543
3544 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003545 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003546
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303547 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02003548 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303549 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02003550
3551 return 0;
3552}
3553
3554/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003555 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3556 * accessed using vendor-defined verbs. These registers can be used for
3557 * interoperability between the HDA and HDMI drivers.
3558 */
3559
3560/* Audio Function Group node */
3561#define NVIDIA_AFG_NID 0x01
3562
3563/*
3564 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3565 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3566 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3567 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3568 * additional bit (at position 30) to signal the validity of the format.
3569 *
3570 * | 31 | 30 | 29 16 | 15 0 |
3571 * +---------+-------+--------+--------+
3572 * | TRIGGER | VALID | UNUSED | FORMAT |
3573 * +-----------------------------------|
3574 *
3575 * Note that for the trigger bit to take effect it needs to change value
3576 * (i.e. it needs to be toggled).
3577 */
3578#define NVIDIA_GET_SCRATCH0 0xfa6
3579#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3580#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3581#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3582#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3583#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3584#define NVIDIA_SCRATCH_VALID (1 << 6)
3585
3586#define NVIDIA_GET_SCRATCH1 0xfab
3587#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3588#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3589#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3590#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3591
3592/*
3593 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3594 * the format is invalidated so that the HDMI codec can be disabled.
3595 */
3596static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3597{
3598 unsigned int value;
3599
3600 /* bits [31:30] contain the trigger and valid bits */
3601 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3602 NVIDIA_GET_SCRATCH0, 0);
3603 value = (value >> 24) & 0xff;
3604
3605 /* bits [15:0] are used to store the HDA format */
3606 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3607 NVIDIA_SET_SCRATCH0_BYTE0,
3608 (format >> 0) & 0xff);
3609 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3610 NVIDIA_SET_SCRATCH0_BYTE1,
3611 (format >> 8) & 0xff);
3612
3613 /* bits [16:24] are unused */
3614 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3615 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3616
3617 /*
3618 * Bit 30 signals that the data is valid and hence that HDMI audio can
3619 * be enabled.
3620 */
3621 if (format == 0)
3622 value &= ~NVIDIA_SCRATCH_VALID;
3623 else
3624 value |= NVIDIA_SCRATCH_VALID;
3625
3626 /*
3627 * Whenever the trigger bit is toggled, an interrupt is raised in the
3628 * HDMI codec. The HDMI driver will use that as trigger to update its
3629 * configuration.
3630 */
3631 value ^= NVIDIA_SCRATCH_TRIGGER;
3632
3633 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3634 NVIDIA_SET_SCRATCH0_BYTE3, value);
3635}
3636
3637static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3638 struct hda_codec *codec,
3639 unsigned int stream_tag,
3640 unsigned int format,
3641 struct snd_pcm_substream *substream)
3642{
3643 int err;
3644
3645 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3646 format, substream);
3647 if (err < 0)
3648 return err;
3649
3650 /* notify the HDMI codec of the format change */
3651 tegra_hdmi_set_format(codec, format);
3652
3653 return 0;
3654}
3655
3656static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3657 struct hda_codec *codec,
3658 struct snd_pcm_substream *substream)
3659{
3660 /* invalidate the format in the HDMI codec */
3661 tegra_hdmi_set_format(codec, 0);
3662
3663 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3664}
3665
3666static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3667{
3668 struct hdmi_spec *spec = codec->spec;
3669 unsigned int i;
3670
3671 for (i = 0; i < spec->num_pins; i++) {
3672 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3673
3674 if (pcm->pcm_type == type)
3675 return pcm;
3676 }
3677
3678 return NULL;
3679}
3680
3681static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3682{
3683 struct hda_pcm_stream *stream;
3684 struct hda_pcm *pcm;
3685 int err;
3686
3687 err = generic_hdmi_build_pcms(codec);
3688 if (err < 0)
3689 return err;
3690
3691 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3692 if (!pcm)
3693 return -ENODEV;
3694
3695 /*
3696 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3697 * codec about format changes.
3698 */
3699 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3700 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3701 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3702
3703 return 0;
3704}
3705
3706static int patch_tegra_hdmi(struct hda_codec *codec)
3707{
3708 int err;
3709
3710 err = patch_generic_hdmi(codec);
3711 if (err)
3712 return err;
3713
3714 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3715
3716 return 0;
3717}
3718
3719/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003720 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003721 */
3722
Anssi Hannula5a6135842013-10-24 21:10:35 +03003723#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003724 ((codec)->core.vendor_id == 0x1002aa01 && \
3725 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003726#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003727
Anssi Hannula5a6135842013-10-24 21:10:35 +03003728/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3729#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3730#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3731#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3732#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3733#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3734#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003735#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003736#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3737#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3738#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3739#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3740#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3741#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3742#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3743#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3744#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3745#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3746#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003747#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003748#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3749#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3750#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3751#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3752#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3753
Anssi Hannula84d69e72013-10-24 21:10:38 +03003754/* AMD specific HDA cvt verbs */
3755#define ATI_VERB_SET_RAMP_RATE 0x770
3756#define ATI_VERB_GET_RAMP_RATE 0xf70
3757
Anssi Hannula5a6135842013-10-24 21:10:35 +03003758#define ATI_OUT_ENABLE 0x1
3759
3760#define ATI_MULTICHANNEL_MODE_PAIRED 0
3761#define ATI_MULTICHANNEL_MODE_SINGLE 1
3762
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003763#define ATI_HBR_CAPABLE 0x01
3764#define ATI_HBR_ENABLE 0x10
3765
Anssi Hannula89250f82013-10-24 21:10:36 +03003766static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3767 unsigned char *buf, int *eld_size)
3768{
3769 /* call hda_eld.c ATI/AMD-specific function */
3770 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3771 is_amdhdmi_rev3_or_later(codec));
3772}
3773
Anssi Hannula5a6135842013-10-24 21:10:35 +03003774static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3775 int active_channels, int conn_type)
3776{
3777 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3778}
3779
3780static int atihdmi_paired_swap_fc_lfe(int pos)
3781{
3782 /*
3783 * ATI/AMD have automatic FC/LFE swap built-in
3784 * when in pairwise mapping mode.
3785 */
3786
3787 switch (pos) {
3788 /* see channel_allocations[].speakers[] */
3789 case 2: return 3;
3790 case 3: return 2;
3791 default: break;
3792 }
3793
3794 return pos;
3795}
3796
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303797static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3798 int ca, int chs, unsigned char *map)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003799{
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303800 struct hdac_cea_channel_speaker_allocation *cap;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003801 int i, j;
3802
3803 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3804
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303805 cap = hdmi_get_ch_alloc_from_ca(ca);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003806 for (i = 0; i < chs; ++i) {
3807 int mask = to_spk_mask(map[i]);
3808 bool ok = false;
3809 bool companion_ok = false;
3810
3811 if (!mask)
3812 continue;
3813
3814 for (j = 0 + i % 2; j < 8; j += 2) {
3815 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3816 if (cap->speakers[chan_idx] == mask) {
3817 /* channel is in a supported position */
3818 ok = true;
3819
3820 if (i % 2 == 0 && i + 1 < chs) {
3821 /* even channel, check the odd companion */
3822 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3823 int comp_mask_req = to_spk_mask(map[i+1]);
3824 int comp_mask_act = cap->speakers[comp_chan_idx];
3825
3826 if (comp_mask_req == comp_mask_act)
3827 companion_ok = true;
3828 else
3829 return -EINVAL;
3830 }
3831 break;
3832 }
3833 }
3834
3835 if (!ok)
3836 return -EINVAL;
3837
3838 if (companion_ok)
3839 i++; /* companion channel already checked */
3840 }
3841
3842 return 0;
3843}
3844
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303845static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3846 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003847{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303848 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003849 int verb;
3850 int ati_channel_setup = 0;
3851
3852 if (hdmi_slot > 7)
3853 return -EINVAL;
3854
3855 if (!has_amd_full_remap_support(codec)) {
3856 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3857
3858 /* In case this is an odd slot but without stream channel, do not
3859 * disable the slot since the corresponding even slot could have a
3860 * channel. In case neither have a channel, the slot pair will be
3861 * disabled when this function is called for the even slot. */
3862 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3863 return 0;
3864
3865 hdmi_slot -= hdmi_slot % 2;
3866
3867 if (stream_channel != 0xf)
3868 stream_channel -= stream_channel % 2;
3869 }
3870
3871 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3872
3873 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3874
3875 if (stream_channel != 0xf)
3876 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3877
3878 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3879}
3880
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303881static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3882 hda_nid_t pin_nid, int asp_slot)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003883{
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05303884 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
Anssi Hannula5a6135842013-10-24 21:10:35 +03003885 bool was_odd = false;
3886 int ati_asp_slot = asp_slot;
3887 int verb;
3888 int ati_channel_setup;
3889
3890 if (asp_slot > 7)
3891 return -EINVAL;
3892
3893 if (!has_amd_full_remap_support(codec)) {
3894 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3895 if (ati_asp_slot % 2 != 0) {
3896 ati_asp_slot -= 1;
3897 was_odd = true;
3898 }
3899 }
3900
3901 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3902
3903 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3904
3905 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3906 return 0xf;
3907
3908 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3909}
3910
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303911static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3912 struct hdac_chmap *chmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303913 struct hdac_cea_channel_speaker_allocation *cap,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303914 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003915{
3916 int c;
3917
3918 /*
3919 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3920 * we need to take that into account (a single channel may take 2
3921 * channel slots if we need to carry a silent channel next to it).
3922 * On Rev3+ AMD codecs this function is not used.
3923 */
3924 int chanpairs = 0;
3925
3926 /* We only produce even-numbered channel count TLVs */
3927 if ((channels % 2) != 0)
3928 return -1;
3929
3930 for (c = 0; c < 7; c += 2) {
3931 if (cap->speakers[c] || cap->speakers[c+1])
3932 chanpairs++;
3933 }
3934
3935 if (chanpairs * 2 != channels)
3936 return -1;
3937
3938 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3939}
3940
Subhransu S. Prusty828cb4e2016-03-04 19:59:50 +05303941static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
Subhransu S. Prustyf3022402016-03-04 19:59:48 +05303942 struct hdac_cea_channel_speaker_allocation *cap,
3943 unsigned int *chmap, int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003944{
3945 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3946 int count = 0;
3947 int c;
3948
3949 for (c = 7; c >= 0; c--) {
3950 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3951 int spk = cap->speakers[chan];
3952 if (!spk) {
3953 /* add N/A channel if the companion channel is occupied */
3954 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3955 chmap[count++] = SNDRV_CHMAP_NA;
3956
3957 continue;
3958 }
3959
3960 chmap[count++] = spk_to_chmap(spk);
3961 }
3962
3963 WARN_ON(count != channels);
3964}
3965
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003966static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3967 bool hbr)
3968{
3969 int hbr_ctl, hbr_ctl_new;
3970
3971 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003972 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003973 if (hbr)
3974 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3975 else
3976 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3977
Takashi Iwai4e76a882014-02-25 12:21:03 +01003978 codec_dbg(codec,
3979 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003980 pin_nid,
3981 hbr_ctl == hbr_ctl_new ? "" : "new-",
3982 hbr_ctl_new);
3983
3984 if (hbr_ctl != hbr_ctl_new)
3985 snd_hda_codec_write(codec, pin_nid, 0,
3986 ATI_VERB_SET_HBR_CONTROL,
3987 hbr_ctl_new);
3988
3989 } else if (hbr)
3990 return -EINVAL;
3991
3992 return 0;
3993}
3994
Anssi Hannula84d69e72013-10-24 21:10:38 +03003995static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3996 hda_nid_t pin_nid, u32 stream_tag, int format)
3997{
3998
3999 if (is_amdhdmi_rev3_or_later(codec)) {
4000 int ramp_rate = 180; /* default as per AMD spec */
4001 /* disable ramp-up/down for non-pcm as per AMD spec */
4002 if (format & AC_FMT_TYPE_NON_PCM)
4003 ramp_rate = 0;
4004
4005 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
4006 }
4007
4008 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
4009}
4010
4011
Anssi Hannula5a6135842013-10-24 21:10:35 +03004012static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004013{
4014 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004015 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004016
Anssi Hannula5a6135842013-10-24 21:10:35 +03004017 err = generic_hdmi_init(codec);
4018
4019 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004020 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004021
4022 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
4023 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
4024
4025 /* make sure downmix information in infoframe is zero */
4026 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
4027
4028 /* enable channel-wise remap mode if supported */
4029 if (has_amd_full_remap_support(codec))
4030 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
4031 ATI_VERB_SET_MULTICHANNEL_MODE,
4032 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004033 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03004034
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004035 return 0;
4036}
4037
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004038static int patch_atihdmi(struct hda_codec *codec)
4039{
4040 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004041 struct hdmi_spec_per_cvt *per_cvt;
4042 int err, cvt_idx;
4043
4044 err = patch_generic_hdmi(codec);
4045
4046 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02004047 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004048
4049 codec->patch_ops.init = atihdmi_init;
4050
Takashi Iwaid0b12522012-06-15 14:34:42 +02004051 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004052
Anssi Hannula89250f82013-10-24 21:10:36 +03004053 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004054 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03004055 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03004056 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004057
4058 if (!has_amd_full_remap_support(codec)) {
4059 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304060 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03004061 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304062 spec->chmap.ops.cea_alloc_to_tlv_chmap =
4063 atihdmi_paired_cea_alloc_to_tlv_chmap;
4064 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Subhransu S. Prusty739ffee2016-03-04 19:59:49 +05304065 spec->chmap.ops.pin_get_slot_channel =
4066 atihdmi_pin_get_slot_channel;
4067 spec->chmap.ops.pin_set_slot_channel =
4068 atihdmi_pin_set_slot_channel;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004069 }
4070
4071 /* ATI/AMD converters do not advertise all of their capabilities */
4072 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4073 per_cvt = get_cvt(spec, cvt_idx);
4074 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4075 per_cvt->rates |= SUPPORTED_RATES;
4076 per_cvt->formats |= SUPPORTED_FORMATS;
4077 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4078 }
4079
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304080 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03004081
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004082 return 0;
4083}
4084
Annie Liu3de5ff82012-06-08 19:18:42 +08004085/* VIA HDMI Implementation */
4086#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4087#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4088
Annie Liu3de5ff82012-06-08 19:18:42 +08004089static int patch_via_hdmi(struct hda_codec *codec)
4090{
Takashi Iwai250e41a2012-06-15 14:40:21 +02004091 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08004092}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004093
4094/*
4095 * patch entries
4096 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004097static const struct hda_device_id snd_hda_id_hdmi[] = {
4098HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4099HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4100HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4101HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4102HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4103HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4104HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4105HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4106HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4107HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4108HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4109HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4110HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4111HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4112HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4113HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4114HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4115HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4116HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4117HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4118HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4119HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4120HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01004121/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004122HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4123HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4124HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4125HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4126HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4127HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4128HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4129HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4130HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4131HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4132HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4133HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4134HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4135HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4136HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4137HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4138HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4139HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4140HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4141HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4142HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08004143HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004144HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4145HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4146HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4147HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4148HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4149HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
4150HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4151HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4152HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4153HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
4154HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4155HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4156HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4157HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4158HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4159HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
Libin Yang91815d82016-01-14 14:09:00 +08004160HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004161HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4162HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4163HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4164HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004165/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004166HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004167{} /* terminator */
4168};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004169MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004170
4171MODULE_LICENSE("GPL");
4172MODULE_DESCRIPTION("HDMI HD-audio codec");
4173MODULE_ALIAS("snd-hda-codec-intelhdmi");
4174MODULE_ALIAS("snd-hda-codec-nvhdmi");
4175MODULE_ALIAS("snd-hda-codec-atihdmi");
4176
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004177static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004178 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004179};
4180
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004181module_hda_codec_driver(hdmi_driver);