blob: 345b83da46c5d539af7432224ef399810dda55cf [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
108 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
109 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
110 int asp_slot);
111 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
112 int asp_slot, int channel);
113
114 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
115 int ca, int active_channels, int conn_type);
116
117 /* enable/disable HBR (HD passthrough) */
118 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
119
120 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
121 hda_nid_t pin_nid, u32 stream_tag, int format);
122
Anssi Hannula307229d2013-10-24 21:10:34 +0300123};
124
Libin Yang2bea2412016-01-12 11:13:26 +0800125struct hdmi_pcm {
126 struct hda_pcm *pcm;
127 struct snd_jack *jack;
Libin Yangfb087ea2016-02-23 16:33:37 +0800128 struct snd_kcontrol *eld_ctl;
Libin Yang2bea2412016-01-12 11:13:26 +0800129};
130
Wu Fengguang079d88c2010-03-08 10:44:23 +0800131struct hdmi_spec {
132 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100133 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
134 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600135
Wu Fengguang079d88c2010-03-08 10:44:23 +0800136 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100137 struct snd_array pins; /* struct hdmi_spec_per_pin */
Libin Yang2bea2412016-01-12 11:13:26 +0800138 struct hdmi_pcm pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800139 struct mutex pcm_lock;
Libin Yanga76056f2015-12-16 16:48:15 +0800140 /* pcm_bitmap means which pcms have been assigned to pins*/
141 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800142 int pcm_used; /* counter of pcm_rec[] */
Libin Yangac983792015-12-16 16:48:16 +0800143 /* bitmap shows whether the pcm is opened in user space
144 * bit 0 means the first playback PCM (PCM3);
145 * bit 1 means the second playback PCM, and so on.
146 */
147 unsigned long pcm_in_use;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800148
David Henningsson4bd038f2013-02-19 16:11:25 +0100149 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300150 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700151
152 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800153 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800154 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300155 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800156 */
157 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200158 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200159
160 /* i915/powerwell (Haswell+/Valleyview+) specific */
161 struct i915_audio_component_audio_ops i915_audio_ops;
Takashi Iwai55913112015-12-10 13:03:29 +0100162 bool i915_bound; /* was i915 bound in this driver? */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +0530163
164 struct hdac_chmap chmap;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800165};
166
Takashi Iwaif4e30402015-12-10 13:01:28 +0100167#ifdef CONFIG_SND_HDA_I915
Takashi Iwai66032492015-12-01 16:49:35 +0100168#define codec_has_acomp(codec) \
169 ((codec)->bus->core.audio_component != NULL)
Takashi Iwaif4e30402015-12-10 13:01:28 +0100170#else
171#define codec_has_acomp(codec) false
172#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800173
174struct hdmi_audio_infoframe {
175 u8 type; /* 0x84 */
176 u8 ver; /* 0x01 */
177 u8 len; /* 0x0a */
178
Wu Fengguang53d7d692010-09-21 14:25:49 +0800179 u8 checksum;
180
Wu Fengguang079d88c2010-03-08 10:44:23 +0800181 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
182 u8 SS01_SF24;
183 u8 CXT04;
184 u8 CA;
185 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800186};
187
188struct dp_audio_infoframe {
189 u8 type; /* 0x84 */
190 u8 len; /* 0x1b */
191 u8 ver; /* 0x11 << 2 */
192
193 u8 CC02_CT47; /* match with HDMI infoframe from this on */
194 u8 SS01_SF24;
195 u8 CXT04;
196 u8 CA;
197 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800198};
199
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100200union audio_infoframe {
201 struct hdmi_audio_infoframe hdmi;
202 struct dp_audio_infoframe dp;
203 u8 bytes[0];
204};
205
Wu Fengguang079d88c2010-03-08 10:44:23 +0800206/*
207 * CEA speaker placement:
208 *
209 * FLH FCH FRH
210 * FLW FL FLC FC FRC FR FRW
211 *
212 * LFE
213 * TC
214 *
215 * RL RLC RC RRC RR
216 *
217 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
218 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
219 */
220enum cea_speaker_placement {
221 FL = (1 << 0), /* Front Left */
222 FC = (1 << 1), /* Front Center */
223 FR = (1 << 2), /* Front Right */
224 FLC = (1 << 3), /* Front Left Center */
225 FRC = (1 << 4), /* Front Right Center */
226 RL = (1 << 5), /* Rear Left */
227 RC = (1 << 6), /* Rear Center */
228 RR = (1 << 7), /* Rear Right */
229 RLC = (1 << 8), /* Rear Left Center */
230 RRC = (1 << 9), /* Rear Right Center */
231 LFE = (1 << 10), /* Low Frequency Effect */
232 FLW = (1 << 11), /* Front Left Wide */
233 FRW = (1 << 12), /* Front Right Wide */
234 FLH = (1 << 13), /* Front Left High */
235 FCH = (1 << 14), /* Front Center High */
236 FRH = (1 << 15), /* Front Right High */
237 TC = (1 << 16), /* Top Center */
238};
239
240/*
241 * ELD SA bits in the CEA Speaker Allocation data block
242 */
243static int eld_speaker_allocation_bits[] = {
244 [0] = FL | FR,
245 [1] = LFE,
246 [2] = FC,
247 [3] = RL | RR,
248 [4] = RC,
249 [5] = FLC | FRC,
250 [6] = RLC | RRC,
251 /* the following are not defined in ELD yet */
252 [7] = FLW | FRW,
253 [8] = FLH | FRH,
254 [9] = TC,
255 [10] = FCH,
256};
257
Wu Fengguang079d88c2010-03-08 10:44:23 +0800258/*
259 * ALSA sequence is:
260 *
261 * surround40 surround41 surround50 surround51 surround71
262 * ch0 front left = = = =
263 * ch1 front right = = = =
264 * ch2 rear left = = = =
265 * ch3 rear right = = = =
266 * ch4 LFE center center center
267 * ch5 LFE LFE
268 * ch6 side left
269 * ch7 side right
270 *
271 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
272 */
273static int hdmi_channel_mapping[0x32][8] = {
274 /* stereo */
275 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
276 /* 2.1 */
277 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
278 /* Dolby Surround */
279 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
280 /* surround40 */
281 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
282 /* 4ch */
283 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
284 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800285 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800286 /* surround50 */
287 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
288 /* surround51 */
289 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
290 /* 7.1 */
291 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
292};
293
294/*
295 * This is an ordered list!
296 *
297 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800298 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800299 */
300static struct cea_channel_speaker_allocation channel_allocations[] = {
301/* channel: 7 6 5 4 3 2 1 0 */
302{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
303 /* 2.1 */
304{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
305 /* Dolby Surround */
306{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
307 /* surround40 */
308{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
309 /* surround41 */
310{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
311 /* surround50 */
312{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
313 /* surround51 */
314{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
315 /* 6.1 */
316{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
317 /* surround71 */
318{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
319
320{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
321{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
322{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
323{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
324{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
325{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
326{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
327{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
328{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
329{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
330{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
331{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
332{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
333{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
334{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
335{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
336{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
337{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
338{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
339{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
340{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
341{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
342{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
343{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
344{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
345{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
346{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
347{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
348{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
349{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
350{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
351{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
352{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
353{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
354{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
355{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
356{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
357{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
358{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
359{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
360{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
361};
362
363
364/*
365 * HDMI routines
366 */
367
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100368#define get_pin(spec, idx) \
369 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
370#define get_cvt(spec, idx) \
371 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Libin Yang2bea2412016-01-12 11:13:26 +0800372/* obtain hdmi_pcm object assigned to idx */
373#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
374/* obtain hda_pcm object assigned to idx */
375#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100376
Takashi Iwai4e76a882014-02-25 12:21:03 +0100377static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800378{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100379 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600380 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800381
Stephen Warren384a48d2011-06-01 11:14:21 -0600382 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100383 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600384 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800385
Takashi Iwai4e76a882014-02-25 12:21:03 +0100386 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600387 return -EINVAL;
388}
389
Libin Yang2bf3c852015-12-16 13:42:43 +0800390static int hinfo_to_pcm_index(struct hda_codec *codec,
391 struct hda_pcm_stream *hinfo)
392{
393 struct hdmi_spec *spec = codec->spec;
394 int pcm_idx;
395
396 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
397 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
398 return pcm_idx;
399
400 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
401 return -EINVAL;
402}
403
Takashi Iwai4e76a882014-02-25 12:21:03 +0100404static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600405 struct hda_pcm_stream *hinfo)
406{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100407 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800408 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600409 int pin_idx;
410
Libin Yang6590faa2015-12-16 13:42:41 +0800411 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
412 per_pin = get_pin(spec, pin_idx);
Libin Yang2bea2412016-01-12 11:13:26 +0800413 if (per_pin->pcm &&
414 per_pin->pcm->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600415 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800416 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600417
Libin Yang6590faa2015-12-16 13:42:41 +0800418 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600419 return -EINVAL;
420}
421
Libin Yang022f3442016-02-03 10:48:34 +0800422static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
423 int pcm_idx)
424{
425 int i;
426 struct hdmi_spec_per_pin *per_pin;
427
428 for (i = 0; i < spec->num_pins; i++) {
429 per_pin = get_pin(spec, i);
430 if (per_pin->pcm_idx == pcm_idx)
431 return per_pin;
432 }
433 return NULL;
434}
435
Takashi Iwai4e76a882014-02-25 12:21:03 +0100436static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600437{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100438 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600439 int cvt_idx;
440
441 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100442 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600443 return cvt_idx;
444
Takashi Iwai4e76a882014-02-25 12:21:03 +0100445 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800446 return -EINVAL;
447}
448
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500449static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
450 struct snd_ctl_elem_info *uinfo)
451{
452 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100453 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200454 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100455 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800456 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500457
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500458 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
459
Libin Yangfb087ea2016-02-23 16:33:37 +0800460 pcm_idx = kcontrol->private_value;
461 mutex_lock(&spec->pcm_lock);
462 per_pin = pcm_idx_to_pin(spec, pcm_idx);
463 if (!per_pin) {
464 /* no pin is bound to the pcm */
465 uinfo->count = 0;
466 mutex_unlock(&spec->pcm_lock);
467 return 0;
468 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200469 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100470 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Libin Yangfb087ea2016-02-23 16:33:37 +0800471 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500472
473 return 0;
474}
475
476static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
477 struct snd_ctl_elem_value *ucontrol)
478{
479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100480 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200481 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100482 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800483 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500484
Libin Yangfb087ea2016-02-23 16:33:37 +0800485 pcm_idx = kcontrol->private_value;
486 mutex_lock(&spec->pcm_lock);
487 per_pin = pcm_idx_to_pin(spec, pcm_idx);
488 if (!per_pin) {
489 /* no pin is bound to the pcm */
490 memset(ucontrol->value.bytes.data, 0,
491 ARRAY_SIZE(ucontrol->value.bytes.data));
492 mutex_unlock(&spec->pcm_lock);
493 return 0;
494 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200495 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500496
David Henningsson360a8242016-02-05 09:05:41 +0100497 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
498 eld->eld_size > ELD_MAX_SIZE) {
Libin Yangfb087ea2016-02-23 16:33:37 +0800499 mutex_unlock(&spec->pcm_lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100500 snd_BUG();
501 return -EINVAL;
502 }
503
504 memset(ucontrol->value.bytes.data, 0,
505 ARRAY_SIZE(ucontrol->value.bytes.data));
506 if (eld->eld_valid)
507 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
508 eld->eld_size);
Libin Yangfb087ea2016-02-23 16:33:37 +0800509 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500510
511 return 0;
512}
513
514static struct snd_kcontrol_new eld_bytes_ctl = {
515 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
516 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
517 .name = "ELD",
518 .info = hdmi_eld_ctl_info,
519 .get = hdmi_eld_ctl_get,
520};
521
Libin Yangfb087ea2016-02-23 16:33:37 +0800522static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500523 int device)
524{
525 struct snd_kcontrol *kctl;
526 struct hdmi_spec *spec = codec->spec;
527 int err;
528
529 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
530 if (!kctl)
531 return -ENOMEM;
Libin Yangfb087ea2016-02-23 16:33:37 +0800532 kctl->private_value = pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500533 kctl->id.device = device;
534
Libin Yangfb087ea2016-02-23 16:33:37 +0800535 /* no pin nid is associated with the kctl now
536 * tbd: associate pin nid to eld ctl later
537 */
538 err = snd_hda_ctl_add(codec, 0, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500539 if (err < 0)
540 return err;
541
Libin Yangfb087ea2016-02-23 16:33:37 +0800542 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500543 return 0;
544}
545
Wu Fengguang079d88c2010-03-08 10:44:23 +0800546#ifdef BE_PARANOID
547static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
548 int *packet_index, int *byte_index)
549{
550 int val;
551
552 val = snd_hda_codec_read(codec, pin_nid, 0,
553 AC_VERB_GET_HDMI_DIP_INDEX, 0);
554
555 *packet_index = val >> 5;
556 *byte_index = val & 0x1f;
557}
558#endif
559
560static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
561 int packet_index, int byte_index)
562{
563 int val;
564
565 val = (packet_index << 5) | (byte_index & 0x1f);
566
567 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
568}
569
570static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
571 unsigned char val)
572{
573 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
574}
575
Stephen Warren384a48d2011-06-01 11:14:21 -0600576static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800577{
Stephen Warren75fae112014-01-30 11:52:16 -0700578 struct hdmi_spec *spec = codec->spec;
579 int pin_out;
580
Wu Fengguang079d88c2010-03-08 10:44:23 +0800581 /* Unmute */
582 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
583 snd_hda_codec_write(codec, pin_nid, 0,
584 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700585
586 if (spec->dyn_pin_out)
587 /* Disable pin out until stream is active */
588 pin_out = 0;
589 else
590 /* Enable pin out: some machines with GM965 gets broken output
591 * when the pin is disabled or changed while using with HDMI
592 */
593 pin_out = PIN_OUT;
594
Wu Fengguang079d88c2010-03-08 10:44:23 +0800595 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700596 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800597}
598
Stephen Warren384a48d2011-06-01 11:14:21 -0600599static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800600{
Stephen Warren384a48d2011-06-01 11:14:21 -0600601 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800602 AC_VERB_GET_CVT_CHAN_COUNT, 0);
603}
604
605static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600606 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800607{
Stephen Warren384a48d2011-06-01 11:14:21 -0600608 if (chs != hdmi_get_channel_count(codec, cvt_nid))
609 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800610 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
611}
612
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200613/*
614 * ELD proc files
615 */
616
Jie Yangcd6a6502015-05-27 19:45:45 +0800617#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200618static void print_eld_info(struct snd_info_entry *entry,
619 struct snd_info_buffer *buffer)
620{
621 struct hdmi_spec_per_pin *per_pin = entry->private_data;
622
623 mutex_lock(&per_pin->lock);
624 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
625 mutex_unlock(&per_pin->lock);
626}
627
628static void write_eld_info(struct snd_info_entry *entry,
629 struct snd_info_buffer *buffer)
630{
631 struct hdmi_spec_per_pin *per_pin = entry->private_data;
632
633 mutex_lock(&per_pin->lock);
634 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
635 mutex_unlock(&per_pin->lock);
636}
637
638static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
639{
640 char name[32];
641 struct hda_codec *codec = per_pin->codec;
642 struct snd_info_entry *entry;
643 int err;
644
645 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100646 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200647 if (err < 0)
648 return err;
649
650 snd_info_set_text_ops(entry, per_pin, print_eld_info);
651 entry->c.text.write = write_eld_info;
652 entry->mode |= S_IWUSR;
653 per_pin->proc_entry = entry;
654
655 return 0;
656}
657
658static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
659{
Markus Elfring1947a112015-06-28 11:15:28 +0200660 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200661 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200662 per_pin->proc_entry = NULL;
663 }
664}
665#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200666static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
667 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200668{
669 return 0;
670}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200671static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200672{
673}
674#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800675
676/*
677 * Channel mapping routines
678 */
679
680/*
681 * Compute derived values in channel_allocations[].
682 */
683static void init_channel_allocations(void)
684{
685 int i, j;
686 struct cea_channel_speaker_allocation *p;
687
688 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
689 p = channel_allocations + i;
690 p->channels = 0;
691 p->spk_mask = 0;
692 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
693 if (p->speakers[j]) {
694 p->channels++;
695 p->spk_mask |= p->speakers[j];
696 }
697 }
698}
699
Wang Xingchao72357c72012-09-06 10:02:36 +0800700static int get_channel_allocation_order(int ca)
701{
702 int i;
703
704 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
705 if (channel_allocations[i].ca_index == ca)
706 break;
707 }
708 return i;
709}
710
Wu Fengguang079d88c2010-03-08 10:44:23 +0800711/*
712 * The transformation takes two steps:
713 *
714 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
715 * spk_mask => (channel_allocations[]) => ai->CA
716 *
717 * TODO: it could select the wrong CA from multiple candidates.
718*/
Takashi Iwai79514d42014-06-06 18:04:34 +0200719static int hdmi_channel_allocation(struct hda_codec *codec,
720 struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800721{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800722 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800723 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800724 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800725 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
726
727 /*
728 * CA defaults to 0 for basic stereo audio
729 */
730 if (channels <= 2)
731 return 0;
732
Wu Fengguang079d88c2010-03-08 10:44:23 +0800733 /*
734 * expand ELD's speaker allocation mask
735 *
736 * ELD tells the speaker mask in a compact(paired) form,
737 * expand ELD's notions to match the ones used by Audio InfoFrame.
738 */
739 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100740 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800741 spk_mask |= eld_speaker_allocation_bits[i];
742 }
743
744 /* search for the first working match in the CA table */
745 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
746 if (channels == channel_allocations[i].channels &&
747 (spk_mask & channel_allocations[i].spk_mask) ==
748 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800749 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800750 break;
751 }
752 }
753
Anssi Hannula18e39182013-09-01 14:36:47 +0300754 if (!ca) {
755 /* if there was no match, select the regular ALSA channel
756 * allocation with the matching number of channels */
757 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
758 if (channels == channel_allocations[i].channels) {
759 ca = channel_allocations[i].ca_index;
760 break;
761 }
762 }
763 }
764
David Henningsson1613d6b2013-02-19 16:11:24 +0100765 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Takashi Iwai79514d42014-06-06 18:04:34 +0200766 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800767 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800768
Wu Fengguang53d7d692010-09-21 14:25:49 +0800769 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800770}
771
772static void hdmi_debug_channel_mapping(struct hda_codec *codec,
773 hda_nid_t pin_nid)
774{
775#ifdef CONFIG_SND_DEBUG_VERBOSE
Anssi Hannula307229d2013-10-24 21:10:34 +0300776 struct hdmi_spec *spec = codec->spec;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800777 int i;
Anssi Hannula307229d2013-10-24 21:10:34 +0300778 int channel;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800779
780 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300781 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100782 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300783 channel, i);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800784 }
785#endif
786}
787
Takashi Iwaid45e6882012-07-31 11:36:00 +0200788static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800789 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800790 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800791 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800792{
Anssi Hannula307229d2013-10-24 21:10:34 +0300793 struct hdmi_spec *spec = codec->spec;
Anssi Hannula90f28002013-10-05 02:25:39 +0300794 struct cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800795 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800796 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800797 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800798 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800799
Wang Xingchao72357c72012-09-06 10:02:36 +0800800 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300801 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800802
Wu Fengguang079d88c2010-03-08 10:44:23 +0800803 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300804 int hdmi_slot = 0;
805 /* fill actual channel mappings in ALSA channel (i) order */
806 for (i = 0; i < ch_alloc->channels; i++) {
807 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
808 hdmi_slot++; /* skip zero slots */
809
810 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
811 }
812 /* fill the rest of the slots with ALSA channel 0xf */
813 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
814 if (!ch_alloc->speakers[7 - hdmi_slot])
815 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800816 }
817
Wang Xingchao433968d2012-09-06 10:02:37 +0800818 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300819 for (i = 0; i < ch_alloc->channels; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300820 non_pcm_mapping[i] = (i << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800821 for (; i < 8; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300822 non_pcm_mapping[i] = (0xf << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800823 }
824
Wu Fengguang079d88c2010-03-08 10:44:23 +0800825 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300826 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
827 int hdmi_slot = slotsetup & 0x0f;
828 int channel = (slotsetup & 0xf0) >> 4;
829 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800830 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100831 codec_dbg(codec, "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800832 break;
833 }
834 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800835}
836
Takashi Iwaid45e6882012-07-31 11:36:00 +0200837struct channel_map_table {
838 unsigned char map; /* ALSA API channel map position */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200839 int spk_mask; /* speaker position bit mask */
840};
841
842static struct channel_map_table map_tables[] = {
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300843 { SNDRV_CHMAP_FL, FL },
844 { SNDRV_CHMAP_FR, FR },
845 { SNDRV_CHMAP_RL, RL },
846 { SNDRV_CHMAP_RR, RR },
847 { SNDRV_CHMAP_LFE, LFE },
848 { SNDRV_CHMAP_FC, FC },
849 { SNDRV_CHMAP_RLC, RLC },
850 { SNDRV_CHMAP_RRC, RRC },
851 { SNDRV_CHMAP_RC, RC },
852 { SNDRV_CHMAP_FLC, FLC },
853 { SNDRV_CHMAP_FRC, FRC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200854 { SNDRV_CHMAP_TFL, FLH },
855 { SNDRV_CHMAP_TFR, FRH },
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300856 { SNDRV_CHMAP_FLW, FLW },
857 { SNDRV_CHMAP_FRW, FRW },
858 { SNDRV_CHMAP_TC, TC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200859 { SNDRV_CHMAP_TFC, FCH },
Takashi Iwaid45e6882012-07-31 11:36:00 +0200860 {} /* terminator */
861};
862
863/* from ALSA API channel position to speaker bit mask */
864static int to_spk_mask(unsigned char c)
865{
866 struct channel_map_table *t = map_tables;
867 for (; t->map; t++) {
868 if (t->map == c)
869 return t->spk_mask;
870 }
871 return 0;
872}
873
874/* from ALSA API channel position to CEA slot */
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300875static int to_cea_slot(int ordered_ca, unsigned char pos)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200876{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300877 int mask = to_spk_mask(pos);
878 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200879
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300880 if (mask) {
881 for (i = 0; i < 8; i++) {
882 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
883 return i;
884 }
Takashi Iwaid45e6882012-07-31 11:36:00 +0200885 }
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300886
887 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200888}
889
890/* from speaker bit mask to ALSA API channel position */
891static int spk_to_chmap(int spk)
892{
893 struct channel_map_table *t = map_tables;
894 for (; t->map; t++) {
895 if (t->spk_mask == spk)
896 return t->map;
897 }
898 return 0;
899}
900
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300901/* from CEA slot to ALSA API channel position */
902static int from_cea_slot(int ordered_ca, unsigned char slot)
903{
904 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
905
906 return spk_to_chmap(mask);
907}
908
Takashi Iwaid45e6882012-07-31 11:36:00 +0200909/* get the CA index corresponding to the given ALSA API channel map */
910static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
911{
912 int i, spks = 0, spk_mask = 0;
913
914 for (i = 0; i < chs; i++) {
915 int mask = to_spk_mask(map[i]);
916 if (mask) {
917 spk_mask |= mask;
918 spks++;
919 }
920 }
921
922 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
923 if ((chs == channel_allocations[i].channels ||
924 spks == channel_allocations[i].channels) &&
925 (spk_mask & channel_allocations[i].spk_mask) ==
926 channel_allocations[i].spk_mask)
927 return channel_allocations[i].ca_index;
928 }
929 return -1;
930}
931
932/* set up the channel slots for the given ALSA API channel map */
933static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
934 hda_nid_t pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300935 int chs, unsigned char *map,
936 int ca)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200937{
Anssi Hannula307229d2013-10-24 21:10:34 +0300938 struct hdmi_spec *spec = codec->spec;
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300939 int ordered_ca = get_channel_allocation_order(ca);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300940 int alsa_pos, hdmi_slot;
941 int assignments[8] = {[0 ... 7] = 0xf};
942
943 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
944
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300945 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300946
947 if (hdmi_slot < 0)
948 continue; /* unassigned channel */
949
950 assignments[hdmi_slot] = alsa_pos;
951 }
952
953 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300954 int err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300955
Anssi Hannula307229d2013-10-24 21:10:34 +0300956 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
957 assignments[hdmi_slot]);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200958 if (err)
959 return -EINVAL;
960 }
961 return 0;
962}
963
964/* store ALSA API channel map from the current default map */
965static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
966{
967 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300968 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200969 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300970 if (i < channel_allocations[ordered_ca].channels)
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300971 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200972 else
973 map[i] = 0;
974 }
975}
976
977static void hdmi_setup_channel_mapping(struct hda_codec *codec,
978 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200979 int channels, unsigned char *map,
980 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200981{
Anssi Hannula20608732013-02-03 17:55:45 +0200982 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200983 hdmi_manual_setup_channel_mapping(codec, pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300984 channels, map, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200985 } else {
986 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
987 hdmi_setup_fake_chmap(map, ca);
988 }
Anssi Hannula980b2492013-10-05 02:25:44 +0300989
990 hdmi_debug_channel_mapping(codec, pin_nid);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200991}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800992
Anssi Hannula307229d2013-10-24 21:10:34 +0300993static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
994 int asp_slot, int channel)
995{
996 return snd_hda_codec_write(codec, pin_nid, 0,
997 AC_VERB_SET_HDMI_CHAN_SLOT,
998 (channel << 4) | asp_slot);
999}
1000
1001static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
1002 int asp_slot)
1003{
1004 return (snd_hda_codec_read(codec, pin_nid, 0,
1005 AC_VERB_GET_HDMI_CHAN_SLOT,
1006 asp_slot) & 0xf0) >> 4;
1007}
1008
Wu Fengguang079d88c2010-03-08 10:44:23 +08001009/*
1010 * Audio InfoFrame routines
1011 */
1012
1013/*
1014 * Enable Audio InfoFrame Transmission
1015 */
1016static void hdmi_start_infoframe_trans(struct hda_codec *codec,
1017 hda_nid_t pin_nid)
1018{
1019 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1020 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1021 AC_DIPXMIT_BEST);
1022}
1023
1024/*
1025 * Disable Audio InfoFrame Transmission
1026 */
1027static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1028 hda_nid_t pin_nid)
1029{
1030 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1031 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1032 AC_DIPXMIT_DISABLE);
1033}
1034
1035static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1036{
1037#ifdef CONFIG_SND_DEBUG_VERBOSE
1038 int i;
1039 int size;
1040
1041 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001042 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001043
1044 for (i = 0; i < 8; i++) {
1045 size = snd_hda_codec_read(codec, pin_nid, 0,
1046 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001047 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001048 }
1049#endif
1050}
1051
1052static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1053{
1054#ifdef BE_PARANOID
1055 int i, j;
1056 int size;
1057 int pi, bi;
1058 for (i = 0; i < 8; i++) {
1059 size = snd_hda_codec_read(codec, pin_nid, 0,
1060 AC_VERB_GET_HDMI_DIP_SIZE, i);
1061 if (size == 0)
1062 continue;
1063
1064 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1065 for (j = 1; j < 1000; j++) {
1066 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1067 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1068 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001069 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001070 bi, pi, i);
1071 if (bi == 0) /* byte index wrapped around */
1072 break;
1073 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001074 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001075 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1076 i, size, j);
1077 }
1078#endif
1079}
1080
Wu Fengguang53d7d692010-09-21 14:25:49 +08001081static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001082{
Wu Fengguang53d7d692010-09-21 14:25:49 +08001083 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001084 u8 sum = 0;
1085 int i;
1086
Wu Fengguang53d7d692010-09-21 14:25:49 +08001087 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001088
Wu Fengguang53d7d692010-09-21 14:25:49 +08001089 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001090 sum += bytes[i];
1091
Wu Fengguang53d7d692010-09-21 14:25:49 +08001092 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001093}
1094
1095static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1096 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001097 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001098{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001099 int i;
1100
1101 hdmi_debug_dip_size(codec, pin_nid);
1102 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1103
Wu Fengguang079d88c2010-03-08 10:44:23 +08001104 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001105 for (i = 0; i < size; i++)
1106 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001107}
1108
1109static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001110 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001111{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001112 u8 val;
1113 int i;
1114
1115 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1116 != AC_DIPXMIT_BEST)
1117 return false;
1118
1119 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001120 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001121 val = snd_hda_codec_read(codec, pin_nid, 0,
1122 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001123 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +08001124 return false;
1125 }
1126
1127 return true;
1128}
1129
Anssi Hannula307229d2013-10-24 21:10:34 +03001130static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1131 hda_nid_t pin_nid,
1132 int ca, int active_channels,
1133 int conn_type)
1134{
1135 union audio_infoframe ai;
1136
Mengdong Lincaaf5ef2014-03-11 17:12:52 -04001137 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +03001138 if (conn_type == 0) { /* HDMI */
1139 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1140
1141 hdmi_ai->type = 0x84;
1142 hdmi_ai->ver = 0x01;
1143 hdmi_ai->len = 0x0a;
1144 hdmi_ai->CC02_CT47 = active_channels - 1;
1145 hdmi_ai->CA = ca;
1146 hdmi_checksum_audio_infoframe(hdmi_ai);
1147 } else if (conn_type == 1) { /* DisplayPort */
1148 struct dp_audio_infoframe *dp_ai = &ai.dp;
1149
1150 dp_ai->type = 0x84;
1151 dp_ai->len = 0x1b;
1152 dp_ai->ver = 0x11 << 2;
1153 dp_ai->CC02_CT47 = active_channels - 1;
1154 dp_ai->CA = ca;
1155 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001156 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001157 pin_nid);
1158 return;
1159 }
1160
1161 /*
1162 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1163 * sizeof(*dp_ai) to avoid partial match/update problems when
1164 * the user switches between HDMI/DP monitors.
1165 */
1166 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1167 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001168 codec_dbg(codec,
1169 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001170 pin_nid,
1171 active_channels, ca);
1172 hdmi_stop_infoframe_trans(codec, pin_nid);
1173 hdmi_fill_audio_infoframe(codec, pin_nid,
1174 ai.bytes, sizeof(ai));
1175 hdmi_start_infoframe_trans(codec, pin_nid);
1176 }
1177}
1178
Takashi Iwaib0540872013-09-02 12:33:02 +02001179static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1180 struct hdmi_spec_per_pin *per_pin,
1181 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001182{
Anssi Hannula307229d2013-10-24 21:10:34 +03001183 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001184 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +02001185 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001186 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -06001187 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001188 int ca, ordered_ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001189
Takashi Iwaib0540872013-09-02 12:33:02 +02001190 if (!channels)
1191 return;
1192
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001193 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001194 snd_hda_codec_write(codec, pin_nid, 0,
1195 AC_VERB_SET_AMP_GAIN_MUTE,
1196 AMP_OUT_UNMUTE);
1197
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001198 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001199
Takashi Iwaid45e6882012-07-31 11:36:00 +02001200 if (!non_pcm && per_pin->chmap_set)
1201 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1202 else
Takashi Iwai79514d42014-06-06 18:04:34 +02001203 ca = hdmi_channel_allocation(codec, eld, channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001204 if (ca < 0)
1205 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001206
Anssi Hannula1df5a062013-10-05 02:25:40 +03001207 ordered_ca = get_channel_allocation_order(ca);
1208 active_channels = channel_allocations[ordered_ca].channels;
1209
1210 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1211
Stephen Warren384a48d2011-06-01 11:14:21 -06001212 /*
Anssi Hannula39edac72013-10-07 19:24:52 +03001213 * always configure channel mapping, it may have been changed by the
1214 * user in the meantime
1215 */
1216 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1217 channels, per_pin->chmap,
1218 per_pin->chmap_set);
1219
Anssi Hannula307229d2013-10-24 21:10:34 +03001220 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1221 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +08001222
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001223 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001224}
1225
Wu Fengguang079d88c2010-03-08 10:44:23 +08001226/*
1227 * Unsolicited events
1228 */
1229
Takashi Iwaiefe47102013-11-07 13:38:23 +01001230static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001231
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001232static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001233{
1234 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001235 int pin_idx = pin_nid_to_pin_index(codec, nid);
1236
David Henningsson20ce9022013-12-04 10:19:41 +08001237 if (pin_idx < 0)
1238 return;
David Henningsson20ce9022013-12-04 10:19:41 +08001239 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1240 snd_hda_jack_report_sync(codec);
1241}
1242
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001243static void jack_callback(struct hda_codec *codec,
1244 struct hda_jack_callback *jack)
1245{
Takashi Iwai2ebab402016-02-09 10:23:52 +01001246 check_presence_and_report(codec, jack->nid);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001247}
1248
David Henningsson20ce9022013-12-04 10:19:41 +08001249static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1250{
Takashi Iwai3a938972011-10-28 01:16:55 +02001251 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001252 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001253 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001254
1255 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1256 if (!jack)
1257 return;
Takashi Iwai3a938972011-10-28 01:16:55 +02001258 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001259
Takashi Iwai4e76a882014-02-25 12:21:03 +01001260 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001261 "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 +08001262 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001263 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001264
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001265 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001266}
1267
1268static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1269{
1270 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1271 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1272 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1273 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1274
Takashi Iwai4e76a882014-02-25 12:21:03 +01001275 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001276 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001277 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001278 tag,
1279 subtag,
1280 cp_state,
1281 cp_ready);
1282
1283 /* TODO */
1284 if (cp_state)
1285 ;
1286 if (cp_ready)
1287 ;
1288}
1289
1290
1291static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1292{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001293 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1294 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1295
Takashi Iwai3a938972011-10-28 01:16:55 +02001296 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001297 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001298 return;
1299 }
1300
1301 if (subtag == 0)
1302 hdmi_intrinsic_event(codec, res);
1303 else
1304 hdmi_non_intrinsic_event(codec, res);
1305}
1306
Mengdong Lin58f7d282013-09-04 16:37:12 -04001307static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001308 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001309{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001310 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001311
Wang Xingchao53b434f2013-06-18 10:41:53 +08001312 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1313 * thus pins could only choose converter 0 for use. Make sure the
1314 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001315 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001316 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1317
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001318 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001319 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1320 AC_PWRST_D0);
1321 msleep(40);
1322 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1323 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001324 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 +02001325 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001326}
1327
Wu Fengguang079d88c2010-03-08 10:44:23 +08001328/*
1329 * Callbacks
1330 */
1331
Takashi Iwai92f10b32010-08-03 14:21:00 +02001332/* HBR should be Non-PCM, 8 channels */
1333#define is_hbr_format(format) \
1334 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1335
Anssi Hannula307229d2013-10-24 21:10:34 +03001336static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1337 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001338{
Anssi Hannula307229d2013-10-24 21:10:34 +03001339 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +02001340
Stephen Warren384a48d2011-06-01 11:14:21 -06001341 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1342 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001343 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1344
Anssi Hannula13122e62013-11-10 20:56:10 +02001345 if (pinctl < 0)
1346 return hbr ? -EINVAL : 0;
1347
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001348 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +03001349 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001350 new_pinctl |= AC_PINCTL_EPT_HBR;
1351 else
1352 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1353
Takashi Iwai4e76a882014-02-25 12:21:03 +01001354 codec_dbg(codec,
1355 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001356 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001357 pinctl == new_pinctl ? "" : "new-",
1358 new_pinctl);
1359
1360 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001361 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001362 AC_VERB_SET_PIN_WIDGET_CONTROL,
1363 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +03001364 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001365 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03001366
1367 return 0;
1368}
1369
1370static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1371 hda_nid_t pin_nid, u32 stream_tag, int format)
1372{
1373 struct hdmi_spec *spec = codec->spec;
1374 int err;
1375
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001376 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +03001377 haswell_verify_D0(codec, cvt_nid, pin_nid);
1378
1379 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1380
1381 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001382 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +03001383 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001384 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001385
Stephen Warren384a48d2011-06-01 11:14:21 -06001386 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001387 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001388}
1389
Libin Yang42b29872015-12-16 13:42:42 +08001390/* Try to find an available converter
1391 * If pin_idx is less then zero, just try to find an available converter.
1392 * Otherwise, try to find an available converter and get the cvt mux index
1393 * of the pin.
1394 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001395static int hdmi_choose_cvt(struct hda_codec *codec,
1396 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001397{
1398 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001399 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001400 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001401 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001402
Libin Yang42b29872015-12-16 13:42:42 +08001403 /* pin_idx < 0 means no pin will be bound to the converter */
1404 if (pin_idx < 0)
1405 per_pin = NULL;
1406 else
1407 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001408
Stephen Warren384a48d2011-06-01 11:14:21 -06001409 /* Dynamically assign converter to stream */
1410 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001411 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001412
1413 /* Must not already be assigned */
1414 if (per_cvt->assigned)
1415 continue;
Libin Yang42b29872015-12-16 13:42:42 +08001416 if (per_pin == NULL)
1417 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06001418 /* Must be in pin's mux's list of converters */
1419 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1420 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1421 break;
1422 /* Not in mux list */
1423 if (mux_idx == per_pin->num_mux_nids)
1424 continue;
1425 break;
1426 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001427
Stephen Warren384a48d2011-06-01 11:14:21 -06001428 /* No free converters */
1429 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +08001430 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -06001431
Libin Yang42b29872015-12-16 13:42:42 +08001432 if (per_pin != NULL)
1433 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +08001434
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001435 if (cvt_id)
1436 *cvt_id = cvt_idx;
1437 if (mux_id)
1438 *mux_id = mux_idx;
1439
1440 return 0;
1441}
1442
Mengdong Lin2df67422014-03-20 13:01:06 +08001443/* Assure the pin select the right convetor */
1444static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1445 struct hdmi_spec_per_pin *per_pin)
1446{
1447 hda_nid_t pin_nid = per_pin->pin_nid;
1448 int mux_idx, curr;
1449
1450 mux_idx = per_pin->mux_idx;
1451 curr = snd_hda_codec_read(codec, pin_nid, 0,
1452 AC_VERB_GET_CONNECT_SEL, 0);
1453 if (curr != mux_idx)
1454 snd_hda_codec_write_cache(codec, pin_nid, 0,
1455 AC_VERB_SET_CONNECT_SEL,
1456 mux_idx);
1457}
1458
Libin Yang42b29872015-12-16 13:42:42 +08001459/* get the mux index for the converter of the pins
1460 * converter's mux index is the same for all pins on Intel platform
1461 */
1462static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1463 hda_nid_t cvt_nid)
1464{
1465 int i;
1466
1467 for (i = 0; i < spec->num_cvts; i++)
1468 if (spec->cvt_nids[i] == cvt_nid)
1469 return i;
1470 return -EINVAL;
1471}
1472
Mengdong Lin300016b2013-11-04 01:13:13 -05001473/* Intel HDMI workaround to fix audio routing issue:
1474 * For some Intel display codecs, pins share the same connection list.
1475 * So a conveter can be selected by multiple pins and playback on any of these
1476 * pins will generate sound on the external display, because audio flows from
1477 * the same converter to the display pipeline. Also muting one pin may make
1478 * other pins have no sound output.
1479 * So this function assures that an assigned converter for a pin is not selected
1480 * by any other pins.
1481 */
1482static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001483 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001484{
1485 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001486 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001487 int cvt_idx, curr;
1488 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001489
Mengdong Linf82d7d12013-09-21 20:34:45 -04001490 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +01001491 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -04001492 unsigned int wid_caps = get_wcaps(codec, nid);
1493 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001494
Mengdong Linf82d7d12013-09-21 20:34:45 -04001495 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001496 continue;
1497
Mengdong Linf82d7d12013-09-21 20:34:45 -04001498 if (nid == pin_nid)
1499 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001500
Mengdong Linf82d7d12013-09-21 20:34:45 -04001501 curr = snd_hda_codec_read(codec, nid, 0,
1502 AC_VERB_GET_CONNECT_SEL, 0);
1503 if (curr != mux_idx)
1504 continue;
1505
1506 /* choose an unassigned converter. The conveters in the
1507 * connection list are in the same order as in the codec.
1508 */
1509 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1510 per_cvt = get_cvt(spec, cvt_idx);
1511 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001512 codec_dbg(codec,
1513 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001514 cvt_idx, nid);
1515 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001516 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001517 cvt_idx);
1518 break;
1519 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001520 }
1521 }
1522}
1523
Libin Yang42b29872015-12-16 13:42:42 +08001524/* A wrapper of intel_not_share_asigned_cvt() */
1525static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1526 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1527{
1528 int mux_idx;
1529 struct hdmi_spec *spec = codec->spec;
1530
1531 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1532 return;
1533
1534 /* On Intel platform, the mapping of converter nid to
1535 * mux index of the pins are always the same.
1536 * The pin nid may be 0, this means all pins will not
1537 * share the converter.
1538 */
1539 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1540 if (mux_idx >= 0)
1541 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1542}
1543
1544/* called in hdmi_pcm_open when no pin is assigned to the PCM
1545 * in dyn_pcm_assign mode.
1546 */
1547static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1548 struct hda_codec *codec,
1549 struct snd_pcm_substream *substream)
1550{
1551 struct hdmi_spec *spec = codec->spec;
1552 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001553 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001554 struct hdmi_spec_per_cvt *per_cvt = NULL;
1555 int err;
1556
Libin Yangac983792015-12-16 16:48:16 +08001557 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1558 if (pcm_idx < 0)
1559 return -EINVAL;
1560
Libin Yang42b29872015-12-16 13:42:42 +08001561 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1562 if (err)
1563 return err;
1564
1565 per_cvt = get_cvt(spec, cvt_idx);
1566 per_cvt->assigned = 1;
1567 hinfo->nid = per_cvt->cvt_nid;
1568
1569 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1570
Libin Yangac983792015-12-16 16:48:16 +08001571 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001572 /* todo: setup spdif ctls assign */
1573
1574 /* Initially set the converter's capabilities */
1575 hinfo->channels_min = per_cvt->channels_min;
1576 hinfo->channels_max = per_cvt->channels_max;
1577 hinfo->rates = per_cvt->rates;
1578 hinfo->formats = per_cvt->formats;
1579 hinfo->maxbps = per_cvt->maxbps;
1580
1581 /* Store the updated parameters */
1582 runtime->hw.channels_min = hinfo->channels_min;
1583 runtime->hw.channels_max = hinfo->channels_max;
1584 runtime->hw.formats = hinfo->formats;
1585 runtime->hw.rates = hinfo->rates;
1586
1587 snd_pcm_hw_constraint_step(substream->runtime, 0,
1588 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1589 return 0;
1590}
1591
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001592/*
1593 * HDA PCM callbacks
1594 */
1595static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1596 struct hda_codec *codec,
1597 struct snd_pcm_substream *substream)
1598{
1599 struct hdmi_spec *spec = codec->spec;
1600 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001601 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001602 struct hdmi_spec_per_pin *per_pin;
1603 struct hdmi_eld *eld;
1604 struct hdmi_spec_per_cvt *per_cvt = NULL;
1605 int err;
1606
1607 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001608 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1609 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001610 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001611
Libin Yang42b29872015-12-16 13:42:42 +08001612 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001613 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001614 if (!spec->dyn_pcm_assign) {
1615 if (snd_BUG_ON(pin_idx < 0)) {
1616 mutex_unlock(&spec->pcm_lock);
1617 return -EINVAL;
1618 }
1619 } else {
1620 /* no pin is assigned to the PCM
1621 * PA need pcm open successfully when probe
1622 */
1623 if (pin_idx < 0) {
1624 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1625 mutex_unlock(&spec->pcm_lock);
1626 return err;
1627 }
1628 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001629
1630 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001631 if (err < 0) {
1632 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001633 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001634 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001635
1636 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001637 /* Claim converter */
1638 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001639
Libin Yangac983792015-12-16 16:48:16 +08001640 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001641 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001642 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001643 hinfo->nid = per_cvt->cvt_nid;
1644
Takashi Iwaibddee962013-06-18 16:14:22 +02001645 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001646 AC_VERB_SET_CONNECT_SEL,
1647 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001648
1649 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001650 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001651 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001652
Libin Yang2bf3c852015-12-16 13:42:43 +08001653 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001654
Stephen Warren2def8172011-06-01 11:14:20 -06001655 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001656 hinfo->channels_min = per_cvt->channels_min;
1657 hinfo->channels_max = per_cvt->channels_max;
1658 hinfo->rates = per_cvt->rates;
1659 hinfo->formats = per_cvt->formats;
1660 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001661
Libin Yang42b29872015-12-16 13:42:42 +08001662 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001663 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001664 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001665 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001666 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001667 !hinfo->rates || !hinfo->formats) {
1668 per_cvt->assigned = 0;
1669 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001670 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001671 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001672 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001673 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001674 }
Stephen Warren2def8172011-06-01 11:14:20 -06001675
Libin Yang42b29872015-12-16 13:42:42 +08001676 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001677 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001678 runtime->hw.channels_min = hinfo->channels_min;
1679 runtime->hw.channels_max = hinfo->channels_max;
1680 runtime->hw.formats = hinfo->formats;
1681 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001682
1683 snd_pcm_hw_constraint_step(substream->runtime, 0,
1684 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001685 return 0;
1686}
1687
1688/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001689 * HDA/HDMI auto parsing
1690 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001691static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001692{
1693 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001694 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001695 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001696
1697 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001698 codec_warn(codec,
1699 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001700 pin_nid, get_wcaps(codec, pin_nid));
1701 return -EINVAL;
1702 }
1703
Stephen Warren384a48d2011-06-01 11:14:21 -06001704 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1705 per_pin->mux_nids,
1706 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001707
1708 return 0;
1709}
1710
Libin Yanga76056f2015-12-16 16:48:15 +08001711static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1712 struct hdmi_spec_per_pin *per_pin)
1713{
1714 int i;
1715
1716 /* try the prefer PCM */
1717 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1718 return per_pin->pin_nid_idx;
1719
1720 /* have a second try; check the "reserved area" over num_pins */
1721 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1722 if (!test_bit(i, &spec->pcm_bitmap))
1723 return i;
1724 }
1725
1726 /* the last try; check the empty slots in pins */
1727 for (i = 0; i < spec->num_pins; i++) {
1728 if (!test_bit(i, &spec->pcm_bitmap))
1729 return i;
1730 }
1731 return -EBUSY;
1732}
1733
1734static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1735 struct hdmi_spec_per_pin *per_pin)
1736{
1737 int idx;
1738
1739 /* pcm already be attached to the pin */
1740 if (per_pin->pcm)
1741 return;
1742 idx = hdmi_find_pcm_slot(spec, per_pin);
Libin Yangd10a80d2016-03-01 15:18:26 +08001743 if (idx == -EBUSY)
Libin Yanga76056f2015-12-16 16:48:15 +08001744 return;
1745 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001746 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001747 set_bit(idx, &spec->pcm_bitmap);
1748}
1749
1750static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1751 struct hdmi_spec_per_pin *per_pin)
1752{
1753 int idx;
1754
1755 /* pcm already be detached from the pin */
1756 if (!per_pin->pcm)
1757 return;
1758 idx = per_pin->pcm_idx;
1759 per_pin->pcm_idx = -1;
1760 per_pin->pcm = NULL;
1761 if (idx >= 0 && idx < spec->pcm_used)
1762 clear_bit(idx, &spec->pcm_bitmap);
1763}
1764
Libin Yangac983792015-12-16 16:48:16 +08001765static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1766 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1767{
1768 int mux_idx;
1769
1770 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1771 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1772 break;
1773 return mux_idx;
1774}
1775
1776static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1777
1778static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1779 struct hdmi_spec_per_pin *per_pin)
1780{
1781 struct hda_codec *codec = per_pin->codec;
1782 struct hda_pcm *pcm;
1783 struct hda_pcm_stream *hinfo;
1784 struct snd_pcm_substream *substream;
1785 int mux_idx;
1786 bool non_pcm;
1787
1788 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001789 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001790 else
1791 return;
1792 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1793 return;
1794
1795 /* hdmi audio only uses playback and one substream */
1796 hinfo = pcm->stream;
1797 substream = pcm->pcm->streams[0].substream;
1798
1799 per_pin->cvt_nid = hinfo->nid;
1800
1801 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1802 if (mux_idx < per_pin->num_mux_nids)
1803 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1804 AC_VERB_SET_CONNECT_SEL,
1805 mux_idx);
1806 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1807
1808 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1809 if (substream->runtime)
1810 per_pin->channels = substream->runtime->channels;
1811 per_pin->setup = true;
1812 per_pin->mux_idx = mux_idx;
1813
1814 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1815}
1816
1817static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1818 struct hdmi_spec_per_pin *per_pin)
1819{
1820 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1821 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1822
1823 per_pin->chmap_set = false;
1824 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1825
1826 per_pin->setup = false;
1827 per_pin->channels = 0;
1828}
1829
Takashi Iwaie90247f2015-11-13 09:12:12 +01001830/* update per_pin ELD from the given new ELD;
1831 * setup info frame and notification accordingly
1832 */
1833static void update_eld(struct hda_codec *codec,
1834 struct hdmi_spec_per_pin *per_pin,
1835 struct hdmi_eld *eld)
1836{
1837 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001838 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001839 bool old_eld_valid = pin_eld->eld_valid;
1840 bool eld_changed;
Libin Yangfb087ea2016-02-23 16:33:37 +08001841 int pcm_idx = -1;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001842
Libin Yangfb087ea2016-02-23 16:33:37 +08001843 /* for monitor disconnection, save pcm_idx firstly */
1844 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001845 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001846 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001847 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001848 hdmi_pcm_setup_pin(spec, per_pin);
1849 } else {
1850 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001851 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001852 }
Libin Yanga76056f2015-12-16 16:48:15 +08001853 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001854 /* if pcm_idx == -1, it means this is in monitor connection event
1855 * we can get the correct pcm_idx now.
1856 */
1857 if (pcm_idx == -1)
1858 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001859
Takashi Iwaie90247f2015-11-13 09:12:12 +01001860 if (eld->eld_valid)
1861 snd_hdmi_show_eld(codec, &eld->info);
1862
1863 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1864 if (eld->eld_valid && pin_eld->eld_valid)
1865 if (pin_eld->eld_size != eld->eld_size ||
1866 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1867 eld->eld_size) != 0)
1868 eld_changed = true;
1869
1870 pin_eld->eld_valid = eld->eld_valid;
1871 pin_eld->eld_size = eld->eld_size;
1872 if (eld->eld_valid)
1873 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1874 pin_eld->info = eld->info;
1875
1876 /*
1877 * Re-setup pin and infoframe. This is needed e.g. when
1878 * - sink is first plugged-in
1879 * - transcoder can change during stream playback on Haswell
1880 * and this can make HW reset converter selection on a pin.
1881 */
1882 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1883 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1884 intel_verify_pin_cvt_connect(codec, per_pin);
1885 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1886 per_pin->mux_idx);
1887 }
1888
1889 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1890 }
1891
Libin Yangfb087ea2016-02-23 16:33:37 +08001892 if (eld_changed && pcm_idx >= 0)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001893 snd_ctl_notify(codec->card,
1894 SNDRV_CTL_EVENT_MASK_VALUE |
1895 SNDRV_CTL_EVENT_MASK_INFO,
Libin Yangfb087ea2016-02-23 16:33:37 +08001896 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001897}
1898
Takashi Iwai788d4412015-11-12 15:36:13 +01001899/* update ELD and jack state via HD-audio verbs */
1900static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1901 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001902{
David Henningsson464837a2013-11-07 13:38:25 +01001903 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001904 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001905 struct hdmi_spec *spec = codec->spec;
1906 struct hdmi_eld *eld = &spec->temp_eld;
1907 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001908 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001909 /*
1910 * Always execute a GetPinSense verb here, even when called from
1911 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1912 * response's PD bit is not the real PD value, but indicates that
1913 * the real PD value changed. An older version of the HD-audio
1914 * specification worked this way. Hence, we just ignore the data in
1915 * the unsolicited response to avoid custom WARs.
1916 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001917 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001918 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001919 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001920
Takashi Iwai664c7152015-04-08 11:43:14 +02001921 snd_hda_power_up_pm(codec);
David Henningssonda4a7a32013-12-18 10:46:04 +01001922 present = snd_hda_pin_sense(codec, pin_nid);
1923
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001924 mutex_lock(&per_pin->lock);
David Henningsson4bd038f2013-02-19 16:11:25 +01001925 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1926 if (pin_eld->monitor_present)
1927 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1928 else
1929 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001930
Takashi Iwai4e76a882014-02-25 12:21:03 +01001931 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001932 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001933 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001934
David Henningsson4bd038f2013-02-19 16:11:25 +01001935 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001936 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001937 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001938 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001939 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001940 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001941 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001942 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001943 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001944 if (!eld->eld_valid && repoll)
1945 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001946 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001947
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001948 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001949 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1950 else
1951 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001952
Takashi Iwaiaff747eb2013-11-07 16:39:37 +01001953 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001954
1955 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1956 if (jack)
1957 jack->block_report = !ret;
1958
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001959 mutex_unlock(&per_pin->lock);
Takashi Iwai664c7152015-04-08 11:43:14 +02001960 snd_hda_power_down_pm(codec);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001961 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001962}
1963
Libin Yang31842702016-02-19 15:42:06 +08001964static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1965 struct hdmi_spec_per_pin *per_pin)
1966{
1967 struct hdmi_spec *spec = codec->spec;
1968 struct snd_jack *jack = NULL;
1969 struct hda_jack_tbl *jack_tbl;
1970
1971 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1972 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1973 * NULL even after snd_hda_jack_tbl_clear() is called to
1974 * free snd_jack. This may cause access invalid memory
1975 * when calling snd_jack_report
1976 */
1977 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1978 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1979 else if (!spec->dyn_pcm_assign) {
1980 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1981 if (jack_tbl)
1982 jack = jack_tbl->jack;
1983 }
1984 return jack;
1985}
1986
Takashi Iwai788d4412015-11-12 15:36:13 +01001987/* update ELD and jack state via audio component */
1988static void sync_eld_via_acomp(struct hda_codec *codec,
1989 struct hdmi_spec_per_pin *per_pin)
1990{
Takashi Iwai788d4412015-11-12 15:36:13 +01001991 struct hdmi_spec *spec = codec->spec;
1992 struct hdmi_eld *eld = &spec->temp_eld;
Libin Yang25e4abb2016-01-12 11:13:27 +08001993 struct snd_jack *jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001994 int size;
1995
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001996 mutex_lock(&per_pin->lock);
1997 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1998 &eld->monitor_present, eld->eld_buffer,
1999 ELD_MAX_SIZE);
2000 if (size < 0)
2001 goto unlock;
2002 if (size > 0) {
2003 size = min(size, ELD_MAX_SIZE);
2004 if (snd_hdmi_parse_eld(codec, &eld->info,
2005 eld->eld_buffer, size) < 0)
2006 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002007 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002008
2009 if (size > 0) {
2010 eld->eld_valid = true;
2011 eld->eld_size = size;
2012 } else {
2013 eld->eld_valid = false;
2014 eld->eld_size = 0;
2015 }
2016
Libin Yang25e4abb2016-01-12 11:13:27 +08002017 /* pcm_idx >=0 before update_eld() means it is in monitor
2018 * disconnected event. Jack must be fetched before update_eld()
2019 */
Libin Yang31842702016-02-19 15:42:06 +08002020 jack = pin_idx_to_jack(codec, per_pin);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002021 update_eld(codec, per_pin, eld);
Libin Yang31842702016-02-19 15:42:06 +08002022 if (jack == NULL)
2023 jack = pin_idx_to_jack(codec, per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002024 if (jack == NULL)
2025 goto unlock;
2026 snd_jack_report(jack,
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002027 eld->monitor_present ? SND_JACK_AVOUT : 0);
2028 unlock:
2029 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01002030}
2031
2032static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
2033{
2034 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08002035 struct hdmi_spec *spec = codec->spec;
2036 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01002037
Libin Yanga76056f2015-12-16 16:48:15 +08002038 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01002039 if (codec_has_acomp(codec)) {
2040 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08002041 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01002042 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08002043 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01002044 }
Libin Yanga76056f2015-12-16 16:48:15 +08002045 mutex_unlock(&spec->pcm_lock);
2046
2047 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01002048}
2049
Wu Fengguang744626d2011-11-16 16:29:47 +08002050static void hdmi_repoll_eld(struct work_struct *work)
2051{
2052 struct hdmi_spec_per_pin *per_pin =
2053 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
2054
Wu Fengguangc6e84532011-11-18 16:59:32 -06002055 if (per_pin->repoll_count++ > 6)
2056 per_pin->repoll_count = 0;
2057
Takashi Iwaiefe47102013-11-07 13:38:23 +01002058 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
2059 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08002060}
2061
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002062static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2063 hda_nid_t nid);
2064
Wu Fengguang079d88c2010-03-08 10:44:23 +08002065static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2066{
2067 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002068 unsigned int caps, config;
2069 int pin_idx;
2070 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02002071 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002072
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002073 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002074 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2075 return 0;
2076
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002077 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002078 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2079 return 0;
2080
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002081 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002082 intel_haswell_fixup_connect_list(codec, pin_nid);
2083
Stephen Warren384a48d2011-06-01 11:14:21 -06002084 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002085 per_pin = snd_array_new(&spec->pins);
2086 if (!per_pin)
2087 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002088
2089 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002090 per_pin->non_pcm = false;
Libin Yanga76056f2015-12-16 16:48:15 +08002091 if (spec->dyn_pcm_assign)
2092 per_pin->pcm_idx = -1;
Libin Yang2bea2412016-01-12 11:13:26 +08002093 else {
2094 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
Libin Yanga76056f2015-12-16 16:48:15 +08002095 per_pin->pcm_idx = pin_idx;
Libin Yang2bea2412016-01-12 11:13:26 +08002096 }
Libin Yanga76056f2015-12-16 16:48:15 +08002097 per_pin->pin_nid_idx = pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002098
Stephen Warren384a48d2011-06-01 11:14:21 -06002099 err = hdmi_read_pin_conn(codec, pin_idx);
2100 if (err < 0)
2101 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002102
Wu Fengguang079d88c2010-03-08 10:44:23 +08002103 spec->num_pins++;
2104
Stephen Warren384a48d2011-06-01 11:14:21 -06002105 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002106}
2107
Stephen Warren384a48d2011-06-01 11:14:21 -06002108static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08002109{
2110 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002111 struct hdmi_spec_per_cvt *per_cvt;
2112 unsigned int chans;
2113 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002114
Stephen Warren384a48d2011-06-01 11:14:21 -06002115 chans = get_wcaps(codec, cvt_nid);
2116 chans = get_wcaps_channels(chans);
2117
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002118 per_cvt = snd_array_new(&spec->cvts);
2119 if (!per_cvt)
2120 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002121
2122 per_cvt->cvt_nid = cvt_nid;
2123 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002124 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002125 per_cvt->channels_max = chans;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302126 if (chans > spec->chmap.channels_max)
2127 spec->chmap.channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002128 }
Stephen Warren384a48d2011-06-01 11:14:21 -06002129
2130 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2131 &per_cvt->rates,
2132 &per_cvt->formats,
2133 &per_cvt->maxbps);
2134 if (err < 0)
2135 return err;
2136
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002137 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2138 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2139 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002140
2141 return 0;
2142}
2143
2144static int hdmi_parse_codec(struct hda_codec *codec)
2145{
2146 hda_nid_t nid;
2147 int i, nodes;
2148
Takashi Iwai7639a062015-03-03 10:07:24 +01002149 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002150 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002151 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08002152 return -EINVAL;
2153 }
2154
2155 for (i = 0; i < nodes; i++, nid++) {
2156 unsigned int caps;
2157 unsigned int type;
2158
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002159 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002160 type = get_wcaps_type(caps);
2161
2162 if (!(caps & AC_WCAP_DIGITAL))
2163 continue;
2164
2165 switch (type) {
2166 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06002167 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002168 break;
2169 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08002170 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002171 break;
2172 }
2173 }
2174
Wu Fengguang079d88c2010-03-08 10:44:23 +08002175 return 0;
2176}
2177
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002178/*
2179 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002180static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2181{
2182 struct hda_spdif_out *spdif;
2183 bool non_pcm;
2184
2185 mutex_lock(&codec->spdif_mutex);
2186 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2187 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2188 mutex_unlock(&codec->spdif_mutex);
2189 return non_pcm;
2190}
2191
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002192/*
2193 * HDMI callbacks
2194 */
2195
2196static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2197 struct hda_codec *codec,
2198 unsigned int stream_tag,
2199 unsigned int format,
2200 struct snd_pcm_substream *substream)
2201{
Stephen Warren384a48d2011-06-01 11:14:21 -06002202 hda_nid_t cvt_nid = hinfo->nid;
2203 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08002204 int pin_idx;
2205 struct hdmi_spec_per_pin *per_pin;
2206 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08002207 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002208 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07002209 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08002210 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002211
Libin Yang42b29872015-12-16 13:42:42 +08002212 mutex_lock(&spec->pcm_lock);
2213 pin_idx = hinfo_to_pin_index(codec, hinfo);
2214 if (spec->dyn_pcm_assign && pin_idx < 0) {
2215 /* when dyn_pcm_assign and pcm is not bound to a pin
2216 * skip pin setup and return 0 to make audio playback
2217 * be ongoing
2218 */
2219 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2220 snd_hda_codec_setup_stream(codec, cvt_nid,
2221 stream_tag, 0, format);
2222 mutex_unlock(&spec->pcm_lock);
2223 return 0;
2224 }
2225
2226 if (snd_BUG_ON(pin_idx < 0)) {
2227 mutex_unlock(&spec->pcm_lock);
2228 return -EINVAL;
2229 }
2230 per_pin = get_pin(spec, pin_idx);
2231 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08002232 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08002233 /* Verify pin:cvt selections to avoid silent audio after S3.
2234 * After S3, the audio driver restores pin:cvt selections
2235 * but this can happen before gfx is ready and such selection
2236 * is overlooked by HW. Thus multiple pins can share a same
2237 * default convertor and mute control will affect each other,
2238 * which can cause a resumed audio playback become silent
2239 * after S3.
2240 */
2241 intel_verify_pin_cvt_connect(codec, per_pin);
2242 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2243 }
2244
Libin Yangddd621f2015-09-02 14:11:40 +08002245 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2246 /* Todo: add DP1.2 MST audio support later */
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002247 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08002248
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002249 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002250 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02002251 per_pin->channels = substream->runtime->channels;
2252 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002253
Takashi Iwaib0540872013-09-02 12:33:02 +02002254 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002255 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07002256 if (spec->dyn_pin_out) {
2257 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2258 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2259 snd_hda_codec_write(codec, pin_nid, 0,
2260 AC_VERB_SET_PIN_WIDGET_CONTROL,
2261 pinctl | PIN_OUT);
2262 }
2263
Libin Yang42b29872015-12-16 13:42:42 +08002264 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2265 stream_tag, format);
2266 mutex_unlock(&spec->pcm_lock);
2267 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002268}
2269
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002270static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2271 struct hda_codec *codec,
2272 struct snd_pcm_substream *substream)
2273{
2274 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2275 return 0;
2276}
2277
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002278static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2279 struct hda_codec *codec,
2280 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06002281{
2282 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08002283 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06002284 struct hdmi_spec_per_cvt *per_cvt;
2285 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07002286 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06002287
Stephen Warren384a48d2011-06-01 11:14:21 -06002288 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08002289 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2290 if (snd_BUG_ON(pcm_idx < 0))
2291 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002292 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002293 if (snd_BUG_ON(cvt_idx < 0))
2294 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002295 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002296
2297 snd_BUG_ON(!per_cvt->assigned);
2298 per_cvt->assigned = 0;
2299 hinfo->nid = 0;
2300
Libin Yang42b29872015-12-16 13:42:42 +08002301 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08002302 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08002303 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002304 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08002305 if (spec->dyn_pcm_assign && pin_idx < 0) {
2306 mutex_unlock(&spec->pcm_lock);
2307 return 0;
2308 }
2309
2310 if (snd_BUG_ON(pin_idx < 0)) {
2311 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002312 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08002313 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002314 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002315
Stephen Warren75fae112014-01-30 11:52:16 -07002316 if (spec->dyn_pin_out) {
2317 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2318 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2319 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2320 AC_VERB_SET_PIN_WIDGET_CONTROL,
2321 pinctl & ~PIN_OUT);
2322 }
2323
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002324 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002325 per_pin->chmap_set = false;
2326 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002327
2328 per_pin->setup = false;
2329 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002330 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08002331 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002332 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002333
Stephen Warren384a48d2011-06-01 11:14:21 -06002334 return 0;
2335}
2336
2337static const struct hda_pcm_ops generic_ops = {
2338 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002339 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002340 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002341 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002342};
2343
Takashi Iwaid45e6882012-07-31 11:36:00 +02002344/*
2345 * ALSA API channel-map control callbacks
2346 */
2347static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2348 struct snd_ctl_elem_info *uinfo)
2349{
2350 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302351 struct hdac_chmap *chmap = info->private_data;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302352
Takashi Iwaid45e6882012-07-31 11:36:00 +02002353 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302354 uinfo->count = chmap->channels_max;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002355 uinfo->value.integer.min = 0;
2356 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2357 return 0;
2358}
2359
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302360static int hdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
2361 struct cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula307229d2013-10-24 21:10:34 +03002362{
2363 /* If the speaker allocation matches the channel count, it is OK.*/
2364 if (cap->channels != channels)
2365 return -1;
2366
2367 /* all channels are remappable freely */
2368 return SNDRV_CTL_TLVT_CHMAP_VAR;
2369}
2370
2371static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2372 unsigned int *chmap, int channels)
2373{
2374 int count = 0;
2375 int c;
2376
2377 for (c = 7; c >= 0; c--) {
2378 int spk = cap->speakers[c];
2379 if (!spk)
2380 continue;
2381
2382 chmap[count++] = spk_to_chmap(spk);
2383 }
2384
2385 WARN_ON(count != channels);
2386}
2387
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302388static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2389 unsigned char *chmap)
2390{
2391 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2392 struct hdmi_spec *spec = codec->spec;
2393 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2394
2395 /* chmap is already set to 0 in caller */
2396 if (!per_pin)
2397 return;
2398
2399 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2400}
2401
2402static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2403 unsigned char *chmap, int prepared)
2404{
2405 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2406 struct hdmi_spec *spec = codec->spec;
2407 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2408
2409 mutex_lock(&per_pin->lock);
2410 per_pin->chmap_set = true;
2411 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2412 if (prepared)
2413 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2414 mutex_unlock(&per_pin->lock);
2415}
2416
2417static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2418{
2419 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2420 struct hdmi_spec *spec = codec->spec;
2421 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2422
2423 return per_pin ? true:false;
2424}
2425
Takashi Iwaid45e6882012-07-31 11:36:00 +02002426static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2427 unsigned int size, unsigned int __user *tlv)
2428{
2429 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302430 struct hdac_chmap *chmap = info->private_data;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002431 unsigned int __user *dst;
2432 int chs, count = 0;
2433
2434 if (size < 8)
2435 return -ENOMEM;
2436 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2437 return -EFAULT;
2438 size -= 8;
2439 dst = tlv + 2;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302440 for (chs = 2; chs <= chmap->channels_max; chs++) {
Anssi Hannula307229d2013-10-24 21:10:34 +03002441 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002442 struct cea_channel_speaker_allocation *cap;
2443 cap = channel_allocations;
2444 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2445 int chs_bytes = chs * 4;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302446 int type = chmap->ops.chmap_cea_alloc_validate_get_type(
2447 chmap, cap, chs);
Anssi Hannula307229d2013-10-24 21:10:34 +03002448 unsigned int tlv_chmap[8];
2449
2450 if (type < 0)
Takashi Iwaid45e6882012-07-31 11:36:00 +02002451 continue;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002452 if (size < 8)
2453 return -ENOMEM;
Anssi Hannula307229d2013-10-24 21:10:34 +03002454 if (put_user(type, dst) ||
Takashi Iwaid45e6882012-07-31 11:36:00 +02002455 put_user(chs_bytes, dst + 1))
2456 return -EFAULT;
2457 dst += 2;
2458 size -= 8;
2459 count += 8;
2460 if (size < chs_bytes)
2461 return -ENOMEM;
2462 size -= chs_bytes;
2463 count += chs_bytes;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302464 chmap->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
Anssi Hannula307229d2013-10-24 21:10:34 +03002465 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2466 return -EFAULT;
2467 dst += chs;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002468 }
2469 }
2470 if (put_user(count, tlv + 1))
2471 return -EFAULT;
2472 return 0;
2473}
2474
2475static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2476 struct snd_ctl_elem_value *ucontrol)
2477{
2478 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302479 struct hdac_chmap *chmap = info->private_data;
Libin Yang022f3442016-02-03 10:48:34 +08002480 int pcm_idx = kcontrol->private_value;
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302481 unsigned char pcm_chmap[8];
Takashi Iwaid45e6882012-07-31 11:36:00 +02002482 int i;
2483
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302484 memset(pcm_chmap, 0, sizeof(pcm_chmap));
2485 chmap->ops.get_chmap(chmap->hdac, pcm_idx, pcm_chmap);
Libin Yang022f3442016-02-03 10:48:34 +08002486
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302487 for (i = 0; i < sizeof(chmap); i++)
2488 ucontrol->value.integer.value[i] = pcm_chmap[i];
2489
Takashi Iwaid45e6882012-07-31 11:36:00 +02002490 return 0;
2491}
2492
2493static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2494 struct snd_ctl_elem_value *ucontrol)
2495{
2496 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302497 struct hdac_chmap *hchmap = info->private_data;
Libin Yang022f3442016-02-03 10:48:34 +08002498 int pcm_idx = kcontrol->private_value;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002499 unsigned int ctl_idx;
2500 struct snd_pcm_substream *substream;
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302501 unsigned char chmap[8], per_pin_chmap[8];
Anssi Hannula307229d2013-10-24 21:10:34 +03002502 int i, err, ca, prepared = 0;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002503
Libin Yang022f3442016-02-03 10:48:34 +08002504 /* No monitor is connected in dyn_pcm_assign.
2505 * It's invalid to setup the chmap
2506 */
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302507 if (!hchmap->ops.is_pcm_attached(hchmap->hdac, pcm_idx))
Libin Yang022f3442016-02-03 10:48:34 +08002508 return 0;
2509
Takashi Iwaid45e6882012-07-31 11:36:00 +02002510 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2511 substream = snd_pcm_chmap_substream(info, ctl_idx);
2512 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01002513 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02002514 switch (substream->runtime->status->state) {
2515 case SNDRV_PCM_STATE_OPEN:
2516 case SNDRV_PCM_STATE_SETUP:
2517 break;
2518 case SNDRV_PCM_STATE_PREPARED:
2519 prepared = 1;
2520 break;
2521 default:
2522 return -EBUSY;
2523 }
2524 memset(chmap, 0, sizeof(chmap));
2525 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2526 chmap[i] = ucontrol->value.integer.value[i];
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302527
2528 hchmap->ops.get_chmap(hchmap->hdac, pcm_idx, per_pin_chmap);
2529 if (!memcmp(chmap, per_pin_chmap, sizeof(chmap)))
Takashi Iwaid45e6882012-07-31 11:36:00 +02002530 return 0;
2531 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2532 if (ca < 0)
2533 return -EINVAL;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302534 if (hchmap->ops.chmap_validate) {
2535 err = hchmap->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
Anssi Hannula307229d2013-10-24 21:10:34 +03002536 if (err)
2537 return err;
2538 }
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302539
2540 hchmap->ops.set_chmap(hchmap->hdac, pcm_idx, chmap, prepared);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002541
2542 return 0;
2543}
2544
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002545static int generic_hdmi_build_pcms(struct hda_codec *codec)
2546{
2547 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002548 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002549
Stephen Warren384a48d2011-06-01 11:14:21 -06002550 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2551 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002552 struct hda_pcm_stream *pstr;
2553
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002554 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002555 if (!info)
2556 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002557
2558 spec->pcm_rec[pin_idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002559 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002560 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002561 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002562
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002563 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002564 pstr->substreams = 1;
2565 pstr->ops = generic_ops;
2566 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002567 }
2568
2569 return 0;
2570}
2571
Libin Yang25e4abb2016-01-12 11:13:27 +08002572static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01002573{
Libin Yang25e4abb2016-01-12 11:13:27 +08002574 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01002575
Libin Yang25e4abb2016-01-12 11:13:27 +08002576 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002577}
2578
Libin Yang25e4abb2016-01-12 11:13:27 +08002579static int add_hdmi_jack_kctl(struct hda_codec *codec,
2580 struct hdmi_spec *spec,
2581 int pcm_idx,
Takashi Iwai788d4412015-11-12 15:36:13 +01002582 const char *name)
2583{
2584 struct snd_jack *jack;
2585 int err;
2586
2587 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2588 true, false);
2589 if (err < 0)
2590 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002591
2592 spec->pcm_rec[pcm_idx].jack = jack;
2593 jack->private_data = &spec->pcm_rec[pcm_idx];
2594 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01002595 return 0;
2596}
2597
Libin Yang25e4abb2016-01-12 11:13:27 +08002598static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
David Henningsson0b6c49b2011-08-23 16:56:03 +02002599{
Takashi Iwai31ef2252011-12-01 17:41:36 +01002600 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02002601 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002602 struct hdmi_spec_per_pin *per_pin;
2603 struct hda_jack_tbl *jack;
2604 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01002605 bool phantom_jack;
Libin Yang25e4abb2016-01-12 11:13:27 +08002606 int ret;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002607
Takashi Iwai31ef2252011-12-01 17:41:36 +01002608 if (pcmdev > 0)
2609 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Libin Yang25e4abb2016-01-12 11:13:27 +08002610
2611 if (spec->dyn_pcm_assign)
2612 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2613
2614 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2615 /* if !dyn_pcm_assign, it must be non-MST mode.
2616 * This means pcms and pins are statically mapped.
2617 * And pcm_idx is pin_idx.
2618 */
2619 per_pin = get_pin(spec, pcm_idx);
Takashi Iwai909cadc2015-11-12 11:52:13 +01002620 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2621 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01002622 strncat(hdmi_str, " Phantom",
2623 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
Libin Yang25e4abb2016-01-12 11:13:27 +08002624 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2625 phantom_jack);
2626 if (ret < 0)
2627 return ret;
2628 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2629 if (jack == NULL)
2630 return 0;
2631 /* assign jack->jack to pcm_rec[].jack to
2632 * align with dyn_pcm_assign mode
2633 */
2634 spec->pcm_rec[pcm_idx].jack = jack->jack;
2635 return 0;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002636}
2637
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002638static int generic_hdmi_build_controls(struct hda_codec *codec)
2639{
2640 struct hdmi_spec *spec = codec->spec;
2641 int err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002642 int pin_idx, pcm_idx;
2643
2644
2645 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2646 err = generic_hdmi_build_jack(codec, pcm_idx);
2647 if (err < 0)
2648 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08002649
2650 /* create the spdif for each pcm
2651 * pin will be bound when monitor is connected
2652 */
2653 if (spec->dyn_pcm_assign)
2654 err = snd_hda_create_dig_out_ctls(codec,
2655 0, spec->cvt_nids[0],
2656 HDA_PCM_TYPE_HDMI);
2657 else {
2658 struct hdmi_spec_per_pin *per_pin =
2659 get_pin(spec, pcm_idx);
2660 err = snd_hda_create_dig_out_ctls(codec,
2661 per_pin->pin_nid,
2662 per_pin->mux_nids[0],
2663 HDA_PCM_TYPE_HDMI);
2664 }
2665 if (err < 0)
2666 return err;
2667 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002668
2669 /* add control for ELD Bytes */
2670 err = hdmi_create_eld_ctl(codec, pcm_idx,
2671 get_pcm_rec(spec, pcm_idx)->device);
2672 if (err < 0)
2673 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002674 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002675
Stephen Warren384a48d2011-06-01 11:14:21 -06002676 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002677 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002678
Takashi Iwai82b1d732011-12-20 15:53:07 +01002679 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002680 }
2681
Takashi Iwaid45e6882012-07-31 11:36:00 +02002682 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002683 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002684 struct hda_pcm *pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002685 struct snd_pcm_chmap *chmap;
2686 struct snd_kcontrol *kctl;
2687 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002688
Libin Yang022f3442016-02-03 10:48:34 +08002689 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002690 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002691 break;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002692 err = snd_pcm_add_chmap_ctls(pcm->pcm,
Takashi Iwaid45e6882012-07-31 11:36:00 +02002693 SNDRV_PCM_STREAM_PLAYBACK,
Libin Yang022f3442016-02-03 10:48:34 +08002694 NULL, 0, pcm_idx, &chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002695 if (err < 0)
2696 return err;
2697 /* override handlers */
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302698 chmap->private_data = &spec->chmap;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002699 kctl = chmap->kctl;
2700 for (i = 0; i < kctl->count; i++)
2701 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2702 kctl->info = hdmi_chmap_ctl_info;
2703 kctl->get = hdmi_chmap_ctl_get;
2704 kctl->put = hdmi_chmap_ctl_put;
2705 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2706 }
2707
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002708 return 0;
2709}
2710
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002711static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2712{
2713 struct hdmi_spec *spec = codec->spec;
2714 int pin_idx;
2715
2716 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002717 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002718
2719 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002720 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002721 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002722 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002723 }
2724 return 0;
2725}
2726
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002727static int generic_hdmi_init(struct hda_codec *codec)
2728{
2729 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002730 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002731
Stephen Warren384a48d2011-06-01 11:14:21 -06002732 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002733 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002734 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002735
2736 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002737 if (!codec_has_acomp(codec))
2738 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2739 codec->jackpoll_interval > 0 ?
2740 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002741 }
2742 return 0;
2743}
2744
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002745static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2746{
2747 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2748 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002749}
2750
2751static void hdmi_array_free(struct hdmi_spec *spec)
2752{
2753 snd_array_free(&spec->pins);
2754 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002755}
2756
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002757static void generic_hdmi_free(struct hda_codec *codec)
2758{
2759 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002760 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002761
Takashi Iwai66032492015-12-01 16:49:35 +01002762 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002763 snd_hdac_i915_register_notifier(NULL);
2764
Stephen Warren384a48d2011-06-01 11:14:21 -06002765 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002766 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002767 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002768 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002769 }
2770
2771 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2772 if (spec->pcm_rec[pcm_idx].jack == NULL)
2773 continue;
2774 if (spec->dyn_pcm_assign)
2775 snd_device_free(codec->card,
2776 spec->pcm_rec[pcm_idx].jack);
2777 else
2778 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002779 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002780
Takashi Iwai55913112015-12-10 13:03:29 +01002781 if (spec->i915_bound)
2782 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002783 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002784 kfree(spec);
2785}
2786
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002787#ifdef CONFIG_PM
2788static int generic_hdmi_resume(struct hda_codec *codec)
2789{
2790 struct hdmi_spec *spec = codec->spec;
2791 int pin_idx;
2792
Pierre Ossmana2833682014-06-18 21:48:09 +02002793 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002794 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002795
2796 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2797 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2798 hdmi_present_sense(per_pin, 1);
2799 }
2800 return 0;
2801}
2802#endif
2803
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002804static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002805 .init = generic_hdmi_init,
2806 .free = generic_hdmi_free,
2807 .build_pcms = generic_hdmi_build_pcms,
2808 .build_controls = generic_hdmi_build_controls,
2809 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002810#ifdef CONFIG_PM
2811 .resume = generic_hdmi_resume,
2812#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002813};
2814
Anssi Hannula307229d2013-10-24 21:10:34 +03002815static const struct hdmi_ops generic_standard_hdmi_ops = {
2816 .pin_get_eld = snd_hdmi_get_eld,
2817 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2818 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2819 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2820 .pin_hbr_setup = hdmi_pin_hbr_setup,
2821 .setup_stream = hdmi_setup_stream,
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302822};
2823
2824static const struct hdac_chmap_ops chmap_ops = {
Anssi Hannula307229d2013-10-24 21:10:34 +03002825 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2826 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
Subhransu S. Prusty9b3dc8a2016-03-04 19:59:47 +05302827 .get_chmap = hdmi_get_chmap,
2828 .set_chmap = hdmi_set_chmap,
2829 .is_pcm_attached = is_hdmi_pcm_attached,
Anssi Hannula307229d2013-10-24 21:10:34 +03002830};
2831
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002832static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2833 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002834{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002835 struct hdmi_spec *spec = codec->spec;
2836 hda_nid_t conns[4];
2837 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002838
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002839 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2840 if (nconns == spec->num_cvts &&
2841 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002842 return;
2843
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002844 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002845 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002846 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002847}
2848
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002849#define INTEL_VENDOR_NID 0x08
2850#define INTEL_GET_VENDOR_VERB 0xf81
2851#define INTEL_SET_VENDOR_VERB 0x781
2852#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2853#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2854
2855static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002856 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002857{
2858 unsigned int vendor_param;
2859
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002860 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2861 INTEL_GET_VENDOR_VERB, 0);
2862 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2863 return;
2864
2865 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2866 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2867 INTEL_SET_VENDOR_VERB, vendor_param);
2868 if (vendor_param == -1)
2869 return;
2870
Takashi Iwai17df3f52013-05-08 08:09:34 +02002871 if (update_tree)
2872 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002873}
2874
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002875static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2876{
2877 unsigned int vendor_param;
2878
2879 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2880 INTEL_GET_VENDOR_VERB, 0);
2881 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2882 return;
2883
2884 /* enable DP1.2 mode */
2885 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002886 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002887 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2888 INTEL_SET_VENDOR_VERB, vendor_param);
2889}
2890
Takashi Iwai17df3f52013-05-08 08:09:34 +02002891/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2892 * Otherwise you may get severe h/w communication errors.
2893 */
2894static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2895 unsigned int power_state)
2896{
2897 if (power_state == AC_PWRST_D0) {
2898 intel_haswell_enable_all_pins(codec, false);
2899 intel_haswell_fixup_enable_dp12(codec);
2900 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002901
Takashi Iwai17df3f52013-05-08 08:09:34 +02002902 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2903 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2904}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002905
David Henningssonf0675d42015-09-03 11:51:34 +02002906static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002907{
2908 struct hda_codec *codec = audio_ptr;
2909 int pin_nid = port + 0x04;
2910
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002911 /* skip notification during system suspend (but not in runtime PM);
2912 * the state will be updated at resume
2913 */
2914 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2915 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002916 /* ditto during suspend/resume process itself */
2917 if (atomic_read(&(codec)->core.in_pm))
2918 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002919
David Henningsson25adc132015-08-19 10:48:58 +02002920 check_presence_and_report(codec, pin_nid);
2921}
2922
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002923static int patch_generic_hdmi(struct hda_codec *codec)
2924{
2925 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002926
2927 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2928 if (spec == NULL)
2929 return -ENOMEM;
2930
Anssi Hannula307229d2013-10-24 21:10:34 +03002931 spec->ops = generic_standard_hdmi_ops;
Libin Yang42b29872015-12-16 13:42:42 +08002932 mutex_init(&spec->pcm_lock);
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05302933 spec->chmap.ops = chmap_ops;
2934 spec->chmap.hdac = &codec->core;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002935 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002936 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002937
Takashi Iwai55913112015-12-10 13:03:29 +01002938 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2939 if (!codec_has_acomp(codec) &&
2940 (codec->core.vendor_id >> 16) == 0x8086)
2941 if (!snd_hdac_i915_init(&codec->bus->core))
2942 spec->i915_bound = true;
2943
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002944 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002945 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002946 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002947 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002948
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002949 /* For Valleyview/Cherryview, only the display codec is in the display
2950 * power well and can use link_power ops to request/release the power.
2951 * For Haswell/Broadwell, the controller is also in the power well and
2952 * can cover the codec power request, and so need not set this flag.
2953 * For previous platforms, there is no such power well feature.
2954 */
Lu, Hanff9d8852015-11-19 23:25:13 +08002955 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2956 is_broxton(codec))
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002957 codec->core.link_power_control = 1;
2958
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002959 if (hdmi_parse_codec(codec) < 0) {
Takashi Iwai55913112015-12-10 13:03:29 +01002960 if (spec->i915_bound)
2961 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002962 codec->spec = NULL;
2963 kfree(spec);
2964 return -EINVAL;
2965 }
2966 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002967 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002968 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002969 codec->dp_mst = true;
2970 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002971
Lu, Han2377c3c2015-06-09 16:50:38 +08002972 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2973 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2974 codec->auto_runtime_pm = 1;
2975
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002976 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002977
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002978 init_channel_allocations();
2979
Libin Yang790b4152016-03-04 14:33:43 +08002980 if (codec_has_acomp(codec)) {
2981 codec->depop_delay = 0;
2982 spec->i915_audio_ops.audio_ptr = codec;
2983 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2984 * will call pin_eld_notify with using audio_ptr pointer
2985 * We need make sure audio_ptr is really setup
2986 */
2987 wmb();
2988 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2989 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2990 }
2991
Libin Yang25e4abb2016-01-12 11:13:27 +08002992 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002993 return 0;
2994}
2995
2996/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002997 * Shared non-generic implementations
2998 */
2999
3000static int simple_playback_build_pcms(struct hda_codec *codec)
3001{
3002 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003003 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003004 unsigned int chans;
3005 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003006 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003007
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003008 per_cvt = get_cvt(spec, 0);
3009 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003010 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06003011
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003012 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003013 if (!info)
3014 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08003015 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003016 info->pcm_type = HDA_PCM_TYPE_HDMI;
3017 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
3018 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003019 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003020 if (pstr->channels_max <= 2 && chans && chans <= 16)
3021 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003022
3023 return 0;
3024}
3025
Takashi Iwai4b6ace92012-06-15 11:53:32 +02003026/* unsolicited event for jack sensing */
3027static void simple_hdmi_unsol_event(struct hda_codec *codec,
3028 unsigned int res)
3029{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02003030 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02003031 snd_hda_jack_report_sync(codec);
3032}
3033
3034/* generic_hdmi_build_jack can be used for simple_hdmi, too,
3035 * as long as spec->pins[] is set correctly
3036 */
3037#define simple_hdmi_build_jack generic_hdmi_build_jack
3038
Stephen Warren3aaf8982011-06-01 11:14:19 -06003039static int simple_playback_build_controls(struct hda_codec *codec)
3040{
3041 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003042 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003043 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003044
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003045 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02003046 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
3047 per_cvt->cvt_nid,
3048 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003049 if (err < 0)
3050 return err;
3051 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06003052}
3053
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003054static int simple_playback_init(struct hda_codec *codec)
3055{
3056 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003057 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
3058 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003059
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003060 snd_hda_codec_write(codec, pin, 0,
3061 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3062 /* some codecs require to unmute the pin */
3063 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3064 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3065 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02003066 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003067 return 0;
3068}
3069
Stephen Warren3aaf8982011-06-01 11:14:19 -06003070static void simple_playback_free(struct hda_codec *codec)
3071{
3072 struct hdmi_spec *spec = codec->spec;
3073
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003074 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06003075 kfree(spec);
3076}
3077
3078/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003079 * Nvidia specific implementations
3080 */
3081
3082#define Nv_VERB_SET_Channel_Allocation 0xF79
3083#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
3084#define Nv_VERB_SET_Audio_Protection_On 0xF98
3085#define Nv_VERB_SET_Audio_Protection_Off 0xF99
3086
3087#define nvhdmi_master_con_nid_7x 0x04
3088#define nvhdmi_master_pin_nid_7x 0x05
3089
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003090static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003091 /*front, rear, clfe, rear_surr */
3092 0x6, 0x8, 0xa, 0xc,
3093};
3094
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003095static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3096 /* set audio protect on */
3097 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3098 /* enable digital output on pin widget */
3099 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3100 {} /* terminator */
3101};
3102
3103static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003104 /* set audio protect on */
3105 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3106 /* enable digital output on pin widget */
3107 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3108 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3109 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3110 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3111 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3112 {} /* terminator */
3113};
3114
3115#ifdef LIMITED_RATE_FMT_SUPPORT
3116/* support only the safe format and rate */
3117#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3118#define SUPPORTED_MAXBPS 16
3119#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3120#else
3121/* support all rates and formats */
3122#define SUPPORTED_RATES \
3123 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3124 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3125 SNDRV_PCM_RATE_192000)
3126#define SUPPORTED_MAXBPS 24
3127#define SUPPORTED_FORMATS \
3128 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3129#endif
3130
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003131static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003132{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003133 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3134 return 0;
3135}
3136
3137static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3138{
3139 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003140 return 0;
3141}
3142
Nitin Daga393004b2011-01-10 21:49:31 +05303143static unsigned int channels_2_6_8[] = {
3144 2, 6, 8
3145};
3146
3147static unsigned int channels_2_8[] = {
3148 2, 8
3149};
3150
3151static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3152 .count = ARRAY_SIZE(channels_2_6_8),
3153 .list = channels_2_6_8,
3154 .mask = 0,
3155};
3156
3157static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3158 .count = ARRAY_SIZE(channels_2_8),
3159 .list = channels_2_8,
3160 .mask = 0,
3161};
3162
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003163static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3164 struct hda_codec *codec,
3165 struct snd_pcm_substream *substream)
3166{
3167 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05303168 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3169
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003170 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05303171 case 0x10de0002:
3172 case 0x10de0003:
3173 case 0x10de0005:
3174 case 0x10de0006:
3175 hw_constraints_channels = &hw_constraints_2_8_channels;
3176 break;
3177 case 0x10de0007:
3178 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3179 break;
3180 default:
3181 break;
3182 }
3183
3184 if (hw_constraints_channels != NULL) {
3185 snd_pcm_hw_constraint_list(substream->runtime, 0,
3186 SNDRV_PCM_HW_PARAM_CHANNELS,
3187 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01003188 } else {
3189 snd_pcm_hw_constraint_step(substream->runtime, 0,
3190 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05303191 }
3192
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003193 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3194}
3195
3196static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3197 struct hda_codec *codec,
3198 struct snd_pcm_substream *substream)
3199{
3200 struct hdmi_spec *spec = codec->spec;
3201 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3202}
3203
3204static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3205 struct hda_codec *codec,
3206 unsigned int stream_tag,
3207 unsigned int format,
3208 struct snd_pcm_substream *substream)
3209{
3210 struct hdmi_spec *spec = codec->spec;
3211 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3212 stream_tag, format, substream);
3213}
3214
Takashi Iwaid0b12522012-06-15 14:34:42 +02003215static const struct hda_pcm_stream simple_pcm_playback = {
3216 .substreams = 1,
3217 .channels_min = 2,
3218 .channels_max = 2,
3219 .ops = {
3220 .open = simple_playback_pcm_open,
3221 .close = simple_playback_pcm_close,
3222 .prepare = simple_playback_pcm_prepare
3223 },
3224};
3225
3226static const struct hda_codec_ops simple_hdmi_patch_ops = {
3227 .build_controls = simple_playback_build_controls,
3228 .build_pcms = simple_playback_build_pcms,
3229 .init = simple_playback_init,
3230 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02003231 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02003232};
3233
3234static int patch_simple_hdmi(struct hda_codec *codec,
3235 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3236{
3237 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003238 struct hdmi_spec_per_cvt *per_cvt;
3239 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003240
3241 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3242 if (!spec)
3243 return -ENOMEM;
3244
3245 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003246 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02003247
3248 spec->multiout.num_dacs = 0; /* no analog */
3249 spec->multiout.max_channels = 2;
3250 spec->multiout.dig_out_nid = cvt_nid;
3251 spec->num_cvts = 1;
3252 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003253 per_pin = snd_array_new(&spec->pins);
3254 per_cvt = snd_array_new(&spec->cvts);
3255 if (!per_pin || !per_cvt) {
3256 simple_playback_free(codec);
3257 return -ENOMEM;
3258 }
3259 per_cvt->cvt_nid = cvt_nid;
3260 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003261 spec->pcm_playback = simple_pcm_playback;
3262
3263 codec->patch_ops = simple_hdmi_patch_ops;
3264
3265 return 0;
3266}
3267
Aaron Plattner1f348522011-04-06 17:19:04 -07003268static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3269 int channels)
3270{
3271 unsigned int chanmask;
3272 int chan = channels ? (channels - 1) : 1;
3273
3274 switch (channels) {
3275 default:
3276 case 0:
3277 case 2:
3278 chanmask = 0x00;
3279 break;
3280 case 4:
3281 chanmask = 0x08;
3282 break;
3283 case 6:
3284 chanmask = 0x0b;
3285 break;
3286 case 8:
3287 chanmask = 0x13;
3288 break;
3289 }
3290
3291 /* Set the audio infoframe channel allocation and checksum fields. The
3292 * channel count is computed implicitly by the hardware. */
3293 snd_hda_codec_write(codec, 0x1, 0,
3294 Nv_VERB_SET_Channel_Allocation, chanmask);
3295
3296 snd_hda_codec_write(codec, 0x1, 0,
3297 Nv_VERB_SET_Info_Frame_Checksum,
3298 (0x71 - chan - chanmask));
3299}
3300
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003301static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3302 struct hda_codec *codec,
3303 struct snd_pcm_substream *substream)
3304{
3305 struct hdmi_spec *spec = codec->spec;
3306 int i;
3307
3308 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3309 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3310 for (i = 0; i < 4; i++) {
3311 /* set the stream id */
3312 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3313 AC_VERB_SET_CHANNEL_STREAMID, 0);
3314 /* set the stream format */
3315 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3316 AC_VERB_SET_STREAM_FORMAT, 0);
3317 }
3318
Aaron Plattner1f348522011-04-06 17:19:04 -07003319 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3320 * streams are disabled. */
3321 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3322
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003323 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3324}
3325
3326static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3327 struct hda_codec *codec,
3328 unsigned int stream_tag,
3329 unsigned int format,
3330 struct snd_pcm_substream *substream)
3331{
3332 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003333 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003334 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003335 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003336 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003337 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003338
3339 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003340 per_cvt = get_cvt(spec, 0);
3341 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003342
3343 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003344
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003345 dataDCC2 = 0x2;
3346
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003347 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003348 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003349 snd_hda_codec_write(codec,
3350 nvhdmi_master_con_nid_7x,
3351 0,
3352 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003353 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003354
3355 /* set the stream id */
3356 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3357 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3358
3359 /* set the stream format */
3360 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3361 AC_VERB_SET_STREAM_FORMAT, format);
3362
3363 /* turn on again (if needed) */
3364 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003365 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003366 snd_hda_codec_write(codec,
3367 nvhdmi_master_con_nid_7x,
3368 0,
3369 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003370 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003371 snd_hda_codec_write(codec,
3372 nvhdmi_master_con_nid_7x,
3373 0,
3374 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3375 }
3376
3377 for (i = 0; i < 4; i++) {
3378 if (chs == 2)
3379 channel_id = 0;
3380 else
3381 channel_id = i * 2;
3382
3383 /* turn off SPDIF once;
3384 *otherwise the IEC958 bits won't be updated
3385 */
3386 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003387 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003388 snd_hda_codec_write(codec,
3389 nvhdmi_con_nids_7x[i],
3390 0,
3391 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003392 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003393 /* set the stream id */
3394 snd_hda_codec_write(codec,
3395 nvhdmi_con_nids_7x[i],
3396 0,
3397 AC_VERB_SET_CHANNEL_STREAMID,
3398 (stream_tag << 4) | channel_id);
3399 /* set the stream format */
3400 snd_hda_codec_write(codec,
3401 nvhdmi_con_nids_7x[i],
3402 0,
3403 AC_VERB_SET_STREAM_FORMAT,
3404 format);
3405 /* turn on again (if needed) */
3406 /* enable and set the channel status audio/data flag */
3407 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003408 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003409 snd_hda_codec_write(codec,
3410 nvhdmi_con_nids_7x[i],
3411 0,
3412 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003413 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003414 snd_hda_codec_write(codec,
3415 nvhdmi_con_nids_7x[i],
3416 0,
3417 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3418 }
3419 }
3420
Aaron Plattner1f348522011-04-06 17:19:04 -07003421 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003422
3423 mutex_unlock(&codec->spdif_mutex);
3424 return 0;
3425}
3426
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003427static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003428 .substreams = 1,
3429 .channels_min = 2,
3430 .channels_max = 8,
3431 .nid = nvhdmi_master_con_nid_7x,
3432 .rates = SUPPORTED_RATES,
3433 .maxbps = SUPPORTED_MAXBPS,
3434 .formats = SUPPORTED_FORMATS,
3435 .ops = {
3436 .open = simple_playback_pcm_open,
3437 .close = nvhdmi_8ch_7x_pcm_close,
3438 .prepare = nvhdmi_8ch_7x_pcm_prepare
3439 },
3440};
3441
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003442static int patch_nvhdmi_2ch(struct hda_codec *codec)
3443{
3444 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003445 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3446 nvhdmi_master_pin_nid_7x);
3447 if (err < 0)
3448 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003449
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003450 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003451 /* override the PCM rates, etc, as the codec doesn't give full list */
3452 spec = codec->spec;
3453 spec->pcm_playback.rates = SUPPORTED_RATES;
3454 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3455 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003456 return 0;
3457}
3458
Takashi Iwai53775b02012-08-01 12:17:41 +02003459static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3460{
3461 struct hdmi_spec *spec = codec->spec;
3462 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003463 if (!err) {
3464 struct hda_pcm *info = get_pcm_rec(spec, 0);
3465 info->own_chmap = true;
3466 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003467 return err;
3468}
3469
3470static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3471{
3472 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003473 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003474 struct snd_pcm_chmap *chmap;
3475 int err;
3476
3477 err = simple_playback_build_controls(codec);
3478 if (err < 0)
3479 return err;
3480
3481 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003482 info = get_pcm_rec(spec, 0);
3483 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003484 SNDRV_PCM_STREAM_PLAYBACK,
3485 snd_pcm_alt_chmaps, 8, 0, &chmap);
3486 if (err < 0)
3487 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003488 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003489 case 0x10de0002:
3490 case 0x10de0003:
3491 case 0x10de0005:
3492 case 0x10de0006:
3493 chmap->channel_mask = (1U << 2) | (1U << 8);
3494 break;
3495 case 0x10de0007:
3496 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3497 }
3498 return 0;
3499}
3500
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003501static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3502{
3503 struct hdmi_spec *spec;
3504 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003505 if (err < 0)
3506 return err;
3507 spec = codec->spec;
3508 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003509 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003510 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003511 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3512 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003513
3514 /* Initialize the audio infoframe channel mask and checksum to something
3515 * valid */
3516 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3517
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003518 return 0;
3519}
3520
3521/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003522 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3523 * - 0x10de0015
3524 * - 0x10de0040
3525 */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303526static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
3527 struct cea_channel_speaker_allocation *cap, int channels)
Anssi Hannula611885b2013-11-03 17:15:00 +02003528{
3529 if (cap->ca_index == 0x00 && channels == 2)
3530 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3531
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303532 return chmap->ops.chmap_cea_alloc_validate_get_type(
3533 chmap, cap, channels);
Anssi Hannula611885b2013-11-03 17:15:00 +02003534}
3535
3536static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3537{
3538 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3539 return -EINVAL;
3540
3541 return 0;
3542}
3543
3544static int patch_nvhdmi(struct hda_codec *codec)
3545{
3546 struct hdmi_spec *spec;
3547 int err;
3548
3549 err = patch_generic_hdmi(codec);
3550 if (err)
3551 return err;
3552
3553 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003554 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003555
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303556 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula611885b2013-11-03 17:15:00 +02003557 nvhdmi_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303558 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
Anssi Hannula611885b2013-11-03 17:15:00 +02003559
3560 return 0;
3561}
3562
3563/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003564 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3565 * accessed using vendor-defined verbs. These registers can be used for
3566 * interoperability between the HDA and HDMI drivers.
3567 */
3568
3569/* Audio Function Group node */
3570#define NVIDIA_AFG_NID 0x01
3571
3572/*
3573 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3574 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3575 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3576 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3577 * additional bit (at position 30) to signal the validity of the format.
3578 *
3579 * | 31 | 30 | 29 16 | 15 0 |
3580 * +---------+-------+--------+--------+
3581 * | TRIGGER | VALID | UNUSED | FORMAT |
3582 * +-----------------------------------|
3583 *
3584 * Note that for the trigger bit to take effect it needs to change value
3585 * (i.e. it needs to be toggled).
3586 */
3587#define NVIDIA_GET_SCRATCH0 0xfa6
3588#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3589#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3590#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3591#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3592#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3593#define NVIDIA_SCRATCH_VALID (1 << 6)
3594
3595#define NVIDIA_GET_SCRATCH1 0xfab
3596#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3597#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3598#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3599#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3600
3601/*
3602 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3603 * the format is invalidated so that the HDMI codec can be disabled.
3604 */
3605static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3606{
3607 unsigned int value;
3608
3609 /* bits [31:30] contain the trigger and valid bits */
3610 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3611 NVIDIA_GET_SCRATCH0, 0);
3612 value = (value >> 24) & 0xff;
3613
3614 /* bits [15:0] are used to store the HDA format */
3615 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3616 NVIDIA_SET_SCRATCH0_BYTE0,
3617 (format >> 0) & 0xff);
3618 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3619 NVIDIA_SET_SCRATCH0_BYTE1,
3620 (format >> 8) & 0xff);
3621
3622 /* bits [16:24] are unused */
3623 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3624 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3625
3626 /*
3627 * Bit 30 signals that the data is valid and hence that HDMI audio can
3628 * be enabled.
3629 */
3630 if (format == 0)
3631 value &= ~NVIDIA_SCRATCH_VALID;
3632 else
3633 value |= NVIDIA_SCRATCH_VALID;
3634
3635 /*
3636 * Whenever the trigger bit is toggled, an interrupt is raised in the
3637 * HDMI codec. The HDMI driver will use that as trigger to update its
3638 * configuration.
3639 */
3640 value ^= NVIDIA_SCRATCH_TRIGGER;
3641
3642 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3643 NVIDIA_SET_SCRATCH0_BYTE3, value);
3644}
3645
3646static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3647 struct hda_codec *codec,
3648 unsigned int stream_tag,
3649 unsigned int format,
3650 struct snd_pcm_substream *substream)
3651{
3652 int err;
3653
3654 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3655 format, substream);
3656 if (err < 0)
3657 return err;
3658
3659 /* notify the HDMI codec of the format change */
3660 tegra_hdmi_set_format(codec, format);
3661
3662 return 0;
3663}
3664
3665static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3666 struct hda_codec *codec,
3667 struct snd_pcm_substream *substream)
3668{
3669 /* invalidate the format in the HDMI codec */
3670 tegra_hdmi_set_format(codec, 0);
3671
3672 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3673}
3674
3675static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3676{
3677 struct hdmi_spec *spec = codec->spec;
3678 unsigned int i;
3679
3680 for (i = 0; i < spec->num_pins; i++) {
3681 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3682
3683 if (pcm->pcm_type == type)
3684 return pcm;
3685 }
3686
3687 return NULL;
3688}
3689
3690static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3691{
3692 struct hda_pcm_stream *stream;
3693 struct hda_pcm *pcm;
3694 int err;
3695
3696 err = generic_hdmi_build_pcms(codec);
3697 if (err < 0)
3698 return err;
3699
3700 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3701 if (!pcm)
3702 return -ENODEV;
3703
3704 /*
3705 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3706 * codec about format changes.
3707 */
3708 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3709 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3710 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3711
3712 return 0;
3713}
3714
3715static int patch_tegra_hdmi(struct hda_codec *codec)
3716{
3717 int err;
3718
3719 err = patch_generic_hdmi(codec);
3720 if (err)
3721 return err;
3722
3723 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3724
3725 return 0;
3726}
3727
3728/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003729 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003730 */
3731
Anssi Hannula5a6135842013-10-24 21:10:35 +03003732#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003733 ((codec)->core.vendor_id == 0x1002aa01 && \
3734 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003735#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003736
Anssi Hannula5a6135842013-10-24 21:10:35 +03003737/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3738#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3739#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3740#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3741#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3742#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3743#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003744#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003745#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3746#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3747#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3748#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3749#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3750#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3751#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3752#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3753#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3754#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3755#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003756#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003757#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3758#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3759#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3760#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3761#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3762
Anssi Hannula84d69e72013-10-24 21:10:38 +03003763/* AMD specific HDA cvt verbs */
3764#define ATI_VERB_SET_RAMP_RATE 0x770
3765#define ATI_VERB_GET_RAMP_RATE 0xf70
3766
Anssi Hannula5a6135842013-10-24 21:10:35 +03003767#define ATI_OUT_ENABLE 0x1
3768
3769#define ATI_MULTICHANNEL_MODE_PAIRED 0
3770#define ATI_MULTICHANNEL_MODE_SINGLE 1
3771
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003772#define ATI_HBR_CAPABLE 0x01
3773#define ATI_HBR_ENABLE 0x10
3774
Anssi Hannula89250f82013-10-24 21:10:36 +03003775static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3776 unsigned char *buf, int *eld_size)
3777{
3778 /* call hda_eld.c ATI/AMD-specific function */
3779 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3780 is_amdhdmi_rev3_or_later(codec));
3781}
3782
Anssi Hannula5a6135842013-10-24 21:10:35 +03003783static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3784 int active_channels, int conn_type)
3785{
3786 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3787}
3788
3789static int atihdmi_paired_swap_fc_lfe(int pos)
3790{
3791 /*
3792 * ATI/AMD have automatic FC/LFE swap built-in
3793 * when in pairwise mapping mode.
3794 */
3795
3796 switch (pos) {
3797 /* see channel_allocations[].speakers[] */
3798 case 2: return 3;
3799 case 3: return 2;
3800 default: break;
3801 }
3802
3803 return pos;
3804}
3805
3806static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3807{
3808 struct cea_channel_speaker_allocation *cap;
3809 int i, j;
3810
3811 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3812
3813 cap = &channel_allocations[get_channel_allocation_order(ca)];
3814 for (i = 0; i < chs; ++i) {
3815 int mask = to_spk_mask(map[i]);
3816 bool ok = false;
3817 bool companion_ok = false;
3818
3819 if (!mask)
3820 continue;
3821
3822 for (j = 0 + i % 2; j < 8; j += 2) {
3823 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3824 if (cap->speakers[chan_idx] == mask) {
3825 /* channel is in a supported position */
3826 ok = true;
3827
3828 if (i % 2 == 0 && i + 1 < chs) {
3829 /* even channel, check the odd companion */
3830 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3831 int comp_mask_req = to_spk_mask(map[i+1]);
3832 int comp_mask_act = cap->speakers[comp_chan_idx];
3833
3834 if (comp_mask_req == comp_mask_act)
3835 companion_ok = true;
3836 else
3837 return -EINVAL;
3838 }
3839 break;
3840 }
3841 }
3842
3843 if (!ok)
3844 return -EINVAL;
3845
3846 if (companion_ok)
3847 i++; /* companion channel already checked */
3848 }
3849
3850 return 0;
3851}
3852
3853static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3854 int hdmi_slot, int stream_channel)
3855{
3856 int verb;
3857 int ati_channel_setup = 0;
3858
3859 if (hdmi_slot > 7)
3860 return -EINVAL;
3861
3862 if (!has_amd_full_remap_support(codec)) {
3863 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3864
3865 /* In case this is an odd slot but without stream channel, do not
3866 * disable the slot since the corresponding even slot could have a
3867 * channel. In case neither have a channel, the slot pair will be
3868 * disabled when this function is called for the even slot. */
3869 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3870 return 0;
3871
3872 hdmi_slot -= hdmi_slot % 2;
3873
3874 if (stream_channel != 0xf)
3875 stream_channel -= stream_channel % 2;
3876 }
3877
3878 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3879
3880 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3881
3882 if (stream_channel != 0xf)
3883 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3884
3885 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3886}
3887
3888static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3889 int asp_slot)
3890{
3891 bool was_odd = false;
3892 int ati_asp_slot = asp_slot;
3893 int verb;
3894 int ati_channel_setup;
3895
3896 if (asp_slot > 7)
3897 return -EINVAL;
3898
3899 if (!has_amd_full_remap_support(codec)) {
3900 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3901 if (ati_asp_slot % 2 != 0) {
3902 ati_asp_slot -= 1;
3903 was_odd = true;
3904 }
3905 }
3906
3907 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3908
3909 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3910
3911 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3912 return 0xf;
3913
3914 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3915}
3916
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05303917static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3918 struct hdac_chmap *chmap,
3919 struct cea_channel_speaker_allocation *cap,
3920 int channels)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003921{
3922 int c;
3923
3924 /*
3925 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3926 * we need to take that into account (a single channel may take 2
3927 * channel slots if we need to carry a silent channel next to it).
3928 * On Rev3+ AMD codecs this function is not used.
3929 */
3930 int chanpairs = 0;
3931
3932 /* We only produce even-numbered channel count TLVs */
3933 if ((channels % 2) != 0)
3934 return -1;
3935
3936 for (c = 0; c < 7; c += 2) {
3937 if (cap->speakers[c] || cap->speakers[c+1])
3938 chanpairs++;
3939 }
3940
3941 if (chanpairs * 2 != channels)
3942 return -1;
3943
3944 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3945}
3946
3947static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3948 unsigned int *chmap, int channels)
3949{
3950 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3951 int count = 0;
3952 int c;
3953
3954 for (c = 7; c >= 0; c--) {
3955 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3956 int spk = cap->speakers[chan];
3957 if (!spk) {
3958 /* add N/A channel if the companion channel is occupied */
3959 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3960 chmap[count++] = SNDRV_CHMAP_NA;
3961
3962 continue;
3963 }
3964
3965 chmap[count++] = spk_to_chmap(spk);
3966 }
3967
3968 WARN_ON(count != channels);
3969}
3970
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003971static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3972 bool hbr)
3973{
3974 int hbr_ctl, hbr_ctl_new;
3975
3976 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003977 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003978 if (hbr)
3979 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3980 else
3981 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3982
Takashi Iwai4e76a882014-02-25 12:21:03 +01003983 codec_dbg(codec,
3984 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003985 pin_nid,
3986 hbr_ctl == hbr_ctl_new ? "" : "new-",
3987 hbr_ctl_new);
3988
3989 if (hbr_ctl != hbr_ctl_new)
3990 snd_hda_codec_write(codec, pin_nid, 0,
3991 ATI_VERB_SET_HBR_CONTROL,
3992 hbr_ctl_new);
3993
3994 } else if (hbr)
3995 return -EINVAL;
3996
3997 return 0;
3998}
3999
Anssi Hannula84d69e72013-10-24 21:10:38 +03004000static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
4001 hda_nid_t pin_nid, u32 stream_tag, int format)
4002{
4003
4004 if (is_amdhdmi_rev3_or_later(codec)) {
4005 int ramp_rate = 180; /* default as per AMD spec */
4006 /* disable ramp-up/down for non-pcm as per AMD spec */
4007 if (format & AC_FMT_TYPE_NON_PCM)
4008 ramp_rate = 0;
4009
4010 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
4011 }
4012
4013 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
4014}
4015
4016
Anssi Hannula5a6135842013-10-24 21:10:35 +03004017static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004018{
4019 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004020 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004021
Anssi Hannula5a6135842013-10-24 21:10:35 +03004022 err = generic_hdmi_init(codec);
4023
4024 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004025 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004026
4027 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
4028 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
4029
4030 /* make sure downmix information in infoframe is zero */
4031 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
4032
4033 /* enable channel-wise remap mode if supported */
4034 if (has_amd_full_remap_support(codec))
4035 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
4036 ATI_VERB_SET_MULTICHANNEL_MODE,
4037 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004038 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03004039
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004040 return 0;
4041}
4042
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004043static int patch_atihdmi(struct hda_codec *codec)
4044{
4045 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004046 struct hdmi_spec_per_cvt *per_cvt;
4047 int err, cvt_idx;
4048
4049 err = patch_generic_hdmi(codec);
4050
4051 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02004052 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004053
4054 codec->patch_ops.init = atihdmi_init;
4055
Takashi Iwaid0b12522012-06-15 14:34:42 +02004056 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004057
Anssi Hannula89250f82013-10-24 21:10:36 +03004058 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004059 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4060 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4061 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03004062 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03004063 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004064
4065 if (!has_amd_full_remap_support(codec)) {
4066 /* override to ATI/AMD-specific versions with pairwise mapping */
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304067 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
Anssi Hannula5a6135842013-10-24 21:10:35 +03004068 atihdmi_paired_chmap_cea_alloc_validate_get_type;
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304069 spec->chmap.ops.cea_alloc_to_tlv_chmap =
4070 atihdmi_paired_cea_alloc_to_tlv_chmap;
4071 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004072 }
4073
4074 /* ATI/AMD converters do not advertise all of their capabilities */
4075 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4076 per_cvt = get_cvt(spec, cvt_idx);
4077 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4078 per_cvt->rates |= SUPPORTED_RATES;
4079 per_cvt->formats |= SUPPORTED_FORMATS;
4080 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4081 }
4082
Subhransu S. Prusty67b90cb2016-03-04 19:59:46 +05304083 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
Anssi Hannula5a6135842013-10-24 21:10:35 +03004084
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004085 return 0;
4086}
4087
Annie Liu3de5ff82012-06-08 19:18:42 +08004088/* VIA HDMI Implementation */
4089#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4090#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4091
Annie Liu3de5ff82012-06-08 19:18:42 +08004092static int patch_via_hdmi(struct hda_codec *codec)
4093{
Takashi Iwai250e41a2012-06-15 14:40:21 +02004094 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08004095}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004096
4097/*
4098 * patch entries
4099 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004100static const struct hda_device_id snd_hda_id_hdmi[] = {
4101HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4102HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4103HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4104HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4105HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4106HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4107HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4108HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4109HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4110HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4111HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4112HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4113HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4114HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4115HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4116HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4117HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4118HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4119HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4120HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4121HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4122HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4123HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01004124/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004125HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4126HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4127HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4128HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4129HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4130HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4131HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4132HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4133HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4134HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4135HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4136HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4137HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4138HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4139HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4140HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4141HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4142HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4143HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4144HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4145HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08004146HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004147HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4148HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4149HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4150HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4151HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4152HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
4153HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4154HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4155HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4156HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
4157HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4158HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4159HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4160HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4161HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4162HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
Libin Yang91815d82016-01-14 14:09:00 +08004163HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004164HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4165HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4166HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4167HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004168/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004169HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004170{} /* terminator */
4171};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004172MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004173
4174MODULE_LICENSE("GPL");
4175MODULE_DESCRIPTION("HDMI HD-audio codec");
4176MODULE_ALIAS("snd-hda-codec-intelhdmi");
4177MODULE_ALIAS("snd-hda-codec-nvhdmi");
4178MODULE_ALIAS("snd-hda-codec-atihdmi");
4179
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004180static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004181 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004182};
4183
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004184module_hda_codec_driver(hdmi_driver);