blob: 0687d536b56315af60b0f7d589097a59f31f0d05 [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 Iwai1a6003b2012-09-06 17:42:08 +020070 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020071 bool chmap_set; /* channel-map override by ALSA API? */
72 unsigned char chmap[8]; /* ALSA API channel-map */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010073 char pcm_name[8]; /* filled in build_pcm callbacks */
Stephen Warren384a48d2011-06-01 11:14:21 -060074};
75
Wu Fengguang079d88c2010-03-08 10:44:23 +080076struct hdmi_spec {
77 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010078 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
79 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -060080
Wu Fengguang079d88c2010-03-08 10:44:23 +080081 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010082 struct snd_array pins; /* struct hdmi_spec_per_pin */
83 struct snd_array pcm_rec; /* struct hda_pcm */
Takashi Iwaid45e6882012-07-31 11:36:00 +020084 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080085
David Henningsson4bd038f2013-02-19 16:11:25 +010086 struct hdmi_eld temp_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +080087 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060088 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080089 */
90 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020091 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080092};
93
94
95struct hdmi_audio_infoframe {
96 u8 type; /* 0x84 */
97 u8 ver; /* 0x01 */
98 u8 len; /* 0x0a */
99
Wu Fengguang53d7d692010-09-21 14:25:49 +0800100 u8 checksum;
101
Wu Fengguang079d88c2010-03-08 10:44:23 +0800102 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
103 u8 SS01_SF24;
104 u8 CXT04;
105 u8 CA;
106 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800107};
108
109struct dp_audio_infoframe {
110 u8 type; /* 0x84 */
111 u8 len; /* 0x1b */
112 u8 ver; /* 0x11 << 2 */
113
114 u8 CC02_CT47; /* match with HDMI infoframe from this on */
115 u8 SS01_SF24;
116 u8 CXT04;
117 u8 CA;
118 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800119};
120
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100121union audio_infoframe {
122 struct hdmi_audio_infoframe hdmi;
123 struct dp_audio_infoframe dp;
124 u8 bytes[0];
125};
126
Wu Fengguang079d88c2010-03-08 10:44:23 +0800127/*
128 * CEA speaker placement:
129 *
130 * FLH FCH FRH
131 * FLW FL FLC FC FRC FR FRW
132 *
133 * LFE
134 * TC
135 *
136 * RL RLC RC RRC RR
137 *
138 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
139 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
140 */
141enum cea_speaker_placement {
142 FL = (1 << 0), /* Front Left */
143 FC = (1 << 1), /* Front Center */
144 FR = (1 << 2), /* Front Right */
145 FLC = (1 << 3), /* Front Left Center */
146 FRC = (1 << 4), /* Front Right Center */
147 RL = (1 << 5), /* Rear Left */
148 RC = (1 << 6), /* Rear Center */
149 RR = (1 << 7), /* Rear Right */
150 RLC = (1 << 8), /* Rear Left Center */
151 RRC = (1 << 9), /* Rear Right Center */
152 LFE = (1 << 10), /* Low Frequency Effect */
153 FLW = (1 << 11), /* Front Left Wide */
154 FRW = (1 << 12), /* Front Right Wide */
155 FLH = (1 << 13), /* Front Left High */
156 FCH = (1 << 14), /* Front Center High */
157 FRH = (1 << 15), /* Front Right High */
158 TC = (1 << 16), /* Top Center */
159};
160
161/*
162 * ELD SA bits in the CEA Speaker Allocation data block
163 */
164static int eld_speaker_allocation_bits[] = {
165 [0] = FL | FR,
166 [1] = LFE,
167 [2] = FC,
168 [3] = RL | RR,
169 [4] = RC,
170 [5] = FLC | FRC,
171 [6] = RLC | RRC,
172 /* the following are not defined in ELD yet */
173 [7] = FLW | FRW,
174 [8] = FLH | FRH,
175 [9] = TC,
176 [10] = FCH,
177};
178
179struct cea_channel_speaker_allocation {
180 int ca_index;
181 int speakers[8];
182
183 /* derived values, just for convenience */
184 int channels;
185 int spk_mask;
186};
187
188/*
189 * ALSA sequence is:
190 *
191 * surround40 surround41 surround50 surround51 surround71
192 * ch0 front left = = = =
193 * ch1 front right = = = =
194 * ch2 rear left = = = =
195 * ch3 rear right = = = =
196 * ch4 LFE center center center
197 * ch5 LFE LFE
198 * ch6 side left
199 * ch7 side right
200 *
201 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
202 */
203static int hdmi_channel_mapping[0x32][8] = {
204 /* stereo */
205 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
206 /* 2.1 */
207 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
208 /* Dolby Surround */
209 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
210 /* surround40 */
211 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
212 /* 4ch */
213 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
214 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800215 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800216 /* surround50 */
217 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
218 /* surround51 */
219 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
220 /* 7.1 */
221 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
222};
223
224/*
225 * This is an ordered list!
226 *
227 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800228 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800229 */
230static struct cea_channel_speaker_allocation channel_allocations[] = {
231/* channel: 7 6 5 4 3 2 1 0 */
232{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
233 /* 2.1 */
234{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
235 /* Dolby Surround */
236{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
237 /* surround40 */
238{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
239 /* surround41 */
240{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
241 /* surround50 */
242{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
243 /* surround51 */
244{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
245 /* 6.1 */
246{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
247 /* surround71 */
248{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
249
250{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
251{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
252{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
253{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
254{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
255{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
256{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
257{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
258{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
259{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
260{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
261{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
262{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
263{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
264{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
265{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
266{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
267{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
268{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
269{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
270{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
271{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
272{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
273{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
274{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
275{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
276{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
277{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
278{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
279{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
280{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
281{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
282{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
283{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
284{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
285{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
286{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
287{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
288{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
289{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
290{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
291};
292
293
294/*
295 * HDMI routines
296 */
297
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100298#define get_pin(spec, idx) \
299 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
300#define get_cvt(spec, idx) \
301 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
302#define get_pcm_rec(spec, idx) \
303 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
304
Stephen Warren384a48d2011-06-01 11:14:21 -0600305static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800306{
Stephen Warren384a48d2011-06-01 11:14:21 -0600307 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800308
Stephen Warren384a48d2011-06-01 11:14:21 -0600309 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100310 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600311 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800312
Stephen Warren384a48d2011-06-01 11:14:21 -0600313 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
314 return -EINVAL;
315}
316
317static int hinfo_to_pin_index(struct hdmi_spec *spec,
318 struct hda_pcm_stream *hinfo)
319{
320 int pin_idx;
321
322 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100323 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600324 return pin_idx;
325
326 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
327 return -EINVAL;
328}
329
330static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
331{
332 int cvt_idx;
333
334 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100335 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600336 return cvt_idx;
337
338 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800339 return -EINVAL;
340}
341
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500342static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
343 struct snd_ctl_elem_info *uinfo)
344{
345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100346 struct hdmi_spec *spec = codec->spec;
347 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500348 int pin_idx;
349
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500350 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
351
352 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100353 eld = &get_pin(spec, pin_idx)->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100354
David Henningsson4bd038f2013-02-19 16:11:25 +0100355 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100356 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
David Henningsson4bd038f2013-02-19 16:11:25 +0100357 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500358
359 return 0;
360}
361
362static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
363 struct snd_ctl_elem_value *ucontrol)
364{
365 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100366 struct hdmi_spec *spec = codec->spec;
367 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500368 int pin_idx;
369
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500370 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100371 eld = &get_pin(spec, pin_idx)->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500372
David Henningsson4bd038f2013-02-19 16:11:25 +0100373 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100374 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
David Henningsson4bd038f2013-02-19 16:11:25 +0100375 mutex_unlock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100376 snd_BUG();
377 return -EINVAL;
378 }
379
380 memset(ucontrol->value.bytes.data, 0,
381 ARRAY_SIZE(ucontrol->value.bytes.data));
382 if (eld->eld_valid)
383 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
384 eld->eld_size);
David Henningsson4bd038f2013-02-19 16:11:25 +0100385 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500386
387 return 0;
388}
389
390static struct snd_kcontrol_new eld_bytes_ctl = {
391 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
392 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
393 .name = "ELD",
394 .info = hdmi_eld_ctl_info,
395 .get = hdmi_eld_ctl_get,
396};
397
398static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
399 int device)
400{
401 struct snd_kcontrol *kctl;
402 struct hdmi_spec *spec = codec->spec;
403 int err;
404
405 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
406 if (!kctl)
407 return -ENOMEM;
408 kctl->private_value = pin_idx;
409 kctl->id.device = device;
410
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100411 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500412 if (err < 0)
413 return err;
414
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100415 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500416 return 0;
417}
418
Wu Fengguang079d88c2010-03-08 10:44:23 +0800419#ifdef BE_PARANOID
420static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
421 int *packet_index, int *byte_index)
422{
423 int val;
424
425 val = snd_hda_codec_read(codec, pin_nid, 0,
426 AC_VERB_GET_HDMI_DIP_INDEX, 0);
427
428 *packet_index = val >> 5;
429 *byte_index = val & 0x1f;
430}
431#endif
432
433static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
434 int packet_index, int byte_index)
435{
436 int val;
437
438 val = (packet_index << 5) | (byte_index & 0x1f);
439
440 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
441}
442
443static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
444 unsigned char val)
445{
446 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
447}
448
Stephen Warren384a48d2011-06-01 11:14:21 -0600449static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800450{
451 /* Unmute */
452 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
453 snd_hda_codec_write(codec, pin_nid, 0,
454 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100455 /* Enable pin out: some machines with GM965 gets broken output when
456 * the pin is disabled or changed while using with HDMI
457 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800458 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100459 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800460}
461
Stephen Warren384a48d2011-06-01 11:14:21 -0600462static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800463{
Stephen Warren384a48d2011-06-01 11:14:21 -0600464 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800465 AC_VERB_GET_CVT_CHAN_COUNT, 0);
466}
467
468static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600469 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800470{
Stephen Warren384a48d2011-06-01 11:14:21 -0600471 if (chs != hdmi_get_channel_count(codec, cvt_nid))
472 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800473 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
474}
475
476
477/*
478 * Channel mapping routines
479 */
480
481/*
482 * Compute derived values in channel_allocations[].
483 */
484static void init_channel_allocations(void)
485{
486 int i, j;
487 struct cea_channel_speaker_allocation *p;
488
489 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
490 p = channel_allocations + i;
491 p->channels = 0;
492 p->spk_mask = 0;
493 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
494 if (p->speakers[j]) {
495 p->channels++;
496 p->spk_mask |= p->speakers[j];
497 }
498 }
499}
500
Wang Xingchao72357c72012-09-06 10:02:36 +0800501static int get_channel_allocation_order(int ca)
502{
503 int i;
504
505 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
506 if (channel_allocations[i].ca_index == ca)
507 break;
508 }
509 return i;
510}
511
Wu Fengguang079d88c2010-03-08 10:44:23 +0800512/*
513 * The transformation takes two steps:
514 *
515 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
516 * spk_mask => (channel_allocations[]) => ai->CA
517 *
518 * TODO: it could select the wrong CA from multiple candidates.
519*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600520static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800521{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800522 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800523 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800524 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800525 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
526
527 /*
528 * CA defaults to 0 for basic stereo audio
529 */
530 if (channels <= 2)
531 return 0;
532
Wu Fengguang079d88c2010-03-08 10:44:23 +0800533 /*
534 * expand ELD's speaker allocation mask
535 *
536 * ELD tells the speaker mask in a compact(paired) form,
537 * expand ELD's notions to match the ones used by Audio InfoFrame.
538 */
539 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100540 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800541 spk_mask |= eld_speaker_allocation_bits[i];
542 }
543
544 /* search for the first working match in the CA table */
545 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
546 if (channels == channel_allocations[i].channels &&
547 (spk_mask & channel_allocations[i].spk_mask) ==
548 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800549 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800550 break;
551 }
552 }
553
David Henningsson1613d6b2013-02-19 16:11:24 +0100554 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800555 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800556 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800557
Wu Fengguang53d7d692010-09-21 14:25:49 +0800558 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800559}
560
561static void hdmi_debug_channel_mapping(struct hda_codec *codec,
562 hda_nid_t pin_nid)
563{
564#ifdef CONFIG_SND_DEBUG_VERBOSE
565 int i;
566 int slot;
567
568 for (i = 0; i < 8; i++) {
569 slot = snd_hda_codec_read(codec, pin_nid, 0,
570 AC_VERB_GET_HDMI_CHAN_SLOT, i);
571 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
572 slot >> 4, slot & 0xf);
573 }
574#endif
575}
576
577
Takashi Iwaid45e6882012-07-31 11:36:00 +0200578static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800579 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800580 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800581 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800582{
583 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800584 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800585 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800586 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800587
Wang Xingchao72357c72012-09-06 10:02:36 +0800588 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800589
Wu Fengguang079d88c2010-03-08 10:44:23 +0800590 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800591 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800592 hdmi_channel_mapping[ca][i] = i | (i << 4);
593 for (; i < 8; i++)
594 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
595 }
596
Wang Xingchao433968d2012-09-06 10:02:37 +0800597 if (non_pcm) {
598 for (i = 0; i < channel_allocations[order].channels; i++)
599 non_pcm_mapping[i] = i | (i << 4);
600 for (; i < 8; i++)
601 non_pcm_mapping[i] = 0xf | (i << 4);
602 }
603
Wu Fengguang079d88c2010-03-08 10:44:23 +0800604 for (i = 0; i < 8; i++) {
605 err = snd_hda_codec_write(codec, pin_nid, 0,
606 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800607 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800608 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800609 snd_printdd(KERN_NOTICE
610 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800611 break;
612 }
613 }
614
615 hdmi_debug_channel_mapping(codec, pin_nid);
616}
617
Takashi Iwaid45e6882012-07-31 11:36:00 +0200618struct channel_map_table {
619 unsigned char map; /* ALSA API channel map position */
620 unsigned char cea_slot; /* CEA slot value */
621 int spk_mask; /* speaker position bit mask */
622};
623
624static struct channel_map_table map_tables[] = {
625 { SNDRV_CHMAP_FL, 0x00, FL },
626 { SNDRV_CHMAP_FR, 0x01, FR },
627 { SNDRV_CHMAP_RL, 0x04, RL },
628 { SNDRV_CHMAP_RR, 0x05, RR },
629 { SNDRV_CHMAP_LFE, 0x02, LFE },
630 { SNDRV_CHMAP_FC, 0x03, FC },
631 { SNDRV_CHMAP_RLC, 0x06, RLC },
632 { SNDRV_CHMAP_RRC, 0x07, RRC },
633 {} /* terminator */
634};
635
636/* from ALSA API channel position to speaker bit mask */
637static int to_spk_mask(unsigned char c)
638{
639 struct channel_map_table *t = map_tables;
640 for (; t->map; t++) {
641 if (t->map == c)
642 return t->spk_mask;
643 }
644 return 0;
645}
646
647/* from ALSA API channel position to CEA slot */
648static int to_cea_slot(unsigned char c)
649{
650 struct channel_map_table *t = map_tables;
651 for (; t->map; t++) {
652 if (t->map == c)
653 return t->cea_slot;
654 }
655 return 0x0f;
656}
657
658/* from CEA slot to ALSA API channel position */
659static int from_cea_slot(unsigned char c)
660{
661 struct channel_map_table *t = map_tables;
662 for (; t->map; t++) {
663 if (t->cea_slot == c)
664 return t->map;
665 }
666 return 0;
667}
668
669/* from speaker bit mask to ALSA API channel position */
670static int spk_to_chmap(int spk)
671{
672 struct channel_map_table *t = map_tables;
673 for (; t->map; t++) {
674 if (t->spk_mask == spk)
675 return t->map;
676 }
677 return 0;
678}
679
680/* get the CA index corresponding to the given ALSA API channel map */
681static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
682{
683 int i, spks = 0, spk_mask = 0;
684
685 for (i = 0; i < chs; i++) {
686 int mask = to_spk_mask(map[i]);
687 if (mask) {
688 spk_mask |= mask;
689 spks++;
690 }
691 }
692
693 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
694 if ((chs == channel_allocations[i].channels ||
695 spks == channel_allocations[i].channels) &&
696 (spk_mask & channel_allocations[i].spk_mask) ==
697 channel_allocations[i].spk_mask)
698 return channel_allocations[i].ca_index;
699 }
700 return -1;
701}
702
703/* set up the channel slots for the given ALSA API channel map */
704static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
705 hda_nid_t pin_nid,
706 int chs, unsigned char *map)
707{
708 int i;
709 for (i = 0; i < 8; i++) {
710 int val, err;
711 if (i < chs)
712 val = to_cea_slot(map[i]);
713 else
714 val = 0xf;
715 val |= (i << 4);
716 err = snd_hda_codec_write(codec, pin_nid, 0,
717 AC_VERB_SET_HDMI_CHAN_SLOT, val);
718 if (err)
719 return -EINVAL;
720 }
721 return 0;
722}
723
724/* store ALSA API channel map from the current default map */
725static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
726{
727 int i;
728 for (i = 0; i < 8; i++) {
729 if (i < channel_allocations[ca].channels)
730 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
731 else
732 map[i] = 0;
733 }
734}
735
736static void hdmi_setup_channel_mapping(struct hda_codec *codec,
737 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200738 int channels, unsigned char *map,
739 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200740{
Anssi Hannula20608732013-02-03 17:55:45 +0200741 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200742 hdmi_manual_setup_channel_mapping(codec, pin_nid,
743 channels, map);
744 } else {
745 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
746 hdmi_setup_fake_chmap(map, ca);
747 }
748}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800749
750/*
751 * Audio InfoFrame routines
752 */
753
754/*
755 * Enable Audio InfoFrame Transmission
756 */
757static void hdmi_start_infoframe_trans(struct hda_codec *codec,
758 hda_nid_t pin_nid)
759{
760 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
761 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
762 AC_DIPXMIT_BEST);
763}
764
765/*
766 * Disable Audio InfoFrame Transmission
767 */
768static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
769 hda_nid_t pin_nid)
770{
771 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
772 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
773 AC_DIPXMIT_DISABLE);
774}
775
776static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
777{
778#ifdef CONFIG_SND_DEBUG_VERBOSE
779 int i;
780 int size;
781
782 size = snd_hdmi_get_eld_size(codec, pin_nid);
783 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
784
785 for (i = 0; i < 8; i++) {
786 size = snd_hda_codec_read(codec, pin_nid, 0,
787 AC_VERB_GET_HDMI_DIP_SIZE, i);
788 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
789 }
790#endif
791}
792
793static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
794{
795#ifdef BE_PARANOID
796 int i, j;
797 int size;
798 int pi, bi;
799 for (i = 0; i < 8; i++) {
800 size = snd_hda_codec_read(codec, pin_nid, 0,
801 AC_VERB_GET_HDMI_DIP_SIZE, i);
802 if (size == 0)
803 continue;
804
805 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
806 for (j = 1; j < 1000; j++) {
807 hdmi_write_dip_byte(codec, pin_nid, 0x0);
808 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
809 if (pi != i)
810 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
811 bi, pi, i);
812 if (bi == 0) /* byte index wrapped around */
813 break;
814 }
815 snd_printd(KERN_INFO
816 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
817 i, size, j);
818 }
819#endif
820}
821
Wu Fengguang53d7d692010-09-21 14:25:49 +0800822static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800823{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800824 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800825 u8 sum = 0;
826 int i;
827
Wu Fengguang53d7d692010-09-21 14:25:49 +0800828 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800829
Wu Fengguang53d7d692010-09-21 14:25:49 +0800830 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800831 sum += bytes[i];
832
Wu Fengguang53d7d692010-09-21 14:25:49 +0800833 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800834}
835
836static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
837 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800838 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800839{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800840 int i;
841
842 hdmi_debug_dip_size(codec, pin_nid);
843 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
844
Wu Fengguang079d88c2010-03-08 10:44:23 +0800845 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800846 for (i = 0; i < size; i++)
847 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800848}
849
850static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800851 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800852{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800853 u8 val;
854 int i;
855
856 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
857 != AC_DIPXMIT_BEST)
858 return false;
859
860 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800861 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800862 val = snd_hda_codec_read(codec, pin_nid, 0,
863 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800864 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800865 return false;
866 }
867
868 return true;
869}
870
Stephen Warren384a48d2011-06-01 11:14:21 -0600871static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200872 bool non_pcm,
873 struct snd_pcm_substream *substream)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800874{
875 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100876 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -0600877 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800878 int channels = substream->runtime->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600879 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800880 int ca;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100881 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800882
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100883 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -0600884 if (!eld->monitor_present)
885 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800886
Takashi Iwaid45e6882012-07-31 11:36:00 +0200887 if (!non_pcm && per_pin->chmap_set)
888 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
889 else
890 ca = hdmi_channel_allocation(eld, channels);
891 if (ca < 0)
892 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800893
Stephen Warren384a48d2011-06-01 11:14:21 -0600894 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b2013-02-19 16:11:24 +0100895 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600896 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800897
Stephen Warren384a48d2011-06-01 11:14:21 -0600898 hdmi_ai->type = 0x84;
899 hdmi_ai->ver = 0x01;
900 hdmi_ai->len = 0x0a;
901 hdmi_ai->CC02_CT47 = channels - 1;
902 hdmi_ai->CA = ca;
903 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b2013-02-19 16:11:24 +0100904 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600905 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800906
Stephen Warren384a48d2011-06-01 11:14:21 -0600907 dp_ai->type = 0x84;
908 dp_ai->len = 0x1b;
909 dp_ai->ver = 0x11 << 2;
910 dp_ai->CC02_CT47 = channels - 1;
911 dp_ai->CA = ca;
912 } else {
913 snd_printd("HDMI: unknown connection type at pin %d\n",
914 pin_nid);
915 return;
916 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800917
Stephen Warren384a48d2011-06-01 11:14:21 -0600918 /*
919 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
920 * sizeof(*dp_ai) to avoid partial match/update problems when
921 * the user switches between HDMI/DP monitors.
922 */
923 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
924 sizeof(ai))) {
925 snd_printdd("hdmi_setup_audio_infoframe: "
926 "pin=%d channels=%d\n",
927 pin_nid,
928 channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200929 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200930 channels, per_pin->chmap,
931 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600932 hdmi_stop_infoframe_trans(codec, pin_nid);
933 hdmi_fill_audio_infoframe(codec, pin_nid,
934 ai.bytes, sizeof(ai));
935 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800936 } else {
937 /* For non-pcm audio switch, setup new channel mapping
938 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200939 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200940 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200941 channels, per_pin->chmap,
942 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800943 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800944
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200945 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800946}
947
948
949/*
950 * Unsolicited events
951 */
952
Wu Fengguangc6e84532011-11-18 16:59:32 -0600953static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200954
Wu Fengguang079d88c2010-03-08 10:44:23 +0800955static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
956{
957 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200958 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
959 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600960 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200961 struct hda_jack_tbl *jack;
962
963 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
964 if (!jack)
965 return;
966 pin_nid = jack->nid;
967 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800968
Fengguang Wufae3d882012-04-10 17:00:35 +0800969 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -0600970 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Fengguang Wufae3d882012-04-10 17:00:35 +0800971 codec->addr, pin_nid,
972 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800973
Stephen Warren384a48d2011-06-01 11:14:21 -0600974 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
975 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800976 return;
977
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100978 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200979 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800980}
981
982static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
983{
984 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
985 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
986 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
987 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
988
989 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200990 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600991 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800992 tag,
993 subtag,
994 cp_state,
995 cp_ready);
996
997 /* TODO */
998 if (cp_state)
999 ;
1000 if (cp_ready)
1001 ;
1002}
1003
1004
1005static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1006{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001007 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1008 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1009
Takashi Iwai3a938972011-10-28 01:16:55 +02001010 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001011 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1012 return;
1013 }
1014
1015 if (subtag == 0)
1016 hdmi_intrinsic_event(codec, res);
1017 else
1018 hdmi_non_intrinsic_event(codec, res);
1019}
1020
Wang Xingchao53b434f2013-06-18 10:41:53 +08001021static void haswell_verify_pin_D0(struct hda_codec *codec,
1022 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001023{
1024 int pwr, lamp, ramp;
1025
Wang Xingchao53b434f2013-06-18 10:41:53 +08001026 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1027 * thus pins could only choose converter 0 for use. Make sure the
1028 * converters are in correct power state */
1029 pwr = snd_hda_codec_read(codec, cvt_nid, 0, AC_VERB_GET_POWER_STATE, 0);
1030 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1031 if (pwr != AC_PWRST_D0)
1032 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1033
David Henningsson83f26ad2013-04-10 12:26:07 +02001034 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1035 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1036 if (pwr != AC_PWRST_D0) {
1037 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1038 AC_PWRST_D0);
1039 msleep(40);
1040 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1041 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1042 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1043 }
1044
1045 lamp = snd_hda_codec_read(codec, nid, 0,
1046 AC_VERB_GET_AMP_GAIN_MUTE,
1047 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1048 ramp = snd_hda_codec_read(codec, nid, 0,
1049 AC_VERB_GET_AMP_GAIN_MUTE,
1050 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1051 if (lamp != ramp) {
1052 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1053 AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
1054
1055 lamp = snd_hda_codec_read(codec, nid, 0,
1056 AC_VERB_GET_AMP_GAIN_MUTE,
1057 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1058 ramp = snd_hda_codec_read(codec, nid, 0,
1059 AC_VERB_GET_AMP_GAIN_MUTE,
1060 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1061 snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
1062 }
1063}
1064
Wu Fengguang079d88c2010-03-08 10:44:23 +08001065/*
1066 * Callbacks
1067 */
1068
Takashi Iwai92f10b32010-08-03 14:21:00 +02001069/* HBR should be Non-PCM, 8 channels */
1070#define is_hbr_format(format) \
1071 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1072
Stephen Warren384a48d2011-06-01 11:14:21 -06001073static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1074 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001075{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001076 int pinctl;
1077 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001078
David Henningsson83f26ad2013-04-10 12:26:07 +02001079 if (codec->vendor_id == 0x80862807)
Wang Xingchao53b434f2013-06-18 10:41:53 +08001080 haswell_verify_pin_D0(codec, cvt_nid, pin_nid);
David Henningsson83f26ad2013-04-10 12:26:07 +02001081
Stephen Warren384a48d2011-06-01 11:14:21 -06001082 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1083 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001084 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1085
1086 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001087 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001088 new_pinctl |= AC_PINCTL_EPT_HBR;
1089 else
1090 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1091
1092 snd_printdd("hdmi_setup_stream: "
1093 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001094 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001095 pinctl == new_pinctl ? "" : "new-",
1096 new_pinctl);
1097
1098 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001099 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001100 AC_VERB_SET_PIN_WIDGET_CONTROL,
1101 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001102
Stephen Warren384a48d2011-06-01 11:14:21 -06001103 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001104 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001105 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1106 return -EINVAL;
1107 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001108
Stephen Warren384a48d2011-06-01 11:14:21 -06001109 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001110 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001111}
1112
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001113static int hdmi_choose_cvt(struct hda_codec *codec,
1114 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001115{
1116 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001117 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001118 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001119 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001120
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001121 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001122
Stephen Warren384a48d2011-06-01 11:14:21 -06001123 /* Dynamically assign converter to stream */
1124 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001125 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001126
1127 /* Must not already be assigned */
1128 if (per_cvt->assigned)
1129 continue;
1130 /* Must be in pin's mux's list of converters */
1131 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1132 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1133 break;
1134 /* Not in mux list */
1135 if (mux_idx == per_pin->num_mux_nids)
1136 continue;
1137 break;
1138 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001139
Stephen Warren384a48d2011-06-01 11:14:21 -06001140 /* No free converters */
1141 if (cvt_idx == spec->num_cvts)
1142 return -ENODEV;
1143
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001144 if (cvt_id)
1145 *cvt_id = cvt_idx;
1146 if (mux_id)
1147 *mux_id = mux_idx;
1148
1149 return 0;
1150}
1151
1152static void haswell_config_cvts(struct hda_codec *codec,
1153 int pin_id, int mux_id)
1154{
1155 struct hdmi_spec *spec = codec->spec;
1156 struct hdmi_spec_per_pin *per_pin;
1157 int pin_idx, mux_idx;
1158 int curr;
1159 int err;
1160
1161 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1162 per_pin = get_pin(spec, pin_idx);
1163
1164 if (pin_idx == pin_id)
1165 continue;
1166
1167 curr = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1168 AC_VERB_GET_CONNECT_SEL, 0);
1169
1170 /* Choose another unused converter */
1171 if (curr == mux_id) {
1172 err = hdmi_choose_cvt(codec, pin_idx, NULL, &mux_idx);
1173 if (err < 0)
1174 return;
1175 snd_printdd("HDMI: choose converter %d for pin %d\n", mux_idx, pin_idx);
1176 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1177 AC_VERB_SET_CONNECT_SEL,
1178 mux_idx);
1179 }
1180 }
1181}
1182
1183/*
1184 * HDA PCM callbacks
1185 */
1186static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1187 struct hda_codec *codec,
1188 struct snd_pcm_substream *substream)
1189{
1190 struct hdmi_spec *spec = codec->spec;
1191 struct snd_pcm_runtime *runtime = substream->runtime;
1192 int pin_idx, cvt_idx, mux_idx = 0;
1193 struct hdmi_spec_per_pin *per_pin;
1194 struct hdmi_eld *eld;
1195 struct hdmi_spec_per_cvt *per_cvt = NULL;
1196 int err;
1197
1198 /* Validate hinfo */
1199 pin_idx = hinfo_to_pin_index(spec, hinfo);
1200 if (snd_BUG_ON(pin_idx < 0))
1201 return -EINVAL;
1202 per_pin = get_pin(spec, pin_idx);
1203 eld = &per_pin->sink_eld;
1204
1205 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1206 if (err < 0)
1207 return err;
1208
1209 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001210 /* Claim converter */
1211 per_cvt->assigned = 1;
1212 hinfo->nid = per_cvt->cvt_nid;
1213
Takashi Iwaibddee962013-06-18 16:14:22 +02001214 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001215 AC_VERB_SET_CONNECT_SEL,
1216 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001217
1218 /* configure unused pins to choose other converters */
1219 if (codec->vendor_id == 0x80862807)
1220 haswell_config_cvts(codec, pin_idx, mux_idx);
1221
Stephen Warren384a48d2011-06-01 11:14:21 -06001222 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001223
Stephen Warren2def8172011-06-01 11:14:20 -06001224 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001225 hinfo->channels_min = per_cvt->channels_min;
1226 hinfo->channels_max = per_cvt->channels_max;
1227 hinfo->rates = per_cvt->rates;
1228 hinfo->formats = per_cvt->formats;
1229 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001230
Stephen Warren384a48d2011-06-01 11:14:21 -06001231 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001232 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001233 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001234 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001235 !hinfo->rates || !hinfo->formats) {
1236 per_cvt->assigned = 0;
1237 hinfo->nid = 0;
1238 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001239 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001240 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001241 }
Stephen Warren2def8172011-06-01 11:14:20 -06001242
1243 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001244 runtime->hw.channels_min = hinfo->channels_min;
1245 runtime->hw.channels_max = hinfo->channels_max;
1246 runtime->hw.formats = hinfo->formats;
1247 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001248
1249 snd_pcm_hw_constraint_step(substream->runtime, 0,
1250 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001251 return 0;
1252}
1253
1254/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001255 * HDA/HDMI auto parsing
1256 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001257static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001258{
1259 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001260 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001261 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001262
1263 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1264 snd_printk(KERN_WARNING
1265 "HDMI: pin %d wcaps %#x "
1266 "does not support connection list\n",
1267 pin_nid, get_wcaps(codec, pin_nid));
1268 return -EINVAL;
1269 }
1270
Stephen Warren384a48d2011-06-01 11:14:21 -06001271 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1272 per_pin->mux_nids,
1273 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001274
1275 return 0;
1276}
1277
Wu Fengguangc6e84532011-11-18 16:59:32 -06001278static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001279{
Wu Fengguang744626d2011-11-16 16:29:47 +08001280 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001281 struct hdmi_spec *spec = codec->spec;
1282 struct hdmi_eld *eld = &spec->temp_eld;
1283 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001284 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001285 /*
1286 * Always execute a GetPinSense verb here, even when called from
1287 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1288 * response's PD bit is not the real PD value, but indicates that
1289 * the real PD value changed. An older version of the HD-audio
1290 * specification worked this way. Hence, we just ignore the data in
1291 * the unsolicited response to avoid custom WARs.
1292 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001293 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001294 bool update_eld = false;
1295 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001296
David Henningsson4bd038f2013-02-19 16:11:25 +01001297 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1298 if (pin_eld->monitor_present)
1299 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1300 else
1301 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001302
Fengguang Wufae3d882012-04-10 17:00:35 +08001303 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001304 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001305 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001306
David Henningsson4bd038f2013-02-19 16:11:25 +01001307 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001308 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1309 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001310 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001311 else {
1312 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1313 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1314 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001315 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001316 }
1317
David Henningsson4bd038f2013-02-19 16:11:25 +01001318 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001319 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001320 update_eld = true;
David Henningsson1613d6b2013-02-19 16:11:24 +01001321 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001322 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001323 queue_delayed_work(codec->bus->workq,
1324 &per_pin->work,
1325 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001326 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001327 }
1328 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001329
1330 mutex_lock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001331 if (pin_eld->eld_valid && !eld->eld_valid) {
David Henningsson4bd038f2013-02-19 16:11:25 +01001332 update_eld = true;
David Henningsson92c69e72013-02-19 16:11:26 +01001333 eld_changed = true;
1334 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001335 if (update_eld) {
1336 pin_eld->eld_valid = eld->eld_valid;
David Henningsson92c69e72013-02-19 16:11:26 +01001337 eld_changed = pin_eld->eld_size != eld->eld_size ||
1338 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
David Henningsson4bd038f2013-02-19 16:11:25 +01001339 eld->eld_size) != 0;
1340 if (eld_changed)
1341 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1342 eld->eld_size);
1343 pin_eld->eld_size = eld->eld_size;
1344 pin_eld->info = eld->info;
1345 }
1346 mutex_unlock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001347
1348 if (eld_changed)
1349 snd_ctl_notify(codec->bus->card,
1350 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1351 &per_pin->eld_ctl->id);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001352}
1353
Wu Fengguang744626d2011-11-16 16:29:47 +08001354static void hdmi_repoll_eld(struct work_struct *work)
1355{
1356 struct hdmi_spec_per_pin *per_pin =
1357 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1358
Wu Fengguangc6e84532011-11-18 16:59:32 -06001359 if (per_pin->repoll_count++ > 6)
1360 per_pin->repoll_count = 0;
1361
1362 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001363}
1364
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001365static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1366 hda_nid_t nid);
1367
Wu Fengguang079d88c2010-03-08 10:44:23 +08001368static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1369{
1370 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001371 unsigned int caps, config;
1372 int pin_idx;
1373 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001374 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001375
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001376 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001377 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1378 return 0;
1379
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001380 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001381 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1382 return 0;
1383
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001384 if (codec->vendor_id == 0x80862807)
1385 intel_haswell_fixup_connect_list(codec, pin_nid);
1386
Stephen Warren384a48d2011-06-01 11:14:21 -06001387 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001388 per_pin = snd_array_new(&spec->pins);
1389 if (!per_pin)
1390 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001391
1392 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001393 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001394
Stephen Warren384a48d2011-06-01 11:14:21 -06001395 err = hdmi_read_pin_conn(codec, pin_idx);
1396 if (err < 0)
1397 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001398
Wu Fengguang079d88c2010-03-08 10:44:23 +08001399 spec->num_pins++;
1400
Stephen Warren384a48d2011-06-01 11:14:21 -06001401 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001402}
1403
Stephen Warren384a48d2011-06-01 11:14:21 -06001404static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001405{
1406 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001407 struct hdmi_spec_per_cvt *per_cvt;
1408 unsigned int chans;
1409 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001410
Stephen Warren384a48d2011-06-01 11:14:21 -06001411 chans = get_wcaps(codec, cvt_nid);
1412 chans = get_wcaps_channels(chans);
1413
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001414 per_cvt = snd_array_new(&spec->cvts);
1415 if (!per_cvt)
1416 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001417
1418 per_cvt->cvt_nid = cvt_nid;
1419 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001420 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001421 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001422 if (chans > spec->channels_max)
1423 spec->channels_max = chans;
1424 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001425
1426 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1427 &per_cvt->rates,
1428 &per_cvt->formats,
1429 &per_cvt->maxbps);
1430 if (err < 0)
1431 return err;
1432
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001433 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1434 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1435 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001436
1437 return 0;
1438}
1439
1440static int hdmi_parse_codec(struct hda_codec *codec)
1441{
1442 hda_nid_t nid;
1443 int i, nodes;
1444
1445 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1446 if (!nid || nodes < 0) {
1447 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1448 return -EINVAL;
1449 }
1450
1451 for (i = 0; i < nodes; i++, nid++) {
1452 unsigned int caps;
1453 unsigned int type;
1454
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001455 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001456 type = get_wcaps_type(caps);
1457
1458 if (!(caps & AC_WCAP_DIGITAL))
1459 continue;
1460
1461 switch (type) {
1462 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001463 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001464 break;
1465 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001466 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001467 break;
1468 }
1469 }
1470
David Henningssonc9adeef2012-11-07 09:22:33 +01001471#ifdef CONFIG_PM
1472 /* We're seeing some problems with unsolicited hot plug events on
1473 * PantherPoint after S3, if this is not enabled */
1474 if (codec->vendor_id == 0x80862806)
1475 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001476 /*
1477 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1478 * can be lost and presence sense verb will become inaccurate if the
1479 * HDA link is powered off at hot plug or hw initialization time.
1480 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001481 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001482 AC_PWRST_EPSS))
1483 codec->bus->power_keep_link_on = 1;
1484#endif
1485
1486 return 0;
1487}
1488
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001489/*
1490 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001491static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1492{
1493 struct hda_spdif_out *spdif;
1494 bool non_pcm;
1495
1496 mutex_lock(&codec->spdif_mutex);
1497 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1498 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1499 mutex_unlock(&codec->spdif_mutex);
1500 return non_pcm;
1501}
1502
1503
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001504/*
1505 * HDMI callbacks
1506 */
1507
1508static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1509 struct hda_codec *codec,
1510 unsigned int stream_tag,
1511 unsigned int format,
1512 struct snd_pcm_substream *substream)
1513{
Stephen Warren384a48d2011-06-01 11:14:21 -06001514 hda_nid_t cvt_nid = hinfo->nid;
1515 struct hdmi_spec *spec = codec->spec;
1516 int pin_idx = hinfo_to_pin_index(spec, hinfo);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001517 hda_nid_t pin_nid = get_pin(spec, pin_idx)->pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001518 bool non_pcm;
1519
1520 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001521
Stephen Warren384a48d2011-06-01 11:14:21 -06001522 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001523
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001524 hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
Stephen Warren384a48d2011-06-01 11:14:21 -06001525
1526 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001527}
1528
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001529static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1530 struct hda_codec *codec,
1531 struct snd_pcm_substream *substream)
1532{
1533 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1534 return 0;
1535}
1536
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001537static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1538 struct hda_codec *codec,
1539 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001540{
1541 struct hdmi_spec *spec = codec->spec;
1542 int cvt_idx, pin_idx;
1543 struct hdmi_spec_per_cvt *per_cvt;
1544 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001545
Stephen Warren384a48d2011-06-01 11:14:21 -06001546 if (hinfo->nid) {
1547 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1548 if (snd_BUG_ON(cvt_idx < 0))
1549 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001550 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001551
1552 snd_BUG_ON(!per_cvt->assigned);
1553 per_cvt->assigned = 0;
1554 hinfo->nid = 0;
1555
1556 pin_idx = hinfo_to_pin_index(spec, hinfo);
1557 if (snd_BUG_ON(pin_idx < 0))
1558 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001559 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001560
Stephen Warren384a48d2011-06-01 11:14:21 -06001561 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001562 per_pin->chmap_set = false;
1563 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Stephen Warren384a48d2011-06-01 11:14:21 -06001564 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001565
Stephen Warren384a48d2011-06-01 11:14:21 -06001566 return 0;
1567}
1568
1569static const struct hda_pcm_ops generic_ops = {
1570 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001571 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001572 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001573 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001574};
1575
Takashi Iwaid45e6882012-07-31 11:36:00 +02001576/*
1577 * ALSA API channel-map control callbacks
1578 */
1579static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_info *uinfo)
1581{
1582 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1583 struct hda_codec *codec = info->private_data;
1584 struct hdmi_spec *spec = codec->spec;
1585 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1586 uinfo->count = spec->channels_max;
1587 uinfo->value.integer.min = 0;
1588 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1589 return 0;
1590}
1591
1592static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1593 unsigned int size, unsigned int __user *tlv)
1594{
1595 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1596 struct hda_codec *codec = info->private_data;
1597 struct hdmi_spec *spec = codec->spec;
1598 const unsigned int valid_mask =
1599 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1600 unsigned int __user *dst;
1601 int chs, count = 0;
1602
1603 if (size < 8)
1604 return -ENOMEM;
1605 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1606 return -EFAULT;
1607 size -= 8;
1608 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001609 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001610 int i, c;
1611 struct cea_channel_speaker_allocation *cap;
1612 cap = channel_allocations;
1613 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1614 int chs_bytes = chs * 4;
1615 if (cap->channels != chs)
1616 continue;
1617 if (cap->spk_mask & ~valid_mask)
1618 continue;
1619 if (size < 8)
1620 return -ENOMEM;
1621 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1622 put_user(chs_bytes, dst + 1))
1623 return -EFAULT;
1624 dst += 2;
1625 size -= 8;
1626 count += 8;
1627 if (size < chs_bytes)
1628 return -ENOMEM;
1629 size -= chs_bytes;
1630 count += chs_bytes;
1631 for (c = 7; c >= 0; c--) {
1632 int spk = cap->speakers[c];
1633 if (!spk)
1634 continue;
1635 if (put_user(spk_to_chmap(spk), dst))
1636 return -EFAULT;
1637 dst++;
1638 }
1639 }
1640 }
1641 if (put_user(count, tlv + 1))
1642 return -EFAULT;
1643 return 0;
1644}
1645
1646static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol)
1648{
1649 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1650 struct hda_codec *codec = info->private_data;
1651 struct hdmi_spec *spec = codec->spec;
1652 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001653 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001654 int i;
1655
1656 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1657 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1658 return 0;
1659}
1660
1661static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1662 struct snd_ctl_elem_value *ucontrol)
1663{
1664 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1665 struct hda_codec *codec = info->private_data;
1666 struct hdmi_spec *spec = codec->spec;
1667 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001668 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001669 unsigned int ctl_idx;
1670 struct snd_pcm_substream *substream;
1671 unsigned char chmap[8];
1672 int i, ca, prepared = 0;
1673
1674 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1675 substream = snd_pcm_chmap_substream(info, ctl_idx);
1676 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001677 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001678 switch (substream->runtime->status->state) {
1679 case SNDRV_PCM_STATE_OPEN:
1680 case SNDRV_PCM_STATE_SETUP:
1681 break;
1682 case SNDRV_PCM_STATE_PREPARED:
1683 prepared = 1;
1684 break;
1685 default:
1686 return -EBUSY;
1687 }
1688 memset(chmap, 0, sizeof(chmap));
1689 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1690 chmap[i] = ucontrol->value.integer.value[i];
1691 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1692 return 0;
1693 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1694 if (ca < 0)
1695 return -EINVAL;
1696 per_pin->chmap_set = true;
1697 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1698 if (prepared)
1699 hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
1700 substream);
1701
1702 return 0;
1703}
1704
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001705static int generic_hdmi_build_pcms(struct hda_codec *codec)
1706{
1707 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001708 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001709
Stephen Warren384a48d2011-06-01 11:14:21 -06001710 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1711 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001712 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001713 struct hdmi_spec_per_pin *per_pin;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001714
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001715 per_pin = get_pin(spec, pin_idx);
1716 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1717 info = snd_array_new(&spec->pcm_rec);
1718 if (!info)
1719 return -ENOMEM;
1720 info->name = per_pin->pcm_name;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001721 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001722 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001723
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001724 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001725 pstr->substreams = 1;
1726 pstr->ops = generic_ops;
1727 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001728 }
1729
Stephen Warren384a48d2011-06-01 11:14:21 -06001730 codec->num_pcms = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001731 codec->pcm_info = spec->pcm_rec.list;
Stephen Warren384a48d2011-06-01 11:14:21 -06001732
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001733 return 0;
1734}
1735
David Henningsson0b6c49b2011-08-23 16:56:03 +02001736static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1737{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001738 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001739 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001740 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1741 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001742
Takashi Iwai31ef2252011-12-01 17:41:36 +01001743 if (pcmdev > 0)
1744 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson30efd8d2013-02-22 10:16:28 +01001745 if (!is_jack_detectable(codec, per_pin->pin_nid))
1746 strncat(hdmi_str, " Phantom",
1747 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001748
Takashi Iwai31ef2252011-12-01 17:41:36 +01001749 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001750}
1751
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001752static int generic_hdmi_build_controls(struct hda_codec *codec)
1753{
1754 struct hdmi_spec *spec = codec->spec;
1755 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001756 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001757
Stephen Warren384a48d2011-06-01 11:14:21 -06001758 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001759 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001760
1761 err = generic_hdmi_build_jack(codec, pin_idx);
1762 if (err < 0)
1763 return err;
1764
Takashi Iwaidcda5802012-10-12 17:24:51 +02001765 err = snd_hda_create_dig_out_ctls(codec,
1766 per_pin->pin_nid,
1767 per_pin->mux_nids[0],
1768 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001769 if (err < 0)
1770 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001771 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001772
1773 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001774 err = hdmi_create_eld_ctl(codec, pin_idx,
1775 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001776
1777 if (err < 0)
1778 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001779
Takashi Iwai82b1d732011-12-20 15:53:07 +01001780 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001781 }
1782
Takashi Iwaid45e6882012-07-31 11:36:00 +02001783 /* add channel maps */
1784 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1785 struct snd_pcm_chmap *chmap;
1786 struct snd_kcontrol *kctl;
1787 int i;
1788 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1789 SNDRV_PCM_STREAM_PLAYBACK,
1790 NULL, 0, pin_idx, &chmap);
1791 if (err < 0)
1792 return err;
1793 /* override handlers */
1794 chmap->private_data = codec;
1795 kctl = chmap->kctl;
1796 for (i = 0; i < kctl->count; i++)
1797 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1798 kctl->info = hdmi_chmap_ctl_info;
1799 kctl->get = hdmi_chmap_ctl_get;
1800 kctl->put = hdmi_chmap_ctl_put;
1801 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1802 }
1803
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001804 return 0;
1805}
1806
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001807static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1808{
1809 struct hdmi_spec *spec = codec->spec;
1810 int pin_idx;
1811
1812 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001813 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001814 struct hdmi_eld *eld = &per_pin->sink_eld;
1815
1816 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001817 mutex_init(&eld->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001818 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1819 snd_hda_eld_proc_new(codec, eld, pin_idx);
1820 }
1821 return 0;
1822}
1823
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001824static int generic_hdmi_init(struct hda_codec *codec)
1825{
1826 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001827 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001828
Stephen Warren384a48d2011-06-01 11:14:21 -06001829 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001830 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001831 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001832
1833 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001834 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001835 }
1836 return 0;
1837}
1838
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001839static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1840{
1841 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1842 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1843 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1844}
1845
1846static void hdmi_array_free(struct hdmi_spec *spec)
1847{
1848 snd_array_free(&spec->pins);
1849 snd_array_free(&spec->cvts);
1850 snd_array_free(&spec->pcm_rec);
1851}
1852
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001853static void generic_hdmi_free(struct hda_codec *codec)
1854{
1855 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001856 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001857
Stephen Warren384a48d2011-06-01 11:14:21 -06001858 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001859 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001860 struct hdmi_eld *eld = &per_pin->sink_eld;
1861
Wu Fengguang744626d2011-11-16 16:29:47 +08001862 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001863 snd_hda_eld_proc_free(codec, eld);
1864 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001865
Wu Fengguang744626d2011-11-16 16:29:47 +08001866 flush_workqueue(codec->bus->workq);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001867 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001868 kfree(spec);
1869}
1870
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001871static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001872 .init = generic_hdmi_init,
1873 .free = generic_hdmi_free,
1874 .build_pcms = generic_hdmi_build_pcms,
1875 .build_controls = generic_hdmi_build_controls,
1876 .unsol_event = hdmi_unsol_event,
1877};
1878
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001879
1880static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1881 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001882{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001883 struct hdmi_spec *spec = codec->spec;
1884 hda_nid_t conns[4];
1885 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001886
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001887 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1888 if (nconns == spec->num_cvts &&
1889 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001890 return;
1891
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001892 /* override pins connection list */
1893 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001894 nconns = max(spec->num_cvts, 4);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001895 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001896}
1897
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001898#define INTEL_VENDOR_NID 0x08
1899#define INTEL_GET_VENDOR_VERB 0xf81
1900#define INTEL_SET_VENDOR_VERB 0x781
1901#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1902#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1903
1904static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02001905 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001906{
1907 unsigned int vendor_param;
1908
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001909 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1910 INTEL_GET_VENDOR_VERB, 0);
1911 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1912 return;
1913
1914 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1915 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1916 INTEL_SET_VENDOR_VERB, vendor_param);
1917 if (vendor_param == -1)
1918 return;
1919
Takashi Iwai17df3f52013-05-08 08:09:34 +02001920 if (update_tree)
1921 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001922}
1923
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001924static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1925{
1926 unsigned int vendor_param;
1927
1928 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1929 INTEL_GET_VENDOR_VERB, 0);
1930 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1931 return;
1932
1933 /* enable DP1.2 mode */
1934 vendor_param |= INTEL_EN_DP12;
1935 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1936 INTEL_SET_VENDOR_VERB, vendor_param);
1937}
1938
Takashi Iwai17df3f52013-05-08 08:09:34 +02001939/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
1940 * Otherwise you may get severe h/w communication errors.
1941 */
1942static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1943 unsigned int power_state)
1944{
1945 if (power_state == AC_PWRST_D0) {
1946 intel_haswell_enable_all_pins(codec, false);
1947 intel_haswell_fixup_enable_dp12(codec);
1948 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001949
Takashi Iwai17df3f52013-05-08 08:09:34 +02001950 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
1951 snd_hda_codec_set_power_to_all(codec, fg, power_state);
1952}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001953
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001954static int patch_generic_hdmi(struct hda_codec *codec)
1955{
1956 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001957
1958 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1959 if (spec == NULL)
1960 return -ENOMEM;
1961
1962 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001963 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001964
Takashi Iwai17df3f52013-05-08 08:09:34 +02001965 if (codec->vendor_id == 0x80862807) {
1966 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001967 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02001968 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001969
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001970 if (hdmi_parse_codec(codec) < 0) {
1971 codec->spec = NULL;
1972 kfree(spec);
1973 return -EINVAL;
1974 }
1975 codec->patch_ops = generic_hdmi_patch_ops;
Takashi Iwai17df3f52013-05-08 08:09:34 +02001976 if (codec->vendor_id == 0x80862807)
1977 codec->patch_ops.set_power_state = haswell_set_power_state;
1978
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001979 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001980
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001981 init_channel_allocations();
1982
1983 return 0;
1984}
1985
1986/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06001987 * Shared non-generic implementations
1988 */
1989
1990static int simple_playback_build_pcms(struct hda_codec *codec)
1991{
1992 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001993 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001994 unsigned int chans;
1995 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001996 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001997
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001998 per_cvt = get_cvt(spec, 0);
1999 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002000 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002001
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002002 info = snd_array_new(&spec->pcm_rec);
2003 if (!info)
2004 return -ENOMEM;
2005 info->name = get_pin(spec, 0)->pcm_name;
2006 sprintf(info->name, "HDMI 0");
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002007 info->pcm_type = HDA_PCM_TYPE_HDMI;
2008 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2009 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002010 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002011 if (pstr->channels_max <= 2 && chans && chans <= 16)
2012 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002013
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002014 codec->num_pcms = 1;
2015 codec->pcm_info = info;
2016
Stephen Warren3aaf8982011-06-01 11:14:19 -06002017 return 0;
2018}
2019
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002020/* unsolicited event for jack sensing */
2021static void simple_hdmi_unsol_event(struct hda_codec *codec,
2022 unsigned int res)
2023{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002024 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002025 snd_hda_jack_report_sync(codec);
2026}
2027
2028/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2029 * as long as spec->pins[] is set correctly
2030 */
2031#define simple_hdmi_build_jack generic_hdmi_build_jack
2032
Stephen Warren3aaf8982011-06-01 11:14:19 -06002033static int simple_playback_build_controls(struct hda_codec *codec)
2034{
2035 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002036 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002037 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002038
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002039 per_cvt = get_cvt(spec, 0);
2040 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
2041 per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002042 if (err < 0)
2043 return err;
2044 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002045}
2046
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002047static int simple_playback_init(struct hda_codec *codec)
2048{
2049 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002050 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2051 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002052
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002053 snd_hda_codec_write(codec, pin, 0,
2054 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2055 /* some codecs require to unmute the pin */
2056 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2057 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2058 AMP_OUT_UNMUTE);
2059 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002060 return 0;
2061}
2062
Stephen Warren3aaf8982011-06-01 11:14:19 -06002063static void simple_playback_free(struct hda_codec *codec)
2064{
2065 struct hdmi_spec *spec = codec->spec;
2066
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002067 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002068 kfree(spec);
2069}
2070
2071/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002072 * Nvidia specific implementations
2073 */
2074
2075#define Nv_VERB_SET_Channel_Allocation 0xF79
2076#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2077#define Nv_VERB_SET_Audio_Protection_On 0xF98
2078#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2079
2080#define nvhdmi_master_con_nid_7x 0x04
2081#define nvhdmi_master_pin_nid_7x 0x05
2082
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002083static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002084 /*front, rear, clfe, rear_surr */
2085 0x6, 0x8, 0xa, 0xc,
2086};
2087
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002088static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2089 /* set audio protect on */
2090 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2091 /* enable digital output on pin widget */
2092 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2093 {} /* terminator */
2094};
2095
2096static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002097 /* set audio protect on */
2098 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2099 /* enable digital output on pin widget */
2100 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2101 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2102 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2103 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2104 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2105 {} /* terminator */
2106};
2107
2108#ifdef LIMITED_RATE_FMT_SUPPORT
2109/* support only the safe format and rate */
2110#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2111#define SUPPORTED_MAXBPS 16
2112#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2113#else
2114/* support all rates and formats */
2115#define SUPPORTED_RATES \
2116 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2117 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2118 SNDRV_PCM_RATE_192000)
2119#define SUPPORTED_MAXBPS 24
2120#define SUPPORTED_FORMATS \
2121 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2122#endif
2123
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002124static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002125{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002126 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2127 return 0;
2128}
2129
2130static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2131{
2132 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002133 return 0;
2134}
2135
Nitin Daga393004b2011-01-10 21:49:31 +05302136static unsigned int channels_2_6_8[] = {
2137 2, 6, 8
2138};
2139
2140static unsigned int channels_2_8[] = {
2141 2, 8
2142};
2143
2144static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2145 .count = ARRAY_SIZE(channels_2_6_8),
2146 .list = channels_2_6_8,
2147 .mask = 0,
2148};
2149
2150static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2151 .count = ARRAY_SIZE(channels_2_8),
2152 .list = channels_2_8,
2153 .mask = 0,
2154};
2155
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002156static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2157 struct hda_codec *codec,
2158 struct snd_pcm_substream *substream)
2159{
2160 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302161 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2162
2163 switch (codec->preset->id) {
2164 case 0x10de0002:
2165 case 0x10de0003:
2166 case 0x10de0005:
2167 case 0x10de0006:
2168 hw_constraints_channels = &hw_constraints_2_8_channels;
2169 break;
2170 case 0x10de0007:
2171 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2172 break;
2173 default:
2174 break;
2175 }
2176
2177 if (hw_constraints_channels != NULL) {
2178 snd_pcm_hw_constraint_list(substream->runtime, 0,
2179 SNDRV_PCM_HW_PARAM_CHANNELS,
2180 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002181 } else {
2182 snd_pcm_hw_constraint_step(substream->runtime, 0,
2183 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302184 }
2185
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002186 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2187}
2188
2189static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2190 struct hda_codec *codec,
2191 struct snd_pcm_substream *substream)
2192{
2193 struct hdmi_spec *spec = codec->spec;
2194 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2195}
2196
2197static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2198 struct hda_codec *codec,
2199 unsigned int stream_tag,
2200 unsigned int format,
2201 struct snd_pcm_substream *substream)
2202{
2203 struct hdmi_spec *spec = codec->spec;
2204 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2205 stream_tag, format, substream);
2206}
2207
Takashi Iwaid0b12522012-06-15 14:34:42 +02002208static const struct hda_pcm_stream simple_pcm_playback = {
2209 .substreams = 1,
2210 .channels_min = 2,
2211 .channels_max = 2,
2212 .ops = {
2213 .open = simple_playback_pcm_open,
2214 .close = simple_playback_pcm_close,
2215 .prepare = simple_playback_pcm_prepare
2216 },
2217};
2218
2219static const struct hda_codec_ops simple_hdmi_patch_ops = {
2220 .build_controls = simple_playback_build_controls,
2221 .build_pcms = simple_playback_build_pcms,
2222 .init = simple_playback_init,
2223 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002224 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002225};
2226
2227static int patch_simple_hdmi(struct hda_codec *codec,
2228 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2229{
2230 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002231 struct hdmi_spec_per_cvt *per_cvt;
2232 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002233
2234 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2235 if (!spec)
2236 return -ENOMEM;
2237
2238 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002239 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002240
2241 spec->multiout.num_dacs = 0; /* no analog */
2242 spec->multiout.max_channels = 2;
2243 spec->multiout.dig_out_nid = cvt_nid;
2244 spec->num_cvts = 1;
2245 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002246 per_pin = snd_array_new(&spec->pins);
2247 per_cvt = snd_array_new(&spec->cvts);
2248 if (!per_pin || !per_cvt) {
2249 simple_playback_free(codec);
2250 return -ENOMEM;
2251 }
2252 per_cvt->cvt_nid = cvt_nid;
2253 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002254 spec->pcm_playback = simple_pcm_playback;
2255
2256 codec->patch_ops = simple_hdmi_patch_ops;
2257
2258 return 0;
2259}
2260
Aaron Plattner1f348522011-04-06 17:19:04 -07002261static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2262 int channels)
2263{
2264 unsigned int chanmask;
2265 int chan = channels ? (channels - 1) : 1;
2266
2267 switch (channels) {
2268 default:
2269 case 0:
2270 case 2:
2271 chanmask = 0x00;
2272 break;
2273 case 4:
2274 chanmask = 0x08;
2275 break;
2276 case 6:
2277 chanmask = 0x0b;
2278 break;
2279 case 8:
2280 chanmask = 0x13;
2281 break;
2282 }
2283
2284 /* Set the audio infoframe channel allocation and checksum fields. The
2285 * channel count is computed implicitly by the hardware. */
2286 snd_hda_codec_write(codec, 0x1, 0,
2287 Nv_VERB_SET_Channel_Allocation, chanmask);
2288
2289 snd_hda_codec_write(codec, 0x1, 0,
2290 Nv_VERB_SET_Info_Frame_Checksum,
2291 (0x71 - chan - chanmask));
2292}
2293
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002294static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2295 struct hda_codec *codec,
2296 struct snd_pcm_substream *substream)
2297{
2298 struct hdmi_spec *spec = codec->spec;
2299 int i;
2300
2301 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2302 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2303 for (i = 0; i < 4; i++) {
2304 /* set the stream id */
2305 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2306 AC_VERB_SET_CHANNEL_STREAMID, 0);
2307 /* set the stream format */
2308 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2309 AC_VERB_SET_STREAM_FORMAT, 0);
2310 }
2311
Aaron Plattner1f348522011-04-06 17:19:04 -07002312 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2313 * streams are disabled. */
2314 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2315
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002316 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2317}
2318
2319static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2320 struct hda_codec *codec,
2321 unsigned int stream_tag,
2322 unsigned int format,
2323 struct snd_pcm_substream *substream)
2324{
2325 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002326 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002327 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002328 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002329 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002330 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002331
2332 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002333 per_cvt = get_cvt(spec, 0);
2334 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002335
2336 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002337
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002338 dataDCC2 = 0x2;
2339
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002340 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002341 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002342 snd_hda_codec_write(codec,
2343 nvhdmi_master_con_nid_7x,
2344 0,
2345 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002346 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002347
2348 /* set the stream id */
2349 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2350 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2351
2352 /* set the stream format */
2353 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2354 AC_VERB_SET_STREAM_FORMAT, format);
2355
2356 /* turn on again (if needed) */
2357 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002358 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002359 snd_hda_codec_write(codec,
2360 nvhdmi_master_con_nid_7x,
2361 0,
2362 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002363 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002364 snd_hda_codec_write(codec,
2365 nvhdmi_master_con_nid_7x,
2366 0,
2367 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2368 }
2369
2370 for (i = 0; i < 4; i++) {
2371 if (chs == 2)
2372 channel_id = 0;
2373 else
2374 channel_id = i * 2;
2375
2376 /* turn off SPDIF once;
2377 *otherwise the IEC958 bits won't be updated
2378 */
2379 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002380 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002381 snd_hda_codec_write(codec,
2382 nvhdmi_con_nids_7x[i],
2383 0,
2384 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002385 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002386 /* set the stream id */
2387 snd_hda_codec_write(codec,
2388 nvhdmi_con_nids_7x[i],
2389 0,
2390 AC_VERB_SET_CHANNEL_STREAMID,
2391 (stream_tag << 4) | channel_id);
2392 /* set the stream format */
2393 snd_hda_codec_write(codec,
2394 nvhdmi_con_nids_7x[i],
2395 0,
2396 AC_VERB_SET_STREAM_FORMAT,
2397 format);
2398 /* turn on again (if needed) */
2399 /* enable and set the channel status audio/data flag */
2400 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002401 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002402 snd_hda_codec_write(codec,
2403 nvhdmi_con_nids_7x[i],
2404 0,
2405 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002406 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002407 snd_hda_codec_write(codec,
2408 nvhdmi_con_nids_7x[i],
2409 0,
2410 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2411 }
2412 }
2413
Aaron Plattner1f348522011-04-06 17:19:04 -07002414 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002415
2416 mutex_unlock(&codec->spdif_mutex);
2417 return 0;
2418}
2419
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002420static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002421 .substreams = 1,
2422 .channels_min = 2,
2423 .channels_max = 8,
2424 .nid = nvhdmi_master_con_nid_7x,
2425 .rates = SUPPORTED_RATES,
2426 .maxbps = SUPPORTED_MAXBPS,
2427 .formats = SUPPORTED_FORMATS,
2428 .ops = {
2429 .open = simple_playback_pcm_open,
2430 .close = nvhdmi_8ch_7x_pcm_close,
2431 .prepare = nvhdmi_8ch_7x_pcm_prepare
2432 },
2433};
2434
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002435static int patch_nvhdmi_2ch(struct hda_codec *codec)
2436{
2437 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002438 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2439 nvhdmi_master_pin_nid_7x);
2440 if (err < 0)
2441 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002442
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002443 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002444 /* override the PCM rates, etc, as the codec doesn't give full list */
2445 spec = codec->spec;
2446 spec->pcm_playback.rates = SUPPORTED_RATES;
2447 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2448 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002449 return 0;
2450}
2451
Takashi Iwai53775b02012-08-01 12:17:41 +02002452static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2453{
2454 struct hdmi_spec *spec = codec->spec;
2455 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002456 if (!err) {
2457 struct hda_pcm *info = get_pcm_rec(spec, 0);
2458 info->own_chmap = true;
2459 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002460 return err;
2461}
2462
2463static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2464{
2465 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002466 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002467 struct snd_pcm_chmap *chmap;
2468 int err;
2469
2470 err = simple_playback_build_controls(codec);
2471 if (err < 0)
2472 return err;
2473
2474 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002475 info = get_pcm_rec(spec, 0);
2476 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002477 SNDRV_PCM_STREAM_PLAYBACK,
2478 snd_pcm_alt_chmaps, 8, 0, &chmap);
2479 if (err < 0)
2480 return err;
2481 switch (codec->preset->id) {
2482 case 0x10de0002:
2483 case 0x10de0003:
2484 case 0x10de0005:
2485 case 0x10de0006:
2486 chmap->channel_mask = (1U << 2) | (1U << 8);
2487 break;
2488 case 0x10de0007:
2489 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2490 }
2491 return 0;
2492}
2493
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002494static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2495{
2496 struct hdmi_spec *spec;
2497 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002498 if (err < 0)
2499 return err;
2500 spec = codec->spec;
2501 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002502 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002503 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002504 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2505 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002506
2507 /* Initialize the audio infoframe channel mask and checksum to something
2508 * valid */
2509 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2510
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002511 return 0;
2512}
2513
2514/*
2515 * ATI-specific implementations
2516 *
2517 * FIXME: we may omit the whole this and use the generic code once after
2518 * it's confirmed to work.
2519 */
2520
2521#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2522#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2523
2524static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2525 struct hda_codec *codec,
2526 unsigned int stream_tag,
2527 unsigned int format,
2528 struct snd_pcm_substream *substream)
2529{
2530 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002531 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002532 int chans = substream->runtime->channels;
2533 int i, err;
2534
2535 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2536 substream);
2537 if (err < 0)
2538 return err;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002539 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06002540 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002541 /* FIXME: XXX */
2542 for (i = 0; i < chans; i++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002543 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002544 AC_VERB_SET_HDMI_CHAN_SLOT,
2545 (i << 4) | i);
2546 }
2547 return 0;
2548}
2549
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002550static int patch_atihdmi(struct hda_codec *codec)
2551{
2552 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002553 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2554 if (err < 0)
2555 return err;
2556 spec = codec->spec;
2557 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002558 return 0;
2559}
2560
Annie Liu3de5ff82012-06-08 19:18:42 +08002561/* VIA HDMI Implementation */
2562#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2563#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2564
Annie Liu3de5ff82012-06-08 19:18:42 +08002565static int patch_via_hdmi(struct hda_codec *codec)
2566{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002567 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002568}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002569
2570/*
2571 * patch entries
2572 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002573static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002574{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2575{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2576{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002577{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002578{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2579{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2580{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2581{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2582{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2583{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2584{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2585{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002586{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2587{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2588{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2589{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2590{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2591{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2592{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2593{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2594{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2595{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2596{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002597/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002598{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2599{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2600{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2601{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2602{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2603{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2604{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2605{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2606{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2607{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002608{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002609{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2610{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002611{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2612{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2613{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2614{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002615{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2616{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2617{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2618{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2619{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2620{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002621{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002622{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002623{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002624{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2625{} /* terminator */
2626};
2627
2628MODULE_ALIAS("snd-hda-codec-id:1002793c");
2629MODULE_ALIAS("snd-hda-codec-id:10027919");
2630MODULE_ALIAS("snd-hda-codec-id:1002791a");
2631MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2632MODULE_ALIAS("snd-hda-codec-id:10951390");
2633MODULE_ALIAS("snd-hda-codec-id:10951392");
2634MODULE_ALIAS("snd-hda-codec-id:10de0002");
2635MODULE_ALIAS("snd-hda-codec-id:10de0003");
2636MODULE_ALIAS("snd-hda-codec-id:10de0005");
2637MODULE_ALIAS("snd-hda-codec-id:10de0006");
2638MODULE_ALIAS("snd-hda-codec-id:10de0007");
2639MODULE_ALIAS("snd-hda-codec-id:10de000a");
2640MODULE_ALIAS("snd-hda-codec-id:10de000b");
2641MODULE_ALIAS("snd-hda-codec-id:10de000c");
2642MODULE_ALIAS("snd-hda-codec-id:10de000d");
2643MODULE_ALIAS("snd-hda-codec-id:10de0010");
2644MODULE_ALIAS("snd-hda-codec-id:10de0011");
2645MODULE_ALIAS("snd-hda-codec-id:10de0012");
2646MODULE_ALIAS("snd-hda-codec-id:10de0013");
2647MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002648MODULE_ALIAS("snd-hda-codec-id:10de0015");
2649MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002650MODULE_ALIAS("snd-hda-codec-id:10de0018");
2651MODULE_ALIAS("snd-hda-codec-id:10de0019");
2652MODULE_ALIAS("snd-hda-codec-id:10de001a");
2653MODULE_ALIAS("snd-hda-codec-id:10de001b");
2654MODULE_ALIAS("snd-hda-codec-id:10de001c");
2655MODULE_ALIAS("snd-hda-codec-id:10de0040");
2656MODULE_ALIAS("snd-hda-codec-id:10de0041");
2657MODULE_ALIAS("snd-hda-codec-id:10de0042");
2658MODULE_ALIAS("snd-hda-codec-id:10de0043");
2659MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002660MODULE_ALIAS("snd-hda-codec-id:10de0051");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002661MODULE_ALIAS("snd-hda-codec-id:10de0067");
2662MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002663MODULE_ALIAS("snd-hda-codec-id:11069f80");
2664MODULE_ALIAS("snd-hda-codec-id:11069f81");
2665MODULE_ALIAS("snd-hda-codec-id:11069f84");
2666MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002667MODULE_ALIAS("snd-hda-codec-id:17e80047");
2668MODULE_ALIAS("snd-hda-codec-id:80860054");
2669MODULE_ALIAS("snd-hda-codec-id:80862801");
2670MODULE_ALIAS("snd-hda-codec-id:80862802");
2671MODULE_ALIAS("snd-hda-codec-id:80862803");
2672MODULE_ALIAS("snd-hda-codec-id:80862804");
2673MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002674MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002675MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002676MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002677MODULE_ALIAS("snd-hda-codec-id:808629fb");
2678
2679MODULE_LICENSE("GPL");
2680MODULE_DESCRIPTION("HDMI HD-audio codec");
2681MODULE_ALIAS("snd-hda-codec-intelhdmi");
2682MODULE_ALIAS("snd-hda-codec-nvhdmi");
2683MODULE_ALIAS("snd-hda-codec-atihdmi");
2684
2685static struct hda_codec_preset_list intel_list = {
2686 .preset = snd_hda_preset_hdmi,
2687 .owner = THIS_MODULE,
2688};
2689
2690static int __init patch_hdmi_init(void)
2691{
2692 return snd_hda_add_codec_preset(&intel_list);
2693}
2694
2695static void __exit patch_hdmi_exit(void)
2696{
2697 snd_hda_delete_codec_preset(&intel_list);
2698}
2699
2700module_init(patch_hdmi_init)
2701module_exit(patch_hdmi_exit)