blob: 21a2b28a2ceb6c918778d1c86f77ce3da4329688 [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>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020042#include "hda_codec.h"
43#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020044#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020045
Takashi Iwai0ebaa242011-01-11 18:11:04 +010046static bool static_hdmi_pcm;
47module_param(static_hdmi_pcm, bool, 0644);
48MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
49
Takashi Iwai7639a062015-03-03 10:07:24 +010050#define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
51#define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
52#define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
Lu, Hane2656412015-11-11 16:54:27 +080053#define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
Libin Yang91815d82016-01-14 14:09:00 +080054#define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
Libin Yang432ac1a2014-12-16 13:17:34 +080055#define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
Libin Yang91815d82016-01-14 14:09:00 +080056 || is_skylake(codec) || is_broxton(codec) \
57 || is_kabylake(codec))
Mengdong Lin75dcbe42014-01-08 15:55:32 -050058
Takashi Iwai7639a062015-03-03 10:07:24 +010059#define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
60#define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
Libin Yangca2e7222014-08-19 16:20:12 +080061#define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
Mengdong Linfb87fa32013-09-04 16:36:57 -040062
Stephen Warren384a48d2011-06-01 11:14:21 -060063struct hdmi_spec_per_cvt {
64 hda_nid_t cvt_nid;
65 int assigned;
66 unsigned int channels_min;
67 unsigned int channels_max;
68 u32 rates;
69 u64 formats;
70 unsigned int maxbps;
71};
72
Takashi Iwai4eea3092013-02-07 18:18:19 +010073/* max. connections to a widget */
74#define HDA_MAX_CONNECTIONS 32
75
Stephen Warren384a48d2011-06-01 11:14:21 -060076struct hdmi_spec_per_pin {
77 hda_nid_t pin_nid;
Libin Yanga76056f2015-12-16 16:48:15 +080078 /* pin idx, different device entries on the same pin use the same idx */
79 int pin_nid_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -060080 int num_mux_nids;
81 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Mengdong Lin2df67422014-03-20 13:01:06 +080082 int mux_idx;
Anssi Hannula1df5a062013-10-05 02:25:40 +030083 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080084
85 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060086 struct hdmi_eld sink_eld;
Takashi Iwaia4e9a382013-10-17 18:21:12 +020087 struct mutex lock;
Wu Fengguang744626d2011-11-16 16:29:47 +080088 struct delayed_work work;
Libin Yang2bea2412016-01-12 11:13:26 +080089 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Libin Yanga76056f2015-12-16 16:48:15 +080090 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
Wu Fengguangc6e84532011-11-18 16:59:32 -060091 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020092 bool setup; /* the stream has been set up by prepare callback */
93 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020094 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020095 bool chmap_set; /* channel-map override by ALSA API? */
96 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080097#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +020098 struct snd_info_entry *proc_entry;
99#endif
Stephen Warren384a48d2011-06-01 11:14:21 -0600100};
101
Anssi Hannula307229d2013-10-24 21:10:34 +0300102struct cea_channel_speaker_allocation;
103
104/* operations used by generic code that can be overridden by patches */
105struct hdmi_ops {
106 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
107 unsigned char *buf, int *eld_size);
108
109 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
110 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
111 int asp_slot);
112 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
113 int asp_slot, int channel);
114
115 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
116 int ca, int active_channels, int conn_type);
117
118 /* enable/disable HBR (HD passthrough) */
119 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
120
121 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
122 hda_nid_t pin_nid, u32 stream_tag, int format);
123
124 /* Helpers for producing the channel map TLVs. These can be overridden
125 * for devices that have non-standard mapping requirements. */
126 int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
127 int channels);
128 void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
129 unsigned int *chmap, int channels);
130
131 /* check that the user-given chmap is supported */
132 int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
133};
134
Libin Yang2bea2412016-01-12 11:13:26 +0800135struct hdmi_pcm {
136 struct hda_pcm *pcm;
137 struct snd_jack *jack;
Libin Yangfb087ea2016-02-23 16:33:37 +0800138 struct snd_kcontrol *eld_ctl;
Libin Yang2bea2412016-01-12 11:13:26 +0800139};
140
Wu Fengguang079d88c2010-03-08 10:44:23 +0800141struct hdmi_spec {
142 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100143 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
144 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600145
Wu Fengguang079d88c2010-03-08 10:44:23 +0800146 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100147 struct snd_array pins; /* struct hdmi_spec_per_pin */
Libin Yang2bea2412016-01-12 11:13:26 +0800148 struct hdmi_pcm pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800149 struct mutex pcm_lock;
Libin Yanga76056f2015-12-16 16:48:15 +0800150 /* pcm_bitmap means which pcms have been assigned to pins*/
151 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800152 int pcm_used; /* counter of pcm_rec[] */
Libin Yangac983792015-12-16 16:48:16 +0800153 /* bitmap shows whether the pcm is opened in user space
154 * bit 0 means the first playback PCM (PCM3);
155 * bit 1 means the second playback PCM, and so on.
156 */
157 unsigned long pcm_in_use;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200158 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800159
David Henningsson4bd038f2013-02-19 16:11:25 +0100160 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300161 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700162
163 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800164 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800165 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300166 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800167 */
168 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200169 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200170
171 /* i915/powerwell (Haswell+/Valleyview+) specific */
172 struct i915_audio_component_audio_ops i915_audio_ops;
Takashi Iwai55913112015-12-10 13:03:29 +0100173 bool i915_bound; /* was i915 bound in this driver? */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800174};
175
Takashi Iwaif4e30402015-12-10 13:01:28 +0100176#ifdef CONFIG_SND_HDA_I915
Takashi Iwai66032492015-12-01 16:49:35 +0100177#define codec_has_acomp(codec) \
178 ((codec)->bus->core.audio_component != NULL)
Takashi Iwaif4e30402015-12-10 13:01:28 +0100179#else
180#define codec_has_acomp(codec) false
181#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800182
183struct hdmi_audio_infoframe {
184 u8 type; /* 0x84 */
185 u8 ver; /* 0x01 */
186 u8 len; /* 0x0a */
187
Wu Fengguang53d7d692010-09-21 14:25:49 +0800188 u8 checksum;
189
Wu Fengguang079d88c2010-03-08 10:44:23 +0800190 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
191 u8 SS01_SF24;
192 u8 CXT04;
193 u8 CA;
194 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800195};
196
197struct dp_audio_infoframe {
198 u8 type; /* 0x84 */
199 u8 len; /* 0x1b */
200 u8 ver; /* 0x11 << 2 */
201
202 u8 CC02_CT47; /* match with HDMI infoframe from this on */
203 u8 SS01_SF24;
204 u8 CXT04;
205 u8 CA;
206 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800207};
208
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100209union audio_infoframe {
210 struct hdmi_audio_infoframe hdmi;
211 struct dp_audio_infoframe dp;
212 u8 bytes[0];
213};
214
Wu Fengguang079d88c2010-03-08 10:44:23 +0800215/*
216 * CEA speaker placement:
217 *
218 * FLH FCH FRH
219 * FLW FL FLC FC FRC FR FRW
220 *
221 * LFE
222 * TC
223 *
224 * RL RLC RC RRC RR
225 *
226 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
227 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
228 */
229enum cea_speaker_placement {
230 FL = (1 << 0), /* Front Left */
231 FC = (1 << 1), /* Front Center */
232 FR = (1 << 2), /* Front Right */
233 FLC = (1 << 3), /* Front Left Center */
234 FRC = (1 << 4), /* Front Right Center */
235 RL = (1 << 5), /* Rear Left */
236 RC = (1 << 6), /* Rear Center */
237 RR = (1 << 7), /* Rear Right */
238 RLC = (1 << 8), /* Rear Left Center */
239 RRC = (1 << 9), /* Rear Right Center */
240 LFE = (1 << 10), /* Low Frequency Effect */
241 FLW = (1 << 11), /* Front Left Wide */
242 FRW = (1 << 12), /* Front Right Wide */
243 FLH = (1 << 13), /* Front Left High */
244 FCH = (1 << 14), /* Front Center High */
245 FRH = (1 << 15), /* Front Right High */
246 TC = (1 << 16), /* Top Center */
247};
248
249/*
250 * ELD SA bits in the CEA Speaker Allocation data block
251 */
252static int eld_speaker_allocation_bits[] = {
253 [0] = FL | FR,
254 [1] = LFE,
255 [2] = FC,
256 [3] = RL | RR,
257 [4] = RC,
258 [5] = FLC | FRC,
259 [6] = RLC | RRC,
260 /* the following are not defined in ELD yet */
261 [7] = FLW | FRW,
262 [8] = FLH | FRH,
263 [9] = TC,
264 [10] = FCH,
265};
266
267struct cea_channel_speaker_allocation {
268 int ca_index;
269 int speakers[8];
270
271 /* derived values, just for convenience */
272 int channels;
273 int spk_mask;
274};
275
276/*
277 * ALSA sequence is:
278 *
279 * surround40 surround41 surround50 surround51 surround71
280 * ch0 front left = = = =
281 * ch1 front right = = = =
282 * ch2 rear left = = = =
283 * ch3 rear right = = = =
284 * ch4 LFE center center center
285 * ch5 LFE LFE
286 * ch6 side left
287 * ch7 side right
288 *
289 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
290 */
291static int hdmi_channel_mapping[0x32][8] = {
292 /* stereo */
293 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
294 /* 2.1 */
295 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
296 /* Dolby Surround */
297 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
298 /* surround40 */
299 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
300 /* 4ch */
301 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
302 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800303 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800304 /* surround50 */
305 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
306 /* surround51 */
307 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
308 /* 7.1 */
309 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
310};
311
312/*
313 * This is an ordered list!
314 *
315 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800316 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800317 */
318static struct cea_channel_speaker_allocation channel_allocations[] = {
319/* channel: 7 6 5 4 3 2 1 0 */
320{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
321 /* 2.1 */
322{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
323 /* Dolby Surround */
324{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
325 /* surround40 */
326{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
327 /* surround41 */
328{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
329 /* surround50 */
330{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
331 /* surround51 */
332{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
333 /* 6.1 */
334{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
335 /* surround71 */
336{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
337
338{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
339{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
340{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
341{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
342{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
343{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
344{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
345{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
346{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
347{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
348{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
349{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
350{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
351{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
352{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
353{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
354{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
355{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
356{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
357{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
358{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
359{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
360{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
361{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
362{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
363{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
364{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
365{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
366{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
367{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
368{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
369{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
370{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
371{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
372{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
373{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
374{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
375{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
376{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
377{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
378{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
379};
380
381
382/*
383 * HDMI routines
384 */
385
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100386#define get_pin(spec, idx) \
387 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
388#define get_cvt(spec, idx) \
389 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Libin Yang2bea2412016-01-12 11:13:26 +0800390/* obtain hdmi_pcm object assigned to idx */
391#define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
392/* obtain hda_pcm object assigned to idx */
393#define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100394
Takashi Iwai4e76a882014-02-25 12:21:03 +0100395static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800396{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100397 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600398 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800399
Stephen Warren384a48d2011-06-01 11:14:21 -0600400 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100401 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600402 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800403
Takashi Iwai4e76a882014-02-25 12:21:03 +0100404 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600405 return -EINVAL;
406}
407
Libin Yang2bf3c852015-12-16 13:42:43 +0800408static int hinfo_to_pcm_index(struct hda_codec *codec,
409 struct hda_pcm_stream *hinfo)
410{
411 struct hdmi_spec *spec = codec->spec;
412 int pcm_idx;
413
414 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
415 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
416 return pcm_idx;
417
418 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
419 return -EINVAL;
420}
421
Takashi Iwai4e76a882014-02-25 12:21:03 +0100422static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600423 struct hda_pcm_stream *hinfo)
424{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100425 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800426 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600427 int pin_idx;
428
Libin Yang6590faa2015-12-16 13:42:41 +0800429 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
430 per_pin = get_pin(spec, pin_idx);
Libin Yang2bea2412016-01-12 11:13:26 +0800431 if (per_pin->pcm &&
432 per_pin->pcm->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600433 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800434 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600435
Libin Yang6590faa2015-12-16 13:42:41 +0800436 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600437 return -EINVAL;
438}
439
Libin Yang022f3442016-02-03 10:48:34 +0800440static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
441 int pcm_idx)
442{
443 int i;
444 struct hdmi_spec_per_pin *per_pin;
445
446 for (i = 0; i < spec->num_pins; i++) {
447 per_pin = get_pin(spec, i);
448 if (per_pin->pcm_idx == pcm_idx)
449 return per_pin;
450 }
451 return NULL;
452}
453
Takashi Iwai4e76a882014-02-25 12:21:03 +0100454static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600455{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100456 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600457 int cvt_idx;
458
459 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100460 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600461 return cvt_idx;
462
Takashi Iwai4e76a882014-02-25 12:21:03 +0100463 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800464 return -EINVAL;
465}
466
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500467static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
468 struct snd_ctl_elem_info *uinfo)
469{
470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100471 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200472 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100473 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800474 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500475
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500476 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
477
Libin Yangfb087ea2016-02-23 16:33:37 +0800478 pcm_idx = kcontrol->private_value;
479 mutex_lock(&spec->pcm_lock);
480 per_pin = pcm_idx_to_pin(spec, pcm_idx);
481 if (!per_pin) {
482 /* no pin is bound to the pcm */
483 uinfo->count = 0;
484 mutex_unlock(&spec->pcm_lock);
485 return 0;
486 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200487 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100488 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Libin Yangfb087ea2016-02-23 16:33:37 +0800489 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500490
491 return 0;
492}
493
494static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
495 struct snd_ctl_elem_value *ucontrol)
496{
497 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100498 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200499 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100500 struct hdmi_eld *eld;
Libin Yangfb087ea2016-02-23 16:33:37 +0800501 int pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500502
Libin Yangfb087ea2016-02-23 16:33:37 +0800503 pcm_idx = kcontrol->private_value;
504 mutex_lock(&spec->pcm_lock);
505 per_pin = pcm_idx_to_pin(spec, pcm_idx);
506 if (!per_pin) {
507 /* no pin is bound to the pcm */
508 memset(ucontrol->value.bytes.data, 0,
509 ARRAY_SIZE(ucontrol->value.bytes.data));
510 mutex_unlock(&spec->pcm_lock);
511 return 0;
512 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200513 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500514
David Henningsson360a8242016-02-05 09:05:41 +0100515 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
516 eld->eld_size > ELD_MAX_SIZE) {
Libin Yangfb087ea2016-02-23 16:33:37 +0800517 mutex_unlock(&spec->pcm_lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100518 snd_BUG();
519 return -EINVAL;
520 }
521
522 memset(ucontrol->value.bytes.data, 0,
523 ARRAY_SIZE(ucontrol->value.bytes.data));
524 if (eld->eld_valid)
525 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
526 eld->eld_size);
Libin Yangfb087ea2016-02-23 16:33:37 +0800527 mutex_unlock(&spec->pcm_lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500528
529 return 0;
530}
531
532static struct snd_kcontrol_new eld_bytes_ctl = {
533 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
534 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
535 .name = "ELD",
536 .info = hdmi_eld_ctl_info,
537 .get = hdmi_eld_ctl_get,
538};
539
Libin Yangfb087ea2016-02-23 16:33:37 +0800540static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500541 int device)
542{
543 struct snd_kcontrol *kctl;
544 struct hdmi_spec *spec = codec->spec;
545 int err;
546
547 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
548 if (!kctl)
549 return -ENOMEM;
Libin Yangfb087ea2016-02-23 16:33:37 +0800550 kctl->private_value = pcm_idx;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500551 kctl->id.device = device;
552
Libin Yangfb087ea2016-02-23 16:33:37 +0800553 /* no pin nid is associated with the kctl now
554 * tbd: associate pin nid to eld ctl later
555 */
556 err = snd_hda_ctl_add(codec, 0, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500557 if (err < 0)
558 return err;
559
Libin Yangfb087ea2016-02-23 16:33:37 +0800560 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500561 return 0;
562}
563
Wu Fengguang079d88c2010-03-08 10:44:23 +0800564#ifdef BE_PARANOID
565static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
566 int *packet_index, int *byte_index)
567{
568 int val;
569
570 val = snd_hda_codec_read(codec, pin_nid, 0,
571 AC_VERB_GET_HDMI_DIP_INDEX, 0);
572
573 *packet_index = val >> 5;
574 *byte_index = val & 0x1f;
575}
576#endif
577
578static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
579 int packet_index, int byte_index)
580{
581 int val;
582
583 val = (packet_index << 5) | (byte_index & 0x1f);
584
585 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
586}
587
588static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
589 unsigned char val)
590{
591 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
592}
593
Stephen Warren384a48d2011-06-01 11:14:21 -0600594static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800595{
Stephen Warren75fae112014-01-30 11:52:16 -0700596 struct hdmi_spec *spec = codec->spec;
597 int pin_out;
598
Wu Fengguang079d88c2010-03-08 10:44:23 +0800599 /* Unmute */
600 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
601 snd_hda_codec_write(codec, pin_nid, 0,
602 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700603
604 if (spec->dyn_pin_out)
605 /* Disable pin out until stream is active */
606 pin_out = 0;
607 else
608 /* Enable pin out: some machines with GM965 gets broken output
609 * when the pin is disabled or changed while using with HDMI
610 */
611 pin_out = PIN_OUT;
612
Wu Fengguang079d88c2010-03-08 10:44:23 +0800613 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700614 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800615}
616
Stephen Warren384a48d2011-06-01 11:14:21 -0600617static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800618{
Stephen Warren384a48d2011-06-01 11:14:21 -0600619 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800620 AC_VERB_GET_CVT_CHAN_COUNT, 0);
621}
622
623static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600624 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800625{
Stephen Warren384a48d2011-06-01 11:14:21 -0600626 if (chs != hdmi_get_channel_count(codec, cvt_nid))
627 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800628 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
629}
630
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200631/*
632 * ELD proc files
633 */
634
Jie Yangcd6a6502015-05-27 19:45:45 +0800635#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200636static void print_eld_info(struct snd_info_entry *entry,
637 struct snd_info_buffer *buffer)
638{
639 struct hdmi_spec_per_pin *per_pin = entry->private_data;
640
641 mutex_lock(&per_pin->lock);
642 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
643 mutex_unlock(&per_pin->lock);
644}
645
646static void write_eld_info(struct snd_info_entry *entry,
647 struct snd_info_buffer *buffer)
648{
649 struct hdmi_spec_per_pin *per_pin = entry->private_data;
650
651 mutex_lock(&per_pin->lock);
652 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
653 mutex_unlock(&per_pin->lock);
654}
655
656static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
657{
658 char name[32];
659 struct hda_codec *codec = per_pin->codec;
660 struct snd_info_entry *entry;
661 int err;
662
663 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100664 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200665 if (err < 0)
666 return err;
667
668 snd_info_set_text_ops(entry, per_pin, print_eld_info);
669 entry->c.text.write = write_eld_info;
670 entry->mode |= S_IWUSR;
671 per_pin->proc_entry = entry;
672
673 return 0;
674}
675
676static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
677{
Markus Elfring1947a112015-06-28 11:15:28 +0200678 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200679 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200680 per_pin->proc_entry = NULL;
681 }
682}
683#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200684static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
685 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200686{
687 return 0;
688}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200689static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200690{
691}
692#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800693
694/*
695 * Channel mapping routines
696 */
697
698/*
699 * Compute derived values in channel_allocations[].
700 */
701static void init_channel_allocations(void)
702{
703 int i, j;
704 struct cea_channel_speaker_allocation *p;
705
706 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
707 p = channel_allocations + i;
708 p->channels = 0;
709 p->spk_mask = 0;
710 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
711 if (p->speakers[j]) {
712 p->channels++;
713 p->spk_mask |= p->speakers[j];
714 }
715 }
716}
717
Wang Xingchao72357c72012-09-06 10:02:36 +0800718static int get_channel_allocation_order(int ca)
719{
720 int i;
721
722 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
723 if (channel_allocations[i].ca_index == ca)
724 break;
725 }
726 return i;
727}
728
Wu Fengguang079d88c2010-03-08 10:44:23 +0800729/*
730 * The transformation takes two steps:
731 *
732 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
733 * spk_mask => (channel_allocations[]) => ai->CA
734 *
735 * TODO: it could select the wrong CA from multiple candidates.
736*/
Takashi Iwai79514d42014-06-06 18:04:34 +0200737static int hdmi_channel_allocation(struct hda_codec *codec,
738 struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800739{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800740 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800741 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800742 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800743 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
744
745 /*
746 * CA defaults to 0 for basic stereo audio
747 */
748 if (channels <= 2)
749 return 0;
750
Wu Fengguang079d88c2010-03-08 10:44:23 +0800751 /*
752 * expand ELD's speaker allocation mask
753 *
754 * ELD tells the speaker mask in a compact(paired) form,
755 * expand ELD's notions to match the ones used by Audio InfoFrame.
756 */
757 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100758 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800759 spk_mask |= eld_speaker_allocation_bits[i];
760 }
761
762 /* search for the first working match in the CA table */
763 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
764 if (channels == channel_allocations[i].channels &&
765 (spk_mask & channel_allocations[i].spk_mask) ==
766 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800767 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800768 break;
769 }
770 }
771
Anssi Hannula18e39182013-09-01 14:36:47 +0300772 if (!ca) {
773 /* if there was no match, select the regular ALSA channel
774 * allocation with the matching number of channels */
775 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
776 if (channels == channel_allocations[i].channels) {
777 ca = channel_allocations[i].ca_index;
778 break;
779 }
780 }
781 }
782
David Henningsson1613d6b2013-02-19 16:11:24 +0100783 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Takashi Iwai79514d42014-06-06 18:04:34 +0200784 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800785 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800786
Wu Fengguang53d7d692010-09-21 14:25:49 +0800787 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800788}
789
790static void hdmi_debug_channel_mapping(struct hda_codec *codec,
791 hda_nid_t pin_nid)
792{
793#ifdef CONFIG_SND_DEBUG_VERBOSE
Anssi Hannula307229d2013-10-24 21:10:34 +0300794 struct hdmi_spec *spec = codec->spec;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800795 int i;
Anssi Hannula307229d2013-10-24 21:10:34 +0300796 int channel;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800797
798 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300799 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100800 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300801 channel, i);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800802 }
803#endif
804}
805
Takashi Iwaid45e6882012-07-31 11:36:00 +0200806static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800807 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800808 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800809 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800810{
Anssi Hannula307229d2013-10-24 21:10:34 +0300811 struct hdmi_spec *spec = codec->spec;
Anssi Hannula90f28002013-10-05 02:25:39 +0300812 struct cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800813 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800814 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800815 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800816 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800817
Wang Xingchao72357c72012-09-06 10:02:36 +0800818 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300819 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800820
Wu Fengguang079d88c2010-03-08 10:44:23 +0800821 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300822 int hdmi_slot = 0;
823 /* fill actual channel mappings in ALSA channel (i) order */
824 for (i = 0; i < ch_alloc->channels; i++) {
825 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
826 hdmi_slot++; /* skip zero slots */
827
828 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
829 }
830 /* fill the rest of the slots with ALSA channel 0xf */
831 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
832 if (!ch_alloc->speakers[7 - hdmi_slot])
833 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800834 }
835
Wang Xingchao433968d2012-09-06 10:02:37 +0800836 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300837 for (i = 0; i < ch_alloc->channels; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300838 non_pcm_mapping[i] = (i << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800839 for (; i < 8; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300840 non_pcm_mapping[i] = (0xf << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800841 }
842
Wu Fengguang079d88c2010-03-08 10:44:23 +0800843 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300844 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
845 int hdmi_slot = slotsetup & 0x0f;
846 int channel = (slotsetup & 0xf0) >> 4;
847 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800848 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100849 codec_dbg(codec, "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800850 break;
851 }
852 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800853}
854
Takashi Iwaid45e6882012-07-31 11:36:00 +0200855struct channel_map_table {
856 unsigned char map; /* ALSA API channel map position */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200857 int spk_mask; /* speaker position bit mask */
858};
859
860static struct channel_map_table map_tables[] = {
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300861 { SNDRV_CHMAP_FL, FL },
862 { SNDRV_CHMAP_FR, FR },
863 { SNDRV_CHMAP_RL, RL },
864 { SNDRV_CHMAP_RR, RR },
865 { SNDRV_CHMAP_LFE, LFE },
866 { SNDRV_CHMAP_FC, FC },
867 { SNDRV_CHMAP_RLC, RLC },
868 { SNDRV_CHMAP_RRC, RRC },
869 { SNDRV_CHMAP_RC, RC },
870 { SNDRV_CHMAP_FLC, FLC },
871 { SNDRV_CHMAP_FRC, FRC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200872 { SNDRV_CHMAP_TFL, FLH },
873 { SNDRV_CHMAP_TFR, FRH },
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300874 { SNDRV_CHMAP_FLW, FLW },
875 { SNDRV_CHMAP_FRW, FRW },
876 { SNDRV_CHMAP_TC, TC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200877 { SNDRV_CHMAP_TFC, FCH },
Takashi Iwaid45e6882012-07-31 11:36:00 +0200878 {} /* terminator */
879};
880
881/* from ALSA API channel position to speaker bit mask */
882static int to_spk_mask(unsigned char c)
883{
884 struct channel_map_table *t = map_tables;
885 for (; t->map; t++) {
886 if (t->map == c)
887 return t->spk_mask;
888 }
889 return 0;
890}
891
892/* from ALSA API channel position to CEA slot */
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300893static int to_cea_slot(int ordered_ca, unsigned char pos)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200894{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300895 int mask = to_spk_mask(pos);
896 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200897
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300898 if (mask) {
899 for (i = 0; i < 8; i++) {
900 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
901 return i;
902 }
Takashi Iwaid45e6882012-07-31 11:36:00 +0200903 }
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300904
905 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200906}
907
908/* from speaker bit mask to ALSA API channel position */
909static int spk_to_chmap(int spk)
910{
911 struct channel_map_table *t = map_tables;
912 for (; t->map; t++) {
913 if (t->spk_mask == spk)
914 return t->map;
915 }
916 return 0;
917}
918
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300919/* from CEA slot to ALSA API channel position */
920static int from_cea_slot(int ordered_ca, unsigned char slot)
921{
922 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
923
924 return spk_to_chmap(mask);
925}
926
Takashi Iwaid45e6882012-07-31 11:36:00 +0200927/* get the CA index corresponding to the given ALSA API channel map */
928static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
929{
930 int i, spks = 0, spk_mask = 0;
931
932 for (i = 0; i < chs; i++) {
933 int mask = to_spk_mask(map[i]);
934 if (mask) {
935 spk_mask |= mask;
936 spks++;
937 }
938 }
939
940 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
941 if ((chs == channel_allocations[i].channels ||
942 spks == channel_allocations[i].channels) &&
943 (spk_mask & channel_allocations[i].spk_mask) ==
944 channel_allocations[i].spk_mask)
945 return channel_allocations[i].ca_index;
946 }
947 return -1;
948}
949
950/* set up the channel slots for the given ALSA API channel map */
951static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
952 hda_nid_t pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300953 int chs, unsigned char *map,
954 int ca)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200955{
Anssi Hannula307229d2013-10-24 21:10:34 +0300956 struct hdmi_spec *spec = codec->spec;
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300957 int ordered_ca = get_channel_allocation_order(ca);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300958 int alsa_pos, hdmi_slot;
959 int assignments[8] = {[0 ... 7] = 0xf};
960
961 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
962
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300963 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300964
965 if (hdmi_slot < 0)
966 continue; /* unassigned channel */
967
968 assignments[hdmi_slot] = alsa_pos;
969 }
970
971 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300972 int err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300973
Anssi Hannula307229d2013-10-24 21:10:34 +0300974 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
975 assignments[hdmi_slot]);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200976 if (err)
977 return -EINVAL;
978 }
979 return 0;
980}
981
982/* store ALSA API channel map from the current default map */
983static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
984{
985 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300986 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200987 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300988 if (i < channel_allocations[ordered_ca].channels)
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300989 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200990 else
991 map[i] = 0;
992 }
993}
994
995static void hdmi_setup_channel_mapping(struct hda_codec *codec,
996 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200997 int channels, unsigned char *map,
998 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200999{
Anssi Hannula20608732013-02-03 17:55:45 +02001000 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001001 hdmi_manual_setup_channel_mapping(codec, pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +03001002 channels, map, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001003 } else {
1004 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
1005 hdmi_setup_fake_chmap(map, ca);
1006 }
Anssi Hannula980b2492013-10-05 02:25:44 +03001007
1008 hdmi_debug_channel_mapping(codec, pin_nid);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001009}
Wu Fengguang079d88c2010-03-08 10:44:23 +08001010
Anssi Hannula307229d2013-10-24 21:10:34 +03001011static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
1012 int asp_slot, int channel)
1013{
1014 return snd_hda_codec_write(codec, pin_nid, 0,
1015 AC_VERB_SET_HDMI_CHAN_SLOT,
1016 (channel << 4) | asp_slot);
1017}
1018
1019static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
1020 int asp_slot)
1021{
1022 return (snd_hda_codec_read(codec, pin_nid, 0,
1023 AC_VERB_GET_HDMI_CHAN_SLOT,
1024 asp_slot) & 0xf0) >> 4;
1025}
1026
Wu Fengguang079d88c2010-03-08 10:44:23 +08001027/*
1028 * Audio InfoFrame routines
1029 */
1030
1031/*
1032 * Enable Audio InfoFrame Transmission
1033 */
1034static void hdmi_start_infoframe_trans(struct hda_codec *codec,
1035 hda_nid_t pin_nid)
1036{
1037 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1038 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1039 AC_DIPXMIT_BEST);
1040}
1041
1042/*
1043 * Disable Audio InfoFrame Transmission
1044 */
1045static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1046 hda_nid_t pin_nid)
1047{
1048 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1049 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1050 AC_DIPXMIT_DISABLE);
1051}
1052
1053static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1054{
1055#ifdef CONFIG_SND_DEBUG_VERBOSE
1056 int i;
1057 int size;
1058
1059 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001060 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001061
1062 for (i = 0; i < 8; i++) {
1063 size = snd_hda_codec_read(codec, pin_nid, 0,
1064 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001065 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001066 }
1067#endif
1068}
1069
1070static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1071{
1072#ifdef BE_PARANOID
1073 int i, j;
1074 int size;
1075 int pi, bi;
1076 for (i = 0; i < 8; i++) {
1077 size = snd_hda_codec_read(codec, pin_nid, 0,
1078 AC_VERB_GET_HDMI_DIP_SIZE, i);
1079 if (size == 0)
1080 continue;
1081
1082 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1083 for (j = 1; j < 1000; j++) {
1084 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1085 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1086 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001087 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001088 bi, pi, i);
1089 if (bi == 0) /* byte index wrapped around */
1090 break;
1091 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001092 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001093 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1094 i, size, j);
1095 }
1096#endif
1097}
1098
Wu Fengguang53d7d692010-09-21 14:25:49 +08001099static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001100{
Wu Fengguang53d7d692010-09-21 14:25:49 +08001101 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001102 u8 sum = 0;
1103 int i;
1104
Wu Fengguang53d7d692010-09-21 14:25:49 +08001105 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001106
Wu Fengguang53d7d692010-09-21 14:25:49 +08001107 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001108 sum += bytes[i];
1109
Wu Fengguang53d7d692010-09-21 14:25:49 +08001110 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001111}
1112
1113static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1114 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001115 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001116{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001117 int i;
1118
1119 hdmi_debug_dip_size(codec, pin_nid);
1120 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1121
Wu Fengguang079d88c2010-03-08 10:44:23 +08001122 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001123 for (i = 0; i < size; i++)
1124 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001125}
1126
1127static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001128 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001129{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001130 u8 val;
1131 int i;
1132
1133 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1134 != AC_DIPXMIT_BEST)
1135 return false;
1136
1137 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001138 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001139 val = snd_hda_codec_read(codec, pin_nid, 0,
1140 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001141 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +08001142 return false;
1143 }
1144
1145 return true;
1146}
1147
Anssi Hannula307229d2013-10-24 21:10:34 +03001148static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1149 hda_nid_t pin_nid,
1150 int ca, int active_channels,
1151 int conn_type)
1152{
1153 union audio_infoframe ai;
1154
Mengdong Lincaaf5ef2014-03-11 17:12:52 -04001155 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +03001156 if (conn_type == 0) { /* HDMI */
1157 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1158
1159 hdmi_ai->type = 0x84;
1160 hdmi_ai->ver = 0x01;
1161 hdmi_ai->len = 0x0a;
1162 hdmi_ai->CC02_CT47 = active_channels - 1;
1163 hdmi_ai->CA = ca;
1164 hdmi_checksum_audio_infoframe(hdmi_ai);
1165 } else if (conn_type == 1) { /* DisplayPort */
1166 struct dp_audio_infoframe *dp_ai = &ai.dp;
1167
1168 dp_ai->type = 0x84;
1169 dp_ai->len = 0x1b;
1170 dp_ai->ver = 0x11 << 2;
1171 dp_ai->CC02_CT47 = active_channels - 1;
1172 dp_ai->CA = ca;
1173 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001174 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001175 pin_nid);
1176 return;
1177 }
1178
1179 /*
1180 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1181 * sizeof(*dp_ai) to avoid partial match/update problems when
1182 * the user switches between HDMI/DP monitors.
1183 */
1184 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1185 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001186 codec_dbg(codec,
1187 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001188 pin_nid,
1189 active_channels, ca);
1190 hdmi_stop_infoframe_trans(codec, pin_nid);
1191 hdmi_fill_audio_infoframe(codec, pin_nid,
1192 ai.bytes, sizeof(ai));
1193 hdmi_start_infoframe_trans(codec, pin_nid);
1194 }
1195}
1196
Takashi Iwaib0540872013-09-02 12:33:02 +02001197static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1198 struct hdmi_spec_per_pin *per_pin,
1199 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001200{
Anssi Hannula307229d2013-10-24 21:10:34 +03001201 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001202 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +02001203 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001204 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -06001205 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001206 int ca, ordered_ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001207
Takashi Iwaib0540872013-09-02 12:33:02 +02001208 if (!channels)
1209 return;
1210
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001211 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001212 snd_hda_codec_write(codec, pin_nid, 0,
1213 AC_VERB_SET_AMP_GAIN_MUTE,
1214 AMP_OUT_UNMUTE);
1215
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001216 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001217
Takashi Iwaid45e6882012-07-31 11:36:00 +02001218 if (!non_pcm && per_pin->chmap_set)
1219 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1220 else
Takashi Iwai79514d42014-06-06 18:04:34 +02001221 ca = hdmi_channel_allocation(codec, eld, channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001222 if (ca < 0)
1223 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001224
Anssi Hannula1df5a062013-10-05 02:25:40 +03001225 ordered_ca = get_channel_allocation_order(ca);
1226 active_channels = channel_allocations[ordered_ca].channels;
1227
1228 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1229
Stephen Warren384a48d2011-06-01 11:14:21 -06001230 /*
Anssi Hannula39edac72013-10-07 19:24:52 +03001231 * always configure channel mapping, it may have been changed by the
1232 * user in the meantime
1233 */
1234 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1235 channels, per_pin->chmap,
1236 per_pin->chmap_set);
1237
Anssi Hannula307229d2013-10-24 21:10:34 +03001238 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1239 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +08001240
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001241 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001242}
1243
Wu Fengguang079d88c2010-03-08 10:44:23 +08001244/*
1245 * Unsolicited events
1246 */
1247
Takashi Iwaiefe47102013-11-07 13:38:23 +01001248static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001249
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001250static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001251{
1252 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001253 int pin_idx = pin_nid_to_pin_index(codec, nid);
1254
David Henningsson20ce9022013-12-04 10:19:41 +08001255 if (pin_idx < 0)
1256 return;
David Henningsson20ce9022013-12-04 10:19:41 +08001257 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1258 snd_hda_jack_report_sync(codec);
1259}
1260
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001261static void jack_callback(struct hda_codec *codec,
1262 struct hda_jack_callback *jack)
1263{
Takashi Iwai2ebab402016-02-09 10:23:52 +01001264 check_presence_and_report(codec, jack->nid);
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001265}
1266
David Henningsson20ce9022013-12-04 10:19:41 +08001267static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1268{
Takashi Iwai3a938972011-10-28 01:16:55 +02001269 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001270 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001271 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001272
1273 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1274 if (!jack)
1275 return;
Takashi Iwai3a938972011-10-28 01:16:55 +02001276 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001277
Takashi Iwai4e76a882014-02-25 12:21:03 +01001278 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001279 "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 +08001280 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001281 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001282
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001283 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001284}
1285
1286static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1287{
1288 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1289 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1290 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1291 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1292
Takashi Iwai4e76a882014-02-25 12:21:03 +01001293 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001294 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001295 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001296 tag,
1297 subtag,
1298 cp_state,
1299 cp_ready);
1300
1301 /* TODO */
1302 if (cp_state)
1303 ;
1304 if (cp_ready)
1305 ;
1306}
1307
1308
1309static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1310{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001311 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1312 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1313
Takashi Iwai3a938972011-10-28 01:16:55 +02001314 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001315 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001316 return;
1317 }
1318
1319 if (subtag == 0)
1320 hdmi_intrinsic_event(codec, res);
1321 else
1322 hdmi_non_intrinsic_event(codec, res);
1323}
1324
Mengdong Lin58f7d282013-09-04 16:37:12 -04001325static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001326 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001327{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001328 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001329
Wang Xingchao53b434f2013-06-18 10:41:53 +08001330 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1331 * thus pins could only choose converter 0 for use. Make sure the
1332 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001333 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001334 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1335
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001336 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001337 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1338 AC_PWRST_D0);
1339 msleep(40);
1340 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1341 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001342 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 +02001343 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001344}
1345
Wu Fengguang079d88c2010-03-08 10:44:23 +08001346/*
1347 * Callbacks
1348 */
1349
Takashi Iwai92f10b32010-08-03 14:21:00 +02001350/* HBR should be Non-PCM, 8 channels */
1351#define is_hbr_format(format) \
1352 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1353
Anssi Hannula307229d2013-10-24 21:10:34 +03001354static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1355 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001356{
Anssi Hannula307229d2013-10-24 21:10:34 +03001357 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +02001358
Stephen Warren384a48d2011-06-01 11:14:21 -06001359 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1360 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001361 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1362
Anssi Hannula13122e62013-11-10 20:56:10 +02001363 if (pinctl < 0)
1364 return hbr ? -EINVAL : 0;
1365
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001366 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +03001367 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001368 new_pinctl |= AC_PINCTL_EPT_HBR;
1369 else
1370 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1371
Takashi Iwai4e76a882014-02-25 12:21:03 +01001372 codec_dbg(codec,
1373 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001374 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001375 pinctl == new_pinctl ? "" : "new-",
1376 new_pinctl);
1377
1378 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001379 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001380 AC_VERB_SET_PIN_WIDGET_CONTROL,
1381 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +03001382 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001383 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03001384
1385 return 0;
1386}
1387
1388static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1389 hda_nid_t pin_nid, u32 stream_tag, int format)
1390{
1391 struct hdmi_spec *spec = codec->spec;
1392 int err;
1393
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001394 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +03001395 haswell_verify_D0(codec, cvt_nid, pin_nid);
1396
1397 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1398
1399 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001400 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +03001401 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001402 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001403
Stephen Warren384a48d2011-06-01 11:14:21 -06001404 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001405 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001406}
1407
Libin Yang42b29872015-12-16 13:42:42 +08001408/* Try to find an available converter
1409 * If pin_idx is less then zero, just try to find an available converter.
1410 * Otherwise, try to find an available converter and get the cvt mux index
1411 * of the pin.
1412 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001413static int hdmi_choose_cvt(struct hda_codec *codec,
1414 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001415{
1416 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001417 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001418 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001419 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001420
Libin Yang42b29872015-12-16 13:42:42 +08001421 /* pin_idx < 0 means no pin will be bound to the converter */
1422 if (pin_idx < 0)
1423 per_pin = NULL;
1424 else
1425 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001426
Stephen Warren384a48d2011-06-01 11:14:21 -06001427 /* Dynamically assign converter to stream */
1428 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001429 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001430
1431 /* Must not already be assigned */
1432 if (per_cvt->assigned)
1433 continue;
Libin Yang42b29872015-12-16 13:42:42 +08001434 if (per_pin == NULL)
1435 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06001436 /* Must be in pin's mux's list of converters */
1437 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1438 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1439 break;
1440 /* Not in mux list */
1441 if (mux_idx == per_pin->num_mux_nids)
1442 continue;
1443 break;
1444 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001445
Stephen Warren384a48d2011-06-01 11:14:21 -06001446 /* No free converters */
1447 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +08001448 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -06001449
Libin Yang42b29872015-12-16 13:42:42 +08001450 if (per_pin != NULL)
1451 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +08001452
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001453 if (cvt_id)
1454 *cvt_id = cvt_idx;
1455 if (mux_id)
1456 *mux_id = mux_idx;
1457
1458 return 0;
1459}
1460
Mengdong Lin2df67422014-03-20 13:01:06 +08001461/* Assure the pin select the right convetor */
1462static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1463 struct hdmi_spec_per_pin *per_pin)
1464{
1465 hda_nid_t pin_nid = per_pin->pin_nid;
1466 int mux_idx, curr;
1467
1468 mux_idx = per_pin->mux_idx;
1469 curr = snd_hda_codec_read(codec, pin_nid, 0,
1470 AC_VERB_GET_CONNECT_SEL, 0);
1471 if (curr != mux_idx)
1472 snd_hda_codec_write_cache(codec, pin_nid, 0,
1473 AC_VERB_SET_CONNECT_SEL,
1474 mux_idx);
1475}
1476
Libin Yang42b29872015-12-16 13:42:42 +08001477/* get the mux index for the converter of the pins
1478 * converter's mux index is the same for all pins on Intel platform
1479 */
1480static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1481 hda_nid_t cvt_nid)
1482{
1483 int i;
1484
1485 for (i = 0; i < spec->num_cvts; i++)
1486 if (spec->cvt_nids[i] == cvt_nid)
1487 return i;
1488 return -EINVAL;
1489}
1490
Mengdong Lin300016b2013-11-04 01:13:13 -05001491/* Intel HDMI workaround to fix audio routing issue:
1492 * For some Intel display codecs, pins share the same connection list.
1493 * So a conveter can be selected by multiple pins and playback on any of these
1494 * pins will generate sound on the external display, because audio flows from
1495 * the same converter to the display pipeline. Also muting one pin may make
1496 * other pins have no sound output.
1497 * So this function assures that an assigned converter for a pin is not selected
1498 * by any other pins.
1499 */
1500static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001501 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001502{
1503 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001504 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001505 int cvt_idx, curr;
1506 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001507
Mengdong Linf82d7d12013-09-21 20:34:45 -04001508 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +01001509 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -04001510 unsigned int wid_caps = get_wcaps(codec, nid);
1511 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001512
Mengdong Linf82d7d12013-09-21 20:34:45 -04001513 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001514 continue;
1515
Mengdong Linf82d7d12013-09-21 20:34:45 -04001516 if (nid == pin_nid)
1517 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001518
Mengdong Linf82d7d12013-09-21 20:34:45 -04001519 curr = snd_hda_codec_read(codec, nid, 0,
1520 AC_VERB_GET_CONNECT_SEL, 0);
1521 if (curr != mux_idx)
1522 continue;
1523
1524 /* choose an unassigned converter. The conveters in the
1525 * connection list are in the same order as in the codec.
1526 */
1527 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1528 per_cvt = get_cvt(spec, cvt_idx);
1529 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001530 codec_dbg(codec,
1531 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001532 cvt_idx, nid);
1533 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001534 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001535 cvt_idx);
1536 break;
1537 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001538 }
1539 }
1540}
1541
Libin Yang42b29872015-12-16 13:42:42 +08001542/* A wrapper of intel_not_share_asigned_cvt() */
1543static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1544 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1545{
1546 int mux_idx;
1547 struct hdmi_spec *spec = codec->spec;
1548
1549 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1550 return;
1551
1552 /* On Intel platform, the mapping of converter nid to
1553 * mux index of the pins are always the same.
1554 * The pin nid may be 0, this means all pins will not
1555 * share the converter.
1556 */
1557 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1558 if (mux_idx >= 0)
1559 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1560}
1561
1562/* called in hdmi_pcm_open when no pin is assigned to the PCM
1563 * in dyn_pcm_assign mode.
1564 */
1565static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1566 struct hda_codec *codec,
1567 struct snd_pcm_substream *substream)
1568{
1569 struct hdmi_spec *spec = codec->spec;
1570 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001571 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001572 struct hdmi_spec_per_cvt *per_cvt = NULL;
1573 int err;
1574
Libin Yangac983792015-12-16 16:48:16 +08001575 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1576 if (pcm_idx < 0)
1577 return -EINVAL;
1578
Libin Yang42b29872015-12-16 13:42:42 +08001579 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1580 if (err)
1581 return err;
1582
1583 per_cvt = get_cvt(spec, cvt_idx);
1584 per_cvt->assigned = 1;
1585 hinfo->nid = per_cvt->cvt_nid;
1586
1587 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1588
Libin Yangac983792015-12-16 16:48:16 +08001589 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001590 /* todo: setup spdif ctls assign */
1591
1592 /* Initially set the converter's capabilities */
1593 hinfo->channels_min = per_cvt->channels_min;
1594 hinfo->channels_max = per_cvt->channels_max;
1595 hinfo->rates = per_cvt->rates;
1596 hinfo->formats = per_cvt->formats;
1597 hinfo->maxbps = per_cvt->maxbps;
1598
1599 /* Store the updated parameters */
1600 runtime->hw.channels_min = hinfo->channels_min;
1601 runtime->hw.channels_max = hinfo->channels_max;
1602 runtime->hw.formats = hinfo->formats;
1603 runtime->hw.rates = hinfo->rates;
1604
1605 snd_pcm_hw_constraint_step(substream->runtime, 0,
1606 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1607 return 0;
1608}
1609
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001610/*
1611 * HDA PCM callbacks
1612 */
1613static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1614 struct hda_codec *codec,
1615 struct snd_pcm_substream *substream)
1616{
1617 struct hdmi_spec *spec = codec->spec;
1618 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001619 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001620 struct hdmi_spec_per_pin *per_pin;
1621 struct hdmi_eld *eld;
1622 struct hdmi_spec_per_cvt *per_cvt = NULL;
1623 int err;
1624
1625 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001626 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1627 if (pcm_idx < 0)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001628 return -EINVAL;
Libin Yang2bf3c852015-12-16 13:42:43 +08001629
Libin Yang42b29872015-12-16 13:42:42 +08001630 mutex_lock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001631 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001632 if (!spec->dyn_pcm_assign) {
1633 if (snd_BUG_ON(pin_idx < 0)) {
1634 mutex_unlock(&spec->pcm_lock);
1635 return -EINVAL;
1636 }
1637 } else {
1638 /* no pin is assigned to the PCM
1639 * PA need pcm open successfully when probe
1640 */
1641 if (pin_idx < 0) {
1642 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1643 mutex_unlock(&spec->pcm_lock);
1644 return err;
1645 }
1646 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001647
1648 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001649 if (err < 0) {
1650 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001651 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001652 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001653
1654 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001655 /* Claim converter */
1656 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001657
Libin Yangac983792015-12-16 16:48:16 +08001658 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001659 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001660 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001661 hinfo->nid = per_cvt->cvt_nid;
1662
Takashi Iwaibddee962013-06-18 16:14:22 +02001663 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001664 AC_VERB_SET_CONNECT_SEL,
1665 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001666
1667 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001668 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001669 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001670
Libin Yang2bf3c852015-12-16 13:42:43 +08001671 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001672
Stephen Warren2def8172011-06-01 11:14:20 -06001673 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001674 hinfo->channels_min = per_cvt->channels_min;
1675 hinfo->channels_max = per_cvt->channels_max;
1676 hinfo->rates = per_cvt->rates;
1677 hinfo->formats = per_cvt->formats;
1678 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001679
Libin Yang42b29872015-12-16 13:42:42 +08001680 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001681 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001682 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001683 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001684 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001685 !hinfo->rates || !hinfo->formats) {
1686 per_cvt->assigned = 0;
1687 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001688 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001689 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001690 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001691 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001692 }
Stephen Warren2def8172011-06-01 11:14:20 -06001693
Libin Yang42b29872015-12-16 13:42:42 +08001694 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001695 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001696 runtime->hw.channels_min = hinfo->channels_min;
1697 runtime->hw.channels_max = hinfo->channels_max;
1698 runtime->hw.formats = hinfo->formats;
1699 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001700
1701 snd_pcm_hw_constraint_step(substream->runtime, 0,
1702 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001703 return 0;
1704}
1705
1706/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001707 * HDA/HDMI auto parsing
1708 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001709static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001710{
1711 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001712 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001713 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001714
1715 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001716 codec_warn(codec,
1717 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001718 pin_nid, get_wcaps(codec, pin_nid));
1719 return -EINVAL;
1720 }
1721
Stephen Warren384a48d2011-06-01 11:14:21 -06001722 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1723 per_pin->mux_nids,
1724 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001725
1726 return 0;
1727}
1728
Libin Yanga76056f2015-12-16 16:48:15 +08001729static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1730 struct hdmi_spec_per_pin *per_pin)
1731{
1732 int i;
1733
1734 /* try the prefer PCM */
1735 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1736 return per_pin->pin_nid_idx;
1737
1738 /* have a second try; check the "reserved area" over num_pins */
1739 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1740 if (!test_bit(i, &spec->pcm_bitmap))
1741 return i;
1742 }
1743
1744 /* the last try; check the empty slots in pins */
1745 for (i = 0; i < spec->num_pins; i++) {
1746 if (!test_bit(i, &spec->pcm_bitmap))
1747 return i;
1748 }
1749 return -EBUSY;
1750}
1751
1752static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1753 struct hdmi_spec_per_pin *per_pin)
1754{
1755 int idx;
1756
1757 /* pcm already be attached to the pin */
1758 if (per_pin->pcm)
1759 return;
1760 idx = hdmi_find_pcm_slot(spec, per_pin);
1761 if (idx == -ENODEV)
1762 return;
1763 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001764 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001765 set_bit(idx, &spec->pcm_bitmap);
1766}
1767
1768static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1769 struct hdmi_spec_per_pin *per_pin)
1770{
1771 int idx;
1772
1773 /* pcm already be detached from the pin */
1774 if (!per_pin->pcm)
1775 return;
1776 idx = per_pin->pcm_idx;
1777 per_pin->pcm_idx = -1;
1778 per_pin->pcm = NULL;
1779 if (idx >= 0 && idx < spec->pcm_used)
1780 clear_bit(idx, &spec->pcm_bitmap);
1781}
1782
Libin Yangac983792015-12-16 16:48:16 +08001783static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1784 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1785{
1786 int mux_idx;
1787
1788 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1789 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1790 break;
1791 return mux_idx;
1792}
1793
1794static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1795
1796static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1797 struct hdmi_spec_per_pin *per_pin)
1798{
1799 struct hda_codec *codec = per_pin->codec;
1800 struct hda_pcm *pcm;
1801 struct hda_pcm_stream *hinfo;
1802 struct snd_pcm_substream *substream;
1803 int mux_idx;
1804 bool non_pcm;
1805
1806 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001807 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001808 else
1809 return;
1810 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1811 return;
1812
1813 /* hdmi audio only uses playback and one substream */
1814 hinfo = pcm->stream;
1815 substream = pcm->pcm->streams[0].substream;
1816
1817 per_pin->cvt_nid = hinfo->nid;
1818
1819 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1820 if (mux_idx < per_pin->num_mux_nids)
1821 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1822 AC_VERB_SET_CONNECT_SEL,
1823 mux_idx);
1824 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1825
1826 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1827 if (substream->runtime)
1828 per_pin->channels = substream->runtime->channels;
1829 per_pin->setup = true;
1830 per_pin->mux_idx = mux_idx;
1831
1832 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1833}
1834
1835static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1836 struct hdmi_spec_per_pin *per_pin)
1837{
1838 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1839 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1840
1841 per_pin->chmap_set = false;
1842 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1843
1844 per_pin->setup = false;
1845 per_pin->channels = 0;
1846}
1847
Takashi Iwaie90247f2015-11-13 09:12:12 +01001848/* update per_pin ELD from the given new ELD;
1849 * setup info frame and notification accordingly
1850 */
1851static void update_eld(struct hda_codec *codec,
1852 struct hdmi_spec_per_pin *per_pin,
1853 struct hdmi_eld *eld)
1854{
1855 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001856 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001857 bool old_eld_valid = pin_eld->eld_valid;
1858 bool eld_changed;
Libin Yangfb087ea2016-02-23 16:33:37 +08001859 int pcm_idx = -1;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001860
Libin Yangfb087ea2016-02-23 16:33:37 +08001861 /* for monitor disconnection, save pcm_idx firstly */
1862 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001863 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001864 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001865 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001866 hdmi_pcm_setup_pin(spec, per_pin);
1867 } else {
1868 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001869 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001870 }
Libin Yanga76056f2015-12-16 16:48:15 +08001871 }
Libin Yangfb087ea2016-02-23 16:33:37 +08001872 /* if pcm_idx == -1, it means this is in monitor connection event
1873 * we can get the correct pcm_idx now.
1874 */
1875 if (pcm_idx == -1)
1876 pcm_idx = per_pin->pcm_idx;
Libin Yanga76056f2015-12-16 16:48:15 +08001877
Takashi Iwaie90247f2015-11-13 09:12:12 +01001878 if (eld->eld_valid)
1879 snd_hdmi_show_eld(codec, &eld->info);
1880
1881 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1882 if (eld->eld_valid && pin_eld->eld_valid)
1883 if (pin_eld->eld_size != eld->eld_size ||
1884 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1885 eld->eld_size) != 0)
1886 eld_changed = true;
1887
1888 pin_eld->eld_valid = eld->eld_valid;
1889 pin_eld->eld_size = eld->eld_size;
1890 if (eld->eld_valid)
1891 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1892 pin_eld->info = eld->info;
1893
1894 /*
1895 * Re-setup pin and infoframe. This is needed e.g. when
1896 * - sink is first plugged-in
1897 * - transcoder can change during stream playback on Haswell
1898 * and this can make HW reset converter selection on a pin.
1899 */
1900 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1901 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1902 intel_verify_pin_cvt_connect(codec, per_pin);
1903 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1904 per_pin->mux_idx);
1905 }
1906
1907 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1908 }
1909
Libin Yangfb087ea2016-02-23 16:33:37 +08001910 if (eld_changed && pcm_idx >= 0)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001911 snd_ctl_notify(codec->card,
1912 SNDRV_CTL_EVENT_MASK_VALUE |
1913 SNDRV_CTL_EVENT_MASK_INFO,
Libin Yangfb087ea2016-02-23 16:33:37 +08001914 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
Takashi Iwaie90247f2015-11-13 09:12:12 +01001915}
1916
Takashi Iwai788d4412015-11-12 15:36:13 +01001917/* update ELD and jack state via HD-audio verbs */
1918static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1919 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001920{
David Henningsson464837a2013-11-07 13:38:25 +01001921 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001922 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001923 struct hdmi_spec *spec = codec->spec;
1924 struct hdmi_eld *eld = &spec->temp_eld;
1925 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001926 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001927 /*
1928 * Always execute a GetPinSense verb here, even when called from
1929 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1930 * response's PD bit is not the real PD value, but indicates that
1931 * the real PD value changed. An older version of the HD-audio
1932 * specification worked this way. Hence, we just ignore the data in
1933 * the unsolicited response to avoid custom WARs.
1934 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001935 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001936 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001937 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001938
Takashi Iwai664c7152015-04-08 11:43:14 +02001939 snd_hda_power_up_pm(codec);
David Henningssonda4a7a32013-12-18 10:46:04 +01001940 present = snd_hda_pin_sense(codec, pin_nid);
1941
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001942 mutex_lock(&per_pin->lock);
David Henningsson4bd038f2013-02-19 16:11:25 +01001943 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1944 if (pin_eld->monitor_present)
1945 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1946 else
1947 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001948
Takashi Iwai4e76a882014-02-25 12:21:03 +01001949 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001950 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001951 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001952
David Henningsson4bd038f2013-02-19 16:11:25 +01001953 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001954 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001955 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001956 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001957 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001958 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001959 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001960 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001961 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001962 if (!eld->eld_valid && repoll)
1963 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001964 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001965
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001966 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001967 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1968 else
1969 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001970
Takashi Iwaiaff747eb2013-11-07 16:39:37 +01001971 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001972
1973 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1974 if (jack)
1975 jack->block_report = !ret;
1976
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001977 mutex_unlock(&per_pin->lock);
Takashi Iwai664c7152015-04-08 11:43:14 +02001978 snd_hda_power_down_pm(codec);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001979 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001980}
1981
Libin Yang31842702016-02-19 15:42:06 +08001982static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1983 struct hdmi_spec_per_pin *per_pin)
1984{
1985 struct hdmi_spec *spec = codec->spec;
1986 struct snd_jack *jack = NULL;
1987 struct hda_jack_tbl *jack_tbl;
1988
1989 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1990 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1991 * NULL even after snd_hda_jack_tbl_clear() is called to
1992 * free snd_jack. This may cause access invalid memory
1993 * when calling snd_jack_report
1994 */
1995 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1996 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1997 else if (!spec->dyn_pcm_assign) {
1998 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1999 if (jack_tbl)
2000 jack = jack_tbl->jack;
2001 }
2002 return jack;
2003}
2004
Takashi Iwai788d4412015-11-12 15:36:13 +01002005/* update ELD and jack state via audio component */
2006static void sync_eld_via_acomp(struct hda_codec *codec,
2007 struct hdmi_spec_per_pin *per_pin)
2008{
Takashi Iwai788d4412015-11-12 15:36:13 +01002009 struct hdmi_spec *spec = codec->spec;
2010 struct hdmi_eld *eld = &spec->temp_eld;
Libin Yang25e4abb2016-01-12 11:13:27 +08002011 struct snd_jack *jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002012 int size;
2013
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002014 mutex_lock(&per_pin->lock);
2015 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
2016 &eld->monitor_present, eld->eld_buffer,
2017 ELD_MAX_SIZE);
2018 if (size < 0)
2019 goto unlock;
2020 if (size > 0) {
2021 size = min(size, ELD_MAX_SIZE);
2022 if (snd_hdmi_parse_eld(codec, &eld->info,
2023 eld->eld_buffer, size) < 0)
2024 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002025 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002026
2027 if (size > 0) {
2028 eld->eld_valid = true;
2029 eld->eld_size = size;
2030 } else {
2031 eld->eld_valid = false;
2032 eld->eld_size = 0;
2033 }
2034
Libin Yang25e4abb2016-01-12 11:13:27 +08002035 /* pcm_idx >=0 before update_eld() means it is in monitor
2036 * disconnected event. Jack must be fetched before update_eld()
2037 */
Libin Yang31842702016-02-19 15:42:06 +08002038 jack = pin_idx_to_jack(codec, per_pin);
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002039 update_eld(codec, per_pin, eld);
Libin Yang31842702016-02-19 15:42:06 +08002040 if (jack == NULL)
2041 jack = pin_idx_to_jack(codec, per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002042 if (jack == NULL)
2043 goto unlock;
2044 snd_jack_report(jack,
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002045 eld->monitor_present ? SND_JACK_AVOUT : 0);
2046 unlock:
2047 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01002048}
2049
2050static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
2051{
2052 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08002053 struct hdmi_spec *spec = codec->spec;
2054 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01002055
Libin Yanga76056f2015-12-16 16:48:15 +08002056 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01002057 if (codec_has_acomp(codec)) {
2058 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08002059 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01002060 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08002061 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01002062 }
Libin Yanga76056f2015-12-16 16:48:15 +08002063 mutex_unlock(&spec->pcm_lock);
2064
2065 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01002066}
2067
Wu Fengguang744626d2011-11-16 16:29:47 +08002068static void hdmi_repoll_eld(struct work_struct *work)
2069{
2070 struct hdmi_spec_per_pin *per_pin =
2071 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
2072
Wu Fengguangc6e84532011-11-18 16:59:32 -06002073 if (per_pin->repoll_count++ > 6)
2074 per_pin->repoll_count = 0;
2075
Takashi Iwaiefe47102013-11-07 13:38:23 +01002076 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
2077 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08002078}
2079
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002080static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2081 hda_nid_t nid);
2082
Wu Fengguang079d88c2010-03-08 10:44:23 +08002083static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2084{
2085 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002086 unsigned int caps, config;
2087 int pin_idx;
2088 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02002089 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002090
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002091 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002092 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2093 return 0;
2094
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002095 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002096 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2097 return 0;
2098
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002099 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002100 intel_haswell_fixup_connect_list(codec, pin_nid);
2101
Stephen Warren384a48d2011-06-01 11:14:21 -06002102 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002103 per_pin = snd_array_new(&spec->pins);
2104 if (!per_pin)
2105 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002106
2107 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002108 per_pin->non_pcm = false;
Libin Yanga76056f2015-12-16 16:48:15 +08002109 if (spec->dyn_pcm_assign)
2110 per_pin->pcm_idx = -1;
Libin Yang2bea2412016-01-12 11:13:26 +08002111 else {
2112 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
Libin Yanga76056f2015-12-16 16:48:15 +08002113 per_pin->pcm_idx = pin_idx;
Libin Yang2bea2412016-01-12 11:13:26 +08002114 }
Libin Yanga76056f2015-12-16 16:48:15 +08002115 per_pin->pin_nid_idx = pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002116
Stephen Warren384a48d2011-06-01 11:14:21 -06002117 err = hdmi_read_pin_conn(codec, pin_idx);
2118 if (err < 0)
2119 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002120
Wu Fengguang079d88c2010-03-08 10:44:23 +08002121 spec->num_pins++;
2122
Stephen Warren384a48d2011-06-01 11:14:21 -06002123 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002124}
2125
Stephen Warren384a48d2011-06-01 11:14:21 -06002126static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08002127{
2128 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002129 struct hdmi_spec_per_cvt *per_cvt;
2130 unsigned int chans;
2131 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002132
Stephen Warren384a48d2011-06-01 11:14:21 -06002133 chans = get_wcaps(codec, cvt_nid);
2134 chans = get_wcaps_channels(chans);
2135
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002136 per_cvt = snd_array_new(&spec->cvts);
2137 if (!per_cvt)
2138 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002139
2140 per_cvt->cvt_nid = cvt_nid;
2141 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002142 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002143 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002144 if (chans > spec->channels_max)
2145 spec->channels_max = chans;
2146 }
Stephen Warren384a48d2011-06-01 11:14:21 -06002147
2148 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2149 &per_cvt->rates,
2150 &per_cvt->formats,
2151 &per_cvt->maxbps);
2152 if (err < 0)
2153 return err;
2154
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002155 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2156 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2157 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002158
2159 return 0;
2160}
2161
2162static int hdmi_parse_codec(struct hda_codec *codec)
2163{
2164 hda_nid_t nid;
2165 int i, nodes;
2166
Takashi Iwai7639a062015-03-03 10:07:24 +01002167 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002168 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002169 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08002170 return -EINVAL;
2171 }
2172
2173 for (i = 0; i < nodes; i++, nid++) {
2174 unsigned int caps;
2175 unsigned int type;
2176
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002177 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002178 type = get_wcaps_type(caps);
2179
2180 if (!(caps & AC_WCAP_DIGITAL))
2181 continue;
2182
2183 switch (type) {
2184 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06002185 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002186 break;
2187 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08002188 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002189 break;
2190 }
2191 }
2192
Wu Fengguang079d88c2010-03-08 10:44:23 +08002193 return 0;
2194}
2195
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002196/*
2197 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002198static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2199{
2200 struct hda_spdif_out *spdif;
2201 bool non_pcm;
2202
2203 mutex_lock(&codec->spdif_mutex);
2204 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2205 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2206 mutex_unlock(&codec->spdif_mutex);
2207 return non_pcm;
2208}
2209
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002210/*
2211 * HDMI callbacks
2212 */
2213
2214static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2215 struct hda_codec *codec,
2216 unsigned int stream_tag,
2217 unsigned int format,
2218 struct snd_pcm_substream *substream)
2219{
Stephen Warren384a48d2011-06-01 11:14:21 -06002220 hda_nid_t cvt_nid = hinfo->nid;
2221 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08002222 int pin_idx;
2223 struct hdmi_spec_per_pin *per_pin;
2224 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08002225 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002226 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07002227 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08002228 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002229
Libin Yang42b29872015-12-16 13:42:42 +08002230 mutex_lock(&spec->pcm_lock);
2231 pin_idx = hinfo_to_pin_index(codec, hinfo);
2232 if (spec->dyn_pcm_assign && pin_idx < 0) {
2233 /* when dyn_pcm_assign and pcm is not bound to a pin
2234 * skip pin setup and return 0 to make audio playback
2235 * be ongoing
2236 */
2237 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2238 snd_hda_codec_setup_stream(codec, cvt_nid,
2239 stream_tag, 0, format);
2240 mutex_unlock(&spec->pcm_lock);
2241 return 0;
2242 }
2243
2244 if (snd_BUG_ON(pin_idx < 0)) {
2245 mutex_unlock(&spec->pcm_lock);
2246 return -EINVAL;
2247 }
2248 per_pin = get_pin(spec, pin_idx);
2249 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08002250 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08002251 /* Verify pin:cvt selections to avoid silent audio after S3.
2252 * After S3, the audio driver restores pin:cvt selections
2253 * but this can happen before gfx is ready and such selection
2254 * is overlooked by HW. Thus multiple pins can share a same
2255 * default convertor and mute control will affect each other,
2256 * which can cause a resumed audio playback become silent
2257 * after S3.
2258 */
2259 intel_verify_pin_cvt_connect(codec, per_pin);
2260 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2261 }
2262
Libin Yangddd621f2015-09-02 14:11:40 +08002263 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2264 /* Todo: add DP1.2 MST audio support later */
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002265 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08002266
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002267 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002268 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02002269 per_pin->channels = substream->runtime->channels;
2270 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002271
Takashi Iwaib0540872013-09-02 12:33:02 +02002272 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002273 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07002274 if (spec->dyn_pin_out) {
2275 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2276 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2277 snd_hda_codec_write(codec, pin_nid, 0,
2278 AC_VERB_SET_PIN_WIDGET_CONTROL,
2279 pinctl | PIN_OUT);
2280 }
2281
Libin Yang42b29872015-12-16 13:42:42 +08002282 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2283 stream_tag, format);
2284 mutex_unlock(&spec->pcm_lock);
2285 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002286}
2287
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002288static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2289 struct hda_codec *codec,
2290 struct snd_pcm_substream *substream)
2291{
2292 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2293 return 0;
2294}
2295
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002296static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2297 struct hda_codec *codec,
2298 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06002299{
2300 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08002301 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06002302 struct hdmi_spec_per_cvt *per_cvt;
2303 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07002304 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06002305
Stephen Warren384a48d2011-06-01 11:14:21 -06002306 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08002307 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2308 if (snd_BUG_ON(pcm_idx < 0))
2309 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002310 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002311 if (snd_BUG_ON(cvt_idx < 0))
2312 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002313 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002314
2315 snd_BUG_ON(!per_cvt->assigned);
2316 per_cvt->assigned = 0;
2317 hinfo->nid = 0;
2318
Libin Yang42b29872015-12-16 13:42:42 +08002319 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08002320 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08002321 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002322 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08002323 if (spec->dyn_pcm_assign && pin_idx < 0) {
2324 mutex_unlock(&spec->pcm_lock);
2325 return 0;
2326 }
2327
2328 if (snd_BUG_ON(pin_idx < 0)) {
2329 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002330 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08002331 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002332 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002333
Stephen Warren75fae112014-01-30 11:52:16 -07002334 if (spec->dyn_pin_out) {
2335 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2336 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2337 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2338 AC_VERB_SET_PIN_WIDGET_CONTROL,
2339 pinctl & ~PIN_OUT);
2340 }
2341
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002342 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002343 per_pin->chmap_set = false;
2344 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002345
2346 per_pin->setup = false;
2347 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002348 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08002349 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002350 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002351
Stephen Warren384a48d2011-06-01 11:14:21 -06002352 return 0;
2353}
2354
2355static const struct hda_pcm_ops generic_ops = {
2356 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002357 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002358 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002359 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002360};
2361
Takashi Iwaid45e6882012-07-31 11:36:00 +02002362/*
2363 * ALSA API channel-map control callbacks
2364 */
2365static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2366 struct snd_ctl_elem_info *uinfo)
2367{
2368 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2369 struct hda_codec *codec = info->private_data;
2370 struct hdmi_spec *spec = codec->spec;
2371 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2372 uinfo->count = spec->channels_max;
2373 uinfo->value.integer.min = 0;
2374 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2375 return 0;
2376}
2377
Anssi Hannula307229d2013-10-24 21:10:34 +03002378static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2379 int channels)
2380{
2381 /* If the speaker allocation matches the channel count, it is OK.*/
2382 if (cap->channels != channels)
2383 return -1;
2384
2385 /* all channels are remappable freely */
2386 return SNDRV_CTL_TLVT_CHMAP_VAR;
2387}
2388
2389static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2390 unsigned int *chmap, int channels)
2391{
2392 int count = 0;
2393 int c;
2394
2395 for (c = 7; c >= 0; c--) {
2396 int spk = cap->speakers[c];
2397 if (!spk)
2398 continue;
2399
2400 chmap[count++] = spk_to_chmap(spk);
2401 }
2402
2403 WARN_ON(count != channels);
2404}
2405
Takashi Iwaid45e6882012-07-31 11:36:00 +02002406static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2407 unsigned int size, unsigned int __user *tlv)
2408{
2409 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2410 struct hda_codec *codec = info->private_data;
2411 struct hdmi_spec *spec = codec->spec;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002412 unsigned int __user *dst;
2413 int chs, count = 0;
2414
2415 if (size < 8)
2416 return -ENOMEM;
2417 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2418 return -EFAULT;
2419 size -= 8;
2420 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02002421 for (chs = 2; chs <= spec->channels_max; chs++) {
Anssi Hannula307229d2013-10-24 21:10:34 +03002422 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002423 struct cea_channel_speaker_allocation *cap;
2424 cap = channel_allocations;
2425 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2426 int chs_bytes = chs * 4;
Anssi Hannula307229d2013-10-24 21:10:34 +03002427 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2428 unsigned int tlv_chmap[8];
2429
2430 if (type < 0)
Takashi Iwaid45e6882012-07-31 11:36:00 +02002431 continue;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002432 if (size < 8)
2433 return -ENOMEM;
Anssi Hannula307229d2013-10-24 21:10:34 +03002434 if (put_user(type, dst) ||
Takashi Iwaid45e6882012-07-31 11:36:00 +02002435 put_user(chs_bytes, dst + 1))
2436 return -EFAULT;
2437 dst += 2;
2438 size -= 8;
2439 count += 8;
2440 if (size < chs_bytes)
2441 return -ENOMEM;
2442 size -= chs_bytes;
2443 count += chs_bytes;
Anssi Hannula307229d2013-10-24 21:10:34 +03002444 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2445 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2446 return -EFAULT;
2447 dst += chs;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002448 }
2449 }
2450 if (put_user(count, tlv + 1))
2451 return -EFAULT;
2452 return 0;
2453}
2454
2455static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2457{
2458 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2459 struct hda_codec *codec = info->private_data;
2460 struct hdmi_spec *spec = codec->spec;
Libin Yang022f3442016-02-03 10:48:34 +08002461 int pcm_idx = kcontrol->private_value;
2462 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002463 int i;
2464
Libin Yang022f3442016-02-03 10:48:34 +08002465 if (!per_pin) {
2466 for (i = 0; i < spec->channels_max; i++)
2467 ucontrol->value.integer.value[i] = 0;
2468 return 0;
2469 }
2470
Takashi Iwaid45e6882012-07-31 11:36:00 +02002471 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2472 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2473 return 0;
2474}
2475
2476static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2478{
2479 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2480 struct hda_codec *codec = info->private_data;
2481 struct hdmi_spec *spec = codec->spec;
Libin Yang022f3442016-02-03 10:48:34 +08002482 int pcm_idx = kcontrol->private_value;
2483 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002484 unsigned int ctl_idx;
2485 struct snd_pcm_substream *substream;
2486 unsigned char chmap[8];
Anssi Hannula307229d2013-10-24 21:10:34 +03002487 int i, err, ca, prepared = 0;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002488
Libin Yang022f3442016-02-03 10:48:34 +08002489 /* No monitor is connected in dyn_pcm_assign.
2490 * It's invalid to setup the chmap
2491 */
2492 if (!per_pin)
2493 return 0;
2494
Takashi Iwaid45e6882012-07-31 11:36:00 +02002495 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2496 substream = snd_pcm_chmap_substream(info, ctl_idx);
2497 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01002498 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02002499 switch (substream->runtime->status->state) {
2500 case SNDRV_PCM_STATE_OPEN:
2501 case SNDRV_PCM_STATE_SETUP:
2502 break;
2503 case SNDRV_PCM_STATE_PREPARED:
2504 prepared = 1;
2505 break;
2506 default:
2507 return -EBUSY;
2508 }
2509 memset(chmap, 0, sizeof(chmap));
2510 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2511 chmap[i] = ucontrol->value.integer.value[i];
2512 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2513 return 0;
2514 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2515 if (ca < 0)
2516 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03002517 if (spec->ops.chmap_validate) {
2518 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2519 if (err)
2520 return err;
2521 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002522 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002523 per_pin->chmap_set = true;
2524 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2525 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02002526 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002527 mutex_unlock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002528
2529 return 0;
2530}
2531
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002532static int generic_hdmi_build_pcms(struct hda_codec *codec)
2533{
2534 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002535 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002536
Stephen Warren384a48d2011-06-01 11:14:21 -06002537 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2538 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002539 struct hda_pcm_stream *pstr;
2540
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002541 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002542 if (!info)
2543 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002544
2545 spec->pcm_rec[pin_idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002546 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002547 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002548 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002549
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002550 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002551 pstr->substreams = 1;
2552 pstr->ops = generic_ops;
2553 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002554 }
2555
2556 return 0;
2557}
2558
Libin Yang25e4abb2016-01-12 11:13:27 +08002559static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01002560{
Libin Yang25e4abb2016-01-12 11:13:27 +08002561 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01002562
Libin Yang25e4abb2016-01-12 11:13:27 +08002563 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002564}
2565
Libin Yang25e4abb2016-01-12 11:13:27 +08002566static int add_hdmi_jack_kctl(struct hda_codec *codec,
2567 struct hdmi_spec *spec,
2568 int pcm_idx,
Takashi Iwai788d4412015-11-12 15:36:13 +01002569 const char *name)
2570{
2571 struct snd_jack *jack;
2572 int err;
2573
2574 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2575 true, false);
2576 if (err < 0)
2577 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002578
2579 spec->pcm_rec[pcm_idx].jack = jack;
2580 jack->private_data = &spec->pcm_rec[pcm_idx];
2581 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01002582 return 0;
2583}
2584
Libin Yang25e4abb2016-01-12 11:13:27 +08002585static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
David Henningsson0b6c49b2011-08-23 16:56:03 +02002586{
Takashi Iwai31ef2252011-12-01 17:41:36 +01002587 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02002588 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002589 struct hdmi_spec_per_pin *per_pin;
2590 struct hda_jack_tbl *jack;
2591 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01002592 bool phantom_jack;
Libin Yang25e4abb2016-01-12 11:13:27 +08002593 int ret;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002594
Takashi Iwai31ef2252011-12-01 17:41:36 +01002595 if (pcmdev > 0)
2596 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Libin Yang25e4abb2016-01-12 11:13:27 +08002597
2598 if (spec->dyn_pcm_assign)
2599 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2600
2601 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2602 /* if !dyn_pcm_assign, it must be non-MST mode.
2603 * This means pcms and pins are statically mapped.
2604 * And pcm_idx is pin_idx.
2605 */
2606 per_pin = get_pin(spec, pcm_idx);
Takashi Iwai909cadc2015-11-12 11:52:13 +01002607 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2608 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01002609 strncat(hdmi_str, " Phantom",
2610 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
Libin Yang25e4abb2016-01-12 11:13:27 +08002611 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2612 phantom_jack);
2613 if (ret < 0)
2614 return ret;
2615 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2616 if (jack == NULL)
2617 return 0;
2618 /* assign jack->jack to pcm_rec[].jack to
2619 * align with dyn_pcm_assign mode
2620 */
2621 spec->pcm_rec[pcm_idx].jack = jack->jack;
2622 return 0;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002623}
2624
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002625static int generic_hdmi_build_controls(struct hda_codec *codec)
2626{
2627 struct hdmi_spec *spec = codec->spec;
2628 int err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002629 int pin_idx, pcm_idx;
2630
2631
2632 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2633 err = generic_hdmi_build_jack(codec, pcm_idx);
2634 if (err < 0)
2635 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08002636
2637 /* create the spdif for each pcm
2638 * pin will be bound when monitor is connected
2639 */
2640 if (spec->dyn_pcm_assign)
2641 err = snd_hda_create_dig_out_ctls(codec,
2642 0, spec->cvt_nids[0],
2643 HDA_PCM_TYPE_HDMI);
2644 else {
2645 struct hdmi_spec_per_pin *per_pin =
2646 get_pin(spec, pcm_idx);
2647 err = snd_hda_create_dig_out_ctls(codec,
2648 per_pin->pin_nid,
2649 per_pin->mux_nids[0],
2650 HDA_PCM_TYPE_HDMI);
2651 }
2652 if (err < 0)
2653 return err;
2654 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangfb087ea2016-02-23 16:33:37 +08002655
2656 /* add control for ELD Bytes */
2657 err = hdmi_create_eld_ctl(codec, pcm_idx,
2658 get_pcm_rec(spec, pcm_idx)->device);
2659 if (err < 0)
2660 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002661 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002662
Stephen Warren384a48d2011-06-01 11:14:21 -06002663 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002664 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002665
Takashi Iwai82b1d732011-12-20 15:53:07 +01002666 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002667 }
2668
Takashi Iwaid45e6882012-07-31 11:36:00 +02002669 /* add channel maps */
Libin Yang022f3442016-02-03 10:48:34 +08002670 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002671 struct hda_pcm *pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002672 struct snd_pcm_chmap *chmap;
2673 struct snd_kcontrol *kctl;
2674 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002675
Libin Yang022f3442016-02-03 10:48:34 +08002676 pcm = get_pcm_rec(spec, pcm_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002677 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002678 break;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002679 err = snd_pcm_add_chmap_ctls(pcm->pcm,
Takashi Iwaid45e6882012-07-31 11:36:00 +02002680 SNDRV_PCM_STREAM_PLAYBACK,
Libin Yang022f3442016-02-03 10:48:34 +08002681 NULL, 0, pcm_idx, &chmap);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002682 if (err < 0)
2683 return err;
2684 /* override handlers */
2685 chmap->private_data = codec;
2686 kctl = chmap->kctl;
2687 for (i = 0; i < kctl->count; i++)
2688 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2689 kctl->info = hdmi_chmap_ctl_info;
2690 kctl->get = hdmi_chmap_ctl_get;
2691 kctl->put = hdmi_chmap_ctl_put;
2692 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2693 }
2694
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002695 return 0;
2696}
2697
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002698static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2699{
2700 struct hdmi_spec *spec = codec->spec;
2701 int pin_idx;
2702
2703 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002704 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002705
2706 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002707 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002708 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002709 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002710 }
2711 return 0;
2712}
2713
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002714static int generic_hdmi_init(struct hda_codec *codec)
2715{
2716 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002717 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002718
Stephen Warren384a48d2011-06-01 11:14:21 -06002719 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002720 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002721 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002722
2723 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002724 if (!codec_has_acomp(codec))
2725 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2726 codec->jackpoll_interval > 0 ?
2727 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002728 }
2729 return 0;
2730}
2731
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002732static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2733{
2734 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2735 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002736}
2737
2738static void hdmi_array_free(struct hdmi_spec *spec)
2739{
2740 snd_array_free(&spec->pins);
2741 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002742}
2743
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002744static void generic_hdmi_free(struct hda_codec *codec)
2745{
2746 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002747 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002748
Takashi Iwai66032492015-12-01 16:49:35 +01002749 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002750 snd_hdac_i915_register_notifier(NULL);
2751
Stephen Warren384a48d2011-06-01 11:14:21 -06002752 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002753 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002754 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002755 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002756 }
2757
2758 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2759 if (spec->pcm_rec[pcm_idx].jack == NULL)
2760 continue;
2761 if (spec->dyn_pcm_assign)
2762 snd_device_free(codec->card,
2763 spec->pcm_rec[pcm_idx].jack);
2764 else
2765 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002766 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002767
Takashi Iwai55913112015-12-10 13:03:29 +01002768 if (spec->i915_bound)
2769 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002770 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002771 kfree(spec);
2772}
2773
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002774#ifdef CONFIG_PM
2775static int generic_hdmi_resume(struct hda_codec *codec)
2776{
2777 struct hdmi_spec *spec = codec->spec;
2778 int pin_idx;
2779
Pierre Ossmana2833682014-06-18 21:48:09 +02002780 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002781 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002782
2783 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2784 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2785 hdmi_present_sense(per_pin, 1);
2786 }
2787 return 0;
2788}
2789#endif
2790
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002791static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002792 .init = generic_hdmi_init,
2793 .free = generic_hdmi_free,
2794 .build_pcms = generic_hdmi_build_pcms,
2795 .build_controls = generic_hdmi_build_controls,
2796 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002797#ifdef CONFIG_PM
2798 .resume = generic_hdmi_resume,
2799#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002800};
2801
Anssi Hannula307229d2013-10-24 21:10:34 +03002802static const struct hdmi_ops generic_standard_hdmi_ops = {
2803 .pin_get_eld = snd_hdmi_get_eld,
2804 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2805 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2806 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2807 .pin_hbr_setup = hdmi_pin_hbr_setup,
2808 .setup_stream = hdmi_setup_stream,
2809 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2810 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2811};
2812
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002813
2814static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2815 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002816{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002817 struct hdmi_spec *spec = codec->spec;
2818 hda_nid_t conns[4];
2819 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002820
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002821 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2822 if (nconns == spec->num_cvts &&
2823 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002824 return;
2825
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002826 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002827 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002828 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002829}
2830
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002831#define INTEL_VENDOR_NID 0x08
2832#define INTEL_GET_VENDOR_VERB 0xf81
2833#define INTEL_SET_VENDOR_VERB 0x781
2834#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2835#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2836
2837static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002838 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002839{
2840 unsigned int vendor_param;
2841
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002842 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2843 INTEL_GET_VENDOR_VERB, 0);
2844 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2845 return;
2846
2847 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2848 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2849 INTEL_SET_VENDOR_VERB, vendor_param);
2850 if (vendor_param == -1)
2851 return;
2852
Takashi Iwai17df3f52013-05-08 08:09:34 +02002853 if (update_tree)
2854 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002855}
2856
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002857static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2858{
2859 unsigned int vendor_param;
2860
2861 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2862 INTEL_GET_VENDOR_VERB, 0);
2863 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2864 return;
2865
2866 /* enable DP1.2 mode */
2867 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002868 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002869 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2870 INTEL_SET_VENDOR_VERB, vendor_param);
2871}
2872
Takashi Iwai17df3f52013-05-08 08:09:34 +02002873/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2874 * Otherwise you may get severe h/w communication errors.
2875 */
2876static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2877 unsigned int power_state)
2878{
2879 if (power_state == AC_PWRST_D0) {
2880 intel_haswell_enable_all_pins(codec, false);
2881 intel_haswell_fixup_enable_dp12(codec);
2882 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002883
Takashi Iwai17df3f52013-05-08 08:09:34 +02002884 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2885 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2886}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002887
David Henningssonf0675d42015-09-03 11:51:34 +02002888static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002889{
2890 struct hda_codec *codec = audio_ptr;
2891 int pin_nid = port + 0x04;
2892
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002893 /* skip notification during system suspend (but not in runtime PM);
2894 * the state will be updated at resume
2895 */
2896 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2897 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002898 /* ditto during suspend/resume process itself */
2899 if (atomic_read(&(codec)->core.in_pm))
2900 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002901
David Henningsson25adc132015-08-19 10:48:58 +02002902 check_presence_and_report(codec, pin_nid);
2903}
2904
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002905static int patch_generic_hdmi(struct hda_codec *codec)
2906{
2907 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002908
2909 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2910 if (spec == NULL)
2911 return -ENOMEM;
2912
Anssi Hannula307229d2013-10-24 21:10:34 +03002913 spec->ops = generic_standard_hdmi_ops;
Libin Yang42b29872015-12-16 13:42:42 +08002914 mutex_init(&spec->pcm_lock);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002915 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002916 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002917
Takashi Iwai55913112015-12-10 13:03:29 +01002918 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2919 if (!codec_has_acomp(codec) &&
2920 (codec->core.vendor_id >> 16) == 0x8086)
2921 if (!snd_hdac_i915_init(&codec->bus->core))
2922 spec->i915_bound = true;
2923
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002924 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002925 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002926 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002927 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002928
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002929 /* For Valleyview/Cherryview, only the display codec is in the display
2930 * power well and can use link_power ops to request/release the power.
2931 * For Haswell/Broadwell, the controller is also in the power well and
2932 * can cover the codec power request, and so need not set this flag.
2933 * For previous platforms, there is no such power well feature.
2934 */
Lu, Hanff9d8852015-11-19 23:25:13 +08002935 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2936 is_broxton(codec))
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002937 codec->core.link_power_control = 1;
2938
Takashi Iwai66032492015-12-01 16:49:35 +01002939 if (codec_has_acomp(codec)) {
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002940 codec->depop_delay = 0;
David Henningsson25adc132015-08-19 10:48:58 +02002941 spec->i915_audio_ops.audio_ptr = codec;
2942 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2943 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2944 }
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002945
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002946 if (hdmi_parse_codec(codec) < 0) {
Takashi Iwai55913112015-12-10 13:03:29 +01002947 if (spec->i915_bound)
2948 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002949 codec->spec = NULL;
2950 kfree(spec);
2951 return -EINVAL;
2952 }
2953 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002954 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002955 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002956 codec->dp_mst = true;
2957 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002958
Lu, Han2377c3c2015-06-09 16:50:38 +08002959 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2960 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2961 codec->auto_runtime_pm = 1;
2962
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002963 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002964
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002965 init_channel_allocations();
2966
Libin Yang25e4abb2016-01-12 11:13:27 +08002967 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002968 return 0;
2969}
2970
2971/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002972 * Shared non-generic implementations
2973 */
2974
2975static int simple_playback_build_pcms(struct hda_codec *codec)
2976{
2977 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002978 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002979 unsigned int chans;
2980 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002981 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002982
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002983 per_cvt = get_cvt(spec, 0);
2984 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002985 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002986
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002987 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002988 if (!info)
2989 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002990 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002991 info->pcm_type = HDA_PCM_TYPE_HDMI;
2992 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2993 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002994 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002995 if (pstr->channels_max <= 2 && chans && chans <= 16)
2996 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002997
2998 return 0;
2999}
3000
Takashi Iwai4b6ace92012-06-15 11:53:32 +02003001/* unsolicited event for jack sensing */
3002static void simple_hdmi_unsol_event(struct hda_codec *codec,
3003 unsigned int res)
3004{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02003005 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02003006 snd_hda_jack_report_sync(codec);
3007}
3008
3009/* generic_hdmi_build_jack can be used for simple_hdmi, too,
3010 * as long as spec->pins[] is set correctly
3011 */
3012#define simple_hdmi_build_jack generic_hdmi_build_jack
3013
Stephen Warren3aaf8982011-06-01 11:14:19 -06003014static int simple_playback_build_controls(struct hda_codec *codec)
3015{
3016 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003017 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003018 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06003019
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003020 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02003021 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
3022 per_cvt->cvt_nid,
3023 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003024 if (err < 0)
3025 return err;
3026 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06003027}
3028
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003029static int simple_playback_init(struct hda_codec *codec)
3030{
3031 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003032 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
3033 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003034
Takashi Iwai8ceb3322012-06-21 08:23:27 +02003035 snd_hda_codec_write(codec, pin, 0,
3036 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3037 /* some codecs require to unmute the pin */
3038 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3039 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3040 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02003041 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02003042 return 0;
3043}
3044
Stephen Warren3aaf8982011-06-01 11:14:19 -06003045static void simple_playback_free(struct hda_codec *codec)
3046{
3047 struct hdmi_spec *spec = codec->spec;
3048
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003049 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06003050 kfree(spec);
3051}
3052
3053/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003054 * Nvidia specific implementations
3055 */
3056
3057#define Nv_VERB_SET_Channel_Allocation 0xF79
3058#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
3059#define Nv_VERB_SET_Audio_Protection_On 0xF98
3060#define Nv_VERB_SET_Audio_Protection_Off 0xF99
3061
3062#define nvhdmi_master_con_nid_7x 0x04
3063#define nvhdmi_master_pin_nid_7x 0x05
3064
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003065static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003066 /*front, rear, clfe, rear_surr */
3067 0x6, 0x8, 0xa, 0xc,
3068};
3069
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003070static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3071 /* set audio protect on */
3072 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3073 /* enable digital output on pin widget */
3074 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3075 {} /* terminator */
3076};
3077
3078static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003079 /* set audio protect on */
3080 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3081 /* enable digital output on pin widget */
3082 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3083 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3084 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3085 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3086 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3087 {} /* terminator */
3088};
3089
3090#ifdef LIMITED_RATE_FMT_SUPPORT
3091/* support only the safe format and rate */
3092#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3093#define SUPPORTED_MAXBPS 16
3094#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3095#else
3096/* support all rates and formats */
3097#define SUPPORTED_RATES \
3098 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3099 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3100 SNDRV_PCM_RATE_192000)
3101#define SUPPORTED_MAXBPS 24
3102#define SUPPORTED_FORMATS \
3103 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3104#endif
3105
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003106static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003107{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003108 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3109 return 0;
3110}
3111
3112static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3113{
3114 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003115 return 0;
3116}
3117
Nitin Daga393004b2011-01-10 21:49:31 +05303118static unsigned int channels_2_6_8[] = {
3119 2, 6, 8
3120};
3121
3122static unsigned int channels_2_8[] = {
3123 2, 8
3124};
3125
3126static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3127 .count = ARRAY_SIZE(channels_2_6_8),
3128 .list = channels_2_6_8,
3129 .mask = 0,
3130};
3131
3132static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3133 .count = ARRAY_SIZE(channels_2_8),
3134 .list = channels_2_8,
3135 .mask = 0,
3136};
3137
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003138static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3139 struct hda_codec *codec,
3140 struct snd_pcm_substream *substream)
3141{
3142 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05303143 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3144
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003145 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05303146 case 0x10de0002:
3147 case 0x10de0003:
3148 case 0x10de0005:
3149 case 0x10de0006:
3150 hw_constraints_channels = &hw_constraints_2_8_channels;
3151 break;
3152 case 0x10de0007:
3153 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3154 break;
3155 default:
3156 break;
3157 }
3158
3159 if (hw_constraints_channels != NULL) {
3160 snd_pcm_hw_constraint_list(substream->runtime, 0,
3161 SNDRV_PCM_HW_PARAM_CHANNELS,
3162 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01003163 } else {
3164 snd_pcm_hw_constraint_step(substream->runtime, 0,
3165 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05303166 }
3167
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003168 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3169}
3170
3171static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3172 struct hda_codec *codec,
3173 struct snd_pcm_substream *substream)
3174{
3175 struct hdmi_spec *spec = codec->spec;
3176 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3177}
3178
3179static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3180 struct hda_codec *codec,
3181 unsigned int stream_tag,
3182 unsigned int format,
3183 struct snd_pcm_substream *substream)
3184{
3185 struct hdmi_spec *spec = codec->spec;
3186 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3187 stream_tag, format, substream);
3188}
3189
Takashi Iwaid0b12522012-06-15 14:34:42 +02003190static const struct hda_pcm_stream simple_pcm_playback = {
3191 .substreams = 1,
3192 .channels_min = 2,
3193 .channels_max = 2,
3194 .ops = {
3195 .open = simple_playback_pcm_open,
3196 .close = simple_playback_pcm_close,
3197 .prepare = simple_playback_pcm_prepare
3198 },
3199};
3200
3201static const struct hda_codec_ops simple_hdmi_patch_ops = {
3202 .build_controls = simple_playback_build_controls,
3203 .build_pcms = simple_playback_build_pcms,
3204 .init = simple_playback_init,
3205 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02003206 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02003207};
3208
3209static int patch_simple_hdmi(struct hda_codec *codec,
3210 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3211{
3212 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003213 struct hdmi_spec_per_cvt *per_cvt;
3214 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003215
3216 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3217 if (!spec)
3218 return -ENOMEM;
3219
3220 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003221 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02003222
3223 spec->multiout.num_dacs = 0; /* no analog */
3224 spec->multiout.max_channels = 2;
3225 spec->multiout.dig_out_nid = cvt_nid;
3226 spec->num_cvts = 1;
3227 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003228 per_pin = snd_array_new(&spec->pins);
3229 per_cvt = snd_array_new(&spec->cvts);
3230 if (!per_pin || !per_cvt) {
3231 simple_playback_free(codec);
3232 return -ENOMEM;
3233 }
3234 per_cvt->cvt_nid = cvt_nid;
3235 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003236 spec->pcm_playback = simple_pcm_playback;
3237
3238 codec->patch_ops = simple_hdmi_patch_ops;
3239
3240 return 0;
3241}
3242
Aaron Plattner1f348522011-04-06 17:19:04 -07003243static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3244 int channels)
3245{
3246 unsigned int chanmask;
3247 int chan = channels ? (channels - 1) : 1;
3248
3249 switch (channels) {
3250 default:
3251 case 0:
3252 case 2:
3253 chanmask = 0x00;
3254 break;
3255 case 4:
3256 chanmask = 0x08;
3257 break;
3258 case 6:
3259 chanmask = 0x0b;
3260 break;
3261 case 8:
3262 chanmask = 0x13;
3263 break;
3264 }
3265
3266 /* Set the audio infoframe channel allocation and checksum fields. The
3267 * channel count is computed implicitly by the hardware. */
3268 snd_hda_codec_write(codec, 0x1, 0,
3269 Nv_VERB_SET_Channel_Allocation, chanmask);
3270
3271 snd_hda_codec_write(codec, 0x1, 0,
3272 Nv_VERB_SET_Info_Frame_Checksum,
3273 (0x71 - chan - chanmask));
3274}
3275
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003276static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3277 struct hda_codec *codec,
3278 struct snd_pcm_substream *substream)
3279{
3280 struct hdmi_spec *spec = codec->spec;
3281 int i;
3282
3283 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3284 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3285 for (i = 0; i < 4; i++) {
3286 /* set the stream id */
3287 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3288 AC_VERB_SET_CHANNEL_STREAMID, 0);
3289 /* set the stream format */
3290 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3291 AC_VERB_SET_STREAM_FORMAT, 0);
3292 }
3293
Aaron Plattner1f348522011-04-06 17:19:04 -07003294 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3295 * streams are disabled. */
3296 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3297
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003298 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3299}
3300
3301static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3302 struct hda_codec *codec,
3303 unsigned int stream_tag,
3304 unsigned int format,
3305 struct snd_pcm_substream *substream)
3306{
3307 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003308 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003309 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003310 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003311 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003312 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003313
3314 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003315 per_cvt = get_cvt(spec, 0);
3316 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003317
3318 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003319
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003320 dataDCC2 = 0x2;
3321
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003322 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003323 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003324 snd_hda_codec_write(codec,
3325 nvhdmi_master_con_nid_7x,
3326 0,
3327 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003328 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003329
3330 /* set the stream id */
3331 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3332 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3333
3334 /* set the stream format */
3335 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3336 AC_VERB_SET_STREAM_FORMAT, format);
3337
3338 /* turn on again (if needed) */
3339 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003340 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003341 snd_hda_codec_write(codec,
3342 nvhdmi_master_con_nid_7x,
3343 0,
3344 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003345 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003346 snd_hda_codec_write(codec,
3347 nvhdmi_master_con_nid_7x,
3348 0,
3349 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3350 }
3351
3352 for (i = 0; i < 4; i++) {
3353 if (chs == 2)
3354 channel_id = 0;
3355 else
3356 channel_id = i * 2;
3357
3358 /* turn off SPDIF once;
3359 *otherwise the IEC958 bits won't be updated
3360 */
3361 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003362 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003363 snd_hda_codec_write(codec,
3364 nvhdmi_con_nids_7x[i],
3365 0,
3366 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003367 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003368 /* set the stream id */
3369 snd_hda_codec_write(codec,
3370 nvhdmi_con_nids_7x[i],
3371 0,
3372 AC_VERB_SET_CHANNEL_STREAMID,
3373 (stream_tag << 4) | channel_id);
3374 /* set the stream format */
3375 snd_hda_codec_write(codec,
3376 nvhdmi_con_nids_7x[i],
3377 0,
3378 AC_VERB_SET_STREAM_FORMAT,
3379 format);
3380 /* turn on again (if needed) */
3381 /* enable and set the channel status audio/data flag */
3382 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003383 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003384 snd_hda_codec_write(codec,
3385 nvhdmi_con_nids_7x[i],
3386 0,
3387 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003388 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003389 snd_hda_codec_write(codec,
3390 nvhdmi_con_nids_7x[i],
3391 0,
3392 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3393 }
3394 }
3395
Aaron Plattner1f348522011-04-06 17:19:04 -07003396 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003397
3398 mutex_unlock(&codec->spdif_mutex);
3399 return 0;
3400}
3401
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003402static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003403 .substreams = 1,
3404 .channels_min = 2,
3405 .channels_max = 8,
3406 .nid = nvhdmi_master_con_nid_7x,
3407 .rates = SUPPORTED_RATES,
3408 .maxbps = SUPPORTED_MAXBPS,
3409 .formats = SUPPORTED_FORMATS,
3410 .ops = {
3411 .open = simple_playback_pcm_open,
3412 .close = nvhdmi_8ch_7x_pcm_close,
3413 .prepare = nvhdmi_8ch_7x_pcm_prepare
3414 },
3415};
3416
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003417static int patch_nvhdmi_2ch(struct hda_codec *codec)
3418{
3419 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003420 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3421 nvhdmi_master_pin_nid_7x);
3422 if (err < 0)
3423 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003424
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003425 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003426 /* override the PCM rates, etc, as the codec doesn't give full list */
3427 spec = codec->spec;
3428 spec->pcm_playback.rates = SUPPORTED_RATES;
3429 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3430 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003431 return 0;
3432}
3433
Takashi Iwai53775b02012-08-01 12:17:41 +02003434static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3435{
3436 struct hdmi_spec *spec = codec->spec;
3437 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003438 if (!err) {
3439 struct hda_pcm *info = get_pcm_rec(spec, 0);
3440 info->own_chmap = true;
3441 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003442 return err;
3443}
3444
3445static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3446{
3447 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003448 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003449 struct snd_pcm_chmap *chmap;
3450 int err;
3451
3452 err = simple_playback_build_controls(codec);
3453 if (err < 0)
3454 return err;
3455
3456 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003457 info = get_pcm_rec(spec, 0);
3458 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003459 SNDRV_PCM_STREAM_PLAYBACK,
3460 snd_pcm_alt_chmaps, 8, 0, &chmap);
3461 if (err < 0)
3462 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003463 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003464 case 0x10de0002:
3465 case 0x10de0003:
3466 case 0x10de0005:
3467 case 0x10de0006:
3468 chmap->channel_mask = (1U << 2) | (1U << 8);
3469 break;
3470 case 0x10de0007:
3471 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3472 }
3473 return 0;
3474}
3475
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003476static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3477{
3478 struct hdmi_spec *spec;
3479 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003480 if (err < 0)
3481 return err;
3482 spec = codec->spec;
3483 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003484 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003485 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003486 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3487 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003488
3489 /* Initialize the audio infoframe channel mask and checksum to something
3490 * valid */
3491 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3492
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003493 return 0;
3494}
3495
3496/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003497 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3498 * - 0x10de0015
3499 * - 0x10de0040
3500 */
3501static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3502 int channels)
3503{
3504 if (cap->ca_index == 0x00 && channels == 2)
3505 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3506
3507 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3508}
3509
3510static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3511{
3512 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3513 return -EINVAL;
3514
3515 return 0;
3516}
3517
3518static int patch_nvhdmi(struct hda_codec *codec)
3519{
3520 struct hdmi_spec *spec;
3521 int err;
3522
3523 err = patch_generic_hdmi(codec);
3524 if (err)
3525 return err;
3526
3527 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003528 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003529
3530 spec->ops.chmap_cea_alloc_validate_get_type =
3531 nvhdmi_chmap_cea_alloc_validate_get_type;
3532 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3533
3534 return 0;
3535}
3536
3537/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003538 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3539 * accessed using vendor-defined verbs. These registers can be used for
3540 * interoperability between the HDA and HDMI drivers.
3541 */
3542
3543/* Audio Function Group node */
3544#define NVIDIA_AFG_NID 0x01
3545
3546/*
3547 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3548 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3549 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3550 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3551 * additional bit (at position 30) to signal the validity of the format.
3552 *
3553 * | 31 | 30 | 29 16 | 15 0 |
3554 * +---------+-------+--------+--------+
3555 * | TRIGGER | VALID | UNUSED | FORMAT |
3556 * +-----------------------------------|
3557 *
3558 * Note that for the trigger bit to take effect it needs to change value
3559 * (i.e. it needs to be toggled).
3560 */
3561#define NVIDIA_GET_SCRATCH0 0xfa6
3562#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3563#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3564#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3565#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3566#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3567#define NVIDIA_SCRATCH_VALID (1 << 6)
3568
3569#define NVIDIA_GET_SCRATCH1 0xfab
3570#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3571#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3572#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3573#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3574
3575/*
3576 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3577 * the format is invalidated so that the HDMI codec can be disabled.
3578 */
3579static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3580{
3581 unsigned int value;
3582
3583 /* bits [31:30] contain the trigger and valid bits */
3584 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3585 NVIDIA_GET_SCRATCH0, 0);
3586 value = (value >> 24) & 0xff;
3587
3588 /* bits [15:0] are used to store the HDA format */
3589 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3590 NVIDIA_SET_SCRATCH0_BYTE0,
3591 (format >> 0) & 0xff);
3592 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3593 NVIDIA_SET_SCRATCH0_BYTE1,
3594 (format >> 8) & 0xff);
3595
3596 /* bits [16:24] are unused */
3597 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3598 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3599
3600 /*
3601 * Bit 30 signals that the data is valid and hence that HDMI audio can
3602 * be enabled.
3603 */
3604 if (format == 0)
3605 value &= ~NVIDIA_SCRATCH_VALID;
3606 else
3607 value |= NVIDIA_SCRATCH_VALID;
3608
3609 /*
3610 * Whenever the trigger bit is toggled, an interrupt is raised in the
3611 * HDMI codec. The HDMI driver will use that as trigger to update its
3612 * configuration.
3613 */
3614 value ^= NVIDIA_SCRATCH_TRIGGER;
3615
3616 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3617 NVIDIA_SET_SCRATCH0_BYTE3, value);
3618}
3619
3620static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3621 struct hda_codec *codec,
3622 unsigned int stream_tag,
3623 unsigned int format,
3624 struct snd_pcm_substream *substream)
3625{
3626 int err;
3627
3628 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3629 format, substream);
3630 if (err < 0)
3631 return err;
3632
3633 /* notify the HDMI codec of the format change */
3634 tegra_hdmi_set_format(codec, format);
3635
3636 return 0;
3637}
3638
3639static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3640 struct hda_codec *codec,
3641 struct snd_pcm_substream *substream)
3642{
3643 /* invalidate the format in the HDMI codec */
3644 tegra_hdmi_set_format(codec, 0);
3645
3646 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3647}
3648
3649static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3650{
3651 struct hdmi_spec *spec = codec->spec;
3652 unsigned int i;
3653
3654 for (i = 0; i < spec->num_pins; i++) {
3655 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3656
3657 if (pcm->pcm_type == type)
3658 return pcm;
3659 }
3660
3661 return NULL;
3662}
3663
3664static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3665{
3666 struct hda_pcm_stream *stream;
3667 struct hda_pcm *pcm;
3668 int err;
3669
3670 err = generic_hdmi_build_pcms(codec);
3671 if (err < 0)
3672 return err;
3673
3674 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3675 if (!pcm)
3676 return -ENODEV;
3677
3678 /*
3679 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3680 * codec about format changes.
3681 */
3682 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3683 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3684 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3685
3686 return 0;
3687}
3688
3689static int patch_tegra_hdmi(struct hda_codec *codec)
3690{
3691 int err;
3692
3693 err = patch_generic_hdmi(codec);
3694 if (err)
3695 return err;
3696
3697 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3698
3699 return 0;
3700}
3701
3702/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003703 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003704 */
3705
Anssi Hannula5a6135842013-10-24 21:10:35 +03003706#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003707 ((codec)->core.vendor_id == 0x1002aa01 && \
3708 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003709#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003710
Anssi Hannula5a6135842013-10-24 21:10:35 +03003711/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3712#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3713#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3714#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3715#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3716#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3717#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003718#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003719#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3720#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3721#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3722#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3723#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3724#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3725#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3726#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3727#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3728#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3729#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003730#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003731#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3732#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3733#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3734#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3735#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3736
Anssi Hannula84d69e72013-10-24 21:10:38 +03003737/* AMD specific HDA cvt verbs */
3738#define ATI_VERB_SET_RAMP_RATE 0x770
3739#define ATI_VERB_GET_RAMP_RATE 0xf70
3740
Anssi Hannula5a6135842013-10-24 21:10:35 +03003741#define ATI_OUT_ENABLE 0x1
3742
3743#define ATI_MULTICHANNEL_MODE_PAIRED 0
3744#define ATI_MULTICHANNEL_MODE_SINGLE 1
3745
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003746#define ATI_HBR_CAPABLE 0x01
3747#define ATI_HBR_ENABLE 0x10
3748
Anssi Hannula89250f82013-10-24 21:10:36 +03003749static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3750 unsigned char *buf, int *eld_size)
3751{
3752 /* call hda_eld.c ATI/AMD-specific function */
3753 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3754 is_amdhdmi_rev3_or_later(codec));
3755}
3756
Anssi Hannula5a6135842013-10-24 21:10:35 +03003757static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3758 int active_channels, int conn_type)
3759{
3760 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3761}
3762
3763static int atihdmi_paired_swap_fc_lfe(int pos)
3764{
3765 /*
3766 * ATI/AMD have automatic FC/LFE swap built-in
3767 * when in pairwise mapping mode.
3768 */
3769
3770 switch (pos) {
3771 /* see channel_allocations[].speakers[] */
3772 case 2: return 3;
3773 case 3: return 2;
3774 default: break;
3775 }
3776
3777 return pos;
3778}
3779
3780static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3781{
3782 struct cea_channel_speaker_allocation *cap;
3783 int i, j;
3784
3785 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3786
3787 cap = &channel_allocations[get_channel_allocation_order(ca)];
3788 for (i = 0; i < chs; ++i) {
3789 int mask = to_spk_mask(map[i]);
3790 bool ok = false;
3791 bool companion_ok = false;
3792
3793 if (!mask)
3794 continue;
3795
3796 for (j = 0 + i % 2; j < 8; j += 2) {
3797 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3798 if (cap->speakers[chan_idx] == mask) {
3799 /* channel is in a supported position */
3800 ok = true;
3801
3802 if (i % 2 == 0 && i + 1 < chs) {
3803 /* even channel, check the odd companion */
3804 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3805 int comp_mask_req = to_spk_mask(map[i+1]);
3806 int comp_mask_act = cap->speakers[comp_chan_idx];
3807
3808 if (comp_mask_req == comp_mask_act)
3809 companion_ok = true;
3810 else
3811 return -EINVAL;
3812 }
3813 break;
3814 }
3815 }
3816
3817 if (!ok)
3818 return -EINVAL;
3819
3820 if (companion_ok)
3821 i++; /* companion channel already checked */
3822 }
3823
3824 return 0;
3825}
3826
3827static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3828 int hdmi_slot, int stream_channel)
3829{
3830 int verb;
3831 int ati_channel_setup = 0;
3832
3833 if (hdmi_slot > 7)
3834 return -EINVAL;
3835
3836 if (!has_amd_full_remap_support(codec)) {
3837 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3838
3839 /* In case this is an odd slot but without stream channel, do not
3840 * disable the slot since the corresponding even slot could have a
3841 * channel. In case neither have a channel, the slot pair will be
3842 * disabled when this function is called for the even slot. */
3843 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3844 return 0;
3845
3846 hdmi_slot -= hdmi_slot % 2;
3847
3848 if (stream_channel != 0xf)
3849 stream_channel -= stream_channel % 2;
3850 }
3851
3852 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3853
3854 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3855
3856 if (stream_channel != 0xf)
3857 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3858
3859 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3860}
3861
3862static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3863 int asp_slot)
3864{
3865 bool was_odd = false;
3866 int ati_asp_slot = asp_slot;
3867 int verb;
3868 int ati_channel_setup;
3869
3870 if (asp_slot > 7)
3871 return -EINVAL;
3872
3873 if (!has_amd_full_remap_support(codec)) {
3874 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3875 if (ati_asp_slot % 2 != 0) {
3876 ati_asp_slot -= 1;
3877 was_odd = true;
3878 }
3879 }
3880
3881 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3882
3883 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3884
3885 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3886 return 0xf;
3887
3888 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3889}
3890
3891static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3892 int channels)
3893{
3894 int c;
3895
3896 /*
3897 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3898 * we need to take that into account (a single channel may take 2
3899 * channel slots if we need to carry a silent channel next to it).
3900 * On Rev3+ AMD codecs this function is not used.
3901 */
3902 int chanpairs = 0;
3903
3904 /* We only produce even-numbered channel count TLVs */
3905 if ((channels % 2) != 0)
3906 return -1;
3907
3908 for (c = 0; c < 7; c += 2) {
3909 if (cap->speakers[c] || cap->speakers[c+1])
3910 chanpairs++;
3911 }
3912
3913 if (chanpairs * 2 != channels)
3914 return -1;
3915
3916 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3917}
3918
3919static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3920 unsigned int *chmap, int channels)
3921{
3922 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3923 int count = 0;
3924 int c;
3925
3926 for (c = 7; c >= 0; c--) {
3927 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3928 int spk = cap->speakers[chan];
3929 if (!spk) {
3930 /* add N/A channel if the companion channel is occupied */
3931 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3932 chmap[count++] = SNDRV_CHMAP_NA;
3933
3934 continue;
3935 }
3936
3937 chmap[count++] = spk_to_chmap(spk);
3938 }
3939
3940 WARN_ON(count != channels);
3941}
3942
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003943static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3944 bool hbr)
3945{
3946 int hbr_ctl, hbr_ctl_new;
3947
3948 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003949 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003950 if (hbr)
3951 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3952 else
3953 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3954
Takashi Iwai4e76a882014-02-25 12:21:03 +01003955 codec_dbg(codec,
3956 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003957 pin_nid,
3958 hbr_ctl == hbr_ctl_new ? "" : "new-",
3959 hbr_ctl_new);
3960
3961 if (hbr_ctl != hbr_ctl_new)
3962 snd_hda_codec_write(codec, pin_nid, 0,
3963 ATI_VERB_SET_HBR_CONTROL,
3964 hbr_ctl_new);
3965
3966 } else if (hbr)
3967 return -EINVAL;
3968
3969 return 0;
3970}
3971
Anssi Hannula84d69e72013-10-24 21:10:38 +03003972static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3973 hda_nid_t pin_nid, u32 stream_tag, int format)
3974{
3975
3976 if (is_amdhdmi_rev3_or_later(codec)) {
3977 int ramp_rate = 180; /* default as per AMD spec */
3978 /* disable ramp-up/down for non-pcm as per AMD spec */
3979 if (format & AC_FMT_TYPE_NON_PCM)
3980 ramp_rate = 0;
3981
3982 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3983 }
3984
3985 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3986}
3987
3988
Anssi Hannula5a6135842013-10-24 21:10:35 +03003989static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003990{
3991 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003992 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003993
Anssi Hannula5a6135842013-10-24 21:10:35 +03003994 err = generic_hdmi_init(codec);
3995
3996 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003997 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003998
3999 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
4000 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
4001
4002 /* make sure downmix information in infoframe is zero */
4003 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
4004
4005 /* enable channel-wise remap mode if supported */
4006 if (has_amd_full_remap_support(codec))
4007 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
4008 ATI_VERB_SET_MULTICHANNEL_MODE,
4009 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004010 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03004011
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004012 return 0;
4013}
4014
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004015static int patch_atihdmi(struct hda_codec *codec)
4016{
4017 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004018 struct hdmi_spec_per_cvt *per_cvt;
4019 int err, cvt_idx;
4020
4021 err = patch_generic_hdmi(codec);
4022
4023 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02004024 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004025
4026 codec->patch_ops.init = atihdmi_init;
4027
Takashi Iwaid0b12522012-06-15 14:34:42 +02004028 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004029
Anssi Hannula89250f82013-10-24 21:10:36 +03004030 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004031 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4032 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4033 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03004034 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03004035 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03004036
4037 if (!has_amd_full_remap_support(codec)) {
4038 /* override to ATI/AMD-specific versions with pairwise mapping */
4039 spec->ops.chmap_cea_alloc_validate_get_type =
4040 atihdmi_paired_chmap_cea_alloc_validate_get_type;
4041 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
4042 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
4043 }
4044
4045 /* ATI/AMD converters do not advertise all of their capabilities */
4046 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4047 per_cvt = get_cvt(spec, cvt_idx);
4048 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4049 per_cvt->rates |= SUPPORTED_RATES;
4050 per_cvt->formats |= SUPPORTED_FORMATS;
4051 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4052 }
4053
4054 spec->channels_max = max(spec->channels_max, 8u);
4055
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004056 return 0;
4057}
4058
Annie Liu3de5ff82012-06-08 19:18:42 +08004059/* VIA HDMI Implementation */
4060#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4061#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4062
Annie Liu3de5ff82012-06-08 19:18:42 +08004063static int patch_via_hdmi(struct hda_codec *codec)
4064{
Takashi Iwai250e41a2012-06-15 14:40:21 +02004065 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08004066}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004067
4068/*
4069 * patch entries
4070 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004071static const struct hda_device_id snd_hda_id_hdmi[] = {
4072HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4073HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4074HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4075HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4076HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4077HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4078HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4079HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4080HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4081HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4082HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4083HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4084HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4085HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4086HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4087HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4088HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4089HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4090HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4091HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4092HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4093HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4094HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01004095/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004096HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4097HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4098HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4099HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4100HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4101HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4102HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4103HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4104HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4105HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4106HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4107HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4108HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4109HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4110HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4111HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4112HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4113HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4114HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4115HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4116HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
Aaron Plattner3ec622f2016-01-28 14:07:38 -08004117HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004118HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4119HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4120HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4121HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4122HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4123HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
4124HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4125HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4126HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4127HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
4128HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4129HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4130HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4131HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4132HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4133HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
Libin Yang91815d82016-01-14 14:09:00 +08004134HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004135HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4136HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4137HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4138HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004139/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004140HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004141{} /* terminator */
4142};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004143MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004144
4145MODULE_LICENSE("GPL");
4146MODULE_DESCRIPTION("HDMI HD-audio codec");
4147MODULE_ALIAS("snd-hda-codec-intelhdmi");
4148MODULE_ALIAS("snd-hda-codec-nvhdmi");
4149MODULE_ALIAS("snd-hda-codec-atihdmi");
4150
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004151static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004152 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004153};
4154
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004155module_hda_codec_driver(hdmi_driver);