blob: 796d737366ae0f978168eb0e6dd43803e0cb7c2e [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;
Takashi Iwai788d4412015-11-12 15:36:13 +010090 struct snd_jack *acomp_jack; /* jack via audio component */
Libin Yang6590faa2015-12-16 13:42:41 +080091 struct hda_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Libin Yanga76056f2015-12-16 16:48:15 +080092 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
Wu Fengguangc6e84532011-11-18 16:59:32 -060093 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020094 bool setup; /* the stream has been set up by prepare callback */
95 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020096 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020097 bool chmap_set; /* channel-map override by ALSA API? */
98 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080099#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200100 struct snd_info_entry *proc_entry;
101#endif
Stephen Warren384a48d2011-06-01 11:14:21 -0600102};
103
Anssi Hannula307229d2013-10-24 21:10:34 +0300104struct cea_channel_speaker_allocation;
105
106/* operations used by generic code that can be overridden by patches */
107struct hdmi_ops {
108 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
109 unsigned char *buf, int *eld_size);
110
111 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
112 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
113 int asp_slot);
114 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
115 int asp_slot, int channel);
116
117 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
118 int ca, int active_channels, int conn_type);
119
120 /* enable/disable HBR (HD passthrough) */
121 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
122
123 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
124 hda_nid_t pin_nid, u32 stream_tag, int format);
125
126 /* Helpers for producing the channel map TLVs. These can be overridden
127 * for devices that have non-standard mapping requirements. */
128 int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
129 int channels);
130 void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
131 unsigned int *chmap, int channels);
132
133 /* check that the user-given chmap is supported */
134 int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
135};
136
Wu Fengguang079d88c2010-03-08 10:44:23 +0800137struct hdmi_spec {
138 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100139 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
140 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600141
Wu Fengguang079d88c2010-03-08 10:44:23 +0800142 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100143 struct snd_array pins; /* struct hdmi_spec_per_pin */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100144 struct hda_pcm *pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800145 struct mutex pcm_lock;
Libin Yanga76056f2015-12-16 16:48:15 +0800146 /* pcm_bitmap means which pcms have been assigned to pins*/
147 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800148 int pcm_used; /* counter of pcm_rec[] */
Libin Yangac983792015-12-16 16:48:16 +0800149 /* bitmap shows whether the pcm is opened in user space
150 * bit 0 means the first playback PCM (PCM3);
151 * bit 1 means the second playback PCM, and so on.
152 */
153 unsigned long pcm_in_use;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200154 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800155
David Henningsson4bd038f2013-02-19 16:11:25 +0100156 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300157 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700158
159 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800160 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800161 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300162 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800163 */
164 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200165 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200166
167 /* i915/powerwell (Haswell+/Valleyview+) specific */
168 struct i915_audio_component_audio_ops i915_audio_ops;
Takashi Iwai55913112015-12-10 13:03:29 +0100169 bool i915_bound; /* was i915 bound in this driver? */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800170};
171
Takashi Iwaif4e30402015-12-10 13:01:28 +0100172#ifdef CONFIG_SND_HDA_I915
Takashi Iwai66032492015-12-01 16:49:35 +0100173#define codec_has_acomp(codec) \
174 ((codec)->bus->core.audio_component != NULL)
Takashi Iwaif4e30402015-12-10 13:01:28 +0100175#else
176#define codec_has_acomp(codec) false
177#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800178
179struct hdmi_audio_infoframe {
180 u8 type; /* 0x84 */
181 u8 ver; /* 0x01 */
182 u8 len; /* 0x0a */
183
Wu Fengguang53d7d692010-09-21 14:25:49 +0800184 u8 checksum;
185
Wu Fengguang079d88c2010-03-08 10:44:23 +0800186 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
187 u8 SS01_SF24;
188 u8 CXT04;
189 u8 CA;
190 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800191};
192
193struct dp_audio_infoframe {
194 u8 type; /* 0x84 */
195 u8 len; /* 0x1b */
196 u8 ver; /* 0x11 << 2 */
197
198 u8 CC02_CT47; /* match with HDMI infoframe from this on */
199 u8 SS01_SF24;
200 u8 CXT04;
201 u8 CA;
202 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800203};
204
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100205union audio_infoframe {
206 struct hdmi_audio_infoframe hdmi;
207 struct dp_audio_infoframe dp;
208 u8 bytes[0];
209};
210
Wu Fengguang079d88c2010-03-08 10:44:23 +0800211/*
212 * CEA speaker placement:
213 *
214 * FLH FCH FRH
215 * FLW FL FLC FC FRC FR FRW
216 *
217 * LFE
218 * TC
219 *
220 * RL RLC RC RRC RR
221 *
222 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
223 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
224 */
225enum cea_speaker_placement {
226 FL = (1 << 0), /* Front Left */
227 FC = (1 << 1), /* Front Center */
228 FR = (1 << 2), /* Front Right */
229 FLC = (1 << 3), /* Front Left Center */
230 FRC = (1 << 4), /* Front Right Center */
231 RL = (1 << 5), /* Rear Left */
232 RC = (1 << 6), /* Rear Center */
233 RR = (1 << 7), /* Rear Right */
234 RLC = (1 << 8), /* Rear Left Center */
235 RRC = (1 << 9), /* Rear Right Center */
236 LFE = (1 << 10), /* Low Frequency Effect */
237 FLW = (1 << 11), /* Front Left Wide */
238 FRW = (1 << 12), /* Front Right Wide */
239 FLH = (1 << 13), /* Front Left High */
240 FCH = (1 << 14), /* Front Center High */
241 FRH = (1 << 15), /* Front Right High */
242 TC = (1 << 16), /* Top Center */
243};
244
245/*
246 * ELD SA bits in the CEA Speaker Allocation data block
247 */
248static int eld_speaker_allocation_bits[] = {
249 [0] = FL | FR,
250 [1] = LFE,
251 [2] = FC,
252 [3] = RL | RR,
253 [4] = RC,
254 [5] = FLC | FRC,
255 [6] = RLC | RRC,
256 /* the following are not defined in ELD yet */
257 [7] = FLW | FRW,
258 [8] = FLH | FRH,
259 [9] = TC,
260 [10] = FCH,
261};
262
263struct cea_channel_speaker_allocation {
264 int ca_index;
265 int speakers[8];
266
267 /* derived values, just for convenience */
268 int channels;
269 int spk_mask;
270};
271
272/*
273 * ALSA sequence is:
274 *
275 * surround40 surround41 surround50 surround51 surround71
276 * ch0 front left = = = =
277 * ch1 front right = = = =
278 * ch2 rear left = = = =
279 * ch3 rear right = = = =
280 * ch4 LFE center center center
281 * ch5 LFE LFE
282 * ch6 side left
283 * ch7 side right
284 *
285 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
286 */
287static int hdmi_channel_mapping[0x32][8] = {
288 /* stereo */
289 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
290 /* 2.1 */
291 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
292 /* Dolby Surround */
293 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
294 /* surround40 */
295 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
296 /* 4ch */
297 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
298 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800299 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800300 /* surround50 */
301 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
302 /* surround51 */
303 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
304 /* 7.1 */
305 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
306};
307
308/*
309 * This is an ordered list!
310 *
311 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800312 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800313 */
314static struct cea_channel_speaker_allocation channel_allocations[] = {
315/* channel: 7 6 5 4 3 2 1 0 */
316{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
317 /* 2.1 */
318{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
319 /* Dolby Surround */
320{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
321 /* surround40 */
322{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
323 /* surround41 */
324{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
325 /* surround50 */
326{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
327 /* surround51 */
328{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
329 /* 6.1 */
330{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
331 /* surround71 */
332{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
333
334{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
335{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
336{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
337{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
338{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
339{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
340{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
341{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
342{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
343{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
344{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
345{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
346{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
347{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
348{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
349{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
350{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
351{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
352{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
353{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
354{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
355{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
356{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
357{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
358{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
359{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
360{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
361{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
362{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
363{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
364{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
365{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
366{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
367{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
368{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
369{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
370{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
371{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
372{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
373{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
374{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
375};
376
377
378/*
379 * HDMI routines
380 */
381
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100382#define get_pin(spec, idx) \
383 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
384#define get_cvt(spec, idx) \
385 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100386#define get_pcm_rec(spec, idx) ((spec)->pcm_rec[idx])
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100387
Takashi Iwai4e76a882014-02-25 12:21:03 +0100388static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800389{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100390 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600391 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800392
Stephen Warren384a48d2011-06-01 11:14:21 -0600393 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100394 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600395 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800396
Takashi Iwai4e76a882014-02-25 12:21:03 +0100397 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600398 return -EINVAL;
399}
400
Libin Yang2bf3c852015-12-16 13:42:43 +0800401static int hinfo_to_pcm_index(struct hda_codec *codec,
402 struct hda_pcm_stream *hinfo)
403{
404 struct hdmi_spec *spec = codec->spec;
405 int pcm_idx;
406
407 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
408 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
409 return pcm_idx;
410
411 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
412 return -EINVAL;
413}
414
Takashi Iwai4e76a882014-02-25 12:21:03 +0100415static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600416 struct hda_pcm_stream *hinfo)
417{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100418 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800419 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600420 int pin_idx;
421
Libin Yang6590faa2015-12-16 13:42:41 +0800422 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
423 per_pin = get_pin(spec, pin_idx);
424 if (per_pin->pcm && per_pin->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600425 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800426 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600427
Libin Yang6590faa2015-12-16 13:42:41 +0800428 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600429 return -EINVAL;
430}
431
Takashi Iwai4e76a882014-02-25 12:21:03 +0100432static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600433{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100434 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600435 int cvt_idx;
436
437 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100438 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600439 return cvt_idx;
440
Takashi Iwai4e76a882014-02-25 12:21:03 +0100441 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800442 return -EINVAL;
443}
444
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500445static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
446 struct snd_ctl_elem_info *uinfo)
447{
448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100449 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200450 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100451 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500452 int pin_idx;
453
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500454 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
455
456 pin_idx = kcontrol->private_value;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200457 per_pin = get_pin(spec, pin_idx);
458 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100459
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200460 mutex_lock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100461 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200462 mutex_unlock(&per_pin->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500463
464 return 0;
465}
466
467static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
468 struct snd_ctl_elem_value *ucontrol)
469{
470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100471 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200472 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100473 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500474 int pin_idx;
475
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500476 pin_idx = kcontrol->private_value;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200477 per_pin = get_pin(spec, pin_idx);
478 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500479
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200480 mutex_lock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100481 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200482 mutex_unlock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100483 snd_BUG();
484 return -EINVAL;
485 }
486
487 memset(ucontrol->value.bytes.data, 0,
488 ARRAY_SIZE(ucontrol->value.bytes.data));
489 if (eld->eld_valid)
490 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
491 eld->eld_size);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200492 mutex_unlock(&per_pin->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500493
494 return 0;
495}
496
497static struct snd_kcontrol_new eld_bytes_ctl = {
498 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
499 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
500 .name = "ELD",
501 .info = hdmi_eld_ctl_info,
502 .get = hdmi_eld_ctl_get,
503};
504
505static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
506 int device)
507{
508 struct snd_kcontrol *kctl;
509 struct hdmi_spec *spec = codec->spec;
510 int err;
511
512 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
513 if (!kctl)
514 return -ENOMEM;
515 kctl->private_value = pin_idx;
516 kctl->id.device = device;
517
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100518 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500519 if (err < 0)
520 return err;
521
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100522 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500523 return 0;
524}
525
Wu Fengguang079d88c2010-03-08 10:44:23 +0800526#ifdef BE_PARANOID
527static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
528 int *packet_index, int *byte_index)
529{
530 int val;
531
532 val = snd_hda_codec_read(codec, pin_nid, 0,
533 AC_VERB_GET_HDMI_DIP_INDEX, 0);
534
535 *packet_index = val >> 5;
536 *byte_index = val & 0x1f;
537}
538#endif
539
540static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
541 int packet_index, int byte_index)
542{
543 int val;
544
545 val = (packet_index << 5) | (byte_index & 0x1f);
546
547 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
548}
549
550static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
551 unsigned char val)
552{
553 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
554}
555
Stephen Warren384a48d2011-06-01 11:14:21 -0600556static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800557{
Stephen Warren75fae112014-01-30 11:52:16 -0700558 struct hdmi_spec *spec = codec->spec;
559 int pin_out;
560
Wu Fengguang079d88c2010-03-08 10:44:23 +0800561 /* Unmute */
562 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
563 snd_hda_codec_write(codec, pin_nid, 0,
564 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700565
566 if (spec->dyn_pin_out)
567 /* Disable pin out until stream is active */
568 pin_out = 0;
569 else
570 /* Enable pin out: some machines with GM965 gets broken output
571 * when the pin is disabled or changed while using with HDMI
572 */
573 pin_out = PIN_OUT;
574
Wu Fengguang079d88c2010-03-08 10:44:23 +0800575 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700576 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800577}
578
Stephen Warren384a48d2011-06-01 11:14:21 -0600579static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800580{
Stephen Warren384a48d2011-06-01 11:14:21 -0600581 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800582 AC_VERB_GET_CVT_CHAN_COUNT, 0);
583}
584
585static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600586 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800587{
Stephen Warren384a48d2011-06-01 11:14:21 -0600588 if (chs != hdmi_get_channel_count(codec, cvt_nid))
589 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800590 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
591}
592
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200593/*
594 * ELD proc files
595 */
596
Jie Yangcd6a6502015-05-27 19:45:45 +0800597#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200598static void print_eld_info(struct snd_info_entry *entry,
599 struct snd_info_buffer *buffer)
600{
601 struct hdmi_spec_per_pin *per_pin = entry->private_data;
602
603 mutex_lock(&per_pin->lock);
604 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
605 mutex_unlock(&per_pin->lock);
606}
607
608static void write_eld_info(struct snd_info_entry *entry,
609 struct snd_info_buffer *buffer)
610{
611 struct hdmi_spec_per_pin *per_pin = entry->private_data;
612
613 mutex_lock(&per_pin->lock);
614 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
615 mutex_unlock(&per_pin->lock);
616}
617
618static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
619{
620 char name[32];
621 struct hda_codec *codec = per_pin->codec;
622 struct snd_info_entry *entry;
623 int err;
624
625 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100626 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200627 if (err < 0)
628 return err;
629
630 snd_info_set_text_ops(entry, per_pin, print_eld_info);
631 entry->c.text.write = write_eld_info;
632 entry->mode |= S_IWUSR;
633 per_pin->proc_entry = entry;
634
635 return 0;
636}
637
638static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
639{
Markus Elfring1947a112015-06-28 11:15:28 +0200640 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200641 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200642 per_pin->proc_entry = NULL;
643 }
644}
645#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200646static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
647 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200648{
649 return 0;
650}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200651static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200652{
653}
654#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800655
656/*
657 * Channel mapping routines
658 */
659
660/*
661 * Compute derived values in channel_allocations[].
662 */
663static void init_channel_allocations(void)
664{
665 int i, j;
666 struct cea_channel_speaker_allocation *p;
667
668 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
669 p = channel_allocations + i;
670 p->channels = 0;
671 p->spk_mask = 0;
672 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
673 if (p->speakers[j]) {
674 p->channels++;
675 p->spk_mask |= p->speakers[j];
676 }
677 }
678}
679
Wang Xingchao72357c72012-09-06 10:02:36 +0800680static int get_channel_allocation_order(int ca)
681{
682 int i;
683
684 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
685 if (channel_allocations[i].ca_index == ca)
686 break;
687 }
688 return i;
689}
690
Wu Fengguang079d88c2010-03-08 10:44:23 +0800691/*
692 * The transformation takes two steps:
693 *
694 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
695 * spk_mask => (channel_allocations[]) => ai->CA
696 *
697 * TODO: it could select the wrong CA from multiple candidates.
698*/
Takashi Iwai79514d42014-06-06 18:04:34 +0200699static int hdmi_channel_allocation(struct hda_codec *codec,
700 struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800701{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800702 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800703 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800704 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800705 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
706
707 /*
708 * CA defaults to 0 for basic stereo audio
709 */
710 if (channels <= 2)
711 return 0;
712
Wu Fengguang079d88c2010-03-08 10:44:23 +0800713 /*
714 * expand ELD's speaker allocation mask
715 *
716 * ELD tells the speaker mask in a compact(paired) form,
717 * expand ELD's notions to match the ones used by Audio InfoFrame.
718 */
719 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100720 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800721 spk_mask |= eld_speaker_allocation_bits[i];
722 }
723
724 /* search for the first working match in the CA table */
725 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
726 if (channels == channel_allocations[i].channels &&
727 (spk_mask & channel_allocations[i].spk_mask) ==
728 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800729 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800730 break;
731 }
732 }
733
Anssi Hannula18e39182013-09-01 14:36:47 +0300734 if (!ca) {
735 /* if there was no match, select the regular ALSA channel
736 * allocation with the matching number of channels */
737 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
738 if (channels == channel_allocations[i].channels) {
739 ca = channel_allocations[i].ca_index;
740 break;
741 }
742 }
743 }
744
David Henningsson1613d6b2013-02-19 16:11:24 +0100745 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Takashi Iwai79514d42014-06-06 18:04:34 +0200746 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800747 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800748
Wu Fengguang53d7d692010-09-21 14:25:49 +0800749 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800750}
751
752static void hdmi_debug_channel_mapping(struct hda_codec *codec,
753 hda_nid_t pin_nid)
754{
755#ifdef CONFIG_SND_DEBUG_VERBOSE
Anssi Hannula307229d2013-10-24 21:10:34 +0300756 struct hdmi_spec *spec = codec->spec;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800757 int i;
Anssi Hannula307229d2013-10-24 21:10:34 +0300758 int channel;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800759
760 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300761 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100762 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300763 channel, i);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800764 }
765#endif
766}
767
Takashi Iwaid45e6882012-07-31 11:36:00 +0200768static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800769 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800770 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800771 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800772{
Anssi Hannula307229d2013-10-24 21:10:34 +0300773 struct hdmi_spec *spec = codec->spec;
Anssi Hannula90f28002013-10-05 02:25:39 +0300774 struct cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800775 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800776 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800777 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800778 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800779
Wang Xingchao72357c72012-09-06 10:02:36 +0800780 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300781 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800782
Wu Fengguang079d88c2010-03-08 10:44:23 +0800783 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300784 int hdmi_slot = 0;
785 /* fill actual channel mappings in ALSA channel (i) order */
786 for (i = 0; i < ch_alloc->channels; i++) {
787 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
788 hdmi_slot++; /* skip zero slots */
789
790 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
791 }
792 /* fill the rest of the slots with ALSA channel 0xf */
793 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
794 if (!ch_alloc->speakers[7 - hdmi_slot])
795 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800796 }
797
Wang Xingchao433968d2012-09-06 10:02:37 +0800798 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300799 for (i = 0; i < ch_alloc->channels; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300800 non_pcm_mapping[i] = (i << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800801 for (; i < 8; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300802 non_pcm_mapping[i] = (0xf << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800803 }
804
Wu Fengguang079d88c2010-03-08 10:44:23 +0800805 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300806 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
807 int hdmi_slot = slotsetup & 0x0f;
808 int channel = (slotsetup & 0xf0) >> 4;
809 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800810 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100811 codec_dbg(codec, "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800812 break;
813 }
814 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800815}
816
Takashi Iwaid45e6882012-07-31 11:36:00 +0200817struct channel_map_table {
818 unsigned char map; /* ALSA API channel map position */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200819 int spk_mask; /* speaker position bit mask */
820};
821
822static struct channel_map_table map_tables[] = {
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300823 { SNDRV_CHMAP_FL, FL },
824 { SNDRV_CHMAP_FR, FR },
825 { SNDRV_CHMAP_RL, RL },
826 { SNDRV_CHMAP_RR, RR },
827 { SNDRV_CHMAP_LFE, LFE },
828 { SNDRV_CHMAP_FC, FC },
829 { SNDRV_CHMAP_RLC, RLC },
830 { SNDRV_CHMAP_RRC, RRC },
831 { SNDRV_CHMAP_RC, RC },
832 { SNDRV_CHMAP_FLC, FLC },
833 { SNDRV_CHMAP_FRC, FRC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200834 { SNDRV_CHMAP_TFL, FLH },
835 { SNDRV_CHMAP_TFR, FRH },
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300836 { SNDRV_CHMAP_FLW, FLW },
837 { SNDRV_CHMAP_FRW, FRW },
838 { SNDRV_CHMAP_TC, TC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200839 { SNDRV_CHMAP_TFC, FCH },
Takashi Iwaid45e6882012-07-31 11:36:00 +0200840 {} /* terminator */
841};
842
843/* from ALSA API channel position to speaker bit mask */
844static int to_spk_mask(unsigned char c)
845{
846 struct channel_map_table *t = map_tables;
847 for (; t->map; t++) {
848 if (t->map == c)
849 return t->spk_mask;
850 }
851 return 0;
852}
853
854/* from ALSA API channel position to CEA slot */
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300855static int to_cea_slot(int ordered_ca, unsigned char pos)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200856{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300857 int mask = to_spk_mask(pos);
858 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200859
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300860 if (mask) {
861 for (i = 0; i < 8; i++) {
862 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
863 return i;
864 }
Takashi Iwaid45e6882012-07-31 11:36:00 +0200865 }
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300866
867 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200868}
869
870/* from speaker bit mask to ALSA API channel position */
871static int spk_to_chmap(int spk)
872{
873 struct channel_map_table *t = map_tables;
874 for (; t->map; t++) {
875 if (t->spk_mask == spk)
876 return t->map;
877 }
878 return 0;
879}
880
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300881/* from CEA slot to ALSA API channel position */
882static int from_cea_slot(int ordered_ca, unsigned char slot)
883{
884 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
885
886 return spk_to_chmap(mask);
887}
888
Takashi Iwaid45e6882012-07-31 11:36:00 +0200889/* get the CA index corresponding to the given ALSA API channel map */
890static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
891{
892 int i, spks = 0, spk_mask = 0;
893
894 for (i = 0; i < chs; i++) {
895 int mask = to_spk_mask(map[i]);
896 if (mask) {
897 spk_mask |= mask;
898 spks++;
899 }
900 }
901
902 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
903 if ((chs == channel_allocations[i].channels ||
904 spks == channel_allocations[i].channels) &&
905 (spk_mask & channel_allocations[i].spk_mask) ==
906 channel_allocations[i].spk_mask)
907 return channel_allocations[i].ca_index;
908 }
909 return -1;
910}
911
912/* set up the channel slots for the given ALSA API channel map */
913static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
914 hda_nid_t pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300915 int chs, unsigned char *map,
916 int ca)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200917{
Anssi Hannula307229d2013-10-24 21:10:34 +0300918 struct hdmi_spec *spec = codec->spec;
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300919 int ordered_ca = get_channel_allocation_order(ca);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300920 int alsa_pos, hdmi_slot;
921 int assignments[8] = {[0 ... 7] = 0xf};
922
923 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
924
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300925 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300926
927 if (hdmi_slot < 0)
928 continue; /* unassigned channel */
929
930 assignments[hdmi_slot] = alsa_pos;
931 }
932
933 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300934 int err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300935
Anssi Hannula307229d2013-10-24 21:10:34 +0300936 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
937 assignments[hdmi_slot]);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200938 if (err)
939 return -EINVAL;
940 }
941 return 0;
942}
943
944/* store ALSA API channel map from the current default map */
945static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
946{
947 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300948 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200949 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300950 if (i < channel_allocations[ordered_ca].channels)
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300951 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200952 else
953 map[i] = 0;
954 }
955}
956
957static void hdmi_setup_channel_mapping(struct hda_codec *codec,
958 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200959 int channels, unsigned char *map,
960 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200961{
Anssi Hannula20608732013-02-03 17:55:45 +0200962 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200963 hdmi_manual_setup_channel_mapping(codec, pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300964 channels, map, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200965 } else {
966 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
967 hdmi_setup_fake_chmap(map, ca);
968 }
Anssi Hannula980b2492013-10-05 02:25:44 +0300969
970 hdmi_debug_channel_mapping(codec, pin_nid);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200971}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800972
Anssi Hannula307229d2013-10-24 21:10:34 +0300973static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
974 int asp_slot, int channel)
975{
976 return snd_hda_codec_write(codec, pin_nid, 0,
977 AC_VERB_SET_HDMI_CHAN_SLOT,
978 (channel << 4) | asp_slot);
979}
980
981static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
982 int asp_slot)
983{
984 return (snd_hda_codec_read(codec, pin_nid, 0,
985 AC_VERB_GET_HDMI_CHAN_SLOT,
986 asp_slot) & 0xf0) >> 4;
987}
988
Wu Fengguang079d88c2010-03-08 10:44:23 +0800989/*
990 * Audio InfoFrame routines
991 */
992
993/*
994 * Enable Audio InfoFrame Transmission
995 */
996static void hdmi_start_infoframe_trans(struct hda_codec *codec,
997 hda_nid_t pin_nid)
998{
999 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1000 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1001 AC_DIPXMIT_BEST);
1002}
1003
1004/*
1005 * Disable Audio InfoFrame Transmission
1006 */
1007static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1008 hda_nid_t pin_nid)
1009{
1010 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1011 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1012 AC_DIPXMIT_DISABLE);
1013}
1014
1015static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1016{
1017#ifdef CONFIG_SND_DEBUG_VERBOSE
1018 int i;
1019 int size;
1020
1021 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001022 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001023
1024 for (i = 0; i < 8; i++) {
1025 size = snd_hda_codec_read(codec, pin_nid, 0,
1026 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001027 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001028 }
1029#endif
1030}
1031
1032static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1033{
1034#ifdef BE_PARANOID
1035 int i, j;
1036 int size;
1037 int pi, bi;
1038 for (i = 0; i < 8; i++) {
1039 size = snd_hda_codec_read(codec, pin_nid, 0,
1040 AC_VERB_GET_HDMI_DIP_SIZE, i);
1041 if (size == 0)
1042 continue;
1043
1044 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1045 for (j = 1; j < 1000; j++) {
1046 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1047 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1048 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001049 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001050 bi, pi, i);
1051 if (bi == 0) /* byte index wrapped around */
1052 break;
1053 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001054 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001055 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1056 i, size, j);
1057 }
1058#endif
1059}
1060
Wu Fengguang53d7d692010-09-21 14:25:49 +08001061static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001062{
Wu Fengguang53d7d692010-09-21 14:25:49 +08001063 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001064 u8 sum = 0;
1065 int i;
1066
Wu Fengguang53d7d692010-09-21 14:25:49 +08001067 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001068
Wu Fengguang53d7d692010-09-21 14:25:49 +08001069 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001070 sum += bytes[i];
1071
Wu Fengguang53d7d692010-09-21 14:25:49 +08001072 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001073}
1074
1075static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1076 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001077 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001078{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001079 int i;
1080
1081 hdmi_debug_dip_size(codec, pin_nid);
1082 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1083
Wu Fengguang079d88c2010-03-08 10:44:23 +08001084 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001085 for (i = 0; i < size; i++)
1086 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001087}
1088
1089static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001090 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001091{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001092 u8 val;
1093 int i;
1094
1095 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1096 != AC_DIPXMIT_BEST)
1097 return false;
1098
1099 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001100 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001101 val = snd_hda_codec_read(codec, pin_nid, 0,
1102 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001103 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +08001104 return false;
1105 }
1106
1107 return true;
1108}
1109
Anssi Hannula307229d2013-10-24 21:10:34 +03001110static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1111 hda_nid_t pin_nid,
1112 int ca, int active_channels,
1113 int conn_type)
1114{
1115 union audio_infoframe ai;
1116
Mengdong Lincaaf5ef2014-03-11 17:12:52 -04001117 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +03001118 if (conn_type == 0) { /* HDMI */
1119 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1120
1121 hdmi_ai->type = 0x84;
1122 hdmi_ai->ver = 0x01;
1123 hdmi_ai->len = 0x0a;
1124 hdmi_ai->CC02_CT47 = active_channels - 1;
1125 hdmi_ai->CA = ca;
1126 hdmi_checksum_audio_infoframe(hdmi_ai);
1127 } else if (conn_type == 1) { /* DisplayPort */
1128 struct dp_audio_infoframe *dp_ai = &ai.dp;
1129
1130 dp_ai->type = 0x84;
1131 dp_ai->len = 0x1b;
1132 dp_ai->ver = 0x11 << 2;
1133 dp_ai->CC02_CT47 = active_channels - 1;
1134 dp_ai->CA = ca;
1135 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001136 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001137 pin_nid);
1138 return;
1139 }
1140
1141 /*
1142 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1143 * sizeof(*dp_ai) to avoid partial match/update problems when
1144 * the user switches between HDMI/DP monitors.
1145 */
1146 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1147 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001148 codec_dbg(codec,
1149 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001150 pin_nid,
1151 active_channels, ca);
1152 hdmi_stop_infoframe_trans(codec, pin_nid);
1153 hdmi_fill_audio_infoframe(codec, pin_nid,
1154 ai.bytes, sizeof(ai));
1155 hdmi_start_infoframe_trans(codec, pin_nid);
1156 }
1157}
1158
Takashi Iwaib0540872013-09-02 12:33:02 +02001159static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1160 struct hdmi_spec_per_pin *per_pin,
1161 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001162{
Anssi Hannula307229d2013-10-24 21:10:34 +03001163 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001164 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +02001165 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001166 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -06001167 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001168 int ca, ordered_ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001169
Takashi Iwaib0540872013-09-02 12:33:02 +02001170 if (!channels)
1171 return;
1172
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001173 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001174 snd_hda_codec_write(codec, pin_nid, 0,
1175 AC_VERB_SET_AMP_GAIN_MUTE,
1176 AMP_OUT_UNMUTE);
1177
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001178 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001179
Takashi Iwaid45e6882012-07-31 11:36:00 +02001180 if (!non_pcm && per_pin->chmap_set)
1181 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1182 else
Takashi Iwai79514d42014-06-06 18:04:34 +02001183 ca = hdmi_channel_allocation(codec, eld, channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001184 if (ca < 0)
1185 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001186
Anssi Hannula1df5a062013-10-05 02:25:40 +03001187 ordered_ca = get_channel_allocation_order(ca);
1188 active_channels = channel_allocations[ordered_ca].channels;
1189
1190 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1191
Stephen Warren384a48d2011-06-01 11:14:21 -06001192 /*
Anssi Hannula39edac72013-10-07 19:24:52 +03001193 * always configure channel mapping, it may have been changed by the
1194 * user in the meantime
1195 */
1196 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1197 channels, per_pin->chmap,
1198 per_pin->chmap_set);
1199
Anssi Hannula307229d2013-10-24 21:10:34 +03001200 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1201 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +08001202
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001203 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001204}
1205
Wu Fengguang079d88c2010-03-08 10:44:23 +08001206/*
1207 * Unsolicited events
1208 */
1209
Takashi Iwaiefe47102013-11-07 13:38:23 +01001210static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001211
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001212static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001213{
1214 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001215 int pin_idx = pin_nid_to_pin_index(codec, nid);
1216
David Henningsson20ce9022013-12-04 10:19:41 +08001217 if (pin_idx < 0)
1218 return;
David Henningsson20ce9022013-12-04 10:19:41 +08001219 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1220 snd_hda_jack_report_sync(codec);
1221}
1222
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001223static void jack_callback(struct hda_codec *codec,
1224 struct hda_jack_callback *jack)
1225{
1226 check_presence_and_report(codec, jack->tbl->nid);
1227}
1228
David Henningsson20ce9022013-12-04 10:19:41 +08001229static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1230{
Takashi Iwai3a938972011-10-28 01:16:55 +02001231 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001232 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001233 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001234
1235 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1236 if (!jack)
1237 return;
Takashi Iwai3a938972011-10-28 01:16:55 +02001238 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001239
Takashi Iwai4e76a882014-02-25 12:21:03 +01001240 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001241 "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 +08001242 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001243 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001244
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001245 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001246}
1247
1248static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1249{
1250 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1251 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1252 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1253 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1254
Takashi Iwai4e76a882014-02-25 12:21:03 +01001255 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001256 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001257 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001258 tag,
1259 subtag,
1260 cp_state,
1261 cp_ready);
1262
1263 /* TODO */
1264 if (cp_state)
1265 ;
1266 if (cp_ready)
1267 ;
1268}
1269
1270
1271static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1272{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001273 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1274 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1275
Takashi Iwai3a938972011-10-28 01:16:55 +02001276 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001277 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001278 return;
1279 }
1280
1281 if (subtag == 0)
1282 hdmi_intrinsic_event(codec, res);
1283 else
1284 hdmi_non_intrinsic_event(codec, res);
1285}
1286
Mengdong Lin58f7d282013-09-04 16:37:12 -04001287static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001288 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001289{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001290 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001291
Wang Xingchao53b434f2013-06-18 10:41:53 +08001292 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1293 * thus pins could only choose converter 0 for use. Make sure the
1294 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001295 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001296 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1297
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001298 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001299 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1300 AC_PWRST_D0);
1301 msleep(40);
1302 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1303 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001304 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 +02001305 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001306}
1307
Wu Fengguang079d88c2010-03-08 10:44:23 +08001308/*
1309 * Callbacks
1310 */
1311
Takashi Iwai92f10b32010-08-03 14:21:00 +02001312/* HBR should be Non-PCM, 8 channels */
1313#define is_hbr_format(format) \
1314 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1315
Anssi Hannula307229d2013-10-24 21:10:34 +03001316static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1317 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001318{
Anssi Hannula307229d2013-10-24 21:10:34 +03001319 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +02001320
Stephen Warren384a48d2011-06-01 11:14:21 -06001321 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1322 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001323 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1324
Anssi Hannula13122e62013-11-10 20:56:10 +02001325 if (pinctl < 0)
1326 return hbr ? -EINVAL : 0;
1327
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001328 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +03001329 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001330 new_pinctl |= AC_PINCTL_EPT_HBR;
1331 else
1332 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1333
Takashi Iwai4e76a882014-02-25 12:21:03 +01001334 codec_dbg(codec,
1335 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001336 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001337 pinctl == new_pinctl ? "" : "new-",
1338 new_pinctl);
1339
1340 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001341 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001342 AC_VERB_SET_PIN_WIDGET_CONTROL,
1343 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +03001344 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001345 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03001346
1347 return 0;
1348}
1349
1350static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1351 hda_nid_t pin_nid, u32 stream_tag, int format)
1352{
1353 struct hdmi_spec *spec = codec->spec;
1354 int err;
1355
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001356 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +03001357 haswell_verify_D0(codec, cvt_nid, pin_nid);
1358
1359 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1360
1361 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001362 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +03001363 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001364 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001365
Stephen Warren384a48d2011-06-01 11:14:21 -06001366 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001367 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001368}
1369
Libin Yang42b29872015-12-16 13:42:42 +08001370/* Try to find an available converter
1371 * If pin_idx is less then zero, just try to find an available converter.
1372 * Otherwise, try to find an available converter and get the cvt mux index
1373 * of the pin.
1374 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001375static int hdmi_choose_cvt(struct hda_codec *codec,
1376 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001377{
1378 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001379 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001380 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001381 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001382
Libin Yang42b29872015-12-16 13:42:42 +08001383 /* pin_idx < 0 means no pin will be bound to the converter */
1384 if (pin_idx < 0)
1385 per_pin = NULL;
1386 else
1387 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001388
Stephen Warren384a48d2011-06-01 11:14:21 -06001389 /* Dynamically assign converter to stream */
1390 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001391 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001392
1393 /* Must not already be assigned */
1394 if (per_cvt->assigned)
1395 continue;
Libin Yang42b29872015-12-16 13:42:42 +08001396 if (per_pin == NULL)
1397 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06001398 /* Must be in pin's mux's list of converters */
1399 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1400 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1401 break;
1402 /* Not in mux list */
1403 if (mux_idx == per_pin->num_mux_nids)
1404 continue;
1405 break;
1406 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001407
Stephen Warren384a48d2011-06-01 11:14:21 -06001408 /* No free converters */
1409 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +08001410 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -06001411
Libin Yang42b29872015-12-16 13:42:42 +08001412 if (per_pin != NULL)
1413 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +08001414
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001415 if (cvt_id)
1416 *cvt_id = cvt_idx;
1417 if (mux_id)
1418 *mux_id = mux_idx;
1419
1420 return 0;
1421}
1422
Mengdong Lin2df67422014-03-20 13:01:06 +08001423/* Assure the pin select the right convetor */
1424static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1425 struct hdmi_spec_per_pin *per_pin)
1426{
1427 hda_nid_t pin_nid = per_pin->pin_nid;
1428 int mux_idx, curr;
1429
1430 mux_idx = per_pin->mux_idx;
1431 curr = snd_hda_codec_read(codec, pin_nid, 0,
1432 AC_VERB_GET_CONNECT_SEL, 0);
1433 if (curr != mux_idx)
1434 snd_hda_codec_write_cache(codec, pin_nid, 0,
1435 AC_VERB_SET_CONNECT_SEL,
1436 mux_idx);
1437}
1438
Libin Yang42b29872015-12-16 13:42:42 +08001439/* get the mux index for the converter of the pins
1440 * converter's mux index is the same for all pins on Intel platform
1441 */
1442static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1443 hda_nid_t cvt_nid)
1444{
1445 int i;
1446
1447 for (i = 0; i < spec->num_cvts; i++)
1448 if (spec->cvt_nids[i] == cvt_nid)
1449 return i;
1450 return -EINVAL;
1451}
1452
Mengdong Lin300016b2013-11-04 01:13:13 -05001453/* Intel HDMI workaround to fix audio routing issue:
1454 * For some Intel display codecs, pins share the same connection list.
1455 * So a conveter can be selected by multiple pins and playback on any of these
1456 * pins will generate sound on the external display, because audio flows from
1457 * the same converter to the display pipeline. Also muting one pin may make
1458 * other pins have no sound output.
1459 * So this function assures that an assigned converter for a pin is not selected
1460 * by any other pins.
1461 */
1462static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001463 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001464{
1465 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001466 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001467 int cvt_idx, curr;
1468 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001469
Mengdong Linf82d7d12013-09-21 20:34:45 -04001470 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +01001471 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -04001472 unsigned int wid_caps = get_wcaps(codec, nid);
1473 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001474
Mengdong Linf82d7d12013-09-21 20:34:45 -04001475 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001476 continue;
1477
Mengdong Linf82d7d12013-09-21 20:34:45 -04001478 if (nid == pin_nid)
1479 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001480
Mengdong Linf82d7d12013-09-21 20:34:45 -04001481 curr = snd_hda_codec_read(codec, nid, 0,
1482 AC_VERB_GET_CONNECT_SEL, 0);
1483 if (curr != mux_idx)
1484 continue;
1485
1486 /* choose an unassigned converter. The conveters in the
1487 * connection list are in the same order as in the codec.
1488 */
1489 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1490 per_cvt = get_cvt(spec, cvt_idx);
1491 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001492 codec_dbg(codec,
1493 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001494 cvt_idx, nid);
1495 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001496 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001497 cvt_idx);
1498 break;
1499 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001500 }
1501 }
1502}
1503
Libin Yang42b29872015-12-16 13:42:42 +08001504/* A wrapper of intel_not_share_asigned_cvt() */
1505static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1506 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1507{
1508 int mux_idx;
1509 struct hdmi_spec *spec = codec->spec;
1510
1511 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1512 return;
1513
1514 /* On Intel platform, the mapping of converter nid to
1515 * mux index of the pins are always the same.
1516 * The pin nid may be 0, this means all pins will not
1517 * share the converter.
1518 */
1519 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1520 if (mux_idx >= 0)
1521 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1522}
1523
1524/* called in hdmi_pcm_open when no pin is assigned to the PCM
1525 * in dyn_pcm_assign mode.
1526 */
1527static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1528 struct hda_codec *codec,
1529 struct snd_pcm_substream *substream)
1530{
1531 struct hdmi_spec *spec = codec->spec;
1532 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yangac983792015-12-16 16:48:16 +08001533 int cvt_idx, pcm_idx;
Libin Yang42b29872015-12-16 13:42:42 +08001534 struct hdmi_spec_per_cvt *per_cvt = NULL;
1535 int err;
1536
Libin Yangac983792015-12-16 16:48:16 +08001537 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1538 if (pcm_idx < 0)
1539 return -EINVAL;
1540
Libin Yang42b29872015-12-16 13:42:42 +08001541 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1542 if (err)
1543 return err;
1544
1545 per_cvt = get_cvt(spec, cvt_idx);
1546 per_cvt->assigned = 1;
1547 hinfo->nid = per_cvt->cvt_nid;
1548
1549 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1550
Libin Yangac983792015-12-16 16:48:16 +08001551 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001552 /* todo: setup spdif ctls assign */
1553
1554 /* Initially set the converter's capabilities */
1555 hinfo->channels_min = per_cvt->channels_min;
1556 hinfo->channels_max = per_cvt->channels_max;
1557 hinfo->rates = per_cvt->rates;
1558 hinfo->formats = per_cvt->formats;
1559 hinfo->maxbps = per_cvt->maxbps;
1560
1561 /* Store the updated parameters */
1562 runtime->hw.channels_min = hinfo->channels_min;
1563 runtime->hw.channels_max = hinfo->channels_max;
1564 runtime->hw.formats = hinfo->formats;
1565 runtime->hw.rates = hinfo->rates;
1566
1567 snd_pcm_hw_constraint_step(substream->runtime, 0,
1568 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1569 return 0;
1570}
1571
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001572/*
1573 * HDA PCM callbacks
1574 */
1575static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1576 struct hda_codec *codec,
1577 struct snd_pcm_substream *substream)
1578{
1579 struct hdmi_spec *spec = codec->spec;
1580 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001581 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001582 struct hdmi_spec_per_pin *per_pin;
1583 struct hdmi_eld *eld;
1584 struct hdmi_spec_per_cvt *per_cvt = NULL;
1585 int err;
1586
1587 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001588 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1589 if (pcm_idx < 0)
1590 return -EINVAL;
1591
Libin Yang42b29872015-12-16 13:42:42 +08001592 mutex_lock(&spec->pcm_lock);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001593 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001594 if (!spec->dyn_pcm_assign) {
1595 if (snd_BUG_ON(pin_idx < 0)) {
1596 mutex_unlock(&spec->pcm_lock);
1597 return -EINVAL;
1598 }
1599 } else {
1600 /* no pin is assigned to the PCM
1601 * PA need pcm open successfully when probe
1602 */
1603 if (pin_idx < 0) {
1604 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1605 mutex_unlock(&spec->pcm_lock);
1606 return err;
1607 }
1608 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001609
1610 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001611 if (err < 0) {
1612 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001613 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001614 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001615
1616 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001617 /* Claim converter */
1618 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001619
Libin Yangac983792015-12-16 16:48:16 +08001620 set_bit(pcm_idx, &spec->pcm_in_use);
Libin Yang42b29872015-12-16 13:42:42 +08001621 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001622 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001623 hinfo->nid = per_cvt->cvt_nid;
1624
Takashi Iwaibddee962013-06-18 16:14:22 +02001625 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001626 AC_VERB_SET_CONNECT_SEL,
1627 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001628
1629 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001630 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001631 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001632
Libin Yang2bf3c852015-12-16 13:42:43 +08001633 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001634
Stephen Warren2def8172011-06-01 11:14:20 -06001635 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001636 hinfo->channels_min = per_cvt->channels_min;
1637 hinfo->channels_max = per_cvt->channels_max;
1638 hinfo->rates = per_cvt->rates;
1639 hinfo->formats = per_cvt->formats;
1640 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001641
Libin Yang42b29872015-12-16 13:42:42 +08001642 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001643 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001644 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001645 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001646 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001647 !hinfo->rates || !hinfo->formats) {
1648 per_cvt->assigned = 0;
1649 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001650 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001651 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001652 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001653 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001654 }
Stephen Warren2def8172011-06-01 11:14:20 -06001655
Libin Yang42b29872015-12-16 13:42:42 +08001656 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001657 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001658 runtime->hw.channels_min = hinfo->channels_min;
1659 runtime->hw.channels_max = hinfo->channels_max;
1660 runtime->hw.formats = hinfo->formats;
1661 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001662
1663 snd_pcm_hw_constraint_step(substream->runtime, 0,
1664 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001665 return 0;
1666}
1667
1668/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001669 * HDA/HDMI auto parsing
1670 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001671static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001672{
1673 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001674 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001675 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001676
1677 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001678 codec_warn(codec,
1679 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001680 pin_nid, get_wcaps(codec, pin_nid));
1681 return -EINVAL;
1682 }
1683
Stephen Warren384a48d2011-06-01 11:14:21 -06001684 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1685 per_pin->mux_nids,
1686 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001687
1688 return 0;
1689}
1690
Libin Yanga76056f2015-12-16 16:48:15 +08001691static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1692 struct hdmi_spec_per_pin *per_pin)
1693{
1694 int i;
1695
1696 /* try the prefer PCM */
1697 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1698 return per_pin->pin_nid_idx;
1699
1700 /* have a second try; check the "reserved area" over num_pins */
1701 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1702 if (!test_bit(i, &spec->pcm_bitmap))
1703 return i;
1704 }
1705
1706 /* the last try; check the empty slots in pins */
1707 for (i = 0; i < spec->num_pins; i++) {
1708 if (!test_bit(i, &spec->pcm_bitmap))
1709 return i;
1710 }
1711 return -EBUSY;
1712}
1713
1714static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1715 struct hdmi_spec_per_pin *per_pin)
1716{
1717 int idx;
1718
1719 /* pcm already be attached to the pin */
1720 if (per_pin->pcm)
1721 return;
1722 idx = hdmi_find_pcm_slot(spec, per_pin);
1723 if (idx == -ENODEV)
1724 return;
1725 per_pin->pcm_idx = idx;
1726 per_pin->pcm = spec->pcm_rec[idx];
1727 set_bit(idx, &spec->pcm_bitmap);
1728}
1729
1730static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1731 struct hdmi_spec_per_pin *per_pin)
1732{
1733 int idx;
1734
1735 /* pcm already be detached from the pin */
1736 if (!per_pin->pcm)
1737 return;
1738 idx = per_pin->pcm_idx;
1739 per_pin->pcm_idx = -1;
1740 per_pin->pcm = NULL;
1741 if (idx >= 0 && idx < spec->pcm_used)
1742 clear_bit(idx, &spec->pcm_bitmap);
1743}
1744
Libin Yangac983792015-12-16 16:48:16 +08001745static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1746 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1747{
1748 int mux_idx;
1749
1750 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1751 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1752 break;
1753 return mux_idx;
1754}
1755
1756static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1757
1758static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1759 struct hdmi_spec_per_pin *per_pin)
1760{
1761 struct hda_codec *codec = per_pin->codec;
1762 struct hda_pcm *pcm;
1763 struct hda_pcm_stream *hinfo;
1764 struct snd_pcm_substream *substream;
1765 int mux_idx;
1766 bool non_pcm;
1767
1768 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1769 pcm = spec->pcm_rec[per_pin->pcm_idx];
1770 else
1771 return;
1772 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1773 return;
1774
1775 /* hdmi audio only uses playback and one substream */
1776 hinfo = pcm->stream;
1777 substream = pcm->pcm->streams[0].substream;
1778
1779 per_pin->cvt_nid = hinfo->nid;
1780
1781 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1782 if (mux_idx < per_pin->num_mux_nids)
1783 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1784 AC_VERB_SET_CONNECT_SEL,
1785 mux_idx);
1786 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1787
1788 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1789 if (substream->runtime)
1790 per_pin->channels = substream->runtime->channels;
1791 per_pin->setup = true;
1792 per_pin->mux_idx = mux_idx;
1793
1794 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1795}
1796
1797static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1798 struct hdmi_spec_per_pin *per_pin)
1799{
1800 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1801 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1802
1803 per_pin->chmap_set = false;
1804 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1805
1806 per_pin->setup = false;
1807 per_pin->channels = 0;
1808}
1809
Takashi Iwaie90247f2015-11-13 09:12:12 +01001810/* update per_pin ELD from the given new ELD;
1811 * setup info frame and notification accordingly
1812 */
1813static void update_eld(struct hda_codec *codec,
1814 struct hdmi_spec_per_pin *per_pin,
1815 struct hdmi_eld *eld)
1816{
1817 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001818 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001819 bool old_eld_valid = pin_eld->eld_valid;
1820 bool eld_changed;
1821
Libin Yanga76056f2015-12-16 16:48:15 +08001822 if (spec->dyn_pcm_assign) {
Libin Yangac983792015-12-16 16:48:16 +08001823 if (eld->eld_valid) {
Libin Yanga76056f2015-12-16 16:48:15 +08001824 hdmi_attach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001825 hdmi_pcm_setup_pin(spec, per_pin);
1826 } else {
1827 hdmi_pcm_reset_pin(spec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001828 hdmi_detach_hda_pcm(spec, per_pin);
Libin Yangac983792015-12-16 16:48:16 +08001829 }
Libin Yanga76056f2015-12-16 16:48:15 +08001830 }
1831
Takashi Iwaie90247f2015-11-13 09:12:12 +01001832 if (eld->eld_valid)
1833 snd_hdmi_show_eld(codec, &eld->info);
1834
1835 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1836 if (eld->eld_valid && pin_eld->eld_valid)
1837 if (pin_eld->eld_size != eld->eld_size ||
1838 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1839 eld->eld_size) != 0)
1840 eld_changed = true;
1841
1842 pin_eld->eld_valid = eld->eld_valid;
1843 pin_eld->eld_size = eld->eld_size;
1844 if (eld->eld_valid)
1845 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1846 pin_eld->info = eld->info;
1847
1848 /*
1849 * Re-setup pin and infoframe. This is needed e.g. when
1850 * - sink is first plugged-in
1851 * - transcoder can change during stream playback on Haswell
1852 * and this can make HW reset converter selection on a pin.
1853 */
1854 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1855 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1856 intel_verify_pin_cvt_connect(codec, per_pin);
1857 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1858 per_pin->mux_idx);
1859 }
1860
1861 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1862 }
1863
1864 if (eld_changed)
1865 snd_ctl_notify(codec->card,
1866 SNDRV_CTL_EVENT_MASK_VALUE |
1867 SNDRV_CTL_EVENT_MASK_INFO,
1868 &per_pin->eld_ctl->id);
1869}
1870
Takashi Iwai788d4412015-11-12 15:36:13 +01001871/* update ELD and jack state via HD-audio verbs */
1872static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1873 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001874{
David Henningsson464837a2013-11-07 13:38:25 +01001875 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001876 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001877 struct hdmi_spec *spec = codec->spec;
1878 struct hdmi_eld *eld = &spec->temp_eld;
1879 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001880 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001881 /*
1882 * Always execute a GetPinSense verb here, even when called from
1883 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1884 * response's PD bit is not the real PD value, but indicates that
1885 * the real PD value changed. An older version of the HD-audio
1886 * specification worked this way. Hence, we just ignore the data in
1887 * the unsolicited response to avoid custom WARs.
1888 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001889 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001890 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001891 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001892
Takashi Iwai664c7152015-04-08 11:43:14 +02001893 snd_hda_power_up_pm(codec);
David Henningssonda4a7a32013-12-18 10:46:04 +01001894 present = snd_hda_pin_sense(codec, pin_nid);
1895
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001896 mutex_lock(&per_pin->lock);
David Henningsson4bd038f2013-02-19 16:11:25 +01001897 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1898 if (pin_eld->monitor_present)
1899 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1900 else
1901 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001902
Takashi Iwai4e76a882014-02-25 12:21:03 +01001903 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001904 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001905 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001906
David Henningsson4bd038f2013-02-19 16:11:25 +01001907 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001908 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001909 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001910 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001911 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001912 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001913 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001914 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001915 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001916 if (!eld->eld_valid && repoll)
1917 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001918 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001919
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001920 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001921 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1922 else
1923 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001924
Takashi Iwaiaff747eb2013-11-07 16:39:37 +01001925 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001926
1927 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1928 if (jack)
1929 jack->block_report = !ret;
1930
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001931 mutex_unlock(&per_pin->lock);
Takashi Iwai664c7152015-04-08 11:43:14 +02001932 snd_hda_power_down_pm(codec);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001933 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001934}
1935
Takashi Iwai788d4412015-11-12 15:36:13 +01001936/* update ELD and jack state via audio component */
1937static void sync_eld_via_acomp(struct hda_codec *codec,
1938 struct hdmi_spec_per_pin *per_pin)
1939{
Takashi Iwai788d4412015-11-12 15:36:13 +01001940 struct hdmi_spec *spec = codec->spec;
1941 struct hdmi_eld *eld = &spec->temp_eld;
1942 int size;
1943
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001944 mutex_lock(&per_pin->lock);
1945 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1946 &eld->monitor_present, eld->eld_buffer,
1947 ELD_MAX_SIZE);
1948 if (size < 0)
1949 goto unlock;
1950 if (size > 0) {
1951 size = min(size, ELD_MAX_SIZE);
1952 if (snd_hdmi_parse_eld(codec, &eld->info,
1953 eld->eld_buffer, size) < 0)
1954 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001955 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001956
1957 if (size > 0) {
1958 eld->eld_valid = true;
1959 eld->eld_size = size;
1960 } else {
1961 eld->eld_valid = false;
1962 eld->eld_size = 0;
1963 }
1964
1965 update_eld(codec, per_pin, eld);
1966 snd_jack_report(per_pin->acomp_jack,
1967 eld->monitor_present ? SND_JACK_AVOUT : 0);
1968 unlock:
1969 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001970}
1971
1972static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1973{
1974 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08001975 struct hdmi_spec *spec = codec->spec;
1976 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001977
Libin Yanga76056f2015-12-16 16:48:15 +08001978 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001979 if (codec_has_acomp(codec)) {
1980 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001981 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01001982 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08001983 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01001984 }
Libin Yanga76056f2015-12-16 16:48:15 +08001985 mutex_unlock(&spec->pcm_lock);
1986
1987 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001988}
1989
Wu Fengguang744626d2011-11-16 16:29:47 +08001990static void hdmi_repoll_eld(struct work_struct *work)
1991{
1992 struct hdmi_spec_per_pin *per_pin =
1993 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1994
Wu Fengguangc6e84532011-11-18 16:59:32 -06001995 if (per_pin->repoll_count++ > 6)
1996 per_pin->repoll_count = 0;
1997
Takashi Iwaiefe47102013-11-07 13:38:23 +01001998 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1999 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08002000}
2001
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002002static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2003 hda_nid_t nid);
2004
Wu Fengguang079d88c2010-03-08 10:44:23 +08002005static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2006{
2007 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002008 unsigned int caps, config;
2009 int pin_idx;
2010 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02002011 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002012
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002013 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002014 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2015 return 0;
2016
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002017 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002018 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2019 return 0;
2020
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002021 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002022 intel_haswell_fixup_connect_list(codec, pin_nid);
2023
Stephen Warren384a48d2011-06-01 11:14:21 -06002024 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002025 per_pin = snd_array_new(&spec->pins);
2026 if (!per_pin)
2027 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002028
2029 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002030 per_pin->non_pcm = false;
Libin Yanga76056f2015-12-16 16:48:15 +08002031 if (spec->dyn_pcm_assign)
2032 per_pin->pcm_idx = -1;
2033 else
2034 per_pin->pcm_idx = pin_idx;
2035 per_pin->pin_nid_idx = pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002036
Stephen Warren384a48d2011-06-01 11:14:21 -06002037 err = hdmi_read_pin_conn(codec, pin_idx);
2038 if (err < 0)
2039 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002040
Wu Fengguang079d88c2010-03-08 10:44:23 +08002041 spec->num_pins++;
2042
Stephen Warren384a48d2011-06-01 11:14:21 -06002043 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002044}
2045
Stephen Warren384a48d2011-06-01 11:14:21 -06002046static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08002047{
2048 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002049 struct hdmi_spec_per_cvt *per_cvt;
2050 unsigned int chans;
2051 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002052
Stephen Warren384a48d2011-06-01 11:14:21 -06002053 chans = get_wcaps(codec, cvt_nid);
2054 chans = get_wcaps_channels(chans);
2055
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002056 per_cvt = snd_array_new(&spec->cvts);
2057 if (!per_cvt)
2058 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06002059
2060 per_cvt->cvt_nid = cvt_nid;
2061 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002062 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002063 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002064 if (chans > spec->channels_max)
2065 spec->channels_max = chans;
2066 }
Stephen Warren384a48d2011-06-01 11:14:21 -06002067
2068 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2069 &per_cvt->rates,
2070 &per_cvt->formats,
2071 &per_cvt->maxbps);
2072 if (err < 0)
2073 return err;
2074
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002075 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2076 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2077 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002078
2079 return 0;
2080}
2081
2082static int hdmi_parse_codec(struct hda_codec *codec)
2083{
2084 hda_nid_t nid;
2085 int i, nodes;
2086
Takashi Iwai7639a062015-03-03 10:07:24 +01002087 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002088 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002089 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08002090 return -EINVAL;
2091 }
2092
2093 for (i = 0; i < nodes; i++, nid++) {
2094 unsigned int caps;
2095 unsigned int type;
2096
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002097 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002098 type = get_wcaps_type(caps);
2099
2100 if (!(caps & AC_WCAP_DIGITAL))
2101 continue;
2102
2103 switch (type) {
2104 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06002105 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002106 break;
2107 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08002108 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002109 break;
2110 }
2111 }
2112
Wu Fengguang079d88c2010-03-08 10:44:23 +08002113 return 0;
2114}
2115
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002116/*
2117 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002118static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2119{
2120 struct hda_spdif_out *spdif;
2121 bool non_pcm;
2122
2123 mutex_lock(&codec->spdif_mutex);
2124 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2125 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2126 mutex_unlock(&codec->spdif_mutex);
2127 return non_pcm;
2128}
2129
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002130/*
2131 * HDMI callbacks
2132 */
2133
2134static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2135 struct hda_codec *codec,
2136 unsigned int stream_tag,
2137 unsigned int format,
2138 struct snd_pcm_substream *substream)
2139{
Stephen Warren384a48d2011-06-01 11:14:21 -06002140 hda_nid_t cvt_nid = hinfo->nid;
2141 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08002142 int pin_idx;
2143 struct hdmi_spec_per_pin *per_pin;
2144 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08002145 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002146 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07002147 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08002148 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002149
Libin Yang42b29872015-12-16 13:42:42 +08002150 mutex_lock(&spec->pcm_lock);
2151 pin_idx = hinfo_to_pin_index(codec, hinfo);
2152 if (spec->dyn_pcm_assign && pin_idx < 0) {
2153 /* when dyn_pcm_assign and pcm is not bound to a pin
2154 * skip pin setup and return 0 to make audio playback
2155 * be ongoing
2156 */
2157 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2158 snd_hda_codec_setup_stream(codec, cvt_nid,
2159 stream_tag, 0, format);
2160 mutex_unlock(&spec->pcm_lock);
2161 return 0;
2162 }
2163
2164 if (snd_BUG_ON(pin_idx < 0)) {
2165 mutex_unlock(&spec->pcm_lock);
2166 return -EINVAL;
2167 }
2168 per_pin = get_pin(spec, pin_idx);
2169 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08002170 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08002171 /* Verify pin:cvt selections to avoid silent audio after S3.
2172 * After S3, the audio driver restores pin:cvt selections
2173 * but this can happen before gfx is ready and such selection
2174 * is overlooked by HW. Thus multiple pins can share a same
2175 * default convertor and mute control will affect each other,
2176 * which can cause a resumed audio playback become silent
2177 * after S3.
2178 */
2179 intel_verify_pin_cvt_connect(codec, per_pin);
2180 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2181 }
2182
Libin Yangddd621f2015-09-02 14:11:40 +08002183 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2184 /* Todo: add DP1.2 MST audio support later */
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002185 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08002186
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002187 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002188 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02002189 per_pin->channels = substream->runtime->channels;
2190 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002191
Takashi Iwaib0540872013-09-02 12:33:02 +02002192 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002193 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07002194 if (spec->dyn_pin_out) {
2195 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2196 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2197 snd_hda_codec_write(codec, pin_nid, 0,
2198 AC_VERB_SET_PIN_WIDGET_CONTROL,
2199 pinctl | PIN_OUT);
2200 }
2201
Libin Yang42b29872015-12-16 13:42:42 +08002202 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2203 stream_tag, format);
2204 mutex_unlock(&spec->pcm_lock);
2205 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002206}
2207
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002208static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2209 struct hda_codec *codec,
2210 struct snd_pcm_substream *substream)
2211{
2212 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2213 return 0;
2214}
2215
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002216static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2217 struct hda_codec *codec,
2218 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06002219{
2220 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08002221 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06002222 struct hdmi_spec_per_cvt *per_cvt;
2223 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07002224 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06002225
Stephen Warren384a48d2011-06-01 11:14:21 -06002226 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08002227 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2228 if (snd_BUG_ON(pcm_idx < 0))
2229 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002230 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002231 if (snd_BUG_ON(cvt_idx < 0))
2232 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002233 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002234
2235 snd_BUG_ON(!per_cvt->assigned);
2236 per_cvt->assigned = 0;
2237 hinfo->nid = 0;
2238
Libin Yang42b29872015-12-16 13:42:42 +08002239 mutex_lock(&spec->pcm_lock);
Libin Yangac983792015-12-16 16:48:16 +08002240 clear_bit(pcm_idx, &spec->pcm_in_use);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002241 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08002242 if (spec->dyn_pcm_assign && pin_idx < 0) {
2243 mutex_unlock(&spec->pcm_lock);
2244 return 0;
2245 }
2246
2247 if (snd_BUG_ON(pin_idx < 0)) {
2248 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002249 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08002250 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002251 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002252
Stephen Warren75fae112014-01-30 11:52:16 -07002253 if (spec->dyn_pin_out) {
2254 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2255 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2256 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2257 AC_VERB_SET_PIN_WIDGET_CONTROL,
2258 pinctl & ~PIN_OUT);
2259 }
2260
Libin Yang2bf3c852015-12-16 13:42:43 +08002261 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Takashi Iwaicbbaa602013-10-17 18:03:24 +02002262
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002263 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002264 per_pin->chmap_set = false;
2265 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002266
2267 per_pin->setup = false;
2268 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002269 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08002270 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002271 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002272
Stephen Warren384a48d2011-06-01 11:14:21 -06002273 return 0;
2274}
2275
2276static const struct hda_pcm_ops generic_ops = {
2277 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002278 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002279 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002280 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002281};
2282
Takashi Iwaid45e6882012-07-31 11:36:00 +02002283/*
2284 * ALSA API channel-map control callbacks
2285 */
2286static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_info *uinfo)
2288{
2289 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2290 struct hda_codec *codec = info->private_data;
2291 struct hdmi_spec *spec = codec->spec;
2292 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2293 uinfo->count = spec->channels_max;
2294 uinfo->value.integer.min = 0;
2295 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2296 return 0;
2297}
2298
Anssi Hannula307229d2013-10-24 21:10:34 +03002299static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2300 int channels)
2301{
2302 /* If the speaker allocation matches the channel count, it is OK.*/
2303 if (cap->channels != channels)
2304 return -1;
2305
2306 /* all channels are remappable freely */
2307 return SNDRV_CTL_TLVT_CHMAP_VAR;
2308}
2309
2310static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2311 unsigned int *chmap, int channels)
2312{
2313 int count = 0;
2314 int c;
2315
2316 for (c = 7; c >= 0; c--) {
2317 int spk = cap->speakers[c];
2318 if (!spk)
2319 continue;
2320
2321 chmap[count++] = spk_to_chmap(spk);
2322 }
2323
2324 WARN_ON(count != channels);
2325}
2326
Takashi Iwaid45e6882012-07-31 11:36:00 +02002327static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2328 unsigned int size, unsigned int __user *tlv)
2329{
2330 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2331 struct hda_codec *codec = info->private_data;
2332 struct hdmi_spec *spec = codec->spec;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002333 unsigned int __user *dst;
2334 int chs, count = 0;
2335
2336 if (size < 8)
2337 return -ENOMEM;
2338 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2339 return -EFAULT;
2340 size -= 8;
2341 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02002342 for (chs = 2; chs <= spec->channels_max; chs++) {
Anssi Hannula307229d2013-10-24 21:10:34 +03002343 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002344 struct cea_channel_speaker_allocation *cap;
2345 cap = channel_allocations;
2346 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2347 int chs_bytes = chs * 4;
Anssi Hannula307229d2013-10-24 21:10:34 +03002348 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2349 unsigned int tlv_chmap[8];
2350
2351 if (type < 0)
Takashi Iwaid45e6882012-07-31 11:36:00 +02002352 continue;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002353 if (size < 8)
2354 return -ENOMEM;
Anssi Hannula307229d2013-10-24 21:10:34 +03002355 if (put_user(type, dst) ||
Takashi Iwaid45e6882012-07-31 11:36:00 +02002356 put_user(chs_bytes, dst + 1))
2357 return -EFAULT;
2358 dst += 2;
2359 size -= 8;
2360 count += 8;
2361 if (size < chs_bytes)
2362 return -ENOMEM;
2363 size -= chs_bytes;
2364 count += chs_bytes;
Anssi Hannula307229d2013-10-24 21:10:34 +03002365 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2366 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2367 return -EFAULT;
2368 dst += chs;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002369 }
2370 }
2371 if (put_user(count, tlv + 1))
2372 return -EFAULT;
2373 return 0;
2374}
2375
2376static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_value *ucontrol)
2378{
2379 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2380 struct hda_codec *codec = info->private_data;
2381 struct hdmi_spec *spec = codec->spec;
2382 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002383 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002384 int i;
2385
2386 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2387 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2388 return 0;
2389}
2390
2391static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2393{
2394 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2395 struct hda_codec *codec = info->private_data;
2396 struct hdmi_spec *spec = codec->spec;
2397 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002398 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002399 unsigned int ctl_idx;
2400 struct snd_pcm_substream *substream;
2401 unsigned char chmap[8];
Anssi Hannula307229d2013-10-24 21:10:34 +03002402 int i, err, ca, prepared = 0;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002403
2404 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2405 substream = snd_pcm_chmap_substream(info, ctl_idx);
2406 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01002407 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02002408 switch (substream->runtime->status->state) {
2409 case SNDRV_PCM_STATE_OPEN:
2410 case SNDRV_PCM_STATE_SETUP:
2411 break;
2412 case SNDRV_PCM_STATE_PREPARED:
2413 prepared = 1;
2414 break;
2415 default:
2416 return -EBUSY;
2417 }
2418 memset(chmap, 0, sizeof(chmap));
2419 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2420 chmap[i] = ucontrol->value.integer.value[i];
2421 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2422 return 0;
2423 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2424 if (ca < 0)
2425 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03002426 if (spec->ops.chmap_validate) {
2427 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2428 if (err)
2429 return err;
2430 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002431 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002432 per_pin->chmap_set = true;
2433 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2434 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02002435 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002436 mutex_unlock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002437
2438 return 0;
2439}
2440
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002441static int generic_hdmi_build_pcms(struct hda_codec *codec)
2442{
2443 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +08002444 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06002445 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002446
Stephen Warren384a48d2011-06-01 11:14:21 -06002447 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2448 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002449 struct hda_pcm_stream *pstr;
2450
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002451 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002452 if (!info)
2453 return -ENOMEM;
Libin Yang6590faa2015-12-16 13:42:41 +08002454 if (!spec->dyn_pcm_assign) {
2455 per_pin = get_pin(spec, pin_idx);
2456 per_pin->pcm = info;
2457 }
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002458 spec->pcm_rec[pin_idx] = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002459 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002460 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002461 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002462
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002463 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002464 pstr->substreams = 1;
2465 pstr->ops = generic_ops;
2466 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002467 }
2468
2469 return 0;
2470}
2471
Takashi Iwai788d4412015-11-12 15:36:13 +01002472static void free_acomp_jack_priv(struct snd_jack *jack)
2473{
2474 struct hdmi_spec_per_pin *per_pin = jack->private_data;
2475
2476 per_pin->acomp_jack = NULL;
2477}
2478
2479static int add_acomp_jack_kctl(struct hda_codec *codec,
2480 struct hdmi_spec_per_pin *per_pin,
2481 const char *name)
2482{
2483 struct snd_jack *jack;
2484 int err;
2485
2486 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2487 true, false);
2488 if (err < 0)
2489 return err;
2490 per_pin->acomp_jack = jack;
2491 jack->private_data = per_pin;
2492 jack->private_free = free_acomp_jack_priv;
2493 return 0;
2494}
2495
David Henningsson0b6c49b2011-08-23 16:56:03 +02002496static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
2497{
Takashi Iwai31ef2252011-12-01 17:41:36 +01002498 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02002499 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002500 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2501 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01002502 bool phantom_jack;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002503
Takashi Iwai31ef2252011-12-01 17:41:36 +01002504 if (pcmdev > 0)
2505 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Takashi Iwai788d4412015-11-12 15:36:13 +01002506 if (codec_has_acomp(codec))
2507 return add_acomp_jack_kctl(codec, per_pin, hdmi_str);
Takashi Iwai909cadc2015-11-12 11:52:13 +01002508 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2509 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01002510 strncat(hdmi_str, " Phantom",
2511 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002512
Takashi Iwai909cadc2015-11-12 11:52:13 +01002513 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2514 phantom_jack);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002515}
2516
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002517static int generic_hdmi_build_controls(struct hda_codec *codec)
2518{
2519 struct hdmi_spec *spec = codec->spec;
2520 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002521 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002522
Stephen Warren384a48d2011-06-01 11:14:21 -06002523 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002524 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002525
2526 err = generic_hdmi_build_jack(codec, pin_idx);
2527 if (err < 0)
2528 return err;
2529
Takashi Iwaidcda5802012-10-12 17:24:51 +02002530 err = snd_hda_create_dig_out_ctls(codec,
2531 per_pin->pin_nid,
2532 per_pin->mux_nids[0],
2533 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002534 if (err < 0)
2535 return err;
Libin Yang2bf3c852015-12-16 13:42:43 +08002536 /* pin number is the same with pcm number so far */
Stephen Warren384a48d2011-06-01 11:14:21 -06002537 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05002538
2539 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002540 err = hdmi_create_eld_ctl(codec, pin_idx,
2541 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05002542
2543 if (err < 0)
2544 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01002545
Takashi Iwai82b1d732011-12-20 15:53:07 +01002546 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002547 }
2548
Takashi Iwaid45e6882012-07-31 11:36:00 +02002549 /* add channel maps */
2550 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002551 struct hda_pcm *pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002552 struct snd_pcm_chmap *chmap;
2553 struct snd_kcontrol *kctl;
2554 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002555
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002556 pcm = spec->pcm_rec[pin_idx];
2557 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002558 break;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002559 err = snd_pcm_add_chmap_ctls(pcm->pcm,
Takashi Iwaid45e6882012-07-31 11:36:00 +02002560 SNDRV_PCM_STREAM_PLAYBACK,
2561 NULL, 0, pin_idx, &chmap);
2562 if (err < 0)
2563 return err;
2564 /* override handlers */
2565 chmap->private_data = codec;
2566 kctl = chmap->kctl;
2567 for (i = 0; i < kctl->count; i++)
2568 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2569 kctl->info = hdmi_chmap_ctl_info;
2570 kctl->get = hdmi_chmap_ctl_get;
2571 kctl->put = hdmi_chmap_ctl_put;
2572 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2573 }
2574
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002575 return 0;
2576}
2577
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002578static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2579{
2580 struct hdmi_spec *spec = codec->spec;
2581 int pin_idx;
2582
2583 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002584 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002585
2586 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002587 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002588 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002589 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002590 }
2591 return 0;
2592}
2593
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002594static int generic_hdmi_init(struct hda_codec *codec)
2595{
2596 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002597 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002598
Stephen Warren384a48d2011-06-01 11:14:21 -06002599 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002600 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002601 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002602
2603 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002604 if (!codec_has_acomp(codec))
2605 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2606 codec->jackpoll_interval > 0 ?
2607 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002608 }
2609 return 0;
2610}
2611
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002612static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2613{
2614 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2615 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002616}
2617
2618static void hdmi_array_free(struct hdmi_spec *spec)
2619{
2620 snd_array_free(&spec->pins);
2621 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002622}
2623
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002624static void generic_hdmi_free(struct hda_codec *codec)
2625{
2626 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002627 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002628
Takashi Iwai66032492015-12-01 16:49:35 +01002629 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002630 snd_hdac_i915_register_notifier(NULL);
2631
Stephen Warren384a48d2011-06-01 11:14:21 -06002632 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);
Stephen Warren384a48d2011-06-01 11:14:21 -06002634
Takashi Iwai2f35c632015-02-27 22:43:26 +01002635 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002636 eld_proc_free(per_pin);
Takashi Iwai788d4412015-11-12 15:36:13 +01002637 if (per_pin->acomp_jack)
2638 snd_device_free(codec->card, per_pin->acomp_jack);
Stephen Warren384a48d2011-06-01 11:14:21 -06002639 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002640
Takashi Iwai55913112015-12-10 13:03:29 +01002641 if (spec->i915_bound)
2642 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002643 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002644 kfree(spec);
2645}
2646
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002647#ifdef CONFIG_PM
2648static int generic_hdmi_resume(struct hda_codec *codec)
2649{
2650 struct hdmi_spec *spec = codec->spec;
2651 int pin_idx;
2652
Pierre Ossmana2833682014-06-18 21:48:09 +02002653 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002654 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002655
2656 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2657 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2658 hdmi_present_sense(per_pin, 1);
2659 }
2660 return 0;
2661}
2662#endif
2663
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002664static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002665 .init = generic_hdmi_init,
2666 .free = generic_hdmi_free,
2667 .build_pcms = generic_hdmi_build_pcms,
2668 .build_controls = generic_hdmi_build_controls,
2669 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002670#ifdef CONFIG_PM
2671 .resume = generic_hdmi_resume,
2672#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002673};
2674
Anssi Hannula307229d2013-10-24 21:10:34 +03002675static const struct hdmi_ops generic_standard_hdmi_ops = {
2676 .pin_get_eld = snd_hdmi_get_eld,
2677 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2678 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2679 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2680 .pin_hbr_setup = hdmi_pin_hbr_setup,
2681 .setup_stream = hdmi_setup_stream,
2682 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2683 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2684};
2685
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002686
2687static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2688 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002689{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002690 struct hdmi_spec *spec = codec->spec;
2691 hda_nid_t conns[4];
2692 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002693
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002694 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2695 if (nconns == spec->num_cvts &&
2696 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002697 return;
2698
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002699 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002700 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002701 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002702}
2703
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002704#define INTEL_VENDOR_NID 0x08
2705#define INTEL_GET_VENDOR_VERB 0xf81
2706#define INTEL_SET_VENDOR_VERB 0x781
2707#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2708#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2709
2710static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002711 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002712{
2713 unsigned int vendor_param;
2714
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002715 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2716 INTEL_GET_VENDOR_VERB, 0);
2717 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2718 return;
2719
2720 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2721 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2722 INTEL_SET_VENDOR_VERB, vendor_param);
2723 if (vendor_param == -1)
2724 return;
2725
Takashi Iwai17df3f52013-05-08 08:09:34 +02002726 if (update_tree)
2727 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002728}
2729
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002730static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2731{
2732 unsigned int vendor_param;
2733
2734 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2735 INTEL_GET_VENDOR_VERB, 0);
2736 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2737 return;
2738
2739 /* enable DP1.2 mode */
2740 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002741 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002742 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2743 INTEL_SET_VENDOR_VERB, vendor_param);
2744}
2745
Takashi Iwai17df3f52013-05-08 08:09:34 +02002746/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2747 * Otherwise you may get severe h/w communication errors.
2748 */
2749static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2750 unsigned int power_state)
2751{
2752 if (power_state == AC_PWRST_D0) {
2753 intel_haswell_enable_all_pins(codec, false);
2754 intel_haswell_fixup_enable_dp12(codec);
2755 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002756
Takashi Iwai17df3f52013-05-08 08:09:34 +02002757 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2758 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2759}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002760
David Henningssonf0675d42015-09-03 11:51:34 +02002761static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002762{
2763 struct hda_codec *codec = audio_ptr;
2764 int pin_nid = port + 0x04;
2765
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002766 /* skip notification during system suspend (but not in runtime PM);
2767 * the state will be updated at resume
2768 */
2769 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2770 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002771 /* ditto during suspend/resume process itself */
2772 if (atomic_read(&(codec)->core.in_pm))
2773 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002774
David Henningsson25adc132015-08-19 10:48:58 +02002775 check_presence_and_report(codec, pin_nid);
2776}
2777
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002778static int patch_generic_hdmi(struct hda_codec *codec)
2779{
2780 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002781
2782 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2783 if (spec == NULL)
2784 return -ENOMEM;
2785
Anssi Hannula307229d2013-10-24 21:10:34 +03002786 spec->ops = generic_standard_hdmi_ops;
Libin Yang42b29872015-12-16 13:42:42 +08002787 mutex_init(&spec->pcm_lock);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002788 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002789 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002790
Takashi Iwai55913112015-12-10 13:03:29 +01002791 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2792 if (!codec_has_acomp(codec) &&
2793 (codec->core.vendor_id >> 16) == 0x8086)
2794 if (!snd_hdac_i915_init(&codec->bus->core))
2795 spec->i915_bound = true;
2796
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002797 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002798 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002799 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002800 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002801
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002802 /* For Valleyview/Cherryview, only the display codec is in the display
2803 * power well and can use link_power ops to request/release the power.
2804 * For Haswell/Broadwell, the controller is also in the power well and
2805 * can cover the codec power request, and so need not set this flag.
2806 * For previous platforms, there is no such power well feature.
2807 */
Lu, Hanff9d8852015-11-19 23:25:13 +08002808 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2809 is_broxton(codec))
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002810 codec->core.link_power_control = 1;
2811
Takashi Iwai66032492015-12-01 16:49:35 +01002812 if (codec_has_acomp(codec)) {
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002813 codec->depop_delay = 0;
David Henningsson25adc132015-08-19 10:48:58 +02002814 spec->i915_audio_ops.audio_ptr = codec;
2815 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2816 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2817 }
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002818
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002819 if (hdmi_parse_codec(codec) < 0) {
Takashi Iwai55913112015-12-10 13:03:29 +01002820 if (spec->i915_bound)
2821 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002822 codec->spec = NULL;
2823 kfree(spec);
2824 return -EINVAL;
2825 }
2826 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002827 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002828 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002829 codec->dp_mst = true;
2830 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002831
Lu, Han2377c3c2015-06-09 16:50:38 +08002832 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2833 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2834 codec->auto_runtime_pm = 1;
2835
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002836 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002837
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002838 init_channel_allocations();
2839
2840 return 0;
2841}
2842
2843/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002844 * Shared non-generic implementations
2845 */
2846
2847static int simple_playback_build_pcms(struct hda_codec *codec)
2848{
2849 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002850 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002851 unsigned int chans;
2852 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002853 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002854
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002855 per_cvt = get_cvt(spec, 0);
2856 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002857 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002858
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002859 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002860 if (!info)
2861 return -ENOMEM;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002862 spec->pcm_rec[0] = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002863 info->pcm_type = HDA_PCM_TYPE_HDMI;
2864 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2865 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002866 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002867 if (pstr->channels_max <= 2 && chans && chans <= 16)
2868 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002869
2870 return 0;
2871}
2872
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002873/* unsolicited event for jack sensing */
2874static void simple_hdmi_unsol_event(struct hda_codec *codec,
2875 unsigned int res)
2876{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002877 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002878 snd_hda_jack_report_sync(codec);
2879}
2880
2881/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2882 * as long as spec->pins[] is set correctly
2883 */
2884#define simple_hdmi_build_jack generic_hdmi_build_jack
2885
Stephen Warren3aaf8982011-06-01 11:14:19 -06002886static int simple_playback_build_controls(struct hda_codec *codec)
2887{
2888 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002889 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002890 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002891
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002892 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002893 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2894 per_cvt->cvt_nid,
2895 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002896 if (err < 0)
2897 return err;
2898 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002899}
2900
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002901static int simple_playback_init(struct hda_codec *codec)
2902{
2903 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002904 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2905 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002906
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002907 snd_hda_codec_write(codec, pin, 0,
2908 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2909 /* some codecs require to unmute the pin */
2910 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2911 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2912 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002913 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002914 return 0;
2915}
2916
Stephen Warren3aaf8982011-06-01 11:14:19 -06002917static void simple_playback_free(struct hda_codec *codec)
2918{
2919 struct hdmi_spec *spec = codec->spec;
2920
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002921 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002922 kfree(spec);
2923}
2924
2925/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002926 * Nvidia specific implementations
2927 */
2928
2929#define Nv_VERB_SET_Channel_Allocation 0xF79
2930#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2931#define Nv_VERB_SET_Audio_Protection_On 0xF98
2932#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2933
2934#define nvhdmi_master_con_nid_7x 0x04
2935#define nvhdmi_master_pin_nid_7x 0x05
2936
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002937static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002938 /*front, rear, clfe, rear_surr */
2939 0x6, 0x8, 0xa, 0xc,
2940};
2941
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002942static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2943 /* set audio protect on */
2944 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2945 /* enable digital output on pin widget */
2946 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2947 {} /* terminator */
2948};
2949
2950static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002951 /* set audio protect on */
2952 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2953 /* enable digital output on pin widget */
2954 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2955 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2956 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2957 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2958 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2959 {} /* terminator */
2960};
2961
2962#ifdef LIMITED_RATE_FMT_SUPPORT
2963/* support only the safe format and rate */
2964#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2965#define SUPPORTED_MAXBPS 16
2966#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2967#else
2968/* support all rates and formats */
2969#define SUPPORTED_RATES \
2970 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2971 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2972 SNDRV_PCM_RATE_192000)
2973#define SUPPORTED_MAXBPS 24
2974#define SUPPORTED_FORMATS \
2975 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2976#endif
2977
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002978static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002979{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002980 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2981 return 0;
2982}
2983
2984static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2985{
2986 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002987 return 0;
2988}
2989
Nitin Daga393004b2011-01-10 21:49:31 +05302990static unsigned int channels_2_6_8[] = {
2991 2, 6, 8
2992};
2993
2994static unsigned int channels_2_8[] = {
2995 2, 8
2996};
2997
2998static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2999 .count = ARRAY_SIZE(channels_2_6_8),
3000 .list = channels_2_6_8,
3001 .mask = 0,
3002};
3003
3004static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3005 .count = ARRAY_SIZE(channels_2_8),
3006 .list = channels_2_8,
3007 .mask = 0,
3008};
3009
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003010static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3011 struct hda_codec *codec,
3012 struct snd_pcm_substream *substream)
3013{
3014 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05303015 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3016
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003017 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05303018 case 0x10de0002:
3019 case 0x10de0003:
3020 case 0x10de0005:
3021 case 0x10de0006:
3022 hw_constraints_channels = &hw_constraints_2_8_channels;
3023 break;
3024 case 0x10de0007:
3025 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3026 break;
3027 default:
3028 break;
3029 }
3030
3031 if (hw_constraints_channels != NULL) {
3032 snd_pcm_hw_constraint_list(substream->runtime, 0,
3033 SNDRV_PCM_HW_PARAM_CHANNELS,
3034 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01003035 } else {
3036 snd_pcm_hw_constraint_step(substream->runtime, 0,
3037 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05303038 }
3039
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003040 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3041}
3042
3043static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3044 struct hda_codec *codec,
3045 struct snd_pcm_substream *substream)
3046{
3047 struct hdmi_spec *spec = codec->spec;
3048 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3049}
3050
3051static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3052 struct hda_codec *codec,
3053 unsigned int stream_tag,
3054 unsigned int format,
3055 struct snd_pcm_substream *substream)
3056{
3057 struct hdmi_spec *spec = codec->spec;
3058 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3059 stream_tag, format, substream);
3060}
3061
Takashi Iwaid0b12522012-06-15 14:34:42 +02003062static const struct hda_pcm_stream simple_pcm_playback = {
3063 .substreams = 1,
3064 .channels_min = 2,
3065 .channels_max = 2,
3066 .ops = {
3067 .open = simple_playback_pcm_open,
3068 .close = simple_playback_pcm_close,
3069 .prepare = simple_playback_pcm_prepare
3070 },
3071};
3072
3073static const struct hda_codec_ops simple_hdmi_patch_ops = {
3074 .build_controls = simple_playback_build_controls,
3075 .build_pcms = simple_playback_build_pcms,
3076 .init = simple_playback_init,
3077 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02003078 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02003079};
3080
3081static int patch_simple_hdmi(struct hda_codec *codec,
3082 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3083{
3084 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003085 struct hdmi_spec_per_cvt *per_cvt;
3086 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003087
3088 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3089 if (!spec)
3090 return -ENOMEM;
3091
3092 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003093 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02003094
3095 spec->multiout.num_dacs = 0; /* no analog */
3096 spec->multiout.max_channels = 2;
3097 spec->multiout.dig_out_nid = cvt_nid;
3098 spec->num_cvts = 1;
3099 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003100 per_pin = snd_array_new(&spec->pins);
3101 per_cvt = snd_array_new(&spec->cvts);
3102 if (!per_pin || !per_cvt) {
3103 simple_playback_free(codec);
3104 return -ENOMEM;
3105 }
3106 per_cvt->cvt_nid = cvt_nid;
3107 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003108 spec->pcm_playback = simple_pcm_playback;
3109
3110 codec->patch_ops = simple_hdmi_patch_ops;
3111
3112 return 0;
3113}
3114
Aaron Plattner1f348522011-04-06 17:19:04 -07003115static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3116 int channels)
3117{
3118 unsigned int chanmask;
3119 int chan = channels ? (channels - 1) : 1;
3120
3121 switch (channels) {
3122 default:
3123 case 0:
3124 case 2:
3125 chanmask = 0x00;
3126 break;
3127 case 4:
3128 chanmask = 0x08;
3129 break;
3130 case 6:
3131 chanmask = 0x0b;
3132 break;
3133 case 8:
3134 chanmask = 0x13;
3135 break;
3136 }
3137
3138 /* Set the audio infoframe channel allocation and checksum fields. The
3139 * channel count is computed implicitly by the hardware. */
3140 snd_hda_codec_write(codec, 0x1, 0,
3141 Nv_VERB_SET_Channel_Allocation, chanmask);
3142
3143 snd_hda_codec_write(codec, 0x1, 0,
3144 Nv_VERB_SET_Info_Frame_Checksum,
3145 (0x71 - chan - chanmask));
3146}
3147
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003148static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3149 struct hda_codec *codec,
3150 struct snd_pcm_substream *substream)
3151{
3152 struct hdmi_spec *spec = codec->spec;
3153 int i;
3154
3155 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3156 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3157 for (i = 0; i < 4; i++) {
3158 /* set the stream id */
3159 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3160 AC_VERB_SET_CHANNEL_STREAMID, 0);
3161 /* set the stream format */
3162 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3163 AC_VERB_SET_STREAM_FORMAT, 0);
3164 }
3165
Aaron Plattner1f348522011-04-06 17:19:04 -07003166 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3167 * streams are disabled. */
3168 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3169
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003170 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3171}
3172
3173static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3174 struct hda_codec *codec,
3175 unsigned int stream_tag,
3176 unsigned int format,
3177 struct snd_pcm_substream *substream)
3178{
3179 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003180 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003181 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003182 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003183 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003184 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003185
3186 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003187 per_cvt = get_cvt(spec, 0);
3188 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003189
3190 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003191
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003192 dataDCC2 = 0x2;
3193
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003194 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003195 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003196 snd_hda_codec_write(codec,
3197 nvhdmi_master_con_nid_7x,
3198 0,
3199 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003200 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003201
3202 /* set the stream id */
3203 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3204 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3205
3206 /* set the stream format */
3207 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3208 AC_VERB_SET_STREAM_FORMAT, format);
3209
3210 /* turn on again (if needed) */
3211 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003212 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003213 snd_hda_codec_write(codec,
3214 nvhdmi_master_con_nid_7x,
3215 0,
3216 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003217 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003218 snd_hda_codec_write(codec,
3219 nvhdmi_master_con_nid_7x,
3220 0,
3221 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3222 }
3223
3224 for (i = 0; i < 4; i++) {
3225 if (chs == 2)
3226 channel_id = 0;
3227 else
3228 channel_id = i * 2;
3229
3230 /* turn off SPDIF once;
3231 *otherwise the IEC958 bits won't be updated
3232 */
3233 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003234 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003235 snd_hda_codec_write(codec,
3236 nvhdmi_con_nids_7x[i],
3237 0,
3238 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003239 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003240 /* set the stream id */
3241 snd_hda_codec_write(codec,
3242 nvhdmi_con_nids_7x[i],
3243 0,
3244 AC_VERB_SET_CHANNEL_STREAMID,
3245 (stream_tag << 4) | channel_id);
3246 /* set the stream format */
3247 snd_hda_codec_write(codec,
3248 nvhdmi_con_nids_7x[i],
3249 0,
3250 AC_VERB_SET_STREAM_FORMAT,
3251 format);
3252 /* turn on again (if needed) */
3253 /* enable and set the channel status audio/data flag */
3254 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003255 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003256 snd_hda_codec_write(codec,
3257 nvhdmi_con_nids_7x[i],
3258 0,
3259 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003260 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003261 snd_hda_codec_write(codec,
3262 nvhdmi_con_nids_7x[i],
3263 0,
3264 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3265 }
3266 }
3267
Aaron Plattner1f348522011-04-06 17:19:04 -07003268 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003269
3270 mutex_unlock(&codec->spdif_mutex);
3271 return 0;
3272}
3273
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003274static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003275 .substreams = 1,
3276 .channels_min = 2,
3277 .channels_max = 8,
3278 .nid = nvhdmi_master_con_nid_7x,
3279 .rates = SUPPORTED_RATES,
3280 .maxbps = SUPPORTED_MAXBPS,
3281 .formats = SUPPORTED_FORMATS,
3282 .ops = {
3283 .open = simple_playback_pcm_open,
3284 .close = nvhdmi_8ch_7x_pcm_close,
3285 .prepare = nvhdmi_8ch_7x_pcm_prepare
3286 },
3287};
3288
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003289static int patch_nvhdmi_2ch(struct hda_codec *codec)
3290{
3291 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003292 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3293 nvhdmi_master_pin_nid_7x);
3294 if (err < 0)
3295 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003296
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003297 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003298 /* override the PCM rates, etc, as the codec doesn't give full list */
3299 spec = codec->spec;
3300 spec->pcm_playback.rates = SUPPORTED_RATES;
3301 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3302 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003303 return 0;
3304}
3305
Takashi Iwai53775b02012-08-01 12:17:41 +02003306static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3307{
3308 struct hdmi_spec *spec = codec->spec;
3309 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003310 if (!err) {
3311 struct hda_pcm *info = get_pcm_rec(spec, 0);
3312 info->own_chmap = true;
3313 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003314 return err;
3315}
3316
3317static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3318{
3319 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003320 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003321 struct snd_pcm_chmap *chmap;
3322 int err;
3323
3324 err = simple_playback_build_controls(codec);
3325 if (err < 0)
3326 return err;
3327
3328 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003329 info = get_pcm_rec(spec, 0);
3330 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003331 SNDRV_PCM_STREAM_PLAYBACK,
3332 snd_pcm_alt_chmaps, 8, 0, &chmap);
3333 if (err < 0)
3334 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003335 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003336 case 0x10de0002:
3337 case 0x10de0003:
3338 case 0x10de0005:
3339 case 0x10de0006:
3340 chmap->channel_mask = (1U << 2) | (1U << 8);
3341 break;
3342 case 0x10de0007:
3343 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3344 }
3345 return 0;
3346}
3347
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003348static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3349{
3350 struct hdmi_spec *spec;
3351 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003352 if (err < 0)
3353 return err;
3354 spec = codec->spec;
3355 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003356 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003357 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003358 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3359 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003360
3361 /* Initialize the audio infoframe channel mask and checksum to something
3362 * valid */
3363 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3364
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003365 return 0;
3366}
3367
3368/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003369 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3370 * - 0x10de0015
3371 * - 0x10de0040
3372 */
3373static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3374 int channels)
3375{
3376 if (cap->ca_index == 0x00 && channels == 2)
3377 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3378
3379 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3380}
3381
3382static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3383{
3384 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3385 return -EINVAL;
3386
3387 return 0;
3388}
3389
3390static int patch_nvhdmi(struct hda_codec *codec)
3391{
3392 struct hdmi_spec *spec;
3393 int err;
3394
3395 err = patch_generic_hdmi(codec);
3396 if (err)
3397 return err;
3398
3399 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003400 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003401
3402 spec->ops.chmap_cea_alloc_validate_get_type =
3403 nvhdmi_chmap_cea_alloc_validate_get_type;
3404 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3405
3406 return 0;
3407}
3408
3409/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003410 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3411 * accessed using vendor-defined verbs. These registers can be used for
3412 * interoperability between the HDA and HDMI drivers.
3413 */
3414
3415/* Audio Function Group node */
3416#define NVIDIA_AFG_NID 0x01
3417
3418/*
3419 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3420 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3421 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3422 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3423 * additional bit (at position 30) to signal the validity of the format.
3424 *
3425 * | 31 | 30 | 29 16 | 15 0 |
3426 * +---------+-------+--------+--------+
3427 * | TRIGGER | VALID | UNUSED | FORMAT |
3428 * +-----------------------------------|
3429 *
3430 * Note that for the trigger bit to take effect it needs to change value
3431 * (i.e. it needs to be toggled).
3432 */
3433#define NVIDIA_GET_SCRATCH0 0xfa6
3434#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3435#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3436#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3437#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3438#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3439#define NVIDIA_SCRATCH_VALID (1 << 6)
3440
3441#define NVIDIA_GET_SCRATCH1 0xfab
3442#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3443#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3444#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3445#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3446
3447/*
3448 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3449 * the format is invalidated so that the HDMI codec can be disabled.
3450 */
3451static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3452{
3453 unsigned int value;
3454
3455 /* bits [31:30] contain the trigger and valid bits */
3456 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3457 NVIDIA_GET_SCRATCH0, 0);
3458 value = (value >> 24) & 0xff;
3459
3460 /* bits [15:0] are used to store the HDA format */
3461 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3462 NVIDIA_SET_SCRATCH0_BYTE0,
3463 (format >> 0) & 0xff);
3464 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3465 NVIDIA_SET_SCRATCH0_BYTE1,
3466 (format >> 8) & 0xff);
3467
3468 /* bits [16:24] are unused */
3469 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3470 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3471
3472 /*
3473 * Bit 30 signals that the data is valid and hence that HDMI audio can
3474 * be enabled.
3475 */
3476 if (format == 0)
3477 value &= ~NVIDIA_SCRATCH_VALID;
3478 else
3479 value |= NVIDIA_SCRATCH_VALID;
3480
3481 /*
3482 * Whenever the trigger bit is toggled, an interrupt is raised in the
3483 * HDMI codec. The HDMI driver will use that as trigger to update its
3484 * configuration.
3485 */
3486 value ^= NVIDIA_SCRATCH_TRIGGER;
3487
3488 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3489 NVIDIA_SET_SCRATCH0_BYTE3, value);
3490}
3491
3492static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3493 struct hda_codec *codec,
3494 unsigned int stream_tag,
3495 unsigned int format,
3496 struct snd_pcm_substream *substream)
3497{
3498 int err;
3499
3500 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3501 format, substream);
3502 if (err < 0)
3503 return err;
3504
3505 /* notify the HDMI codec of the format change */
3506 tegra_hdmi_set_format(codec, format);
3507
3508 return 0;
3509}
3510
3511static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3512 struct hda_codec *codec,
3513 struct snd_pcm_substream *substream)
3514{
3515 /* invalidate the format in the HDMI codec */
3516 tegra_hdmi_set_format(codec, 0);
3517
3518 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3519}
3520
3521static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3522{
3523 struct hdmi_spec *spec = codec->spec;
3524 unsigned int i;
3525
3526 for (i = 0; i < spec->num_pins; i++) {
3527 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3528
3529 if (pcm->pcm_type == type)
3530 return pcm;
3531 }
3532
3533 return NULL;
3534}
3535
3536static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3537{
3538 struct hda_pcm_stream *stream;
3539 struct hda_pcm *pcm;
3540 int err;
3541
3542 err = generic_hdmi_build_pcms(codec);
3543 if (err < 0)
3544 return err;
3545
3546 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3547 if (!pcm)
3548 return -ENODEV;
3549
3550 /*
3551 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3552 * codec about format changes.
3553 */
3554 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3555 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3556 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3557
3558 return 0;
3559}
3560
3561static int patch_tegra_hdmi(struct hda_codec *codec)
3562{
3563 int err;
3564
3565 err = patch_generic_hdmi(codec);
3566 if (err)
3567 return err;
3568
3569 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3570
3571 return 0;
3572}
3573
3574/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003575 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003576 */
3577
Anssi Hannula5a6135842013-10-24 21:10:35 +03003578#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003579 ((codec)->core.vendor_id == 0x1002aa01 && \
3580 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003581#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003582
Anssi Hannula5a6135842013-10-24 21:10:35 +03003583/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3584#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3585#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3586#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3587#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3588#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3589#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003590#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003591#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3592#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3593#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3594#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3595#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3596#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3597#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3598#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3599#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3600#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3601#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003602#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003603#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3604#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3605#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3606#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3607#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3608
Anssi Hannula84d69e72013-10-24 21:10:38 +03003609/* AMD specific HDA cvt verbs */
3610#define ATI_VERB_SET_RAMP_RATE 0x770
3611#define ATI_VERB_GET_RAMP_RATE 0xf70
3612
Anssi Hannula5a6135842013-10-24 21:10:35 +03003613#define ATI_OUT_ENABLE 0x1
3614
3615#define ATI_MULTICHANNEL_MODE_PAIRED 0
3616#define ATI_MULTICHANNEL_MODE_SINGLE 1
3617
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003618#define ATI_HBR_CAPABLE 0x01
3619#define ATI_HBR_ENABLE 0x10
3620
Anssi Hannula89250f82013-10-24 21:10:36 +03003621static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3622 unsigned char *buf, int *eld_size)
3623{
3624 /* call hda_eld.c ATI/AMD-specific function */
3625 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3626 is_amdhdmi_rev3_or_later(codec));
3627}
3628
Anssi Hannula5a6135842013-10-24 21:10:35 +03003629static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3630 int active_channels, int conn_type)
3631{
3632 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3633}
3634
3635static int atihdmi_paired_swap_fc_lfe(int pos)
3636{
3637 /*
3638 * ATI/AMD have automatic FC/LFE swap built-in
3639 * when in pairwise mapping mode.
3640 */
3641
3642 switch (pos) {
3643 /* see channel_allocations[].speakers[] */
3644 case 2: return 3;
3645 case 3: return 2;
3646 default: break;
3647 }
3648
3649 return pos;
3650}
3651
3652static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3653{
3654 struct cea_channel_speaker_allocation *cap;
3655 int i, j;
3656
3657 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3658
3659 cap = &channel_allocations[get_channel_allocation_order(ca)];
3660 for (i = 0; i < chs; ++i) {
3661 int mask = to_spk_mask(map[i]);
3662 bool ok = false;
3663 bool companion_ok = false;
3664
3665 if (!mask)
3666 continue;
3667
3668 for (j = 0 + i % 2; j < 8; j += 2) {
3669 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3670 if (cap->speakers[chan_idx] == mask) {
3671 /* channel is in a supported position */
3672 ok = true;
3673
3674 if (i % 2 == 0 && i + 1 < chs) {
3675 /* even channel, check the odd companion */
3676 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3677 int comp_mask_req = to_spk_mask(map[i+1]);
3678 int comp_mask_act = cap->speakers[comp_chan_idx];
3679
3680 if (comp_mask_req == comp_mask_act)
3681 companion_ok = true;
3682 else
3683 return -EINVAL;
3684 }
3685 break;
3686 }
3687 }
3688
3689 if (!ok)
3690 return -EINVAL;
3691
3692 if (companion_ok)
3693 i++; /* companion channel already checked */
3694 }
3695
3696 return 0;
3697}
3698
3699static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3700 int hdmi_slot, int stream_channel)
3701{
3702 int verb;
3703 int ati_channel_setup = 0;
3704
3705 if (hdmi_slot > 7)
3706 return -EINVAL;
3707
3708 if (!has_amd_full_remap_support(codec)) {
3709 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3710
3711 /* In case this is an odd slot but without stream channel, do not
3712 * disable the slot since the corresponding even slot could have a
3713 * channel. In case neither have a channel, the slot pair will be
3714 * disabled when this function is called for the even slot. */
3715 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3716 return 0;
3717
3718 hdmi_slot -= hdmi_slot % 2;
3719
3720 if (stream_channel != 0xf)
3721 stream_channel -= stream_channel % 2;
3722 }
3723
3724 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3725
3726 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3727
3728 if (stream_channel != 0xf)
3729 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3730
3731 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3732}
3733
3734static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3735 int asp_slot)
3736{
3737 bool was_odd = false;
3738 int ati_asp_slot = asp_slot;
3739 int verb;
3740 int ati_channel_setup;
3741
3742 if (asp_slot > 7)
3743 return -EINVAL;
3744
3745 if (!has_amd_full_remap_support(codec)) {
3746 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3747 if (ati_asp_slot % 2 != 0) {
3748 ati_asp_slot -= 1;
3749 was_odd = true;
3750 }
3751 }
3752
3753 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3754
3755 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3756
3757 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3758 return 0xf;
3759
3760 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3761}
3762
3763static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3764 int channels)
3765{
3766 int c;
3767
3768 /*
3769 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3770 * we need to take that into account (a single channel may take 2
3771 * channel slots if we need to carry a silent channel next to it).
3772 * On Rev3+ AMD codecs this function is not used.
3773 */
3774 int chanpairs = 0;
3775
3776 /* We only produce even-numbered channel count TLVs */
3777 if ((channels % 2) != 0)
3778 return -1;
3779
3780 for (c = 0; c < 7; c += 2) {
3781 if (cap->speakers[c] || cap->speakers[c+1])
3782 chanpairs++;
3783 }
3784
3785 if (chanpairs * 2 != channels)
3786 return -1;
3787
3788 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3789}
3790
3791static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3792 unsigned int *chmap, int channels)
3793{
3794 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3795 int count = 0;
3796 int c;
3797
3798 for (c = 7; c >= 0; c--) {
3799 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3800 int spk = cap->speakers[chan];
3801 if (!spk) {
3802 /* add N/A channel if the companion channel is occupied */
3803 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3804 chmap[count++] = SNDRV_CHMAP_NA;
3805
3806 continue;
3807 }
3808
3809 chmap[count++] = spk_to_chmap(spk);
3810 }
3811
3812 WARN_ON(count != channels);
3813}
3814
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003815static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3816 bool hbr)
3817{
3818 int hbr_ctl, hbr_ctl_new;
3819
3820 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003821 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003822 if (hbr)
3823 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3824 else
3825 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3826
Takashi Iwai4e76a882014-02-25 12:21:03 +01003827 codec_dbg(codec,
3828 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003829 pin_nid,
3830 hbr_ctl == hbr_ctl_new ? "" : "new-",
3831 hbr_ctl_new);
3832
3833 if (hbr_ctl != hbr_ctl_new)
3834 snd_hda_codec_write(codec, pin_nid, 0,
3835 ATI_VERB_SET_HBR_CONTROL,
3836 hbr_ctl_new);
3837
3838 } else if (hbr)
3839 return -EINVAL;
3840
3841 return 0;
3842}
3843
Anssi Hannula84d69e72013-10-24 21:10:38 +03003844static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3845 hda_nid_t pin_nid, u32 stream_tag, int format)
3846{
3847
3848 if (is_amdhdmi_rev3_or_later(codec)) {
3849 int ramp_rate = 180; /* default as per AMD spec */
3850 /* disable ramp-up/down for non-pcm as per AMD spec */
3851 if (format & AC_FMT_TYPE_NON_PCM)
3852 ramp_rate = 0;
3853
3854 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3855 }
3856
3857 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3858}
3859
3860
Anssi Hannula5a6135842013-10-24 21:10:35 +03003861static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003862{
3863 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003864 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003865
Anssi Hannula5a6135842013-10-24 21:10:35 +03003866 err = generic_hdmi_init(codec);
3867
3868 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003869 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003870
3871 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3872 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3873
3874 /* make sure downmix information in infoframe is zero */
3875 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3876
3877 /* enable channel-wise remap mode if supported */
3878 if (has_amd_full_remap_support(codec))
3879 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3880 ATI_VERB_SET_MULTICHANNEL_MODE,
3881 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003882 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003883
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003884 return 0;
3885}
3886
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003887static int patch_atihdmi(struct hda_codec *codec)
3888{
3889 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003890 struct hdmi_spec_per_cvt *per_cvt;
3891 int err, cvt_idx;
3892
3893 err = patch_generic_hdmi(codec);
3894
3895 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003896 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003897
3898 codec->patch_ops.init = atihdmi_init;
3899
Takashi Iwaid0b12522012-06-15 14:34:42 +02003900 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003901
Anssi Hannula89250f82013-10-24 21:10:36 +03003902 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003903 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3904 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3905 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003906 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003907 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003908
3909 if (!has_amd_full_remap_support(codec)) {
3910 /* override to ATI/AMD-specific versions with pairwise mapping */
3911 spec->ops.chmap_cea_alloc_validate_get_type =
3912 atihdmi_paired_chmap_cea_alloc_validate_get_type;
3913 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
3914 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
3915 }
3916
3917 /* ATI/AMD converters do not advertise all of their capabilities */
3918 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3919 per_cvt = get_cvt(spec, cvt_idx);
3920 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3921 per_cvt->rates |= SUPPORTED_RATES;
3922 per_cvt->formats |= SUPPORTED_FORMATS;
3923 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3924 }
3925
3926 spec->channels_max = max(spec->channels_max, 8u);
3927
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003928 return 0;
3929}
3930
Annie Liu3de5ff82012-06-08 19:18:42 +08003931/* VIA HDMI Implementation */
3932#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3933#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3934
Annie Liu3de5ff82012-06-08 19:18:42 +08003935static int patch_via_hdmi(struct hda_codec *codec)
3936{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003937 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003938}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003939
3940/*
3941 * patch entries
3942 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003943static const struct hda_device_id snd_hda_id_hdmi[] = {
3944HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3945HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3946HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3947HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3948HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3949HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3950HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3951HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3952HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3953HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3954HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3955HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3956HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3957HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3958HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3959HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3960HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3961HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3962HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3963HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3964HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3965HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3966HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01003967/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003968HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3969HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3970HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3971HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3972HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3973HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3974HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3975HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3976HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3977HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3978HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3979HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3980HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3981HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3982HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3983HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3984HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3985HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3986HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3987HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3988HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
3989HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3990HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3991HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3992HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3993HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3994HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
3995HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3996HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3997HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3998HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
3999HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4000HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4001HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4002HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4003HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4004HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
Libin Yang91815d82016-01-14 14:09:00 +08004005HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004006HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4007HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4008HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4009HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004010/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004011HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004012{} /* terminator */
4013};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004014MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004015
4016MODULE_LICENSE("GPL");
4017MODULE_DESCRIPTION("HDMI HD-audio codec");
4018MODULE_ALIAS("snd-hda-codec-intelhdmi");
4019MODULE_ALIAS("snd-hda-codec-nvhdmi");
4020MODULE_ALIAS("snd-hda-codec-atihdmi");
4021
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004022static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02004023 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02004024};
4025
Takashi Iwaid8a766a2015-02-17 15:25:37 +01004026module_hda_codec_driver(hdmi_driver);