blob: 91c716541f61e1adef26e3565ae592c0c4c765f9 [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;
78 int num_mux_nids;
79 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Mengdong Lin2df67422014-03-20 13:01:06 +080080 int mux_idx;
Anssi Hannula1df5a062013-10-05 02:25:40 +030081 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080082
83 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060084 struct hdmi_eld sink_eld;
Takashi Iwaia4e9a382013-10-17 18:21:12 +020085 struct mutex lock;
Wu Fengguang744626d2011-11-16 16:29:47 +080086 struct delayed_work work;
David Henningsson92c69e72013-02-19 16:11:26 +010087 struct snd_kcontrol *eld_ctl;
Takashi Iwai788d4412015-11-12 15:36:13 +010088 struct snd_jack *acomp_jack; /* jack via audio component */
Libin Yang6590faa2015-12-16 13:42:41 +080089 struct hda_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Wu Fengguangc6e84532011-11-18 16:59:32 -060090 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020091 bool setup; /* the stream has been set up by prepare callback */
92 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020093 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020094 bool chmap_set; /* channel-map override by ALSA API? */
95 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080096#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +020097 struct snd_info_entry *proc_entry;
98#endif
Stephen Warren384a48d2011-06-01 11:14:21 -060099};
100
Anssi Hannula307229d2013-10-24 21:10:34 +0300101struct cea_channel_speaker_allocation;
102
103/* operations used by generic code that can be overridden by patches */
104struct hdmi_ops {
105 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
106 unsigned char *buf, int *eld_size);
107
108 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
109 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
110 int asp_slot);
111 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
112 int asp_slot, int channel);
113
114 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
115 int ca, int active_channels, int conn_type);
116
117 /* enable/disable HBR (HD passthrough) */
118 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
119
120 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
121 hda_nid_t pin_nid, u32 stream_tag, int format);
122
123 /* Helpers for producing the channel map TLVs. These can be overridden
124 * for devices that have non-standard mapping requirements. */
125 int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
126 int channels);
127 void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
128 unsigned int *chmap, int channels);
129
130 /* check that the user-given chmap is supported */
131 int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
132};
133
Wu Fengguang079d88c2010-03-08 10:44:23 +0800134struct hdmi_spec {
135 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100136 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
137 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600138
Wu Fengguang079d88c2010-03-08 10:44:23 +0800139 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100140 struct snd_array pins; /* struct hdmi_spec_per_pin */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100141 struct hda_pcm *pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800142 struct mutex pcm_lock;
Libin Yang2bf3c852015-12-16 13:42:43 +0800143 int pcm_used; /* counter of pcm_rec[] */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200144 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800145
David Henningsson4bd038f2013-02-19 16:11:25 +0100146 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300147 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700148
149 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800150 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800151 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300152 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800153 */
154 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200155 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200156
157 /* i915/powerwell (Haswell+/Valleyview+) specific */
158 struct i915_audio_component_audio_ops i915_audio_ops;
Takashi Iwai55913112015-12-10 13:03:29 +0100159 bool i915_bound; /* was i915 bound in this driver? */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800160};
161
Takashi Iwaif4e30402015-12-10 13:01:28 +0100162#ifdef CONFIG_SND_HDA_I915
Takashi Iwai66032492015-12-01 16:49:35 +0100163#define codec_has_acomp(codec) \
164 ((codec)->bus->core.audio_component != NULL)
Takashi Iwaif4e30402015-12-10 13:01:28 +0100165#else
166#define codec_has_acomp(codec) false
167#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800168
169struct hdmi_audio_infoframe {
170 u8 type; /* 0x84 */
171 u8 ver; /* 0x01 */
172 u8 len; /* 0x0a */
173
Wu Fengguang53d7d692010-09-21 14:25:49 +0800174 u8 checksum;
175
Wu Fengguang079d88c2010-03-08 10:44:23 +0800176 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
177 u8 SS01_SF24;
178 u8 CXT04;
179 u8 CA;
180 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800181};
182
183struct dp_audio_infoframe {
184 u8 type; /* 0x84 */
185 u8 len; /* 0x1b */
186 u8 ver; /* 0x11 << 2 */
187
188 u8 CC02_CT47; /* match with HDMI infoframe from this on */
189 u8 SS01_SF24;
190 u8 CXT04;
191 u8 CA;
192 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800193};
194
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100195union audio_infoframe {
196 struct hdmi_audio_infoframe hdmi;
197 struct dp_audio_infoframe dp;
198 u8 bytes[0];
199};
200
Wu Fengguang079d88c2010-03-08 10:44:23 +0800201/*
202 * CEA speaker placement:
203 *
204 * FLH FCH FRH
205 * FLW FL FLC FC FRC FR FRW
206 *
207 * LFE
208 * TC
209 *
210 * RL RLC RC RRC RR
211 *
212 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
213 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
214 */
215enum cea_speaker_placement {
216 FL = (1 << 0), /* Front Left */
217 FC = (1 << 1), /* Front Center */
218 FR = (1 << 2), /* Front Right */
219 FLC = (1 << 3), /* Front Left Center */
220 FRC = (1 << 4), /* Front Right Center */
221 RL = (1 << 5), /* Rear Left */
222 RC = (1 << 6), /* Rear Center */
223 RR = (1 << 7), /* Rear Right */
224 RLC = (1 << 8), /* Rear Left Center */
225 RRC = (1 << 9), /* Rear Right Center */
226 LFE = (1 << 10), /* Low Frequency Effect */
227 FLW = (1 << 11), /* Front Left Wide */
228 FRW = (1 << 12), /* Front Right Wide */
229 FLH = (1 << 13), /* Front Left High */
230 FCH = (1 << 14), /* Front Center High */
231 FRH = (1 << 15), /* Front Right High */
232 TC = (1 << 16), /* Top Center */
233};
234
235/*
236 * ELD SA bits in the CEA Speaker Allocation data block
237 */
238static int eld_speaker_allocation_bits[] = {
239 [0] = FL | FR,
240 [1] = LFE,
241 [2] = FC,
242 [3] = RL | RR,
243 [4] = RC,
244 [5] = FLC | FRC,
245 [6] = RLC | RRC,
246 /* the following are not defined in ELD yet */
247 [7] = FLW | FRW,
248 [8] = FLH | FRH,
249 [9] = TC,
250 [10] = FCH,
251};
252
253struct cea_channel_speaker_allocation {
254 int ca_index;
255 int speakers[8];
256
257 /* derived values, just for convenience */
258 int channels;
259 int spk_mask;
260};
261
262/*
263 * ALSA sequence is:
264 *
265 * surround40 surround41 surround50 surround51 surround71
266 * ch0 front left = = = =
267 * ch1 front right = = = =
268 * ch2 rear left = = = =
269 * ch3 rear right = = = =
270 * ch4 LFE center center center
271 * ch5 LFE LFE
272 * ch6 side left
273 * ch7 side right
274 *
275 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
276 */
277static int hdmi_channel_mapping[0x32][8] = {
278 /* stereo */
279 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
280 /* 2.1 */
281 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
282 /* Dolby Surround */
283 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
284 /* surround40 */
285 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
286 /* 4ch */
287 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
288 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800289 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800290 /* surround50 */
291 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
292 /* surround51 */
293 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
294 /* 7.1 */
295 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
296};
297
298/*
299 * This is an ordered list!
300 *
301 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800302 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800303 */
304static struct cea_channel_speaker_allocation channel_allocations[] = {
305/* channel: 7 6 5 4 3 2 1 0 */
306{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
307 /* 2.1 */
308{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
309 /* Dolby Surround */
310{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
311 /* surround40 */
312{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
313 /* surround41 */
314{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
315 /* surround50 */
316{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
317 /* surround51 */
318{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
319 /* 6.1 */
320{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
321 /* surround71 */
322{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
323
324{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
325{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
326{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
327{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
328{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
329{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
330{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
331{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
332{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
333{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
334{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
335{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
336{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
337{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
338{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
339{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
340{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
341{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
342{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
343{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
344{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
345{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
346{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
347{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
348{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
349{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
350{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
351{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
352{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
353{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
354{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
355{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
356{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
357{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
358{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
359{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
360{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
361{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
362{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
363{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
364{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
365};
366
367
368/*
369 * HDMI routines
370 */
371
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100372#define get_pin(spec, idx) \
373 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
374#define get_cvt(spec, idx) \
375 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100376#define get_pcm_rec(spec, idx) ((spec)->pcm_rec[idx])
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100377
Takashi Iwai4e76a882014-02-25 12:21:03 +0100378static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800379{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100380 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600381 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800382
Stephen Warren384a48d2011-06-01 11:14:21 -0600383 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100384 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600385 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800386
Takashi Iwai4e76a882014-02-25 12:21:03 +0100387 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600388 return -EINVAL;
389}
390
Libin Yang2bf3c852015-12-16 13:42:43 +0800391static int hinfo_to_pcm_index(struct hda_codec *codec,
392 struct hda_pcm_stream *hinfo)
393{
394 struct hdmi_spec *spec = codec->spec;
395 int pcm_idx;
396
397 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
398 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
399 return pcm_idx;
400
401 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
402 return -EINVAL;
403}
404
Takashi Iwai4e76a882014-02-25 12:21:03 +0100405static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600406 struct hda_pcm_stream *hinfo)
407{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100408 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800409 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600410 int pin_idx;
411
Libin Yang6590faa2015-12-16 13:42:41 +0800412 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
413 per_pin = get_pin(spec, pin_idx);
414 if (per_pin->pcm && per_pin->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600415 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800416 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600417
Libin Yang6590faa2015-12-16 13:42:41 +0800418 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600419 return -EINVAL;
420}
421
Takashi Iwai4e76a882014-02-25 12:21:03 +0100422static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600423{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100424 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600425 int cvt_idx;
426
427 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100428 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600429 return cvt_idx;
430
Takashi Iwai4e76a882014-02-25 12:21:03 +0100431 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800432 return -EINVAL;
433}
434
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500435static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
436 struct snd_ctl_elem_info *uinfo)
437{
438 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100439 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200440 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100441 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500442 int pin_idx;
443
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500444 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
445
446 pin_idx = kcontrol->private_value;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200447 per_pin = get_pin(spec, pin_idx);
448 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100449
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200450 mutex_lock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100451 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200452 mutex_unlock(&per_pin->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500453
454 return 0;
455}
456
457static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
458 struct snd_ctl_elem_value *ucontrol)
459{
460 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100461 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200462 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100463 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500464 int pin_idx;
465
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500466 pin_idx = kcontrol->private_value;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200467 per_pin = get_pin(spec, pin_idx);
468 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500469
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200470 mutex_lock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100471 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200472 mutex_unlock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100473 snd_BUG();
474 return -EINVAL;
475 }
476
477 memset(ucontrol->value.bytes.data, 0,
478 ARRAY_SIZE(ucontrol->value.bytes.data));
479 if (eld->eld_valid)
480 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
481 eld->eld_size);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200482 mutex_unlock(&per_pin->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500483
484 return 0;
485}
486
487static struct snd_kcontrol_new eld_bytes_ctl = {
488 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
489 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
490 .name = "ELD",
491 .info = hdmi_eld_ctl_info,
492 .get = hdmi_eld_ctl_get,
493};
494
495static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
496 int device)
497{
498 struct snd_kcontrol *kctl;
499 struct hdmi_spec *spec = codec->spec;
500 int err;
501
502 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
503 if (!kctl)
504 return -ENOMEM;
505 kctl->private_value = pin_idx;
506 kctl->id.device = device;
507
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100508 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500509 if (err < 0)
510 return err;
511
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100512 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500513 return 0;
514}
515
Wu Fengguang079d88c2010-03-08 10:44:23 +0800516#ifdef BE_PARANOID
517static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
518 int *packet_index, int *byte_index)
519{
520 int val;
521
522 val = snd_hda_codec_read(codec, pin_nid, 0,
523 AC_VERB_GET_HDMI_DIP_INDEX, 0);
524
525 *packet_index = val >> 5;
526 *byte_index = val & 0x1f;
527}
528#endif
529
530static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
531 int packet_index, int byte_index)
532{
533 int val;
534
535 val = (packet_index << 5) | (byte_index & 0x1f);
536
537 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
538}
539
540static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
541 unsigned char val)
542{
543 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
544}
545
Stephen Warren384a48d2011-06-01 11:14:21 -0600546static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800547{
Stephen Warren75fae112014-01-30 11:52:16 -0700548 struct hdmi_spec *spec = codec->spec;
549 int pin_out;
550
Wu Fengguang079d88c2010-03-08 10:44:23 +0800551 /* Unmute */
552 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
553 snd_hda_codec_write(codec, pin_nid, 0,
554 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700555
556 if (spec->dyn_pin_out)
557 /* Disable pin out until stream is active */
558 pin_out = 0;
559 else
560 /* Enable pin out: some machines with GM965 gets broken output
561 * when the pin is disabled or changed while using with HDMI
562 */
563 pin_out = PIN_OUT;
564
Wu Fengguang079d88c2010-03-08 10:44:23 +0800565 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700566 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800567}
568
Stephen Warren384a48d2011-06-01 11:14:21 -0600569static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800570{
Stephen Warren384a48d2011-06-01 11:14:21 -0600571 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800572 AC_VERB_GET_CVT_CHAN_COUNT, 0);
573}
574
575static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600576 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800577{
Stephen Warren384a48d2011-06-01 11:14:21 -0600578 if (chs != hdmi_get_channel_count(codec, cvt_nid))
579 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800580 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
581}
582
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200583/*
584 * ELD proc files
585 */
586
Jie Yangcd6a6502015-05-27 19:45:45 +0800587#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200588static void print_eld_info(struct snd_info_entry *entry,
589 struct snd_info_buffer *buffer)
590{
591 struct hdmi_spec_per_pin *per_pin = entry->private_data;
592
593 mutex_lock(&per_pin->lock);
594 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
595 mutex_unlock(&per_pin->lock);
596}
597
598static void write_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_write_eld_info(&per_pin->sink_eld, buffer);
605 mutex_unlock(&per_pin->lock);
606}
607
608static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
609{
610 char name[32];
611 struct hda_codec *codec = per_pin->codec;
612 struct snd_info_entry *entry;
613 int err;
614
615 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100616 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200617 if (err < 0)
618 return err;
619
620 snd_info_set_text_ops(entry, per_pin, print_eld_info);
621 entry->c.text.write = write_eld_info;
622 entry->mode |= S_IWUSR;
623 per_pin->proc_entry = entry;
624
625 return 0;
626}
627
628static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
629{
Markus Elfring1947a112015-06-28 11:15:28 +0200630 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200631 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200632 per_pin->proc_entry = NULL;
633 }
634}
635#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200636static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
637 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200638{
639 return 0;
640}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200641static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200642{
643}
644#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800645
646/*
647 * Channel mapping routines
648 */
649
650/*
651 * Compute derived values in channel_allocations[].
652 */
653static void init_channel_allocations(void)
654{
655 int i, j;
656 struct cea_channel_speaker_allocation *p;
657
658 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
659 p = channel_allocations + i;
660 p->channels = 0;
661 p->spk_mask = 0;
662 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
663 if (p->speakers[j]) {
664 p->channels++;
665 p->spk_mask |= p->speakers[j];
666 }
667 }
668}
669
Wang Xingchao72357c72012-09-06 10:02:36 +0800670static int get_channel_allocation_order(int ca)
671{
672 int i;
673
674 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
675 if (channel_allocations[i].ca_index == ca)
676 break;
677 }
678 return i;
679}
680
Wu Fengguang079d88c2010-03-08 10:44:23 +0800681/*
682 * The transformation takes two steps:
683 *
684 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
685 * spk_mask => (channel_allocations[]) => ai->CA
686 *
687 * TODO: it could select the wrong CA from multiple candidates.
688*/
Takashi Iwai79514d42014-06-06 18:04:34 +0200689static int hdmi_channel_allocation(struct hda_codec *codec,
690 struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800691{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800692 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800693 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800694 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800695 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
696
697 /*
698 * CA defaults to 0 for basic stereo audio
699 */
700 if (channels <= 2)
701 return 0;
702
Wu Fengguang079d88c2010-03-08 10:44:23 +0800703 /*
704 * expand ELD's speaker allocation mask
705 *
706 * ELD tells the speaker mask in a compact(paired) form,
707 * expand ELD's notions to match the ones used by Audio InfoFrame.
708 */
709 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100710 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800711 spk_mask |= eld_speaker_allocation_bits[i];
712 }
713
714 /* search for the first working match in the CA table */
715 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
716 if (channels == channel_allocations[i].channels &&
717 (spk_mask & channel_allocations[i].spk_mask) ==
718 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800719 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800720 break;
721 }
722 }
723
Anssi Hannula18e39182013-09-01 14:36:47 +0300724 if (!ca) {
725 /* if there was no match, select the regular ALSA channel
726 * allocation with the matching number of channels */
727 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
728 if (channels == channel_allocations[i].channels) {
729 ca = channel_allocations[i].ca_index;
730 break;
731 }
732 }
733 }
734
David Henningsson1613d6b2013-02-19 16:11:24 +0100735 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Takashi Iwai79514d42014-06-06 18:04:34 +0200736 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800737 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800738
Wu Fengguang53d7d692010-09-21 14:25:49 +0800739 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800740}
741
742static void hdmi_debug_channel_mapping(struct hda_codec *codec,
743 hda_nid_t pin_nid)
744{
745#ifdef CONFIG_SND_DEBUG_VERBOSE
Anssi Hannula307229d2013-10-24 21:10:34 +0300746 struct hdmi_spec *spec = codec->spec;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800747 int i;
Anssi Hannula307229d2013-10-24 21:10:34 +0300748 int channel;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800749
750 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300751 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100752 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300753 channel, i);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800754 }
755#endif
756}
757
Takashi Iwaid45e6882012-07-31 11:36:00 +0200758static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800759 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800760 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800761 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800762{
Anssi Hannula307229d2013-10-24 21:10:34 +0300763 struct hdmi_spec *spec = codec->spec;
Anssi Hannula90f28002013-10-05 02:25:39 +0300764 struct cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800765 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800766 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800767 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800768 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800769
Wang Xingchao72357c72012-09-06 10:02:36 +0800770 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300771 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800772
Wu Fengguang079d88c2010-03-08 10:44:23 +0800773 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300774 int hdmi_slot = 0;
775 /* fill actual channel mappings in ALSA channel (i) order */
776 for (i = 0; i < ch_alloc->channels; i++) {
777 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
778 hdmi_slot++; /* skip zero slots */
779
780 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
781 }
782 /* fill the rest of the slots with ALSA channel 0xf */
783 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
784 if (!ch_alloc->speakers[7 - hdmi_slot])
785 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800786 }
787
Wang Xingchao433968d2012-09-06 10:02:37 +0800788 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300789 for (i = 0; i < ch_alloc->channels; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300790 non_pcm_mapping[i] = (i << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800791 for (; i < 8; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300792 non_pcm_mapping[i] = (0xf << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800793 }
794
Wu Fengguang079d88c2010-03-08 10:44:23 +0800795 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300796 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
797 int hdmi_slot = slotsetup & 0x0f;
798 int channel = (slotsetup & 0xf0) >> 4;
799 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800800 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100801 codec_dbg(codec, "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800802 break;
803 }
804 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800805}
806
Takashi Iwaid45e6882012-07-31 11:36:00 +0200807struct channel_map_table {
808 unsigned char map; /* ALSA API channel map position */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200809 int spk_mask; /* speaker position bit mask */
810};
811
812static struct channel_map_table map_tables[] = {
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300813 { SNDRV_CHMAP_FL, FL },
814 { SNDRV_CHMAP_FR, FR },
815 { SNDRV_CHMAP_RL, RL },
816 { SNDRV_CHMAP_RR, RR },
817 { SNDRV_CHMAP_LFE, LFE },
818 { SNDRV_CHMAP_FC, FC },
819 { SNDRV_CHMAP_RLC, RLC },
820 { SNDRV_CHMAP_RRC, RRC },
821 { SNDRV_CHMAP_RC, RC },
822 { SNDRV_CHMAP_FLC, FLC },
823 { SNDRV_CHMAP_FRC, FRC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200824 { SNDRV_CHMAP_TFL, FLH },
825 { SNDRV_CHMAP_TFR, FRH },
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300826 { SNDRV_CHMAP_FLW, FLW },
827 { SNDRV_CHMAP_FRW, FRW },
828 { SNDRV_CHMAP_TC, TC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200829 { SNDRV_CHMAP_TFC, FCH },
Takashi Iwaid45e6882012-07-31 11:36:00 +0200830 {} /* terminator */
831};
832
833/* from ALSA API channel position to speaker bit mask */
834static int to_spk_mask(unsigned char c)
835{
836 struct channel_map_table *t = map_tables;
837 for (; t->map; t++) {
838 if (t->map == c)
839 return t->spk_mask;
840 }
841 return 0;
842}
843
844/* from ALSA API channel position to CEA slot */
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300845static int to_cea_slot(int ordered_ca, unsigned char pos)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200846{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300847 int mask = to_spk_mask(pos);
848 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200849
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300850 if (mask) {
851 for (i = 0; i < 8; i++) {
852 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
853 return i;
854 }
Takashi Iwaid45e6882012-07-31 11:36:00 +0200855 }
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300856
857 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200858}
859
860/* from speaker bit mask to ALSA API channel position */
861static int spk_to_chmap(int spk)
862{
863 struct channel_map_table *t = map_tables;
864 for (; t->map; t++) {
865 if (t->spk_mask == spk)
866 return t->map;
867 }
868 return 0;
869}
870
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300871/* from CEA slot to ALSA API channel position */
872static int from_cea_slot(int ordered_ca, unsigned char slot)
873{
874 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
875
876 return spk_to_chmap(mask);
877}
878
Takashi Iwaid45e6882012-07-31 11:36:00 +0200879/* get the CA index corresponding to the given ALSA API channel map */
880static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
881{
882 int i, spks = 0, spk_mask = 0;
883
884 for (i = 0; i < chs; i++) {
885 int mask = to_spk_mask(map[i]);
886 if (mask) {
887 spk_mask |= mask;
888 spks++;
889 }
890 }
891
892 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
893 if ((chs == channel_allocations[i].channels ||
894 spks == channel_allocations[i].channels) &&
895 (spk_mask & channel_allocations[i].spk_mask) ==
896 channel_allocations[i].spk_mask)
897 return channel_allocations[i].ca_index;
898 }
899 return -1;
900}
901
902/* set up the channel slots for the given ALSA API channel map */
903static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
904 hda_nid_t pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300905 int chs, unsigned char *map,
906 int ca)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200907{
Anssi Hannula307229d2013-10-24 21:10:34 +0300908 struct hdmi_spec *spec = codec->spec;
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300909 int ordered_ca = get_channel_allocation_order(ca);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300910 int alsa_pos, hdmi_slot;
911 int assignments[8] = {[0 ... 7] = 0xf};
912
913 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
914
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300915 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300916
917 if (hdmi_slot < 0)
918 continue; /* unassigned channel */
919
920 assignments[hdmi_slot] = alsa_pos;
921 }
922
923 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300924 int err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300925
Anssi Hannula307229d2013-10-24 21:10:34 +0300926 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
927 assignments[hdmi_slot]);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200928 if (err)
929 return -EINVAL;
930 }
931 return 0;
932}
933
934/* store ALSA API channel map from the current default map */
935static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
936{
937 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300938 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200939 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300940 if (i < channel_allocations[ordered_ca].channels)
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300941 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200942 else
943 map[i] = 0;
944 }
945}
946
947static void hdmi_setup_channel_mapping(struct hda_codec *codec,
948 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200949 int channels, unsigned char *map,
950 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200951{
Anssi Hannula20608732013-02-03 17:55:45 +0200952 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200953 hdmi_manual_setup_channel_mapping(codec, pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300954 channels, map, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200955 } else {
956 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
957 hdmi_setup_fake_chmap(map, ca);
958 }
Anssi Hannula980b2492013-10-05 02:25:44 +0300959
960 hdmi_debug_channel_mapping(codec, pin_nid);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200961}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800962
Anssi Hannula307229d2013-10-24 21:10:34 +0300963static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
964 int asp_slot, int channel)
965{
966 return snd_hda_codec_write(codec, pin_nid, 0,
967 AC_VERB_SET_HDMI_CHAN_SLOT,
968 (channel << 4) | asp_slot);
969}
970
971static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
972 int asp_slot)
973{
974 return (snd_hda_codec_read(codec, pin_nid, 0,
975 AC_VERB_GET_HDMI_CHAN_SLOT,
976 asp_slot) & 0xf0) >> 4;
977}
978
Wu Fengguang079d88c2010-03-08 10:44:23 +0800979/*
980 * Audio InfoFrame routines
981 */
982
983/*
984 * Enable Audio InfoFrame Transmission
985 */
986static void hdmi_start_infoframe_trans(struct hda_codec *codec,
987 hda_nid_t pin_nid)
988{
989 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
990 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
991 AC_DIPXMIT_BEST);
992}
993
994/*
995 * Disable Audio InfoFrame Transmission
996 */
997static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
998 hda_nid_t pin_nid)
999{
1000 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1001 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1002 AC_DIPXMIT_DISABLE);
1003}
1004
1005static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1006{
1007#ifdef CONFIG_SND_DEBUG_VERBOSE
1008 int i;
1009 int size;
1010
1011 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001012 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001013
1014 for (i = 0; i < 8; i++) {
1015 size = snd_hda_codec_read(codec, pin_nid, 0,
1016 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001017 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001018 }
1019#endif
1020}
1021
1022static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1023{
1024#ifdef BE_PARANOID
1025 int i, j;
1026 int size;
1027 int pi, bi;
1028 for (i = 0; i < 8; i++) {
1029 size = snd_hda_codec_read(codec, pin_nid, 0,
1030 AC_VERB_GET_HDMI_DIP_SIZE, i);
1031 if (size == 0)
1032 continue;
1033
1034 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1035 for (j = 1; j < 1000; j++) {
1036 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1037 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1038 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001039 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001040 bi, pi, i);
1041 if (bi == 0) /* byte index wrapped around */
1042 break;
1043 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001044 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001045 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1046 i, size, j);
1047 }
1048#endif
1049}
1050
Wu Fengguang53d7d692010-09-21 14:25:49 +08001051static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001052{
Wu Fengguang53d7d692010-09-21 14:25:49 +08001053 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001054 u8 sum = 0;
1055 int i;
1056
Wu Fengguang53d7d692010-09-21 14:25:49 +08001057 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001058
Wu Fengguang53d7d692010-09-21 14:25:49 +08001059 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001060 sum += bytes[i];
1061
Wu Fengguang53d7d692010-09-21 14:25:49 +08001062 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001063}
1064
1065static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1066 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001067 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001068{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001069 int i;
1070
1071 hdmi_debug_dip_size(codec, pin_nid);
1072 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1073
Wu Fengguang079d88c2010-03-08 10:44:23 +08001074 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001075 for (i = 0; i < size; i++)
1076 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001077}
1078
1079static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001080 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001081{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001082 u8 val;
1083 int i;
1084
1085 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1086 != AC_DIPXMIT_BEST)
1087 return false;
1088
1089 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001090 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001091 val = snd_hda_codec_read(codec, pin_nid, 0,
1092 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001093 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +08001094 return false;
1095 }
1096
1097 return true;
1098}
1099
Anssi Hannula307229d2013-10-24 21:10:34 +03001100static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1101 hda_nid_t pin_nid,
1102 int ca, int active_channels,
1103 int conn_type)
1104{
1105 union audio_infoframe ai;
1106
Mengdong Lincaaf5ef2014-03-11 17:12:52 -04001107 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +03001108 if (conn_type == 0) { /* HDMI */
1109 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1110
1111 hdmi_ai->type = 0x84;
1112 hdmi_ai->ver = 0x01;
1113 hdmi_ai->len = 0x0a;
1114 hdmi_ai->CC02_CT47 = active_channels - 1;
1115 hdmi_ai->CA = ca;
1116 hdmi_checksum_audio_infoframe(hdmi_ai);
1117 } else if (conn_type == 1) { /* DisplayPort */
1118 struct dp_audio_infoframe *dp_ai = &ai.dp;
1119
1120 dp_ai->type = 0x84;
1121 dp_ai->len = 0x1b;
1122 dp_ai->ver = 0x11 << 2;
1123 dp_ai->CC02_CT47 = active_channels - 1;
1124 dp_ai->CA = ca;
1125 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001126 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001127 pin_nid);
1128 return;
1129 }
1130
1131 /*
1132 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1133 * sizeof(*dp_ai) to avoid partial match/update problems when
1134 * the user switches between HDMI/DP monitors.
1135 */
1136 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1137 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001138 codec_dbg(codec,
1139 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001140 pin_nid,
1141 active_channels, ca);
1142 hdmi_stop_infoframe_trans(codec, pin_nid);
1143 hdmi_fill_audio_infoframe(codec, pin_nid,
1144 ai.bytes, sizeof(ai));
1145 hdmi_start_infoframe_trans(codec, pin_nid);
1146 }
1147}
1148
Takashi Iwaib0540872013-09-02 12:33:02 +02001149static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1150 struct hdmi_spec_per_pin *per_pin,
1151 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001152{
Anssi Hannula307229d2013-10-24 21:10:34 +03001153 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001154 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +02001155 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001156 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -06001157 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001158 int ca, ordered_ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001159
Takashi Iwaib0540872013-09-02 12:33:02 +02001160 if (!channels)
1161 return;
1162
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001163 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001164 snd_hda_codec_write(codec, pin_nid, 0,
1165 AC_VERB_SET_AMP_GAIN_MUTE,
1166 AMP_OUT_UNMUTE);
1167
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001168 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001169
Takashi Iwaid45e6882012-07-31 11:36:00 +02001170 if (!non_pcm && per_pin->chmap_set)
1171 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1172 else
Takashi Iwai79514d42014-06-06 18:04:34 +02001173 ca = hdmi_channel_allocation(codec, eld, channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001174 if (ca < 0)
1175 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001176
Anssi Hannula1df5a062013-10-05 02:25:40 +03001177 ordered_ca = get_channel_allocation_order(ca);
1178 active_channels = channel_allocations[ordered_ca].channels;
1179
1180 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1181
Stephen Warren384a48d2011-06-01 11:14:21 -06001182 /*
Anssi Hannula39edac72013-10-07 19:24:52 +03001183 * always configure channel mapping, it may have been changed by the
1184 * user in the meantime
1185 */
1186 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1187 channels, per_pin->chmap,
1188 per_pin->chmap_set);
1189
Anssi Hannula307229d2013-10-24 21:10:34 +03001190 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1191 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +08001192
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001193 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001194}
1195
Wu Fengguang079d88c2010-03-08 10:44:23 +08001196/*
1197 * Unsolicited events
1198 */
1199
Takashi Iwaiefe47102013-11-07 13:38:23 +01001200static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001201
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001202static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001203{
1204 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001205 int pin_idx = pin_nid_to_pin_index(codec, nid);
1206
David Henningsson20ce9022013-12-04 10:19:41 +08001207 if (pin_idx < 0)
1208 return;
David Henningsson20ce9022013-12-04 10:19:41 +08001209 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1210 snd_hda_jack_report_sync(codec);
1211}
1212
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001213static void jack_callback(struct hda_codec *codec,
1214 struct hda_jack_callback *jack)
1215{
1216 check_presence_and_report(codec, jack->tbl->nid);
1217}
1218
David Henningsson20ce9022013-12-04 10:19:41 +08001219static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1220{
Takashi Iwai3a938972011-10-28 01:16:55 +02001221 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001222 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001223 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001224
1225 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1226 if (!jack)
1227 return;
Takashi Iwai3a938972011-10-28 01:16:55 +02001228 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001229
Takashi Iwai4e76a882014-02-25 12:21:03 +01001230 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001231 "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 +08001232 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001233 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001234
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001235 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001236}
1237
1238static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1239{
1240 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1241 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1242 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1243 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1244
Takashi Iwai4e76a882014-02-25 12:21:03 +01001245 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001246 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001247 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001248 tag,
1249 subtag,
1250 cp_state,
1251 cp_ready);
1252
1253 /* TODO */
1254 if (cp_state)
1255 ;
1256 if (cp_ready)
1257 ;
1258}
1259
1260
1261static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1262{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001263 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1264 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1265
Takashi Iwai3a938972011-10-28 01:16:55 +02001266 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001267 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001268 return;
1269 }
1270
1271 if (subtag == 0)
1272 hdmi_intrinsic_event(codec, res);
1273 else
1274 hdmi_non_intrinsic_event(codec, res);
1275}
1276
Mengdong Lin58f7d282013-09-04 16:37:12 -04001277static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001278 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001279{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001280 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001281
Wang Xingchao53b434f2013-06-18 10:41:53 +08001282 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1283 * thus pins could only choose converter 0 for use. Make sure the
1284 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001285 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001286 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1287
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001288 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001289 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1290 AC_PWRST_D0);
1291 msleep(40);
1292 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1293 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001294 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 +02001295 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001296}
1297
Wu Fengguang079d88c2010-03-08 10:44:23 +08001298/*
1299 * Callbacks
1300 */
1301
Takashi Iwai92f10b32010-08-03 14:21:00 +02001302/* HBR should be Non-PCM, 8 channels */
1303#define is_hbr_format(format) \
1304 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1305
Anssi Hannula307229d2013-10-24 21:10:34 +03001306static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1307 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001308{
Anssi Hannula307229d2013-10-24 21:10:34 +03001309 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +02001310
Stephen Warren384a48d2011-06-01 11:14:21 -06001311 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1312 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001313 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1314
Anssi Hannula13122e62013-11-10 20:56:10 +02001315 if (pinctl < 0)
1316 return hbr ? -EINVAL : 0;
1317
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001318 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +03001319 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001320 new_pinctl |= AC_PINCTL_EPT_HBR;
1321 else
1322 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1323
Takashi Iwai4e76a882014-02-25 12:21:03 +01001324 codec_dbg(codec,
1325 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001326 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001327 pinctl == new_pinctl ? "" : "new-",
1328 new_pinctl);
1329
1330 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001331 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001332 AC_VERB_SET_PIN_WIDGET_CONTROL,
1333 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +03001334 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001335 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03001336
1337 return 0;
1338}
1339
1340static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1341 hda_nid_t pin_nid, u32 stream_tag, int format)
1342{
1343 struct hdmi_spec *spec = codec->spec;
1344 int err;
1345
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001346 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +03001347 haswell_verify_D0(codec, cvt_nid, pin_nid);
1348
1349 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1350
1351 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001352 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +03001353 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001354 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001355
Stephen Warren384a48d2011-06-01 11:14:21 -06001356 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001357 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001358}
1359
Libin Yang42b29872015-12-16 13:42:42 +08001360/* Try to find an available converter
1361 * If pin_idx is less then zero, just try to find an available converter.
1362 * Otherwise, try to find an available converter and get the cvt mux index
1363 * of the pin.
1364 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001365static int hdmi_choose_cvt(struct hda_codec *codec,
1366 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001367{
1368 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001369 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001370 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001371 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001372
Libin Yang42b29872015-12-16 13:42:42 +08001373 /* pin_idx < 0 means no pin will be bound to the converter */
1374 if (pin_idx < 0)
1375 per_pin = NULL;
1376 else
1377 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001378
Stephen Warren384a48d2011-06-01 11:14:21 -06001379 /* Dynamically assign converter to stream */
1380 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001381 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001382
1383 /* Must not already be assigned */
1384 if (per_cvt->assigned)
1385 continue;
Libin Yang42b29872015-12-16 13:42:42 +08001386 if (per_pin == NULL)
1387 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06001388 /* Must be in pin's mux's list of converters */
1389 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1390 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1391 break;
1392 /* Not in mux list */
1393 if (mux_idx == per_pin->num_mux_nids)
1394 continue;
1395 break;
1396 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001397
Stephen Warren384a48d2011-06-01 11:14:21 -06001398 /* No free converters */
1399 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +08001400 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -06001401
Libin Yang42b29872015-12-16 13:42:42 +08001402 if (per_pin != NULL)
1403 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +08001404
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001405 if (cvt_id)
1406 *cvt_id = cvt_idx;
1407 if (mux_id)
1408 *mux_id = mux_idx;
1409
1410 return 0;
1411}
1412
Mengdong Lin2df67422014-03-20 13:01:06 +08001413/* Assure the pin select the right convetor */
1414static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1415 struct hdmi_spec_per_pin *per_pin)
1416{
1417 hda_nid_t pin_nid = per_pin->pin_nid;
1418 int mux_idx, curr;
1419
1420 mux_idx = per_pin->mux_idx;
1421 curr = snd_hda_codec_read(codec, pin_nid, 0,
1422 AC_VERB_GET_CONNECT_SEL, 0);
1423 if (curr != mux_idx)
1424 snd_hda_codec_write_cache(codec, pin_nid, 0,
1425 AC_VERB_SET_CONNECT_SEL,
1426 mux_idx);
1427}
1428
Libin Yang42b29872015-12-16 13:42:42 +08001429/* get the mux index for the converter of the pins
1430 * converter's mux index is the same for all pins on Intel platform
1431 */
1432static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1433 hda_nid_t cvt_nid)
1434{
1435 int i;
1436
1437 for (i = 0; i < spec->num_cvts; i++)
1438 if (spec->cvt_nids[i] == cvt_nid)
1439 return i;
1440 return -EINVAL;
1441}
1442
Mengdong Lin300016b2013-11-04 01:13:13 -05001443/* Intel HDMI workaround to fix audio routing issue:
1444 * For some Intel display codecs, pins share the same connection list.
1445 * So a conveter can be selected by multiple pins and playback on any of these
1446 * pins will generate sound on the external display, because audio flows from
1447 * the same converter to the display pipeline. Also muting one pin may make
1448 * other pins have no sound output.
1449 * So this function assures that an assigned converter for a pin is not selected
1450 * by any other pins.
1451 */
1452static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001453 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001454{
1455 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001456 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001457 int cvt_idx, curr;
1458 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001459
Mengdong Linf82d7d12013-09-21 20:34:45 -04001460 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +01001461 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -04001462 unsigned int wid_caps = get_wcaps(codec, nid);
1463 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001464
Mengdong Linf82d7d12013-09-21 20:34:45 -04001465 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001466 continue;
1467
Mengdong Linf82d7d12013-09-21 20:34:45 -04001468 if (nid == pin_nid)
1469 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001470
Mengdong Linf82d7d12013-09-21 20:34:45 -04001471 curr = snd_hda_codec_read(codec, nid, 0,
1472 AC_VERB_GET_CONNECT_SEL, 0);
1473 if (curr != mux_idx)
1474 continue;
1475
1476 /* choose an unassigned converter. The conveters in the
1477 * connection list are in the same order as in the codec.
1478 */
1479 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1480 per_cvt = get_cvt(spec, cvt_idx);
1481 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001482 codec_dbg(codec,
1483 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001484 cvt_idx, nid);
1485 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001486 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001487 cvt_idx);
1488 break;
1489 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001490 }
1491 }
1492}
1493
Libin Yang42b29872015-12-16 13:42:42 +08001494/* A wrapper of intel_not_share_asigned_cvt() */
1495static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1496 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1497{
1498 int mux_idx;
1499 struct hdmi_spec *spec = codec->spec;
1500
1501 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1502 return;
1503
1504 /* On Intel platform, the mapping of converter nid to
1505 * mux index of the pins are always the same.
1506 * The pin nid may be 0, this means all pins will not
1507 * share the converter.
1508 */
1509 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1510 if (mux_idx >= 0)
1511 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1512}
1513
1514/* called in hdmi_pcm_open when no pin is assigned to the PCM
1515 * in dyn_pcm_assign mode.
1516 */
1517static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1518 struct hda_codec *codec,
1519 struct snd_pcm_substream *substream)
1520{
1521 struct hdmi_spec *spec = codec->spec;
1522 struct snd_pcm_runtime *runtime = substream->runtime;
1523 int cvt_idx;
1524 struct hdmi_spec_per_cvt *per_cvt = NULL;
1525 int err;
1526
1527 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1528 if (err)
1529 return err;
1530
1531 per_cvt = get_cvt(spec, cvt_idx);
1532 per_cvt->assigned = 1;
1533 hinfo->nid = per_cvt->cvt_nid;
1534
1535 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1536
1537 /* todo: setup spdif ctls assign */
1538
1539 /* Initially set the converter's capabilities */
1540 hinfo->channels_min = per_cvt->channels_min;
1541 hinfo->channels_max = per_cvt->channels_max;
1542 hinfo->rates = per_cvt->rates;
1543 hinfo->formats = per_cvt->formats;
1544 hinfo->maxbps = per_cvt->maxbps;
1545
1546 /* Store the updated parameters */
1547 runtime->hw.channels_min = hinfo->channels_min;
1548 runtime->hw.channels_max = hinfo->channels_max;
1549 runtime->hw.formats = hinfo->formats;
1550 runtime->hw.rates = hinfo->rates;
1551
1552 snd_pcm_hw_constraint_step(substream->runtime, 0,
1553 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1554 return 0;
1555}
1556
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001557/*
1558 * HDA PCM callbacks
1559 */
1560static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1561 struct hda_codec *codec,
1562 struct snd_pcm_substream *substream)
1563{
1564 struct hdmi_spec *spec = codec->spec;
1565 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001566 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001567 struct hdmi_spec_per_pin *per_pin;
1568 struct hdmi_eld *eld;
1569 struct hdmi_spec_per_cvt *per_cvt = NULL;
1570 int err;
1571
1572 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001573 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1574 if (pcm_idx < 0)
1575 return -EINVAL;
1576
Libin Yang42b29872015-12-16 13:42:42 +08001577 mutex_lock(&spec->pcm_lock);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001578 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001579 if (!spec->dyn_pcm_assign) {
1580 if (snd_BUG_ON(pin_idx < 0)) {
1581 mutex_unlock(&spec->pcm_lock);
1582 return -EINVAL;
1583 }
1584 } else {
1585 /* no pin is assigned to the PCM
1586 * PA need pcm open successfully when probe
1587 */
1588 if (pin_idx < 0) {
1589 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1590 mutex_unlock(&spec->pcm_lock);
1591 return err;
1592 }
1593 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001594
1595 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001596 if (err < 0) {
1597 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001598 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001599 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001600
1601 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001602 /* Claim converter */
1603 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001604
1605
1606 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001607 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001608 hinfo->nid = per_cvt->cvt_nid;
1609
Takashi Iwaibddee962013-06-18 16:14:22 +02001610 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001611 AC_VERB_SET_CONNECT_SEL,
1612 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001613
1614 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001615 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001616 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001617
Libin Yang2bf3c852015-12-16 13:42:43 +08001618 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001619
Stephen Warren2def8172011-06-01 11:14:20 -06001620 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001621 hinfo->channels_min = per_cvt->channels_min;
1622 hinfo->channels_max = per_cvt->channels_max;
1623 hinfo->rates = per_cvt->rates;
1624 hinfo->formats = per_cvt->formats;
1625 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001626
Libin Yang42b29872015-12-16 13:42:42 +08001627 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001628 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001629 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001630 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001631 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001632 !hinfo->rates || !hinfo->formats) {
1633 per_cvt->assigned = 0;
1634 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001635 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001636 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001637 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001638 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001639 }
Stephen Warren2def8172011-06-01 11:14:20 -06001640
Libin Yang42b29872015-12-16 13:42:42 +08001641 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001642 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001643 runtime->hw.channels_min = hinfo->channels_min;
1644 runtime->hw.channels_max = hinfo->channels_max;
1645 runtime->hw.formats = hinfo->formats;
1646 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001647
1648 snd_pcm_hw_constraint_step(substream->runtime, 0,
1649 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001650 return 0;
1651}
1652
1653/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001654 * HDA/HDMI auto parsing
1655 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001656static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001657{
1658 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001659 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001660 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001661
1662 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001663 codec_warn(codec,
1664 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001665 pin_nid, get_wcaps(codec, pin_nid));
1666 return -EINVAL;
1667 }
1668
Stephen Warren384a48d2011-06-01 11:14:21 -06001669 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1670 per_pin->mux_nids,
1671 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001672
1673 return 0;
1674}
1675
Takashi Iwaie90247f2015-11-13 09:12:12 +01001676/* update per_pin ELD from the given new ELD;
1677 * setup info frame and notification accordingly
1678 */
1679static void update_eld(struct hda_codec *codec,
1680 struct hdmi_spec_per_pin *per_pin,
1681 struct hdmi_eld *eld)
1682{
1683 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1684 bool old_eld_valid = pin_eld->eld_valid;
1685 bool eld_changed;
1686
1687 if (eld->eld_valid)
1688 snd_hdmi_show_eld(codec, &eld->info);
1689
1690 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1691 if (eld->eld_valid && pin_eld->eld_valid)
1692 if (pin_eld->eld_size != eld->eld_size ||
1693 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1694 eld->eld_size) != 0)
1695 eld_changed = true;
1696
1697 pin_eld->eld_valid = eld->eld_valid;
1698 pin_eld->eld_size = eld->eld_size;
1699 if (eld->eld_valid)
1700 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1701 pin_eld->info = eld->info;
1702
1703 /*
1704 * Re-setup pin and infoframe. This is needed e.g. when
1705 * - sink is first plugged-in
1706 * - transcoder can change during stream playback on Haswell
1707 * and this can make HW reset converter selection on a pin.
1708 */
1709 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1710 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1711 intel_verify_pin_cvt_connect(codec, per_pin);
1712 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1713 per_pin->mux_idx);
1714 }
1715
1716 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1717 }
1718
1719 if (eld_changed)
1720 snd_ctl_notify(codec->card,
1721 SNDRV_CTL_EVENT_MASK_VALUE |
1722 SNDRV_CTL_EVENT_MASK_INFO,
1723 &per_pin->eld_ctl->id);
1724}
1725
Takashi Iwai788d4412015-11-12 15:36:13 +01001726/* update ELD and jack state via HD-audio verbs */
1727static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1728 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001729{
David Henningsson464837a2013-11-07 13:38:25 +01001730 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001731 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001732 struct hdmi_spec *spec = codec->spec;
1733 struct hdmi_eld *eld = &spec->temp_eld;
1734 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001735 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001736 /*
1737 * Always execute a GetPinSense verb here, even when called from
1738 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1739 * response's PD bit is not the real PD value, but indicates that
1740 * the real PD value changed. An older version of the HD-audio
1741 * specification worked this way. Hence, we just ignore the data in
1742 * the unsolicited response to avoid custom WARs.
1743 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001744 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001745 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001746 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001747
Takashi Iwai664c7152015-04-08 11:43:14 +02001748 snd_hda_power_up_pm(codec);
David Henningssonda4a7a32013-12-18 10:46:04 +01001749 present = snd_hda_pin_sense(codec, pin_nid);
1750
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001751 mutex_lock(&per_pin->lock);
David Henningsson4bd038f2013-02-19 16:11:25 +01001752 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1753 if (pin_eld->monitor_present)
1754 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1755 else
1756 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001757
Takashi Iwai4e76a882014-02-25 12:21:03 +01001758 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001759 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001760 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001761
David Henningsson4bd038f2013-02-19 16:11:25 +01001762 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001763 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001764 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001765 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001766 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001767 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001768 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001769 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001770 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001771 if (!eld->eld_valid && repoll)
1772 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001773 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001774
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001775 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001776 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1777 else
1778 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001779
Takashi Iwaiaff747eb2013-11-07 16:39:37 +01001780 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001781
1782 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1783 if (jack)
1784 jack->block_report = !ret;
1785
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001786 mutex_unlock(&per_pin->lock);
Takashi Iwai664c7152015-04-08 11:43:14 +02001787 snd_hda_power_down_pm(codec);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001788 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001789}
1790
Takashi Iwai788d4412015-11-12 15:36:13 +01001791/* update ELD and jack state via audio component */
1792static void sync_eld_via_acomp(struct hda_codec *codec,
1793 struct hdmi_spec_per_pin *per_pin)
1794{
Takashi Iwai788d4412015-11-12 15:36:13 +01001795 struct hdmi_spec *spec = codec->spec;
1796 struct hdmi_eld *eld = &spec->temp_eld;
1797 int size;
1798
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001799 mutex_lock(&per_pin->lock);
1800 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1801 &eld->monitor_present, eld->eld_buffer,
1802 ELD_MAX_SIZE);
1803 if (size < 0)
1804 goto unlock;
1805 if (size > 0) {
1806 size = min(size, ELD_MAX_SIZE);
1807 if (snd_hdmi_parse_eld(codec, &eld->info,
1808 eld->eld_buffer, size) < 0)
1809 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001810 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001811
1812 if (size > 0) {
1813 eld->eld_valid = true;
1814 eld->eld_size = size;
1815 } else {
1816 eld->eld_valid = false;
1817 eld->eld_size = 0;
1818 }
1819
1820 update_eld(codec, per_pin, eld);
1821 snd_jack_report(per_pin->acomp_jack,
1822 eld->monitor_present ? SND_JACK_AVOUT : 0);
1823 unlock:
1824 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001825}
1826
1827static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1828{
1829 struct hda_codec *codec = per_pin->codec;
1830
1831 if (codec_has_acomp(codec)) {
1832 sync_eld_via_acomp(codec, per_pin);
1833 return false; /* don't call snd_hda_jack_report_sync() */
1834 } else {
1835 return hdmi_present_sense_via_verbs(per_pin, repoll);
1836 }
1837}
1838
Wu Fengguang744626d2011-11-16 16:29:47 +08001839static void hdmi_repoll_eld(struct work_struct *work)
1840{
1841 struct hdmi_spec_per_pin *per_pin =
1842 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1843
Wu Fengguangc6e84532011-11-18 16:59:32 -06001844 if (per_pin->repoll_count++ > 6)
1845 per_pin->repoll_count = 0;
1846
Takashi Iwaiefe47102013-11-07 13:38:23 +01001847 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1848 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08001849}
1850
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001851static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1852 hda_nid_t nid);
1853
Wu Fengguang079d88c2010-03-08 10:44:23 +08001854static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1855{
1856 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001857 unsigned int caps, config;
1858 int pin_idx;
1859 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001860 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001861
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001862 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001863 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1864 return 0;
1865
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001866 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001867 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1868 return 0;
1869
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001870 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001871 intel_haswell_fixup_connect_list(codec, pin_nid);
1872
Stephen Warren384a48d2011-06-01 11:14:21 -06001873 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001874 per_pin = snd_array_new(&spec->pins);
1875 if (!per_pin)
1876 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001877
1878 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001879 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001880
Stephen Warren384a48d2011-06-01 11:14:21 -06001881 err = hdmi_read_pin_conn(codec, pin_idx);
1882 if (err < 0)
1883 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001884
Wu Fengguang079d88c2010-03-08 10:44:23 +08001885 spec->num_pins++;
1886
Stephen Warren384a48d2011-06-01 11:14:21 -06001887 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001888}
1889
Stephen Warren384a48d2011-06-01 11:14:21 -06001890static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001891{
1892 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001893 struct hdmi_spec_per_cvt *per_cvt;
1894 unsigned int chans;
1895 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001896
Stephen Warren384a48d2011-06-01 11:14:21 -06001897 chans = get_wcaps(codec, cvt_nid);
1898 chans = get_wcaps_channels(chans);
1899
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001900 per_cvt = snd_array_new(&spec->cvts);
1901 if (!per_cvt)
1902 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001903
1904 per_cvt->cvt_nid = cvt_nid;
1905 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001906 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001907 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001908 if (chans > spec->channels_max)
1909 spec->channels_max = chans;
1910 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001911
1912 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1913 &per_cvt->rates,
1914 &per_cvt->formats,
1915 &per_cvt->maxbps);
1916 if (err < 0)
1917 return err;
1918
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001919 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1920 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1921 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001922
1923 return 0;
1924}
1925
1926static int hdmi_parse_codec(struct hda_codec *codec)
1927{
1928 hda_nid_t nid;
1929 int i, nodes;
1930
Takashi Iwai7639a062015-03-03 10:07:24 +01001931 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001932 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001933 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08001934 return -EINVAL;
1935 }
1936
1937 for (i = 0; i < nodes; i++, nid++) {
1938 unsigned int caps;
1939 unsigned int type;
1940
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001941 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001942 type = get_wcaps_type(caps);
1943
1944 if (!(caps & AC_WCAP_DIGITAL))
1945 continue;
1946
1947 switch (type) {
1948 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001949 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001950 break;
1951 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001952 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001953 break;
1954 }
1955 }
1956
Wu Fengguang079d88c2010-03-08 10:44:23 +08001957 return 0;
1958}
1959
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001960/*
1961 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001962static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1963{
1964 struct hda_spdif_out *spdif;
1965 bool non_pcm;
1966
1967 mutex_lock(&codec->spdif_mutex);
1968 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1969 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1970 mutex_unlock(&codec->spdif_mutex);
1971 return non_pcm;
1972}
1973
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001974/*
1975 * HDMI callbacks
1976 */
1977
1978static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1979 struct hda_codec *codec,
1980 unsigned int stream_tag,
1981 unsigned int format,
1982 struct snd_pcm_substream *substream)
1983{
Stephen Warren384a48d2011-06-01 11:14:21 -06001984 hda_nid_t cvt_nid = hinfo->nid;
1985 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08001986 int pin_idx;
1987 struct hdmi_spec_per_pin *per_pin;
1988 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08001989 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001990 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07001991 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08001992 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001993
Libin Yang42b29872015-12-16 13:42:42 +08001994 mutex_lock(&spec->pcm_lock);
1995 pin_idx = hinfo_to_pin_index(codec, hinfo);
1996 if (spec->dyn_pcm_assign && pin_idx < 0) {
1997 /* when dyn_pcm_assign and pcm is not bound to a pin
1998 * skip pin setup and return 0 to make audio playback
1999 * be ongoing
2000 */
2001 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2002 snd_hda_codec_setup_stream(codec, cvt_nid,
2003 stream_tag, 0, format);
2004 mutex_unlock(&spec->pcm_lock);
2005 return 0;
2006 }
2007
2008 if (snd_BUG_ON(pin_idx < 0)) {
2009 mutex_unlock(&spec->pcm_lock);
2010 return -EINVAL;
2011 }
2012 per_pin = get_pin(spec, pin_idx);
2013 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08002014 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08002015 /* Verify pin:cvt selections to avoid silent audio after S3.
2016 * After S3, the audio driver restores pin:cvt selections
2017 * but this can happen before gfx is ready and such selection
2018 * is overlooked by HW. Thus multiple pins can share a same
2019 * default convertor and mute control will affect each other,
2020 * which can cause a resumed audio playback become silent
2021 * after S3.
2022 */
2023 intel_verify_pin_cvt_connect(codec, per_pin);
2024 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2025 }
2026
Libin Yangddd621f2015-09-02 14:11:40 +08002027 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2028 /* Todo: add DP1.2 MST audio support later */
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002029 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08002030
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002031 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002032 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02002033 per_pin->channels = substream->runtime->channels;
2034 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002035
Takashi Iwaib0540872013-09-02 12:33:02 +02002036 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002037 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07002038 if (spec->dyn_pin_out) {
2039 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2040 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2041 snd_hda_codec_write(codec, pin_nid, 0,
2042 AC_VERB_SET_PIN_WIDGET_CONTROL,
2043 pinctl | PIN_OUT);
2044 }
2045
Libin Yang42b29872015-12-16 13:42:42 +08002046 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2047 stream_tag, format);
2048 mutex_unlock(&spec->pcm_lock);
2049 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002050}
2051
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002052static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2053 struct hda_codec *codec,
2054 struct snd_pcm_substream *substream)
2055{
2056 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2057 return 0;
2058}
2059
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002060static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2061 struct hda_codec *codec,
2062 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06002063{
2064 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08002065 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06002066 struct hdmi_spec_per_cvt *per_cvt;
2067 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07002068 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06002069
Stephen Warren384a48d2011-06-01 11:14:21 -06002070 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08002071 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2072 if (snd_BUG_ON(pcm_idx < 0))
2073 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002074 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002075 if (snd_BUG_ON(cvt_idx < 0))
2076 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002077 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002078
2079 snd_BUG_ON(!per_cvt->assigned);
2080 per_cvt->assigned = 0;
2081 hinfo->nid = 0;
2082
Libin Yang42b29872015-12-16 13:42:42 +08002083 mutex_lock(&spec->pcm_lock);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002084 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08002085 if (spec->dyn_pcm_assign && pin_idx < 0) {
2086 mutex_unlock(&spec->pcm_lock);
2087 return 0;
2088 }
2089
2090 if (snd_BUG_ON(pin_idx < 0)) {
2091 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002092 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08002093 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002094 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002095
Stephen Warren75fae112014-01-30 11:52:16 -07002096 if (spec->dyn_pin_out) {
2097 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2098 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2099 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2100 AC_VERB_SET_PIN_WIDGET_CONTROL,
2101 pinctl & ~PIN_OUT);
2102 }
2103
Libin Yang2bf3c852015-12-16 13:42:43 +08002104 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Takashi Iwaicbbaa602013-10-17 18:03:24 +02002105
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002106 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002107 per_pin->chmap_set = false;
2108 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002109
2110 per_pin->setup = false;
2111 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002112 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08002113 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002114 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002115
Stephen Warren384a48d2011-06-01 11:14:21 -06002116 return 0;
2117}
2118
2119static const struct hda_pcm_ops generic_ops = {
2120 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002121 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002122 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002123 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002124};
2125
Takashi Iwaid45e6882012-07-31 11:36:00 +02002126/*
2127 * ALSA API channel-map control callbacks
2128 */
2129static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2130 struct snd_ctl_elem_info *uinfo)
2131{
2132 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2133 struct hda_codec *codec = info->private_data;
2134 struct hdmi_spec *spec = codec->spec;
2135 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2136 uinfo->count = spec->channels_max;
2137 uinfo->value.integer.min = 0;
2138 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2139 return 0;
2140}
2141
Anssi Hannula307229d2013-10-24 21:10:34 +03002142static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2143 int channels)
2144{
2145 /* If the speaker allocation matches the channel count, it is OK.*/
2146 if (cap->channels != channels)
2147 return -1;
2148
2149 /* all channels are remappable freely */
2150 return SNDRV_CTL_TLVT_CHMAP_VAR;
2151}
2152
2153static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2154 unsigned int *chmap, int channels)
2155{
2156 int count = 0;
2157 int c;
2158
2159 for (c = 7; c >= 0; c--) {
2160 int spk = cap->speakers[c];
2161 if (!spk)
2162 continue;
2163
2164 chmap[count++] = spk_to_chmap(spk);
2165 }
2166
2167 WARN_ON(count != channels);
2168}
2169
Takashi Iwaid45e6882012-07-31 11:36:00 +02002170static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2171 unsigned int size, unsigned int __user *tlv)
2172{
2173 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2174 struct hda_codec *codec = info->private_data;
2175 struct hdmi_spec *spec = codec->spec;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002176 unsigned int __user *dst;
2177 int chs, count = 0;
2178
2179 if (size < 8)
2180 return -ENOMEM;
2181 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2182 return -EFAULT;
2183 size -= 8;
2184 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02002185 for (chs = 2; chs <= spec->channels_max; chs++) {
Anssi Hannula307229d2013-10-24 21:10:34 +03002186 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002187 struct cea_channel_speaker_allocation *cap;
2188 cap = channel_allocations;
2189 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2190 int chs_bytes = chs * 4;
Anssi Hannula307229d2013-10-24 21:10:34 +03002191 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2192 unsigned int tlv_chmap[8];
2193
2194 if (type < 0)
Takashi Iwaid45e6882012-07-31 11:36:00 +02002195 continue;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002196 if (size < 8)
2197 return -ENOMEM;
Anssi Hannula307229d2013-10-24 21:10:34 +03002198 if (put_user(type, dst) ||
Takashi Iwaid45e6882012-07-31 11:36:00 +02002199 put_user(chs_bytes, dst + 1))
2200 return -EFAULT;
2201 dst += 2;
2202 size -= 8;
2203 count += 8;
2204 if (size < chs_bytes)
2205 return -ENOMEM;
2206 size -= chs_bytes;
2207 count += chs_bytes;
Anssi Hannula307229d2013-10-24 21:10:34 +03002208 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2209 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2210 return -EFAULT;
2211 dst += chs;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002212 }
2213 }
2214 if (put_user(count, tlv + 1))
2215 return -EFAULT;
2216 return 0;
2217}
2218
2219static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol)
2221{
2222 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2223 struct hda_codec *codec = info->private_data;
2224 struct hdmi_spec *spec = codec->spec;
2225 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002226 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002227 int i;
2228
2229 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2230 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2231 return 0;
2232}
2233
2234static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2235 struct snd_ctl_elem_value *ucontrol)
2236{
2237 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2238 struct hda_codec *codec = info->private_data;
2239 struct hdmi_spec *spec = codec->spec;
2240 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002241 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002242 unsigned int ctl_idx;
2243 struct snd_pcm_substream *substream;
2244 unsigned char chmap[8];
Anssi Hannula307229d2013-10-24 21:10:34 +03002245 int i, err, ca, prepared = 0;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002246
2247 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2248 substream = snd_pcm_chmap_substream(info, ctl_idx);
2249 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01002250 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02002251 switch (substream->runtime->status->state) {
2252 case SNDRV_PCM_STATE_OPEN:
2253 case SNDRV_PCM_STATE_SETUP:
2254 break;
2255 case SNDRV_PCM_STATE_PREPARED:
2256 prepared = 1;
2257 break;
2258 default:
2259 return -EBUSY;
2260 }
2261 memset(chmap, 0, sizeof(chmap));
2262 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2263 chmap[i] = ucontrol->value.integer.value[i];
2264 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2265 return 0;
2266 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2267 if (ca < 0)
2268 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03002269 if (spec->ops.chmap_validate) {
2270 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2271 if (err)
2272 return err;
2273 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002274 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002275 per_pin->chmap_set = true;
2276 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2277 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02002278 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002279 mutex_unlock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002280
2281 return 0;
2282}
2283
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002284static int generic_hdmi_build_pcms(struct hda_codec *codec)
2285{
2286 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +08002287 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06002288 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002289
Stephen Warren384a48d2011-06-01 11:14:21 -06002290 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2291 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002292 struct hda_pcm_stream *pstr;
2293
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002294 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002295 if (!info)
2296 return -ENOMEM;
Libin Yang6590faa2015-12-16 13:42:41 +08002297 if (!spec->dyn_pcm_assign) {
2298 per_pin = get_pin(spec, pin_idx);
2299 per_pin->pcm = info;
2300 }
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002301 spec->pcm_rec[pin_idx] = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002302 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002303 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002304 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002305
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002306 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002307 pstr->substreams = 1;
2308 pstr->ops = generic_ops;
2309 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002310 }
2311
2312 return 0;
2313}
2314
Takashi Iwai788d4412015-11-12 15:36:13 +01002315static void free_acomp_jack_priv(struct snd_jack *jack)
2316{
2317 struct hdmi_spec_per_pin *per_pin = jack->private_data;
2318
2319 per_pin->acomp_jack = NULL;
2320}
2321
2322static int add_acomp_jack_kctl(struct hda_codec *codec,
2323 struct hdmi_spec_per_pin *per_pin,
2324 const char *name)
2325{
2326 struct snd_jack *jack;
2327 int err;
2328
2329 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2330 true, false);
2331 if (err < 0)
2332 return err;
2333 per_pin->acomp_jack = jack;
2334 jack->private_data = per_pin;
2335 jack->private_free = free_acomp_jack_priv;
2336 return 0;
2337}
2338
David Henningsson0b6c49b2011-08-23 16:56:03 +02002339static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
2340{
Takashi Iwai31ef2252011-12-01 17:41:36 +01002341 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02002342 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002343 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2344 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01002345 bool phantom_jack;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002346
Takashi Iwai31ef2252011-12-01 17:41:36 +01002347 if (pcmdev > 0)
2348 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Takashi Iwai788d4412015-11-12 15:36:13 +01002349 if (codec_has_acomp(codec))
2350 return add_acomp_jack_kctl(codec, per_pin, hdmi_str);
Takashi Iwai909cadc2015-11-12 11:52:13 +01002351 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2352 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01002353 strncat(hdmi_str, " Phantom",
2354 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002355
Takashi Iwai909cadc2015-11-12 11:52:13 +01002356 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2357 phantom_jack);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002358}
2359
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002360static int generic_hdmi_build_controls(struct hda_codec *codec)
2361{
2362 struct hdmi_spec *spec = codec->spec;
2363 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002364 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002365
Stephen Warren384a48d2011-06-01 11:14:21 -06002366 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002367 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002368
2369 err = generic_hdmi_build_jack(codec, pin_idx);
2370 if (err < 0)
2371 return err;
2372
Takashi Iwaidcda5802012-10-12 17:24:51 +02002373 err = snd_hda_create_dig_out_ctls(codec,
2374 per_pin->pin_nid,
2375 per_pin->mux_nids[0],
2376 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002377 if (err < 0)
2378 return err;
Libin Yang2bf3c852015-12-16 13:42:43 +08002379 /* pin number is the same with pcm number so far */
Stephen Warren384a48d2011-06-01 11:14:21 -06002380 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05002381
2382 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002383 err = hdmi_create_eld_ctl(codec, pin_idx,
2384 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05002385
2386 if (err < 0)
2387 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01002388
Takashi Iwai82b1d732011-12-20 15:53:07 +01002389 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002390 }
2391
Takashi Iwaid45e6882012-07-31 11:36:00 +02002392 /* add channel maps */
2393 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002394 struct hda_pcm *pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002395 struct snd_pcm_chmap *chmap;
2396 struct snd_kcontrol *kctl;
2397 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002398
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002399 pcm = spec->pcm_rec[pin_idx];
2400 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002401 break;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002402 err = snd_pcm_add_chmap_ctls(pcm->pcm,
Takashi Iwaid45e6882012-07-31 11:36:00 +02002403 SNDRV_PCM_STREAM_PLAYBACK,
2404 NULL, 0, pin_idx, &chmap);
2405 if (err < 0)
2406 return err;
2407 /* override handlers */
2408 chmap->private_data = codec;
2409 kctl = chmap->kctl;
2410 for (i = 0; i < kctl->count; i++)
2411 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2412 kctl->info = hdmi_chmap_ctl_info;
2413 kctl->get = hdmi_chmap_ctl_get;
2414 kctl->put = hdmi_chmap_ctl_put;
2415 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2416 }
2417
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002418 return 0;
2419}
2420
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002421static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2422{
2423 struct hdmi_spec *spec = codec->spec;
2424 int pin_idx;
2425
2426 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002427 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002428
2429 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002430 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002431 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002432 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002433 }
2434 return 0;
2435}
2436
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002437static int generic_hdmi_init(struct hda_codec *codec)
2438{
2439 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002440 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002441
Stephen Warren384a48d2011-06-01 11:14:21 -06002442 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002443 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002444 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002445
2446 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002447 if (!codec_has_acomp(codec))
2448 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2449 codec->jackpoll_interval > 0 ?
2450 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002451 }
2452 return 0;
2453}
2454
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002455static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2456{
2457 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2458 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002459}
2460
2461static void hdmi_array_free(struct hdmi_spec *spec)
2462{
2463 snd_array_free(&spec->pins);
2464 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002465}
2466
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002467static void generic_hdmi_free(struct hda_codec *codec)
2468{
2469 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002470 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002471
Takashi Iwai66032492015-12-01 16:49:35 +01002472 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002473 snd_hdac_i915_register_notifier(NULL);
2474
Stephen Warren384a48d2011-06-01 11:14:21 -06002475 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002476 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002477
Takashi Iwai2f35c632015-02-27 22:43:26 +01002478 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002479 eld_proc_free(per_pin);
Takashi Iwai788d4412015-11-12 15:36:13 +01002480 if (per_pin->acomp_jack)
2481 snd_device_free(codec->card, per_pin->acomp_jack);
Stephen Warren384a48d2011-06-01 11:14:21 -06002482 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002483
Takashi Iwai55913112015-12-10 13:03:29 +01002484 if (spec->i915_bound)
2485 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002486 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002487 kfree(spec);
2488}
2489
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002490#ifdef CONFIG_PM
2491static int generic_hdmi_resume(struct hda_codec *codec)
2492{
2493 struct hdmi_spec *spec = codec->spec;
2494 int pin_idx;
2495
Pierre Ossmana2833682014-06-18 21:48:09 +02002496 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002497 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002498
2499 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2500 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2501 hdmi_present_sense(per_pin, 1);
2502 }
2503 return 0;
2504}
2505#endif
2506
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002507static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002508 .init = generic_hdmi_init,
2509 .free = generic_hdmi_free,
2510 .build_pcms = generic_hdmi_build_pcms,
2511 .build_controls = generic_hdmi_build_controls,
2512 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002513#ifdef CONFIG_PM
2514 .resume = generic_hdmi_resume,
2515#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002516};
2517
Anssi Hannula307229d2013-10-24 21:10:34 +03002518static const struct hdmi_ops generic_standard_hdmi_ops = {
2519 .pin_get_eld = snd_hdmi_get_eld,
2520 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2521 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2522 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2523 .pin_hbr_setup = hdmi_pin_hbr_setup,
2524 .setup_stream = hdmi_setup_stream,
2525 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2526 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2527};
2528
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002529
2530static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2531 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002532{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002533 struct hdmi_spec *spec = codec->spec;
2534 hda_nid_t conns[4];
2535 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002536
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002537 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2538 if (nconns == spec->num_cvts &&
2539 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002540 return;
2541
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002542 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002543 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002544 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002545}
2546
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002547#define INTEL_VENDOR_NID 0x08
2548#define INTEL_GET_VENDOR_VERB 0xf81
2549#define INTEL_SET_VENDOR_VERB 0x781
2550#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2551#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2552
2553static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002554 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002555{
2556 unsigned int vendor_param;
2557
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002558 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2559 INTEL_GET_VENDOR_VERB, 0);
2560 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2561 return;
2562
2563 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2564 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2565 INTEL_SET_VENDOR_VERB, vendor_param);
2566 if (vendor_param == -1)
2567 return;
2568
Takashi Iwai17df3f52013-05-08 08:09:34 +02002569 if (update_tree)
2570 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002571}
2572
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002573static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2574{
2575 unsigned int vendor_param;
2576
2577 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2578 INTEL_GET_VENDOR_VERB, 0);
2579 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2580 return;
2581
2582 /* enable DP1.2 mode */
2583 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002584 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002585 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2586 INTEL_SET_VENDOR_VERB, vendor_param);
2587}
2588
Takashi Iwai17df3f52013-05-08 08:09:34 +02002589/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2590 * Otherwise you may get severe h/w communication errors.
2591 */
2592static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2593 unsigned int power_state)
2594{
2595 if (power_state == AC_PWRST_D0) {
2596 intel_haswell_enable_all_pins(codec, false);
2597 intel_haswell_fixup_enable_dp12(codec);
2598 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002599
Takashi Iwai17df3f52013-05-08 08:09:34 +02002600 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2601 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2602}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002603
David Henningssonf0675d42015-09-03 11:51:34 +02002604static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002605{
2606 struct hda_codec *codec = audio_ptr;
2607 int pin_nid = port + 0x04;
2608
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002609 /* skip notification during system suspend (but not in runtime PM);
2610 * the state will be updated at resume
2611 */
2612 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2613 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002614 /* ditto during suspend/resume process itself */
2615 if (atomic_read(&(codec)->core.in_pm))
2616 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002617
David Henningsson25adc132015-08-19 10:48:58 +02002618 check_presence_and_report(codec, pin_nid);
2619}
2620
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002621static int patch_generic_hdmi(struct hda_codec *codec)
2622{
2623 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002624
2625 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2626 if (spec == NULL)
2627 return -ENOMEM;
2628
Anssi Hannula307229d2013-10-24 21:10:34 +03002629 spec->ops = generic_standard_hdmi_ops;
Libin Yang42b29872015-12-16 13:42:42 +08002630 mutex_init(&spec->pcm_lock);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002631 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002632 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002633
Takashi Iwai55913112015-12-10 13:03:29 +01002634 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2635 if (!codec_has_acomp(codec) &&
2636 (codec->core.vendor_id >> 16) == 0x8086)
2637 if (!snd_hdac_i915_init(&codec->bus->core))
2638 spec->i915_bound = true;
2639
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002640 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002641 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002642 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002643 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002644
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002645 /* For Valleyview/Cherryview, only the display codec is in the display
2646 * power well and can use link_power ops to request/release the power.
2647 * For Haswell/Broadwell, the controller is also in the power well and
2648 * can cover the codec power request, and so need not set this flag.
2649 * For previous platforms, there is no such power well feature.
2650 */
Lu, Hanff9d8852015-11-19 23:25:13 +08002651 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2652 is_broxton(codec))
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002653 codec->core.link_power_control = 1;
2654
Takashi Iwai66032492015-12-01 16:49:35 +01002655 if (codec_has_acomp(codec)) {
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002656 codec->depop_delay = 0;
David Henningsson25adc132015-08-19 10:48:58 +02002657 spec->i915_audio_ops.audio_ptr = codec;
2658 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2659 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2660 }
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002661
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002662 if (hdmi_parse_codec(codec) < 0) {
Takashi Iwai55913112015-12-10 13:03:29 +01002663 if (spec->i915_bound)
2664 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002665 codec->spec = NULL;
2666 kfree(spec);
2667 return -EINVAL;
2668 }
2669 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002670 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002671 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002672 codec->dp_mst = true;
2673 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002674
Lu, Han2377c3c2015-06-09 16:50:38 +08002675 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2676 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2677 codec->auto_runtime_pm = 1;
2678
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002679 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002680
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002681 init_channel_allocations();
2682
2683 return 0;
2684}
2685
2686/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002687 * Shared non-generic implementations
2688 */
2689
2690static int simple_playback_build_pcms(struct hda_codec *codec)
2691{
2692 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002693 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002694 unsigned int chans;
2695 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002696 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002697
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002698 per_cvt = get_cvt(spec, 0);
2699 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002700 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002701
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002702 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002703 if (!info)
2704 return -ENOMEM;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002705 spec->pcm_rec[0] = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002706 info->pcm_type = HDA_PCM_TYPE_HDMI;
2707 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2708 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002709 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002710 if (pstr->channels_max <= 2 && chans && chans <= 16)
2711 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002712
2713 return 0;
2714}
2715
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002716/* unsolicited event for jack sensing */
2717static void simple_hdmi_unsol_event(struct hda_codec *codec,
2718 unsigned int res)
2719{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002720 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002721 snd_hda_jack_report_sync(codec);
2722}
2723
2724/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2725 * as long as spec->pins[] is set correctly
2726 */
2727#define simple_hdmi_build_jack generic_hdmi_build_jack
2728
Stephen Warren3aaf8982011-06-01 11:14:19 -06002729static int simple_playback_build_controls(struct hda_codec *codec)
2730{
2731 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002732 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002733 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002734
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002735 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002736 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2737 per_cvt->cvt_nid,
2738 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002739 if (err < 0)
2740 return err;
2741 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002742}
2743
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002744static int simple_playback_init(struct hda_codec *codec)
2745{
2746 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002747 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2748 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002749
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002750 snd_hda_codec_write(codec, pin, 0,
2751 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2752 /* some codecs require to unmute the pin */
2753 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2754 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2755 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002756 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002757 return 0;
2758}
2759
Stephen Warren3aaf8982011-06-01 11:14:19 -06002760static void simple_playback_free(struct hda_codec *codec)
2761{
2762 struct hdmi_spec *spec = codec->spec;
2763
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002764 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002765 kfree(spec);
2766}
2767
2768/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002769 * Nvidia specific implementations
2770 */
2771
2772#define Nv_VERB_SET_Channel_Allocation 0xF79
2773#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2774#define Nv_VERB_SET_Audio_Protection_On 0xF98
2775#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2776
2777#define nvhdmi_master_con_nid_7x 0x04
2778#define nvhdmi_master_pin_nid_7x 0x05
2779
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002780static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002781 /*front, rear, clfe, rear_surr */
2782 0x6, 0x8, 0xa, 0xc,
2783};
2784
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002785static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2786 /* set audio protect on */
2787 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2788 /* enable digital output on pin widget */
2789 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2790 {} /* terminator */
2791};
2792
2793static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002794 /* set audio protect on */
2795 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2796 /* enable digital output on pin widget */
2797 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2798 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2799 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2800 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2801 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2802 {} /* terminator */
2803};
2804
2805#ifdef LIMITED_RATE_FMT_SUPPORT
2806/* support only the safe format and rate */
2807#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2808#define SUPPORTED_MAXBPS 16
2809#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2810#else
2811/* support all rates and formats */
2812#define SUPPORTED_RATES \
2813 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2814 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2815 SNDRV_PCM_RATE_192000)
2816#define SUPPORTED_MAXBPS 24
2817#define SUPPORTED_FORMATS \
2818 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2819#endif
2820
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002821static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002822{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002823 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2824 return 0;
2825}
2826
2827static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2828{
2829 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002830 return 0;
2831}
2832
Nitin Daga393004b2011-01-10 21:49:31 +05302833static unsigned int channels_2_6_8[] = {
2834 2, 6, 8
2835};
2836
2837static unsigned int channels_2_8[] = {
2838 2, 8
2839};
2840
2841static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2842 .count = ARRAY_SIZE(channels_2_6_8),
2843 .list = channels_2_6_8,
2844 .mask = 0,
2845};
2846
2847static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2848 .count = ARRAY_SIZE(channels_2_8),
2849 .list = channels_2_8,
2850 .mask = 0,
2851};
2852
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002853static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2854 struct hda_codec *codec,
2855 struct snd_pcm_substream *substream)
2856{
2857 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302858 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2859
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002860 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05302861 case 0x10de0002:
2862 case 0x10de0003:
2863 case 0x10de0005:
2864 case 0x10de0006:
2865 hw_constraints_channels = &hw_constraints_2_8_channels;
2866 break;
2867 case 0x10de0007:
2868 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2869 break;
2870 default:
2871 break;
2872 }
2873
2874 if (hw_constraints_channels != NULL) {
2875 snd_pcm_hw_constraint_list(substream->runtime, 0,
2876 SNDRV_PCM_HW_PARAM_CHANNELS,
2877 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002878 } else {
2879 snd_pcm_hw_constraint_step(substream->runtime, 0,
2880 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302881 }
2882
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002883 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2884}
2885
2886static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2887 struct hda_codec *codec,
2888 struct snd_pcm_substream *substream)
2889{
2890 struct hdmi_spec *spec = codec->spec;
2891 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2892}
2893
2894static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2895 struct hda_codec *codec,
2896 unsigned int stream_tag,
2897 unsigned int format,
2898 struct snd_pcm_substream *substream)
2899{
2900 struct hdmi_spec *spec = codec->spec;
2901 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2902 stream_tag, format, substream);
2903}
2904
Takashi Iwaid0b12522012-06-15 14:34:42 +02002905static const struct hda_pcm_stream simple_pcm_playback = {
2906 .substreams = 1,
2907 .channels_min = 2,
2908 .channels_max = 2,
2909 .ops = {
2910 .open = simple_playback_pcm_open,
2911 .close = simple_playback_pcm_close,
2912 .prepare = simple_playback_pcm_prepare
2913 },
2914};
2915
2916static const struct hda_codec_ops simple_hdmi_patch_ops = {
2917 .build_controls = simple_playback_build_controls,
2918 .build_pcms = simple_playback_build_pcms,
2919 .init = simple_playback_init,
2920 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002921 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002922};
2923
2924static int patch_simple_hdmi(struct hda_codec *codec,
2925 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2926{
2927 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002928 struct hdmi_spec_per_cvt *per_cvt;
2929 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002930
2931 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2932 if (!spec)
2933 return -ENOMEM;
2934
2935 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002936 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002937
2938 spec->multiout.num_dacs = 0; /* no analog */
2939 spec->multiout.max_channels = 2;
2940 spec->multiout.dig_out_nid = cvt_nid;
2941 spec->num_cvts = 1;
2942 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002943 per_pin = snd_array_new(&spec->pins);
2944 per_cvt = snd_array_new(&spec->cvts);
2945 if (!per_pin || !per_cvt) {
2946 simple_playback_free(codec);
2947 return -ENOMEM;
2948 }
2949 per_cvt->cvt_nid = cvt_nid;
2950 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002951 spec->pcm_playback = simple_pcm_playback;
2952
2953 codec->patch_ops = simple_hdmi_patch_ops;
2954
2955 return 0;
2956}
2957
Aaron Plattner1f348522011-04-06 17:19:04 -07002958static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2959 int channels)
2960{
2961 unsigned int chanmask;
2962 int chan = channels ? (channels - 1) : 1;
2963
2964 switch (channels) {
2965 default:
2966 case 0:
2967 case 2:
2968 chanmask = 0x00;
2969 break;
2970 case 4:
2971 chanmask = 0x08;
2972 break;
2973 case 6:
2974 chanmask = 0x0b;
2975 break;
2976 case 8:
2977 chanmask = 0x13;
2978 break;
2979 }
2980
2981 /* Set the audio infoframe channel allocation and checksum fields. The
2982 * channel count is computed implicitly by the hardware. */
2983 snd_hda_codec_write(codec, 0x1, 0,
2984 Nv_VERB_SET_Channel_Allocation, chanmask);
2985
2986 snd_hda_codec_write(codec, 0x1, 0,
2987 Nv_VERB_SET_Info_Frame_Checksum,
2988 (0x71 - chan - chanmask));
2989}
2990
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002991static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2992 struct hda_codec *codec,
2993 struct snd_pcm_substream *substream)
2994{
2995 struct hdmi_spec *spec = codec->spec;
2996 int i;
2997
2998 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2999 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3000 for (i = 0; i < 4; i++) {
3001 /* set the stream id */
3002 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3003 AC_VERB_SET_CHANNEL_STREAMID, 0);
3004 /* set the stream format */
3005 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3006 AC_VERB_SET_STREAM_FORMAT, 0);
3007 }
3008
Aaron Plattner1f348522011-04-06 17:19:04 -07003009 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3010 * streams are disabled. */
3011 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3012
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003013 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3014}
3015
3016static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3017 struct hda_codec *codec,
3018 unsigned int stream_tag,
3019 unsigned int format,
3020 struct snd_pcm_substream *substream)
3021{
3022 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003023 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003024 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003025 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003026 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003027 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003028
3029 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003030 per_cvt = get_cvt(spec, 0);
3031 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003032
3033 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003034
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003035 dataDCC2 = 0x2;
3036
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003037 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003038 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003039 snd_hda_codec_write(codec,
3040 nvhdmi_master_con_nid_7x,
3041 0,
3042 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003043 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003044
3045 /* set the stream id */
3046 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3047 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3048
3049 /* set the stream format */
3050 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3051 AC_VERB_SET_STREAM_FORMAT, format);
3052
3053 /* turn on again (if needed) */
3054 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003055 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003056 snd_hda_codec_write(codec,
3057 nvhdmi_master_con_nid_7x,
3058 0,
3059 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003060 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003061 snd_hda_codec_write(codec,
3062 nvhdmi_master_con_nid_7x,
3063 0,
3064 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3065 }
3066
3067 for (i = 0; i < 4; i++) {
3068 if (chs == 2)
3069 channel_id = 0;
3070 else
3071 channel_id = i * 2;
3072
3073 /* turn off SPDIF once;
3074 *otherwise the IEC958 bits won't be updated
3075 */
3076 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003077 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003078 snd_hda_codec_write(codec,
3079 nvhdmi_con_nids_7x[i],
3080 0,
3081 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003082 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003083 /* set the stream id */
3084 snd_hda_codec_write(codec,
3085 nvhdmi_con_nids_7x[i],
3086 0,
3087 AC_VERB_SET_CHANNEL_STREAMID,
3088 (stream_tag << 4) | channel_id);
3089 /* set the stream format */
3090 snd_hda_codec_write(codec,
3091 nvhdmi_con_nids_7x[i],
3092 0,
3093 AC_VERB_SET_STREAM_FORMAT,
3094 format);
3095 /* turn on again (if needed) */
3096 /* enable and set the channel status audio/data flag */
3097 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003098 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003099 snd_hda_codec_write(codec,
3100 nvhdmi_con_nids_7x[i],
3101 0,
3102 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003103 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003104 snd_hda_codec_write(codec,
3105 nvhdmi_con_nids_7x[i],
3106 0,
3107 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3108 }
3109 }
3110
Aaron Plattner1f348522011-04-06 17:19:04 -07003111 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003112
3113 mutex_unlock(&codec->spdif_mutex);
3114 return 0;
3115}
3116
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003117static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003118 .substreams = 1,
3119 .channels_min = 2,
3120 .channels_max = 8,
3121 .nid = nvhdmi_master_con_nid_7x,
3122 .rates = SUPPORTED_RATES,
3123 .maxbps = SUPPORTED_MAXBPS,
3124 .formats = SUPPORTED_FORMATS,
3125 .ops = {
3126 .open = simple_playback_pcm_open,
3127 .close = nvhdmi_8ch_7x_pcm_close,
3128 .prepare = nvhdmi_8ch_7x_pcm_prepare
3129 },
3130};
3131
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003132static int patch_nvhdmi_2ch(struct hda_codec *codec)
3133{
3134 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003135 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3136 nvhdmi_master_pin_nid_7x);
3137 if (err < 0)
3138 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003139
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003140 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003141 /* override the PCM rates, etc, as the codec doesn't give full list */
3142 spec = codec->spec;
3143 spec->pcm_playback.rates = SUPPORTED_RATES;
3144 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3145 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003146 return 0;
3147}
3148
Takashi Iwai53775b02012-08-01 12:17:41 +02003149static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3150{
3151 struct hdmi_spec *spec = codec->spec;
3152 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003153 if (!err) {
3154 struct hda_pcm *info = get_pcm_rec(spec, 0);
3155 info->own_chmap = true;
3156 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003157 return err;
3158}
3159
3160static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3161{
3162 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003163 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003164 struct snd_pcm_chmap *chmap;
3165 int err;
3166
3167 err = simple_playback_build_controls(codec);
3168 if (err < 0)
3169 return err;
3170
3171 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003172 info = get_pcm_rec(spec, 0);
3173 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003174 SNDRV_PCM_STREAM_PLAYBACK,
3175 snd_pcm_alt_chmaps, 8, 0, &chmap);
3176 if (err < 0)
3177 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003178 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003179 case 0x10de0002:
3180 case 0x10de0003:
3181 case 0x10de0005:
3182 case 0x10de0006:
3183 chmap->channel_mask = (1U << 2) | (1U << 8);
3184 break;
3185 case 0x10de0007:
3186 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3187 }
3188 return 0;
3189}
3190
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003191static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3192{
3193 struct hdmi_spec *spec;
3194 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003195 if (err < 0)
3196 return err;
3197 spec = codec->spec;
3198 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003199 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003200 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003201 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3202 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003203
3204 /* Initialize the audio infoframe channel mask and checksum to something
3205 * valid */
3206 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3207
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003208 return 0;
3209}
3210
3211/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003212 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3213 * - 0x10de0015
3214 * - 0x10de0040
3215 */
3216static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3217 int channels)
3218{
3219 if (cap->ca_index == 0x00 && channels == 2)
3220 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3221
3222 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3223}
3224
3225static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3226{
3227 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3228 return -EINVAL;
3229
3230 return 0;
3231}
3232
3233static int patch_nvhdmi(struct hda_codec *codec)
3234{
3235 struct hdmi_spec *spec;
3236 int err;
3237
3238 err = patch_generic_hdmi(codec);
3239 if (err)
3240 return err;
3241
3242 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003243 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003244
3245 spec->ops.chmap_cea_alloc_validate_get_type =
3246 nvhdmi_chmap_cea_alloc_validate_get_type;
3247 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3248
3249 return 0;
3250}
3251
3252/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003253 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3254 * accessed using vendor-defined verbs. These registers can be used for
3255 * interoperability between the HDA and HDMI drivers.
3256 */
3257
3258/* Audio Function Group node */
3259#define NVIDIA_AFG_NID 0x01
3260
3261/*
3262 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3263 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3264 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3265 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3266 * additional bit (at position 30) to signal the validity of the format.
3267 *
3268 * | 31 | 30 | 29 16 | 15 0 |
3269 * +---------+-------+--------+--------+
3270 * | TRIGGER | VALID | UNUSED | FORMAT |
3271 * +-----------------------------------|
3272 *
3273 * Note that for the trigger bit to take effect it needs to change value
3274 * (i.e. it needs to be toggled).
3275 */
3276#define NVIDIA_GET_SCRATCH0 0xfa6
3277#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3278#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3279#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3280#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3281#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3282#define NVIDIA_SCRATCH_VALID (1 << 6)
3283
3284#define NVIDIA_GET_SCRATCH1 0xfab
3285#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3286#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3287#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3288#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3289
3290/*
3291 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3292 * the format is invalidated so that the HDMI codec can be disabled.
3293 */
3294static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3295{
3296 unsigned int value;
3297
3298 /* bits [31:30] contain the trigger and valid bits */
3299 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3300 NVIDIA_GET_SCRATCH0, 0);
3301 value = (value >> 24) & 0xff;
3302
3303 /* bits [15:0] are used to store the HDA format */
3304 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3305 NVIDIA_SET_SCRATCH0_BYTE0,
3306 (format >> 0) & 0xff);
3307 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3308 NVIDIA_SET_SCRATCH0_BYTE1,
3309 (format >> 8) & 0xff);
3310
3311 /* bits [16:24] are unused */
3312 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3313 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3314
3315 /*
3316 * Bit 30 signals that the data is valid and hence that HDMI audio can
3317 * be enabled.
3318 */
3319 if (format == 0)
3320 value &= ~NVIDIA_SCRATCH_VALID;
3321 else
3322 value |= NVIDIA_SCRATCH_VALID;
3323
3324 /*
3325 * Whenever the trigger bit is toggled, an interrupt is raised in the
3326 * HDMI codec. The HDMI driver will use that as trigger to update its
3327 * configuration.
3328 */
3329 value ^= NVIDIA_SCRATCH_TRIGGER;
3330
3331 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3332 NVIDIA_SET_SCRATCH0_BYTE3, value);
3333}
3334
3335static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3336 struct hda_codec *codec,
3337 unsigned int stream_tag,
3338 unsigned int format,
3339 struct snd_pcm_substream *substream)
3340{
3341 int err;
3342
3343 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3344 format, substream);
3345 if (err < 0)
3346 return err;
3347
3348 /* notify the HDMI codec of the format change */
3349 tegra_hdmi_set_format(codec, format);
3350
3351 return 0;
3352}
3353
3354static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3355 struct hda_codec *codec,
3356 struct snd_pcm_substream *substream)
3357{
3358 /* invalidate the format in the HDMI codec */
3359 tegra_hdmi_set_format(codec, 0);
3360
3361 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3362}
3363
3364static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3365{
3366 struct hdmi_spec *spec = codec->spec;
3367 unsigned int i;
3368
3369 for (i = 0; i < spec->num_pins; i++) {
3370 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3371
3372 if (pcm->pcm_type == type)
3373 return pcm;
3374 }
3375
3376 return NULL;
3377}
3378
3379static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3380{
3381 struct hda_pcm_stream *stream;
3382 struct hda_pcm *pcm;
3383 int err;
3384
3385 err = generic_hdmi_build_pcms(codec);
3386 if (err < 0)
3387 return err;
3388
3389 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3390 if (!pcm)
3391 return -ENODEV;
3392
3393 /*
3394 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3395 * codec about format changes.
3396 */
3397 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3398 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3399 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3400
3401 return 0;
3402}
3403
3404static int patch_tegra_hdmi(struct hda_codec *codec)
3405{
3406 int err;
3407
3408 err = patch_generic_hdmi(codec);
3409 if (err)
3410 return err;
3411
3412 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3413
3414 return 0;
3415}
3416
3417/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003418 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003419 */
3420
Anssi Hannula5a6135842013-10-24 21:10:35 +03003421#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003422 ((codec)->core.vendor_id == 0x1002aa01 && \
3423 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003424#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003425
Anssi Hannula5a6135842013-10-24 21:10:35 +03003426/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3427#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3428#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3429#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3430#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3431#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3432#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003433#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003434#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3435#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3436#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3437#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3438#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3439#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3440#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3441#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3442#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3443#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3444#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003445#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003446#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3447#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3448#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3449#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3450#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3451
Anssi Hannula84d69e72013-10-24 21:10:38 +03003452/* AMD specific HDA cvt verbs */
3453#define ATI_VERB_SET_RAMP_RATE 0x770
3454#define ATI_VERB_GET_RAMP_RATE 0xf70
3455
Anssi Hannula5a6135842013-10-24 21:10:35 +03003456#define ATI_OUT_ENABLE 0x1
3457
3458#define ATI_MULTICHANNEL_MODE_PAIRED 0
3459#define ATI_MULTICHANNEL_MODE_SINGLE 1
3460
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003461#define ATI_HBR_CAPABLE 0x01
3462#define ATI_HBR_ENABLE 0x10
3463
Anssi Hannula89250f82013-10-24 21:10:36 +03003464static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3465 unsigned char *buf, int *eld_size)
3466{
3467 /* call hda_eld.c ATI/AMD-specific function */
3468 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3469 is_amdhdmi_rev3_or_later(codec));
3470}
3471
Anssi Hannula5a6135842013-10-24 21:10:35 +03003472static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3473 int active_channels, int conn_type)
3474{
3475 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3476}
3477
3478static int atihdmi_paired_swap_fc_lfe(int pos)
3479{
3480 /*
3481 * ATI/AMD have automatic FC/LFE swap built-in
3482 * when in pairwise mapping mode.
3483 */
3484
3485 switch (pos) {
3486 /* see channel_allocations[].speakers[] */
3487 case 2: return 3;
3488 case 3: return 2;
3489 default: break;
3490 }
3491
3492 return pos;
3493}
3494
3495static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3496{
3497 struct cea_channel_speaker_allocation *cap;
3498 int i, j;
3499
3500 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3501
3502 cap = &channel_allocations[get_channel_allocation_order(ca)];
3503 for (i = 0; i < chs; ++i) {
3504 int mask = to_spk_mask(map[i]);
3505 bool ok = false;
3506 bool companion_ok = false;
3507
3508 if (!mask)
3509 continue;
3510
3511 for (j = 0 + i % 2; j < 8; j += 2) {
3512 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3513 if (cap->speakers[chan_idx] == mask) {
3514 /* channel is in a supported position */
3515 ok = true;
3516
3517 if (i % 2 == 0 && i + 1 < chs) {
3518 /* even channel, check the odd companion */
3519 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3520 int comp_mask_req = to_spk_mask(map[i+1]);
3521 int comp_mask_act = cap->speakers[comp_chan_idx];
3522
3523 if (comp_mask_req == comp_mask_act)
3524 companion_ok = true;
3525 else
3526 return -EINVAL;
3527 }
3528 break;
3529 }
3530 }
3531
3532 if (!ok)
3533 return -EINVAL;
3534
3535 if (companion_ok)
3536 i++; /* companion channel already checked */
3537 }
3538
3539 return 0;
3540}
3541
3542static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3543 int hdmi_slot, int stream_channel)
3544{
3545 int verb;
3546 int ati_channel_setup = 0;
3547
3548 if (hdmi_slot > 7)
3549 return -EINVAL;
3550
3551 if (!has_amd_full_remap_support(codec)) {
3552 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3553
3554 /* In case this is an odd slot but without stream channel, do not
3555 * disable the slot since the corresponding even slot could have a
3556 * channel. In case neither have a channel, the slot pair will be
3557 * disabled when this function is called for the even slot. */
3558 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3559 return 0;
3560
3561 hdmi_slot -= hdmi_slot % 2;
3562
3563 if (stream_channel != 0xf)
3564 stream_channel -= stream_channel % 2;
3565 }
3566
3567 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3568
3569 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3570
3571 if (stream_channel != 0xf)
3572 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3573
3574 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3575}
3576
3577static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3578 int asp_slot)
3579{
3580 bool was_odd = false;
3581 int ati_asp_slot = asp_slot;
3582 int verb;
3583 int ati_channel_setup;
3584
3585 if (asp_slot > 7)
3586 return -EINVAL;
3587
3588 if (!has_amd_full_remap_support(codec)) {
3589 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3590 if (ati_asp_slot % 2 != 0) {
3591 ati_asp_slot -= 1;
3592 was_odd = true;
3593 }
3594 }
3595
3596 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3597
3598 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3599
3600 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3601 return 0xf;
3602
3603 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3604}
3605
3606static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3607 int channels)
3608{
3609 int c;
3610
3611 /*
3612 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3613 * we need to take that into account (a single channel may take 2
3614 * channel slots if we need to carry a silent channel next to it).
3615 * On Rev3+ AMD codecs this function is not used.
3616 */
3617 int chanpairs = 0;
3618
3619 /* We only produce even-numbered channel count TLVs */
3620 if ((channels % 2) != 0)
3621 return -1;
3622
3623 for (c = 0; c < 7; c += 2) {
3624 if (cap->speakers[c] || cap->speakers[c+1])
3625 chanpairs++;
3626 }
3627
3628 if (chanpairs * 2 != channels)
3629 return -1;
3630
3631 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3632}
3633
3634static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3635 unsigned int *chmap, int channels)
3636{
3637 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3638 int count = 0;
3639 int c;
3640
3641 for (c = 7; c >= 0; c--) {
3642 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3643 int spk = cap->speakers[chan];
3644 if (!spk) {
3645 /* add N/A channel if the companion channel is occupied */
3646 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3647 chmap[count++] = SNDRV_CHMAP_NA;
3648
3649 continue;
3650 }
3651
3652 chmap[count++] = spk_to_chmap(spk);
3653 }
3654
3655 WARN_ON(count != channels);
3656}
3657
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003658static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3659 bool hbr)
3660{
3661 int hbr_ctl, hbr_ctl_new;
3662
3663 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003664 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003665 if (hbr)
3666 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3667 else
3668 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3669
Takashi Iwai4e76a882014-02-25 12:21:03 +01003670 codec_dbg(codec,
3671 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003672 pin_nid,
3673 hbr_ctl == hbr_ctl_new ? "" : "new-",
3674 hbr_ctl_new);
3675
3676 if (hbr_ctl != hbr_ctl_new)
3677 snd_hda_codec_write(codec, pin_nid, 0,
3678 ATI_VERB_SET_HBR_CONTROL,
3679 hbr_ctl_new);
3680
3681 } else if (hbr)
3682 return -EINVAL;
3683
3684 return 0;
3685}
3686
Anssi Hannula84d69e72013-10-24 21:10:38 +03003687static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3688 hda_nid_t pin_nid, u32 stream_tag, int format)
3689{
3690
3691 if (is_amdhdmi_rev3_or_later(codec)) {
3692 int ramp_rate = 180; /* default as per AMD spec */
3693 /* disable ramp-up/down for non-pcm as per AMD spec */
3694 if (format & AC_FMT_TYPE_NON_PCM)
3695 ramp_rate = 0;
3696
3697 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3698 }
3699
3700 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3701}
3702
3703
Anssi Hannula5a6135842013-10-24 21:10:35 +03003704static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003705{
3706 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003707 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003708
Anssi Hannula5a6135842013-10-24 21:10:35 +03003709 err = generic_hdmi_init(codec);
3710
3711 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003712 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003713
3714 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3715 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3716
3717 /* make sure downmix information in infoframe is zero */
3718 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3719
3720 /* enable channel-wise remap mode if supported */
3721 if (has_amd_full_remap_support(codec))
3722 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3723 ATI_VERB_SET_MULTICHANNEL_MODE,
3724 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003725 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003726
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003727 return 0;
3728}
3729
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003730static int patch_atihdmi(struct hda_codec *codec)
3731{
3732 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003733 struct hdmi_spec_per_cvt *per_cvt;
3734 int err, cvt_idx;
3735
3736 err = patch_generic_hdmi(codec);
3737
3738 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003739 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003740
3741 codec->patch_ops.init = atihdmi_init;
3742
Takashi Iwaid0b12522012-06-15 14:34:42 +02003743 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003744
Anssi Hannula89250f82013-10-24 21:10:36 +03003745 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003746 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3747 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3748 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003749 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003750 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003751
3752 if (!has_amd_full_remap_support(codec)) {
3753 /* override to ATI/AMD-specific versions with pairwise mapping */
3754 spec->ops.chmap_cea_alloc_validate_get_type =
3755 atihdmi_paired_chmap_cea_alloc_validate_get_type;
3756 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
3757 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
3758 }
3759
3760 /* ATI/AMD converters do not advertise all of their capabilities */
3761 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3762 per_cvt = get_cvt(spec, cvt_idx);
3763 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3764 per_cvt->rates |= SUPPORTED_RATES;
3765 per_cvt->formats |= SUPPORTED_FORMATS;
3766 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3767 }
3768
3769 spec->channels_max = max(spec->channels_max, 8u);
3770
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003771 return 0;
3772}
3773
Annie Liu3de5ff82012-06-08 19:18:42 +08003774/* VIA HDMI Implementation */
3775#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3776#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3777
Annie Liu3de5ff82012-06-08 19:18:42 +08003778static int patch_via_hdmi(struct hda_codec *codec)
3779{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003780 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003781}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003782
3783/*
3784 * patch entries
3785 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003786static const struct hda_device_id snd_hda_id_hdmi[] = {
3787HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3788HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3789HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3790HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3791HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3792HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3793HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3794HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3795HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3796HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3797HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3798HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3799HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3800HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3801HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3802HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3803HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3804HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3805HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3806HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3807HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3808HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3809HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01003810/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003811HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3812HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3813HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3814HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3815HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3816HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3817HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3818HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3819HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3820HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3821HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3822HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3823HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3824HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3825HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3826HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3827HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3828HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3829HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3830HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3831HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
3832HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3833HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3834HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3835HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3836HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3837HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
3838HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3839HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3840HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3841HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
3842HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
3843HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
3844HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
3845HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
3846HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
3847HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
Libin Yang91815d82016-01-14 14:09:00 +08003848HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003849HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
3850HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
3851HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
3852HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003853/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003854HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003855{} /* terminator */
3856};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003857MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003858
3859MODULE_LICENSE("GPL");
3860MODULE_DESCRIPTION("HDMI HD-audio codec");
3861MODULE_ALIAS("snd-hda-codec-intelhdmi");
3862MODULE_ALIAS("snd-hda-codec-nvhdmi");
3863MODULE_ALIAS("snd-hda-codec-atihdmi");
3864
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003865static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003866 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003867};
3868
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003869module_hda_codec_driver(hdmi_driver);