blob: ba442d24257aa9a8e24b3999c6baa63f9e2c990e [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>
Wu Fengguang079d88c2010-03-08 10:44:23 +08009 *
10 * Authors:
11 * Wu Fengguang <wfg@linux.intel.com>
12 *
13 * Maintained by:
14 * Wu Fengguang <wfg@linux.intel.com>
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
Takashi Iwai84eb01b2010-09-07 12:27:25 +020031#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040034#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020035#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020036#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080037#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020038#include <sound/tlv.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020039#include "hda_codec.h"
40#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020041#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020042
Takashi Iwai0ebaa242011-01-11 18:11:04 +010043static bool static_hdmi_pcm;
44module_param(static_hdmi_pcm, bool, 0644);
45MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
46
Stephen Warren384a48d2011-06-01 11:14:21 -060047struct hdmi_spec_per_cvt {
48 hda_nid_t cvt_nid;
49 int assigned;
50 unsigned int channels_min;
51 unsigned int channels_max;
52 u32 rates;
53 u64 formats;
54 unsigned int maxbps;
55};
56
Takashi Iwai4eea3092013-02-07 18:18:19 +010057/* max. connections to a widget */
58#define HDA_MAX_CONNECTIONS 32
59
Stephen Warren384a48d2011-06-01 11:14:21 -060060struct hdmi_spec_per_pin {
61 hda_nid_t pin_nid;
62 int num_mux_nids;
63 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Wu Fengguang744626d2011-11-16 16:29:47 +080064
65 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060066 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080067 struct delayed_work work;
David Henningsson92c69e72013-02-19 16:11:26 +010068 struct snd_kcontrol *eld_ctl;
Wu Fengguangc6e84532011-11-18 16:59:32 -060069 int repoll_count;
Takashi Iwaidc780f72013-09-02 12:33:02 +020070 bool setup; /* the stream has been set up by prepare callback */
71 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020072 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020073 bool chmap_set; /* channel-map override by ALSA API? */
74 unsigned char chmap[8]; /* ALSA API channel-map */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010075 char pcm_name[8]; /* filled in build_pcm callbacks */
Stephen Warren384a48d2011-06-01 11:14:21 -060076};
77
Wu Fengguang079d88c2010-03-08 10:44:23 +080078struct hdmi_spec {
79 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010080 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
81 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -060082
Wu Fengguang079d88c2010-03-08 10:44:23 +080083 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010084 struct snd_array pins; /* struct hdmi_spec_per_pin */
85 struct snd_array pcm_rec; /* struct hda_pcm */
Takashi Iwaid45e6882012-07-31 11:36:00 +020086 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080087
David Henningsson4bd038f2013-02-19 16:11:25 +010088 struct hdmi_eld temp_eld;
Stephen Warren2b1461b2014-02-03 16:54:04 -070089
90 bool dyn_pin_out;
91
Wu Fengguang079d88c2010-03-08 10:44:23 +080092 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060093 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080094 */
95 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020096 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080097};
98
99
100struct hdmi_audio_infoframe {
101 u8 type; /* 0x84 */
102 u8 ver; /* 0x01 */
103 u8 len; /* 0x0a */
104
Wu Fengguang53d7d692010-09-21 14:25:49 +0800105 u8 checksum;
106
Wu Fengguang079d88c2010-03-08 10:44:23 +0800107 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
108 u8 SS01_SF24;
109 u8 CXT04;
110 u8 CA;
111 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800112};
113
114struct dp_audio_infoframe {
115 u8 type; /* 0x84 */
116 u8 len; /* 0x1b */
117 u8 ver; /* 0x11 << 2 */
118
119 u8 CC02_CT47; /* match with HDMI infoframe from this on */
120 u8 SS01_SF24;
121 u8 CXT04;
122 u8 CA;
123 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800124};
125
Takashi Iwai2b203db2011-02-11 12:17:30 +0100126union audio_infoframe {
127 struct hdmi_audio_infoframe hdmi;
128 struct dp_audio_infoframe dp;
129 u8 bytes[0];
130};
131
Wu Fengguang079d88c2010-03-08 10:44:23 +0800132/*
133 * CEA speaker placement:
134 *
135 * FLH FCH FRH
136 * FLW FL FLC FC FRC FR FRW
137 *
138 * LFE
139 * TC
140 *
141 * RL RLC RC RRC RR
142 *
143 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
144 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
145 */
146enum cea_speaker_placement {
147 FL = (1 << 0), /* Front Left */
148 FC = (1 << 1), /* Front Center */
149 FR = (1 << 2), /* Front Right */
150 FLC = (1 << 3), /* Front Left Center */
151 FRC = (1 << 4), /* Front Right Center */
152 RL = (1 << 5), /* Rear Left */
153 RC = (1 << 6), /* Rear Center */
154 RR = (1 << 7), /* Rear Right */
155 RLC = (1 << 8), /* Rear Left Center */
156 RRC = (1 << 9), /* Rear Right Center */
157 LFE = (1 << 10), /* Low Frequency Effect */
158 FLW = (1 << 11), /* Front Left Wide */
159 FRW = (1 << 12), /* Front Right Wide */
160 FLH = (1 << 13), /* Front Left High */
161 FCH = (1 << 14), /* Front Center High */
162 FRH = (1 << 15), /* Front Right High */
163 TC = (1 << 16), /* Top Center */
164};
165
166/*
167 * ELD SA bits in the CEA Speaker Allocation data block
168 */
169static int eld_speaker_allocation_bits[] = {
170 [0] = FL | FR,
171 [1] = LFE,
172 [2] = FC,
173 [3] = RL | RR,
174 [4] = RC,
175 [5] = FLC | FRC,
176 [6] = RLC | RRC,
177 /* the following are not defined in ELD yet */
178 [7] = FLW | FRW,
179 [8] = FLH | FRH,
180 [9] = TC,
181 [10] = FCH,
182};
183
184struct cea_channel_speaker_allocation {
185 int ca_index;
186 int speakers[8];
187
188 /* derived values, just for convenience */
189 int channels;
190 int spk_mask;
191};
192
193/*
194 * ALSA sequence is:
195 *
196 * surround40 surround41 surround50 surround51 surround71
197 * ch0 front left = = = =
198 * ch1 front right = = = =
199 * ch2 rear left = = = =
200 * ch3 rear right = = = =
201 * ch4 LFE center center center
202 * ch5 LFE LFE
203 * ch6 side left
204 * ch7 side right
205 *
206 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
207 */
208static int hdmi_channel_mapping[0x32][8] = {
209 /* stereo */
210 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
211 /* 2.1 */
212 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
213 /* Dolby Surround */
214 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
215 /* surround40 */
216 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
217 /* 4ch */
218 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
219 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800220 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800221 /* surround50 */
222 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
223 /* surround51 */
224 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
225 /* 7.1 */
226 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
227};
228
229/*
230 * This is an ordered list!
231 *
232 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800233 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800234 */
235static struct cea_channel_speaker_allocation channel_allocations[] = {
236/* channel: 7 6 5 4 3 2 1 0 */
237{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
238 /* 2.1 */
239{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
240 /* Dolby Surround */
241{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
242 /* surround40 */
243{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
244 /* surround41 */
245{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
246 /* surround50 */
247{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
248 /* surround51 */
249{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
250 /* 6.1 */
251{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
252 /* surround71 */
253{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
254
255{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
256{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
257{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
258{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
259{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
260{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
261{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
262{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
263{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
264{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
265{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
266{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
267{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
268{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
269{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
270{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
271{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
272{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
273{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
274{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
275{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
276{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
277{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
278{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
279{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
280{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
281{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
282{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
283{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
284{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
285{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
286{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
287{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
288{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
289{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
290{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
291{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
292{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
293{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
294{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
295{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
296};
297
298
299/*
300 * HDMI routines
301 */
302
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100303#define get_pin(spec, idx) \
304 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
305#define get_cvt(spec, idx) \
306 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
307#define get_pcm_rec(spec, idx) \
308 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
309
Stephen Warren384a48d2011-06-01 11:14:21 -0600310static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800311{
Stephen Warren384a48d2011-06-01 11:14:21 -0600312 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800313
Stephen Warren384a48d2011-06-01 11:14:21 -0600314 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100315 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600316 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800317
Stephen Warren384a48d2011-06-01 11:14:21 -0600318 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
319 return -EINVAL;
320}
321
322static int hinfo_to_pin_index(struct hdmi_spec *spec,
323 struct hda_pcm_stream *hinfo)
324{
325 int pin_idx;
326
327 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100328 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600329 return pin_idx;
330
331 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
332 return -EINVAL;
333}
334
335static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
336{
337 int cvt_idx;
338
339 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100340 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600341 return cvt_idx;
342
343 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800344 return -EINVAL;
345}
346
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500347static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
348 struct snd_ctl_elem_info *uinfo)
349{
350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100351 struct hdmi_spec *spec = codec->spec;
352 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500353 int pin_idx;
354
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500355 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
356
357 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100358 eld = &get_pin(spec, pin_idx)->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100359
David Henningsson4bd038f2013-02-19 16:11:25 +0100360 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100361 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
David Henningsson4bd038f2013-02-19 16:11:25 +0100362 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500363
364 return 0;
365}
366
367static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
368 struct snd_ctl_elem_value *ucontrol)
369{
370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100371 struct hdmi_spec *spec = codec->spec;
372 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500373 int pin_idx;
374
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500375 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100376 eld = &get_pin(spec, pin_idx)->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500377
David Henningsson4bd038f2013-02-19 16:11:25 +0100378 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100379 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
David Henningsson4bd038f2013-02-19 16:11:25 +0100380 mutex_unlock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100381 snd_BUG();
382 return -EINVAL;
383 }
384
385 memset(ucontrol->value.bytes.data, 0,
386 ARRAY_SIZE(ucontrol->value.bytes.data));
387 if (eld->eld_valid)
388 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
389 eld->eld_size);
David Henningsson4bd038f2013-02-19 16:11:25 +0100390 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500391
392 return 0;
393}
394
395static struct snd_kcontrol_new eld_bytes_ctl = {
396 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
397 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
398 .name = "ELD",
399 .info = hdmi_eld_ctl_info,
400 .get = hdmi_eld_ctl_get,
401};
402
403static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
404 int device)
405{
406 struct snd_kcontrol *kctl;
407 struct hdmi_spec *spec = codec->spec;
408 int err;
409
410 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
411 if (!kctl)
412 return -ENOMEM;
413 kctl->private_value = pin_idx;
414 kctl->id.device = device;
415
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100416 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500417 if (err < 0)
418 return err;
419
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100420 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500421 return 0;
422}
423
Wu Fengguang079d88c2010-03-08 10:44:23 +0800424#ifdef BE_PARANOID
425static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
426 int *packet_index, int *byte_index)
427{
428 int val;
429
430 val = snd_hda_codec_read(codec, pin_nid, 0,
431 AC_VERB_GET_HDMI_DIP_INDEX, 0);
432
433 *packet_index = val >> 5;
434 *byte_index = val & 0x1f;
435}
436#endif
437
438static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
439 int packet_index, int byte_index)
440{
441 int val;
442
443 val = (packet_index << 5) | (byte_index & 0x1f);
444
445 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
446}
447
448static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
449 unsigned char val)
450{
451 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
452}
453
Stephen Warren384a48d2011-06-01 11:14:21 -0600454static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800455{
Stephen Warren2b1461b2014-02-03 16:54:04 -0700456 struct hdmi_spec *spec = codec->spec;
457 int pin_out;
458
Wu Fengguang079d88c2010-03-08 10:44:23 +0800459 /* Unmute */
460 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
461 snd_hda_codec_write(codec, pin_nid, 0,
462 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren2b1461b2014-02-03 16:54:04 -0700463
464 if (spec->dyn_pin_out)
465 /* Disable pin out until stream is active */
466 pin_out = 0;
467 else
468 /* Enable pin out: some machines with GM965 gets broken output
469 * when the pin is disabled or changed while using with HDMI
470 */
471 pin_out = PIN_OUT;
472
Wu Fengguang079d88c2010-03-08 10:44:23 +0800473 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren2b1461b2014-02-03 16:54:04 -0700474 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800475}
476
Stephen Warren384a48d2011-06-01 11:14:21 -0600477static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800478{
Stephen Warren384a48d2011-06-01 11:14:21 -0600479 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800480 AC_VERB_GET_CVT_CHAN_COUNT, 0);
481}
482
483static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600484 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800485{
Stephen Warren384a48d2011-06-01 11:14:21 -0600486 if (chs != hdmi_get_channel_count(codec, cvt_nid))
487 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800488 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
489}
490
491
492/*
493 * Channel mapping routines
494 */
495
496/*
497 * Compute derived values in channel_allocations[].
498 */
499static void init_channel_allocations(void)
500{
501 int i, j;
502 struct cea_channel_speaker_allocation *p;
503
504 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
505 p = channel_allocations + i;
506 p->channels = 0;
507 p->spk_mask = 0;
508 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
509 if (p->speakers[j]) {
510 p->channels++;
511 p->spk_mask |= p->speakers[j];
512 }
513 }
514}
515
Wang Xingchao72357c72012-09-06 10:02:36 +0800516static int get_channel_allocation_order(int ca)
517{
518 int i;
519
520 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
521 if (channel_allocations[i].ca_index == ca)
522 break;
523 }
524 return i;
525}
526
Wu Fengguang079d88c2010-03-08 10:44:23 +0800527/*
528 * The transformation takes two steps:
529 *
530 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
531 * spk_mask => (channel_allocations[]) => ai->CA
532 *
533 * TODO: it could select the wrong CA from multiple candidates.
534*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600535static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800536{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800537 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800538 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800539 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800540 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
541
542 /*
543 * CA defaults to 0 for basic stereo audio
544 */
545 if (channels <= 2)
546 return 0;
547
Wu Fengguang079d88c2010-03-08 10:44:23 +0800548 /*
549 * expand ELD's speaker allocation mask
550 *
551 * ELD tells the speaker mask in a compact(paired) form,
552 * expand ELD's notions to match the ones used by Audio InfoFrame.
553 */
554 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b42013-02-19 16:11:24 +0100555 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800556 spk_mask |= eld_speaker_allocation_bits[i];
557 }
558
559 /* search for the first working match in the CA table */
560 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
561 if (channels == channel_allocations[i].channels &&
562 (spk_mask & channel_allocations[i].spk_mask) ==
563 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800564 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800565 break;
566 }
567 }
568
Anssi Hannula686edbc2013-09-01 14:36:47 +0300569 if (!ca) {
570 /* if there was no match, select the regular ALSA channel
571 * allocation with the matching number of channels */
572 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
573 if (channels == channel_allocations[i].channels) {
574 ca = channel_allocations[i].ca_index;
575 break;
576 }
577 }
578 }
579
David Henningsson1613d6b42013-02-19 16:11:24 +0100580 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800581 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800582 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800583
Wu Fengguang53d7d692010-09-21 14:25:49 +0800584 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800585}
586
587static void hdmi_debug_channel_mapping(struct hda_codec *codec,
588 hda_nid_t pin_nid)
589{
590#ifdef CONFIG_SND_DEBUG_VERBOSE
591 int i;
592 int slot;
593
594 for (i = 0; i < 8; i++) {
595 slot = snd_hda_codec_read(codec, pin_nid, 0,
596 AC_VERB_GET_HDMI_CHAN_SLOT, i);
597 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
598 slot >> 4, slot & 0xf);
599 }
600#endif
601}
602
603
Takashi Iwaid45e6882012-07-31 11:36:00 +0200604static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800605 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800606 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800607 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800608{
609 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800610 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800611 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800612 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800613
Wang Xingchao72357c72012-09-06 10:02:36 +0800614 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800615
Wu Fengguang079d88c2010-03-08 10:44:23 +0800616 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800617 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800618 hdmi_channel_mapping[ca][i] = i | (i << 4);
619 for (; i < 8; i++)
620 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
621 }
622
Wang Xingchao433968d2012-09-06 10:02:37 +0800623 if (non_pcm) {
624 for (i = 0; i < channel_allocations[order].channels; i++)
625 non_pcm_mapping[i] = i | (i << 4);
626 for (; i < 8; i++)
627 non_pcm_mapping[i] = 0xf | (i << 4);
628 }
629
Wu Fengguang079d88c2010-03-08 10:44:23 +0800630 for (i = 0; i < 8; i++) {
631 err = snd_hda_codec_write(codec, pin_nid, 0,
632 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800633 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800634 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800635 snd_printdd(KERN_NOTICE
636 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800637 break;
638 }
639 }
640
641 hdmi_debug_channel_mapping(codec, pin_nid);
642}
643
Takashi Iwaid45e6882012-07-31 11:36:00 +0200644struct channel_map_table {
645 unsigned char map; /* ALSA API channel map position */
646 unsigned char cea_slot; /* CEA slot value */
647 int spk_mask; /* speaker position bit mask */
648};
649
650static struct channel_map_table map_tables[] = {
651 { SNDRV_CHMAP_FL, 0x00, FL },
652 { SNDRV_CHMAP_FR, 0x01, FR },
653 { SNDRV_CHMAP_RL, 0x04, RL },
654 { SNDRV_CHMAP_RR, 0x05, RR },
655 { SNDRV_CHMAP_LFE, 0x02, LFE },
656 { SNDRV_CHMAP_FC, 0x03, FC },
657 { SNDRV_CHMAP_RLC, 0x06, RLC },
658 { SNDRV_CHMAP_RRC, 0x07, RRC },
659 {} /* terminator */
660};
661
662/* from ALSA API channel position to speaker bit mask */
663static int to_spk_mask(unsigned char c)
664{
665 struct channel_map_table *t = map_tables;
666 for (; t->map; t++) {
667 if (t->map == c)
668 return t->spk_mask;
669 }
670 return 0;
671}
672
673/* from ALSA API channel position to CEA slot */
674static int to_cea_slot(unsigned char c)
675{
676 struct channel_map_table *t = map_tables;
677 for (; t->map; t++) {
678 if (t->map == c)
679 return t->cea_slot;
680 }
681 return 0x0f;
682}
683
684/* from CEA slot to ALSA API channel position */
685static int from_cea_slot(unsigned char c)
686{
687 struct channel_map_table *t = map_tables;
688 for (; t->map; t++) {
689 if (t->cea_slot == c)
690 return t->map;
691 }
692 return 0;
693}
694
695/* from speaker bit mask to ALSA API channel position */
696static int spk_to_chmap(int spk)
697{
698 struct channel_map_table *t = map_tables;
699 for (; t->map; t++) {
700 if (t->spk_mask == spk)
701 return t->map;
702 }
703 return 0;
704}
705
706/* get the CA index corresponding to the given ALSA API channel map */
707static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
708{
709 int i, spks = 0, spk_mask = 0;
710
711 for (i = 0; i < chs; i++) {
712 int mask = to_spk_mask(map[i]);
713 if (mask) {
714 spk_mask |= mask;
715 spks++;
716 }
717 }
718
719 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
720 if ((chs == channel_allocations[i].channels ||
721 spks == channel_allocations[i].channels) &&
722 (spk_mask & channel_allocations[i].spk_mask) ==
723 channel_allocations[i].spk_mask)
724 return channel_allocations[i].ca_index;
725 }
726 return -1;
727}
728
729/* set up the channel slots for the given ALSA API channel map */
730static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
731 hda_nid_t pin_nid,
732 int chs, unsigned char *map)
733{
734 int i;
735 for (i = 0; i < 8; i++) {
736 int val, err;
737 if (i < chs)
738 val = to_cea_slot(map[i]);
739 else
740 val = 0xf;
741 val |= (i << 4);
742 err = snd_hda_codec_write(codec, pin_nid, 0,
743 AC_VERB_SET_HDMI_CHAN_SLOT, val);
744 if (err)
745 return -EINVAL;
746 }
747 return 0;
748}
749
750/* store ALSA API channel map from the current default map */
751static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
752{
753 int i;
Anssi Hannula1c645a12013-10-05 02:25:38 +0300754 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200755 for (i = 0; i < 8; i++) {
Anssi Hannula1c645a12013-10-05 02:25:38 +0300756 if (i < channel_allocations[ordered_ca].channels)
757 map[i] = from_cea_slot(hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200758 else
759 map[i] = 0;
760 }
761}
762
763static void hdmi_setup_channel_mapping(struct hda_codec *codec,
764 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200765 int channels, unsigned char *map,
766 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200767{
Anssi Hannula20608732013-02-03 17:55:45 +0200768 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200769 hdmi_manual_setup_channel_mapping(codec, pin_nid,
770 channels, map);
771 } else {
772 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
773 hdmi_setup_fake_chmap(map, ca);
774 }
775}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800776
777/*
778 * Audio InfoFrame routines
779 */
780
781/*
782 * Enable Audio InfoFrame Transmission
783 */
784static void hdmi_start_infoframe_trans(struct hda_codec *codec,
785 hda_nid_t pin_nid)
786{
787 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
788 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
789 AC_DIPXMIT_BEST);
790}
791
792/*
793 * Disable Audio InfoFrame Transmission
794 */
795static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
796 hda_nid_t pin_nid)
797{
798 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
799 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
800 AC_DIPXMIT_DISABLE);
801}
802
803static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
804{
805#ifdef CONFIG_SND_DEBUG_VERBOSE
806 int i;
807 int size;
808
809 size = snd_hdmi_get_eld_size(codec, pin_nid);
810 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
811
812 for (i = 0; i < 8; i++) {
813 size = snd_hda_codec_read(codec, pin_nid, 0,
814 AC_VERB_GET_HDMI_DIP_SIZE, i);
815 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
816 }
817#endif
818}
819
820static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
821{
822#ifdef BE_PARANOID
823 int i, j;
824 int size;
825 int pi, bi;
826 for (i = 0; i < 8; i++) {
827 size = snd_hda_codec_read(codec, pin_nid, 0,
828 AC_VERB_GET_HDMI_DIP_SIZE, i);
829 if (size == 0)
830 continue;
831
832 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
833 for (j = 1; j < 1000; j++) {
834 hdmi_write_dip_byte(codec, pin_nid, 0x0);
835 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
836 if (pi != i)
837 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
838 bi, pi, i);
839 if (bi == 0) /* byte index wrapped around */
840 break;
841 }
842 snd_printd(KERN_INFO
843 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
844 i, size, j);
845 }
846#endif
847}
848
Wu Fengguang53d7d692010-09-21 14:25:49 +0800849static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800850{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800851 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800852 u8 sum = 0;
853 int i;
854
Wu Fengguang53d7d692010-09-21 14:25:49 +0800855 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800856
Wu Fengguang53d7d692010-09-21 14:25:49 +0800857 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800858 sum += bytes[i];
859
Wu Fengguang53d7d692010-09-21 14:25:49 +0800860 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800861}
862
863static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
864 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800865 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800866{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800867 int i;
868
869 hdmi_debug_dip_size(codec, pin_nid);
870 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
871
Wu Fengguang079d88c2010-03-08 10:44:23 +0800872 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800873 for (i = 0; i < size; i++)
874 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800875}
876
877static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800878 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800879{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800880 u8 val;
881 int i;
882
883 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
884 != AC_DIPXMIT_BEST)
885 return false;
886
887 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800888 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800889 val = snd_hda_codec_read(codec, pin_nid, 0,
890 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800891 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800892 return false;
893 }
894
895 return true;
896}
897
Takashi Iwaidc780f72013-09-02 12:33:02 +0200898static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
899 struct hdmi_spec_per_pin *per_pin,
900 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800901{
Stephen Warren384a48d2011-06-01 11:14:21 -0600902 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaidc780f72013-09-02 12:33:02 +0200903 int channels = per_pin->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600904 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800905 int ca;
Takashi Iwai2b203db2011-02-11 12:17:30 +0100906 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800907
Takashi Iwaidc780f72013-09-02 12:33:02 +0200908 if (!channels)
909 return;
910
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100911 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -0600912 if (!eld->monitor_present)
913 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800914
Takashi Iwaid45e6882012-07-31 11:36:00 +0200915 if (!non_pcm && per_pin->chmap_set)
916 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
917 else
918 ca = hdmi_channel_allocation(eld, channels);
919 if (ca < 0)
920 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800921
Stephen Warren384a48d2011-06-01 11:14:21 -0600922 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b42013-02-19 16:11:24 +0100923 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600924 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800925
Stephen Warren384a48d2011-06-01 11:14:21 -0600926 hdmi_ai->type = 0x84;
927 hdmi_ai->ver = 0x01;
928 hdmi_ai->len = 0x0a;
929 hdmi_ai->CC02_CT47 = channels - 1;
930 hdmi_ai->CA = ca;
931 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b42013-02-19 16:11:24 +0100932 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600933 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800934
Stephen Warren384a48d2011-06-01 11:14:21 -0600935 dp_ai->type = 0x84;
936 dp_ai->len = 0x1b;
937 dp_ai->ver = 0x11 << 2;
938 dp_ai->CC02_CT47 = channels - 1;
939 dp_ai->CA = ca;
940 } else {
941 snd_printd("HDMI: unknown connection type at pin %d\n",
942 pin_nid);
943 return;
944 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800945
Stephen Warren384a48d2011-06-01 11:14:21 -0600946 /*
Anssi Hannula43d3dd12013-10-07 19:24:52 +0300947 * always configure channel mapping, it may have been changed by the
948 * user in the meantime
949 */
950 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
951 channels, per_pin->chmap,
952 per_pin->chmap_set);
953
954 /*
Stephen Warren384a48d2011-06-01 11:14:21 -0600955 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
956 * sizeof(*dp_ai) to avoid partial match/update problems when
957 * the user switches between HDMI/DP monitors.
958 */
959 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
960 sizeof(ai))) {
961 snd_printdd("hdmi_setup_audio_infoframe: "
962 "pin=%d channels=%d\n",
963 pin_nid,
964 channels);
Stephen Warren384a48d2011-06-01 11:14:21 -0600965 hdmi_stop_infoframe_trans(codec, pin_nid);
966 hdmi_fill_audio_infoframe(codec, pin_nid,
967 ai.bytes, sizeof(ai));
968 hdmi_start_infoframe_trans(codec, pin_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800969 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800970
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200971 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800972}
973
974
975/*
976 * Unsolicited events
977 */
978
Wu Fengguangc6e84532011-11-18 16:59:32 -0600979static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200980
Wu Fengguang079d88c2010-03-08 10:44:23 +0800981static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
982{
983 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200984 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
985 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600986 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200987 struct hda_jack_tbl *jack;
988
989 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
990 if (!jack)
991 return;
992 pin_nid = jack->nid;
993 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800994
Fengguang Wufae3d882012-04-10 17:00:35 +0800995 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -0600996 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Fengguang Wufae3d882012-04-10 17:00:35 +0800997 codec->addr, pin_nid,
998 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800999
Stephen Warren384a48d2011-06-01 11:14:21 -06001000 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
1001 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001002 return;
1003
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001004 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001005 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001006}
1007
1008static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1009{
1010 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1011 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1012 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1013 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1014
1015 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001016 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001017 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001018 tag,
1019 subtag,
1020 cp_state,
1021 cp_ready);
1022
1023 /* TODO */
1024 if (cp_state)
1025 ;
1026 if (cp_ready)
1027 ;
1028}
1029
1030
1031static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1032{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001033 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1034 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1035
Takashi Iwai3a938972011-10-28 01:16:55 +02001036 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001037 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1038 return;
1039 }
1040
1041 if (subtag == 0)
1042 hdmi_intrinsic_event(codec, res);
1043 else
1044 hdmi_non_intrinsic_event(codec, res);
1045}
1046
David Henningsson83f26ad2013-04-10 12:26:07 +02001047static void haswell_verify_pin_D0(struct hda_codec *codec, hda_nid_t nid)
1048{
1049 int pwr, lamp, ramp;
1050
1051 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1052 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1053 if (pwr != AC_PWRST_D0) {
1054 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1055 AC_PWRST_D0);
1056 msleep(40);
1057 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1058 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1059 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1060 }
1061
1062 lamp = snd_hda_codec_read(codec, nid, 0,
1063 AC_VERB_GET_AMP_GAIN_MUTE,
1064 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1065 ramp = snd_hda_codec_read(codec, nid, 0,
1066 AC_VERB_GET_AMP_GAIN_MUTE,
1067 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1068 if (lamp != ramp) {
1069 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1070 AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
1071
1072 lamp = snd_hda_codec_read(codec, nid, 0,
1073 AC_VERB_GET_AMP_GAIN_MUTE,
1074 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1075 ramp = snd_hda_codec_read(codec, nid, 0,
1076 AC_VERB_GET_AMP_GAIN_MUTE,
1077 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1078 snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
1079 }
1080}
1081
Wu Fengguang079d88c2010-03-08 10:44:23 +08001082/*
1083 * Callbacks
1084 */
1085
Takashi Iwai92f10b32010-08-03 14:21:00 +02001086/* HBR should be Non-PCM, 8 channels */
1087#define is_hbr_format(format) \
1088 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1089
Stephen Warren384a48d2011-06-01 11:14:21 -06001090static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1091 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001092{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001093 int pinctl;
1094 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001095
David Henningsson83f26ad2013-04-10 12:26:07 +02001096 if (codec->vendor_id == 0x80862807)
1097 haswell_verify_pin_D0(codec, pin_nid);
1098
Stephen Warren384a48d2011-06-01 11:14:21 -06001099 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1100 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001101 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1102
1103 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001104 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001105 new_pinctl |= AC_PINCTL_EPT_HBR;
1106 else
1107 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1108
1109 snd_printdd("hdmi_setup_stream: "
1110 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001111 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001112 pinctl == new_pinctl ? "" : "new-",
1113 new_pinctl);
1114
1115 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001116 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001117 AC_VERB_SET_PIN_WIDGET_CONTROL,
1118 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001119
Stephen Warren384a48d2011-06-01 11:14:21 -06001120 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001121 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001122 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1123 return -EINVAL;
1124 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001125
Stephen Warren384a48d2011-06-01 11:14:21 -06001126 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001127 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001128}
1129
1130/*
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001131 * HDA PCM callbacks
1132 */
1133static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1134 struct hda_codec *codec,
1135 struct snd_pcm_substream *substream)
1136{
1137 struct hdmi_spec *spec = codec->spec;
Takashi Iwai639cef02011-01-14 10:30:46 +01001138 struct snd_pcm_runtime *runtime = substream->runtime;
Stephen Warren384a48d2011-06-01 11:14:21 -06001139 int pin_idx, cvt_idx, mux_idx = 0;
1140 struct hdmi_spec_per_pin *per_pin;
1141 struct hdmi_eld *eld;
1142 struct hdmi_spec_per_cvt *per_cvt = NULL;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001143
Stephen Warren384a48d2011-06-01 11:14:21 -06001144 /* Validate hinfo */
1145 pin_idx = hinfo_to_pin_index(spec, hinfo);
1146 if (snd_BUG_ON(pin_idx < 0))
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001147 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001148 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001149 eld = &per_pin->sink_eld;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001150
Stephen Warren384a48d2011-06-01 11:14:21 -06001151 /* Dynamically assign converter to stream */
1152 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001153 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001154
1155 /* Must not already be assigned */
1156 if (per_cvt->assigned)
1157 continue;
1158 /* Must be in pin's mux's list of converters */
1159 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1160 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1161 break;
1162 /* Not in mux list */
1163 if (mux_idx == per_pin->num_mux_nids)
1164 continue;
1165 break;
1166 }
1167 /* No free converters */
1168 if (cvt_idx == spec->num_cvts)
1169 return -ENODEV;
1170
1171 /* Claim converter */
1172 per_cvt->assigned = 1;
1173 hinfo->nid = per_cvt->cvt_nid;
1174
Takashi Iwai27035ca2013-06-18 16:14:22 +02001175 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001176 AC_VERB_SET_CONNECT_SEL,
1177 mux_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001178 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001179
Stephen Warren2def8172011-06-01 11:14:20 -06001180 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001181 hinfo->channels_min = per_cvt->channels_min;
1182 hinfo->channels_max = per_cvt->channels_max;
1183 hinfo->rates = per_cvt->rates;
1184 hinfo->formats = per_cvt->formats;
1185 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001186
Stephen Warren384a48d2011-06-01 11:14:21 -06001187 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001188 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b42013-02-19 16:11:24 +01001189 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001190 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001191 !hinfo->rates || !hinfo->formats) {
1192 per_cvt->assigned = 0;
1193 hinfo->nid = 0;
1194 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001195 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001196 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001197 }
Stephen Warren2def8172011-06-01 11:14:20 -06001198
1199 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001200 runtime->hw.channels_min = hinfo->channels_min;
1201 runtime->hw.channels_max = hinfo->channels_max;
1202 runtime->hw.formats = hinfo->formats;
1203 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001204
1205 snd_pcm_hw_constraint_step(substream->runtime, 0,
1206 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001207 return 0;
1208}
1209
1210/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001211 * HDA/HDMI auto parsing
1212 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001213static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001214{
1215 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001216 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001217 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001218
1219 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1220 snd_printk(KERN_WARNING
1221 "HDMI: pin %d wcaps %#x "
1222 "does not support connection list\n",
1223 pin_nid, get_wcaps(codec, pin_nid));
1224 return -EINVAL;
1225 }
1226
Stephen Warren384a48d2011-06-01 11:14:21 -06001227 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1228 per_pin->mux_nids,
1229 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001230
1231 return 0;
1232}
1233
Wu Fengguangc6e84532011-11-18 16:59:32 -06001234static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001235{
Wu Fengguang744626d2011-11-16 16:29:47 +08001236 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001237 struct hdmi_spec *spec = codec->spec;
1238 struct hdmi_eld *eld = &spec->temp_eld;
1239 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001240 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001241 /*
1242 * Always execute a GetPinSense verb here, even when called from
1243 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1244 * response's PD bit is not the real PD value, but indicates that
1245 * the real PD value changed. An older version of the HD-audio
1246 * specification worked this way. Hence, we just ignore the data in
1247 * the unsolicited response to avoid custom WARs.
1248 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001249 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001250 bool update_eld = false;
1251 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001252
David Henningsson4bd038f2013-02-19 16:11:25 +01001253 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1254 if (pin_eld->monitor_present)
1255 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1256 else
1257 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001258
Fengguang Wufae3d882012-04-10 17:00:35 +08001259 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001260 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001261 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001262
David Henningsson4bd038f2013-02-19 16:11:25 +01001263 if (eld->eld_valid) {
David Henningsson1613d6b42013-02-19 16:11:24 +01001264 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1265 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001266 eld->eld_valid = false;
David Henningsson1613d6b42013-02-19 16:11:24 +01001267 else {
1268 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1269 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1270 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001271 eld->eld_valid = false;
David Henningsson1613d6b42013-02-19 16:11:24 +01001272 }
1273
David Henningsson4bd038f2013-02-19 16:11:25 +01001274 if (eld->eld_valid) {
David Henningsson1613d6b42013-02-19 16:11:24 +01001275 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001276 update_eld = true;
David Henningsson1613d6b42013-02-19 16:11:24 +01001277 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001278 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001279 queue_delayed_work(codec->bus->workq,
1280 &per_pin->work,
1281 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001282 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001283 }
1284 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001285
1286 mutex_lock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001287 if (pin_eld->eld_valid && !eld->eld_valid) {
David Henningsson4bd038f2013-02-19 16:11:25 +01001288 update_eld = true;
David Henningsson92c69e72013-02-19 16:11:26 +01001289 eld_changed = true;
1290 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001291 if (update_eld) {
Takashi Iwaidc780f72013-09-02 12:33:02 +02001292 bool old_eld_valid = pin_eld->eld_valid;
David Henningsson4bd038f2013-02-19 16:11:25 +01001293 pin_eld->eld_valid = eld->eld_valid;
David Henningsson92c69e72013-02-19 16:11:26 +01001294 eld_changed = pin_eld->eld_size != eld->eld_size ||
1295 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
David Henningsson4bd038f2013-02-19 16:11:25 +01001296 eld->eld_size) != 0;
1297 if (eld_changed)
1298 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1299 eld->eld_size);
1300 pin_eld->eld_size = eld->eld_size;
1301 pin_eld->info = eld->info;
Takashi Iwaidc780f72013-09-02 12:33:02 +02001302
1303 /* Haswell-specific workaround: re-setup when the transcoder is
1304 * changed during the stream playback
1305 */
1306 if (codec->vendor_id == 0x80862807 &&
1307 eld->eld_valid && !old_eld_valid && per_pin->setup) {
1308 snd_hda_codec_write(codec, pin_nid, 0,
1309 AC_VERB_SET_AMP_GAIN_MUTE,
1310 AMP_OUT_UNMUTE);
1311 hdmi_setup_audio_infoframe(codec, per_pin,
1312 per_pin->non_pcm);
1313 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001314 }
1315 mutex_unlock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001316
1317 if (eld_changed)
1318 snd_ctl_notify(codec->bus->card,
1319 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1320 &per_pin->eld_ctl->id);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001321}
1322
Wu Fengguang744626d2011-11-16 16:29:47 +08001323static void hdmi_repoll_eld(struct work_struct *work)
1324{
1325 struct hdmi_spec_per_pin *per_pin =
1326 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1327
Wu Fengguangc6e84532011-11-18 16:59:32 -06001328 if (per_pin->repoll_count++ > 6)
1329 per_pin->repoll_count = 0;
1330
1331 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001332}
1333
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001334static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1335 hda_nid_t nid);
1336
Wu Fengguang079d88c2010-03-08 10:44:23 +08001337static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1338{
1339 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001340 unsigned int caps, config;
1341 int pin_idx;
1342 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001343 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001344
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001345 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001346 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1347 return 0;
1348
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001349 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001350 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1351 return 0;
1352
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001353 if (codec->vendor_id == 0x80862807)
1354 intel_haswell_fixup_connect_list(codec, pin_nid);
1355
Stephen Warren384a48d2011-06-01 11:14:21 -06001356 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001357 per_pin = snd_array_new(&spec->pins);
1358 if (!per_pin)
1359 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001360
1361 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001362 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001363
Stephen Warren384a48d2011-06-01 11:14:21 -06001364 err = hdmi_read_pin_conn(codec, pin_idx);
1365 if (err < 0)
1366 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001367
Wu Fengguang079d88c2010-03-08 10:44:23 +08001368 spec->num_pins++;
1369
Stephen Warren384a48d2011-06-01 11:14:21 -06001370 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001371}
1372
Stephen Warren384a48d2011-06-01 11:14:21 -06001373static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001374{
1375 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001376 struct hdmi_spec_per_cvt *per_cvt;
1377 unsigned int chans;
1378 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001379
Stephen Warren384a48d2011-06-01 11:14:21 -06001380 chans = get_wcaps(codec, cvt_nid);
1381 chans = get_wcaps_channels(chans);
1382
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001383 per_cvt = snd_array_new(&spec->cvts);
1384 if (!per_cvt)
1385 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001386
1387 per_cvt->cvt_nid = cvt_nid;
1388 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001389 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001390 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001391 if (chans > spec->channels_max)
1392 spec->channels_max = chans;
1393 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001394
1395 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1396 &per_cvt->rates,
1397 &per_cvt->formats,
1398 &per_cvt->maxbps);
1399 if (err < 0)
1400 return err;
1401
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001402 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1403 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1404 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001405
1406 return 0;
1407}
1408
1409static int hdmi_parse_codec(struct hda_codec *codec)
1410{
1411 hda_nid_t nid;
1412 int i, nodes;
1413
1414 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1415 if (!nid || nodes < 0) {
1416 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1417 return -EINVAL;
1418 }
1419
1420 for (i = 0; i < nodes; i++, nid++) {
1421 unsigned int caps;
1422 unsigned int type;
1423
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001424 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001425 type = get_wcaps_type(caps);
1426
1427 if (!(caps & AC_WCAP_DIGITAL))
1428 continue;
1429
1430 switch (type) {
1431 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001432 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001433 break;
1434 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001435 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001436 break;
1437 }
1438 }
1439
David Henningssonc9adeef2012-11-07 09:22:33 +01001440#ifdef CONFIG_PM
1441 /* We're seeing some problems with unsolicited hot plug events on
1442 * PantherPoint after S3, if this is not enabled */
1443 if (codec->vendor_id == 0x80862806)
1444 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001445 /*
1446 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1447 * can be lost and presence sense verb will become inaccurate if the
1448 * HDA link is powered off at hot plug or hw initialization time.
1449 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001450 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001451 AC_PWRST_EPSS))
1452 codec->bus->power_keep_link_on = 1;
1453#endif
1454
1455 return 0;
1456}
1457
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001458/*
1459 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001460static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1461{
1462 struct hda_spdif_out *spdif;
1463 bool non_pcm;
1464
1465 mutex_lock(&codec->spdif_mutex);
1466 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1467 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1468 mutex_unlock(&codec->spdif_mutex);
1469 return non_pcm;
1470}
1471
1472
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001473/*
1474 * HDMI callbacks
1475 */
1476
1477static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1478 struct hda_codec *codec,
1479 unsigned int stream_tag,
1480 unsigned int format,
1481 struct snd_pcm_substream *substream)
1482{
Stephen Warren384a48d2011-06-01 11:14:21 -06001483 hda_nid_t cvt_nid = hinfo->nid;
1484 struct hdmi_spec *spec = codec->spec;
1485 int pin_idx = hinfo_to_pin_index(spec, hinfo);
Takashi Iwaidc780f72013-09-02 12:33:02 +02001486 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1487 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001488 bool non_pcm;
Stephen Warren2b1461b2014-02-03 16:54:04 -07001489 int pinctl;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001490
1491 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaidc780f72013-09-02 12:33:02 +02001492 per_pin->channels = substream->runtime->channels;
1493 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001494
Stephen Warren384a48d2011-06-01 11:14:21 -06001495 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001496
Takashi Iwaidc780f72013-09-02 12:33:02 +02001497 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Stephen Warren384a48d2011-06-01 11:14:21 -06001498
Stephen Warren2b1461b2014-02-03 16:54:04 -07001499 if (spec->dyn_pin_out) {
1500 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1501 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1502 snd_hda_codec_write(codec, pin_nid, 0,
1503 AC_VERB_SET_PIN_WIDGET_CONTROL,
1504 pinctl | PIN_OUT);
1505 }
1506
Stephen Warren384a48d2011-06-01 11:14:21 -06001507 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001508}
1509
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001510static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1511 struct hda_codec *codec,
1512 struct snd_pcm_substream *substream)
1513{
1514 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1515 return 0;
1516}
1517
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001518static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1519 struct hda_codec *codec,
1520 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001521{
1522 struct hdmi_spec *spec = codec->spec;
1523 int cvt_idx, pin_idx;
1524 struct hdmi_spec_per_cvt *per_cvt;
1525 struct hdmi_spec_per_pin *per_pin;
Stephen Warren2b1461b2014-02-03 16:54:04 -07001526 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06001527
Stephen Warren384a48d2011-06-01 11:14:21 -06001528 if (hinfo->nid) {
1529 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1530 if (snd_BUG_ON(cvt_idx < 0))
1531 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001532 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001533
1534 snd_BUG_ON(!per_cvt->assigned);
1535 per_cvt->assigned = 0;
1536 hinfo->nid = 0;
1537
1538 pin_idx = hinfo_to_pin_index(spec, hinfo);
1539 if (snd_BUG_ON(pin_idx < 0))
1540 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001541 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001542
Stephen Warren2b1461b2014-02-03 16:54:04 -07001543 if (spec->dyn_pin_out) {
1544 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1545 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1546 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1547 AC_VERB_SET_PIN_WIDGET_CONTROL,
1548 pinctl & ~PIN_OUT);
1549 }
1550
Stephen Warren384a48d2011-06-01 11:14:21 -06001551 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001552 per_pin->chmap_set = false;
1553 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaidc780f72013-09-02 12:33:02 +02001554
1555 per_pin->setup = false;
1556 per_pin->channels = 0;
Stephen Warren384a48d2011-06-01 11:14:21 -06001557 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001558
Stephen Warren384a48d2011-06-01 11:14:21 -06001559 return 0;
1560}
1561
1562static const struct hda_pcm_ops generic_ops = {
1563 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001564 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001565 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001566 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001567};
1568
Takashi Iwaid45e6882012-07-31 11:36:00 +02001569/*
1570 * ALSA API channel-map control callbacks
1571 */
1572static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1573 struct snd_ctl_elem_info *uinfo)
1574{
1575 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1576 struct hda_codec *codec = info->private_data;
1577 struct hdmi_spec *spec = codec->spec;
1578 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1579 uinfo->count = spec->channels_max;
1580 uinfo->value.integer.min = 0;
1581 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1582 return 0;
1583}
1584
1585static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1586 unsigned int size, unsigned int __user *tlv)
1587{
1588 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1589 struct hda_codec *codec = info->private_data;
1590 struct hdmi_spec *spec = codec->spec;
1591 const unsigned int valid_mask =
1592 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1593 unsigned int __user *dst;
1594 int chs, count = 0;
1595
1596 if (size < 8)
1597 return -ENOMEM;
1598 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1599 return -EFAULT;
1600 size -= 8;
1601 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001602 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001603 int i, c;
1604 struct cea_channel_speaker_allocation *cap;
1605 cap = channel_allocations;
1606 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1607 int chs_bytes = chs * 4;
1608 if (cap->channels != chs)
1609 continue;
1610 if (cap->spk_mask & ~valid_mask)
1611 continue;
1612 if (size < 8)
1613 return -ENOMEM;
1614 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1615 put_user(chs_bytes, dst + 1))
1616 return -EFAULT;
1617 dst += 2;
1618 size -= 8;
1619 count += 8;
1620 if (size < chs_bytes)
1621 return -ENOMEM;
1622 size -= chs_bytes;
1623 count += chs_bytes;
1624 for (c = 7; c >= 0; c--) {
1625 int spk = cap->speakers[c];
1626 if (!spk)
1627 continue;
1628 if (put_user(spk_to_chmap(spk), dst))
1629 return -EFAULT;
1630 dst++;
1631 }
1632 }
1633 }
1634 if (put_user(count, tlv + 1))
1635 return -EFAULT;
1636 return 0;
1637}
1638
1639static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1640 struct snd_ctl_elem_value *ucontrol)
1641{
1642 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1643 struct hda_codec *codec = info->private_data;
1644 struct hdmi_spec *spec = codec->spec;
1645 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001646 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001647 int i;
1648
1649 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1650 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1651 return 0;
1652}
1653
1654static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1655 struct snd_ctl_elem_value *ucontrol)
1656{
1657 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1658 struct hda_codec *codec = info->private_data;
1659 struct hdmi_spec *spec = codec->spec;
1660 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001661 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001662 unsigned int ctl_idx;
1663 struct snd_pcm_substream *substream;
1664 unsigned char chmap[8];
1665 int i, ca, prepared = 0;
1666
1667 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1668 substream = snd_pcm_chmap_substream(info, ctl_idx);
1669 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001670 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001671 switch (substream->runtime->status->state) {
1672 case SNDRV_PCM_STATE_OPEN:
1673 case SNDRV_PCM_STATE_SETUP:
1674 break;
1675 case SNDRV_PCM_STATE_PREPARED:
1676 prepared = 1;
1677 break;
1678 default:
1679 return -EBUSY;
1680 }
1681 memset(chmap, 0, sizeof(chmap));
1682 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1683 chmap[i] = ucontrol->value.integer.value[i];
1684 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1685 return 0;
1686 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1687 if (ca < 0)
1688 return -EINVAL;
1689 per_pin->chmap_set = true;
1690 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1691 if (prepared)
Takashi Iwaidc780f72013-09-02 12:33:02 +02001692 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001693
1694 return 0;
1695}
1696
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001697static int generic_hdmi_build_pcms(struct hda_codec *codec)
1698{
1699 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001700 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001701
Stephen Warren384a48d2011-06-01 11:14:21 -06001702 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1703 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001704 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001705 struct hdmi_spec_per_pin *per_pin;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001706
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001707 per_pin = get_pin(spec, pin_idx);
1708 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1709 info = snd_array_new(&spec->pcm_rec);
1710 if (!info)
1711 return -ENOMEM;
1712 info->name = per_pin->pcm_name;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001713 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001714 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001715
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001716 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001717 pstr->substreams = 1;
1718 pstr->ops = generic_ops;
1719 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001720 }
1721
Stephen Warren384a48d2011-06-01 11:14:21 -06001722 codec->num_pcms = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001723 codec->pcm_info = spec->pcm_rec.list;
Stephen Warren384a48d2011-06-01 11:14:21 -06001724
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001725 return 0;
1726}
1727
David Henningsson0b6c49b2011-08-23 16:56:03 +02001728static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1729{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001730 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001731 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001732 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1733 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001734
Takashi Iwai31ef2252011-12-01 17:41:36 +01001735 if (pcmdev > 0)
1736 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson30efd8d2013-02-22 10:16:28 +01001737 if (!is_jack_detectable(codec, per_pin->pin_nid))
1738 strncat(hdmi_str, " Phantom",
1739 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001740
Takashi Iwai31ef2252011-12-01 17:41:36 +01001741 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001742}
1743
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001744static int generic_hdmi_build_controls(struct hda_codec *codec)
1745{
1746 struct hdmi_spec *spec = codec->spec;
1747 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001748 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001749
Stephen Warren384a48d2011-06-01 11:14:21 -06001750 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001751 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001752
1753 err = generic_hdmi_build_jack(codec, pin_idx);
1754 if (err < 0)
1755 return err;
1756
Takashi Iwaidcda5802012-10-12 17:24:51 +02001757 err = snd_hda_create_dig_out_ctls(codec,
1758 per_pin->pin_nid,
1759 per_pin->mux_nids[0],
1760 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001761 if (err < 0)
1762 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001763 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001764
1765 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001766 err = hdmi_create_eld_ctl(codec, pin_idx,
1767 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001768
1769 if (err < 0)
1770 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001771
Takashi Iwai82b1d732011-12-20 15:53:07 +01001772 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001773 }
1774
Takashi Iwaid45e6882012-07-31 11:36:00 +02001775 /* add channel maps */
1776 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1777 struct snd_pcm_chmap *chmap;
1778 struct snd_kcontrol *kctl;
1779 int i;
Takashi Iwaida958132013-08-22 09:55:36 +02001780
1781 if (!codec->pcm_info[pin_idx].pcm)
1782 break;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001783 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1784 SNDRV_PCM_STREAM_PLAYBACK,
1785 NULL, 0, pin_idx, &chmap);
1786 if (err < 0)
1787 return err;
1788 /* override handlers */
1789 chmap->private_data = codec;
1790 kctl = chmap->kctl;
1791 for (i = 0; i < kctl->count; i++)
1792 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1793 kctl->info = hdmi_chmap_ctl_info;
1794 kctl->get = hdmi_chmap_ctl_get;
1795 kctl->put = hdmi_chmap_ctl_put;
1796 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1797 }
1798
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001799 return 0;
1800}
1801
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001802static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1803{
1804 struct hdmi_spec *spec = codec->spec;
1805 int pin_idx;
1806
1807 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001808 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001809 struct hdmi_eld *eld = &per_pin->sink_eld;
1810
1811 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001812 mutex_init(&eld->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001813 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1814 snd_hda_eld_proc_new(codec, eld, pin_idx);
1815 }
1816 return 0;
1817}
1818
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001819static int generic_hdmi_init(struct hda_codec *codec)
1820{
1821 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001822 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001823
Stephen Warren384a48d2011-06-01 11:14:21 -06001824 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001825 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001826 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001827
1828 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001829 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001830 }
1831 return 0;
1832}
1833
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001834static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1835{
1836 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1837 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1838 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1839}
1840
1841static void hdmi_array_free(struct hdmi_spec *spec)
1842{
1843 snd_array_free(&spec->pins);
1844 snd_array_free(&spec->cvts);
1845 snd_array_free(&spec->pcm_rec);
1846}
1847
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001848static void generic_hdmi_free(struct hda_codec *codec)
1849{
1850 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001851 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001852
Stephen Warren384a48d2011-06-01 11:14:21 -06001853 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001854 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001855 struct hdmi_eld *eld = &per_pin->sink_eld;
1856
Wu Fengguang744626d2011-11-16 16:29:47 +08001857 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001858 snd_hda_eld_proc_free(codec, eld);
1859 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001860
Wu Fengguang744626d2011-11-16 16:29:47 +08001861 flush_workqueue(codec->bus->workq);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001862 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001863 kfree(spec);
1864}
1865
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001866static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001867 .init = generic_hdmi_init,
1868 .free = generic_hdmi_free,
1869 .build_pcms = generic_hdmi_build_pcms,
1870 .build_controls = generic_hdmi_build_controls,
1871 .unsol_event = hdmi_unsol_event,
1872};
1873
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001874
1875static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1876 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001877{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001878 struct hdmi_spec *spec = codec->spec;
1879 hda_nid_t conns[4];
1880 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001881
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001882 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1883 if (nconns == spec->num_cvts &&
1884 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001885 return;
1886
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001887 /* override pins connection list */
1888 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001889 nconns = max(spec->num_cvts, 4);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001890 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001891}
1892
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001893#define INTEL_VENDOR_NID 0x08
1894#define INTEL_GET_VENDOR_VERB 0xf81
1895#define INTEL_SET_VENDOR_VERB 0x781
1896#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1897#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1898
1899static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02001900 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001901{
1902 unsigned int vendor_param;
1903
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001904 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1905 INTEL_GET_VENDOR_VERB, 0);
1906 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1907 return;
1908
1909 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1910 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1911 INTEL_SET_VENDOR_VERB, vendor_param);
1912 if (vendor_param == -1)
1913 return;
1914
Takashi Iwai17df3f52013-05-08 08:09:34 +02001915 if (update_tree)
1916 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001917}
1918
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001919static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1920{
1921 unsigned int vendor_param;
1922
1923 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1924 INTEL_GET_VENDOR_VERB, 0);
1925 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1926 return;
1927
1928 /* enable DP1.2 mode */
1929 vendor_param |= INTEL_EN_DP12;
1930 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1931 INTEL_SET_VENDOR_VERB, vendor_param);
1932}
1933
Takashi Iwai17df3f52013-05-08 08:09:34 +02001934/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
1935 * Otherwise you may get severe h/w communication errors.
1936 */
1937static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1938 unsigned int power_state)
1939{
1940 if (power_state == AC_PWRST_D0) {
1941 intel_haswell_enable_all_pins(codec, false);
1942 intel_haswell_fixup_enable_dp12(codec);
1943 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001944
Takashi Iwai17df3f52013-05-08 08:09:34 +02001945 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
1946 snd_hda_codec_set_power_to_all(codec, fg, power_state);
1947}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001948
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001949static int patch_generic_hdmi(struct hda_codec *codec)
1950{
1951 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001952
1953 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1954 if (spec == NULL)
1955 return -ENOMEM;
1956
1957 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001958 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001959
Takashi Iwai17df3f52013-05-08 08:09:34 +02001960 if (codec->vendor_id == 0x80862807) {
1961 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001962 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02001963 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001964
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001965 if (hdmi_parse_codec(codec) < 0) {
1966 codec->spec = NULL;
1967 kfree(spec);
1968 return -EINVAL;
1969 }
1970 codec->patch_ops = generic_hdmi_patch_ops;
Takashi Iwai17df3f52013-05-08 08:09:34 +02001971 if (codec->vendor_id == 0x80862807)
1972 codec->patch_ops.set_power_state = haswell_set_power_state;
1973
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001974 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001975
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001976 init_channel_allocations();
1977
1978 return 0;
1979}
1980
1981/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06001982 * Shared non-generic implementations
1983 */
1984
1985static int simple_playback_build_pcms(struct hda_codec *codec)
1986{
1987 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001988 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001989 unsigned int chans;
1990 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001991 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001992
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001993 per_cvt = get_cvt(spec, 0);
1994 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001995 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001996
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001997 info = snd_array_new(&spec->pcm_rec);
1998 if (!info)
1999 return -ENOMEM;
2000 info->name = get_pin(spec, 0)->pcm_name;
2001 sprintf(info->name, "HDMI 0");
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002002 info->pcm_type = HDA_PCM_TYPE_HDMI;
2003 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2004 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002005 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002006 if (pstr->channels_max <= 2 && chans && chans <= 16)
2007 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002008
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002009 codec->num_pcms = 1;
2010 codec->pcm_info = info;
2011
Stephen Warren3aaf8982011-06-01 11:14:19 -06002012 return 0;
2013}
2014
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002015/* unsolicited event for jack sensing */
2016static void simple_hdmi_unsol_event(struct hda_codec *codec,
2017 unsigned int res)
2018{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002019 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002020 snd_hda_jack_report_sync(codec);
2021}
2022
2023/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2024 * as long as spec->pins[] is set correctly
2025 */
2026#define simple_hdmi_build_jack generic_hdmi_build_jack
2027
Stephen Warren3aaf8982011-06-01 11:14:19 -06002028static int simple_playback_build_controls(struct hda_codec *codec)
2029{
2030 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002031 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002032 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002033
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002034 per_cvt = get_cvt(spec, 0);
Anssi Hannula6492e3d2013-12-10 22:46:34 +02002035 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2036 per_cvt->cvt_nid,
2037 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002038 if (err < 0)
2039 return err;
2040 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002041}
2042
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002043static int simple_playback_init(struct hda_codec *codec)
2044{
2045 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002046 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2047 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002048
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002049 snd_hda_codec_write(codec, pin, 0,
2050 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2051 /* some codecs require to unmute the pin */
2052 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2053 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2054 AMP_OUT_UNMUTE);
2055 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002056 return 0;
2057}
2058
Stephen Warren3aaf8982011-06-01 11:14:19 -06002059static void simple_playback_free(struct hda_codec *codec)
2060{
2061 struct hdmi_spec *spec = codec->spec;
2062
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002063 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002064 kfree(spec);
2065}
2066
2067/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002068 * Nvidia specific implementations
2069 */
2070
2071#define Nv_VERB_SET_Channel_Allocation 0xF79
2072#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2073#define Nv_VERB_SET_Audio_Protection_On 0xF98
2074#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2075
2076#define nvhdmi_master_con_nid_7x 0x04
2077#define nvhdmi_master_pin_nid_7x 0x05
2078
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002079static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002080 /*front, rear, clfe, rear_surr */
2081 0x6, 0x8, 0xa, 0xc,
2082};
2083
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002084static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2085 /* set audio protect on */
2086 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2087 /* enable digital output on pin widget */
2088 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2089 {} /* terminator */
2090};
2091
2092static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002093 /* set audio protect on */
2094 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2095 /* enable digital output on pin widget */
2096 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2097 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2098 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2099 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2100 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2101 {} /* terminator */
2102};
2103
2104#ifdef LIMITED_RATE_FMT_SUPPORT
2105/* support only the safe format and rate */
2106#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2107#define SUPPORTED_MAXBPS 16
2108#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2109#else
2110/* support all rates and formats */
2111#define SUPPORTED_RATES \
2112 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2113 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2114 SNDRV_PCM_RATE_192000)
2115#define SUPPORTED_MAXBPS 24
2116#define SUPPORTED_FORMATS \
2117 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2118#endif
2119
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002120static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002121{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002122 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2123 return 0;
2124}
2125
2126static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2127{
2128 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002129 return 0;
2130}
2131
Nitin Daga393004b2011-01-10 21:49:31 +05302132static unsigned int channels_2_6_8[] = {
2133 2, 6, 8
2134};
2135
2136static unsigned int channels_2_8[] = {
2137 2, 8
2138};
2139
2140static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2141 .count = ARRAY_SIZE(channels_2_6_8),
2142 .list = channels_2_6_8,
2143 .mask = 0,
2144};
2145
2146static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2147 .count = ARRAY_SIZE(channels_2_8),
2148 .list = channels_2_8,
2149 .mask = 0,
2150};
2151
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002152static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2153 struct hda_codec *codec,
2154 struct snd_pcm_substream *substream)
2155{
2156 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302157 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2158
2159 switch (codec->preset->id) {
2160 case 0x10de0002:
2161 case 0x10de0003:
2162 case 0x10de0005:
2163 case 0x10de0006:
2164 hw_constraints_channels = &hw_constraints_2_8_channels;
2165 break;
2166 case 0x10de0007:
2167 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2168 break;
2169 default:
2170 break;
2171 }
2172
2173 if (hw_constraints_channels != NULL) {
2174 snd_pcm_hw_constraint_list(substream->runtime, 0,
2175 SNDRV_PCM_HW_PARAM_CHANNELS,
2176 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002177 } else {
2178 snd_pcm_hw_constraint_step(substream->runtime, 0,
2179 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302180 }
2181
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002182 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2183}
2184
2185static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2186 struct hda_codec *codec,
2187 struct snd_pcm_substream *substream)
2188{
2189 struct hdmi_spec *spec = codec->spec;
2190 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2191}
2192
2193static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2194 struct hda_codec *codec,
2195 unsigned int stream_tag,
2196 unsigned int format,
2197 struct snd_pcm_substream *substream)
2198{
2199 struct hdmi_spec *spec = codec->spec;
2200 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2201 stream_tag, format, substream);
2202}
2203
Takashi Iwaid0b12522012-06-15 14:34:42 +02002204static const struct hda_pcm_stream simple_pcm_playback = {
2205 .substreams = 1,
2206 .channels_min = 2,
2207 .channels_max = 2,
2208 .ops = {
2209 .open = simple_playback_pcm_open,
2210 .close = simple_playback_pcm_close,
2211 .prepare = simple_playback_pcm_prepare
2212 },
2213};
2214
2215static const struct hda_codec_ops simple_hdmi_patch_ops = {
2216 .build_controls = simple_playback_build_controls,
2217 .build_pcms = simple_playback_build_pcms,
2218 .init = simple_playback_init,
2219 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002220 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002221};
2222
2223static int patch_simple_hdmi(struct hda_codec *codec,
2224 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2225{
2226 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002227 struct hdmi_spec_per_cvt *per_cvt;
2228 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002229
2230 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2231 if (!spec)
2232 return -ENOMEM;
2233
2234 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002235 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002236
2237 spec->multiout.num_dacs = 0; /* no analog */
2238 spec->multiout.max_channels = 2;
2239 spec->multiout.dig_out_nid = cvt_nid;
2240 spec->num_cvts = 1;
2241 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002242 per_pin = snd_array_new(&spec->pins);
2243 per_cvt = snd_array_new(&spec->cvts);
2244 if (!per_pin || !per_cvt) {
2245 simple_playback_free(codec);
2246 return -ENOMEM;
2247 }
2248 per_cvt->cvt_nid = cvt_nid;
2249 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002250 spec->pcm_playback = simple_pcm_playback;
2251
2252 codec->patch_ops = simple_hdmi_patch_ops;
2253
2254 return 0;
2255}
2256
Aaron Plattner1f348522011-04-06 17:19:04 -07002257static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2258 int channels)
2259{
2260 unsigned int chanmask;
2261 int chan = channels ? (channels - 1) : 1;
2262
2263 switch (channels) {
2264 default:
2265 case 0:
2266 case 2:
2267 chanmask = 0x00;
2268 break;
2269 case 4:
2270 chanmask = 0x08;
2271 break;
2272 case 6:
2273 chanmask = 0x0b;
2274 break;
2275 case 8:
2276 chanmask = 0x13;
2277 break;
2278 }
2279
2280 /* Set the audio infoframe channel allocation and checksum fields. The
2281 * channel count is computed implicitly by the hardware. */
2282 snd_hda_codec_write(codec, 0x1, 0,
2283 Nv_VERB_SET_Channel_Allocation, chanmask);
2284
2285 snd_hda_codec_write(codec, 0x1, 0,
2286 Nv_VERB_SET_Info_Frame_Checksum,
2287 (0x71 - chan - chanmask));
2288}
2289
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002290static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2291 struct hda_codec *codec,
2292 struct snd_pcm_substream *substream)
2293{
2294 struct hdmi_spec *spec = codec->spec;
2295 int i;
2296
2297 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2298 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2299 for (i = 0; i < 4; i++) {
2300 /* set the stream id */
2301 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2302 AC_VERB_SET_CHANNEL_STREAMID, 0);
2303 /* set the stream format */
2304 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2305 AC_VERB_SET_STREAM_FORMAT, 0);
2306 }
2307
Aaron Plattner1f348522011-04-06 17:19:04 -07002308 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2309 * streams are disabled. */
2310 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2311
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002312 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2313}
2314
2315static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2316 struct hda_codec *codec,
2317 unsigned int stream_tag,
2318 unsigned int format,
2319 struct snd_pcm_substream *substream)
2320{
2321 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002322 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002323 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002324 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002325 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002326 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002327
2328 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002329 per_cvt = get_cvt(spec, 0);
2330 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002331
2332 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002333
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002334 dataDCC2 = 0x2;
2335
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002336 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002337 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002338 snd_hda_codec_write(codec,
2339 nvhdmi_master_con_nid_7x,
2340 0,
2341 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002342 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002343
2344 /* set the stream id */
2345 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2346 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2347
2348 /* set the stream format */
2349 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2350 AC_VERB_SET_STREAM_FORMAT, format);
2351
2352 /* turn on again (if needed) */
2353 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002354 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002355 snd_hda_codec_write(codec,
2356 nvhdmi_master_con_nid_7x,
2357 0,
2358 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002359 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002360 snd_hda_codec_write(codec,
2361 nvhdmi_master_con_nid_7x,
2362 0,
2363 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2364 }
2365
2366 for (i = 0; i < 4; i++) {
2367 if (chs == 2)
2368 channel_id = 0;
2369 else
2370 channel_id = i * 2;
2371
2372 /* turn off SPDIF once;
2373 *otherwise the IEC958 bits won't be updated
2374 */
2375 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002376 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002377 snd_hda_codec_write(codec,
2378 nvhdmi_con_nids_7x[i],
2379 0,
2380 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002381 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002382 /* set the stream id */
2383 snd_hda_codec_write(codec,
2384 nvhdmi_con_nids_7x[i],
2385 0,
2386 AC_VERB_SET_CHANNEL_STREAMID,
2387 (stream_tag << 4) | channel_id);
2388 /* set the stream format */
2389 snd_hda_codec_write(codec,
2390 nvhdmi_con_nids_7x[i],
2391 0,
2392 AC_VERB_SET_STREAM_FORMAT,
2393 format);
2394 /* turn on again (if needed) */
2395 /* enable and set the channel status audio/data flag */
2396 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002397 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002398 snd_hda_codec_write(codec,
2399 nvhdmi_con_nids_7x[i],
2400 0,
2401 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002402 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002403 snd_hda_codec_write(codec,
2404 nvhdmi_con_nids_7x[i],
2405 0,
2406 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2407 }
2408 }
2409
Aaron Plattner1f348522011-04-06 17:19:04 -07002410 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002411
2412 mutex_unlock(&codec->spdif_mutex);
2413 return 0;
2414}
2415
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002416static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002417 .substreams = 1,
2418 .channels_min = 2,
2419 .channels_max = 8,
2420 .nid = nvhdmi_master_con_nid_7x,
2421 .rates = SUPPORTED_RATES,
2422 .maxbps = SUPPORTED_MAXBPS,
2423 .formats = SUPPORTED_FORMATS,
2424 .ops = {
2425 .open = simple_playback_pcm_open,
2426 .close = nvhdmi_8ch_7x_pcm_close,
2427 .prepare = nvhdmi_8ch_7x_pcm_prepare
2428 },
2429};
2430
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002431static int patch_nvhdmi_2ch(struct hda_codec *codec)
2432{
2433 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002434 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2435 nvhdmi_master_pin_nid_7x);
2436 if (err < 0)
2437 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002438
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002439 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002440 /* override the PCM rates, etc, as the codec doesn't give full list */
2441 spec = codec->spec;
2442 spec->pcm_playback.rates = SUPPORTED_RATES;
2443 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2444 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002445 return 0;
2446}
2447
Takashi Iwai53775b02012-08-01 12:17:41 +02002448static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2449{
2450 struct hdmi_spec *spec = codec->spec;
2451 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002452 if (!err) {
2453 struct hda_pcm *info = get_pcm_rec(spec, 0);
2454 info->own_chmap = true;
2455 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002456 return err;
2457}
2458
2459static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2460{
2461 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002462 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002463 struct snd_pcm_chmap *chmap;
2464 int err;
2465
2466 err = simple_playback_build_controls(codec);
2467 if (err < 0)
2468 return err;
2469
2470 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002471 info = get_pcm_rec(spec, 0);
2472 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002473 SNDRV_PCM_STREAM_PLAYBACK,
2474 snd_pcm_alt_chmaps, 8, 0, &chmap);
2475 if (err < 0)
2476 return err;
2477 switch (codec->preset->id) {
2478 case 0x10de0002:
2479 case 0x10de0003:
2480 case 0x10de0005:
2481 case 0x10de0006:
2482 chmap->channel_mask = (1U << 2) | (1U << 8);
2483 break;
2484 case 0x10de0007:
2485 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2486 }
2487 return 0;
2488}
2489
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002490static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2491{
2492 struct hdmi_spec *spec;
2493 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002494 if (err < 0)
2495 return err;
2496 spec = codec->spec;
2497 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002498 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002499 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002500 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2501 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002502
2503 /* Initialize the audio infoframe channel mask and checksum to something
2504 * valid */
2505 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2506
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002507 return 0;
2508}
2509
Anssi Hannulac6a3cab2014-02-03 16:54:03 -07002510static int patch_nvhdmi(struct hda_codec *codec)
2511{
2512 struct hdmi_spec *spec;
2513 int err;
2514
2515 err = patch_generic_hdmi(codec);
2516 if (err)
2517 return err;
2518
2519 spec = codec->spec;
Stephen Warren2b1461b2014-02-03 16:54:04 -07002520 spec->dyn_pin_out = true;
Anssi Hannulac6a3cab2014-02-03 16:54:03 -07002521
2522 return 0;
2523}
2524
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002525/*
2526 * ATI-specific implementations
2527 *
2528 * FIXME: we may omit the whole this and use the generic code once after
2529 * it's confirmed to work.
2530 */
2531
2532#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2533#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2534
2535static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2536 struct hda_codec *codec,
2537 unsigned int stream_tag,
2538 unsigned int format,
2539 struct snd_pcm_substream *substream)
2540{
2541 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002542 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002543 int chans = substream->runtime->channels;
2544 int i, err;
2545
2546 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2547 substream);
2548 if (err < 0)
2549 return err;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002550 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06002551 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002552 /* FIXME: XXX */
2553 for (i = 0; i < chans; i++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002554 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002555 AC_VERB_SET_HDMI_CHAN_SLOT,
2556 (i << 4) | i);
2557 }
2558 return 0;
2559}
2560
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002561static int patch_atihdmi(struct hda_codec *codec)
2562{
2563 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002564 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2565 if (err < 0)
2566 return err;
2567 spec = codec->spec;
2568 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002569 return 0;
2570}
2571
Annie Liu3de5ff82012-06-08 19:18:42 +08002572/* VIA HDMI Implementation */
2573#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2574#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2575
Annie Liu3de5ff82012-06-08 19:18:42 +08002576static int patch_via_hdmi(struct hda_codec *codec)
2577{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002578 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002579}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002580
2581/*
2582 * patch entries
2583 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002584static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002585{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2586{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2587{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002588{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002589{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2590{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2591{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2592{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2593{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2594{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2595{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2596{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Anssi Hannulac6a3cab2014-02-03 16:54:03 -07002597{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_nvhdmi },
2598{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_nvhdmi },
2599{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_nvhdmi },
2600{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_nvhdmi },
2601{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_nvhdmi },
2602{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_nvhdmi },
2603{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_nvhdmi },
2604{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_nvhdmi },
2605{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_nvhdmi },
2606{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_nvhdmi },
2607{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_nvhdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002608/* 17 is known to be absent */
Anssi Hannulac6a3cab2014-02-03 16:54:03 -07002609{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_nvhdmi },
2610{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_nvhdmi },
2611{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_nvhdmi },
2612{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_nvhdmi },
2613{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_nvhdmi },
2614{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_nvhdmi },
2615{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_nvhdmi },
2616{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_nvhdmi },
2617{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_nvhdmi },
2618{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_nvhdmi },
2619{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_nvhdmi },
2620{ .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_nvhdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002621{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2622{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002623{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2624{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2625{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2626{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002627{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2628{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2629{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2630{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2631{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2632{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002633{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002634{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002635{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002636{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2637{} /* terminator */
2638};
2639
2640MODULE_ALIAS("snd-hda-codec-id:1002793c");
2641MODULE_ALIAS("snd-hda-codec-id:10027919");
2642MODULE_ALIAS("snd-hda-codec-id:1002791a");
2643MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2644MODULE_ALIAS("snd-hda-codec-id:10951390");
2645MODULE_ALIAS("snd-hda-codec-id:10951392");
2646MODULE_ALIAS("snd-hda-codec-id:10de0002");
2647MODULE_ALIAS("snd-hda-codec-id:10de0003");
2648MODULE_ALIAS("snd-hda-codec-id:10de0005");
2649MODULE_ALIAS("snd-hda-codec-id:10de0006");
2650MODULE_ALIAS("snd-hda-codec-id:10de0007");
2651MODULE_ALIAS("snd-hda-codec-id:10de000a");
2652MODULE_ALIAS("snd-hda-codec-id:10de000b");
2653MODULE_ALIAS("snd-hda-codec-id:10de000c");
2654MODULE_ALIAS("snd-hda-codec-id:10de000d");
2655MODULE_ALIAS("snd-hda-codec-id:10de0010");
2656MODULE_ALIAS("snd-hda-codec-id:10de0011");
2657MODULE_ALIAS("snd-hda-codec-id:10de0012");
2658MODULE_ALIAS("snd-hda-codec-id:10de0013");
2659MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002660MODULE_ALIAS("snd-hda-codec-id:10de0015");
2661MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002662MODULE_ALIAS("snd-hda-codec-id:10de0018");
2663MODULE_ALIAS("snd-hda-codec-id:10de0019");
2664MODULE_ALIAS("snd-hda-codec-id:10de001a");
2665MODULE_ALIAS("snd-hda-codec-id:10de001b");
2666MODULE_ALIAS("snd-hda-codec-id:10de001c");
2667MODULE_ALIAS("snd-hda-codec-id:10de0040");
2668MODULE_ALIAS("snd-hda-codec-id:10de0041");
2669MODULE_ALIAS("snd-hda-codec-id:10de0042");
2670MODULE_ALIAS("snd-hda-codec-id:10de0043");
2671MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002672MODULE_ALIAS("snd-hda-codec-id:10de0051");
Aaron Plattner782e9ca2013-07-12 11:01:37 -07002673MODULE_ALIAS("snd-hda-codec-id:10de0060");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002674MODULE_ALIAS("snd-hda-codec-id:10de0067");
2675MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002676MODULE_ALIAS("snd-hda-codec-id:11069f80");
2677MODULE_ALIAS("snd-hda-codec-id:11069f81");
2678MODULE_ALIAS("snd-hda-codec-id:11069f84");
2679MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002680MODULE_ALIAS("snd-hda-codec-id:17e80047");
2681MODULE_ALIAS("snd-hda-codec-id:80860054");
2682MODULE_ALIAS("snd-hda-codec-id:80862801");
2683MODULE_ALIAS("snd-hda-codec-id:80862802");
2684MODULE_ALIAS("snd-hda-codec-id:80862803");
2685MODULE_ALIAS("snd-hda-codec-id:80862804");
2686MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002687MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002688MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002689MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002690MODULE_ALIAS("snd-hda-codec-id:808629fb");
2691
2692MODULE_LICENSE("GPL");
2693MODULE_DESCRIPTION("HDMI HD-audio codec");
2694MODULE_ALIAS("snd-hda-codec-intelhdmi");
2695MODULE_ALIAS("snd-hda-codec-nvhdmi");
2696MODULE_ALIAS("snd-hda-codec-atihdmi");
2697
2698static struct hda_codec_preset_list intel_list = {
2699 .preset = snd_hda_preset_hdmi,
2700 .owner = THIS_MODULE,
2701};
2702
2703static int __init patch_hdmi_init(void)
2704{
2705 return snd_hda_add_codec_preset(&intel_list);
2706}
2707
2708static void __exit patch_hdmi_exit(void)
2709{
2710 snd_hda_delete_codec_preset(&intel_list);
2711}
2712
2713module_init(patch_hdmi_init)
2714module_exit(patch_hdmi_exit)