blob: 275b68afb57520709a13e6e400aa90e8437b8e42 [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;
David Henningsson92c69e72013-02-19 16:11:26 +010089 struct snd_kcontrol *eld_ctl;
Libin Yang2bea2412016-01-12 11:13:26 +080090 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Libin Yanga76056f2015-12-16 16:48:15 +080091 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
Wu Fengguangc6e84532011-11-18 16:59:32 -060092 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020093 bool setup; /* the stream has been set up by prepare callback */
94 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020095 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020096 bool chmap_set; /* channel-map override by ALSA API? */
97 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080098#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +020099 struct snd_info_entry *proc_entry;
100#endif
Stephen Warren384a48d2011-06-01 11:14:21 -0600101};
102
Anssi Hannula307229d2013-10-24 21:10:34 +0300103struct cea_channel_speaker_allocation;
104
105/* operations used by generic code that can be overridden by patches */
106struct hdmi_ops {
107 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
108 unsigned char *buf, int *eld_size);
109
110 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
111 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
112 int asp_slot);
113 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
114 int asp_slot, int channel);
115
116 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
117 int ca, int active_channels, int conn_type);
118
119 /* enable/disable HBR (HD passthrough) */
120 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
121
122 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
123 hda_nid_t pin_nid, u32 stream_tag, int format);
124
125 /* Helpers for producing the channel map TLVs. These can be overridden
126 * for devices that have non-standard mapping requirements. */
127 int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
128 int channels);
129 void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
130 unsigned int *chmap, int channels);
131
132 /* check that the user-given chmap is supported */
133 int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
134};
135
Libin Yang2bea2412016-01-12 11:13:26 +0800136struct hdmi_pcm {
137 struct hda_pcm *pcm;
138 struct snd_jack *jack;
139};
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
Takashi Iwai4e76a882014-02-25 12:21:03 +0100440static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600441{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100442 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600443 int cvt_idx;
444
445 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100446 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600447 return cvt_idx;
448
Takashi Iwai4e76a882014-02-25 12:21:03 +0100449 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800450 return -EINVAL;
451}
452
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500453static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
454 struct snd_ctl_elem_info *uinfo)
455{
456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100457 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200458 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100459 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500460 int pin_idx;
461
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500462 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
463
464 pin_idx = kcontrol->private_value;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200465 per_pin = get_pin(spec, pin_idx);
466 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100467
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200468 mutex_lock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100469 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200470 mutex_unlock(&per_pin->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500471
472 return 0;
473}
474
475static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
476 struct snd_ctl_elem_value *ucontrol)
477{
478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100479 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200480 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100481 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500482 int pin_idx;
483
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500484 pin_idx = kcontrol->private_value;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200485 per_pin = get_pin(spec, pin_idx);
486 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500487
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200488 mutex_lock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100489 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200490 mutex_unlock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100491 snd_BUG();
492 return -EINVAL;
493 }
494
495 memset(ucontrol->value.bytes.data, 0,
496 ARRAY_SIZE(ucontrol->value.bytes.data));
497 if (eld->eld_valid)
498 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
499 eld->eld_size);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200500 mutex_unlock(&per_pin->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500501
502 return 0;
503}
504
505static struct snd_kcontrol_new eld_bytes_ctl = {
506 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
507 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
508 .name = "ELD",
509 .info = hdmi_eld_ctl_info,
510 .get = hdmi_eld_ctl_get,
511};
512
513static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
514 int device)
515{
516 struct snd_kcontrol *kctl;
517 struct hdmi_spec *spec = codec->spec;
518 int err;
519
520 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
521 if (!kctl)
522 return -ENOMEM;
523 kctl->private_value = pin_idx;
524 kctl->id.device = device;
525
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100526 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500527 if (err < 0)
528 return err;
529
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100530 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500531 return 0;
532}
533
Wu Fengguang079d88c2010-03-08 10:44:23 +0800534#ifdef BE_PARANOID
535static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
536 int *packet_index, int *byte_index)
537{
538 int val;
539
540 val = snd_hda_codec_read(codec, pin_nid, 0,
541 AC_VERB_GET_HDMI_DIP_INDEX, 0);
542
543 *packet_index = val >> 5;
544 *byte_index = val & 0x1f;
545}
546#endif
547
548static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
549 int packet_index, int byte_index)
550{
551 int val;
552
553 val = (packet_index << 5) | (byte_index & 0x1f);
554
555 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
556}
557
558static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
559 unsigned char val)
560{
561 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
562}
563
Stephen Warren384a48d2011-06-01 11:14:21 -0600564static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800565{
Stephen Warren75fae112014-01-30 11:52:16 -0700566 struct hdmi_spec *spec = codec->spec;
567 int pin_out;
568
Wu Fengguang079d88c2010-03-08 10:44:23 +0800569 /* Unmute */
570 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
571 snd_hda_codec_write(codec, pin_nid, 0,
572 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700573
574 if (spec->dyn_pin_out)
575 /* Disable pin out until stream is active */
576 pin_out = 0;
577 else
578 /* Enable pin out: some machines with GM965 gets broken output
579 * when the pin is disabled or changed while using with HDMI
580 */
581 pin_out = PIN_OUT;
582
Wu Fengguang079d88c2010-03-08 10:44:23 +0800583 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700584 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800585}
586
Stephen Warren384a48d2011-06-01 11:14:21 -0600587static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800588{
Stephen Warren384a48d2011-06-01 11:14:21 -0600589 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800590 AC_VERB_GET_CVT_CHAN_COUNT, 0);
591}
592
593static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600594 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800595{
Stephen Warren384a48d2011-06-01 11:14:21 -0600596 if (chs != hdmi_get_channel_count(codec, cvt_nid))
597 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800598 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
599}
600
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200601/*
602 * ELD proc files
603 */
604
Jie Yangcd6a6502015-05-27 19:45:45 +0800605#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200606static void print_eld_info(struct snd_info_entry *entry,
607 struct snd_info_buffer *buffer)
608{
609 struct hdmi_spec_per_pin *per_pin = entry->private_data;
610
611 mutex_lock(&per_pin->lock);
612 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
613 mutex_unlock(&per_pin->lock);
614}
615
616static void write_eld_info(struct snd_info_entry *entry,
617 struct snd_info_buffer *buffer)
618{
619 struct hdmi_spec_per_pin *per_pin = entry->private_data;
620
621 mutex_lock(&per_pin->lock);
622 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
623 mutex_unlock(&per_pin->lock);
624}
625
626static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
627{
628 char name[32];
629 struct hda_codec *codec = per_pin->codec;
630 struct snd_info_entry *entry;
631 int err;
632
633 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100634 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200635 if (err < 0)
636 return err;
637
638 snd_info_set_text_ops(entry, per_pin, print_eld_info);
639 entry->c.text.write = write_eld_info;
640 entry->mode |= S_IWUSR;
641 per_pin->proc_entry = entry;
642
643 return 0;
644}
645
646static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
647{
Markus Elfring1947a112015-06-28 11:15:28 +0200648 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200649 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200650 per_pin->proc_entry = NULL;
651 }
652}
653#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200654static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
655 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200656{
657 return 0;
658}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200659static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200660{
661}
662#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800663
664/*
665 * Channel mapping routines
666 */
667
668/*
669 * Compute derived values in channel_allocations[].
670 */
671static void init_channel_allocations(void)
672{
673 int i, j;
674 struct cea_channel_speaker_allocation *p;
675
676 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
677 p = channel_allocations + i;
678 p->channels = 0;
679 p->spk_mask = 0;
680 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
681 if (p->speakers[j]) {
682 p->channels++;
683 p->spk_mask |= p->speakers[j];
684 }
685 }
686}
687
Wang Xingchao72357c72012-09-06 10:02:36 +0800688static int get_channel_allocation_order(int ca)
689{
690 int i;
691
692 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
693 if (channel_allocations[i].ca_index == ca)
694 break;
695 }
696 return i;
697}
698
Wu Fengguang079d88c2010-03-08 10:44:23 +0800699/*
700 * The transformation takes two steps:
701 *
702 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
703 * spk_mask => (channel_allocations[]) => ai->CA
704 *
705 * TODO: it could select the wrong CA from multiple candidates.
706*/
Takashi Iwai79514d42014-06-06 18:04:34 +0200707static int hdmi_channel_allocation(struct hda_codec *codec,
708 struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800709{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800710 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800711 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800712 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800713 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
714
715 /*
716 * CA defaults to 0 for basic stereo audio
717 */
718 if (channels <= 2)
719 return 0;
720
Wu Fengguang079d88c2010-03-08 10:44:23 +0800721 /*
722 * expand ELD's speaker allocation mask
723 *
724 * ELD tells the speaker mask in a compact(paired) form,
725 * expand ELD's notions to match the ones used by Audio InfoFrame.
726 */
727 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100728 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800729 spk_mask |= eld_speaker_allocation_bits[i];
730 }
731
732 /* search for the first working match in the CA table */
733 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
734 if (channels == channel_allocations[i].channels &&
735 (spk_mask & channel_allocations[i].spk_mask) ==
736 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800737 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800738 break;
739 }
740 }
741
Anssi Hannula18e39182013-09-01 14:36:47 +0300742 if (!ca) {
743 /* if there was no match, select the regular ALSA channel
744 * allocation with the matching number of channels */
745 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
746 if (channels == channel_allocations[i].channels) {
747 ca = channel_allocations[i].ca_index;
748 break;
749 }
750 }
751 }
752
David Henningsson1613d6b2013-02-19 16:11:24 +0100753 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Takashi Iwai79514d42014-06-06 18:04:34 +0200754 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800755 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800756
Wu Fengguang53d7d692010-09-21 14:25:49 +0800757 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800758}
759
760static void hdmi_debug_channel_mapping(struct hda_codec *codec,
761 hda_nid_t pin_nid)
762{
763#ifdef CONFIG_SND_DEBUG_VERBOSE
Anssi Hannula307229d2013-10-24 21:10:34 +0300764 struct hdmi_spec *spec = codec->spec;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800765 int i;
Anssi Hannula307229d2013-10-24 21:10:34 +0300766 int channel;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800767
768 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300769 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100770 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300771 channel, i);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800772 }
773#endif
774}
775
Takashi Iwaid45e6882012-07-31 11:36:00 +0200776static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800777 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800778 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800779 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800780{
Anssi Hannula307229d2013-10-24 21:10:34 +0300781 struct hdmi_spec *spec = codec->spec;
Anssi Hannula90f28002013-10-05 02:25:39 +0300782 struct cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800783 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800784 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800785 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800786 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800787
Wang Xingchao72357c72012-09-06 10:02:36 +0800788 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300789 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800790
Wu Fengguang079d88c2010-03-08 10:44:23 +0800791 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300792 int hdmi_slot = 0;
793 /* fill actual channel mappings in ALSA channel (i) order */
794 for (i = 0; i < ch_alloc->channels; i++) {
795 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
796 hdmi_slot++; /* skip zero slots */
797
798 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
799 }
800 /* fill the rest of the slots with ALSA channel 0xf */
801 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
802 if (!ch_alloc->speakers[7 - hdmi_slot])
803 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800804 }
805
Wang Xingchao433968d2012-09-06 10:02:37 +0800806 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300807 for (i = 0; i < ch_alloc->channels; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300808 non_pcm_mapping[i] = (i << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800809 for (; i < 8; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300810 non_pcm_mapping[i] = (0xf << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800811 }
812
Wu Fengguang079d88c2010-03-08 10:44:23 +0800813 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300814 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
815 int hdmi_slot = slotsetup & 0x0f;
816 int channel = (slotsetup & 0xf0) >> 4;
817 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800818 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100819 codec_dbg(codec, "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800820 break;
821 }
822 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800823}
824
Takashi Iwaid45e6882012-07-31 11:36:00 +0200825struct channel_map_table {
826 unsigned char map; /* ALSA API channel map position */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200827 int spk_mask; /* speaker position bit mask */
828};
829
830static struct channel_map_table map_tables[] = {
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300831 { SNDRV_CHMAP_FL, FL },
832 { SNDRV_CHMAP_FR, FR },
833 { SNDRV_CHMAP_RL, RL },
834 { SNDRV_CHMAP_RR, RR },
835 { SNDRV_CHMAP_LFE, LFE },
836 { SNDRV_CHMAP_FC, FC },
837 { SNDRV_CHMAP_RLC, RLC },
838 { SNDRV_CHMAP_RRC, RRC },
839 { SNDRV_CHMAP_RC, RC },
840 { SNDRV_CHMAP_FLC, FLC },
841 { SNDRV_CHMAP_FRC, FRC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200842 { SNDRV_CHMAP_TFL, FLH },
843 { SNDRV_CHMAP_TFR, FRH },
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300844 { SNDRV_CHMAP_FLW, FLW },
845 { SNDRV_CHMAP_FRW, FRW },
846 { SNDRV_CHMAP_TC, TC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200847 { SNDRV_CHMAP_TFC, FCH },
Takashi Iwaid45e6882012-07-31 11:36:00 +0200848 {} /* terminator */
849};
850
851/* from ALSA API channel position to speaker bit mask */
852static int to_spk_mask(unsigned char c)
853{
854 struct channel_map_table *t = map_tables;
855 for (; t->map; t++) {
856 if (t->map == c)
857 return t->spk_mask;
858 }
859 return 0;
860}
861
862/* from ALSA API channel position to CEA slot */
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300863static int to_cea_slot(int ordered_ca, unsigned char pos)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200864{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300865 int mask = to_spk_mask(pos);
866 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200867
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300868 if (mask) {
869 for (i = 0; i < 8; i++) {
870 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
871 return i;
872 }
Takashi Iwaid45e6882012-07-31 11:36:00 +0200873 }
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300874
875 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200876}
877
878/* from speaker bit mask to ALSA API channel position */
879static int spk_to_chmap(int spk)
880{
881 struct channel_map_table *t = map_tables;
882 for (; t->map; t++) {
883 if (t->spk_mask == spk)
884 return t->map;
885 }
886 return 0;
887}
888
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300889/* from CEA slot to ALSA API channel position */
890static int from_cea_slot(int ordered_ca, unsigned char slot)
891{
892 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
893
894 return spk_to_chmap(mask);
895}
896
Takashi Iwaid45e6882012-07-31 11:36:00 +0200897/* get the CA index corresponding to the given ALSA API channel map */
898static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
899{
900 int i, spks = 0, spk_mask = 0;
901
902 for (i = 0; i < chs; i++) {
903 int mask = to_spk_mask(map[i]);
904 if (mask) {
905 spk_mask |= mask;
906 spks++;
907 }
908 }
909
910 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
911 if ((chs == channel_allocations[i].channels ||
912 spks == channel_allocations[i].channels) &&
913 (spk_mask & channel_allocations[i].spk_mask) ==
914 channel_allocations[i].spk_mask)
915 return channel_allocations[i].ca_index;
916 }
917 return -1;
918}
919
920/* set up the channel slots for the given ALSA API channel map */
921static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
922 hda_nid_t pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300923 int chs, unsigned char *map,
924 int ca)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200925{
Anssi Hannula307229d2013-10-24 21:10:34 +0300926 struct hdmi_spec *spec = codec->spec;
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300927 int ordered_ca = get_channel_allocation_order(ca);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300928 int alsa_pos, hdmi_slot;
929 int assignments[8] = {[0 ... 7] = 0xf};
930
931 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
932
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300933 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300934
935 if (hdmi_slot < 0)
936 continue; /* unassigned channel */
937
938 assignments[hdmi_slot] = alsa_pos;
939 }
940
941 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300942 int err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300943
Anssi Hannula307229d2013-10-24 21:10:34 +0300944 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
945 assignments[hdmi_slot]);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200946 if (err)
947 return -EINVAL;
948 }
949 return 0;
950}
951
952/* store ALSA API channel map from the current default map */
953static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
954{
955 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300956 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200957 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300958 if (i < channel_allocations[ordered_ca].channels)
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300959 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200960 else
961 map[i] = 0;
962 }
963}
964
965static void hdmi_setup_channel_mapping(struct hda_codec *codec,
966 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200967 int channels, unsigned char *map,
968 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200969{
Anssi Hannula20608732013-02-03 17:55:45 +0200970 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200971 hdmi_manual_setup_channel_mapping(codec, pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300972 channels, map, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200973 } else {
974 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
975 hdmi_setup_fake_chmap(map, ca);
976 }
Anssi Hannula980b2492013-10-05 02:25:44 +0300977
978 hdmi_debug_channel_mapping(codec, pin_nid);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200979}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800980
Anssi Hannula307229d2013-10-24 21:10:34 +0300981static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
982 int asp_slot, int channel)
983{
984 return snd_hda_codec_write(codec, pin_nid, 0,
985 AC_VERB_SET_HDMI_CHAN_SLOT,
986 (channel << 4) | asp_slot);
987}
988
989static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
990 int asp_slot)
991{
992 return (snd_hda_codec_read(codec, pin_nid, 0,
993 AC_VERB_GET_HDMI_CHAN_SLOT,
994 asp_slot) & 0xf0) >> 4;
995}
996
Wu Fengguang079d88c2010-03-08 10:44:23 +0800997/*
998 * Audio InfoFrame routines
999 */
1000
1001/*
1002 * Enable Audio InfoFrame Transmission
1003 */
1004static void hdmi_start_infoframe_trans(struct hda_codec *codec,
1005 hda_nid_t pin_nid)
1006{
1007 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1008 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1009 AC_DIPXMIT_BEST);
1010}
1011
1012/*
1013 * Disable Audio InfoFrame Transmission
1014 */
1015static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1016 hda_nid_t pin_nid)
1017{
1018 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1019 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1020 AC_DIPXMIT_DISABLE);
1021}
1022
1023static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1024{
1025#ifdef CONFIG_SND_DEBUG_VERBOSE
1026 int i;
1027 int size;
1028
1029 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001030 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001031
1032 for (i = 0; i < 8; i++) {
1033 size = snd_hda_codec_read(codec, pin_nid, 0,
1034 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001035 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001036 }
1037#endif
1038}
1039
1040static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1041{
1042#ifdef BE_PARANOID
1043 int i, j;
1044 int size;
1045 int pi, bi;
1046 for (i = 0; i < 8; i++) {
1047 size = snd_hda_codec_read(codec, pin_nid, 0,
1048 AC_VERB_GET_HDMI_DIP_SIZE, i);
1049 if (size == 0)
1050 continue;
1051
1052 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1053 for (j = 1; j < 1000; j++) {
1054 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1055 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1056 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001057 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001058 bi, pi, i);
1059 if (bi == 0) /* byte index wrapped around */
1060 break;
1061 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001062 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001063 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1064 i, size, j);
1065 }
1066#endif
1067}
1068
Wu Fengguang53d7d692010-09-21 14:25:49 +08001069static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001070{
Wu Fengguang53d7d692010-09-21 14:25:49 +08001071 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001072 u8 sum = 0;
1073 int i;
1074
Wu Fengguang53d7d692010-09-21 14:25:49 +08001075 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001076
Wu Fengguang53d7d692010-09-21 14:25:49 +08001077 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001078 sum += bytes[i];
1079
Wu Fengguang53d7d692010-09-21 14:25:49 +08001080 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001081}
1082
1083static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1084 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001085 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001086{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001087 int i;
1088
1089 hdmi_debug_dip_size(codec, pin_nid);
1090 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1091
Wu Fengguang079d88c2010-03-08 10:44:23 +08001092 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001093 for (i = 0; i < size; i++)
1094 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001095}
1096
1097static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001098 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001099{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001100 u8 val;
1101 int i;
1102
1103 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1104 != AC_DIPXMIT_BEST)
1105 return false;
1106
1107 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001108 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001109 val = snd_hda_codec_read(codec, pin_nid, 0,
1110 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001111 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +08001112 return false;
1113 }
1114
1115 return true;
1116}
1117
Anssi Hannula307229d2013-10-24 21:10:34 +03001118static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1119 hda_nid_t pin_nid,
1120 int ca, int active_channels,
1121 int conn_type)
1122{
1123 union audio_infoframe ai;
1124
Mengdong Lincaaf5ef2014-03-11 17:12:52 -04001125 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +03001126 if (conn_type == 0) { /* HDMI */
1127 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1128
1129 hdmi_ai->type = 0x84;
1130 hdmi_ai->ver = 0x01;
1131 hdmi_ai->len = 0x0a;
1132 hdmi_ai->CC02_CT47 = active_channels - 1;
1133 hdmi_ai->CA = ca;
1134 hdmi_checksum_audio_infoframe(hdmi_ai);
1135 } else if (conn_type == 1) { /* DisplayPort */
1136 struct dp_audio_infoframe *dp_ai = &ai.dp;
1137
1138 dp_ai->type = 0x84;
1139 dp_ai->len = 0x1b;
1140 dp_ai->ver = 0x11 << 2;
1141 dp_ai->CC02_CT47 = active_channels - 1;
1142 dp_ai->CA = ca;
1143 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001144 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001145 pin_nid);
1146 return;
1147 }
1148
1149 /*
1150 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1151 * sizeof(*dp_ai) to avoid partial match/update problems when
1152 * the user switches between HDMI/DP monitors.
1153 */
1154 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1155 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001156 codec_dbg(codec,
1157 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001158 pin_nid,
1159 active_channels, ca);
1160 hdmi_stop_infoframe_trans(codec, pin_nid);
1161 hdmi_fill_audio_infoframe(codec, pin_nid,
1162 ai.bytes, sizeof(ai));
1163 hdmi_start_infoframe_trans(codec, pin_nid);
1164 }
1165}
1166
Takashi Iwaib0540872013-09-02 12:33:02 +02001167static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1168 struct hdmi_spec_per_pin *per_pin,
1169 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001170{
Anssi Hannula307229d2013-10-24 21:10:34 +03001171 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001172 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +02001173 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001174 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -06001175 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001176 int ca, ordered_ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001177
Takashi Iwaib0540872013-09-02 12:33:02 +02001178 if (!channels)
1179 return;
1180
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001181 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001182 snd_hda_codec_write(codec, pin_nid, 0,
1183 AC_VERB_SET_AMP_GAIN_MUTE,
1184 AMP_OUT_UNMUTE);
1185
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001186 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001187
Takashi Iwaid45e6882012-07-31 11:36:00 +02001188 if (!non_pcm && per_pin->chmap_set)
1189 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1190 else
Takashi Iwai79514d42014-06-06 18:04:34 +02001191 ca = hdmi_channel_allocation(codec, eld, channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001192 if (ca < 0)
1193 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001194
Anssi Hannula1df5a062013-10-05 02:25:40 +03001195 ordered_ca = get_channel_allocation_order(ca);
1196 active_channels = channel_allocations[ordered_ca].channels;
1197
1198 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1199
Stephen Warren384a48d2011-06-01 11:14:21 -06001200 /*
Anssi Hannula39edac72013-10-07 19:24:52 +03001201 * always configure channel mapping, it may have been changed by the
1202 * user in the meantime
1203 */
1204 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1205 channels, per_pin->chmap,
1206 per_pin->chmap_set);
1207
Anssi Hannula307229d2013-10-24 21:10:34 +03001208 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1209 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +08001210
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001211 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001212}
1213
Wu Fengguang079d88c2010-03-08 10:44:23 +08001214/*
1215 * Unsolicited events
1216 */
1217
Takashi Iwaiefe47102013-11-07 13:38:23 +01001218static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001219
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001220static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001221{
1222 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001223 int pin_idx = pin_nid_to_pin_index(codec, nid);
1224
David Henningsson20ce9022013-12-04 10:19:41 +08001225 if (pin_idx < 0)
1226 return;
David Henningsson20ce9022013-12-04 10:19:41 +08001227 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1228 snd_hda_jack_report_sync(codec);
1229}
1230
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001231static void jack_callback(struct hda_codec *codec,
1232 struct hda_jack_callback *jack)
1233{
1234 check_presence_and_report(codec, jack->tbl->nid);
1235}
1236
David Henningsson20ce9022013-12-04 10:19:41 +08001237static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1238{
Takashi Iwai3a938972011-10-28 01:16:55 +02001239 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001240 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001241 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001242
1243 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1244 if (!jack)
1245 return;
Takashi Iwai3a938972011-10-28 01:16:55 +02001246 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001247
Takashi Iwai4e76a882014-02-25 12:21:03 +01001248 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001249 "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 +08001250 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001251 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001252
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001253 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001254}
1255
1256static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1257{
1258 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1259 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1260 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1261 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1262
Takashi Iwai4e76a882014-02-25 12:21:03 +01001263 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001264 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001265 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001266 tag,
1267 subtag,
1268 cp_state,
1269 cp_ready);
1270
1271 /* TODO */
1272 if (cp_state)
1273 ;
1274 if (cp_ready)
1275 ;
1276}
1277
1278
1279static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1280{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001281 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1282 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1283
Takashi Iwai3a938972011-10-28 01:16:55 +02001284 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001285 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001286 return;
1287 }
1288
1289 if (subtag == 0)
1290 hdmi_intrinsic_event(codec, res);
1291 else
1292 hdmi_non_intrinsic_event(codec, res);
1293}
1294
Mengdong Lin58f7d282013-09-04 16:37:12 -04001295static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001296 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001297{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001298 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001299
Wang Xingchao53b434f2013-06-18 10:41:53 +08001300 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1301 * thus pins could only choose converter 0 for use. Make sure the
1302 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001303 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001304 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1305
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001306 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001307 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1308 AC_PWRST_D0);
1309 msleep(40);
1310 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1311 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001312 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 +02001313 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001314}
1315
Wu Fengguang079d88c2010-03-08 10:44:23 +08001316/*
1317 * Callbacks
1318 */
1319
Takashi Iwai92f10b32010-08-03 14:21:00 +02001320/* HBR should be Non-PCM, 8 channels */
1321#define is_hbr_format(format) \
1322 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1323
Anssi Hannula307229d2013-10-24 21:10:34 +03001324static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1325 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001326{
Anssi Hannula307229d2013-10-24 21:10:34 +03001327 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +02001328
Stephen Warren384a48d2011-06-01 11:14:21 -06001329 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1330 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001331 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1332
Anssi Hannula13122e62013-11-10 20:56:10 +02001333 if (pinctl < 0)
1334 return hbr ? -EINVAL : 0;
1335
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001336 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +03001337 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001338 new_pinctl |= AC_PINCTL_EPT_HBR;
1339 else
1340 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1341
Takashi Iwai4e76a882014-02-25 12:21:03 +01001342 codec_dbg(codec,
1343 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001344 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001345 pinctl == new_pinctl ? "" : "new-",
1346 new_pinctl);
1347
1348 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001349 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001350 AC_VERB_SET_PIN_WIDGET_CONTROL,
1351 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +03001352 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001353 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03001354
1355 return 0;
1356}
1357
1358static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1359 hda_nid_t pin_nid, u32 stream_tag, int format)
1360{
1361 struct hdmi_spec *spec = codec->spec;
1362 int err;
1363
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001364 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +03001365 haswell_verify_D0(codec, cvt_nid, pin_nid);
1366
1367 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1368
1369 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001370 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +03001371 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001372 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001373
Stephen Warren384a48d2011-06-01 11:14:21 -06001374 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001375 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001376}
1377
Libin Yang42b29872015-12-16 13:42:42 +08001378/* Try to find an available converter
1379 * If pin_idx is less then zero, just try to find an available converter.
1380 * Otherwise, try to find an available converter and get the cvt mux index
1381 * of the pin.
1382 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001383static int hdmi_choose_cvt(struct hda_codec *codec,
1384 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001385{
1386 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001387 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001388 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001389 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001390
Libin Yang42b29872015-12-16 13:42:42 +08001391 /* pin_idx < 0 means no pin will be bound to the converter */
1392 if (pin_idx < 0)
1393 per_pin = NULL;
1394 else
1395 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001396
Stephen Warren384a48d2011-06-01 11:14:21 -06001397 /* Dynamically assign converter to stream */
1398 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001399 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001400
1401 /* Must not already be assigned */
1402 if (per_cvt->assigned)
1403 continue;
Libin Yang42b29872015-12-16 13:42:42 +08001404 if (per_pin == NULL)
1405 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06001406 /* Must be in pin's mux's list of converters */
1407 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1408 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1409 break;
1410 /* Not in mux list */
1411 if (mux_idx == per_pin->num_mux_nids)
1412 continue;
1413 break;
1414 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001415
Stephen Warren384a48d2011-06-01 11:14:21 -06001416 /* No free converters */
1417 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +08001418 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -06001419
Libin Yang42b29872015-12-16 13:42:42 +08001420 if (per_pin != NULL)
1421 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +08001422
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001423 if (cvt_id)
1424 *cvt_id = cvt_idx;
1425 if (mux_id)
1426 *mux_id = mux_idx;
1427
1428 return 0;
1429}
1430
Mengdong Lin2df67422014-03-20 13:01:06 +08001431/* Assure the pin select the right convetor */
1432static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1433 struct hdmi_spec_per_pin *per_pin)
1434{
1435 hda_nid_t pin_nid = per_pin->pin_nid;
1436 int mux_idx, curr;
1437
1438 mux_idx = per_pin->mux_idx;
1439 curr = snd_hda_codec_read(codec, pin_nid, 0,
1440 AC_VERB_GET_CONNECT_SEL, 0);
1441 if (curr != mux_idx)
1442 snd_hda_codec_write_cache(codec, pin_nid, 0,
1443 AC_VERB_SET_CONNECT_SEL,
1444 mux_idx);
1445}
1446
Libin Yang42b29872015-12-16 13:42:42 +08001447/* get the mux index for the converter of the pins
1448 * converter's mux index is the same for all pins on Intel platform
1449 */
1450static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1451 hda_nid_t cvt_nid)
1452{
1453 int i;
1454
1455 for (i = 0; i < spec->num_cvts; i++)
1456 if (spec->cvt_nids[i] == cvt_nid)
1457 return i;
1458 return -EINVAL;
1459}
1460
Mengdong Lin300016b2013-11-04 01:13:13 -05001461/* Intel HDMI workaround to fix audio routing issue:
1462 * For some Intel display codecs, pins share the same connection list.
1463 * So a conveter can be selected by multiple pins and playback on any of these
1464 * pins will generate sound on the external display, because audio flows from
1465 * the same converter to the display pipeline. Also muting one pin may make
1466 * other pins have no sound output.
1467 * So this function assures that an assigned converter for a pin is not selected
1468 * by any other pins.
1469 */
1470static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001471 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001472{
1473 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001474 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001475 int cvt_idx, curr;
1476 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001477
Mengdong Linf82d7d12013-09-21 20:34:45 -04001478 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +01001479 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -04001480 unsigned int wid_caps = get_wcaps(codec, nid);
1481 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001482
Mengdong Linf82d7d12013-09-21 20:34:45 -04001483 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001484 continue;
1485
Mengdong Linf82d7d12013-09-21 20:34:45 -04001486 if (nid == pin_nid)
1487 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001488
Mengdong Linf82d7d12013-09-21 20:34:45 -04001489 curr = snd_hda_codec_read(codec, nid, 0,
1490 AC_VERB_GET_CONNECT_SEL, 0);
1491 if (curr != mux_idx)
1492 continue;
1493
1494 /* choose an unassigned converter. The conveters in the
1495 * connection list are in the same order as in the codec.
1496 */
1497 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1498 per_cvt = get_cvt(spec, cvt_idx);
1499 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001500 codec_dbg(codec,
1501 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001502 cvt_idx, nid);
1503 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001504 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001505 cvt_idx);
1506 break;
1507 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001508 }
1509 }
1510}
1511
Libin Yang42b29872015-12-16 13:42:42 +08001512/* A wrapper of intel_not_share_asigned_cvt() */
1513static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1514 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1515{
1516 int mux_idx;
1517 struct hdmi_spec *spec = codec->spec;
1518
1519 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1520 return;
1521
1522 /* On Intel platform, the mapping of converter nid to
1523 * mux index of the pins are always the same.
1524 * The pin nid may be 0, this means all pins will not
1525 * share the converter.
1526 */
1527 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1528 if (mux_idx >= 0)
1529 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1530}
1531
1532/* called in hdmi_pcm_open when no pin is assigned to the PCM
1533 * in dyn_pcm_assign mode.
1534 */
1535static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1536 struct hda_codec *codec,
1537 struct snd_pcm_substream *substream)
1538{
1539 struct hdmi_spec *spec = codec->spec;
1540 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001541 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001542 struct hdmi_spec_per_cvt *per_cvt = NULL;
1543 int err;
1544
Libin Yangac983792015-12-16 16:48:16 +08001545 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1546 if (pcm_idx < 0)
1547 return -EINVAL;
1548
Libin Yang42b29872015-12-16 13:42:42 +08001549 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1550 if (err)
1551 return err;
1552
1553 per_cvt = get_cvt(spec, cvt_idx);
1554 per_cvt->assigned = 1;
1555 hinfo->nid = per_cvt->cvt_nid;
1556
1557 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1558
Libin Yangac983792015-12-16 16:48:16 +08001559 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001560 /* todo: setup spdif ctls assign */
1561
1562 /* Initially set the converter's capabilities */
1563 hinfo->channels_min = per_cvt->channels_min;
1564 hinfo->channels_max = per_cvt->channels_max;
1565 hinfo->rates = per_cvt->rates;
1566 hinfo->formats = per_cvt->formats;
1567 hinfo->maxbps = per_cvt->maxbps;
1568
1569 /* Store the updated parameters */
1570 runtime->hw.channels_min = hinfo->channels_min;
1571 runtime->hw.channels_max = hinfo->channels_max;
1572 runtime->hw.formats = hinfo->formats;
1573 runtime->hw.rates = hinfo->rates;
1574
1575 snd_pcm_hw_constraint_step(substream->runtime, 0,
1576 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1577 return 0;
1578}
1579
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001580/*
1581 * HDA PCM callbacks
1582 */
1583static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1584 struct hda_codec *codec,
1585 struct snd_pcm_substream *substream)
1586{
1587 struct hdmi_spec *spec = codec->spec;
1588 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001589 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001590 struct hdmi_spec_per_pin *per_pin;
1591 struct hdmi_eld *eld;
1592 struct hdmi_spec_per_cvt *per_cvt = NULL;
1593 int err;
1594
1595 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001596 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1597 if (pcm_idx < 0)
1598 return -EINVAL;
1599
Libin Yang42b29872015-12-16 13:42:42 +08001600 mutex_lock(&spec->pcm_lock);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001601 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001602 if (!spec->dyn_pcm_assign) {
1603 if (snd_BUG_ON(pin_idx < 0)) {
1604 mutex_unlock(&spec->pcm_lock);
1605 return -EINVAL;
1606 }
1607 } else {
1608 /* no pin is assigned to the PCM
1609 * PA need pcm open successfully when probe
1610 */
1611 if (pin_idx < 0) {
1612 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1613 mutex_unlock(&spec->pcm_lock);
1614 return err;
1615 }
1616 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001617
1618 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001619 if (err < 0) {
1620 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001621 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001622 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001623
1624 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001625 /* Claim converter */
1626 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001627
Libin Yangac983792015-12-16 16:48:16 +08001628 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001629 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001630 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001631 hinfo->nid = per_cvt->cvt_nid;
1632
Takashi Iwaibddee962013-06-18 16:14:22 +02001633 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001634 AC_VERB_SET_CONNECT_SEL,
1635 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001636
1637 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001638 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001639 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001640
Libin Yang2bf3c852015-12-16 13:42:43 +08001641 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001642
Stephen Warren2def8172011-06-01 11:14:20 -06001643 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001644 hinfo->channels_min = per_cvt->channels_min;
1645 hinfo->channels_max = per_cvt->channels_max;
1646 hinfo->rates = per_cvt->rates;
1647 hinfo->formats = per_cvt->formats;
1648 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001649
Libin Yang42b29872015-12-16 13:42:42 +08001650 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001651 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001652 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001653 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001654 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001655 !hinfo->rates || !hinfo->formats) {
1656 per_cvt->assigned = 0;
1657 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001658 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001659 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001660 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001661 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001662 }
Stephen Warren2def8172011-06-01 11:14:20 -06001663
Libin Yang42b29872015-12-16 13:42:42 +08001664 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001665 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001666 runtime->hw.channels_min = hinfo->channels_min;
1667 runtime->hw.channels_max = hinfo->channels_max;
1668 runtime->hw.formats = hinfo->formats;
1669 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001670
1671 snd_pcm_hw_constraint_step(substream->runtime, 0,
1672 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001673 return 0;
1674}
1675
1676/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001677 * HDA/HDMI auto parsing
1678 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001679static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001680{
1681 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001682 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001683 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001684
1685 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001686 codec_warn(codec,
1687 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001688 pin_nid, get_wcaps(codec, pin_nid));
1689 return -EINVAL;
1690 }
1691
Stephen Warren384a48d2011-06-01 11:14:21 -06001692 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1693 per_pin->mux_nids,
1694 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001695
1696 return 0;
1697}
1698
Libin Yanga76056f2015-12-16 16:48:15 +08001699static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1700 struct hdmi_spec_per_pin *per_pin)
1701{
1702 int i;
1703
1704 /* try the prefer PCM */
1705 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1706 return per_pin->pin_nid_idx;
1707
1708 /* have a second try; check the "reserved area" over num_pins */
1709 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1710 if (!test_bit(i, &spec->pcm_bitmap))
1711 return i;
1712 }
1713
1714 /* the last try; check the empty slots in pins */
1715 for (i = 0; i < spec->num_pins; i++) {
1716 if (!test_bit(i, &spec->pcm_bitmap))
1717 return i;
1718 }
1719 return -EBUSY;
1720}
1721
1722static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1723 struct hdmi_spec_per_pin *per_pin)
1724{
1725 int idx;
1726
1727 /* pcm already be attached to the pin */
1728 if (per_pin->pcm)
1729 return;
1730 idx = hdmi_find_pcm_slot(spec, per_pin);
1731 if (idx == -ENODEV)
1732 return;
1733 per_pin->pcm_idx = idx;
Libin Yang2bea2412016-01-12 11:13:26 +08001734 per_pin->pcm = get_hdmi_pcm(spec, idx);
Libin Yanga76056f2015-12-16 16:48:15 +08001735 set_bit(idx, &spec->pcm_bitmap);
1736}
1737
1738static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1739 struct hdmi_spec_per_pin *per_pin)
1740{
1741 int idx;
1742
1743 /* pcm already be detached from the pin */
1744 if (!per_pin->pcm)
1745 return;
1746 idx = per_pin->pcm_idx;
1747 per_pin->pcm_idx = -1;
1748 per_pin->pcm = NULL;
1749 if (idx >= 0 && idx < spec->pcm_used)
1750 clear_bit(idx, &spec->pcm_bitmap);
1751}
1752
Libin Yangac983792015-12-16 16:48:16 +08001753static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1754 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1755{
1756 int mux_idx;
1757
1758 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1759 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1760 break;
1761 return mux_idx;
1762}
1763
1764static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1765
1766static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1767 struct hdmi_spec_per_pin *per_pin)
1768{
1769 struct hda_codec *codec = per_pin->codec;
1770 struct hda_pcm *pcm;
1771 struct hda_pcm_stream *hinfo;
1772 struct snd_pcm_substream *substream;
1773 int mux_idx;
1774 bool non_pcm;
1775
1776 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
Libin Yang2bea2412016-01-12 11:13:26 +08001777 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08001778 else
1779 return;
1780 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1781 return;
1782
1783 /* hdmi audio only uses playback and one substream */
1784 hinfo = pcm->stream;
1785 substream = pcm->pcm->streams[0].substream;
1786
1787 per_pin->cvt_nid = hinfo->nid;
1788
1789 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1790 if (mux_idx < per_pin->num_mux_nids)
1791 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1792 AC_VERB_SET_CONNECT_SEL,
1793 mux_idx);
1794 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1795
1796 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1797 if (substream->runtime)
1798 per_pin->channels = substream->runtime->channels;
1799 per_pin->setup = true;
1800 per_pin->mux_idx = mux_idx;
1801
1802 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1803}
1804
1805static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1806 struct hdmi_spec_per_pin *per_pin)
1807{
1808 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1809 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1810
1811 per_pin->chmap_set = false;
1812 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1813
1814 per_pin->setup = false;
1815 per_pin->channels = 0;
1816}
1817
Takashi Iwaie90247f2015-11-13 09:12:12 +01001818/* update per_pin ELD from the given new ELD;
1819 * setup info frame and notification accordingly
1820 */
1821static void update_eld(struct hda_codec *codec,
1822 struct hdmi_spec_per_pin *per_pin,
1823 struct hdmi_eld *eld)
1824{
1825 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001826 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001827 bool old_eld_valid = pin_eld->eld_valid;
1828 bool eld_changed;
1829
Libin Yanga76056f2015-12-16 16:48:15 +08001830 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001831 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001832 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001833 hdmi_pcm_setup_pin(spec, per_pin);
1834 } else {
1835 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001836 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001837 }
Libin Yanga76056f2015-12-16 16:48:15 +08001838 }
1839
Takashi Iwaie90247f2015-11-13 09:12:12 +01001840 if (eld->eld_valid)
1841 snd_hdmi_show_eld(codec, &eld->info);
1842
1843 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1844 if (eld->eld_valid && pin_eld->eld_valid)
1845 if (pin_eld->eld_size != eld->eld_size ||
1846 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1847 eld->eld_size) != 0)
1848 eld_changed = true;
1849
1850 pin_eld->eld_valid = eld->eld_valid;
1851 pin_eld->eld_size = eld->eld_size;
1852 if (eld->eld_valid)
1853 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1854 pin_eld->info = eld->info;
1855
1856 /*
1857 * Re-setup pin and infoframe. This is needed e.g. when
1858 * - sink is first plugged-in
1859 * - transcoder can change during stream playback on Haswell
1860 * and this can make HW reset converter selection on a pin.
1861 */
1862 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1863 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1864 intel_verify_pin_cvt_connect(codec, per_pin);
1865 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1866 per_pin->mux_idx);
1867 }
1868
1869 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1870 }
1871
1872 if (eld_changed)
1873 snd_ctl_notify(codec->card,
1874 SNDRV_CTL_EVENT_MASK_VALUE |
1875 SNDRV_CTL_EVENT_MASK_INFO,
1876 &per_pin->eld_ctl->id);
1877}
1878
Takashi Iwai788d4412015-11-12 15:36:13 +01001879/* update ELD and jack state via HD-audio verbs */
1880static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1881 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001882{
David Henningsson464837a2013-11-07 13:38:25 +01001883 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001884 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001885 struct hdmi_spec *spec = codec->spec;
1886 struct hdmi_eld *eld = &spec->temp_eld;
1887 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001888 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001889 /*
1890 * Always execute a GetPinSense verb here, even when called from
1891 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1892 * response's PD bit is not the real PD value, but indicates that
1893 * the real PD value changed. An older version of the HD-audio
1894 * specification worked this way. Hence, we just ignore the data in
1895 * the unsolicited response to avoid custom WARs.
1896 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001897 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001898 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001899 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001900
Takashi Iwai664c7152015-04-08 11:43:14 +02001901 snd_hda_power_up_pm(codec);
David Henningssonda4a7a32013-12-18 10:46:04 +01001902 present = snd_hda_pin_sense(codec, pin_nid);
1903
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001904 mutex_lock(&per_pin->lock);
David Henningsson4bd038f2013-02-19 16:11:25 +01001905 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1906 if (pin_eld->monitor_present)
1907 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1908 else
1909 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001910
Takashi Iwai4e76a882014-02-25 12:21:03 +01001911 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001912 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001913 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001914
David Henningsson4bd038f2013-02-19 16:11:25 +01001915 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001916 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001917 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001918 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001919 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001920 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001921 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001922 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001923 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001924 if (!eld->eld_valid && repoll)
1925 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001926 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001927
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001928 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001929 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1930 else
1931 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001932
Takashi Iwaiaff747eb2013-11-07 16:39:37 +01001933 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001934
1935 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1936 if (jack)
1937 jack->block_report = !ret;
1938
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001939 mutex_unlock(&per_pin->lock);
Takashi Iwai664c7152015-04-08 11:43:14 +02001940 snd_hda_power_down_pm(codec);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001941 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001942}
1943
Takashi Iwai788d4412015-11-12 15:36:13 +01001944/* update ELD and jack state via audio component */
1945static void sync_eld_via_acomp(struct hda_codec *codec,
1946 struct hdmi_spec_per_pin *per_pin)
1947{
Takashi Iwai788d4412015-11-12 15:36:13 +01001948 struct hdmi_spec *spec = codec->spec;
1949 struct hdmi_eld *eld = &spec->temp_eld;
Libin Yang25e4abb2016-01-12 11:13:27 +08001950 struct snd_jack *jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001951 int size;
1952
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001953 mutex_lock(&per_pin->lock);
1954 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1955 &eld->monitor_present, eld->eld_buffer,
1956 ELD_MAX_SIZE);
1957 if (size < 0)
1958 goto unlock;
1959 if (size > 0) {
1960 size = min(size, ELD_MAX_SIZE);
1961 if (snd_hdmi_parse_eld(codec, &eld->info,
1962 eld->eld_buffer, size) < 0)
1963 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001964 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001965
1966 if (size > 0) {
1967 eld->eld_valid = true;
1968 eld->eld_size = size;
1969 } else {
1970 eld->eld_valid = false;
1971 eld->eld_size = 0;
1972 }
1973
Libin Yang25e4abb2016-01-12 11:13:27 +08001974 /* pcm_idx >=0 before update_eld() means it is in monitor
1975 * disconnected event. Jack must be fetched before update_eld()
1976 */
1977 if (per_pin->pcm_idx >= 0)
1978 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001979 update_eld(codec, per_pin, eld);
Libin Yang25e4abb2016-01-12 11:13:27 +08001980 if (jack == NULL && per_pin->pcm_idx >= 0)
1981 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1982 if (jack == NULL)
1983 goto unlock;
1984 snd_jack_report(jack,
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001985 eld->monitor_present ? SND_JACK_AVOUT : 0);
1986 unlock:
1987 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001988}
1989
1990static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1991{
1992 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08001993 struct hdmi_spec *spec = codec->spec;
1994 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001995
Libin Yanga76056f2015-12-16 16:48:15 +08001996 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001997 if (codec_has_acomp(codec)) {
1998 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001999 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01002000 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08002001 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01002002 }
Libin Yanga76056f2015-12-16 16:48:15 +08002003 mutex_unlock(&spec->pcm_lock);
2004
2005 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01002006}
2007
Wu Fengguang744626d2011-11-16 16:29:47 +08002008static void hdmi_repoll_eld(struct work_struct *work)
2009{
2010 struct hdmi_spec_per_pin *per_pin =
2011 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
2012
Wu Fengguangc6e84532011-11-18 16:59:32 -06002013 if (per_pin->repoll_count++ > 6)
2014 per_pin->repoll_count = 0;
2015
Takashi Iwaiefe47102013-11-07 13:38:23 +01002016 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
2017 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08002018}
2019
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002020static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2021 hda_nid_t nid);
2022
Wu Fengguang079d88c2010-03-08 10:44:23 +08002023static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2024{
2025 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002026 unsigned int caps, config;
2027 int pin_idx;
2028 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02002029 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002030
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002031 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002032 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2033 return 0;
2034
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002035 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002036 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2037 return 0;
2038
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002039 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002040 intel_haswell_fixup_connect_list(codec, pin_nid);
2041
Stephen Warren384a48d2011-06-01 11:14:21 -06002042 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002043 per_pin = snd_array_new(&spec->pins);
2044 if (!per_pin)
2045 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002046
2047 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002048 per_pin->non_pcm = false;
Libin Yanga76056f2015-12-16 16:48:15 +08002049 if (spec->dyn_pcm_assign)
2050 per_pin->pcm_idx = -1;
Libin Yang2bea2412016-01-12 11:13:26 +08002051 else {
2052 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
Libin Yanga76056f2015-12-16 16:48:15 +08002053 per_pin->pcm_idx = pin_idx;
Libin Yang2bea2412016-01-12 11:13:26 +08002054 }
Libin Yanga76056f2015-12-16 16:48:15 +08002055 per_pin->pin_nid_idx = pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002056
Stephen Warren384a48d2011-06-01 11:14:21 -06002057 err = hdmi_read_pin_conn(codec, pin_idx);
2058 if (err < 0)
2059 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002060
Wu Fengguang079d88c2010-03-08 10:44:23 +08002061 spec->num_pins++;
2062
Stephen Warren384a48d2011-06-01 11:14:21 -06002063 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002064}
2065
Stephen Warren384a48d2011-06-01 11:14:21 -06002066static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08002067{
2068 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002069 struct hdmi_spec_per_cvt *per_cvt;
2070 unsigned int chans;
2071 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002072
Stephen Warren384a48d2011-06-01 11:14:21 -06002073 chans = get_wcaps(codec, cvt_nid);
2074 chans = get_wcaps_channels(chans);
2075
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002076 per_cvt = snd_array_new(&spec->cvts);
2077 if (!per_cvt)
2078 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002079
2080 per_cvt->cvt_nid = cvt_nid;
2081 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002082 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002083 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002084 if (chans > spec->channels_max)
2085 spec->channels_max = chans;
2086 }
Stephen Warren384a48d2011-06-01 11:14:21 -06002087
2088 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2089 &per_cvt->rates,
2090 &per_cvt->formats,
2091 &per_cvt->maxbps);
2092 if (err < 0)
2093 return err;
2094
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002095 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2096 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2097 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002098
2099 return 0;
2100}
2101
2102static int hdmi_parse_codec(struct hda_codec *codec)
2103{
2104 hda_nid_t nid;
2105 int i, nodes;
2106
Takashi Iwai7639a062015-03-03 10:07:24 +01002107 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002108 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002109 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08002110 return -EINVAL;
2111 }
2112
2113 for (i = 0; i < nodes; i++, nid++) {
2114 unsigned int caps;
2115 unsigned int type;
2116
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002117 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002118 type = get_wcaps_type(caps);
2119
2120 if (!(caps & AC_WCAP_DIGITAL))
2121 continue;
2122
2123 switch (type) {
2124 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06002125 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002126 break;
2127 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08002128 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002129 break;
2130 }
2131 }
2132
Wu Fengguang079d88c2010-03-08 10:44:23 +08002133 return 0;
2134}
2135
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002136/*
2137 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002138static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2139{
2140 struct hda_spdif_out *spdif;
2141 bool non_pcm;
2142
2143 mutex_lock(&codec->spdif_mutex);
2144 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2145 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2146 mutex_unlock(&codec->spdif_mutex);
2147 return non_pcm;
2148}
2149
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002150/*
2151 * HDMI callbacks
2152 */
2153
2154static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2155 struct hda_codec *codec,
2156 unsigned int stream_tag,
2157 unsigned int format,
2158 struct snd_pcm_substream *substream)
2159{
Stephen Warren384a48d2011-06-01 11:14:21 -06002160 hda_nid_t cvt_nid = hinfo->nid;
2161 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08002162 int pin_idx;
2163 struct hdmi_spec_per_pin *per_pin;
2164 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08002165 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002166 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07002167 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08002168 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002169
Libin Yang42b29872015-12-16 13:42:42 +08002170 mutex_lock(&spec->pcm_lock);
2171 pin_idx = hinfo_to_pin_index(codec, hinfo);
2172 if (spec->dyn_pcm_assign && pin_idx < 0) {
2173 /* when dyn_pcm_assign and pcm is not bound to a pin
2174 * skip pin setup and return 0 to make audio playback
2175 * be ongoing
2176 */
2177 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2178 snd_hda_codec_setup_stream(codec, cvt_nid,
2179 stream_tag, 0, format);
2180 mutex_unlock(&spec->pcm_lock);
2181 return 0;
2182 }
2183
2184 if (snd_BUG_ON(pin_idx < 0)) {
2185 mutex_unlock(&spec->pcm_lock);
2186 return -EINVAL;
2187 }
2188 per_pin = get_pin(spec, pin_idx);
2189 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08002190 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08002191 /* Verify pin:cvt selections to avoid silent audio after S3.
2192 * After S3, the audio driver restores pin:cvt selections
2193 * but this can happen before gfx is ready and such selection
2194 * is overlooked by HW. Thus multiple pins can share a same
2195 * default convertor and mute control will affect each other,
2196 * which can cause a resumed audio playback become silent
2197 * after S3.
2198 */
2199 intel_verify_pin_cvt_connect(codec, per_pin);
2200 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2201 }
2202
Libin Yangddd621f2015-09-02 14:11:40 +08002203 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2204 /* Todo: add DP1.2 MST audio support later */
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002205 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08002206
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002207 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002208 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02002209 per_pin->channels = substream->runtime->channels;
2210 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002211
Takashi Iwaib0540872013-09-02 12:33:02 +02002212 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002213 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07002214 if (spec->dyn_pin_out) {
2215 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2216 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2217 snd_hda_codec_write(codec, pin_nid, 0,
2218 AC_VERB_SET_PIN_WIDGET_CONTROL,
2219 pinctl | PIN_OUT);
2220 }
2221
Libin Yang42b29872015-12-16 13:42:42 +08002222 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2223 stream_tag, format);
2224 mutex_unlock(&spec->pcm_lock);
2225 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002226}
2227
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002228static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2229 struct hda_codec *codec,
2230 struct snd_pcm_substream *substream)
2231{
2232 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2233 return 0;
2234}
2235
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002236static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2237 struct hda_codec *codec,
2238 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06002239{
2240 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08002241 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06002242 struct hdmi_spec_per_cvt *per_cvt;
2243 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07002244 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06002245
Stephen Warren384a48d2011-06-01 11:14:21 -06002246 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08002247 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2248 if (snd_BUG_ON(pcm_idx < 0))
2249 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002250 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002251 if (snd_BUG_ON(cvt_idx < 0))
2252 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002253 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002254
2255 snd_BUG_ON(!per_cvt->assigned);
2256 per_cvt->assigned = 0;
2257 hinfo->nid = 0;
2258
Libin Yang42b29872015-12-16 13:42:42 +08002259 mutex_lock(&spec->pcm_lock);
Libin Yangb09887f82016-01-29 13:53:27 +08002260 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yangac983792015-12-16 16:48:16 +08002261 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002262 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08002263 if (spec->dyn_pcm_assign && pin_idx < 0) {
2264 mutex_unlock(&spec->pcm_lock);
2265 return 0;
2266 }
2267
2268 if (snd_BUG_ON(pin_idx < 0)) {
2269 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002270 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08002271 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002272 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002273
Stephen Warren75fae112014-01-30 11:52:16 -07002274 if (spec->dyn_pin_out) {
2275 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2276 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2277 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2278 AC_VERB_SET_PIN_WIDGET_CONTROL,
2279 pinctl & ~PIN_OUT);
2280 }
2281
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002282 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002283 per_pin->chmap_set = false;
2284 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002285
2286 per_pin->setup = false;
2287 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002288 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08002289 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002290 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002291
Stephen Warren384a48d2011-06-01 11:14:21 -06002292 return 0;
2293}
2294
2295static const struct hda_pcm_ops generic_ops = {
2296 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002297 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002298 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002299 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002300};
2301
Takashi Iwaid45e6882012-07-31 11:36:00 +02002302/*
2303 * ALSA API channel-map control callbacks
2304 */
2305static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2306 struct snd_ctl_elem_info *uinfo)
2307{
2308 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2309 struct hda_codec *codec = info->private_data;
2310 struct hdmi_spec *spec = codec->spec;
2311 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2312 uinfo->count = spec->channels_max;
2313 uinfo->value.integer.min = 0;
2314 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2315 return 0;
2316}
2317
Anssi Hannula307229d2013-10-24 21:10:34 +03002318static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2319 int channels)
2320{
2321 /* If the speaker allocation matches the channel count, it is OK.*/
2322 if (cap->channels != channels)
2323 return -1;
2324
2325 /* all channels are remappable freely */
2326 return SNDRV_CTL_TLVT_CHMAP_VAR;
2327}
2328
2329static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2330 unsigned int *chmap, int channels)
2331{
2332 int count = 0;
2333 int c;
2334
2335 for (c = 7; c >= 0; c--) {
2336 int spk = cap->speakers[c];
2337 if (!spk)
2338 continue;
2339
2340 chmap[count++] = spk_to_chmap(spk);
2341 }
2342
2343 WARN_ON(count != channels);
2344}
2345
Takashi Iwaid45e6882012-07-31 11:36:00 +02002346static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2347 unsigned int size, unsigned int __user *tlv)
2348{
2349 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2350 struct hda_codec *codec = info->private_data;
2351 struct hdmi_spec *spec = codec->spec;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002352 unsigned int __user *dst;
2353 int chs, count = 0;
2354
2355 if (size < 8)
2356 return -ENOMEM;
2357 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2358 return -EFAULT;
2359 size -= 8;
2360 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02002361 for (chs = 2; chs <= spec->channels_max; chs++) {
Anssi Hannula307229d2013-10-24 21:10:34 +03002362 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002363 struct cea_channel_speaker_allocation *cap;
2364 cap = channel_allocations;
2365 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2366 int chs_bytes = chs * 4;
Anssi Hannula307229d2013-10-24 21:10:34 +03002367 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2368 unsigned int tlv_chmap[8];
2369
2370 if (type < 0)
Takashi Iwaid45e6882012-07-31 11:36:00 +02002371 continue;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002372 if (size < 8)
2373 return -ENOMEM;
Anssi Hannula307229d2013-10-24 21:10:34 +03002374 if (put_user(type, dst) ||
Takashi Iwaid45e6882012-07-31 11:36:00 +02002375 put_user(chs_bytes, dst + 1))
2376 return -EFAULT;
2377 dst += 2;
2378 size -= 8;
2379 count += 8;
2380 if (size < chs_bytes)
2381 return -ENOMEM;
2382 size -= chs_bytes;
2383 count += chs_bytes;
Anssi Hannula307229d2013-10-24 21:10:34 +03002384 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2385 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2386 return -EFAULT;
2387 dst += chs;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002388 }
2389 }
2390 if (put_user(count, tlv + 1))
2391 return -EFAULT;
2392 return 0;
2393}
2394
2395static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_value *ucontrol)
2397{
2398 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2399 struct hda_codec *codec = info->private_data;
2400 struct hdmi_spec *spec = codec->spec;
2401 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002402 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002403 int i;
2404
2405 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2406 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2407 return 0;
2408}
2409
2410static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2411 struct snd_ctl_elem_value *ucontrol)
2412{
2413 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2414 struct hda_codec *codec = info->private_data;
2415 struct hdmi_spec *spec = codec->spec;
2416 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002417 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002418 unsigned int ctl_idx;
2419 struct snd_pcm_substream *substream;
2420 unsigned char chmap[8];
Anssi Hannula307229d2013-10-24 21:10:34 +03002421 int i, err, ca, prepared = 0;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002422
2423 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2424 substream = snd_pcm_chmap_substream(info, ctl_idx);
2425 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01002426 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02002427 switch (substream->runtime->status->state) {
2428 case SNDRV_PCM_STATE_OPEN:
2429 case SNDRV_PCM_STATE_SETUP:
2430 break;
2431 case SNDRV_PCM_STATE_PREPARED:
2432 prepared = 1;
2433 break;
2434 default:
2435 return -EBUSY;
2436 }
2437 memset(chmap, 0, sizeof(chmap));
2438 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2439 chmap[i] = ucontrol->value.integer.value[i];
2440 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2441 return 0;
2442 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2443 if (ca < 0)
2444 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03002445 if (spec->ops.chmap_validate) {
2446 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2447 if (err)
2448 return err;
2449 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002450 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002451 per_pin->chmap_set = true;
2452 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2453 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02002454 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002455 mutex_unlock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002456
2457 return 0;
2458}
2459
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002460static int generic_hdmi_build_pcms(struct hda_codec *codec)
2461{
2462 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002463 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002464
Stephen Warren384a48d2011-06-01 11:14:21 -06002465 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2466 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002467 struct hda_pcm_stream *pstr;
2468
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002469 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002470 if (!info)
2471 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002472
2473 spec->pcm_rec[pin_idx].pcm = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002474 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002475 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002476 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002477
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002478 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002479 pstr->substreams = 1;
2480 pstr->ops = generic_ops;
2481 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002482 }
2483
2484 return 0;
2485}
2486
Libin Yang25e4abb2016-01-12 11:13:27 +08002487static void free_hdmi_jack_priv(struct snd_jack *jack)
Takashi Iwai788d4412015-11-12 15:36:13 +01002488{
Libin Yang25e4abb2016-01-12 11:13:27 +08002489 struct hdmi_pcm *pcm = jack->private_data;
Takashi Iwai788d4412015-11-12 15:36:13 +01002490
Libin Yang25e4abb2016-01-12 11:13:27 +08002491 pcm->jack = NULL;
Takashi Iwai788d4412015-11-12 15:36:13 +01002492}
2493
Libin Yang25e4abb2016-01-12 11:13:27 +08002494static int add_hdmi_jack_kctl(struct hda_codec *codec,
2495 struct hdmi_spec *spec,
2496 int pcm_idx,
Takashi Iwai788d4412015-11-12 15:36:13 +01002497 const char *name)
2498{
2499 struct snd_jack *jack;
2500 int err;
2501
2502 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2503 true, false);
2504 if (err < 0)
2505 return err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002506
2507 spec->pcm_rec[pcm_idx].jack = jack;
2508 jack->private_data = &spec->pcm_rec[pcm_idx];
2509 jack->private_free = free_hdmi_jack_priv;
Takashi Iwai788d4412015-11-12 15:36:13 +01002510 return 0;
2511}
2512
Libin Yang25e4abb2016-01-12 11:13:27 +08002513static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
David Henningsson0b6c49b2011-08-23 16:56:03 +02002514{
Takashi Iwai31ef2252011-12-01 17:41:36 +01002515 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02002516 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002517 struct hdmi_spec_per_pin *per_pin;
2518 struct hda_jack_tbl *jack;
2519 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01002520 bool phantom_jack;
Libin Yang25e4abb2016-01-12 11:13:27 +08002521 int ret;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002522
Takashi Iwai31ef2252011-12-01 17:41:36 +01002523 if (pcmdev > 0)
2524 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Libin Yang25e4abb2016-01-12 11:13:27 +08002525
2526 if (spec->dyn_pcm_assign)
2527 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2528
2529 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2530 /* if !dyn_pcm_assign, it must be non-MST mode.
2531 * This means pcms and pins are statically mapped.
2532 * And pcm_idx is pin_idx.
2533 */
2534 per_pin = get_pin(spec, pcm_idx);
Takashi Iwai909cadc2015-11-12 11:52:13 +01002535 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2536 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01002537 strncat(hdmi_str, " Phantom",
2538 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
Libin Yang25e4abb2016-01-12 11:13:27 +08002539 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2540 phantom_jack);
2541 if (ret < 0)
2542 return ret;
2543 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2544 if (jack == NULL)
2545 return 0;
2546 /* assign jack->jack to pcm_rec[].jack to
2547 * align with dyn_pcm_assign mode
2548 */
2549 spec->pcm_rec[pcm_idx].jack = jack->jack;
2550 return 0;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002551}
2552
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002553static int generic_hdmi_build_controls(struct hda_codec *codec)
2554{
2555 struct hdmi_spec *spec = codec->spec;
2556 int err;
Libin Yang25e4abb2016-01-12 11:13:27 +08002557 int pin_idx, pcm_idx;
2558
2559
2560 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2561 err = generic_hdmi_build_jack(codec, pcm_idx);
2562 if (err < 0)
2563 return err;
Libin Yangb09887f82016-01-29 13:53:27 +08002564
2565 /* create the spdif for each pcm
2566 * pin will be bound when monitor is connected
2567 */
2568 if (spec->dyn_pcm_assign)
2569 err = snd_hda_create_dig_out_ctls(codec,
2570 0, spec->cvt_nids[0],
2571 HDA_PCM_TYPE_HDMI);
2572 else {
2573 struct hdmi_spec_per_pin *per_pin =
2574 get_pin(spec, pcm_idx);
2575 err = snd_hda_create_dig_out_ctls(codec,
2576 per_pin->pin_nid,
2577 per_pin->mux_nids[0],
2578 HDA_PCM_TYPE_HDMI);
2579 }
2580 if (err < 0)
2581 return err;
2582 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang25e4abb2016-01-12 11:13:27 +08002583 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002584
Stephen Warren384a48d2011-06-01 11:14:21 -06002585 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002586 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002587
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05002588 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002589 err = hdmi_create_eld_ctl(codec, pin_idx,
Libin Yang2bea2412016-01-12 11:13:26 +08002590 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05002591
2592 if (err < 0)
2593 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01002594
Takashi Iwai82b1d732011-12-20 15:53:07 +01002595 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002596 }
2597
Takashi Iwaid45e6882012-07-31 11:36:00 +02002598 /* add channel maps */
2599 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002600 struct hda_pcm *pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002601 struct snd_pcm_chmap *chmap;
2602 struct snd_kcontrol *kctl;
2603 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002604
Libin Yang2bea2412016-01-12 11:13:26 +08002605 pcm = get_pcm_rec(spec, pin_idx);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002606 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002607 break;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002608 err = snd_pcm_add_chmap_ctls(pcm->pcm,
Takashi Iwaid45e6882012-07-31 11:36:00 +02002609 SNDRV_PCM_STREAM_PLAYBACK,
2610 NULL, 0, pin_idx, &chmap);
2611 if (err < 0)
2612 return err;
2613 /* override handlers */
2614 chmap->private_data = codec;
2615 kctl = chmap->kctl;
2616 for (i = 0; i < kctl->count; i++)
2617 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2618 kctl->info = hdmi_chmap_ctl_info;
2619 kctl->get = hdmi_chmap_ctl_get;
2620 kctl->put = hdmi_chmap_ctl_put;
2621 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2622 }
2623
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002624 return 0;
2625}
2626
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002627static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2628{
2629 struct hdmi_spec *spec = codec->spec;
2630 int pin_idx;
2631
2632 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002633 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002634
2635 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002636 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002637 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002638 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002639 }
2640 return 0;
2641}
2642
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002643static int generic_hdmi_init(struct hda_codec *codec)
2644{
2645 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002646 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002647
Stephen Warren384a48d2011-06-01 11:14:21 -06002648 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002649 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002650 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002651
2652 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002653 if (!codec_has_acomp(codec))
2654 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2655 codec->jackpoll_interval > 0 ?
2656 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002657 }
2658 return 0;
2659}
2660
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002661static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2662{
2663 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2664 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002665}
2666
2667static void hdmi_array_free(struct hdmi_spec *spec)
2668{
2669 snd_array_free(&spec->pins);
2670 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002671}
2672
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002673static void generic_hdmi_free(struct hda_codec *codec)
2674{
2675 struct hdmi_spec *spec = codec->spec;
Libin Yang25e4abb2016-01-12 11:13:27 +08002676 int pin_idx, pcm_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002677
Takashi Iwai66032492015-12-01 16:49:35 +01002678 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002679 snd_hdac_i915_register_notifier(NULL);
2680
Stephen Warren384a48d2011-06-01 11:14:21 -06002681 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002682 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai2f35c632015-02-27 22:43:26 +01002683 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002684 eld_proc_free(per_pin);
Libin Yang25e4abb2016-01-12 11:13:27 +08002685 }
2686
2687 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2688 if (spec->pcm_rec[pcm_idx].jack == NULL)
2689 continue;
2690 if (spec->dyn_pcm_assign)
2691 snd_device_free(codec->card,
2692 spec->pcm_rec[pcm_idx].jack);
2693 else
2694 spec->pcm_rec[pcm_idx].jack = NULL;
Stephen Warren384a48d2011-06-01 11:14:21 -06002695 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002696
Takashi Iwai55913112015-12-10 13:03:29 +01002697 if (spec->i915_bound)
2698 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002699 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002700 kfree(spec);
2701}
2702
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002703#ifdef CONFIG_PM
2704static int generic_hdmi_resume(struct hda_codec *codec)
2705{
2706 struct hdmi_spec *spec = codec->spec;
2707 int pin_idx;
2708
Pierre Ossmana2833682014-06-18 21:48:09 +02002709 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002710 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002711
2712 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2713 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2714 hdmi_present_sense(per_pin, 1);
2715 }
2716 return 0;
2717}
2718#endif
2719
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002720static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002721 .init = generic_hdmi_init,
2722 .free = generic_hdmi_free,
2723 .build_pcms = generic_hdmi_build_pcms,
2724 .build_controls = generic_hdmi_build_controls,
2725 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002726#ifdef CONFIG_PM
2727 .resume = generic_hdmi_resume,
2728#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002729};
2730
Anssi Hannula307229d2013-10-24 21:10:34 +03002731static const struct hdmi_ops generic_standard_hdmi_ops = {
2732 .pin_get_eld = snd_hdmi_get_eld,
2733 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2734 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2735 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2736 .pin_hbr_setup = hdmi_pin_hbr_setup,
2737 .setup_stream = hdmi_setup_stream,
2738 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2739 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2740};
2741
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002742
2743static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2744 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002745{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002746 struct hdmi_spec *spec = codec->spec;
2747 hda_nid_t conns[4];
2748 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002749
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002750 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2751 if (nconns == spec->num_cvts &&
2752 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002753 return;
2754
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002755 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002756 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002757 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002758}
2759
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002760#define INTEL_VENDOR_NID 0x08
2761#define INTEL_GET_VENDOR_VERB 0xf81
2762#define INTEL_SET_VENDOR_VERB 0x781
2763#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2764#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2765
2766static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002767 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002768{
2769 unsigned int vendor_param;
2770
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002771 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2772 INTEL_GET_VENDOR_VERB, 0);
2773 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2774 return;
2775
2776 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2777 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2778 INTEL_SET_VENDOR_VERB, vendor_param);
2779 if (vendor_param == -1)
2780 return;
2781
Takashi Iwai17df3f52013-05-08 08:09:34 +02002782 if (update_tree)
2783 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002784}
2785
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002786static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2787{
2788 unsigned int vendor_param;
2789
2790 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2791 INTEL_GET_VENDOR_VERB, 0);
2792 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2793 return;
2794
2795 /* enable DP1.2 mode */
2796 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002797 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002798 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2799 INTEL_SET_VENDOR_VERB, vendor_param);
2800}
2801
Takashi Iwai17df3f52013-05-08 08:09:34 +02002802/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2803 * Otherwise you may get severe h/w communication errors.
2804 */
2805static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2806 unsigned int power_state)
2807{
2808 if (power_state == AC_PWRST_D0) {
2809 intel_haswell_enable_all_pins(codec, false);
2810 intel_haswell_fixup_enable_dp12(codec);
2811 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002812
Takashi Iwai17df3f52013-05-08 08:09:34 +02002813 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2814 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2815}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002816
David Henningssonf0675d42015-09-03 11:51:34 +02002817static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002818{
2819 struct hda_codec *codec = audio_ptr;
2820 int pin_nid = port + 0x04;
2821
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002822 /* skip notification during system suspend (but not in runtime PM);
2823 * the state will be updated at resume
2824 */
2825 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2826 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002827 /* ditto during suspend/resume process itself */
2828 if (atomic_read(&(codec)->core.in_pm))
2829 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002830
David Henningsson25adc132015-08-19 10:48:58 +02002831 check_presence_and_report(codec, pin_nid);
2832}
2833
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002834static int patch_generic_hdmi(struct hda_codec *codec)
2835{
2836 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002837
2838 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2839 if (spec == NULL)
2840 return -ENOMEM;
2841
Anssi Hannula307229d2013-10-24 21:10:34 +03002842 spec->ops = generic_standard_hdmi_ops;
Libin Yang42b29872015-12-16 13:42:42 +08002843 mutex_init(&spec->pcm_lock);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002844 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002845 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002846
Takashi Iwai55913112015-12-10 13:03:29 +01002847 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2848 if (!codec_has_acomp(codec) &&
2849 (codec->core.vendor_id >> 16) == 0x8086)
2850 if (!snd_hdac_i915_init(&codec->bus->core))
2851 spec->i915_bound = true;
2852
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002853 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002854 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002855 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002856 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002857
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002858 /* For Valleyview/Cherryview, only the display codec is in the display
2859 * power well and can use link_power ops to request/release the power.
2860 * For Haswell/Broadwell, the controller is also in the power well and
2861 * can cover the codec power request, and so need not set this flag.
2862 * For previous platforms, there is no such power well feature.
2863 */
Lu, Hanff9d8852015-11-19 23:25:13 +08002864 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2865 is_broxton(codec))
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002866 codec->core.link_power_control = 1;
2867
Takashi Iwai66032492015-12-01 16:49:35 +01002868 if (codec_has_acomp(codec)) {
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002869 codec->depop_delay = 0;
David Henningsson25adc132015-08-19 10:48:58 +02002870 spec->i915_audio_ops.audio_ptr = codec;
2871 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2872 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2873 }
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002874
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002875 if (hdmi_parse_codec(codec) < 0) {
Takashi Iwai55913112015-12-10 13:03:29 +01002876 if (spec->i915_bound)
2877 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002878 codec->spec = NULL;
2879 kfree(spec);
2880 return -EINVAL;
2881 }
2882 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002883 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002884 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002885 codec->dp_mst = true;
2886 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002887
Lu, Han2377c3c2015-06-09 16:50:38 +08002888 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2889 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2890 codec->auto_runtime_pm = 1;
2891
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002892 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002893
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002894 init_channel_allocations();
2895
Libin Yang25e4abb2016-01-12 11:13:27 +08002896 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002897 return 0;
2898}
2899
2900/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002901 * Shared non-generic implementations
2902 */
2903
2904static int simple_playback_build_pcms(struct hda_codec *codec)
2905{
2906 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002907 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002908 unsigned int chans;
2909 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002910 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002911
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002912 per_cvt = get_cvt(spec, 0);
2913 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002914 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002915
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002916 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002917 if (!info)
2918 return -ENOMEM;
Libin Yang2bea2412016-01-12 11:13:26 +08002919 spec->pcm_rec[0].pcm = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002920 info->pcm_type = HDA_PCM_TYPE_HDMI;
2921 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2922 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002923 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002924 if (pstr->channels_max <= 2 && chans && chans <= 16)
2925 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002926
2927 return 0;
2928}
2929
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002930/* unsolicited event for jack sensing */
2931static void simple_hdmi_unsol_event(struct hda_codec *codec,
2932 unsigned int res)
2933{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002934 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002935 snd_hda_jack_report_sync(codec);
2936}
2937
2938/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2939 * as long as spec->pins[] is set correctly
2940 */
2941#define simple_hdmi_build_jack generic_hdmi_build_jack
2942
Stephen Warren3aaf8982011-06-01 11:14:19 -06002943static int simple_playback_build_controls(struct hda_codec *codec)
2944{
2945 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002946 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002947 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002948
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002949 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002950 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2951 per_cvt->cvt_nid,
2952 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002953 if (err < 0)
2954 return err;
2955 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002956}
2957
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002958static int simple_playback_init(struct hda_codec *codec)
2959{
2960 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002961 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2962 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002963
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002964 snd_hda_codec_write(codec, pin, 0,
2965 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2966 /* some codecs require to unmute the pin */
2967 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2968 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2969 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002970 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002971 return 0;
2972}
2973
Stephen Warren3aaf8982011-06-01 11:14:19 -06002974static void simple_playback_free(struct hda_codec *codec)
2975{
2976 struct hdmi_spec *spec = codec->spec;
2977
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002978 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002979 kfree(spec);
2980}
2981
2982/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002983 * Nvidia specific implementations
2984 */
2985
2986#define Nv_VERB_SET_Channel_Allocation 0xF79
2987#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2988#define Nv_VERB_SET_Audio_Protection_On 0xF98
2989#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2990
2991#define nvhdmi_master_con_nid_7x 0x04
2992#define nvhdmi_master_pin_nid_7x 0x05
2993
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002994static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002995 /*front, rear, clfe, rear_surr */
2996 0x6, 0x8, 0xa, 0xc,
2997};
2998
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002999static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3000 /* set audio protect on */
3001 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3002 /* enable digital output on pin widget */
3003 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3004 {} /* terminator */
3005};
3006
3007static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003008 /* set audio protect on */
3009 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3010 /* enable digital output on pin widget */
3011 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3012 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3013 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3014 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3015 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3016 {} /* terminator */
3017};
3018
3019#ifdef LIMITED_RATE_FMT_SUPPORT
3020/* support only the safe format and rate */
3021#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3022#define SUPPORTED_MAXBPS 16
3023#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3024#else
3025/* support all rates and formats */
3026#define SUPPORTED_RATES \
3027 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3028 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3029 SNDRV_PCM_RATE_192000)
3030#define SUPPORTED_MAXBPS 24
3031#define SUPPORTED_FORMATS \
3032 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3033#endif
3034
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003035static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003036{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003037 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3038 return 0;
3039}
3040
3041static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3042{
3043 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003044 return 0;
3045}
3046
Nitin Daga393004b2011-01-10 21:49:31 +05303047static unsigned int channels_2_6_8[] = {
3048 2, 6, 8
3049};
3050
3051static unsigned int channels_2_8[] = {
3052 2, 8
3053};
3054
3055static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3056 .count = ARRAY_SIZE(channels_2_6_8),
3057 .list = channels_2_6_8,
3058 .mask = 0,
3059};
3060
3061static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3062 .count = ARRAY_SIZE(channels_2_8),
3063 .list = channels_2_8,
3064 .mask = 0,
3065};
3066
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003067static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3068 struct hda_codec *codec,
3069 struct snd_pcm_substream *substream)
3070{
3071 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05303072 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3073
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003074 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05303075 case 0x10de0002:
3076 case 0x10de0003:
3077 case 0x10de0005:
3078 case 0x10de0006:
3079 hw_constraints_channels = &hw_constraints_2_8_channels;
3080 break;
3081 case 0x10de0007:
3082 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3083 break;
3084 default:
3085 break;
3086 }
3087
3088 if (hw_constraints_channels != NULL) {
3089 snd_pcm_hw_constraint_list(substream->runtime, 0,
3090 SNDRV_PCM_HW_PARAM_CHANNELS,
3091 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01003092 } else {
3093 snd_pcm_hw_constraint_step(substream->runtime, 0,
3094 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05303095 }
3096
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003097 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3098}
3099
3100static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3101 struct hda_codec *codec,
3102 struct snd_pcm_substream *substream)
3103{
3104 struct hdmi_spec *spec = codec->spec;
3105 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3106}
3107
3108static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3109 struct hda_codec *codec,
3110 unsigned int stream_tag,
3111 unsigned int format,
3112 struct snd_pcm_substream *substream)
3113{
3114 struct hdmi_spec *spec = codec->spec;
3115 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3116 stream_tag, format, substream);
3117}
3118
Takashi Iwaid0b12522012-06-15 14:34:42 +02003119static const struct hda_pcm_stream simple_pcm_playback = {
3120 .substreams = 1,
3121 .channels_min = 2,
3122 .channels_max = 2,
3123 .ops = {
3124 .open = simple_playback_pcm_open,
3125 .close = simple_playback_pcm_close,
3126 .prepare = simple_playback_pcm_prepare
3127 },
3128};
3129
3130static const struct hda_codec_ops simple_hdmi_patch_ops = {
3131 .build_controls = simple_playback_build_controls,
3132 .build_pcms = simple_playback_build_pcms,
3133 .init = simple_playback_init,
3134 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02003135 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02003136};
3137
3138static int patch_simple_hdmi(struct hda_codec *codec,
3139 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3140{
3141 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003142 struct hdmi_spec_per_cvt *per_cvt;
3143 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003144
3145 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3146 if (!spec)
3147 return -ENOMEM;
3148
3149 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003150 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02003151
3152 spec->multiout.num_dacs = 0; /* no analog */
3153 spec->multiout.max_channels = 2;
3154 spec->multiout.dig_out_nid = cvt_nid;
3155 spec->num_cvts = 1;
3156 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003157 per_pin = snd_array_new(&spec->pins);
3158 per_cvt = snd_array_new(&spec->cvts);
3159 if (!per_pin || !per_cvt) {
3160 simple_playback_free(codec);
3161 return -ENOMEM;
3162 }
3163 per_cvt->cvt_nid = cvt_nid;
3164 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003165 spec->pcm_playback = simple_pcm_playback;
3166
3167 codec->patch_ops = simple_hdmi_patch_ops;
3168
3169 return 0;
3170}
3171
Aaron Plattner1f348522011-04-06 17:19:04 -07003172static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3173 int channels)
3174{
3175 unsigned int chanmask;
3176 int chan = channels ? (channels - 1) : 1;
3177
3178 switch (channels) {
3179 default:
3180 case 0:
3181 case 2:
3182 chanmask = 0x00;
3183 break;
3184 case 4:
3185 chanmask = 0x08;
3186 break;
3187 case 6:
3188 chanmask = 0x0b;
3189 break;
3190 case 8:
3191 chanmask = 0x13;
3192 break;
3193 }
3194
3195 /* Set the audio infoframe channel allocation and checksum fields. The
3196 * channel count is computed implicitly by the hardware. */
3197 snd_hda_codec_write(codec, 0x1, 0,
3198 Nv_VERB_SET_Channel_Allocation, chanmask);
3199
3200 snd_hda_codec_write(codec, 0x1, 0,
3201 Nv_VERB_SET_Info_Frame_Checksum,
3202 (0x71 - chan - chanmask));
3203}
3204
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003205static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3206 struct hda_codec *codec,
3207 struct snd_pcm_substream *substream)
3208{
3209 struct hdmi_spec *spec = codec->spec;
3210 int i;
3211
3212 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3213 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3214 for (i = 0; i < 4; i++) {
3215 /* set the stream id */
3216 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3217 AC_VERB_SET_CHANNEL_STREAMID, 0);
3218 /* set the stream format */
3219 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3220 AC_VERB_SET_STREAM_FORMAT, 0);
3221 }
3222
Aaron Plattner1f348522011-04-06 17:19:04 -07003223 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3224 * streams are disabled. */
3225 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3226
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003227 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3228}
3229
3230static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3231 struct hda_codec *codec,
3232 unsigned int stream_tag,
3233 unsigned int format,
3234 struct snd_pcm_substream *substream)
3235{
3236 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003237 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003238 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003239 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003240 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003241 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003242
3243 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003244 per_cvt = get_cvt(spec, 0);
3245 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003246
3247 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003248
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003249 dataDCC2 = 0x2;
3250
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003251 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003252 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003253 snd_hda_codec_write(codec,
3254 nvhdmi_master_con_nid_7x,
3255 0,
3256 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003257 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003258
3259 /* set the stream id */
3260 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3261 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3262
3263 /* set the stream format */
3264 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3265 AC_VERB_SET_STREAM_FORMAT, format);
3266
3267 /* turn on again (if needed) */
3268 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003269 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003270 snd_hda_codec_write(codec,
3271 nvhdmi_master_con_nid_7x,
3272 0,
3273 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003274 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003275 snd_hda_codec_write(codec,
3276 nvhdmi_master_con_nid_7x,
3277 0,
3278 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3279 }
3280
3281 for (i = 0; i < 4; i++) {
3282 if (chs == 2)
3283 channel_id = 0;
3284 else
3285 channel_id = i * 2;
3286
3287 /* turn off SPDIF once;
3288 *otherwise the IEC958 bits won't be updated
3289 */
3290 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003291 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003292 snd_hda_codec_write(codec,
3293 nvhdmi_con_nids_7x[i],
3294 0,
3295 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003296 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003297 /* set the stream id */
3298 snd_hda_codec_write(codec,
3299 nvhdmi_con_nids_7x[i],
3300 0,
3301 AC_VERB_SET_CHANNEL_STREAMID,
3302 (stream_tag << 4) | channel_id);
3303 /* set the stream format */
3304 snd_hda_codec_write(codec,
3305 nvhdmi_con_nids_7x[i],
3306 0,
3307 AC_VERB_SET_STREAM_FORMAT,
3308 format);
3309 /* turn on again (if needed) */
3310 /* enable and set the channel status audio/data flag */
3311 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003312 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003313 snd_hda_codec_write(codec,
3314 nvhdmi_con_nids_7x[i],
3315 0,
3316 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003317 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003318 snd_hda_codec_write(codec,
3319 nvhdmi_con_nids_7x[i],
3320 0,
3321 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3322 }
3323 }
3324
Aaron Plattner1f348522011-04-06 17:19:04 -07003325 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003326
3327 mutex_unlock(&codec->spdif_mutex);
3328 return 0;
3329}
3330
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003331static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003332 .substreams = 1,
3333 .channels_min = 2,
3334 .channels_max = 8,
3335 .nid = nvhdmi_master_con_nid_7x,
3336 .rates = SUPPORTED_RATES,
3337 .maxbps = SUPPORTED_MAXBPS,
3338 .formats = SUPPORTED_FORMATS,
3339 .ops = {
3340 .open = simple_playback_pcm_open,
3341 .close = nvhdmi_8ch_7x_pcm_close,
3342 .prepare = nvhdmi_8ch_7x_pcm_prepare
3343 },
3344};
3345
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003346static int patch_nvhdmi_2ch(struct hda_codec *codec)
3347{
3348 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003349 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3350 nvhdmi_master_pin_nid_7x);
3351 if (err < 0)
3352 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003353
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003354 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003355 /* override the PCM rates, etc, as the codec doesn't give full list */
3356 spec = codec->spec;
3357 spec->pcm_playback.rates = SUPPORTED_RATES;
3358 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3359 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003360 return 0;
3361}
3362
Takashi Iwai53775b02012-08-01 12:17:41 +02003363static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3364{
3365 struct hdmi_spec *spec = codec->spec;
3366 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003367 if (!err) {
3368 struct hda_pcm *info = get_pcm_rec(spec, 0);
3369 info->own_chmap = true;
3370 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003371 return err;
3372}
3373
3374static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3375{
3376 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003377 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003378 struct snd_pcm_chmap *chmap;
3379 int err;
3380
3381 err = simple_playback_build_controls(codec);
3382 if (err < 0)
3383 return err;
3384
3385 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003386 info = get_pcm_rec(spec, 0);
3387 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003388 SNDRV_PCM_STREAM_PLAYBACK,
3389 snd_pcm_alt_chmaps, 8, 0, &chmap);
3390 if (err < 0)
3391 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003392 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003393 case 0x10de0002:
3394 case 0x10de0003:
3395 case 0x10de0005:
3396 case 0x10de0006:
3397 chmap->channel_mask = (1U << 2) | (1U << 8);
3398 break;
3399 case 0x10de0007:
3400 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3401 }
3402 return 0;
3403}
3404
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003405static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3406{
3407 struct hdmi_spec *spec;
3408 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003409 if (err < 0)
3410 return err;
3411 spec = codec->spec;
3412 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003413 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003414 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003415 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3416 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003417
3418 /* Initialize the audio infoframe channel mask and checksum to something
3419 * valid */
3420 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3421
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003422 return 0;
3423}
3424
3425/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003426 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3427 * - 0x10de0015
3428 * - 0x10de0040
3429 */
3430static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3431 int channels)
3432{
3433 if (cap->ca_index == 0x00 && channels == 2)
3434 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3435
3436 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3437}
3438
3439static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3440{
3441 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3442 return -EINVAL;
3443
3444 return 0;
3445}
3446
3447static int patch_nvhdmi(struct hda_codec *codec)
3448{
3449 struct hdmi_spec *spec;
3450 int err;
3451
3452 err = patch_generic_hdmi(codec);
3453 if (err)
3454 return err;
3455
3456 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003457 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003458
3459 spec->ops.chmap_cea_alloc_validate_get_type =
3460 nvhdmi_chmap_cea_alloc_validate_get_type;
3461 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3462
3463 return 0;
3464}
3465
3466/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003467 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3468 * accessed using vendor-defined verbs. These registers can be used for
3469 * interoperability between the HDA and HDMI drivers.
3470 */
3471
3472/* Audio Function Group node */
3473#define NVIDIA_AFG_NID 0x01
3474
3475/*
3476 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3477 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3478 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3479 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3480 * additional bit (at position 30) to signal the validity of the format.
3481 *
3482 * | 31 | 30 | 29 16 | 15 0 |
3483 * +---------+-------+--------+--------+
3484 * | TRIGGER | VALID | UNUSED | FORMAT |
3485 * +-----------------------------------|
3486 *
3487 * Note that for the trigger bit to take effect it needs to change value
3488 * (i.e. it needs to be toggled).
3489 */
3490#define NVIDIA_GET_SCRATCH0 0xfa6
3491#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3492#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3493#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3494#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3495#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3496#define NVIDIA_SCRATCH_VALID (1 << 6)
3497
3498#define NVIDIA_GET_SCRATCH1 0xfab
3499#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3500#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3501#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3502#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3503
3504/*
3505 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3506 * the format is invalidated so that the HDMI codec can be disabled.
3507 */
3508static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3509{
3510 unsigned int value;
3511
3512 /* bits [31:30] contain the trigger and valid bits */
3513 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3514 NVIDIA_GET_SCRATCH0, 0);
3515 value = (value >> 24) & 0xff;
3516
3517 /* bits [15:0] are used to store the HDA format */
3518 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3519 NVIDIA_SET_SCRATCH0_BYTE0,
3520 (format >> 0) & 0xff);
3521 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3522 NVIDIA_SET_SCRATCH0_BYTE1,
3523 (format >> 8) & 0xff);
3524
3525 /* bits [16:24] are unused */
3526 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3527 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3528
3529 /*
3530 * Bit 30 signals that the data is valid and hence that HDMI audio can
3531 * be enabled.
3532 */
3533 if (format == 0)
3534 value &= ~NVIDIA_SCRATCH_VALID;
3535 else
3536 value |= NVIDIA_SCRATCH_VALID;
3537
3538 /*
3539 * Whenever the trigger bit is toggled, an interrupt is raised in the
3540 * HDMI codec. The HDMI driver will use that as trigger to update its
3541 * configuration.
3542 */
3543 value ^= NVIDIA_SCRATCH_TRIGGER;
3544
3545 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3546 NVIDIA_SET_SCRATCH0_BYTE3, value);
3547}
3548
3549static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3550 struct hda_codec *codec,
3551 unsigned int stream_tag,
3552 unsigned int format,
3553 struct snd_pcm_substream *substream)
3554{
3555 int err;
3556
3557 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3558 format, substream);
3559 if (err < 0)
3560 return err;
3561
3562 /* notify the HDMI codec of the format change */
3563 tegra_hdmi_set_format(codec, format);
3564
3565 return 0;
3566}
3567
3568static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3569 struct hda_codec *codec,
3570 struct snd_pcm_substream *substream)
3571{
3572 /* invalidate the format in the HDMI codec */
3573 tegra_hdmi_set_format(codec, 0);
3574
3575 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3576}
3577
3578static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3579{
3580 struct hdmi_spec *spec = codec->spec;
3581 unsigned int i;
3582
3583 for (i = 0; i < spec->num_pins; i++) {
3584 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3585
3586 if (pcm->pcm_type == type)
3587 return pcm;
3588 }
3589
3590 return NULL;
3591}
3592
3593static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3594{
3595 struct hda_pcm_stream *stream;
3596 struct hda_pcm *pcm;
3597 int err;
3598
3599 err = generic_hdmi_build_pcms(codec);
3600 if (err < 0)
3601 return err;
3602
3603 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3604 if (!pcm)
3605 return -ENODEV;
3606
3607 /*
3608 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3609 * codec about format changes.
3610 */
3611 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3612 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3613 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3614
3615 return 0;
3616}
3617
3618static int patch_tegra_hdmi(struct hda_codec *codec)
3619{
3620 int err;
3621
3622 err = patch_generic_hdmi(codec);
3623 if (err)
3624 return err;
3625
3626 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3627
3628 return 0;
3629}
3630
3631/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003632 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003633 */
3634
Anssi Hannula5a6135842013-10-24 21:10:35 +03003635#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003636 ((codec)->core.vendor_id == 0x1002aa01 && \
3637 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003638#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003639
Anssi Hannula5a6135842013-10-24 21:10:35 +03003640/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3641#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3642#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3643#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3644#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3645#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3646#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003647#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003648#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3649#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3650#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3651#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3652#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3653#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3654#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3655#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3656#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3657#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3658#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003659#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003660#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3661#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3662#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3663#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3664#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3665
Anssi Hannula84d69e72013-10-24 21:10:38 +03003666/* AMD specific HDA cvt verbs */
3667#define ATI_VERB_SET_RAMP_RATE 0x770
3668#define ATI_VERB_GET_RAMP_RATE 0xf70
3669
Anssi Hannula5a6135842013-10-24 21:10:35 +03003670#define ATI_OUT_ENABLE 0x1
3671
3672#define ATI_MULTICHANNEL_MODE_PAIRED 0
3673#define ATI_MULTICHANNEL_MODE_SINGLE 1
3674
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003675#define ATI_HBR_CAPABLE 0x01
3676#define ATI_HBR_ENABLE 0x10
3677
Anssi Hannula89250f82013-10-24 21:10:36 +03003678static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3679 unsigned char *buf, int *eld_size)
3680{
3681 /* call hda_eld.c ATI/AMD-specific function */
3682 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3683 is_amdhdmi_rev3_or_later(codec));
3684}
3685
Anssi Hannula5a6135842013-10-24 21:10:35 +03003686static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3687 int active_channels, int conn_type)
3688{
3689 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3690}
3691
3692static int atihdmi_paired_swap_fc_lfe(int pos)
3693{
3694 /*
3695 * ATI/AMD have automatic FC/LFE swap built-in
3696 * when in pairwise mapping mode.
3697 */
3698
3699 switch (pos) {
3700 /* see channel_allocations[].speakers[] */
3701 case 2: return 3;
3702 case 3: return 2;
3703 default: break;
3704 }
3705
3706 return pos;
3707}
3708
3709static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3710{
3711 struct cea_channel_speaker_allocation *cap;
3712 int i, j;
3713
3714 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3715
3716 cap = &channel_allocations[get_channel_allocation_order(ca)];
3717 for (i = 0; i < chs; ++i) {
3718 int mask = to_spk_mask(map[i]);
3719 bool ok = false;
3720 bool companion_ok = false;
3721
3722 if (!mask)
3723 continue;
3724
3725 for (j = 0 + i % 2; j < 8; j += 2) {
3726 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3727 if (cap->speakers[chan_idx] == mask) {
3728 /* channel is in a supported position */
3729 ok = true;
3730
3731 if (i % 2 == 0 && i + 1 < chs) {
3732 /* even channel, check the odd companion */
3733 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3734 int comp_mask_req = to_spk_mask(map[i+1]);
3735 int comp_mask_act = cap->speakers[comp_chan_idx];
3736
3737 if (comp_mask_req == comp_mask_act)
3738 companion_ok = true;
3739 else
3740 return -EINVAL;
3741 }
3742 break;
3743 }
3744 }
3745
3746 if (!ok)
3747 return -EINVAL;
3748
3749 if (companion_ok)
3750 i++; /* companion channel already checked */
3751 }
3752
3753 return 0;
3754}
3755
3756static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3757 int hdmi_slot, int stream_channel)
3758{
3759 int verb;
3760 int ati_channel_setup = 0;
3761
3762 if (hdmi_slot > 7)
3763 return -EINVAL;
3764
3765 if (!has_amd_full_remap_support(codec)) {
3766 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3767
3768 /* In case this is an odd slot but without stream channel, do not
3769 * disable the slot since the corresponding even slot could have a
3770 * channel. In case neither have a channel, the slot pair will be
3771 * disabled when this function is called for the even slot. */
3772 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3773 return 0;
3774
3775 hdmi_slot -= hdmi_slot % 2;
3776
3777 if (stream_channel != 0xf)
3778 stream_channel -= stream_channel % 2;
3779 }
3780
3781 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3782
3783 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3784
3785 if (stream_channel != 0xf)
3786 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3787
3788 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3789}
3790
3791static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3792 int asp_slot)
3793{
3794 bool was_odd = false;
3795 int ati_asp_slot = asp_slot;
3796 int verb;
3797 int ati_channel_setup;
3798
3799 if (asp_slot > 7)
3800 return -EINVAL;
3801
3802 if (!has_amd_full_remap_support(codec)) {
3803 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3804 if (ati_asp_slot % 2 != 0) {
3805 ati_asp_slot -= 1;
3806 was_odd = true;
3807 }
3808 }
3809
3810 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3811
3812 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3813
3814 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3815 return 0xf;
3816
3817 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3818}
3819
3820static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3821 int channels)
3822{
3823 int c;
3824
3825 /*
3826 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3827 * we need to take that into account (a single channel may take 2
3828 * channel slots if we need to carry a silent channel next to it).
3829 * On Rev3+ AMD codecs this function is not used.
3830 */
3831 int chanpairs = 0;
3832
3833 /* We only produce even-numbered channel count TLVs */
3834 if ((channels % 2) != 0)
3835 return -1;
3836
3837 for (c = 0; c < 7; c += 2) {
3838 if (cap->speakers[c] || cap->speakers[c+1])
3839 chanpairs++;
3840 }
3841
3842 if (chanpairs * 2 != channels)
3843 return -1;
3844
3845 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3846}
3847
3848static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3849 unsigned int *chmap, int channels)
3850{
3851 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3852 int count = 0;
3853 int c;
3854
3855 for (c = 7; c >= 0; c--) {
3856 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3857 int spk = cap->speakers[chan];
3858 if (!spk) {
3859 /* add N/A channel if the companion channel is occupied */
3860 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3861 chmap[count++] = SNDRV_CHMAP_NA;
3862
3863 continue;
3864 }
3865
3866 chmap[count++] = spk_to_chmap(spk);
3867 }
3868
3869 WARN_ON(count != channels);
3870}
3871
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003872static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3873 bool hbr)
3874{
3875 int hbr_ctl, hbr_ctl_new;
3876
3877 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003878 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003879 if (hbr)
3880 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3881 else
3882 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3883
Takashi Iwai4e76a882014-02-25 12:21:03 +01003884 codec_dbg(codec,
3885 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003886 pin_nid,
3887 hbr_ctl == hbr_ctl_new ? "" : "new-",
3888 hbr_ctl_new);
3889
3890 if (hbr_ctl != hbr_ctl_new)
3891 snd_hda_codec_write(codec, pin_nid, 0,
3892 ATI_VERB_SET_HBR_CONTROL,
3893 hbr_ctl_new);
3894
3895 } else if (hbr)
3896 return -EINVAL;
3897
3898 return 0;
3899}
3900
Anssi Hannula84d69e72013-10-24 21:10:38 +03003901static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3902 hda_nid_t pin_nid, u32 stream_tag, int format)
3903{
3904
3905 if (is_amdhdmi_rev3_or_later(codec)) {
3906 int ramp_rate = 180; /* default as per AMD spec */
3907 /* disable ramp-up/down for non-pcm as per AMD spec */
3908 if (format & AC_FMT_TYPE_NON_PCM)
3909 ramp_rate = 0;
3910
3911 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3912 }
3913
3914 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3915}
3916
3917
Anssi Hannula5a6135842013-10-24 21:10:35 +03003918static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003919{
3920 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003921 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003922
Anssi Hannula5a6135842013-10-24 21:10:35 +03003923 err = generic_hdmi_init(codec);
3924
3925 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003926 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003927
3928 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3929 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3930
3931 /* make sure downmix information in infoframe is zero */
3932 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3933
3934 /* enable channel-wise remap mode if supported */
3935 if (has_amd_full_remap_support(codec))
3936 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3937 ATI_VERB_SET_MULTICHANNEL_MODE,
3938 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003939 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003940
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003941 return 0;
3942}
3943
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003944static int patch_atihdmi(struct hda_codec *codec)
3945{
3946 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003947 struct hdmi_spec_per_cvt *per_cvt;
3948 int err, cvt_idx;
3949
3950 err = patch_generic_hdmi(codec);
3951
3952 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003953 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003954
3955 codec->patch_ops.init = atihdmi_init;
3956
Takashi Iwaid0b12522012-06-15 14:34:42 +02003957 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003958
Anssi Hannula89250f82013-10-24 21:10:36 +03003959 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003960 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3961 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3962 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003963 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003964 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003965
3966 if (!has_amd_full_remap_support(codec)) {
3967 /* override to ATI/AMD-specific versions with pairwise mapping */
3968 spec->ops.chmap_cea_alloc_validate_get_type =
3969 atihdmi_paired_chmap_cea_alloc_validate_get_type;
3970 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
3971 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
3972 }
3973
3974 /* ATI/AMD converters do not advertise all of their capabilities */
3975 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3976 per_cvt = get_cvt(spec, cvt_idx);
3977 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3978 per_cvt->rates |= SUPPORTED_RATES;
3979 per_cvt->formats |= SUPPORTED_FORMATS;
3980 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3981 }
3982
3983 spec->channels_max = max(spec->channels_max, 8u);
3984
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003985 return 0;
3986}
3987
Annie Liu3de5ff82012-06-08 19:18:42 +08003988/* VIA HDMI Implementation */
3989#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3990#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3991
Annie Liu3de5ff82012-06-08 19:18:42 +08003992static int patch_via_hdmi(struct hda_codec *codec)
3993{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003994 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003995}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003996
3997/*
3998 * patch entries
3999 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004000static const struct hda_device_id snd_hda_id_hdmi[] = {
4001HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4002HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4003HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4004HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4005HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4006HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4007HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4008HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4009HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4010HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4011HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4012HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4013HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4014HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4015HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4016HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4017HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4018HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4019HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4020HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4021HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4022HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4023HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01004024/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004025HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4026HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4027HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4028HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4029HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4030HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4031HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4032HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4033HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4034HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4035HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4036HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4037HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4038HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4039HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4040HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4041HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4042HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4043HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4044HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4045HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
4046HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4047HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4048HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4049HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4050HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4051HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
4052HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4053HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4054HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4055HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
4056HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4057HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4058HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4059HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4060HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4061HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
Libin Yang91815d82016-01-14 14:09:00 +08004062HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004063HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4064HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4065HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4066HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004067/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004068HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004069{} /* terminator */
4070};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004071MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004072
4073MODULE_LICENSE("GPL");
4074MODULE_DESCRIPTION("HDMI HD-audio codec");
4075MODULE_ALIAS("snd-hda-codec-intelhdmi");
4076MODULE_ALIAS("snd-hda-codec-nvhdmi");
4077MODULE_ALIAS("snd-hda-codec-atihdmi");
4078
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004079static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004080 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004081};
4082
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004083module_hda_codec_driver(hdmi_driver);